Commit bb124da
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 <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20231121020701.26440-11-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>1 parent 9f3330a commit bb124da
File tree
3 files changed
+53
-12
lines changed- include/linux
- kernel/bpf
- tools/testing/selftests/bpf/progs
3 files changed
+53
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
301 | 301 | | |
302 | 302 | | |
303 | 303 | | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
304 | 315 | | |
305 | 316 | | |
306 | 317 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9505 | 9505 | | |
9506 | 9506 | | |
9507 | 9507 | | |
| 9508 | + | |
| 9509 | + | |
9508 | 9510 | | |
9509 | 9511 | | |
9510 | 9512 | | |
| |||
10309 | 10311 | | |
10310 | 10312 | | |
10311 | 10313 | | |
10312 | | - | |
10313 | | - | |
| 10314 | + | |
| 10315 | + | |
| 10316 | + | |
| 10317 | + | |
| 10318 | + | |
| 10319 | + | |
| 10320 | + | |
| 10321 | + | |
| 10322 | + | |
| 10323 | + | |
| 10324 | + | |
| 10325 | + | |
| 10326 | + | |
| 10327 | + | |
| 10328 | + | |
10314 | 10329 | | |
10315 | 10330 | | |
10316 | 10331 | | |
| |||
Lines changed: 25 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
123 | 139 | | |
124 | 140 | | |
125 | 141 | | |
| |||
134 | 150 | | |
135 | 151 | | |
136 | 152 | | |
137 | | - | |
138 | | - | |
| 153 | + | |
139 | 154 | | |
140 | | - | |
| 155 | + | |
141 | 156 | | |
142 | 157 | | |
143 | 158 | | |
| |||
264 | 279 | | |
265 | 280 | | |
266 | 281 | | |
267 | | - | |
| 282 | + | |
268 | 283 | | |
269 | 284 | | |
270 | | - | |
| 285 | + | |
271 | 286 | | |
272 | | - | |
| 287 | + | |
273 | 288 | | |
274 | 289 | | |
275 | 290 | | |
| |||
422 | 437 | | |
423 | 438 | | |
424 | 439 | | |
425 | | - | |
| 440 | + | |
426 | 441 | | |
427 | 442 | | |
428 | | - | |
| 443 | + | |
429 | 444 | | |
430 | | - | |
| 445 | + | |
431 | 446 | | |
432 | 447 | | |
433 | 448 | | |
| |||
0 commit comments