Hello everyone! Ever since we started using PAM for 2fa, we noticed that sshd’s behavior was not the same for existing and non existing users, we didn’t have the time to troubleshoot so they put an intern on the case (me). When trying to login via a password we have (existing user):
ssh user@host
(user@host) Password:
(user@host) Password:
(user@host) Password:
user@host: Permission denied (publickey,keyboard-interactive,hostbased).
Now for non existing users we have:
ssh user@host
(user@host) Password:
Connection closed by XXX.XXX.XXX.XX port 22
After a lot of debugging, we found out that a specific RHEL patch (preserve-pam-errors.patch) causes the issue when a PAM module returns the value PAM_USER_UNKNOWN (affects RHEL’s versions of openssh 8.0p1 and up) . It’s seems like RHEL is aware of this since it’s getting patched in centos-stream sshd doesn't propose to enter password again when a non-existing user is specified (!87) · Merge requests · Red Hat / centos-stream / rpms / openssh · GitLab
Here are some steps to reproduce the issue on Rocky8/9
- Setup the sshd_config
Make sure you are using keyboard-interective:pam for password authentification, here are our auth methods for example (password +2fa):
AuthenticationMethods hostbased publickey,keyboard-interactive:pam keyboard-interactive:pam,keyboard-interactive:pam
PasswordAuthentication no
ChallengeResponseAuthentication yes
UsePAM yes
- Put a PAM module that returns PAM_USER_UNKNOWN when a user is invalid, you could use pam_debug but you can achieve the same by putting pam_access.so as a required module in /etc/pam.d/password-auth.
password-auth stack (make sure your are using it in /etc/pam.d/sshd):
auth required pam_env.so
auth required pam_access.so
auth sufficient pam_unix.so try_first_pass nullok
auth sufficient pam_sss.so use_first_pass
auth required pam_deny.so
- systemctl restart sshd
- try to do a password login with a valid and invalid user.
A quick fix would be to do something like this:
auth required pam_env.so
auth [success=ok new_authtok_reqd=ok ignore=ignore user_unknown=ignore default=bad] pam_access.so
auth sufficient pam_unix.so try_first_pass nullok
auth sufficient pam_sss.so use_first_pass
auth required pam_deny.so
My main question is how would you handle this ? Should an issue be opened with Rocky/RHEL ?
Thanks.