1d15f5b68SEduard Zingerman // SPDX-License-Identifier: GPL-2.0
2d15f5b68SEduard Zingerman /* Converted from tools/testing/selftests/bpf/verifier/var_off.c */
3d15f5b68SEduard Zingerman
4d15f5b68SEduard Zingerman #include <linux/bpf.h>
5d15f5b68SEduard Zingerman #include <bpf/bpf_helpers.h>
6d15f5b68SEduard Zingerman #include "bpf_misc.h"
7d15f5b68SEduard Zingerman
8d15f5b68SEduard Zingerman struct {
9d15f5b68SEduard Zingerman __uint(type, BPF_MAP_TYPE_HASH);
10d15f5b68SEduard Zingerman __uint(max_entries, 1);
11d15f5b68SEduard Zingerman __type(key, long long);
12d15f5b68SEduard Zingerman __type(value, long long);
13d15f5b68SEduard Zingerman } map_hash_8b SEC(".maps");
14d15f5b68SEduard Zingerman
15d15f5b68SEduard Zingerman SEC("lwt_in")
16d15f5b68SEduard Zingerman __description("variable-offset ctx access")
17d15f5b68SEduard Zingerman __failure __msg("variable ctx access var_off=(0x0; 0x4)")
variable_offset_ctx_access(void)18d15f5b68SEduard Zingerman __naked void variable_offset_ctx_access(void)
19d15f5b68SEduard Zingerman {
20d15f5b68SEduard Zingerman asm volatile (" \
21d15f5b68SEduard Zingerman /* Get an unknown value */ \
22d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
23d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned */ \
24d15f5b68SEduard Zingerman r2 &= 4; \
25d15f5b68SEduard Zingerman /* add it to skb. We now have either &skb->len or\
26d15f5b68SEduard Zingerman * &skb->pkt_type, but we don't know which \
27d15f5b68SEduard Zingerman */ \
28d15f5b68SEduard Zingerman r1 += r2; \
29d15f5b68SEduard Zingerman /* dereference it */ \
30d15f5b68SEduard Zingerman r0 = *(u32*)(r1 + 0); \
31d15f5b68SEduard Zingerman exit; \
32d15f5b68SEduard Zingerman " ::: __clobber_all);
33d15f5b68SEduard Zingerman }
34d15f5b68SEduard Zingerman
35d15f5b68SEduard Zingerman SEC("cgroup/skb")
36d15f5b68SEduard Zingerman __description("variable-offset stack read, priv vs unpriv")
37d15f5b68SEduard Zingerman __success __failure_unpriv
38d15f5b68SEduard Zingerman __msg_unpriv("R2 variable stack access prohibited for !root")
39d15f5b68SEduard Zingerman __retval(0)
stack_read_priv_vs_unpriv(void)40d15f5b68SEduard Zingerman __naked void stack_read_priv_vs_unpriv(void)
41d15f5b68SEduard Zingerman {
42d15f5b68SEduard Zingerman asm volatile (" \
43d15f5b68SEduard Zingerman /* Fill the top 8 bytes of the stack */ \
44d15f5b68SEduard Zingerman r0 = 0; \
45d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r0; \
46d15f5b68SEduard Zingerman /* Get an unknown value */ \
47d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
48d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned */ \
49d15f5b68SEduard Zingerman r2 &= 4; \
50d15f5b68SEduard Zingerman r2 -= 8; \
51d15f5b68SEduard Zingerman /* add it to fp. We now have either fp-4 or fp-8, but\
52d15f5b68SEduard Zingerman * we don't know which \
53d15f5b68SEduard Zingerman */ \
54d15f5b68SEduard Zingerman r2 += r10; \
55d15f5b68SEduard Zingerman /* dereference it for a stack read */ \
56d15f5b68SEduard Zingerman r0 = *(u32*)(r2 + 0); \
57d15f5b68SEduard Zingerman r0 = 0; \
58d15f5b68SEduard Zingerman exit; \
59d15f5b68SEduard Zingerman " ::: __clobber_all);
60d15f5b68SEduard Zingerman }
61d15f5b68SEduard Zingerman
62*6b4a64baSAndrei Matei SEC("cgroup/skb")
63d15f5b68SEduard Zingerman __description("variable-offset stack read, uninitialized")
64*6b4a64baSAndrei Matei __success
65*6b4a64baSAndrei Matei __failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
variable_offset_stack_read_uninitialized(void)66d15f5b68SEduard Zingerman __naked void variable_offset_stack_read_uninitialized(void)
67d15f5b68SEduard Zingerman {
68d15f5b68SEduard Zingerman asm volatile (" \
69d15f5b68SEduard Zingerman /* Get an unknown value */ \
70d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
71d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned */ \
72d15f5b68SEduard Zingerman r2 &= 4; \
73d15f5b68SEduard Zingerman r2 -= 8; \
74d15f5b68SEduard Zingerman /* add it to fp. We now have either fp-4 or fp-8, but\
75d15f5b68SEduard Zingerman * we don't know which \
76d15f5b68SEduard Zingerman */ \
77d15f5b68SEduard Zingerman r2 += r10; \
78d15f5b68SEduard Zingerman /* dereference it for a stack read */ \
79d15f5b68SEduard Zingerman r0 = *(u32*)(r2 + 0); \
80d15f5b68SEduard Zingerman r0 = 0; \
81d15f5b68SEduard Zingerman exit; \
82d15f5b68SEduard Zingerman " ::: __clobber_all);
83d15f5b68SEduard Zingerman }
84d15f5b68SEduard Zingerman
85d15f5b68SEduard Zingerman SEC("socket")
86d15f5b68SEduard Zingerman __description("variable-offset stack write, priv vs unpriv")
87*6b4a64baSAndrei Matei __success
88*6b4a64baSAndrei Matei /* Check that the maximum stack depth is correctly maintained according to the
89*6b4a64baSAndrei Matei * maximum possible variable offset.
90*6b4a64baSAndrei Matei */
91*6b4a64baSAndrei Matei __log_level(4) __msg("stack depth 16")
92*6b4a64baSAndrei Matei __failure_unpriv
93d15f5b68SEduard Zingerman /* Variable stack access is rejected for unprivileged.
94d15f5b68SEduard Zingerman */
95d15f5b68SEduard Zingerman __msg_unpriv("R2 variable stack access prohibited for !root")
96d15f5b68SEduard Zingerman __retval(0)
stack_write_priv_vs_unpriv(void)97d15f5b68SEduard Zingerman __naked void stack_write_priv_vs_unpriv(void)
98d15f5b68SEduard Zingerman {
99d15f5b68SEduard Zingerman asm volatile (" \
100d15f5b68SEduard Zingerman /* Get an unknown value */ \
101d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
102d15f5b68SEduard Zingerman /* Make it small and 8-byte aligned */ \
103d15f5b68SEduard Zingerman r2 &= 8; \
104d15f5b68SEduard Zingerman r2 -= 16; \
105*6b4a64baSAndrei Matei /* Add it to fp. We now have either fp-8 or \
106*6b4a64baSAndrei Matei * fp-16, but we don't know which \
107*6b4a64baSAndrei Matei */ \
108*6b4a64baSAndrei Matei r2 += r10; \
109*6b4a64baSAndrei Matei /* Dereference it for a stack write */ \
110*6b4a64baSAndrei Matei r0 = 0; \
111*6b4a64baSAndrei Matei *(u64*)(r2 + 0) = r0; \
112*6b4a64baSAndrei Matei exit; \
113*6b4a64baSAndrei Matei " ::: __clobber_all);
114*6b4a64baSAndrei Matei }
115*6b4a64baSAndrei Matei
116*6b4a64baSAndrei Matei /* Similar to the previous test, but this time also perform a read from the
117*6b4a64baSAndrei Matei * address written to with a variable offset. The read is allowed, showing that,
118*6b4a64baSAndrei Matei * after a variable-offset write, a priviledged program can read the slots that
119*6b4a64baSAndrei Matei * were in the range of that write (even if the verifier doesn't actually know if
120*6b4a64baSAndrei Matei * the slot being read was really written to or not.
121*6b4a64baSAndrei Matei *
122*6b4a64baSAndrei Matei * Despite this test being mostly a superset, the previous test is also kept for
123*6b4a64baSAndrei Matei * the sake of it checking the stack depth in the case where there is no read.
124*6b4a64baSAndrei Matei */
125*6b4a64baSAndrei Matei SEC("socket")
126*6b4a64baSAndrei Matei __description("variable-offset stack write followed by read")
127*6b4a64baSAndrei Matei __success
128*6b4a64baSAndrei Matei /* Check that the maximum stack depth is correctly maintained according to the
129*6b4a64baSAndrei Matei * maximum possible variable offset.
130*6b4a64baSAndrei Matei */
131*6b4a64baSAndrei Matei __log_level(4) __msg("stack depth 16")
132*6b4a64baSAndrei Matei __failure_unpriv
133*6b4a64baSAndrei Matei __msg_unpriv("R2 variable stack access prohibited for !root")
134*6b4a64baSAndrei Matei __retval(0)
stack_write_followed_by_read(void)135*6b4a64baSAndrei Matei __naked void stack_write_followed_by_read(void)
136*6b4a64baSAndrei Matei {
137*6b4a64baSAndrei Matei asm volatile (" \
138*6b4a64baSAndrei Matei /* Get an unknown value */ \
139*6b4a64baSAndrei Matei r2 = *(u32*)(r1 + 0); \
140*6b4a64baSAndrei Matei /* Make it small and 8-byte aligned */ \
141*6b4a64baSAndrei Matei r2 &= 8; \
142*6b4a64baSAndrei Matei r2 -= 16; \
143d15f5b68SEduard Zingerman /* Add it to fp. We now have either fp-8 or fp-16, but\
144d15f5b68SEduard Zingerman * we don't know which \
145d15f5b68SEduard Zingerman */ \
146d15f5b68SEduard Zingerman r2 += r10; \
147d15f5b68SEduard Zingerman /* Dereference it for a stack write */ \
148d15f5b68SEduard Zingerman r0 = 0; \
149d15f5b68SEduard Zingerman *(u64*)(r2 + 0) = r0; \
150*6b4a64baSAndrei Matei /* Now read from the address we just wrote. */ \
151d15f5b68SEduard Zingerman r3 = *(u64*)(r2 + 0); \
152d15f5b68SEduard Zingerman r0 = 0; \
153d15f5b68SEduard Zingerman exit; \
154d15f5b68SEduard Zingerman " ::: __clobber_all);
155d15f5b68SEduard Zingerman }
156d15f5b68SEduard Zingerman
157d15f5b68SEduard Zingerman SEC("socket")
158d15f5b68SEduard Zingerman __description("variable-offset stack write clobbers spilled regs")
159d15f5b68SEduard Zingerman __failure
160d15f5b68SEduard Zingerman /* In the priviledged case, dereferencing a spilled-and-then-filled
161d15f5b68SEduard Zingerman * register is rejected because the previous variable offset stack
162d15f5b68SEduard Zingerman * write might have overwritten the spilled pointer (i.e. we lose track
163d15f5b68SEduard Zingerman * of the spilled register when we analyze the write).
164d15f5b68SEduard Zingerman */
165d15f5b68SEduard Zingerman __msg("R2 invalid mem access 'scalar'")
166d15f5b68SEduard Zingerman __failure_unpriv
167d15f5b68SEduard Zingerman /* The unprivileged case is not too interesting; variable
168d15f5b68SEduard Zingerman * stack access is rejected.
169d15f5b68SEduard Zingerman */
170d15f5b68SEduard Zingerman __msg_unpriv("R2 variable stack access prohibited for !root")
stack_write_clobbers_spilled_regs(void)171d15f5b68SEduard Zingerman __naked void stack_write_clobbers_spilled_regs(void)
172d15f5b68SEduard Zingerman {
173d15f5b68SEduard Zingerman asm volatile (" \
174d15f5b68SEduard Zingerman /* Dummy instruction; needed because we need to patch the next one\
175d15f5b68SEduard Zingerman * and we can't patch the first instruction. \
176d15f5b68SEduard Zingerman */ \
177d15f5b68SEduard Zingerman r6 = 0; \
178d15f5b68SEduard Zingerman /* Make R0 a map ptr */ \
179d15f5b68SEduard Zingerman r0 = %[map_hash_8b] ll; \
180d15f5b68SEduard Zingerman /* Get an unknown value */ \
181d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
182d15f5b68SEduard Zingerman /* Make it small and 8-byte aligned */ \
183d15f5b68SEduard Zingerman r2 &= 8; \
184d15f5b68SEduard Zingerman r2 -= 16; \
185d15f5b68SEduard Zingerman /* Add it to fp. We now have either fp-8 or fp-16, but\
186d15f5b68SEduard Zingerman * we don't know which. \
187d15f5b68SEduard Zingerman */ \
188d15f5b68SEduard Zingerman r2 += r10; \
189d15f5b68SEduard Zingerman /* Spill R0(map ptr) into stack */ \
190d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r0; \
191d15f5b68SEduard Zingerman /* Dereference the unknown value for a stack write */\
192d15f5b68SEduard Zingerman r0 = 0; \
193d15f5b68SEduard Zingerman *(u64*)(r2 + 0) = r0; \
194d15f5b68SEduard Zingerman /* Fill the register back into R2 */ \
195d15f5b68SEduard Zingerman r2 = *(u64*)(r10 - 8); \
196d15f5b68SEduard Zingerman /* Try to dereference R2 for a memory load */ \
197d15f5b68SEduard Zingerman r0 = *(u64*)(r2 + 8); \
198d15f5b68SEduard Zingerman exit; \
199d15f5b68SEduard Zingerman " :
200d15f5b68SEduard Zingerman : __imm_addr(map_hash_8b)
201d15f5b68SEduard Zingerman : __clobber_all);
202d15f5b68SEduard Zingerman }
203d15f5b68SEduard Zingerman
204d15f5b68SEduard Zingerman SEC("sockops")
205d15f5b68SEduard Zingerman __description("indirect variable-offset stack access, unbounded")
206d15f5b68SEduard Zingerman __failure __msg("invalid unbounded variable-offset indirect access to stack R4")
variable_offset_stack_access_unbounded(void)207d15f5b68SEduard Zingerman __naked void variable_offset_stack_access_unbounded(void)
208d15f5b68SEduard Zingerman {
209d15f5b68SEduard Zingerman asm volatile (" \
210d15f5b68SEduard Zingerman r2 = 6; \
211d15f5b68SEduard Zingerman r3 = 28; \
212d15f5b68SEduard Zingerman /* Fill the top 16 bytes of the stack. */ \
213d15f5b68SEduard Zingerman r4 = 0; \
214d15f5b68SEduard Zingerman *(u64*)(r10 - 16) = r4; \
215d15f5b68SEduard Zingerman r4 = 0; \
216d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r4; \
217d15f5b68SEduard Zingerman /* Get an unknown value. */ \
218d15f5b68SEduard Zingerman r4 = *(u64*)(r1 + %[bpf_sock_ops_bytes_received]);\
219d15f5b68SEduard Zingerman /* Check the lower bound but don't check the upper one. */\
220d15f5b68SEduard Zingerman if r4 s< 0 goto l0_%=; \
221d15f5b68SEduard Zingerman /* Point the lower bound to initialized stack. Offset is now in range\
222d15f5b68SEduard Zingerman * from fp-16 to fp+0x7fffffffffffffef, i.e. max value is unbounded.\
223d15f5b68SEduard Zingerman */ \
224d15f5b68SEduard Zingerman r4 -= 16; \
225d15f5b68SEduard Zingerman r4 += r10; \
226d15f5b68SEduard Zingerman r5 = 8; \
227d15f5b68SEduard Zingerman /* Dereference it indirectly. */ \
228d15f5b68SEduard Zingerman call %[bpf_getsockopt]; \
229d15f5b68SEduard Zingerman l0_%=: r0 = 0; \
230d15f5b68SEduard Zingerman exit; \
231d15f5b68SEduard Zingerman " :
232d15f5b68SEduard Zingerman : __imm(bpf_getsockopt),
233d15f5b68SEduard Zingerman __imm_const(bpf_sock_ops_bytes_received, offsetof(struct bpf_sock_ops, bytes_received))
234d15f5b68SEduard Zingerman : __clobber_all);
235d15f5b68SEduard Zingerman }
236d15f5b68SEduard Zingerman
237d15f5b68SEduard Zingerman SEC("lwt_in")
238d15f5b68SEduard Zingerman __description("indirect variable-offset stack access, max out of bound")
239d15f5b68SEduard Zingerman __failure __msg("invalid variable-offset indirect access to stack R2")
access_max_out_of_bound(void)240d15f5b68SEduard Zingerman __naked void access_max_out_of_bound(void)
241d15f5b68SEduard Zingerman {
242d15f5b68SEduard Zingerman asm volatile (" \
243d15f5b68SEduard Zingerman /* Fill the top 8 bytes of the stack */ \
244d15f5b68SEduard Zingerman r2 = 0; \
245d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r2; \
246d15f5b68SEduard Zingerman /* Get an unknown value */ \
247d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
248d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned */ \
249d15f5b68SEduard Zingerman r2 &= 4; \
250d15f5b68SEduard Zingerman r2 -= 8; \
251d15f5b68SEduard Zingerman /* add it to fp. We now have either fp-4 or fp-8, but\
252d15f5b68SEduard Zingerman * we don't know which \
253d15f5b68SEduard Zingerman */ \
254d15f5b68SEduard Zingerman r2 += r10; \
255d15f5b68SEduard Zingerman /* dereference it indirectly */ \
256d15f5b68SEduard Zingerman r1 = %[map_hash_8b] ll; \
257d15f5b68SEduard Zingerman call %[bpf_map_lookup_elem]; \
258d15f5b68SEduard Zingerman r0 = 0; \
259d15f5b68SEduard Zingerman exit; \
260d15f5b68SEduard Zingerman " :
261d15f5b68SEduard Zingerman : __imm(bpf_map_lookup_elem),
262d15f5b68SEduard Zingerman __imm_addr(map_hash_8b)
263d15f5b68SEduard Zingerman : __clobber_all);
264d15f5b68SEduard Zingerman }
265d15f5b68SEduard Zingerman
266e28bd359SAndrei Matei /* Similar to the test above, but this time check the special case of a
267e28bd359SAndrei Matei * zero-sized stack access. We used to have a bug causing crashes for zero-sized
268e28bd359SAndrei Matei * out-of-bounds accesses.
269e28bd359SAndrei Matei */
270e28bd359SAndrei Matei SEC("socket")
271e28bd359SAndrei Matei __description("indirect variable-offset stack access, zero-sized, max out of bound")
272e28bd359SAndrei Matei __failure __msg("invalid variable-offset indirect access to stack R1")
zero_sized_access_max_out_of_bound(void)273e28bd359SAndrei Matei __naked void zero_sized_access_max_out_of_bound(void)
274e28bd359SAndrei Matei {
275e28bd359SAndrei Matei asm volatile (" \
276e28bd359SAndrei Matei r0 = 0; \
277e28bd359SAndrei Matei /* Fill some stack */ \
278e28bd359SAndrei Matei *(u64*)(r10 - 16) = r0; \
279e28bd359SAndrei Matei *(u64*)(r10 - 8) = r0; \
280e28bd359SAndrei Matei /* Get an unknown value */ \
281e28bd359SAndrei Matei r1 = *(u32*)(r1 + 0); \
282e28bd359SAndrei Matei r1 &= 63; \
283e28bd359SAndrei Matei r1 += -16; \
284e28bd359SAndrei Matei /* r1 is now anywhere in [-16,48) */ \
285e28bd359SAndrei Matei r1 += r10; \
286e28bd359SAndrei Matei r2 = 0; \
287e28bd359SAndrei Matei r3 = 0; \
288e28bd359SAndrei Matei call %[bpf_probe_read_kernel]; \
289e28bd359SAndrei Matei exit; \
290e28bd359SAndrei Matei " :
291e28bd359SAndrei Matei : __imm(bpf_probe_read_kernel)
292e28bd359SAndrei Matei : __clobber_all);
293e28bd359SAndrei Matei }
294e28bd359SAndrei Matei
295d15f5b68SEduard Zingerman SEC("lwt_in")
296d15f5b68SEduard Zingerman __description("indirect variable-offset stack access, min out of bound")
297d15f5b68SEduard Zingerman __failure __msg("invalid variable-offset indirect access to stack R2")
access_min_out_of_bound(void)298d15f5b68SEduard Zingerman __naked void access_min_out_of_bound(void)
299d15f5b68SEduard Zingerman {
300d15f5b68SEduard Zingerman asm volatile (" \
301d15f5b68SEduard Zingerman /* Fill the top 8 bytes of the stack */ \
302d15f5b68SEduard Zingerman r2 = 0; \
303d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r2; \
304d15f5b68SEduard Zingerman /* Get an unknown value */ \
305d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
306d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned */ \
307d15f5b68SEduard Zingerman r2 &= 4; \
308d15f5b68SEduard Zingerman r2 -= 516; \
309d15f5b68SEduard Zingerman /* add it to fp. We now have either fp-516 or fp-512, but\
310d15f5b68SEduard Zingerman * we don't know which \
311d15f5b68SEduard Zingerman */ \
312d15f5b68SEduard Zingerman r2 += r10; \
313d15f5b68SEduard Zingerman /* dereference it indirectly */ \
314d15f5b68SEduard Zingerman r1 = %[map_hash_8b] ll; \
315d15f5b68SEduard Zingerman call %[bpf_map_lookup_elem]; \
316d15f5b68SEduard Zingerman r0 = 0; \
317d15f5b68SEduard Zingerman exit; \
318d15f5b68SEduard Zingerman " :
319d15f5b68SEduard Zingerman : __imm(bpf_map_lookup_elem),
320d15f5b68SEduard Zingerman __imm_addr(map_hash_8b)
321d15f5b68SEduard Zingerman : __clobber_all);
322d15f5b68SEduard Zingerman }
323d15f5b68SEduard Zingerman
324*6b4a64baSAndrei Matei SEC("cgroup/skb")
325d15f5b68SEduard Zingerman __description("indirect variable-offset stack access, min_off < min_initialized")
326*6b4a64baSAndrei Matei __success
327*6b4a64baSAndrei Matei __failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
access_min_off_min_initialized(void)328d15f5b68SEduard Zingerman __naked void access_min_off_min_initialized(void)
329d15f5b68SEduard Zingerman {
330d15f5b68SEduard Zingerman asm volatile (" \
331d15f5b68SEduard Zingerman /* Fill only the top 8 bytes of the stack. */ \
332d15f5b68SEduard Zingerman r2 = 0; \
333d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r2; \
334d15f5b68SEduard Zingerman /* Get an unknown value */ \
335d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
336d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned. */ \
337d15f5b68SEduard Zingerman r2 &= 4; \
338d15f5b68SEduard Zingerman r2 -= 16; \
339d15f5b68SEduard Zingerman /* Add it to fp. We now have either fp-12 or fp-16, but we don't know\
340d15f5b68SEduard Zingerman * which. fp-16 size 8 is partially uninitialized stack.\
341d15f5b68SEduard Zingerman */ \
342d15f5b68SEduard Zingerman r2 += r10; \
343d15f5b68SEduard Zingerman /* Dereference it indirectly. */ \
344d15f5b68SEduard Zingerman r1 = %[map_hash_8b] ll; \
345d15f5b68SEduard Zingerman call %[bpf_map_lookup_elem]; \
346d15f5b68SEduard Zingerman r0 = 0; \
347d15f5b68SEduard Zingerman exit; \
348d15f5b68SEduard Zingerman " :
349d15f5b68SEduard Zingerman : __imm(bpf_map_lookup_elem),
350d15f5b68SEduard Zingerman __imm_addr(map_hash_8b)
351d15f5b68SEduard Zingerman : __clobber_all);
352d15f5b68SEduard Zingerman }
353d15f5b68SEduard Zingerman
354d15f5b68SEduard Zingerman SEC("cgroup/skb")
355d15f5b68SEduard Zingerman __description("indirect variable-offset stack access, priv vs unpriv")
356d15f5b68SEduard Zingerman __success __failure_unpriv
357d15f5b68SEduard Zingerman __msg_unpriv("R2 variable stack access prohibited for !root")
358d15f5b68SEduard Zingerman __retval(0)
stack_access_priv_vs_unpriv(void)359d15f5b68SEduard Zingerman __naked void stack_access_priv_vs_unpriv(void)
360d15f5b68SEduard Zingerman {
361d15f5b68SEduard Zingerman asm volatile (" \
362d15f5b68SEduard Zingerman /* Fill the top 16 bytes of the stack. */ \
363d15f5b68SEduard Zingerman r2 = 0; \
364d15f5b68SEduard Zingerman *(u64*)(r10 - 16) = r2; \
365d15f5b68SEduard Zingerman r2 = 0; \
366d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r2; \
367d15f5b68SEduard Zingerman /* Get an unknown value. */ \
368d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
369d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned. */ \
370d15f5b68SEduard Zingerman r2 &= 4; \
371d15f5b68SEduard Zingerman r2 -= 16; \
372d15f5b68SEduard Zingerman /* Add it to fp. We now have either fp-12 or fp-16, we don't know\
373d15f5b68SEduard Zingerman * which, but either way it points to initialized stack.\
374d15f5b68SEduard Zingerman */ \
375d15f5b68SEduard Zingerman r2 += r10; \
376d15f5b68SEduard Zingerman /* Dereference it indirectly. */ \
377d15f5b68SEduard Zingerman r1 = %[map_hash_8b] ll; \
378d15f5b68SEduard Zingerman call %[bpf_map_lookup_elem]; \
379d15f5b68SEduard Zingerman r0 = 0; \
380d15f5b68SEduard Zingerman exit; \
381d15f5b68SEduard Zingerman " :
382d15f5b68SEduard Zingerman : __imm(bpf_map_lookup_elem),
383d15f5b68SEduard Zingerman __imm_addr(map_hash_8b)
384d15f5b68SEduard Zingerman : __clobber_all);
385d15f5b68SEduard Zingerman }
386d15f5b68SEduard Zingerman
387d15f5b68SEduard Zingerman SEC("lwt_in")
388d15f5b68SEduard Zingerman __description("indirect variable-offset stack access, ok")
389d15f5b68SEduard Zingerman __success __retval(0)
variable_offset_stack_access_ok(void)390d15f5b68SEduard Zingerman __naked void variable_offset_stack_access_ok(void)
391d15f5b68SEduard Zingerman {
392d15f5b68SEduard Zingerman asm volatile (" \
393d15f5b68SEduard Zingerman /* Fill the top 16 bytes of the stack. */ \
394d15f5b68SEduard Zingerman r2 = 0; \
395d15f5b68SEduard Zingerman *(u64*)(r10 - 16) = r2; \
396d15f5b68SEduard Zingerman r2 = 0; \
397d15f5b68SEduard Zingerman *(u64*)(r10 - 8) = r2; \
398d15f5b68SEduard Zingerman /* Get an unknown value. */ \
399d15f5b68SEduard Zingerman r2 = *(u32*)(r1 + 0); \
400d15f5b68SEduard Zingerman /* Make it small and 4-byte aligned. */ \
401d15f5b68SEduard Zingerman r2 &= 4; \
402d15f5b68SEduard Zingerman r2 -= 16; \
403d15f5b68SEduard Zingerman /* Add it to fp. We now have either fp-12 or fp-16, we don't know\
404d15f5b68SEduard Zingerman * which, but either way it points to initialized stack.\
405d15f5b68SEduard Zingerman */ \
406d15f5b68SEduard Zingerman r2 += r10; \
407d15f5b68SEduard Zingerman /* Dereference it indirectly. */ \
408d15f5b68SEduard Zingerman r1 = %[map_hash_8b] ll; \
409d15f5b68SEduard Zingerman call %[bpf_map_lookup_elem]; \
410d15f5b68SEduard Zingerman r0 = 0; \
411d15f5b68SEduard Zingerman exit; \
412d15f5b68SEduard Zingerman " :
413d15f5b68SEduard Zingerman : __imm(bpf_map_lookup_elem),
414d15f5b68SEduard Zingerman __imm_addr(map_hash_8b)
415d15f5b68SEduard Zingerman : __clobber_all);
416d15f5b68SEduard Zingerman }
417d15f5b68SEduard Zingerman
418d15f5b68SEduard Zingerman char _license[] SEC("license") = "GPL";
419