Skip to contentSkip to conversation

bpf: Compare iterator types during state pruning

PATCH Applied Ning Ding 4 participants 6 messages Aug 11

Subsystems (3)
lore
Share & tools
lore

Ning Ding

author
#0 lore raw

v2 cover bpf: Compare iterator types during state pruning

Iterator stack slots can be marked MEM_RCU or PTR_UNTRUSTED. The STACK_ITER check in stacksafe() does not compare this type, so state pruning can treat these states as equal and prune an unsafe path.

Compare the type and add a test where RCU protection has a gap.

---
Changes in v2:
- Convert the regression test to inline assembly so its verifier-sensitive control-flow layout is stable.
- Add Eduard Zingerman's Acked-by tag to patch 1.

v1: https://lore.kernel.org/bpf/[email protected]/

Ning Ding (2):
bpf: Compare iterator types during state pruning selftests/bpf: Test RCU iterator state pruning

 kernel/bpf/states.c                           |  3 +-
 .../selftests/bpf/progs/iters_task_failure.c  | 46 +++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 bpf: Compare iterator types during state pruning

An iterator stack slot can be MEM_RCU or PTR_UNTRUSTED. These states must not be equal, or the verifier can prune an unsafe path.

Compare the pointer type for STACK_ITER slots.

Fixes dfab99df147b ("bpf: teach the verifier to enforce css_iter and task_iter in RCU CS")
---
 kernel/bpf/states.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/states.c +2 -1
index ea2153cf28d0a..4e6aafad33bd2 100644
@@ -812,7 +812,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
 			 * infinite loop check triggering, see
 			 * iter_active_depths_differ()
 			 */
-			if (old_reg->iter.btf != cur_reg->iter.btf ||
+			if (old_reg->type != cur_reg->type ||
+			    old_reg->iter.btf != cur_reg->iter.btf ||
 			    old_reg->iter.btf_id != cur_reg->iter.btf_id ||
 			    old_reg->iter.state != cur_reg->iter.state ||
 			    /* ignore {old_reg,cur_reg}->iter.depth, see above */
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/2 selftests/bpf: Test RCU iterator state pruning

In reply to Ning Ding

Add a path where RCU protection reaches zero and then starts again. The iterator is untrusted after this gap and must be rejected.

---
 .../selftests/bpf/progs/iters_task_failure.c  | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
tools/testing/selftests/bpf/progs/iters_task_failure.c +46 -0
index fe3663dedbe14..566a1d3dffeab 100644
@@ -61,6 +61,52 @@ int BPF_PROG(iter_tasks_lock_and_unlock)
 	return 0;
 }
 
+SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
+__failure __msg("expected an RCU CS when using bpf_iter_task_next")
+__flag(BPF_F_TEST_STATE_FREQ)
+int BPF_PROG(iter_tasks_rcu_state_pruning)
+{
+	struct bpf_iter_task it;
+
+	asm volatile (
+		"call %[bpf_rcu_read_lock];"
+		"r1 = %[it];"
+		"r2 = 0;"
+		"r3 = 0;" /* BPF_TASK_ITER_ALL_PROCS */
+		"call %[bpf_iter_task_new];"
+
+		"call %[bpf_get_prandom_u32];"
+		"if w0 == 0 goto unprotected_%=;"
+
+		/* Keep the outer RCU lock active on the straight-line path. */
+		"call %[bpf_rcu_read_lock];"
+		"call %[bpf_rcu_read_unlock];"
+		"goto merge_%=;"
+
+	"unprotected_%=:"
+		/* Create an unprotected gap on the taken path. */
+		"call %[bpf_rcu_read_unlock];"
+		"call %[bpf_rcu_read_lock];"
+
+	"merge_%=: r1 = %[it];"
+		"call %[bpf_iter_task_next];"
+		"r1 = %[it];"
+		"call %[bpf_iter_task_destroy];"
+		"call %[bpf_rcu_read_unlock];"
+		:
+		: __imm_ptr(it),
+		  __imm(bpf_get_prandom_u32),
+		  __imm(bpf_iter_task_new),
+		  __imm(bpf_iter_task_next),
+		  __imm(bpf_iter_task_destroy),
+		  __imm(bpf_rcu_read_lock),
+		  __imm(bpf_rcu_read_unlock)
+		: __clobber_common
+	);
+
+	return 0;
+}
+
 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
 __failure __msg("expected an RCU CS when using bpf_iter_css_next")
 int BPF_PROG(iter_css_lock_and_unlock)
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Test RCU iterator state pruning

This isn't a bug, but could the changelog mention why BPF_F_TEST_STATE_FREQ is needed and that both arms deliberately converge with the same RCU lock depth, so a future reader knows what keeps this…
Quoted 6 lines

selftests/bpf: Test RCU iterator state pruning

Add a path where RCU protection reaches zero and then starts again. The iterator is untrusted after this gap and must be rejected.

This isn't a bug, but could the changelog mention why BPF_F_TEST_STATE_FREQ is needed and that both arms deliberately converge with the same RCU lock depth, so a future reader knows what keeps this test meaningful?

Quoted 18 lines
tools/testing/selftests/bpf/progs/iters_task_failure.c +10 -0
index fe3663dedbe1..566a1d3dffea 100644
@@ -61,6 +61,52 @@ int BPF_PROG(iter_tasks_lock_and_unlock)
 	return 0;
 }
 
+SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
+__failure __msg("expected an RCU CS when using bpf_iter_task_next")
+__flag(BPF_F_TEST_STATE_FREQ)
+int BPF_PROG(iter_tasks_rcu_state_pruning)
+{
+	struct bpf_iter_task it;
+
+	asm volatile (
+		"call %[bpf_rcu_read_lock];"
+		"r1 = %[it];"

tools/testing/selftests/bpf/progs/iters_task_failure.c int BPF_PROG(iter_tasks_lock_and_unlock) · line 83

Quoted 10 lines
tools/testing/selftests/bpf/progs/iters_task_failure.c +10 -0
+		"r2 = 0;"
+		"r3 = 0;" /* BPF_TASK_ITER_ALL_PROCS */
+		"call %[bpf_iter_task_new];"
+
+		"call %[bpf_get_prandom_u32];"
+		"if w0 == 0 goto unprotected_%=;"
+
+		/* Keep the outer RCU lock active on the straight-line path. */
+		"call %[bpf_rcu_read_lock];"
+		"call %[bpf_rcu_read_unlock];"

This isn't a bug, but would it help to say what the nested lock/unlock pair is for here - e.g. that it scratches the caller-saved registers so the only state difference at merge_ is the iterator's MEM_RCU vs PTR_UNTRUSTED type?

Quoted 16 lines
tools/testing/selftests/bpf/progs/iters_task_failure.c +16 -0
+		"goto merge_%=;"
+
+	"unprotected_%=:"
+		/* Create an unprotected gap on the taken path. */
+		"call %[bpf_rcu_read_unlock];"
+		"call %[bpf_rcu_read_lock];"
+
+	"merge_%=: r1 = %[it];"
+		"call %[bpf_iter_task_next];"
+		"r1 = %[it];"
+		"call %[bpf_iter_task_destroy];"
+		"call %[bpf_rcu_read_unlock];"
+		:
+		: __imm_ptr(it),
+		  __imm(bpf_get_prandom_u32),
+		  __imm(bpf_iter_task_new),

tools/testing/selftests/bpf/progs/iters_task_failure.c int BPF_PROG(iter_tasks_lock_and_unlock) · line 109

Quoted 10 lines
tools/testing/selftests/bpf/progs/iters_task_failure.c +10 -0
+		  __imm(bpf_iter_task_next),
+		  __imm(bpf_iter_task_destroy),
+		  __imm(bpf_rcu_read_lock),
+		  __imm(bpf_rcu_read_unlock)
+		: __clobber_common
+	);
+
+	return 0;
+}
+

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31457825387

#5 lore raw
In reply to Ning Ding
This series was applied to bpf/bpf-next.git (master) by Eduard Zingerman <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Eduard Zingerman <[email protected]>:

Quoted 8 lines · On Mon, 10 Aug 2026 20:59:53 -0700 you wrote:

Iterator stack slots can be marked MEM_RCU or PTR_UNTRUSTED. The STACK_ITER check in stacksafe() does not compare this type, so state pruning can treat these states as equal and prune an unsafe path.

Compare the type and add a test where RCU protection has a gap.

[...]

Here is the summary with links:
- [bpf-next,v2,1/2] bpf: Compare iterator types during state pruning

    https://git.kernel.org/bpf/bpf-next/c/83608e303b95
  - [bpf-next,v2,2/2] selftests/bpf: Test RCU iterator state pruning
    https://git.kernel.org/bpf/bpf-next/c/81f209d5f743

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html