1 // SPDX-License-Identifier: GPL-2.0
2 /* Converted from tools/testing/selftests/bpf/verifier/var_off.c */
5 #include <bpf/bpf_helpers.h>
9 __uint(type, BPF_MAP_TYPE_HASH);
10 __uint(max_entries, 1);
11 __type(key, long long);
12 __type(value, long long);
13 } map_hash_8b SEC(".maps");
16 __description("variable-offset ctx access")
17 __failure __msg("variable ctx access var_off=(0x0; 0x4)")
18 __naked void variable_offset_ctx_access(void)
21 /* Get an unknown value */ \
22 r2 = *(u32*)(r1 + 0); \
23 /* Make it small and 4-byte aligned */ \
25 /* add it to skb. We now have either &skb->len or\
26 * &skb->pkt_type, but we don't know which \
29 /* dereference it */ \
30 r0 = *(u32*)(r1 + 0); \
36 __description("variable-offset stack read, priv vs unpriv")
37 __success __failure_unpriv
38 __msg_unpriv("R2 variable stack access prohibited for !root")
40 __naked void stack_read_priv_vs_unpriv(void)
43 /* Fill the top 8 bytes of the stack */ \
45 *(u64*)(r10 - 8) = r0; \
46 /* Get an unknown value */ \
47 r2 = *(u32*)(r1 + 0); \
48 /* Make it small and 4-byte aligned */ \
51 /* add it to fp. We now have either fp-4 or fp-8, but\
52 * we don't know which \
55 /* dereference it for a stack read */ \
56 r0 = *(u32*)(r2 + 0); \
63 __description("variable-offset stack read, uninitialized")
64 __failure __msg("invalid variable-offset read from stack R2")
65 __naked void variable_offset_stack_read_uninitialized(void)
68 /* Get an unknown value */ \
69 r2 = *(u32*)(r1 + 0); \
70 /* Make it small and 4-byte aligned */ \
73 /* add it to fp. We now have either fp-4 or fp-8, but\
74 * we don't know which \
77 /* dereference it for a stack read */ \
78 r0 = *(u32*)(r2 + 0); \
85 __description("variable-offset stack write, priv vs unpriv")
86 __success __failure_unpriv
87 /* Variable stack access is rejected for unprivileged.
89 __msg_unpriv("R2 variable stack access prohibited for !root")
91 __naked void stack_write_priv_vs_unpriv(void)
94 /* Get an unknown value */ \
95 r2 = *(u32*)(r1 + 0); \
96 /* Make it small and 8-byte aligned */ \
99 /* Add it to fp. We now have either fp-8 or fp-16, but\
100 * we don't know which \
103 /* Dereference it for a stack write */ \
105 *(u64*)(r2 + 0) = r0; \
106 /* Now read from the address we just wrote. This shows\
107 * that, after a variable-offset write, a priviledged\
108 * program can read the slots that were in the range of\
109 * that write (even if the verifier doesn't actually know\
110 * if the slot being read was really written to or not.\
112 r3 = *(u64*)(r2 + 0); \
115 " ::: __clobber_all);
119 __description("variable-offset stack write clobbers spilled regs")
121 /* In the priviledged case, dereferencing a spilled-and-then-filled
122 * register is rejected because the previous variable offset stack
123 * write might have overwritten the spilled pointer (i.e. we lose track
124 * of the spilled register when we analyze the write).
126 __msg("R2 invalid mem access 'scalar'")
128 /* The unprivileged case is not too interesting; variable
129 * stack access is rejected.
131 __msg_unpriv("R2 variable stack access prohibited for !root")
132 __naked void stack_write_clobbers_spilled_regs(void)
135 /* Dummy instruction; needed because we need to patch the next one\
136 * and we can't patch the first instruction. \
139 /* Make R0 a map ptr */ \
140 r0 = %[map_hash_8b] ll; \
141 /* Get an unknown value */ \
142 r2 = *(u32*)(r1 + 0); \
143 /* Make it small and 8-byte aligned */ \
146 /* Add it to fp. We now have either fp-8 or fp-16, but\
147 * we don't know which. \
150 /* Spill R0(map ptr) into stack */ \
151 *(u64*)(r10 - 8) = r0; \
152 /* Dereference the unknown value for a stack write */\
154 *(u64*)(r2 + 0) = r0; \
155 /* Fill the register back into R2 */ \
156 r2 = *(u64*)(r10 - 8); \
157 /* Try to dereference R2 for a memory load */ \
158 r0 = *(u64*)(r2 + 8); \
161 : __imm_addr(map_hash_8b)
166 __description("indirect variable-offset stack access, unbounded")
167 __failure __msg("invalid unbounded variable-offset indirect access to stack R4")
168 __naked void variable_offset_stack_access_unbounded(void)
173 /* Fill the top 16 bytes of the stack. */ \
175 *(u64*)(r10 - 16) = r4; \
177 *(u64*)(r10 - 8) = r4; \
178 /* Get an unknown value. */ \
179 r4 = *(u64*)(r1 + %[bpf_sock_ops_bytes_received]);\
180 /* Check the lower bound but don't check the upper one. */\
181 if r4 s< 0 goto l0_%=; \
182 /* Point the lower bound to initialized stack. Offset is now in range\
183 * from fp-16 to fp+0x7fffffffffffffef, i.e. max value is unbounded.\
188 /* Dereference it indirectly. */ \
189 call %[bpf_getsockopt]; \
193 : __imm(bpf_getsockopt),
194 __imm_const(bpf_sock_ops_bytes_received, offsetof(struct bpf_sock_ops, bytes_received))
199 __description("indirect variable-offset stack access, max out of bound")
200 __failure __msg("invalid variable-offset indirect access to stack R2")
201 __naked void access_max_out_of_bound(void)
204 /* Fill the top 8 bytes of the stack */ \
206 *(u64*)(r10 - 8) = r2; \
207 /* Get an unknown value */ \
208 r2 = *(u32*)(r1 + 0); \
209 /* Make it small and 4-byte aligned */ \
212 /* add it to fp. We now have either fp-4 or fp-8, but\
213 * we don't know which \
216 /* dereference it indirectly */ \
217 r1 = %[map_hash_8b] ll; \
218 call %[bpf_map_lookup_elem]; \
222 : __imm(bpf_map_lookup_elem),
223 __imm_addr(map_hash_8b)
228 __description("indirect variable-offset stack access, min out of bound")
229 __failure __msg("invalid variable-offset indirect access to stack R2")
230 __naked void access_min_out_of_bound(void)
233 /* Fill the top 8 bytes of the stack */ \
235 *(u64*)(r10 - 8) = r2; \
236 /* Get an unknown value */ \
237 r2 = *(u32*)(r1 + 0); \
238 /* Make it small and 4-byte aligned */ \
241 /* add it to fp. We now have either fp-516 or fp-512, but\
242 * we don't know which \
245 /* dereference it indirectly */ \
246 r1 = %[map_hash_8b] ll; \
247 call %[bpf_map_lookup_elem]; \
251 : __imm(bpf_map_lookup_elem),
252 __imm_addr(map_hash_8b)
257 __description("indirect variable-offset stack access, min_off < min_initialized")
258 __failure __msg("invalid indirect read from stack R2 var_off")
259 __naked void access_min_off_min_initialized(void)
262 /* Fill only the top 8 bytes of the stack. */ \
264 *(u64*)(r10 - 8) = r2; \
265 /* Get an unknown value */ \
266 r2 = *(u32*)(r1 + 0); \
267 /* Make it small and 4-byte aligned. */ \
270 /* Add it to fp. We now have either fp-12 or fp-16, but we don't know\
271 * which. fp-16 size 8 is partially uninitialized stack.\
274 /* Dereference it indirectly. */ \
275 r1 = %[map_hash_8b] ll; \
276 call %[bpf_map_lookup_elem]; \
280 : __imm(bpf_map_lookup_elem),
281 __imm_addr(map_hash_8b)
286 __description("indirect variable-offset stack access, priv vs unpriv")
287 __success __failure_unpriv
288 __msg_unpriv("R2 variable stack access prohibited for !root")
290 __naked void stack_access_priv_vs_unpriv(void)
293 /* Fill the top 16 bytes of the stack. */ \
295 *(u64*)(r10 - 16) = r2; \
297 *(u64*)(r10 - 8) = r2; \
298 /* Get an unknown value. */ \
299 r2 = *(u32*)(r1 + 0); \
300 /* Make it small and 4-byte aligned. */ \
303 /* Add it to fp. We now have either fp-12 or fp-16, we don't know\
304 * which, but either way it points to initialized stack.\
307 /* Dereference it indirectly. */ \
308 r1 = %[map_hash_8b] ll; \
309 call %[bpf_map_lookup_elem]; \
313 : __imm(bpf_map_lookup_elem),
314 __imm_addr(map_hash_8b)
319 __description("indirect variable-offset stack access, ok")
320 __success __retval(0)
321 __naked void variable_offset_stack_access_ok(void)
324 /* Fill the top 16 bytes of the stack. */ \
326 *(u64*)(r10 - 16) = r2; \
328 *(u64*)(r10 - 8) = r2; \
329 /* Get an unknown value. */ \
330 r2 = *(u32*)(r1 + 0); \
331 /* Make it small and 4-byte aligned. */ \
334 /* Add it to fp. We now have either fp-12 or fp-16, we don't know\
335 * which, but either way it points to initialized stack.\
338 /* Dereference it indirectly. */ \
339 r1 = %[map_hash_8b] ll; \
340 call %[bpf_map_lookup_elem]; \
344 : __imm(bpf_map_lookup_elem),
345 __imm_addr(map_hash_8b)
349 char _license[] SEC("license") = "GPL";