* [PATCH] bpf: make css iterator stay drained after NULL
@ 2026-11-10 12:51 Kang Chen
2026-11-10 19:40 ` Tejun Heo
0 siblings, 1 reply; 2+ messages in thread
From: Kang Chen @ 2026-11-10 12:51 UTC (permalink / raw)
To: Yonghong Song, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: bpf, linux-kernel, Tejun Heo, Johannes Weiner, Michal Koutný,
cgroups
The css_next_*() helpers start from @start when given a NULL position.
bpf_iter_css_next() leaves kit->pos == NULL after the last element, but
keeps kit->start set, so a next() call after exhaustion starts the
iteration over from @start again.
The verifier relies on the iterator contract that once next() returns
NULL it keeps returning NULL: process_iter_next_call() marks the
iterator DRAINED and treats the result of later next() calls as the
constant zero. A restarted css iteration therefore hands the BPF
program a kernel pointer while the verifier believes R0 == 0.
Clear kit->start when the position becomes NULL. bpf_iter_css_destroy()
is a no-op, so no cleanup depends on @start after exhaustion.
Fixes: 7251d0905e75 ("bpf: Introduce css open-coded iterator kfuncs")
Signed-off-by: Kang Chen <k.chen@smail.nju.edu.cn>
---
kernel/bpf/cgroup_iter.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/kernel/bpf/cgroup_iter.c b/kernel/bpf/cgroup_iter.c
index fd51fe3d92cc..fb7ba1e45590 100644
--- a/kernel/bpf/cgroup_iter.c
+++ b/kernel/bpf/cgroup_iter.c
@@ -365,6 +365,16 @@ __bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *i
kit->pos = kit->pos ? kit->pos->parent : kit->start;
}
+ /*
+ * A NULL position means either "not started yet" or "drained" for
+ * css_next_*(), and all of them restart from @start when passed a
+ * NULL position. The verifier relies on an iterator which returned
+ * NULL to keep returning NULL (see process_iter_next_call()), so
+ * clear @start on exhaustion to make the next call return NULL.
+ */
+ if (!kit->pos)
+ kit->start = NULL;
+
return kit->pos;
}
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] bpf: make css iterator stay drained after NULL
2026-11-10 12:51 [PATCH] bpf: make css iterator stay drained after NULL Kang Chen
@ 2026-11-10 19:40 ` Tejun Heo
0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2026-11-10 19:40 UTC (permalink / raw)
To: Kang Chen
Cc: Yonghong Song, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, bpf,
linux-kernel, Johannes Weiner, Michal Koutný,
cgroups
On Tue, Nov 10, 2026 at 08:51:10PM +0800, Kang Chen wrote:
> The css_next_*() helpers start from @start when given a NULL position.
> bpf_iter_css_next() leaves kit->pos == NULL after the last element, but
> keeps kit->start set, so a next() call after exhaustion starts the
> iteration over from @start again.
>
> The verifier relies on the iterator contract that once next() returns
> NULL it keeps returning NULL: process_iter_next_call() marks the
> iterator DRAINED and treats the result of later next() calls as the
> constant zero. A restarted css iteration therefore hands the BPF
> program a kernel pointer while the verifier believes R0 == 0.
>
> Clear kit->start when the position becomes NULL. bpf_iter_css_destroy()
> is a no-op, so no cleanup depends on @start after exhaustion.
>
> Fixes: 7251d0905e75 ("bpf: Introduce css open-coded iterator kfuncs")
> Signed-off-by: Kang Chen <k.chen@smail.nju.edu.cn>
Acked-by: Tejun Heo <tj@kernel.org>
BPF folks, how do you want to route this? It can go through the bpf
tree or I can take it through cgroup/for-7.4-fixes.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-10 19:40 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-10 12:51 [PATCH] bpf: make css iterator stay drained after NULL Kang Chen
2026-11-10 19:40 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®