Skip to content

Commit b9c09fb

Browse files
committed
Merge branch 'bpf-allow-access-to-const-void-pointer-arguments-in-tracing-programs'
KaFai Wan says: ==================== bpf: Allow access to const void pointer arguments in tracing programs If we try to access argument which is pointer to const void, it's an UNKNOWN type, verifier will fail to load. Use is_void_or_int_ptr to check if type is void or int pointer. Add a selftest to check it. --- ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 6aca583 + 4c0a42c commit b9c09fb

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

kernel/bpf/btf.c

+7-11
Original file line numberDiff line numberDiff line change
@@ -6383,12 +6383,11 @@ struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
63836383
return prog->aux->attach_btf;
63846384
}
63856385

6386-
static bool is_int_ptr(struct btf *btf, const struct btf_type *t)
6386+
static bool is_void_or_int_ptr(struct btf *btf, const struct btf_type *t)
63876387
{
63886388
/* skip modifiers */
63896389
t = btf_type_skip_modifiers(btf, t->type, NULL);
6390-
6391-
return btf_type_is_int(t);
6390+
return btf_type_is_void(t) || btf_type_is_int(t);
63926391
}
63936392

63946393
static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto,
@@ -6776,14 +6775,11 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
67766775
}
67776776
}
67786777

6779-
if (t->type == 0)
6780-
/* This is a pointer to void.
6781-
* It is the same as scalar from the verifier safety pov.
6782-
* No further pointer walking is allowed.
6783-
*/
6784-
return true;
6785-
6786-
if (is_int_ptr(btf, t))
6778+
/*
6779+
* If it's a pointer to void, it's the same as scalar from the verifier
6780+
* safety POV. Either way, no futher pointer walking is allowed.
6781+
*/
6782+
if (is_void_or_int_ptr(btf, t))
67876783
return true;
67886784

67896785
/* this is a pointer to another type */

net/bpf/test_run.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ __bpf_kfunc u32 bpf_fentry_test9(u32 *a)
569569
return *a;
570570
}
571571

572+
int noinline bpf_fentry_test10(const void *a)
573+
{
574+
return (long)a;
575+
}
576+
572577
void noinline bpf_fentry_test_sinfo(struct skb_shared_info *sinfo)
573578
{
574579
}
@@ -699,7 +704,8 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog,
699704
bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
700705
bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
701706
bpf_fentry_test8(&arg) != 0 ||
702-
bpf_fentry_test9(&retval) != 0)
707+
bpf_fentry_test9(&retval) != 0 ||
708+
bpf_fentry_test10((void *)0) != 0)
703709
goto out;
704710
break;
705711
case BPF_MODIFY_RETURN:

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

+12
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ __naked void ctx_access_u32_pointer_reject_8(void)
6565
" ::: __clobber_all);
6666
}
6767

68+
SEC("fentry/bpf_fentry_test10")
69+
__description("btf_ctx_access const void pointer accept")
70+
__success __retval(0)
71+
__naked void ctx_access_const_void_pointer_accept(void)
72+
{
73+
asm volatile (" \
74+
r2 = *(u64 *)(r1 + 0); /* load 1st argument value (const void pointer) */\
75+
r0 = 0; \
76+
exit; \
77+
" ::: __clobber_all);
78+
}
79+
6880
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)