1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
4 */
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/slab.h>
8 #include <linux/bpf.h>
9 #include <linux/bpf_verifier.h>
10 #include <linux/bpf_perf_event.h>
11 #include <linux/btf.h>
12 #include <linux/filter.h>
13 #include <linux/uaccess.h>
14 #include <linux/ctype.h>
15 #include <linux/kprobes.h>
16 #include <linux/spinlock.h>
17 #include <linux/syscalls.h>
18 #include <linux/error-injection.h>
19 #include <linux/btf_ids.h>
20 #include <linux/bpf_lsm.h>
21 #include <linux/fprobe.h>
22 #include <linux/bsearch.h>
23 #include <linux/sort.h>
24 #include <linux/key.h>
25 #include <linux/verification.h>
26 #include <linux/namei.h>
27
28 #include <net/bpf_sk_storage.h>
29
30 #include <uapi/linux/bpf.h>
31 #include <uapi/linux/btf.h>
32
33 #include <asm/tlb.h>
34
35 #include "trace_probe.h"
36 #include "trace.h"
37
38 #define CREATE_TRACE_POINTS
39 #include "bpf_trace.h"
40
41 #define bpf_event_rcu_dereference(p) \
42 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
43
44 #define MAX_UPROBE_MULTI_CNT (1U << 20)
45 #define MAX_KPROBE_MULTI_CNT (1U << 20)
46
47 #ifdef CONFIG_MODULES
48 struct bpf_trace_module {
49 struct module *module;
50 struct list_head list;
51 };
52
53 static LIST_HEAD(bpf_trace_modules);
54 static DEFINE_MUTEX(bpf_module_mutex);
55
bpf_get_raw_tracepoint_module(const char * name)56 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
57 {
58 struct bpf_raw_event_map *btp, *ret = NULL;
59 struct bpf_trace_module *btm;
60 unsigned int i;
61
62 mutex_lock(&bpf_module_mutex);
63 list_for_each_entry(btm, &bpf_trace_modules, list) {
64 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
65 btp = &btm->module->bpf_raw_events[i];
66 if (!strcmp(btp->tp->name, name)) {
67 if (try_module_get(btm->module))
68 ret = btp;
69 goto out;
70 }
71 }
72 }
73 out:
74 mutex_unlock(&bpf_module_mutex);
75 return ret;
76 }
77 #else
bpf_get_raw_tracepoint_module(const char * name)78 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
79 {
80 return NULL;
81 }
82 #endif /* CONFIG_MODULES */
83
84 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
85 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
86
87 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
88 u64 flags, const struct btf **btf,
89 s32 *btf_id);
90 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
91 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
92
93 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx);
94 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
95
96 /**
97 * trace_call_bpf - invoke BPF program
98 * @call: tracepoint event
99 * @ctx: opaque context pointer
100 *
101 * kprobe handlers execute BPF programs via this helper.
102 * Can be used from static tracepoints in the future.
103 *
104 * Return: BPF programs always return an integer which is interpreted by
105 * kprobe handler as:
106 * 0 - return from kprobe (event is filtered out)
107 * 1 - store kprobe event into ring buffer
108 * Other values are reserved and currently alias to 1
109 */
trace_call_bpf(struct trace_event_call * call,void * ctx)110 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
111 {
112 unsigned int ret;
113
114 cant_sleep();
115
116 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
117 /*
118 * since some bpf program is already running on this cpu,
119 * don't call into another bpf program (same or different)
120 * and don't send kprobe event into ring-buffer,
121 * so return zero here
122 */
123 ret = 0;
124 goto out;
125 }
126
127 /*
128 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
129 * to all call sites, we did a bpf_prog_array_valid() there to check
130 * whether call->prog_array is empty or not, which is
131 * a heuristic to speed up execution.
132 *
133 * If bpf_prog_array_valid() fetched prog_array was
134 * non-NULL, we go into trace_call_bpf() and do the actual
135 * proper rcu_dereference() under RCU lock.
136 * If it turns out that prog_array is NULL then, we bail out.
137 * For the opposite, if the bpf_prog_array_valid() fetched pointer
138 * was NULL, you'll skip the prog_array with the risk of missing
139 * out of events when it was updated in between this and the
140 * rcu_dereference() which is accepted risk.
141 */
142 rcu_read_lock();
143 ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
144 ctx, bpf_prog_run);
145 rcu_read_unlock();
146
147 out:
148 __this_cpu_dec(bpf_prog_active);
149
150 return ret;
151 }
152
153 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
BPF_CALL_2(bpf_override_return,struct pt_regs *,regs,unsigned long,rc)154 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
155 {
156 regs_set_return_value(regs, rc);
157 override_function_with_return(regs);
158 return 0;
159 }
160
161 static const struct bpf_func_proto bpf_override_return_proto = {
162 .func = bpf_override_return,
163 .gpl_only = true,
164 .ret_type = RET_INTEGER,
165 .arg1_type = ARG_PTR_TO_CTX,
166 .arg2_type = ARG_ANYTHING,
167 };
168 #endif
169
170 static __always_inline int
bpf_probe_read_user_common(void * dst,u32 size,const void __user * unsafe_ptr)171 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
172 {
173 int ret;
174
175 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
176 if (unlikely(ret < 0))
177 memset(dst, 0, size);
178 return ret;
179 }
180
BPF_CALL_3(bpf_probe_read_user,void *,dst,u32,size,const void __user *,unsafe_ptr)181 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
182 const void __user *, unsafe_ptr)
183 {
184 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
185 }
186
187 const struct bpf_func_proto bpf_probe_read_user_proto = {
188 .func = bpf_probe_read_user,
189 .gpl_only = true,
190 .ret_type = RET_INTEGER,
191 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
192 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
193 .arg3_type = ARG_ANYTHING,
194 };
195
196 static __always_inline int
bpf_probe_read_user_str_common(void * dst,u32 size,const void __user * unsafe_ptr)197 bpf_probe_read_user_str_common(void *dst, u32 size,
198 const void __user *unsafe_ptr)
199 {
200 int ret;
201
202 /*
203 * NB: We rely on strncpy_from_user() not copying junk past the NUL
204 * terminator into `dst`.
205 *
206 * strncpy_from_user() does long-sized strides in the fast path. If the
207 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
208 * then there could be junk after the NUL in `dst`. If user takes `dst`
209 * and keys a hash map with it, then semantically identical strings can
210 * occupy multiple entries in the map.
211 */
212 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
213 if (unlikely(ret < 0))
214 memset(dst, 0, size);
215 return ret;
216 }
217
BPF_CALL_3(bpf_probe_read_user_str,void *,dst,u32,size,const void __user *,unsafe_ptr)218 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
219 const void __user *, unsafe_ptr)
220 {
221 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
222 }
223
224 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
225 .func = bpf_probe_read_user_str,
226 .gpl_only = true,
227 .ret_type = RET_INTEGER,
228 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
229 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
230 .arg3_type = ARG_ANYTHING,
231 };
232
BPF_CALL_3(bpf_probe_read_kernel,void *,dst,u32,size,const void *,unsafe_ptr)233 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
234 const void *, unsafe_ptr)
235 {
236 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
237 }
238
239 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
240 .func = bpf_probe_read_kernel,
241 .gpl_only = true,
242 .ret_type = RET_INTEGER,
243 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
244 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
245 .arg3_type = ARG_ANYTHING,
246 };
247
248 static __always_inline int
bpf_probe_read_kernel_str_common(void * dst,u32 size,const void * unsafe_ptr)249 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
250 {
251 int ret;
252
253 /*
254 * The strncpy_from_kernel_nofault() call will likely not fill the
255 * entire buffer, but that's okay in this circumstance as we're probing
256 * arbitrary memory anyway similar to bpf_probe_read_*() and might
257 * as well probe the stack. Thus, memory is explicitly cleared
258 * only in error case, so that improper users ignoring return
259 * code altogether don't copy garbage; otherwise length of string
260 * is returned that can be used for bpf_perf_event_output() et al.
261 */
262 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
263 if (unlikely(ret < 0))
264 memset(dst, 0, size);
265 return ret;
266 }
267
BPF_CALL_3(bpf_probe_read_kernel_str,void *,dst,u32,size,const void *,unsafe_ptr)268 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
269 const void *, unsafe_ptr)
270 {
271 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
272 }
273
274 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
275 .func = bpf_probe_read_kernel_str,
276 .gpl_only = true,
277 .ret_type = RET_INTEGER,
278 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
279 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
280 .arg3_type = ARG_ANYTHING,
281 };
282
283 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
BPF_CALL_3(bpf_probe_read_compat,void *,dst,u32,size,const void *,unsafe_ptr)284 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
285 const void *, unsafe_ptr)
286 {
287 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
288 return bpf_probe_read_user_common(dst, size,
289 (__force void __user *)unsafe_ptr);
290 }
291 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
292 }
293
294 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
295 .func = bpf_probe_read_compat,
296 .gpl_only = true,
297 .ret_type = RET_INTEGER,
298 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
299 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
300 .arg3_type = ARG_ANYTHING,
301 };
302
BPF_CALL_3(bpf_probe_read_compat_str,void *,dst,u32,size,const void *,unsafe_ptr)303 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
304 const void *, unsafe_ptr)
305 {
306 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
307 return bpf_probe_read_user_str_common(dst, size,
308 (__force void __user *)unsafe_ptr);
309 }
310 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
311 }
312
313 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
314 .func = bpf_probe_read_compat_str,
315 .gpl_only = true,
316 .ret_type = RET_INTEGER,
317 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
318 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
319 .arg3_type = ARG_ANYTHING,
320 };
321 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
322
BPF_CALL_3(bpf_probe_write_user,void __user *,unsafe_ptr,const void *,src,u32,size)323 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
324 u32, size)
325 {
326 /*
327 * Ensure we're in user context which is safe for the helper to
328 * run. This helper has no business in a kthread.
329 *
330 * access_ok() should prevent writing to non-user memory, but in
331 * some situations (nommu, temporary switch, etc) access_ok() does
332 * not provide enough validation, hence the check on KERNEL_DS.
333 *
334 * nmi_uaccess_okay() ensures the probe is not run in an interim
335 * state, when the task or mm are switched. This is specifically
336 * required to prevent the use of temporary mm.
337 */
338
339 if (unlikely(in_interrupt() ||
340 current->flags & (PF_KTHREAD | PF_EXITING)))
341 return -EPERM;
342 if (unlikely(!nmi_uaccess_okay()))
343 return -EPERM;
344
345 return copy_to_user_nofault(unsafe_ptr, src, size);
346 }
347
348 static const struct bpf_func_proto bpf_probe_write_user_proto = {
349 .func = bpf_probe_write_user,
350 .gpl_only = true,
351 .ret_type = RET_INTEGER,
352 .arg1_type = ARG_ANYTHING,
353 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
354 .arg3_type = ARG_CONST_SIZE,
355 };
356
bpf_get_probe_write_proto(void)357 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
358 {
359 if (!capable(CAP_SYS_ADMIN))
360 return NULL;
361
362 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
363 current->comm, task_pid_nr(current));
364
365 return &bpf_probe_write_user_proto;
366 }
367
368 #define MAX_TRACE_PRINTK_VARARGS 3
369 #define BPF_TRACE_PRINTK_SIZE 1024
370
BPF_CALL_5(bpf_trace_printk,char *,fmt,u32,fmt_size,u64,arg1,u64,arg2,u64,arg3)371 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
372 u64, arg2, u64, arg3)
373 {
374 u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
375 struct bpf_bprintf_data data = {
376 .get_bin_args = true,
377 .get_buf = true,
378 };
379 int ret;
380
381 ret = bpf_bprintf_prepare(fmt, fmt_size, args,
382 MAX_TRACE_PRINTK_VARARGS, &data);
383 if (ret < 0)
384 return ret;
385
386 ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
387
388 trace_bpf_trace_printk(data.buf);
389
390 bpf_bprintf_cleanup(&data);
391
392 return ret;
393 }
394
395 static const struct bpf_func_proto bpf_trace_printk_proto = {
396 .func = bpf_trace_printk,
397 .gpl_only = true,
398 .ret_type = RET_INTEGER,
399 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
400 .arg2_type = ARG_CONST_SIZE,
401 };
402
__set_printk_clr_event(void)403 static void __set_printk_clr_event(void)
404 {
405 /*
406 * This program might be calling bpf_trace_printk,
407 * so enable the associated bpf_trace/bpf_trace_printk event.
408 * Repeat this each time as it is possible a user has
409 * disabled bpf_trace_printk events. By loading a program
410 * calling bpf_trace_printk() however the user has expressed
411 * the intent to see such events.
412 */
413 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
414 pr_warn_ratelimited("could not enable bpf_trace_printk events");
415 }
416
bpf_get_trace_printk_proto(void)417 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
418 {
419 __set_printk_clr_event();
420 return &bpf_trace_printk_proto;
421 }
422
BPF_CALL_4(bpf_trace_vprintk,char *,fmt,u32,fmt_size,const void *,args,u32,data_len)423 BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,
424 u32, data_len)
425 {
426 struct bpf_bprintf_data data = {
427 .get_bin_args = true,
428 .get_buf = true,
429 };
430 int ret, num_args;
431
432 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
433 (data_len && !args))
434 return -EINVAL;
435 num_args = data_len / 8;
436
437 ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
438 if (ret < 0)
439 return ret;
440
441 ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
442
443 trace_bpf_trace_printk(data.buf);
444
445 bpf_bprintf_cleanup(&data);
446
447 return ret;
448 }
449
450 static const struct bpf_func_proto bpf_trace_vprintk_proto = {
451 .func = bpf_trace_vprintk,
452 .gpl_only = true,
453 .ret_type = RET_INTEGER,
454 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
455 .arg2_type = ARG_CONST_SIZE,
456 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
457 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
458 };
459
bpf_get_trace_vprintk_proto(void)460 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
461 {
462 __set_printk_clr_event();
463 return &bpf_trace_vprintk_proto;
464 }
465
BPF_CALL_5(bpf_seq_printf,struct seq_file *,m,char *,fmt,u32,fmt_size,const void *,args,u32,data_len)466 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
467 const void *, args, u32, data_len)
468 {
469 struct bpf_bprintf_data data = {
470 .get_bin_args = true,
471 };
472 int err, num_args;
473
474 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
475 (data_len && !args))
476 return -EINVAL;
477 num_args = data_len / 8;
478
479 err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
480 if (err < 0)
481 return err;
482
483 seq_bprintf(m, fmt, data.bin_args);
484
485 bpf_bprintf_cleanup(&data);
486
487 return seq_has_overflowed(m) ? -EOVERFLOW : 0;
488 }
489
490 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
491
492 static const struct bpf_func_proto bpf_seq_printf_proto = {
493 .func = bpf_seq_printf,
494 .gpl_only = true,
495 .ret_type = RET_INTEGER,
496 .arg1_type = ARG_PTR_TO_BTF_ID,
497 .arg1_btf_id = &btf_seq_file_ids[0],
498 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
499 .arg3_type = ARG_CONST_SIZE,
500 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
501 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
502 };
503
BPF_CALL_3(bpf_seq_write,struct seq_file *,m,const void *,data,u32,len)504 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
505 {
506 return seq_write(m, data, len) ? -EOVERFLOW : 0;
507 }
508
509 static const struct bpf_func_proto bpf_seq_write_proto = {
510 .func = bpf_seq_write,
511 .gpl_only = true,
512 .ret_type = RET_INTEGER,
513 .arg1_type = ARG_PTR_TO_BTF_ID,
514 .arg1_btf_id = &btf_seq_file_ids[0],
515 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
516 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
517 };
518
BPF_CALL_4(bpf_seq_printf_btf,struct seq_file *,m,struct btf_ptr *,ptr,u32,btf_ptr_size,u64,flags)519 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
520 u32, btf_ptr_size, u64, flags)
521 {
522 const struct btf *btf;
523 s32 btf_id;
524 int ret;
525
526 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
527 if (ret)
528 return ret;
529
530 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
531 }
532
533 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
534 .func = bpf_seq_printf_btf,
535 .gpl_only = true,
536 .ret_type = RET_INTEGER,
537 .arg1_type = ARG_PTR_TO_BTF_ID,
538 .arg1_btf_id = &btf_seq_file_ids[0],
539 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
540 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
541 .arg4_type = ARG_ANYTHING,
542 };
543
544 static __always_inline int
get_map_perf_counter(struct bpf_map * map,u64 flags,u64 * value,u64 * enabled,u64 * running)545 get_map_perf_counter(struct bpf_map *map, u64 flags,
546 u64 *value, u64 *enabled, u64 *running)
547 {
548 struct bpf_array *array = container_of(map, struct bpf_array, map);
549 unsigned int cpu = smp_processor_id();
550 u64 index = flags & BPF_F_INDEX_MASK;
551 struct bpf_event_entry *ee;
552
553 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
554 return -EINVAL;
555 if (index == BPF_F_CURRENT_CPU)
556 index = cpu;
557 if (unlikely(index >= array->map.max_entries))
558 return -E2BIG;
559
560 ee = READ_ONCE(array->ptrs[index]);
561 if (!ee)
562 return -ENOENT;
563
564 return perf_event_read_local(ee->event, value, enabled, running);
565 }
566
BPF_CALL_2(bpf_perf_event_read,struct bpf_map *,map,u64,flags)567 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
568 {
569 u64 value = 0;
570 int err;
571
572 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
573 /*
574 * this api is ugly since we miss [-22..-2] range of valid
575 * counter values, but that's uapi
576 */
577 if (err)
578 return err;
579 return value;
580 }
581
582 static const struct bpf_func_proto bpf_perf_event_read_proto = {
583 .func = bpf_perf_event_read,
584 .gpl_only = true,
585 .ret_type = RET_INTEGER,
586 .arg1_type = ARG_CONST_MAP_PTR,
587 .arg2_type = ARG_ANYTHING,
588 };
589
BPF_CALL_4(bpf_perf_event_read_value,struct bpf_map *,map,u64,flags,struct bpf_perf_event_value *,buf,u32,size)590 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
591 struct bpf_perf_event_value *, buf, u32, size)
592 {
593 int err = -EINVAL;
594
595 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
596 goto clear;
597 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
598 &buf->running);
599 if (unlikely(err))
600 goto clear;
601 return 0;
602 clear:
603 memset(buf, 0, size);
604 return err;
605 }
606
607 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
608 .func = bpf_perf_event_read_value,
609 .gpl_only = true,
610 .ret_type = RET_INTEGER,
611 .arg1_type = ARG_CONST_MAP_PTR,
612 .arg2_type = ARG_ANYTHING,
613 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
614 .arg4_type = ARG_CONST_SIZE,
615 };
616
617 static __always_inline u64
__bpf_perf_event_output(struct pt_regs * regs,struct bpf_map * map,u64 flags,struct perf_raw_record * raw,struct perf_sample_data * sd)618 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
619 u64 flags, struct perf_raw_record *raw,
620 struct perf_sample_data *sd)
621 {
622 struct bpf_array *array = container_of(map, struct bpf_array, map);
623 unsigned int cpu = smp_processor_id();
624 u64 index = flags & BPF_F_INDEX_MASK;
625 struct bpf_event_entry *ee;
626 struct perf_event *event;
627
628 if (index == BPF_F_CURRENT_CPU)
629 index = cpu;
630 if (unlikely(index >= array->map.max_entries))
631 return -E2BIG;
632
633 ee = READ_ONCE(array->ptrs[index]);
634 if (!ee)
635 return -ENOENT;
636
637 event = ee->event;
638 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
639 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
640 return -EINVAL;
641
642 if (unlikely(event->oncpu != cpu))
643 return -EOPNOTSUPP;
644
645 perf_sample_save_raw_data(sd, event, raw);
646
647 return perf_event_output(event, sd, regs);
648 }
649
650 /*
651 * Support executing tracepoints in normal, irq, and nmi context that each call
652 * bpf_perf_event_output
653 */
654 struct bpf_trace_sample_data {
655 struct perf_sample_data sds[3];
656 };
657
658 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
659 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
BPF_CALL_5(bpf_perf_event_output,struct pt_regs *,regs,struct bpf_map *,map,u64,flags,void *,data,u64,size)660 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
661 u64, flags, void *, data, u64, size)
662 {
663 struct bpf_trace_sample_data *sds;
664 struct perf_raw_record raw = {
665 .frag = {
666 .size = size,
667 .data = data,
668 },
669 };
670 struct perf_sample_data *sd;
671 int nest_level, err;
672
673 preempt_disable();
674 sds = this_cpu_ptr(&bpf_trace_sds);
675 nest_level = this_cpu_inc_return(bpf_trace_nest_level);
676
677 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
678 err = -EBUSY;
679 goto out;
680 }
681
682 sd = &sds->sds[nest_level - 1];
683
684 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
685 err = -EINVAL;
686 goto out;
687 }
688
689 perf_sample_data_init(sd, 0, 0);
690
691 err = __bpf_perf_event_output(regs, map, flags, &raw, sd);
692 out:
693 this_cpu_dec(bpf_trace_nest_level);
694 preempt_enable();
695 return err;
696 }
697
698 static const struct bpf_func_proto bpf_perf_event_output_proto = {
699 .func = bpf_perf_event_output,
700 .gpl_only = true,
701 .ret_type = RET_INTEGER,
702 .arg1_type = ARG_PTR_TO_CTX,
703 .arg2_type = ARG_CONST_MAP_PTR,
704 .arg3_type = ARG_ANYTHING,
705 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
706 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
707 };
708
709 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
710 struct bpf_nested_pt_regs {
711 struct pt_regs regs[3];
712 };
713 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
714 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
715
bpf_event_output(struct bpf_map * map,u64 flags,void * meta,u64 meta_size,void * ctx,u64 ctx_size,bpf_ctx_copy_t ctx_copy)716 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
717 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
718 {
719 struct perf_raw_frag frag = {
720 .copy = ctx_copy,
721 .size = ctx_size,
722 .data = ctx,
723 };
724 struct perf_raw_record raw = {
725 .frag = {
726 {
727 .next = ctx_size ? &frag : NULL,
728 },
729 .size = meta_size,
730 .data = meta,
731 },
732 };
733 struct perf_sample_data *sd;
734 struct pt_regs *regs;
735 int nest_level;
736 u64 ret;
737
738 preempt_disable();
739 nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
740
741 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
742 ret = -EBUSY;
743 goto out;
744 }
745 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
746 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
747
748 perf_fetch_caller_regs(regs);
749 perf_sample_data_init(sd, 0, 0);
750
751 ret = __bpf_perf_event_output(regs, map, flags, &raw, sd);
752 out:
753 this_cpu_dec(bpf_event_output_nest_level);
754 preempt_enable();
755 return ret;
756 }
757
BPF_CALL_0(bpf_get_current_task)758 BPF_CALL_0(bpf_get_current_task)
759 {
760 return (long) current;
761 }
762
763 const struct bpf_func_proto bpf_get_current_task_proto = {
764 .func = bpf_get_current_task,
765 .gpl_only = true,
766 .ret_type = RET_INTEGER,
767 };
768
BPF_CALL_0(bpf_get_current_task_btf)769 BPF_CALL_0(bpf_get_current_task_btf)
770 {
771 return (unsigned long) current;
772 }
773
774 const struct bpf_func_proto bpf_get_current_task_btf_proto = {
775 .func = bpf_get_current_task_btf,
776 .gpl_only = true,
777 .ret_type = RET_PTR_TO_BTF_ID_TRUSTED,
778 .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
779 };
780
BPF_CALL_1(bpf_task_pt_regs,struct task_struct *,task)781 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
782 {
783 return (unsigned long) task_pt_regs(task);
784 }
785
786 BTF_ID_LIST(bpf_task_pt_regs_ids)
787 BTF_ID(struct, pt_regs)
788
789 const struct bpf_func_proto bpf_task_pt_regs_proto = {
790 .func = bpf_task_pt_regs,
791 .gpl_only = true,
792 .arg1_type = ARG_PTR_TO_BTF_ID,
793 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
794 .ret_type = RET_PTR_TO_BTF_ID,
795 .ret_btf_id = &bpf_task_pt_regs_ids[0],
796 };
797
BPF_CALL_2(bpf_current_task_under_cgroup,struct bpf_map *,map,u32,idx)798 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
799 {
800 struct bpf_array *array = container_of(map, struct bpf_array, map);
801 struct cgroup *cgrp;
802
803 if (unlikely(idx >= array->map.max_entries))
804 return -E2BIG;
805
806 cgrp = READ_ONCE(array->ptrs[idx]);
807 if (unlikely(!cgrp))
808 return -EAGAIN;
809
810 return task_under_cgroup_hierarchy(current, cgrp);
811 }
812
813 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
814 .func = bpf_current_task_under_cgroup,
815 .gpl_only = false,
816 .ret_type = RET_INTEGER,
817 .arg1_type = ARG_CONST_MAP_PTR,
818 .arg2_type = ARG_ANYTHING,
819 };
820
821 struct send_signal_irq_work {
822 struct irq_work irq_work;
823 struct task_struct *task;
824 u32 sig;
825 enum pid_type type;
826 };
827
828 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
829
do_bpf_send_signal(struct irq_work * entry)830 static void do_bpf_send_signal(struct irq_work *entry)
831 {
832 struct send_signal_irq_work *work;
833
834 work = container_of(entry, struct send_signal_irq_work, irq_work);
835 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
836 put_task_struct(work->task);
837 }
838
bpf_send_signal_common(u32 sig,enum pid_type type)839 static int bpf_send_signal_common(u32 sig, enum pid_type type)
840 {
841 struct send_signal_irq_work *work = NULL;
842
843 /* Similar to bpf_probe_write_user, task needs to be
844 * in a sound condition and kernel memory access be
845 * permitted in order to send signal to the current
846 * task.
847 */
848 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
849 return -EPERM;
850 if (unlikely(!nmi_uaccess_okay()))
851 return -EPERM;
852 /* Task should not be pid=1 to avoid kernel panic. */
853 if (unlikely(is_global_init(current)))
854 return -EPERM;
855
856 if (irqs_disabled()) {
857 /* Do an early check on signal validity. Otherwise,
858 * the error is lost in deferred irq_work.
859 */
860 if (unlikely(!valid_signal(sig)))
861 return -EINVAL;
862
863 work = this_cpu_ptr(&send_signal_work);
864 if (irq_work_is_busy(&work->irq_work))
865 return -EBUSY;
866
867 /* Add the current task, which is the target of sending signal,
868 * to the irq_work. The current task may change when queued
869 * irq works get executed.
870 */
871 work->task = get_task_struct(current);
872 work->sig = sig;
873 work->type = type;
874 irq_work_queue(&work->irq_work);
875 return 0;
876 }
877
878 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
879 }
880
BPF_CALL_1(bpf_send_signal,u32,sig)881 BPF_CALL_1(bpf_send_signal, u32, sig)
882 {
883 return bpf_send_signal_common(sig, PIDTYPE_TGID);
884 }
885
886 static const struct bpf_func_proto bpf_send_signal_proto = {
887 .func = bpf_send_signal,
888 .gpl_only = false,
889 .ret_type = RET_INTEGER,
890 .arg1_type = ARG_ANYTHING,
891 };
892
BPF_CALL_1(bpf_send_signal_thread,u32,sig)893 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
894 {
895 return bpf_send_signal_common(sig, PIDTYPE_PID);
896 }
897
898 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
899 .func = bpf_send_signal_thread,
900 .gpl_only = false,
901 .ret_type = RET_INTEGER,
902 .arg1_type = ARG_ANYTHING,
903 };
904
BPF_CALL_3(bpf_d_path,struct path *,path,char *,buf,u32,sz)905 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
906 {
907 struct path copy;
908 long len;
909 char *p;
910
911 if (!sz)
912 return 0;
913
914 /*
915 * The path pointer is verified as trusted and safe to use,
916 * but let's double check it's valid anyway to workaround
917 * potentially broken verifier.
918 */
919 len = copy_from_kernel_nofault(©, path, sizeof(*path));
920 if (len < 0)
921 return len;
922
923 p = d_path(©, buf, sz);
924 if (IS_ERR(p)) {
925 len = PTR_ERR(p);
926 } else {
927 len = buf + sz - p;
928 memmove(buf, p, len);
929 }
930
931 return len;
932 }
933
934 BTF_SET_START(btf_allowlist_d_path)
935 #ifdef CONFIG_SECURITY
BTF_ID(func,security_file_permission)936 BTF_ID(func, security_file_permission)
937 BTF_ID(func, security_inode_getattr)
938 BTF_ID(func, security_file_open)
939 #endif
940 #ifdef CONFIG_SECURITY_PATH
941 BTF_ID(func, security_path_truncate)
942 #endif
943 BTF_ID(func, vfs_truncate)
944 BTF_ID(func, vfs_fallocate)
945 BTF_ID(func, dentry_open)
946 BTF_ID(func, vfs_getattr)
947 BTF_ID(func, filp_close)
948 BTF_SET_END(btf_allowlist_d_path)
949
950 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
951 {
952 if (prog->type == BPF_PROG_TYPE_TRACING &&
953 prog->expected_attach_type == BPF_TRACE_ITER)
954 return true;
955
956 if (prog->type == BPF_PROG_TYPE_LSM)
957 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
958
959 return btf_id_set_contains(&btf_allowlist_d_path,
960 prog->aux->attach_btf_id);
961 }
962
963 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
964
965 static const struct bpf_func_proto bpf_d_path_proto = {
966 .func = bpf_d_path,
967 .gpl_only = false,
968 .ret_type = RET_INTEGER,
969 .arg1_type = ARG_PTR_TO_BTF_ID,
970 .arg1_btf_id = &bpf_d_path_btf_ids[0],
971 .arg2_type = ARG_PTR_TO_MEM,
972 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
973 .allowed = bpf_d_path_allowed,
974 };
975
976 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
977 BTF_F_PTR_RAW | BTF_F_ZERO)
978
bpf_btf_printf_prepare(struct btf_ptr * ptr,u32 btf_ptr_size,u64 flags,const struct btf ** btf,s32 * btf_id)979 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
980 u64 flags, const struct btf **btf,
981 s32 *btf_id)
982 {
983 const struct btf_type *t;
984
985 if (unlikely(flags & ~(BTF_F_ALL)))
986 return -EINVAL;
987
988 if (btf_ptr_size != sizeof(struct btf_ptr))
989 return -EINVAL;
990
991 *btf = bpf_get_btf_vmlinux();
992
993 if (IS_ERR_OR_NULL(*btf))
994 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
995
996 if (ptr->type_id > 0)
997 *btf_id = ptr->type_id;
998 else
999 return -EINVAL;
1000
1001 if (*btf_id > 0)
1002 t = btf_type_by_id(*btf, *btf_id);
1003 if (*btf_id <= 0 || !t)
1004 return -ENOENT;
1005
1006 return 0;
1007 }
1008
BPF_CALL_5(bpf_snprintf_btf,char *,str,u32,str_size,struct btf_ptr *,ptr,u32,btf_ptr_size,u64,flags)1009 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1010 u32, btf_ptr_size, u64, flags)
1011 {
1012 const struct btf *btf;
1013 s32 btf_id;
1014 int ret;
1015
1016 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1017 if (ret)
1018 return ret;
1019
1020 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1021 flags);
1022 }
1023
1024 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1025 .func = bpf_snprintf_btf,
1026 .gpl_only = false,
1027 .ret_type = RET_INTEGER,
1028 .arg1_type = ARG_PTR_TO_MEM,
1029 .arg2_type = ARG_CONST_SIZE,
1030 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1031 .arg4_type = ARG_CONST_SIZE,
1032 .arg5_type = ARG_ANYTHING,
1033 };
1034
BPF_CALL_1(bpf_get_func_ip_tracing,void *,ctx)1035 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1036 {
1037 /* This helper call is inlined by verifier. */
1038 return ((u64 *)ctx)[-2];
1039 }
1040
1041 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1042 .func = bpf_get_func_ip_tracing,
1043 .gpl_only = true,
1044 .ret_type = RET_INTEGER,
1045 .arg1_type = ARG_PTR_TO_CTX,
1046 };
1047
1048 #ifdef CONFIG_X86_KERNEL_IBT
get_entry_ip(unsigned long fentry_ip)1049 static unsigned long get_entry_ip(unsigned long fentry_ip)
1050 {
1051 u32 instr;
1052
1053 /* Being extra safe in here in case entry ip is on the page-edge. */
1054 if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1))
1055 return fentry_ip;
1056 if (is_endbr(instr))
1057 fentry_ip -= ENDBR_INSN_SIZE;
1058 return fentry_ip;
1059 }
1060 #else
1061 #define get_entry_ip(fentry_ip) fentry_ip
1062 #endif
1063
BPF_CALL_1(bpf_get_func_ip_kprobe,struct pt_regs *,regs)1064 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1065 {
1066 struct bpf_trace_run_ctx *run_ctx __maybe_unused;
1067 struct kprobe *kp;
1068
1069 #ifdef CONFIG_UPROBES
1070 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1071 if (run_ctx->is_uprobe)
1072 return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr;
1073 #endif
1074
1075 kp = kprobe_running();
1076
1077 if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
1078 return 0;
1079
1080 return get_entry_ip((uintptr_t)kp->addr);
1081 }
1082
1083 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1084 .func = bpf_get_func_ip_kprobe,
1085 .gpl_only = true,
1086 .ret_type = RET_INTEGER,
1087 .arg1_type = ARG_PTR_TO_CTX,
1088 };
1089
BPF_CALL_1(bpf_get_func_ip_kprobe_multi,struct pt_regs *,regs)1090 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1091 {
1092 return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
1093 }
1094
1095 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1096 .func = bpf_get_func_ip_kprobe_multi,
1097 .gpl_only = false,
1098 .ret_type = RET_INTEGER,
1099 .arg1_type = ARG_PTR_TO_CTX,
1100 };
1101
BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi,struct pt_regs *,regs)1102 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1103 {
1104 return bpf_kprobe_multi_cookie(current->bpf_ctx);
1105 }
1106
1107 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1108 .func = bpf_get_attach_cookie_kprobe_multi,
1109 .gpl_only = false,
1110 .ret_type = RET_INTEGER,
1111 .arg1_type = ARG_PTR_TO_CTX,
1112 };
1113
BPF_CALL_1(bpf_get_func_ip_uprobe_multi,struct pt_regs *,regs)1114 BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs)
1115 {
1116 return bpf_uprobe_multi_entry_ip(current->bpf_ctx);
1117 }
1118
1119 static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = {
1120 .func = bpf_get_func_ip_uprobe_multi,
1121 .gpl_only = false,
1122 .ret_type = RET_INTEGER,
1123 .arg1_type = ARG_PTR_TO_CTX,
1124 };
1125
BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi,struct pt_regs *,regs)1126 BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs)
1127 {
1128 return bpf_uprobe_multi_cookie(current->bpf_ctx);
1129 }
1130
1131 static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = {
1132 .func = bpf_get_attach_cookie_uprobe_multi,
1133 .gpl_only = false,
1134 .ret_type = RET_INTEGER,
1135 .arg1_type = ARG_PTR_TO_CTX,
1136 };
1137
BPF_CALL_1(bpf_get_attach_cookie_trace,void *,ctx)1138 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1139 {
1140 struct bpf_trace_run_ctx *run_ctx;
1141
1142 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1143 return run_ctx->bpf_cookie;
1144 }
1145
1146 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1147 .func = bpf_get_attach_cookie_trace,
1148 .gpl_only = false,
1149 .ret_type = RET_INTEGER,
1150 .arg1_type = ARG_PTR_TO_CTX,
1151 };
1152
BPF_CALL_1(bpf_get_attach_cookie_pe,struct bpf_perf_event_data_kern *,ctx)1153 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1154 {
1155 return ctx->event->bpf_cookie;
1156 }
1157
1158 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1159 .func = bpf_get_attach_cookie_pe,
1160 .gpl_only = false,
1161 .ret_type = RET_INTEGER,
1162 .arg1_type = ARG_PTR_TO_CTX,
1163 };
1164
BPF_CALL_1(bpf_get_attach_cookie_tracing,void *,ctx)1165 BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
1166 {
1167 struct bpf_trace_run_ctx *run_ctx;
1168
1169 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1170 return run_ctx->bpf_cookie;
1171 }
1172
1173 static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
1174 .func = bpf_get_attach_cookie_tracing,
1175 .gpl_only = false,
1176 .ret_type = RET_INTEGER,
1177 .arg1_type = ARG_PTR_TO_CTX,
1178 };
1179
BPF_CALL_3(bpf_get_branch_snapshot,void *,buf,u32,size,u64,flags)1180 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1181 {
1182 #ifndef CONFIG_X86
1183 return -ENOENT;
1184 #else
1185 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1186 u32 entry_cnt = size / br_entry_size;
1187
1188 entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1189
1190 if (unlikely(flags))
1191 return -EINVAL;
1192
1193 if (!entry_cnt)
1194 return -ENOENT;
1195
1196 return entry_cnt * br_entry_size;
1197 #endif
1198 }
1199
1200 static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1201 .func = bpf_get_branch_snapshot,
1202 .gpl_only = true,
1203 .ret_type = RET_INTEGER,
1204 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1205 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1206 };
1207
BPF_CALL_3(get_func_arg,void *,ctx,u32,n,u64 *,value)1208 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1209 {
1210 /* This helper call is inlined by verifier. */
1211 u64 nr_args = ((u64 *)ctx)[-1];
1212
1213 if ((u64) n >= nr_args)
1214 return -EINVAL;
1215 *value = ((u64 *)ctx)[n];
1216 return 0;
1217 }
1218
1219 static const struct bpf_func_proto bpf_get_func_arg_proto = {
1220 .func = get_func_arg,
1221 .ret_type = RET_INTEGER,
1222 .arg1_type = ARG_PTR_TO_CTX,
1223 .arg2_type = ARG_ANYTHING,
1224 .arg3_type = ARG_PTR_TO_LONG,
1225 };
1226
BPF_CALL_2(get_func_ret,void *,ctx,u64 *,value)1227 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1228 {
1229 /* This helper call is inlined by verifier. */
1230 u64 nr_args = ((u64 *)ctx)[-1];
1231
1232 *value = ((u64 *)ctx)[nr_args];
1233 return 0;
1234 }
1235
1236 static const struct bpf_func_proto bpf_get_func_ret_proto = {
1237 .func = get_func_ret,
1238 .ret_type = RET_INTEGER,
1239 .arg1_type = ARG_PTR_TO_CTX,
1240 .arg2_type = ARG_PTR_TO_LONG,
1241 };
1242
BPF_CALL_1(get_func_arg_cnt,void *,ctx)1243 BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1244 {
1245 /* This helper call is inlined by verifier. */
1246 return ((u64 *)ctx)[-1];
1247 }
1248
1249 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1250 .func = get_func_arg_cnt,
1251 .ret_type = RET_INTEGER,
1252 .arg1_type = ARG_PTR_TO_CTX,
1253 };
1254
1255 #ifdef CONFIG_KEYS
1256 __diag_push();
1257 __diag_ignore_all("-Wmissing-prototypes",
1258 "kfuncs which will be used in BPF programs");
1259
1260 /**
1261 * bpf_lookup_user_key - lookup a key by its serial
1262 * @serial: key handle serial number
1263 * @flags: lookup-specific flags
1264 *
1265 * Search a key with a given *serial* and the provided *flags*.
1266 * If found, increment the reference count of the key by one, and
1267 * return it in the bpf_key structure.
1268 *
1269 * The bpf_key structure must be passed to bpf_key_put() when done
1270 * with it, so that the key reference count is decremented and the
1271 * bpf_key structure is freed.
1272 *
1273 * Permission checks are deferred to the time the key is used by
1274 * one of the available key-specific kfuncs.
1275 *
1276 * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
1277 * special keyring (e.g. session keyring), if it doesn't yet exist.
1278 * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
1279 * for the key construction, and to retrieve uninstantiated keys (keys
1280 * without data attached to them).
1281 *
1282 * Return: a bpf_key pointer with a valid key pointer if the key is found, a
1283 * NULL pointer otherwise.
1284 */
bpf_lookup_user_key(u32 serial,u64 flags)1285 __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1286 {
1287 key_ref_t key_ref;
1288 struct bpf_key *bkey;
1289
1290 if (flags & ~KEY_LOOKUP_ALL)
1291 return NULL;
1292
1293 /*
1294 * Permission check is deferred until the key is used, as the
1295 * intent of the caller is unknown here.
1296 */
1297 key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
1298 if (IS_ERR(key_ref))
1299 return NULL;
1300
1301 bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
1302 if (!bkey) {
1303 key_put(key_ref_to_ptr(key_ref));
1304 return NULL;
1305 }
1306
1307 bkey->key = key_ref_to_ptr(key_ref);
1308 bkey->has_ref = true;
1309
1310 return bkey;
1311 }
1312
1313 /**
1314 * bpf_lookup_system_key - lookup a key by a system-defined ID
1315 * @id: key ID
1316 *
1317 * Obtain a bpf_key structure with a key pointer set to the passed key ID.
1318 * The key pointer is marked as invalid, to prevent bpf_key_put() from
1319 * attempting to decrement the key reference count on that pointer. The key
1320 * pointer set in such way is currently understood only by
1321 * verify_pkcs7_signature().
1322 *
1323 * Set *id* to one of the values defined in include/linux/verification.h:
1324 * 0 for the primary keyring (immutable keyring of system keys);
1325 * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
1326 * (where keys can be added only if they are vouched for by existing keys
1327 * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
1328 * keyring (primarily used by the integrity subsystem to verify a kexec'ed
1329 * kerned image and, possibly, the initramfs signature).
1330 *
1331 * Return: a bpf_key pointer with an invalid key pointer set from the
1332 * pre-determined ID on success, a NULL pointer otherwise
1333 */
bpf_lookup_system_key(u64 id)1334 __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id)
1335 {
1336 struct bpf_key *bkey;
1337
1338 if (system_keyring_id_check(id) < 0)
1339 return NULL;
1340
1341 bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
1342 if (!bkey)
1343 return NULL;
1344
1345 bkey->key = (struct key *)(unsigned long)id;
1346 bkey->has_ref = false;
1347
1348 return bkey;
1349 }
1350
1351 /**
1352 * bpf_key_put - decrement key reference count if key is valid and free bpf_key
1353 * @bkey: bpf_key structure
1354 *
1355 * Decrement the reference count of the key inside *bkey*, if the pointer
1356 * is valid, and free *bkey*.
1357 */
bpf_key_put(struct bpf_key * bkey)1358 __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
1359 {
1360 if (bkey->has_ref)
1361 key_put(bkey->key);
1362
1363 kfree(bkey);
1364 }
1365
1366 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1367 /**
1368 * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
1369 * @data_ptr: data to verify
1370 * @sig_ptr: signature of the data
1371 * @trusted_keyring: keyring with keys trusted for signature verification
1372 *
1373 * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
1374 * with keys in a keyring referenced by *trusted_keyring*.
1375 *
1376 * Return: 0 on success, a negative value on error.
1377 */
bpf_verify_pkcs7_signature(struct bpf_dynptr_kern * data_ptr,struct bpf_dynptr_kern * sig_ptr,struct bpf_key * trusted_keyring)1378 __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
1379 struct bpf_dynptr_kern *sig_ptr,
1380 struct bpf_key *trusted_keyring)
1381 {
1382 int ret;
1383
1384 if (trusted_keyring->has_ref) {
1385 /*
1386 * Do the permission check deferred in bpf_lookup_user_key().
1387 * See bpf_lookup_user_key() for more details.
1388 *
1389 * A call to key_task_permission() here would be redundant, as
1390 * it is already done by keyring_search() called by
1391 * find_asymmetric_key().
1392 */
1393 ret = key_validate(trusted_keyring->key);
1394 if (ret < 0)
1395 return ret;
1396 }
1397
1398 return verify_pkcs7_signature(data_ptr->data,
1399 __bpf_dynptr_size(data_ptr),
1400 sig_ptr->data,
1401 __bpf_dynptr_size(sig_ptr),
1402 trusted_keyring->key,
1403 VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
1404 NULL);
1405 }
1406 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
1407
1408 __diag_pop();
1409
1410 BTF_SET8_START(key_sig_kfunc_set)
1411 BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
1412 BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
1413 BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
1414 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1415 BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
1416 #endif
1417 BTF_SET8_END(key_sig_kfunc_set)
1418
1419 static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
1420 .owner = THIS_MODULE,
1421 .set = &key_sig_kfunc_set,
1422 };
1423
bpf_key_sig_kfuncs_init(void)1424 static int __init bpf_key_sig_kfuncs_init(void)
1425 {
1426 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
1427 &bpf_key_sig_kfunc_set);
1428 }
1429
1430 late_initcall(bpf_key_sig_kfuncs_init);
1431 #endif /* CONFIG_KEYS */
1432
1433 static const struct bpf_func_proto *
bpf_tracing_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1434 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1435 {
1436 switch (func_id) {
1437 case BPF_FUNC_map_lookup_elem:
1438 return &bpf_map_lookup_elem_proto;
1439 case BPF_FUNC_map_update_elem:
1440 return &bpf_map_update_elem_proto;
1441 case BPF_FUNC_map_delete_elem:
1442 return &bpf_map_delete_elem_proto;
1443 case BPF_FUNC_map_push_elem:
1444 return &bpf_map_push_elem_proto;
1445 case BPF_FUNC_map_pop_elem:
1446 return &bpf_map_pop_elem_proto;
1447 case BPF_FUNC_map_peek_elem:
1448 return &bpf_map_peek_elem_proto;
1449 case BPF_FUNC_map_lookup_percpu_elem:
1450 return &bpf_map_lookup_percpu_elem_proto;
1451 case BPF_FUNC_ktime_get_ns:
1452 return &bpf_ktime_get_ns_proto;
1453 case BPF_FUNC_ktime_get_boot_ns:
1454 return &bpf_ktime_get_boot_ns_proto;
1455 case BPF_FUNC_tail_call:
1456 return &bpf_tail_call_proto;
1457 case BPF_FUNC_get_current_pid_tgid:
1458 return &bpf_get_current_pid_tgid_proto;
1459 case BPF_FUNC_get_current_task:
1460 return &bpf_get_current_task_proto;
1461 case BPF_FUNC_get_current_task_btf:
1462 return &bpf_get_current_task_btf_proto;
1463 case BPF_FUNC_task_pt_regs:
1464 return &bpf_task_pt_regs_proto;
1465 case BPF_FUNC_get_current_uid_gid:
1466 return &bpf_get_current_uid_gid_proto;
1467 case BPF_FUNC_get_current_comm:
1468 return &bpf_get_current_comm_proto;
1469 case BPF_FUNC_trace_printk:
1470 return bpf_get_trace_printk_proto();
1471 case BPF_FUNC_get_smp_processor_id:
1472 return &bpf_get_smp_processor_id_proto;
1473 case BPF_FUNC_get_numa_node_id:
1474 return &bpf_get_numa_node_id_proto;
1475 case BPF_FUNC_perf_event_read:
1476 return &bpf_perf_event_read_proto;
1477 case BPF_FUNC_current_task_under_cgroup:
1478 return &bpf_current_task_under_cgroup_proto;
1479 case BPF_FUNC_get_prandom_u32:
1480 return &bpf_get_prandom_u32_proto;
1481 case BPF_FUNC_probe_write_user:
1482 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1483 NULL : bpf_get_probe_write_proto();
1484 case BPF_FUNC_probe_read_user:
1485 return &bpf_probe_read_user_proto;
1486 case BPF_FUNC_probe_read_kernel:
1487 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1488 NULL : &bpf_probe_read_kernel_proto;
1489 case BPF_FUNC_probe_read_user_str:
1490 return &bpf_probe_read_user_str_proto;
1491 case BPF_FUNC_probe_read_kernel_str:
1492 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1493 NULL : &bpf_probe_read_kernel_str_proto;
1494 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1495 case BPF_FUNC_probe_read:
1496 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1497 NULL : &bpf_probe_read_compat_proto;
1498 case BPF_FUNC_probe_read_str:
1499 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1500 NULL : &bpf_probe_read_compat_str_proto;
1501 #endif
1502 #ifdef CONFIG_CGROUPS
1503 case BPF_FUNC_cgrp_storage_get:
1504 return &bpf_cgrp_storage_get_proto;
1505 case BPF_FUNC_cgrp_storage_delete:
1506 return &bpf_cgrp_storage_delete_proto;
1507 #endif
1508 case BPF_FUNC_send_signal:
1509 return &bpf_send_signal_proto;
1510 case BPF_FUNC_send_signal_thread:
1511 return &bpf_send_signal_thread_proto;
1512 case BPF_FUNC_perf_event_read_value:
1513 return &bpf_perf_event_read_value_proto;
1514 case BPF_FUNC_get_ns_current_pid_tgid:
1515 return &bpf_get_ns_current_pid_tgid_proto;
1516 case BPF_FUNC_ringbuf_output:
1517 return &bpf_ringbuf_output_proto;
1518 case BPF_FUNC_ringbuf_reserve:
1519 return &bpf_ringbuf_reserve_proto;
1520 case BPF_FUNC_ringbuf_submit:
1521 return &bpf_ringbuf_submit_proto;
1522 case BPF_FUNC_ringbuf_discard:
1523 return &bpf_ringbuf_discard_proto;
1524 case BPF_FUNC_ringbuf_query:
1525 return &bpf_ringbuf_query_proto;
1526 case BPF_FUNC_jiffies64:
1527 return &bpf_jiffies64_proto;
1528 case BPF_FUNC_get_task_stack:
1529 return &bpf_get_task_stack_proto;
1530 case BPF_FUNC_copy_from_user:
1531 return &bpf_copy_from_user_proto;
1532 case BPF_FUNC_copy_from_user_task:
1533 return &bpf_copy_from_user_task_proto;
1534 case BPF_FUNC_snprintf_btf:
1535 return &bpf_snprintf_btf_proto;
1536 case BPF_FUNC_per_cpu_ptr:
1537 return &bpf_per_cpu_ptr_proto;
1538 case BPF_FUNC_this_cpu_ptr:
1539 return &bpf_this_cpu_ptr_proto;
1540 case BPF_FUNC_task_storage_get:
1541 if (bpf_prog_check_recur(prog))
1542 return &bpf_task_storage_get_recur_proto;
1543 return &bpf_task_storage_get_proto;
1544 case BPF_FUNC_task_storage_delete:
1545 if (bpf_prog_check_recur(prog))
1546 return &bpf_task_storage_delete_recur_proto;
1547 return &bpf_task_storage_delete_proto;
1548 case BPF_FUNC_for_each_map_elem:
1549 return &bpf_for_each_map_elem_proto;
1550 case BPF_FUNC_snprintf:
1551 return &bpf_snprintf_proto;
1552 case BPF_FUNC_get_func_ip:
1553 return &bpf_get_func_ip_proto_tracing;
1554 case BPF_FUNC_get_branch_snapshot:
1555 return &bpf_get_branch_snapshot_proto;
1556 case BPF_FUNC_find_vma:
1557 return &bpf_find_vma_proto;
1558 case BPF_FUNC_trace_vprintk:
1559 return bpf_get_trace_vprintk_proto();
1560 default:
1561 return bpf_base_func_proto(func_id);
1562 }
1563 }
1564
1565 static const struct bpf_func_proto *
kprobe_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1566 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1567 {
1568 switch (func_id) {
1569 case BPF_FUNC_perf_event_output:
1570 return &bpf_perf_event_output_proto;
1571 case BPF_FUNC_get_stackid:
1572 return &bpf_get_stackid_proto;
1573 case BPF_FUNC_get_stack:
1574 return &bpf_get_stack_proto;
1575 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1576 case BPF_FUNC_override_return:
1577 return &bpf_override_return_proto;
1578 #endif
1579 case BPF_FUNC_get_func_ip:
1580 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI)
1581 return &bpf_get_func_ip_proto_kprobe_multi;
1582 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
1583 return &bpf_get_func_ip_proto_uprobe_multi;
1584 return &bpf_get_func_ip_proto_kprobe;
1585 case BPF_FUNC_get_attach_cookie:
1586 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI)
1587 return &bpf_get_attach_cookie_proto_kmulti;
1588 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI)
1589 return &bpf_get_attach_cookie_proto_umulti;
1590 return &bpf_get_attach_cookie_proto_trace;
1591 default:
1592 return bpf_tracing_func_proto(func_id, prog);
1593 }
1594 }
1595
1596 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
kprobe_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)1597 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1598 const struct bpf_prog *prog,
1599 struct bpf_insn_access_aux *info)
1600 {
1601 if (off < 0 || off >= sizeof(struct pt_regs))
1602 return false;
1603 if (type != BPF_READ)
1604 return false;
1605 if (off % size != 0)
1606 return false;
1607 /*
1608 * Assertion for 32 bit to make sure last 8 byte access
1609 * (BPF_DW) to the last 4 byte member is disallowed.
1610 */
1611 if (off + size > sizeof(struct pt_regs))
1612 return false;
1613
1614 return true;
1615 }
1616
1617 const struct bpf_verifier_ops kprobe_verifier_ops = {
1618 .get_func_proto = kprobe_prog_func_proto,
1619 .is_valid_access = kprobe_prog_is_valid_access,
1620 };
1621
1622 const struct bpf_prog_ops kprobe_prog_ops = {
1623 };
1624
BPF_CALL_5(bpf_perf_event_output_tp,void *,tp_buff,struct bpf_map *,map,u64,flags,void *,data,u64,size)1625 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1626 u64, flags, void *, data, u64, size)
1627 {
1628 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1629
1630 /*
1631 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1632 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1633 * from there and call the same bpf_perf_event_output() helper inline.
1634 */
1635 return ____bpf_perf_event_output(regs, map, flags, data, size);
1636 }
1637
1638 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1639 .func = bpf_perf_event_output_tp,
1640 .gpl_only = true,
1641 .ret_type = RET_INTEGER,
1642 .arg1_type = ARG_PTR_TO_CTX,
1643 .arg2_type = ARG_CONST_MAP_PTR,
1644 .arg3_type = ARG_ANYTHING,
1645 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1646 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1647 };
1648
BPF_CALL_3(bpf_get_stackid_tp,void *,tp_buff,struct bpf_map *,map,u64,flags)1649 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1650 u64, flags)
1651 {
1652 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1653
1654 /*
1655 * Same comment as in bpf_perf_event_output_tp(), only that this time
1656 * the other helper's function body cannot be inlined due to being
1657 * external, thus we need to call raw helper function.
1658 */
1659 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1660 flags, 0, 0);
1661 }
1662
1663 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1664 .func = bpf_get_stackid_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
BPF_CALL_4(bpf_get_stack_tp,void *,tp_buff,void *,buf,u32,size,u64,flags)1672 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1673 u64, flags)
1674 {
1675 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1676
1677 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1678 (unsigned long) size, flags, 0);
1679 }
1680
1681 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1682 .func = bpf_get_stack_tp,
1683 .gpl_only = true,
1684 .ret_type = RET_INTEGER,
1685 .arg1_type = ARG_PTR_TO_CTX,
1686 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1687 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1688 .arg4_type = ARG_ANYTHING,
1689 };
1690
1691 static const struct bpf_func_proto *
tp_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1692 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1693 {
1694 switch (func_id) {
1695 case BPF_FUNC_perf_event_output:
1696 return &bpf_perf_event_output_proto_tp;
1697 case BPF_FUNC_get_stackid:
1698 return &bpf_get_stackid_proto_tp;
1699 case BPF_FUNC_get_stack:
1700 return &bpf_get_stack_proto_tp;
1701 case BPF_FUNC_get_attach_cookie:
1702 return &bpf_get_attach_cookie_proto_trace;
1703 default:
1704 return bpf_tracing_func_proto(func_id, prog);
1705 }
1706 }
1707
tp_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)1708 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1709 const struct bpf_prog *prog,
1710 struct bpf_insn_access_aux *info)
1711 {
1712 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1713 return false;
1714 if (type != BPF_READ)
1715 return false;
1716 if (off % size != 0)
1717 return false;
1718
1719 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1720 return true;
1721 }
1722
1723 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1724 .get_func_proto = tp_prog_func_proto,
1725 .is_valid_access = tp_prog_is_valid_access,
1726 };
1727
1728 const struct bpf_prog_ops tracepoint_prog_ops = {
1729 };
1730
BPF_CALL_3(bpf_perf_prog_read_value,struct bpf_perf_event_data_kern *,ctx,struct bpf_perf_event_value *,buf,u32,size)1731 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1732 struct bpf_perf_event_value *, buf, u32, size)
1733 {
1734 int err = -EINVAL;
1735
1736 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1737 goto clear;
1738 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1739 &buf->running);
1740 if (unlikely(err))
1741 goto clear;
1742 return 0;
1743 clear:
1744 memset(buf, 0, size);
1745 return err;
1746 }
1747
1748 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1749 .func = bpf_perf_prog_read_value,
1750 .gpl_only = true,
1751 .ret_type = RET_INTEGER,
1752 .arg1_type = ARG_PTR_TO_CTX,
1753 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1754 .arg3_type = ARG_CONST_SIZE,
1755 };
1756
BPF_CALL_4(bpf_read_branch_records,struct bpf_perf_event_data_kern *,ctx,void *,buf,u32,size,u64,flags)1757 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1758 void *, buf, u32, size, u64, flags)
1759 {
1760 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1761 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1762 u32 to_copy;
1763
1764 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1765 return -EINVAL;
1766
1767 if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
1768 return -ENOENT;
1769
1770 if (unlikely(!br_stack))
1771 return -ENOENT;
1772
1773 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1774 return br_stack->nr * br_entry_size;
1775
1776 if (!buf || (size % br_entry_size != 0))
1777 return -EINVAL;
1778
1779 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1780 memcpy(buf, br_stack->entries, to_copy);
1781
1782 return to_copy;
1783 }
1784
1785 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1786 .func = bpf_read_branch_records,
1787 .gpl_only = true,
1788 .ret_type = RET_INTEGER,
1789 .arg1_type = ARG_PTR_TO_CTX,
1790 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1791 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1792 .arg4_type = ARG_ANYTHING,
1793 };
1794
1795 static const struct bpf_func_proto *
pe_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1796 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1797 {
1798 switch (func_id) {
1799 case BPF_FUNC_perf_event_output:
1800 return &bpf_perf_event_output_proto_tp;
1801 case BPF_FUNC_get_stackid:
1802 return &bpf_get_stackid_proto_pe;
1803 case BPF_FUNC_get_stack:
1804 return &bpf_get_stack_proto_pe;
1805 case BPF_FUNC_perf_prog_read_value:
1806 return &bpf_perf_prog_read_value_proto;
1807 case BPF_FUNC_read_branch_records:
1808 return &bpf_read_branch_records_proto;
1809 case BPF_FUNC_get_attach_cookie:
1810 return &bpf_get_attach_cookie_proto_pe;
1811 default:
1812 return bpf_tracing_func_proto(func_id, prog);
1813 }
1814 }
1815
1816 /*
1817 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1818 * to avoid potential recursive reuse issue when/if tracepoints are added
1819 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1820 *
1821 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1822 * in normal, irq, and nmi context.
1823 */
1824 struct bpf_raw_tp_regs {
1825 struct pt_regs regs[3];
1826 };
1827 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1828 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
get_bpf_raw_tp_regs(void)1829 static struct pt_regs *get_bpf_raw_tp_regs(void)
1830 {
1831 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1832 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1833
1834 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1835 this_cpu_dec(bpf_raw_tp_nest_level);
1836 return ERR_PTR(-EBUSY);
1837 }
1838
1839 return &tp_regs->regs[nest_level - 1];
1840 }
1841
put_bpf_raw_tp_regs(void)1842 static void put_bpf_raw_tp_regs(void)
1843 {
1844 this_cpu_dec(bpf_raw_tp_nest_level);
1845 }
1846
BPF_CALL_5(bpf_perf_event_output_raw_tp,struct bpf_raw_tracepoint_args *,args,struct bpf_map *,map,u64,flags,void *,data,u64,size)1847 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1848 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1849 {
1850 struct pt_regs *regs = get_bpf_raw_tp_regs();
1851 int ret;
1852
1853 if (IS_ERR(regs))
1854 return PTR_ERR(regs);
1855
1856 perf_fetch_caller_regs(regs);
1857 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1858
1859 put_bpf_raw_tp_regs();
1860 return ret;
1861 }
1862
1863 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1864 .func = bpf_perf_event_output_raw_tp,
1865 .gpl_only = true,
1866 .ret_type = RET_INTEGER,
1867 .arg1_type = ARG_PTR_TO_CTX,
1868 .arg2_type = ARG_CONST_MAP_PTR,
1869 .arg3_type = ARG_ANYTHING,
1870 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1871 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1872 };
1873
1874 extern const struct bpf_func_proto bpf_skb_output_proto;
1875 extern const struct bpf_func_proto bpf_xdp_output_proto;
1876 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1877
BPF_CALL_3(bpf_get_stackid_raw_tp,struct bpf_raw_tracepoint_args *,args,struct bpf_map *,map,u64,flags)1878 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1879 struct bpf_map *, map, u64, flags)
1880 {
1881 struct pt_regs *regs = get_bpf_raw_tp_regs();
1882 int ret;
1883
1884 if (IS_ERR(regs))
1885 return PTR_ERR(regs);
1886
1887 perf_fetch_caller_regs(regs);
1888 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1889 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1890 flags, 0, 0);
1891 put_bpf_raw_tp_regs();
1892 return ret;
1893 }
1894
1895 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1896 .func = bpf_get_stackid_raw_tp,
1897 .gpl_only = true,
1898 .ret_type = RET_INTEGER,
1899 .arg1_type = ARG_PTR_TO_CTX,
1900 .arg2_type = ARG_CONST_MAP_PTR,
1901 .arg3_type = ARG_ANYTHING,
1902 };
1903
BPF_CALL_4(bpf_get_stack_raw_tp,struct bpf_raw_tracepoint_args *,args,void *,buf,u32,size,u64,flags)1904 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1905 void *, buf, u32, size, u64, flags)
1906 {
1907 struct pt_regs *regs = get_bpf_raw_tp_regs();
1908 int ret;
1909
1910 if (IS_ERR(regs))
1911 return PTR_ERR(regs);
1912
1913 perf_fetch_caller_regs(regs);
1914 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1915 (unsigned long) size, flags, 0);
1916 put_bpf_raw_tp_regs();
1917 return ret;
1918 }
1919
1920 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1921 .func = bpf_get_stack_raw_tp,
1922 .gpl_only = true,
1923 .ret_type = RET_INTEGER,
1924 .arg1_type = ARG_PTR_TO_CTX,
1925 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1926 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1927 .arg4_type = ARG_ANYTHING,
1928 };
1929
1930 static const struct bpf_func_proto *
raw_tp_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1931 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1932 {
1933 switch (func_id) {
1934 case BPF_FUNC_perf_event_output:
1935 return &bpf_perf_event_output_proto_raw_tp;
1936 case BPF_FUNC_get_stackid:
1937 return &bpf_get_stackid_proto_raw_tp;
1938 case BPF_FUNC_get_stack:
1939 return &bpf_get_stack_proto_raw_tp;
1940 default:
1941 return bpf_tracing_func_proto(func_id, prog);
1942 }
1943 }
1944
1945 const struct bpf_func_proto *
tracing_prog_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1946 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1947 {
1948 const struct bpf_func_proto *fn;
1949
1950 switch (func_id) {
1951 #ifdef CONFIG_NET
1952 case BPF_FUNC_skb_output:
1953 return &bpf_skb_output_proto;
1954 case BPF_FUNC_xdp_output:
1955 return &bpf_xdp_output_proto;
1956 case BPF_FUNC_skc_to_tcp6_sock:
1957 return &bpf_skc_to_tcp6_sock_proto;
1958 case BPF_FUNC_skc_to_tcp_sock:
1959 return &bpf_skc_to_tcp_sock_proto;
1960 case BPF_FUNC_skc_to_tcp_timewait_sock:
1961 return &bpf_skc_to_tcp_timewait_sock_proto;
1962 case BPF_FUNC_skc_to_tcp_request_sock:
1963 return &bpf_skc_to_tcp_request_sock_proto;
1964 case BPF_FUNC_skc_to_udp6_sock:
1965 return &bpf_skc_to_udp6_sock_proto;
1966 case BPF_FUNC_skc_to_unix_sock:
1967 return &bpf_skc_to_unix_sock_proto;
1968 case BPF_FUNC_skc_to_mptcp_sock:
1969 return &bpf_skc_to_mptcp_sock_proto;
1970 case BPF_FUNC_sk_storage_get:
1971 return &bpf_sk_storage_get_tracing_proto;
1972 case BPF_FUNC_sk_storage_delete:
1973 return &bpf_sk_storage_delete_tracing_proto;
1974 case BPF_FUNC_sock_from_file:
1975 return &bpf_sock_from_file_proto;
1976 case BPF_FUNC_get_socket_cookie:
1977 return &bpf_get_socket_ptr_cookie_proto;
1978 case BPF_FUNC_xdp_get_buff_len:
1979 return &bpf_xdp_get_buff_len_trace_proto;
1980 #endif
1981 case BPF_FUNC_seq_printf:
1982 return prog->expected_attach_type == BPF_TRACE_ITER ?
1983 &bpf_seq_printf_proto :
1984 NULL;
1985 case BPF_FUNC_seq_write:
1986 return prog->expected_attach_type == BPF_TRACE_ITER ?
1987 &bpf_seq_write_proto :
1988 NULL;
1989 case BPF_FUNC_seq_printf_btf:
1990 return prog->expected_attach_type == BPF_TRACE_ITER ?
1991 &bpf_seq_printf_btf_proto :
1992 NULL;
1993 case BPF_FUNC_d_path:
1994 return &bpf_d_path_proto;
1995 case BPF_FUNC_get_func_arg:
1996 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1997 case BPF_FUNC_get_func_ret:
1998 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1999 case BPF_FUNC_get_func_arg_cnt:
2000 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
2001 case BPF_FUNC_get_attach_cookie:
2002 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
2003 default:
2004 fn = raw_tp_prog_func_proto(func_id, prog);
2005 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
2006 fn = bpf_iter_get_func_proto(func_id, prog);
2007 return fn;
2008 }
2009 }
2010
raw_tp_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2011 static bool raw_tp_prog_is_valid_access(int off, int size,
2012 enum bpf_access_type type,
2013 const struct bpf_prog *prog,
2014 struct bpf_insn_access_aux *info)
2015 {
2016 return bpf_tracing_ctx_access(off, size, type);
2017 }
2018
tracing_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2019 static bool tracing_prog_is_valid_access(int off, int size,
2020 enum bpf_access_type type,
2021 const struct bpf_prog *prog,
2022 struct bpf_insn_access_aux *info)
2023 {
2024 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
2025 }
2026
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2027 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
2028 const union bpf_attr *kattr,
2029 union bpf_attr __user *uattr)
2030 {
2031 return -ENOTSUPP;
2032 }
2033
2034 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
2035 .get_func_proto = raw_tp_prog_func_proto,
2036 .is_valid_access = raw_tp_prog_is_valid_access,
2037 };
2038
2039 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
2040 #ifdef CONFIG_NET
2041 .test_run = bpf_prog_test_run_raw_tp,
2042 #endif
2043 };
2044
2045 const struct bpf_verifier_ops tracing_verifier_ops = {
2046 .get_func_proto = tracing_prog_func_proto,
2047 .is_valid_access = tracing_prog_is_valid_access,
2048 };
2049
2050 const struct bpf_prog_ops tracing_prog_ops = {
2051 .test_run = bpf_prog_test_run_tracing,
2052 };
2053
raw_tp_writable_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2054 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
2055 enum bpf_access_type type,
2056 const struct bpf_prog *prog,
2057 struct bpf_insn_access_aux *info)
2058 {
2059 if (off == 0) {
2060 if (size != sizeof(u64) || type != BPF_READ)
2061 return false;
2062 info->reg_type = PTR_TO_TP_BUFFER;
2063 }
2064 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
2065 }
2066
2067 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
2068 .get_func_proto = raw_tp_prog_func_proto,
2069 .is_valid_access = raw_tp_writable_prog_is_valid_access,
2070 };
2071
2072 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
2073 };
2074
pe_prog_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2075 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
2076 const struct bpf_prog *prog,
2077 struct bpf_insn_access_aux *info)
2078 {
2079 const int size_u64 = sizeof(u64);
2080
2081 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
2082 return false;
2083 if (type != BPF_READ)
2084 return false;
2085 if (off % size != 0) {
2086 if (sizeof(unsigned long) != 4)
2087 return false;
2088 if (size != 8)
2089 return false;
2090 if (off % size != 4)
2091 return false;
2092 }
2093
2094 switch (off) {
2095 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
2096 bpf_ctx_record_field_size(info, size_u64);
2097 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2098 return false;
2099 break;
2100 case bpf_ctx_range(struct bpf_perf_event_data, addr):
2101 bpf_ctx_record_field_size(info, size_u64);
2102 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2103 return false;
2104 break;
2105 default:
2106 if (size != sizeof(long))
2107 return false;
2108 }
2109
2110 return true;
2111 }
2112
pe_prog_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)2113 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
2114 const struct bpf_insn *si,
2115 struct bpf_insn *insn_buf,
2116 struct bpf_prog *prog, u32 *target_size)
2117 {
2118 struct bpf_insn *insn = insn_buf;
2119
2120 switch (si->off) {
2121 case offsetof(struct bpf_perf_event_data, sample_period):
2122 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2123 data), si->dst_reg, si->src_reg,
2124 offsetof(struct bpf_perf_event_data_kern, data));
2125 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2126 bpf_target_off(struct perf_sample_data, period, 8,
2127 target_size));
2128 break;
2129 case offsetof(struct bpf_perf_event_data, addr):
2130 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2131 data), si->dst_reg, si->src_reg,
2132 offsetof(struct bpf_perf_event_data_kern, data));
2133 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2134 bpf_target_off(struct perf_sample_data, addr, 8,
2135 target_size));
2136 break;
2137 default:
2138 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2139 regs), si->dst_reg, si->src_reg,
2140 offsetof(struct bpf_perf_event_data_kern, regs));
2141 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
2142 si->off);
2143 break;
2144 }
2145
2146 return insn - insn_buf;
2147 }
2148
2149 const struct bpf_verifier_ops perf_event_verifier_ops = {
2150 .get_func_proto = pe_prog_func_proto,
2151 .is_valid_access = pe_prog_is_valid_access,
2152 .convert_ctx_access = pe_prog_convert_ctx_access,
2153 };
2154
2155 const struct bpf_prog_ops perf_event_prog_ops = {
2156 };
2157
2158 static DEFINE_MUTEX(bpf_event_mutex);
2159
2160 #define BPF_TRACE_MAX_PROGS 64
2161
perf_event_attach_bpf_prog(struct perf_event * event,struct bpf_prog * prog,u64 bpf_cookie)2162 int perf_event_attach_bpf_prog(struct perf_event *event,
2163 struct bpf_prog *prog,
2164 u64 bpf_cookie)
2165 {
2166 struct bpf_prog_array *old_array;
2167 struct bpf_prog_array *new_array;
2168 int ret = -EEXIST;
2169
2170 /*
2171 * Kprobe override only works if they are on the function entry,
2172 * and only if they are on the opt-in list.
2173 */
2174 if (prog->kprobe_override &&
2175 (!trace_kprobe_on_func_entry(event->tp_event) ||
2176 !trace_kprobe_error_injectable(event->tp_event)))
2177 return -EINVAL;
2178
2179 mutex_lock(&bpf_event_mutex);
2180
2181 if (event->prog)
2182 goto unlock;
2183
2184 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2185 if (old_array &&
2186 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
2187 ret = -E2BIG;
2188 goto unlock;
2189 }
2190
2191 ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
2192 if (ret < 0)
2193 goto unlock;
2194
2195 /* set the new array to event->tp_event and set event->prog */
2196 event->prog = prog;
2197 event->bpf_cookie = bpf_cookie;
2198 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2199 bpf_prog_array_free_sleepable(old_array);
2200
2201 unlock:
2202 mutex_unlock(&bpf_event_mutex);
2203 return ret;
2204 }
2205
perf_event_detach_bpf_prog(struct perf_event * event)2206 void perf_event_detach_bpf_prog(struct perf_event *event)
2207 {
2208 struct bpf_prog_array *old_array;
2209 struct bpf_prog_array *new_array;
2210 int ret;
2211
2212 mutex_lock(&bpf_event_mutex);
2213
2214 if (!event->prog)
2215 goto unlock;
2216
2217 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2218 ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2219 if (ret == -ENOENT)
2220 goto unlock;
2221 if (ret < 0) {
2222 bpf_prog_array_delete_safe(old_array, event->prog);
2223 } else {
2224 rcu_assign_pointer(event->tp_event->prog_array, new_array);
2225 bpf_prog_array_free_sleepable(old_array);
2226 }
2227
2228 bpf_prog_put(event->prog);
2229 event->prog = NULL;
2230
2231 unlock:
2232 mutex_unlock(&bpf_event_mutex);
2233 }
2234
perf_event_query_prog_array(struct perf_event * event,void __user * info)2235 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
2236 {
2237 struct perf_event_query_bpf __user *uquery = info;
2238 struct perf_event_query_bpf query = {};
2239 struct bpf_prog_array *progs;
2240 u32 *ids, prog_cnt, ids_len;
2241 int ret;
2242
2243 if (!perfmon_capable())
2244 return -EPERM;
2245 if (event->attr.type != PERF_TYPE_TRACEPOINT)
2246 return -EINVAL;
2247 if (copy_from_user(&query, uquery, sizeof(query)))
2248 return -EFAULT;
2249
2250 ids_len = query.ids_len;
2251 if (ids_len > BPF_TRACE_MAX_PROGS)
2252 return -E2BIG;
2253 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2254 if (!ids)
2255 return -ENOMEM;
2256 /*
2257 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2258 * is required when user only wants to check for uquery->prog_cnt.
2259 * There is no need to check for it since the case is handled
2260 * gracefully in bpf_prog_array_copy_info.
2261 */
2262
2263 mutex_lock(&bpf_event_mutex);
2264 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2265 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2266 mutex_unlock(&bpf_event_mutex);
2267
2268 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2269 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2270 ret = -EFAULT;
2271
2272 kfree(ids);
2273 return ret;
2274 }
2275
2276 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2277 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2278
bpf_get_raw_tracepoint(const char * name)2279 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2280 {
2281 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2282
2283 for (; btp < __stop__bpf_raw_tp; btp++) {
2284 if (!strcmp(btp->tp->name, name))
2285 return btp;
2286 }
2287
2288 return bpf_get_raw_tracepoint_module(name);
2289 }
2290
bpf_put_raw_tracepoint(struct bpf_raw_event_map * btp)2291 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2292 {
2293 struct module *mod;
2294
2295 preempt_disable();
2296 mod = __module_address((unsigned long)btp);
2297 module_put(mod);
2298 preempt_enable();
2299 }
2300
2301 static __always_inline
__bpf_trace_run(struct bpf_prog * prog,u64 * args)2302 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2303 {
2304 cant_sleep();
2305 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
2306 bpf_prog_inc_misses_counter(prog);
2307 goto out;
2308 }
2309 rcu_read_lock();
2310 (void) bpf_prog_run(prog, args);
2311 rcu_read_unlock();
2312 out:
2313 this_cpu_dec(*(prog->active));
2314 }
2315
2316 #define UNPACK(...) __VA_ARGS__
2317 #define REPEAT_1(FN, DL, X, ...) FN(X)
2318 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2319 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2320 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2321 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2322 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2323 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2324 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2325 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2326 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2327 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2328 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2329 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2330
2331 #define SARG(X) u64 arg##X
2332 #define COPY(X) args[X] = arg##X
2333
2334 #define __DL_COM (,)
2335 #define __DL_SEM (;)
2336
2337 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2338
2339 #define BPF_TRACE_DEFN_x(x) \
2340 void bpf_trace_run##x(struct bpf_prog *prog, \
2341 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2342 { \
2343 u64 args[x]; \
2344 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2345 __bpf_trace_run(prog, args); \
2346 } \
2347 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2348 BPF_TRACE_DEFN_x(1);
2349 BPF_TRACE_DEFN_x(2);
2350 BPF_TRACE_DEFN_x(3);
2351 BPF_TRACE_DEFN_x(4);
2352 BPF_TRACE_DEFN_x(5);
2353 BPF_TRACE_DEFN_x(6);
2354 BPF_TRACE_DEFN_x(7);
2355 BPF_TRACE_DEFN_x(8);
2356 BPF_TRACE_DEFN_x(9);
2357 BPF_TRACE_DEFN_x(10);
2358 BPF_TRACE_DEFN_x(11);
2359 BPF_TRACE_DEFN_x(12);
2360
__bpf_probe_register(struct bpf_raw_event_map * btp,struct bpf_prog * prog)2361 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2362 {
2363 struct tracepoint *tp = btp->tp;
2364
2365 /*
2366 * check that program doesn't access arguments beyond what's
2367 * available in this tracepoint
2368 */
2369 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2370 return -EINVAL;
2371
2372 if (prog->aux->max_tp_access > btp->writable_size)
2373 return -EINVAL;
2374
2375 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2376 prog);
2377 }
2378
bpf_probe_register(struct bpf_raw_event_map * btp,struct bpf_prog * prog)2379 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2380 {
2381 return __bpf_probe_register(btp, prog);
2382 }
2383
bpf_probe_unregister(struct bpf_raw_event_map * btp,struct bpf_prog * prog)2384 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2385 {
2386 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2387 }
2388
bpf_get_perf_event_info(const struct perf_event * event,u32 * prog_id,u32 * fd_type,const char ** buf,u64 * probe_offset,u64 * probe_addr)2389 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2390 u32 *fd_type, const char **buf,
2391 u64 *probe_offset, u64 *probe_addr)
2392 {
2393 bool is_tracepoint, is_syscall_tp;
2394 struct bpf_prog *prog;
2395 int flags, err = 0;
2396
2397 prog = event->prog;
2398 if (!prog)
2399 return -ENOENT;
2400
2401 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2402 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2403 return -EOPNOTSUPP;
2404
2405 *prog_id = prog->aux->id;
2406 flags = event->tp_event->flags;
2407 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2408 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2409
2410 if (is_tracepoint || is_syscall_tp) {
2411 *buf = is_tracepoint ? event->tp_event->tp->name
2412 : event->tp_event->name;
2413 /* We allow NULL pointer for tracepoint */
2414 if (fd_type)
2415 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2416 if (probe_offset)
2417 *probe_offset = 0x0;
2418 if (probe_addr)
2419 *probe_addr = 0x0;
2420 } else {
2421 /* kprobe/uprobe */
2422 err = -EOPNOTSUPP;
2423 #ifdef CONFIG_KPROBE_EVENTS
2424 if (flags & TRACE_EVENT_FL_KPROBE)
2425 err = bpf_get_kprobe_info(event, fd_type, buf,
2426 probe_offset, probe_addr,
2427 event->attr.type == PERF_TYPE_TRACEPOINT);
2428 #endif
2429 #ifdef CONFIG_UPROBE_EVENTS
2430 if (flags & TRACE_EVENT_FL_UPROBE)
2431 err = bpf_get_uprobe_info(event, fd_type, buf,
2432 probe_offset, probe_addr,
2433 event->attr.type == PERF_TYPE_TRACEPOINT);
2434 #endif
2435 }
2436
2437 return err;
2438 }
2439
send_signal_irq_work_init(void)2440 static int __init send_signal_irq_work_init(void)
2441 {
2442 int cpu;
2443 struct send_signal_irq_work *work;
2444
2445 for_each_possible_cpu(cpu) {
2446 work = per_cpu_ptr(&send_signal_work, cpu);
2447 init_irq_work(&work->irq_work, do_bpf_send_signal);
2448 }
2449 return 0;
2450 }
2451
2452 subsys_initcall(send_signal_irq_work_init);
2453
2454 #ifdef CONFIG_MODULES
bpf_event_notify(struct notifier_block * nb,unsigned long op,void * module)2455 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2456 void *module)
2457 {
2458 struct bpf_trace_module *btm, *tmp;
2459 struct module *mod = module;
2460 int ret = 0;
2461
2462 if (mod->num_bpf_raw_events == 0 ||
2463 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2464 goto out;
2465
2466 mutex_lock(&bpf_module_mutex);
2467
2468 switch (op) {
2469 case MODULE_STATE_COMING:
2470 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2471 if (btm) {
2472 btm->module = module;
2473 list_add(&btm->list, &bpf_trace_modules);
2474 } else {
2475 ret = -ENOMEM;
2476 }
2477 break;
2478 case MODULE_STATE_GOING:
2479 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2480 if (btm->module == module) {
2481 list_del(&btm->list);
2482 kfree(btm);
2483 break;
2484 }
2485 }
2486 break;
2487 }
2488
2489 mutex_unlock(&bpf_module_mutex);
2490
2491 out:
2492 return notifier_from_errno(ret);
2493 }
2494
2495 static struct notifier_block bpf_module_nb = {
2496 .notifier_call = bpf_event_notify,
2497 };
2498
bpf_event_init(void)2499 static int __init bpf_event_init(void)
2500 {
2501 register_module_notifier(&bpf_module_nb);
2502 return 0;
2503 }
2504
2505 fs_initcall(bpf_event_init);
2506 #endif /* CONFIG_MODULES */
2507
2508 #ifdef CONFIG_FPROBE
2509 struct bpf_kprobe_multi_link {
2510 struct bpf_link link;
2511 struct fprobe fp;
2512 unsigned long *addrs;
2513 u64 *cookies;
2514 u32 cnt;
2515 u32 mods_cnt;
2516 struct module **mods;
2517 u32 flags;
2518 };
2519
2520 struct bpf_kprobe_multi_run_ctx {
2521 struct bpf_run_ctx run_ctx;
2522 struct bpf_kprobe_multi_link *link;
2523 unsigned long entry_ip;
2524 };
2525
2526 struct user_syms {
2527 const char **syms;
2528 char *buf;
2529 };
2530
copy_user_syms(struct user_syms * us,unsigned long __user * usyms,u32 cnt)2531 static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
2532 {
2533 unsigned long __user usymbol;
2534 const char **syms = NULL;
2535 char *buf = NULL, *p;
2536 int err = -ENOMEM;
2537 unsigned int i;
2538
2539 syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
2540 if (!syms)
2541 goto error;
2542
2543 buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
2544 if (!buf)
2545 goto error;
2546
2547 for (p = buf, i = 0; i < cnt; i++) {
2548 if (__get_user(usymbol, usyms + i)) {
2549 err = -EFAULT;
2550 goto error;
2551 }
2552 err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2553 if (err == KSYM_NAME_LEN)
2554 err = -E2BIG;
2555 if (err < 0)
2556 goto error;
2557 syms[i] = p;
2558 p += err + 1;
2559 }
2560
2561 us->syms = syms;
2562 us->buf = buf;
2563 return 0;
2564
2565 error:
2566 if (err) {
2567 kvfree(syms);
2568 kvfree(buf);
2569 }
2570 return err;
2571 }
2572
kprobe_multi_put_modules(struct module ** mods,u32 cnt)2573 static void kprobe_multi_put_modules(struct module **mods, u32 cnt)
2574 {
2575 u32 i;
2576
2577 for (i = 0; i < cnt; i++)
2578 module_put(mods[i]);
2579 }
2580
free_user_syms(struct user_syms * us)2581 static void free_user_syms(struct user_syms *us)
2582 {
2583 kvfree(us->syms);
2584 kvfree(us->buf);
2585 }
2586
bpf_kprobe_multi_link_release(struct bpf_link * link)2587 static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2588 {
2589 struct bpf_kprobe_multi_link *kmulti_link;
2590
2591 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2592 unregister_fprobe(&kmulti_link->fp);
2593 kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
2594 }
2595
bpf_kprobe_multi_link_dealloc(struct bpf_link * link)2596 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2597 {
2598 struct bpf_kprobe_multi_link *kmulti_link;
2599
2600 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2601 kvfree(kmulti_link->addrs);
2602 kvfree(kmulti_link->cookies);
2603 kfree(kmulti_link->mods);
2604 kfree(kmulti_link);
2605 }
2606
bpf_kprobe_multi_link_fill_link_info(const struct bpf_link * link,struct bpf_link_info * info)2607 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2608 struct bpf_link_info *info)
2609 {
2610 u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
2611 struct bpf_kprobe_multi_link *kmulti_link;
2612 u32 ucount = info->kprobe_multi.count;
2613 int err = 0, i;
2614
2615 if (!uaddrs ^ !ucount)
2616 return -EINVAL;
2617
2618 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2619 info->kprobe_multi.count = kmulti_link->cnt;
2620 info->kprobe_multi.flags = kmulti_link->flags;
2621
2622 if (!uaddrs)
2623 return 0;
2624 if (ucount < kmulti_link->cnt)
2625 err = -ENOSPC;
2626 else
2627 ucount = kmulti_link->cnt;
2628
2629 if (kallsyms_show_value(current_cred())) {
2630 if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
2631 return -EFAULT;
2632 } else {
2633 for (i = 0; i < ucount; i++) {
2634 if (put_user(0, uaddrs + i))
2635 return -EFAULT;
2636 }
2637 }
2638 return err;
2639 }
2640
2641 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2642 .release = bpf_kprobe_multi_link_release,
2643 .dealloc_deferred = bpf_kprobe_multi_link_dealloc,
2644 .fill_link_info = bpf_kprobe_multi_link_fill_link_info,
2645 };
2646
bpf_kprobe_multi_cookie_swap(void * a,void * b,int size,const void * priv)2647 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2648 {
2649 const struct bpf_kprobe_multi_link *link = priv;
2650 unsigned long *addr_a = a, *addr_b = b;
2651 u64 *cookie_a, *cookie_b;
2652
2653 cookie_a = link->cookies + (addr_a - link->addrs);
2654 cookie_b = link->cookies + (addr_b - link->addrs);
2655
2656 /* swap addr_a/addr_b and cookie_a/cookie_b values */
2657 swap(*addr_a, *addr_b);
2658 swap(*cookie_a, *cookie_b);
2659 }
2660
bpf_kprobe_multi_addrs_cmp(const void * a,const void * b)2661 static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b)
2662 {
2663 const unsigned long *addr_a = a, *addr_b = b;
2664
2665 if (*addr_a == *addr_b)
2666 return 0;
2667 return *addr_a < *addr_b ? -1 : 1;
2668 }
2669
bpf_kprobe_multi_cookie_cmp(const void * a,const void * b,const void * priv)2670 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2671 {
2672 return bpf_kprobe_multi_addrs_cmp(a, b);
2673 }
2674
bpf_kprobe_multi_cookie(struct bpf_run_ctx * ctx)2675 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2676 {
2677 struct bpf_kprobe_multi_run_ctx *run_ctx;
2678 struct bpf_kprobe_multi_link *link;
2679 u64 *cookie, entry_ip;
2680 unsigned long *addr;
2681
2682 if (WARN_ON_ONCE(!ctx))
2683 return 0;
2684 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2685 link = run_ctx->link;
2686 if (!link->cookies)
2687 return 0;
2688 entry_ip = run_ctx->entry_ip;
2689 addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2690 bpf_kprobe_multi_addrs_cmp);
2691 if (!addr)
2692 return 0;
2693 cookie = link->cookies + (addr - link->addrs);
2694 return *cookie;
2695 }
2696
bpf_kprobe_multi_entry_ip(struct bpf_run_ctx * ctx)2697 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2698 {
2699 struct bpf_kprobe_multi_run_ctx *run_ctx;
2700
2701 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2702 return run_ctx->entry_ip;
2703 }
2704
2705 static int
kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link * link,unsigned long entry_ip,struct pt_regs * regs)2706 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2707 unsigned long entry_ip, struct pt_regs *regs)
2708 {
2709 struct bpf_kprobe_multi_run_ctx run_ctx = {
2710 .link = link,
2711 .entry_ip = entry_ip,
2712 };
2713 struct bpf_run_ctx *old_run_ctx;
2714 int err;
2715
2716 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2717 err = 0;
2718 goto out;
2719 }
2720
2721 migrate_disable();
2722 rcu_read_lock();
2723 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2724 err = bpf_prog_run(link->link.prog, regs);
2725 bpf_reset_run_ctx(old_run_ctx);
2726 rcu_read_unlock();
2727 migrate_enable();
2728
2729 out:
2730 __this_cpu_dec(bpf_prog_active);
2731 return err;
2732 }
2733
2734 static int
kprobe_multi_link_handler(struct fprobe * fp,unsigned long fentry_ip,unsigned long ret_ip,struct pt_regs * regs,void * data)2735 kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
2736 unsigned long ret_ip, struct pt_regs *regs,
2737 void *data)
2738 {
2739 struct bpf_kprobe_multi_link *link;
2740
2741 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2742 kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2743 return 0;
2744 }
2745
2746 static void
kprobe_multi_link_exit_handler(struct fprobe * fp,unsigned long fentry_ip,unsigned long ret_ip,struct pt_regs * regs,void * data)2747 kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
2748 unsigned long ret_ip, struct pt_regs *regs,
2749 void *data)
2750 {
2751 struct bpf_kprobe_multi_link *link;
2752
2753 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2754 kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2755 }
2756
symbols_cmp_r(const void * a,const void * b,const void * priv)2757 static int symbols_cmp_r(const void *a, const void *b, const void *priv)
2758 {
2759 const char **str_a = (const char **) a;
2760 const char **str_b = (const char **) b;
2761
2762 return strcmp(*str_a, *str_b);
2763 }
2764
2765 struct multi_symbols_sort {
2766 const char **funcs;
2767 u64 *cookies;
2768 };
2769
symbols_swap_r(void * a,void * b,int size,const void * priv)2770 static void symbols_swap_r(void *a, void *b, int size, const void *priv)
2771 {
2772 const struct multi_symbols_sort *data = priv;
2773 const char **name_a = a, **name_b = b;
2774
2775 swap(*name_a, *name_b);
2776
2777 /* If defined, swap also related cookies. */
2778 if (data->cookies) {
2779 u64 *cookie_a, *cookie_b;
2780
2781 cookie_a = data->cookies + (name_a - data->funcs);
2782 cookie_b = data->cookies + (name_b - data->funcs);
2783 swap(*cookie_a, *cookie_b);
2784 }
2785 }
2786
2787 struct modules_array {
2788 struct module **mods;
2789 int mods_cnt;
2790 int mods_cap;
2791 };
2792
add_module(struct modules_array * arr,struct module * mod)2793 static int add_module(struct modules_array *arr, struct module *mod)
2794 {
2795 struct module **mods;
2796
2797 if (arr->mods_cnt == arr->mods_cap) {
2798 arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
2799 mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
2800 if (!mods)
2801 return -ENOMEM;
2802 arr->mods = mods;
2803 }
2804
2805 arr->mods[arr->mods_cnt] = mod;
2806 arr->mods_cnt++;
2807 return 0;
2808 }
2809
has_module(struct modules_array * arr,struct module * mod)2810 static bool has_module(struct modules_array *arr, struct module *mod)
2811 {
2812 int i;
2813
2814 for (i = arr->mods_cnt - 1; i >= 0; i--) {
2815 if (arr->mods[i] == mod)
2816 return true;
2817 }
2818 return false;
2819 }
2820
get_modules_for_addrs(struct module *** mods,unsigned long * addrs,u32 addrs_cnt)2821 static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
2822 {
2823 struct modules_array arr = {};
2824 u32 i, err = 0;
2825
2826 for (i = 0; i < addrs_cnt; i++) {
2827 struct module *mod;
2828
2829 preempt_disable();
2830 mod = __module_address(addrs[i]);
2831 /* Either no module or we it's already stored */
2832 if (!mod || has_module(&arr, mod)) {
2833 preempt_enable();
2834 continue;
2835 }
2836 if (!try_module_get(mod))
2837 err = -EINVAL;
2838 preempt_enable();
2839 if (err)
2840 break;
2841 err = add_module(&arr, mod);
2842 if (err) {
2843 module_put(mod);
2844 break;
2845 }
2846 }
2847
2848 /* We return either err < 0 in case of error, ... */
2849 if (err) {
2850 kprobe_multi_put_modules(arr.mods, arr.mods_cnt);
2851 kfree(arr.mods);
2852 return err;
2853 }
2854
2855 /* or number of modules found if everything is ok. */
2856 *mods = arr.mods;
2857 return arr.mods_cnt;
2858 }
2859
addrs_check_error_injection_list(unsigned long * addrs,u32 cnt)2860 static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
2861 {
2862 u32 i;
2863
2864 for (i = 0; i < cnt; i++) {
2865 if (!within_error_injection_list(addrs[i]))
2866 return -EINVAL;
2867 }
2868 return 0;
2869 }
2870
bpf_kprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)2871 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2872 {
2873 struct bpf_kprobe_multi_link *link = NULL;
2874 struct bpf_link_primer link_primer;
2875 void __user *ucookies;
2876 unsigned long *addrs;
2877 u32 flags, cnt, size;
2878 void __user *uaddrs;
2879 u64 *cookies = NULL;
2880 void __user *usyms;
2881 int err;
2882
2883 /* no support for 32bit archs yet */
2884 if (sizeof(u64) != sizeof(void *))
2885 return -EOPNOTSUPP;
2886
2887 if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
2888 return -EINVAL;
2889
2890 flags = attr->link_create.kprobe_multi.flags;
2891 if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2892 return -EINVAL;
2893
2894 uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2895 usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2896 if (!!uaddrs == !!usyms)
2897 return -EINVAL;
2898
2899 cnt = attr->link_create.kprobe_multi.cnt;
2900 if (!cnt)
2901 return -EINVAL;
2902 if (cnt > MAX_KPROBE_MULTI_CNT)
2903 return -E2BIG;
2904
2905 size = cnt * sizeof(*addrs);
2906 addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2907 if (!addrs)
2908 return -ENOMEM;
2909
2910 ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2911 if (ucookies) {
2912 cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2913 if (!cookies) {
2914 err = -ENOMEM;
2915 goto error;
2916 }
2917 if (copy_from_user(cookies, ucookies, size)) {
2918 err = -EFAULT;
2919 goto error;
2920 }
2921 }
2922
2923 if (uaddrs) {
2924 if (copy_from_user(addrs, uaddrs, size)) {
2925 err = -EFAULT;
2926 goto error;
2927 }
2928 } else {
2929 struct multi_symbols_sort data = {
2930 .cookies = cookies,
2931 };
2932 struct user_syms us;
2933
2934 err = copy_user_syms(&us, usyms, cnt);
2935 if (err)
2936 goto error;
2937
2938 if (cookies)
2939 data.funcs = us.syms;
2940
2941 sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
2942 symbols_swap_r, &data);
2943
2944 err = ftrace_lookup_symbols(us.syms, cnt, addrs);
2945 free_user_syms(&us);
2946 if (err)
2947 goto error;
2948 }
2949
2950 if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
2951 err = -EINVAL;
2952 goto error;
2953 }
2954
2955 link = kzalloc(sizeof(*link), GFP_KERNEL);
2956 if (!link) {
2957 err = -ENOMEM;
2958 goto error;
2959 }
2960
2961 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
2962 &bpf_kprobe_multi_link_lops, prog);
2963
2964 err = bpf_link_prime(&link->link, &link_primer);
2965 if (err)
2966 goto error;
2967
2968 if (flags & BPF_F_KPROBE_MULTI_RETURN)
2969 link->fp.exit_handler = kprobe_multi_link_exit_handler;
2970 else
2971 link->fp.entry_handler = kprobe_multi_link_handler;
2972
2973 link->addrs = addrs;
2974 link->cookies = cookies;
2975 link->cnt = cnt;
2976 link->flags = flags;
2977
2978 if (cookies) {
2979 /*
2980 * Sorting addresses will trigger sorting cookies as well
2981 * (check bpf_kprobe_multi_cookie_swap). This way we can
2982 * find cookie based on the address in bpf_get_attach_cookie
2983 * helper.
2984 */
2985 sort_r(addrs, cnt, sizeof(*addrs),
2986 bpf_kprobe_multi_cookie_cmp,
2987 bpf_kprobe_multi_cookie_swap,
2988 link);
2989 }
2990
2991 err = get_modules_for_addrs(&link->mods, addrs, cnt);
2992 if (err < 0) {
2993 bpf_link_cleanup(&link_primer);
2994 return err;
2995 }
2996 link->mods_cnt = err;
2997
2998 err = register_fprobe_ips(&link->fp, addrs, cnt);
2999 if (err) {
3000 kprobe_multi_put_modules(link->mods, link->mods_cnt);
3001 bpf_link_cleanup(&link_primer);
3002 return err;
3003 }
3004
3005 return bpf_link_settle(&link_primer);
3006
3007 error:
3008 kfree(link);
3009 kvfree(addrs);
3010 kvfree(cookies);
3011 return err;
3012 }
3013 #else /* !CONFIG_FPROBE */
bpf_kprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)3014 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3015 {
3016 return -EOPNOTSUPP;
3017 }
bpf_kprobe_multi_cookie(struct bpf_run_ctx * ctx)3018 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
3019 {
3020 return 0;
3021 }
bpf_kprobe_multi_entry_ip(struct bpf_run_ctx * ctx)3022 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3023 {
3024 return 0;
3025 }
3026 #endif
3027
3028 #ifdef CONFIG_UPROBES
3029 struct bpf_uprobe_multi_link;
3030
3031 struct bpf_uprobe {
3032 struct bpf_uprobe_multi_link *link;
3033 loff_t offset;
3034 unsigned long ref_ctr_offset;
3035 u64 cookie;
3036 struct uprobe_consumer consumer;
3037 };
3038
3039 struct bpf_uprobe_multi_link {
3040 struct path path;
3041 struct bpf_link link;
3042 u32 cnt;
3043 struct bpf_uprobe *uprobes;
3044 struct task_struct *task;
3045 };
3046
3047 struct bpf_uprobe_multi_run_ctx {
3048 struct bpf_run_ctx run_ctx;
3049 unsigned long entry_ip;
3050 struct bpf_uprobe *uprobe;
3051 };
3052
bpf_uprobe_unregister(struct path * path,struct bpf_uprobe * uprobes,u32 cnt)3053 static void bpf_uprobe_unregister(struct path *path, struct bpf_uprobe *uprobes,
3054 u32 cnt)
3055 {
3056 u32 i;
3057
3058 for (i = 0; i < cnt; i++) {
3059 uprobe_unregister(d_real_inode(path->dentry), uprobes[i].offset,
3060 &uprobes[i].consumer);
3061 }
3062 }
3063
bpf_uprobe_multi_link_release(struct bpf_link * link)3064 static void bpf_uprobe_multi_link_release(struct bpf_link *link)
3065 {
3066 struct bpf_uprobe_multi_link *umulti_link;
3067
3068 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3069 bpf_uprobe_unregister(&umulti_link->path, umulti_link->uprobes, umulti_link->cnt);
3070 if (umulti_link->task)
3071 put_task_struct(umulti_link->task);
3072 path_put(&umulti_link->path);
3073 }
3074
bpf_uprobe_multi_link_dealloc(struct bpf_link * link)3075 static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
3076 {
3077 struct bpf_uprobe_multi_link *umulti_link;
3078
3079 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3080 kvfree(umulti_link->uprobes);
3081 kfree(umulti_link);
3082 }
3083
3084 static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
3085 .release = bpf_uprobe_multi_link_release,
3086 .dealloc_deferred = bpf_uprobe_multi_link_dealloc,
3087 };
3088
uprobe_prog_run(struct bpf_uprobe * uprobe,unsigned long entry_ip,struct pt_regs * regs)3089 static int uprobe_prog_run(struct bpf_uprobe *uprobe,
3090 unsigned long entry_ip,
3091 struct pt_regs *regs)
3092 {
3093 struct bpf_uprobe_multi_link *link = uprobe->link;
3094 struct bpf_uprobe_multi_run_ctx run_ctx = {
3095 .entry_ip = entry_ip,
3096 .uprobe = uprobe,
3097 };
3098 struct bpf_prog *prog = link->link.prog;
3099 bool sleepable = prog->aux->sleepable;
3100 struct bpf_run_ctx *old_run_ctx;
3101 int err = 0;
3102
3103 if (link->task && current->mm != link->task->mm)
3104 return 0;
3105
3106 if (sleepable)
3107 rcu_read_lock_trace();
3108 else
3109 rcu_read_lock();
3110
3111 migrate_disable();
3112
3113 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
3114 err = bpf_prog_run(link->link.prog, regs);
3115 bpf_reset_run_ctx(old_run_ctx);
3116
3117 migrate_enable();
3118
3119 if (sleepable)
3120 rcu_read_unlock_trace();
3121 else
3122 rcu_read_unlock();
3123 return err;
3124 }
3125
3126 static bool
uprobe_multi_link_filter(struct uprobe_consumer * con,enum uprobe_filter_ctx ctx,struct mm_struct * mm)3127 uprobe_multi_link_filter(struct uprobe_consumer *con, enum uprobe_filter_ctx ctx,
3128 struct mm_struct *mm)
3129 {
3130 struct bpf_uprobe *uprobe;
3131
3132 uprobe = container_of(con, struct bpf_uprobe, consumer);
3133 return uprobe->link->task->mm == mm;
3134 }
3135
3136 static int
uprobe_multi_link_handler(struct uprobe_consumer * con,struct pt_regs * regs)3137 uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs)
3138 {
3139 struct bpf_uprobe *uprobe;
3140
3141 uprobe = container_of(con, struct bpf_uprobe, consumer);
3142 return uprobe_prog_run(uprobe, instruction_pointer(regs), regs);
3143 }
3144
3145 static int
uprobe_multi_link_ret_handler(struct uprobe_consumer * con,unsigned long func,struct pt_regs * regs)3146 uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs)
3147 {
3148 struct bpf_uprobe *uprobe;
3149
3150 uprobe = container_of(con, struct bpf_uprobe, consumer);
3151 return uprobe_prog_run(uprobe, func, regs);
3152 }
3153
bpf_uprobe_multi_entry_ip(struct bpf_run_ctx * ctx)3154 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3155 {
3156 struct bpf_uprobe_multi_run_ctx *run_ctx;
3157
3158 run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
3159 return run_ctx->entry_ip;
3160 }
3161
bpf_uprobe_multi_cookie(struct bpf_run_ctx * ctx)3162 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3163 {
3164 struct bpf_uprobe_multi_run_ctx *run_ctx;
3165
3166 run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx);
3167 return run_ctx->uprobe->cookie;
3168 }
3169
bpf_uprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)3170 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3171 {
3172 struct bpf_uprobe_multi_link *link = NULL;
3173 unsigned long __user *uref_ctr_offsets;
3174 struct bpf_link_primer link_primer;
3175 struct bpf_uprobe *uprobes = NULL;
3176 struct task_struct *task = NULL;
3177 unsigned long __user *uoffsets;
3178 u64 __user *ucookies;
3179 void __user *upath;
3180 u32 flags, cnt, i;
3181 struct path path;
3182 char *name;
3183 pid_t pid;
3184 int err;
3185
3186 /* no support for 32bit archs yet */
3187 if (sizeof(u64) != sizeof(void *))
3188 return -EOPNOTSUPP;
3189
3190 if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI)
3191 return -EINVAL;
3192
3193 flags = attr->link_create.uprobe_multi.flags;
3194 if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
3195 return -EINVAL;
3196
3197 /*
3198 * path, offsets and cnt are mandatory,
3199 * ref_ctr_offsets and cookies are optional
3200 */
3201 upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
3202 uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets);
3203 cnt = attr->link_create.uprobe_multi.cnt;
3204 pid = attr->link_create.uprobe_multi.pid;
3205
3206 if (!upath || !uoffsets || !cnt || pid < 0)
3207 return -EINVAL;
3208 if (cnt > MAX_UPROBE_MULTI_CNT)
3209 return -E2BIG;
3210
3211 uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
3212 ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
3213
3214 name = strndup_user(upath, PATH_MAX);
3215 if (IS_ERR(name)) {
3216 err = PTR_ERR(name);
3217 return err;
3218 }
3219
3220 err = kern_path(name, LOOKUP_FOLLOW, &path);
3221 kfree(name);
3222 if (err)
3223 return err;
3224
3225 if (!d_is_reg(path.dentry)) {
3226 err = -EBADF;
3227 goto error_path_put;
3228 }
3229
3230 if (pid) {
3231 rcu_read_lock();
3232 task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
3233 rcu_read_unlock();
3234 if (!task) {
3235 err = -ESRCH;
3236 goto error_path_put;
3237 }
3238 }
3239
3240 err = -ENOMEM;
3241
3242 link = kzalloc(sizeof(*link), GFP_KERNEL);
3243 uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL);
3244
3245 if (!uprobes || !link)
3246 goto error_free;
3247
3248 for (i = 0; i < cnt; i++) {
3249 if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
3250 err = -EFAULT;
3251 goto error_free;
3252 }
3253 if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
3254 err = -EFAULT;
3255 goto error_free;
3256 }
3257 if (__get_user(uprobes[i].offset, uoffsets + i)) {
3258 err = -EFAULT;
3259 goto error_free;
3260 }
3261
3262 uprobes[i].link = link;
3263
3264 if (flags & BPF_F_UPROBE_MULTI_RETURN)
3265 uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler;
3266 else
3267 uprobes[i].consumer.handler = uprobe_multi_link_handler;
3268
3269 if (pid)
3270 uprobes[i].consumer.filter = uprobe_multi_link_filter;
3271 }
3272
3273 link->cnt = cnt;
3274 link->uprobes = uprobes;
3275 link->path = path;
3276 link->task = task;
3277
3278 bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
3279 &bpf_uprobe_multi_link_lops, prog);
3280
3281 for (i = 0; i < cnt; i++) {
3282 err = uprobe_register_refctr(d_real_inode(link->path.dentry),
3283 uprobes[i].offset,
3284 uprobes[i].ref_ctr_offset,
3285 &uprobes[i].consumer);
3286 if (err) {
3287 bpf_uprobe_unregister(&path, uprobes, i);
3288 goto error_free;
3289 }
3290 }
3291
3292 err = bpf_link_prime(&link->link, &link_primer);
3293 if (err)
3294 goto error_free;
3295
3296 return bpf_link_settle(&link_primer);
3297
3298 error_free:
3299 kvfree(uprobes);
3300 kfree(link);
3301 if (task)
3302 put_task_struct(task);
3303 error_path_put:
3304 path_put(&path);
3305 return err;
3306 }
3307 #else /* !CONFIG_UPROBES */
bpf_uprobe_multi_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)3308 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3309 {
3310 return -EOPNOTSUPP;
3311 }
bpf_uprobe_multi_cookie(struct bpf_run_ctx * ctx)3312 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3313 {
3314 return 0;
3315 }
bpf_uprobe_multi_entry_ip(struct bpf_run_ctx * ctx)3316 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3317 {
3318 return 0;
3319 }
3320 #endif /* CONFIG_UPROBES */
3321