BPF List
 help / color / mirror / Atom feed
* [PATCH v6 0/2] security: Expand task_setscheduler LSM hook
@ 2026-11-19  4:07 Aaron Tomlin
  2026-11-19  4:07 ` [PATCH v6 1/2] " Aaron Tomlin
  2026-11-19  4:07 ` [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters Aaron Tomlin
  0 siblings, 2 replies; 4+ messages in thread
From: Aaron Tomlin @ 2026-11-19  4:07 UTC (permalink / raw)
  To: tsbogend, paul, jmorris, serge, andrii, ast, daniel, eddyz87,
	memxor, mingo, peterz, juri.lelli, vincent.guittot,
	stephen.smalley.work, casey, longman, tj, hannes, mkoutny
  Cc: ihor.solodrai, martin.lau, song, yonghong.song, jolsa, emil,
	chenridong, dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
	kprateek.nayak, omosnace, kees, atomlin, neelx, sean, chjohnst,
	steve, mproche, nick.lange, cgroups, linux-mips, linux-fsdevel,
	linux-security-module, selinux, bpf, linux-kselftest,
	linux-kernel

This series expands the task_setscheduler LSM hook to accept nullable
pointers to the requested sched_attr and cpumask alongside the target
task_struct. For sched_setaffinity(), passing the raw user-supplied
affinity mask allows LSMs such as the BPF LSM to audit or filter the
mask as requested by userspace before __sched_setaffinity() intersects
it with the task's allowed cpuset CPUs, which seccomp filters cannot
inspect behind user pointers and scheduler tracepoints only observe
post-intersection. Similarly, __sched_setscheduler() passes a local
sched_attr populated with the resolved scheduling policy and effective
SCHED_FLAG_RESET_ON_FORK state so parameter-only updates expose the
effective values rather than internal sentinels or caller defaults, and
cpuset task migrations and CLONE_INTO_CGROUP pass the destination
cpuset's effective_cpus mask (note that direct writes to cpuset.cpus
update task affinity via cpuset_update_tasks_cpumask() without invoking
this hook).

Because BPF_LSM_MAC trampolines place the previous return value in
context argument slot nr_args, expanding task_setscheduler from one
parameter to three shifts ret from slot 1 to slot 3; existing
two-argument BPF_PROG(name, struct task_struct *p, int ret) programs on
lsm/task_setscheduler will therefore be rejected by the verifier until
updated to the expanded signature. Patch 2 extends the BPF LSM
selftests to verify the hook's runtime arguments and -EPERM mediation
across sched_setscheduler(), sched_setparam(), sched_setattr() with
SCHED_FLAG_KEEP_POLICY, sched_setaffinity(), and
/proc/[pid]/timerslack_ns, alongside verifier checks for the __nullable
annotations.

Changes since v5:

 - Replaced the second paragraph of Patch 1's commit message with the
   concrete rationale for inspecting the user-requested affinity mask
   and scheduling attributes prior to cpuset intersection, and clarified
   the scope of affinity mediation relative to cpuset.cpus writes
   (Chris Down, Peter Zijlstra)

 - In __sched_setscheduler(), synchronised SCHED_FLAG_RESET_ON_FORK in
   lsm_attr.sched_flags with the resolved reset_on_fork value so
   parameter-only updates accurately reflect the task's effective
   reset-on-fork state (Chris Down)

 - Clarified the kerneldoc for security_task_setscheduler() to document
   what @in_mask represents for sched_setaffinity(), cpuset cgroup
   migrations, and CLONE_INTO_CGROUP (Chris Down)

 - Added Patch 2 to extend the BPF LSM runtime and verifier selftests
   for the expanded task_setscheduler hook

 - Link to v5: https://lore.kernel.org/lkml/20261105172210.76706-1-atomlin@atomlin.com/

