[PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters

Aaron Tomlin atomlin at atomlin.com
Thu Nov 19 04:07:05 UTC 2026


Extend the BPF LSM selftest to attach to the task_setscheduler hook and
exercise scheduling policy updates via sched_setscheduler(), parameter
updates via sched_setparam() and sched_setattr() with
SCHED_FLAG_KEEP_POLICY, CPU affinity changes via sched_setaffinity(),
and child timer slack updates via /proc/[pid]/timerslack_ns. Verify that
the hook observes the expected sched_attr and cpumask arguments across
each path, including the resolved scheduling policy and effective
SCHED_FLAG_RESET_ON_FORK state for parameter-only updates as well as
NULL pointers when an argument does not apply, and that returning -EPERM
denies each operation.

Also add verifier test cases confirming that dereferencing the nullable
attr or in_mask parameters without an explicit NULL check is rejected by
the BPF verifier.

Signed-off-by: Aaron Tomlin <atomlin at atomlin.com>
---
 .../selftests/bpf/prog_tests/test_lsm.c       | 164 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/lsm.c       |  51 ++++++
 .../selftests/bpf/progs/verifier_lsm.c        |  49 ++++++
 3 files changed, 264 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/test_lsm.c b/tools/testing/selftests/bpf/prog_tests/test_lsm.c
index c0ae813698ae..5081c243bc5e 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_lsm.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_lsm.c
@@ -21,8 +21,30 @@
 
 char *CMD_ARGS[] = {"true", NULL};
 
+#ifndef SCHED_RESET_ON_FORK
+#define SCHED_RESET_ON_FORK	0x40000000
+#endif
+
+#ifndef SCHED_FLAG_KEEP_POLICY
+#define SCHED_FLAG_KEEP_POLICY	0x08
+#endif
+
+struct test_sched_attr {
+	__u32 size;
+	__u32 sched_policy;
+	__u64 sched_flags;
+	__s32 sched_nice;
+	__u32 sched_priority;
+	__u64 sched_runtime;
+	__u64 sched_deadline;
+	__u64 sched_period;
+	__u32 sched_util_min;
+	__u32 sched_util_max;
+};
+
 enum {
 	INODE_IDMAP_ALL = (1U << 6) - 1,
+	SETSCHED_SAW_ALL = (1U << 5) - 1,
 };
 
 int exec_cmd(int *monitored_pid)
@@ -255,6 +277,141 @@ static int test_lsm_inode_idmap(struct lsm *skel)
 	return ret;
 }
 
