Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add a test case to write mtu result into .rodata

Add a test which attempts to call bpf_check_mtu() and writes the MTU
into .rodata section of the BPF program, and for comparison this adds
test cases also for .bss and .data section again. The bpf_check_mtu()
is a bit more special in that the passed mtu argument is read and
written by the helper (instead of just written to). Assert that writes
into .rodata remain rejected by the verifier.

# ./vmtest.sh -- ./test_progs -t verifier_const
[...]
./test_progs -t verifier_const
[ 1.657367] bpf_testmod: loading out-of-tree module taints kernel.
[ 1.657773] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
#473/1 verifier_const/rodata/strtol: write rejected:OK
#473/2 verifier_const/bss/strtol: write accepted:OK
#473/3 verifier_const/data/strtol: write accepted:OK
#473/4 verifier_const/rodata/mtu: write rejected:OK
#473/5 verifier_const/bss/mtu: write accepted:OK
#473/6 verifier_const/data/mtu: write accepted:OK
#473 verifier_const:OK
[...]
Summary: 2/10 PASSED, 0 SKIPPED, 0 FAILED

For comparison, without the MEM_UNINIT on bpf_check_mtu's proto:

# ./vmtest.sh -- ./test_progs -t verifier_const
[...]
#473/3 verifier_const/data/strtol: write accepted:OK
run_subtest:PASS:obj_open_mem 0 nsec
run_subtest:FAIL:unexpected_load_success unexpected success: 0
#473/4 verifier_const/rodata/mtu: write rejected:FAIL
#473/5 verifier_const/bss/mtu: write accepted:OK
#473/6 verifier_const/data/mtu: write accepted:OK
#473 verifier_const:FAIL
[...]

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20240913191754.13290-9-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Daniel Borkmann and committed by
Alexei Starovoitov
211bf9cf 2e3f0660

+30 -3
+30 -3
tools/testing/selftests/bpf/progs/verifier_const.c
··· 10 10 long bart = 96; 11 11 12 12 SEC("tc/ingress") 13 - __description("rodata: write rejected") 13 + __description("rodata/strtol: write rejected") 14 14 __failure __msg("write into map forbidden") 15 15 int tcx1(struct __sk_buff *skb) 16 16 { ··· 20 20 } 21 21 22 22 SEC("tc/ingress") 23 - __description("bss: write accepted") 23 + __description("bss/strtol: write accepted") 24 24 __success 25 25 int tcx2(struct __sk_buff *skb) 26 26 { ··· 30 30 } 31 31 32 32 SEC("tc/ingress") 33 - __description("data: write accepted") 33 + __description("data/strtol: write accepted") 34 34 __success 35 35 int tcx3(struct __sk_buff *skb) 36 36 { 37 37 char buff[] = { '8', '4', '\0' }; 38 38 bpf_strtol(buff, sizeof(buff), 0, &bart); 39 + return TCX_PASS; 40 + } 41 + 42 + SEC("tc/ingress") 43 + __description("rodata/mtu: write rejected") 44 + __failure __msg("write into map forbidden") 45 + int tcx4(struct __sk_buff *skb) 46 + { 47 + bpf_check_mtu(skb, skb->ifindex, (__u32 *)&foo, 0, 0); 48 + return TCX_PASS; 49 + } 50 + 51 + SEC("tc/ingress") 52 + __description("bss/mtu: write accepted") 53 + __success 54 + int tcx5(struct __sk_buff *skb) 55 + { 56 + bpf_check_mtu(skb, skb->ifindex, (__u32 *)&bar, 0, 0); 57 + return TCX_PASS; 58 + } 59 + 60 + SEC("tc/ingress") 61 + __description("data/mtu: write accepted") 62 + __success 63 + int tcx6(struct __sk_buff *skb) 64 + { 65 + bpf_check_mtu(skb, skb->ifindex, (__u32 *)&bart, 0, 0); 39 66 return TCX_PASS; 40 67 } 41 68