Changes since v4:

 - Rebased against v7.4-rc1

 - In __sched_setscheduler(), populate a local copy of sched_attr with
   the resolved scheduling policy before calling
   security_task_setscheduler() so parameter-only updates (such as
   sched_setparam() or sched_setattr() with SCHED_FLAG_KEEP_POLICY) pass
   the effective policy rather than SETPARAM_POLICY (-1)

 - Clarified the commit message to reflect that both the sched_attr and
   cpumask parameters are introduced by this patch

 - Link to v4: https://lore.kernel.org/all/20260720014156.459386-1-atomlin@atomlin.com/

Changes since v3:

 - Expanded the task_setscheduler LSM hook to accept the 'sched_attr'
   payload, enabling BPF and other LSMs to inspect scheduling attributes
   (Peter Zijlstra)

 - Updated all call sites for security_task_setscheduler() to correctly
   pass the attr structure when available, or NULL otherwise
   (Peter Zijlstra)

 - Added the necessary forward declaration for struct sched_attr to
   include/linux/security.h

 - Link to v3: https://lore.kernel.org/lkml/20260526142838.774711-1-atomlin@atomlin.com/

Changes since v2:

 - Dropped patch 1. This is to be addressed by the cgroup cpuset
   maintainer (Waiman Long)

 - Dropped patch 3. Will be submitted as a separate patch (Paul Moore)

 - Link to v2: https://lore.kernel.org/lkml/20260509213803.968464-1-atomlin@atomlin.com/

Changes since v1:

 - Reordered the allocation and user-copy of new_mask in the MIPS
   architecture's mipsmt_sys_sched_setaffinity() to occur before the
   LSM hook is invoked. This ensures the security modules evaluate a fully
   populated mask rather than uninitialised memory, while cleanly handling
   error unwinding

 - Updated cpuset_can_fork() to pass the destination cpuset's effective CPU
   mask instead of NULL

 - Link to v1: https://lore.kernel.org/lkml/20260509164847.939294-1-atomlin@atomlin.com/

Aaron Tomlin (2):
  security: Expand task_setscheduler LSM hook
  selftests/bpf: Verify task_setscheduler LSM hook parameters

 arch/mips/kernel/mips-mt-fpaff.c              |   2 +-
 fs/proc/base.c                                |   2 +-
 include/linux/lsm_hook_defs.h                 |   4 +-
 include/linux/security.h                      |  15 +-
 kernel/cgroup/cpuset.c                        |   4 +-
 kernel/sched/syscalls.c                       |  11 +-
 security/commoncap.c                          |   9 +-
 security/security.c                           |  21 ++-
 security/selinux/hooks.c                      |   4 +-
 security/smack/smack_lsm.c                    |  13 +-
 .../selftests/bpf/prog_tests/test_lsm.c       | 164 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/lsm.c       |  51 ++++++
 .../selftests/bpf/progs/verifier_lsm.c        |  49 ++++++
 13 files changed, 328 insertions(+), 21 deletions(-)

-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v6 1/2] security: Expand task_setscheduler LSM hook
  2026-11-19  4:07 [PATCH v6 0/2] security: Expand task_setscheduler LSM hook Aaron Tomlin
