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

selftests/bpf: Migrate LOAD_REJECT test cases to prog_tests

Move LOAD_REJECT test cases from test_sock.c to an equivalent set of
verifier tests in progs/verifier_sock.c.

Signed-off-by: Jordan Rife <jrife@google.com>
Link: https://lore.kernel.org/r/20241022152913.574836-3-jrife@google.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

authored by

Jordan Rife and committed by
Martin KaFai Lau
c17f9734 94682d6a

+60 -52
+60
tools/testing/selftests/bpf/progs/verifier_sock.c
··· 977 977 : __clobber_all); 978 978 } 979 979 980 + SEC("cgroup/post_bind4") 981 + __description("sk->src_ip6[0] [load 1st byte]") 982 + __failure __msg("invalid bpf_context access off=28 size=2") 983 + __naked void post_bind4_read_src_ip6(void) 984 + { 985 + asm volatile (" \ 986 + r6 = r1; \ 987 + r7 = *(u16*)(r6 + %[bpf_sock_src_ip6_0]); \ 988 + r0 = 1; \ 989 + exit; \ 990 + " : 991 + : __imm_const(bpf_sock_src_ip6_0, offsetof(struct bpf_sock, src_ip6[0])) 992 + : __clobber_all); 993 + } 994 + 995 + SEC("cgroup/post_bind4") 996 + __description("sk->mark [load mark]") 997 + __failure __msg("invalid bpf_context access off=16 size=2") 998 + __naked void post_bind4_read_mark(void) 999 + { 1000 + asm volatile (" \ 1001 + r6 = r1; \ 1002 + r7 = *(u16*)(r6 + %[bpf_sock_mark]); \ 1003 + r0 = 1; \ 1004 + exit; \ 1005 + " : 1006 + : __imm_const(bpf_sock_mark, offsetof(struct bpf_sock, mark)) 1007 + : __clobber_all); 1008 + } 1009 + 1010 + SEC("cgroup/post_bind6") 1011 + __description("sk->src_ip4 [load src_ip4]") 1012 + __failure __msg("invalid bpf_context access off=24 size=2") 1013 + __naked void post_bind6_read_src_ip4(void) 1014 + { 1015 + asm volatile (" \ 1016 + r6 = r1; \ 1017 + r7 = *(u16*)(r6 + %[bpf_sock_src_ip4]); \ 1018 + r0 = 1; \ 1019 + exit; \ 1020 + " : 1021 + : __imm_const(bpf_sock_src_ip4, offsetof(struct bpf_sock, src_ip4)) 1022 + : __clobber_all); 1023 + } 1024 + 1025 + SEC("cgroup/sock_create") 1026 + __description("sk->src_port [word load]") 1027 + __failure __msg("invalid bpf_context access off=44 size=2") 1028 + __naked void sock_create_read_src_port(void) 1029 + { 1030 + asm volatile (" \ 1031 + r6 = r1; \ 1032 + r7 = *(u16*)(r6 + %[bpf_sock_src_port]); \ 1033 + r0 = 1; \ 1034 + exit; \ 1035 + " : 1036 + : __imm_const(bpf_sock_src_port, offsetof(struct bpf_sock, src_port)) 1037 + : __clobber_all); 1038 + } 1039 + 980 1040 char _license[] SEC("license") = "GPL";
-52
tools/testing/selftests/bpf/test_sock.c
··· 48 48 49 49 static struct sock_test tests[] = { 50 50 { 51 - .descr = "bind4 load with invalid access: src_ip6", 52 - .insns = { 53 - BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 54 - BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 55 - offsetof(struct bpf_sock, src_ip6[0])), 56 - BPF_MOV64_IMM(BPF_REG_0, 1), 57 - BPF_EXIT_INSN(), 58 - }, 59 - .expected_attach_type = BPF_CGROUP_INET4_POST_BIND, 60 - .attach_type = BPF_CGROUP_INET4_POST_BIND, 61 - .result = LOAD_REJECT, 62 - }, 63 - { 64 - .descr = "bind4 load with invalid access: mark", 65 - .insns = { 66 - BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 67 - BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 68 - offsetof(struct bpf_sock, mark)), 69 - BPF_MOV64_IMM(BPF_REG_0, 1), 70 - BPF_EXIT_INSN(), 71 - }, 72 - .expected_attach_type = BPF_CGROUP_INET4_POST_BIND, 73 - .attach_type = BPF_CGROUP_INET4_POST_BIND, 74 - .result = LOAD_REJECT, 75 - }, 76 - { 77 - .descr = "bind6 load with invalid access: src_ip4", 78 - .insns = { 79 - BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 80 - BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 81 - offsetof(struct bpf_sock, src_ip4)), 82 - BPF_MOV64_IMM(BPF_REG_0, 1), 83 - BPF_EXIT_INSN(), 84 - }, 85 - .expected_attach_type = BPF_CGROUP_INET6_POST_BIND, 86 - .attach_type = BPF_CGROUP_INET6_POST_BIND, 87 - .result = LOAD_REJECT, 88 - }, 89 - { 90 - .descr = "sock_create load with invalid access: src_port", 91 - .insns = { 92 - BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 93 - BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 94 - offsetof(struct bpf_sock, src_port)), 95 - BPF_MOV64_IMM(BPF_REG_0, 1), 96 - BPF_EXIT_INSN(), 97 - }, 98 - .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE, 99 - .attach_type = BPF_CGROUP_INET_SOCK_CREATE, 100 - .result = LOAD_REJECT, 101 - }, 102 - { 103 51 .descr = "sock_create load w/o expected_attach_type (compat mode)", 104 52 .insns = { 105 53 BPF_MOV64_IMM(BPF_REG_0, 1),