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_perf_event.h> 10 #include <linux/filter.h> 11 #include <linux/uaccess.h> 12 #include <linux/ctype.h> 13 #include <linux/kprobes.h> 14 #include <linux/syscalls.h> 15 #include <linux/error-injection.h> 16 17 #include <asm/tlb.h> 18 19 #include "trace_probe.h" 20 #include "trace.h" 21 22 #define bpf_event_rcu_dereference(p) \ 23 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) 24 25 #ifdef CONFIG_MODULES 26 struct bpf_trace_module { 27 struct module *module; 28 struct list_head list; 29 }; 30 31 static LIST_HEAD(bpf_trace_modules); 32 static DEFINE_MUTEX(bpf_module_mutex); 33 34 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) 35 { 36 struct bpf_raw_event_map *btp, *ret = NULL; 37 struct bpf_trace_module *btm; 38 unsigned int i; 39 40 mutex_lock(&bpf_module_mutex); 41 list_for_each_entry(btm, &bpf_trace_modules, list) { 42 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { 43 btp = &btm->module->bpf_raw_events[i]; 44 if (!strcmp(btp->tp->name, name)) { 45 if (try_module_get(btm->module)) 46 ret = btp; 47 goto out; 48 } 49 } 50 } 51 out: 52 mutex_unlock(&bpf_module_mutex); 53 return ret; 54 } 55 #else 56 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) 57 { 58 return NULL; 59 } 60 #endif /* CONFIG_MODULES */ 61 62 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 63 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 64 65 /** 66 * trace_call_bpf - invoke BPF program 67 * @call: tracepoint event 68 * @ctx: opaque context pointer 69 * 70 * kprobe handlers execute BPF programs via this helper. 71 * Can be used from static tracepoints in the future. 72 * 73 * Return: BPF programs always return an integer which is interpreted by 74 * kprobe handler as: 75 * 0 - return from kprobe (event is filtered out) 76 * 1 - store kprobe event into ring buffer 77 * Other values are reserved and currently alias to 1 78 */ 79 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) 80 { 81 unsigned int ret; 82 83 if (in_nmi()) /* not supported yet */ 84 return 1; 85 86 cant_sleep(); 87 88 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { 89 /* 90 * since some bpf program is already running on this cpu, 91 * don't call into another bpf program (same or different) 92 * and don't send kprobe event into ring-buffer, 93 * so return zero here 94 */ 95 ret = 0; 96 goto out; 97 } 98 99 /* 100 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock 101 * to all call sites, we did a bpf_prog_array_valid() there to check 102 * whether call->prog_array is empty or not, which is 103 * a heurisitc to speed up execution. 104 * 105 * If bpf_prog_array_valid() fetched prog_array was 106 * non-NULL, we go into trace_call_bpf() and do the actual 107 * proper rcu_dereference() under RCU lock. 108 * If it turns out that prog_array is NULL then, we bail out. 109 * For the opposite, if the bpf_prog_array_valid() fetched pointer 110 * was NULL, you'll skip the prog_array with the risk of missing 111 * out of events when it was updated in between this and the 112 * rcu_dereference() which is accepted risk. 113 */ 114 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN); 115 116 out: 117 __this_cpu_dec(bpf_prog_active); 118 119 return ret; 120 } 121 122 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 123 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) 124 { 125 regs_set_return_value(regs, rc); 126 override_function_with_return(regs); 127 return 0; 128 } 129 130 static const struct bpf_func_proto bpf_override_return_proto = { 131 .func = bpf_override_return, 132 .gpl_only = true, 133 .ret_type = RET_INTEGER, 134 .arg1_type = ARG_PTR_TO_CTX, 135 .arg2_type = ARG_ANYTHING, 136 }; 137 #endif 138 139 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, 140 const void __user *, unsafe_ptr) 141 { 142 int ret = probe_user_read(dst, unsafe_ptr, size); 143 144 if (unlikely(ret < 0)) 145 memset(dst, 0, size); 146 147 return ret; 148 } 149 150 static const struct bpf_func_proto bpf_probe_read_user_proto = { 151 .func = bpf_probe_read_user, 152 .gpl_only = true, 153 .ret_type = RET_INTEGER, 154 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 155 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 156 .arg3_type = ARG_ANYTHING, 157 }; 158 159 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size, 160 const void __user *, unsafe_ptr) 161 { 162 int ret = strncpy_from_unsafe_user(dst, unsafe_ptr, size); 163 164 if (unlikely(ret < 0)) 165 memset(dst, 0, size); 166 167 return ret; 168 } 169 170 static const struct bpf_func_proto bpf_probe_read_user_str_proto = { 171 .func = bpf_probe_read_user_str, 172 .gpl_only = true, 173 .ret_type = RET_INTEGER, 174 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 175 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 176 .arg3_type = ARG_ANYTHING, 177 }; 178 179 static __always_inline int 180 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr, 181 const bool compat) 182 { 183 int ret = security_locked_down(LOCKDOWN_BPF_READ); 184 185 if (unlikely(ret < 0)) 186 goto out; 187 ret = compat ? probe_kernel_read(dst, unsafe_ptr, size) : 188 probe_kernel_read_strict(dst, unsafe_ptr, size); 189 if (unlikely(ret < 0)) 190 out: 191 memset(dst, 0, size); 192 return ret; 193 } 194 195 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size, 196 const void *, unsafe_ptr) 197 { 198 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false); 199 } 200 201 static const struct bpf_func_proto bpf_probe_read_kernel_proto = { 202 .func = bpf_probe_read_kernel, 203 .gpl_only = true, 204 .ret_type = RET_INTEGER, 205 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 206 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 207 .arg3_type = ARG_ANYTHING, 208 }; 209 210 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size, 211 const void *, unsafe_ptr) 212 { 213 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true); 214 } 215 216 static const struct bpf_func_proto bpf_probe_read_compat_proto = { 217 .func = bpf_probe_read_compat, 218 .gpl_only = true, 219 .ret_type = RET_INTEGER, 220 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 221 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 222 .arg3_type = ARG_ANYTHING, 223 }; 224 225 static __always_inline int 226 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr, 227 const bool compat) 228 { 229 int ret = security_locked_down(LOCKDOWN_BPF_READ); 230 231 if (unlikely(ret < 0)) 232 goto out; 233 /* 234 * The strncpy_from_unsafe_*() call will likely not fill the entire 235 * buffer, but that's okay in this circumstance as we're probing 236 * arbitrary memory anyway similar to bpf_probe_read_*() and might 237 * as well probe the stack. Thus, memory is explicitly cleared 238 * only in error case, so that improper users ignoring return 239 * code altogether don't copy garbage; otherwise length of string 240 * is returned that can be used for bpf_perf_event_output() et al. 241 */ 242 ret = compat ? strncpy_from_unsafe(dst, unsafe_ptr, size) : 243 strncpy_from_unsafe_strict(dst, unsafe_ptr, size); 244 if (unlikely(ret < 0)) 245 out: 246 memset(dst, 0, size); 247 return ret; 248 } 249 250 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size, 251 const void *, unsafe_ptr) 252 { 253 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false); 254 } 255 256 static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = { 257 .func = bpf_probe_read_kernel_str, 258 .gpl_only = true, 259 .ret_type = RET_INTEGER, 260 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 261 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 262 .arg3_type = ARG_ANYTHING, 263 }; 264 265 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size, 266 const void *, unsafe_ptr) 267 { 268 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true); 269 } 270 271 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = { 272 .func = bpf_probe_read_compat_str, 273 .gpl_only = true, 274 .ret_type = RET_INTEGER, 275 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 276 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 277 .arg3_type = ARG_ANYTHING, 278 }; 279 280 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src, 281 u32, size) 282 { 283 /* 284 * Ensure we're in user context which is safe for the helper to 285 * run. This helper has no business in a kthread. 286 * 287 * access_ok() should prevent writing to non-user memory, but in 288 * some situations (nommu, temporary switch, etc) access_ok() does 289 * not provide enough validation, hence the check on KERNEL_DS. 290 * 291 * nmi_uaccess_okay() ensures the probe is not run in an interim 292 * state, when the task or mm are switched. This is specifically 293 * required to prevent the use of temporary mm. 294 */ 295 296 if (unlikely(in_interrupt() || 297 current->flags & (PF_KTHREAD | PF_EXITING))) 298 return -EPERM; 299 if (unlikely(uaccess_kernel())) 300 return -EPERM; 301 if (unlikely(!nmi_uaccess_okay())) 302 return -EPERM; 303 304 return probe_user_write(unsafe_ptr, src, size); 305 } 306 307 static const struct bpf_func_proto bpf_probe_write_user_proto = { 308 .func = bpf_probe_write_user, 309 .gpl_only = true, 310 .ret_type = RET_INTEGER, 311 .arg1_type = ARG_ANYTHING, 312 .arg2_type = ARG_PTR_TO_MEM, 313 .arg3_type = ARG_CONST_SIZE, 314 }; 315 316 static const struct bpf_func_proto *bpf_get_probe_write_proto(void) 317 { 318 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", 319 current->comm, task_pid_nr(current)); 320 321 return &bpf_probe_write_user_proto; 322 } 323 324 /* 325 * Only limited trace_printk() conversion specifiers allowed: 326 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s 327 */ 328 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, 329 u64, arg2, u64, arg3) 330 { 331 bool str_seen = false; 332 int mod[3] = {}; 333 int fmt_cnt = 0; 334 u64 unsafe_addr; 335 char buf[64]; 336 int i; 337 338 /* 339 * bpf_check()->check_func_arg()->check_stack_boundary() 340 * guarantees that fmt points to bpf program stack, 341 * fmt_size bytes of it were initialized and fmt_size > 0 342 */ 343 if (fmt[--fmt_size] != 0) 344 return -EINVAL; 345 346 /* check format string for allowed specifiers */ 347 for (i = 0; i < fmt_size; i++) { 348 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) 349 return -EINVAL; 350 351 if (fmt[i] != '%') 352 continue; 353 354 if (fmt_cnt >= 3) 355 return -EINVAL; 356 357 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ 358 i++; 359 if (fmt[i] == 'l') { 360 mod[fmt_cnt]++; 361 i++; 362 } else if (fmt[i] == 'p' || fmt[i] == 's') { 363 mod[fmt_cnt]++; 364 /* disallow any further format extensions */ 365 if (fmt[i + 1] != 0 && 366 !isspace(fmt[i + 1]) && 367 !ispunct(fmt[i + 1])) 368 return -EINVAL; 369 fmt_cnt++; 370 if (fmt[i] == 's') { 371 if (str_seen) 372 /* allow only one '%s' per fmt string */ 373 return -EINVAL; 374 str_seen = true; 375 376 switch (fmt_cnt) { 377 case 1: 378 unsafe_addr = arg1; 379 arg1 = (long) buf; 380 break; 381 case 2: 382 unsafe_addr = arg2; 383 arg2 = (long) buf; 384 break; 385 case 3: 386 unsafe_addr = arg3; 387 arg3 = (long) buf; 388 break; 389 } 390 buf[0] = 0; 391 strncpy_from_unsafe(buf, 392 (void *) (long) unsafe_addr, 393 sizeof(buf)); 394 } 395 continue; 396 } 397 398 if (fmt[i] == 'l') { 399 mod[fmt_cnt]++; 400 i++; 401 } 402 403 if (fmt[i] != 'i' && fmt[i] != 'd' && 404 fmt[i] != 'u' && fmt[i] != 'x') 405 return -EINVAL; 406 fmt_cnt++; 407 } 408 409 /* Horrid workaround for getting va_list handling working with different 410 * argument type combinations generically for 32 and 64 bit archs. 411 */ 412 #define __BPF_TP_EMIT() __BPF_ARG3_TP() 413 #define __BPF_TP(...) \ 414 __trace_printk(0 /* Fake ip */, \ 415 fmt, ##__VA_ARGS__) 416 417 #define __BPF_ARG1_TP(...) \ 418 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ 419 ? __BPF_TP(arg1, ##__VA_ARGS__) \ 420 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ 421 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ 422 : __BPF_TP((u32)arg1, ##__VA_ARGS__))) 423 424 #define __BPF_ARG2_TP(...) \ 425 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ 426 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ 427 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ 428 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ 429 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) 430 431 #define __BPF_ARG3_TP(...) \ 432 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ 433 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ 434 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ 435 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ 436 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) 437 438 return __BPF_TP_EMIT(); 439 } 440 441 static const struct bpf_func_proto bpf_trace_printk_proto = { 442 .func = bpf_trace_printk, 443 .gpl_only = true, 444 .ret_type = RET_INTEGER, 445 .arg1_type = ARG_PTR_TO_MEM, 446 .arg2_type = ARG_CONST_SIZE, 447 }; 448 449 const struct bpf_func_proto *bpf_get_trace_printk_proto(void) 450 { 451 /* 452 * this program might be calling bpf_trace_printk, 453 * so allocate per-cpu printk buffers 454 */ 455 trace_printk_init_buffers(); 456 457 return &bpf_trace_printk_proto; 458 } 459 460 static __always_inline int 461 get_map_perf_counter(struct bpf_map *map, u64 flags, 462 u64 *value, u64 *enabled, u64 *running) 463 { 464 struct bpf_array *array = container_of(map, struct bpf_array, map); 465 unsigned int cpu = smp_processor_id(); 466 u64 index = flags & BPF_F_INDEX_MASK; 467 struct bpf_event_entry *ee; 468 469 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) 470 return -EINVAL; 471 if (index == BPF_F_CURRENT_CPU) 472 index = cpu; 473 if (unlikely(index >= array->map.max_entries)) 474 return -E2BIG; 475 476 ee = READ_ONCE(array->ptrs[index]); 477 if (!ee) 478 return -ENOENT; 479 480 return perf_event_read_local(ee->event, value, enabled, running); 481 } 482 483 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) 484 { 485 u64 value = 0; 486 int err; 487 488 err = get_map_perf_counter(map, flags, &value, NULL, NULL); 489 /* 490 * this api is ugly since we miss [-22..-2] range of valid 491 * counter values, but that's uapi 492 */ 493 if (err) 494 return err; 495 return value; 496 } 497 498 static const struct bpf_func_proto bpf_perf_event_read_proto = { 499 .func = bpf_perf_event_read, 500 .gpl_only = true, 501 .ret_type = RET_INTEGER, 502 .arg1_type = ARG_CONST_MAP_PTR, 503 .arg2_type = ARG_ANYTHING, 504 }; 505 506 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, 507 struct bpf_perf_event_value *, buf, u32, size) 508 { 509 int err = -EINVAL; 510 511 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 512 goto clear; 513 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, 514 &buf->running); 515 if (unlikely(err)) 516 goto clear; 517 return 0; 518 clear: 519 memset(buf, 0, size); 520 return err; 521 } 522 523 static const struct bpf_func_proto bpf_perf_event_read_value_proto = { 524 .func = bpf_perf_event_read_value, 525 .gpl_only = true, 526 .ret_type = RET_INTEGER, 527 .arg1_type = ARG_CONST_MAP_PTR, 528 .arg2_type = ARG_ANYTHING, 529 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 530 .arg4_type = ARG_CONST_SIZE, 531 }; 532 533 static __always_inline u64 534 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, 535 u64 flags, struct perf_sample_data *sd) 536 { 537 struct bpf_array *array = container_of(map, struct bpf_array, map); 538 unsigned int cpu = smp_processor_id(); 539 u64 index = flags & BPF_F_INDEX_MASK; 540 struct bpf_event_entry *ee; 541 struct perf_event *event; 542 543 if (index == BPF_F_CURRENT_CPU) 544 index = cpu; 545 if (unlikely(index >= array->map.max_entries)) 546 return -E2BIG; 547 548 ee = READ_ONCE(array->ptrs[index]); 549 if (!ee) 550 return -ENOENT; 551 552 event = ee->event; 553 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || 554 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) 555 return -EINVAL; 556 557 if (unlikely(event->oncpu != cpu)) 558 return -EOPNOTSUPP; 559 560 return perf_event_output(event, sd, regs); 561 } 562 563 /* 564 * Support executing tracepoints in normal, irq, and nmi context that each call 565 * bpf_perf_event_output 566 */ 567 struct bpf_trace_sample_data { 568 struct perf_sample_data sds[3]; 569 }; 570 571 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds); 572 static DEFINE_PER_CPU(int, bpf_trace_nest_level); 573 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, 574 u64, flags, void *, data, u64, size) 575 { 576 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds); 577 int nest_level = this_cpu_inc_return(bpf_trace_nest_level); 578 struct perf_raw_record raw = { 579 .frag = { 580 .size = size, 581 .data = data, 582 }, 583 }; 584 struct perf_sample_data *sd; 585 int err; 586 587 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) { 588 err = -EBUSY; 589 goto out; 590 } 591 592 sd = &sds->sds[nest_level - 1]; 593 594 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) { 595 err = -EINVAL; 596 goto out; 597 } 598 599 perf_sample_data_init(sd, 0, 0); 600 sd->raw = &raw; 601 602 err = __bpf_perf_event_output(regs, map, flags, sd); 603 604 out: 605 this_cpu_dec(bpf_trace_nest_level); 606 return err; 607 } 608 609 static const struct bpf_func_proto bpf_perf_event_output_proto = { 610 .func = bpf_perf_event_output, 611 .gpl_only = true, 612 .ret_type = RET_INTEGER, 613 .arg1_type = ARG_PTR_TO_CTX, 614 .arg2_type = ARG_CONST_MAP_PTR, 615 .arg3_type = ARG_ANYTHING, 616 .arg4_type = ARG_PTR_TO_MEM, 617 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 618 }; 619 620 static DEFINE_PER_CPU(int, bpf_event_output_nest_level); 621 struct bpf_nested_pt_regs { 622 struct pt_regs regs[3]; 623 }; 624 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs); 625 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds); 626 627 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, 628 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) 629 { 630 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level); 631 struct perf_raw_frag frag = { 632 .copy = ctx_copy, 633 .size = ctx_size, 634 .data = ctx, 635 }; 636 struct perf_raw_record raw = { 637 .frag = { 638 { 639 .next = ctx_size ? &frag : NULL, 640 }, 641 .size = meta_size, 642 .data = meta, 643 }, 644 }; 645 struct perf_sample_data *sd; 646 struct pt_regs *regs; 647 u64 ret; 648 649 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) { 650 ret = -EBUSY; 651 goto out; 652 } 653 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]); 654 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]); 655 656 perf_fetch_caller_regs(regs); 657 perf_sample_data_init(sd, 0, 0); 658 sd->raw = &raw; 659 660 ret = __bpf_perf_event_output(regs, map, flags, sd); 661 out: 662 this_cpu_dec(bpf_event_output_nest_level); 663 return ret; 664 } 665 666 BPF_CALL_0(bpf_get_current_task) 667 { 668 return (long) current; 669 } 670 671 static const struct bpf_func_proto bpf_get_current_task_proto = { 672 .func = bpf_get_current_task, 673 .gpl_only = true, 674 .ret_type = RET_INTEGER, 675 }; 676 677 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) 678 { 679 struct bpf_array *array = container_of(map, struct bpf_array, map); 680 struct cgroup *cgrp; 681 682 if (unlikely(idx >= array->map.max_entries)) 683 return -E2BIG; 684 685 cgrp = READ_ONCE(array->ptrs[idx]); 686 if (unlikely(!cgrp)) 687 return -EAGAIN; 688 689 return task_under_cgroup_hierarchy(current, cgrp); 690 } 691 692 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { 693 .func = bpf_current_task_under_cgroup, 694 .gpl_only = false, 695 .ret_type = RET_INTEGER, 696 .arg1_type = ARG_CONST_MAP_PTR, 697 .arg2_type = ARG_ANYTHING, 698 }; 699 700 struct send_signal_irq_work { 701 struct irq_work irq_work; 702 struct task_struct *task; 703 u32 sig; 704 enum pid_type type; 705 }; 706 707 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work); 708 709 static void do_bpf_send_signal(struct irq_work *entry) 710 { 711 struct send_signal_irq_work *work; 712 713 work = container_of(entry, struct send_signal_irq_work, irq_work); 714 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type); 715 } 716 717 static int bpf_send_signal_common(u32 sig, enum pid_type type) 718 { 719 struct send_signal_irq_work *work = NULL; 720 721 /* Similar to bpf_probe_write_user, task needs to be 722 * in a sound condition and kernel memory access be 723 * permitted in order to send signal to the current 724 * task. 725 */ 726 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING))) 727 return -EPERM; 728 if (unlikely(uaccess_kernel())) 729 return -EPERM; 730 if (unlikely(!nmi_uaccess_okay())) 731 return -EPERM; 732 733 if (irqs_disabled()) { 734 /* Do an early check on signal validity. Otherwise, 735 * the error is lost in deferred irq_work. 736 */ 737 if (unlikely(!valid_signal(sig))) 738 return -EINVAL; 739 740 work = this_cpu_ptr(&send_signal_work); 741 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY) 742 return -EBUSY; 743 744 /* Add the current task, which is the target of sending signal, 745 * to the irq_work. The current task may change when queued 746 * irq works get executed. 747 */ 748 work->task = current; 749 work->sig = sig; 750 work->type = type; 751 irq_work_queue(&work->irq_work); 752 return 0; 753 } 754 755 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type); 756 } 757 758 BPF_CALL_1(bpf_send_signal, u32, sig) 759 { 760 return bpf_send_signal_common(sig, PIDTYPE_TGID); 761 } 762 763 static const struct bpf_func_proto bpf_send_signal_proto = { 764 .func = bpf_send_signal, 765 .gpl_only = false, 766 .ret_type = RET_INTEGER, 767 .arg1_type = ARG_ANYTHING, 768 }; 769 770 BPF_CALL_1(bpf_send_signal_thread, u32, sig) 771 { 772 return bpf_send_signal_common(sig, PIDTYPE_PID); 773 } 774 775 static const struct bpf_func_proto bpf_send_signal_thread_proto = { 776 .func = bpf_send_signal_thread, 777 .gpl_only = false, 778 .ret_type = RET_INTEGER, 779 .arg1_type = ARG_ANYTHING, 780 }; 781 782 const struct bpf_func_proto * 783 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 784 { 785 switch (func_id) { 786 case BPF_FUNC_map_lookup_elem: 787 return &bpf_map_lookup_elem_proto; 788 case BPF_FUNC_map_update_elem: 789 return &bpf_map_update_elem_proto; 790 case BPF_FUNC_map_delete_elem: 791 return &bpf_map_delete_elem_proto; 792 case BPF_FUNC_map_push_elem: 793 return &bpf_map_push_elem_proto; 794 case BPF_FUNC_map_pop_elem: 795 return &bpf_map_pop_elem_proto; 796 case BPF_FUNC_map_peek_elem: 797 return &bpf_map_peek_elem_proto; 798 case BPF_FUNC_ktime_get_ns: 799 return &bpf_ktime_get_ns_proto; 800 case BPF_FUNC_tail_call: 801 return &bpf_tail_call_proto; 802 case BPF_FUNC_get_current_pid_tgid: 803 return &bpf_get_current_pid_tgid_proto; 804 case BPF_FUNC_get_current_task: 805 return &bpf_get_current_task_proto; 806 case BPF_FUNC_get_current_uid_gid: 807 return &bpf_get_current_uid_gid_proto; 808 case BPF_FUNC_get_current_comm: 809 return &bpf_get_current_comm_proto; 810 case BPF_FUNC_trace_printk: 811 return bpf_get_trace_printk_proto(); 812 case BPF_FUNC_get_smp_processor_id: 813 return &bpf_get_smp_processor_id_proto; 814 case BPF_FUNC_get_numa_node_id: 815 return &bpf_get_numa_node_id_proto; 816 case BPF_FUNC_perf_event_read: 817 return &bpf_perf_event_read_proto; 818 case BPF_FUNC_probe_write_user: 819 return bpf_get_probe_write_proto(); 820 case BPF_FUNC_current_task_under_cgroup: 821 return &bpf_current_task_under_cgroup_proto; 822 case BPF_FUNC_get_prandom_u32: 823 return &bpf_get_prandom_u32_proto; 824 case BPF_FUNC_probe_read_user: 825 return &bpf_probe_read_user_proto; 826 case BPF_FUNC_probe_read_kernel: 827 return &bpf_probe_read_kernel_proto; 828 case BPF_FUNC_probe_read: 829 return &bpf_probe_read_compat_proto; 830 case BPF_FUNC_probe_read_user_str: 831 return &bpf_probe_read_user_str_proto; 832 case BPF_FUNC_probe_read_kernel_str: 833 return &bpf_probe_read_kernel_str_proto; 834 case BPF_FUNC_probe_read_str: 835 return &bpf_probe_read_compat_str_proto; 836 #ifdef CONFIG_CGROUPS 837 case BPF_FUNC_get_current_cgroup_id: 838 return &bpf_get_current_cgroup_id_proto; 839 #endif 840 case BPF_FUNC_send_signal: 841 return &bpf_send_signal_proto; 842 case BPF_FUNC_send_signal_thread: 843 return &bpf_send_signal_thread_proto; 844 case BPF_FUNC_perf_event_read_value: 845 return &bpf_perf_event_read_value_proto; 846 case BPF_FUNC_get_ns_current_pid_tgid: 847 return &bpf_get_ns_current_pid_tgid_proto; 848 default: 849 return NULL; 850 } 851 } 852 853 static const struct bpf_func_proto * 854 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 855 { 856 switch (func_id) { 857 case BPF_FUNC_perf_event_output: 858 return &bpf_perf_event_output_proto; 859 case BPF_FUNC_get_stackid: 860 return &bpf_get_stackid_proto; 861 case BPF_FUNC_get_stack: 862 return &bpf_get_stack_proto; 863 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 864 case BPF_FUNC_override_return: 865 return &bpf_override_return_proto; 866 #endif 867 default: 868 return bpf_tracing_func_proto(func_id, prog); 869 } 870 } 871 872 /* bpf+kprobe programs can access fields of 'struct pt_regs' */ 873 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 874 const struct bpf_prog *prog, 875 struct bpf_insn_access_aux *info) 876 { 877 if (off < 0 || off >= sizeof(struct pt_regs)) 878 return false; 879 if (type != BPF_READ) 880 return false; 881 if (off % size != 0) 882 return false; 883 /* 884 * Assertion for 32 bit to make sure last 8 byte access 885 * (BPF_DW) to the last 4 byte member is disallowed. 886 */ 887 if (off + size > sizeof(struct pt_regs)) 888 return false; 889 890 return true; 891 } 892 893 const struct bpf_verifier_ops kprobe_verifier_ops = { 894 .get_func_proto = kprobe_prog_func_proto, 895 .is_valid_access = kprobe_prog_is_valid_access, 896 }; 897 898 const struct bpf_prog_ops kprobe_prog_ops = { 899 }; 900 901 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, 902 u64, flags, void *, data, u64, size) 903 { 904 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 905 906 /* 907 * r1 points to perf tracepoint buffer where first 8 bytes are hidden 908 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it 909 * from there and call the same bpf_perf_event_output() helper inline. 910 */ 911 return ____bpf_perf_event_output(regs, map, flags, data, size); 912 } 913 914 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { 915 .func = bpf_perf_event_output_tp, 916 .gpl_only = true, 917 .ret_type = RET_INTEGER, 918 .arg1_type = ARG_PTR_TO_CTX, 919 .arg2_type = ARG_CONST_MAP_PTR, 920 .arg3_type = ARG_ANYTHING, 921 .arg4_type = ARG_PTR_TO_MEM, 922 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 923 }; 924 925 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, 926 u64, flags) 927 { 928 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 929 930 /* 931 * Same comment as in bpf_perf_event_output_tp(), only that this time 932 * the other helper's function body cannot be inlined due to being 933 * external, thus we need to call raw helper function. 934 */ 935 return bpf_get_stackid((unsigned long) regs, (unsigned long) map, 936 flags, 0, 0); 937 } 938 939 static const struct bpf_func_proto bpf_get_stackid_proto_tp = { 940 .func = bpf_get_stackid_tp, 941 .gpl_only = true, 942 .ret_type = RET_INTEGER, 943 .arg1_type = ARG_PTR_TO_CTX, 944 .arg2_type = ARG_CONST_MAP_PTR, 945 .arg3_type = ARG_ANYTHING, 946 }; 947 948 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, 949 u64, flags) 950 { 951 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 952 953 return bpf_get_stack((unsigned long) regs, (unsigned long) buf, 954 (unsigned long) size, flags, 0); 955 } 956 957 static const struct bpf_func_proto bpf_get_stack_proto_tp = { 958 .func = bpf_get_stack_tp, 959 .gpl_only = true, 960 .ret_type = RET_INTEGER, 961 .arg1_type = ARG_PTR_TO_CTX, 962 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 963 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 964 .arg4_type = ARG_ANYTHING, 965 }; 966 967 static const struct bpf_func_proto * 968 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 969 { 970 switch (func_id) { 971 case BPF_FUNC_perf_event_output: 972 return &bpf_perf_event_output_proto_tp; 973 case BPF_FUNC_get_stackid: 974 return &bpf_get_stackid_proto_tp; 975 case BPF_FUNC_get_stack: 976 return &bpf_get_stack_proto_tp; 977 default: 978 return bpf_tracing_func_proto(func_id, prog); 979 } 980 } 981 982 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, 983 const struct bpf_prog *prog, 984 struct bpf_insn_access_aux *info) 985 { 986 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) 987 return false; 988 if (type != BPF_READ) 989 return false; 990 if (off % size != 0) 991 return false; 992 993 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); 994 return true; 995 } 996 997 const struct bpf_verifier_ops tracepoint_verifier_ops = { 998 .get_func_proto = tp_prog_func_proto, 999 .is_valid_access = tp_prog_is_valid_access, 1000 }; 1001 1002 const struct bpf_prog_ops tracepoint_prog_ops = { 1003 }; 1004 1005 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, 1006 struct bpf_perf_event_value *, buf, u32, size) 1007 { 1008 int err = -EINVAL; 1009 1010 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 1011 goto clear; 1012 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, 1013 &buf->running); 1014 if (unlikely(err)) 1015 goto clear; 1016 return 0; 1017 clear: 1018 memset(buf, 0, size); 1019 return err; 1020 } 1021 1022 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { 1023 .func = bpf_perf_prog_read_value, 1024 .gpl_only = true, 1025 .ret_type = RET_INTEGER, 1026 .arg1_type = ARG_PTR_TO_CTX, 1027 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 1028 .arg3_type = ARG_CONST_SIZE, 1029 }; 1030 1031 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx, 1032 void *, buf, u32, size, u64, flags) 1033 { 1034 #ifndef CONFIG_X86 1035 return -ENOENT; 1036 #else 1037 static const u32 br_entry_size = sizeof(struct perf_branch_entry); 1038 struct perf_branch_stack *br_stack = ctx->data->br_stack; 1039 u32 to_copy; 1040 1041 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) 1042 return -EINVAL; 1043 1044 if (unlikely(!br_stack)) 1045 return -EINVAL; 1046 1047 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE) 1048 return br_stack->nr * br_entry_size; 1049 1050 if (!buf || (size % br_entry_size != 0)) 1051 return -EINVAL; 1052 1053 to_copy = min_t(u32, br_stack->nr * br_entry_size, size); 1054 memcpy(buf, br_stack->entries, to_copy); 1055 1056 return to_copy; 1057 #endif 1058 } 1059 1060 static const struct bpf_func_proto bpf_read_branch_records_proto = { 1061 .func = bpf_read_branch_records, 1062 .gpl_only = true, 1063 .ret_type = RET_INTEGER, 1064 .arg1_type = ARG_PTR_TO_CTX, 1065 .arg2_type = ARG_PTR_TO_MEM_OR_NULL, 1066 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 1067 .arg4_type = ARG_ANYTHING, 1068 }; 1069 1070 static const struct bpf_func_proto * 1071 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1072 { 1073 switch (func_id) { 1074 case BPF_FUNC_perf_event_output: 1075 return &bpf_perf_event_output_proto_tp; 1076 case BPF_FUNC_get_stackid: 1077 return &bpf_get_stackid_proto_tp; 1078 case BPF_FUNC_get_stack: 1079 return &bpf_get_stack_proto_tp; 1080 case BPF_FUNC_perf_prog_read_value: 1081 return &bpf_perf_prog_read_value_proto; 1082 case BPF_FUNC_read_branch_records: 1083 return &bpf_read_branch_records_proto; 1084 default: 1085 return bpf_tracing_func_proto(func_id, prog); 1086 } 1087 } 1088 1089 /* 1090 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp 1091 * to avoid potential recursive reuse issue when/if tracepoints are added 1092 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack. 1093 * 1094 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage 1095 * in normal, irq, and nmi context. 1096 */ 1097 struct bpf_raw_tp_regs { 1098 struct pt_regs regs[3]; 1099 }; 1100 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs); 1101 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level); 1102 static struct pt_regs *get_bpf_raw_tp_regs(void) 1103 { 1104 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs); 1105 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level); 1106 1107 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) { 1108 this_cpu_dec(bpf_raw_tp_nest_level); 1109 return ERR_PTR(-EBUSY); 1110 } 1111 1112 return &tp_regs->regs[nest_level - 1]; 1113 } 1114 1115 static void put_bpf_raw_tp_regs(void) 1116 { 1117 this_cpu_dec(bpf_raw_tp_nest_level); 1118 } 1119 1120 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, 1121 struct bpf_map *, map, u64, flags, void *, data, u64, size) 1122 { 1123 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1124 int ret; 1125 1126 if (IS_ERR(regs)) 1127 return PTR_ERR(regs); 1128 1129 perf_fetch_caller_regs(regs); 1130 ret = ____bpf_perf_event_output(regs, map, flags, data, size); 1131 1132 put_bpf_raw_tp_regs(); 1133 return ret; 1134 } 1135 1136 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { 1137 .func = bpf_perf_event_output_raw_tp, 1138 .gpl_only = true, 1139 .ret_type = RET_INTEGER, 1140 .arg1_type = ARG_PTR_TO_CTX, 1141 .arg2_type = ARG_CONST_MAP_PTR, 1142 .arg3_type = ARG_ANYTHING, 1143 .arg4_type = ARG_PTR_TO_MEM, 1144 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 1145 }; 1146 1147 extern const struct bpf_func_proto bpf_skb_output_proto; 1148 extern const struct bpf_func_proto bpf_xdp_output_proto; 1149 1150 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, 1151 struct bpf_map *, map, u64, flags) 1152 { 1153 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1154 int ret; 1155 1156 if (IS_ERR(regs)) 1157 return PTR_ERR(regs); 1158 1159 perf_fetch_caller_regs(regs); 1160 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ 1161 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map, 1162 flags, 0, 0); 1163 put_bpf_raw_tp_regs(); 1164 return ret; 1165 } 1166 1167 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { 1168 .func = bpf_get_stackid_raw_tp, 1169 .gpl_only = true, 1170 .ret_type = RET_INTEGER, 1171 .arg1_type = ARG_PTR_TO_CTX, 1172 .arg2_type = ARG_CONST_MAP_PTR, 1173 .arg3_type = ARG_ANYTHING, 1174 }; 1175 1176 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, 1177 void *, buf, u32, size, u64, flags) 1178 { 1179 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1180 int ret; 1181 1182 if (IS_ERR(regs)) 1183 return PTR_ERR(regs); 1184 1185 perf_fetch_caller_regs(regs); 1186 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf, 1187 (unsigned long) size, flags, 0); 1188 put_bpf_raw_tp_regs(); 1189 return ret; 1190 } 1191 1192 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { 1193 .func = bpf_get_stack_raw_tp, 1194 .gpl_only = true, 1195 .ret_type = RET_INTEGER, 1196 .arg1_type = ARG_PTR_TO_CTX, 1197 .arg2_type = ARG_PTR_TO_MEM, 1198 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 1199 .arg4_type = ARG_ANYTHING, 1200 }; 1201 1202 static const struct bpf_func_proto * 1203 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1204 { 1205 switch (func_id) { 1206 case BPF_FUNC_perf_event_output: 1207 return &bpf_perf_event_output_proto_raw_tp; 1208 case BPF_FUNC_get_stackid: 1209 return &bpf_get_stackid_proto_raw_tp; 1210 case BPF_FUNC_get_stack: 1211 return &bpf_get_stack_proto_raw_tp; 1212 default: 1213 return bpf_tracing_func_proto(func_id, prog); 1214 } 1215 } 1216 1217 static const struct bpf_func_proto * 1218 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1219 { 1220 switch (func_id) { 1221 #ifdef CONFIG_NET 1222 case BPF_FUNC_skb_output: 1223 return &bpf_skb_output_proto; 1224 case BPF_FUNC_xdp_output: 1225 return &bpf_xdp_output_proto; 1226 #endif 1227 default: 1228 return raw_tp_prog_func_proto(func_id, prog); 1229 } 1230 } 1231 1232 static bool raw_tp_prog_is_valid_access(int off, int size, 1233 enum bpf_access_type type, 1234 const struct bpf_prog *prog, 1235 struct bpf_insn_access_aux *info) 1236 { 1237 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) 1238 return false; 1239 if (type != BPF_READ) 1240 return false; 1241 if (off % size != 0) 1242 return false; 1243 return true; 1244 } 1245 1246 static bool tracing_prog_is_valid_access(int off, int size, 1247 enum bpf_access_type type, 1248 const struct bpf_prog *prog, 1249 struct bpf_insn_access_aux *info) 1250 { 1251 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) 1252 return false; 1253 if (type != BPF_READ) 1254 return false; 1255 if (off % size != 0) 1256 return false; 1257 return btf_ctx_access(off, size, type, prog, info); 1258 } 1259 1260 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog, 1261 const union bpf_attr *kattr, 1262 union bpf_attr __user *uattr) 1263 { 1264 return -ENOTSUPP; 1265 } 1266 1267 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { 1268 .get_func_proto = raw_tp_prog_func_proto, 1269 .is_valid_access = raw_tp_prog_is_valid_access, 1270 }; 1271 1272 const struct bpf_prog_ops raw_tracepoint_prog_ops = { 1273 }; 1274 1275 const struct bpf_verifier_ops tracing_verifier_ops = { 1276 .get_func_proto = tracing_prog_func_proto, 1277 .is_valid_access = tracing_prog_is_valid_access, 1278 }; 1279 1280 const struct bpf_prog_ops tracing_prog_ops = { 1281 .test_run = bpf_prog_test_run_tracing, 1282 }; 1283 1284 static bool raw_tp_writable_prog_is_valid_access(int off, int size, 1285 enum bpf_access_type type, 1286 const struct bpf_prog *prog, 1287 struct bpf_insn_access_aux *info) 1288 { 1289 if (off == 0) { 1290 if (size != sizeof(u64) || type != BPF_READ) 1291 return false; 1292 info->reg_type = PTR_TO_TP_BUFFER; 1293 } 1294 return raw_tp_prog_is_valid_access(off, size, type, prog, info); 1295 } 1296 1297 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { 1298 .get_func_proto = raw_tp_prog_func_proto, 1299 .is_valid_access = raw_tp_writable_prog_is_valid_access, 1300 }; 1301 1302 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { 1303 }; 1304 1305 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1306 const struct bpf_prog *prog, 1307 struct bpf_insn_access_aux *info) 1308 { 1309 const int size_u64 = sizeof(u64); 1310 1311 if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) 1312 return false; 1313 if (type != BPF_READ) 1314 return false; 1315 if (off % size != 0) { 1316 if (sizeof(unsigned long) != 4) 1317 return false; 1318 if (size != 8) 1319 return false; 1320 if (off % size != 4) 1321 return false; 1322 } 1323 1324 switch (off) { 1325 case bpf_ctx_range(struct bpf_perf_event_data, sample_period): 1326 bpf_ctx_record_field_size(info, size_u64); 1327 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1328 return false; 1329 break; 1330 case bpf_ctx_range(struct bpf_perf_event_data, addr): 1331 bpf_ctx_record_field_size(info, size_u64); 1332 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1333 return false; 1334 break; 1335 default: 1336 if (size != sizeof(long)) 1337 return false; 1338 } 1339 1340 return true; 1341 } 1342 1343 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, 1344 const struct bpf_insn *si, 1345 struct bpf_insn *insn_buf, 1346 struct bpf_prog *prog, u32 *target_size) 1347 { 1348 struct bpf_insn *insn = insn_buf; 1349 1350 switch (si->off) { 1351 case offsetof(struct bpf_perf_event_data, sample_period): 1352 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1353 data), si->dst_reg, si->src_reg, 1354 offsetof(struct bpf_perf_event_data_kern, data)); 1355 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1356 bpf_target_off(struct perf_sample_data, period, 8, 1357 target_size)); 1358 break; 1359 case offsetof(struct bpf_perf_event_data, addr): 1360 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1361 data), si->dst_reg, si->src_reg, 1362 offsetof(struct bpf_perf_event_data_kern, data)); 1363 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1364 bpf_target_off(struct perf_sample_data, addr, 8, 1365 target_size)); 1366 break; 1367 default: 1368 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1369 regs), si->dst_reg, si->src_reg, 1370 offsetof(struct bpf_perf_event_data_kern, regs)); 1371 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, 1372 si->off); 1373 break; 1374 } 1375 1376 return insn - insn_buf; 1377 } 1378 1379 const struct bpf_verifier_ops perf_event_verifier_ops = { 1380 .get_func_proto = pe_prog_func_proto, 1381 .is_valid_access = pe_prog_is_valid_access, 1382 .convert_ctx_access = pe_prog_convert_ctx_access, 1383 }; 1384 1385 const struct bpf_prog_ops perf_event_prog_ops = { 1386 }; 1387 1388 static DEFINE_MUTEX(bpf_event_mutex); 1389 1390 #define BPF_TRACE_MAX_PROGS 64 1391 1392 int perf_event_attach_bpf_prog(struct perf_event *event, 1393 struct bpf_prog *prog) 1394 { 1395 struct bpf_prog_array *old_array; 1396 struct bpf_prog_array *new_array; 1397 int ret = -EEXIST; 1398 1399 /* 1400 * Kprobe override only works if they are on the function entry, 1401 * and only if they are on the opt-in list. 1402 */ 1403 if (prog->kprobe_override && 1404 (!trace_kprobe_on_func_entry(event->tp_event) || 1405 !trace_kprobe_error_injectable(event->tp_event))) 1406 return -EINVAL; 1407 1408 mutex_lock(&bpf_event_mutex); 1409 1410 if (event->prog) 1411 goto unlock; 1412 1413 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1414 if (old_array && 1415 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { 1416 ret = -E2BIG; 1417 goto unlock; 1418 } 1419 1420 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); 1421 if (ret < 0) 1422 goto unlock; 1423 1424 /* set the new array to event->tp_event and set event->prog */ 1425 event->prog = prog; 1426 rcu_assign_pointer(event->tp_event->prog_array, new_array); 1427 bpf_prog_array_free(old_array); 1428 1429 unlock: 1430 mutex_unlock(&bpf_event_mutex); 1431 return ret; 1432 } 1433 1434 void perf_event_detach_bpf_prog(struct perf_event *event) 1435 { 1436 struct bpf_prog_array *old_array; 1437 struct bpf_prog_array *new_array; 1438 int ret; 1439 1440 mutex_lock(&bpf_event_mutex); 1441 1442 if (!event->prog) 1443 goto unlock; 1444 1445 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1446 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); 1447 if (ret == -ENOENT) 1448 goto unlock; 1449 if (ret < 0) { 1450 bpf_prog_array_delete_safe(old_array, event->prog); 1451 } else { 1452 rcu_assign_pointer(event->tp_event->prog_array, new_array); 1453 bpf_prog_array_free(old_array); 1454 } 1455 1456 bpf_prog_put(event->prog); 1457 event->prog = NULL; 1458 1459 unlock: 1460 mutex_unlock(&bpf_event_mutex); 1461 } 1462 1463 int perf_event_query_prog_array(struct perf_event *event, void __user *info) 1464 { 1465 struct perf_event_query_bpf __user *uquery = info; 1466 struct perf_event_query_bpf query = {}; 1467 struct bpf_prog_array *progs; 1468 u32 *ids, prog_cnt, ids_len; 1469 int ret; 1470 1471 if (!capable(CAP_SYS_ADMIN)) 1472 return -EPERM; 1473 if (event->attr.type != PERF_TYPE_TRACEPOINT) 1474 return -EINVAL; 1475 if (copy_from_user(&query, uquery, sizeof(query))) 1476 return -EFAULT; 1477 1478 ids_len = query.ids_len; 1479 if (ids_len > BPF_TRACE_MAX_PROGS) 1480 return -E2BIG; 1481 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); 1482 if (!ids) 1483 return -ENOMEM; 1484 /* 1485 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which 1486 * is required when user only wants to check for uquery->prog_cnt. 1487 * There is no need to check for it since the case is handled 1488 * gracefully in bpf_prog_array_copy_info. 1489 */ 1490 1491 mutex_lock(&bpf_event_mutex); 1492 progs = bpf_event_rcu_dereference(event->tp_event->prog_array); 1493 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); 1494 mutex_unlock(&bpf_event_mutex); 1495 1496 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || 1497 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) 1498 ret = -EFAULT; 1499 1500 kfree(ids); 1501 return ret; 1502 } 1503 1504 extern struct bpf_raw_event_map __start__bpf_raw_tp[]; 1505 extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; 1506 1507 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) 1508 { 1509 struct bpf_raw_event_map *btp = __start__bpf_raw_tp; 1510 1511 for (; btp < __stop__bpf_raw_tp; btp++) { 1512 if (!strcmp(btp->tp->name, name)) 1513 return btp; 1514 } 1515 1516 return bpf_get_raw_tracepoint_module(name); 1517 } 1518 1519 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) 1520 { 1521 struct module *mod = __module_address((unsigned long)btp); 1522 1523 if (mod) 1524 module_put(mod); 1525 } 1526 1527 static __always_inline 1528 void __bpf_trace_run(struct bpf_prog *prog, u64 *args) 1529 { 1530 cant_sleep(); 1531 rcu_read_lock(); 1532 (void) BPF_PROG_RUN(prog, args); 1533 rcu_read_unlock(); 1534 } 1535 1536 #define UNPACK(...) __VA_ARGS__ 1537 #define REPEAT_1(FN, DL, X, ...) FN(X) 1538 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) 1539 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) 1540 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) 1541 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) 1542 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) 1543 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) 1544 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) 1545 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) 1546 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) 1547 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) 1548 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) 1549 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) 1550 1551 #define SARG(X) u64 arg##X 1552 #define COPY(X) args[X] = arg##X 1553 1554 #define __DL_COM (,) 1555 #define __DL_SEM (;) 1556 1557 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 1558 1559 #define BPF_TRACE_DEFN_x(x) \ 1560 void bpf_trace_run##x(struct bpf_prog *prog, \ 1561 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ 1562 { \ 1563 u64 args[x]; \ 1564 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ 1565 __bpf_trace_run(prog, args); \ 1566 } \ 1567 EXPORT_SYMBOL_GPL(bpf_trace_run##x) 1568 BPF_TRACE_DEFN_x(1); 1569 BPF_TRACE_DEFN_x(2); 1570 BPF_TRACE_DEFN_x(3); 1571 BPF_TRACE_DEFN_x(4); 1572 BPF_TRACE_DEFN_x(5); 1573 BPF_TRACE_DEFN_x(6); 1574 BPF_TRACE_DEFN_x(7); 1575 BPF_TRACE_DEFN_x(8); 1576 BPF_TRACE_DEFN_x(9); 1577 BPF_TRACE_DEFN_x(10); 1578 BPF_TRACE_DEFN_x(11); 1579 BPF_TRACE_DEFN_x(12); 1580 1581 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 1582 { 1583 struct tracepoint *tp = btp->tp; 1584 1585 /* 1586 * check that program doesn't access arguments beyond what's 1587 * available in this tracepoint 1588 */ 1589 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) 1590 return -EINVAL; 1591 1592 if (prog->aux->max_tp_access > btp->writable_size) 1593 return -EINVAL; 1594 1595 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog); 1596 } 1597 1598 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 1599 { 1600 return __bpf_probe_register(btp, prog); 1601 } 1602 1603 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 1604 { 1605 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); 1606 } 1607 1608 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, 1609 u32 *fd_type, const char **buf, 1610 u64 *probe_offset, u64 *probe_addr) 1611 { 1612 bool is_tracepoint, is_syscall_tp; 1613 struct bpf_prog *prog; 1614 int flags, err = 0; 1615 1616 prog = event->prog; 1617 if (!prog) 1618 return -ENOENT; 1619 1620 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ 1621 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) 1622 return -EOPNOTSUPP; 1623 1624 *prog_id = prog->aux->id; 1625 flags = event->tp_event->flags; 1626 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; 1627 is_syscall_tp = is_syscall_trace_event(event->tp_event); 1628 1629 if (is_tracepoint || is_syscall_tp) { 1630 *buf = is_tracepoint ? event->tp_event->tp->name 1631 : event->tp_event->name; 1632 *fd_type = BPF_FD_TYPE_TRACEPOINT; 1633 *probe_offset = 0x0; 1634 *probe_addr = 0x0; 1635 } else { 1636 /* kprobe/uprobe */ 1637 err = -EOPNOTSUPP; 1638 #ifdef CONFIG_KPROBE_EVENTS 1639 if (flags & TRACE_EVENT_FL_KPROBE) 1640 err = bpf_get_kprobe_info(event, fd_type, buf, 1641 probe_offset, probe_addr, 1642 event->attr.type == PERF_TYPE_TRACEPOINT); 1643 #endif 1644 #ifdef CONFIG_UPROBE_EVENTS 1645 if (flags & TRACE_EVENT_FL_UPROBE) 1646 err = bpf_get_uprobe_info(event, fd_type, buf, 1647 probe_offset, 1648 event->attr.type == PERF_TYPE_TRACEPOINT); 1649 #endif 1650 } 1651 1652 return err; 1653 } 1654 1655 static int __init send_signal_irq_work_init(void) 1656 { 1657 int cpu; 1658 struct send_signal_irq_work *work; 1659 1660 for_each_possible_cpu(cpu) { 1661 work = per_cpu_ptr(&send_signal_work, cpu); 1662 init_irq_work(&work->irq_work, do_bpf_send_signal); 1663 } 1664 return 0; 1665 } 1666 1667 subsys_initcall(send_signal_irq_work_init); 1668 1669 #ifdef CONFIG_MODULES 1670 static int bpf_event_notify(struct notifier_block *nb, unsigned long op, 1671 void *module) 1672 { 1673 struct bpf_trace_module *btm, *tmp; 1674 struct module *mod = module; 1675 1676 if (mod->num_bpf_raw_events == 0 || 1677 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) 1678 return 0; 1679 1680 mutex_lock(&bpf_module_mutex); 1681 1682 switch (op) { 1683 case MODULE_STATE_COMING: 1684 btm = kzalloc(sizeof(*btm), GFP_KERNEL); 1685 if (btm) { 1686 btm->module = module; 1687 list_add(&btm->list, &bpf_trace_modules); 1688 } 1689 break; 1690 case MODULE_STATE_GOING: 1691 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { 1692 if (btm->module == module) { 1693 list_del(&btm->list); 1694 kfree(btm); 1695 break; 1696 } 1697 } 1698 break; 1699 } 1700 1701 mutex_unlock(&bpf_module_mutex); 1702 1703 return 0; 1704 } 1705 1706 static struct notifier_block bpf_module_nb = { 1707 .notifier_call = bpf_event_notify, 1708 }; 1709 1710 static int __init bpf_event_init(void) 1711 { 1712 register_module_notifier(&bpf_module_nb); 1713 return 0; 1714 } 1715 1716 fs_initcall(bpf_event_init); 1717 #endif /* CONFIG_MODULES */ 1718