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