Skip to content

Commit 5c2bc5e

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: test case for callback_depth states pruning logic
The test case was minimized from mailing list discussion [0]. It is equivalent to the following C program: struct iter_limit_bug_ctx { __u64 a; __u64 b; __u64 c; }; static __naked void iter_limit_bug_cb(void) { switch (bpf_get_prandom_u32()) { case 1: ctx->a = 42; break; case 2: ctx->b = 42; break; default: ctx->c = 42; break; } } int iter_limit_bug(struct __sk_buff *skb) { struct iter_limit_bug_ctx ctx = { 7, 7, 7 }; bpf_loop(2, iter_limit_bug_cb, &ctx, 0); if (ctx.a == 42 && ctx.b == 42 && ctx.c == 7) asm volatile("r1 /= 0;":::"r1"); return 0; } The main idea is that each loop iteration changes one of the state variables in a non-deterministic manner. Hence it is premature to prune the states that have two iterations left comparing them to states with one iteration left. E.g. {{7,7,7}, callback_depth=0} can reach state {42,42,7}, while {{7,7,7}, callback_depth=1} can't. [0] https://lore.kernel.org/bpf/9b251840-7cb8-4d17-bd23-1fc8071d8eef@linux.dev/ Acked-by: Yonghong Song <yonghong.song@linux.dev> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20240222154121.6991-3-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e9a8e5a commit 5c2bc5e

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,74 @@ int bpf_loop_iter_limit_nested(void *unused)
239239
return 1000 * a + b + c;
240240
}
241241

242+
struct iter_limit_bug_ctx {
243+
__u64 a;
244+
__u64 b;
245+
__u64 c;
246+
};
247+
248+
static __naked void iter_limit_bug_cb(void)
249+
{
250+
/* This is the same as C code below, but written
251+
* in assembly to control which branches are fall-through.
252+
*
253+
* switch (bpf_get_prandom_u32()) {
254+
* case 1: ctx->a = 42; break;
255+
* case 2: ctx->b = 42; break;
256+
* default: ctx->c = 42; break;
257+
* }
258+
*/
259+
asm volatile (
260+
"r9 = r2;"
261+
"call %[bpf_get_prandom_u32];"
262+
"r1 = r0;"
263+
"r2 = 42;"
264+
"r0 = 0;"
265+
"if r1 == 0x1 goto 1f;"
266+
"if r1 == 0x2 goto 2f;"
267+
"*(u64 *)(r9 + 16) = r2;"
268+
"exit;"
269+
"1: *(u64 *)(r9 + 0) = r2;"
270+
"exit;"
271+
"2: *(u64 *)(r9 + 8) = r2;"
272+
"exit;"
273+
:
274+
: __imm(bpf_get_prandom_u32)
275+
: __clobber_all
276+
);
277+
}
278+
279+
SEC("tc")
280+
__failure
281+
__flag(BPF_F_TEST_STATE_FREQ)
282+
int iter_limit_bug(struct __sk_buff *skb)
283+
{
284+
struct iter_limit_bug_ctx ctx = { 7, 7, 7 };
285+
286+
bpf_loop(2, iter_limit_bug_cb, &ctx, 0);
287+
288+
/* This is the same as C code below,
289+
* written in assembly to guarantee checks order.
290+
*
291+
* if (ctx.a == 42 && ctx.b == 42 && ctx.c == 7)
292+
* asm volatile("r1 /= 0;":::"r1");
293+
*/
294+
asm volatile (
295+
"r1 = *(u64 *)%[ctx_a];"
296+
"if r1 != 42 goto 1f;"
297+
"r1 = *(u64 *)%[ctx_b];"
298+
"if r1 != 42 goto 1f;"
299+
"r1 = *(u64 *)%[ctx_c];"
300+
"if r1 != 7 goto 1f;"
301+
"r1 /= 0;"
302+
"1:"
303+
:
304+
: [ctx_a]"m"(ctx.a),
305+
[ctx_b]"m"(ctx.b),
306+
[ctx_c]"m"(ctx.c)
307+
: "r1"
308+
);
309+
return 0;
310+
}
311+
242312
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)