Skip to contentSkip to conversation

sched_ext: Prevent null deref in scx_dsq_move()

PATCH In review Cheng-Yang Chou 3 participants 4 messages Feb 25 – Feb 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 9280381f8923..79d8184e4e11 100644
@@ -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
reviewer
#1 lore raw
Quoted 14 lines · Cheng-Yang Chou

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 +0 -0
index 9280381f8923..79d8184e4e11 100644

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
reviewer
#2 lore raw
Quoted 18 lines · Tejun Heo
Quoted 17 lines · Cheng-Yang Chou

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 +0 -0
index 9280381f8923..79d8184e4e11 100644
@@ -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

#3 lore raw
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_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.

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

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