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