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