Skip to contentSkip to conversation

sched_ext: bpf_iter_scx_dsq_new() should always initialize iterator

PATCH Applied Tejun Heo 2 participants 3 messages May 5, 2025 – May 7, 2025 Scheduler

lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 sched_ext: bpf_iter_scx_dsq_new() should always initialize iterator

From 3ca42b7aea35cbcfb8d1fdde09e10a54edf97b26 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Mon, 5 May 2025 11:28:21 -1000

BPF programs may call next() and destroy() on BPF iterators even after new() returns an error value. bpf_iter_scx_dsq_new() could leave the iterator in an uninitialized state after an error return causing bpf_iter_scx_dsq_next() to dereference garbage data. Make bpf_iter_scx_dsq_new() always clear $kit->dsq so that next() and destroy() become noops.

Fixes 650ba21b131e ("sched_ext: Implement DSQ iterator")
Cc [email protected] # v6.12+
---
 kernel/sched/ext.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/sched/ext.c +6 -0
index 4e37b40ce280..f5133249fd4d 100644
@@ -6827,6 +6827,12 @@ __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id,
 	BUILD_BUG_ON(__alignof__(struct bpf_iter_scx_dsq_kern) !=
 		     __alignof__(struct bpf_iter_scx_dsq));
 
+	/*
+	 * next() and destroy() will be called regardless of the return value.
+	 * Always clear $kit->dsq.
+	 */
+	kit->dsq = NULL;
+
 	if (flags & ~__SCX_DSQ_ITER_USER_FLAGS)
 		return -EINVAL;
 
Signature · 1 line
2.49.0
Acked-by on v1 1/1
#1 lore raw
Quoted 22 lines · Tejun Heo

From 3ca42b7aea35cbcfb8d1fdde09e10a54edf97b26 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Mon, 5 May 2025 11:28:21 -1000

BPF programs may call next() and destroy() on BPF iterators even after new() returns an error value. bpf_iter_scx_dsq_new() could leave the iterator in an uninitialized state after an error return causing bpf_iter_scx_dsq_next() to dereference garbage data. Make bpf_iter_scx_dsq_new() always clear $kit->dsq so that next() and destroy() become noops.

Fixes 650ba21b131e ("sched_ext: Implement DSQ iterator")
Cc [email protected] # v6.12+
---
 kernel/sched/ext.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/sched/ext.c +0 -0
index 4e37b40ce280..f5133249fd4d 100644

kernel/sched/ext.c __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, · line 6832

Quoted 6 lines
kernel/sched/ext.c +3 -0
@@ -6827,6 +6827,12 @@ __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id,
 	BUILD_BUG_ON(__alignof__(struct bpf_iter_scx_dsq_kern) !=
 		     __alignof__(struct bpf_iter_scx_dsq));
 
+	/*
+	 * next() and destroy() will be called regardless of the return value.
+	 * Always clear $kit->dsq.

Nit (feel free to ignore): we could mention that bpf_for_each() is the one calling next() and destroy(), but overall looks good to me.

Thanks,
-Andrea

Quoted 8 lines
kernel/sched/ext.c +3 -0
+	 */
+	kit->dsq = NULL;
+
 	if (flags & ~__SCX_DSQ_ITER_USER_FLAGS)
 		return -EINVAL;
 
Signature · 1 line
2.49.0
author Acked-by Andrea Righi on v1 1/1 Applied
#2 lore raw
In reply to Tejun Heo

Applied to sched_ext/for-6.15-fixes with a small update to commit message as suggested by Andrea.

------ 8< ------
From 428dc9fc0873989d73918d4a9cc22745b7bbc799 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Mon, 5 May 2025 11:30:39 -1000
Subject: [PATCH] sched_ext: bpf_iter_scx_dsq_new() should always initialize
iterator

BPF programs may call next() and destroy() on BPF iterators even after new() returns an error value (e.g. bpf_for_each() macro ignores error returns from new()). bpf_iter_scx_dsq_new() could leave the iterator in an uninitialized state after an error return causing bpf_iter_scx_dsq_next() to dereference garbage data. Make bpf_iter_scx_dsq_new() always clear $kit->dsq so that next() and destroy() become noops.

Fixes 650ba21b131e ("sched_ext: Implement DSQ iterator")
Cc [email protected] # v6.12+
---
 kernel/sched/ext.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/sched/ext.c +6 -0
index 4e37b40ce280..f5133249fd4d 100644
@@ -6827,6 +6827,12 @@ __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id,
 	BUILD_BUG_ON(__alignof__(struct bpf_iter_scx_dsq_kern) !=
 		     __alignof__(struct bpf_iter_scx_dsq));
 
+	/*
+	 * next() and destroy() will be called regardless of the return value.
+	 * Always clear $kit->dsq.
+	 */
+	kit->dsq = NULL;
+
 	if (flags & ~__SCX_DSQ_ITER_USER_FLAGS)
 		return -EINVAL;
 
Signature · 1 line
2.49.0