1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2019 Facebook */ 3 #include <linux/hash.h> 4 #include <linux/bpf.h> 5 #include <linux/filter.h> 6 #include <linux/ftrace.h> 7 #include <linux/rbtree_latch.h> 8 #include <linux/perf_event.h> 9 #include <linux/btf.h> 10 #include <linux/rcupdate_trace.h> 11 #include <linux/rcupdate_wait.h> 12 #include <linux/static_call.h> 13 #include <linux/bpf_verifier.h> 14 #include <linux/bpf_lsm.h> 15 #include <linux/delay.h> 16 17 /* dummy _ops. The verifier will operate on target program's ops. */ 18 const struct bpf_verifier_ops bpf_extension_verifier_ops = { 19 }; 20 const struct bpf_prog_ops bpf_extension_prog_ops = { 21 }; 22 23 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */ 24 #define TRAMPOLINE_HASH_BITS 10 25 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS) 26 27 static struct hlist_head trampoline_key_table[TRAMPOLINE_TABLE_SIZE]; 28 static struct hlist_head trampoline_ip_table[TRAMPOLINE_TABLE_SIZE]; 29 30 /* serializes access to trampoline tables */ 31 static DEFINE_MUTEX(trampoline_mutex); 32 33 /* 34 * Keep 32 trampoline locks (5 bits) in the pool so trampoline_lock_all() 35 * stays below MAX_LOCK_DEPTH. Each pool slot has a distinct lockdep 36 * class because trampoline_lock_all() takes all pool mutexes at once; 37 * otherwise lockdep would report recursive locking on same-class mutexes. 38 */ 39 #define TRAMPOLINE_LOCKS_BITS 5 40 #define TRAMPOLINE_LOCKS_TABLE_SIZE (1 << TRAMPOLINE_LOCKS_BITS) 41 42 static struct { 43 struct mutex mutex; 44 struct lock_class_key key; 45 } trampoline_locks[TRAMPOLINE_LOCKS_TABLE_SIZE]; 46 47 static struct mutex *select_trampoline_lock(struct bpf_trampoline *tr) 48 { 49 return &trampoline_locks[hash_ptr(tr, TRAMPOLINE_LOCKS_BITS)].mutex; 50 } 51 52 static void trampoline_lock(struct bpf_trampoline *tr) 53 { 54 mutex_lock(select_trampoline_lock(tr)); 55 } 56 57 static void trampoline_unlock(struct bpf_trampoline *tr) 58 { 59 mutex_unlock(select_trampoline_lock(tr)); 60 } 61 62 struct bpf_trampoline_ops { 63 int (*register_fentry)(struct bpf_trampoline *tr, struct bpf_tramp_image *im, void *data); 64 int (*unregister_fentry)(struct bpf_trampoline *tr, u32 orig_flags, void *data); 65 int (*modify_fentry)(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, 66 bool lock_direct_mutex, void *data); 67 }; 68 69 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 70 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex, 71 const struct bpf_trampoline_ops *ops, void *data); 72 static const struct bpf_trampoline_ops trampoline_ops; 73 74 #ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS 75 static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip) 76 { 77 struct hlist_head *head_ip; 78 struct bpf_trampoline *tr; 79 80 mutex_lock(&trampoline_mutex); 81 head_ip = &trampoline_ip_table[hash_64(ip, TRAMPOLINE_HASH_BITS)]; 82 hlist_for_each_entry(tr, head_ip, hlist_ip) { 83 if (tr->ip == ip) 84 goto out; 85 } 86 tr = NULL; 87 out: 88 mutex_unlock(&trampoline_mutex); 89 return tr; 90 } 91 #else 92 static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip) 93 { 94 return ops->private; 95 } 96 #endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */ 97 98 static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, unsigned long ip, 99 enum ftrace_ops_cmd cmd) 100 { 101 struct bpf_trampoline *tr; 102 int ret = 0; 103 104 tr = direct_ops_ip_lookup(ops, ip); 105 if (!tr) 106 return -EINVAL; 107 108 if (cmd == FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF) { 109 /* This is called inside register_ftrace_direct_multi(), so 110 * trampoline's mutex is already locked. 111 */ 112 lockdep_assert_held_once(select_trampoline_lock(tr)); 113 114 /* Instead of updating the trampoline here, we propagate 115 * -EAGAIN to register_ftrace_direct(). Then we can 116 * retry register_ftrace_direct() after updating the 117 * trampoline. 118 */ 119 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && 120 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) { 121 if (WARN_ON_ONCE(tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY)) 122 return -EBUSY; 123 124 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; 125 return -EAGAIN; 126 } 127 128 return 0; 129 } 130 131 /* The normal locking order is 132 * select_trampoline_lock(tr) => direct_mutex (ftrace.c) => ftrace_lock (ftrace.c) 133 * 134 * The following two commands are called from 135 * 136 * prepare_direct_functions_for_ipmodify 137 * cleanup_direct_functions_after_ipmodify 138 * 139 * In both cases, direct_mutex is already locked. Use 140 * mutex_trylock(select_trampoline_lock(tr)) to avoid deadlock in race condition 141 * (something else holds the same pool lock). 142 */ 143 if (!mutex_trylock(select_trampoline_lock(tr))) { 144 /* sleep 1 ms to make sure whatever holding select_trampoline_lock(tr) 145 * makes some progress. 146 */ 147 msleep(1); 148 return -EAGAIN; 149 } 150 151 switch (cmd) { 152 case FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER: 153 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; 154 155 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && 156 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) 157 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */, 158 &trampoline_ops, NULL); 159 break; 160 case FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER: 161 tr->flags &= ~BPF_TRAMP_F_SHARE_IPMODIFY; 162 163 if (tr->flags & BPF_TRAMP_F_ORIG_STACK) 164 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */, 165 &trampoline_ops, NULL); 166 break; 167 default: 168 ret = -EINVAL; 169 break; 170 } 171 172 trampoline_unlock(tr); 173 return ret; 174 } 175 #endif 176 177 bool bpf_prog_has_trampoline(const struct bpf_prog *prog) 178 { 179 enum bpf_attach_type eatype = prog->expected_attach_type; 180 enum bpf_prog_type ptype = prog->type; 181 182 switch (ptype) { 183 case BPF_PROG_TYPE_TRACING: 184 if (eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT || 185 eatype == BPF_MODIFY_RETURN || eatype == BPF_TRACE_FSESSION || 186 eatype == BPF_TRACE_FENTRY_MULTI || eatype == BPF_TRACE_FEXIT_MULTI || 187 eatype == BPF_TRACE_FSESSION_MULTI) 188 return true; 189 return false; 190 case BPF_PROG_TYPE_LSM: 191 return eatype == BPF_LSM_MAC; 192 default: 193 return false; 194 } 195 } 196 197 void bpf_image_ksym_init(void *data, unsigned int size, struct bpf_ksym *ksym) 198 { 199 ksym->start = (unsigned long) data; 200 ksym->end = ksym->start + size; 201 } 202 203 void bpf_image_ksym_add(struct bpf_ksym *ksym) 204 { 205 bpf_ksym_add(ksym); 206 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 207 PAGE_SIZE, false, ksym->name); 208 } 209 210 void bpf_image_ksym_del(struct bpf_ksym *ksym) 211 { 212 bpf_ksym_del(ksym); 213 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 214 PAGE_SIZE, true, ksym->name); 215 } 216 217 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 218 #ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS 219 /* 220 * We have only single direct_ops which contains all the direct call 221 * sites and is the only global ftrace_ops for all trampolines. 222 * 223 * We use 'update_ftrace_direct_*' api for attachment. 224 */ 225 struct ftrace_ops direct_ops = { 226 .ops_func = bpf_tramp_ftrace_ops_func, 227 }; 228 229 static int direct_ops_alloc(struct bpf_trampoline *tr) 230 { 231 tr->fops = &direct_ops; 232 return 0; 233 } 234 235 static void direct_ops_free(struct bpf_trampoline *tr) { } 236 237 static struct ftrace_hash *hash_from_ip(struct bpf_trampoline *tr, void *ptr) 238 { 239 unsigned long ip, addr = (unsigned long) ptr; 240 struct ftrace_hash *hash; 241 242 ip = ftrace_location(tr->ip); 243 if (!ip) 244 return NULL; 245 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 246 if (!hash) 247 return NULL; 248 if (bpf_trampoline_use_jmp(tr->flags)) 249 addr = ftrace_jmp_set(addr); 250 if (!add_ftrace_hash_entry_direct(hash, ip, addr)) { 251 free_ftrace_hash(hash); 252 return NULL; 253 } 254 return hash; 255 } 256 257 static int direct_ops_add(struct bpf_trampoline *tr, void *addr) 258 { 259 struct ftrace_hash *hash = hash_from_ip(tr, addr); 260 int err; 261 262 if (!hash) 263 return -ENOMEM; 264 err = update_ftrace_direct_add(tr->fops, hash); 265 free_ftrace_hash(hash); 266 return err; 267 } 268 269 static int direct_ops_del(struct bpf_trampoline *tr, void *addr) 270 { 271 struct ftrace_hash *hash = hash_from_ip(tr, addr); 272 int err; 273 274 if (!hash) 275 return -ENOMEM; 276 err = update_ftrace_direct_del(tr->fops, hash); 277 free_ftrace_hash(hash); 278 return err; 279 } 280 281 static int direct_ops_mod(struct bpf_trampoline *tr, void *addr, bool lock_direct_mutex) 282 { 283 struct ftrace_hash *hash = hash_from_ip(tr, addr); 284 int err; 285 286 if (!hash) 287 return -ENOMEM; 288 err = update_ftrace_direct_mod(tr->fops, hash, lock_direct_mutex); 289 free_ftrace_hash(hash); 290 return err; 291 } 292 #else 293 /* 294 * We allocate ftrace_ops object for each trampoline and it contains 295 * call site specific for that trampoline. 296 * 297 * We use *_ftrace_direct api for attachment. 298 */ 299 static int direct_ops_alloc(struct bpf_trampoline *tr) 300 { 301 tr->fops = kzalloc_obj(struct ftrace_ops); 302 if (!tr->fops) 303 return -ENOMEM; 304 tr->fops->private = tr; 305 tr->fops->ops_func = bpf_tramp_ftrace_ops_func; 306 return 0; 307 } 308 309 static void direct_ops_free(struct bpf_trampoline *tr) 310 { 311 if (!tr->fops) 312 return; 313 ftrace_free_filter(tr->fops); 314 kfree(tr->fops); 315 } 316 317 static int direct_ops_add(struct bpf_trampoline *tr, void *ptr) 318 { 319 unsigned long addr = (unsigned long) ptr; 320 struct ftrace_ops *ops = tr->fops; 321 int ret; 322 323 if (bpf_trampoline_use_jmp(tr->flags)) 324 addr = ftrace_jmp_set(addr); 325 326 ret = ftrace_set_filter_ip(ops, tr->ip, 0, 1); 327 if (ret) 328 return ret; 329 return register_ftrace_direct(ops, addr); 330 } 331 332 static int direct_ops_del(struct bpf_trampoline *tr, void *addr) 333 { 334 return unregister_ftrace_direct(tr->fops, (long)addr, false); 335 } 336 337 static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex) 338 { 339 unsigned long addr = (unsigned long) ptr; 340 struct ftrace_ops *ops = tr->fops; 341 342 if (bpf_trampoline_use_jmp(tr->flags)) 343 addr = ftrace_jmp_set(addr); 344 if (lock_direct_mutex) 345 return modify_ftrace_direct(ops, addr); 346 return modify_ftrace_direct_nolock(ops, addr); 347 } 348 #endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */ 349 #else 350 static void direct_ops_free(struct bpf_trampoline *tr) { } 351 352 static int direct_ops_alloc(struct bpf_trampoline *tr) 353 { 354 return 0; 355 } 356 357 static int direct_ops_add(struct bpf_trampoline *tr, void *addr) 358 { 359 return -ENODEV; 360 } 361 362 static int direct_ops_del(struct bpf_trampoline *tr, void *addr) 363 { 364 return -ENODEV; 365 } 366 367 static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex) 368 { 369 return -ENODEV; 370 } 371 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 372 373 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip) 374 { 375 struct bpf_trampoline *tr; 376 struct hlist_head *head; 377 int i; 378 379 mutex_lock(&trampoline_mutex); 380 head = &trampoline_key_table[hash_64(key, TRAMPOLINE_HASH_BITS)]; 381 hlist_for_each_entry(tr, head, hlist_key) { 382 if (tr->key == key) { 383 refcount_inc(&tr->refcnt); 384 goto out; 385 } 386 } 387 tr = kzalloc_obj(*tr); 388 if (!tr) 389 goto out; 390 if (direct_ops_alloc(tr)) { 391 kfree(tr); 392 tr = NULL; 393 goto out; 394 } 395 396 tr->key = key; 397 tr->ip = ftrace_location(ip); 398 INIT_HLIST_NODE(&tr->hlist_key); 399 INIT_HLIST_NODE(&tr->hlist_ip); 400 hlist_add_head(&tr->hlist_key, head); 401 head = &trampoline_ip_table[hash_64(tr->ip, TRAMPOLINE_HASH_BITS)]; 402 hlist_add_head(&tr->hlist_ip, head); 403 refcount_set(&tr->refcnt, 1); 404 INIT_LIST_HEAD(&tr->images); 405 for (i = 0; i < BPF_TRAMP_MAX; i++) 406 INIT_HLIST_HEAD(&tr->progs_hlist[i]); 407 out: 408 mutex_unlock(&trampoline_mutex); 409 return tr; 410 } 411 412 static int bpf_trampoline_update_fentry(struct bpf_trampoline *tr, u32 orig_flags, 413 void *old_addr, void *new_addr) 414 { 415 enum bpf_text_poke_type new_t = BPF_MOD_CALL, old_t = BPF_MOD_CALL; 416 void *ip = tr->func.addr; 417 418 if (!new_addr) 419 new_t = BPF_MOD_NOP; 420 else if (bpf_trampoline_use_jmp(tr->flags)) 421 new_t = BPF_MOD_JUMP; 422 423 if (!old_addr) 424 old_t = BPF_MOD_NOP; 425 else if (bpf_trampoline_use_jmp(orig_flags)) 426 old_t = BPF_MOD_JUMP; 427 428 return bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr); 429 } 430 431 static void bpf_tramp_image_put(struct bpf_tramp_image *im); 432 433 static int unregister_fentry(struct bpf_trampoline *tr, u32 orig_flags, void *data __maybe_unused) 434 { 435 void *old_addr = tr->cur_image->image; 436 int ret; 437 438 if (tr->func.ftrace_managed) 439 ret = direct_ops_del(tr, old_addr); 440 else 441 ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, NULL); 442 443 if (ret) 444 return ret; 445 446 bpf_tramp_image_put(tr->cur_image); 447 tr->cur_image = NULL; 448 return 0; 449 } 450 451 static int modify_fentry(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, 452 bool lock_direct_mutex, void *data __maybe_unused) 453 { 454 void *old_addr = tr->cur_image->image; 455 void *new_addr = im->image; 456 int ret; 457 458 if (tr->func.ftrace_managed) { 459 ret = direct_ops_mod(tr, new_addr, lock_direct_mutex); 460 } else { 461 ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, 462 new_addr); 463 } 464 465 if (ret) 466 return ret; 467 468 bpf_tramp_image_put(tr->cur_image); 469 tr->cur_image = im; 470 return 0; 471 } 472 473 /* first time registering */ 474 static int register_fentry(struct bpf_trampoline *tr, struct bpf_tramp_image *im, 475 void *data __maybe_unused) 476 { 477 void *new_addr = im->image; 478 void *ip = tr->func.addr; 479 unsigned long faddr; 480 int ret; 481 482 faddr = ftrace_location((unsigned long)ip); 483 if (faddr) { 484 if (!tr->fops) 485 return -ENOTSUPP; 486 tr->func.ftrace_managed = true; 487 } 488 489 if (tr->func.ftrace_managed) { 490 ret = direct_ops_add(tr, new_addr); 491 } else { 492 ret = bpf_trampoline_update_fentry(tr, 0, NULL, new_addr); 493 } 494 495 if (ret) 496 return ret; 497 498 tr->cur_image = im; 499 return 0; 500 } 501 502 static const struct bpf_trampoline_ops trampoline_ops = { 503 .register_fentry = register_fentry, 504 .unregister_fentry = unregister_fentry, 505 .modify_fentry = modify_fentry, 506 }; 507 508 static struct bpf_tramp_nodes * 509 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) 510 { 511 struct bpf_tramp_node *node, **nodes; 512 struct bpf_tramp_nodes *tnodes; 513 int kind; 514 515 *total = 0; 516 tnodes = kzalloc_objs(*tnodes, BPF_TRAMP_MAX); 517 if (!tnodes) 518 return ERR_PTR(-ENOMEM); 519 520 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 521 tnodes[kind].nr_nodes = tr->progs_cnt[kind]; 522 *total += tr->progs_cnt[kind]; 523 nodes = tnodes[kind].nodes; 524 525 hlist_for_each_entry(node, &tr->progs_hlist[kind], tramp_hlist) { 526 *ip_arg |= node->link->prog->call_get_func_ip; 527 *nodes++ = node; 528 } 529 } 530 return tnodes; 531 } 532 533 /* 534 * The arena base against which save_args() converts the arguments marked 535 * with BTF_FMODEL_ARENA_ARG. Only the struct_ops indirect trampoline 536 * converts: it dispatches to a single prog whose arena is known at 537 * generation time. Return 0 when there is nothing to convert. 538 */ 539 u64 bpf_tramp_arena_base(const struct btf_func_model *m, 540 struct bpf_tramp_nodes *tnodes, u32 flags) 541 { 542 const struct bpf_prog *prog; 543 int i; 544 545 if (!(flags & BPF_TRAMP_F_INDIRECT) || 546 tnodes[BPF_TRAMP_FENTRY].nr_nodes != 1) 547 return 0; 548 549 for (i = 0; i < m->nr_args; i++) 550 if (m->arg_flags[i] & BTF_FMODEL_ARENA_ARG) 551 break; 552 if (i == m->nr_args) 553 return 0; 554 555 /* Verification rejects an arena argument without an arena. */ 556 prog = tnodes[BPF_TRAMP_FENTRY].nodes[0]->link->prog; 557 if (WARN_ON_ONCE(!prog->aux->arena)) 558 return 0; 559 560 return bpf_arena_get_kern_vm_start(prog->aux->arena); 561 } 562 563 static void bpf_tramp_image_free(struct bpf_tramp_image *im) 564 { 565 bpf_image_ksym_del(&im->ksym); 566 arch_free_bpf_trampoline(im->image, im->size); 567 bpf_jit_uncharge_modmem(im->size); 568 percpu_ref_exit(&im->pcref); 569 kfree(im->skips); 570 kfree_rcu(im, rcu); 571 } 572 573 static void __bpf_tramp_image_put_deferred(struct work_struct *work) 574 { 575 struct bpf_tramp_image *im; 576 struct bpf_trampoline *tr; 577 578 im = container_of(work, struct bpf_tramp_image, work); 579 tr = im->tr; 580 trampoline_lock(tr); 581 list_del(&im->list); 582 trampoline_unlock(tr); 583 bpf_tramp_image_free(im); 584 bpf_trampoline_put(tr); 585 } 586 587 /* callback, fexit step 3 or fentry step 2 */ 588 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) 589 { 590 struct bpf_tramp_image *im; 591 592 im = container_of(rcu, struct bpf_tramp_image, rcu); 593 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred); 594 schedule_work(&im->work); 595 } 596 597 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */ 598 static void __bpf_tramp_image_release(struct percpu_ref *pcref) 599 { 600 struct bpf_tramp_image *im; 601 602 im = container_of(pcref, struct bpf_tramp_image, pcref); 603 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 604 } 605 606 /* callback, fexit or fentry step 1 */ 607 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu) 608 { 609 struct bpf_tramp_image *im; 610 611 im = container_of(rcu, struct bpf_tramp_image, rcu); 612 if (im->call_orig) 613 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */ 614 percpu_ref_kill(&im->pcref); 615 else 616 /* the case of fentry trampoline */ 617 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 618 } 619 620 static void bpf_tramp_image_put(struct bpf_tramp_image *im) 621 { 622 /* The trampoline image that calls original function is using: 623 * rcu_read_lock_trace to protect sleepable bpf progs 624 * rcu_read_lock to protect normal bpf progs 625 * percpu_ref to protect trampoline itself 626 * rcu tasks to protect trampoline asm not covered by percpu_ref 627 * (which are few asm insns before __bpf_tramp_enter and 628 * after __bpf_tramp_exit) 629 * 630 * The trampoline is unreachable before bpf_tramp_image_put(). 631 * 632 * Progs are patched out of the image when they are detached, see 633 * bpf_trampoline_skip_prog(), so they can be freed even if a task is 634 * still in the image. 635 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on 636 * first few asm instructions to execute and call into 637 * __bpf_tramp_enter->percpu_ref_get. 638 * Then use percpu_ref_kill to wait for the trampoline and the original 639 * function to finish. 640 * Then use call_rcu_tasks() to make sure few asm insns in 641 * the trampoline epilogue are done as well. 642 * 643 * In !PREEMPT case the task that got interrupted in the first asm 644 * insns won't go through an RCU quiescent state which the 645 * percpu_ref_kill will be waiting for. Hence the first 646 * call_rcu_tasks() is not necessary. 647 */ 648 if (im->call_orig) { 649 if (IS_ENABLED(CONFIG_TASKS_RCU)) 650 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 651 else 652 percpu_ref_kill(&im->pcref); 653 return; 654 } 655 656 /* The trampoline without fexit and fmod_ret progs doesn't call original 657 * function and doesn't use percpu_ref. 658 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. 659 * Then use call_rcu_tasks() to wait for the rest of trampoline asm 660 * and normal progs. 661 */ 662 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 663 } 664 665 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs) 666 { 667 struct bpf_tramp_image *im; 668 struct bpf_ksym *ksym; 669 void *image; 670 int err = -ENOMEM; 671 672 im = kzalloc_obj(*im); 673 if (!im) 674 goto out; 675 676 im->skips = kzalloc_objs(*im->skips, nr_progs); 677 if (!im->skips) 678 goto out_free_im; 679 680 err = bpf_jit_charge_modmem(size); 681 if (err) 682 goto out_free_im; 683 im->size = size; 684 685 err = -ENOMEM; 686 im->image = image = arch_alloc_bpf_trampoline(size); 687 if (!image) 688 goto out_uncharge; 689 690 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL); 691 if (err) 692 goto out_free_image; 693 694 ksym = &im->ksym; 695 INIT_LIST_HEAD_RCU(&ksym->lnode); 696 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key); 697 bpf_image_ksym_init(image, size, ksym); 698 bpf_image_ksym_add(ksym); 699 return im; 700 701 out_free_image: 702 arch_free_bpf_trampoline(im->image, im->size); 703 out_uncharge: 704 bpf_jit_uncharge_modmem(size); 705 out_free_im: 706 kfree(im->skips); 707 kfree(im); 708 out: 709 return ERR_PTR(err); 710 } 711 712 void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags) 713 { 714 trampoline_lock(tr); 715 tr->flags |= flags; 716 trampoline_unlock(tr); 717 } 718 719 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex, 720 const struct bpf_trampoline_ops *ops, void *data) 721 { 722 struct bpf_tramp_image *im; 723 struct bpf_tramp_nodes *tnodes; 724 u32 orig_flags = tr->flags; 725 bool ip_arg = false; 726 int err, total, size; 727 728 tnodes = bpf_trampoline_get_progs(tr, &total, &ip_arg); 729 if (IS_ERR(tnodes)) 730 return PTR_ERR(tnodes); 731 732 if (total == 0) { 733 err = ops->unregister_fentry(tr, orig_flags, data); 734 goto out; 735 } 736 737 /* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */ 738 tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX); 739 740 if (tnodes[BPF_TRAMP_FEXIT].nr_nodes || 741 tnodes[BPF_TRAMP_MODIFY_RETURN].nr_nodes) { 742 /* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME 743 * should not be set together. 744 */ 745 tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 746 } else { 747 tr->flags |= BPF_TRAMP_F_RESTORE_REGS; 748 } 749 750 if (ip_arg) 751 tr->flags |= BPF_TRAMP_F_IP_ARG; 752 753 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 754 again: 755 if (tr->flags & BPF_TRAMP_F_CALL_ORIG) { 756 if (tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) { 757 /* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the 758 * first try, reset it in the second try. 759 */ 760 tr->flags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME; 761 } else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) { 762 /* Use "jmp" instead of "call" for the trampoline 763 * in the origin call case, and we don't need to 764 * skip the frame. 765 */ 766 tr->flags &= ~BPF_TRAMP_F_SKIP_FRAME; 767 } 768 } 769 #endif 770 771 size = arch_bpf_trampoline_size(&tr->func.model, tr->flags, 772 tnodes, tr->func.addr); 773 if (size < 0) { 774 err = size; 775 goto out; 776 } 777 778 if (size > PAGE_SIZE) { 779 err = -E2BIG; 780 goto out; 781 } 782 783 im = bpf_tramp_image_alloc(tr->key, size, total); 784 if (IS_ERR(im)) { 785 err = PTR_ERR(im); 786 goto out; 787 } 788 im->call_orig = tr->flags & BPF_TRAMP_F_CALL_ORIG; 789 790 err = arch_prepare_bpf_trampoline(im, im->image, im->image + size, 791 &tr->func.model, tr->flags, tnodes, 792 tr->func.addr); 793 if (err < 0) 794 goto out_free; 795 796 err = arch_protect_bpf_trampoline(im->image, im->size); 797 if (err) 798 goto out_free; 799 800 if (tr->cur_image) 801 /* progs already running at this address */ 802 err = ops->modify_fentry(tr, orig_flags, im, lock_direct_mutex, data); 803 else 804 /* first time registering */ 805 err = ops->register_fentry(tr, im, data); 806 807 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 808 if (err == -EAGAIN) { 809 /* -EAGAIN from bpf_tramp_ftrace_ops_func. Now 810 * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the 811 * trampoline again, and retry register. 812 */ 813 bpf_tramp_image_free(im); 814 goto again; 815 } 816 #endif 817 818 out_free: 819 if (err) { 820 bpf_tramp_image_free(im); 821 } else { 822 /* track the image until it is freed, for bpf_trampoline_skip_prog() */ 823 refcount_inc(&tr->refcnt); 824 im->tr = tr; 825 list_add(&im->list, &tr->images); 826 } 827 out: 828 /* If any error happens, restore previous flags */ 829 if (err) 830 tr->flags = orig_flags; 831 kfree(tnodes); 832 return err; 833 } 834 835 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) 836 { 837 switch (prog->expected_attach_type) { 838 case BPF_TRACE_FENTRY: 839 case BPF_TRACE_FENTRY_MULTI: 840 return BPF_TRAMP_FENTRY; 841 case BPF_MODIFY_RETURN: 842 return BPF_TRAMP_MODIFY_RETURN; 843 case BPF_TRACE_FEXIT: 844 case BPF_TRACE_FEXIT_MULTI: 845 return BPF_TRAMP_FEXIT; 846 case BPF_TRACE_FSESSION: 847 case BPF_TRACE_FSESSION_MULTI: 848 return BPF_TRAMP_FSESSION; 849 case BPF_LSM_MAC: 850 if (!prog->aux->attach_func_proto->type) 851 /* The function returns void, we cannot modify its 852 * return value. 853 */ 854 return BPF_TRAMP_FEXIT; 855 else 856 return BPF_TRAMP_MODIFY_RETURN; 857 default: 858 return BPF_TRAMP_REPLACE; 859 } 860 } 861 862 static int bpf_freplace_link_tgt_prog(struct bpf_prog *tgt_prog) 863 { 864 struct bpf_prog_aux *aux = tgt_prog->aux; 865 866 guard(mutex)(&aux->ext_mutex); 867 if (aux->prog_array_member_cnt) 868 /* Program extensions can not extend target prog when the target 869 * prog has been updated to any prog_array map as tail callee. 870 * It's to prevent a potential infinite loop like: 871 * tgt prog entry -> tgt prog subprog -> freplace prog entry 872 * --tailcall-> tgt prog entry. 873 */ 874 return -EBUSY; 875 876 aux->freplace_link_cnt++; 877 return 0; 878 } 879 880 static struct bpf_tramp_node *fsession_exit(struct bpf_tramp_node *node) 881 { 882 if (node->link->type == BPF_LINK_TYPE_TRACING) { 883 struct bpf_tracing_link *link; 884 885 link = container_of(node->link, struct bpf_tracing_link, link.link); 886 return &link->fexit; 887 } else if (node->link->type == BPF_LINK_TYPE_TRACING_MULTI) { 888 struct bpf_tracing_multi_link *link; 889 struct bpf_tracing_multi_node *mnode; 890 891 link = container_of(node->link, struct bpf_tracing_multi_link, link); 892 mnode = container_of(node, struct bpf_tracing_multi_node, node); 893 return &link->fexits[mnode - link->nodes]; 894 } 895 return NULL; 896 } 897 898 static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, 899 struct bpf_tramp_node *node, 900 int cnt) 901 { 902 enum bpf_tramp_prog_type kind; 903 struct bpf_tramp_node *node_existing, *fexit; 904 struct hlist_head *prog_list; 905 906 kind = bpf_attach_type_to_tramp(node->link->prog); 907 if (kind == BPF_TRAMP_FSESSION) { 908 prog_list = &tr->progs_hlist[BPF_TRAMP_FENTRY]; 909 cnt++; 910 } else { 911 prog_list = &tr->progs_hlist[kind]; 912 } 913 if (cnt >= BPF_MAX_TRAMP_LINKS) 914 return -E2BIG; 915 if (!hlist_unhashed(&node->tramp_hlist)) 916 /* prog already linked */ 917 return -EBUSY; 918 hlist_for_each_entry(node_existing, prog_list, tramp_hlist) { 919 if (node_existing->link->prog != node->link->prog) 920 continue; 921 /* prog already linked */ 922 return -EBUSY; 923 } 924 925 hlist_add_head(&node->tramp_hlist, prog_list); 926 node->link->prog->aux->tramp_linked = true; 927 if (kind == BPF_TRAMP_FSESSION) { 928 tr->progs_cnt[BPF_TRAMP_FENTRY]++; 929 fexit = fsession_exit(node); 930 if (WARN_ON_ONCE(!fexit)) 931 return -EINVAL; 932 hlist_add_head(&fexit->tramp_hlist, &tr->progs_hlist[BPF_TRAMP_FEXIT]); 933 tr->progs_cnt[BPF_TRAMP_FEXIT]++; 934 } else { 935 tr->progs_cnt[kind]++; 936 } 937 return 0; 938 } 939 940 /* 941 * Patch the nop in front of a prog call to a jump over it. A task can be 942 * preempted anywhere in the image, so archs that need several instructions for 943 * a jump of any range patch a single near branch here instead. 944 */ 945 int __weak arch_bpf_trampoline_skip(void *nop, void *target) 946 { 947 return bpf_arch_text_poke(nop, BPF_MOD_NOP, BPF_MOD_JUMP, NULL, target); 948 } 949 950 /* 951 * prog was detached and can be freed, but tasks may still be running in images 952 * that call it, sleeping in an earlier prog for example. They can be in any 953 * image that is not freed yet, not only in cur_image, so patch all of them to 954 * jump over prog. 955 */ 956 static void bpf_trampoline_skip_prog(struct bpf_trampoline *tr, struct bpf_prog *prog) 957 { 958 struct bpf_tramp_image *im; 959 int i, err; 960 961 list_for_each_entry(im, &tr->images, list) { 962 for (i = 0; i < im->nr_skips; i++) { 963 struct bpf_tramp_skip *skip = &im->skips[i]; 964 965 if (skip->prog != prog) 966 continue; 967 err = arch_bpf_trampoline_skip(skip->nop, skip->target); 968 WARN_ON_ONCE(err); 969 /* not a nop anymore, and prog's address can be reused */ 970 skip->prog = NULL; 971 } 972 } 973 } 974 975 static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr, 976 struct bpf_tramp_node *node) 977 { 978 enum bpf_tramp_prog_type kind; 979 struct bpf_tramp_node *fexit; 980 981 kind = bpf_attach_type_to_tramp(node->link->prog); 982 if (kind == BPF_TRAMP_FSESSION) { 983 fexit = fsession_exit(node); 984 if (WARN_ON_ONCE(!fexit)) 985 return; 986 hlist_del_init(&fexit->tramp_hlist); 987 tr->progs_cnt[BPF_TRAMP_FEXIT]--; 988 kind = BPF_TRAMP_FENTRY; 989 } 990 hlist_del_init(&node->tramp_hlist); 991 tr->progs_cnt[kind]--; 992 bpf_trampoline_skip_prog(tr, node->link->prog); 993 } 994 995 static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node, 996 struct bpf_trampoline *tr, 997 struct bpf_prog *tgt_prog, 998 const struct bpf_trampoline_ops *ops, 999 void *data) 1000 { 1001 enum bpf_tramp_prog_type kind; 1002 int err = 0; 1003 int cnt = 0, i; 1004 1005 kind = bpf_attach_type_to_tramp(node->link->prog); 1006 /* 1007 * Arena ctx args are converted only by struct_ops indirect 1008 * trampolines. They must never be attached to a generic trampoline. 1009 */ 1010 if (WARN_ON_ONCE(bpf_prog_has_arena_ctx_arg(node->link->prog))) 1011 return -ENOTSUPP; 1012 1013 if (tr->extension_prog) 1014 /* cannot attach fentry/fexit if extension prog is attached. 1015 * cannot overwrite extension prog either. 1016 */ 1017 return -EBUSY; 1018 1019 for (i = 0; i < BPF_TRAMP_MAX; i++) 1020 cnt += tr->progs_cnt[i]; 1021 1022 if (kind == BPF_TRAMP_REPLACE) { 1023 /* Cannot attach extension if fentry/fexit are in use. */ 1024 if (cnt) 1025 return -EBUSY; 1026 err = bpf_freplace_link_tgt_prog(tgt_prog); 1027 if (err) 1028 return err; 1029 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_NOP, 1030 BPF_MOD_JUMP, NULL, 1031 node->link->prog->bpf_func); 1032 if (err) { 1033 /* Undo the claim from bpf_freplace_link_tgt_prog(). */ 1034 guard(mutex)(&tgt_prog->aux->ext_mutex); 1035 tgt_prog->aux->freplace_link_cnt--; 1036 return err; 1037 } 1038 tr->extension_prog = node->link->prog; 1039 return 0; 1040 } 1041 err = bpf_trampoline_add_prog(tr, node, cnt); 1042 if (err) 1043 return err; 1044 err = bpf_trampoline_update(tr, true /* lock_direct_mutex */, ops, data); 1045 if (err) 1046 bpf_trampoline_remove_prog(tr, node); 1047 return err; 1048 } 1049 1050 int bpf_trampoline_link_prog(struct bpf_tramp_node *node, 1051 struct bpf_trampoline *tr, 1052 struct bpf_prog *tgt_prog) 1053 { 1054 int err; 1055 1056 trampoline_lock(tr); 1057 err = __bpf_trampoline_link_prog(node, tr, tgt_prog, &trampoline_ops, NULL); 1058 trampoline_unlock(tr); 1059 return err; 1060 } 1061 1062 static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, 1063 struct bpf_trampoline *tr, 1064 struct bpf_prog *tgt_prog, 1065 const struct bpf_trampoline_ops *ops, 1066 void *data) 1067 { 1068 enum bpf_tramp_prog_type kind; 1069 int err; 1070 1071 kind = bpf_attach_type_to_tramp(node->link->prog); 1072 if (kind == BPF_TRAMP_REPLACE) { 1073 WARN_ON_ONCE(!tr->extension_prog); 1074 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, 1075 BPF_MOD_NOP, 1076 tr->extension_prog->bpf_func, NULL); 1077 tr->extension_prog = NULL; 1078 guard(mutex)(&tgt_prog->aux->ext_mutex); 1079 tgt_prog->aux->freplace_link_cnt--; 1080 return err; 1081 } 1082 bpf_trampoline_remove_prog(tr, node); 1083 return bpf_trampoline_update(tr, true /* lock_direct_mutex */, ops, data); 1084 } 1085 1086 /* bpf_trampoline_unlink_prog() should never fail. */ 1087 int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, 1088 struct bpf_trampoline *tr, 1089 struct bpf_prog *tgt_prog) 1090 { 1091 int err; 1092 1093 trampoline_lock(tr); 1094 err = __bpf_trampoline_unlink_prog(node, tr, tgt_prog, &trampoline_ops, NULL); 1095 trampoline_unlock(tr); 1096 return err; 1097 } 1098 1099 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM) 1100 static void bpf_shim_tramp_link_release(struct bpf_link *link) 1101 { 1102 struct bpf_shim_tramp_link *shim_link = 1103 container_of(link, struct bpf_shim_tramp_link, link.link); 1104 int err; 1105 1106 /* paired with 'shim_link->trampoline = tr' in bpf_trampoline_link_cgroup_shim */ 1107 if (!shim_link->trampoline) 1108 return; 1109 1110 err = bpf_trampoline_unlink_prog(&shim_link->link.node, shim_link->trampoline, NULL); 1111 WARN_ONCE(err, "bpf_trampoline_unlink_prog failed: %d\n", err); 1112 1113 bpf_trampoline_put(shim_link->trampoline); 1114 } 1115 1116 static void bpf_shim_tramp_link_dealloc(struct bpf_link *link) 1117 { 1118 struct bpf_shim_tramp_link *shim_link = 1119 container_of(link, struct bpf_shim_tramp_link, link.link); 1120 1121 kfree(shim_link); 1122 } 1123 1124 static const struct bpf_link_ops bpf_shim_tramp_link_lops = { 1125 .release = bpf_shim_tramp_link_release, 1126 .dealloc = bpf_shim_tramp_link_dealloc, 1127 }; 1128 1129 static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog, 1130 bpf_func_t bpf_func, 1131 int cgroup_atype, 1132 enum bpf_attach_type attach_type) 1133 { 1134 struct bpf_shim_tramp_link *shim_link = NULL; 1135 struct bpf_prog *p; 1136 1137 shim_link = kzalloc_obj(*shim_link, GFP_USER); 1138 if (!shim_link) 1139 return NULL; 1140 1141 p = bpf_prog_alloc(1, 0); 1142 if (!p) { 1143 kfree(shim_link); 1144 return NULL; 1145 } 1146 1147 p->jited = false; 1148 p->bpf_func = bpf_func; 1149 1150 p->aux->cgroup_atype = cgroup_atype; 1151 p->aux->attach_func_proto = prog->aux->attach_func_proto; 1152 p->aux->attach_btf_id = prog->aux->attach_btf_id; 1153 p->aux->attach_btf = prog->aux->attach_btf; 1154 btf_get(p->aux->attach_btf); 1155 p->type = BPF_PROG_TYPE_LSM; 1156 p->expected_attach_type = BPF_LSM_MAC; 1157 bpf_prog_inc(p); 1158 bpf_tramp_link_init(&shim_link->link, BPF_LINK_TYPE_UNSPEC, 1159 &bpf_shim_tramp_link_lops, p, attach_type, 0); 1160 bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype); 1161 1162 return shim_link; 1163 } 1164 1165 static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr, 1166 bpf_func_t bpf_func) 1167 { 1168 struct bpf_tramp_node *node; 1169 int kind; 1170 1171 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 1172 hlist_for_each_entry(node, &tr->progs_hlist[kind], tramp_hlist) { 1173 struct bpf_prog *p = node->link->prog; 1174 1175 if (p->bpf_func == bpf_func) 1176 return container_of(node, struct bpf_shim_tramp_link, link.node); 1177 } 1178 } 1179 1180 return NULL; 1181 } 1182 1183 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 1184 int cgroup_atype, 1185 enum bpf_attach_type attach_type) 1186 { 1187 struct bpf_shim_tramp_link *shim_link = NULL; 1188 struct bpf_attach_target_info tgt_info = {}; 1189 struct bpf_trampoline *tr; 1190 bpf_func_t bpf_func; 1191 u64 key; 1192 int err; 1193 1194 err = bpf_check_attach_target(NULL, prog, NULL, 1195 prog->aux->attach_btf_id, 1196 &tgt_info); 1197 if (err) 1198 return err; 1199 1200 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 1201 prog->aux->attach_btf_id); 1202 1203 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 1204 tr = bpf_trampoline_get(key, &tgt_info); 1205 if (!tr) 1206 return -ENOMEM; 1207 1208 trampoline_lock(tr); 1209 1210 shim_link = cgroup_shim_find(tr, bpf_func); 1211 if (shim_link && !IS_ERR(bpf_link_inc_not_zero(&shim_link->link.link))) { 1212 /* Reusing existing shim attached by the other program. */ 1213 trampoline_unlock(tr); 1214 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 1215 return 0; 1216 } 1217 1218 /* Allocate and install new shim. */ 1219 1220 shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype, attach_type); 1221 if (!shim_link) { 1222 err = -ENOMEM; 1223 goto err; 1224 } 1225 1226 err = __bpf_trampoline_link_prog(&shim_link->link.node, tr, NULL, &trampoline_ops, NULL); 1227 if (err) 1228 goto err; 1229 1230 shim_link->trampoline = tr; 1231 /* note, we're still holding tr refcnt from above */ 1232 1233 trampoline_unlock(tr); 1234 1235 return 0; 1236 err: 1237 trampoline_unlock(tr); 1238 1239 if (shim_link) 1240 bpf_link_put(&shim_link->link.link); 1241 1242 /* have to release tr while _not_ holding pool mutex for trampoline */ 1243 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 1244 1245 return err; 1246 } 1247 1248 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog) 1249 { 1250 struct bpf_shim_tramp_link *shim_link = NULL; 1251 struct bpf_trampoline *tr; 1252 bpf_func_t bpf_func; 1253 u64 key; 1254 1255 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 1256 prog->aux->attach_btf_id); 1257 1258 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 1259 tr = bpf_trampoline_lookup(key, 0); 1260 if (WARN_ON_ONCE(!tr)) 1261 return; 1262 1263 trampoline_lock(tr); 1264 shim_link = cgroup_shim_find(tr, bpf_func); 1265 trampoline_unlock(tr); 1266 1267 if (shim_link) 1268 bpf_link_put(&shim_link->link.link); 1269 1270 bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */ 1271 } 1272 #endif 1273 1274 struct bpf_trampoline *bpf_trampoline_get(u64 key, 1275 struct bpf_attach_target_info *tgt_info) 1276 { 1277 struct bpf_trampoline *tr; 1278 1279 tr = bpf_trampoline_lookup(key, tgt_info->tgt_addr); 1280 if (!tr) 1281 return NULL; 1282 1283 trampoline_lock(tr); 1284 if (tr->func.addr) 1285 goto out; 1286 1287 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); 1288 tr->func.addr = (void *)tgt_info->tgt_addr; 1289 out: 1290 trampoline_unlock(tr); 1291 return tr; 1292 } 1293 1294 void bpf_trampoline_put(struct bpf_trampoline *tr) 1295 { 1296 int i; 1297 1298 if (!tr) 1299 return; 1300 mutex_lock(&trampoline_mutex); 1301 if (!refcount_dec_and_test(&tr->refcnt)) 1302 goto out; 1303 1304 for (i = 0; i < BPF_TRAMP_MAX; i++) 1305 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i]))) 1306 goto out; 1307 1308 /* 1309 * All progs are detached and the last image has been freed, images 1310 * hold a reference on the trampoline until then. 1311 */ 1312 hlist_del(&tr->hlist_key); 1313 hlist_del(&tr->hlist_ip); 1314 direct_ops_free(tr); 1315 kfree(tr); 1316 out: 1317 mutex_unlock(&trampoline_mutex); 1318 } 1319 1320 #define NO_START_TIME 1 1321 static __always_inline u64 notrace bpf_prog_start_time(void) 1322 { 1323 u64 start = NO_START_TIME; 1324 1325 if (static_branch_unlikely(&bpf_stats_enabled_key)) { 1326 start = sched_clock(); 1327 if (unlikely(!start)) 1328 start = NO_START_TIME; 1329 } 1330 return start; 1331 } 1332 1333 /* The logic is similar to bpf_prog_run(), but with an explicit 1334 * rcu_read_lock() and migrate_disable() which are required 1335 * for the trampoline. The macro is split into 1336 * call __bpf_prog_enter 1337 * call prog->bpf_func 1338 * call __bpf_prog_exit 1339 * 1340 * __bpf_prog_enter returns: 1341 * 0 - skip execution of the bpf prog 1342 * 1 - execute bpf prog 1343 * [2..MAX_U64] - execute bpf prog and record execution time. 1344 * This is start time. 1345 */ 1346 static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) 1347 __acquires(RCU) 1348 { 1349 rcu_read_lock_dont_migrate(); 1350 1351 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1352 1353 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 1354 bpf_prog_inc_misses_counter(prog); 1355 if (prog->aux->recursion_detected) 1356 prog->aux->recursion_detected(prog); 1357 return 0; 1358 } 1359 return bpf_prog_start_time(); 1360 } 1361 1362 static void notrace __update_prog_stats(struct bpf_prog *prog, u64 start) 1363 { 1364 struct bpf_prog_stats *stats; 1365 unsigned long flags; 1366 u64 duration; 1367 1368 /* 1369 * static_key could be enabled in __bpf_prog_enter* and disabled in 1370 * __bpf_prog_exit*. And vice versa. Check that 'start' is valid. 1371 */ 1372 if (start <= NO_START_TIME) 1373 return; 1374 1375 duration = sched_clock() - start; 1376 stats = this_cpu_ptr(prog->stats); 1377 flags = u64_stats_update_begin_irqsave(&stats->syncp); 1378 u64_stats_inc(&stats->cnt); 1379 u64_stats_add(&stats->nsecs, duration); 1380 u64_stats_update_end_irqrestore(&stats->syncp, flags); 1381 } 1382 1383 static __always_inline void notrace update_prog_stats(struct bpf_prog *prog, 1384 u64 start) 1385 { 1386 if (static_branch_unlikely(&bpf_stats_enabled_key)) 1387 __update_prog_stats(prog, start); 1388 } 1389 1390 static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start, 1391 struct bpf_tramp_run_ctx *run_ctx) 1392 __releases(RCU) 1393 { 1394 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1395 1396 update_prog_stats(prog, start); 1397 bpf_prog_put_recursion_context(prog); 1398 rcu_read_unlock_migrate(); 1399 } 1400 1401 static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog, 1402 struct bpf_tramp_run_ctx *run_ctx) 1403 __acquires(RCU) 1404 { 1405 /* Runtime stats are exported via actual BPF_LSM_CGROUP 1406 * programs, not the shims. 1407 */ 1408 rcu_read_lock_dont_migrate(); 1409 1410 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1411 1412 return NO_START_TIME; 1413 } 1414 1415 static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start, 1416 struct bpf_tramp_run_ctx *run_ctx) 1417 __releases(RCU) 1418 { 1419 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1420 1421 rcu_read_unlock_migrate(); 1422 } 1423 1424 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, 1425 struct bpf_tramp_run_ctx *run_ctx) 1426 { 1427 rcu_read_lock_trace(); 1428 migrate_disable(); 1429 might_fault(); 1430 1431 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1432 1433 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 1434 bpf_prog_inc_misses_counter(prog); 1435 if (prog->aux->recursion_detected) 1436 prog->aux->recursion_detected(prog); 1437 return 0; 1438 } 1439 return bpf_prog_start_time(); 1440 } 1441 1442 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start, 1443 struct bpf_tramp_run_ctx *run_ctx) 1444 { 1445 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1446 1447 update_prog_stats(prog, start); 1448 bpf_prog_put_recursion_context(prog); 1449 migrate_enable(); 1450 rcu_read_unlock_trace(); 1451 } 1452 1453 static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, 1454 struct bpf_tramp_run_ctx *run_ctx) 1455 { 1456 rcu_read_lock_trace(); 1457 migrate_disable(); 1458 might_fault(); 1459 1460 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1461 1462 return bpf_prog_start_time(); 1463 } 1464 1465 static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start, 1466 struct bpf_tramp_run_ctx *run_ctx) 1467 { 1468 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1469 1470 update_prog_stats(prog, start); 1471 migrate_enable(); 1472 rcu_read_unlock_trace(); 1473 } 1474 1475 static u64 notrace __bpf_prog_enter(struct bpf_prog *prog, 1476 struct bpf_tramp_run_ctx *run_ctx) 1477 __acquires(RCU) 1478 { 1479 rcu_read_lock_dont_migrate(); 1480 1481 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1482 1483 return bpf_prog_start_time(); 1484 } 1485 1486 static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, 1487 struct bpf_tramp_run_ctx *run_ctx) 1488 __releases(RCU) 1489 { 1490 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1491 1492 update_prog_stats(prog, start); 1493 rcu_read_unlock_migrate(); 1494 } 1495 1496 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr) 1497 { 1498 percpu_ref_get(&tr->pcref); 1499 } 1500 1501 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr) 1502 { 1503 percpu_ref_put(&tr->pcref); 1504 } 1505 1506 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog) 1507 { 1508 bool sleepable = prog->sleepable; 1509 1510 if (bpf_prog_check_recur(prog)) 1511 return sleepable ? __bpf_prog_enter_sleepable_recur : 1512 __bpf_prog_enter_recur; 1513 1514 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1515 prog->expected_attach_type == BPF_LSM_CGROUP) 1516 return __bpf_prog_enter_lsm_cgroup; 1517 1518 return sleepable ? __bpf_prog_enter_sleepable : __bpf_prog_enter; 1519 } 1520 1521 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog) 1522 { 1523 bool sleepable = prog->sleepable; 1524 1525 if (bpf_prog_check_recur(prog)) 1526 return sleepable ? __bpf_prog_exit_sleepable_recur : 1527 __bpf_prog_exit_recur; 1528 1529 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1530 prog->expected_attach_type == BPF_LSM_CGROUP) 1531 return __bpf_prog_exit_lsm_cgroup; 1532 1533 return sleepable ? __bpf_prog_exit_sleepable : __bpf_prog_exit; 1534 } 1535 1536 int __weak 1537 arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end, 1538 const struct btf_func_model *m, u32 flags, 1539 struct bpf_tramp_nodes *tnodes, 1540 void *func_addr) 1541 { 1542 return -ENOTSUPP; 1543 } 1544 1545 void * __weak arch_alloc_bpf_trampoline(unsigned int size) 1546 { 1547 void *image; 1548 1549 if (WARN_ON_ONCE(size > PAGE_SIZE)) 1550 return NULL; 1551 image = bpf_jit_alloc_exec(PAGE_SIZE); 1552 if (image) 1553 set_vm_flush_reset_perms(image); 1554 return image; 1555 } 1556 1557 void __weak arch_free_bpf_trampoline(void *image, unsigned int size) 1558 { 1559 WARN_ON_ONCE(size > PAGE_SIZE); 1560 /* bpf_jit_free_exec doesn't need "size", but 1561 * bpf_prog_pack_free() needs it. 1562 */ 1563 bpf_jit_free_exec(image); 1564 } 1565 1566 int __weak arch_protect_bpf_trampoline(void *image, unsigned int size) 1567 { 1568 WARN_ON_ONCE(size > PAGE_SIZE); 1569 return set_memory_rox((long)image, 1); 1570 } 1571 1572 int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, 1573 struct bpf_tramp_nodes *tnodes, void *func_addr) 1574 { 1575 return -ENOTSUPP; 1576 } 1577 1578 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) && \ 1579 defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) && \ 1580 defined(CONFIG_BPF_SYSCALL) 1581 1582 static void trampoline_lock_all(void) 1583 { 1584 int i; 1585 1586 for (i = 0; i < TRAMPOLINE_LOCKS_TABLE_SIZE; i++) 1587 mutex_lock(&trampoline_locks[i].mutex); 1588 } 1589 1590 static void trampoline_unlock_all(void) 1591 { 1592 int i; 1593 1594 for (i = 0; i < TRAMPOLINE_LOCKS_TABLE_SIZE; i++) 1595 mutex_unlock(&trampoline_locks[i].mutex); 1596 } 1597 1598 static void remove_tracing_multi_data(struct bpf_tracing_multi_data *data) 1599 { 1600 ftrace_hash_remove(data->reg); 1601 ftrace_hash_remove(data->unreg); 1602 ftrace_hash_remove(data->modify); 1603 } 1604 1605 static void clear_tracing_multi_data(struct bpf_tracing_multi_data *data) 1606 { 1607 remove_tracing_multi_data(data); 1608 1609 free_ftrace_hash(data->reg); 1610 free_ftrace_hash(data->unreg); 1611 free_ftrace_hash(data->modify); 1612 } 1613 1614 static int init_tracing_multi_data(struct bpf_tracing_multi_data *data) 1615 { 1616 data->reg = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 1617 data->unreg = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 1618 data->modify = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 1619 1620 if (!data->reg || !data->unreg || !data->modify) { 1621 clear_tracing_multi_data(data); 1622 return -ENOMEM; 1623 } 1624 return 0; 1625 } 1626 1627 static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry *entry, 1628 unsigned long ip, unsigned long direct) 1629 { 1630 entry->ip = ip; 1631 entry->direct = direct; 1632 add_ftrace_hash_entry(hash, entry); 1633 } 1634 1635 static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_image *im, void *ptr) 1636 { 1637 unsigned long addr = (unsigned long) im->image; 1638 unsigned long ip = ftrace_location(tr->ip); 1639 struct bpf_tracing_multi_data *data = ptr; 1640 1641 if (bpf_trampoline_use_jmp(tr->flags)) 1642 addr = ftrace_jmp_set(addr); 1643 1644 tr->func.ftrace_managed = true; 1645 ftrace_hash_add(data->reg, data->entry, ip, addr); 1646 tr->cur_image = im; 1647 return 0; 1648 } 1649 1650 static int unregister_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, void *ptr) 1651 { 1652 unsigned long addr = (unsigned long) tr->cur_image->image; 1653 unsigned long ip = ftrace_location(tr->ip); 1654 struct bpf_tracing_multi_data *data = ptr; 1655 1656 if (bpf_trampoline_use_jmp(tr->flags)) 1657 addr = ftrace_jmp_set(addr); 1658 1659 ftrace_hash_add(data->unreg, data->entry, ip, addr); 1660 tr->cur_image = NULL; 1661 return 0; 1662 } 1663 1664 static int modify_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, 1665 bool lock_direct_mutex, void *ptr) 1666 { 1667 unsigned long addr = (unsigned long) im->image; 1668 unsigned long ip = ftrace_location(tr->ip); 1669 struct bpf_tracing_multi_data *data = ptr; 1670 1671 if (bpf_trampoline_use_jmp(tr->flags)) 1672 addr = ftrace_jmp_set(addr); 1673 1674 ftrace_hash_add(data->modify, data->entry, ip, addr); 1675 tr->cur_image = im; 1676 return 0; 1677 } 1678 1679 static const struct bpf_trampoline_ops trampoline_multi_ops = { 1680 .register_fentry = register_fentry_multi, 1681 .unregister_fentry = unregister_fentry_multi, 1682 .modify_fentry = modify_fentry_multi, 1683 }; 1684 1685 static void bpf_trampoline_multi_attach_init(struct bpf_trampoline *tr) 1686 { 1687 tr->multi_attach.old_image = tr->cur_image; 1688 tr->multi_attach.old_flags = tr->flags; 1689 } 1690 1691 static void bpf_trampoline_multi_attach_free(struct bpf_trampoline *tr) 1692 { 1693 /* 1694 * Only free old_image if it is no longer the active image. 1695 * When bpf_trampoline_update() fails before modify_fentry_multi()/ 1696 * unregister_fentry_multi() is called, cur_image is unchanged 1697 * (cur_image == old_image) and ftrace still points to it. Freeing 1698 * it would cause a UAF when ftrace calls into the freed memory. 1699 * On success, cur_image is either a new image or NULL, so 1700 * old_image != cur_image means the image is stale. 1701 */ 1702 if (tr->multi_attach.old_image && 1703 tr->multi_attach.old_image != tr->cur_image) 1704 bpf_tramp_image_put(tr->multi_attach.old_image); 1705 1706 tr->multi_attach.old_image = NULL; 1707 tr->multi_attach.old_flags = 0; 1708 } 1709 1710 static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr) 1711 { 1712 if (tr->cur_image) 1713 bpf_tramp_image_put(tr->cur_image); 1714 tr->cur_image = tr->multi_attach.old_image; 1715 tr->flags = tr->multi_attach.old_flags; 1716 1717 tr->multi_attach.old_image = NULL; 1718 tr->multi_attach.old_flags = 0; 1719 } 1720 1721 #define for_each_mnode_cnt(mnode, link, cnt) \ 1722 for (i = 0, mnode = &link->nodes[i]; i < cnt; i++, mnode = &link->nodes[i]) 1723 1724 #define for_each_mnode(mnode, link) \ 1725 for_each_mnode_cnt(mnode, link, link->nodes_cnt) 1726 1727 int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, 1728 struct bpf_tracing_multi_link *link) 1729 { 1730 struct bpf_tracing_multi_data *data = &link->data; 1731 struct bpf_attach_target_info tgt_info = {}; 1732 struct btf *btf = prog->aux->attach_btf; 1733 struct bpf_tracing_multi_node *mnode; 1734 struct bpf_trampoline *tr; 1735 int i, err, rollback_cnt; 1736 u64 key; 1737 1738 for_each_mnode(mnode, link) { 1739 rollback_cnt = i; 1740 1741 err = bpf_check_attach_btf_id_multi(btf, prog, ids[i], &tgt_info); 1742 if (err) 1743 goto rollback_put; 1744 1745 key = bpf_trampoline_compute_key(NULL, btf, ids[i]); 1746 1747 tr = bpf_trampoline_get(key, &tgt_info); 1748 if (!tr) { 1749 err = -ENOMEM; 1750 goto rollback_put; 1751 } 1752 1753 mnode->trampoline = tr; 1754 mnode->node.link = &link->link; 1755 mnode->node.cookie = link->cookies ? link->cookies[i] : 0; 1756 1757 if (prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI) { 1758 link->fexits[i].link = &link->link; 1759 link->fexits[i].cookie = link->cookies ? link->cookies[i] : 0; 1760 } 1761 1762 cond_resched(); 1763 } 1764 1765 err = init_tracing_multi_data(data); 1766 if (err) { 1767 rollback_cnt = link->nodes_cnt; 1768 goto rollback_put; 1769 } 1770 1771 trampoline_lock_all(); 1772 1773 for_each_mnode(mnode, link) { 1774 bpf_trampoline_multi_attach_init(mnode->trampoline); 1775 1776 data->entry = &mnode->entry; 1777 err = __bpf_trampoline_link_prog(&mnode->node, mnode->trampoline, NULL, 1778 &trampoline_multi_ops, data); 1779 if (err) { 1780 rollback_cnt = i; 1781 goto rollback_unlink; 1782 } 1783 } 1784 1785 rollback_cnt = link->nodes_cnt; 1786 if (ftrace_hash_count(data->reg)) { 1787 err = update_ftrace_direct_add(&direct_ops, data->reg); 1788 if (err) 1789 goto rollback_unlink; 1790 } 1791 1792 if (ftrace_hash_count(data->modify)) { 1793 err = update_ftrace_direct_mod(&direct_ops, data->modify, true); 1794 if (err) { 1795 if (ftrace_hash_count(data->reg)) 1796 WARN_ON_ONCE(update_ftrace_direct_del(&direct_ops, data->reg)); 1797 goto rollback_unlink; 1798 } 1799 } 1800 1801 for_each_mnode(mnode, link) 1802 bpf_trampoline_multi_attach_free(mnode->trampoline); 1803 1804 trampoline_unlock_all(); 1805 1806 remove_tracing_multi_data(data); 1807 return 0; 1808 1809 rollback_unlink: 1810 for_each_mnode_cnt(mnode, link, rollback_cnt) { 1811 bpf_trampoline_remove_prog(mnode->trampoline, &mnode->node); 1812 bpf_trampoline_multi_attach_rollback(mnode->trampoline); 1813 } 1814 1815 trampoline_unlock_all(); 1816 1817 clear_tracing_multi_data(data); 1818 rollback_cnt = link->nodes_cnt; 1819 1820 rollback_put: 1821 for_each_mnode_cnt(mnode, link, rollback_cnt) 1822 bpf_trampoline_put(mnode->trampoline); 1823 1824 return err; 1825 } 1826 1827 void bpf_trampoline_multi_detach(struct bpf_prog *prog, 1828 struct bpf_tracing_multi_link *link) 1829 { 1830 struct bpf_tracing_multi_data *data = &link->data; 1831 struct bpf_tracing_multi_node *mnode; 1832 int i, err; 1833 1834 trampoline_lock_all(); 1835 1836 for_each_mnode(mnode, link) { 1837 data->entry = &mnode->entry; 1838 bpf_trampoline_multi_attach_init(mnode->trampoline); 1839 err = __bpf_trampoline_unlink_prog(&mnode->node, mnode->trampoline, NULL, 1840 &trampoline_multi_ops, data); 1841 WARN_ONCE(err, "__bpf_trampoline_unlink_prog failed: %d\n", err); 1842 } 1843 1844 if (ftrace_hash_count(data->unreg)) 1845 WARN_ON_ONCE(update_ftrace_direct_del(&direct_ops, data->unreg)); 1846 if (ftrace_hash_count(data->modify)) 1847 WARN_ON_ONCE(update_ftrace_direct_mod(&direct_ops, data->modify, true)); 1848 1849 for_each_mnode(mnode, link) 1850 bpf_trampoline_multi_attach_free(mnode->trampoline); 1851 1852 trampoline_unlock_all(); 1853 1854 for_each_mnode(mnode, link) 1855 bpf_trampoline_put(mnode->trampoline); 1856 1857 clear_tracing_multi_data(data); 1858 } 1859 1860 #undef for_each_mnode_cnt 1861 #undef for_each_mnode 1862 1863 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS && 1864 CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS && 1865 CONFIG_BPF_SYSCALL */ 1866 1867 static int __init init_trampolines(void) 1868 { 1869 int i; 1870 1871 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1872 INIT_HLIST_HEAD(&trampoline_key_table[i]); 1873 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1874 INIT_HLIST_HEAD(&trampoline_ip_table[i]); 1875 for (i = 0; i < TRAMPOLINE_LOCKS_TABLE_SIZE; i++) 1876 __mutex_init(&trampoline_locks[i].mutex, "trampoline_lock", &trampoline_locks[i].key); 1877 return 0; 1878 } 1879 late_initcall(init_trampolines); 1880