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