From 7c65ef790fae9321e44a270a35e2ccfbbc148699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 18 Nov 2026 22:23:24 +0000 Subject: [PATCH 4/9] sshd: Report deferred invalid users on connection teardown Keep reporting a rejected lookup immediately unless its result has been deferred. A provisioned login should not be recorded as an invalid user just because it had no passwd entry before PAM has processed it. --- auth.c | 23 ++++++++++++++--------- auth.h | 2 ++ auth2.c | 3 ++- monitor.c | 2 ++ sshd-session.c | 3 +++ 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/auth.c b/auth.c index 16d8158f2..2f5bc10e4 100644 --- a/auth.c +++ b/auth.c @@ -456,6 +456,20 @@ check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host, return host_status; } +void +auth_invalid_user(struct ssh *ssh, const char *user) +{ + logit("Invalid user %.100s from %.100s port %d", + user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); +#ifdef CUSTOM_FAILED_LOGIN + record_failed_login(ssh, user, + auth_get_canonical_hostname(ssh, options.use_dns), "ssh"); +#endif +#ifdef SSH_AUDIT_EVENTS + audit_event(ssh, SSH_INVALID_USER); +#endif +} + struct passwd * getpwnamallow(struct ssh *ssh, const char *user, int *notfound) { @@ -505,15 +519,6 @@ getpwnamallow(struct ssh *ssh, const char *user, int *notfound) } if (pw == NULL) { *notfound = 1; - logit("Invalid user %.100s from %.100s port %d", - user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); -#ifdef CUSTOM_FAILED_LOGIN - record_failed_login(ssh, user, - auth_get_canonical_hostname(ssh, options.use_dns), "ssh"); -#endif -#ifdef SSH_AUDIT_EVENTS - audit_event(ssh, SSH_INVALID_USER); -#endif /* SSH_AUDIT_EVENTS */ return (NULL); } if (!allowed_user(ssh, pw)) diff --git a/auth.h b/auth.h index 9b9341dab..c58982d23 100644 --- a/auth.h +++ b/auth.h @@ -57,6 +57,7 @@ struct Authctxt { int authenticated; /* authenticated and alarms cancelled */ int postponed; /* authentication needs another step */ int valid; /* user exists and is allowed to login */ + int deferred; /* PAM may provision this user */ int attempt; int failures; int pk_ok_failures; @@ -191,6 +192,7 @@ int bsdauth_query(void *, char **, char **, u_int *, char ***, u_int **); int bsdauth_respond(void *, u_int, char **); int allowed_user(struct ssh *, struct passwd *); +void auth_invalid_user(struct ssh *, const char *); struct passwd * getpwnamallow(struct ssh *, const char *, int *); char *expand_authorized_keys(const char *, struct passwd *pw); diff --git a/auth2.c b/auth2.c index db9e6695f..8e8ded2f7 100644 --- a/auth2.c +++ b/auth2.c @@ -312,7 +312,8 @@ input_userauth_request(int type, uint32_t seq, struct ssh *ssh) /* Invalid user, fake password information */ authctxt->pw = fakepw(); #ifdef SSH_AUDIT_EVENTS - mm_audit_event(ssh, SSH_INVALID_USER); + if (!authctxt->deferred) + mm_audit_event(ssh, SSH_INVALID_USER); #endif } #ifdef USE_PAM diff --git a/monitor.c b/monitor.c index b3b161734..a5040530a 100644 --- a/monitor.c +++ b/monitor.c @@ -873,6 +873,8 @@ mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m) if (pwent == NULL) { invalid_user = 1; + if (notfound && !authctxt->deferred) + auth_invalid_user(ssh, authctxt->user); if ((r = sshbuf_put_u8(m, 0)) != 0) fatal_fr(r, "assemble fakepw"); authctxt->pw = fakepw(); diff --git a/sshd-session.c b/sshd-session.c index 0e4c1b309..79122f88a 100644 --- a/sshd-session.c +++ b/sshd-session.c @@ -1341,6 +1341,9 @@ cleanup_exit(int i) { if (the_active_state != NULL && the_authctxt != NULL) { do_cleanup(the_active_state, the_authctxt); + if (mm_is_monitor() && !the_authctxt->authenticated && + the_authctxt->deferred && the_authctxt->user != NULL) + auth_invalid_user(the_active_state, the_authctxt->user); if (privsep_is_preauth && pmonitor != NULL && pmonitor->m_pid > 1) { debug("Killing privsep child %d", pmonitor->m_pid); -- 2.53.0