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/btf.h> 11 #include <linux/filter.h> 12 #include <linux/uaccess.h> 13 #include <linux/ctype.h> 14 #include <linux/kprobes.h> 15 #include <linux/spinlock.h> 16 #include <linux/syscalls.h> 17 #include <linux/error-injection.h> 18 #include <linux/btf_ids.h> 19 #include <linux/bpf_lsm.h> 20 21 #include <net/bpf_sk_storage.h> 22 23 #include <uapi/linux/bpf.h> 24 #include <uapi/linux/btf.h> 25 26 #include <asm/tlb.h> 27 28 #include "trace_probe.h" 29 #include "trace.h" 30 31 #define CREATE_TRACE_POINTS 32 #include "bpf_trace.h" 33 34 #define bpf_event_rcu_dereference(p) \ 35 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) 36 37 #ifdef CONFIG_MODULES 38 struct bpf_trace_module { 39 struct module *module; 40 struct list_head list; 41 }; 42 43 static LIST_HEAD(bpf_trace_modules); 44 static DEFINE_MUTEX(bpf_module_mutex); 45 46 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) 47 { 48 struct bpf_raw_event_map *btp, *ret = NULL; 49 struct bpf_trace_module *btm; 50 unsigned int i; 51 52 mutex_lock(&bpf_module_mutex); 53 list_for_each_entry(btm, &bpf_trace_modules, list) { 54 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { 55 btp = &btm->module->bpf_raw_events[i]; 56 if (!strcmp(btp->tp->name, name)) { 57 if (try_module_get(btm->module)) 58 ret = btp; 59 goto out; 60 } 61 } 62 } 63 out: 64 mutex_unlock(&bpf_module_mutex); 65 return ret; 66 } 67 #else 68 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) 69 { 70 return NULL; 71 } 72 #endif /* CONFIG_MODULES */ 73 74 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 75 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 76 77 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, 78 u64 flags, const struct btf **btf, 79 s32 *btf_id); 80 81 /** 82 * trace_call_bpf - invoke BPF program 83 * @call: tracepoint event 84 * @ctx: opaque context pointer 85 * 86 * kprobe handlers execute BPF programs via this helper. 87 * Can be used from static tracepoints in the future. 88 * 89 * Return: BPF programs always return an integer which is interpreted by 90 * kprobe handler as: 91 * 0 - return from kprobe (event is filtered out) 92 * 1 - store kprobe event into ring buffer 93 * Other values are reserved and currently alias to 1 94 */ 95 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) 96 { 97 unsigned int ret; 98 99 if (in_nmi()) /* not supported yet */ 100 return 1; 101 102 cant_sleep(); 103 104 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { 105 /* 106 * since some bpf program is already running on this cpu, 107 * don't call into another bpf program (same or different) 108 * and don't send kprobe event into ring-buffer, 109 * so return zero here 110 */ 111 ret = 0; 112 goto out; 113 } 114 115 /* 116 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock 117 * to all call sites, we did a bpf_prog_array_valid() there to check 118 * whether call->prog_array is empty or not, which is 119 * a heurisitc to speed up execution. 120 * 121 * If bpf_prog_array_valid() fetched prog_array was 122 * non-NULL, we go into trace_call_bpf() and do the actual 123 * proper rcu_dereference() under RCU lock. 124 * If it turns out that prog_array is NULL then, we bail out. 125 * For the opposite, if the bpf_prog_array_valid() fetched pointer 126 * was NULL, you'll skip the prog_array with the risk of missing 127 * out of events when it was updated in between this and the 128 * rcu_dereference() which is accepted risk. 129 */ 130 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN); 131 132 out: 133 __this_cpu_dec(bpf_prog_active); 134 135 return ret; 136 } 137 138 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 139 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) 140 { 141 regs_set_return_value(regs, rc); 142 override_function_with_return(regs); 143 return 0; 144 } 145 146 static const struct bpf_func_proto bpf_override_return_proto = { 147 .func = bpf_override_return, 148 .gpl_only = true, 149 .ret_type = RET_INTEGER, 150 .arg1_type = ARG_PTR_TO_CTX, 151 .arg2_type = ARG_ANYTHING, 152 }; 153 #endif 154 155 static __always_inline int 156 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr) 157 { 158 int ret; 159 160 ret = copy_from_user_nofault(dst, unsafe_ptr, size); 161 if (unlikely(ret < 0)) 162 memset(dst, 0, size); 163 return ret; 164 } 165 166 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, 167 const void __user *, unsafe_ptr) 168 { 169 return bpf_probe_read_user_common(dst, size, unsafe_ptr); 170 } 171 172 const struct bpf_func_proto bpf_probe_read_user_proto = { 173 .func = bpf_probe_read_user, 174 .gpl_only = true, 175 .ret_type = RET_INTEGER, 176 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 177 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 178 .arg3_type = ARG_ANYTHING, 179 }; 180 181 static __always_inline int 182 bpf_probe_read_user_str_common(void *dst, u32 size, 183 const void __user *unsafe_ptr) 184 { 185 int ret; 186 187 ret = strncpy_from_user_nofault(dst, unsafe_ptr, size); 188 if (unlikely(ret < 0)) 189 memset(dst, 0, size); 190 return ret; 191 } 192 193 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size, 194 const void __user *, unsafe_ptr) 195 { 196 return bpf_probe_read_user_str_common(dst, size, unsafe_ptr); 197 } 198 199 const struct bpf_func_proto bpf_probe_read_user_str_proto = { 200 .func = bpf_probe_read_user_str, 201 .gpl_only = true, 202 .ret_type = RET_INTEGER, 203 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 204 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 205 .arg3_type = ARG_ANYTHING, 206 }; 207 208 static __always_inline int 209 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr) 210 { 211 int ret = security_locked_down(LOCKDOWN_BPF_READ); 212 213 if (unlikely(ret < 0)) 214 goto fail; 215 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size); 216 if (unlikely(ret < 0)) 217 goto fail; 218 return ret; 219 fail: 220 memset(dst, 0, size); 221 return ret; 222 } 223 224 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size, 225 const void *, unsafe_ptr) 226 { 227 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); 228 } 229 230 const struct bpf_func_proto bpf_probe_read_kernel_proto = { 231 .func = bpf_probe_read_kernel, 232 .gpl_only = true, 233 .ret_type = RET_INTEGER, 234 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 235 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 236 .arg3_type = ARG_ANYTHING, 237 }; 238 239 static __always_inline int 240 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr) 241 { 242 int ret = security_locked_down(LOCKDOWN_BPF_READ); 243 244 if (unlikely(ret < 0)) 245 goto fail; 246 247 /* 248 * The strncpy_from_kernel_nofault() call will likely not fill the 249 * entire buffer, but that's okay in this circumstance as we're probing 250 * arbitrary memory anyway similar to bpf_probe_read_*() and might 251 * as well probe the stack. Thus, memory is explicitly cleared 252 * only in error case, so that improper users ignoring return 253 * code altogether don't copy garbage; otherwise length of string 254 * is returned that can be used for bpf_perf_event_output() et al. 255 */ 256 ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size); 257 if (unlikely(ret < 0)) 258 goto fail; 259 260 return ret; 261 fail: 262 memset(dst, 0, size); 263 return ret; 264 } 265 266 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size, 267 const void *, unsafe_ptr) 268 { 269 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); 270 } 271 272 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = { 273 .func = bpf_probe_read_kernel_str, 274 .gpl_only = true, 275 .ret_type = RET_INTEGER, 276 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 277 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 278 .arg3_type = ARG_ANYTHING, 279 }; 280 281 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 282 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size, 283 const void *, unsafe_ptr) 284 { 285 if ((unsigned long)unsafe_ptr < TASK_SIZE) { 286 return bpf_probe_read_user_common(dst, size, 287 (__force void __user *)unsafe_ptr); 288 } 289 return bpf_probe_read_kernel_common(dst, size, unsafe_ptr); 290 } 291 292 static const struct bpf_func_proto bpf_probe_read_compat_proto = { 293 .func = bpf_probe_read_compat, 294 .gpl_only = true, 295 .ret_type = RET_INTEGER, 296 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 297 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 298 .arg3_type = ARG_ANYTHING, 299 }; 300 301 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size, 302 const void *, unsafe_ptr) 303 { 304 if ((unsigned long)unsafe_ptr < TASK_SIZE) { 305 return bpf_probe_read_user_str_common(dst, size, 306 (__force void __user *)unsafe_ptr); 307 } 308 return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr); 309 } 310 311 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = { 312 .func = bpf_probe_read_compat_str, 313 .gpl_only = true, 314 .ret_type = RET_INTEGER, 315 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 316 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 317 .arg3_type = ARG_ANYTHING, 318 }; 319 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */ 320 321 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src, 322 u32, size) 323 { 324 /* 325 * Ensure we're in user context which is safe for the helper to 326 * run. This helper has no business in a kthread. 327 * 328 * access_ok() should prevent writing to non-user memory, but in 329 * some situations (nommu, temporary switch, etc) access_ok() does 330 * not provide enough validation, hence the check on KERNEL_DS. 331 * 332 * nmi_uaccess_okay() ensures the probe is not run in an interim 333 * state, when the task or mm are switched. This is specifically 334 * required to prevent the use of temporary mm. 335 */ 336 337 if (unlikely(in_interrupt() || 338 current->flags & (PF_KTHREAD | PF_EXITING))) 339 return -EPERM; 340 if (unlikely(uaccess_kernel())) 341 return -EPERM; 342 if (unlikely(!nmi_uaccess_okay())) 343 return -EPERM; 344 345 return copy_to_user_nofault(unsafe_ptr, src, size); 346 } 347 348 static const struct bpf_func_proto bpf_probe_write_user_proto = { 349 .func = bpf_probe_write_user, 350 .gpl_only = true, 351 .ret_type = RET_INTEGER, 352 .arg1_type = ARG_ANYTHING, 353 .arg2_type = ARG_PTR_TO_MEM, 354 .arg3_type = ARG_CONST_SIZE, 355 }; 356 357 static const struct bpf_func_proto *bpf_get_probe_write_proto(void) 358 { 359 if (!capable(CAP_SYS_ADMIN)) 360 return NULL; 361 362 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", 363 current->comm, task_pid_nr(current)); 364 365 return &bpf_probe_write_user_proto; 366 } 367 368 static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, 369 size_t bufsz) 370 { 371 void __user *user_ptr = (__force void __user *)unsafe_ptr; 372 373 buf[0] = 0; 374 375 switch (fmt_ptype) { 376 case 's': 377 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 378 if ((unsigned long)unsafe_ptr < TASK_SIZE) { 379 strncpy_from_user_nofault(buf, user_ptr, bufsz); 380 break; 381 } 382 fallthrough; 383 #endif 384 case 'k': 385 strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz); 386 break; 387 case 'u': 388 strncpy_from_user_nofault(buf, user_ptr, bufsz); 389 break; 390 } 391 } 392 393 static DEFINE_RAW_SPINLOCK(trace_printk_lock); 394 395 #define BPF_TRACE_PRINTK_SIZE 1024 396 397 static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...) 398 { 399 static char buf[BPF_TRACE_PRINTK_SIZE]; 400 unsigned long flags; 401 va_list ap; 402 int ret; 403 404 raw_spin_lock_irqsave(&trace_printk_lock, flags); 405 va_start(ap, fmt); 406 ret = vsnprintf(buf, sizeof(buf), fmt, ap); 407 va_end(ap); 408 /* vsnprintf() will not append null for zero-length strings */ 409 if (ret == 0) 410 buf[0] = '\0'; 411 trace_bpf_trace_printk(buf); 412 raw_spin_unlock_irqrestore(&trace_printk_lock, flags); 413 414 return ret; 415 } 416 417 /* 418 * Only limited trace_printk() conversion specifiers allowed: 419 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pB %pks %pus %s 420 */ 421 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, 422 u64, arg2, u64, arg3) 423 { 424 int i, mod[3] = {}, fmt_cnt = 0; 425 char buf[64], fmt_ptype; 426 void *unsafe_ptr = NULL; 427 bool str_seen = false; 428 429 /* 430 * bpf_check()->check_func_arg()->check_stack_boundary() 431 * guarantees that fmt points to bpf program stack, 432 * fmt_size bytes of it were initialized and fmt_size > 0 433 */ 434 if (fmt[--fmt_size] != 0) 435 return -EINVAL; 436 437 /* check format string for allowed specifiers */ 438 for (i = 0; i < fmt_size; i++) { 439 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) 440 return -EINVAL; 441 442 if (fmt[i] != '%') 443 continue; 444 445 if (fmt_cnt >= 3) 446 return -EINVAL; 447 448 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ 449 i++; 450 if (fmt[i] == 'l') { 451 mod[fmt_cnt]++; 452 i++; 453 } else if (fmt[i] == 'p') { 454 mod[fmt_cnt]++; 455 if ((fmt[i + 1] == 'k' || 456 fmt[i + 1] == 'u') && 457 fmt[i + 2] == 's') { 458 fmt_ptype = fmt[i + 1]; 459 i += 2; 460 goto fmt_str; 461 } 462 463 if (fmt[i + 1] == 'B') { 464 i++; 465 goto fmt_next; 466 } 467 468 /* disallow any further format extensions */ 469 if (fmt[i + 1] != 0 && 470 !isspace(fmt[i + 1]) && 471 !ispunct(fmt[i + 1])) 472 return -EINVAL; 473 474 goto fmt_next; 475 } else if (fmt[i] == 's') { 476 mod[fmt_cnt]++; 477 fmt_ptype = fmt[i]; 478 fmt_str: 479 if (str_seen) 480 /* allow only one '%s' per fmt string */ 481 return -EINVAL; 482 str_seen = true; 483 484 if (fmt[i + 1] != 0 && 485 !isspace(fmt[i + 1]) && 486 !ispunct(fmt[i + 1])) 487 return -EINVAL; 488 489 switch (fmt_cnt) { 490 case 0: 491 unsafe_ptr = (void *)(long)arg1; 492 arg1 = (long)buf; 493 break; 494 case 1: 495 unsafe_ptr = (void *)(long)arg2; 496 arg2 = (long)buf; 497 break; 498 case 2: 499 unsafe_ptr = (void *)(long)arg3; 500 arg3 = (long)buf; 501 break; 502 } 503 504 bpf_trace_copy_string(buf, unsafe_ptr, fmt_ptype, 505 sizeof(buf)); 506 goto fmt_next; 507 } 508 509 if (fmt[i] == 'l') { 510 mod[fmt_cnt]++; 511 i++; 512 } 513 514 if (fmt[i] != 'i' && fmt[i] != 'd' && 515 fmt[i] != 'u' && fmt[i] != 'x') 516 return -EINVAL; 517 fmt_next: 518 fmt_cnt++; 519 } 520 521 /* Horrid workaround for getting va_list handling working with different 522 * argument type combinations generically for 32 and 64 bit archs. 523 */ 524 #define __BPF_TP_EMIT() __BPF_ARG3_TP() 525 #define __BPF_TP(...) \ 526 bpf_do_trace_printk(fmt, ##__VA_ARGS__) 527 528 #define __BPF_ARG1_TP(...) \ 529 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ 530 ? __BPF_TP(arg1, ##__VA_ARGS__) \ 531 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ 532 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ 533 : __BPF_TP((u32)arg1, ##__VA_ARGS__))) 534 535 #define __BPF_ARG2_TP(...) \ 536 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ 537 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ 538 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ 539 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ 540 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) 541 542 #define __BPF_ARG3_TP(...) \ 543 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ 544 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ 545 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ 546 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ 547 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) 548 549 return __BPF_TP_EMIT(); 550 } 551 552 static const struct bpf_func_proto bpf_trace_printk_proto = { 553 .func = bpf_trace_printk, 554 .gpl_only = true, 555 .ret_type = RET_INTEGER, 556 .arg1_type = ARG_PTR_TO_MEM, 557 .arg2_type = ARG_CONST_SIZE, 558 }; 559 560 const struct bpf_func_proto *bpf_get_trace_printk_proto(void) 561 { 562 /* 563 * This program might be calling bpf_trace_printk, 564 * so enable the associated bpf_trace/bpf_trace_printk event. 565 * Repeat this each time as it is possible a user has 566 * disabled bpf_trace_printk events. By loading a program 567 * calling bpf_trace_printk() however the user has expressed 568 * the intent to see such events. 569 */ 570 if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1)) 571 pr_warn_ratelimited("could not enable bpf_trace_printk events"); 572 573 return &bpf_trace_printk_proto; 574 } 575 576 #define MAX_SEQ_PRINTF_VARARGS 12 577 #define MAX_SEQ_PRINTF_MAX_MEMCPY 6 578 #define MAX_SEQ_PRINTF_STR_LEN 128 579 580 struct bpf_seq_printf_buf { 581 char buf[MAX_SEQ_PRINTF_MAX_MEMCPY][MAX_SEQ_PRINTF_STR_LEN]; 582 }; 583 static DEFINE_PER_CPU(struct bpf_seq_printf_buf, bpf_seq_printf_buf); 584 static DEFINE_PER_CPU(int, bpf_seq_printf_buf_used); 585 586 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, 587 const void *, data, u32, data_len) 588 { 589 int err = -EINVAL, fmt_cnt = 0, memcpy_cnt = 0; 590 int i, buf_used, copy_size, num_args; 591 u64 params[MAX_SEQ_PRINTF_VARARGS]; 592 struct bpf_seq_printf_buf *bufs; 593 const u64 *args = data; 594 595 buf_used = this_cpu_inc_return(bpf_seq_printf_buf_used); 596 if (WARN_ON_ONCE(buf_used > 1)) { 597 err = -EBUSY; 598 goto out; 599 } 600 601 bufs = this_cpu_ptr(&bpf_seq_printf_buf); 602 603 /* 604 * bpf_check()->check_func_arg()->check_stack_boundary() 605 * guarantees that fmt points to bpf program stack, 606 * fmt_size bytes of it were initialized and fmt_size > 0 607 */ 608 if (fmt[--fmt_size] != 0) 609 goto out; 610 611 if (data_len & 7) 612 goto out; 613 614 for (i = 0; i < fmt_size; i++) { 615 if (fmt[i] == '%') { 616 if (fmt[i + 1] == '%') 617 i++; 618 else if (!data || !data_len) 619 goto out; 620 } 621 } 622 623 num_args = data_len / 8; 624 625 /* check format string for allowed specifiers */ 626 for (i = 0; i < fmt_size; i++) { 627 /* only printable ascii for now. */ 628 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) { 629 err = -EINVAL; 630 goto out; 631 } 632 633 if (fmt[i] != '%') 634 continue; 635 636 if (fmt[i + 1] == '%') { 637 i++; 638 continue; 639 } 640 641 if (fmt_cnt >= MAX_SEQ_PRINTF_VARARGS) { 642 err = -E2BIG; 643 goto out; 644 } 645 646 if (fmt_cnt >= num_args) { 647 err = -EINVAL; 648 goto out; 649 } 650 651 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ 652 i++; 653 654 /* skip optional "[0 +-][num]" width formating field */ 655 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' || 656 fmt[i] == ' ') 657 i++; 658 if (fmt[i] >= '1' && fmt[i] <= '9') { 659 i++; 660 while (fmt[i] >= '0' && fmt[i] <= '9') 661 i++; 662 } 663 664 if (fmt[i] == 's') { 665 void *unsafe_ptr; 666 667 /* try our best to copy */ 668 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { 669 err = -E2BIG; 670 goto out; 671 } 672 673 unsafe_ptr = (void *)(long)args[fmt_cnt]; 674 err = strncpy_from_kernel_nofault(bufs->buf[memcpy_cnt], 675 unsafe_ptr, MAX_SEQ_PRINTF_STR_LEN); 676 if (err < 0) 677 bufs->buf[memcpy_cnt][0] = '\0'; 678 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; 679 680 fmt_cnt++; 681 memcpy_cnt++; 682 continue; 683 } 684 685 if (fmt[i] == 'p') { 686 if (fmt[i + 1] == 0 || 687 fmt[i + 1] == 'K' || 688 fmt[i + 1] == 'x' || 689 fmt[i + 1] == 'B') { 690 /* just kernel pointers */ 691 params[fmt_cnt] = args[fmt_cnt]; 692 fmt_cnt++; 693 continue; 694 } 695 696 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */ 697 if (fmt[i + 1] != 'i' && fmt[i + 1] != 'I') { 698 err = -EINVAL; 699 goto out; 700 } 701 if (fmt[i + 2] != '4' && fmt[i + 2] != '6') { 702 err = -EINVAL; 703 goto out; 704 } 705 706 if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { 707 err = -E2BIG; 708 goto out; 709 } 710 711 712 copy_size = (fmt[i + 2] == '4') ? 4 : 16; 713 714 err = copy_from_kernel_nofault(bufs->buf[memcpy_cnt], 715 (void *) (long) args[fmt_cnt], 716 copy_size); 717 if (err < 0) 718 memset(bufs->buf[memcpy_cnt], 0, copy_size); 719 params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; 720 721 i += 2; 722 fmt_cnt++; 723 memcpy_cnt++; 724 continue; 725 } 726 727 if (fmt[i] == 'l') { 728 i++; 729 if (fmt[i] == 'l') 730 i++; 731 } 732 733 if (fmt[i] != 'i' && fmt[i] != 'd' && 734 fmt[i] != 'u' && fmt[i] != 'x' && 735 fmt[i] != 'X') { 736 err = -EINVAL; 737 goto out; 738 } 739 740 params[fmt_cnt] = args[fmt_cnt]; 741 fmt_cnt++; 742 } 743 744 /* Maximumly we can have MAX_SEQ_PRINTF_VARARGS parameter, just give 745 * all of them to seq_printf(). 746 */ 747 seq_printf(m, fmt, params[0], params[1], params[2], params[3], 748 params[4], params[5], params[6], params[7], params[8], 749 params[9], params[10], params[11]); 750 751 err = seq_has_overflowed(m) ? -EOVERFLOW : 0; 752 out: 753 this_cpu_dec(bpf_seq_printf_buf_used); 754 return err; 755 } 756 757 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file) 758 759 static const struct bpf_func_proto bpf_seq_printf_proto = { 760 .func = bpf_seq_printf, 761 .gpl_only = true, 762 .ret_type = RET_INTEGER, 763 .arg1_type = ARG_PTR_TO_BTF_ID, 764 .arg1_btf_id = &btf_seq_file_ids[0], 765 .arg2_type = ARG_PTR_TO_MEM, 766 .arg3_type = ARG_CONST_SIZE, 767 .arg4_type = ARG_PTR_TO_MEM_OR_NULL, 768 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 769 }; 770 771 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) 772 { 773 return seq_write(m, data, len) ? -EOVERFLOW : 0; 774 } 775 776 static const struct bpf_func_proto bpf_seq_write_proto = { 777 .func = bpf_seq_write, 778 .gpl_only = true, 779 .ret_type = RET_INTEGER, 780 .arg1_type = ARG_PTR_TO_BTF_ID, 781 .arg1_btf_id = &btf_seq_file_ids[0], 782 .arg2_type = ARG_PTR_TO_MEM, 783 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 784 }; 785 786 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr, 787 u32, btf_ptr_size, u64, flags) 788 { 789 const struct btf *btf; 790 s32 btf_id; 791 int ret; 792 793 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); 794 if (ret) 795 return ret; 796 797 return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags); 798 } 799 800 static const struct bpf_func_proto bpf_seq_printf_btf_proto = { 801 .func = bpf_seq_printf_btf, 802 .gpl_only = true, 803 .ret_type = RET_INTEGER, 804 .arg1_type = ARG_PTR_TO_BTF_ID, 805 .arg1_btf_id = &btf_seq_file_ids[0], 806 .arg2_type = ARG_PTR_TO_MEM, 807 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 808 .arg4_type = ARG_ANYTHING, 809 }; 810 811 static __always_inline int 812 get_map_perf_counter(struct bpf_map *map, u64 flags, 813 u64 *value, u64 *enabled, u64 *running) 814 { 815 struct bpf_array *array = container_of(map, struct bpf_array, map); 816 unsigned int cpu = smp_processor_id(); 817 u64 index = flags & BPF_F_INDEX_MASK; 818 struct bpf_event_entry *ee; 819 820 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) 821 return -EINVAL; 822 if (index == BPF_F_CURRENT_CPU) 823 index = cpu; 824 if (unlikely(index >= array->map.max_entries)) 825 return -E2BIG; 826 827 ee = READ_ONCE(array->ptrs[index]); 828 if (!ee) 829 return -ENOENT; 830 831 return perf_event_read_local(ee->event, value, enabled, running); 832 } 833 834 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) 835 { 836 u64 value = 0; 837 int err; 838 839 err = get_map_perf_counter(map, flags, &value, NULL, NULL); 840 /* 841 * this api is ugly since we miss [-22..-2] range of valid 842 * counter values, but that's uapi 843 */ 844 if (err) 845 return err; 846 return value; 847 } 848 849 static const struct bpf_func_proto bpf_perf_event_read_proto = { 850 .func = bpf_perf_event_read, 851 .gpl_only = true, 852 .ret_type = RET_INTEGER, 853 .arg1_type = ARG_CONST_MAP_PTR, 854 .arg2_type = ARG_ANYTHING, 855 }; 856 857 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, 858 struct bpf_perf_event_value *, buf, u32, size) 859 { 860 int err = -EINVAL; 861 862 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 863 goto clear; 864 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, 865 &buf->running); 866 if (unlikely(err)) 867 goto clear; 868 return 0; 869 clear: 870 memset(buf, 0, size); 871 return err; 872 } 873 874 static const struct bpf_func_proto bpf_perf_event_read_value_proto = { 875 .func = bpf_perf_event_read_value, 876 .gpl_only = true, 877 .ret_type = RET_INTEGER, 878 .arg1_type = ARG_CONST_MAP_PTR, 879 .arg2_type = ARG_ANYTHING, 880 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 881 .arg4_type = ARG_CONST_SIZE, 882 }; 883 884 static __always_inline u64 885 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, 886 u64 flags, struct perf_sample_data *sd) 887 { 888 struct bpf_array *array = container_of(map, struct bpf_array, map); 889 unsigned int cpu = smp_processor_id(); 890 u64 index = flags & BPF_F_INDEX_MASK; 891 struct bpf_event_entry *ee; 892 struct perf_event *event; 893 894 if (index == BPF_F_CURRENT_CPU) 895 index = cpu; 896 if (unlikely(index >= array->map.max_entries)) 897 return -E2BIG; 898 899 ee = READ_ONCE(array->ptrs[index]); 900 if (!ee) 901 return -ENOENT; 902 903 event = ee->event; 904 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || 905 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) 906 return -EINVAL; 907 908 if (unlikely(event->oncpu != cpu)) 909 return -EOPNOTSUPP; 910 911 return perf_event_output(event, sd, regs); 912 } 913 914 /* 915 * Support executing tracepoints in normal, irq, and nmi context that each call 916 * bpf_perf_event_output 917 */ 918 struct bpf_trace_sample_data { 919 struct perf_sample_data sds[3]; 920 }; 921 922 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds); 923 static DEFINE_PER_CPU(int, bpf_trace_nest_level); 924 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, 925 u64, flags, void *, data, u64, size) 926 { 927 struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds); 928 int nest_level = this_cpu_inc_return(bpf_trace_nest_level); 929 struct perf_raw_record raw = { 930 .frag = { 931 .size = size, 932 .data = data, 933 }, 934 }; 935 struct perf_sample_data *sd; 936 int err; 937 938 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) { 939 err = -EBUSY; 940 goto out; 941 } 942 943 sd = &sds->sds[nest_level - 1]; 944 945 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) { 946 err = -EINVAL; 947 goto out; 948 } 949 950 perf_sample_data_init(sd, 0, 0); 951 sd->raw = &raw; 952 953 err = __bpf_perf_event_output(regs, map, flags, sd); 954 955 out: 956 this_cpu_dec(bpf_trace_nest_level); 957 return err; 958 } 959 960 static const struct bpf_func_proto bpf_perf_event_output_proto = { 961 .func = bpf_perf_event_output, 962 .gpl_only = true, 963 .ret_type = RET_INTEGER, 964 .arg1_type = ARG_PTR_TO_CTX, 965 .arg2_type = ARG_CONST_MAP_PTR, 966 .arg3_type = ARG_ANYTHING, 967 .arg4_type = ARG_PTR_TO_MEM, 968 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 969 }; 970 971 static DEFINE_PER_CPU(int, bpf_event_output_nest_level); 972 struct bpf_nested_pt_regs { 973 struct pt_regs regs[3]; 974 }; 975 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs); 976 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds); 977 978 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, 979 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) 980 { 981 int nest_level = this_cpu_inc_return(bpf_event_output_nest_level); 982 struct perf_raw_frag frag = { 983 .copy = ctx_copy, 984 .size = ctx_size, 985 .data = ctx, 986 }; 987 struct perf_raw_record raw = { 988 .frag = { 989 { 990 .next = ctx_size ? &frag : NULL, 991 }, 992 .size = meta_size, 993 .data = meta, 994 }, 995 }; 996 struct perf_sample_data *sd; 997 struct pt_regs *regs; 998 u64 ret; 999 1000 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) { 1001 ret = -EBUSY; 1002 goto out; 1003 } 1004 sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]); 1005 regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]); 1006 1007 perf_fetch_caller_regs(regs); 1008 perf_sample_data_init(sd, 0, 0); 1009 sd->raw = &raw; 1010 1011 ret = __bpf_perf_event_output(regs, map, flags, sd); 1012 out: 1013 this_cpu_dec(bpf_event_output_nest_level); 1014 return ret; 1015 } 1016 1017 BPF_CALL_0(bpf_get_current_task) 1018 { 1019 return (long) current; 1020 } 1021 1022 const struct bpf_func_proto bpf_get_current_task_proto = { 1023 .func = bpf_get_current_task, 1024 .gpl_only = true, 1025 .ret_type = RET_INTEGER, 1026 }; 1027 1028 BPF_CALL_0(bpf_get_current_task_btf) 1029 { 1030 return (unsigned long) current; 1031 } 1032 1033 BTF_ID_LIST_SINGLE(bpf_get_current_btf_ids, struct, task_struct) 1034 1035 static const struct bpf_func_proto bpf_get_current_task_btf_proto = { 1036 .func = bpf_get_current_task_btf, 1037 .gpl_only = true, 1038 .ret_type = RET_PTR_TO_BTF_ID, 1039 .ret_btf_id = &bpf_get_current_btf_ids[0], 1040 }; 1041 1042 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) 1043 { 1044 struct bpf_array *array = container_of(map, struct bpf_array, map); 1045 struct cgroup *cgrp; 1046 1047 if (unlikely(idx >= array->map.max_entries)) 1048 return -E2BIG; 1049 1050 cgrp = READ_ONCE(array->ptrs[idx]); 1051 if (unlikely(!cgrp)) 1052 return -EAGAIN; 1053 1054 return task_under_cgroup_hierarchy(current, cgrp); 1055 } 1056 1057 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { 1058 .func = bpf_current_task_under_cgroup, 1059 .gpl_only = false, 1060 .ret_type = RET_INTEGER, 1061 .arg1_type = ARG_CONST_MAP_PTR, 1062 .arg2_type = ARG_ANYTHING, 1063 }; 1064 1065 struct send_signal_irq_work { 1066 struct irq_work irq_work; 1067 struct task_struct *task; 1068 u32 sig; 1069 enum pid_type type; 1070 }; 1071 1072 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work); 1073 1074 static void do_bpf_send_signal(struct irq_work *entry) 1075 { 1076 struct send_signal_irq_work *work; 1077 1078 work = container_of(entry, struct send_signal_irq_work, irq_work); 1079 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type); 1080 } 1081 1082 static int bpf_send_signal_common(u32 sig, enum pid_type type) 1083 { 1084 struct send_signal_irq_work *work = NULL; 1085 1086 /* Similar to bpf_probe_write_user, task needs to be 1087 * in a sound condition and kernel memory access be 1088 * permitted in order to send signal to the current 1089 * task. 1090 */ 1091 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING))) 1092 return -EPERM; 1093 if (unlikely(uaccess_kernel())) 1094 return -EPERM; 1095 if (unlikely(!nmi_uaccess_okay())) 1096 return -EPERM; 1097 1098 if (irqs_disabled()) { 1099 /* Do an early check on signal validity. Otherwise, 1100 * the error is lost in deferred irq_work. 1101 */ 1102 if (unlikely(!valid_signal(sig))) 1103 return -EINVAL; 1104 1105 work = this_cpu_ptr(&send_signal_work); 1106 if (atomic_read(&work->irq_work.flags) & IRQ_WORK_BUSY) 1107 return -EBUSY; 1108 1109 /* Add the current task, which is the target of sending signal, 1110 * to the irq_work. The current task may change when queued 1111 * irq works get executed. 1112 */ 1113 work->task = current; 1114 work->sig = sig; 1115 work->type = type; 1116 irq_work_queue(&work->irq_work); 1117 return 0; 1118 } 1119 1120 return group_send_sig_info(sig, SEND_SIG_PRIV, current, type); 1121 } 1122 1123 BPF_CALL_1(bpf_send_signal, u32, sig) 1124 { 1125 return bpf_send_signal_common(sig, PIDTYPE_TGID); 1126 } 1127 1128 static const struct bpf_func_proto bpf_send_signal_proto = { 1129 .func = bpf_send_signal, 1130 .gpl_only = false, 1131 .ret_type = RET_INTEGER, 1132 .arg1_type = ARG_ANYTHING, 1133 }; 1134 1135 BPF_CALL_1(bpf_send_signal_thread, u32, sig) 1136 { 1137 return bpf_send_signal_common(sig, PIDTYPE_PID); 1138 } 1139 1140 static const struct bpf_func_proto bpf_send_signal_thread_proto = { 1141 .func = bpf_send_signal_thread, 1142 .gpl_only = false, 1143 .ret_type = RET_INTEGER, 1144 .arg1_type = ARG_ANYTHING, 1145 }; 1146 1147 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz) 1148 { 1149 long len; 1150 char *p; 1151 1152 if (!sz) 1153 return 0; 1154 1155 p = d_path(path, buf, sz); 1156 if (IS_ERR(p)) { 1157 len = PTR_ERR(p); 1158 } else { 1159 len = buf + sz - p; 1160 memmove(buf, p, len); 1161 } 1162 1163 return len; 1164 } 1165 1166 BTF_SET_START(btf_allowlist_d_path) 1167 #ifdef CONFIG_SECURITY 1168 BTF_ID(func, security_file_permission) 1169 BTF_ID(func, security_inode_getattr) 1170 BTF_ID(func, security_file_open) 1171 #endif 1172 #ifdef CONFIG_SECURITY_PATH 1173 BTF_ID(func, security_path_truncate) 1174 #endif 1175 BTF_ID(func, vfs_truncate) 1176 BTF_ID(func, vfs_fallocate) 1177 BTF_ID(func, dentry_open) 1178 BTF_ID(func, vfs_getattr) 1179 BTF_ID(func, filp_close) 1180 BTF_SET_END(btf_allowlist_d_path) 1181 1182 static bool bpf_d_path_allowed(const struct bpf_prog *prog) 1183 { 1184 if (prog->type == BPF_PROG_TYPE_LSM) 1185 return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id); 1186 1187 return btf_id_set_contains(&btf_allowlist_d_path, 1188 prog->aux->attach_btf_id); 1189 } 1190 1191 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path) 1192 1193 static const struct bpf_func_proto bpf_d_path_proto = { 1194 .func = bpf_d_path, 1195 .gpl_only = false, 1196 .ret_type = RET_INTEGER, 1197 .arg1_type = ARG_PTR_TO_BTF_ID, 1198 .arg1_btf_id = &bpf_d_path_btf_ids[0], 1199 .arg2_type = ARG_PTR_TO_MEM, 1200 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 1201 .allowed = bpf_d_path_allowed, 1202 }; 1203 1204 #define BTF_F_ALL (BTF_F_COMPACT | BTF_F_NONAME | \ 1205 BTF_F_PTR_RAW | BTF_F_ZERO) 1206 1207 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size, 1208 u64 flags, const struct btf **btf, 1209 s32 *btf_id) 1210 { 1211 const struct btf_type *t; 1212 1213 if (unlikely(flags & ~(BTF_F_ALL))) 1214 return -EINVAL; 1215 1216 if (btf_ptr_size != sizeof(struct btf_ptr)) 1217 return -EINVAL; 1218 1219 *btf = bpf_get_btf_vmlinux(); 1220 1221 if (IS_ERR_OR_NULL(*btf)) 1222 return PTR_ERR(*btf); 1223 1224 if (ptr->type_id > 0) 1225 *btf_id = ptr->type_id; 1226 else 1227 return -EINVAL; 1228 1229 if (*btf_id > 0) 1230 t = btf_type_by_id(*btf, *btf_id); 1231 if (*btf_id <= 0 || !t) 1232 return -ENOENT; 1233 1234 return 0; 1235 } 1236 1237 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr, 1238 u32, btf_ptr_size, u64, flags) 1239 { 1240 const struct btf *btf; 1241 s32 btf_id; 1242 int ret; 1243 1244 ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id); 1245 if (ret) 1246 return ret; 1247 1248 return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size, 1249 flags); 1250 } 1251 1252 const struct bpf_func_proto bpf_snprintf_btf_proto = { 1253 .func = bpf_snprintf_btf, 1254 .gpl_only = false, 1255 .ret_type = RET_INTEGER, 1256 .arg1_type = ARG_PTR_TO_MEM, 1257 .arg2_type = ARG_CONST_SIZE, 1258 .arg3_type = ARG_PTR_TO_MEM, 1259 .arg4_type = ARG_CONST_SIZE, 1260 .arg5_type = ARG_ANYTHING, 1261 }; 1262 1263 const struct bpf_func_proto * 1264 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1265 { 1266 switch (func_id) { 1267 case BPF_FUNC_map_lookup_elem: 1268 return &bpf_map_lookup_elem_proto; 1269 case BPF_FUNC_map_update_elem: 1270 return &bpf_map_update_elem_proto; 1271 case BPF_FUNC_map_delete_elem: 1272 return &bpf_map_delete_elem_proto; 1273 case BPF_FUNC_map_push_elem: 1274 return &bpf_map_push_elem_proto; 1275 case BPF_FUNC_map_pop_elem: 1276 return &bpf_map_pop_elem_proto; 1277 case BPF_FUNC_map_peek_elem: 1278 return &bpf_map_peek_elem_proto; 1279 case BPF_FUNC_ktime_get_ns: 1280 return &bpf_ktime_get_ns_proto; 1281 case BPF_FUNC_ktime_get_boot_ns: 1282 return &bpf_ktime_get_boot_ns_proto; 1283 case BPF_FUNC_tail_call: 1284 return &bpf_tail_call_proto; 1285 case BPF_FUNC_get_current_pid_tgid: 1286 return &bpf_get_current_pid_tgid_proto; 1287 case BPF_FUNC_get_current_task: 1288 return &bpf_get_current_task_proto; 1289 case BPF_FUNC_get_current_task_btf: 1290 return &bpf_get_current_task_btf_proto; 1291 case BPF_FUNC_get_current_uid_gid: 1292 return &bpf_get_current_uid_gid_proto; 1293 case BPF_FUNC_get_current_comm: 1294 return &bpf_get_current_comm_proto; 1295 case BPF_FUNC_trace_printk: 1296 return bpf_get_trace_printk_proto(); 1297 case BPF_FUNC_get_smp_processor_id: 1298 return &bpf_get_smp_processor_id_proto; 1299 case BPF_FUNC_get_numa_node_id: 1300 return &bpf_get_numa_node_id_proto; 1301 case BPF_FUNC_perf_event_read: 1302 return &bpf_perf_event_read_proto; 1303 case BPF_FUNC_probe_write_user: 1304 return bpf_get_probe_write_proto(); 1305 case BPF_FUNC_current_task_under_cgroup: 1306 return &bpf_current_task_under_cgroup_proto; 1307 case BPF_FUNC_get_prandom_u32: 1308 return &bpf_get_prandom_u32_proto; 1309 case BPF_FUNC_probe_read_user: 1310 return &bpf_probe_read_user_proto; 1311 case BPF_FUNC_probe_read_kernel: 1312 return &bpf_probe_read_kernel_proto; 1313 case BPF_FUNC_probe_read_user_str: 1314 return &bpf_probe_read_user_str_proto; 1315 case BPF_FUNC_probe_read_kernel_str: 1316 return &bpf_probe_read_kernel_str_proto; 1317 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 1318 case BPF_FUNC_probe_read: 1319 return &bpf_probe_read_compat_proto; 1320 case BPF_FUNC_probe_read_str: 1321 return &bpf_probe_read_compat_str_proto; 1322 #endif 1323 #ifdef CONFIG_CGROUPS 1324 case BPF_FUNC_get_current_cgroup_id: 1325 return &bpf_get_current_cgroup_id_proto; 1326 #endif 1327 case BPF_FUNC_send_signal: 1328 return &bpf_send_signal_proto; 1329 case BPF_FUNC_send_signal_thread: 1330 return &bpf_send_signal_thread_proto; 1331 case BPF_FUNC_perf_event_read_value: 1332 return &bpf_perf_event_read_value_proto; 1333 case BPF_FUNC_get_ns_current_pid_tgid: 1334 return &bpf_get_ns_current_pid_tgid_proto; 1335 case BPF_FUNC_ringbuf_output: 1336 return &bpf_ringbuf_output_proto; 1337 case BPF_FUNC_ringbuf_reserve: 1338 return &bpf_ringbuf_reserve_proto; 1339 case BPF_FUNC_ringbuf_submit: 1340 return &bpf_ringbuf_submit_proto; 1341 case BPF_FUNC_ringbuf_discard: 1342 return &bpf_ringbuf_discard_proto; 1343 case BPF_FUNC_ringbuf_query: 1344 return &bpf_ringbuf_query_proto; 1345 case BPF_FUNC_jiffies64: 1346 return &bpf_jiffies64_proto; 1347 case BPF_FUNC_get_task_stack: 1348 return &bpf_get_task_stack_proto; 1349 case BPF_FUNC_copy_from_user: 1350 return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL; 1351 case BPF_FUNC_snprintf_btf: 1352 return &bpf_snprintf_btf_proto; 1353 case BPF_FUNC_bpf_per_cpu_ptr: 1354 return &bpf_per_cpu_ptr_proto; 1355 case BPF_FUNC_bpf_this_cpu_ptr: 1356 return &bpf_this_cpu_ptr_proto; 1357 default: 1358 return NULL; 1359 } 1360 } 1361 1362 static const struct bpf_func_proto * 1363 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1364 { 1365 switch (func_id) { 1366 case BPF_FUNC_perf_event_output: 1367 return &bpf_perf_event_output_proto; 1368 case BPF_FUNC_get_stackid: 1369 return &bpf_get_stackid_proto; 1370 case BPF_FUNC_get_stack: 1371 return &bpf_get_stack_proto; 1372 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 1373 case BPF_FUNC_override_return: 1374 return &bpf_override_return_proto; 1375 #endif 1376 default: 1377 return bpf_tracing_func_proto(func_id, prog); 1378 } 1379 } 1380 1381 /* bpf+kprobe programs can access fields of 'struct pt_regs' */ 1382 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1383 const struct bpf_prog *prog, 1384 struct bpf_insn_access_aux *info) 1385 { 1386 if (off < 0 || off >= sizeof(struct pt_regs)) 1387 return false; 1388 if (type != BPF_READ) 1389 return false; 1390 if (off % size != 0) 1391 return false; 1392 /* 1393 * Assertion for 32 bit to make sure last 8 byte access 1394 * (BPF_DW) to the last 4 byte member is disallowed. 1395 */ 1396 if (off + size > sizeof(struct pt_regs)) 1397 return false; 1398 1399 return true; 1400 } 1401 1402 const struct bpf_verifier_ops kprobe_verifier_ops = { 1403 .get_func_proto = kprobe_prog_func_proto, 1404 .is_valid_access = kprobe_prog_is_valid_access, 1405 }; 1406 1407 const struct bpf_prog_ops kprobe_prog_ops = { 1408 }; 1409 1410 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, 1411 u64, flags, void *, data, u64, size) 1412 { 1413 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 1414 1415 /* 1416 * r1 points to perf tracepoint buffer where first 8 bytes are hidden 1417 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it 1418 * from there and call the same bpf_perf_event_output() helper inline. 1419 */ 1420 return ____bpf_perf_event_output(regs, map, flags, data, size); 1421 } 1422 1423 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { 1424 .func = bpf_perf_event_output_tp, 1425 .gpl_only = true, 1426 .ret_type = RET_INTEGER, 1427 .arg1_type = ARG_PTR_TO_CTX, 1428 .arg2_type = ARG_CONST_MAP_PTR, 1429 .arg3_type = ARG_ANYTHING, 1430 .arg4_type = ARG_PTR_TO_MEM, 1431 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 1432 }; 1433 1434 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, 1435 u64, flags) 1436 { 1437 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 1438 1439 /* 1440 * Same comment as in bpf_perf_event_output_tp(), only that this time 1441 * the other helper's function body cannot be inlined due to being 1442 * external, thus we need to call raw helper function. 1443 */ 1444 return bpf_get_stackid((unsigned long) regs, (unsigned long) map, 1445 flags, 0, 0); 1446 } 1447 1448 static const struct bpf_func_proto bpf_get_stackid_proto_tp = { 1449 .func = bpf_get_stackid_tp, 1450 .gpl_only = true, 1451 .ret_type = RET_INTEGER, 1452 .arg1_type = ARG_PTR_TO_CTX, 1453 .arg2_type = ARG_CONST_MAP_PTR, 1454 .arg3_type = ARG_ANYTHING, 1455 }; 1456 1457 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, 1458 u64, flags) 1459 { 1460 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 1461 1462 return bpf_get_stack((unsigned long) regs, (unsigned long) buf, 1463 (unsigned long) size, flags, 0); 1464 } 1465 1466 static const struct bpf_func_proto bpf_get_stack_proto_tp = { 1467 .func = bpf_get_stack_tp, 1468 .gpl_only = true, 1469 .ret_type = RET_INTEGER, 1470 .arg1_type = ARG_PTR_TO_CTX, 1471 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 1472 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 1473 .arg4_type = ARG_ANYTHING, 1474 }; 1475 1476 static const struct bpf_func_proto * 1477 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1478 { 1479 switch (func_id) { 1480 case BPF_FUNC_perf_event_output: 1481 return &bpf_perf_event_output_proto_tp; 1482 case BPF_FUNC_get_stackid: 1483 return &bpf_get_stackid_proto_tp; 1484 case BPF_FUNC_get_stack: 1485 return &bpf_get_stack_proto_tp; 1486 default: 1487 return bpf_tracing_func_proto(func_id, prog); 1488 } 1489 } 1490 1491 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1492 const struct bpf_prog *prog, 1493 struct bpf_insn_access_aux *info) 1494 { 1495 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) 1496 return false; 1497 if (type != BPF_READ) 1498 return false; 1499 if (off % size != 0) 1500 return false; 1501 1502 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); 1503 return true; 1504 } 1505 1506 const struct bpf_verifier_ops tracepoint_verifier_ops = { 1507 .get_func_proto = tp_prog_func_proto, 1508 .is_valid_access = tp_prog_is_valid_access, 1509 }; 1510 1511 const struct bpf_prog_ops tracepoint_prog_ops = { 1512 }; 1513 1514 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, 1515 struct bpf_perf_event_value *, buf, u32, size) 1516 { 1517 int err = -EINVAL; 1518 1519 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 1520 goto clear; 1521 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, 1522 &buf->running); 1523 if (unlikely(err)) 1524 goto clear; 1525 return 0; 1526 clear: 1527 memset(buf, 0, size); 1528 return err; 1529 } 1530 1531 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { 1532 .func = bpf_perf_prog_read_value, 1533 .gpl_only = true, 1534 .ret_type = RET_INTEGER, 1535 .arg1_type = ARG_PTR_TO_CTX, 1536 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 1537 .arg3_type = ARG_CONST_SIZE, 1538 }; 1539 1540 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx, 1541 void *, buf, u32, size, u64, flags) 1542 { 1543 #ifndef CONFIG_X86 1544 return -ENOENT; 1545 #else 1546 static const u32 br_entry_size = sizeof(struct perf_branch_entry); 1547 struct perf_branch_stack *br_stack = ctx->data->br_stack; 1548 u32 to_copy; 1549 1550 if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE)) 1551 return -EINVAL; 1552 1553 if (unlikely(!br_stack)) 1554 return -EINVAL; 1555 1556 if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE) 1557 return br_stack->nr * br_entry_size; 1558 1559 if (!buf || (size % br_entry_size != 0)) 1560 return -EINVAL; 1561 1562 to_copy = min_t(u32, br_stack->nr * br_entry_size, size); 1563 memcpy(buf, br_stack->entries, to_copy); 1564 1565 return to_copy; 1566 #endif 1567 } 1568 1569 static const struct bpf_func_proto bpf_read_branch_records_proto = { 1570 .func = bpf_read_branch_records, 1571 .gpl_only = true, 1572 .ret_type = RET_INTEGER, 1573 .arg1_type = ARG_PTR_TO_CTX, 1574 .arg2_type = ARG_PTR_TO_MEM_OR_NULL, 1575 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 1576 .arg4_type = ARG_ANYTHING, 1577 }; 1578 1579 static const struct bpf_func_proto * 1580 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1581 { 1582 switch (func_id) { 1583 case BPF_FUNC_perf_event_output: 1584 return &bpf_perf_event_output_proto_tp; 1585 case BPF_FUNC_get_stackid: 1586 return &bpf_get_stackid_proto_pe; 1587 case BPF_FUNC_get_stack: 1588 return &bpf_get_stack_proto_pe; 1589 case BPF_FUNC_perf_prog_read_value: 1590 return &bpf_perf_prog_read_value_proto; 1591 case BPF_FUNC_read_branch_records: 1592 return &bpf_read_branch_records_proto; 1593 default: 1594 return bpf_tracing_func_proto(func_id, prog); 1595 } 1596 } 1597 1598 /* 1599 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp 1600 * to avoid potential recursive reuse issue when/if tracepoints are added 1601 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack. 1602 * 1603 * Since raw tracepoints run despite bpf_prog_active, support concurrent usage 1604 * in normal, irq, and nmi context. 1605 */ 1606 struct bpf_raw_tp_regs { 1607 struct pt_regs regs[3]; 1608 }; 1609 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs); 1610 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level); 1611 static struct pt_regs *get_bpf_raw_tp_regs(void) 1612 { 1613 struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs); 1614 int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level); 1615 1616 if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) { 1617 this_cpu_dec(bpf_raw_tp_nest_level); 1618 return ERR_PTR(-EBUSY); 1619 } 1620 1621 return &tp_regs->regs[nest_level - 1]; 1622 } 1623 1624 static void put_bpf_raw_tp_regs(void) 1625 { 1626 this_cpu_dec(bpf_raw_tp_nest_level); 1627 } 1628 1629 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, 1630 struct bpf_map *, map, u64, flags, void *, data, u64, size) 1631 { 1632 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1633 int ret; 1634 1635 if (IS_ERR(regs)) 1636 return PTR_ERR(regs); 1637 1638 perf_fetch_caller_regs(regs); 1639 ret = ____bpf_perf_event_output(regs, map, flags, data, size); 1640 1641 put_bpf_raw_tp_regs(); 1642 return ret; 1643 } 1644 1645 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { 1646 .func = bpf_perf_event_output_raw_tp, 1647 .gpl_only = true, 1648 .ret_type = RET_INTEGER, 1649 .arg1_type = ARG_PTR_TO_CTX, 1650 .arg2_type = ARG_CONST_MAP_PTR, 1651 .arg3_type = ARG_ANYTHING, 1652 .arg4_type = ARG_PTR_TO_MEM, 1653 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 1654 }; 1655 1656 extern const struct bpf_func_proto bpf_skb_output_proto; 1657 extern const struct bpf_func_proto bpf_xdp_output_proto; 1658 1659 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, 1660 struct bpf_map *, map, u64, flags) 1661 { 1662 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1663 int ret; 1664 1665 if (IS_ERR(regs)) 1666 return PTR_ERR(regs); 1667 1668 perf_fetch_caller_regs(regs); 1669 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ 1670 ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map, 1671 flags, 0, 0); 1672 put_bpf_raw_tp_regs(); 1673 return ret; 1674 } 1675 1676 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { 1677 .func = bpf_get_stackid_raw_tp, 1678 .gpl_only = true, 1679 .ret_type = RET_INTEGER, 1680 .arg1_type = ARG_PTR_TO_CTX, 1681 .arg2_type = ARG_CONST_MAP_PTR, 1682 .arg3_type = ARG_ANYTHING, 1683 }; 1684 1685 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, 1686 void *, buf, u32, size, u64, flags) 1687 { 1688 struct pt_regs *regs = get_bpf_raw_tp_regs(); 1689 int ret; 1690 1691 if (IS_ERR(regs)) 1692 return PTR_ERR(regs); 1693 1694 perf_fetch_caller_regs(regs); 1695 ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf, 1696 (unsigned long) size, flags, 0); 1697 put_bpf_raw_tp_regs(); 1698 return ret; 1699 } 1700 1701 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { 1702 .func = bpf_get_stack_raw_tp, 1703 .gpl_only = true, 1704 .ret_type = RET_INTEGER, 1705 .arg1_type = ARG_PTR_TO_CTX, 1706 .arg2_type = ARG_PTR_TO_MEM, 1707 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 1708 .arg4_type = ARG_ANYTHING, 1709 }; 1710 1711 static const struct bpf_func_proto * 1712 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1713 { 1714 switch (func_id) { 1715 case BPF_FUNC_perf_event_output: 1716 return &bpf_perf_event_output_proto_raw_tp; 1717 case BPF_FUNC_get_stackid: 1718 return &bpf_get_stackid_proto_raw_tp; 1719 case BPF_FUNC_get_stack: 1720 return &bpf_get_stack_proto_raw_tp; 1721 default: 1722 return bpf_tracing_func_proto(func_id, prog); 1723 } 1724 } 1725 1726 const struct bpf_func_proto * 1727 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 1728 { 1729 switch (func_id) { 1730 #ifdef CONFIG_NET 1731 case BPF_FUNC_skb_output: 1732 return &bpf_skb_output_proto; 1733 case BPF_FUNC_xdp_output: 1734 return &bpf_xdp_output_proto; 1735 case BPF_FUNC_skc_to_tcp6_sock: 1736 return &bpf_skc_to_tcp6_sock_proto; 1737 case BPF_FUNC_skc_to_tcp_sock: 1738 return &bpf_skc_to_tcp_sock_proto; 1739 case BPF_FUNC_skc_to_tcp_timewait_sock: 1740 return &bpf_skc_to_tcp_timewait_sock_proto; 1741 case BPF_FUNC_skc_to_tcp_request_sock: 1742 return &bpf_skc_to_tcp_request_sock_proto; 1743 case BPF_FUNC_skc_to_udp6_sock: 1744 return &bpf_skc_to_udp6_sock_proto; 1745 case BPF_FUNC_sk_storage_get: 1746 return &bpf_sk_storage_get_tracing_proto; 1747 case BPF_FUNC_sk_storage_delete: 1748 return &bpf_sk_storage_delete_tracing_proto; 1749 #endif 1750 case BPF_FUNC_seq_printf: 1751 return prog->expected_attach_type == BPF_TRACE_ITER ? 1752 &bpf_seq_printf_proto : 1753 NULL; 1754 case BPF_FUNC_seq_write: 1755 return prog->expected_attach_type == BPF_TRACE_ITER ? 1756 &bpf_seq_write_proto : 1757 NULL; 1758 case BPF_FUNC_seq_printf_btf: 1759 return prog->expected_attach_type == BPF_TRACE_ITER ? 1760 &bpf_seq_printf_btf_proto : 1761 NULL; 1762 case BPF_FUNC_d_path: 1763 return &bpf_d_path_proto; 1764 default: 1765 return raw_tp_prog_func_proto(func_id, prog); 1766 } 1767 } 1768 1769 static bool raw_tp_prog_is_valid_access(int off, int size, 1770 enum bpf_access_type type, 1771 const struct bpf_prog *prog, 1772 struct bpf_insn_access_aux *info) 1773 { 1774 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) 1775 return false; 1776 if (type != BPF_READ) 1777 return false; 1778 if (off % size != 0) 1779 return false; 1780 return true; 1781 } 1782 1783 static bool tracing_prog_is_valid_access(int off, int size, 1784 enum bpf_access_type type, 1785 const struct bpf_prog *prog, 1786 struct bpf_insn_access_aux *info) 1787 { 1788 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) 1789 return false; 1790 if (type != BPF_READ) 1791 return false; 1792 if (off % size != 0) 1793 return false; 1794 return btf_ctx_access(off, size, type, prog, info); 1795 } 1796 1797 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog, 1798 const union bpf_attr *kattr, 1799 union bpf_attr __user *uattr) 1800 { 1801 return -ENOTSUPP; 1802 } 1803 1804 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { 1805 .get_func_proto = raw_tp_prog_func_proto, 1806 .is_valid_access = raw_tp_prog_is_valid_access, 1807 }; 1808 1809 const struct bpf_prog_ops raw_tracepoint_prog_ops = { 1810 #ifdef CONFIG_NET 1811 .test_run = bpf_prog_test_run_raw_tp, 1812 #endif 1813 }; 1814 1815 const struct bpf_verifier_ops tracing_verifier_ops = { 1816 .get_func_proto = tracing_prog_func_proto, 1817 .is_valid_access = tracing_prog_is_valid_access, 1818 }; 1819 1820 const struct bpf_prog_ops tracing_prog_ops = { 1821 .test_run = bpf_prog_test_run_tracing, 1822 }; 1823 1824 static bool raw_tp_writable_prog_is_valid_access(int off, int size, 1825 enum bpf_access_type type, 1826 const struct bpf_prog *prog, 1827 struct bpf_insn_access_aux *info) 1828 { 1829 if (off == 0) { 1830 if (size != sizeof(u64) || type != BPF_READ) 1831 return false; 1832 info->reg_type = PTR_TO_TP_BUFFER; 1833 } 1834 return raw_tp_prog_is_valid_access(off, size, type, prog, info); 1835 } 1836 1837 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { 1838 .get_func_proto = raw_tp_prog_func_proto, 1839 .is_valid_access = raw_tp_writable_prog_is_valid_access, 1840 }; 1841 1842 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { 1843 }; 1844 1845 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1846 const struct bpf_prog *prog, 1847 struct bpf_insn_access_aux *info) 1848 { 1849 const int size_u64 = sizeof(u64); 1850 1851 if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) 1852 return false; 1853 if (type != BPF_READ) 1854 return false; 1855 if (off % size != 0) { 1856 if (sizeof(unsigned long) != 4) 1857 return false; 1858 if (size != 8) 1859 return false; 1860 if (off % size != 4) 1861 return false; 1862 } 1863 1864 switch (off) { 1865 case bpf_ctx_range(struct bpf_perf_event_data, sample_period): 1866 bpf_ctx_record_field_size(info, size_u64); 1867 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1868 return false; 1869 break; 1870 case bpf_ctx_range(struct bpf_perf_event_data, addr): 1871 bpf_ctx_record_field_size(info, size_u64); 1872 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1873 return false; 1874 break; 1875 default: 1876 if (size != sizeof(long)) 1877 return false; 1878 } 1879 1880 return true; 1881 } 1882 1883 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, 1884 const struct bpf_insn *si, 1885 struct bpf_insn *insn_buf, 1886 struct bpf_prog *prog, u32 *target_size) 1887 { 1888 struct bpf_insn *insn = insn_buf; 1889 1890 switch (si->off) { 1891 case offsetof(struct bpf_perf_event_data, sample_period): 1892 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1893 data), si->dst_reg, si->src_reg, 1894 offsetof(struct bpf_perf_event_data_kern, data)); 1895 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1896 bpf_target_off(struct perf_sample_data, period, 8, 1897 target_size)); 1898 break; 1899 case offsetof(struct bpf_perf_event_data, addr): 1900 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1901 data), si->dst_reg, si->src_reg, 1902 offsetof(struct bpf_perf_event_data_kern, data)); 1903 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1904 bpf_target_off(struct perf_sample_data, addr, 8, 1905 target_size)); 1906 break; 1907 default: 1908 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1909 regs), si->dst_reg, si->src_reg, 1910 offsetof(struct bpf_perf_event_data_kern, regs)); 1911 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, 1912 si->off); 1913 break; 1914 } 1915 1916 return insn - insn_buf; 1917 } 1918 1919 const struct bpf_verifier_ops perf_event_verifier_ops = { 1920 .get_func_proto = pe_prog_func_proto, 1921 .is_valid_access = pe_prog_is_valid_access, 1922 .convert_ctx_access = pe_prog_convert_ctx_access, 1923 }; 1924 1925 const struct bpf_prog_ops perf_event_prog_ops = { 1926 }; 1927 1928 static DEFINE_MUTEX(bpf_event_mutex); 1929 1930 #define BPF_TRACE_MAX_PROGS 64 1931 1932 int perf_event_attach_bpf_prog(struct perf_event *event, 1933 struct bpf_prog *prog) 1934 { 1935 struct bpf_prog_array *old_array; 1936 struct bpf_prog_array *new_array; 1937 int ret = -EEXIST; 1938 1939 /* 1940 * Kprobe override only works if they are on the function entry, 1941 * and only if they are on the opt-in list. 1942 */ 1943 if (prog->kprobe_override && 1944 (!trace_kprobe_on_func_entry(event->tp_event) || 1945 !trace_kprobe_error_injectable(event->tp_event))) 1946 return -EINVAL; 1947 1948 mutex_lock(&bpf_event_mutex); 1949 1950 if (event->prog) 1951 goto unlock; 1952 1953 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1954 if (old_array && 1955 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { 1956 ret = -E2BIG; 1957 goto unlock; 1958 } 1959 1960 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); 1961 if (ret < 0) 1962 goto unlock; 1963 1964 /* set the new array to event->tp_event and set event->prog */ 1965 event->prog = prog; 1966 rcu_assign_pointer(event->tp_event->prog_array, new_array); 1967 bpf_prog_array_free(old_array); 1968 1969 unlock: 1970 mutex_unlock(&bpf_event_mutex); 1971 return ret; 1972 } 1973 1974 void perf_event_detach_bpf_prog(struct perf_event *event) 1975 { 1976 struct bpf_prog_array *old_array; 1977 struct bpf_prog_array *new_array; 1978 int ret; 1979 1980 mutex_lock(&bpf_event_mutex); 1981 1982 if (!event->prog) 1983 goto unlock; 1984 1985 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1986 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); 1987 if (ret == -ENOENT) 1988 goto unlock; 1989 if (ret < 0) { 1990 bpf_prog_array_delete_safe(old_array, event->prog); 1991 } else { 1992 rcu_assign_pointer(event->tp_event->prog_array, new_array); 1993 bpf_prog_array_free(old_array); 1994 } 1995 1996 bpf_prog_put(event->prog); 1997 event->prog = NULL; 1998 1999 unlock: 2000 mutex_unlock(&bpf_event_mutex); 2001 } 2002 2003 int perf_event_query_prog_array(struct perf_event *event, void __user *info) 2004 { 2005 struct perf_event_query_bpf __user *uquery = info; 2006 struct perf_event_query_bpf query = {}; 2007 struct bpf_prog_array *progs; 2008 u32 *ids, prog_cnt, ids_len; 2009 int ret; 2010 2011 if (!perfmon_capable()) 2012 return -EPERM; 2013 if (event->attr.type != PERF_TYPE_TRACEPOINT) 2014 return -EINVAL; 2015 if (copy_from_user(&query, uquery, sizeof(query))) 2016 return -EFAULT; 2017 2018 ids_len = query.ids_len; 2019 if (ids_len > BPF_TRACE_MAX_PROGS) 2020 return -E2BIG; 2021 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); 2022 if (!ids) 2023 return -ENOMEM; 2024 /* 2025 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which 2026 * is required when user only wants to check for uquery->prog_cnt. 2027 * There is no need to check for it since the case is handled 2028 * gracefully in bpf_prog_array_copy_info. 2029 */ 2030 2031 mutex_lock(&bpf_event_mutex); 2032 progs = bpf_event_rcu_dereference(event->tp_event->prog_array); 2033 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); 2034 mutex_unlock(&bpf_event_mutex); 2035 2036 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || 2037 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) 2038 ret = -EFAULT; 2039 2040 kfree(ids); 2041 return ret; 2042 } 2043 2044 extern struct bpf_raw_event_map __start__bpf_raw_tp[]; 2045 extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; 2046 2047 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) 2048 { 2049 struct bpf_raw_event_map *btp = __start__bpf_raw_tp; 2050 2051 for (; btp < __stop__bpf_raw_tp; btp++) { 2052 if (!strcmp(btp->tp->name, name)) 2053 return btp; 2054 } 2055 2056 return bpf_get_raw_tracepoint_module(name); 2057 } 2058 2059 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) 2060 { 2061 struct module *mod = __module_address((unsigned long)btp); 2062 2063 if (mod) 2064 module_put(mod); 2065 } 2066 2067 static __always_inline 2068 void __bpf_trace_run(struct bpf_prog *prog, u64 *args) 2069 { 2070 cant_sleep(); 2071 rcu_read_lock(); 2072 (void) BPF_PROG_RUN(prog, args); 2073 rcu_read_unlock(); 2074 } 2075 2076 #define UNPACK(...) __VA_ARGS__ 2077 #define REPEAT_1(FN, DL, X, ...) FN(X) 2078 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) 2079 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) 2080 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) 2081 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) 2082 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) 2083 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) 2084 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) 2085 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) 2086 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) 2087 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) 2088 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) 2089 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) 2090 2091 #define SARG(X) u64 arg##X 2092 #define COPY(X) args[X] = arg##X 2093 2094 #define __DL_COM (,) 2095 #define __DL_SEM (;) 2096 2097 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 2098 2099 #define BPF_TRACE_DEFN_x(x) \ 2100 void bpf_trace_run##x(struct bpf_prog *prog, \ 2101 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ 2102 { \ 2103 u64 args[x]; \ 2104 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ 2105 __bpf_trace_run(prog, args); \ 2106 } \ 2107 EXPORT_SYMBOL_GPL(bpf_trace_run##x) 2108 BPF_TRACE_DEFN_x(1); 2109 BPF_TRACE_DEFN_x(2); 2110 BPF_TRACE_DEFN_x(3); 2111 BPF_TRACE_DEFN_x(4); 2112 BPF_TRACE_DEFN_x(5); 2113 BPF_TRACE_DEFN_x(6); 2114 BPF_TRACE_DEFN_x(7); 2115 BPF_TRACE_DEFN_x(8); 2116 BPF_TRACE_DEFN_x(9); 2117 BPF_TRACE_DEFN_x(10); 2118 BPF_TRACE_DEFN_x(11); 2119 BPF_TRACE_DEFN_x(12); 2120 2121 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 2122 { 2123 struct tracepoint *tp = btp->tp; 2124 2125 /* 2126 * check that program doesn't access arguments beyond what's 2127 * available in this tracepoint 2128 */ 2129 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) 2130 return -EINVAL; 2131 2132 if (prog->aux->max_tp_access > btp->writable_size) 2133 return -EINVAL; 2134 2135 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog); 2136 } 2137 2138 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 2139 { 2140 return __bpf_probe_register(btp, prog); 2141 } 2142 2143 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 2144 { 2145 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); 2146 } 2147 2148 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, 2149 u32 *fd_type, const char **buf, 2150 u64 *probe_offset, u64 *probe_addr) 2151 { 2152 bool is_tracepoint, is_syscall_tp; 2153 struct bpf_prog *prog; 2154 int flags, err = 0; 2155 2156 prog = event->prog; 2157 if (!prog) 2158 return -ENOENT; 2159 2160 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ 2161 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) 2162 return -EOPNOTSUPP; 2163 2164 *prog_id = prog->aux->id; 2165 flags = event->tp_event->flags; 2166 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; 2167 is_syscall_tp = is_syscall_trace_event(event->tp_event); 2168 2169 if (is_tracepoint || is_syscall_tp) { 2170 *buf = is_tracepoint ? event->tp_event->tp->name 2171 : event->tp_event->name; 2172 *fd_type = BPF_FD_TYPE_TRACEPOINT; 2173 *probe_offset = 0x0; 2174 *probe_addr = 0x0; 2175 } else { 2176 /* kprobe/uprobe */ 2177 err = -EOPNOTSUPP; 2178 #ifdef CONFIG_KPROBE_EVENTS 2179 if (flags & TRACE_EVENT_FL_KPROBE) 2180 err = bpf_get_kprobe_info(event, fd_type, buf, 2181 probe_offset, probe_addr, 2182 event->attr.type == PERF_TYPE_TRACEPOINT); 2183 #endif 2184 #ifdef CONFIG_UPROBE_EVENTS 2185 if (flags & TRACE_EVENT_FL_UPROBE) 2186 err = bpf_get_uprobe_info(event, fd_type, buf, 2187 probe_offset, 2188 event->attr.type == PERF_TYPE_TRACEPOINT); 2189 #endif 2190 } 2191 2192 return err; 2193 } 2194 2195 static int __init send_signal_irq_work_init(void) 2196 { 2197 int cpu; 2198 struct send_signal_irq_work *work; 2199 2200 for_each_possible_cpu(cpu) { 2201 work = per_cpu_ptr(&send_signal_work, cpu); 2202 init_irq_work(&work->irq_work, do_bpf_send_signal); 2203 } 2204 return 0; 2205 } 2206 2207 subsys_initcall(send_signal_irq_work_init); 2208 2209 #ifdef CONFIG_MODULES 2210 static int bpf_event_notify(struct notifier_block *nb, unsigned long op, 2211 void *module) 2212 { 2213 struct bpf_trace_module *btm, *tmp; 2214 struct module *mod = module; 2215 int ret = 0; 2216 2217 if (mod->num_bpf_raw_events == 0 || 2218 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) 2219 goto out; 2220 2221 mutex_lock(&bpf_module_mutex); 2222 2223 switch (op) { 2224 case MODULE_STATE_COMING: 2225 btm = kzalloc(sizeof(*btm), GFP_KERNEL); 2226 if (btm) { 2227 btm->module = module; 2228 list_add(&btm->list, &bpf_trace_modules); 2229 } else { 2230 ret = -ENOMEM; 2231 } 2232 break; 2233 case MODULE_STATE_GOING: 2234 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { 2235 if (btm->module == module) { 2236 list_del(&btm->list); 2237 kfree(btm); 2238 break; 2239 } 2240 } 2241 break; 2242 } 2243 2244 mutex_unlock(&bpf_module_mutex); 2245 2246 out: 2247 return notifier_from_errno(ret); 2248 } 2249 2250 static struct notifier_block bpf_module_nb = { 2251 .notifier_call = bpf_event_notify, 2252 }; 2253 2254 static int __init bpf_event_init(void) 2255 { 2256 register_module_notifier(&bpf_module_nb); 2257 return 0; 2258 } 2259 2260 fs_initcall(bpf_event_init); 2261 #endif /* CONFIG_MODULES */ 2262