RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 1997969 - pam_keyinit isn't thread safe
Summary: pam_keyinit isn't thread safe
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Enterprise Linux 8
Classification: Red Hat
Component: pam
Version: 8.4
Hardware: x86_64
OS: Linux
high
high
Target Milestone: rc
: ---
Assignee: Iker Pedrosa
QA Contact: Anuj Borah
URL:
Whiteboard: sync-to-jira review
: 2036107 (view as bug list)
Depends On:
Blocks: 2061696
TreeView+ depends on / blocked
 
Reported: 2021-08-26 07:59 UTC by Renaud Marigny
Modified: 2022-11-08 12:34 UTC (History)
13 users (show)

Fixed In Version: pam-1.3.1-17.el8
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 2061696 (view as bug list)
Environment:
Last Closed: 2022-11-08 10:48:39 UTC
Type: Bug
Target Upstream Version:
Embargoed:
pm-rhel: mirror+


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Issue Tracker RHELPLAN-95265 0 None None None 2021-08-26 08:00:42 UTC
Red Hat Issue Tracker SSSD-4310 0 None None None 2022-02-02 13:27:04 UTC
Red Hat Product Errata RHBA-2022:7723 0 None None None 2022-11-08 10:49:01 UTC

Comment 3 Florian Weimer 2021-09-01 10:12:04 UTC
The backtrace indicates that the crash happens because setregid has succeeded for at least one thread, but then fails for some other thread. There is no way to continue safely, so glibc aborts. Some threads have already switched credentials, and there is no provision for partial setregid failures.

This is likely caused by Samba's credential switching using direct system calls. The pam_keyinit module seems not quite thread-safe, in part due to what it does. There are several such modules. In general, PAM does not interact well with multiple threads.

Comment 4 Andreas Schneider 2021-09-01 10:35:06 UTC
This means the issue is the pam_keyinit module.

Comment 5 Andreas Schneider 2021-09-01 10:45:27 UTC
It needs something like this:


 modules/pam_keyinit/pam_keyinit.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/modules/pam_keyinit/pam_keyinit.c b/modules/pam_keyinit/pam_keyinit.c
index 92e4953b..5707776c 100644
--- a/modules/pam_keyinit/pam_keyinit.c
+++ b/modules/pam_keyinit/pam_keyinit.c
@@ -64,6 +64,24 @@ static void error(pam_handle_t *pamh, const char *fmt, ...)
 	va_end(va);
 }
 
+static int pam_setreuid(uid_t ruid, uid_t euid)
+{
+#if defined(HAVE_LINUX_32BIT_SYSCALLS)
+        return syscall(SYS_setreuid32, ruid, euid);
+#else
+        return syscall(SYS_setreuid, ruid, euid);
+#endif
+}
+
+static int pam_setregid(gid_t rgid, gid_t egid)
+{
+#if defined(HAVE_LINUX_32BIT_SYSCALLS)
+        return syscall(SYS_setregid32, rgid, egid);
+#else
+        return syscall(SYS_setregid, rgid, egid);
+#endif
+}
+
 /*
  * initialise the session keyring for this process
  */
@@ -140,14 +158,14 @@ static int kill_keyrings(pam_handle_t *pamh, int error_ret)
 
 		/* switch to the real UID and GID so that we have permission to
 		 * revoke the key */
-		if (revoke_as_gid != old_gid && setregid(-1, revoke_as_gid) < 0) {
+		if (revoke_as_gid != old_gid && pam_setregid(-1, revoke_as_gid) < 0) {
 			error(pamh, "Unable to change GID to %d temporarily\n", revoke_as_gid);
 			return error_ret;
 		}
 
 		if (revoke_as_uid != old_uid && setresuid(-1, revoke_as_uid, old_uid) < 0) {
 			error(pamh, "Unable to change UID to %d temporarily\n", revoke_as_uid);
-			if (getegid() != old_gid && setregid(-1, old_gid) < 0)
+			if (getegid() != old_gid && pam_setregid(-1, old_gid) < 0)
 				error(pamh, "Unable to change GID back to %d\n", old_gid);
 			return error_ret;
 		}
@@ -157,12 +175,12 @@ static int kill_keyrings(pam_handle_t *pamh, int error_ret)
 		}
 
 		/* return to the original UID and GID (probably root) */
