security: Expand task_setscheduler LSM hook
v6 cover security: Expand task_setscheduler LSM hook
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/[email protected]/
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]/
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(-)
Signature · 1 line
2.55.0
PATCH v6 1/2 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 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.
--- 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(-)
arch/mips/kernel/mips-mt-fpaff.c +1 -1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 +9 -2
@@ -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;
security/commoncap.c +7 -2
@@ -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); }
security/security.c +16 -5
@@ -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); }/**
security/selinux/hooks.c +3 -1
@@ -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
@@ -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__); }
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 14 lines
At present, the
task_setschedulerLSM hook only accepts the targettask_structpointer. 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.
Quoted 7 lines
This patch expands the
security_task_setscheduler()hook signature to [ ... ]
mediation of explicit scheduling policy and parameter updates,sched_setaffinity()requests, and cpuset task migrations.Reviewed-by Casey Schaufler <[email protected]> # SmackSigned-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v6 2/2 selftests/bpf: Verify task_setscheduler LSM hook parameters
In reply to Aaron TomlinExtend 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.
--- .../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(+)
tools/testing/selftests/bpf/prog_tests/test_lsm.c +164 -0
@@ -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 ; }
tools/testing/selftests/bpf/progs/lsm.c +51 -0
69 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_lsm.c +49 -0
55 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
Extend the BPF LSM selftest to attach to the
task_setschedulerhook and exercise scheduling policy updates viasched_setscheduler(), parameter updates viasched_setparam()andsched_setattr()withSCHED_FLAG_KEEP_POLICY, CPU affinity changes viasched_setaffinity(), and child timer slack updates via /proc/[pid]/timerslack_ns. Verify that the hook observes the expectedsched_attrand cpumask arguments across each path, including the resolved scheduling policy and effectiveSCHED_FLAG_RESET_ON_FORKstate for parameter-only updates as well as NULL pointers when an argument does not apply, and that returning-EPERMdenies each operation.
Quoted 5 lines
Also add verifier test cases confirming that dereferencing the nullable attr or
in_maskparameters without an explicit NULL check is rejected by the BPF verifier.Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v6 2/2] selftests/bpf: Verify task_setscheduler LSM hook parameters
In reply to Aaron TomlinTL;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.
Quoted 1 line
selftests/bpf: Verify
task_setschedulerLSM 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.
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c +0
-0
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c +2
-0+static int test_lsm_task_setscheduler (struct lsm *skel) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/test_lsm.c static int test_lsm_inode_idmap(struct lsm *skel) · line 312
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c +12
-0+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?
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c +2
-0+out: + skel->bss->setsched_deny =0 ;
[ ... ]
tools/testing/selftests/bpf/prog_tests/test_lsm.c static int test_lsm_inode_idmap(struct lsm *skel) · line 413
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c +4
-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