1
2
3
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_perf_event.h>
10#include <linux/btf.h>
11#include <linux/filter.h>
12#include <linux/uaccess.h>
13#include <linux/ctype.h>
14#include <linux/kprobes.h>
15#include <linux/spinlock.h>
16#include <linux/syscalls.h>
17#include <linux/error-injection.h>
18#include <linux/btf_ids.h>
19#include <linux/bpf_lsm.h>
20#include <linux/fprobe.h>
21#include <linux/bsearch.h>
22#include <linux/sort.h>
23
24#include <net/bpf_sk_storage.h>
25
26#include <uapi/linux/bpf.h>
27#include <uapi/linux/btf.h>
28
29#include <asm/tlb.h>
30
31#include "trace_probe.h"
32#include "trace.h"
33
34#define CREATE_TRACE_POINTS
35#include "bpf_trace.h"
36
37#define bpf_event_rcu_dereference(p) \
38 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
39
40#ifdef CONFIG_MODULES
41struct bpf_trace_module {
42 struct module *module;
43 struct list_head list;
44};
45
46static LIST_HEAD(bpf_trace_modules);
47static DEFINE_MUTEX(bpf_module_mutex);
48
49static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
50{
51 struct bpf_raw_event_map *btp, *ret = NULL;
52 struct bpf_trace_module *btm;
53 unsigned int i;
54
55 mutex_lock(&bpf_module_mutex);
56 list_for_each_entry(btm, &bpf_trace_modules, list) {
57 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
58 btp = &btm->module->bpf_raw_events[i];
59 if (!strcmp(btp->tp->name, name)) {
60 if (try_module_get(btm->module))
61 ret = btp;
62 goto out;
63 }
64 }
65 }
66out:
67 mutex_unlock(&bpf_module_mutex);
68 return ret;
69}
70#else
71static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
72{
73 return NULL;
74}
75#endif
76
77u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
78u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
79
80static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
81 u64 flags, const struct btf **btf,
82 s32 *btf_id);
83static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
84static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
101{
102 unsigned int ret;
103
104 cant_sleep();
105
106 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
107
108
109
110
111
112
113 ret = 0;
114 goto out;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 rcu_read_lock();
133 ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
134 ctx, bpf_prog_run);
135 rcu_read_unlock();
136
137 out:
138 __this_cpu_dec(bpf_prog_active);
139
140 return ret;
141}
142
143#ifdef CONFIG_BPF_KPROBE_OVERRIDE
144BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
145{
146 regs_set_return_value(regs, rc);
147 override_function_with_return(regs);
148 return 0;
149}
150
151static const struct bpf_func_proto bpf_override_return_proto = {
152 .func = bpf_override_return,
153 .gpl_only = true,
154 .ret_type = RET_INTEGER,
155 .arg1_type = ARG_PTR_TO_CTX,
156 .arg2_type = ARG_ANYTHING,
157};
158#endif
159
160static __always_inline int
161bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
162{
163 int ret;
164
165 ret = copy_from_user_nofault(dst, unsafe_ptr, size);
166 if (unlikely(ret < 0))
167 memset(dst, 0, size);
168 return ret;
169}
170
171BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
172 const void __user *, unsafe_ptr)
173{
174 return bpf_probe_read_user_common(dst, size, unsafe_ptr);
175}
176
177const struct bpf_func_proto bpf_probe_read_user_proto = {
178 .func = bpf_probe_read_user,
179 .gpl_only = true,
180 .ret_type = RET_INTEGER,
181 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
182 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
183 .arg3_type = ARG_ANYTHING,
184};
185
186static __always_inline int
187bpf_probe_read_user_str_common(void *dst, u32 size,
188 const void __user *unsafe_ptr)
189{
190 int ret;
191
192
193
194
195
196
197
198
199
200
201
202 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
203 if (unlikely(ret < 0))
204 memset(dst, 0, size);
205 return ret;
206}
207
208BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
209 const void __user *, unsafe_ptr)
210{
211 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
212}
213
214const struct bpf_func_proto bpf_probe_read_user_str_proto = {
215 .func = bpf_probe_read_user_str,
216 .gpl_only = true,
217 .ret_type = RET_INTEGER,
218 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
219 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
220 .arg3_type = ARG_ANYTHING,
221};
222
223static __always_inline int
224bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
225{
226 int ret;
227
228 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
229 if (unlikely(ret < 0))
230 memset(dst, 0, size);
231 return ret;
232}
233
234BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
235 const void *, unsafe_ptr)
236{
237 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
238}
239
240const struct bpf_func_proto bpf_probe_read_kernel_proto = {
241 .func = bpf_probe_read_kernel,
242 .gpl_only = true,
243 .ret_type = RET_INTEGER,
244 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
245 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
246 .arg3_type = ARG_ANYTHING,
247};
248
249static __always_inline int
250bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
251{
252 int ret;
253
254
255
256
257
258
259
260
261
262
263 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
264 if (unlikely(ret < 0))
265 memset(dst, 0, size);
266 return ret;
267}
268
269BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
270 const void *, unsafe_ptr)
271{
272 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
273}
274
275const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
276 .func = bpf_probe_read_kernel_str,
277 .gpl_only = true,
278 .ret_type = RET_INTEGER,
279 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
280 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
281 .arg3_type = ARG_ANYTHING,
282};
283
284#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
285BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
286 const void *, unsafe_ptr)
287{
288 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
289 return bpf_probe_read_user_common(dst, size,
290 (__force void __user *)unsafe_ptr);
291 }
292 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
293}
294
295static const struct bpf_func_proto bpf_probe_read_compat_proto = {
296 .func = bpf_probe_read_compat,
297 .gpl_only = true,
298 .ret_type = RET_INTEGER,
299 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
300 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
301 .arg3_type = ARG_ANYTHING,
302};
303
304BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
305 const void *, unsafe_ptr)
306{
307 if ((unsigned long)unsafe_ptr < TASK_SIZE) {
308 return bpf_probe_read_user_str_common(dst, size,
309 (__force void __user *)unsafe_ptr);
310 }
311 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
312}
313
314static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
315 .func = bpf_probe_read_compat_str,
316 .gpl_only = true,
317 .ret_type = RET_INTEGER,
318 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
319 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
320 .arg3_type = ARG_ANYTHING,
321};
322#endif
323
324BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
325 u32, size)
326{
327
328
329
330
331
332
333
334
335
336
337
338
339
340 if (unlikely(in_interrupt() ||
341 current->flags & (PF_KTHREAD | PF_EXITING)))
342 return -EPERM;
343 if (unlikely(!nmi_uaccess_okay()))
344 return -EPERM;
345
346 return copy_to_user_nofault(unsafe_ptr, src, size);
347}
348
349static const struct bpf_func_proto bpf_probe_write_user_proto = {
350 .func = bpf_probe_write_user,
351 .gpl_only = true,
352 .ret_type = RET_INTEGER,
353 .arg1_type = ARG_ANYTHING,
354 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
355 .arg3_type = ARG_CONST_SIZE,
356};
357
358static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
359{
360 if (!capable(CAP_SYS_ADMIN))
361 return NULL;
362
363 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
364 current->comm, task_pid_nr(current));
365
366 return &bpf_probe_write_user_proto;
367}
368
369static DEFINE_RAW_SPINLOCK(trace_printk_lock);
370
371#define MAX_TRACE_PRINTK_VARARGS 3
372#define BPF_TRACE_PRINTK_SIZE 1024
373
374BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
375 u64, arg2, u64, arg3)
376{
377 u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
378 u32 *bin_args;
379 static char buf[BPF_TRACE_PRINTK_SIZE];
380 unsigned long flags;
381 int ret;
382
383 ret = bpf_bprintf_prepare(fmt, fmt_size, args, &bin_args,
384 MAX_TRACE_PRINTK_VARARGS);
385 if (ret < 0)
386 return ret;
387
388 raw_spin_lock_irqsave(&trace_printk_lock, flags);
389 ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
390
391 trace_bpf_trace_printk(buf);
392 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
393
394 bpf_bprintf_cleanup();
395
396 return ret;
397}
398
399static const struct bpf_func_proto bpf_trace_printk_proto = {
400 .func = bpf_trace_printk,
401 .gpl_only = true,
402 .ret_type = RET_INTEGER,
403 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
404 .arg2_type = ARG_CONST_SIZE,
405};
406
407static void __set_printk_clr_event(void)
408{
409
410
411
412
413
414
415
416
417 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
418 pr_warn_ratelimited("could not enable bpf_trace_printk events");
419}
420
421const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
422{
423 __set_printk_clr_event();
424 return &bpf_trace_printk_proto;
425}
426
427BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, data,
428 u32, data_len)
429{
430 static char buf[BPF_TRACE_PRINTK_SIZE];
431 unsigned long flags;
432 int ret, num_args;
433 u32 *bin_args;
434
435 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
436 (data_len && !data))
437 return -EINVAL;
438 num_args = data_len / 8;
439
440 ret = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
441 if (ret < 0)
442 return ret;
443
444 raw_spin_lock_irqsave(&trace_printk_lock, flags);
445 ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
446
447 trace_bpf_trace_printk(buf);
448 raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
449
450 bpf_bprintf_cleanup();
451
452 return ret;
453}
454
455static const struct bpf_func_proto bpf_trace_vprintk_proto = {
456 .func = bpf_trace_vprintk,
457 .gpl_only = true,
458 .ret_type = RET_INTEGER,
459 .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
460 .arg2_type = ARG_CONST_SIZE,
461 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
462 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
463};
464
465const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
466{
467 __set_printk_clr_event();
468 return &bpf_trace_vprintk_proto;
469}
470
471BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
472 const void *, data, u32, data_len)
473{
474 int err, num_args;
475 u32 *bin_args;
476
477 if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
478 (data_len && !data))
479 return -EINVAL;
480 num_args = data_len / 8;
481
482 err = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
483 if (err < 0)
484 return err;
485
486 seq_bprintf(m, fmt, bin_args);
487
488 bpf_bprintf_cleanup();
489
490 return seq_has_overflowed(m) ? -EOVERFLOW : 0;
491}
492
493BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
494
495static const struct bpf_func_proto bpf_seq_printf_proto = {
496 .func = bpf_seq_printf,
497 .gpl_only = true,
498 .ret_type = RET_INTEGER,
499 .arg1_type = ARG_PTR_TO_BTF_ID,
500 .arg1_btf_id = &btf_seq_file_ids[0],
501 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
502 .arg3_type = ARG_CONST_SIZE,
503 .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
504 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
505};
506
507BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
508{
509 return seq_write(m, data, len) ? -EOVERFLOW : 0;
510}
511
512static const struct bpf_func_proto bpf_seq_write_proto = {
513 .func = bpf_seq_write,
514 .gpl_only = true,
515 .ret_type = RET_INTEGER,
516 .arg1_type = ARG_PTR_TO_BTF_ID,
517 .arg1_btf_id = &btf_seq_file_ids[0],
518 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
519 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
520};
521
522BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
523 u32, btf_ptr_size, u64, flags)
524{
525 const struct btf *btf;
526 s32 btf_id;
527 int ret;
528
529 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
530 if (ret)
531 return ret;
532
533 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
534}
535
536static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
537 .func = bpf_seq_printf_btf,
538 .gpl_only = true,
539 .ret_type = RET_INTEGER,
540 .arg1_type = ARG_PTR_TO_BTF_ID,
541 .arg1_btf_id = &btf_seq_file_ids[0],
542 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
543 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
544 .arg4_type = ARG_ANYTHING,
545};
546
547static __always_inline int
548get_map_perf_counter(struct bpf_map *map, u64 flags,
549 u64 *value, u64 *enabled, u64 *running)
550{
551 struct bpf_array *array = container_of(map, struct bpf_array, map);
552 unsigned int cpu = smp_processor_id();
553 u64 index = flags & BPF_F_INDEX_MASK;
554 struct bpf_event_entry *ee;
555
556 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
557 return -EINVAL;
558 if (index == BPF_F_CURRENT_CPU)
559 index = cpu;
560 if (unlikely(index >= array->map.max_entries))
561 return -E2BIG;
562
563 ee = READ_ONCE(array->ptrs[index]);
564 if (!ee)
565 return -ENOENT;
566
567 return perf_event_read_local(ee->event, value, enabled, running);
568}
569
570BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
571{
572 u64 value = 0;
573 int err;
574
575 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
576
577
578
579
580 if (err)
581 return err;
582 return value;
583}
584
585static const struct bpf_func_proto bpf_perf_event_read_proto = {
586 .func = bpf_perf_event_read,
587 .gpl_only = true,
588 .ret_type = RET_INTEGER,
589 .arg1_type = ARG_CONST_MAP_PTR,
590 .arg2_type = ARG_ANYTHING,
591};
592
593BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
594 struct bpf_perf_event_value *, buf, u32, size)
595{
596 int err = -EINVAL;
597
598 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
599 goto clear;
600 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
601 &buf->running);
602 if (unlikely(err))
603 goto clear;
604 return 0;
605clear:
606 memset(buf, 0, size);
607 return err;
608}
609
610static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
611 .func = bpf_perf_event_read_value,
612 .gpl_only = true,
613 .ret_type = RET_INTEGER,
614 .arg1_type = ARG_CONST_MAP_PTR,
615 .arg2_type = ARG_ANYTHING,
616 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
617 .arg4_type = ARG_CONST_SIZE,
618};
619
620static __always_inline u64
621__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
622 u64 flags, struct perf_sample_data *sd)
623{
624 struct bpf_array *array = container_of(map, struct bpf_array, map);
625 unsigned int cpu = smp_processor_id();
626 u64 index = flags & BPF_F_INDEX_MASK;
627 struct bpf_event_entry *ee;
628 struct perf_event *event;
629
630 if (index == BPF_F_CURRENT_CPU)
631 index = cpu;
632 if (unlikely(index >= array->map.max_entries))
633 return -E2BIG;
634
635 ee = READ_ONCE(array->ptrs[index]);
636 if (!ee)
637 return -ENOENT;
638
639 event = ee->event;
640 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
641 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
642 return -EINVAL;
643
644 if (unlikely(event->oncpu != cpu))
645 return -EOPNOTSUPP;
646
647 return perf_event_output(event, sd, regs);
648}
649
650
651
652
653
654struct bpf_trace_sample_data {
655 struct perf_sample_data sds[3];
656};
657
658static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
659static DEFINE_PER_CPU(int, bpf_trace_nest_level);
660BPF_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 = this_cpu_ptr(&bpf_trace_sds);
664 int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
665 struct perf_raw_record raw = {
666 .frag = {
667 .size = size,
668 .data = data,
669 },
670 };
671 struct perf_sample_data *sd;
672 int err;
673
674 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
675 err = -EBUSY;
676 goto out;
677 }
678
679 sd = &sds->sds[nest_level - 1];
680
681 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
682 err = -EINVAL;
683 goto out;
684 }
685
686 perf_sample_data_init(sd, 0, 0);
687 sd->raw = &raw;
688
689 err = __bpf_perf_event_output(regs, map, flags, sd);
690
691out:
692 this_cpu_dec(bpf_trace_nest_level);
693 return err;
694}
695
696static const struct bpf_func_proto bpf_perf_event_output_proto = {
697 .func = bpf_perf_event_output,
698 .gpl_only = true,
699 .ret_type = RET_INTEGER,
700 .arg1_type = ARG_PTR_TO_CTX,
701 .arg2_type = ARG_CONST_MAP_PTR,
702 .arg3_type = ARG_ANYTHING,
703 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
704 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
705};
706
707static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
708struct bpf_nested_pt_regs {
709 struct pt_regs regs[3];
710};
711static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
712static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
713
714u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
715 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
716{
717 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
718 struct perf_raw_frag frag = {
719 .copy = ctx_copy,
720 .size = ctx_size,
721 .data = ctx,
722 };
723 struct perf_raw_record raw = {
724 .frag = {
725 {
726 .next = ctx_size ? &frag : NULL,
727 },
728 .size = meta_size,
729 .data = meta,
730 },
731 };
732 struct perf_sample_data *sd;
733 struct pt_regs *regs;
734 u64 ret;
735
736 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
737 ret = -EBUSY;
738 goto out;
739 }
740 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
741 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
742
743 perf_fetch_caller_regs(regs);
744 perf_sample_data_init(sd, 0, 0);
745 sd->raw = &raw;
746
747 ret = __bpf_perf_event_output(regs, map, flags, sd);
748out:
749 this_cpu_dec(bpf_event_output_nest_level);
750 return ret;
751}
752
753BPF_CALL_0(bpf_get_current_task)
754{
755 return (long) current;
756}
757
758const struct bpf_func_proto bpf_get_current_task_proto = {
759 .func = bpf_get_current_task,
760 .gpl_only = true,
761 .ret_type = RET_INTEGER,
762};
763
764BPF_CALL_0(bpf_get_current_task_btf)
765{
766 return (unsigned long) current;
767}
768
769const struct bpf_func_proto bpf_get_current_task_btf_proto = {
770 .func = bpf_get_current_task_btf,
771 .gpl_only = true,
772 .ret_type = RET_PTR_TO_BTF_ID,
773 .ret_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
774};
775
776BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
777{
778 return (unsigned long) task_pt_regs(task);
779}
780
781BTF_ID_LIST(bpf_task_pt_regs_ids)
782BTF_ID(struct, pt_regs)
783
784const struct bpf_func_proto bpf_task_pt_regs_proto = {
785 .func = bpf_task_pt_regs,
786 .gpl_only = true,
787 .arg1_type = ARG_PTR_TO_BTF_ID,
788 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
789 .ret_type = RET_PTR_TO_BTF_ID,
790 .ret_btf_id = &bpf_task_pt_regs_ids[0],
791};
792
793BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
794{
795 struct bpf_array *array = container_of(map, struct bpf_array, map);
796 struct cgroup *cgrp;
797
798 if (unlikely(idx >= array->map.max_entries))
799 return -E2BIG;
800
801 cgrp = READ_ONCE(array->ptrs[idx]);
802 if (unlikely(!cgrp))
803 return -EAGAIN;
804
805 return task_under_cgroup_hierarchy(current, cgrp);
806}
807
808static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
809 .func = bpf_current_task_under_cgroup,
810 .gpl_only = false,
811 .ret_type = RET_INTEGER,
812 .arg1_type = ARG_CONST_MAP_PTR,
813 .arg2_type = ARG_ANYTHING,
814};
815
816struct send_signal_irq_work {
817 struct irq_work irq_work;
818 struct task_struct *task;
819 u32 sig;
820 enum pid_type type;
821};
822
823static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
824
825static void do_bpf_send_signal(struct irq_work *entry)
826{
827 struct send_signal_irq_work *work;
828
829 work = container_of(entry, struct send_signal_irq_work, irq_work);
830 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
831}
832
833static int bpf_send_signal_common(u32 sig, enum pid_type type)
834{
835 struct send_signal_irq_work *work = NULL;
836
837
838
839
840
841
842 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
843 return -EPERM;
844 if (unlikely(!nmi_uaccess_okay()))
845 return -EPERM;
846
847 if (irqs_disabled()) {
848
849
850
851 if (unlikely(!valid_signal(sig)))
852 return -EINVAL;
853
854 work = this_cpu_ptr(&send_signal_work);
855 if (irq_work_is_busy(&work->irq_work))
856 return -EBUSY;
857
858
859
860
861
862 work->task = current;
863 work->sig = sig;
864 work->type = type;
865 irq_work_queue(&work->irq_work);
866 return 0;
867 }
868
869 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
870}
871
872BPF_CALL_1(bpf_send_signal, u32, sig)
873{
874 return bpf_send_signal_common(sig, PIDTYPE_TGID);
875}
876
877static const struct bpf_func_proto bpf_send_signal_proto = {
878 .func = bpf_send_signal,
879 .gpl_only = false,
880 .ret_type = RET_INTEGER,
881 .arg1_type = ARG_ANYTHING,
882};
883
884BPF_CALL_1(bpf_send_signal_thread, u32, sig)
885{
886 return bpf_send_signal_common(sig, PIDTYPE_PID);
887}
888
889static const struct bpf_func_proto bpf_send_signal_thread_proto = {
890 .func = bpf_send_signal_thread,
891 .gpl_only = false,
892 .ret_type = RET_INTEGER,
893 .arg1_type = ARG_ANYTHING,
894};
895
896BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
897{
898 long len;
899 char *p;
900
901 if (!sz)
902 return 0;
903
904 p = d_path(path, buf, sz);
905 if (IS_ERR(p)) {
906 len = PTR_ERR(p);
907 } else {
908 len = buf + sz - p;
909 memmove(buf, p, len);
910 }
911
912 return len;
913}
914
915BTF_SET_START(btf_allowlist_d_path)
916#ifdef CONFIG_SECURITY
917BTF_ID(func, security_file_permission)
918BTF_ID(func, security_inode_getattr)
919BTF_ID(func, security_file_open)
920#endif
921#ifdef CONFIG_SECURITY_PATH
922BTF_ID(func, security_path_truncate)
923#endif
924BTF_ID(func, vfs_truncate)
925BTF_ID(func, vfs_fallocate)
926BTF_ID(func, dentry_open)
927BTF_ID(func, vfs_getattr)
928BTF_ID(func, filp_close)
929BTF_SET_END(btf_allowlist_d_path)
930
931static bool bpf_d_path_allowed(const struct bpf_prog *prog)
932{
933 if (prog->type == BPF_PROG_TYPE_TRACING &&
934 prog->expected_attach_type == BPF_TRACE_ITER)
935 return true;
936
937 if (prog->type == BPF_PROG_TYPE_LSM)
938 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
939
940 return btf_id_set_contains(&btf_allowlist_d_path,
941 prog->aux->attach_btf_id);
942}
943
944BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
945
946static const struct bpf_func_proto bpf_d_path_proto = {
947 .func = bpf_d_path,
948 .gpl_only = false,
949 .ret_type = RET_INTEGER,
950 .arg1_type = ARG_PTR_TO_BTF_ID,
951 .arg1_btf_id = &bpf_d_path_btf_ids[0],
952 .arg2_type = ARG_PTR_TO_MEM,
953 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
954 .allowed = bpf_d_path_allowed,
955};
956
957#define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \
958 BTF_F_PTR_RAW | BTF_F_ZERO)
959
960static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
961 u64 flags, const struct btf **btf,
962 s32 *btf_id)
963{
964 const struct btf_type *t;
965
966 if (unlikely(flags & ~(BTF_F_ALL)))
967 return -EINVAL;
968
969 if (btf_ptr_size != sizeof(struct btf_ptr))
970 return -EINVAL;
971
972 *btf = bpf_get_btf_vmlinux();
973
974 if (IS_ERR_OR_NULL(*btf))
975 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
976
977 if (ptr->type_id > 0)
978 *btf_id = ptr->type_id;
979 else
980 return -EINVAL;
981
982 if (*btf_id > 0)
983 t = btf_type_by_id(*btf, *btf_id);
984 if (*btf_id <= 0 || !t)
985 return -ENOENT;
986
987 return 0;
988}
989
990BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
991 u32, btf_ptr_size, u64, flags)
992{
993 const struct btf *btf;
994 s32 btf_id;
995 int ret;
996
997 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
998 if (ret)
999 return ret;
1000
1001 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1002 flags);
1003}
1004
1005const struct bpf_func_proto bpf_snprintf_btf_proto = {
1006 .func = bpf_snprintf_btf,
1007 .gpl_only = false,
1008 .ret_type = RET_INTEGER,
1009 .arg1_type = ARG_PTR_TO_MEM,
1010 .arg2_type = ARG_CONST_SIZE,
1011 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1012 .arg4_type = ARG_CONST_SIZE,
1013 .arg5_type = ARG_ANYTHING,
1014};
1015
1016BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1017{
1018
1019 return ((u64 *)ctx)[-2];
1020}
1021
1022static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1023 .func = bpf_get_func_ip_tracing,
1024 .gpl_only = true,
1025 .ret_type = RET_INTEGER,
1026 .arg1_type = ARG_PTR_TO_CTX,
1027};
1028
1029#ifdef CONFIG_X86_KERNEL_IBT
1030static unsigned long get_entry_ip(unsigned long fentry_ip)
1031{
1032 u32 instr;
1033
1034
1035 if (get_kernel_nofault(instr, (u32 *) fentry_ip - 1))
1036 return fentry_ip;
1037 if (is_endbr(instr))
1038 fentry_ip -= ENDBR_INSN_SIZE;
1039 return fentry_ip;
1040}
1041#else
1042#define get_entry_ip(fentry_ip) fentry_ip
1043#endif
1044
1045BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1046{
1047 struct kprobe *kp = kprobe_running();
1048
1049 return kp ? (uintptr_t)kp->addr : 0;
1050}
1051
1052static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1053 .func = bpf_get_func_ip_kprobe,
1054 .gpl_only = true,
1055 .ret_type = RET_INTEGER,
1056 .arg1_type = ARG_PTR_TO_CTX,
1057};
1058
1059BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1060{
1061 return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
1062}
1063
1064static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1065 .func = bpf_get_func_ip_kprobe_multi,
1066 .gpl_only = false,
1067 .ret_type = RET_INTEGER,
1068 .arg1_type = ARG_PTR_TO_CTX,
1069};
1070
1071BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1072{
1073 return bpf_kprobe_multi_cookie(current->bpf_ctx);
1074}
1075
1076static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1077 .func = bpf_get_attach_cookie_kprobe_multi,
1078 .gpl_only = false,
1079 .ret_type = RET_INTEGER,
1080 .arg1_type = ARG_PTR_TO_CTX,
1081};
1082
1083BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1084{
1085 struct bpf_trace_run_ctx *run_ctx;
1086
1087 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1088 return run_ctx->bpf_cookie;
1089}
1090
1091static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1092 .func = bpf_get_attach_cookie_trace,
1093 .gpl_only = false,
1094 .ret_type = RET_INTEGER,
1095 .arg1_type = ARG_PTR_TO_CTX,
1096};
1097
1098BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1099{
1100 return ctx->event->bpf_cookie;
1101}
1102
1103static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1104 .func = bpf_get_attach_cookie_pe,
1105 .gpl_only = false,
1106 .ret_type = RET_INTEGER,
1107 .arg1_type = ARG_PTR_TO_CTX,
1108};
1109
1110BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
1111{
1112 struct bpf_trace_run_ctx *run_ctx;
1113
1114 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1115 return run_ctx->bpf_cookie;
1116}
1117
1118static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
1119 .func = bpf_get_attach_cookie_tracing,
1120 .gpl_only = false,
1121 .ret_type = RET_INTEGER,
1122 .arg1_type = ARG_PTR_TO_CTX,
1123};
1124
1125BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1126{
1127#ifndef CONFIG_X86
1128 return -ENOENT;
1129#else
1130 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1131 u32 entry_cnt = size / br_entry_size;
1132
1133 entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1134
1135 if (unlikely(flags))
1136 return -EINVAL;
1137
1138 if (!entry_cnt)
1139 return -ENOENT;
1140
1141 return entry_cnt * br_entry_size;
1142#endif
1143}
1144
1145static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1146 .func = bpf_get_branch_snapshot,
1147 .gpl_only = true,
1148 .ret_type = RET_INTEGER,
1149 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
1150 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1151};
1152
1153BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1154{
1155
1156 u64 nr_args = ((u64 *)ctx)[-1];
1157
1158 if ((u64) n >= nr_args)
1159 return -EINVAL;
1160 *value = ((u64 *)ctx)[n];
1161 return 0;
1162}
1163
1164static const struct bpf_func_proto bpf_get_func_arg_proto = {
1165 .func = get_func_arg,
1166 .ret_type = RET_INTEGER,
1167 .arg1_type = ARG_PTR_TO_CTX,
1168 .arg2_type = ARG_ANYTHING,
1169 .arg3_type = ARG_PTR_TO_LONG,
1170};
1171
1172BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1173{
1174
1175 u64 nr_args = ((u64 *)ctx)[-1];
1176
1177 *value = ((u64 *)ctx)[nr_args];
1178 return 0;
1179}
1180
1181static const struct bpf_func_proto bpf_get_func_ret_proto = {
1182 .func = get_func_ret,
1183 .ret_type = RET_INTEGER,
1184 .arg1_type = ARG_PTR_TO_CTX,
1185 .arg2_type = ARG_PTR_TO_LONG,
1186};
1187
1188BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1189{
1190
1191 return ((u64 *)ctx)[-1];
1192}
1193
1194static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1195 .func = get_func_arg_cnt,
1196 .ret_type = RET_INTEGER,
1197 .arg1_type = ARG_PTR_TO_CTX,
1198};
1199
1200static const struct bpf_func_proto *
1201bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1202{
1203 switch (func_id) {
1204 case BPF_FUNC_map_lookup_elem:
1205 return &bpf_map_lookup_elem_proto;
1206 case BPF_FUNC_map_update_elem:
1207 return &bpf_map_update_elem_proto;
1208 case BPF_FUNC_map_delete_elem:
1209 return &bpf_map_delete_elem_proto;
1210 case BPF_FUNC_map_push_elem:
1211 return &bpf_map_push_elem_proto;
1212 case BPF_FUNC_map_pop_elem:
1213 return &bpf_map_pop_elem_proto;
1214 case BPF_FUNC_map_peek_elem:
1215 return &bpf_map_peek_elem_proto;
1216 case BPF_FUNC_map_lookup_percpu_elem:
1217 return &bpf_map_lookup_percpu_elem_proto;
1218 case BPF_FUNC_ktime_get_ns:
1219 return &bpf_ktime_get_ns_proto;
1220 case BPF_FUNC_ktime_get_boot_ns:
1221 return &bpf_ktime_get_boot_ns_proto;
1222 case BPF_FUNC_tail_call:
1223 return &bpf_tail_call_proto;
1224 case BPF_FUNC_get_current_pid_tgid:
1225 return &bpf_get_current_pid_tgid_proto;
1226 case BPF_FUNC_get_current_task:
1227 return &bpf_get_current_task_proto;
1228 case BPF_FUNC_get_current_task_btf:
1229 return &bpf_get_current_task_btf_proto;
1230 case BPF_FUNC_task_pt_regs:
1231 return &bpf_task_pt_regs_proto;
1232 case BPF_FUNC_get_current_uid_gid:
1233 return &bpf_get_current_uid_gid_proto;
1234 case BPF_FUNC_get_current_comm:
1235 return &bpf_get_current_comm_proto;
1236 case BPF_FUNC_trace_printk:
1237 return bpf_get_trace_printk_proto();
1238 case BPF_FUNC_get_smp_processor_id:
1239 return &bpf_get_smp_processor_id_proto;
1240 case BPF_FUNC_get_numa_node_id:
1241 return &bpf_get_numa_node_id_proto;
1242 case BPF_FUNC_perf_event_read:
1243 return &bpf_perf_event_read_proto;
1244 case BPF_FUNC_current_task_under_cgroup:
1245 return &bpf_current_task_under_cgroup_proto;
1246 case BPF_FUNC_get_prandom_u32:
1247 return &bpf_get_prandom_u32_proto;
1248 case BPF_FUNC_probe_write_user:
1249 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1250 NULL : bpf_get_probe_write_proto();
1251 case BPF_FUNC_probe_read_user:
1252 return &bpf_probe_read_user_proto;
1253 case BPF_FUNC_probe_read_kernel:
1254 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1255 NULL : &bpf_probe_read_kernel_proto;
1256 case BPF_FUNC_probe_read_user_str:
1257 return &bpf_probe_read_user_str_proto;
1258 case BPF_FUNC_probe_read_kernel_str:
1259 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1260 NULL : &bpf_probe_read_kernel_str_proto;
1261#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1262 case BPF_FUNC_probe_read:
1263 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1264 NULL : &bpf_probe_read_compat_proto;
1265 case BPF_FUNC_probe_read_str:
1266 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1267 NULL : &bpf_probe_read_compat_str_proto;
1268#endif
1269#ifdef CONFIG_CGROUPS
1270 case BPF_FUNC_get_current_cgroup_id:
1271 return &bpf_get_current_cgroup_id_proto;
1272 case BPF_FUNC_get_current_ancestor_cgroup_id:
1273 return &bpf_get_current_ancestor_cgroup_id_proto;
1274#endif
1275 case BPF_FUNC_send_signal:
1276 return &bpf_send_signal_proto;
1277 case BPF_FUNC_send_signal_thread:
1278 return &bpf_send_signal_thread_proto;
1279 case BPF_FUNC_perf_event_read_value:
1280 return &bpf_perf_event_read_value_proto;
1281 case BPF_FUNC_get_ns_current_pid_tgid:
1282 return &bpf_get_ns_current_pid_tgid_proto;
1283 case BPF_FUNC_ringbuf_output:
1284 return &bpf_ringbuf_output_proto;
1285 case BPF_FUNC_ringbuf_reserve:
1286 return &bpf_ringbuf_reserve_proto;
1287 case BPF_FUNC_ringbuf_submit:
1288 return &bpf_ringbuf_submit_proto;
1289 case BPF_FUNC_ringbuf_discard:
1290 return &bpf_ringbuf_discard_proto;
1291 case BPF_FUNC_ringbuf_query:
1292 return &bpf_ringbuf_query_proto;
1293 case BPF_FUNC_jiffies64:
1294 return &bpf_jiffies64_proto;
1295 case BPF_FUNC_get_task_stack:
1296 return &bpf_get_task_stack_proto;
1297 case BPF_FUNC_copy_from_user:
1298 return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1299 case BPF_FUNC_copy_from_user_task:
1300 return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
1301 case BPF_FUNC_snprintf_btf:
1302 return &bpf_snprintf_btf_proto;
1303 case BPF_FUNC_per_cpu_ptr:
1304 return &bpf_per_cpu_ptr_proto;
1305 case BPF_FUNC_this_cpu_ptr:
1306 return &bpf_this_cpu_ptr_proto;
1307 case BPF_FUNC_task_storage_get:
1308 return &bpf_task_storage_get_proto;
1309 case BPF_FUNC_task_storage_delete:
1310 return &bpf_task_storage_delete_proto;
1311 case BPF_FUNC_for_each_map_elem:
1312 return &bpf_for_each_map_elem_proto;
1313 case BPF_FUNC_snprintf:
1314 return &bpf_snprintf_proto;
1315 case BPF_FUNC_get_func_ip:
1316 return &bpf_get_func_ip_proto_tracing;
1317 case BPF_FUNC_get_branch_snapshot:
1318 return &bpf_get_branch_snapshot_proto;
1319 case BPF_FUNC_find_vma:
1320 return &bpf_find_vma_proto;
1321 case BPF_FUNC_trace_vprintk:
1322 return bpf_get_trace_vprintk_proto();
1323 default:
1324 return bpf_base_func_proto(func_id);
1325 }
1326}
1327
1328static const struct bpf_func_proto *
1329kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1330{
1331 switch (func_id) {
1332 case BPF_FUNC_perf_event_output:
1333 return &bpf_perf_event_output_proto;
1334 case BPF_FUNC_get_stackid:
1335 return &bpf_get_stackid_proto;
1336 case BPF_FUNC_get_stack:
1337 return &bpf_get_stack_proto;
1338#ifdef CONFIG_BPF_KPROBE_OVERRIDE
1339 case BPF_FUNC_override_return:
1340 return &bpf_override_return_proto;
1341#endif
1342 case BPF_FUNC_get_func_ip:
1343 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1344 &bpf_get_func_ip_proto_kprobe_multi :
1345 &bpf_get_func_ip_proto_kprobe;
1346 case BPF_FUNC_get_attach_cookie:
1347 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1348 &bpf_get_attach_cookie_proto_kmulti :
1349 &bpf_get_attach_cookie_proto_trace;
1350 default:
1351 return bpf_tracing_func_proto(func_id, prog);
1352 }
1353}
1354
1355
1356static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1357 const struct bpf_prog *prog,
1358 struct bpf_insn_access_aux *info)
1359{
1360 if (off < 0 || off >= sizeof(struct pt_regs))
1361 return false;
1362 if (type != BPF_READ)
1363 return false;
1364 if (off % size != 0)
1365 return false;
1366
1367
1368
1369
1370 if (off + size > sizeof(struct pt_regs))
1371 return false;
1372
1373 return true;
1374}
1375
1376const struct bpf_verifier_ops kprobe_verifier_ops = {
1377 .get_func_proto = kprobe_prog_func_proto,
1378 .is_valid_access = kprobe_prog_is_valid_access,
1379};
1380
1381const struct bpf_prog_ops kprobe_prog_ops = {
1382};
1383
1384BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1385 u64, flags, void *, data, u64, size)
1386{
1387 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1388
1389
1390
1391
1392
1393
1394 return ____bpf_perf_event_output(regs, map, flags, data, size);
1395}
1396
1397static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1398 .func = bpf_perf_event_output_tp,
1399 .gpl_only = true,
1400 .ret_type = RET_INTEGER,
1401 .arg1_type = ARG_PTR_TO_CTX,
1402 .arg2_type = ARG_CONST_MAP_PTR,
1403 .arg3_type = ARG_ANYTHING,
1404 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1405 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1406};
1407
1408BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1409 u64, flags)
1410{
1411 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1412
1413
1414
1415
1416
1417
1418 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1419 flags, 0, 0);
1420}
1421
1422static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1423 .func = bpf_get_stackid_tp,
1424 .gpl_only = true,
1425 .ret_type = RET_INTEGER,
1426 .arg1_type = ARG_PTR_TO_CTX,
1427 .arg2_type = ARG_CONST_MAP_PTR,
1428 .arg3_type = ARG_ANYTHING,
1429};
1430
1431BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1432 u64, flags)
1433{
1434 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1435
1436 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1437 (unsigned long) size, flags, 0);
1438}
1439
1440static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1441 .func = bpf_get_stack_tp,
1442 .gpl_only = true,
1443 .ret_type = RET_INTEGER,
1444 .arg1_type = ARG_PTR_TO_CTX,
1445 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1446 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1447 .arg4_type = ARG_ANYTHING,
1448};
1449
1450static const struct bpf_func_proto *
1451tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1452{
1453 switch (func_id) {
1454 case BPF_FUNC_perf_event_output:
1455 return &bpf_perf_event_output_proto_tp;
1456 case BPF_FUNC_get_stackid:
1457 return &bpf_get_stackid_proto_tp;
1458 case BPF_FUNC_get_stack:
1459 return &bpf_get_stack_proto_tp;
1460 case BPF_FUNC_get_attach_cookie:
1461 return &bpf_get_attach_cookie_proto_trace;
1462 default:
1463 return bpf_tracing_func_proto(func_id, prog);
1464 }
1465}
1466
1467static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1468 const struct bpf_prog *prog,
1469 struct bpf_insn_access_aux *info)
1470{
1471 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1472 return false;
1473 if (type != BPF_READ)
1474 return false;
1475 if (off % size != 0)
1476 return false;
1477
1478 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1479 return true;
1480}
1481
1482const struct bpf_verifier_ops tracepoint_verifier_ops = {
1483 .get_func_proto = tp_prog_func_proto,
1484 .is_valid_access = tp_prog_is_valid_access,
1485};
1486
1487const struct bpf_prog_ops tracepoint_prog_ops = {
1488};
1489
1490BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1491 struct bpf_perf_event_value *, buf, u32, size)
1492{
1493 int err = -EINVAL;
1494
1495 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1496 goto clear;
1497 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1498 &buf->running);
1499 if (unlikely(err))
1500 goto clear;
1501 return 0;
1502clear:
1503 memset(buf, 0, size);
1504 return err;
1505}
1506
1507static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1508 .func = bpf_perf_prog_read_value,
1509 .gpl_only = true,
1510 .ret_type = RET_INTEGER,
1511 .arg1_type = ARG_PTR_TO_CTX,
1512 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
1513 .arg3_type = ARG_CONST_SIZE,
1514};
1515
1516BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1517 void *, buf, u32, size, u64, flags)
1518{
1519 static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1520 struct perf_branch_stack *br_stack = ctx->data->br_stack;
1521 u32 to_copy;
1522
1523 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1524 return -EINVAL;
1525
1526 if (unlikely(!br_stack))
1527 return -ENOENT;
1528
1529 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1530 return br_stack->nr * br_entry_size;
1531
1532 if (!buf || (size % br_entry_size != 0))
1533 return -EINVAL;
1534
1535 to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1536 memcpy(buf, br_stack->entries, to_copy);
1537
1538 return to_copy;
1539}
1540
1541static const struct bpf_func_proto bpf_read_branch_records_proto = {
1542 .func = bpf_read_branch_records,
1543 .gpl_only = true,
1544 .ret_type = RET_INTEGER,
1545 .arg1_type = ARG_PTR_TO_CTX,
1546 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
1547 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1548 .arg4_type = ARG_ANYTHING,
1549};
1550
1551static const struct bpf_func_proto *
1552pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1553{
1554 switch (func_id) {
1555 case BPF_FUNC_perf_event_output:
1556 return &bpf_perf_event_output_proto_tp;
1557 case BPF_FUNC_get_stackid:
1558 return &bpf_get_stackid_proto_pe;
1559 case BPF_FUNC_get_stack:
1560 return &bpf_get_stack_proto_pe;
1561 case BPF_FUNC_perf_prog_read_value:
1562 return &bpf_perf_prog_read_value_proto;
1563 case BPF_FUNC_read_branch_records:
1564 return &bpf_read_branch_records_proto;
1565 case BPF_FUNC_get_attach_cookie:
1566 return &bpf_get_attach_cookie_proto_pe;
1567 default:
1568 return bpf_tracing_func_proto(func_id, prog);
1569 }
1570}
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580struct bpf_raw_tp_regs {
1581 struct pt_regs regs[3];
1582};
1583static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1584static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1585static struct pt_regs *get_bpf_raw_tp_regs(void)
1586{
1587 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1588 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1589
1590 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1591 this_cpu_dec(bpf_raw_tp_nest_level);
1592 return ERR_PTR(-EBUSY);
1593 }
1594
1595 return &tp_regs->regs[nest_level - 1];
1596}
1597
1598static void put_bpf_raw_tp_regs(void)
1599{
1600 this_cpu_dec(bpf_raw_tp_nest_level);
1601}
1602
1603BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1604 struct bpf_map *, map, u64, flags, void *, data, u64, size)
1605{
1606 struct pt_regs *regs = get_bpf_raw_tp_regs();
1607 int ret;
1608
1609 if (IS_ERR(regs))
1610 return PTR_ERR(regs);
1611
1612 perf_fetch_caller_regs(regs);
1613 ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1614
1615 put_bpf_raw_tp_regs();
1616 return ret;
1617}
1618
1619static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1620 .func = bpf_perf_event_output_raw_tp,
1621 .gpl_only = true,
1622 .ret_type = RET_INTEGER,
1623 .arg1_type = ARG_PTR_TO_CTX,
1624 .arg2_type = ARG_CONST_MAP_PTR,
1625 .arg3_type = ARG_ANYTHING,
1626 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1627 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
1628};
1629
1630extern const struct bpf_func_proto bpf_skb_output_proto;
1631extern const struct bpf_func_proto bpf_xdp_output_proto;
1632extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1633
1634BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1635 struct bpf_map *, map, u64, flags)
1636{
1637 struct pt_regs *regs = get_bpf_raw_tp_regs();
1638 int ret;
1639
1640 if (IS_ERR(regs))
1641 return PTR_ERR(regs);
1642
1643 perf_fetch_caller_regs(regs);
1644
1645 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1646 flags, 0, 0);
1647 put_bpf_raw_tp_regs();
1648 return ret;
1649}
1650
1651static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1652 .func = bpf_get_stackid_raw_tp,
1653 .gpl_only = true,
1654 .ret_type = RET_INTEGER,
1655 .arg1_type = ARG_PTR_TO_CTX,
1656 .arg2_type = ARG_CONST_MAP_PTR,
1657 .arg3_type = ARG_ANYTHING,
1658};
1659
1660BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1661 void *, buf, u32, size, u64, flags)
1662{
1663 struct pt_regs *regs = get_bpf_raw_tp_regs();
1664 int ret;
1665
1666 if (IS_ERR(regs))
1667 return PTR_ERR(regs);
1668
1669 perf_fetch_caller_regs(regs);
1670 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1671 (unsigned long) size, flags, 0);
1672 put_bpf_raw_tp_regs();
1673 return ret;
1674}
1675
1676static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1677 .func = bpf_get_stack_raw_tp,
1678 .gpl_only = true,
1679 .ret_type = RET_INTEGER,
1680 .arg1_type = ARG_PTR_TO_CTX,
1681 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1682 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
1683 .arg4_type = ARG_ANYTHING,
1684};
1685
1686static const struct bpf_func_proto *
1687raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1688{
1689 switch (func_id) {
1690 case BPF_FUNC_perf_event_output:
1691 return &bpf_perf_event_output_proto_raw_tp;
1692 case BPF_FUNC_get_stackid:
1693 return &bpf_get_stackid_proto_raw_tp;
1694 case BPF_FUNC_get_stack:
1695 return &bpf_get_stack_proto_raw_tp;
1696 default:
1697 return bpf_tracing_func_proto(func_id, prog);
1698 }
1699}
1700
1701const struct bpf_func_proto *
1702tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1703{
1704 const struct bpf_func_proto *fn;
1705
1706 switch (func_id) {
1707#ifdef CONFIG_NET
1708 case BPF_FUNC_skb_output:
1709 return &bpf_skb_output_proto;
1710 case BPF_FUNC_xdp_output:
1711 return &bpf_xdp_output_proto;
1712 case BPF_FUNC_skc_to_tcp6_sock:
1713 return &bpf_skc_to_tcp6_sock_proto;
1714 case BPF_FUNC_skc_to_tcp_sock:
1715 return &bpf_skc_to_tcp_sock_proto;
1716 case BPF_FUNC_skc_to_tcp_timewait_sock:
1717 return &bpf_skc_to_tcp_timewait_sock_proto;
1718 case BPF_FUNC_skc_to_tcp_request_sock:
1719 return &bpf_skc_to_tcp_request_sock_proto;
1720 case BPF_FUNC_skc_to_udp6_sock:
1721 return &bpf_skc_to_udp6_sock_proto;
1722 case BPF_FUNC_skc_to_unix_sock:
1723 return &bpf_skc_to_unix_sock_proto;
1724 case BPF_FUNC_skc_to_mptcp_sock:
1725 return &bpf_skc_to_mptcp_sock_proto;
1726 case BPF_FUNC_sk_storage_get:
1727 return &bpf_sk_storage_get_tracing_proto;
1728 case BPF_FUNC_sk_storage_delete:
1729 return &bpf_sk_storage_delete_tracing_proto;
1730 case BPF_FUNC_sock_from_file:
1731 return &bpf_sock_from_file_proto;
1732 case BPF_FUNC_get_socket_cookie:
1733 return &bpf_get_socket_ptr_cookie_proto;
1734 case BPF_FUNC_xdp_get_buff_len:
1735 return &bpf_xdp_get_buff_len_trace_proto;
1736#endif
1737 case BPF_FUNC_seq_printf:
1738 return prog->expected_attach_type == BPF_TRACE_ITER ?
1739 &bpf_seq_printf_proto :
1740 NULL;
1741 case BPF_FUNC_seq_write:
1742 return prog->expected_attach_type == BPF_TRACE_ITER ?
1743 &bpf_seq_write_proto :
1744 NULL;
1745 case BPF_FUNC_seq_printf_btf:
1746 return prog->expected_attach_type == BPF_TRACE_ITER ?
1747 &bpf_seq_printf_btf_proto :
1748 NULL;
1749 case BPF_FUNC_d_path:
1750 return &bpf_d_path_proto;
1751 case BPF_FUNC_get_func_arg:
1752 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1753 case BPF_FUNC_get_func_ret:
1754 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1755 case BPF_FUNC_get_func_arg_cnt:
1756 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
1757 case BPF_FUNC_get_attach_cookie:
1758 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
1759 default:
1760 fn = raw_tp_prog_func_proto(func_id, prog);
1761 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
1762 fn = bpf_iter_get_func_proto(func_id, prog);
1763 return fn;
1764 }
1765}
1766
1767static bool raw_tp_prog_is_valid_access(int off, int size,
1768 enum bpf_access_type type,
1769 const struct bpf_prog *prog,
1770 struct bpf_insn_access_aux *info)
1771{
1772 return bpf_tracing_ctx_access(off, size, type);
1773}
1774
1775static bool tracing_prog_is_valid_access(int off, int size,
1776 enum bpf_access_type type,
1777 const struct bpf_prog *prog,
1778 struct bpf_insn_access_aux *info)
1779{
1780 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
1781}
1782
1783int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1784 const union bpf_attr *kattr,
1785 union bpf_attr __user *uattr)
1786{
1787 return -ENOTSUPP;
1788}
1789
1790const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1791 .get_func_proto = raw_tp_prog_func_proto,
1792 .is_valid_access = raw_tp_prog_is_valid_access,
1793};
1794
1795const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1796#ifdef CONFIG_NET
1797 .test_run = bpf_prog_test_run_raw_tp,
1798#endif
1799};
1800
1801const struct bpf_verifier_ops tracing_verifier_ops = {
1802 .get_func_proto = tracing_prog_func_proto,
1803 .is_valid_access = tracing_prog_is_valid_access,
1804};
1805
1806const struct bpf_prog_ops tracing_prog_ops = {
1807 .test_run = bpf_prog_test_run_tracing,
1808};
1809
1810static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1811 enum bpf_access_type type,
1812 const struct bpf_prog *prog,
1813 struct bpf_insn_access_aux *info)
1814{
1815 if (off == 0) {
1816 if (size != sizeof(u64) || type != BPF_READ)
1817 return false;
1818 info->reg_type = PTR_TO_TP_BUFFER;
1819 }
1820 return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1821}
1822
1823const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1824 .get_func_proto = raw_tp_prog_func_proto,
1825 .is_valid_access = raw_tp_writable_prog_is_valid_access,
1826};
1827
1828const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1829};
1830
1831static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1832 const struct bpf_prog *prog,
1833 struct bpf_insn_access_aux *info)
1834{
1835 const int size_u64 = sizeof(u64);
1836
1837 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1838 return false;
1839 if (type != BPF_READ)
1840 return false;
1841 if (off % size != 0) {
1842 if (sizeof(unsigned long) != 4)
1843 return false;
1844 if (size != 8)
1845 return false;
1846 if (off % size != 4)
1847 return false;
1848 }
1849
1850 switch (off) {
1851 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
1852 bpf_ctx_record_field_size(info, size_u64);
1853 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1854 return false;
1855 break;
1856 case bpf_ctx_range(struct bpf_perf_event_data, addr):
1857 bpf_ctx_record_field_size(info, size_u64);
1858 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1859 return false;
1860 break;
1861 default:
1862 if (size != sizeof(long))
1863 return false;
1864 }
1865
1866 return true;
1867}
1868
1869static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1870 const struct bpf_insn *si,
1871 struct bpf_insn *insn_buf,
1872 struct bpf_prog *prog, u32 *target_size)
1873{
1874 struct bpf_insn *insn = insn_buf;
1875
1876 switch (si->off) {
1877 case offsetof(struct bpf_perf_event_data, sample_period):
1878 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1879 data), si->dst_reg, si->src_reg,
1880 offsetof(struct bpf_perf_event_data_kern, data));
1881 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1882 bpf_target_off(struct perf_sample_data, period, 8,
1883 target_size));
1884 break;
1885 case offsetof(struct bpf_perf_event_data, addr):
1886 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1887 data), si->dst_reg, si->src_reg,
1888 offsetof(struct bpf_perf_event_data_kern, data));
1889 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1890 bpf_target_off(struct perf_sample_data, addr, 8,
1891 target_size));
1892 break;
1893 default:
1894 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1895 regs), si->dst_reg, si->src_reg,
1896 offsetof(struct bpf_perf_event_data_kern, regs));
1897 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1898 si->off);
1899 break;
1900 }
1901
1902 return insn - insn_buf;
1903}
1904
1905const struct bpf_verifier_ops perf_event_verifier_ops = {
1906 .get_func_proto = pe_prog_func_proto,
1907 .is_valid_access = pe_prog_is_valid_access,
1908 .convert_ctx_access = pe_prog_convert_ctx_access,
1909};
1910
1911const struct bpf_prog_ops perf_event_prog_ops = {
1912};
1913
1914static DEFINE_MUTEX(bpf_event_mutex);
1915
1916#define BPF_TRACE_MAX_PROGS 64
1917
1918int perf_event_attach_bpf_prog(struct perf_event *event,
1919 struct bpf_prog *prog,
1920 u64 bpf_cookie)
1921{
1922 struct bpf_prog_array *old_array;
1923 struct bpf_prog_array *new_array;
1924 int ret = -EEXIST;
1925
1926
1927
1928
1929
1930 if (prog->kprobe_override &&
1931 (!trace_kprobe_on_func_entry(event->tp_event) ||
1932 !trace_kprobe_error_injectable(event->tp_event)))
1933 return -EINVAL;
1934
1935 mutex_lock(&bpf_event_mutex);
1936
1937 if (event->prog)
1938 goto unlock;
1939
1940 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1941 if (old_array &&
1942 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1943 ret = -E2BIG;
1944 goto unlock;
1945 }
1946
1947 ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
1948 if (ret < 0)
1949 goto unlock;
1950
1951
1952 event->prog = prog;
1953 event->bpf_cookie = bpf_cookie;
1954 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1955 bpf_prog_array_free_sleepable(old_array);
1956
1957unlock:
1958 mutex_unlock(&bpf_event_mutex);
1959 return ret;
1960}
1961
1962void perf_event_detach_bpf_prog(struct perf_event *event)
1963{
1964 struct bpf_prog_array *old_array;
1965 struct bpf_prog_array *new_array;
1966 int ret;
1967
1968 mutex_lock(&bpf_event_mutex);
1969
1970 if (!event->prog)
1971 goto unlock;
1972
1973 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1974 ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
1975 if (ret == -ENOENT)
1976 goto unlock;
1977 if (ret < 0) {
1978 bpf_prog_array_delete_safe(old_array, event->prog);
1979 } else {
1980 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1981 bpf_prog_array_free_sleepable(old_array);
1982 }
1983
1984 bpf_prog_put(event->prog);
1985 event->prog = NULL;
1986
1987unlock:
1988 mutex_unlock(&bpf_event_mutex);
1989}
1990
1991int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1992{
1993 struct perf_event_query_bpf __user *uquery = info;
1994 struct perf_event_query_bpf query = {};
1995 struct bpf_prog_array *progs;
1996 u32 *ids, prog_cnt, ids_len;
1997 int ret;
1998
1999 if (!perfmon_capable())
2000 return -EPERM;
2001 if (event->attr.type != PERF_TYPE_TRACEPOINT)
2002 return -EINVAL;
2003 if (copy_from_user(&query, uquery, sizeof(query)))
2004 return -EFAULT;
2005
2006 ids_len = query.ids_len;
2007 if (ids_len > BPF_TRACE_MAX_PROGS)
2008 return -E2BIG;
2009 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2010 if (!ids)
2011 return -ENOMEM;
2012
2013
2014
2015
2016
2017
2018
2019 mutex_lock(&bpf_event_mutex);
2020 progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2021 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2022 mutex_unlock(&bpf_event_mutex);
2023
2024 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2025 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2026 ret = -EFAULT;
2027
2028 kfree(ids);
2029 return ret;
2030}
2031
2032extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2033extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2034
2035struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2036{
2037 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2038
2039 for (; btp < __stop__bpf_raw_tp; btp++) {
2040 if (!strcmp(btp->tp->name, name))
2041 return btp;
2042 }
2043
2044 return bpf_get_raw_tracepoint_module(name);
2045}
2046
2047void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2048{
2049 struct module *mod;
2050
2051 preempt_disable();
2052 mod = __module_address((unsigned long)btp);
2053 module_put(mod);
2054 preempt_enable();
2055}
2056
2057static __always_inline
2058void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2059{
2060 cant_sleep();
2061 rcu_read_lock();
2062 (void) bpf_prog_run(prog, args);
2063 rcu_read_unlock();
2064}
2065
2066#define UNPACK(...) __VA_ARGS__
2067#define REPEAT_1(FN, DL, X, ...) FN(X)
2068#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2069#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2070#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2071#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2072#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2073#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2074#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2075#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2076#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2077#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2078#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2079#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
2080
2081#define SARG(X) u64 arg##X
2082#define COPY(X) args[X] = arg##X
2083
2084#define __DL_COM (,)
2085#define __DL_SEM (;)
2086
2087#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2088
2089#define BPF_TRACE_DEFN_x(x) \
2090 void bpf_trace_run##x(struct bpf_prog *prog, \
2091 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
2092 { \
2093 u64 args[x]; \
2094 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
2095 __bpf_trace_run(prog, args); \
2096 } \
2097 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2098BPF_TRACE_DEFN_x(1);
2099BPF_TRACE_DEFN_x(2);
2100BPF_TRACE_DEFN_x(3);
2101BPF_TRACE_DEFN_x(4);
2102BPF_TRACE_DEFN_x(5);
2103BPF_TRACE_DEFN_x(6);
2104BPF_TRACE_DEFN_x(7);
2105BPF_TRACE_DEFN_x(8);
2106BPF_TRACE_DEFN_x(9);
2107BPF_TRACE_DEFN_x(10);
2108BPF_TRACE_DEFN_x(11);
2109BPF_TRACE_DEFN_x(12);
2110
2111static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2112{
2113 struct tracepoint *tp = btp->tp;
2114
2115
2116
2117
2118
2119 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2120 return -EINVAL;
2121
2122 if (prog->aux->max_tp_access > btp->writable_size)
2123 return -EINVAL;
2124
2125 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2126 prog);
2127}
2128
2129int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2130{
2131 return __bpf_probe_register(btp, prog);
2132}
2133
2134int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2135{
2136 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2137}
2138
2139int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2140 u32 *fd_type, const char **buf,
2141 u64 *probe_offset, u64 *probe_addr)
2142{
2143 bool is_tracepoint, is_syscall_tp;
2144 struct bpf_prog *prog;
2145 int flags, err = 0;
2146
2147 prog = event->prog;
2148 if (!prog)
2149 return -ENOENT;
2150
2151
2152 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2153 return -EOPNOTSUPP;
2154
2155 *prog_id = prog->aux->id;
2156 flags = event->tp_event->flags;
2157 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2158 is_syscall_tp = is_syscall_trace_event(event->tp_event);
2159
2160 if (is_tracepoint || is_syscall_tp) {
2161 *buf = is_tracepoint ? event->tp_event->tp->name
2162 : event->tp_event->name;
2163 *fd_type = BPF_FD_TYPE_TRACEPOINT;
2164 *probe_offset = 0x0;
2165 *probe_addr = 0x0;
2166 } else {
2167
2168 err = -EOPNOTSUPP;
2169#ifdef CONFIG_KPROBE_EVENTS
2170 if (flags & TRACE_EVENT_FL_KPROBE)
2171 err = bpf_get_kprobe_info(event, fd_type, buf,
2172 probe_offset, probe_addr,
2173 event->attr.type == PERF_TYPE_TRACEPOINT);
2174#endif
2175#ifdef CONFIG_UPROBE_EVENTS
2176 if (flags & TRACE_EVENT_FL_UPROBE)
2177 err = bpf_get_uprobe_info(event, fd_type, buf,
2178 probe_offset,
2179 event->attr.type == PERF_TYPE_TRACEPOINT);
2180#endif
2181 }
2182
2183 return err;
2184}
2185
2186static int __init send_signal_irq_work_init(void)
2187{
2188 int cpu;
2189 struct send_signal_irq_work *work;
2190
2191 for_each_possible_cpu(cpu) {
2192 work = per_cpu_ptr(&send_signal_work, cpu);
2193 init_irq_work(&work->irq_work, do_bpf_send_signal);
2194 }
2195 return 0;
2196}
2197
2198subsys_initcall(send_signal_irq_work_init);
2199
2200#ifdef CONFIG_MODULES
2201static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2202 void *module)
2203{
2204 struct bpf_trace_module *btm, *tmp;
2205 struct module *mod = module;
2206 int ret = 0;
2207
2208 if (mod->num_bpf_raw_events == 0 ||
2209 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2210 goto out;
2211
2212 mutex_lock(&bpf_module_mutex);
2213
2214 switch (op) {
2215 case MODULE_STATE_COMING:
2216 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2217 if (btm) {
2218 btm->module = module;
2219 list_add(&btm->list, &bpf_trace_modules);
2220 } else {
2221 ret = -ENOMEM;
2222 }
2223 break;
2224 case MODULE_STATE_GOING:
2225 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2226 if (btm->module == module) {
2227 list_del(&btm->list);
2228 kfree(btm);
2229 break;
2230 }
2231 }
2232 break;
2233 }
2234
2235 mutex_unlock(&bpf_module_mutex);
2236
2237out:
2238 return notifier_from_errno(ret);
2239}
2240
2241static struct notifier_block bpf_module_nb = {
2242 .notifier_call = bpf_event_notify,
2243};
2244
2245static int __init bpf_event_init(void)
2246{
2247 register_module_notifier(&bpf_module_nb);
2248 return 0;
2249}
2250
2251fs_initcall(bpf_event_init);
2252#endif
2253
2254#ifdef CONFIG_FPROBE
2255struct bpf_kprobe_multi_link {
2256 struct bpf_link link;
2257 struct fprobe fp;
2258 unsigned long *addrs;
2259 u64 *cookies;
2260 u32 cnt;
2261};
2262
2263struct bpf_kprobe_multi_run_ctx {
2264 struct bpf_run_ctx run_ctx;
2265 struct bpf_kprobe_multi_link *link;
2266 unsigned long entry_ip;
2267};
2268
2269struct user_syms {
2270 const char **syms;
2271 char *buf;
2272};
2273
2274static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
2275{
2276 unsigned long __user usymbol;
2277 const char **syms = NULL;
2278 char *buf = NULL, *p;
2279 int err = -ENOMEM;
2280 unsigned int i;
2281
2282 syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
2283 if (!syms)
2284 goto error;
2285
2286 buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
2287 if (!buf)
2288 goto error;
2289
2290 for (p = buf, i = 0; i < cnt; i++) {
2291 if (__get_user(usymbol, usyms + i)) {
2292 err = -EFAULT;
2293 goto error;
2294 }
2295 err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2296 if (err == KSYM_NAME_LEN)
2297 err = -E2BIG;
2298 if (err < 0)
2299 goto error;
2300 syms[i] = p;
2301 p += err + 1;
2302 }
2303
2304 us->syms = syms;
2305 us->buf = buf;
2306 return 0;
2307
2308error:
2309 if (err) {
2310 kvfree(syms);
2311 kvfree(buf);
2312 }
2313 return err;
2314}
2315
2316static void free_user_syms(struct user_syms *us)
2317{
2318 kvfree(us->syms);
2319 kvfree(us->buf);
2320}
2321
2322static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2323{
2324 struct bpf_kprobe_multi_link *kmulti_link;
2325
2326 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2327 unregister_fprobe(&kmulti_link->fp);
2328}
2329
2330static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2331{
2332 struct bpf_kprobe_multi_link *kmulti_link;
2333
2334 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2335 kvfree(kmulti_link->addrs);
2336 kvfree(kmulti_link->cookies);
2337 kfree(kmulti_link);
2338}
2339
2340static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2341 .release = bpf_kprobe_multi_link_release,
2342 .dealloc = bpf_kprobe_multi_link_dealloc,
2343};
2344
2345static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2346{
2347 const struct bpf_kprobe_multi_link *link = priv;
2348 unsigned long *addr_a = a, *addr_b = b;
2349 u64 *cookie_a, *cookie_b;
2350
2351 cookie_a = link->cookies + (addr_a - link->addrs);
2352 cookie_b = link->cookies + (addr_b - link->addrs);
2353
2354
2355 swap(*addr_a, *addr_b);
2356 swap(*cookie_a, *cookie_b);
2357}
2358
2359static int __bpf_kprobe_multi_cookie_cmp(const void *a, const void *b)
2360{
2361 const unsigned long *addr_a = a, *addr_b = b;
2362
2363 if (*addr_a == *addr_b)
2364 return 0;
2365 return *addr_a < *addr_b ? -1 : 1;
2366}
2367
2368static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2369{
2370 return __bpf_kprobe_multi_cookie_cmp(a, b);
2371}
2372
2373static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2374{
2375 struct bpf_kprobe_multi_run_ctx *run_ctx;
2376 struct bpf_kprobe_multi_link *link;
2377 u64 *cookie, entry_ip;
2378 unsigned long *addr;
2379
2380 if (WARN_ON_ONCE(!ctx))
2381 return 0;
2382 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2383 link = run_ctx->link;
2384 if (!link->cookies)
2385 return 0;
2386 entry_ip = run_ctx->entry_ip;
2387 addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2388 __bpf_kprobe_multi_cookie_cmp);
2389 if (!addr)
2390 return 0;
2391 cookie = link->cookies + (addr - link->addrs);
2392 return *cookie;
2393}
2394
2395static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2396{
2397 struct bpf_kprobe_multi_run_ctx *run_ctx;
2398
2399 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx);
2400 return run_ctx->entry_ip;
2401}
2402
2403static int
2404kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2405 unsigned long entry_ip, struct pt_regs *regs)
2406{
2407 struct bpf_kprobe_multi_run_ctx run_ctx = {
2408 .link = link,
2409 .entry_ip = entry_ip,
2410 };
2411 struct bpf_run_ctx *old_run_ctx;
2412 int err;
2413
2414 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2415 err = 0;
2416 goto out;
2417 }
2418
2419 migrate_disable();
2420 rcu_read_lock();
2421 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2422 err = bpf_prog_run(link->link.prog, regs);
2423 bpf_reset_run_ctx(old_run_ctx);
2424 rcu_read_unlock();
2425 migrate_enable();
2426
2427 out:
2428 __this_cpu_dec(bpf_prog_active);
2429 return err;
2430}
2431
2432static void
2433kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
2434 struct pt_regs *regs)
2435{
2436 struct bpf_kprobe_multi_link *link;
2437
2438 link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2439 kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs);
2440}
2441
2442static int symbols_cmp_r(const void *a, const void *b, const void *priv)
2443{
2444 const char **str_a = (const char **) a;
2445 const char **str_b = (const char **) b;
2446
2447 return strcmp(*str_a, *str_b);
2448}
2449
2450struct multi_symbols_sort {
2451 const char **funcs;
2452 u64 *cookies;
2453};
2454
2455static void symbols_swap_r(void *a, void *b, int size, const void *priv)
2456{
2457 const struct multi_symbols_sort *data = priv;
2458 const char **name_a = a, **name_b = b;
2459
2460 swap(*name_a, *name_b);
2461
2462
2463 if (data->cookies) {
2464 u64 *cookie_a, *cookie_b;
2465
2466 cookie_a = data->cookies + (name_a - data->funcs);
2467 cookie_b = data->cookies + (name_b - data->funcs);
2468 swap(*cookie_a, *cookie_b);
2469 }
2470}
2471
2472int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2473{
2474 struct bpf_kprobe_multi_link *link = NULL;
2475 struct bpf_link_primer link_primer;
2476 void __user *ucookies;
2477 unsigned long *addrs;
2478 u32 flags, cnt, size;
2479 void __user *uaddrs;
2480 u64 *cookies = NULL;
2481 void __user *usyms;
2482 int err;
2483
2484
2485 if (sizeof(u64) != sizeof(void *))
2486 return -EOPNOTSUPP;
2487
2488 if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
2489 return -EINVAL;
2490
2491 flags = attr->link_create.kprobe_multi.flags;
2492 if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2493 return -EINVAL;
2494
2495 uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2496 usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2497 if (!!uaddrs == !!usyms)
2498 return -EINVAL;
2499
2500 cnt = attr->link_create.kprobe_multi.cnt;
2501 if (!cnt)
2502 return -EINVAL;
2503
2504 size = cnt * sizeof(*addrs);
2505 addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2506 if (!addrs)
2507 return -ENOMEM;
2508
2509 ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2510 if (ucookies) {
2511 cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2512 if (!cookies) {
2513 err = -ENOMEM;
2514 goto error;
2515 }
2516 if (copy_from_user(cookies, ucookies, size)) {
2517 err = -EFAULT;
2518 goto error;
2519 }
2520 }
2521
2522 if (uaddrs) {
2523 if (copy_from_user(addrs, uaddrs, size)) {
2524 err = -EFAULT;
2525 goto error;
2526 }
2527 } else {
2528 struct multi_symbols_sort data = {
2529 .cookies = cookies,
2530 };
2531 struct user_syms us;
2532
2533 err = copy_user_syms(&us, usyms, cnt);
2534 if (err)
2535 goto error;
2536
2537 if (cookies)
2538 data.funcs = us.syms;
2539
2540 sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
2541 symbols_swap_r, &data);
2542
2543 err = ftrace_lookup_symbols(us.syms, cnt, addrs);
2544 free_user_syms(&us);
2545 if (err)
2546 goto error;
2547 }
2548
2549 link = kzalloc(sizeof(*link), GFP_KERNEL);
2550 if (!link) {
2551 err = -ENOMEM;
2552 goto error;
2553 }
2554
2555 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
2556 &bpf_kprobe_multi_link_lops, prog);
2557
2558 err = bpf_link_prime(&link->link, &link_primer);
2559 if (err)
2560 goto error;
2561
2562 if (flags & BPF_F_KPROBE_MULTI_RETURN)
2563 link->fp.exit_handler = kprobe_multi_link_handler;
2564 else
2565 link->fp.entry_handler = kprobe_multi_link_handler;
2566
2567 link->addrs = addrs;
2568 link->cookies = cookies;
2569 link->cnt = cnt;
2570
2571 if (cookies) {
2572
2573
2574
2575
2576
2577
2578 sort_r(addrs, cnt, sizeof(*addrs),
2579 bpf_kprobe_multi_cookie_cmp,
2580 bpf_kprobe_multi_cookie_swap,
2581 link);
2582 }
2583
2584 err = register_fprobe_ips(&link->fp, addrs, cnt);
2585 if (err) {
2586 bpf_link_cleanup(&link_primer);
2587 return err;
2588 }
2589
2590 return bpf_link_settle(&link_primer);
2591
2592error:
2593 kfree(link);
2594 kvfree(addrs);
2595 kvfree(cookies);
2596 return err;
2597}
2598#else
2599int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2600{
2601 return -EOPNOTSUPP;
2602}
2603static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2604{
2605 return 0;
2606}
2607static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2608{
2609 return 0;
2610}
2611#endif
2612