1 // SPDX-License-Identifier: GPL-2.0
2 /* Converted from tools/testing/selftests/bpf/verifier/helper_restricted.c */
3 
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7 
8 struct val {
9 	int cnt;
10 	struct bpf_spin_lock l;
11 };
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_ARRAY);
15 	__uint(max_entries, 1);
16 	__type(key, int);
17 	__type(value, struct val);
18 } map_spin_lock SEC(".maps");
19 
20 struct timer {
21 	struct bpf_timer t;
22 };
23 
24 struct {
25 	__uint(type, BPF_MAP_TYPE_ARRAY);
26 	__uint(max_entries, 1);
27 	__type(key, int);
28 	__type(value, struct timer);
29 } map_timer SEC(".maps");
30 
31 SEC("kprobe")
32 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_KPROBE")
33 __failure __msg("unknown func bpf_ktime_get_coarse_ns")
34 __naked void in_bpf_prog_type_kprobe_1(void)
35 {
36 	asm volatile ("					\
37 	call %[bpf_ktime_get_coarse_ns];		\
38 	r0 = 0;						\
39 	exit;						\
40 "	:
41 	: __imm(bpf_ktime_get_coarse_ns)
42 	: __clobber_all);
43 }
44 
45 SEC("tracepoint")
46 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_TRACEPOINT")
47 __failure __msg("unknown func bpf_ktime_get_coarse_ns")
48 __naked void in_bpf_prog_type_tracepoint_1(void)
49 {
50 	asm volatile ("					\
51 	call %[bpf_ktime_get_coarse_ns];		\
52 	r0 = 0;						\
53 	exit;						\
54 "	:
55 	: __imm(bpf_ktime_get_coarse_ns)
56 	: __clobber_all);
57 }
58 
59 SEC("perf_event")
60 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_PERF_EVENT")
61 __failure __msg("unknown func bpf_ktime_get_coarse_ns")
62 __naked void bpf_prog_type_perf_event_1(void)
63 {
64 	asm volatile ("					\
65 	call %[bpf_ktime_get_coarse_ns];		\
66 	r0 = 0;						\
67 	exit;						\
68 "	:
69 	: __imm(bpf_ktime_get_coarse_ns)
70 	: __clobber_all);
71 }
72 
73 SEC("raw_tracepoint")
74 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_RAW_TRACEPOINT")
75 __failure __msg("unknown func bpf_ktime_get_coarse_ns")
76 __naked void bpf_prog_type_raw_tracepoint_1(void)
77 {
78 	asm volatile ("					\
79 	call %[bpf_ktime_get_coarse_ns];		\
80 	r0 = 0;						\
81 	exit;						\
82 "	:
83 	: __imm(bpf_ktime_get_coarse_ns)
84 	: __clobber_all);
85 }
86 
87 SEC("kprobe")
88 __description("bpf_timer_init isn restricted in BPF_PROG_TYPE_KPROBE")
89 __failure __msg("tracing progs cannot use bpf_timer yet")
90 __naked void in_bpf_prog_type_kprobe_2(void)
91 {
92 	asm volatile ("					\
93 	r2 = r10;					\
94 	r2 += -8;					\
95 	r1 = 0;						\
96 	*(u64*)(r2 + 0) = r1;				\
97 	r1 = %[map_timer] ll;				\
98 	call %[bpf_map_lookup_elem];			\
99 	if r0 == 0 goto l0_%=;				\
100 	r1 = r0;					\
101 	r2 = %[map_timer] ll;				\
102 	r3 = 1;						\
103 l0_%=:	call %[bpf_timer_init];				\
104 	exit;						\
105 "	:
106 	: __imm(bpf_map_lookup_elem),
107 	  __imm(bpf_timer_init),
108 	  __imm_addr(map_timer)
109 	: __clobber_all);
110 }
111 
112 SEC("perf_event")
113 __description("bpf_timer_init is forbidden in BPF_PROG_TYPE_PERF_EVENT")
114 __failure __msg("tracing progs cannot use bpf_timer yet")
115 __naked void bpf_prog_type_perf_event_2(void)
116 {
117 	asm volatile ("					\
118 	r2 = r10;					\
119 	r2 += -8;					\
120 	r1 = 0;						\
121 	*(u64*)(r2 + 0) = r1;				\
122 	r1 = %[map_timer] ll;				\
123 	call %[bpf_map_lookup_elem];			\
124 	if r0 == 0 goto l0_%=;				\
125 	r1 = r0;					\
126 	r2 = %[map_timer] ll;				\
127 	r3 = 1;						\
128 l0_%=:	call %[bpf_timer_init];				\
129 	exit;						\
130 "	:
131 	: __imm(bpf_map_lookup_elem),
132 	  __imm(bpf_timer_init),
133 	  __imm_addr(map_timer)
134 	: __clobber_all);
135 }
136 
137 SEC("tracepoint")
138 __description("bpf_timer_init is forbidden in BPF_PROG_TYPE_TRACEPOINT")
139 __failure __msg("tracing progs cannot use bpf_timer yet")
140 __naked void in_bpf_prog_type_tracepoint_2(void)
141 {
142 	asm volatile ("					\
143 	r2 = r10;					\
144 	r2 += -8;					\
145 	r1 = 0;						\
146 	*(u64*)(r2 + 0) = r1;				\
147 	r1 = %[map_timer] ll;				\
148 	call %[bpf_map_lookup_elem];			\
149 	if r0 == 0 goto l0_%=;				\
150 	r1 = r0;					\
151 	r2 = %[map_timer] ll;				\
152 	r3 = 1;						\
153 l0_%=:	call %[bpf_timer_init];				\
154 	exit;						\
155 "	:
156 	: __imm(bpf_map_lookup_elem),
157 	  __imm(bpf_timer_init),
158 	  __imm_addr(map_timer)
159 	: __clobber_all);
160 }
161 
162 SEC("raw_tracepoint")
163 __description("bpf_timer_init is forbidden in BPF_PROG_TYPE_RAW_TRACEPOINT")
164 __failure __msg("tracing progs cannot use bpf_timer yet")
165 __naked void bpf_prog_type_raw_tracepoint_2(void)
166 {
167 	asm volatile ("					\
168 	r2 = r10;					\
169 	r2 += -8;					\
170 	r1 = 0;						\
171 	*(u64*)(r2 + 0) = r1;				\
172 	r1 = %[map_timer] ll;				\
173 	call %[bpf_map_lookup_elem];			\
174 	if r0 == 0 goto l0_%=;				\
175 	r1 = r0;					\
176 	r2 = %[map_timer] ll;				\
177 	r3 = 1;						\
178 l0_%=:	call %[bpf_timer_init];				\
179 	exit;						\
180 "	:
181 	: __imm(bpf_map_lookup_elem),
182 	  __imm(bpf_timer_init),
183 	  __imm_addr(map_timer)
184 	: __clobber_all);
185 }
186 
187 SEC("kprobe")
188 __description("bpf_spin_lock is forbidden in BPF_PROG_TYPE_KPROBE")
189 __failure __msg("tracing progs cannot use bpf_spin_lock yet")
190 __naked void in_bpf_prog_type_kprobe_3(void)
191 {
192 	asm volatile ("					\
193 	r2 = r10;					\
194 	r2 += -8;					\
195 	r1 = 0;						\
196 	*(u64*)(r2 + 0) = r1;				\
197 	r1 = %[map_spin_lock] ll;			\
198 	call %[bpf_map_lookup_elem];			\
199 	if r0 == 0 goto l0_%=;				\
200 	r1 = r0;					\
201 	call %[bpf_spin_lock];				\
202 l0_%=:	exit;						\
203 "	:
204 	: __imm(bpf_map_lookup_elem),
205 	  __imm(bpf_spin_lock),
206 	  __imm_addr(map_spin_lock)
207 	: __clobber_all);
208 }
209 
210 SEC("tracepoint")
211 __description("bpf_spin_lock is forbidden in BPF_PROG_TYPE_TRACEPOINT")
212 __failure __msg("tracing progs cannot use bpf_spin_lock yet")
213 __naked void in_bpf_prog_type_tracepoint_3(void)
214 {
215 	asm volatile ("					\
216 	r2 = r10;					\
217 	r2 += -8;					\
218 	r1 = 0;						\
219 	*(u64*)(r2 + 0) = r1;				\
220 	r1 = %[map_spin_lock] ll;			\
221 	call %[bpf_map_lookup_elem];			\
222 	if r0 == 0 goto l0_%=;				\
223 	r1 = r0;					\
224 	call %[bpf_spin_lock];				\
225 l0_%=:	exit;						\
226 "	:
227 	: __imm(bpf_map_lookup_elem),
228 	  __imm(bpf_spin_lock),
229 	  __imm_addr(map_spin_lock)
230 	: __clobber_all);
231 }
232 
233 SEC("perf_event")
234 __description("bpf_spin_lock is forbidden in BPF_PROG_TYPE_PERF_EVENT")
235 __failure __msg("tracing progs cannot use bpf_spin_lock yet")
236 __naked void bpf_prog_type_perf_event_3(void)
237 {
238 	asm volatile ("					\
239 	r2 = r10;					\
240 	r2 += -8;					\
241 	r1 = 0;						\
242 	*(u64*)(r2 + 0) = r1;				\
243 	r1 = %[map_spin_lock] ll;			\
244 	call %[bpf_map_lookup_elem];			\
245 	if r0 == 0 goto l0_%=;				\
246 	r1 = r0;					\
247 	call %[bpf_spin_lock];				\
248 l0_%=:	exit;						\
249 "	:
250 	: __imm(bpf_map_lookup_elem),
251 	  __imm(bpf_spin_lock),
252 	  __imm_addr(map_spin_lock)
253 	: __clobber_all);
254 }
255 
256 SEC("raw_tracepoint")
257 __description("bpf_spin_lock is forbidden in BPF_PROG_TYPE_RAW_TRACEPOINT")
258 __failure __msg("tracing progs cannot use bpf_spin_lock yet")
259 __naked void bpf_prog_type_raw_tracepoint_3(void)
260 {
261 	asm volatile ("					\
262 	r2 = r10;					\
263 	r2 += -8;					\
264 	r1 = 0;						\
265 	*(u64*)(r2 + 0) = r1;				\
266 	r1 = %[map_spin_lock] ll;			\
267 	call %[bpf_map_lookup_elem];			\
268 	if r0 == 0 goto l0_%=;				\
269 	r1 = r0;					\
270 	call %[bpf_spin_lock];				\
271 l0_%=:	exit;						\
272 "	:
273 	: __imm(bpf_map_lookup_elem),
274 	  __imm(bpf_spin_lock),
275 	  __imm_addr(map_spin_lock)
276 	: __clobber_all);
277 }
278 
279 char _license[] SEC("license") = "GPL";
280