@ 2026-11-19  4:07 ` Aaron Tomlin
  2026-11-19  4:07 ` [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters Aaron Tomlin
  1 sibling, 0 replies; 4+ messages in thread
From: Aaron Tomlin @ 2026-11-19  4:07 UTC (permalink / raw)
  To: tsbogend, paul, jmorris, serge, andrii, ast, daniel, eddyz87,
	memxor, mingo, peterz, juri.lelli, vincent.guittot,
	stephen.smalley.work, casey, longman, tj, hannes, mkoutny
  Cc: ihor.solodrai, martin.lau, song, yonghong.song, jolsa, emil,
	chenridong, dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
	kprateek.nayak, omosnace, kees, atomlin, neelx, sean, chjohnst,
	steve, mproche, nick.lange, cgroups, linux-mips, linux-fsdevel,
	linux-security-module, selinux, bpf, linux-kselftest,
	linux-kernel

At present, the task_setscheduler LSM hook only accepts the target
task_struct pointer. It lacks visibility into the requested scheduling
attributes (sched_attr) and the CPU affinity or cpuset mask (cpumask).
Consequently, security modules cannot inspect the target scheduling
parameters or CPU mask passed through the hook.

For CPU affinity requests via sched_setaffinity(), auditing or
filtering the raw mask requested by userspace requires inspecting the
mask before __sched_setaffinity() intersects it with the task's allowed
cpuset CPUs. Seccomp filters cannot perform this inspection because the
affinity mask and scheduling attribute structures are passed behind
user pointers, and scheduler tracepoints only observe the effective
mask after intersection with the cpuset configuration.

This patch expands the security_task_setscheduler() hook signature to
include pointers to the requested sched_attr and cpumask alongside the
target task_struct. In __sched_setscheduler(), a local copy of
sched_attr is populated with the resolved scheduling policy and
effective SCHED_FLAG_RESET_ON_FORK state so that parameter-only updates
(such as sched_setparam() or sched_setattr() with
SCHED_FLAG_KEEP_POLICY) always pass the effective values to the hook
rather than internal sentinels or caller defaults. For
sched_setaffinity(), the user-requested affinity mask is passed, whilst
for cpuset migrations and CLONE_INTO_CGROUP the destination cpuset's
effective_cpus mask is passed. Note that direct writes to cpuset.cpus
update task affinity via cpuset_update_tasks_cpumask() without invoking
this hook. Call sites that do not provide scheduling attributes or a
CPU mask (such as /proc/[pid]/timerslack_ns writes) pass NULL for the
respective parameter. To protect against unverified dereferences, both
parameters are annotated with __nullable in the LSM hook definition,
ensuring the BPF verifier mandates explicit NULL checks for attached
eBPF programs.

This change updates all in-tree security modules (capabilities,
SELinux, and Smack) to accommodate the new parameters mechanically,
whilst providing LSMs such as the BPF LSM with visibility into and
mediation of explicit scheduling policy and parameter updates,
sched_setaffinity() requests, and cpuset task migrations.

Reviewed-by: Casey Schaufler <casey@schaufler-ca.com> # Smack
Signed-off-by: Aaron Tomlin <atomlin@atomlin.com>
---
 arch/mips/kernel/mips-mt-fpaff.c |  2 +-
 fs/proc/base.c                   |  2 +-
 include/linux/lsm_hook_defs.h    |  4 +++-
 include/linux/security.h         | 15 +++++++++++----
 kernel/cgroup/cpuset.c           |  4 ++--
 kernel/sched/syscalls.c          | 11 +++++++++--
 security/commoncap.c             |  9 +++++++--
 security/security.c              | 21 ++++++++++++++++-----
 security/selinux/hooks.c         |  4 +++-
 security/smack/smack_lsm.c       | 13 +++++++++++--
 10 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/arch/mips/kernel/mips-mt-fpaff.c b/arch/mips/kernel/mips-mt-fpaff.c
index 4fead87d2f43..c7a5d55cf7ca 100644
--- a/arch/mips/kernel/mips-mt-fpaff.c
+++ b/arch/mips/kernel/mips-mt-fpaff.c
@@ -110,7 +110,7 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,
 		goto out_unlock;
 	}
 
-	retval = security_task_setscheduler(p);
+	retval = security_task_setscheduler(p, NULL, new_mask);
 	if (retval)
 		goto out_unlock;
 
diff --git a/fs/proc/base.c b/fs/proc/base.c
index c9aebee0a338..d9a3c892586e 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2596,7 +2596,7 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
 		}
 		rcu_read_unlock();
 
