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