Re: [PATCH 2/2] selftests/safesetid: test GID policy with mismatched real IDs
From: Guicai Ma
Date: Wed Nov 25 2026 - 03:52:13 EST
On Sat, 3 Oct 2026 19:34:16 +0900, Sung Byeongchan wrote:
> +#define GID_POLICY "2000:2001\n"
Because 2001 has no self-rule, verify_ruleset() logs an "insecure policy
detected" warning and inserts 2001:2001 on every run. Adding 2001:2001
here (as safesetid-test.c does with 2:2/3:3) keeps dmesg clean.
> + policy = fopen(GID_POLICY_FILE, "w");
> + if (!policy) {
> + perror("fopen gid policy");
> + return -1;
> + }
If SafeSetID is not built in or enabled in the active LSM list, fopen()
fails with ENOENT and main() exits 1 instead of 4 (ksft_skip). Returning
4 on ENOENT, and adding CONFIG_SECURITY_SAFESETID=y to
tools/testing/selftests/safesetid/config (like landlock/config does),
would avoid false failures on kernels without SafeSetID active.
> + for (i = 0; i <= CAP_LAST_CAP; i++) {
> + if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0) != 0) {
> + fprintf(stderr, "unexpected ambient capability %d\n", i);
> + return -1;
> + }
> + }
When built with newer headers and run on an older kernel,
PR_CAP_AMBIENT_IS_SET returns -1 with EINVAL for unknown capabilities.
Checking `== 1` (or ignoring -1/EINVAL) avoids false failures there.
> + pidfd = syscall(SYS_pidfd_open, child, 0);
Using __NR_pidfd_open (with an #ifndef fallback like selftests/pidfd)
avoids requiring glibc >= 2.36.
Tested on arm64 under KVM with KASAN (v7.4-rc3-178-g20eef29b7c84): the
two forbidden cases fail on the unpatched kernel and all four pass with
1/2 applied, including when run after safesetid-test.sh in TEST_PROGS
order.
Tested-by: Guicai Ma <maguicai@xxxxxxxxxx>
Thanks,
Guicai