From: Aaron Tomlin <atomlin@atomlin.com>
To: tsbogend@alpha.franken.de, paul@paul-moore.com,
jmorris@namei.org, serge@hallyn.com, andrii@kernel.org,
ast@kernel.org, daniel@iogearbox.net, eddyz87@gmail.com,
memxor@gmail.com, mingo@redhat.com, peterz@infradead.org,
juri.lelli@redhat.com, vincent.guittot@linaro.org,
stephen.smalley.work@gmail.com, casey@schaufler-ca.com,
longman@redhat.com, tj@kernel.org, hannes@cmpxchg.org,
mkoutny@suse.com
Cc: ihor.solodrai@linux.dev, martin.lau@linux.dev, song@kernel.org,
yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com,
chenridong@huaweicloud.com, dietmar.eggemann@arm.com,
rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
vschneid@redhat.com, kprateek.nayak@amd.com, omosnace@redhat.com,
kees@kernel.org, atomlin@atomlin.com, neelx@suse.com,
sean@ashe.io, chjohnst@gmail.com, steve@abita.co,
mproche@gmail.com, nick.lange@gmail.com, cgroups@vger.kernel.org,
linux-mips@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-security-module@vger.kernel.org, selinux@vger.kernel.org,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters
Date: Wed, 18 Nov 2026 23:07:05 -0500 [thread overview]
Message-ID: <20261119040705.684987-3-atomlin@atomlin.com> (raw)
In-Reply-To: <20261119040705.684987-1-atomlin@atomlin.com>
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@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, ¶m),
+ "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,
+ ¶m),
+ "sched_setscheduler_batch"))
+ goto out;
+ if (!ASSERT_OK(sched_setparam(0, ¶m), "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, ¶m), -1,
+ "sched_setscheduler_deny") ||
+ !ASSERT_EQ(errno, EPERM, "sched_setscheduler_deny_errno"))
+ goto out;
+
+ if (!ASSERT_EQ(sched_setparam(0, ¶m), -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
next prev parent reply other threads:[~2026-11-19 4:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 4:07 [PATCH v6 0/2] security: Expand task_setscheduler LSM hook Aaron Tomlin
2026-11-19 4:07 ` [PATCH v6 1/2] " Aaron Tomlin
2026-11-19 4:22 ` sashiko-bot
2026-11-19 4:07 ` Aaron Tomlin [this message]
2026-11-19 4:13 ` [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters sashiko-bot
2026-11-19 4:52 ` bot+bpf-ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119040705.684987-3-atomlin@atomlin.com \
--to=atomlin@atomlin.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=bsegall@google.com \
--cc=casey@schaufler-ca.com \
--cc=cgroups@vger.kernel.org \
--cc=chenridong@huaweicloud.com \
--cc=chjohnst@gmail.com \
--cc=daniel@iogearbox.net \
--cc=dietmar.eggemann@arm.com \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hannes@cmpxchg.org \
--cc=ihor.solodrai@linux.dev \
--cc=jmorris@namei.org \
--cc=jolsa@kernel.org \
--cc=juri.lelli@redhat.com \
--cc=kees@kernel.org \
--cc=kprateek.nayak@amd.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=longman@redhat.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=mproche@gmail.com \
--cc=neelx@suse.com \
--cc=nick.lange@gmail.com \
--cc=omosnace@redhat.com \
--cc=paul@paul-moore.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=sean@ashe.io \
--cc=selinux@vger.kernel.org \
--cc=serge@hallyn.com \
--cc=song@kernel.org \
--cc=stephen.smalley.work@gmail.com \
--cc=steve@abita.co \
--cc=tj@kernel.org \
--cc=tsbogend@alpha.franken.de \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.