sched_ext: Fix ops->priv clobber on concurrent attach/detach
Subsystems (2)
Andrea Righi
reviewerPATCH v2 1/1 sched_ext: Fix ops->priv clobber on concurrent attach/detach
Under heavy concurrent attach/detach operations, scx_claim_exit() can trigger a NULL pointer dereference. This can be reproduced running the reload_loop kselftests inside a virtme-ng session:
$ vng -v -- ./tools/testing/selftests/sched_ext/runner -t reload_loop ... BUG: kernel NULL pointer dereference, address: 0000000000000400 RIP: 0010:scx_claim_exit+0x3b/0x120 Call Trace: <TASK> bpf_scx_unreg+0x45/0xb0 bpf_struct_ops_map_link_dealloc+0x39/0x50 bpf_link_release+0x18/0x20 __fput+0x10b/0x2e0 __x64_sys_close+0x47/0xa0
The underlying race (diagnosed by Tejun Heo) is a stomp of @ops->priv, not a missing NULL check:
T2 unreg(K) T1 reg(K)
----------- ---------
sch = ops->priv = sch_b800
scx_disable; flush_disable_work
[scx_root_disable: scx_root=NULL,
mutex_unlock, state=DISABLED]
mutex_lock; state ok
scx_alloc_and_add_sched:
ops->priv = sch_a800
scx_root = sch_a800; init=0
state=ENABLED; mutex_unlock
[flush returns] RCU_INIT_POINTER(ops->priv, NULL) <-- clobbers sch_a800 kobject_put(sch_b800)
T1 acquires scx_enable_mutex inside scx_root_disable()'s mutex_unlock window and starts a fresh attach on the same kdata, assigning sch_a800 to @ops->priv. T2 then continues out of scx_disable()/flush_disable_work and clobbers @ops->priv to NULL, leaking sch_a800; the bpf_link is gone but state stays SCX_ENABLED, so all future attaches fail with -EBUSY permanently. The next bpf_scx_unreg() on that kdata then reads NULL @ops->priv and dereferences it in scx_claim_exit().
Make @ops->priv the lifecycle binding: in scx_root_enable_workfn() and scx_sub_enable_workfn(), after the existing state check and still under scx_enable_mutex, refuse with -EBUSY if @ops->priv is non-NULL. This rejects an attempt to reuse a kdata that is still bound to a previous scheduler instance, closing the race without changing the unreg side.
---
Changes in v2:
- Address the root cause (a @ops->priv clobber during concurrent attach/detach) instead of masking the resulting NULL deref (Tejun Heo)
- Drop the v1 scx_prog_sched() fallback to scx_root and the NULL guard in bpf_scx_unreg().
- Reword the title to reflect the actual bug.
- Link to v1: https://lore.kernel.org/all/[email protected]
kernel/sched/ext.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
kernel/sched/ext.c +19 -0
@@ -6800,6 +6800,19 @@ static void scx_root_enable_workfn(struct kthread_work *work) goto err_unlock; } +/* +* @ops->priv binds @ops to its scx_sched instance. It is set here by +* scx_alloc_and_add_sched() and cleared at the tail of bpf_scx_unreg(), +* which runs after scx_root_disable() has dropped scx_enable_mutex. If +* it's still non-NULL here, a previous attachment on @ops has not +* finished tearing down; proceeding would let the in-flight unreg's +* RCU_INIT_POINTER(NULL) clobber the @ops->priv we are about to assign. +*/ +if (rcu_access_pointer (ops->priv)) { + ret = -EBUSY ; + goto err_unlock; + } + ret =alloc_kick_syncs ();if (ret) goto err_unlock;
@@ -7118,6 +7131,12 @@ static void scx_sub_enable_workfn(struct kthread_work *work) goto out_unlock; } +/* See scx_root_enable_workfn() for the @ops->priv check. */ +if (rcu_access_pointer (ops->priv)) { + ret = -EBUSY ; + goto out_unlock; + } + cgrp =cgroup_get_from_id (ops->sub_cgroup_id);if (IS_ERR (cgrp)) { ret =PTR_ERR (cgrp);
Signature · 1 line
2.54.0
Hello, Andrea.
Applied to sched_ext/for-7.1-fixes.
One followup if you have cycles: scx_alloc_and_add_sched() can still fail after rcu_assign_pointer(ops->priv, sch) (sub-sched kzalloc/kstrdup and kobject paths). With the new gate, that would leave the kdata permanently -EBUSY. Could probably be addressed by clearing @ops->priv on those error paths.
Thanks.
--
tejun
Andrea Righi
reviewerQuoted 9 lines · Tejun Heo
Hello, Andrea.
Applied to sched_ext/for-7.1-fixes.
One followup if you have cycles:
scx_alloc_and_add_sched()can still fail afterrcu_assign_pointer(ops->priv, sch)(sub-sched kzalloc/kstrdup and kobject paths). With the new gate, that would leave the kdata permanently-EBUSY. Could probably be addressed by clearing @ops->priv on those error paths.
Ah! Makes sense, I'll send a follow-up patch.
Thanks,
-Andrea
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Error paths in `scx_alloc_and_add_sched()` leave `ops->priv` as a dangling pointer, causing the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Error paths in scx_alloc_and_add_sched() leave ops->priv as a dangling pointer, causing the new -EBUSY check to permanently brick the BPF map and exposing a Use-After-Free. --
commit 451a33ccc662950a2e5e3da8cd4a0309ed7f2725
Author: Andrea Righi <[email protected]>
sched_ext: Fix ops->priv clobber on concurrent attach/detach
This commit addresses a NULL pointer dereference caused by a race condition during concurrent attach and detach operations. It adds a check to reject attempts to reuse a kdata that is still bound to a previous scheduler instance.
Quoted 9 lines
kernel/sched/ext.c +2
-0@@ -6733,6 +6733,19 @@ static void scx_root_enable_workfn(struct kthread_work *work) goto err_unlock; } +/* +* @ops->priv binds @ops to its scx_sched instance. It is set here by
kernel/sched/ext.c static void scx_root_enable_workfn(struct kthread_work *work) · line 6747
Quoted 10 lines
kernel/sched/ext.c +10
-0+* scx_alloc_and_add_sched() and cleared at the tail of bpf_scx_unreg(), +* which runs after scx_root_disable() has dropped scx_enable_mutex. If +* it's still non-NULL here, a previous attachment on @ops has not +* finished tearing down; proceeding would let the in-flight unreg's +* RCU_INIT_POINTER(NULL) clobber the @ops->priv we are about to assign. +*/ +if (rcu_access_pointer (ops->priv)) { + ret = -EBUSY ; + goto err_unlock; + }
If scx_alloc_and_add_sched() fails during a subsequent attachment attempt, does this leave ops->priv pointing to freed memory?
Looking at scx_alloc_and_add_sched(), the BPF operations map is bound to the new scheduler instance early:
rcu_assign_pointer(ops->priv, sch);
If a later initialization step in that function fails, such as allocating cgrp_path or failing kobject_init_and_add, the error paths free sch via kfree(sch) or kobject_put(&sch->kobj). However, the error paths do not reset ops->priv to NULL.
Because ops->priv retains the dangling pointer to the freed memory, will the new check added here permanently block any future attachments by always returning -EBUSY?
Could this dangling ops->priv pointer also be accessed by a BPF program running a sched_ext kfunc like scx_bpf_kick_cpu, resulting in a use-after-free when scx_prog_sched() retrieves and dereferences it?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1