+static int find_first_cpu(const cpu_set_t *mask)
+{
+	int cpu;
+
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		if (CPU_ISSET(cpu, mask))
+			return cpu;
+	}
+
+	return -1;
+}
+
+static int test_lsm_task_setscheduler(struct lsm *skel)
+{
+	struct test_sched_attr sattr = {
+		.size = sizeof(sattr),
+		.sched_flags = SCHED_FLAG_KEEP_POLICY,
+	};
+	struct sched_param param = {}, saved_param = {};
+	cpu_set_t mask, saved_mask;
+	char timerslack_path[64];
+	int cpu, saved_policy = -1;
+	pid_t child_pid = -1, waited;
+	bool restore_sched = false;
+	bool restore_aff = false;
+	int len, ret = -1;
+
+	saved_policy = sched_getscheduler(0);
+	if (!ASSERT_GE(saved_policy, 0, "sched_getscheduler"))
+		return -1;
+	if (!ASSERT_OK(sched_getparam(0, &saved_param), "sched_getparam"))
+		return -1;
+	restore_sched = true;
+
+	if (!ASSERT_OK(sched_getaffinity(0, sizeof(saved_mask), &saved_mask),
+		       "sched_getaffinity"))
+		goto out;
+	restore_aff = true;
+
+	cpu = find_first_cpu(&saved_mask);
+	if (!ASSERT_GE(cpu, 0, "find_first_cpu"))
+		goto out;
+
+	if (saved_policy != SCHED_OTHER &&
+	    !ASSERT_OK(sched_setscheduler(0, SCHED_OTHER, &param),
+		       "sched_setscheduler_other"))
+		goto out;
+
+	child_pid = fork();
+	if (!ASSERT_GE(child_pid, 0, "fork"))
+		goto out;
+	if (child_pid == 0) {
+		pause();
+		_exit(0);
+	}
+
+	len = snprintf(timerslack_path, sizeof(timerslack_path),
+		       "/proc/%d/timerslack_ns", child_pid);
+	if (!ASSERT_GE(len, 0, "timerslack_snprintf") ||
+	    !ASSERT_LT(len, sizeof(timerslack_path), "timerslack_snprintf_len"))
+		goto out;
+
+	skel->bss->setsched_seen = 0;
+	skel->bss->setsched_affinity_cpu = cpu;
+	skel->bss->setsched_deny = 0;
+
+	if (!ASSERT_OK(sched_setscheduler(0, SCHED_BATCH | SCHED_RESET_ON_FORK,
+					  &param),
+		       "sched_setscheduler_batch"))
+		goto out;
+	if (!ASSERT_OK(sched_setparam(0, &param), "sched_setparam_batch"))
+		goto out;
+	if (!ASSERT_OK(syscall(__NR_sched_setattr, 0, &sattr, 0),
+		       "sched_setattr_keep_policy"))
+		goto out;
+	if (!ASSERT_EQ(sched_getscheduler(0),
+		       SCHED_BATCH | SCHED_RESET_ON_FORK,
+		       "sched_getscheduler_reset_on_fork"))
+		goto out;
+
+	CPU_ZERO(&mask);
+	CPU_SET(cpu, &mask);
+	if (!ASSERT_OK(sched_setaffinity(0, sizeof(mask), &mask),
+		       "sched_setaffinity"))
+		goto out;
+
+	if (!ASSERT_OK(write_file(timerslack_path, "0"), "timerslack_ns_allow"))
+		goto out;
+
+	if (!ASSERT_EQ(skel->bss->setsched_seen, SETSCHED_SAW_ALL,
+		       "setsched_seen"))
+		goto out;
+
+	skel->bss->setsched_deny = 1;
+
+	if (!ASSERT_EQ(sched_setscheduler(0, SCHED_BATCH, &param), -1,
+		       "sched_setscheduler_deny") ||
+	    !ASSERT_EQ(errno, EPERM, "sched_setscheduler_deny_errno"))
+		goto out;
+
+	if (!ASSERT_EQ(sched_setparam(0, &param), -1, "sched_setparam_deny") ||
+	    !ASSERT_EQ(errno, EPERM, "sched_setparam_deny_errno"))
+		goto out;
+
+	if (!ASSERT_EQ(syscall(__NR_sched_setattr, 0, &sattr, 0), -1,
+		       "sched_setattr_deny") ||
+	    !ASSERT_EQ(errno, EPERM, "sched_setattr_deny_errno"))
+		goto out;
+
+	if (!ASSERT_EQ(sched_setaffinity(0, sizeof(mask), &mask), -1,
+		       "sched_setaffinity_deny") ||
+	    !ASSERT_EQ(errno, EPERM, "sched_setaffinity_deny_errno"))
+		goto out;
+
+	if (!ASSERT_EQ(write_file(timerslack_path, "0"), -1,
+		       "timerslack_ns_deny") ||
+	    !ASSERT_EQ(errno, EPERM, "timerslack_ns_deny_errno"))
+		goto out;
+
+	ret = 0;
+out:
+	skel->bss->setsched_deny = 0;
+	if (child_pid > 0) {
+		kill(child_pid, SIGKILL);
+		do {
+			waited = waitpid(child_pid, NULL, 0);
+		} while (waited < 0 && errno == EINTR);
+	}
+	if (restore_aff)
+		sched_setaffinity(0, sizeof(saved_mask), &saved_mask);
+	if (restore_sched)
+		sched_setscheduler(0, saved_policy, &saved_param);
+	return ret;
+}
+
 static int test_lsm(struct lsm *skel)
 {
 	struct bpf_link *link;
@@ -282,6 +439,10 @@ static int test_lsm(struct lsm *skel)
 	if (!ASSERT_OK(err, "test_lsm_inode_idmap"))
 		return err;
 
+	err = test_lsm_task_setscheduler(skel);
+	if (!ASSERT_OK(err, "test_lsm_task_setscheduler"))
+		return err;
+
 	err = stack_mprotect();
 	if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
 	    !ASSERT_EQ(errno, EPERM, "stack_mprotect"))
@@ -302,6 +463,9 @@ static int test_lsm(struct lsm *skel)
 	skel->bss->mprotect_count = 0;
 	skel->bss->inode_identity_idmap_seen = 0;
 	skel->bss->inode_idmapped_mount_seen = 0;
+	skel->bss->setsched_seen = 0;
+	skel->bss->setsched_affinity_cpu = 0;
+	skel->bss->setsched_deny = 0;
 	return 0;
 }
 
diff --git a/tools/testing/selftests/bpf/progs/lsm.c b/tools/testing/selftests/bpf/progs/lsm.c
index e210e07d3a37..d8ff10d4174e 100644
--- a/tools/testing/selftests/bpf/progs/lsm.c
+++ b/tools/testing/selftests/bpf/progs/lsm.c
@@ -86,6 +86,15 @@ int mprotect_count = 0;
 int bprm_count = 0;
 __u32 inode_identity_idmap_seen = 0;
 __u32 inode_idmapped_mount_seen = 0;
