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