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