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