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 task ? (unsigned long)task_pt_regs(task) : 0; 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 if (!copy.mnt || !copy.dentry) 950 return -EFAULT; 951 952 p = d_path(©, buf, sz); 953 if (IS_ERR(p)) { 954 len = PTR_ERR(p); 955 } else { 956 len = buf + sz - p; 957 memmove(buf, p, len); 958 } 959 960 return len; 961 } 962 963 BTF_SET_START(btf_allowlist_d_path) 964 #ifdef CONFIG_SECURITY 965 BTF_ID(func, security_file_permission) 966 BTF_ID(func, security_inode_getattr) 967 BTF_ID(func, security_file_open) 968 #endif 969 #ifdef CONFIG_SECURITY_PATH 970 BTF_ID(func, security_path_truncate) 971 #endif 972 BTF_ID(func, vfs_truncate) 973 BTF_ID(func, vfs_fallocate) 974 BTF_ID(func, dentry_open) 975 BTF_ID(func, vfs_getattr) 976 BTF_ID(func, filp_close) 977 BTF_SET_END(btf_allowlist_d_path) 978 979 BTF_SET_START(btf_allowlist_d_path_fentry) 980 BTF_ID(func, filp_close_sync) 981 BTF_SET_END(btf_allowlist_d_path_fentry) 982 983 static bool bpf_d_path_allowed(const struct bpf_prog *prog) 984 { 985 /* attach_btf_id is a function of the target program */ 986 if (prog->aux->dst_prog) 987 return false; 988 989 if (prog->type == BPF_PROG_TYPE_TRACING && 990 prog->expected_attach_type == BPF_TRACE_ITER) 991 return true; 992 993 if (prog->type == BPF_PROG_TYPE_LSM) 994 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id); 995 996 if (btf_id_set_contains(&btf_allowlist_d_path, 997 prog->aux->attach_btf_id)) 998 return true; 999 1000 if (btf_id_set_contains(&btf_allowlist_d_path_fentry, 1001 prog->aux->attach_btf_id)) 1002 return prog->expected_attach_type == BPF_TRACE_FENTRY; 1003 1004 return false; 1005 } 1006 1007 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path) 1008 1009 static const struct bpf_func_proto bpf_d_path_proto = { 1010 .func = bpf_d_path, 1011 .gpl_only = false, 1012 .ret_type = RET_INTEGER, 1013 .arg1_type = ARG_PTR_TO_BTF_ID, 1014 .arg1_btf_id = &bpf_d_path_btf_ids[0], 1015 .arg2_type = ARG_PTR_TO_MEM | MEM_UNINIT | MEM_WRITE, 1016 .arg3_type = ARG_MEM_SIZE_OR_ZERO, 1017 .allowed = bpf_d_path_allowed, 1018 }; 1019 1020 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \ 1021 BTF_F_PTR_RAW | BTF_F_ZERO) 1022 1023 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, 1024 u64 flags, const struct btf **btf, 1025 s32 *btf_id) 1026 { 1027 const struct btf_type *t; 1028 1029 if (unlikely(flags & ~(BTF_F_ALL))) 1030 return -EINVAL; 1031 1032 if (btf_ptr_size != sizeof(struct btf_ptr)) 1033 return -EINVAL; 1034 1035 *btf = bpf_get_btf_vmlinux(); 1036 1037 if (IS_ERR_OR_NULL(*btf)) 1038 return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL; 1039 1040 if (ptr->type_id > 0) 1041 *btf_id = ptr->type_id; 1042 else 1043 return -EINVAL; 1044 1045 if (*btf_id > 0) 1046 t = btf_type_by_id(*btf, *btf_id); 1047 if (*btf_id <= 0 || !t) 1048 return -ENOENT; 1049 1050 return 0; 1051 } 1052 1053 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr, 1054 u32, btf_ptr_size, u64, flags) 1055 { 1056 const struct btf *btf; 1057 s32 btf_id; 1058 int ret; 1059 1060 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); 1061 if (ret) 1062 return ret; 1063 1064 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, 1065 flags); 1066 } 1067 1068 const struct bpf_func_proto bpf_snprintf_btf_proto = { 1069 .func = bpf_snprintf_btf, 1070 .gpl_only = false, 1071 .ret_type = RET_INTEGER, 1072 .arg1_type = ARG_PTR_TO_MEM | MEM_UNINIT | MEM_WRITE, 1073 .arg2_type = ARG_MEM_SIZE, 1074 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, 1075 .arg4_type = ARG_MEM_SIZE, 1076 .arg5_type = ARG_ANYTHING, 1077 }; 1078 1079 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) 1080 { 1081 /* This helper call is inlined by verifier. */ 1082 return ((u64 *)ctx)[-2]; 1083 } 1084 1085 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { 1086 .func = bpf_get_func_ip_tracing, 1087 .gpl_only = true, 1088 .ret_type = RET_INTEGER, 1089 .arg1_type = ARG_PTR_TO_CTX, 1090 }; 1091 1092 static inline unsigned long get_entry_ip(unsigned long fentry_ip) 1093 { 1094 #ifdef CONFIG_X86_KERNEL_IBT 1095 if (is_endbr((void *)(fentry_ip - ENDBR_INSN_SIZE))) 1096 fentry_ip -= ENDBR_INSN_SIZE; 1097 #endif 1098 return fentry_ip; 1099 } 1100 1101 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs) 1102 { 1103 struct bpf_trace_run_ctx *run_ctx __maybe_unused; 1104 struct kprobe *kp; 1105 1106 #ifdef CONFIG_UPROBES 1107 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); 1108 if (run_ctx->is_uprobe) 1109 return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr; 1110 #endif 1111 1112 kp = kprobe_running(); 1113 1114 if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY)) 1115 return 0; 1116 1117 return get_entry_ip((uintptr_t)kp->addr); 1118 } 1119 1120 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { 1121 .func = bpf_get_func_ip_kprobe, 1122 .gpl_only = true, 1123 .ret_type = RET_INTEGER, 1124 .arg1_type = ARG_PTR_TO_CTX, 1125 }; 1126 1127 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs) 1128 { 1129 return bpf_kprobe_multi_entry_ip(current->bpf_ctx); 1130 } 1131 1132 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = { 1133 .func = bpf_get_func_ip_kprobe_multi, 1134 .gpl_only = false, 1135 .ret_type = RET_INTEGER, 1136 .arg1_type = ARG_PTR_TO_CTX, 1137 }; 1138 1139 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs) 1140 { 1141 return bpf_kprobe_multi_cookie(current->bpf_ctx); 1142 } 1143 1144 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = { 1145 .func = bpf_get_attach_cookie_kprobe_multi, 1146 .gpl_only = false, 1147 .ret_type = RET_INTEGER, 1148 .arg1_type = ARG_PTR_TO_CTX, 1149 }; 1150 1151 BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs) 1152 { 1153 return bpf_uprobe_multi_entry_ip(current->bpf_ctx); 1154 } 1155 1156 static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = { 1157 .func = bpf_get_func_ip_uprobe_multi, 1158 .gpl_only = false, 1159 .ret_type = RET_INTEGER, 1160 .arg1_type = ARG_PTR_TO_CTX, 1161 }; 1162 1163 BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs) 1164 { 1165 return bpf_uprobe_multi_cookie(current->bpf_ctx); 1166 } 1167 1168 static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = { 1169 .func = bpf_get_attach_cookie_uprobe_multi, 1170 .gpl_only = false, 1171 .ret_type = RET_INTEGER, 1172 .arg1_type = ARG_PTR_TO_CTX, 1173 }; 1174 1175 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) 1176 { 1177 struct bpf_trace_run_ctx *run_ctx; 1178 1179 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); 1180 return run_ctx->bpf_cookie; 1181 } 1182 1183 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = { 1184 .func = bpf_get_attach_cookie_trace, 1185 .gpl_only = false, 1186 .ret_type = RET_INTEGER, 1187 .arg1_type = ARG_PTR_TO_CTX, 1188 }; 1189 1190 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx) 1191 { 1192 return ctx->event->bpf_cookie; 1193 } 1194 1195 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = { 1196 .func = bpf_get_attach_cookie_pe, 1197 .gpl_only = false, 1198 .ret_type = RET_INTEGER, 1199 .arg1_type = ARG_PTR_TO_CTX, 1200 }; 1201 1202 BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx) 1203 { 1204 struct bpf_trace_run_ctx *run_ctx; 1205 1206 run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx); 1207 return run_ctx->bpf_cookie; 1208 } 1209 1210 static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = { 1211 .func = bpf_get_attach_cookie_tracing, 1212 .gpl_only = false, 1213 .ret_type = RET_INTEGER, 1214 .arg1_type = ARG_PTR_TO_CTX, 1215 }; 1216 1217 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags) 1218 { 1219 static const u32 br_entry_size = sizeof(struct perf_branch_entry); 1220 u32 entry_cnt = size / br_entry_size; 1221 1222 entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt); 1223 1224 if (unlikely(flags)) 1225 return -EINVAL; 1226 1227 if (!entry_cnt) 1228 return -ENOENT; 1229 1230 return entry_cnt * br_entry_size; 1231 } 1232 1233 const struct bpf_func_proto bpf_get_branch_snapshot_proto = { 1234 .func = bpf_get_branch_snapshot, 1235 .gpl_only = true, 1236 .ret_type = RET_INTEGER, 1237 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 1238 .arg2_type = ARG_MEM_SIZE_OR_ZERO, 1239 }; 1240 1241 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value) 1242 { 1243 /* This helper call is inlined by verifier. */ 1244 u64 nr_args = ((u64 *)ctx)[-1] & 0xFF; 1245 1246 if ((u64) n >= nr_args) 1247 return -EINVAL; 1248 *value = ((u64 *)ctx)[n]; 1249 return 0; 1250 } 1251 1252 static const struct bpf_func_proto bpf_get_func_arg_proto = { 1253 .func = get_func_arg, 1254 .ret_type = RET_INTEGER, 1255 .arg1_type = ARG_PTR_TO_CTX, 1256 .arg2_type = ARG_ANYTHING, 1257 .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 1258 .arg3_size = sizeof(u64), 1259 }; 1260 1261 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value) 1262 { 1263 /* This helper call is inlined by verifier. */ 1264 u64 nr_args = ((u64 *)ctx)[-1] & 0xFF; 1265 1266 *value = ((u64 *)ctx)[nr_args]; 1267 return 0; 1268 } 1269 1270 static const struct bpf_func_proto bpf_get_func_ret_proto = { 1271 .func = get_func_ret, 1272 .ret_type = RET_INTEGER, 1273 .arg1_type = ARG_PTR_TO_CTX, 1274 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, 1275 .arg2_size = sizeof(u64), 1276 }; 1277 1278 BPF_CALL_1(get_func_arg_cnt, void *, ctx) 1279 { 1280 /* This helper call is inlined by verifier. */ 1281 return ((u64 *)ctx)[-1] & 0xFF; 1282 } 1283 1284 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { 1285 .func = get_func_arg_cnt, 1286 .ret_type = RET_INTEGER, 1287 .arg1_type = ARG_PTR_TO_CTX, 1288 }; 1289 1290 static const struct bpf_func_proto * 1291 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1292 { 1293 const struct bpf_func_proto *func_proto; 1294 1295 switch (func_id) { 1296 case BPF_FUNC_get_smp_processor_id: 1297 return &bpf_get_smp_processor_id_proto; 1298 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 1299 case BPF_FUNC_probe_read: 1300 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 1301 NULL : &bpf_probe_read_compat_proto; 1302 case BPF_FUNC_probe_read_str: 1303 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 1304 NULL : &bpf_probe_read_compat_str_proto; 1305 #endif 1306 case BPF_FUNC_get_func_ip: 1307 return &bpf_get_func_ip_proto_tracing; 1308 default: 1309 break; 1310 } 1311 1312 func_proto = bpf_base_func_proto(func_id, prog); 1313 if (func_proto) 1314 return func_proto; 1315 1316 if (!bpf_token_capable(prog->aux->token, CAP_SYS_ADMIN)) 1317 return NULL; 1318 1319 switch (func_id) { 1320 case BPF_FUNC_probe_write_user: 1321 return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ? 1322 NULL : &bpf_probe_write_user_proto; 1323 default: 1324 return NULL; 1325 } 1326 } 1327 1328 static bool is_kprobe_multi(const struct bpf_prog *prog) 1329 { 1330 return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI || 1331 prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION; 1332 } 1333 1334 static inline bool is_kprobe_session(const struct bpf_prog *prog) 1335 { 1336 return prog->type == BPF_PROG_TYPE_KPROBE && 1337 prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION; 1338 } 1339 1340 static inline bool is_uprobe_multi(const struct bpf_prog *prog) 1341 { 1342 return prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI || 1343 prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION; 1344 } 1345 1346 static inline bool is_uprobe_session(const struct bpf_prog *prog) 1347 { 1348 return prog->type == BPF_PROG_TYPE_KPROBE && 1349 prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION; 1350 } 1351 1352 static inline bool is_trace_fsession(const struct bpf_prog *prog) 1353 { 1354 return prog->type == BPF_PROG_TYPE_TRACING && 1355 (prog->expected_attach_type == BPF_TRACE_FSESSION || 1356 prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI); 1357 } 1358 1359 static const struct bpf_func_proto * 1360 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1361 { 1362 switch (func_id) { 1363 case BPF_FUNC_perf_event_output: 1364 return &bpf_perf_event_output_proto; 1365 case BPF_FUNC_get_stackid: 1366 return &bpf_get_stackid_proto; 1367 case BPF_FUNC_get_stack: 1368 return prog->sleepable ? &bpf_get_stack_sleepable_proto : &bpf_get_stack_proto; 1369 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 1370 case BPF_FUNC_override_return: 1371 return &bpf_override_return_proto; 1372 #endif 1373 case BPF_FUNC_get_func_ip: 1374 if (is_kprobe_multi(prog)) 1375 return &bpf_get_func_ip_proto_kprobe_multi; 1376 if (is_uprobe_multi(prog)) 1377 return &bpf_get_func_ip_proto_uprobe_multi; 1378 return &bpf_get_func_ip_proto_kprobe; 1379 case BPF_FUNC_get_attach_cookie: 1380 if (is_kprobe_multi(prog)) 1381 return &bpf_get_attach_cookie_proto_kmulti; 1382 if (is_uprobe_multi(prog)) 1383 return &bpf_get_attach_cookie_proto_umulti; 1384 return &bpf_get_attach_cookie_proto_trace; 1385 default: 1386 return bpf_tracing_func_proto(func_id, prog); 1387 } 1388 } 1389 1390 /* bpf+kprobe programs can access fields of 'struct pt_regs' */ 1391 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1392 const struct bpf_prog *prog, 1393 struct bpf_insn_access_aux *info) 1394 { 1395 if (off < 0 || off >= sizeof(struct pt_regs)) 1396 return false; 1397 if (off % size != 0) 1398 return false; 1399 /* 1400 * Assertion for 32 bit to make sure last 8 byte access 1401 * (BPF_DW) to the last 4 byte member is disallowed. 1402 */ 1403 if (off + size > sizeof(struct pt_regs)) 1404 return false; 1405 1406 if (type == BPF_WRITE) { 1407 /* 1408 * pt_regs also holds what the kernel restores on the way to 1409 * user space: cs and flags on x86, pstate on arm64, status on 1410 * riscv. A prog can write the registers of the task only. 1411 */ 1412 if (!arch_uprobe_regs_writable(off, size)) 1413 return false; 1414 prog->aux->kprobe_write_ctx = true; 1415 } 1416 1417 return true; 1418 } 1419 1420 const struct bpf_verifier_ops kprobe_verifier_ops = { 1421 .get_func_proto = kprobe_prog_func_proto, 1422 .is_valid_access = kprobe_prog_is_valid_access, 1423 }; 1424 1425 const struct bpf_prog_ops kprobe_prog_ops = { 1426 }; 1427 1428 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, 1429 u64, flags, void *, data, u64, size) 1430 { 1431 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 1432 1433 /* 1434 * r1 points to perf tracepoint buffer where first 8 bytes are hidden 1435 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it 1436 * from there and call the same bpf_perf_event_output() helper inline. 1437 */ 1438 return ____bpf_perf_event_output(regs, map, flags, data, size); 1439 } 1440 1441 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { 1442 .func = bpf_perf_event_output_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 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 1449 .arg5_type = ARG_MEM_SIZE_OR_ZERO, 1450 }; 1451 1452 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, 1453 u64, flags) 1454 { 1455 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 1456 1457 /* 1458 * Same comment as in bpf_perf_event_output_tp(), only that this time 1459 * the other helper's function body cannot be inlined due to being 1460 * external, thus we need to call raw helper function. 1461 */ 1462 return bpf_get_stackid((unsigned long) regs, (unsigned long) map, 1463 flags, 0, 0); 1464 } 1465 1466 static const struct bpf_func_proto bpf_get_stackid_proto_tp = { 1467 .func = bpf_get_stackid_tp, 1468 .gpl_only = true, 1469 .ret_type = RET_INTEGER, 1470 .arg1_type = ARG_PTR_TO_CTX, 1471 .arg2_type = ARG_CONST_MAP_PTR, 1472 .arg3_type = ARG_ANYTHING, 1473 }; 1474 1475 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, 1476 u64, flags) 1477 { 1478 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 1479 1480 return bpf_get_stack((unsigned long) regs, (unsigned long) buf, 1481 (unsigned long) size, flags, 0); 1482 } 1483 1484 static const struct bpf_func_proto bpf_get_stack_proto_tp = { 1485 .func = bpf_get_stack_tp, 1486 .gpl_only = true, 1487 .ret_type = RET_INTEGER, 1488 .arg1_type = ARG_PTR_TO_CTX, 1489 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 1490 .arg3_type = ARG_MEM_SIZE_OR_ZERO, 1491 .arg4_type = ARG_ANYTHING, 1492 }; 1493 1494 static const struct bpf_func_proto * 1495 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1496 { 1497 switch (func_id) { 1498 case BPF_FUNC_perf_event_output: 1499 return &bpf_perf_event_output_proto_tp; 1500 case BPF_FUNC_get_stackid: 1501 return &bpf_get_stackid_proto_tp; 1502 case BPF_FUNC_get_stack: 1503 return &bpf_get_stack_proto_tp; 1504 case BPF_FUNC_get_attach_cookie: 1505 return &bpf_get_attach_cookie_proto_trace; 1506 default: 1507 return bpf_tracing_func_proto(func_id, prog); 1508 } 1509 } 1510 1511 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1512 const struct bpf_prog *prog, 1513 struct bpf_insn_access_aux *info) 1514 { 1515 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) 1516 return false; 1517 if (type != BPF_READ) 1518 return false; 1519 if (off % size != 0) 1520 return false; 1521 1522 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); 1523 return true; 1524 } 1525 1526 const struct bpf_verifier_ops tracepoint_verifier_ops = { 1527 .get_func_proto = tp_prog_func_proto, 1528 .is_valid_access = tp_prog_is_valid_access, 1529 }; 1530 1531 const struct bpf_prog_ops tracepoint_prog_ops = { 1532 }; 1533 1534 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, 1535 struct bpf_perf_event_value *, buf, u32, size) 1536 { 1537 int err = -EINVAL; 1538 1539 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 1540 goto clear; 1541 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, 1542 &buf->running); 1543 if (unlikely(err)) 1544 goto clear; 1545 return 0; 1546 clear: 1547 memset(buf, 0, size); 1548 return err; 1549 } 1550 1551 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { 1552 .func = bpf_perf_prog_read_value, 1553 .gpl_only = true, 1554 .ret_type = RET_INTEGER, 1555 .arg1_type = ARG_PTR_TO_CTX, 1556 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 1557 .arg3_type = ARG_MEM_SIZE, 1558 }; 1559 1560 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx, 1561 void *, buf, u32, size, u64, flags) 1562 { 1563 static const u32 br_entry_size = sizeof(struct perf_branch_entry); 1564 struct perf_branch_stack *br_stack = ctx->data->br_stack; 1565 u32 to_copy; 1566 1567 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) 1568 return -EINVAL; 1569 1570 if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK))) 1571 return -ENOENT; 1572 1573 if (unlikely(!br_stack)) 1574 return -ENOENT; 1575 1576 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE) 1577 return br_stack->nr * br_entry_size; 1578 1579 if (!buf || (size % br_entry_size != 0)) 1580 return -EINVAL; 1581 1582 to_copy = min_t(u32, br_stack->nr * br_entry_size, size); 1583 memcpy(buf, br_stack->entries, to_copy); 1584 1585 return to_copy; 1586 } 1587 1588 static const struct bpf_func_proto bpf_read_branch_records_proto = { 1589 .func = bpf_read_branch_records, 1590 .gpl_only = true, 1591 .ret_type = RET_INTEGER, 1592 .arg1_type = ARG_PTR_TO_CTX, 1593 .arg2_type = ARG_PTR_TO_MEM_OR_NULL | MEM_UNINIT | MEM_WRITE, 1594 .arg3_type = ARG_MEM_SIZE_OR_ZERO, 1595 .arg4_type = ARG_ANYTHING, 1596 }; 1597 1598 static const struct bpf_func_proto * 1599 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1600 { 1601 switch (func_id) { 1602 case BPF_FUNC_perf_event_output: 1603 return &bpf_perf_event_output_proto_tp; 1604 case BPF_FUNC_get_stackid: 1605 return &bpf_get_stackid_proto_pe; 1606 case BPF_FUNC_get_stack: 1607 return &bpf_get_stack_proto_pe; 1608 case BPF_FUNC_perf_prog_read_value: 1609 return &bpf_perf_prog_read_value_proto; 1610 case BPF_FUNC_read_branch_records: 1611 return &bpf_read_branch_records_proto; 1612 case BPF_FUNC_get_attach_cookie: 1613 return &bpf_get_attach_cookie_proto_pe; 1614 default: 1615 return bpf_tracing_func_proto(func_id, prog); 1616 } 1617 } 1618 1619 /* 1620 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp 1621 * to avoid potential recursive reuse issue when/if tracepoints are added 1622 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack. 1623 * 1624 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage 1625 * in normal, irq, and nmi context. 1626 */ 1627 struct bpf_raw_tp_regs { 1628 struct pt_regs regs[3]; 1629 }; 1630 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs); 1631 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level); 1632 static struct pt_regs *get_bpf_raw_tp_regs(void) 1633 { 1634 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs); 1635 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level); 1636 1637 if (nest_level > ARRAY_SIZE(tp_regs->regs)) { 1638 this_cpu_dec(bpf_raw_tp_nest_level); 1639 return ERR_PTR(-EBUSY); 1640 } 1641 1642 return &tp_regs->regs[nest_level - 1]; 1643 } 1644 1645 static void put_bpf_raw_tp_regs(void) 1646 { 1647 this_cpu_dec(bpf_raw_tp_nest_level); 1648 } 1649 1650 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, 1651 struct bpf_map *, map, u64, flags, void *, data, u64, size) 1652 { 1653 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1654 int ret; 1655 1656 if (IS_ERR(regs)) 1657 return PTR_ERR(regs); 1658 1659 perf_fetch_caller_regs(regs); 1660 ret = ____bpf_perf_event_output(regs, map, flags, data, size); 1661 1662 put_bpf_raw_tp_regs(); 1663 return ret; 1664 } 1665 1666 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { 1667 .func = bpf_perf_event_output_raw_tp, 1668 .gpl_only = true, 1669 .ret_type = RET_INTEGER, 1670 .arg1_type = ARG_PTR_TO_CTX, 1671 .arg2_type = ARG_CONST_MAP_PTR, 1672 .arg3_type = ARG_ANYTHING, 1673 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, 1674 .arg5_type = ARG_MEM_SIZE_OR_ZERO, 1675 }; 1676 1677 extern const struct bpf_func_proto bpf_skb_output_proto; 1678 extern const struct bpf_func_proto bpf_xdp_output_proto; 1679 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto; 1680 1681 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, 1682 struct bpf_map *, map, u64, flags) 1683 { 1684 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1685 int ret; 1686 1687 if (IS_ERR(regs)) 1688 return PTR_ERR(regs); 1689 1690 perf_fetch_caller_regs(regs); 1691 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ 1692 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map, 1693 flags, 0, 0); 1694 put_bpf_raw_tp_regs(); 1695 return ret; 1696 } 1697 1698 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { 1699 .func = bpf_get_stackid_raw_tp, 1700 .gpl_only = true, 1701 .ret_type = RET_INTEGER, 1702 .arg1_type = ARG_PTR_TO_CTX, 1703 .arg2_type = ARG_CONST_MAP_PTR, 1704 .arg3_type = ARG_ANYTHING, 1705 }; 1706 1707 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, 1708 void *, buf, u32, size, u64, flags) 1709 { 1710 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1711 int ret; 1712 1713 if (IS_ERR(regs)) 1714 return PTR_ERR(regs); 1715 1716 perf_fetch_caller_regs(regs); 1717 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf, 1718 (unsigned long) size, flags, 0); 1719 put_bpf_raw_tp_regs(); 1720 return ret; 1721 } 1722 1723 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { 1724 .func = bpf_get_stack_raw_tp, 1725 .gpl_only = true, 1726 .ret_type = RET_INTEGER, 1727 .arg1_type = ARG_PTR_TO_CTX, 1728 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 1729 .arg3_type = ARG_MEM_SIZE_OR_ZERO, 1730 .arg4_type = ARG_ANYTHING, 1731 }; 1732 1733 static const struct bpf_func_proto * 1734 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1735 { 1736 switch (func_id) { 1737 case BPF_FUNC_perf_event_output: 1738 return &bpf_perf_event_output_proto_raw_tp; 1739 case BPF_FUNC_get_stackid: 1740 return &bpf_get_stackid_proto_raw_tp; 1741 case BPF_FUNC_get_stack: 1742 return &bpf_get_stack_proto_raw_tp; 1743 case BPF_FUNC_get_attach_cookie: 1744 return &bpf_get_attach_cookie_proto_tracing; 1745 default: 1746 return bpf_tracing_func_proto(func_id, prog); 1747 } 1748 } 1749 1750 const struct bpf_func_proto * 1751 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1752 { 1753 const struct bpf_func_proto *fn; 1754 1755 switch (func_id) { 1756 #ifdef CONFIG_NET 1757 case BPF_FUNC_skb_output: 1758 return &bpf_skb_output_proto; 1759 case BPF_FUNC_xdp_output: 1760 return &bpf_xdp_output_proto; 1761 case BPF_FUNC_skc_to_tcp6_sock: 1762 return &bpf_skc_to_tcp6_sock_proto; 1763 case BPF_FUNC_skc_to_tcp_sock: 1764 return &bpf_skc_to_tcp_sock_proto; 1765 case BPF_FUNC_skc_to_tcp_timewait_sock: 1766 return &bpf_skc_to_tcp_timewait_sock_proto; 1767 case BPF_FUNC_skc_to_tcp_request_sock: 1768 return &bpf_skc_to_tcp_request_sock_proto; 1769 case BPF_FUNC_skc_to_udp6_sock: 1770 return &bpf_skc_to_udp6_sock_proto; 1771 case BPF_FUNC_skc_to_unix_sock: 1772 return &bpf_skc_to_unix_sock_proto; 1773 case BPF_FUNC_skc_to_mptcp_sock: 1774 return &bpf_skc_to_mptcp_sock_proto; 1775 case BPF_FUNC_sk_storage_get: 1776 return &bpf_sk_storage_get_tracing_proto; 1777 case BPF_FUNC_sk_storage_delete: 1778 return &bpf_sk_storage_delete_tracing_proto; 1779 case BPF_FUNC_sock_from_file: 1780 return &bpf_sock_from_file_proto; 1781 case BPF_FUNC_get_socket_cookie: 1782 return &bpf_get_socket_ptr_cookie_proto; 1783 case BPF_FUNC_xdp_get_buff_len: 1784 return &bpf_xdp_get_buff_len_trace_proto; 1785 #endif 1786 case BPF_FUNC_seq_printf: 1787 return prog->expected_attach_type == BPF_TRACE_ITER ? 1788 &bpf_seq_printf_proto : 1789 NULL; 1790 case BPF_FUNC_seq_write: 1791 return prog->expected_attach_type == BPF_TRACE_ITER ? 1792 &bpf_seq_write_proto : 1793 NULL; 1794 case BPF_FUNC_seq_printf_btf: 1795 return prog->expected_attach_type == BPF_TRACE_ITER ? 1796 &bpf_seq_printf_btf_proto : 1797 NULL; 1798 case BPF_FUNC_d_path: 1799 return &bpf_d_path_proto; 1800 case BPF_FUNC_get_func_arg: 1801 if (bpf_prog_has_trampoline(prog) || 1802 prog->expected_attach_type == BPF_TRACE_RAW_TP) 1803 return &bpf_get_func_arg_proto; 1804 return NULL; 1805 case BPF_FUNC_get_func_ret: 1806 return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL; 1807 case BPF_FUNC_get_func_arg_cnt: 1808 if (bpf_prog_has_trampoline(prog) || 1809 prog->expected_attach_type == BPF_TRACE_RAW_TP) 1810 return &bpf_get_func_arg_cnt_proto; 1811 return NULL; 1812 case BPF_FUNC_get_attach_cookie: 1813 if (prog->type == BPF_PROG_TYPE_TRACING && 1814 prog->expected_attach_type == BPF_TRACE_RAW_TP) 1815 return &bpf_get_attach_cookie_proto_tracing; 1816 return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL; 1817 default: 1818 fn = raw_tp_prog_func_proto(func_id, prog); 1819 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER) 1820 fn = bpf_iter_get_func_proto(func_id, prog); 1821 return fn; 1822 } 1823 } 1824 1825 static bool raw_tp_prog_is_valid_access(int off, int size, 1826 enum bpf_access_type type, 1827 const struct bpf_prog *prog, 1828 struct bpf_insn_access_aux *info) 1829 { 1830 return bpf_tracing_ctx_access(off, size, type); 1831 } 1832 1833 static bool tracing_prog_is_valid_access(int off, int size, 1834 enum bpf_access_type type, 1835 const struct bpf_prog *prog, 1836 struct bpf_insn_access_aux *info) 1837 { 1838 return bpf_tracing_btf_ctx_access(off, size, type, prog, info); 1839 } 1840 1841 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog, 1842 const union bpf_attr *kattr, 1843 union bpf_attr __user *uattr) 1844 { 1845 return -ENOTSUPP; 1846 } 1847 1848 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { 1849 .get_func_proto = raw_tp_prog_func_proto, 1850 .is_valid_access = raw_tp_prog_is_valid_access, 1851 }; 1852 1853 const struct bpf_prog_ops raw_tracepoint_prog_ops = { 1854 #ifdef CONFIG_NET 1855 .test_run = bpf_prog_test_run_raw_tp, 1856 #endif 1857 }; 1858 1859 const struct bpf_verifier_ops tracing_verifier_ops = { 1860 .get_func_proto = tracing_prog_func_proto, 1861 .is_valid_access = tracing_prog_is_valid_access, 1862 }; 1863 1864 const struct bpf_prog_ops tracing_prog_ops = { 1865 .test_run = bpf_prog_test_run_tracing, 1866 }; 1867 1868 static bool raw_tp_writable_prog_is_valid_access(int off, int size, 1869 enum bpf_access_type type, 1870 const struct bpf_prog *prog, 1871 struct bpf_insn_access_aux *info) 1872 { 1873 if (off == 0) { 1874 if (size != sizeof(u64) || type != BPF_READ) 1875 return false; 1876 info->reg_type = PTR_TO_TP_BUFFER; 1877 } 1878 return raw_tp_prog_is_valid_access(off, size, type, prog, info); 1879 } 1880 1881 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { 1882 .get_func_proto = raw_tp_prog_func_proto, 1883 .is_valid_access = raw_tp_writable_prog_is_valid_access, 1884 }; 1885 1886 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { 1887 }; 1888 1889 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1890 const struct bpf_prog *prog, 1891 struct bpf_insn_access_aux *info) 1892 { 1893 const int size_u64 = sizeof(u64); 1894 1895 if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) 1896 return false; 1897 if (type != BPF_READ) 1898 return false; 1899 if (off % size != 0) { 1900 if (sizeof(unsigned long) != 4) 1901 return false; 1902 if (size != 8) 1903 return false; 1904 if (off % size != 4) 1905 return false; 1906 } 1907 1908 switch (off) { 1909 case bpf_ctx_range(struct bpf_perf_event_data, sample_period): 1910 bpf_ctx_record_field_size(info, size_u64); 1911 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1912 return false; 1913 break; 1914 case bpf_ctx_range(struct bpf_perf_event_data, addr): 1915 bpf_ctx_record_field_size(info, size_u64); 1916 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1917 return false; 1918 break; 1919 default: 1920 if (size != sizeof(long)) 1921 return false; 1922 } 1923 1924 return true; 1925 } 1926 1927 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, 1928 const struct bpf_insn *si, 1929 struct bpf_insn *insn_buf, 1930 struct bpf_prog *prog, u32 *target_size) 1931 { 1932 struct bpf_insn *insn = insn_buf; 1933 1934 switch (si->off) { 1935 case offsetof(struct bpf_perf_event_data, sample_period): 1936 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1937 data), si->dst_reg, si->src_reg, 1938 offsetof(struct bpf_perf_event_data_kern, data)); 1939 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1940 bpf_target_off(struct perf_sample_data, period, 8, 1941 target_size)); 1942 break; 1943 case offsetof(struct bpf_perf_event_data, addr): 1944 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1945 data), si->dst_reg, si->src_reg, 1946 offsetof(struct bpf_perf_event_data_kern, data)); 1947 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1948 bpf_target_off(struct perf_sample_data, addr, 8, 1949 target_size)); 1950 break; 1951 default: 1952 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1953 regs), si->dst_reg, si->src_reg, 1954 offsetof(struct bpf_perf_event_data_kern, regs)); 1955 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, 1956 si->off); 1957 break; 1958 } 1959 1960 return insn - insn_buf; 1961 } 1962 1963 const struct bpf_verifier_ops perf_event_verifier_ops = { 1964 .get_func_proto = pe_prog_func_proto, 1965 .is_valid_access = pe_prog_is_valid_access, 1966 .convert_ctx_access = pe_prog_convert_ctx_access, 1967 }; 1968 1969 const struct bpf_prog_ops perf_event_prog_ops = { 1970 }; 1971 1972 static DEFINE_MUTEX(bpf_event_mutex); 1973 1974 #define BPF_TRACE_MAX_PROGS 64 1975 1976 int perf_event_attach_bpf_prog(struct perf_event *event, 1977 struct bpf_prog *prog, 1978 u64 bpf_cookie) 1979 { 1980 struct bpf_prog_array *old_array; 1981 struct bpf_prog_array *new_array; 1982 int ret = -EEXIST; 1983 1984 /* 1985 * Kprobe override only works if they are on the function entry, 1986 * and only if they are on the opt-in list. 1987 */ 1988 if (prog->kprobe_override && 1989 (!trace_kprobe_on_func_entry(event->tp_event) || 1990 !trace_kprobe_error_injectable(event->tp_event))) 1991 return -EINVAL; 1992 1993 mutex_lock(&bpf_event_mutex); 1994 1995 if (event->prog) 1996 goto unlock; 1997 1998 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1999 if (old_array && 2000 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { 2001 ret = -E2BIG; 2002 goto unlock; 2003 } 2004 2005 ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array); 2006 if (ret < 0) 2007 goto unlock; 2008 2009 /* set the new array to event->tp_event and set event->prog */ 2010 event->prog = prog; 2011 event->bpf_cookie = bpf_cookie; 2012 rcu_assign_pointer(event->tp_event->prog_array, new_array); 2013 bpf_prog_array_free_sleepable(old_array); 2014 2015 unlock: 2016 mutex_unlock(&bpf_event_mutex); 2017 return ret; 2018 } 2019 2020 void perf_event_detach_bpf_prog(struct perf_event *event) 2021 { 2022 struct bpf_prog_array *old_array; 2023 struct bpf_prog_array *new_array; 2024 struct bpf_prog *prog = NULL; 2025 int ret; 2026 2027 mutex_lock(&bpf_event_mutex); 2028 2029 if (!event->prog) 2030 goto unlock; 2031 2032 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 2033 if (!old_array) 2034 goto put; 2035 2036 ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array); 2037 if (ret < 0) { 2038 bpf_prog_array_delete_safe(old_array, event->prog); 2039 } else { 2040 rcu_assign_pointer(event->tp_event->prog_array, new_array); 2041 bpf_prog_array_free_sleepable(old_array); 2042 } 2043 2044 put: 2045 prog = event->prog; 2046 event->prog = NULL; 2047 2048 unlock: 2049 mutex_unlock(&bpf_event_mutex); 2050 2051 if (prog) { 2052 /* 2053 * It could be that the bpf_prog is not sleepable (and will be freed 2054 * via normal RCU), but is called from a point that supports sleepable 2055 * programs and uses tasks-trace-RCU. 2056 */ 2057 synchronize_rcu_tasks_trace(); 2058 2059 bpf_prog_put(prog); 2060 } 2061 } 2062 2063 int perf_event_query_prog_array(struct perf_event *event, void __user *info) 2064 { 2065 struct perf_event_query_bpf __user *uquery = info; 2066 struct perf_event_query_bpf query = {}; 2067 struct bpf_prog_array *progs; 2068 u32 *ids, prog_cnt, ids_len; 2069 int ret; 2070 2071 if (!perfmon_capable()) 2072 return -EPERM; 2073 if (event->attr.type != PERF_TYPE_TRACEPOINT) 2074 return -EINVAL; 2075 if (copy_from_user(&query, uquery, sizeof(query))) 2076 return -EFAULT; 2077 2078 ids_len = query.ids_len; 2079 if (ids_len > BPF_TRACE_MAX_PROGS) 2080 return -E2BIG; 2081 ids = kzalloc_objs(u32, ids_len, GFP_USER | __GFP_NOWARN); 2082 if (!ids) 2083 return -ENOMEM; 2084 /* 2085 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which 2086 * is required when user only wants to check for uquery->prog_cnt. 2087 * There is no need to check for it since the case is handled 2088 * gracefully in bpf_prog_array_copy_info. 2089 */ 2090 2091 mutex_lock(&bpf_event_mutex); 2092 progs = bpf_event_rcu_dereference(event->tp_event->prog_array); 2093 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); 2094 mutex_unlock(&bpf_event_mutex); 2095 2096 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || 2097 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) 2098 ret = -EFAULT; 2099 2100 kfree(ids); 2101 return ret; 2102 } 2103 2104 extern struct bpf_raw_event_map __start__bpf_raw_tp[]; 2105 extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; 2106 2107 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) 2108 { 2109 struct bpf_raw_event_map *btp = __start__bpf_raw_tp; 2110 2111 for (; btp < __stop__bpf_raw_tp; btp++) { 2112 if (!strcmp(btp->tp->name, name)) 2113 return btp; 2114 } 2115 2116 return bpf_get_raw_tracepoint_module(name); 2117 } 2118 2119 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) 2120 { 2121 struct module *mod; 2122 2123 guard(rcu)(); 2124 mod = __module_address((unsigned long)btp); 2125 module_put(mod); 2126 } 2127 2128 static __always_inline 2129 void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) 2130 { 2131 struct srcu_ctr __percpu *scp = NULL; 2132 struct bpf_prog *prog = link->link.prog; 2133 bool sleepable = prog->sleepable; 2134 struct bpf_run_ctx *old_run_ctx; 2135 struct bpf_trace_run_ctx run_ctx; 2136 2137 if (sleepable) { 2138 scp = rcu_read_lock_tasks_trace(); 2139 migrate_disable(); 2140 } else { 2141 rcu_read_lock_dont_migrate(); 2142 } 2143 2144 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 2145 bpf_prog_inc_misses_counter(prog); 2146 goto out; 2147 } 2148 2149 run_ctx.bpf_cookie = link->cookie; 2150 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 2151 2152 (void)bpf_prog_run(prog, args); 2153 2154 bpf_reset_run_ctx(old_run_ctx); 2155 out: 2156 bpf_prog_put_recursion_context(prog); 2157 2158 if (sleepable) { 2159 migrate_enable(); 2160 rcu_read_unlock_tasks_trace(scp); 2161 } else { 2162 rcu_read_unlock_migrate(); 2163 } 2164 } 2165 2166 #define UNPACK(...) __VA_ARGS__ 2167 #define REPEAT_1(FN, DL, X, ...) FN(X) 2168 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) 2169 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) 2170 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) 2171 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) 2172 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) 2173 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) 2174 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) 2175 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) 2176 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) 2177 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) 2178 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) 2179 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) 2180 2181 #define SARG(X) u64 arg##X 2182 #define COPY(X) args[X] = arg##X 2183 2184 #define __DL_COM (,) 2185 #define __DL_SEM (;) 2186 2187 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 2188 2189 #define BPF_TRACE_DEFN_x(x) \ 2190 void bpf_trace_run##x(struct bpf_raw_tp_link *link, \ 2191 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ 2192 { \ 2193 u64 args[x]; \ 2194 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ 2195 __bpf_trace_run(link, args); \ 2196 } \ 2197 EXPORT_SYMBOL_GPL(bpf_trace_run##x) 2198 BPF_TRACE_DEFN_x(1); 2199 BPF_TRACE_DEFN_x(2); 2200 BPF_TRACE_DEFN_x(3); 2201 BPF_TRACE_DEFN_x(4); 2202 BPF_TRACE_DEFN_x(5); 2203 BPF_TRACE_DEFN_x(6); 2204 BPF_TRACE_DEFN_x(7); 2205 BPF_TRACE_DEFN_x(8); 2206 BPF_TRACE_DEFN_x(9); 2207 BPF_TRACE_DEFN_x(10); 2208 BPF_TRACE_DEFN_x(11); 2209 BPF_TRACE_DEFN_x(12); 2210 2211 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link) 2212 { 2213 struct tracepoint *tp = btp->tp; 2214 struct bpf_prog *prog = link->link.prog; 2215 2216 /* 2217 * check that program doesn't access arguments beyond what's 2218 * available in this tracepoint 2219 */ 2220 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) 2221 return -EINVAL; 2222 2223 if (prog->aux->max_tp_access > btp->writable_size) 2224 return -EINVAL; 2225 2226 return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, link); 2227 } 2228 2229 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link) 2230 { 2231 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, link); 2232 } 2233 2234 /* On success the caller releases *buf with kfree_const() */ 2235 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, 2236 u32 *fd_type, const char **buf, 2237 u64 *probe_offset, u64 *probe_addr, 2238 unsigned long *missed) 2239 { 2240 bool is_tracepoint, is_syscall_tp; 2241 struct bpf_prog *prog; 2242 int flags, err = 0; 2243 2244 /* 2245 * The list of dynamic events is walked, and a probe which has siblings 2246 * can be deleted, with its strings, while its event is in use. 2247 */ 2248 guard(mutex)(&event_mutex); 2249 2250 prog = event->prog; 2251 if (!prog) 2252 return -ENOENT; 2253 2254 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ 2255 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) 2256 return -EOPNOTSUPP; 2257 2258 *prog_id = prog->aux->id; 2259 flags = event->tp_event->flags; 2260 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; 2261 is_syscall_tp = is_syscall_trace_event(event->tp_event); 2262 2263 if (is_tracepoint || is_syscall_tp) { 2264 *buf = is_tracepoint ? event->tp_event->tp->name 2265 : event->tp_event->name; 2266 /* We allow NULL pointer for tracepoint */ 2267 if (fd_type) 2268 *fd_type = BPF_FD_TYPE_TRACEPOINT; 2269 if (probe_offset) 2270 *probe_offset = 0x0; 2271 if (probe_addr) 2272 *probe_addr = 0x0; 2273 } else { 2274 /* kprobe/uprobe */ 2275 err = -EOPNOTSUPP; 2276 #ifdef CONFIG_KPROBE_EVENTS 2277 if (flags & TRACE_EVENT_FL_KPROBE) 2278 err = bpf_get_kprobe_info(event, fd_type, buf, 2279 probe_offset, probe_addr, missed, 2280 event->attr.type == PERF_TYPE_TRACEPOINT); 2281 #endif 2282 #ifdef CONFIG_UPROBE_EVENTS 2283 if (flags & TRACE_EVENT_FL_UPROBE) 2284 err = bpf_get_uprobe_info(event, fd_type, buf, 2285 probe_offset, probe_addr, 2286 event->attr.type == PERF_TYPE_TRACEPOINT); 2287 #endif 2288 } 2289 2290 if (!err && *buf) { 2291 *buf = kstrdup_const(*buf, GFP_KERNEL); 2292 if (!*buf) 2293 err = -ENOMEM; 2294 } 2295 2296 return err; 2297 } 2298 2299 static int __init send_signal_irq_work_init(void) 2300 { 2301 int cpu; 2302 struct send_signal_irq_work *work; 2303 2304 for_each_possible_cpu(cpu) { 2305 work = per_cpu_ptr(&send_signal_work, cpu); 2306 init_irq_work(&work->irq_work, do_bpf_send_signal); 2307 } 2308 return 0; 2309 } 2310 2311 subsys_initcall(send_signal_irq_work_init); 2312 2313 #ifdef CONFIG_MODULES 2314 static int bpf_event_notify(struct notifier_block *nb, unsigned long op, 2315 void *module) 2316 { 2317 struct bpf_trace_module *btm, *tmp; 2318 struct module *mod = module; 2319 int ret = 0; 2320 2321 if (mod->num_bpf_raw_events == 0 || 2322 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) 2323 goto out; 2324 2325 mutex_lock(&bpf_module_mutex); 2326 2327 switch (op) { 2328 case MODULE_STATE_COMING: 2329 btm = kzalloc_obj(*btm); 2330 if (btm) { 2331 btm->module = module; 2332 list_add(&btm->list, &bpf_trace_modules); 2333 } else { 2334 ret = -ENOMEM; 2335 } 2336 break; 2337 case MODULE_STATE_GOING: 2338 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { 2339 if (btm->module == module) { 2340 list_del(&btm->list); 2341 kfree(btm); 2342 break; 2343 } 2344 } 2345 break; 2346 } 2347 2348 mutex_unlock(&bpf_module_mutex); 2349 2350 out: 2351 return notifier_from_errno(ret); 2352 } 2353 2354 static struct notifier_block bpf_module_nb = { 2355 .notifier_call = bpf_event_notify, 2356 }; 2357 2358 static int __init bpf_event_init(void) 2359 { 2360 register_module_notifier(&bpf_module_nb); 2361 return 0; 2362 } 2363 2364 fs_initcall(bpf_event_init); 2365 #endif /* CONFIG_MODULES */ 2366 2367 struct bpf_session_run_ctx { 2368 struct bpf_run_ctx run_ctx; 2369 bool is_return; 2370 void *data; 2371 }; 2372 2373 #ifdef CONFIG_FPROBE 2374 struct bpf_kprobe_multi_link { 2375 struct bpf_link link; 2376 struct fprobe fp; 2377 unsigned long *addrs; 2378 u64 *cookies; 2379 u32 cnt; 2380 u32 mods_cnt; 2381 struct module **mods; 2382 }; 2383 2384 struct bpf_kprobe_multi_run_ctx { 2385 struct bpf_session_run_ctx session_ctx; 2386 struct bpf_kprobe_multi_link *link; 2387 unsigned long entry_ip; 2388 }; 2389 2390 struct user_syms { 2391 const char **syms; 2392 char *buf; 2393 }; 2394 2395 #ifndef CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS 2396 static DEFINE_PER_CPU(struct pt_regs, bpf_kprobe_multi_pt_regs); 2397 #define bpf_kprobe_multi_pt_regs_ptr() this_cpu_ptr(&bpf_kprobe_multi_pt_regs) 2398 #else 2399 #define bpf_kprobe_multi_pt_regs_ptr() (NULL) 2400 #endif 2401 2402 static unsigned long ftrace_get_entry_ip(unsigned long fentry_ip) 2403 { 2404 unsigned long ip = ftrace_get_symaddr(fentry_ip); 2405 2406 return ip ? : fentry_ip; 2407 } 2408 2409 static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt) 2410 { 2411 unsigned long __user usymbol; 2412 const char **syms = NULL; 2413 char *buf = NULL, *p; 2414 int err = -ENOMEM; 2415 unsigned int i; 2416 2417 if (!access_ok(usyms, cnt * sizeof(*usyms))) 2418 return -EFAULT; 2419 2420 syms = kvmalloc_objs(*syms, cnt); 2421 if (!syms) 2422 return -ENOMEM; 2423 2424 buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL); 2425 if (!buf) 2426 goto error; 2427 2428 for (p = buf, i = 0; i < cnt; i++) { 2429 if (get_user(usymbol, usyms + i)) { 2430 err = -EFAULT; 2431 goto error; 2432 } 2433 err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN); 2434 if (err == KSYM_NAME_LEN) 2435 err = -E2BIG; 2436 if (err < 0) 2437 goto error; 2438 syms[i] = p; 2439 p += err + 1; 2440 } 2441 2442 us->syms = syms; 2443 us->buf = buf; 2444 return 0; 2445 2446 error: 2447 kvfree(syms); 2448 kvfree(buf); 2449 return err; 2450 } 2451 2452 static void kprobe_multi_put_modules(struct module **mods, u32 cnt) 2453 { 2454 u32 i; 2455 2456 for (i = 0; i < cnt; i++) 2457 module_put(mods[i]); 2458 } 2459 2460 static void free_user_syms(struct user_syms *us) 2461 { 2462 kvfree(us->syms); 2463 kvfree(us->buf); 2464 } 2465 2466 static void bpf_kprobe_multi_link_release(struct bpf_link *link) 2467 { 2468 struct bpf_kprobe_multi_link *kmulti_link; 2469 2470 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); 2471 /* Don't wait for RCU GP here. */ 2472 unregister_fprobe_async(&kmulti_link->fp); 2473 kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt); 2474 } 2475 2476 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) 2477 { 2478 struct bpf_kprobe_multi_link *kmulti_link; 2479 2480 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); 2481 kvfree(kmulti_link->addrs); 2482 kvfree(kmulti_link->cookies); 2483 kfree(kmulti_link->mods); 2484 kfree(kmulti_link); 2485 } 2486 2487 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, 2488 struct bpf_link_info *info) 2489 { 2490 u64 __user *ucookies = u64_to_user_ptr(info->kprobe_multi.cookies); 2491 u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs); 2492 struct bpf_kprobe_multi_link *kmulti_link; 2493 u32 ucount = info->kprobe_multi.count; 2494 int err = 0, i; 2495 2496 if (!uaddrs ^ !ucount) 2497 return -EINVAL; 2498 if (ucookies && !ucount) 2499 return -EINVAL; 2500 2501 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); 2502 info->kprobe_multi.count = kmulti_link->cnt; 2503 info->kprobe_multi.flags = kmulti_link->link.flags; 2504 info->kprobe_multi.missed = kmulti_link->fp.nmissed; 2505 2506 if (!uaddrs) 2507 return 0; 2508 if (ucount < kmulti_link->cnt) 2509 err = -ENOSPC; 2510 else 2511 ucount = kmulti_link->cnt; 2512 2513 if (ucookies) { 2514 if (kmulti_link->cookies) { 2515 if (copy_to_user(ucookies, kmulti_link->cookies, ucount * sizeof(u64))) 2516 return -EFAULT; 2517 } else { 2518 for (i = 0; i < ucount; i++) { 2519 if (put_user(0, ucookies + i)) 2520 return -EFAULT; 2521 } 2522 } 2523 } 2524 2525 if (kallsyms_show_value(current_cred())) { 2526 if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64))) 2527 return -EFAULT; 2528 } else { 2529 for (i = 0; i < ucount; i++) { 2530 if (put_user(0, uaddrs + i)) 2531 return -EFAULT; 2532 } 2533 } 2534 return err; 2535 } 2536 2537 #ifdef CONFIG_PROC_FS 2538 static void bpf_kprobe_multi_show_fdinfo(const struct bpf_link *link, 2539 struct seq_file *seq) 2540 { 2541 struct bpf_kprobe_multi_link *kmulti_link; 2542 bool has_cookies; 2543 2544 kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); 2545 has_cookies = !!kmulti_link->cookies; 2546 2547 seq_printf(seq, 2548 "kprobe_cnt:\t%u\n" 2549 "missed:\t%lu\n", 2550 kmulti_link->cnt, 2551 kmulti_link->fp.nmissed); 2552 2553 seq_printf(seq, "%s\t %s\n", "cookie", "func"); 2554 for (int i = 0; i < kmulti_link->cnt; i++) { 2555 seq_printf(seq, 2556 "%llu\t %pS\n", 2557 has_cookies ? kmulti_link->cookies[i] : 0, 2558 (void *)kmulti_link->addrs[i]); 2559 } 2560 } 2561 #endif 2562 2563 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = { 2564 .release = bpf_kprobe_multi_link_release, 2565 .dealloc_deferred = bpf_kprobe_multi_link_dealloc, 2566 .fill_link_info = bpf_kprobe_multi_link_fill_link_info, 2567 #ifdef CONFIG_PROC_FS 2568 .show_fdinfo = bpf_kprobe_multi_show_fdinfo, 2569 #endif 2570 }; 2571 2572 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv) 2573 { 2574 const struct bpf_kprobe_multi_link *link = priv; 2575 unsigned long *addr_a = a, *addr_b = b; 2576 u64 *cookie_a, *cookie_b; 2577 2578 cookie_a = link->cookies + (addr_a - link->addrs); 2579 cookie_b = link->cookies + (addr_b - link->addrs); 2580 2581 /* swap addr_a/addr_b and cookie_a/cookie_b values */ 2582 swap(*addr_a, *addr_b); 2583 swap(*cookie_a, *cookie_b); 2584 } 2585 2586 static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b) 2587 { 2588 const unsigned long *addr_a = a, *addr_b = b; 2589 2590 if (*addr_a == *addr_b) 2591 return 0; 2592 return *addr_a < *addr_b ? -1 : 1; 2593 } 2594 2595 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv) 2596 { 2597 return bpf_kprobe_multi_addrs_cmp(a, b); 2598 } 2599 2600 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx) 2601 { 2602 struct bpf_kprobe_multi_run_ctx *run_ctx; 2603 struct bpf_kprobe_multi_link *link; 2604 u64 *cookie, entry_ip; 2605 unsigned long *addr; 2606 2607 if (WARN_ON_ONCE(!ctx)) 2608 return 0; 2609 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, 2610 session_ctx.run_ctx); 2611 link = run_ctx->link; 2612 if (!link->cookies) 2613 return 0; 2614 entry_ip = run_ctx->entry_ip; 2615 addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip), 2616 bpf_kprobe_multi_addrs_cmp); 2617 if (!addr) 2618 return 0; 2619 cookie = link->cookies + (addr - link->addrs); 2620 return *cookie; 2621 } 2622 2623 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) 2624 { 2625 struct bpf_kprobe_multi_run_ctx *run_ctx; 2626 2627 run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, 2628 session_ctx.run_ctx); 2629 return run_ctx->entry_ip; 2630 } 2631 2632 static __always_inline int 2633 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, 2634 unsigned long entry_ip, struct ftrace_regs *fregs, 2635 bool is_return, void *data) 2636 { 2637 struct bpf_kprobe_multi_run_ctx run_ctx = { 2638 .session_ctx = { 2639 .is_return = is_return, 2640 .data = data, 2641 }, 2642 .link = link, 2643 .entry_ip = entry_ip, 2644 }; 2645 struct bpf_run_ctx *old_run_ctx; 2646 struct pt_regs *regs; 2647 int err; 2648 2649 /* 2650 * graph tracer framework ensures we won't migrate, so there is no need 2651 * to use migrate_disable for bpf_prog_run again. The check here just for 2652 * __this_cpu_inc_return. 2653 */ 2654 cant_sleep(); 2655 2656 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { 2657 bpf_prog_inc_misses_counter(link->link.prog); 2658 err = 1; 2659 goto out; 2660 } 2661 2662 rcu_read_lock(); 2663 regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr()); 2664 old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx); 2665 err = bpf_prog_run(link->link.prog, regs); 2666 bpf_reset_run_ctx(old_run_ctx); 2667 ftrace_partial_regs_update(fregs, bpf_kprobe_multi_pt_regs_ptr()); 2668 rcu_read_unlock(); 2669 2670 out: 2671 __this_cpu_dec(bpf_prog_active); 2672 return err; 2673 } 2674 2675 static int 2676 kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, 2677 unsigned long ret_ip, struct ftrace_regs *fregs, 2678 void *data) 2679 { 2680 struct bpf_kprobe_multi_link *link; 2681 int err; 2682 2683 link = container_of(fp, struct bpf_kprobe_multi_link, fp); 2684 err = kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip), 2685 fregs, false, data); 2686 return is_kprobe_session(link->link.prog) ? err : 0; 2687 } 2688 2689 static void 2690 kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, 2691 unsigned long ret_ip, struct ftrace_regs *fregs, 2692 void *data) 2693 { 2694 struct bpf_kprobe_multi_link *link; 2695 2696 link = container_of(fp, struct bpf_kprobe_multi_link, fp); 2697 kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip), 2698 fregs, true, data); 2699 } 2700 2701 static int symbols_cmp_r(const void *a, const void *b, const void *priv) 2702 { 2703 const char **str_a = (const char **) a; 2704 const char **str_b = (const char **) b; 2705 2706 return strcmp(*str_a, *str_b); 2707 } 2708 2709 struct multi_symbols_sort { 2710 const char **funcs; 2711 u64 *cookies; 2712 }; 2713 2714 static void symbols_swap_r(void *a, void *b, int size, const void *priv) 2715 { 2716 const struct multi_symbols_sort *data = priv; 2717 const char **name_a = a, **name_b = b; 2718 2719 swap(*name_a, *name_b); 2720 2721 /* If defined, swap also related cookies. */ 2722 if (data->cookies) { 2723 u64 *cookie_a, *cookie_b; 2724 2725 cookie_a = data->cookies + (name_a - data->funcs); 2726 cookie_b = data->cookies + (name_b - data->funcs); 2727 swap(*cookie_a, *cookie_b); 2728 } 2729 } 2730 2731 struct modules_array { 2732 struct module **mods; 2733 int mods_cnt; 2734 int mods_cap; 2735 }; 2736 2737 static int add_module(struct modules_array *arr, struct module *mod) 2738 { 2739 struct module **mods; 2740 2741 if (arr->mods_cnt == arr->mods_cap) { 2742 arr->mods_cap = max(16, arr->mods_cap * 3 / 2); 2743 mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL); 2744 if (!mods) 2745 return -ENOMEM; 2746 arr->mods = mods; 2747 } 2748 2749 arr->mods[arr->mods_cnt] = mod; 2750 arr->mods_cnt++; 2751 return 0; 2752 } 2753 2754 static bool has_module(struct modules_array *arr, struct module *mod) 2755 { 2756 int i; 2757 2758 for (i = arr->mods_cnt - 1; i >= 0; i--) { 2759 if (arr->mods[i] == mod) 2760 return true; 2761 } 2762 return false; 2763 } 2764 2765 static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt) 2766 { 2767 struct modules_array arr = {}; 2768 u32 i, err = 0; 2769 2770 for (i = 0; i < addrs_cnt; i++) { 2771 bool skip_add = false; 2772 struct module *mod; 2773 2774 scoped_guard(rcu) { 2775 mod = __module_address(addrs[i]); 2776 /* Either no module or it's already stored */ 2777 if (!mod || has_module(&arr, mod)) { 2778 skip_add = true; 2779 break; /* scoped_guard */ 2780 } 2781 if (!try_module_get(mod)) 2782 err = -EINVAL; 2783 } 2784 if (skip_add) 2785 continue; 2786 if (err) 2787 break; 2788 err = add_module(&arr, mod); 2789 if (err) { 2790 module_put(mod); 2791 break; 2792 } 2793 } 2794 2795 /* We return either err < 0 in case of error, ... */ 2796 if (err) { 2797 kprobe_multi_put_modules(arr.mods, arr.mods_cnt); 2798 kfree(arr.mods); 2799 return err; 2800 } 2801 2802 /* or number of modules found if everything is ok. */ 2803 *mods = arr.mods; 2804 return arr.mods_cnt; 2805 } 2806 2807 static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt) 2808 { 2809 u32 i; 2810 2811 for (i = 0; i < cnt; i++) { 2812 if (!within_error_injection_list(addrs[i])) 2813 return -EINVAL; 2814 } 2815 return 0; 2816 } 2817 2818 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 2819 { 2820 struct bpf_kprobe_multi_link *link = NULL; 2821 struct bpf_link_primer link_primer; 2822 void __user *ucookies; 2823 unsigned long *addrs; 2824 u32 flags, cnt, size; 2825 void __user *uaddrs; 2826 u64 *cookies = NULL; 2827 void __user *usyms; 2828 int err; 2829 2830 /* no support for 32bit archs yet */ 2831 if (sizeof(u64) != sizeof(void *)) 2832 return -EOPNOTSUPP; 2833 2834 if (attr->link_create.flags) 2835 return -EINVAL; 2836 2837 if (!is_kprobe_multi(prog)) 2838 return -EINVAL; 2839 2840 /* kprobe_multi is not allowed to be sleepable. */ 2841 if (prog->sleepable) 2842 return -EINVAL; 2843 2844 /* Writing to context is not allowed for kprobes. */ 2845 if (prog->aux->kprobe_write_ctx) 2846 return -EINVAL; 2847 2848 flags = attr->link_create.kprobe_multi.flags; 2849 if (flags & ~BPF_F_KPROBE_MULTI_RETURN) 2850 return -EINVAL; 2851 2852 uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs); 2853 usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms); 2854 if (!!uaddrs == !!usyms) 2855 return -EINVAL; 2856 2857 cnt = attr->link_create.kprobe_multi.cnt; 2858 if (!cnt) 2859 return -EINVAL; 2860 if (cnt > MAX_KPROBE_MULTI_CNT) 2861 return -E2BIG; 2862 2863 size = cnt * sizeof(*addrs); 2864 addrs = kvmalloc_objs(*addrs, cnt); 2865 if (!addrs) 2866 return -ENOMEM; 2867 2868 ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies); 2869 if (ucookies) { 2870 cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL); 2871 if (!cookies) { 2872 err = -ENOMEM; 2873 goto error; 2874 } 2875 if (copy_from_user(cookies, ucookies, size)) { 2876 err = -EFAULT; 2877 goto error; 2878 } 2879 } 2880 2881 if (uaddrs) { 2882 if (copy_from_user(addrs, uaddrs, size)) { 2883 err = -EFAULT; 2884 goto error; 2885 } 2886 } else { 2887 struct multi_symbols_sort data = { 2888 .cookies = cookies, 2889 }; 2890 struct user_syms us; 2891 2892 err = copy_user_syms(&us, usyms, cnt); 2893 if (err) 2894 goto error; 2895 2896 if (cookies) 2897 data.funcs = us.syms; 2898 2899 sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r, 2900 symbols_swap_r, &data); 2901 2902 err = ftrace_lookup_symbols(us.syms, cnt, addrs); 2903 free_user_syms(&us); 2904 if (err) 2905 goto error; 2906 } 2907 2908 if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) { 2909 err = -EINVAL; 2910 goto error; 2911 } 2912 2913 link = kzalloc_obj(*link); 2914 if (!link) { 2915 err = -ENOMEM; 2916 goto error; 2917 } 2918 2919 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI, 2920 &bpf_kprobe_multi_link_lops, prog, attr->link_create.attach_type); 2921 2922 err = bpf_link_prime(&link->link, &link_primer); 2923 if (err) 2924 goto error; 2925 2926 if (!(flags & BPF_F_KPROBE_MULTI_RETURN)) 2927 link->fp.entry_handler = kprobe_multi_link_handler; 2928 if ((flags & BPF_F_KPROBE_MULTI_RETURN) || is_kprobe_session(prog)) 2929 link->fp.exit_handler = kprobe_multi_link_exit_handler; 2930 if (is_kprobe_session(prog)) 2931 link->fp.entry_data_size = sizeof(u64); 2932 2933 link->addrs = addrs; 2934 link->cookies = cookies; 2935 link->cnt = cnt; 2936 link->link.flags = flags; 2937 2938 if (cookies) { 2939 /* 2940 * Sorting addresses will trigger sorting cookies as well 2941 * (check bpf_kprobe_multi_cookie_swap). This way we can 2942 * find cookie based on the address in bpf_get_attach_cookie 2943 * helper. 2944 */ 2945 sort_r(addrs, cnt, sizeof(*addrs), 2946 bpf_kprobe_multi_cookie_cmp, 2947 bpf_kprobe_multi_cookie_swap, 2948 link); 2949 } 2950 2951 err = get_modules_for_addrs(&link->mods, addrs, cnt); 2952 if (err < 0) { 2953 bpf_link_cleanup(&link_primer); 2954 return err; 2955 } 2956 link->mods_cnt = err; 2957 2958 err = register_fprobe_ips(&link->fp, addrs, cnt); 2959 if (err) { 2960 kprobe_multi_put_modules(link->mods, link->mods_cnt); 2961 bpf_link_cleanup(&link_primer); 2962 return err; 2963 } 2964 2965 return bpf_link_settle(&link_primer); 2966 2967 error: 2968 kfree(link); 2969 kvfree(addrs); 2970 kvfree(cookies); 2971 return err; 2972 } 2973 #else /* !CONFIG_FPROBE */ 2974 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 2975 { 2976 return -EOPNOTSUPP; 2977 } 2978 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx) 2979 { 2980 return 0; 2981 } 2982 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) 2983 { 2984 return 0; 2985 } 2986 #endif 2987 2988 #ifdef CONFIG_UPROBES 2989 struct bpf_uprobe_multi_link; 2990 2991 struct bpf_uprobe { 2992 struct bpf_uprobe_multi_link *link; 2993 loff_t offset; 2994 unsigned long ref_ctr_offset; 2995 u64 cookie; 2996 struct uprobe *uprobe; 2997 struct uprobe_consumer consumer; 2998 bool session; 2999 }; 3000 3001 struct bpf_uprobe_multi_link { 3002 struct path path; 3003 struct bpf_link link; 3004 u32 cnt; 3005 struct bpf_uprobe *uprobes; 3006 struct task_struct *task; 3007 }; 3008 3009 struct bpf_uprobe_multi_run_ctx { 3010 struct bpf_session_run_ctx session_ctx; 3011 unsigned long entry_ip; 3012 struct bpf_uprobe *uprobe; 3013 }; 3014 3015 static void bpf_uprobe_unregister(struct bpf_uprobe *uprobes, u32 cnt) 3016 { 3017 u32 i; 3018 3019 for (i = 0; i < cnt; i++) 3020 uprobe_unregister_nosync(uprobes[i].uprobe, &uprobes[i].consumer); 3021 3022 if (cnt) 3023 uprobe_unregister_sync(); 3024 } 3025 3026 static void bpf_uprobe_multi_link_release(struct bpf_link *link) 3027 { 3028 struct bpf_uprobe_multi_link *umulti_link; 3029 3030 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); 3031 bpf_uprobe_unregister(umulti_link->uprobes, umulti_link->cnt); 3032 if (umulti_link->task) 3033 put_task_struct(umulti_link->task); 3034 path_put(&umulti_link->path); 3035 } 3036 3037 static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link) 3038 { 3039 struct bpf_uprobe_multi_link *umulti_link; 3040 3041 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); 3042 kvfree(umulti_link->uprobes); 3043 kfree(umulti_link); 3044 } 3045 3046 static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link, 3047 struct bpf_link_info *info) 3048 { 3049 u64 __user *uref_ctr_offsets = u64_to_user_ptr(info->uprobe_multi.ref_ctr_offsets); 3050 u64 __user *ucookies = u64_to_user_ptr(info->uprobe_multi.cookies); 3051 u64 __user *uoffsets = u64_to_user_ptr(info->uprobe_multi.offsets); 3052 u64 __user *upath = u64_to_user_ptr(info->uprobe_multi.path); 3053 u32 upath_size = info->uprobe_multi.path_size; 3054 struct bpf_uprobe_multi_link *umulti_link; 3055 u32 ucount = info->uprobe_multi.count; 3056 int err = 0, i; 3057 char *p, *buf; 3058 long left = 0; 3059 3060 if (!upath ^ !upath_size) 3061 return -EINVAL; 3062 3063 if ((uoffsets || uref_ctr_offsets || ucookies) && !ucount) 3064 return -EINVAL; 3065 3066 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); 3067 info->uprobe_multi.count = umulti_link->cnt; 3068 info->uprobe_multi.flags = umulti_link->link.flags; 3069 info->uprobe_multi.pid = umulti_link->task ? 3070 task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0; 3071 3072 upath_size = upath_size ? min_t(u32, upath_size, PATH_MAX) : PATH_MAX; 3073 buf = kmalloc(upath_size, GFP_KERNEL); 3074 if (!buf) 3075 return -ENOMEM; 3076 p = d_path(&umulti_link->path, buf, upath_size); 3077 if (IS_ERR(p)) { 3078 kfree(buf); 3079 return PTR_ERR(p); 3080 } 3081 upath_size = buf + upath_size - p; 3082 3083 if (upath) 3084 left = copy_to_user(upath, p, upath_size); 3085 kfree(buf); 3086 if (left) 3087 return -EFAULT; 3088 info->uprobe_multi.path_size = upath_size; 3089 3090 if (!uoffsets && !ucookies && !uref_ctr_offsets) 3091 return 0; 3092 3093 if (ucount < umulti_link->cnt) 3094 err = -ENOSPC; 3095 else 3096 ucount = umulti_link->cnt; 3097 3098 for (i = 0; i < ucount; i++) { 3099 if (uoffsets && 3100 put_user(umulti_link->uprobes[i].offset, uoffsets + i)) 3101 return -EFAULT; 3102 if (uref_ctr_offsets && 3103 put_user(umulti_link->uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) 3104 return -EFAULT; 3105 if (ucookies && 3106 put_user(umulti_link->uprobes[i].cookie, ucookies + i)) 3107 return -EFAULT; 3108 } 3109 3110 return err; 3111 } 3112 3113 #ifdef CONFIG_PROC_FS 3114 static void bpf_uprobe_multi_show_fdinfo(const struct bpf_link *link, 3115 struct seq_file *seq) 3116 { 3117 struct bpf_uprobe_multi_link *umulti_link; 3118 char *p, *buf; 3119 pid_t pid; 3120 3121 umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); 3122 3123 buf = kmalloc(PATH_MAX, GFP_KERNEL); 3124 if (!buf) 3125 return; 3126 3127 p = d_path(&umulti_link->path, buf, PATH_MAX); 3128 if (IS_ERR(p)) { 3129 kfree(buf); 3130 return; 3131 } 3132 3133 pid = umulti_link->task ? 3134 task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0; 3135 seq_printf(seq, 3136 "uprobe_cnt:\t%u\n" 3137 "pid:\t%u\n" 3138 "path:\t%s\n", 3139 umulti_link->cnt, pid, p); 3140 3141 seq_printf(seq, "%s\t %s\t %s\n", "cookie", "offset", "ref_ctr_offset"); 3142 for (int i = 0; i < umulti_link->cnt; i++) { 3143 seq_printf(seq, 3144 "%llu\t %#llx\t %#lx\n", 3145 umulti_link->uprobes[i].cookie, 3146 umulti_link->uprobes[i].offset, 3147 umulti_link->uprobes[i].ref_ctr_offset); 3148 } 3149 3150 kfree(buf); 3151 } 3152 #endif 3153 3154 static const struct bpf_link_ops bpf_uprobe_multi_link_lops = { 3155 .release = bpf_uprobe_multi_link_release, 3156 .dealloc_deferred = bpf_uprobe_multi_link_dealloc, 3157 .fill_link_info = bpf_uprobe_multi_link_fill_link_info, 3158 #ifdef CONFIG_PROC_FS 3159 .show_fdinfo = bpf_uprobe_multi_show_fdinfo, 3160 #endif 3161 }; 3162 3163 static int uprobe_prog_run(struct bpf_uprobe *uprobe, 3164 unsigned long entry_ip, 3165 struct pt_regs *regs, 3166 bool is_return, void *data) 3167 { 3168 struct bpf_uprobe_multi_link *link = uprobe->link; 3169 struct bpf_uprobe_multi_run_ctx run_ctx = { 3170 .session_ctx = { 3171 .is_return = is_return, 3172 .data = data, 3173 }, 3174 .entry_ip = entry_ip, 3175 .uprobe = uprobe, 3176 }; 3177 struct bpf_prog *prog = link->link.prog; 3178 bool sleepable = prog->sleepable; 3179 struct bpf_run_ctx *old_run_ctx; 3180 int err; 3181 3182 if (link->task && !same_thread_group(current, link->task)) 3183 return 0; 3184 3185 if (sleepable) 3186 rcu_read_lock_trace(); 3187 else 3188 rcu_read_lock(); 3189 3190 migrate_disable(); 3191 3192 old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx); 3193 err = bpf_prog_run(link->link.prog, regs); 3194 bpf_reset_run_ctx(old_run_ctx); 3195 3196 migrate_enable(); 3197 3198 if (sleepable) 3199 rcu_read_unlock_trace(); 3200 else 3201 rcu_read_unlock(); 3202 return err; 3203 } 3204 3205 static bool 3206 uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm) 3207 { 3208 struct bpf_uprobe *uprobe; 3209 3210 uprobe = container_of(con, struct bpf_uprobe, consumer); 3211 return uprobe->link->task->mm == mm; 3212 } 3213 3214 static int 3215 uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs, 3216 __u64 *data) 3217 { 3218 struct bpf_uprobe *uprobe; 3219 int ret; 3220 3221 uprobe = container_of(con, struct bpf_uprobe, consumer); 3222 ret = uprobe_prog_run(uprobe, instruction_pointer(regs), regs, false, data); 3223 if (uprobe->session) 3224 return ret ? UPROBE_HANDLER_IGNORE : 0; 3225 return 0; 3226 } 3227 3228 static int 3229 uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs, 3230 __u64 *data) 3231 { 3232 struct bpf_uprobe *uprobe; 3233 3234 uprobe = container_of(con, struct bpf_uprobe, consumer); 3235 uprobe_prog_run(uprobe, func, regs, true, data); 3236 return 0; 3237 } 3238 3239 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) 3240 { 3241 struct bpf_uprobe_multi_run_ctx *run_ctx; 3242 3243 run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, 3244 session_ctx.run_ctx); 3245 return run_ctx->entry_ip; 3246 } 3247 3248 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx) 3249 { 3250 struct bpf_uprobe_multi_run_ctx *run_ctx; 3251 3252 run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, 3253 session_ctx.run_ctx); 3254 return run_ctx->uprobe->cookie; 3255 } 3256 3257 static int bpf_uprobe_multi_get_path(const union bpf_attr *attr, struct path *path) 3258 { 3259 void __user *upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path); 3260 u32 path_fd = attr->link_create.uprobe_multi.path_fd; 3261 u32 flags = attr->link_create.uprobe_multi.flags; 3262 3263 if (flags & BPF_F_UPROBE_MULTI_PATH_FD) { 3264 /* 3265 * When BPF_F_UPROBE_MULTI_PATH_FD is set, the executable is 3266 * identified by path_fd, upath must be NULL. 3267 */ 3268 if (upath) 3269 return -EINVAL; 3270 3271 CLASS(fd, f)(path_fd); 3272 if (fd_empty(f)) 3273 return -EBADF; 3274 *path = fd_file(f)->f_path; 3275 path_get(path); 3276 return 0; 3277 } 3278 3279 /* 3280 * When BPF_F_UPROBE_MULTI_PATH_FD is not set, the path is resolved 3281 * relative to the cwd (AT_FDCWD) or absolute using the upath string. 3282 */ 3283 if (!upath || path_fd) 3284 return -EINVAL; 3285 3286 return user_path_at(AT_FDCWD, upath, LOOKUP_FOLLOW, path); 3287 } 3288 3289 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3290 { 3291 struct bpf_uprobe_multi_link *link = NULL; 3292 unsigned long __user *uref_ctr_offsets; 3293 struct bpf_link_primer link_primer; 3294 struct bpf_uprobe *uprobes = NULL; 3295 struct task_struct *task = NULL; 3296 unsigned long __user *uoffsets; 3297 u64 __user *ucookies; 3298 unsigned long size; 3299 u32 flags, cnt, i; 3300 struct path path; 3301 pid_t pid; 3302 int err; 3303 3304 /* no support for 32bit archs yet */ 3305 if (sizeof(u64) != sizeof(void *)) 3306 return -EOPNOTSUPP; 3307 3308 if (attr->link_create.flags) 3309 return -EINVAL; 3310 3311 if (!is_uprobe_multi(prog)) 3312 return -EINVAL; 3313 3314 flags = attr->link_create.uprobe_multi.flags; 3315 if (flags & ~(BPF_F_UPROBE_MULTI_RETURN | BPF_F_UPROBE_MULTI_PATH_FD)) 3316 return -EINVAL; 3317 3318 /* 3319 * offsets and cnt are mandatory, 3320 * ref_ctr_offsets and cookies are optional 3321 */ 3322 uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets); 3323 cnt = attr->link_create.uprobe_multi.cnt; 3324 pid = attr->link_create.uprobe_multi.pid; 3325 3326 if (!uoffsets || !cnt || pid < 0) 3327 return -EINVAL; 3328 if (cnt > MAX_UPROBE_MULTI_CNT) 3329 return -E2BIG; 3330 3331 uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets); 3332 ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies); 3333 3334 /* 3335 * All uoffsets/uref_ctr_offsets/ucookies arrays have the same value 3336 * size. Check their address ranges up front, so that a bad pointer 3337 * fails before anything is looked up or allocated. 3338 */ 3339 size = sizeof(*uoffsets) * cnt; 3340 if (!access_ok(uoffsets, size) || !access_ok(uref_ctr_offsets, size) || 3341 !access_ok(ucookies, size)) 3342 return -EFAULT; 3343 3344 err = bpf_uprobe_multi_get_path(attr, &path); 3345 if (err) 3346 return err; 3347 3348 if (!d_is_reg(path.dentry)) { 3349 err = -EBADF; 3350 goto error_path_put; 3351 } 3352 3353 if (pid) { 3354 rcu_read_lock(); 3355 task = get_pid_task(find_vpid(pid), PIDTYPE_TGID); 3356 rcu_read_unlock(); 3357 if (!task) { 3358 err = -ESRCH; 3359 goto error_path_put; 3360 } 3361 } 3362 3363 err = -ENOMEM; 3364 3365 link = kzalloc_obj(*link); 3366 uprobes = kvzalloc_objs(*uprobes, cnt); 3367 3368 if (!uprobes || !link) 3369 goto error_free; 3370 3371 for (i = 0; i < cnt; i++) { 3372 if (get_user(uprobes[i].offset, uoffsets + i)) { 3373 err = -EFAULT; 3374 goto error_free; 3375 } 3376 if (uprobes[i].offset < 0) { 3377 err = -EINVAL; 3378 goto error_free; 3379 } 3380 if (uref_ctr_offsets && get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) { 3381 err = -EFAULT; 3382 goto error_free; 3383 } 3384 if (ucookies && get_user(uprobes[i].cookie, ucookies + i)) { 3385 err = -EFAULT; 3386 goto error_free; 3387 } 3388 3389 uprobes[i].link = link; 3390 3391 if (!(flags & BPF_F_UPROBE_MULTI_RETURN)) 3392 uprobes[i].consumer.handler = uprobe_multi_link_handler; 3393 if (flags & BPF_F_UPROBE_MULTI_RETURN || is_uprobe_session(prog)) 3394 uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; 3395 if (is_uprobe_session(prog)) 3396 uprobes[i].session = true; 3397 if (pid) 3398 uprobes[i].consumer.filter = uprobe_multi_link_filter; 3399 } 3400 3401 link->cnt = cnt; 3402 link->uprobes = uprobes; 3403 link->path = path; 3404 link->task = task; 3405 link->link.flags = flags; 3406 3407 bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI, 3408 &bpf_uprobe_multi_link_lops, prog, attr->link_create.attach_type); 3409 3410 for (i = 0; i < cnt; i++) { 3411 uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry), 3412 uprobes[i].offset, 3413 uprobes[i].ref_ctr_offset, 3414 &uprobes[i].consumer); 3415 if (IS_ERR(uprobes[i].uprobe)) { 3416 err = PTR_ERR(uprobes[i].uprobe); 3417 link->cnt = i; 3418 goto error_unregister; 3419 } 3420 } 3421 3422 err = bpf_link_prime(&link->link, &link_primer); 3423 if (err) 3424 goto error_unregister; 3425 3426 return bpf_link_settle(&link_primer); 3427 3428 error_unregister: 3429 bpf_uprobe_unregister(uprobes, link->cnt); 3430 3431 error_free: 3432 kvfree(uprobes); 3433 kfree(link); 3434 if (task) 3435 put_task_struct(task); 3436 error_path_put: 3437 path_put(&path); 3438 return err; 3439 } 3440 #else /* !CONFIG_UPROBES */ 3441 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 3442 { 3443 return -EOPNOTSUPP; 3444 } 3445 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx) 3446 { 3447 return 0; 3448 } 3449 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) 3450 { 3451 return 0; 3452 } 3453 #endif /* CONFIG_UPROBES */ 3454 3455 __bpf_kfunc_start_defs(); 3456 3457 __bpf_kfunc bool bpf_session_is_return(void *ctx) 3458 { 3459 struct bpf_session_run_ctx *session_ctx; 3460 3461 session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx); 3462 return session_ctx->is_return; 3463 } 3464 3465 __bpf_kfunc __u64 *bpf_session_cookie(void *ctx) 3466 { 3467 struct bpf_session_run_ctx *session_ctx; 3468 3469 session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx); 3470 return session_ctx->data; 3471 } 3472 3473 __bpf_kfunc_end_defs(); 3474 3475 BTF_KFUNCS_START(session_kfunc_set_ids) 3476 BTF_ID_FLAGS(func, bpf_session_is_return) 3477 BTF_ID_FLAGS(func, bpf_session_cookie) 3478 BTF_KFUNCS_END(session_kfunc_set_ids) 3479 3480 static int bpf_session_filter(const struct bpf_prog *prog, u32 kfunc_id) 3481 { 3482 if (!btf_id_set8_contains(&session_kfunc_set_ids, kfunc_id)) 3483 return 0; 3484 3485 if (!is_kprobe_session(prog) && !is_uprobe_session(prog) && !is_trace_fsession(prog)) 3486 return -EACCES; 3487 3488 return 0; 3489 } 3490 3491 static const struct btf_kfunc_id_set bpf_session_kfunc_set = { 3492 .owner = THIS_MODULE, 3493 .set = &session_kfunc_set_ids, 3494 .filter = bpf_session_filter, 3495 }; 3496 3497 static int __init bpf_trace_kfuncs_init(void) 3498 { 3499 int err = 0; 3500 3501 err = err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_session_kfunc_set); 3502 err = err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_session_kfunc_set); 3503 3504 return err; 3505 } 3506 3507 late_initcall(bpf_trace_kfuncs_init); 3508 3509 typedef int (*copy_fn_t)(void *dst, const void *src, u32 size, struct task_struct *tsk); 3510 3511 /* 3512 * The __always_inline is to make sure the compiler doesn't 3513 * generate indirect calls into callbacks, which is expensive, 3514 * on some kernel configurations. This allows compiler to put 3515 * direct calls into all the specific callback implementations 3516 * (copy_user_data_sleepable, copy_user_data_nofault, and so on) 3517 */ 3518 static __always_inline int __bpf_dynptr_copy_str(const struct bpf_dynptr *dptr, u64 doff, u64 size, 3519 const void *unsafe_src, 3520 copy_fn_t str_copy_fn, 3521 struct task_struct *tsk) 3522 { 3523 const struct bpf_dynptr_kern *dst; 3524 u64 chunk_sz, off; 3525 void *dst_slice; 3526 int cnt, err; 3527 char buf[256]; 3528 3529 dst_slice = bpf_dynptr_slice_rdwr(dptr, doff, NULL, size); 3530 if (likely(dst_slice)) 3531 return str_copy_fn(dst_slice, unsafe_src, size, tsk); 3532 3533 dst = (struct bpf_dynptr_kern *)dptr; 3534 if (bpf_dynptr_check_off_len(dst, doff, size)) 3535 return -E2BIG; 3536 3537 for (off = 0; off < size; off += chunk_sz - 1) { 3538 chunk_sz = min_t(u64, sizeof(buf), size - off); 3539 /* Expect str_copy_fn to return count of copied bytes, including 3540 * zero terminator. Next iteration increment off by chunk_sz - 1 to 3541 * overwrite NUL. 3542 */ 3543 cnt = str_copy_fn(buf, unsafe_src + off, chunk_sz, tsk); 3544 if (cnt < 0) 3545 return cnt; 3546 err = __bpf_dynptr_write(dst, doff + off, buf, cnt, 0); 3547 if (err) 3548 return err; 3549 if (cnt < chunk_sz || chunk_sz == 1) /* we are done */ 3550 return off + cnt; 3551 } 3552 return off; 3553 } 3554 3555 static __always_inline int __bpf_dynptr_copy(const struct bpf_dynptr *dptr, u64 doff, 3556 u64 size, const void *unsafe_src, 3557 copy_fn_t copy_fn, struct task_struct *tsk) 3558 { 3559 const struct bpf_dynptr_kern *dst; 3560 void *dst_slice; 3561 char buf[256]; 3562 u64 off, chunk_sz; 3563 int err; 3564 3565 dst_slice = bpf_dynptr_slice_rdwr(dptr, doff, NULL, size); 3566 if (likely(dst_slice)) 3567 return copy_fn(dst_slice, unsafe_src, size, tsk); 3568 3569 dst = (struct bpf_dynptr_kern *)dptr; 3570 if (bpf_dynptr_check_off_len(dst, doff, size)) 3571 return -E2BIG; 3572 3573 for (off = 0; off < size; off += chunk_sz) { 3574 chunk_sz = min_t(u64, sizeof(buf), size - off); 3575 err = copy_fn(buf, unsafe_src + off, chunk_sz, tsk); 3576 if (err) 3577 return err; 3578 err = __bpf_dynptr_write(dst, doff + off, buf, chunk_sz, 0); 3579 if (err) 3580 return err; 3581 } 3582 return 0; 3583 } 3584 3585 static __always_inline int copy_user_data_nofault(void *dst, const void *unsafe_src, 3586 u32 size, struct task_struct *tsk) 3587 { 3588 return copy_from_user_nofault(dst, (const void __user *)unsafe_src, size); 3589 } 3590 3591 static __always_inline int copy_user_data_sleepable(void *dst, const void *unsafe_src, 3592 u32 size, struct task_struct *tsk) 3593 { 3594 int ret; 3595 3596 if (!tsk) { /* Read from the current task */ 3597 ret = copy_from_user(dst, (const void __user *)unsafe_src, size); 3598 if (ret) 3599 return -EFAULT; 3600 return 0; 3601 } 3602 3603 ret = access_process_vm(tsk, (unsigned long)unsafe_src, dst, size, 0); 3604 if (ret != size) 3605 return -EFAULT; 3606 return 0; 3607 } 3608 3609 static __always_inline int copy_kernel_data_nofault(void *dst, const void *unsafe_src, 3610 u32 size, struct task_struct *tsk) 3611 { 3612 return copy_from_kernel_nofault(dst, unsafe_src, size); 3613 } 3614 3615 static __always_inline int copy_user_str_nofault(void *dst, const void *unsafe_src, 3616 u32 size, struct task_struct *tsk) 3617 { 3618 return strncpy_from_user_nofault(dst, (const void __user *)unsafe_src, size); 3619 } 3620 3621 static __always_inline int copy_user_str_sleepable(void *dst, const void *unsafe_src, 3622 u32 size, struct task_struct *tsk) 3623 { 3624 int ret; 3625 3626 if (unlikely(size == 0)) 3627 return 0; 3628 3629 if (tsk) { 3630 ret = copy_remote_vm_str(tsk, (unsigned long)unsafe_src, dst, size, 0); 3631 } else { 3632 ret = strncpy_from_user(dst, (const void __user *)unsafe_src, size - 1); 3633 /* strncpy_from_user does not guarantee NUL termination */ 3634 if (ret >= 0) 3635 ((char *)dst)[ret] = '\0'; 3636 } 3637 3638 if (ret < 0) 3639 return ret; 3640 return ret + 1; 3641 } 3642 3643 static __always_inline int copy_kernel_str_nofault(void *dst, const void *unsafe_src, 3644 u32 size, struct task_struct *tsk) 3645 { 3646 return strncpy_from_kernel_nofault(dst, unsafe_src, size); 3647 } 3648 3649 __bpf_kfunc_start_defs(); 3650 3651 __bpf_kfunc int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type, 3652 u64 value) 3653 { 3654 if (type != PIDTYPE_PID && type != PIDTYPE_TGID) 3655 return -EINVAL; 3656 3657 return bpf_send_signal_common(sig, type, task, value); 3658 } 3659 3660 __bpf_kfunc int bpf_probe_read_user_dynptr(const struct bpf_dynptr *dptr, u64 off, 3661 u64 size, const void __user *unsafe_ptr__ign) 3662 { 3663 return __bpf_dynptr_copy(dptr, off, size, (const void __force *)unsafe_ptr__ign, 3664 copy_user_data_nofault, NULL); 3665 } 3666 3667 __bpf_kfunc int bpf_probe_read_kernel_dynptr(const struct bpf_dynptr *dptr, u64 off, 3668 u64 size, const void *unsafe_ptr__ign) 3669 { 3670 return __bpf_dynptr_copy(dptr, off, size, unsafe_ptr__ign, 3671 copy_kernel_data_nofault, NULL); 3672 } 3673 3674 __bpf_kfunc int bpf_probe_read_user_str_dynptr(const struct bpf_dynptr *dptr, u64 off, 3675 u64 size, const void __user *unsafe_ptr__ign) 3676 { 3677 return __bpf_dynptr_copy_str(dptr, off, size, (const void __force *)unsafe_ptr__ign, 3678 copy_user_str_nofault, NULL); 3679 } 3680 3681 __bpf_kfunc int bpf_probe_read_kernel_str_dynptr(const struct bpf_dynptr *dptr, u64 off, 3682 u64 size, const void *unsafe_ptr__ign) 3683 { 3684 return __bpf_dynptr_copy_str(dptr, off, size, unsafe_ptr__ign, 3685 copy_kernel_str_nofault, NULL); 3686 } 3687 3688 __bpf_kfunc int bpf_copy_from_user_dynptr(const struct bpf_dynptr *dptr, u64 off, 3689 u64 size, const void __user *unsafe_ptr__ign) 3690 { 3691 return __bpf_dynptr_copy(dptr, off, size, (const void __force *)unsafe_ptr__ign, 3692 copy_user_data_sleepable, NULL); 3693 } 3694 3695 __bpf_kfunc int bpf_copy_from_user_str_dynptr(const struct bpf_dynptr *dptr, u64 off, 3696 u64 size, const void __user *unsafe_ptr__ign) 3697 { 3698 return __bpf_dynptr_copy_str(dptr, off, size, (const void __force *)unsafe_ptr__ign, 3699 copy_user_str_sleepable, NULL); 3700 } 3701 3702 __bpf_kfunc int bpf_copy_from_user_task_dynptr(const struct bpf_dynptr *dptr, u64 off, 3703 u64 size, const void __user *unsafe_ptr__ign, 3704 struct task_struct *tsk) 3705 { 3706 return __bpf_dynptr_copy(dptr, off, size, (const void __force *)unsafe_ptr__ign, 3707 copy_user_data_sleepable, tsk); 3708 } 3709 3710 __bpf_kfunc int bpf_copy_from_user_task_str_dynptr(const struct bpf_dynptr *dptr, u64 off, 3711 u64 size, const void __user *unsafe_ptr__ign, 3712 struct task_struct *tsk) 3713 { 3714 return __bpf_dynptr_copy_str(dptr, off, size, (const void __force *)unsafe_ptr__ign, 3715 copy_user_str_sleepable, tsk); 3716 } 3717 3718 __bpf_kfunc_end_defs(); 3719 3720 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) && \ 3721 defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) 3722 3723 static void bpf_tracing_multi_link_release(struct bpf_link *link) 3724 { 3725 struct bpf_tracing_multi_link *tr_link = 3726 container_of(link, struct bpf_tracing_multi_link, link); 3727 3728 bpf_trampoline_multi_detach(link->prog, tr_link); 3729 } 3730 3731 static void bpf_tracing_multi_link_dealloc(struct bpf_link *link) 3732 { 3733 struct bpf_tracing_multi_link *tr_link = 3734 container_of(link, struct bpf_tracing_multi_link, link); 3735 3736 kvfree(tr_link->fexits); 3737 kvfree(tr_link->cookies); 3738 kvfree(tr_link); 3739 } 3740 3741 static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, 3742 struct bpf_link_info *info) 3743 { 3744 u64 __user *ucookies = u64_to_user_ptr(info->tracing_multi.cookies); 3745 u64 __user *uaddrs = u64_to_user_ptr(info->tracing_multi.addrs); 3746 u32 __user *uids = u64_to_user_ptr(info->tracing_multi.ids); 3747 struct bpf_tracing_multi_link *tr_link; 3748 u32 ucount = info->tracing_multi.count; 3749 bool has_cookies, show_addrs; 3750 int err = 0; 3751 3752 if ((uids || ucookies || uaddrs) && !ucount) 3753 return -EINVAL; 3754 3755 tr_link = container_of(link, struct bpf_tracing_multi_link, link); 3756 3757 info->tracing_multi.attach_type = tr_link->link.attach_type; 3758 info->tracing_multi.count = tr_link->nodes_cnt; 3759 info->tracing_multi.btf_obj_id = btf_obj_id(tr_link->link.prog->aux->attach_btf); 3760 3761 if (!uids && !ucookies && !uaddrs) 3762 return 0; 3763 3764 if (ucount < tr_link->nodes_cnt) 3765 err = -ENOSPC; 3766 else 3767 ucount = tr_link->nodes_cnt; 3768 3769 has_cookies = !!tr_link->cookies; 3770 show_addrs = kallsyms_show_value(current_cred()); 3771 3772 for (int i = 0; i < ucount; i++) { 3773 struct bpf_tracing_multi_node *mnode = &tr_link->nodes[i]; 3774 u64 addr, cookie; 3775 u32 id; 3776 3777 bpf_trampoline_unpack_key(mnode->trampoline->key, NULL, &id); 3778 3779 addr = show_addrs ? mnode->trampoline->ip : 0; 3780 cookie = has_cookies ? tr_link->cookies[i] : 0; 3781 3782 if (uids && put_user(id, uids + i)) 3783 return -EFAULT; 3784 if (uaddrs && put_user(addr, uaddrs + i)) 3785 return -EFAULT; 3786 if (ucookies && put_user(cookie, ucookies + i)) 3787 return -EFAULT; 3788 3789 cond_resched(); 3790 } 3791 3792 return err; 3793 } 3794 3795 #ifdef CONFIG_PROC_FS 3796 static void bpf_tracing_multi_show_fdinfo(const struct bpf_link *link, 3797 struct seq_file *seq) 3798 { 3799 struct bpf_tracing_multi_link *tr_link = 3800 container_of(link, struct bpf_tracing_multi_link, link); 3801 bool has_cookies = !!tr_link->cookies; 3802 3803 seq_printf(seq, "attach_type:\t%u\n", tr_link->link.attach_type); 3804 seq_printf(seq, "cnt:\t%u\n", tr_link->nodes_cnt); 3805 3806 seq_printf(seq, "%s\t %s\t %s\t %s\n", "obj-id", "btf-id", "cookie", "func"); 3807 for (int i = 0; i < tr_link->nodes_cnt; i++) { 3808 struct bpf_tracing_multi_node *mnode = &tr_link->nodes[i]; 3809 u32 btf_id, obj_id; 3810 3811 bpf_trampoline_unpack_key(mnode->trampoline->key, &obj_id, &btf_id); 3812 seq_printf(seq, "%u\t %u\t %llu\t %pS\n", 3813 obj_id, btf_id, 3814 has_cookies ? tr_link->cookies[i] : 0, 3815 (void *) mnode->trampoline->ip); 3816 3817 cond_resched(); 3818 } 3819 } 3820 #endif 3821 3822 static const struct bpf_link_ops bpf_tracing_multi_link_lops = { 3823 .release = bpf_tracing_multi_link_release, 3824 .dealloc_deferred = bpf_tracing_multi_link_dealloc, 3825 .fill_link_info = bpf_tracing_multi_link_fill_link_info, 3826 #ifdef CONFIG_PROC_FS 3827 .show_fdinfo = bpf_tracing_multi_show_fdinfo, 3828 #endif 3829 }; 3830 3831 static int ids_cmp_r(const void *pa, const void *pb, const void *priv __maybe_unused) 3832 { 3833 u32 a = *(u32 *) pa; 3834 u32 b = *(u32 *) pb; 3835 3836 return (a > b) - (a < b); 3837 } 3838 3839 static void ids_swap_r(void *a, void *b, int size __maybe_unused, 3840 const void *priv __maybe_unused) 3841 { 3842 u64 *cookie_a, *cookie_b, *cookies; 3843 u32 *id_a = a, *id_b = b, *ids; 3844 void **data = (void **) priv; 3845 3846 ids = data[0]; 3847 cookies = data[1]; 3848 3849 if (cookies) { 3850 cookie_a = cookies + (id_a - ids); 3851 cookie_b = cookies + (id_b - ids); 3852 swap(*cookie_a, *cookie_b); 3853 } 3854 swap(*id_a, *id_b); 3855 } 3856 3857 static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt) 3858 { 3859 void *data[2] = { ids, cookies }; 3860 int err = 0; 3861 3862 /* 3863 * Sort ids array (together with cookies array if defined) 3864 * and check it for duplicates. The ids and cookies arrays 3865 * are left sorted. 3866 */ 3867 sort_r_nonatomic(ids, cnt, sizeof(ids[0]), ids_cmp_r, ids_swap_r, data); 3868 3869 for (int i = 1; i < cnt; i++) { 3870 if (ids[i] == ids[i - 1]) { 3871 err = -EINVAL; 3872 break; 3873 } 3874 } 3875 return err; 3876 } 3877 3878 int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr) 3879 { 3880 struct bpf_tracing_multi_link *link = NULL; 3881 struct bpf_tramp_node *fexits = NULL; 3882 struct bpf_link_primer link_primer; 3883 u32 cnt, *ids = NULL; 3884 u64 __user *ucookies; 3885 u64 *cookies = NULL; 3886 u32 __user *uids; 3887 int err; 3888 3889 uids = u64_to_user_ptr(attr->link_create.tracing_multi.ids); 3890 cnt = attr->link_create.tracing_multi.cnt; 3891 3892 if (!cnt || !uids) 3893 return -EINVAL; 3894 if (cnt > MAX_TRACING_MULTI_CNT) 3895 return -E2BIG; 3896 if (attr->link_create.flags || attr->link_create.target_fd) 3897 return -EINVAL; 3898 3899 ids = kvmalloc_objs(*ids, cnt); 3900 if (!ids) 3901 return -ENOMEM; 3902 3903 if (copy_from_user(ids, uids, cnt * sizeof(*ids))) { 3904 err = -EFAULT; 3905 goto error; 3906 } 3907 3908 ucookies = u64_to_user_ptr(attr->link_create.tracing_multi.cookies); 3909 if (ucookies) { 3910 cookies = kvmalloc_objs(*cookies, cnt); 3911 if (!cookies) { 3912 err = -ENOMEM; 3913 goto error; 3914 } 3915 if (copy_from_user(cookies, ucookies, cnt * sizeof(*cookies))) { 3916 err = -EFAULT; 3917 goto error; 3918 } 3919 } 3920 3921 err = check_dup_ids(ids, cookies, cnt); 3922 if (err) 3923 goto error; 3924 3925 if (prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI) { 3926 fexits = kvmalloc_objs(*fexits, cnt); 3927 if (!fexits) { 3928 err = -ENOMEM; 3929 goto error; 3930 } 3931 } 3932 3933 link = kvzalloc_flex(*link, nodes, cnt); 3934 if (!link) { 3935 err = -ENOMEM; 3936 goto error; 3937 } 3938 3939 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING_MULTI, 3940 &bpf_tracing_multi_link_lops, prog, prog->expected_attach_type); 3941 3942 err = bpf_link_prime(&link->link, &link_primer); 3943 if (err) 3944 goto error; 3945 3946 link->nodes_cnt = cnt; 3947 link->cookies = cookies; 3948 link->fexits = fexits; 3949 3950 err = bpf_trampoline_multi_attach(prog, ids, link); 3951 kvfree(ids); 3952 if (err) { 3953 bpf_link_cleanup(&link_primer); 3954 return err; 3955 } 3956 return bpf_link_settle(&link_primer); 3957 3958 error: 3959 kvfree(fexits); 3960 kvfree(cookies); 3961 kvfree(ids); 3962 kvfree(link); 3963 return err; 3964 } 3965 3966 #else 3967 3968 int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr) 3969 { 3970 return -EOPNOTSUPP; 3971 } 3972 3973 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS && CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */ 3974