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