Skip to content

Commit e4c80f6

Browse files
author
Alexei Starovoitov
committed
Merge branch 'add-missing-size-check-for-btf-based-ctx-access'
Kumar Kartikeya Dwivedi says: ==================== Add missing size check for BTF-based ctx access This set fixes a issue reported for tracing and struct ops programs using btf_ctx_access for ctx checks, where loading a pointer argument from the ctx doesn't enforce a BPF_DW access size check. The original report is at link [0]. Also add a regression test along with the fix. [0]: https://lore.kernel.org/bpf/51338.1732985814@localhost ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 04789af + 8025731 commit e4c80f6

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

kernel/bpf/btf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6543,6 +6543,12 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
65436543
return false;
65446544
}
65456545

6546+
if (size != sizeof(u64)) {
6547+
bpf_log(log, "func '%s' size %d must be 8\n",
6548+
tname, size);
6549+
return false;
6550+
}
6551+
65466552
/* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
65476553
for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
65486554
const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ __success __retval(0)
1111
__naked void btf_ctx_access_accept(void)
1212
{
1313
asm volatile (" \
14-
r2 = *(u32*)(r1 + 8); /* load 2nd argument value (int pointer) */\
14+
r2 = *(u64 *)(r1 + 8); /* load 2nd argument value (int pointer) */\
1515
r0 = 0; \
1616
exit; \
1717
" ::: __clobber_all);
@@ -23,7 +23,43 @@ __success __retval(0)
2323
__naked void ctx_access_u32_pointer_accept(void)
2424
{
2525
asm volatile (" \
26-
r2 = *(u32*)(r1 + 0); /* load 1nd argument value (u32 pointer) */\
26+
r2 = *(u64 *)(r1 + 0); /* load 1nd argument value (u32 pointer) */\
27+
r0 = 0; \
28+
exit; \
29+
" ::: __clobber_all);
30+
}
31+
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 */\
2763
r0 = 0; \
2864
exit; \
2965
" ::: __clobber_all);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ __success __retval(0)
1111
__naked void d_path_accept(void)
1212
{
1313
asm volatile (" \
14-
r1 = *(u32*)(r1 + 0); \
14+
r1 = *(u64 *)(r1 + 0); \
1515
r2 = r10; \
1616
r2 += -8; \
1717
r6 = 0; \
@@ -31,7 +31,7 @@ __failure __msg("helper call is not allowed in probe")
3131
__naked void d_path_reject(void)
3232
{
3333
asm volatile (" \
34-
r1 = *(u32*)(r1 + 0); \
34+
r1 = *(u64 *)(r1 + 0); \
3535
r2 = r10; \
3636
r2 += -8; \
3737
r6 = 0; \

0 commit comments

Comments
 (0)