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