sched_ext: Prevent null deref in scx_dsq_move()
Subsystems (2)
PATCH 1/1 sched_ext: Prevent null deref in scx_dsq_move()
Add a null check for the sch pointer at the start of scx_dsq_move(). This prevents a panic if the function is called when the sched_ext scheduler is not active.
--- kernel/sched/ext.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/ext.c +3 -0
@@ -5990,6 +5990,9 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,bool in_balance;unsigned long flags; +if (unlikely (!sch)) +return false; +if (!scx_kf_allowed_if_unlocked () && !scx_kf_allowed (sch,SCX_KF_DISPATCH ))return false;
Signature · 1 line
2.48.1
Tejun Heo
reviewerQuoted 14 lines · Cheng-Yang Chou
Add a null check for the
schpointer at the start ofscx_dsq_move(). This prevents a panic if the function is called when thesched_extscheduler is not active.Signed-off-by Cheng-Yang Chou <[email protected]>--- kernel/sched/ext.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/ext.c +0
-0
kernel/sched/ext.c static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, · line 5994
Quoted 5 lines
kernel/sched/ext.c +2
-0@@ -5990,6 +5990,9 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,bool in_balance;unsigned long flags; +if (unlikely (!sch)) +return false;
But sch is from src_dsq->sched. Can you describe a scenario where that could be NULL or do you have repro?
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerQuoted 18 lines · Tejun Heo
Quoted 17 lines · Cheng-Yang Chou
Add a null check for the
schpointer at the start ofscx_dsq_move(). This prevents a panic if the function is called when thesched_extscheduler is not active.Signed-off-by Cheng-Yang Chou <[email protected]>--- kernel/sched/ext.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/ext.c +0
-0@@ -5990,6 +5990,9 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit,bool in_balance;unsigned long flags;
Quoted 5 lines
kernel/sched/ext.c static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, · line 5994
Quoted 2 lines
kernel/sched/ext.c +2
-0+if (unlikely (!sch)) +return false;But sch is from
src_dsq->sched. Can you describe a scenario where that could be NULL or do you have repro?
Yeah, scx_dsq_move can only be invoked from scx_bpf_dsq_move() or scx_bpf_dsq_move_vtime(), which in turn may only be called from a BPF scheduler callback.
If the scheduler is inactive, this condition should theoretically never occur. Even during teardown, all tasks are migrated and no CPU can still be executing a BPF callback by the time scx_root is set back to NULL. So, I don't see how sch can be NULL at this point, unless we have a reproducer...
Thanks,
-Andrea
Quoted 7 lines · Andrea Righi
Quoted 2 lines
But sch is from
src_dsq->sched. Can you describe a scenario where that could be NULL or do you have repro?Yeah,
scx_dsq_movecan only be invoked fromscx_bpf_dsq_move()orscx_bpf_dsq_move_vtime(), which in turn may only be called from a BPF scheduler callback.
Quoted 7 lines
If the scheduler is inactive, this condition should theoretically never occur. Even during teardown, all tasks are migrated and no CPU can still be executing a BPF callback by the time
scx_rootis set back to NULL. So, I don't see how sch can be NULL at this point, unless we have a reproducer...Thanks,
-Andrea
I added this purely from a defensive programming perspective, missing the fact that the teardown phase guarantees all tasks are migrated before scx_root is cleared.
I don't have a reproducer. Since the architecture prevents this scenario, the check is indeed unnecessary.
Please drop this patch.
Thanks for the detailed explanation.
Thanks,
Cheng-Yang