-		err = security_task_setscheduler(p);
+		err = security_task_setscheduler(p, NULL, NULL);
 		if (err) {
 			count = err;
 			goto out;
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index 87d9d06710cb..411a5b977915 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -256,7 +256,9 @@ LSM_HOOK(int, 0, task_prlimit, const struct cred *cred,
 	 const struct cred *tcred, unsigned int flags)
 LSM_HOOK(int, 0, task_setrlimit, struct task_struct *p, unsigned int resource,
 	 struct rlimit *new_rlim)
-LSM_HOOK(int, 0, task_setscheduler, struct task_struct *p)
+LSM_HOOK(int, 0, task_setscheduler, struct task_struct *p,
+	 const struct sched_attr *attr__nullable,
+	 const struct cpumask *in_mask__nullable)
 LSM_HOOK(int, 0, task_getscheduler, struct task_struct *p)
 LSM_HOOK(int, 0, task_movememory, struct task_struct *p)
 LSM_HOOK(int, 0, task_kill, struct task_struct *p, struct kernel_siginfo *info,
diff --git a/include/linux/security.h b/include/linux/security.h
index 40831fab5470..b2c43e745447 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -84,6 +84,7 @@ struct audit_krule;
 struct ns_common;
 struct user_namespace;
 struct timezone;
+struct sched_attr;
 
 enum lsm_event {
 	LSM_POLICY_CHANGE,
@@ -198,7 +199,9 @@ extern int cap_mmap_addr(unsigned long addr);
 extern int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags);
 extern int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 			  unsigned long arg4, unsigned long arg5);
-extern int cap_task_setscheduler(struct task_struct *p);
+extern int cap_task_setscheduler(struct task_struct *p,
+				 const struct sched_attr *attr,
+				 const struct cpumask *in_mask);
 extern int cap_task_setioprio(struct task_struct *p, int ioprio);
 extern int cap_task_setnice(struct task_struct *p, int nice);
 extern int cap_vm_enough_memory(struct mm_struct *mm, long pages);
@@ -537,7 +540,9 @@ int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
 			  unsigned int flags);
 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
 		struct rlimit *new_rlim);
-int security_task_setscheduler(struct task_struct *p);
+int security_task_setscheduler(struct task_struct *p,
+			       const struct sched_attr *attr,
+			       const struct cpumask *in_mask);
 int security_task_getscheduler(struct task_struct *p);
 int security_task_movememory(struct task_struct *p);
 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
@@ -1408,9 +1413,11 @@ static inline int security_task_setrlimit(struct task_struct *p,
 	return 0;
 }
 
-static inline int security_task_setscheduler(struct task_struct *p)
+static inline int security_task_setscheduler(struct task_struct *p,
+					     const struct sched_attr *attr,
+					     const struct cpumask *in_mask)
 {
-	return cap_task_setscheduler(p);
+	return cap_task_setscheduler(p, attr, in_mask);
 }
 
 static inline int security_task_getscheduler(struct task_struct *p)
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 3219f927c35e..d122de948572 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -3267,7 +3267,7 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
 			attach_ctx.old_cs = task_cs(task);
 
 		if (setsched_check) {
-			ret = security_task_setscheduler(task);
+			ret = security_task_setscheduler(task, NULL, cs->effective_cpus);
 			if (ret)
 				goto out_unlock;
 		}
@@ -3816,7 +3816,7 @@ static int cpuset_can_fork(struct task_struct *task, struct css_set *cset)
 	if (ret)
 		goto out_unlock;
 
-	ret = security_task_setscheduler(task);
+	ret = security_task_setscheduler(task, NULL, cs->effective_cpus);
 	if (ret)
 		goto out_unlock;
 
diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 45a127deeadd..16c97553e5ff 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -533,6 +533,8 @@ int __sched_setscheduler(struct task_struct *p,
 		return -EINVAL;
 
 	if (user) {
+		struct sched_attr lsm_attr = *attr;
+
 		retval = user_check_sched_setscheduler(p, attr, policy, reset_on_fork);
 		if (retval)
 			return retval;
@@ -540,7 +542,12 @@ int __sched_setscheduler(struct task_struct *p,
 		if (attr->sched_flags & SCHED_FLAG_SUGOV)
 			return -EINVAL;
 
-		retval = security_task_setscheduler(p);
+		lsm_attr.sched_policy = policy;
+		if (reset_on_fork)
+			lsm_attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
+		else
+			lsm_attr.sched_flags &= ~SCHED_FLAG_RESET_ON_FORK;
+		retval = security_task_setscheduler(p, &lsm_attr, NULL);
 		if (retval)
 			return retval;
 	}
@@ -1215,7 +1222,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
 			return -EPERM;
 	}
 
-	retval = security_task_setscheduler(p);
+	retval = security_task_setscheduler(p, NULL, in_mask);
 	if (retval)
 		return retval;
 
diff --git a/security/commoncap.c b/security/commoncap.c
index d47ab3022343..7a9a5091d761 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -1222,13 +1222,18 @@ static int cap_safe_nice(struct task_struct *p)
 /**
  * cap_task_setscheduler - Determine if scheduler policy change is permitted
  * @p: The task to affect
+ * @attr: Requested scheduling attributes (ignored)
+ * @in_mask: Requested CPU affinity or destination cpuset mask (ignored)
  *
  * Determine if the requested scheduler policy change is permitted for the
- * specified task.
+ * specified task. The capabilities security module does not evaluate the
+ * @attr or @in_mask parameters, relying solely on cap_safe_nice().
  *
  * Return: 0 if permission is granted, -ve if denied.
  */
-int cap_task_setscheduler(struct task_struct *p)
+int cap_task_setscheduler(struct task_struct *p,
+			  const struct sched_attr *attr __always_unused,
+			  const struct cpumask *in_mask __always_unused)
 {
 	return cap_safe_nice(p);
 }
diff --git a/security/security.c b/security/security.c
index f6583d507395..ebb06f1044e5 100644
--- a/security/security.c
+++ b/security/security.c
@@ -3261,17 +3261,28 @@ int security_task_setrlimit(struct task_struct *p, unsigned int resource,
 }
 
 /**
- * security_task_setscheduler() - Check if setting sched policy/param is allowed
+ * security_task_setscheduler() - Check if setting sched policy/param/affinity is allowed
  * @p: target task
+ * @attr: requested scheduling attributes, or NULL if not changing parameters
+ * @in_mask: user-requested CPU affinity mask for sched_setaffinity(), the
+ *           destination cpuset's effective_cpus mask for cpuset cgroup
+ *           migration and CLONE_INTO_CGROUP, or NULL otherwise
  *
- * Check permission before setting scheduling policy and/or parameters of
- * process @p.
+ * Check permission before setting the scheduling policy, parameters, and/or
+ * CPU affinity of process @p, or migrating/cloning @p into a cpuset. Note
+ * that during cpuset migration and CLONE_INTO_CGROUP, @in_mask reflects the
+ * destination cpuset's effective CPU configuration rather than a per-task
+ * pinning request, and may be passed even when the effective CPU mask itself
+ * does not change (for example, on cgroup v1 migrations or when only memory
+ * nodes change on cgroup v2).
  *
  * Return: Returns 0 if permission is granted.
  */
-int security_task_setscheduler(struct task_struct *p)
+int security_task_setscheduler(struct task_struct *p,
+			       const struct sched_attr *attr,
+			       const struct cpumask *in_mask)
 {
-	return call_int_hook(task_setscheduler, p);
+	return call_int_hook(task_setscheduler, p, attr, in_mask);
 }
 
 /**
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 6168dd5f91c2..4fc3e8c51035 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -4690,7 +4690,9 @@ static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
 	return 0;
 }
 
-static int selinux_task_setscheduler(struct task_struct *p)
+static int selinux_task_setscheduler(struct task_struct *p,
+				     const struct sched_attr *attr __always_unused,
+				     const struct cpumask *in_mask __always_unused)
 {
 	return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
 			    PROCESS__SETSCHED, NULL);
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 28a8e12603ce..61574ee35c85 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -2350,10 +2350,19 @@ static int smack_task_getioprio(struct task_struct *p)
 /**
  * smack_task_setscheduler - Smack check on setting scheduler
  * @p: the task object
+ * @attr: Requested scheduling attributes (ignored)
+ * @in_mask: Requested CPU affinity or destination cpuset mask (ignored)
  *
- * Return 0 if read access is permitted
+ * Evaluate whether the current task has write access to the target task @p
+ * to change its scheduling policy. The Smack security module relies
+ * strictly on label-based access control and does not evaluate CPU
+ * affinity masks or scheduling attributes.
+ *
+ * Return: 0 if write access is permitted
  */
-static int smack_task_setscheduler(struct task_struct *p)
+static int smack_task_setscheduler(struct task_struct *p,
+				   const struct sched_attr *attr __always_unused,
+				   const struct cpumask *in_mask __always_unused)
 {
 	return smk_curacc_on_task(p, MAY_WRITE, __func__);
 }
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters
  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:07 ` Aaron Tomlin
  2026-11-19  4:52   ` bot+bpf-ci
  1 sibling, 1 reply; 4+ messages in thread
From: Aaron Tomlin @ 2026-11-19  4:07 UTC (permalink / raw)
  To: tsbogend, paul, jmorris, serge, andrii, ast, daniel, eddyz87,
	memxor, mingo, peterz, juri.lelli, vincent.guittot,
	stephen.smalley.work, casey, longman, tj, hannes, mkoutny
  Cc: ihor.solodrai, martin.lau, song, yonghong.song, jolsa, emil,
	chenridong, dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
	kprateek.nayak, omosnace, kees, atomlin, neelx, sean, chjohnst,
	steve, mproche, nick.lange, cgroups, linux-mips, linux-fsdevel,
	linux-security-module, selinux, bpf, linux-kselftest,
	linux-kernel

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, &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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters
  2026-11-19  4:07 ` [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters Aaron Tomlin
@ 2026-11-19  4:52   ` bot+bpf-ci
  0 siblings, 0 replies; 4+ messages in thread
From: bot+bpf-ci @ 2026-11-19  4:52 UTC (permalink / raw)
  To: atomlin, tsbogend, paul, jmorris, serge, andrii, ast, daniel,
	eddyz87, memxor, mingo, peterz, juri.lelli, vincent.guittot,
	stephen.smalley.work, casey, longman, tj, hannes, mkoutny
  Cc: ihor.solodrai, martin.lau, song, yonghong.song, jolsa, emil,
	chenridong, dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
	kprateek.nayak, omosnace, kees, atomlin, neelx, sean, chjohnst,
	steve, mproche, nick.lange, cgroups, linux-mips, linux-fsdevel,
	linux-security-module, selinux, bpf, linux-kselftest,
	linux-kernel, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 1610 bytes --]

