Skip to content

Commit 68cca81

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: tests for __arg_untrusted void * global func params
Check usage of __arg_untrusted parameters of primitive type: - passing of {trusted, untrusted, map value, scalar value, values with variable offset} to untrusted `void *`, `char *` or enum is ok; - varifier represents such parameters as rdonly_untrusted_mem(sz=0). Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c4aa454 commit 68cca81

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,57 @@ int untrusted_to_trusted(void *ctx)
260260
return subprog_untrusted2(bpf_get_current_task_btf());
261261
}
262262

263+
__weak int subprog_void_untrusted(void *p __arg_untrusted)
264+
{
265+
return *(int *)p;
266+
}
267+
268+
__weak int subprog_char_untrusted(char *p __arg_untrusted)
269+
{
270+
return *(int *)p;
271+
}
272+
273+
__weak int subprog_enum_untrusted(enum bpf_attach_type *p __arg_untrusted)
274+
{
275+
return *(int *)p;
276+
}
277+
278+
__weak int subprog_enum64_untrusted(enum scx_public_consts *p __arg_untrusted)
279+
{
280+
return *(int *)p;
281+
}
282+
283+
SEC("tp_btf/sys_enter")
284+
__success
285+
__log_level(2)
286+
__msg("r1 = {{.*}}; {{.*}}R1_w=trusted_ptr_task_struct()")
287+
__msg("Func#1 ('subprog_void_untrusted') is global and assumed valid.")
288+
__msg("Validating subprog_void_untrusted() func#1...")
289+
__msg(": R1=rdonly_untrusted_mem(sz=0)")
290+
int trusted_to_untrusted_mem(void *ctx)
291+
{
292+
return subprog_void_untrusted(bpf_get_current_task_btf());
293+
}
294+
295+
SEC("tp_btf/sys_enter")
296+
__success
297+
int anything_to_untrusted_mem(void *ctx)
298+
{
299+
/* untrusted to untrusted mem */
300+
subprog_void_untrusted(bpf_core_cast(0, struct task_struct));
301+
/* map value to untrusted mem */
302+
subprog_void_untrusted(mem);
303+
/* scalar to untrusted mem */
304+
subprog_void_untrusted(0);
305+
/* variable offset to untrusted mem (map) */
306+
subprog_void_untrusted((void *)mem + off);
307+
/* variable offset to untrusted mem (trusted) */
308+
subprog_void_untrusted(bpf_get_current_task_btf() + off);
309+
/* variable offset to untrusted char/enum/enum64 (map) */
310+
subprog_char_untrusted(mem + off);
311+
subprog_enum_untrusted((void *)mem + off);
312+
subprog_enum64_untrusted((void *)mem + off);
313+
return 0;
314+
}
315+
263316
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)