Skip to content

Commit 8025731

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
selftests/bpf: Add test for narrow ctx load for pointer args
Ensure that performing narrow ctx loads other than size == 8 are rejected when the argument is a pointer type. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20241212092050.3204165-3-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 659b9ba commit 8025731

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,40 @@ __naked void ctx_access_u32_pointer_accept(void)
2929
" ::: __clobber_all);
3030
}
3131

32+
SEC("fentry/bpf_fentry_test9")
33+
__description("btf_ctx_access u32 pointer reject u32")
34+
__failure __msg("size 4 must be 8")
35+
__naked void ctx_access_u32_pointer_reject_32(void)
36+
{
37+
asm volatile (" \
38+
r2 = *(u32 *)(r1 + 0); /* load 1st argument with narrow load */\
39+
r0 = 0; \
40+
exit; \
41+
" ::: __clobber_all);
42+
}
43+
44+
SEC("fentry/bpf_fentry_test9")
45+
__description("btf_ctx_access u32 pointer reject u16")
46+
__failure __msg("size 2 must be 8")
47+
__naked void ctx_access_u32_pointer_reject_16(void)
48+
{
49+
asm volatile (" \
50+
r2 = *(u16 *)(r1 + 0); /* load 1st argument with narrow load */\
51+
r0 = 0; \
52+
exit; \
53+
" ::: __clobber_all);
54+
}
55+
56+
SEC("fentry/bpf_fentry_test9")
57+
__description("btf_ctx_access u32 pointer reject u8")
58+
__failure __msg("size 1 must be 8")
59+
__naked void ctx_access_u32_pointer_reject_8(void)
60+
{
61+
asm volatile (" \
62+
r2 = *(u8 *)(r1 + 0); /* load 1st argument with narrow load */\
63+
r0 = 0; \
64+
exit; \
65+
" ::: __clobber_all);
66+
}
67+
3268
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)