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