Skip to contentSkip to conversation

security: Expand task_setscheduler LSM hook

PATCH Superseded Aaron Tomlin 4 participants 7 messages Nov 5 – Nov 19

Subsystems (10)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 security: Expand task_setscheduler LSM hook

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 requested CPU affinity mask (cpumask). Consequently, security modules are blind to both the target scheduling parameters and target CPUs, preventing granular access control decisions based on scheduling policy or spatial isolation.

In modern multi-tenant and real-time environments, CPU and scheduling isolation are critical boundaries. The inability to audit or restrict specific scheduling policy changes or CPU pinning requests limits the effectiveness of security policies, particularly when attempting to shield isolated or cryptographic cores from unprivileged or compromised tasks.

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 so that parameter-only updates (such as sched_setparam() or sched_setattr() with SCHED_FLAG_KEEP_POLICY) always pass the effective scheduling policy to the hook rather than an internal sentinel. Because this is a shared hook used across multiple paths, call sites that do not modify scheduling attributes or CPU affinity 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.

Historically, SELinux has governed all scheduling alterations under a single, monolithic PROCESS__SETSCHED access vector. By inspecting fields such as attr->sched_policy or attr->sched_priority, as well as the requested cpumask, a security module can introduce granular access controls, such as differentiating between standard time-sharing policies and latency-sensitive real-time policies.

This change updates all in-tree security modules (capabilities, SELinux, and Smack) to accommodate the new parameters mechanically, while providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

---
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/[email protected]/

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/[email protected]/

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/[email protected]/

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/[email protected]/

---
 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          |  7 +++++--
 security/commoncap.c             |  9 +++++++--
 security/security.c              | 14 +++++++++-----
 security/selinux/hooks.c         |  4 +++-
 security/smack/smack_lsm.c       | 13 +++++++++++--
 10 files changed, 53 insertions(+), 21 deletions(-)
arch/mips/kernel/mips-mt-fpaff.c +1 -1
index 4fead87d2f43..c7a5d55cf7ca 100644
@@ -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;
 
fs/proc/base.c +1 -1
index c9aebee0a338..d9a3c892586e 100644
@@ -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;
include/linux/lsm_hook_defs.h +3 -1
index 87d9d06710cb..411a5b977915 100644
@@ -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,
include/linux/security.h +11 -4
index 40831fab5470..b2c43e745447 100644
@@ -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)
kernel/cgroup/cpuset.c +2 -2
index 3219f927c35e..d122de948572 100644
@@ -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;
 
kernel/sched/syscalls.c +5 -2
index 45a127deeadd..1b3f81e6e18e 100644
@@ -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,8 @@ 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;
+		retval = security_task_setscheduler(p, &lsm_attr, NULL);
 		if (retval)
 			return retval;
 	}
@@ -1215,7 +1218,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;
 
security/commoncap.c +7 -2
index d47ab3022343..0c33f94a5707 100644
@@ -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 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);
 }
security/security.c +9 -5
index f6583d507395..9ca3438ef96a 100644
@@ -3261,17 +3261,21 @@ 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: requested CPU affinity mask, or NULL if not changing affinity
  *
