Skip to content

Commit c6f96b3

Browse files
committed
Shut down forked PAM handle
Shutting down the forked PAM handle must be done as per the Linux-PAM documentation, specifically after setuid. This is not a new requirement, but until recently (~2021) there was no consequence to not doing so. `pam_cap` now requires the handle to be shut down correctly in order to configure ambient capabilities for the session. Importantly, these must be configured after setuid, as setuid clears the ambient capability vector. Signed-off-by: Tudor Brindus <me@tbrindus.ca>
1 parent 67a115e commit c6f96b3

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

auth-pam.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,18 @@ sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
661661

662662
static struct pam_conv store_conv = { sshpam_store_conv, NULL };
663663

664+
void
665+
sshpam_cleanup_in_child(void)
666+
{
667+
if (sshpam_handle == NULL)
668+
return;
669+
670+
#ifdef PAM_DATA_SILENT
671+
/* macOS PAM doesn't support PAM_DATA_SILENT. */
672+
pam_end(sshpam_handle, PAM_SUCCESS | PAM_DATA_SILENT);
673+
#endif
674+
}
675+
664676
void
665677
sshpam_cleanup(void)
666678
{

auth-pam.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ char ** fetch_pam_child_environment(void);
3939
void free_pam_environment(char **);
4040
void sshpam_thread_cleanup(void);
4141
void sshpam_cleanup(void);
42+
void sshpam_cleanup_in_child(void);
4243
int sshpam_auth_passwd(Authctxt *, const char *);
4344
int sshpam_get_maxtries_reached(void);
4445
void sshpam_set_maxtries_reached(int);

session.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,21 @@ do_child(struct ssh *ssh, Session *s, const char *command)
15641564
*/
15651565
env = do_setup_env(ssh, s, shell);
15661566

1567+
#ifdef USE_PAM
1568+
if (options.use_pam) {
1569+
/*
1570+
* Shutting down the forked PAM handle must be done as per the
1571+
* Linux-PAM documentation, specifically after setuid.
1572+
*
1573+
* Concretely, this ensures pam_cap can configure ambient
1574+
* capabilities for the session by applying them during
1575+
* cleanup. Without this, the ambient capability vector gets
1576+
* cleared during setuid.
1577+
*/
1578+
sshpam_cleanup_in_child();
1579+
}
1580+
#endif
1581+
15671582
#ifdef HAVE_LOGIN_CAP
15681583
shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
15691584
#endif

0 commit comments

Comments
 (0)