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

selftests/bpf: Add test for dynptr reinit in user_ringbuf callback

The original support for bpf_user_ringbuf_drain callbacks simply
short-circuited checks for the dynptr state, allowing users to pass
PTR_TO_DYNPTR (now CONST_PTR_TO_DYNPTR) to helpers that initialize a
dynptr. This bug would have also surfaced with other dynptr helpers in
the future that changed dynptr view or modified it in some way.

Include test cases for all cases, i.e. both bpf_dynptr_from_mem and
bpf_ringbuf_reserve_dynptr, and ensure verifier rejects both of them.
Without the fix, both of these programs load and pass verification.

While at it, remove sys_nanosleep target from failure cases' SEC
definition, as there is no such tracepoint.

Acked-by: David Vernet <void@manifault.com>
Acked-by: Joanne Koong <joannelkoong@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20221207204141.308952-8-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Kumar Kartikeya Dwivedi and committed by
Alexei Starovoitov
292064cc 76d16077

+45 -8
+2
tools/testing/selftests/bpf/prog_tests/user_ringbuf.c
··· 676 676 {"user_ringbuf_callback_discard_dynptr", "cannot release unowned const bpf_dynptr"}, 677 677 {"user_ringbuf_callback_submit_dynptr", "cannot release unowned const bpf_dynptr"}, 678 678 {"user_ringbuf_callback_invalid_return", "At callback return the register R0 has value"}, 679 + {"user_ringbuf_callback_reinit_dynptr_mem", "Dynptr has to be an uninitialized dynptr"}, 680 + {"user_ringbuf_callback_reinit_dynptr_ringbuf", "Dynptr has to be an uninitialized dynptr"}, 679 681 }; 680 682 681 683 #define SUCCESS_TEST(_func) { _func, #_func }
+43 -8
tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
··· 18 18 __uint(type, BPF_MAP_TYPE_USER_RINGBUF); 19 19 } user_ringbuf SEC(".maps"); 20 20 21 + struct { 22 + __uint(type, BPF_MAP_TYPE_RINGBUF); 23 + __uint(max_entries, 2); 24 + } ringbuf SEC(".maps"); 25 + 26 + static int map_value; 27 + 21 28 static long 22 29 bad_access1(struct bpf_dynptr *dynptr, void *context) 23 30 { ··· 39 32 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 40 33 * not be able to read before the pointer. 41 34 */ 42 - SEC("?raw_tp/sys_nanosleep") 35 + SEC("?raw_tp/") 43 36 int user_ringbuf_callback_bad_access1(void *ctx) 44 37 { 45 38 bpf_user_ringbuf_drain(&user_ringbuf, bad_access1, NULL, 0); ··· 61 54 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 62 55 * not be able to read past the end of the pointer. 63 56 */ 64 - SEC("?raw_tp/sys_nanosleep") 57 + SEC("?raw_tp/") 65 58 int user_ringbuf_callback_bad_access2(void *ctx) 66 59 { 67 60 bpf_user_ringbuf_drain(&user_ringbuf, bad_access2, NULL, 0); ··· 80 73 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 81 74 * not be able to write to that pointer. 82 75 */ 83 - SEC("?raw_tp/sys_nanosleep") 76 + SEC("?raw_tp/") 84 77 int user_ringbuf_callback_write_forbidden(void *ctx) 85 78 { 86 79 bpf_user_ringbuf_drain(&user_ringbuf, write_forbidden, NULL, 0); ··· 99 92 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 100 93 * not be able to write to that pointer. 101 94 */ 102 - SEC("?raw_tp/sys_nanosleep") 95 + SEC("?raw_tp/") 103 96 int user_ringbuf_callback_null_context_write(void *ctx) 104 97 { 105 98 bpf_user_ringbuf_drain(&user_ringbuf, null_context_write, NULL, 0); ··· 120 113 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 121 114 * not be able to write to that pointer. 122 115 */ 123 - SEC("?raw_tp/sys_nanosleep") 116 + SEC("?raw_tp/") 124 117 int user_ringbuf_callback_null_context_read(void *ctx) 125 118 { 126 119 bpf_user_ringbuf_drain(&user_ringbuf, null_context_read, NULL, 0); ··· 139 132 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 140 133 * not be able to read past the end of the pointer. 141 134 */ 142 - SEC("?raw_tp/sys_nanosleep") 135 + SEC("?raw_tp/") 143 136 int user_ringbuf_callback_discard_dynptr(void *ctx) 144 137 { 145 138 bpf_user_ringbuf_drain(&user_ringbuf, try_discard_dynptr, NULL, 0); ··· 158 151 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 159 152 * not be able to read past the end of the pointer. 160 153 */ 161 - SEC("?raw_tp/sys_nanosleep") 154 + SEC("?raw_tp/") 162 155 int user_ringbuf_callback_submit_dynptr(void *ctx) 163 156 { 164 157 bpf_user_ringbuf_drain(&user_ringbuf, try_submit_dynptr, NULL, 0); ··· 175 168 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should 176 169 * not be able to write to that pointer. 177 170 */ 178 - SEC("?raw_tp/sys_nanosleep") 171 + SEC("?raw_tp/") 179 172 int user_ringbuf_callback_invalid_return(void *ctx) 180 173 { 181 174 bpf_user_ringbuf_drain(&user_ringbuf, invalid_drain_callback_return, NULL, 0); 182 175 176 + return 0; 177 + } 178 + 179 + static long 180 + try_reinit_dynptr_mem(struct bpf_dynptr *dynptr, void *context) 181 + { 182 + bpf_dynptr_from_mem(&map_value, 4, 0, dynptr); 183 + return 0; 184 + } 185 + 186 + static long 187 + try_reinit_dynptr_ringbuf(struct bpf_dynptr *dynptr, void *context) 188 + { 189 + bpf_ringbuf_reserve_dynptr(&ringbuf, 8, 0, dynptr); 190 + return 0; 191 + } 192 + 193 + SEC("?raw_tp/") 194 + int user_ringbuf_callback_reinit_dynptr_mem(void *ctx) 195 + { 196 + bpf_user_ringbuf_drain(&user_ringbuf, try_reinit_dynptr_mem, NULL, 0); 197 + return 0; 198 + } 199 + 200 + SEC("?raw_tp/") 201 + int user_ringbuf_callback_reinit_dynptr_ringbuf(void *ctx) 202 + { 203 + bpf_user_ringbuf_drain(&user_ringbuf, try_reinit_dynptr_ringbuf, NULL, 0); 183 204 return 0; 184 205 }