Skip to contentSkip to conversation

SafeSetID: use real GID for GID policy lookups

PATCH In review [email protected] 5 participants 9 messages Oct 3 – Nov 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover SafeSetID: use real GID for GID policy lookups

From: Sung Byeongchan <[email protected]>

SafeSetID GID policy enforcement first checks whether a policy applies to the old real GID. Its per-target helper then incorrectly constructs the source lookup key from the old real UID. When those numeric IDs differ, the helper can miss the policy for the real GID and return the unconstrained default.

The issue is present at Torvalds mainline commit e767a4ea70a3992c37ed604157d32f0dfbf9b1e3. In three clean QEMU boots, a task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 policy. The bypass covered setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups(), including supplementary-group acquisition and access to a synthetic group-protected resource. The acquired group credentials also persisted across exec.

This demonstrates a SafeSetID GID policy bypass and group privilege expansion. It does not demonstrate direct UID 0 elevation or a universal local privilege escalation, and the CAP_SETGID prerequisite is explicit. The security boundary is that SafeSetID was configured to constrain that capability but failed to enforce its GID allowlist.

Patch 1 selects the old real UID for UID checks and the old real GID for GID checks. Patch 2 adds a focused regression test for mismatched real IDs, covering both setresgid() and setgroups(), and records the SafeSetID selftest path in MAINTAINERS.

The vulnerable and fixed kernels used identical debug configurations. Across three clean boots per kernel, the vulnerable matrix reproduced all tested forbidden transitions and the fixed matrix blocked all of them. Allowed, existing-ID, no-policy, no-capability and UID-policy controls behaved as expected. The existing SafeSetID selftest passed on both, the new regression test failed on the vulnerable kernel and passed on the fixed kernel, and no KASAN, UBSAN, BUG, WARNING, Oops, panic, lockdep, refcount, hung-task or kmemleak finding was observed.

A reproducer and the complete QEMU validation logs are available privately on request. They are not included in this public posting.

This finding and patch preparation were assisted by OpenAI Codex.

Sung Byeongchan (2):
security: safesetid: use real GID for GID policy lookup selftests/safesetid: test GID policy with mismatched real IDs

 MAINTAINERS                                   |   1 +
 security/safesetid/lsm.c                      |   8 +-
 tools/testing/selftests/safesetid/Makefile    |   4 +-
 .../safesetid/safesetid-gid-policy-test.c     | 252 ++++++++++++++++++
 .../safesetid/safesetid-gid-policy-test.sh    |  11 +
 5 files changed, 271 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/safesetid/safesetid-gid-policy-test.c
 create mode 100755 tools/testing/selftests/safesetid/safesetid-gid-policy-test.sh

base-commit: e767a4ea70a3992c37ed604157d32f0dfbf9b1e3

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 security: safesetid: use real GID for GID policy lookup

From: Sung Byeongchan <[email protected]>

SafeSetID policies constrain the ID transitions available to a task for each source ID. id_permitted_for_cred() always builds that source key from the old real UID, even when it is checking a GID transition. If the real UID and real GID differ, the policy attached to the real GID is therefore missed and the lookup can return the unconstrained default.

On current mainline, a test task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 GID policy. The bypass reproduced for setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups() across three clean QEMU boots, and enabled access to a synthetic group-protected resource. This shows a SafeSetID GID policy bypass and group privilege expansion; it does not show direct UID 0 elevation or a universal local privilege escalation.

Build the source key from the old real UID for UID policy checks and the old real GID for GID policy checks. In three fixed-kernel boots, all tested non-allowlisted transitions were blocked while the allowed target, existing-ID, no-policy and UID-policy controls, and the existing SafeSetID selftest continued to pass.

Fixes 5294bac97e12 ("LSM: SafeSetID: Add GID security policy handling")
Assisted-by OpenAI Codex
---
 security/safesetid/lsm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
