Skip to content

Commit

Permalink
selftests/bpf: Add a test case to write mtu result into .rodata
Browse files Browse the repository at this point in the history
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
  torvalds#473/1   verifier_const/rodata/strtol: write rejected:OK
  torvalds#473/2   verifier_const/bss/strtol: write accepted:OK
  torvalds#473/3   verifier_const/data/strtol: write accepted:OK
  torvalds#473/4   verifier_const/rodata/mtu: write rejected:OK
  torvalds#473/5   verifier_const/bss/mtu: write accepted:OK
  torvalds#473/6   verifier_const/data/mtu: write accepted:OK
  torvalds#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
  [...]
  torvalds#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
  torvalds#473/4   verifier_const/rodata/mtu: write rejected:FAIL
  torvalds#473/5   verifier_const/bss/mtu: write accepted:OK
  torvalds#473/6   verifier_const/data/mtu: write accepted:OK
  torvalds#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>
  • Loading branch information
borkmann authored and Alexei Starovoitov committed Sep 13, 2024
1 parent 2e3f066 commit 211bf9c
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions tools/testing/selftests/bpf/progs/verifier_const.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ long bar;
long bart = 96;

SEC("tc/ingress")
__description("rodata: write rejected")
__description("rodata/strtol: write rejected")
__failure __msg("write into map forbidden")
int tcx1(struct __sk_buff *skb)
{
Expand All @@ -20,7 +20,7 @@ int tcx1(struct __sk_buff *skb)
}

SEC("tc/ingress")
__description("bss: write accepted")
__description("bss/strtol: write accepted")
__success
int tcx2(struct __sk_buff *skb)
{
Expand All @@ -30,7 +30,7 @@ int tcx2(struct __sk_buff *skb)
}

SEC("tc/ingress")
__description("data: write accepted")
__description("data/strtol: write accepted")
__success
int tcx3(struct __sk_buff *skb)
{
Expand All @@ -39,4 +39,31 @@ int tcx3(struct __sk_buff *skb)
return TCX_PASS;
}

SEC("tc/ingress")
__description("rodata/mtu: write rejected")
__failure __msg("write into map forbidden")
int tcx4(struct __sk_buff *skb)
{
bpf_check_mtu(skb, skb->ifindex, (__u32 *)&foo, 0, 0);
return TCX_PASS;
}

SEC("tc/ingress")
__description("bss/mtu: write accepted")
__success
int tcx5(struct __sk_buff *skb)
{
bpf_check_mtu(skb, skb->ifindex, (__u32 *)&bar, 0, 0);
return TCX_PASS;
}

SEC("tc/ingress")
__description("data/mtu: write accepted")
__success
int tcx6(struct __sk_buff *skb)
{
bpf_check_mtu(skb, skb->ifindex, (__u32 *)&bart, 0, 0);
return TCX_PASS;
}

char LICENSE[] SEC("license") = "GPL";

0 comments on commit 211bf9c

Please sign in to comment.