+__u32 setsched_seen = 0;
+__u32 setsched_affinity_cpu = 0;
+int setsched_deny = 0;
+
+#define SCHED_BATCH			3
+#define SCHED_FLAG_RESET_ON_FORK	0x01
+#define SCHED_FLAG_KEEP_POLICY		0x08
+
+bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym __weak;
 
 enum {
 	INODE_IDMAP_CREATE	= 1U << 0,
@@ -96,6 +105,14 @@ enum {
 	INODE_IDMAP_PERMISSION	= 1U << 5,
 };
 
+enum {
+	SETSCHED_SAW_ATTR		= 1U << 0,
+	SETSCHED_SAW_SETPARAM		= 1U << 1,
+	SETSCHED_SAW_KEEP_POLICY	= 1U << 2,
+	SETSCHED_SAW_AFFINITY		= 1U << 3,
+	SETSCHED_SAW_BOTH_NULL		= 1U << 4,
+};
+
 static __always_inline bool is_monitored_idmap(struct mnt_idmap *idmap)
 {
 	__u32 pid = bpf_get_current_pid_tgid() >> 32;
@@ -164,6 +181,40 @@ int BPF_PROG(test_inode_permission, struct mnt_idmap *idmap,
 	return record_inode_idmap(idmap, INODE_IDMAP_PERMISSION, ret);
 }
 
+SEC("lsm/task_setscheduler")
+int BPF_PROG(test_task_setscheduler, struct task_struct *p,
+	     const struct sched_attr *attr, const struct cpumask *in_mask,
+	     int ret)
+{
+	__s32 pid = bpf_get_current_pid_tgid() >> 32;
+
+	if (ret || !monitored_pid || monitored_pid != pid)
+		return ret;
+
+	if (attr && !in_mask) {
+		if (attr->sched_policy == SCHED_BATCH &&
+		    (attr->sched_flags & SCHED_FLAG_RESET_ON_FORK)) {
+			if (p->policy != SCHED_BATCH)
+				setsched_seen |= SETSCHED_SAW_ATTR;
+			else if (attr->sched_flags & SCHED_FLAG_KEEP_POLICY)
+				setsched_seen |= SETSCHED_SAW_KEEP_POLICY;
+			else
+				setsched_seen |= SETSCHED_SAW_SETPARAM;
+		}
+	} else if (!attr && in_mask) {
+		if (bpf_cpumask_test_cpu(setsched_affinity_cpu, in_mask))
+			setsched_seen |= SETSCHED_SAW_AFFINITY;
+	} else if (!attr && !in_mask) {
+		if (p->pid != pid)
+			setsched_seen |= SETSCHED_SAW_BOTH_NULL;
+	}
+
+	if (setsched_deny)
+		return -EPERM;
+
+	return 0;
+}
+
 SEC("lsm/file_mprotect")
 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
 	     unsigned long reqprot, unsigned long prot, int ret)
diff --git a/tools/testing/selftests/bpf/progs/verifier_lsm.c b/tools/testing/selftests/bpf/progs/verifier_lsm.c
index 8fbf7862265f..8e2bc3040867 100644
--- a/tools/testing/selftests/bpf/progs/verifier_lsm.c
+++ b/tools/testing/selftests/bpf/progs/verifier_lsm.c
@@ -188,6 +188,55 @@ int BPF_PROG(null_check, struct file *file)
 	return 0;
 }
 
+SEC("lsm/task_setscheduler")
+__description("not null checking nullable attr in bpf_lsm_task_setscheduler")
+__failure __msg("invalid mem access 'trusted_ptr_or_null_'")
+int BPF_PROG(task_setscheduler_attr_no_null_check, struct task_struct *p,
+	     const struct sched_attr *attr, const struct cpumask *in_mask)
+{
+	__u32 policy;
+
+	policy = attr->sched_policy;
+	__sink(policy);
+
+	return 0;
+}
+
+SEC("lsm/task_setscheduler")
+__description("not null checking nullable in_mask in bpf_lsm_task_setscheduler")
+__failure __msg("invalid mem access 'trusted_ptr_or_null_'")
+int BPF_PROG(task_setscheduler_mask_no_null_check, struct task_struct *p,
+	     const struct sched_attr *attr, const struct cpumask *in_mask)
+{
+	unsigned long bits;
+
+	bits = in_mask->bits[0];
+	__sink(bits);
+
+	return 0;
+}
+
+SEC("lsm/task_setscheduler")
+__description("null checking nullable pointers in bpf_lsm_task_setscheduler")
+__success
+int BPF_PROG(task_setscheduler_null_check, struct task_struct *p,
+	     const struct sched_attr *attr, const struct cpumask *in_mask)
+{
+	unsigned long bits;
+	__u32 policy;
+
+	if (attr) {
+		policy = attr->sched_policy;
+		__sink(policy);
+	}
+	if (in_mask) {
+		bits = in_mask->bits[0];
+		__sink(bits);
+	}
+
+	return 0;
+}
+
 SEC("lsm_cgroup/file_open")
 __description("sleepable lsm_cgroup program is rejected")
 __failure __msg("Program of this type cannot be sleepable")
-- 
2.55.0




More information about the Linux-security-module-archive mailing list