Skip to content

Commit bb124da

Browse files
eddyz87Alexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: keep track of max number of bpf_loop callback iterations
In some cases verifier can't infer convergence of the bpf_loop() iteration. E.g. for the following program: static int cb(__u32 idx, struct num_context* ctx) { ctx->i++; return 0; } SEC("?raw_tp") int prog(void *_) { struct num_context ctx = { .i = 0 }; __u8 choice_arr[2] = { 0, 1 }; bpf_loop(2, cb, &ctx, 0); return choice_arr[ctx.i]; } Each 'cb' simulation would eventually return to 'prog' and reach 'return choice_arr[ctx.i]' statement. At which point ctx.i would be marked precise, thus forcing verifier to track multitude of separate states with {.i=0}, {.i=1}, ... at bpf_loop() callback entry. This commit allows "brute force" handling for such cases by limiting number of callback body simulations using 'umax' value of the first bpf_loop() parameter. For this, extend bpf_func_state with 'callback_depth' field. Increment this field when callback visiting state is pushed to states traversal stack. For frame #N it's 'callback_depth' field counts how many times callback with frame depth N+1 had been executed. Use bpf_func_state specifically to allow independent tracking of callback depths when multiple nested bpf_loop() calls are present. Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9f3330a commit bb124da

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

include/linux/bpf_verifier.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ struct bpf_func_state {
301301
struct tnum callback_ret_range;
302302
bool in_async_callback_fn;
303303
bool in_exception_callback_fn;
304+
/* For callback calling functions that limit number of possible
305+
* callback executions (e.g. bpf_loop) keeps track of current
306+
* simulated iteration number.
307+
* Value in frame N refers to number of times callback with frame
308+
* N+1 was simulated, e.g. for the following call:
309+
*
310+
* bpf_loop(..., fn, ...); | suppose current frame is N
311+
* | fn would be simulated in frame N+1
312+
* | number of simulations is tracked in frame N
313+
*/
314+
u32 callback_depth;
304315

305316
/* The following fields should be last. See copy_func_state() */
306317
int acquired_refs;

kernel/bpf/verifier.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9505,6 +9505,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
95059505
return err;
95069506

95079507
callback_state->callback_unroll_depth++;
9508+
callback_state->frame[callback_state->curframe - 1]->callback_depth++;
9509+
caller->callback_depth = 0;
95089510
return 0;
95099511
}
95109512