-		if (revoke_as_uid != old_uid && setreuid(-1, old_uid) < 0) {
+		if (revoke_as_uid != old_uid && pam_setreuid(-1, old_uid) < 0) {
 			error(pamh, "Unable to change UID back to %d\n", old_uid);
 			ret = error_ret;
 		}
 
-		if (revoke_as_gid != old_gid && setregid(-1, old_gid) < 0) {
+		if (revoke_as_gid != old_gid && pam_setregid(-1, old_gid) < 0) {
 			error(pamh, "Unable to change GID back to %d\n", old_gid);
 			ret = error_ret;
 		}
@@ -215,14 +233,14 @@ static int do_keyinit(pam_handle_t *pamh, int argc, const char **argv, int error
 
 	/* switch to the real UID and GID so that the keyring ends up owned by
 	 * the right user */
-	if (gid != old_gid && setregid(gid, -1) < 0) {
+	if (gid != old_gid && pam_setregid(gid, -1) < 0) {
 		error(pamh, "Unable to change GID to %d temporarily\n", gid);
 		return error_ret;
 	}
 
-	if (uid != old_uid && setreuid(uid, -1) < 0) {
+	if (uid != old_uid && pam_setreuid(uid, -1) < 0) {
 		error(pamh, "Unable to change UID to %d temporarily\n", uid);
-		if (setregid(old_gid, -1) < 0)
+		if (pam_setregid(old_gid, -1) < 0)
 			error(pamh, "Unable to change GID back to %d\n", old_gid);
 		return error_ret;
 	}
@@ -230,12 +248,12 @@ static int do_keyinit(pam_handle_t *pamh, int argc, const char **argv, int error
 	ret = init_keyrings(pamh, force, error_ret);
 
 	/* return to the original UID and GID (probably root) */
-	if (uid != old_uid && setreuid(old_uid, -1) < 0) {
+	if (uid != old_uid && pam_setreuid(old_uid, -1) < 0) {
 		error(pamh, "Unable to change UID back to %d\n", old_uid);
 		ret = error_ret;
 	}
 
-	if (gid != old_gid && setregid(old_gid, -1) < 0) {
+	if (gid != old_gid && pam_setregid(old_gid, -1) < 0) {
 		error(pamh, "Unable to change GID back to %d\n", old_gid);
 		ret = error_ret;
 	}


Configure check for HAVE_LINUX_32BIT_SYSCALLS should look like this:

https://git.samba.org/?p=uid_wrapper.git;a=blob;f=ConfigureChecks.cmake;h=0e2d84f90d16f9b7afeb0016dcd6f8473fbbf944;hb=refs/heads/master#l101


There are also several local variables which should be thread local or atomic.

Comment 6 Andreas Schneider 2021-09-01 10:46:26 UTC
s/local variables/global variables/

Comment 7 Andreas Schneider 2021-09-01 10:55:07 UTC
For writing tests you can use pam_wrapper and libpamtest https://cwrap.org/pam_wrapper.html
https://gitlab.com/cwrap/pam_wrapper/-/blob/master/tests/test_pam_wrapper.c <<< tests using libpamtest

Comment 8 David Howells 2021-09-01 11:13:53 UTC
Do we know why smbd is loading pam_keyinit?  It's not in /etc/pam.d/samba

Comment 9 Andreas Schneider 2021-09-01 11:56:40 UTC
I have no idea. Looking into sosreport-KYOTO2-2021-08-11-dpmuupi it isn't configured in /etc.pam.d/samba. However the backtrace above shows we end up in pam_keyinit.

Comment 14 Andreas Schneider 2022-01-19 10:49:14 UTC
*** Bug 2036107 has been marked as a duplicate of this bug. ***

Comment 15 Andreas Schneider 2022-01-20 14:14:48 UTC
From the setreuid(2) manpage:

   C library/kernel differences
       At the kernel level, user IDs and group IDs are a per-thread attribute.  However, POSIX requires that all threads in a process share the same credentials.   The
       NPTL  threading  implementation  handles  the  POSIX requirements by providing wrapper functions for the various system calls that change process UIDs and GIDs.
       These wrapper functions (including those for setreuid() and setregid()) employ a signal-based technique to ensure that when one thread changes credentials,  all
       of the other threads in the process also change their credentials.  For details, see nptl(7).

Comment 16 Iker Pedrosa 2022-02-01 13:40:12 UTC
Upstream PR:
    https://github.com/linux-pam/linux-pam/pull/432

Comment 17 Iker Pedrosa 2022-02-21 14:14:26 UTC
master:
    pam_keyinit: thread-safe implementation - a35e092e24ee7632346a0e1b4a203c04d4cd2c62

Comment 24 errata-xmlrpc 2022-11-08 10:48:39 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (pam bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:7723


Note You need to log in before you can comment on or make changes to this bug.