Re: [PATCH v5] security: Expand task_setscheduler LSM hook

From: Aaron Tomlin

Date: Wed Nov 18 2026 - 21:44:14 EST


On Thu, Nov 12, 2026 at 02:46:15PM -0800, Chris Down wrote:
> Hi Aaron,

Hi Chris,

I appreciate your feedback.

> Aaron Tomlin writes:
> > 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.)

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.

>
> > 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.

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.

>
> > -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
> bpf@xxxxxxxxxxxxxxx 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 :-)

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 bpf@xxxxxxxxxxxxxxx on v6.

>
> > if (setsched_check) {
> > - 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.

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.

>
> > + 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,
--
Aaron Tomlin