security/safesetid/lsm.c +5 -3
index d5fb949050dd8..18124fc499dbc 100644
@@ -148,13 +148,16 @@ static int safesetid_security_capable(const struct cred *cred,
 static bool id_permitted_for_cred(const struct cred *old, kid_t new_id, enum setid_type new_type)
 {
 	bool permitted;
+	kid_t source_id;
 
 	/* If our old creds already had this ID in it, it's fine. */
 	if (new_type == UID) {
+		source_id.uid = old->uid;
 		if (uid_eq(new_id.uid, old->uid) || uid_eq(new_id.uid, old->euid) ||
 			uid_eq(new_id.uid, old->suid))
 			return true;
 	} else if (new_type == GID){
+		source_id.gid = old->gid;
 		if (gid_eq(new_id.gid, old->gid) || gid_eq(new_id.gid, old->egid) ||
 			gid_eq(new_id.gid, old->sgid))
 			return true;
@@ -162,11 +165,10 @@ static bool id_permitted_for_cred(const struct cred *old, kid_t new_id, enum set
 		return false;
 
 	/*
-	 * Transitions to new UIDs require a check against the policy of the old
-	 * RUID.
+	 * Transitions require a check against the policy of the old real ID.
 	 */
 	permitted =
-	    setid_policy_lookup((kid_t){.uid = old->uid}, new_id, new_type) != SIDPOL_CONSTRAINED;
+	    setid_policy_lookup(source_id, new_id, new_type) != SIDPOL_CONSTRAINED;
 
 	if (!permitted) {
 		if (new_type == UID) {
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] security: safesetid: use real GID for GID policy lookup

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines

From: Sung Byeongchan <[email protected]>

SafeSetID policies constrain the ID transitions available to a task for each source ID. id_permitted_for_cred() always builds that source key from the old real UID, even when it is checking a GID transition. If the real UID and real GID differ, the policy attached to the real GID is therefore missed and the lookup can return the unconstrained default.

On current mainline, a test task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 GID policy. The bypass reproduced for setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups() across three clean QEMU boots, and enabled access to a synthetic group-protected resource. This shows a SafeSetID GID policy bypass and group privilege expansion; it does not show direct UID 0 elevation or a universal local privilege escalation.

Build the source key from the old real UID for UID policy checks and the old real GID for GID policy checks. In three fixed-kernel boots, all tested non-allowlisted transitions were blocked while the allowed target, existing-ID, no-policy and UID-policy controls, and the existing SafeSetID selftest continued to pass.

Quoted 4 lines
Fixes 5294bac97e12 ("LSM: SafeSetID: Add GID security policy handling")
Assisted-by OpenAI Codex

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] security: safesetid: use real GID for GID policy lookup

In reply to [email protected]
Quoted 22 lines · On Sat, Oct 03, 2026 at 07:34:15PM +0900, [email protected] wrote:

From: Sung Byeongchan <[email protected]>

SafeSetID policies constrain the ID transitions available to a task for each source ID. id_permitted_for_cred() always builds that source key from the old real UID, even when it is checking a GID transition. If the real UID and real GID differ, the policy attached to the real GID is therefore missed and the lookup can return the unconstrained default.

On current mainline, a test task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 GID policy. The bypass reproduced for setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups() across three clean QEMU boots, and enabled access to a synthetic group-protected resource. This shows a SafeSetID GID policy bypass and group privilege expansion; it does not show direct UID 0 elevation or a universal local privilege escalation.

Build the source key from the old real UID for UID policy checks and the old real GID for GID policy checks. In three fixed-kernel boots, all tested non-allowlisted transitions were blocked while the allowed target, existing-ID, no-policy and UID-policy controls, and the existing SafeSetID selftest continued to pass.

Quoted 4 lines
Fixes 5294bac97e12 ("LSM: SafeSetID: Add GID security policy handling")
Assisted-by OpenAI Codex

Seems obviously correct. Thanks for the find and the fix.

Quoted 41 lines
---
 security/safesetid/lsm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
security/safesetid/lsm.c +5 -3
index d5fb949050dd8..18124fc499dbc 100644
@@ -148,13 +148,16 @@ static int safesetid_security_capable(const struct cred *cred,
 static bool id_permitted_for_cred(const struct cred *old, kid_t new_id, enum setid_type new_type)
 {
 	bool permitted;
+	kid_t source_id;
 
 	/* If our old creds already had this ID in it, it's fine. */
 	if (new_type == UID) {
+		source_id.uid = old->uid;
 		if (uid_eq(new_id.uid, old->uid) || uid_eq(new_id.uid, old->euid) ||
 			uid_eq(new_id.uid, old->suid))
 			return true;
 	} else if (new_type == GID){
+		source_id.gid = old->gid;
 		if (gid_eq(new_id.gid, old->gid) || gid_eq(new_id.gid, old->egid) ||
 			gid_eq(new_id.gid, old->sgid))
 			return true;
@@ -162,11 +165,10 @@ static bool id_permitted_for_cred(const struct cred *old, kid_t new_id, enum set
 		return false;
 
 	/*
-	 * Transitions to new UIDs require a check against the policy of the old
-	 * RUID.
+	 * Transitions require a check against the policy of the old real ID.
 	 */
 	permitted =
-	    setid_policy_lookup((kid_t){.uid = old->uid}, new_id, new_type) != SIDPOL_CONSTRAINED;
+	    setid_policy_lookup(source_id, new_id, new_type) != SIDPOL_CONSTRAINED;
 
 	if (!permitted) {
 		if (new_type == UID) {
Signature · 1 line
2.43.0
maintainer Reviewed-by
#4 lore raw

Re: [PATCH 1/2] security: safesetid: use real GID for GID policy lookup

Quoted 23 lines · Serge E. Hallyn
Quoted 22 lines · On Sat, Oct 03, 2026 at 07:34:15PM +0900, [email protected] wrote:

From: Sung Byeongchan <[email protected]>

SafeSetID policies constrain the ID transitions available to a task for each source ID. id_permitted_for_cred() always builds that source key from the old real UID, even when it is checking a GID transition. If the real UID and real GID differ, the policy attached to the real GID is therefore missed and the lookup can return the unconstrained default.

On current mainline, a test task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 GID policy. The bypass reproduced for setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups() across three clean QEMU boots, and enabled access to a synthetic group-protected resource. This shows a SafeSetID GID policy bypass and group privilege expansion; it does not show direct UID 0 elevation or a universal local privilege escalation.

Build the source key from the old real UID for UID policy checks and the old real GID for GID policy checks. In three fixed-kernel boots, all tested non-allowlisted transitions were blocked while the allowed target, existing-ID, no-policy and UID-policy controls, and the existing SafeSetID selftest continued to pass.

Quoted 8 lines
Quoted 4 lines
Fixes 5294bac97e12 ("LSM: SafeSetID: Add GID security policy handling")
Assisted-by OpenAI Codex

Seems obviously correct. Thanks for the find and the fix.

Looks good to me too, thanks!

Micah, assuming this looks good to you, are you planning to pick this up? If you would like me to take this via the LSM tree, please let me know.

Signature · 1 line
paul-moore.com
maintainer
#5 lore raw

Re: [PATCH 1/2] security: safesetid: use real GID for GID policy lookup

Quoted 34 lines · Paul Moore
Quoted 23 lines · Serge E. Hallyn
Quoted 22 lines · On Sat, Oct 03, 2026 at 07:34:15PM +0900, [email protected] wrote:

From: Sung Byeongchan <[email protected]>

SafeSetID policies constrain the ID transitions available to a task for each source ID. id_permitted_for_cred() always builds that source key from the old real UID, even when it is checking a GID transition. If the real UID and real GID differ, the policy attached to the real GID is therefore missed and the lookup can return the unconstrained default.

On current mainline, a test task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 GID policy. The bypass reproduced for setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups() across three clean QEMU boots, and enabled access to a synthetic group-protected resource. This shows a SafeSetID GID policy bypass and group privilege expansion; it does not show direct UID 0 elevation or a universal local privilege escalation.

Build the source key from the old real UID for UID policy checks and the old real GID for GID policy checks. In three fixed-kernel boots, all tested non-allowlisted transitions were blocked while the allowed target, existing-ID, no-policy and UID-policy controls, and the existing SafeSetID selftest continued to pass.

Quoted 8 lines
Quoted 4 lines
Fixes 5294bac97e12 ("LSM: SafeSetID: Add GID security policy handling")
Assisted-by OpenAI Codex

Seems obviously correct. Thanks for the find and the fix.

Quoted 7 lines

Looks good to me too, thanks!

Micah, assuming this looks good to you, are you planning to pick this up? If you would like me to take this via the LSM tree, please let me know.

Hi guys,

Sorry for the delay! Yes this patch looks good to me -- thanks for catching this, Sung!

It's been a really long time since I sent any pull requests to Linus, so it might be a good idea for this to come from the LSM tree if you are up for it, Paul.

Let me know, if you think its better for me to dust off my old GitHub repo and pull request etiquette I'm fine to do so as well :)

Thanks,
Micah

Quoted 3 lines

--
paul-moore.com

maintainer
#6 lore raw

Re: [PATCH 1/2] security: safesetid: use real GID for GID policy lookup

Quoted 47 lines · Micah Morton
Quoted 33 lines · Paul Moore
Quoted 23 lines · Serge E. Hallyn
Quoted 22 lines · On Sat, Oct 03, 2026 at 07:34:15PM +0900, [email protected] wrote:

From: Sung Byeongchan <[email protected]>

SafeSetID policies constrain the ID transitions available to a task for each source ID. id_permitted_for_cred() always builds that source key from the old real UID, even when it is checking a GID transition. If the real UID and real GID differ, the policy attached to the real GID is therefore missed and the lookup can return the unconstrained default.

On current mainline, a test task with real UID 1000, real GID 2000 and only CAP_SETGID obtained non-allowlisted GID 2002 despite a 2000:2001 GID policy. The bypass reproduced for setgid(), setegid(), setregid(), setresgid(), setfsgid() and setgroups() across three clean QEMU boots, and enabled access to a synthetic group-protected resource. This shows a SafeSetID GID policy bypass and group privilege expansion; it does not show direct UID 0 elevation or a universal local privilege escalation.

Build the source key from the old real UID for UID policy checks and the old real GID for GID policy checks. In three fixed-kernel boots, all tested non-allowlisted transitions were blocked while the allowed target, existing-ID, no-policy and UID-policy controls, and the existing SafeSetID selftest continued to pass.

Quoted 8 lines
Quoted 4 lines
Fixes 5294bac97e12 ("LSM: SafeSetID: Add GID security policy handling")
Assisted-by OpenAI Codex

Seems obviously correct. Thanks for the find and the fix.

Quoted 7 lines

Looks good to me too, thanks!

Micah, assuming this looks good to you, are you planning to pick this up? If you would like me to take this via the LSM tree, please let me know.

Hi guys,

Sorry for the delay! Yes this patch looks good to me -- thanks for catching this, Sung!

Quoted 6 lines

It's been a really long time since I sent any pull requests to Linus, so it might be a good idea for this to come from the LSM tree if you are up for it, Paul.

Let me know, if you think its better for me to dust off my old GitHub repo and pull request etiquette I'm fine to do so as well :)

I guess it's up to you Micah. If you want an excuse to get current on merging patches and sending pull requests to Linus this would be a good one. However, I also understand that it can be a non-trivial amount of work for one patch (which is why I made the offer).

Let me know what you would like to do; both options are fine with me. If you need some help on the administrative side, e.g. sending stuff to Linus, let me know and I'll do my best to help you out. If you would prefer me to include this in the LSM tree, please reply with an ACK tag so I can include that in the merge.

Thanks!

Signature · 1 line
paul-moore.com
#7 lore raw

PATCH 2/2 selftests/safesetid: test GID policy with mismatched real IDs

In reply to [email protected]

From: Sung Byeongchan <[email protected]>

The existing test uses equal numeric UID and GID values, so it cannot distinguish which real ID SafeSetID uses as the source of a GID policy lookup.

Add isolated child-process cases with real UID 1000, real GID 2000 and a 2000:2001 GID policy. Verify that 2001 is allowed and 2002 is rejected for both setresgid() and setgroups(). Each child retains only CAP_SETGID in its permitted and effective sets, and pidfd polling bounds every case without timing the transition.

On an affected kernel, the forbidden transitions survive and make the test fail. With the fix, SafeSetID terminates those children and the test passes.

Assisted-by OpenAI Codex
---
 MAINTAINERS                                   |   1 +
 tools/testing/selftests/safesetid/Makefile    |   4 +-
 .../safesetid/safesetid-gid-policy-test.c     | 252 ++++++++++++++++++
 .../safesetid/safesetid-gid-policy-test.sh    |  11 +
 4 files changed, 266 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/safesetid/safesetid-gid-policy-test.c
 create mode 100755 tools/testing/selftests/safesetid/safesetid-gid-policy-test.sh
MAINTAINERS +1 -0
index 7235a92ff879b..68b355ad33e0f 100644
@@ -24266,6 +24266,7 @@ M:	Micah Morton <[email protected]>
 S:	Supported
 F:	Documentation/admin-guide/LSM/SafeSetID.rst
 F:	security/safesetid/
+F:	tools/testing/selftests/safesetid/
 
 SAMSUNG AUDIO (ASoC) DRIVERS
 M:	Sylwester Nawrocki <[email protected]>
tools/testing/selftests/safesetid/Makefile +2 -2
index e815bbf2d0f4a..1a4a6630858c0 100644
@@ -3,7 +3,7 @@
 CFLAGS = -Wall -O2
 LDLIBS = -lcap
 
-TEST_PROGS := safesetid-test.sh
-TEST_GEN_FILES := safesetid-test
+TEST_PROGS := safesetid-test.sh safesetid-gid-policy-test.sh
+TEST_GEN_FILES := safesetid-test safesetid-gid-policy-test
 
 include ../lib.mk
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +252 -0

252 lines not on this page · raw message

tools/testing/selftests/safesetid/safesetid-gid-policy-test.sh new file +11 -0
new file mode 100755
index 0000000000000..7318ad3403121
@@ -0,0 +1,11 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+ksft_skip=4
+
+if [ "$(id -u)" -ne 0 ]; then
+	echo "safesetid-gid-policy-test: must be run as root" >&2
+	exit "$ksft_skip"
+fi
+
+exec ./safesetid-gid-policy-test
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH 2/2] selftests/safesetid: test GID policy with mismatched real IDs

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests: false test failure on older kernels due to strict prctl() check - [Low] selftests…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests: false test failure on older kernels due to strict prctl() check
- [Low] selftests: build failure on older glibc due to SYS_pidfd_open dependency --

commit dbf0ec68f44394083debd3f938ca7365c6cad4f1
Author: Sung Byeongchan <[email protected]>

selftests/safesetid: test GID policy with mismatched real IDs

This commit adds child-process cases to the SafeSetID selftests that use mismatched real IDs to properly distinguish the source of a GID policy lookup. It verifies that allowed and forbidden transitions are handled correctly using both setresgid() and setgroups().

Quoted 5 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +0 -0
new file mode 100644
index 0000000000000..873d65627c494

[ ... ]

Quoted 2 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +2 -0
+static int check_child_credentials(void)
+{

[ ... ]

tools/testing/selftests/safesetid/safesetid-gid-policy-test.c line 120

Quoted 6 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +6 -0
+	if (effective != expected || permitted != expected || inheritable) {
+		fprintf(stderr, "child does not hold only CAP_SETGID\n");
+		return -1;
+	}
+	for (i = 0; i <= CAP_LAST_CAP; i++) {
+		if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0) != 0) {

[Severity: Medium]
Will this strict non-zero check cause the test to falsely fail on older kernels?

If the selftest is compiled with newer kernel headers (where CAP_LAST_CAP is higher) but executed on an older kernel, prctl(PR_CAP_AMBIENT_IS_SET) will return -1 (-EINVAL) for newly defined capabilities that the older kernel does not support.

Since the test checks for != 0 instead of exactly == 1 (or explicitly ignoring -1), the -EINVAL error return is treated as an unexpected ambient capability.

Could this be updated to check for == 1 or ignore -1 to prevent false failures?

Quoted 6 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +6 -0
+			fprintf(stderr, "unexpected ambient capability %d\n", i);
+			return -1;
+		}
+	}
+	return 0;
+}

[ ... ]

Quoted 7 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +7 -0
+static int run_case(const char *name, enum test_operation operation,
+		    gid_t target, bool expect_kill)
+{
+	struct pollfd pollfd = {};
+	int status;
+	int pidfd;
+	pid_t child;

tools/testing/selftests/safesetid/safesetid-gid-policy-test.c line 191

Quoted 10 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +10 -0
+
+	child = fork();
+	if (child < 0) {
+		perror("fork");
+		return -1;
+	}
+	if (!child)
+		run_child(operation, target);
+
+	pidfd = syscall(SYS_pidfd_open, child, 0);

[Severity: Low]
Does this rely on host glibc syscall definitions instead of the kernel-provided headers?

Older glibc versions (prior to 2.36) do not define SYS_pidfd_open, which will result in a compilation error when building the selftests on an older host.

Using the kernel-provided __NR_pidfd_open is the standard approach in selftests to maintain backwards compatibility and independence from the host libc's system call knowledge. Could __NR_pidfd_open be used here instead?

Quoted 6 lines
tools/testing/selftests/safesetid/safesetid-gid-policy-test.c new file +6 -0
+	if (pidfd < 0) {
+		perror("pidfd_open");
+		kill(child, SIGKILL);
+		waitpid(child, &status, 0);
+		return -1;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2