]>
Commit | Line | Data |
---|---|---|
179a0cc4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2541517c | 2 | /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com |
0515e599 | 3 | * Copyright (c) 2016 Facebook |
2541517c AS |
4 | */ |
5 | #include <linux/kernel.h> | |
6 | #include <linux/types.h> | |
7 | #include <linux/slab.h> | |
8 | #include <linux/bpf.h> | |
0515e599 | 9 | #include <linux/bpf_perf_event.h> |
c4d0bfb4 | 10 | #include <linux/btf.h> |
2541517c AS |
11 | #include <linux/filter.h> |
12 | #include <linux/uaccess.h> | |
9c959c86 | 13 | #include <linux/ctype.h> |
9802d865 | 14 | #include <linux/kprobes.h> |
ac5a72ea | 15 | #include <linux/spinlock.h> |
41bdc4b4 | 16 | #include <linux/syscalls.h> |
540adea3 | 17 | #include <linux/error-injection.h> |
c9a0f3b8 | 18 | #include <linux/btf_ids.h> |
9802d865 | 19 | |
c4d0bfb4 AM |
20 | #include <uapi/linux/bpf.h> |
21 | #include <uapi/linux/btf.h> | |
22 | ||
c7b6f29b NA |
23 | #include <asm/tlb.h> |
24 | ||
9802d865 | 25 | #include "trace_probe.h" |
2541517c AS |
26 | #include "trace.h" |
27 | ||
ac5a72ea AM |
28 | #define CREATE_TRACE_POINTS |
29 | #include "bpf_trace.h" | |
30 | ||
e672db03 SF |
31 | #define bpf_event_rcu_dereference(p) \ |
32 | rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) | |
33 | ||
a38d1107 MM |
34 | #ifdef CONFIG_MODULES |
35 | struct bpf_trace_module { | |
36 | struct module *module; | |
37 | struct list_head list; | |
38 | }; | |
39 | ||
40 | static LIST_HEAD(bpf_trace_modules); | |
41 | static DEFINE_MUTEX(bpf_module_mutex); | |
42 | ||
43 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) | |
44 | { | |
45 | struct bpf_raw_event_map *btp, *ret = NULL; | |
46 | struct bpf_trace_module *btm; | |
47 | unsigned int i; | |
48 | ||
49 | mutex_lock(&bpf_module_mutex); | |
50 | list_for_each_entry(btm, &bpf_trace_modules, list) { | |
51 | for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { | |
52 | btp = &btm->module->bpf_raw_events[i]; | |
53 | if (!strcmp(btp->tp->name, name)) { | |
54 | if (try_module_get(btm->module)) | |
55 | ret = btp; | |
56 | goto out; | |
57 | } | |
58 | } | |
59 | } | |
60 | out: | |
61 | mutex_unlock(&bpf_module_mutex); | |
62 | return ret; | |
63 | } | |
64 | #else | |
65 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) | |
66 | { | |
67 | return NULL; | |
68 | } | |
69 | #endif /* CONFIG_MODULES */ | |
70 | ||
035226b9 | 71 | u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
c195651e | 72 | u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
035226b9 | 73 | |
eb411377 AM |
74 | static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, |
75 | u64 flags, const struct btf **btf, | |
76 | s32 *btf_id); | |
77 | ||
2541517c AS |
78 | /** |
79 | * trace_call_bpf - invoke BPF program | |
e87c6bc3 | 80 | * @call: tracepoint event |
2541517c AS |
81 | * @ctx: opaque context pointer |
82 | * | |
83 | * kprobe handlers execute BPF programs via this helper. | |
84 | * Can be used from static tracepoints in the future. | |
85 | * | |
86 | * Return: BPF programs always return an integer which is interpreted by | |
87 | * kprobe handler as: | |
88 | * 0 - return from kprobe (event is filtered out) | |
89 | * 1 - store kprobe event into ring buffer | |
90 | * Other values are reserved and currently alias to 1 | |
91 | */ | |
e87c6bc3 | 92 | unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) |
2541517c AS |
93 | { |
94 | unsigned int ret; | |
95 | ||
96 | if (in_nmi()) /* not supported yet */ | |
97 | return 1; | |
98 | ||
b0a81b94 | 99 | cant_sleep(); |
2541517c AS |
100 | |
101 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { | |
102 | /* | |
103 | * since some bpf program is already running on this cpu, | |
104 | * don't call into another bpf program (same or different) | |
105 | * and don't send kprobe event into ring-buffer, | |
106 | * so return zero here | |
107 | */ | |
108 | ret = 0; | |
109 | goto out; | |
110 | } | |
111 | ||
e87c6bc3 YS |
112 | /* |
113 | * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock | |
114 | * to all call sites, we did a bpf_prog_array_valid() there to check | |
115 | * whether call->prog_array is empty or not, which is | |
116 | * a heurisitc to speed up execution. | |
117 | * | |
118 | * If bpf_prog_array_valid() fetched prog_array was | |
119 | * non-NULL, we go into trace_call_bpf() and do the actual | |
120 | * proper rcu_dereference() under RCU lock. | |
121 | * If it turns out that prog_array is NULL then, we bail out. | |
122 | * For the opposite, if the bpf_prog_array_valid() fetched pointer | |
123 | * was NULL, you'll skip the prog_array with the risk of missing | |
124 | * out of events when it was updated in between this and the | |
125 | * rcu_dereference() which is accepted risk. | |
126 | */ | |
127 | ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN); | |
2541517c AS |
128 | |
129 | out: | |
130 | __this_cpu_dec(bpf_prog_active); | |
2541517c AS |
131 | |
132 | return ret; | |
133 | } | |
2541517c | 134 | |
9802d865 JB |
135 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
136 | BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) | |
137 | { | |
9802d865 | 138 | regs_set_return_value(regs, rc); |
540adea3 | 139 | override_function_with_return(regs); |
9802d865 JB |
140 | return 0; |
141 | } | |
142 | ||
143 | static const struct bpf_func_proto bpf_override_return_proto = { | |
144 | .func = bpf_override_return, | |
145 | .gpl_only = true, | |
146 | .ret_type = RET_INTEGER, | |
147 | .arg1_type = ARG_PTR_TO_CTX, | |
148 | .arg2_type = ARG_ANYTHING, | |
149 | }; | |
150 | #endif | |
151 | ||
8d92db5c CH |
152 | static __always_inline int |
153 | bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr) | |
2541517c | 154 | { |
8d92db5c | 155 | int ret; |
2541517c | 156 | |
c0ee37e8 | 157 | ret = copy_from_user_nofault(dst, unsafe_ptr, size); |
6ae08ae3 DB |
158 | if (unlikely(ret < 0)) |
159 | memset(dst, 0, size); | |
6ae08ae3 DB |
160 | return ret; |
161 | } | |
162 | ||
8d92db5c CH |
163 | BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, |
164 | const void __user *, unsafe_ptr) | |
165 | { | |
166 | return bpf_probe_read_user_common(dst, size, unsafe_ptr); | |
167 | } | |
168 | ||
f470378c | 169 | const struct bpf_func_proto bpf_probe_read_user_proto = { |
6ae08ae3 DB |
170 | .func = bpf_probe_read_user, |
171 | .gpl_only = true, | |
172 | .ret_type = RET_INTEGER, | |
173 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
174 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
175 | .arg3_type = ARG_ANYTHING, | |
176 | }; | |
177 | ||
8d92db5c CH |
178 | static __always_inline int |
179 | bpf_probe_read_user_str_common(void *dst, u32 size, | |
180 | const void __user *unsafe_ptr) | |
6ae08ae3 | 181 | { |
8d92db5c | 182 | int ret; |
6ae08ae3 | 183 | |
6fa6d280 DX |
184 | /* |
185 | * NB: We rely on strncpy_from_user() not copying junk past the NUL | |
186 | * terminator into `dst`. | |
187 | * | |
188 | * strncpy_from_user() does long-sized strides in the fast path. If the | |
189 | * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`, | |
190 | * then there could be junk after the NUL in `dst`. If user takes `dst` | |
191 | * and keys a hash map with it, then semantically identical strings can | |
192 | * occupy multiple entries in the map. | |
193 | */ | |
8d92db5c | 194 | ret = strncpy_from_user_nofault(dst, unsafe_ptr, size); |
6ae08ae3 DB |
195 | if (unlikely(ret < 0)) |
196 | memset(dst, 0, size); | |
6ae08ae3 DB |
197 | return ret; |
198 | } | |
199 | ||
8d92db5c CH |
200 | BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size, |
201 | const void __user *, unsafe_ptr) | |
202 | { | |
203 | return bpf_probe_read_user_str_common(dst, size, unsafe_ptr); | |
204 | } | |
205 | ||
f470378c | 206 | const struct bpf_func_proto bpf_probe_read_user_str_proto = { |
6ae08ae3 DB |
207 | .func = bpf_probe_read_user_str, |
208 | .gpl_only = true, | |
209 | .ret_type = RET_INTEGER, | |
210 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
211 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
212 | .arg3_type = ARG_ANYTHING, | |
213 | }; | |
214 | ||
215 | static __always_inline int | |
8d92db5c | 216 | bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr) |
6ae08ae3 DB |
217 | { |
218 | int ret = security_locked_down(LOCKDOWN_BPF_READ); | |
9d1f8be5 | 219 | |
6ae08ae3 | 220 | if (unlikely(ret < 0)) |
8d92db5c | 221 | goto fail; |
fe557319 | 222 | ret = copy_from_kernel_nofault(dst, unsafe_ptr, size); |
074f528e | 223 | if (unlikely(ret < 0)) |
8d92db5c CH |
224 | goto fail; |
225 | return ret; | |
226 | fail: | |
227 | memset(dst, 0, size); | |
6ae08ae3 DB |
228 | return ret; |
229 | } | |
074f528e | 230 | |
6ae08ae3 DB |
231 | BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size, |
232 | const void *, unsafe_ptr) | |
233 | { | |
8d92db5c | 234 | return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); |
6ae08ae3 DB |
235 | } |
236 | ||
f470378c | 237 | const struct bpf_func_proto bpf_probe_read_kernel_proto = { |
6ae08ae3 DB |
238 | .func = bpf_probe_read_kernel, |
239 | .gpl_only = true, | |
240 | .ret_type = RET_INTEGER, | |
241 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
242 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
243 | .arg3_type = ARG_ANYTHING, | |
244 | }; | |
245 | ||
6ae08ae3 | 246 | static __always_inline int |
8d92db5c | 247 | bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr) |
6ae08ae3 DB |
248 | { |
249 | int ret = security_locked_down(LOCKDOWN_BPF_READ); | |
250 | ||
251 | if (unlikely(ret < 0)) | |
8d92db5c CH |
252 | goto fail; |
253 | ||
6ae08ae3 | 254 | /* |
8d92db5c CH |
255 | * The strncpy_from_kernel_nofault() call will likely not fill the |
256 | * entire buffer, but that's okay in this circumstance as we're probing | |
6ae08ae3 DB |
257 | * arbitrary memory anyway similar to bpf_probe_read_*() and might |
258 | * as well probe the stack. Thus, memory is explicitly cleared | |
259 | * only in error case, so that improper users ignoring return | |
260 | * code altogether don't copy garbage; otherwise length of string | |
261 | * is returned that can be used for bpf_perf_event_output() et al. | |
262 | */ | |
8d92db5c | 263 | ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size); |
6ae08ae3 | 264 | if (unlikely(ret < 0)) |
8d92db5c CH |
265 | goto fail; |
266 | ||
02553b91 | 267 | return ret; |
8d92db5c CH |
268 | fail: |
269 | memset(dst, 0, size); | |
074f528e | 270 | return ret; |
2541517c AS |
271 | } |
272 | ||
6ae08ae3 DB |
273 | BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size, |
274 | const void *, unsafe_ptr) | |
275 | { | |
8d92db5c | 276 | return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); |
6ae08ae3 DB |
277 | } |
278 | ||
f470378c | 279 | const struct bpf_func_proto bpf_probe_read_kernel_str_proto = { |
6ae08ae3 DB |
280 | .func = bpf_probe_read_kernel_str, |
281 | .gpl_only = true, | |
282 | .ret_type = RET_INTEGER, | |
283 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
284 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
285 | .arg3_type = ARG_ANYTHING, | |
286 | }; | |
287 | ||
8d92db5c CH |
288 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
289 | BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size, | |
290 | const void *, unsafe_ptr) | |
291 | { | |
292 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { | |
293 | return bpf_probe_read_user_common(dst, size, | |
294 | (__force void __user *)unsafe_ptr); | |
295 | } | |
296 | return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); | |
297 | } | |
298 | ||
299 | static const struct bpf_func_proto bpf_probe_read_compat_proto = { | |
300 | .func = bpf_probe_read_compat, | |
301 | .gpl_only = true, | |
302 | .ret_type = RET_INTEGER, | |
303 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, | |
304 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, | |
305 | .arg3_type = ARG_ANYTHING, | |
306 | }; | |
307 | ||
6ae08ae3 DB |
308 | BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size, |
309 | const void *, unsafe_ptr) | |
310 | { | |
8d92db5c CH |
311 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { |
312 | return bpf_probe_read_user_str_common(dst, size, | |
313 | (__force void __user *)unsafe_ptr); | |
314 | } | |
315 | return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); | |
6ae08ae3 DB |
316 | } |
317 | ||
318 | static const struct bpf_func_proto bpf_probe_read_compat_str_proto = { | |
319 | .func = bpf_probe_read_compat_str, | |
2541517c AS |
320 | .gpl_only = true, |
321 | .ret_type = RET_INTEGER, | |
39f19ebb | 322 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
9c019e2b | 323 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
2541517c AS |
324 | .arg3_type = ARG_ANYTHING, |
325 | }; | |
8d92db5c | 326 | #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */ |
2541517c | 327 | |
eb1b6688 | 328 | BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src, |
f3694e00 | 329 | u32, size) |
96ae5227 | 330 | { |
96ae5227 SD |
331 | /* |
332 | * Ensure we're in user context which is safe for the helper to | |
333 | * run. This helper has no business in a kthread. | |
334 | * | |
335 | * access_ok() should prevent writing to non-user memory, but in | |
336 | * some situations (nommu, temporary switch, etc) access_ok() does | |
337 | * not provide enough validation, hence the check on KERNEL_DS. | |
c7b6f29b NA |
338 | * |
339 | * nmi_uaccess_okay() ensures the probe is not run in an interim | |
340 | * state, when the task or mm are switched. This is specifically | |
341 | * required to prevent the use of temporary mm. | |
96ae5227 SD |
342 | */ |
343 | ||
344 | if (unlikely(in_interrupt() || | |
345 | current->flags & (PF_KTHREAD | PF_EXITING))) | |
346 | return -EPERM; | |
db68ce10 | 347 | if (unlikely(uaccess_kernel())) |
96ae5227 | 348 | return -EPERM; |
c7b6f29b NA |
349 | if (unlikely(!nmi_uaccess_okay())) |
350 | return -EPERM; | |
96ae5227 | 351 | |
c0ee37e8 | 352 | return copy_to_user_nofault(unsafe_ptr, src, size); |
96ae5227 SD |
353 | } |
354 | ||
355 | static const struct bpf_func_proto bpf_probe_write_user_proto = { | |
356 | .func = bpf_probe_write_user, | |
357 | .gpl_only = true, | |
358 | .ret_type = RET_INTEGER, | |
359 | .arg1_type = ARG_ANYTHING, | |
39f19ebb AS |
360 | .arg2_type = ARG_PTR_TO_MEM, |
361 | .arg3_type = ARG_CONST_SIZE, | |
96ae5227 SD |
362 | }; |
363 | ||
364 | static const struct bpf_func_proto *bpf_get_probe_write_proto(void) | |
365 | { | |
2c78ee89 AS |
366 | if (!capable(CAP_SYS_ADMIN)) |
367 | return NULL; | |
368 | ||
96ae5227 SD |
369 | pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", |
370 | current->comm, task_pid_nr(current)); | |
371 | ||
372 | return &bpf_probe_write_user_proto; | |
373 | } | |
374 | ||
d7b2977b CH |
375 | static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, |
376 | size_t bufsz) | |
377 | { | |
378 | void __user *user_ptr = (__force void __user *)unsafe_ptr; | |
379 | ||
380 | buf[0] = 0; | |
381 | ||
382 | switch (fmt_ptype) { | |
383 | case 's': | |
384 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE | |
aec6ce59 CH |
385 | if ((unsigned long)unsafe_ptr < TASK_SIZE) { |
386 | strncpy_from_user_nofault(buf, user_ptr, bufsz); | |
387 | break; | |
388 | } | |
389 | fallthrough; | |
d7b2977b CH |
390 | #endif |
391 | case 'k': | |
392 | strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz); | |
393 | break; | |
394 | case 'u': | |
395 | strncpy_from_user_nofault(buf, user_ptr, bufsz); | |
396 | break; | |
397 | } | |
398 | } | |
399 | ||
ac5a72ea AM |
400 | static DEFINE_RAW_SPINLOCK(trace_printk_lock); |
401 | ||
402 | #define BPF_TRACE_PRINTK_SIZE 1024 | |
403 | ||
0d360d64 | 404 | static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...) |
ac5a72ea AM |
405 | { |
406 | static char buf[BPF_TRACE_PRINTK_SIZE]; | |
407 | unsigned long flags; | |
408 | va_list ap; | |
409 | int ret; | |
410 | ||
411 | raw_spin_lock_irqsave(&trace_printk_lock, flags); | |
412 | va_start(ap, fmt); | |
413 | ret = vsnprintf(buf, sizeof(buf), fmt, ap); | |
414 | va_end(ap); | |
415 | /* vsnprintf() will not append null for zero-length strings */ | |
416 | if (ret == 0) | |
417 | buf[0] = '\0'; | |
418 | trace_bpf_trace_printk(buf); | |
419 | raw_spin_unlock_irqrestore(&trace_printk_lock, flags); | |
420 | ||
421 | return ret; | |
422 | } | |
423 | ||
9c959c86 | 424 | /* |
7bda4b40 | 425 | * Only limited trace_printk() conversion specifiers allowed: |
2df6bb54 | 426 | * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s |
9c959c86 | 427 | */ |
f3694e00 DB |
428 | BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, |
429 | u64, arg2, u64, arg3) | |
9c959c86 | 430 | { |
b2a5212f DB |
431 | int i, mod[3] = {}, fmt_cnt = 0; |
432 | char buf[64], fmt_ptype; | |
433 | void *unsafe_ptr = NULL; | |
8d3b7dce | 434 | bool str_seen = false; |
9c959c86 AS |
435 | |
436 | /* | |
437 | * bpf_check()->check_func_arg()->check_stack_boundary() | |
438 | * guarantees that fmt points to bpf program stack, | |
439 | * fmt_size bytes of it were initialized and fmt_size > 0 | |
440 | */ | |
441 | if (fmt[--fmt_size] != 0) | |
442 | return -EINVAL; | |
443 | ||
444 | /* check format string for allowed specifiers */ | |
445 | for (i = 0; i < fmt_size; i++) { | |
446 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) | |
447 | return -EINVAL; | |
448 | ||
449 | if (fmt[i] != '%') | |
450 | continue; | |
451 | ||
452 | if (fmt_cnt >= 3) | |
453 | return -EINVAL; | |
454 | ||
455 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ | |
456 | i++; | |
457 | if (fmt[i] == 'l') { | |
458 | mod[fmt_cnt]++; | |
459 | i++; | |
b2a5212f | 460 | } else if (fmt[i] == 'p') { |
9c959c86 | 461 | mod[fmt_cnt]++; |
b2a5212f DB |
462 | if ((fmt[i + 1] == 'k' || |
463 | fmt[i + 1] == 'u') && | |
464 | fmt[i + 2] == 's') { | |
465 | fmt_ptype = fmt[i + 1]; | |
466 | i += 2; | |
467 | goto fmt_str; | |
468 | } | |
469 | ||
2df6bb54 SL |
470 | if (fmt[i + 1] == 'B') { |
471 | i++; | |
472 | goto fmt_next; | |
473 | } | |
474 | ||
1efb6ee3 MP |
475 | /* disallow any further format extensions */ |
476 | if (fmt[i + 1] != 0 && | |
477 | !isspace(fmt[i + 1]) && | |
478 | !ispunct(fmt[i + 1])) | |
9c959c86 | 479 | return -EINVAL; |
b2a5212f DB |
480 | |
481 | goto fmt_next; | |
482 | } else if (fmt[i] == 's') { | |
483 | mod[fmt_cnt]++; | |
484 | fmt_ptype = fmt[i]; | |
485 | fmt_str: | |
486 | if (str_seen) | |
487 | /* allow only one '%s' per fmt string */ | |
488 | return -EINVAL; | |
489 | str_seen = true; | |
490 | ||
491 | if (fmt[i + 1] != 0 && | |
492 | !isspace(fmt[i + 1]) && | |
493 | !ispunct(fmt[i + 1])) | |
494 | return -EINVAL; | |
495 | ||
496 | switch (fmt_cnt) { | |
497 | case 0: | |
498 | unsafe_ptr = (void *)(long)arg1; | |
499 | arg1 = (long)buf; | |
500 | break; | |
501 | case 1: | |
502 | unsafe_ptr = (void *)(long)arg2; | |
503 | arg2 = (long)buf; | |
504 | break; | |
505 | case 2: | |
506 | unsafe_ptr = (void *)(long)arg3; | |
507 | arg3 = (long)buf; | |
508 | break; | |
509 | } | |
510 | ||
d7b2977b CH |
511 | bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype, |
512 | sizeof(buf)); | |
b2a5212f | 513 | goto fmt_next; |
9c959c86 AS |
514 | } |
515 | ||
516 | if (fmt[i] == 'l') { | |
517 | mod[fmt_cnt]++; | |
518 | i++; | |
519 | } | |
520 | ||
7bda4b40 JF |
521 | if (fmt[i] != 'i' && fmt[i] != 'd' && |
522 | fmt[i] != 'u' && fmt[i] != 'x') | |
9c959c86 | 523 | return -EINVAL; |
b2a5212f | 524 | fmt_next: |
9c959c86 AS |
525 | fmt_cnt++; |
526 | } | |
527 | ||
88a5c690 DB |
528 | /* Horrid workaround for getting va_list handling working with different |
529 | * argument type combinations generically for 32 and 64 bit archs. | |
530 | */ | |
531 | #define __BPF_TP_EMIT() __BPF_ARG3_TP() | |
532 | #define __BPF_TP(...) \ | |
ac5a72ea | 533 | bpf_do_trace_printk(fmt, ##__VA_ARGS__) |
88a5c690 DB |
534 | |
535 | #define __BPF_ARG1_TP(...) \ | |
536 | ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ | |
537 | ? __BPF_TP(arg1, ##__VA_ARGS__) \ | |
538 | : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ | |
539 | ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ | |
540 | : __BPF_TP((u32)arg1, ##__VA_ARGS__))) | |
541 | ||
542 | #define __BPF_ARG2_TP(...) \ | |
543 | ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ | |
544 | ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ | |
545 | : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ | |
546 | ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ | |
547 | : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) | |
548 | ||
549 | #define __BPF_ARG3_TP(...) \ | |
550 | ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ | |
551 | ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ | |
552 | : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ | |
553 | ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ | |
554 | : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) | |
555 | ||
556 | return __BPF_TP_EMIT(); | |
9c959c86 AS |
557 | } |
558 | ||
559 | static const struct bpf_func_proto bpf_trace_printk_proto = { | |
560 | .func = bpf_trace_printk, | |
561 | .gpl_only = true, | |
562 | .ret_type = RET_INTEGER, | |
39f19ebb AS |
563 | .arg1_type = ARG_PTR_TO_MEM, |
564 | .arg2_type = ARG_CONST_SIZE, | |
9c959c86 AS |
565 | }; |
566 | ||
0756ea3e AS |
567 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void) |
568 | { | |
569 | /* | |
ac5a72ea AM |
570 | * This program might be calling bpf_trace_printk, |
571 | * so enable the associated bpf_trace/bpf_trace_printk event. | |
572 | * Repeat this each time as it is possible a user has | |
573 | * disabled bpf_trace_printk events. By loading a program | |
574 | * calling bpf_trace_printk() however the user has expressed | |
575 | * the intent to see such events. | |
0756ea3e | 576 | */ |
ac5a72ea AM |
577 | if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1)) |
578 | pr_warn_ratelimited("could not enable bpf_trace_printk events"); | |
0756ea3e AS |
579 | |
580 | return &bpf_trace_printk_proto; | |
581 | } | |
582 | ||
492e639f YS |
583 | #define MAX_SEQ_PRINTF_VARARGS 12 |
584 | #define MAX_SEQ_PRINTF_MAX_MEMCPY 6 | |
585 | #define MAX_SEQ_PRINTF_STR_LEN 128 | |
586 | ||
587 | struct bpf_seq_printf_buf { | |
588 | char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN]; | |
589 | }; | |
590 | static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf); | |
591 | static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used); | |
592 | ||
593 | BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, | |
594 | const void *, data, u32, data_len) | |
595 | { | |
596 | int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0; | |
597 | int i, buf_used, copy_size, num_args; | |
598 | u64 params[MAX_SEQ_PRINTF_VARARGS]; | |
599 | struct bpf_seq_printf_buf *bufs; | |
600 | const u64 *args = data; | |
601 | ||
602 | buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used); | |
603 | if (WARN_ON_ONCE(buf_used > 1)) { | |
604 | err = -EBUSY; | |
605 | goto out; | |
606 | } | |
607 | ||
608 | bufs = this_cpu_ptr(&bpf_seq_printf_buf); | |
609 | ||
610 | /* | |
611 | * bpf_check()->check_func_arg()->check_stack_boundary() | |
612 | * guarantees that fmt points to bpf program stack, | |
613 | * fmt_size bytes of it were initialized and fmt_size > 0 | |
614 | */ | |
615 | if (fmt[--fmt_size] != 0) | |
616 | goto out; | |
617 | ||
618 | if (data_len & 7) | |
619 | goto out; | |
620 | ||
621 | for (i = 0; i < fmt_size; i++) { | |
622 | if (fmt[i] == '%') { | |
623 | if (fmt[i + 1] == '%') | |
624 | i++; | |
625 | else if (!data || !data_len) | |
626 | goto out; | |
627 | } | |
628 | } | |
629 | ||
630 | num_args = data_len / 8; | |
631 | ||
632 | /* check format string for allowed specifiers */ | |
633 | for (i = 0; i < fmt_size; i++) { | |
634 | /* only printable ascii for now. */ | |
635 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) { | |
636 | err = -EINVAL; | |
637 | goto out; | |
638 | } | |
639 | ||
640 | if (fmt[i] != '%') | |
641 | continue; | |
642 | ||
643 | if (fmt[i + 1] == '%') { | |
644 | i++; | |
645 | continue; | |
646 | } | |
647 | ||
648 | if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) { | |
649 | err = -E2BIG; | |
650 | goto out; | |
651 | } | |
652 | ||
653 | if (fmt_cnt >= num_args) { | |
654 | err = -EINVAL; | |
655 | goto out; | |
656 | } | |
657 | ||
658 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ | |
659 | i++; | |
660 | ||
661 | /* skip optional "[0 +-][num]" width formating field */ | |
662 | while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' || | |
663 | fmt[i] == ' ') | |
664 | i++; | |
665 | if (fmt[i] >= '1' && fmt[i] <= '9') { | |
666 | i++; | |
667 | while (fmt[i] >= '0' && fmt[i] <= '9') | |
668 | i++; | |
669 | } | |
670 | ||
671 | if (fmt[i] == 's') { | |
19c8d8ac AM |
672 | void *unsafe_ptr; |
673 | ||
492e639f YS |
674 | /* try our best to copy */ |
675 | if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { | |
676 | err = -E2BIG; | |
677 | goto out; | |
678 | } | |
679 | ||
19c8d8ac AM |
680 | unsafe_ptr = (void *)(long)args[fmt_cnt]; |
681 | err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt], | |
682 | unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN); | |
492e639f YS |
683 | if (err < 0) |
684 | bufs->buf[memcpy_cnt][0] = '\0'; | |
685 | params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; | |
686 | ||
687 | fmt_cnt++; | |
688 | memcpy_cnt++; | |
689 | continue; | |
690 | } | |
691 | ||
692 | if (fmt[i] == 'p') { | |
693 | if (fmt[i + 1] == 0 || | |
694 | fmt[i + 1] == 'K' || | |
2df6bb54 SL |
695 | fmt[i + 1] == 'x' || |
696 | fmt[i + 1] == 'B') { | |
492e639f YS |
697 | /* just kernel pointers */ |
698 | params[fmt_cnt] = args[fmt_cnt]; | |
699 | fmt_cnt++; | |
700 | continue; | |
701 | } | |
702 | ||
703 | /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */ | |
704 | if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') { | |
705 | err = -EINVAL; | |
706 | goto out; | |
707 | } | |
708 | if (fmt[i + 2] != '4' && fmt[i + 2] != '6') { | |
709 | err = -EINVAL; | |
710 | goto out; | |
711 | } | |
712 | ||
713 | if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { | |
714 | err = -E2BIG; | |
715 | goto out; | |
716 | } | |
717 | ||
718 | ||
719 | copy_size = (fmt[i + 2] == '4') ? 4 : 16; | |
720 | ||
fe557319 | 721 | err = copy_from_kernel_nofault(bufs->buf[memcpy_cnt], |
492e639f YS |
722 | (void *) (long) args[fmt_cnt], |
723 | copy_size); | |
724 | if (err < 0) | |
725 | memset(bufs->buf[memcpy_cnt], 0, copy_size); | |
726 | params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; | |
727 | ||
728 | i += 2; | |
729 | fmt_cnt++; | |
730 | memcpy_cnt++; | |
731 | continue; | |
732 | } | |
733 | ||
734 | if (fmt[i] == 'l') { | |
735 | i++; | |
736 | if (fmt[i] == 'l') | |
737 | i++; | |
738 | } | |
739 | ||
740 | if (fmt[i] != 'i' && fmt[i] != 'd' && | |
c06b0229 YS |
741 | fmt[i] != 'u' && fmt[i] != 'x' && |
742 | fmt[i] != 'X') { | |
492e639f YS |
743 | err = -EINVAL; |
744 | goto out; | |
745 | } | |
746 | ||
747 | params[fmt_cnt] = args[fmt_cnt]; | |
748 | fmt_cnt++; | |
749 | } | |
750 | ||
751 | /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give | |
752 | * all of them to seq_printf(). | |
753 | */ | |
754 | seq_printf(m, fmt, params[0], params[1], params[2], params[3], | |
755 | params[4], params[5], params[6], params[7], params[8], | |
756 | params[9], params[10], params[11]); | |
757 | ||
758 | err = seq_has_overflowed(m) ? -EOVERFLOW : 0; | |
759 | out: | |
760 | this_cpu_dec(bpf_seq_printf_buf_used); | |
761 | return err; | |
762 | } | |
763 | ||
9436ef6e | 764 | BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file) |
c9a0f3b8 | 765 | |
492e639f YS |
766 | static const struct bpf_func_proto bpf_seq_printf_proto = { |
767 | .func = bpf_seq_printf, | |
768 | .gpl_only = true, | |
769 | .ret_type = RET_INTEGER, | |
770 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 771 | .arg1_btf_id = &btf_seq_file_ids[0], |
492e639f YS |
772 | .arg2_type = ARG_PTR_TO_MEM, |
773 | .arg3_type = ARG_CONST_SIZE, | |
774 | .arg4_type = ARG_PTR_TO_MEM_OR_NULL, | |
775 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, | |
492e639f YS |
776 | }; |
777 | ||
778 | BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) | |
779 | { | |
780 | return seq_write(m, data, len) ? -EOVERFLOW : 0; | |
781 | } | |
782 | ||
492e639f YS |
783 | static const struct bpf_func_proto bpf_seq_write_proto = { |
784 | .func = bpf_seq_write, | |
785 | .gpl_only = true, | |
786 | .ret_type = RET_INTEGER, | |
787 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 788 | .arg1_btf_id = &btf_seq_file_ids[0], |
492e639f YS |
789 | .arg2_type = ARG_PTR_TO_MEM, |
790 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
492e639f YS |
791 | }; |
792 | ||
eb411377 AM |
793 | BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr, |
794 | u32, btf_ptr_size, u64, flags) | |
795 | { | |
796 | const struct btf *btf; | |
797 | s32 btf_id; | |
798 | int ret; | |
799 | ||
800 | ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); | |
801 | if (ret) | |
802 | return ret; | |
803 | ||
804 | return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags); | |
805 | } | |
806 | ||
807 | static const struct bpf_func_proto bpf_seq_printf_btf_proto = { | |
808 | .func = bpf_seq_printf_btf, | |
809 | .gpl_only = true, | |
810 | .ret_type = RET_INTEGER, | |
811 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
812 | .arg1_btf_id = &btf_seq_file_ids[0], | |
492e639f YS |
813 | .arg2_type = ARG_PTR_TO_MEM, |
814 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
eb411377 | 815 | .arg4_type = ARG_ANYTHING, |
492e639f YS |
816 | }; |
817 | ||
908432ca YS |
818 | static __always_inline int |
819 | get_map_perf_counter(struct bpf_map *map, u64 flags, | |
820 | u64 *value, u64 *enabled, u64 *running) | |
35578d79 | 821 | { |
35578d79 | 822 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
6816a7ff DB |
823 | unsigned int cpu = smp_processor_id(); |
824 | u64 index = flags & BPF_F_INDEX_MASK; | |
3b1efb19 | 825 | struct bpf_event_entry *ee; |
35578d79 | 826 | |
6816a7ff DB |
827 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
828 | return -EINVAL; | |
829 | if (index == BPF_F_CURRENT_CPU) | |
830 | index = cpu; | |
35578d79 KX |
831 | if (unlikely(index >= array->map.max_entries)) |
832 | return -E2BIG; | |
833 | ||
3b1efb19 | 834 | ee = READ_ONCE(array->ptrs[index]); |
1ca1cc98 | 835 | if (!ee) |
35578d79 KX |
836 | return -ENOENT; |
837 | ||
908432ca YS |
838 | return perf_event_read_local(ee->event, value, enabled, running); |
839 | } | |
840 | ||
841 | BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) | |
842 | { | |
843 | u64 value = 0; | |
844 | int err; | |
845 | ||
846 | err = get_map_perf_counter(map, flags, &value, NULL, NULL); | |
35578d79 | 847 | /* |
f91840a3 AS |
848 | * this api is ugly since we miss [-22..-2] range of valid |
849 | * counter values, but that's uapi | |
35578d79 | 850 | */ |
f91840a3 AS |
851 | if (err) |
852 | return err; | |
853 | return value; | |
35578d79 KX |
854 | } |
855 | ||
62544ce8 | 856 | static const struct bpf_func_proto bpf_perf_event_read_proto = { |
35578d79 | 857 | .func = bpf_perf_event_read, |
1075ef59 | 858 | .gpl_only = true, |
35578d79 KX |
859 | .ret_type = RET_INTEGER, |
860 | .arg1_type = ARG_CONST_MAP_PTR, | |
861 | .arg2_type = ARG_ANYTHING, | |
862 | }; | |
863 | ||
908432ca YS |
864 | BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, |
865 | struct bpf_perf_event_value *, buf, u32, size) | |
866 | { | |
867 | int err = -EINVAL; | |
868 | ||
869 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) | |
870 | goto clear; | |
871 | err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, | |
872 | &buf->running); | |
873 | if (unlikely(err)) | |
874 | goto clear; | |
875 | return 0; | |
876 | clear: | |
877 | memset(buf, 0, size); | |
878 | return err; | |
879 | } | |
880 | ||
881 | static const struct bpf_func_proto bpf_perf_event_read_value_proto = { | |
882 | .func = bpf_perf_event_read_value, | |
883 | .gpl_only = true, | |
884 | .ret_type = RET_INTEGER, | |
885 | .arg1_type = ARG_CONST_MAP_PTR, | |
886 | .arg2_type = ARG_ANYTHING, | |
887 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
888 | .arg4_type = ARG_CONST_SIZE, | |
889 | }; | |
890 | ||
8e7a3920 DB |
891 | static __always_inline u64 |
892 | __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, | |
283ca526 | 893 | u64 flags, struct perf_sample_data *sd) |
a43eec30 | 894 | { |
a43eec30 | 895 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
d7931330 | 896 | unsigned int cpu = smp_processor_id(); |
1e33759c | 897 | u64 index = flags & BPF_F_INDEX_MASK; |
3b1efb19 | 898 | struct bpf_event_entry *ee; |
a43eec30 | 899 | struct perf_event *event; |
a43eec30 | 900 | |
1e33759c | 901 | if (index == BPF_F_CURRENT_CPU) |
d7931330 | 902 | index = cpu; |
a43eec30 AS |
903 | if (unlikely(index >= array->map.max_entries)) |
904 | return -E2BIG; | |
905 | ||
3b1efb19 | 906 | ee = READ_ONCE(array->ptrs[index]); |
1ca1cc98 | 907 | if (!ee) |
a43eec30 AS |
908 | return -ENOENT; |
909 | ||
3b1efb19 | 910 | event = ee->event; |
a43eec30 AS |
911 | if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || |
912 | event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) | |
913 | return -EINVAL; | |
914 | ||
d7931330 | 915 | if (unlikely(event->oncpu != cpu)) |
a43eec30 AS |
916 | return -EOPNOTSUPP; |
917 | ||
56201969 | 918 | return perf_event_output(event, sd, regs); |
a43eec30 AS |
919 | } |
920 | ||
9594dc3c MM |
921 | /* |
922 | * Support executing tracepoints in normal, irq, and nmi context that each call | |
923 | * bpf_perf_event_output | |
924 | */ | |
925 | struct bpf_trace_sample_data { | |
926 | struct perf_sample_data sds[3]; | |
927 | }; | |
928 | ||
929 | static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds); | |
930 | static DEFINE_PER_CPU(int, bpf_trace_nest_level); | |
f3694e00 DB |
931 | BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, |
932 | u64, flags, void *, data, u64, size) | |
8e7a3920 | 933 | { |
9594dc3c MM |
934 | struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds); |
935 | int nest_level = this_cpu_inc_return(bpf_trace_nest_level); | |
8e7a3920 DB |
936 | struct perf_raw_record raw = { |
937 | .frag = { | |
938 | .size = size, | |
939 | .data = data, | |
940 | }, | |
941 | }; | |
9594dc3c MM |
942 | struct perf_sample_data *sd; |
943 | int err; | |
8e7a3920 | 944 | |
9594dc3c MM |
945 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) { |
946 | err = -EBUSY; | |
947 | goto out; | |
948 | } | |
949 | ||
950 | sd = &sds->sds[nest_level - 1]; | |
951 | ||
952 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) { | |
953 | err = -EINVAL; | |
954 | goto out; | |
955 | } | |
8e7a3920 | 956 | |
283ca526 DB |
957 | perf_sample_data_init(sd, 0, 0); |
958 | sd->raw = &raw; | |
959 | ||
9594dc3c MM |
960 | err = __bpf_perf_event_output(regs, map, flags, sd); |
961 | ||
962 | out: | |
963 | this_cpu_dec(bpf_trace_nest_level); | |
964 | return err; | |
8e7a3920 DB |
965 | } |
966 | ||
a43eec30 AS |
967 | static const struct bpf_func_proto bpf_perf_event_output_proto = { |
968 | .func = bpf_perf_event_output, | |
1075ef59 | 969 | .gpl_only = true, |
a43eec30 AS |
970 | .ret_type = RET_INTEGER, |
971 | .arg1_type = ARG_PTR_TO_CTX, | |
972 | .arg2_type = ARG_CONST_MAP_PTR, | |
973 | .arg3_type = ARG_ANYTHING, | |
39f19ebb | 974 | .arg4_type = ARG_PTR_TO_MEM, |
a60dd35d | 975 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
a43eec30 AS |
976 | }; |
977 | ||
768fb61f AZ |
978 | static DEFINE_PER_CPU(int, bpf_event_output_nest_level); |
979 | struct bpf_nested_pt_regs { | |
980 | struct pt_regs regs[3]; | |
981 | }; | |
982 | static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs); | |
983 | static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds); | |
bd570ff9 | 984 | |
555c8a86 DB |
985 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
986 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) | |
bd570ff9 | 987 | { |
768fb61f | 988 | int nest_level = this_cpu_inc_return(bpf_event_output_nest_level); |
555c8a86 DB |
989 | struct perf_raw_frag frag = { |
990 | .copy = ctx_copy, | |
991 | .size = ctx_size, | |
992 | .data = ctx, | |
993 | }; | |
994 | struct perf_raw_record raw = { | |
995 | .frag = { | |
183fc153 AM |
996 | { |
997 | .next = ctx_size ? &frag : NULL, | |
998 | }, | |
555c8a86 DB |
999 | .size = meta_size, |
1000 | .data = meta, | |
1001 | }, | |
1002 | }; | |
768fb61f AZ |
1003 | struct perf_sample_data *sd; |
1004 | struct pt_regs *regs; | |
1005 | u64 ret; | |
1006 | ||
1007 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) { | |
1008 | ret = -EBUSY; | |
1009 | goto out; | |
1010 | } | |
1011 | sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]); | |
1012 | regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]); | |
bd570ff9 DB |
1013 | |
1014 | perf_fetch_caller_regs(regs); | |
283ca526 DB |
1015 | perf_sample_data_init(sd, 0, 0); |
1016 | sd->raw = &raw; | |
bd570ff9 | 1017 | |
768fb61f AZ |
1018 | ret = __bpf_perf_event_output(regs, map, flags, sd); |
1019 | out: | |
1020 | this_cpu_dec(bpf_event_output_nest_level); | |
1021 | return ret; | |
bd570ff9 DB |
1022 | } |
1023 | ||
f3694e00 | 1024 | BPF_CALL_0(bpf_get_current_task) |
606274c5 AS |
1025 | { |
1026 | return (long) current; | |
1027 | } | |
1028 | ||
f470378c | 1029 | const struct bpf_func_proto bpf_get_current_task_proto = { |
606274c5 AS |
1030 | .func = bpf_get_current_task, |
1031 | .gpl_only = true, | |
1032 | .ret_type = RET_INTEGER, | |
1033 | }; | |
1034 | ||
f3694e00 | 1035 | BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) |
60d20f91 | 1036 | { |
60d20f91 SD |
1037 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
1038 | struct cgroup *cgrp; | |
60d20f91 | 1039 | |
60d20f91 SD |
1040 | if (unlikely(idx >= array->map.max_entries)) |
1041 | return -E2BIG; | |
1042 | ||
1043 | cgrp = READ_ONCE(array->ptrs[idx]); | |
1044 | if (unlikely(!cgrp)) | |
1045 | return -EAGAIN; | |
1046 | ||
1047 | return task_under_cgroup_hierarchy(current, cgrp); | |
1048 | } | |
1049 | ||
1050 | static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { | |
1051 | .func = bpf_current_task_under_cgroup, | |
1052 | .gpl_only = false, | |
1053 | .ret_type = RET_INTEGER, | |
1054 | .arg1_type = ARG_CONST_MAP_PTR, | |
1055 | .arg2_type = ARG_ANYTHING, | |
1056 | }; | |
1057 | ||
8b401f9e YS |
1058 | struct send_signal_irq_work { |
1059 | struct irq_work irq_work; | |
1060 | struct task_struct *task; | |
1061 | u32 sig; | |
8482941f | 1062 | enum pid_type type; |
8b401f9e YS |
1063 | }; |
1064 | ||
1065 | static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work); | |
1066 | ||
1067 | static void do_bpf_send_signal(struct irq_work *entry) | |
1068 | { | |
1069 | struct send_signal_irq_work *work; | |
1070 | ||
1071 | work = container_of(entry, struct send_signal_irq_work, irq_work); | |
8482941f | 1072 | group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type); |
8b401f9e YS |
1073 | } |
1074 | ||
8482941f | 1075 | static int bpf_send_signal_common(u32 sig, enum pid_type type) |
8b401f9e YS |
1076 | { |
1077 | struct send_signal_irq_work *work = NULL; | |
1078 | ||
1079 | /* Similar to bpf_probe_write_user, task needs to be | |
1080 | * in a sound condition and kernel memory access be | |
1081 | * permitted in order to send signal to the current | |
1082 | * task. | |
1083 | */ | |
1084 | if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING))) | |
1085 | return -EPERM; | |
1086 | if (unlikely(uaccess_kernel())) | |
1087 | return -EPERM; | |
1088 | if (unlikely(!nmi_uaccess_okay())) | |
1089 | return -EPERM; | |
1090 | ||
1bc7896e | 1091 | if (irqs_disabled()) { |
e1afb702 YS |
1092 | /* Do an early check on signal validity. Otherwise, |
1093 | * the error is lost in deferred irq_work. | |
1094 | */ | |
1095 | if (unlikely(!valid_signal(sig))) | |
1096 | return -EINVAL; | |
1097 | ||
8b401f9e | 1098 | work = this_cpu_ptr(&send_signal_work); |
153bedba | 1099 | if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY) |
8b401f9e YS |
1100 | return -EBUSY; |
1101 | ||
1102 | /* Add the current task, which is the target of sending signal, | |
1103 | * to the irq_work. The current task may change when queued | |
1104 | * irq works get executed. | |
1105 | */ | |
1106 | work->task = current; | |
1107 | work->sig = sig; | |
8482941f | 1108 | work->type = type; |
8b401f9e YS |
1109 | irq_work_queue(&work->irq_work); |
1110 | return 0; | |
1111 | } | |
1112 | ||
8482941f YS |
1113 | return group_send_sig_info(sig, SEND_SIG_PRIV, current, type); |
1114 | } | |
1115 | ||
1116 | BPF_CALL_1(bpf_send_signal, u32, sig) | |
1117 | { | |
1118 | return bpf_send_signal_common(sig, PIDTYPE_TGID); | |
8b401f9e YS |
1119 | } |
1120 | ||
1121 | static const struct bpf_func_proto bpf_send_signal_proto = { | |
1122 | .func = bpf_send_signal, | |
1123 | .gpl_only = false, | |
1124 | .ret_type = RET_INTEGER, | |
1125 | .arg1_type = ARG_ANYTHING, | |
1126 | }; | |
1127 | ||
8482941f YS |
1128 | BPF_CALL_1(bpf_send_signal_thread, u32, sig) |
1129 | { | |
1130 | return bpf_send_signal_common(sig, PIDTYPE_PID); | |
1131 | } | |
1132 | ||
1133 | static const struct bpf_func_proto bpf_send_signal_thread_proto = { | |
1134 | .func = bpf_send_signal_thread, | |
1135 | .gpl_only = false, | |
1136 | .ret_type = RET_INTEGER, | |
1137 | .arg1_type = ARG_ANYTHING, | |
1138 | }; | |
1139 | ||
6e22ab9d JO |
1140 | BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz) |
1141 | { | |
1142 | long len; | |
1143 | char *p; | |
1144 | ||
1145 | if (!sz) | |
1146 | return 0; | |
1147 | ||
1148 | p = d_path(path, buf, sz); | |
1149 | if (IS_ERR(p)) { | |
1150 | len = PTR_ERR(p); | |
1151 | } else { | |
1152 | len = buf + sz - p; | |
1153 | memmove(buf, p, len); | |
1154 | } | |
1155 | ||
1156 | return len; | |
1157 | } | |
1158 | ||
1159 | BTF_SET_START(btf_allowlist_d_path) | |
a8a71796 JO |
1160 | #ifdef CONFIG_SECURITY |
1161 | BTF_ID(func, security_file_permission) | |
1162 | BTF_ID(func, security_inode_getattr) | |
1163 | BTF_ID(func, security_file_open) | |
1164 | #endif | |
1165 | #ifdef CONFIG_SECURITY_PATH | |
1166 | BTF_ID(func, security_path_truncate) | |
1167 | #endif | |
6e22ab9d JO |
1168 | BTF_ID(func, vfs_truncate) |
1169 | BTF_ID(func, vfs_fallocate) | |
1170 | BTF_ID(func, dentry_open) | |
1171 | BTF_ID(func, vfs_getattr) | |
1172 | BTF_ID(func, filp_close) | |
1173 | BTF_SET_END(btf_allowlist_d_path) | |
1174 | ||
1175 | static bool bpf_d_path_allowed(const struct bpf_prog *prog) | |
1176 | { | |
1177 | return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id); | |
1178 | } | |
1179 | ||
9436ef6e | 1180 | BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path) |
6e22ab9d JO |
1181 | |
1182 | static const struct bpf_func_proto bpf_d_path_proto = { | |
1183 | .func = bpf_d_path, | |
1184 | .gpl_only = false, | |
1185 | .ret_type = RET_INTEGER, | |
1186 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 1187 | .arg1_btf_id = &bpf_d_path_btf_ids[0], |
6e22ab9d JO |
1188 | .arg2_type = ARG_PTR_TO_MEM, |
1189 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
6e22ab9d JO |
1190 | .allowed = bpf_d_path_allowed, |
1191 | }; | |
1192 | ||
c4d0bfb4 AM |
1193 | #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \ |
1194 | BTF_F_PTR_RAW | BTF_F_ZERO) | |
1195 | ||
1196 | static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, | |
1197 | u64 flags, const struct btf **btf, | |
1198 | s32 *btf_id) | |
1199 | { | |
1200 | const struct btf_type *t; | |
1201 | ||
1202 | if (unlikely(flags & ~(BTF_F_ALL))) | |
1203 | return -EINVAL; | |
1204 | ||
1205 | if (btf_ptr_size != sizeof(struct btf_ptr)) | |
1206 | return -EINVAL; | |
1207 | ||
1208 | *btf = bpf_get_btf_vmlinux(); | |
1209 | ||
1210 | if (IS_ERR_OR_NULL(*btf)) | |
abbaa433 | 1211 | return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL; |
c4d0bfb4 AM |
1212 | |
1213 | if (ptr->type_id > 0) | |
1214 | *btf_id = ptr->type_id; | |
1215 | else | |
1216 | return -EINVAL; | |
1217 | ||
1218 | if (*btf_id > 0) | |
1219 | t = btf_type_by_id(*btf, *btf_id); | |
1220 | if (*btf_id <= 0 || !t) | |
1221 | return -ENOENT; | |
1222 | ||
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr, | |
1227 | u32, btf_ptr_size, u64, flags) | |
1228 | { | |
1229 | const struct btf *btf; | |
1230 | s32 btf_id; | |
1231 | int ret; | |
1232 | ||
1233 | ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); | |
1234 | if (ret) | |
1235 | return ret; | |
1236 | ||
1237 | return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, | |
1238 | flags); | |
1239 | } | |
1240 | ||
1241 | const struct bpf_func_proto bpf_snprintf_btf_proto = { | |
1242 | .func = bpf_snprintf_btf, | |
1243 | .gpl_only = false, | |
1244 | .ret_type = RET_INTEGER, | |
1245 | .arg1_type = ARG_PTR_TO_MEM, | |
1246 | .arg2_type = ARG_CONST_SIZE, | |
1247 | .arg3_type = ARG_PTR_TO_MEM, | |
1248 | .arg4_type = ARG_CONST_SIZE, | |
1249 | .arg5_type = ARG_ANYTHING, | |
1250 | }; | |
1251 | ||
fc611f47 KS |
1252 | const struct bpf_func_proto * |
1253 | bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
2541517c AS |
1254 | { |
1255 | switch (func_id) { | |
1256 | case BPF_FUNC_map_lookup_elem: | |
1257 | return &bpf_map_lookup_elem_proto; | |
1258 | case BPF_FUNC_map_update_elem: | |
1259 | return &bpf_map_update_elem_proto; | |
1260 | case BPF_FUNC_map_delete_elem: | |
1261 | return &bpf_map_delete_elem_proto; | |
02a8c817 AC |
1262 | case BPF_FUNC_map_push_elem: |
1263 | return &bpf_map_push_elem_proto; | |
1264 | case BPF_FUNC_map_pop_elem: | |
1265 | return &bpf_map_pop_elem_proto; | |
1266 | case BPF_FUNC_map_peek_elem: | |
1267 | return &bpf_map_peek_elem_proto; | |
d9847d31 AS |
1268 | case BPF_FUNC_ktime_get_ns: |
1269 | return &bpf_ktime_get_ns_proto; | |
71d19214 MÅ» |
1270 | case BPF_FUNC_ktime_get_boot_ns: |
1271 | return &bpf_ktime_get_boot_ns_proto; | |
04fd61ab AS |
1272 | case BPF_FUNC_tail_call: |
1273 | return &bpf_tail_call_proto; | |
ffeedafb AS |
1274 | case BPF_FUNC_get_current_pid_tgid: |
1275 | return &bpf_get_current_pid_tgid_proto; | |
606274c5 AS |
1276 | case BPF_FUNC_get_current_task: |
1277 | return &bpf_get_current_task_proto; | |
ffeedafb AS |
1278 | case BPF_FUNC_get_current_uid_gid: |
1279 | return &bpf_get_current_uid_gid_proto; | |
1280 | case BPF_FUNC_get_current_comm: | |
1281 | return &bpf_get_current_comm_proto; | |
9c959c86 | 1282 | case BPF_FUNC_trace_printk: |
0756ea3e | 1283 | return bpf_get_trace_printk_proto(); |
ab1973d3 AS |
1284 | case BPF_FUNC_get_smp_processor_id: |
1285 | return &bpf_get_smp_processor_id_proto; | |
2d0e30c3 DB |
1286 | case BPF_FUNC_get_numa_node_id: |
1287 | return &bpf_get_numa_node_id_proto; | |
35578d79 KX |
1288 | case BPF_FUNC_perf_event_read: |
1289 | return &bpf_perf_event_read_proto; | |
96ae5227 SD |
1290 | case BPF_FUNC_probe_write_user: |
1291 | return bpf_get_probe_write_proto(); | |
60d20f91 SD |
1292 | case BPF_FUNC_current_task_under_cgroup: |
1293 | return &bpf_current_task_under_cgroup_proto; | |
8937bd80 AS |
1294 | case BPF_FUNC_get_prandom_u32: |
1295 | return &bpf_get_prandom_u32_proto; | |
6ae08ae3 DB |
1296 | case BPF_FUNC_probe_read_user: |
1297 | return &bpf_probe_read_user_proto; | |
1298 | case BPF_FUNC_probe_read_kernel: | |
1299 | return &bpf_probe_read_kernel_proto; | |
6ae08ae3 DB |
1300 | case BPF_FUNC_probe_read_user_str: |
1301 | return &bpf_probe_read_user_str_proto; | |
1302 | case BPF_FUNC_probe_read_kernel_str: | |
1303 | return &bpf_probe_read_kernel_str_proto; | |
0ebeea8c DB |
1304 | #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE |
1305 | case BPF_FUNC_probe_read: | |
1306 | return &bpf_probe_read_compat_proto; | |
a5e8c070 | 1307 | case BPF_FUNC_probe_read_str: |
6ae08ae3 | 1308 | return &bpf_probe_read_compat_str_proto; |
0ebeea8c | 1309 | #endif |
34ea38ca | 1310 | #ifdef CONFIG_CGROUPS |
bf6fa2c8 YS |
1311 | case BPF_FUNC_get_current_cgroup_id: |
1312 | return &bpf_get_current_cgroup_id_proto; | |
34ea38ca | 1313 | #endif |
8b401f9e YS |
1314 | case BPF_FUNC_send_signal: |
1315 | return &bpf_send_signal_proto; | |
8482941f YS |
1316 | case BPF_FUNC_send_signal_thread: |
1317 | return &bpf_send_signal_thread_proto; | |
b80b033b SL |
1318 | case BPF_FUNC_perf_event_read_value: |
1319 | return &bpf_perf_event_read_value_proto; | |
b4490c5c CN |
1320 | case BPF_FUNC_get_ns_current_pid_tgid: |
1321 | return &bpf_get_ns_current_pid_tgid_proto; | |
457f4436 AN |
1322 | case BPF_FUNC_ringbuf_output: |
1323 | return &bpf_ringbuf_output_proto; | |
1324 | case BPF_FUNC_ringbuf_reserve: | |
1325 | return &bpf_ringbuf_reserve_proto; | |
1326 | case BPF_FUNC_ringbuf_submit: | |
1327 | return &bpf_ringbuf_submit_proto; | |
1328 | case BPF_FUNC_ringbuf_discard: | |
1329 | return &bpf_ringbuf_discard_proto; | |
1330 | case BPF_FUNC_ringbuf_query: | |
1331 | return &bpf_ringbuf_query_proto; | |
72e2b2b6 YS |
1332 | case BPF_FUNC_jiffies64: |
1333 | return &bpf_jiffies64_proto; | |
fa28dcb8 SL |
1334 | case BPF_FUNC_get_task_stack: |
1335 | return &bpf_get_task_stack_proto; | |
07be4c4a AS |
1336 | case BPF_FUNC_copy_from_user: |
1337 | return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL; | |
c4d0bfb4 AM |
1338 | case BPF_FUNC_snprintf_btf: |
1339 | return &bpf_snprintf_btf_proto; | |
eaa6bcb7 HL |
1340 | case BPF_FUNC_bpf_per_cpu_ptr: |
1341 | return &bpf_per_cpu_ptr_proto; | |
63d9b80d HL |
1342 | case BPF_FUNC_bpf_this_cpu_ptr: |
1343 | return &bpf_this_cpu_ptr_proto; | |
9fd82b61 AS |
1344 | default: |
1345 | return NULL; | |
1346 | } | |
1347 | } | |
1348 | ||
5e43f899 AI |
1349 | static const struct bpf_func_proto * |
1350 | kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
9fd82b61 AS |
1351 | { |
1352 | switch (func_id) { | |
a43eec30 AS |
1353 | case BPF_FUNC_perf_event_output: |
1354 | return &bpf_perf_event_output_proto; | |
d5a3b1f6 AS |
1355 | case BPF_FUNC_get_stackid: |
1356 | return &bpf_get_stackid_proto; | |
c195651e YS |
1357 | case BPF_FUNC_get_stack: |
1358 | return &bpf_get_stack_proto; | |
9802d865 JB |
1359 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
1360 | case BPF_FUNC_override_return: | |
1361 | return &bpf_override_return_proto; | |
1362 | #endif | |
2541517c | 1363 | default: |
fc611f47 | 1364 | return bpf_tracing_func_proto(func_id, prog); |
2541517c AS |
1365 | } |
1366 | } | |
1367 | ||
1368 | /* bpf+kprobe programs can access fields of 'struct pt_regs' */ | |
19de99f7 | 1369 | static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
5e43f899 | 1370 | const struct bpf_prog *prog, |
23994631 | 1371 | struct bpf_insn_access_aux *info) |
2541517c | 1372 | { |
2541517c AS |
1373 | if (off < 0 || off >= sizeof(struct pt_regs)) |
1374 | return false; | |
2541517c AS |
1375 | if (type != BPF_READ) |
1376 | return false; | |
2541517c AS |
1377 | if (off % size != 0) |
1378 | return false; | |
2d071c64 DB |
1379 | /* |
1380 | * Assertion for 32 bit to make sure last 8 byte access | |
1381 | * (BPF_DW) to the last 4 byte member is disallowed. | |
1382 | */ | |
1383 | if (off + size > sizeof(struct pt_regs)) | |
1384 | return false; | |
1385 | ||
2541517c AS |
1386 | return true; |
1387 | } | |
1388 | ||
7de16e3a | 1389 | const struct bpf_verifier_ops kprobe_verifier_ops = { |
2541517c AS |
1390 | .get_func_proto = kprobe_prog_func_proto, |
1391 | .is_valid_access = kprobe_prog_is_valid_access, | |
1392 | }; | |
1393 | ||
7de16e3a JK |
1394 | const struct bpf_prog_ops kprobe_prog_ops = { |
1395 | }; | |
1396 | ||
f3694e00 DB |
1397 | BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, |
1398 | u64, flags, void *, data, u64, size) | |
9940d67c | 1399 | { |
f3694e00 DB |
1400 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
1401 | ||
9940d67c AS |
1402 | /* |
1403 | * r1 points to perf tracepoint buffer where first 8 bytes are hidden | |
1404 | * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it | |
f3694e00 | 1405 | * from there and call the same bpf_perf_event_output() helper inline. |
9940d67c | 1406 | */ |
f3694e00 | 1407 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
9940d67c AS |
1408 | } |
1409 | ||
1410 | static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { | |
1411 | .func = bpf_perf_event_output_tp, | |
1412 | .gpl_only = true, | |
1413 | .ret_type = RET_INTEGER, | |
1414 | .arg1_type = ARG_PTR_TO_CTX, | |
1415 | .arg2_type = ARG_CONST_MAP_PTR, | |
1416 | .arg3_type = ARG_ANYTHING, | |
39f19ebb | 1417 | .arg4_type = ARG_PTR_TO_MEM, |
a60dd35d | 1418 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
9940d67c AS |
1419 | }; |
1420 | ||
f3694e00 DB |
1421 | BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, |
1422 | u64, flags) | |
9940d67c | 1423 | { |
f3694e00 | 1424 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
9940d67c | 1425 | |
f3694e00 DB |
1426 | /* |
1427 | * Same comment as in bpf_perf_event_output_tp(), only that this time | |
1428 | * the other helper's function body cannot be inlined due to being | |
1429 | * external, thus we need to call raw helper function. | |
1430 | */ | |
1431 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, | |
1432 | flags, 0, 0); | |
9940d67c AS |
1433 | } |
1434 | ||
1435 | static const struct bpf_func_proto bpf_get_stackid_proto_tp = { | |
1436 | .func = bpf_get_stackid_tp, | |
1437 | .gpl_only = true, | |
1438 | .ret_type = RET_INTEGER, | |
1439 | .arg1_type = ARG_PTR_TO_CTX, | |
1440 | .arg2_type = ARG_CONST_MAP_PTR, | |
1441 | .arg3_type = ARG_ANYTHING, | |
1442 | }; | |
1443 | ||
c195651e YS |
1444 | BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, |
1445 | u64, flags) | |
1446 | { | |
1447 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; | |
1448 | ||
1449 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, | |
1450 | (unsigned long) size, flags, 0); | |
1451 | } | |
1452 | ||
1453 | static const struct bpf_func_proto bpf_get_stack_proto_tp = { | |
1454 | .func = bpf_get_stack_tp, | |
1455 | .gpl_only = true, | |
1456 | .ret_type = RET_INTEGER, | |
1457 | .arg1_type = ARG_PTR_TO_CTX, | |
1458 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, | |
1459 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
1460 | .arg4_type = ARG_ANYTHING, | |
1461 | }; | |
1462 | ||
5e43f899 AI |
1463 | static const struct bpf_func_proto * |
1464 | tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
f005afed YS |
1465 | { |
1466 | switch (func_id) { | |
1467 | case BPF_FUNC_perf_event_output: | |
1468 | return &bpf_perf_event_output_proto_tp; | |
1469 | case BPF_FUNC_get_stackid: | |
1470 | return &bpf_get_stackid_proto_tp; | |
c195651e YS |
1471 | case BPF_FUNC_get_stack: |
1472 | return &bpf_get_stack_proto_tp; | |
f005afed | 1473 | default: |
fc611f47 | 1474 | return bpf_tracing_func_proto(func_id, prog); |
f005afed YS |
1475 | } |
1476 | } | |
1477 | ||
1478 | static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, | |
5e43f899 | 1479 | const struct bpf_prog *prog, |
f005afed YS |
1480 | struct bpf_insn_access_aux *info) |
1481 | { | |
1482 | if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) | |
1483 | return false; | |
1484 | if (type != BPF_READ) | |
1485 | return false; | |
1486 | if (off % size != 0) | |
1487 | return false; | |
1488 | ||
1489 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); | |
1490 | return true; | |
1491 | } | |
1492 | ||
1493 | const struct bpf_verifier_ops tracepoint_verifier_ops = { | |
1494 | .get_func_proto = tp_prog_func_proto, | |
1495 | .is_valid_access = tp_prog_is_valid_access, | |
1496 | }; | |
1497 | ||
1498 | const struct bpf_prog_ops tracepoint_prog_ops = { | |
1499 | }; | |
1500 | ||
1501 | BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, | |
4bebdc7a YS |
1502 | struct bpf_perf_event_value *, buf, u32, size) |
1503 | { | |
1504 | int err = -EINVAL; | |
1505 | ||
1506 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) | |
1507 | goto clear; | |
1508 | err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, | |
1509 | &buf->running); | |
1510 | if (unlikely(err)) | |
1511 | goto clear; | |
1512 | return 0; | |
1513 | clear: | |
1514 | memset(buf, 0, size); | |
1515 | return err; | |
1516 | } | |
1517 | ||
f005afed YS |
1518 | static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { |
1519 | .func = bpf_perf_prog_read_value, | |
4bebdc7a YS |
1520 | .gpl_only = true, |
1521 | .ret_type = RET_INTEGER, | |
1522 | .arg1_type = ARG_PTR_TO_CTX, | |
1523 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, | |
1524 | .arg3_type = ARG_CONST_SIZE, | |
1525 | }; | |
1526 | ||
fff7b643 DX |
1527 | BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx, |
1528 | void *, buf, u32, size, u64, flags) | |
1529 | { | |
1530 | #ifndef CONFIG_X86 | |
1531 | return -ENOENT; | |
1532 | #else | |
1533 | static const u32 br_entry_size = sizeof(struct perf_branch_entry); | |
1534 | struct perf_branch_stack *br_stack = ctx->data->br_stack; | |
1535 | u32 to_copy; | |
1536 | ||
1537 | if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) | |
1538 | return -EINVAL; | |
1539 | ||
1540 | if (unlikely(!br_stack)) | |
1541 | return -EINVAL; | |
1542 | ||
1543 | if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE) | |
1544 | return br_stack->nr * br_entry_size; | |
1545 | ||
1546 | if (!buf || (size % br_entry_size != 0)) | |
1547 | return -EINVAL; | |
1548 | ||
1549 | to_copy = min_t(u32, br_stack->nr * br_entry_size, size); | |
1550 | memcpy(buf, br_stack->entries, to_copy); | |
1551 | ||
1552 | return to_copy; | |
1553 | #endif | |
1554 | } | |
1555 | ||
1556 | static const struct bpf_func_proto bpf_read_branch_records_proto = { | |
1557 | .func = bpf_read_branch_records, | |
1558 | .gpl_only = true, | |
1559 | .ret_type = RET_INTEGER, | |
1560 | .arg1_type = ARG_PTR_TO_CTX, | |
1561 | .arg2_type = ARG_PTR_TO_MEM_OR_NULL, | |
1562 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
1563 | .arg4_type = ARG_ANYTHING, | |
1564 | }; | |
1565 | ||
5e43f899 AI |
1566 | static const struct bpf_func_proto * |
1567 | pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
9fd82b61 AS |
1568 | { |
1569 | switch (func_id) { | |
1570 | case BPF_FUNC_perf_event_output: | |
9940d67c | 1571 | return &bpf_perf_event_output_proto_tp; |
9fd82b61 | 1572 | case BPF_FUNC_get_stackid: |
7b04d6d6 | 1573 | return &bpf_get_stackid_proto_pe; |
c195651e | 1574 | case BPF_FUNC_get_stack: |
7b04d6d6 | 1575 | return &bpf_get_stack_proto_pe; |
4bebdc7a | 1576 | case BPF_FUNC_perf_prog_read_value: |
f005afed | 1577 | return &bpf_perf_prog_read_value_proto; |
fff7b643 DX |
1578 | case BPF_FUNC_read_branch_records: |
1579 | return &bpf_read_branch_records_proto; | |
9fd82b61 | 1580 | default: |
fc611f47 | 1581 | return bpf_tracing_func_proto(func_id, prog); |
9fd82b61 AS |
1582 | } |
1583 | } | |
1584 | ||
c4f6699d AS |
1585 | /* |
1586 | * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp | |
1587 | * to avoid potential recursive reuse issue when/if tracepoints are added | |
9594dc3c MM |
1588 | * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack. |
1589 | * | |
1590 | * Since raw tracepoints run despite bpf_prog_active, support concurrent usage | |
1591 | * in normal, irq, and nmi context. | |
c4f6699d | 1592 | */ |
9594dc3c MM |
1593 | struct bpf_raw_tp_regs { |
1594 | struct pt_regs regs[3]; | |
1595 | }; | |
1596 | static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs); | |
1597 | static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level); | |
1598 | static struct pt_regs *get_bpf_raw_tp_regs(void) | |
1599 | { | |
1600 | struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs); | |
1601 | int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level); | |
1602 | ||
1603 | if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) { | |
1604 | this_cpu_dec(bpf_raw_tp_nest_level); | |
1605 | return ERR_PTR(-EBUSY); | |
1606 | } | |
1607 | ||
1608 | return &tp_regs->regs[nest_level - 1]; | |
1609 | } | |
1610 | ||
1611 | static void put_bpf_raw_tp_regs(void) | |
1612 | { | |
1613 | this_cpu_dec(bpf_raw_tp_nest_level); | |
1614 | } | |
1615 | ||
c4f6699d AS |
1616 | BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, |
1617 | struct bpf_map *, map, u64, flags, void *, data, u64, size) | |
1618 | { | |
9594dc3c MM |
1619 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
1620 | int ret; | |
1621 | ||
1622 | if (IS_ERR(regs)) | |
1623 | return PTR_ERR(regs); | |
c4f6699d AS |
1624 | |
1625 | perf_fetch_caller_regs(regs); | |
9594dc3c MM |
1626 | ret = ____bpf_perf_event_output(regs, map, flags, data, size); |
1627 | ||
1628 | put_bpf_raw_tp_regs(); | |
1629 | return ret; | |
c4f6699d AS |
1630 | } |
1631 | ||
1632 | static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { | |
1633 | .func = bpf_perf_event_output_raw_tp, | |
1634 | .gpl_only = true, | |
1635 | .ret_type = RET_INTEGER, | |
1636 | .arg1_type = ARG_PTR_TO_CTX, | |
1637 | .arg2_type = ARG_CONST_MAP_PTR, | |
1638 | .arg3_type = ARG_ANYTHING, | |
1639 | .arg4_type = ARG_PTR_TO_MEM, | |
1640 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, | |
1641 | }; | |
1642 | ||
a7658e1a | 1643 | extern const struct bpf_func_proto bpf_skb_output_proto; |
d831ee84 | 1644 | extern const struct bpf_func_proto bpf_xdp_output_proto; |
a7658e1a | 1645 | |
c4f6699d AS |
1646 | BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, |
1647 | struct bpf_map *, map, u64, flags) | |
1648 | { | |
9594dc3c MM |
1649 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
1650 | int ret; | |
1651 | ||
1652 | if (IS_ERR(regs)) | |
1653 | return PTR_ERR(regs); | |
c4f6699d AS |
1654 | |
1655 | perf_fetch_caller_regs(regs); | |
1656 | /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ | |
9594dc3c MM |
1657 | ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
1658 | flags, 0, 0); | |
1659 | put_bpf_raw_tp_regs(); | |
1660 | return ret; | |
c4f6699d AS |
1661 | } |
1662 | ||
1663 | static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { | |
1664 | .func = bpf_get_stackid_raw_tp, | |
1665 | .gpl_only = true, | |
1666 | .ret_type = RET_INTEGER, | |
1667 | .arg1_type = ARG_PTR_TO_CTX, | |
1668 | .arg2_type = ARG_CONST_MAP_PTR, | |
1669 | .arg3_type = ARG_ANYTHING, | |
1670 | }; | |
1671 | ||
c195651e YS |
1672 | BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, |
1673 | void *, buf, u32, size, u64, flags) | |
1674 | { | |
9594dc3c MM |
1675 | struct pt_regs *regs = get_bpf_raw_tp_regs(); |
1676 | int ret; | |
1677 | ||
1678 | if (IS_ERR(regs)) | |
1679 | return PTR_ERR(regs); | |
c195651e YS |
1680 | |
1681 | perf_fetch_caller_regs(regs); | |
9594dc3c MM |
1682 | ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
1683 | (unsigned long) size, flags, 0); | |
1684 | put_bpf_raw_tp_regs(); | |
1685 | return ret; | |
c195651e YS |
1686 | } |
1687 | ||
1688 | static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { | |
1689 | .func = bpf_get_stack_raw_tp, | |
1690 | .gpl_only = true, | |
1691 | .ret_type = RET_INTEGER, | |
1692 | .arg1_type = ARG_PTR_TO_CTX, | |
1693 | .arg2_type = ARG_PTR_TO_MEM, | |
1694 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, | |
1695 | .arg4_type = ARG_ANYTHING, | |
1696 | }; | |
1697 | ||
5e43f899 AI |
1698 | static const struct bpf_func_proto * |
1699 | raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
c4f6699d AS |
1700 | { |
1701 | switch (func_id) { | |
1702 | case BPF_FUNC_perf_event_output: | |
1703 | return &bpf_perf_event_output_proto_raw_tp; | |
1704 | case BPF_FUNC_get_stackid: | |
1705 | return &bpf_get_stackid_proto_raw_tp; | |
c195651e YS |
1706 | case BPF_FUNC_get_stack: |
1707 | return &bpf_get_stack_proto_raw_tp; | |
c4f6699d | 1708 | default: |
fc611f47 | 1709 | return bpf_tracing_func_proto(func_id, prog); |
c4f6699d AS |
1710 | } |
1711 | } | |
1712 | ||
958a3f2d | 1713 | const struct bpf_func_proto * |
f1b9509c AS |
1714 | tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
1715 | { | |
1716 | switch (func_id) { | |
1717 | #ifdef CONFIG_NET | |
1718 | case BPF_FUNC_skb_output: | |
1719 | return &bpf_skb_output_proto; | |
d831ee84 EC |
1720 | case BPF_FUNC_xdp_output: |
1721 | return &bpf_xdp_output_proto; | |
af7ec138 YS |
1722 | case BPF_FUNC_skc_to_tcp6_sock: |
1723 | return &bpf_skc_to_tcp6_sock_proto; | |
478cfbdf YS |
1724 | case BPF_FUNC_skc_to_tcp_sock: |
1725 | return &bpf_skc_to_tcp_sock_proto; | |
1726 | case BPF_FUNC_skc_to_tcp_timewait_sock: | |
1727 | return &bpf_skc_to_tcp_timewait_sock_proto; | |
1728 | case BPF_FUNC_skc_to_tcp_request_sock: | |
1729 | return &bpf_skc_to_tcp_request_sock_proto; | |
0d4fad3e YS |
1730 | case BPF_FUNC_skc_to_udp6_sock: |
1731 | return &bpf_skc_to_udp6_sock_proto; | |
f1b9509c | 1732 | #endif |
492e639f YS |
1733 | case BPF_FUNC_seq_printf: |
1734 | return prog->expected_attach_type == BPF_TRACE_ITER ? | |
1735 | &bpf_seq_printf_proto : | |
1736 | NULL; | |
1737 | case BPF_FUNC_seq_write: | |
1738 | return prog->expected_attach_type == BPF_TRACE_ITER ? | |
1739 | &bpf_seq_write_proto : | |
1740 | NULL; | |
eb411377 AM |
1741 | case BPF_FUNC_seq_printf_btf: |
1742 | return prog->expected_attach_type == BPF_TRACE_ITER ? | |
1743 | &bpf_seq_printf_btf_proto : | |
1744 | NULL; | |
6e22ab9d JO |
1745 | case BPF_FUNC_d_path: |
1746 | return &bpf_d_path_proto; | |
f1b9509c AS |
1747 | default: |
1748 | return raw_tp_prog_func_proto(func_id, prog); | |
1749 | } | |
1750 | } | |
1751 | ||
c4f6699d AS |
1752 | static bool raw_tp_prog_is_valid_access(int off, int size, |
1753 | enum bpf_access_type type, | |
5e43f899 | 1754 | const struct bpf_prog *prog, |
c4f6699d AS |
1755 | struct bpf_insn_access_aux *info) |
1756 | { | |
f1b9509c AS |
1757 | if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) |
1758 | return false; | |
1759 | if (type != BPF_READ) | |
1760 | return false; | |
1761 | if (off % size != 0) | |
1762 | return false; | |
1763 | return true; | |
1764 | } | |
1765 | ||
1766 | static bool tracing_prog_is_valid_access(int off, int size, | |
1767 | enum bpf_access_type type, | |
1768 | const struct bpf_prog *prog, | |
1769 | struct bpf_insn_access_aux *info) | |
1770 | { | |
1771 | if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) | |
c4f6699d AS |
1772 | return false; |
1773 | if (type != BPF_READ) | |
1774 | return false; | |
1775 | if (off % size != 0) | |
1776 | return false; | |
9e15db66 | 1777 | return btf_ctx_access(off, size, type, prog, info); |
c4f6699d AS |
1778 | } |
1779 | ||
3e7c67d9 KS |
1780 | int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog, |
1781 | const union bpf_attr *kattr, | |
1782 | union bpf_attr __user *uattr) | |
1783 | { | |
1784 | return -ENOTSUPP; | |
1785 | } | |
1786 | ||
c4f6699d AS |
1787 | const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { |
1788 | .get_func_proto = raw_tp_prog_func_proto, | |
1789 | .is_valid_access = raw_tp_prog_is_valid_access, | |
1790 | }; | |
1791 | ||
1792 | const struct bpf_prog_ops raw_tracepoint_prog_ops = { | |
ebfb4d40 | 1793 | #ifdef CONFIG_NET |
1b4d60ec | 1794 | .test_run = bpf_prog_test_run_raw_tp, |
ebfb4d40 | 1795 | #endif |
c4f6699d AS |
1796 | }; |
1797 | ||
f1b9509c AS |
1798 | const struct bpf_verifier_ops tracing_verifier_ops = { |
1799 | .get_func_proto = tracing_prog_func_proto, | |
1800 | .is_valid_access = tracing_prog_is_valid_access, | |
1801 | }; | |
1802 | ||
1803 | const struct bpf_prog_ops tracing_prog_ops = { | |
da00d2f1 | 1804 | .test_run = bpf_prog_test_run_tracing, |
f1b9509c AS |
1805 | }; |
1806 | ||
9df1c28b MM |
1807 | static bool raw_tp_writable_prog_is_valid_access(int off, int size, |
1808 | enum bpf_access_type type, | |
1809 | const struct bpf_prog *prog, | |
1810 | struct bpf_insn_access_aux *info) | |
1811 | { | |
1812 | if (off == 0) { | |
1813 | if (size != sizeof(u64) || type != BPF_READ) | |
1814 | return false; | |
1815 | info->reg_type = PTR_TO_TP_BUFFER; | |
1816 | } | |
1817 | return raw_tp_prog_is_valid_access(off, size, type, prog, info); | |
1818 | } | |
1819 | ||
1820 | const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { | |
1821 | .get_func_proto = raw_tp_prog_func_proto, | |
1822 | .is_valid_access = raw_tp_writable_prog_is_valid_access, | |
1823 | }; | |
1824 | ||
1825 | const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { | |
1826 | }; | |
1827 | ||
0515e599 | 1828 | static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
5e43f899 | 1829 | const struct bpf_prog *prog, |
23994631 | 1830 | struct bpf_insn_access_aux *info) |
0515e599 | 1831 | { |
95da0cdb | 1832 | const int size_u64 = sizeof(u64); |
31fd8581 | 1833 | |
0515e599 AS |
1834 | if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) |
1835 | return false; | |
1836 | if (type != BPF_READ) | |
1837 | return false; | |
bc23105c DB |
1838 | if (off % size != 0) { |
1839 | if (sizeof(unsigned long) != 4) | |
1840 | return false; | |
1841 | if (size != 8) | |
1842 | return false; | |
1843 | if (off % size != 4) | |
1844 | return false; | |
1845 | } | |
31fd8581 | 1846 | |
f96da094 DB |
1847 | switch (off) { |
1848 | case bpf_ctx_range(struct bpf_perf_event_data, sample_period): | |
95da0cdb TQ |
1849 | bpf_ctx_record_field_size(info, size_u64); |
1850 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) | |
1851 | return false; | |
1852 | break; | |
1853 | case bpf_ctx_range(struct bpf_perf_event_data, addr): | |
1854 | bpf_ctx_record_field_size(info, size_u64); | |
1855 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) | |
23994631 | 1856 | return false; |
f96da094 DB |
1857 | break; |
1858 | default: | |
0515e599 AS |
1859 | if (size != sizeof(long)) |
1860 | return false; | |
1861 | } | |
f96da094 | 1862 | |
0515e599 AS |
1863 | return true; |
1864 | } | |
1865 | ||
6b8cc1d1 DB |
1866 | static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, |
1867 | const struct bpf_insn *si, | |
0515e599 | 1868 | struct bpf_insn *insn_buf, |
f96da094 | 1869 | struct bpf_prog *prog, u32 *target_size) |
0515e599 AS |
1870 | { |
1871 | struct bpf_insn *insn = insn_buf; | |
1872 | ||
6b8cc1d1 | 1873 | switch (si->off) { |
0515e599 | 1874 | case offsetof(struct bpf_perf_event_data, sample_period): |
f035a515 | 1875 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
6b8cc1d1 | 1876 | data), si->dst_reg, si->src_reg, |
0515e599 | 1877 | offsetof(struct bpf_perf_event_data_kern, data)); |
6b8cc1d1 | 1878 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
f96da094 DB |
1879 | bpf_target_off(struct perf_sample_data, period, 8, |
1880 | target_size)); | |
0515e599 | 1881 | break; |
95da0cdb TQ |
1882 | case offsetof(struct bpf_perf_event_data, addr): |
1883 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, | |
1884 | data), si->dst_reg, si->src_reg, | |
1885 | offsetof(struct bpf_perf_event_data_kern, data)); | |
1886 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, | |
1887 | bpf_target_off(struct perf_sample_data, addr, 8, | |
1888 | target_size)); | |
1889 | break; | |
0515e599 | 1890 | default: |
f035a515 | 1891 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
6b8cc1d1 | 1892 | regs), si->dst_reg, si->src_reg, |
0515e599 | 1893 | offsetof(struct bpf_perf_event_data_kern, regs)); |
6b8cc1d1 DB |
1894 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, |
1895 | si->off); | |
0515e599 AS |
1896 | break; |
1897 | } | |
1898 | ||
1899 | return insn - insn_buf; | |
1900 | } | |
1901 | ||
7de16e3a | 1902 | const struct bpf_verifier_ops perf_event_verifier_ops = { |
f005afed | 1903 | .get_func_proto = pe_prog_func_proto, |
0515e599 AS |
1904 | .is_valid_access = pe_prog_is_valid_access, |
1905 | .convert_ctx_access = pe_prog_convert_ctx_access, | |
1906 | }; | |
7de16e3a JK |
1907 | |
1908 | const struct bpf_prog_ops perf_event_prog_ops = { | |
1909 | }; | |
e87c6bc3 YS |
1910 | |
1911 | static DEFINE_MUTEX(bpf_event_mutex); | |
1912 | ||
c8c088ba YS |
1913 | #define BPF_TRACE_MAX_PROGS 64 |
1914 | ||
e87c6bc3 YS |
1915 | int perf_event_attach_bpf_prog(struct perf_event *event, |
1916 | struct bpf_prog *prog) | |
1917 | { | |
e672db03 | 1918 | struct bpf_prog_array *old_array; |
e87c6bc3 YS |
1919 | struct bpf_prog_array *new_array; |
1920 | int ret = -EEXIST; | |
1921 | ||
9802d865 | 1922 | /* |
b4da3340 MH |
1923 | * Kprobe override only works if they are on the function entry, |
1924 | * and only if they are on the opt-in list. | |
9802d865 JB |
1925 | */ |
1926 | if (prog->kprobe_override && | |
b4da3340 | 1927 | (!trace_kprobe_on_func_entry(event->tp_event) || |
9802d865 JB |
1928 | !trace_kprobe_error_injectable(event->tp_event))) |
1929 | return -EINVAL; | |
1930 | ||
e87c6bc3 YS |
1931 | mutex_lock(&bpf_event_mutex); |
1932 | ||
1933 | if (event->prog) | |
07c41a29 | 1934 | goto unlock; |
e87c6bc3 | 1935 | |
e672db03 | 1936 | old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); |
c8c088ba YS |
1937 | if (old_array && |
1938 | bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { | |
1939 | ret = -E2BIG; | |
1940 | goto unlock; | |
1941 | } | |
1942 | ||
e87c6bc3 YS |
1943 | ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); |
1944 | if (ret < 0) | |
07c41a29 | 1945 | goto unlock; |
e87c6bc3 YS |
1946 | |
1947 | /* set the new array to event->tp_event and set event->prog */ | |
1948 | event->prog = prog; | |
1949 | rcu_assign_pointer(event->tp_event->prog_array, new_array); | |
1950 | bpf_prog_array_free(old_array); | |
1951 | ||
07c41a29 | 1952 | unlock: |
e87c6bc3 YS |
1953 | mutex_unlock(&bpf_event_mutex); |
1954 | return ret; | |
1955 | } | |
1956 | ||
1957 | void perf_event_detach_bpf_prog(struct perf_event *event) | |
1958 | { | |
e672db03 | 1959 | struct bpf_prog_array *old_array; |
e87c6bc3 YS |
1960 | struct bpf_prog_array *new_array; |
1961 | int ret; | |
1962 | ||
1963 | mutex_lock(&bpf_event_mutex); | |
1964 | ||
1965 | if (!event->prog) | |
07c41a29 | 1966 | goto unlock; |
e87c6bc3 | 1967 | |
e672db03 | 1968 | old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); |
e87c6bc3 | 1969 | ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); |
170a7e3e SY |
1970 | if (ret == -ENOENT) |
1971 | goto unlock; | |
e87c6bc3 YS |
1972 | if (ret < 0) { |
1973 | bpf_prog_array_delete_safe(old_array, event->prog); | |
1974 | } else { | |
1975 | rcu_assign_pointer(event->tp_event->prog_array, new_array); | |
1976 | bpf_prog_array_free(old_array); | |
1977 | } | |
1978 | ||
1979 | bpf_prog_put(event->prog); | |
1980 | event->prog = NULL; | |
1981 | ||
07c41a29 | 1982 | unlock: |
e87c6bc3 YS |
1983 | mutex_unlock(&bpf_event_mutex); |
1984 | } | |
f371b304 | 1985 | |
f4e2298e | 1986 | int perf_event_query_prog_array(struct perf_event *event, void __user *info) |
f371b304 YS |
1987 | { |
1988 | struct perf_event_query_bpf __user *uquery = info; | |
1989 | struct perf_event_query_bpf query = {}; | |
e672db03 | 1990 | struct bpf_prog_array *progs; |
3a38bb98 | 1991 | u32 *ids, prog_cnt, ids_len; |
f371b304 YS |
1992 | int ret; |
1993 | ||
031258da | 1994 | if (!perfmon_capable()) |
f371b304 YS |
1995 | return -EPERM; |
1996 | if (event->attr.type != PERF_TYPE_TRACEPOINT) | |
1997 | return -EINVAL; | |
1998 | if (copy_from_user(&query, uquery, sizeof(query))) | |
1999 | return -EFAULT; | |
3a38bb98 YS |
2000 | |
2001 | ids_len = query.ids_len; | |
2002 | if (ids_len > BPF_TRACE_MAX_PROGS) | |
9c481b90 | 2003 | return -E2BIG; |
3a38bb98 YS |
2004 | ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); |
2005 | if (!ids) | |
2006 | return -ENOMEM; | |
2007 | /* | |
2008 | * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which | |
2009 | * is required when user only wants to check for uquery->prog_cnt. | |
2010 | * There is no need to check for it since the case is handled | |
2011 | * gracefully in bpf_prog_array_copy_info. | |
2012 | */ | |
f371b304 YS |
2013 | |
2014 | mutex_lock(&bpf_event_mutex); | |
e672db03 SF |
2015 | progs = bpf_event_rcu_dereference(event->tp_event->prog_array); |
2016 | ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); | |
f371b304 YS |
2017 | mutex_unlock(&bpf_event_mutex); |
2018 | ||
3a38bb98 YS |
2019 | if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || |
2020 | copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) | |
2021 | ret = -EFAULT; | |
2022 | ||
2023 | kfree(ids); | |
f371b304 YS |
2024 | return ret; |
2025 | } | |
c4f6699d AS |
2026 | |
2027 | extern struct bpf_raw_event_map __start__bpf_raw_tp[]; | |
2028 | extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; | |
2029 | ||
a38d1107 | 2030 | struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) |
c4f6699d AS |
2031 | { |
2032 | struct bpf_raw_event_map *btp = __start__bpf_raw_tp; | |
2033 | ||
2034 | for (; btp < __stop__bpf_raw_tp; btp++) { | |
2035 | if (!strcmp(btp->tp->name, name)) | |
2036 | return btp; | |
2037 | } | |
a38d1107 MM |
2038 | |
2039 | return bpf_get_raw_tracepoint_module(name); | |
2040 | } | |
2041 | ||
2042 | void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) | |
2043 | { | |
2044 | struct module *mod = __module_address((unsigned long)btp); | |
2045 | ||
2046 | if (mod) | |
2047 | module_put(mod); | |
c4f6699d AS |
2048 | } |
2049 | ||
2050 | static __always_inline | |
2051 | void __bpf_trace_run(struct bpf_prog *prog, u64 *args) | |
2052 | { | |
f03efe49 | 2053 | cant_sleep(); |
c4f6699d | 2054 | rcu_read_lock(); |
c4f6699d | 2055 | (void) BPF_PROG_RUN(prog, args); |
c4f6699d AS |
2056 | rcu_read_unlock(); |
2057 | } | |
2058 | ||
2059 | #define UNPACK(...) __VA_ARGS__ | |
2060 | #define REPEAT_1(FN, DL, X, ...) FN(X) | |
2061 | #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) | |
2062 | #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) | |
2063 | #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) | |
2064 | #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) | |
2065 | #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) | |
2066 | #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) | |
2067 | #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) | |
2068 | #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) | |
2069 | #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) | |
2070 | #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) | |
2071 | #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) | |
2072 | #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) | |
2073 | ||
2074 | #define SARG(X) u64 arg##X | |
2075 | #define COPY(X) args[X] = arg##X | |
2076 | ||
2077 | #define __DL_COM (,) | |
2078 | #define __DL_SEM (;) | |
2079 | ||
2080 | #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 | |
2081 | ||
2082 | #define BPF_TRACE_DEFN_x(x) \ | |
2083 | void bpf_trace_run##x(struct bpf_prog *prog, \ | |
2084 | REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ | |
2085 | { \ | |
2086 | u64 args[x]; \ | |
2087 | REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ | |
2088 | __bpf_trace_run(prog, args); \ | |
2089 | } \ | |
2090 | EXPORT_SYMBOL_GPL(bpf_trace_run##x) | |
2091 | BPF_TRACE_DEFN_x(1); | |
2092 | BPF_TRACE_DEFN_x(2); | |
2093 | BPF_TRACE_DEFN_x(3); | |
2094 | BPF_TRACE_DEFN_x(4); | |
2095 | BPF_TRACE_DEFN_x(5); | |
2096 | BPF_TRACE_DEFN_x(6); | |
2097 | BPF_TRACE_DEFN_x(7); | |
2098 | BPF_TRACE_DEFN_x(8); | |
2099 | BPF_TRACE_DEFN_x(9); | |
2100 | BPF_TRACE_DEFN_x(10); | |
2101 | BPF_TRACE_DEFN_x(11); | |
2102 | BPF_TRACE_DEFN_x(12); | |
2103 | ||
2104 | static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) | |
2105 | { | |
2106 | struct tracepoint *tp = btp->tp; | |
2107 | ||
2108 | /* | |
2109 | * check that program doesn't access arguments beyond what's | |
2110 | * available in this tracepoint | |
2111 | */ | |
2112 | if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) | |
2113 | return -EINVAL; | |
2114 | ||
9df1c28b MM |
2115 | if (prog->aux->max_tp_access > btp->writable_size) |
2116 | return -EINVAL; | |
2117 | ||
c4f6699d AS |
2118 | return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog); |
2119 | } | |
2120 | ||
2121 | int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) | |
2122 | { | |
e16ec340 | 2123 | return __bpf_probe_register(btp, prog); |
c4f6699d AS |
2124 | } |
2125 | ||
2126 | int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) | |
2127 | { | |
e16ec340 | 2128 | return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); |
c4f6699d | 2129 | } |
41bdc4b4 YS |
2130 | |
2131 | int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, | |
2132 | u32 *fd_type, const char **buf, | |
2133 | u64 *probe_offset, u64 *probe_addr) | |
2134 | { | |
2135 | bool is_tracepoint, is_syscall_tp; | |
2136 | struct bpf_prog *prog; | |
2137 | int flags, err = 0; | |
2138 | ||
2139 | prog = event->prog; | |
2140 | if (!prog) | |
2141 | return -ENOENT; | |
2142 | ||
2143 | /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ | |
2144 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) | |
2145 | return -EOPNOTSUPP; | |
2146 | ||
2147 | *prog_id = prog->aux->id; | |
2148 | flags = event->tp_event->flags; | |
2149 | is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; | |
2150 | is_syscall_tp = is_syscall_trace_event(event->tp_event); | |
2151 | ||
2152 | if (is_tracepoint || is_syscall_tp) { | |
2153 | *buf = is_tracepoint ? event->tp_event->tp->name | |
2154 | : event->tp_event->name; | |
2155 | *fd_type = BPF_FD_TYPE_TRACEPOINT; | |
2156 | *probe_offset = 0x0; | |
2157 | *probe_addr = 0x0; | |
2158 | } else { | |
2159 | /* kprobe/uprobe */ | |
2160 | err = -EOPNOTSUPP; | |
2161 | #ifdef CONFIG_KPROBE_EVENTS | |
2162 | if (flags & TRACE_EVENT_FL_KPROBE) | |
2163 | err = bpf_get_kprobe_info(event, fd_type, buf, | |
2164 | probe_offset, probe_addr, | |
2165 | event->attr.type == PERF_TYPE_TRACEPOINT); | |
2166 | #endif | |
2167 | #ifdef CONFIG_UPROBE_EVENTS | |
2168 | if (flags & TRACE_EVENT_FL_UPROBE) | |
2169 | err = bpf_get_uprobe_info(event, fd_type, buf, | |
2170 | probe_offset, | |
2171 | event->attr.type == PERF_TYPE_TRACEPOINT); | |
2172 | #endif | |
2173 | } | |
2174 | ||
2175 | return err; | |
2176 | } | |
a38d1107 | 2177 | |
9db1ff0a YS |
2178 | static int __init send_signal_irq_work_init(void) |
2179 | { | |
2180 | int cpu; | |
2181 | struct send_signal_irq_work *work; | |
2182 | ||
2183 | for_each_possible_cpu(cpu) { | |
2184 | work = per_cpu_ptr(&send_signal_work, cpu); | |
2185 | init_irq_work(&work->irq_work, do_bpf_send_signal); | |
2186 | } | |
2187 | return 0; | |
2188 | } | |
2189 | ||
2190 | subsys_initcall(send_signal_irq_work_init); | |
2191 | ||
a38d1107 | 2192 | #ifdef CONFIG_MODULES |
390e99cf SF |
2193 | static int bpf_event_notify(struct notifier_block *nb, unsigned long op, |
2194 | void *module) | |
a38d1107 MM |
2195 | { |
2196 | struct bpf_trace_module *btm, *tmp; | |
2197 | struct module *mod = module; | |
0340a6b7 | 2198 | int ret = 0; |
a38d1107 MM |
2199 | |
2200 | if (mod->num_bpf_raw_events == 0 || | |
2201 | (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) | |
0340a6b7 | 2202 | goto out; |
a38d1107 MM |
2203 | |
2204 | mutex_lock(&bpf_module_mutex); | |
2205 | ||
2206 | switch (op) { | |
2207 | case MODULE_STATE_COMING: | |
2208 | btm = kzalloc(sizeof(*btm), GFP_KERNEL); | |
2209 | if (btm) { | |
2210 | btm->module = module; | |
2211 | list_add(&btm->list, &bpf_trace_modules); | |
0340a6b7 PZ |
2212 | } else { |
2213 | ret = -ENOMEM; | |
a38d1107 MM |
2214 | } |
2215 | break; | |
2216 | case MODULE_STATE_GOING: | |
2217 | list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { | |
2218 | if (btm->module == module) { | |
2219 | list_del(&btm->list); | |
2220 | kfree(btm); | |
2221 | break; | |
2222 | } | |
2223 | } | |
2224 | break; | |
2225 | } | |
2226 | ||
2227 | mutex_unlock(&bpf_module_mutex); | |
2228 | ||
0340a6b7 PZ |
2229 | out: |
2230 | return notifier_from_errno(ret); | |
a38d1107 MM |
2231 | } |
2232 | ||
2233 | static struct notifier_block bpf_module_nb = { | |
2234 | .notifier_call = bpf_event_notify, | |
2235 | }; | |
2236 | ||
390e99cf | 2237 | static int __init bpf_event_init(void) |
a38d1107 MM |
2238 | { |
2239 | register_module_notifier(&bpf_module_nb); | |
2240 | return 0; | |
2241 | } | |
2242 | ||
2243 | fs_initcall(bpf_event_init); | |
2244 | #endif /* CONFIG_MODULES */ |