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

selftests/bpf: Add test for passing in uninit mtu_len

Add a small test to pass an uninitialized mtu_len to the bpf_check_mtu()
helper to probe whether the verifier rejects it under !CAP_PERFMON.

# ./vmtest.sh -- ./test_progs -t verifier_mtu
[...]
./test_progs -t verifier_mtu
[ 1.414712] tsc: Refined TSC clocksource calibration: 3407.993 MHz
[ 1.415327] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fcd52370, max_idle_ns: 440795242006 ns
[ 1.416463] clocksource: Switched to clocksource tsc
[ 1.429842] bpf_testmod: loading out-of-tree module taints kernel.
[ 1.430283] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
#510/1 verifier_mtu/uninit/mtu: write rejected:OK
#510 verifier_mtu:OK
Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20241021152809.33343-5-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Daniel Borkmann and committed by
Alexei Starovoitov
82bbe133 baa802d2

+37
+19
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 54 54 #include "verifier_masking.skel.h" 55 55 #include "verifier_meta_access.skel.h" 56 56 #include "verifier_movsx.skel.h" 57 + #include "verifier_mtu.skel.h" 57 58 #include "verifier_netfilter_ctx.skel.h" 58 59 #include "verifier_netfilter_retcode.skel.h" 59 60 #include "verifier_bpf_fastcall.skel.h" ··· 223 222 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); } 224 223 void test_verifier_bits_iter(void) { RUN(verifier_bits_iter); } 225 224 void test_verifier_lsm(void) { RUN(verifier_lsm); } 225 + 226 + void test_verifier_mtu(void) 227 + { 228 + __u64 caps = 0; 229 + int ret; 230 + 231 + /* In case CAP_BPF and CAP_PERFMON is not set */ 232 + ret = cap_enable_effective(1ULL << CAP_BPF | 1ULL << CAP_NET_ADMIN, &caps); 233 + if (!ASSERT_OK(ret, "set_cap_bpf_cap_net_admin")) 234 + return; 235 + ret = cap_disable_effective(1ULL << CAP_SYS_ADMIN | 1ULL << CAP_PERFMON, NULL); 236 + if (!ASSERT_OK(ret, "disable_cap_sys_admin")) 237 + goto restore_cap; 238 + RUN(verifier_mtu); 239 + restore_cap: 240 + if (caps) 241 + cap_enable_effective(caps, NULL); 242 + } 226 243 227 244 static int init_test_val_map(struct bpf_object *obj, char *map_name) 228 245 {
+18
tools/testing/selftests/bpf/progs/verifier_mtu.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include "vmlinux.h" 4 + #include <bpf/bpf_helpers.h> 5 + #include "bpf_misc.h" 6 + 7 + SEC("tc/ingress") 8 + __description("uninit/mtu: write rejected") 9 + __failure __msg("invalid indirect read from stack") 10 + int tc_uninit_mtu(struct __sk_buff *ctx) 11 + { 12 + __u32 mtu; 13 + 14 + bpf_check_mtu(ctx, 0, &mtu, 0, 0); 15 + return TCX_PASS; 16 + } 17 + 18 + char LICENSE[] SEC("license") = "GPL";