- * 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.
  *
  * 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);
 }
 
 /**
security/selinux/hooks.c +3 -1
index 6168dd5f91c2..4fc3e8c51035 100644
@@ -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);
security/smack/smack_lsm.c +11 -2
index 28a8e12603ce..a5f0de59dfc0 100644
@@ -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 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__);
 }
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

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 requested CPU affinity mask (cpumask). Consequently, security modules are blind to both the target scheduling parameters and target CPUs, preventing granular access control decisions based on scheduling policy or spatial isolation.

In modern multi-tenant and real-time environments, CPU and scheduling isolation are critical boundaries. The inability to audit or restrict specific scheduling policy changes or CPU pinning requests limits the effectiveness of security policies, particularly when attempting to shield isolated or cryptographic cores from unprivileged or compromised tasks.

Quoted 7 lines

This patch expands the security_task_setscheduler() hook signature to [ ... ]
providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Aaron Tomlin
Quoted 34 lines · Aaron Tomlin

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 requested CPU affinity mask (cpumask). Consequently, security modules are blind to both the target scheduling parameters and target CPUs, preventing granular access control decisions based on scheduling policy or spatial isolation.

In modern multi-tenant and real-time environments, CPU and scheduling isolation are critical boundaries. The inability to audit or restrict specific scheduling policy changes or CPU pinning requests limits the effectiveness of security policies, particularly when attempting to shield isolated or cryptographic cores from unprivileged or compromised tasks.

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 so that parameter-only updates (such as sched_setparam() or sched_setattr() with SCHED_FLAG_KEEP_POLICY) always pass the effective scheduling policy to the hook rather than an internal sentinel. Because this is a shared hook used across multiple paths, call sites that do not modify scheduling attributes or CPU affinity 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.

Historically, SELinux has governed all scheduling alterations under a single, monolithic PROCESS__SETSCHED access vector. By inspecting fields such as attr->sched_policy or attr->sched_priority, as well as the requested cpumask, a security module can introduce granular access controls, such as differentiating between standard time-sharing policies and latency-sensitive real-time policies.

Quoted 6 lines

This change updates all in-tree security modules (capabilities, SELinux, and Smack) to accommodate the new parameters mechanically, while providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

This seems misleading - Casey explicitly said "for the smack bits". This makes it seem like he reviewed and approved of the whole set.

Quoted 2 lines

---

I see no issues with the commoncap.c and security.c changes, but as Paul said two versions ago in
https://lore.kernel.org/lkml/CAHC9VhSROg6RGUN4_ZVBoEwYjRnKvyjnkbx2D88c09KiTgY3KQ@mail.gmail.com/
nothing appaers to be using these. Where is the user?

Quoted 306 lines

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/[email protected]/

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/[email protected]/

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/[email protected]/

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/[email protected]/

---
 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          |  7 +++++--
 security/commoncap.c             |  9 +++++++--
 security/security.c              | 14 +++++++++-----
 security/selinux/hooks.c         |  4 +++-
 security/smack/smack_lsm.c       | 13 +++++++++++--
 10 files changed, 53 insertions(+), 21 deletions(-)
arch/mips/kernel/mips-mt-fpaff.c +1 -1
index 4fead87d2f43..c7a5d55cf7ca 100644
@@ -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;
 
fs/proc/base.c +1 -1
index c9aebee0a338..d9a3c892586e 100644
@@ -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;
include/linux/lsm_hook_defs.h +3 -1
index 87d9d06710cb..411a5b977915 100644
@@ -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,
include/linux/security.h +11 -4
index 40831fab5470..b2c43e745447 100644
@@ -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)
kernel/cgroup/cpuset.c +2 -2
index 3219f927c35e..d122de948572 100644
@@ -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;
 
kernel/sched/syscalls.c +5 -2
index 45a127deeadd..1b3f81e6e18e 100644
@@ -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,8 @@ 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;
+		retval = security_task_setscheduler(p, &lsm_attr, NULL);
 		if (retval)
 			return retval;
 	}
@@ -1215,7 +1218,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;
 
security/commoncap.c +7 -2
index d47ab3022343..0c33f94a5707 100644
@@ -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 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);
 }
security/security.c +9 -5
index f6583d507395..9ca3438ef96a 100644
@@ -3261,17 +3261,21 @@ 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: requested CPU affinity mask, or NULL if not changing affinity
  *
- * 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.
  *
  * 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);
 }
 
 /**
security/selinux/hooks.c +3 -1
index 6168dd5f91c2..4fc3e8c51035 100644
@@ -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);
security/smack/smack_lsm.c +11 -2
index 28a8e12603ce..a5f0de59dfc0 100644
@@ -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 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__);
 }
Signature · 1 line
2.55.0
author Reviewed-by Casey Schaufler
#3 lore raw
Quoted 9 lines · Serge E. Hallyn
Quoted 6 lines

This change updates all in-tree security modules (capabilities, SELinux, and Smack) to accommodate the new parameters mechanically, while providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

This seems misleading - Casey explicitly said "for the smack bits". This makes it seem like he reviewed and approved of the whole set.

Hi Serge,

Apologies for that, it was not my intention to misrepresent the scope of Casey's review. The qualifier line was dropped when collecting the trailer. I will update the tag in the next revision to explicitly scope it:

Quoted 4 lines

I see no issues with the commoncap.c and security.c changes, but as Paul said two versions ago in
https://lore.kernel.org/lkml/CAHC9VhSROg6RGUN4_ZVBoEwYjRnKvyjnkbx2D88c09KiTgY3KQ@mail.gmail.com/
nothing appaers to be using these. Where is the user?

The intended consumer is the BPF LSM (via the generated bpf_lsm_task_setscheduler() hook and the __nullable BTF annotations in include/linux/lsm_hook_defs.h). As discussed with Peter and Paul in the v3 thread [1], when sched_setaffinity() is called, the cpuset controller silently intersects and truncates the requested mask without surfacing the original request, and seccomp cannot safely dereference the __user cpumask or sched_attr pointers without TOCTOU races. Exposing the copied-in sched_attr and cpumask to task_setscheduler allows BPF LSM programs to audit or mediate the actual userspace request before it is sanitised or applied.

In my reply to Paul on v3 [1], I mentioned that once the MIPS prerequisite landed, I would include a concrete in-tree demonstration alongside the hook expansion, which I neglected to include in v4 and v5.

Would adding a BPF LSM selftest (under tools/testing/selftests/bpf/) that attaches to lsm/task_setscheduler and exercises both the sched_attr and cpumask parameters (including NULL and non-NULL paths) be sufficient as an in-tree user/test?

[1]: https://lore.kernel.org/lkml/ozsngetxq44lahategdi43sqrwdnpotsrebssczymqtkbh7snl@u6ytkznmpne5/

Kind regards,

Signature · 1 line
Aaron Tomlin
maintainer
#4 lore raw
Quoted 19 lines · Aaron Tomlin
Quoted 9 lines · Serge E. Hallyn
Quoted 6 lines

This change updates all in-tree security modules (capabilities, SELinux, and Smack) to accommodate the new parameters mechanically, while providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

This seems misleading - Casey explicitly said "for the smack bits". This makes it seem like he reviewed and approved of the whole set.

Hi Serge,

Apologies for that, it was not my intention to misrepresent the scope of Casey's review. The qualifier line was dropped when collecting the trailer. I will update the tag in the next revision to explicitly scope it:

Quoted 7 lines
Quoted 4 lines

I see no issues with the commoncap.c and security.c changes, but as Paul said two versions ago in
https://lore.kernel.org/lkml/CAHC9VhSROg6RGUN4_ZVBoEwYjRnKvyjnkbx2D88c09KiTgY3KQ@mail.gmail.com/
nothing appaers to be using these. Where is the user?

The intended consumer is the BPF LSM (via the generated bpf_lsm_task_setscheduler() hook and the __nullable BTF annotations in

Ah, I see, thanks.

Quoted 9 lines

include/linux/lsm_hook_defs.h). As discussed with Peter and Paul in the v3 thread [1], when sched_setaffinity() is called, the cpuset controller silently intersects and truncates the requested mask without surfacing the original request, and seccomp cannot safely dereference the __user cpumask or sched_attr pointers without TOCTOU races. Exposing the copied-in sched_attr and cpumask to task_setscheduler allows BPF LSM programs to audit or mediate the actual userspace request before it is sanitised or applied.

Quoted 8 lines

In my reply to Paul on v3 [1], I mentioned that once the MIPS prerequisite landed, I would include a concrete in-tree demonstration alongside the hook expansion, which I neglected to include in v4 and v5.

Would adding a BPF LSM selftest (under tools/testing/selftests/bpf/) that attaches to lsm/task_setscheduler and exercises both the sched_attr and cpumask parameters (including NULL and non-NULL paths) be sufficient as an in-tree user/test?

Self-tests are good :)

Quoted 5 lines

[1]: https://lore.kernel.org/lkml/ozsngetxq44lahategdi43sqrwdnpotsrebssczymqtkbh7snl@u6ytkznmpne5/

Kind regards,

Signature · 1 line
Aaron Tomlin
#5 lore raw
In reply to Aaron Tomlin

Hi Aaron,

Quoted 6 lines · Aaron Tomlin

In modern multi-tenant and real-time environments, CPU and scheduling isolation are critical boundaries. The inability to audit or restrict specific scheduling policy changes or CPU pinning requests limits the effectiveness of security policies, particularly when attempting to shield isolated or cryptographic cores from unprivileged or compromised tasks.

Peter objected to this paragraph on v3, and you said there that you would drop it. What you told him the patch is really for was auditing the affinity mask as userspace asked for it, before cpuset intersects it with the allowed CPUs, which seccomp can't do because the mask is behind a user pointer. That should be in the changelog in place of this paragraph. (I object to it as well. Please avoid such LLM grandiosity.)

Quoted 4 lines

This change updates all in-tree security modules (capabilities, SELinux, and Smack) to accommodate the new parameters mechanically, while providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

An LSM can only enforce an affinity policy through this hook if all possible ways of changing a task's affinity go through it, but writes to cpuset.cpus don't. With a BPF LSM program on this hook that rejects any mask containing CPU 3, sched_setaffinity() to CPU 3 gets EPERM. An unprivileged user with a delegated cgroup v2 subtree can still write 3 to the task's cpuset.cpus, though, and the task ends up on CPU 3 without the hook being called. cpuset_update_tasks_cpumask() just calls set_cpus_allowed_ptr() on every task in the cpuset.

So what this gives an LSM is visibility into sched_setaffinity() requests and cpuset migrations, not control over where a task runs. That may be enough for auditing, but the changelog shouldn't promise enforcement. It also looks like the kind of gap Paul meant on v3 when he asked how this fits into a more comprehensive approach to resource control in the LSM.

include/linux/lsm_hook_defs.h LSM_HOOK(int, 0, task_prlimit, const struct cred *cred, · line 261

Quoted 4 lines
include/linux/lsm_hook_defs.h +3 -1
-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)

Since the BPF LSM is the only thing that reads these, please Cc [email protected] and the BPF LSM maintainers (KP Singh and Matt Bobrowski) on the next version.

While the __nullable annotations do what you say, and a program that dereferences either argument without a NULL check is rejected by the verifier here, there is still a problem. Existing programs on this hook that follow Documentation/bpf/prog_lsm.rst and pass on the previous program's ret now get attr in that slot. One declared as

int BPF_PROG(x, struct task_struct *p, int ret)

that returns ret when it's non-zero loads and runs fine, but with this patch the verifier rejects it. LSM hooks aren't stable ABI, so that may well be fine, but the BPF folks should get to say so :-)

Review

Quoted 3 lines
		if (setsched_check) {
(quoted diff) +1 -1
-			ret = security_task_setscheduler(task);
+			ret = security_task_setscheduler(task, NULL, cs->effective_cpus);

The kerneldoc you add in security.c says @in_mask is NULL if affinity isn't changing, but that isn't what happens in cpuset_can_attach(). On v1 setsched_check is always true, so moving a task between two cpusets with the same cpus and mems still calls the hook with the destination's effective_cpus. With a v1 cpuset mount I see the hook get 0xf for a move between two cpusets that both have cpus 0-3, and Cpus_allowed_list stays at 0-3. On v2 the same happens for moves where only the mems differ.

The hook also can't tell this from a sched_setaffinity() call, since both pass a NULL attr and a mask, but here the mask is the destination cpuset's configuration, rather than something anyone asked to pin to. Please say in the kerneldoc what @in_mask means for cgroup migration and CLONE_INTO_CGROUP, so LSM authors don't treat it as a pinning request.

kernel/sched/syscalls.c int __sched_setscheduler(struct task_struct *p, · line 546

Quoted 2 lines
kernel/sched/syscalls.c +2 -0
+		lsm_attr.sched_policy = policy;
+		retval = security_task_setscheduler(p, &lsm_attr, NULL);

For sched_setparam() and SCHED_FLAG_KEEP_POLICY, the reset-on-fork setting that gets applied also comes from the task (reset_on_fork is set from p->sched_reset_on_fork above), but lsm_attr.sched_flags still belongs to the caller. On a task with SCHED_RESET_ON_FORK set, sched_setparam() reaches the hook with sched_flags of 0, and sched_getscheduler() still reports SCHED_RESET_ON_FORK afterwards. If lsm_attr is meant to show the hook what will be applied, SCHED_FLAG_RESET_ON_FORK should follow reset_on_fork as well.

Chris

#6 lore raw
Quoted 1 line · Chris Down

Hi Aaron,

Hi Chris,

I appreciate your feedback.

Quoted 8 lines
Quoted 6 lines · Aaron Tomlin

In modern multi-tenant and real-time environments, CPU and scheduling isolation are critical boundaries. The inability to audit or restrict specific scheduling policy changes or CPU pinning requests limits the effectiveness of security policies, particularly when attempting to shield isolated or cryptographic cores from unprivileged or compromised tasks.

Quoted 6 lines

Peter objected to this paragraph on v3, and you said there that you would drop it. What you told him the patch is really for was auditing the affinity mask as userspace asked for it, before cpuset intersects it with the allowed CPUs, which seccomp can't do because the mask is behind a user pointer. That should be in the changelog in place of this paragraph. (I object to it as well. Please avoid such LLM grandiosity.)

Apologies for the oversight in leaving that paragraph in after v3. I will drop it in v6 and replace it with the concrete rationale: auditing and filtering the raw user-supplied affinity mask and scheduling attributes before __sched_setaffinity() intersects the mask with the task's allowed cpuset CPUs, which seccomp cannot inspect because both are passed behind user pointers.

Quoted 15 lines
Quoted 5 lines

This change updates all in-tree security modules (capabilities, SELinux, and Smack) to accommodate the new parameters mechanically, while providing LSMs with the necessary context to enforce strict scheduling and affinity policies.

An LSM can only enforce an affinity policy through this hook if all possible ways of changing a task's affinity go through it, but writes to cpuset.cpus don't. With a BPF LSM program on this hook that rejects any mask containing CPU 3, sched_setaffinity() to CPU 3 gets EPERM. An unprivileged user with a delegated cgroup v2 subtree can still write 3 to the task's cpuset.cpus, though, and the task ends up on CPU 3 without the hook being called. cpuset_update_tasks_cpumask() just calls set_cpus_allowed_ptr() on every task in the cpuset.

Quoted 5 lines

So what this gives an LSM is visibility into sched_setaffinity() requests and cpuset migrations, not control over where a task runs. That may be enough for auditing, but the changelog shouldn't promise enforcement. It also looks like the kind of gap Paul meant on v3 when he asked how this fits into a more comprehensive approach to resource control in the LSM.

I see, fair point. While all user-initiated scheduling policy and parameter syscalls pass through __sched_setscheduler(), writes to cpuset.cpus update task affinity directly via cpuset_update_tasks_cpumask() -> set_cpus_allowed_ptr() without invoking security_task_setscheduler().

I will reword the commit message in v6 so that it does not claim comprehensive CPU placement enforcement, and instead accurately states that the expanded hook provides visibility into and mediation of explicit scheduling policy/parameter updates, sched_setaffinity() requests, and cpuset task migrations.

Quoted 16 lines

include/linux/lsm_hook_defs.h LSM_HOOK(int, 0, task_prlimit, const struct cred *cred, · line 261

Quoted 5 lines
include/linux/lsm_hook_defs.h +3 -1
-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)

Since the BPF LSM is the only thing that reads these, please Cc [email protected] and the BPF LSM maintainers (KP Singh and Matt Bobrowski) on the next version.

While the __nullable annotations do what you say, and a program that dereferences either argument without a NULL check is rejected by the verifier here, there is still a problem. Existing programs on this hook that follow Documentation/bpf/prog_lsm.rst and pass on the previous program's ret now get attr in that slot. One declared as

Quoted 5 lines

int BPF_PROG(x, struct task_struct *p, int ret)

that returns ret when it's non-zero loads and runs fine, but with this patch the verifier rejects it. LSM hooks aren't stable ABI, so that may well be fine, but the BPF folks should get to say so :-)

Agreed. Because BPF_LSM_MAC trampolines place the previous return value in argument slot nr_args, expanding the hook from one parameter to three shifts ret from slot 1 to slot 3, causing existing two-argument programs to fail verification in btf_ctx_access(). I will Cc [email protected] on v6.

Quoted 13 lines

Review

Quoted 4 lines
		if (setsched_check) {
(quoted diff) +1 -1
-			ret = security_task_setscheduler(task);
+			ret = security_task_setscheduler(task, NULL, cs->effective_cpus);

The kerneldoc you add in security.c says @in_mask is NULL if affinity isn't changing, but that isn't what happens in cpuset_can_attach(). On v1 setsched_check is always true, so moving a task between two cpusets with the same cpus and mems still calls the hook with the destination's effective_cpus. With a v1 cpuset mount I see the hook get 0xf for a move between two cpusets that both have cpus 0-3, and Cpus_allowed_list stays at 0-3. On v2 the same happens for moves where only the mems differ.

Quoted 5 lines

The hook also can't tell this from a sched_setaffinity() call, since both pass a NULL attr and a mask, but here the mask is the destination cpuset's configuration, rather than something anyone asked to pin to. Please say in the kerneldoc what @in_mask means for cgroup migration and CLONE_INTO_CGROUP, so LSM authors don't treat it as a pinning request.

Agreed. In cpuset_can_attach_check(), setsched_check is set whenever !cpuset_v2() || cpus_updated || mems_updated (provided oldcs has non-empty effective_cpus on v1), and cpuset_can_fork() likewise passes cs->effective_cpus whenever the destination cpuset differs from the parent's.

In v6, I will update the kerneldoc in security/security.c, including the parameter descriptions in commoncap.c and smack_lsm.c to explicitly document that @in_mask is the user-requested affinity mask for sched_setaffinity(), the destination cpuset's effective_cpus mask for cpuset cgroup migration and CLONE_INTO_CGROUP (even when only memory nodes differ or on cgroup v1 migrations with identical CPUs), and NULL for scheduling parameter updates or /proc/[pid]/timerslack_ns writes.

Quoted 12 lines

kernel/sched/syscalls.c int __sched_setscheduler(struct task_struct *p, · line 546

Quoted 3 lines
kernel/sched/syscalls.c +2 -0
+		lsm_attr.sched_policy = policy;
+		retval = security_task_setscheduler(p, &lsm_attr, NULL);

For sched_setparam() and SCHED_FLAG_KEEP_POLICY, the reset-on-fork setting that gets applied also comes from the task (reset_on_fork is set from p->sched_reset_on_fork above), but lsm_attr.sched_flags still belongs to the caller. On a task with SCHED_RESET_ON_FORK set, sched_setparam() reaches the hook with sched_flags of 0, and sched_getscheduler() still reports SCHED_RESET_ON_FORK afterwards. If lsm_attr is meant to show the hook what will be applied, SCHED_FLAG_RESET_ON_FORK should follow reset_on_fork as well.

Agreed. Since policy < 0 resolves reset_on_fork from p->sched_reset_on_fork for both sched_setparam() and SCHED_FLAG_KEEP_POLICY, lsm_attr.sched_flags should reflect that resolved state as well. In v6, I will synchronise SCHED_FLAG_RESET_ON_FORK in lsm_attr.sched_flags with reset_on_fork prior to invoking security_task_setscheduler():

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

Thank you for the thorough review.

Kind regards,

Signature · 1 line
Aaron Tomlin