@@ -10309,8 +10311,21 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1030910311
break;
1031010312
case BPF_FUNC_loop:
1031110313
update_loop_inline_state(env, meta.subprogno);
10312-
err = push_callback_call(env, insn, insn_idx, meta.subprogno,
10313-
set_loop_callback_state);
10314+
/* Verifier relies on R1 value to determine if bpf_loop() iteration
10315+
* is finished, thus mark it precise.
10316+
*/
10317+
err = mark_chain_precision(env, BPF_REG_1);
10318+
if (err)
10319+
return err;
10320+
if (cur_func(env)->callback_depth < regs[BPF_REG_1].umax_value) {
10321+
err = push_callback_call(env, insn, insn_idx, meta.subprogno,
10322+
set_loop_callback_state);
10323+
} else {
10324+
cur_func(env)->callback_depth = 0;
10325+
if (env->log.level & BPF_LOG_LEVEL2)
10326+
verbose(env, "frame%d bpf_loop iteration limit reached\n",
10327+
env->cur_state->curframe);
10328+
}
1031410329
break;
1031510330
case BPF_FUNC_dynptr_from_mem:
1031610331
if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) {

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,23 @@ __naked int global_subprog_result_precise(void)
119119

120120
SEC("?raw_tp")
121121
__success __log_level(2)
122-
/* First simulated path does not include callback body */
122+
/* First simulated path does not include callback body,
123+
* r1 and r4 are always precise for bpf_loop() calls.
124+
*/
125+
__msg("9: (85) call bpf_loop#181")
126+
__msg("mark_precise: frame0: last_idx 9 first_idx 9 subseq_idx -1")
127+
__msg("mark_precise: frame0: parent state regs=r4 stack=:")
128+
__msg("mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx 9")
129+
__msg("mark_precise: frame0: regs=r4 stack= before 8: (b7) r4 = 0")
130+
__msg("mark_precise: frame0: last_idx 9 first_idx 9 subseq_idx -1")
131+
__msg("mark_precise: frame0: parent state regs=r1 stack=:")
132+
__msg("mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx 9")
133+
__msg("mark_precise: frame0: regs=r1 stack= before 8: (b7) r4 = 0")
134+
__msg("mark_precise: frame0: regs=r1 stack= before 7: (b7) r3 = 0")
135+
__msg("mark_precise: frame0: regs=r1 stack= before 6: (bf) r2 = r8")
136+
__msg("mark_precise: frame0: regs=r1 stack= before 5: (bf) r1 = r6")
137+
__msg("mark_precise: frame0: regs=r6 stack= before 4: (b7) r6 = 3")
138+
/* r6 precision propagation */
123139
__msg("14: (0f) r1 += r6")
124140
__msg("mark_precise: frame0: last_idx 14 first_idx 9")
125141
__msg("mark_precise: frame0: regs=r6 stack= before 13: (bf) r1 = r7")
@@ -134,10 +150,9 @@ __msg("17: (b7) r0 = 0")
134150
__msg("18: (95) exit")
135151
__msg("returning from callee:")
136152
__msg("to caller at 9:")
137-
/* r4 (flags) is always precise for bpf_loop() */
138-
__msg("frame 0: propagating r4")
153+
__msg("frame 0: propagating r1,r4")
139154
__msg("mark_precise: frame0: last_idx 9 first_idx 9 subseq_idx -1")
140-
__msg("mark_precise: frame0: regs=r4 stack= before 18: (95) exit")
155+
__msg("mark_precise: frame0: regs=r1,r4 stack= before 18: (95) exit")
141156
__msg("from 18 to 9: safe")
142157
__naked int callback_result_precise(void)
143158
{
@@ -264,12 +279,12 @@ __msg("15: (b7) r0 = 0")
264279
__msg("16: (95) exit")
265280
__msg("returning from callee:")
266281
__msg("to caller at 9:")
267-
/* r4 (flags) is always precise for bpf_loop(),
282+
/* r1, r4 are always precise for bpf_loop(),
268283
* r6 was marked before backtracking to callback body.
269284
*/
270-
__msg("frame 0: propagating r4,r6")
285+
__msg("frame 0: propagating r1,r4,r6")
271286
__msg("mark_precise: frame0: last_idx 9 first_idx 9 subseq_idx -1")
272-
__msg("mark_precise: frame0: regs=r4,r6 stack= before 16: (95) exit")
287+
__msg("mark_precise: frame0: regs=r1,r4,r6 stack= before 16: (95) exit")
273288
__msg("mark_precise: frame1: regs= stack= before 15: (b7) r0 = 0")
274289
__msg("mark_precise: frame1: regs= stack= before 9: (85) call bpf_loop")
275290
__msg("mark_precise: frame0: parent state regs= stack=:")
@@ -422,12 +437,12 @@ __msg("17: (b7) r0 = 0")
422437
__msg("18: (95) exit")
423438
__msg("returning from callee:")
424439
__msg("to caller at 10:")
425-
/* r4 (flags) is always precise for bpf_loop(),
440+
/* r1, r4 are always precise for bpf_loop(),
426441
* fp-8 was marked before backtracking to callback body.
427442
*/
428-
__msg("frame 0: propagating r4,fp-8")
443+
__msg("frame 0: propagating r1,r4,fp-8")
429444
__msg("mark_precise: frame0: last_idx 10 first_idx 10 subseq_idx -1")
430-
__msg("mark_precise: frame0: regs=r4 stack=-8 before 18: (95) exit")
445+
__msg("mark_precise: frame0: regs=r1,r4 stack=-8 before 18: (95) exit")
431446
__msg("mark_precise: frame1: regs= stack= before 17: (b7) r0 = 0")
432447
__msg("mark_precise: frame1: regs= stack= before 10: (85) call bpf_loop#181")
433448
__msg("mark_precise: frame0: parent state regs= stack=:")

0 commit comments

Comments
 (0)