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

selftests/bpf: populate map_array_ro map for verifier_array_access test

Two test cases:
- "valid read map access into a read-only array 1" and
- "valid read map access into a read-only array 2"

Expect that map_array_ro map is filled with mock data. This logic was
not taken into acount during initial test conversion.

This commit modifies prog_tests/verifier.c entry point for this test
to fill the map.

Fixes: a3c830ae0209 ("selftests/bpf: verifier/array_access.c converted to inline assembly")
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20230420232317.2181776-5-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
cbb110bc 5b22f4d1

+41 -5
+39 -3
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 44 44 #include "verifier_xdp.skel.h" 45 45 #include "verifier_xdp_direct_packet_access.skel.h" 46 46 47 + #define MAX_ENTRIES 11 48 + 49 + struct test_val { 50 + unsigned int index; 51 + int foo[MAX_ENTRIES]; 52 + }; 53 + 47 54 __maybe_unused 48 - static void run_tests_aux(const char *skel_name, skel_elf_bytes_fn elf_bytes_factory) 55 + static void run_tests_aux(const char *skel_name, 56 + skel_elf_bytes_fn elf_bytes_factory, 57 + pre_execution_cb pre_execution_cb) 49 58 { 50 59 struct test_loader tester = {}; 51 60 __u64 old_caps; ··· 67 58 return; 68 59 } 69 60 61 + test_loader__set_pre_execution_cb(&tester, pre_execution_cb); 70 62 test_loader__run_subtests(&tester, skel_name, elf_bytes_factory); 71 63 test_loader_fini(&tester); 72 64 ··· 76 66 PRINT_FAIL("failed to restore CAP_SYS_ADMIN: %i, %s\n", err, strerror(err)); 77 67 } 78 68 79 - #define RUN(skel) run_tests_aux(#skel, skel##__elf_bytes) 69 + #define RUN(skel) run_tests_aux(#skel, skel##__elf_bytes, NULL) 80 70 81 71 void test_verifier_and(void) { RUN(verifier_and); } 82 - void test_verifier_array_access(void) { RUN(verifier_array_access); } 83 72 void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); } 84 73 void test_verifier_bounds_deduction(void) { RUN(verifier_bounds_deduction); } 85 74 void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_bounds_deduction_non_const); } ··· 117 108 void test_verifier_xadd(void) { RUN(verifier_xadd); } 118 109 void test_verifier_xdp(void) { RUN(verifier_xdp); } 119 110 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); } 111 + 112 + static int init_array_access_maps(struct bpf_object *obj) 113 + { 114 + struct bpf_map *array_ro; 115 + struct test_val value = { 116 + .index = (6 + 1) * sizeof(int), 117 + .foo[6] = 0xabcdef12, 118 + }; 119 + int err, key = 0; 120 + 121 + array_ro = bpf_object__find_map_by_name(obj, "map_array_ro"); 122 + if (!ASSERT_OK_PTR(array_ro, "lookup map_array_ro")) 123 + return -EINVAL; 124 + 125 + err = bpf_map_update_elem(bpf_map__fd(array_ro), &key, &value, 0); 126 + if (!ASSERT_OK(err, "map_array_ro update")) 127 + return err; 128 + 129 + return 0; 130 + } 131 + 132 + void test_verifier_array_access(void) 133 + { 134 + run_tests_aux("verifier_array_access", 135 + verifier_array_access__elf_bytes, 136 + init_array_access_maps); 137 + }
+2 -2
tools/testing/selftests/bpf/progs/verifier_array_access.c
··· 330 330 331 331 SEC("socket") 332 332 __description("valid read map access into a read-only array 1") 333 - __success __success_unpriv /* __retval(28) temporarily disable */ 333 + __success __success_unpriv __retval(28) 334 334 __naked void a_read_only_array_1_1(void) 335 335 { 336 336 asm volatile (" \ ··· 351 351 352 352 SEC("tc") 353 353 __description("valid read map access into a read-only array 2") 354 - __success /* __retval(65507) temporarily disable */ 354 + __success __retval(65507) 355 355 __naked void a_read_only_array_2_1(void) 356 356 { 357 357 asm volatile (" \