Let PAM to provision users on their first login
Damien Miller
djm at mindrot.org
Fri Nov 20 11:55:07 AEDT 2026
On Thu, 19 Nov 2026, Marco Trevisan wrote:
> I do understand that makes things a bit more complex, but the decision of
> ignoring accounts that are not exposed by NSS at the time of the initial
> lookup is something that is not something that matches with the idea of
> NSS.
Ok, but you get that you're basically asking us to reduce the complexity
in your part of the stack by taking on complexity in sshd. I don't think
this is a good deal for myself personally as one of the people who would
have to maintain this extra complexity, but I also believe that it's not
a good deal for our users, because that complexity is in a place where a
little mistake could result in a major security problem.
> > The "monitor: Receive PAM account requests only after authentication"
> > bit is good though and mirrors a hardening that I've wanted to do for
> > a while.
>
> Ok, let me know if there's some work you want me to driver based on that or
> if that's good for landing.
This is what I had in mind. Relative to your patch, it removes the
separate pam_start monitor call and just does it in getpwnamallow().
Also it adds a guard to prevent an unpriv process that fails a
pam_account check can never successfully authenticate again, to
ensure that a compromised unpriv process can't play games where they
keep going.
diff --git a/auth2.c b/auth2.c
index db9e6695f..e1d8af7c6 100644
--- a/auth2.c
+++ b/auth2.c
@@ -315,10 +315,6 @@ input_userauth_request(int type, uint32_t seq, struct ssh *ssh)
mm_audit_event(ssh, SSH_INVALID_USER);
#endif
}
-#ifdef USE_PAM
- if (options.use_pam)
- mm_start_pam(ssh);
-#endif
ssh_packet_set_log_preamble(ssh, "%suser %s",
authctxt->valid ? "authenticating " : "invalid ", user);
setproctitle("%s [net]", authctxt->valid ? user : "unknown");
diff --git a/monitor.c b/monitor.c
index ebcd9c8c8..17159272b 100644
--- a/monitor.c
+++ b/monitor.c
@@ -122,8 +122,6 @@ int mm_answer_term(struct ssh *, int, struct sshbuf *);
int mm_answer_state(struct ssh *, int, struct sshbuf *);
#ifdef USE_PAM
-int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
-int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
@@ -191,8 +189,6 @@ struct mon_table mon_dispatch_proto20[] = {
{MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
{MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
#ifdef USE_PAM
- {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
- {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
{MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
{MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
{MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
@@ -262,11 +258,38 @@ monitor_permit_authentications(int permit)
}
}
+#ifdef USE_PAM
+static int
+monitor_pam_account(struct ssh *ssh, struct monitor *pmonitor)
+{
+ struct sshbuf *m;
+ u_int ret;
+ int r;
+
+ if (!options.use_pam)
+ fatal_f("PAM not enabled");
+ if ((m = sshbuf_new()) == NULL)
+ fatal_f("sshbuf_new failed");
+ mm_request_receive_expect(pmonitor->m_sendfd,
+ MONITOR_REQ_PAM_ACCOUNT, m);
+ if ((ret = do_pam_account()) > INT_MAX)
+ fatal_f("do_pam_account bad return %u", ret);
+ if ((r = sshbuf_put_u32(m, ret)) != 0 ||
+ (r = sshbuf_put_stringb(m, loginmsg)) != 0)
+ fatal_fr(r, "buffer error");
+ sshbuf_reset(loginmsg);
+
+ mm_request_send(pmonitor->m_sendfd, MONITOR_ANS_PAM_ACCOUNT, m);
+ sshbuf_free(m);
+ return (int)ret;
+}
+#endif
+
void
monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
{
struct mon_table *ent;
- int status, authenticated = 0, partial = 0;
+ int status, pam_account_failed = 0, authenticated = 0, partial = 0;
debug3("preauth child monitor started");
@@ -315,6 +338,10 @@ monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
}
if (authenticated) {
+ if (pam_account_failed) {
+ fatal_f("authentication success after "
+ "PAM account failure");
+ }
if (!(ent->flags & MON_AUTHDECIDE))
fatal_f("unexpected authentication from %d",
ent->type);
@@ -322,18 +349,23 @@ monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
!auth_root_allowed(ssh, auth_method))
authenticated = 0;
#ifdef USE_PAM
- /* PAM needs to perform account checks after auth */
- if (options.use_pam && authenticated) {
- struct sshbuf *m;
-
- if ((m = sshbuf_new()) == NULL)
- fatal("%s: sshbuf_new failed",
- __func__);
- mm_request_receive_expect(pmonitor->m_sendfd,
- MONITOR_REQ_PAM_ACCOUNT, m);
- authenticated = mm_answer_pam_account(
- ssh, pmonitor->m_sendfd, m);
- sshbuf_free(m);
+ /*
+ * If PAM is enabled and authentication was successful,
+ * then the unpriv process is next expected to make a
+ * MONITOR_REQ_PAM_ACCOUNT request. This can cancel
+ * the successful authentication and return a reason
+ * to the client.
+ */
+ if (options.use_pam && authenticated &&
+ !monitor_pam_account(ssh, pmonitor)) {
+ pam_account_failed = 1;
+ authenticated = 0;
+ /*
+ * If the account check failed then the unpriv
+ * process is expected to fatal() immediately
+ * It will be prevented from circumventing
+ * this by the pam_account_failed check above.
+ */
}
#endif
}
@@ -874,6 +906,11 @@ mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
goto out;
}
+#ifdef USE_PAM
+ if (options.use_pam)
+ start_pam(ssh);
+#endif
+
allowed = 1;
authctxt->pw = pwent;
authctxt->valid = 1;
@@ -936,7 +973,7 @@ mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
#ifdef USE_PAM
if (options.use_pam)
- monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
+ monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
#endif
return (0);
@@ -1139,42 +1176,6 @@ mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
#endif
#ifdef USE_PAM
-int
-mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
-{
- if (!options.use_pam)
- fatal_f("UsePAM not set, but ended up in %s anyway", __func__);
-
- start_pam(ssh);
-
- monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
- if (options.kbd_interactive_authentication)
- monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
-
- return (0);
-}
-
-int
-mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
-{
- u_int ret;
- int r;
-
- if (!options.use_pam)
- fatal_f("PAM not enabled");
-
- ret = do_pam_account();
-
- if ((r = sshbuf_put_u32(m, ret)) != 0 ||
- (r = sshbuf_put_stringb(m, loginmsg)) != 0)
- fatal_fr(r, "buffer error");
- sshbuf_reset(loginmsg);
-
- mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
-
- return (ret);
-}
-
static void *sshpam_ctxt, *sshpam_authok;
extern KbdintDevice sshpam_device;
diff --git a/monitor.h b/monitor.h
index fe0b00b2e..d5a1c5d24 100644
--- a/monitor.h
+++ b/monitor.h
@@ -57,7 +57,6 @@ enum monitor_reqtype {
MONITOR_REQ_TERM = 50,
MONITOR_REQ_STATE = 51, MONITOR_ANS_STATE = 52,
- MONITOR_REQ_PAM_START = 100,
MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
MONITOR_REQ_PAM_INIT_CTX = 104, MONITOR_ANS_PAM_INIT_CTX = 105,
MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
diff --git a/monitor_wrap.c b/monitor_wrap.c
index 457a6196f..3df3121ed 100644
--- a/monitor_wrap.c
+++ b/monitor_wrap.c
@@ -704,21 +704,6 @@ mm_session_pty_cleanup2(Session *s)
}
#ifdef USE_PAM
-void
-mm_start_pam(struct ssh *ssh)
-{
- struct sshbuf *m;
-
- debug3_f("entering");
- if (!options.use_pam)
- fatal_f("UsePAM=no, but ended up in %s anyway", __func__);
- if ((m = sshbuf_new()) == NULL)
- fatal_f("sshbuf_new failed");
- mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
-
- sshbuf_free(m);
-}
-
u_int
mm_do_pam_account(void)
{
diff --git a/monitor_wrap.h b/monitor_wrap.h
index f43759b21..ae2b05ab8 100644
--- a/monitor_wrap.h
+++ b/monitor_wrap.h
@@ -74,7 +74,6 @@ OM_uint32 mm_ssh_gssapi_checkmic(Gssctxt *, gss_buffer_t, gss_buffer_t);
#endif
#ifdef USE_PAM
-void mm_start_pam(struct ssh *ssh);
u_int mm_do_pam_account(void);
void *mm_sshpam_init_ctx(struct Authctxt *);
int mm_sshpam_query(void *, char **, char **, u_int *, char ***, u_int **);
More information about the openssh-unix-dev
mailing list