Skip to content

Commit 57e2a52

Browse files
eddyz87Alexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: check if max number of bpf_loop iterations is tracked
Check that even if bpf_loop() callback simulation does not converge to a specific state, verification could proceed via "brute force" simulation of maximal number of callback calls. Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent bb124da commit 57e2a52

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,79 @@ int unsafe_find_vma(void *unused)
164164
return choice_arr[loop_ctx.i];
165165
}
166166

167+
static int iter_limit_cb(__u32 idx, struct num_context *ctx)
168+
{
169+
ctx->i++;
170+
return 0;
171+
}
172+
173+
SEC("?raw_tp")
174+
__success
175+
int bpf_loop_iter_limit_ok(void *unused)
176+
{
177+
struct num_context ctx = { .i = 0 };
178+
179+
bpf_loop(1, iter_limit_cb, &ctx, 0);
180+
return choice_arr[ctx.i];
181+
}
182+
183+
SEC("?raw_tp")
184+
__failure __msg("invalid access to map value, value_size=2 off=2 size=1")
185+
int bpf_loop_iter_limit_overflow(void *unused)
186+
{
187+
struct num_context ctx = { .i = 0 };
188+
189+
bpf_loop(2, iter_limit_cb, &ctx, 0);
190+
return choice_arr[ctx.i];
191+
}
192+
193+
static int iter_limit_level2a_cb(__u32 idx, struct num_context *ctx)
194+
{
195+
ctx->i += 100;
196+
return 0;
197+
}
198+
199+
static int iter_limit_level2b_cb(__u32 idx, struct num_context *ctx)
200+
{
201+
ctx->i += 10;
202+
return 0;
203+
}
204+
205+
static int iter_limit_level1_cb(__u32 idx, struct num_context *ctx)
206+
{
207+
ctx->i += 1;
208+
bpf_loop(1, iter_limit_level2a_cb, ctx, 0);
209+
bpf_loop(1, iter_limit_level2b_cb, ctx, 0);
210+
return 0;
211+
}
212+
213+
/* Check that path visiting every callback function once had been
214+
* reached by verifier. Variables 'ctx{1,2}i' below serve as flags,
215+
* with each decimal digit corresponding to a callback visit marker.
216+
*/
217+
SEC("socket")
218+
__success __retval(111111)
219+
int bpf_loop_iter_limit_nested(void *unused)
220+
{
221+
struct num_context ctx1 = { .i = 0 };
222+
struct num_context ctx2 = { .i = 0 };
223+
__u64 a, b, c;
224+
225+
bpf_loop(1, iter_limit_level1_cb, &ctx1, 0);
226+
bpf_loop(1, iter_limit_level1_cb, &ctx2, 0);
227+
a = ctx1.i;
228+
b = ctx2.i;
229+
/* Force 'ctx1.i' and 'ctx2.i' precise. */
230+
c = choice_arr[(a + b) % 2];
231+
/* This makes 'c' zero, but neither clang nor verifier know it. */
232+
c /= 10;
233+
/* Make sure that verifier does not visit 'impossible' states:
234+
* enumerate all possible callback visit masks.
235+
*/
236+
if (a != 0 && a != 1 && a != 11 && a != 101 && a != 111 &&
237+
b != 0 && b != 1 && b != 11 && b != 101 && b != 111)
238+
asm volatile ("r0 /= 0;" ::: "r0");
239+
return 1000 * a + b + c;
240+
}
241+
167242
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)