> selftests/bpf: Verify task_setscheduler LSM hook parameters

TL;DR: Nit only, in the new task_setscheduler selftest: the restore_sched flag
and the saved_policy initializer look redundant, so the scheduler restore at
out: could be unconditional.

> diff --git a/tools/testing/selftests/bpf/prog_tests/test_lsm.c b/tools/testing/selftests/bpf/prog_tests/test_lsm.c
> --- a/tools/testing/selftests/bpf/prog_tests/test_lsm.c
> +++ b/tools/testing/selftests/bpf/prog_tests/test_lsm.c

[ ... ]

> +static int test_lsm_task_setscheduler(struct lsm *skel)
> +{

[ ... ]

> +	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;

this isn't a bug, but since every "goto out" happens after
"restore_sched = true", could the restore_sched flag (and the
"saved_policy = -1" initializer, which is overwritten by the next
statement) be dropped and the restore at out: done unconditionally?

[ ... ]

> +out:
> +	skel->bss->setsched_deny = 0;

[ ... ]

> +	if (restore_sched)
> +		sched_setscheduler(0, saved_policy, &saved_param);
> +	return ret;
> +}


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44458173002

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-19  4:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:07 ` [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters Aaron Tomlin
2026-11-19  4:52   ` bot+bpf-ci

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox