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 int ret; 479 480 if (tr->func.ftrace_managed) { 481 if (!tr->fops) 482 return -ENOTSUPP; 483 ret = direct_ops_add(tr, new_addr); 484 } else { 485 ret = bpf_trampoline_update_fentry(tr, 0, NULL, new_addr); 486 } 487 488 if (ret) 489 return ret; 490 491 tr->cur_image = im; 492 return 0; 493 } 494 495 static const struct bpf_trampoline_ops trampoline_ops = { 496 .register_fentry = register_fentry, 497 .unregister_fentry = unregister_fentry, 498 .modify_fentry = modify_fentry, 499 }; 500 501 static struct bpf_tramp_nodes * 502 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) 503 { 504 struct bpf_tramp_node *node, **nodes; 505 struct bpf_tramp_nodes *tnodes; 506 int kind; 507 508 *total = 0; 509 tnodes = kzalloc_objs(*tnodes, BPF_TRAMP_MAX); 510 if (!tnodes) 511 return ERR_PTR(-ENOMEM); 512 513 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 514 tnodes[kind].nr_nodes = tr->progs_cnt[kind]; 515 *total += tr->progs_cnt[kind]; 516 nodes = tnodes[kind].nodes; 517 518 hlist_for_each_entry(node, &tr->progs_hlist[kind], tramp_hlist) { 519 *ip_arg |= node->link->prog->call_get_func_ip; 520 *nodes++ = node; 521 } 522 } 523 return tnodes; 524 } 525 526 /* 527 * The arena base against which save_args() converts the arguments marked 528 * with BTF_FMODEL_ARENA_ARG. Only the struct_ops indirect trampoline 529 * converts: it dispatches to a single prog whose arena is known at 530 * generation time. Return 0 when there is nothing to convert. 531 */ 532 u64 bpf_tramp_arena_base(const struct btf_func_model *m, 533 struct bpf_tramp_nodes *tnodes, u32 flags) 534 { 535 const struct bpf_prog *prog; 536 int i; 537 538 if (!(flags & BPF_TRAMP_F_INDIRECT) || 539 tnodes[BPF_TRAMP_FENTRY].nr_nodes != 1) 540 return 0; 541 542 for (i = 0; i < m->nr_args; i++) 543 if (m->arg_flags[i] & BTF_FMODEL_ARENA_ARG) 544 break; 545 if (i == m->nr_args) 546 return 0; 547 548 /* Verification rejects an arena argument without an arena. */ 549 prog = tnodes[BPF_TRAMP_FENTRY].nodes[0]->link->prog; 550 if (WARN_ON_ONCE(!prog->aux->arena)) 551 return 0; 552 553 return bpf_arena_get_kern_vm_start(prog->aux->arena); 554 } 555 556 static void bpf_tramp_image_free(struct bpf_tramp_image *im) 557 { 558 bpf_image_ksym_del(&im->ksym); 559 arch_free_bpf_trampoline(im->image, im->size); 560 bpf_jit_uncharge_modmem(im->size); 561 percpu_ref_exit(&im->pcref); 562 kfree(im->skips); 563 kfree_rcu(im, rcu); 564 } 565 566 static void __bpf_tramp_image_put_deferred(struct work_struct *work) 567 { 568 struct bpf_tramp_image *im; 569 struct bpf_trampoline *tr; 570 571 im = container_of(work, struct bpf_tramp_image, work); 572 tr = im->tr; 573 trampoline_lock(tr); 574 list_del(&im->list); 575 trampoline_unlock(tr); 576 bpf_tramp_image_free(im); 577 bpf_trampoline_put(tr); 578 } 579 580 /* callback, fexit step 3 or fentry step 2 */ 581 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) 582 { 583 struct bpf_tramp_image *im; 584 585 im = container_of(rcu, struct bpf_tramp_image, rcu); 586 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred); 587 schedule_work(&im->work); 588 } 589 590 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */ 591 static void __bpf_tramp_image_release(struct percpu_ref *pcref) 592 { 593 struct bpf_tramp_image *im; 594 595 im = container_of(pcref, struct bpf_tramp_image, pcref); 596 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 597 } 598 599 /* callback, fexit or fentry step 1 */ 600 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu) 601 { 602 struct bpf_tramp_image *im; 603 604 im = container_of(rcu, struct bpf_tramp_image, rcu); 605 if (im->call_orig) 606 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */ 607 percpu_ref_kill(&im->pcref); 608 else 609 /* the case of fentry trampoline */ 610 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 611 } 612 613 static void bpf_tramp_image_put(struct bpf_tramp_image *im) 614 { 615 /* The trampoline image that calls original function is using: 616 * rcu_read_lock_trace to protect sleepable bpf progs 617 * rcu_read_lock to protect normal bpf progs 618 * percpu_ref to protect trampoline itself 619 * rcu tasks to protect trampoline asm not covered by percpu_ref 620 * (which are few asm insns before __bpf_tramp_enter and 621 * after __bpf_tramp_exit) 622 * 623 * The trampoline is unreachable before bpf_tramp_image_put(). 624 * 625 * Progs are patched out of the image when they are detached, see 626 * bpf_trampoline_skip_prog(), so they can be freed even if a task is 627 * still in the image. 628 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on 629 * first few asm instructions to execute and call into 630 * __bpf_tramp_enter->percpu_ref_get. 631 * Then use percpu_ref_kill to wait for the trampoline and the original 632 * function to finish. 633 * Then use call_rcu_tasks() to make sure few asm insns in 634 * the trampoline epilogue are done as well. 635 * 636 * In !PREEMPT case the task that got interrupted in the first asm 637 * insns won't go through an RCU quiescent state which the 638 * percpu_ref_kill will be waiting for. Hence the first 639 * call_rcu_tasks() is not necessary. 640 */ 641 if (im->call_orig) { 642 if (IS_ENABLED(CONFIG_TASKS_RCU)) 643 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 644 else 645 percpu_ref_kill(&im->pcref); 646 return; 647 } 648 649 /* The trampoline without fexit and fmod_ret progs doesn't call original 650 * function and doesn't use percpu_ref. 651 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. 652 * Then use call_rcu_tasks() to wait for the rest of trampoline asm 653 * and normal progs. 654 */ 655 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 656 } 657 658 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size, int nr_progs) 659 { 660 struct bpf_tramp_image *im; 661 struct bpf_ksym *ksym; 662 void *image; 663 int err = -ENOMEM; 664 665 im = kzalloc_obj(*im); 666 if (!im) 667 goto out; 668 669 im->skips = kzalloc_objs(*im->skips, nr_progs); 670 if (!im->skips) 671 goto out_free_im; 672 673 err = bpf_jit_charge_modmem(size); 674 if (err) 675 goto out_free_im; 676 im->size = size; 677 678 err = -ENOMEM; 679 im->image = image = arch_alloc_bpf_trampoline(size); 680 if (!image) 681 goto out_uncharge; 682 683 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL); 684 if (err) 685 goto out_free_image; 686 687 ksym = &im->ksym; 688 INIT_LIST_HEAD_RCU(&ksym->lnode); 689 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key); 690 bpf_image_ksym_init(image, size, ksym); 691 bpf_image_ksym_add(ksym); 692 return im; 693 694 out_free_image: 695 arch_free_bpf_trampoline(im->image, im->size); 696 out_uncharge: 697 bpf_jit_uncharge_modmem(size); 698 out_free_im: 699 kfree(im->skips); 700 kfree(im); 701 out: 702 return ERR_PTR(err); 703 } 704 705 void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags) 706 { 707 trampoline_lock(tr); 708 tr->flags |= flags; 709 trampoline_unlock(tr); 710 } 711 712 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex, 713 const struct bpf_trampoline_ops *ops, void *data) 714 { 715 struct bpf_tramp_image *im; 716 struct bpf_tramp_nodes *tnodes; 717 u32 orig_flags = tr->flags; 718 bool ip_arg = false; 719 int err, total, size; 720 721 tnodes = bpf_trampoline_get_progs(tr, &total, &ip_arg); 722 if (IS_ERR(tnodes)) 723 return PTR_ERR(tnodes); 724 725 if (total == 0) { 726 err = ops->unregister_fentry(tr, orig_flags, data); 727 goto out; 728 } 729 730 /* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */ 731 tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX); 732 733 if (tnodes[BPF_TRAMP_FEXIT].nr_nodes || 734 tnodes[BPF_TRAMP_MODIFY_RETURN].nr_nodes) { 735 /* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME 736 * should not be set together. 737 */ 738 tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 739 } else { 740 tr->flags |= BPF_TRAMP_F_RESTORE_REGS; 741 } 742 743 if (ip_arg) 744 tr->flags |= BPF_TRAMP_F_IP_ARG; 745 746 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 747 again: 748 if (tr->flags & BPF_TRAMP_F_CALL_ORIG) { 749 if (tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) { 750 /* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the 751 * first try, reset it in the second try. 752 */ 753 tr->flags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME; 754 } else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) { 755 /* Use "jmp" instead of "call" for the trampoline 756 * in the origin call case, and we don't need to 757 * skip the frame. 758 */ 759 tr->flags &= ~BPF_TRAMP_F_SKIP_FRAME; 760 } 761 } 762 #endif 763 764 size = arch_bpf_trampoline_size(&tr->func.model, tr->flags, 765 tnodes, tr->func.addr); 766 if (size < 0) { 767 err = size; 768 goto out; 769 } 770 771 if (size > PAGE_SIZE) { 772 err = -E2BIG; 773 goto out; 774 } 775 776 im = bpf_tramp_image_alloc(tr->key, size, total); 777 if (IS_ERR(im)) { 778 err = PTR_ERR(im); 779 goto out; 780 } 781 im->call_orig = tr->flags & BPF_TRAMP_F_CALL_ORIG; 782 783 err = arch_prepare_bpf_trampoline(im, im->image, im->image + size, 784 &tr->func.model, tr->flags, tnodes, 785 tr->func.addr); 786 if (err < 0) 787 goto out_free; 788 789 err = arch_protect_bpf_trampoline(im->image, im->size); 790 if (err) 791 goto out_free; 792 793 if (tr->cur_image) 794 /* progs already running at this address */ 795 err = ops->modify_fentry(tr, orig_flags, im, lock_direct_mutex, data); 796 else 797 /* first time registering */ 798 err = ops->register_fentry(tr, im, data); 799 800 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 801 if (err == -EAGAIN) { 802 /* -EAGAIN from bpf_tramp_ftrace_ops_func. Now 803 * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the 804 * trampoline again, and retry register. 805 */ 806 bpf_tramp_image_free(im); 807 goto again; 808 } 809 #endif 810 811 out_free: 812 if (err) { 813 bpf_tramp_image_free(im); 814 } else { 815 /* track the image until it is freed, for bpf_trampoline_skip_prog() */ 816 refcount_inc(&tr->refcnt); 817 im->tr = tr; 818 list_add(&im->list, &tr->images); 819 } 820 out: 821 /* If any error happens, restore previous flags */ 822 if (err) 823 tr->flags = orig_flags; 824 kfree(tnodes); 825 return err; 826 } 827 828 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) 829 { 830 switch (prog->expected_attach_type) { 831 case BPF_TRACE_FENTRY: 832 case BPF_TRACE_FENTRY_MULTI: 833 return BPF_TRAMP_FENTRY; 834 case BPF_MODIFY_RETURN: 835 return BPF_TRAMP_MODIFY_RETURN; 836 case BPF_TRACE_FEXIT: 837 case BPF_TRACE_FEXIT_MULTI: 838 return BPF_TRAMP_FEXIT; 839 case BPF_TRACE_FSESSION: 840 case BPF_TRACE_FSESSION_MULTI: 841 return BPF_TRAMP_FSESSION; 842 case BPF_LSM_MAC: 843 if (!prog->aux->attach_func_proto->type) 844 /* The function returns void, we cannot modify its 845 * return value. 846 */ 847 return BPF_TRAMP_FEXIT; 848 else 849 return BPF_TRAMP_MODIFY_RETURN; 850 default: 851 return BPF_TRAMP_REPLACE; 852 } 853 } 854 855 static int bpf_freplace_link_tgt_prog(struct bpf_prog *tgt_prog) 856 { 857 struct bpf_prog_aux *aux = tgt_prog->aux; 858 859 guard(mutex)(&aux->ext_mutex); 860 if (aux->prog_array_member_cnt) 861 /* Program extensions can not extend target prog when the target 862 * prog has been updated to any prog_array map as tail callee. 863 * It's to prevent a potential infinite loop like: 864 * tgt prog entry -> tgt prog subprog -> freplace prog entry 865 * --tailcall-> tgt prog entry. 866 */ 867 return -EBUSY; 868 869 aux->freplace_link_cnt++; 870 return 0; 871 } 872 873 static struct bpf_tramp_node *fsession_exit(struct bpf_tramp_node *node) 874 { 875 if (node->link->type == BPF_LINK_TYPE_TRACING) { 876 struct bpf_tracing_link *link; 877 878 link = container_of(node->link, struct bpf_tracing_link, link.link); 879 return &link->fexit; 880 } else if (node->link->type == BPF_LINK_TYPE_TRACING_MULTI) { 881 struct bpf_tracing_multi_link *link; 882 struct bpf_tracing_multi_node *mnode; 883 884 link = container_of(node->link, struct bpf_tracing_multi_link, link); 885 mnode = container_of(node, struct bpf_tracing_multi_node, node); 886 return &link->fexits[mnode - link->nodes]; 887 } 888 return NULL; 889 } 890 891 static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, 892 struct bpf_tramp_node *node, 893 int cnt) 894 { 895 enum bpf_tramp_prog_type kind; 896 struct bpf_tramp_node *node_existing, *fexit; 897 struct hlist_head *prog_list; 898 899 kind = bpf_attach_type_to_tramp(node->link->prog); 900 if (kind == BPF_TRAMP_FSESSION) { 901 prog_list = &tr->progs_hlist[BPF_TRAMP_FENTRY]; 902 cnt++; 903 } else { 904 prog_list = &tr->progs_hlist[kind]; 905 } 906 if (cnt >= BPF_MAX_TRAMP_LINKS) 907 return -E2BIG; 908 if (!hlist_unhashed(&node->tramp_hlist)) 909 /* prog already linked */ 910 return -EBUSY; 911 hlist_for_each_entry(node_existing, prog_list, tramp_hlist) { 912 if (node_existing->link->prog != node->link->prog) 913 continue; 914 /* prog already linked */ 915 return -EBUSY; 916 } 917 918 hlist_add_head(&node->tramp_hlist, prog_list); 919 node->link->prog->aux->tramp_linked = true; 920 if (kind == BPF_TRAMP_FSESSION) { 921 tr->progs_cnt[BPF_TRAMP_FENTRY]++; 922 fexit = fsession_exit(node); 923 if (WARN_ON_ONCE(!fexit)) 924 return -EINVAL; 925 hlist_add_head(&fexit->tramp_hlist, &tr->progs_hlist[BPF_TRAMP_FEXIT]); 926 tr->progs_cnt[BPF_TRAMP_FEXIT]++; 927 } else { 928 tr->progs_cnt[kind]++; 929 } 930 return 0; 931 } 932 933 /* 934 * Patch the nop in front of a prog call to a jump over it. A task can be 935 * preempted anywhere in the image, so archs that need several instructions for 936 * a jump of any range patch a single near branch here instead. 937 */ 938 int __weak arch_bpf_trampoline_skip(void *nop, void *target) 939 { 940 return bpf_arch_text_poke(nop, BPF_MOD_NOP, BPF_MOD_JUMP, NULL, target); 941 } 942 943 /* 944 * prog was detached and can be freed, but tasks may still be running in images 945 * that call it, sleeping in an earlier prog for example. They can be in any 946 * image that is not freed yet, not only in cur_image, so patch all of them to 947 * jump over prog. 948 */ 949 static void bpf_trampoline_skip_prog(struct bpf_trampoline *tr, struct bpf_prog *prog) 950 { 951 struct bpf_tramp_image *im; 952 int i, err; 953 954 list_for_each_entry(im, &tr->images, list) { 955 for (i = 0; i < im->nr_skips; i++) { 956 struct bpf_tramp_skip *skip = &im->skips[i]; 957 958 if (skip->prog != prog) 959 continue; 960 err = arch_bpf_trampoline_skip(skip->nop, skip->target); 961 WARN_ON_ONCE(err); 962 /* not a nop anymore, and prog's address can be reused */ 963 skip->prog = NULL; 964 } 965 } 966 } 967 968 static void bpf_trampoline_remove_prog(struct bpf_trampoline *tr, 969 struct bpf_tramp_node *node) 970 { 971 enum bpf_tramp_prog_type kind; 972 struct bpf_tramp_node *fexit; 973 974 kind = bpf_attach_type_to_tramp(node->link->prog); 975 if (kind == BPF_TRAMP_FSESSION) { 976 fexit = fsession_exit(node); 977 if (WARN_ON_ONCE(!fexit)) 978 return; 979 hlist_del_init(&fexit->tramp_hlist); 980 tr->progs_cnt[BPF_TRAMP_FEXIT]--; 981 kind = BPF_TRAMP_FENTRY; 982 } 983 hlist_del_init(&node->tramp_hlist); 984 tr->progs_cnt[kind]--; 985 bpf_trampoline_skip_prog(tr, node->link->prog); 986 } 987 988 static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node, 989 struct bpf_trampoline *tr, 990 struct bpf_prog *tgt_prog, 991 const struct bpf_trampoline_ops *ops, 992 void *data) 993 { 994 enum bpf_tramp_prog_type kind; 995 int err = 0; 996 int cnt = 0, i; 997 998 kind = bpf_attach_type_to_tramp(node->link->prog); 999 /* 1000 * Arena ctx args are converted only by struct_ops indirect 1001 * trampolines. They must never be attached to a generic trampoline. 1002 */ 1003 if (WARN_ON_ONCE(bpf_prog_has_arena_ctx_arg(node->link->prog))) 1004 return -ENOTSUPP; 1005 1006 if (tr->extension_prog) 1007 /* cannot attach fentry/fexit if extension prog is attached. 1008 * cannot overwrite extension prog either. 1009 */ 1010 return -EBUSY; 1011 1012 for (i = 0; i < BPF_TRAMP_MAX; i++) 1013 cnt += tr->progs_cnt[i]; 1014 1015 if (kind == BPF_TRAMP_REPLACE) { 1016 /* Cannot attach extension if fentry/fexit are in use. */ 1017 if (cnt) 1018 return -EBUSY; 1019 err = bpf_freplace_link_tgt_prog(tgt_prog); 1020 if (err) 1021 return err; 1022 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_NOP, 1023 BPF_MOD_JUMP, NULL, 1024 node->link->prog->bpf_func); 1025 if (err) { 1026 /* Undo the claim from bpf_freplace_link_tgt_prog(). */ 1027 guard(mutex)(&tgt_prog->aux->ext_mutex); 1028 tgt_prog->aux->freplace_link_cnt--; 1029 return err; 1030 } 1031 tr->extension_prog = node->link->prog; 1032 return 0; 1033 } 1034 err = bpf_trampoline_add_prog(tr, node, cnt); 1035 if (err) 1036 return err; 1037 err = bpf_trampoline_update(tr, true /* lock_direct_mutex */, ops, data); 1038 if (err) 1039 bpf_trampoline_remove_prog(tr, node); 1040 return err; 1041 } 1042 1043 int bpf_trampoline_link_prog(struct bpf_tramp_node *node, 1044 struct bpf_trampoline *tr, 1045 struct bpf_prog *tgt_prog) 1046 { 1047 int err; 1048 1049 trampoline_lock(tr); 1050 err = __bpf_trampoline_link_prog(node, tr, tgt_prog, &trampoline_ops, NULL); 1051 trampoline_unlock(tr); 1052 return err; 1053 } 1054 1055 static int __bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, 1056 struct bpf_trampoline *tr, 1057 struct bpf_prog *tgt_prog, 1058 const struct bpf_trampoline_ops *ops, 1059 void *data) 1060 { 1061 enum bpf_tramp_prog_type kind; 1062 int err; 1063 1064 kind = bpf_attach_type_to_tramp(node->link->prog); 1065 if (kind == BPF_TRAMP_REPLACE) { 1066 WARN_ON_ONCE(!tr->extension_prog); 1067 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, 1068 BPF_MOD_NOP, 1069 tr->extension_prog->bpf_func, NULL); 1070 tr->extension_prog = NULL; 1071 guard(mutex)(&tgt_prog->aux->ext_mutex); 1072 tgt_prog->aux->freplace_link_cnt--; 1073 return err; 1074 } 1075 bpf_trampoline_remove_prog(tr, node); 1076 return bpf_trampoline_update(tr, true /* lock_direct_mutex */, ops, data); 1077 } 1078 1079 /* bpf_trampoline_unlink_prog() should never fail. */ 1080 int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, 1081 struct bpf_trampoline *tr, 1082 struct bpf_prog *tgt_prog) 1083 { 1084 int err; 1085 1086 trampoline_lock(tr); 1087 err = __bpf_trampoline_unlink_prog(node, tr, tgt_prog, &trampoline_ops, NULL); 1088 trampoline_unlock(tr); 1089 return err; 1090 } 1091 1092 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM) 1093 static void bpf_shim_tramp_link_release(struct bpf_link *link) 1094 { 1095 struct bpf_shim_tramp_link *shim_link = 1096 container_of(link, struct bpf_shim_tramp_link, link.link); 1097 int err; 1098 1099 /* paired with 'shim_link->trampoline = tr' in bpf_trampoline_link_cgroup_shim */ 1100 if (!shim_link->trampoline) 1101 return; 1102 1103 err = bpf_trampoline_unlink_prog(&shim_link->link.node, shim_link->trampoline, NULL); 1104 WARN_ONCE(err, "bpf_trampoline_unlink_prog failed: %d\n", err); 1105 1106 bpf_trampoline_put(shim_link->trampoline); 1107 } 1108 1109 static void bpf_shim_tramp_link_dealloc(struct bpf_link *link) 1110 { 1111 struct bpf_shim_tramp_link *shim_link = 1112 container_of(link, struct bpf_shim_tramp_link, link.link); 1113 1114 kfree(shim_link); 1115 } 1116 1117 static const struct bpf_link_ops bpf_shim_tramp_link_lops = { 1118 .release = bpf_shim_tramp_link_release, 1119 .dealloc = bpf_shim_tramp_link_dealloc, 1120 }; 1121 1122 static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog, 1123 bpf_func_t bpf_func, 1124 int cgroup_atype, 1125 enum bpf_attach_type attach_type) 1126 { 1127 struct bpf_shim_tramp_link *shim_link = NULL; 1128 struct bpf_prog *p; 1129 1130 shim_link = kzalloc_obj(*shim_link, GFP_USER); 1131 if (!shim_link) 1132 return NULL; 1133 1134 p = bpf_prog_alloc(1, 0); 1135 if (!p) { 1136 kfree(shim_link); 1137 return NULL; 1138 } 1139 1140 p->jited = false; 1141 p->bpf_func = bpf_func; 1142 1143 p->aux->cgroup_atype = cgroup_atype; 1144 p->aux->attach_func_proto = prog->aux->attach_func_proto; 1145 p->aux->attach_btf_id = prog->aux->attach_btf_id; 1146 p->aux->attach_btf = prog->aux->attach_btf; 1147 btf_get(p->aux->attach_btf); 1148 p->type = BPF_PROG_TYPE_LSM; 1149 p->expected_attach_type = BPF_LSM_MAC; 1150 bpf_prog_inc(p); 1151 bpf_tramp_link_init(&shim_link->link, BPF_LINK_TYPE_UNSPEC, 1152 &bpf_shim_tramp_link_lops, p, attach_type, 0); 1153 bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype); 1154 1155 return shim_link; 1156 } 1157 1158 static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr, 1159 bpf_func_t bpf_func) 1160 { 1161 struct bpf_tramp_node *node; 1162 int kind; 1163 1164 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 1165 hlist_for_each_entry(node, &tr->progs_hlist[kind], tramp_hlist) { 1166 struct bpf_prog *p = node->link->prog; 1167 1168 if (p->bpf_func == bpf_func) 1169 return container_of(node, struct bpf_shim_tramp_link, link.node); 1170 } 1171 } 1172 1173 return NULL; 1174 } 1175 1176 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 1177 int cgroup_atype, 1178 enum bpf_attach_type attach_type) 1179 { 1180 struct bpf_shim_tramp_link *shim_link = NULL; 1181 struct bpf_attach_target_info tgt_info = {}; 1182 struct bpf_trampoline *tr; 1183 bpf_func_t bpf_func; 1184 u64 key; 1185 int err; 1186 1187 err = bpf_check_attach_target(NULL, prog, NULL, 1188 prog->aux->attach_btf_id, 1189 &tgt_info); 1190 if (err) 1191 return err; 1192 1193 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 1194 prog->aux->attach_btf_id); 1195 1196 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 1197 tr = bpf_trampoline_get(key, &tgt_info); 1198 if (!tr) 1199 return -ENOMEM; 1200 1201 trampoline_lock(tr); 1202 1203 shim_link = cgroup_shim_find(tr, bpf_func); 1204 if (shim_link && !IS_ERR(bpf_link_inc_not_zero(&shim_link->link.link))) { 1205 /* Reusing existing shim attached by the other program. */ 1206 trampoline_unlock(tr); 1207 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 1208 return 0; 1209 } 1210 1211 /* Allocate and install new shim. */ 1212 1213 shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype, attach_type); 1214 if (!shim_link) { 1215 err = -ENOMEM; 1216 goto err; 1217 } 1218 1219 err = __bpf_trampoline_link_prog(&shim_link->link.node, tr, NULL, &trampoline_ops, NULL); 1220 if (err) 1221 goto err; 1222 1223 shim_link->trampoline = tr; 1224 /* note, we're still holding tr refcnt from above */ 1225 1226 trampoline_unlock(tr); 1227 1228 return 0; 1229 err: 1230 trampoline_unlock(tr); 1231 1232 if (shim_link) 1233 bpf_link_put(&shim_link->link.link); 1234 1235 /* have to release tr while _not_ holding pool mutex for trampoline */ 1236 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 1237 1238 return err; 1239 } 1240 1241 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog) 1242 { 1243 struct bpf_shim_tramp_link *shim_link = NULL; 1244 struct bpf_trampoline *tr; 1245 bpf_func_t bpf_func; 1246 u64 key; 1247 1248 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 1249 prog->aux->attach_btf_id); 1250 1251 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 1252 tr = bpf_trampoline_lookup(key, 0); 1253 if (WARN_ON_ONCE(!tr)) 1254 return; 1255 1256 trampoline_lock(tr); 1257 shim_link = cgroup_shim_find(tr, bpf_func); 1258 trampoline_unlock(tr); 1259 1260 if (shim_link) 1261 bpf_link_put(&shim_link->link.link); 1262 1263 bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */ 1264 } 1265 #endif 1266 1267 struct bpf_trampoline *bpf_trampoline_get(u64 key, 1268 struct bpf_attach_target_info *tgt_info) 1269 { 1270 struct bpf_trampoline *tr; 1271 1272 tr = bpf_trampoline_lookup(key, tgt_info->tgt_addr); 1273 if (!tr) 1274 return NULL; 1275 1276 trampoline_lock(tr); 1277 if (tr->func.addr) 1278 goto out; 1279 1280 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); 1281 tr->func.addr = (void *)tgt_info->tgt_addr; 1282 tr->func.ftrace_managed = tr->ip != 0; 1283 out: 1284 trampoline_unlock(tr); 1285 return tr; 1286 } 1287 1288 void bpf_trampoline_put(struct bpf_trampoline *tr) 1289 { 1290 int i; 1291 1292 if (!tr) 1293 return; 1294 mutex_lock(&trampoline_mutex); 1295 if (!refcount_dec_and_test(&tr->refcnt)) 1296 goto out; 1297 1298 for (i = 0; i < BPF_TRAMP_MAX; i++) 1299 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i]))) 1300 goto out; 1301 1302 /* 1303 * All progs are detached and the last image has been freed, images 1304 * hold a reference on the trampoline until then. 1305 */ 1306 hlist_del(&tr->hlist_key); 1307 hlist_del(&tr->hlist_ip); 1308 direct_ops_free(tr); 1309 kfree(tr); 1310 out: 1311 mutex_unlock(&trampoline_mutex); 1312 } 1313 1314 #define NO_START_TIME 1 1315 static __always_inline u64 notrace bpf_prog_start_time(void) 1316 { 1317 u64 start = NO_START_TIME; 1318 1319 if (static_branch_unlikely(&bpf_stats_enabled_key)) { 1320 start = sched_clock(); 1321 if (unlikely(!start)) 1322 start = NO_START_TIME; 1323 } 1324 return start; 1325 } 1326 1327 /* The logic is similar to bpf_prog_run(), but with an explicit 1328 * rcu_read_lock() and migrate_disable() which are required 1329 * for the trampoline. The macro is split into 1330 * call __bpf_prog_enter 1331 * call prog->bpf_func 1332 * call __bpf_prog_exit 1333 * 1334 * __bpf_prog_enter returns: 1335 * 0 - skip execution of the bpf prog 1336 * 1 - execute bpf prog 1337 * [2..MAX_U64] - execute bpf prog and record execution time. 1338 * This is start time. 1339 */ 1340 static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) 1341 __acquires(RCU) 1342 { 1343 rcu_read_lock_dont_migrate(); 1344 1345 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1346 1347 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 1348 bpf_prog_inc_misses_counter(prog); 1349 if (prog->aux->recursion_detected) 1350 prog->aux->recursion_detected(prog); 1351 return 0; 1352 } 1353 return bpf_prog_start_time(); 1354 } 1355 1356 static void notrace __update_prog_stats(struct bpf_prog *prog, u64 start) 1357 { 1358 struct bpf_prog_stats *stats; 1359 unsigned long flags; 1360 u64 duration; 1361 1362 /* 1363 * static_key could be enabled in __bpf_prog_enter* and disabled in 1364 * __bpf_prog_exit*. And vice versa. Check that 'start' is valid. 1365 */ 1366 if (start <= NO_START_TIME) 1367 return; 1368 1369 duration = sched_clock() - start; 1370 stats = this_cpu_ptr(prog->stats); 1371 flags = u64_stats_update_begin_irqsave(&stats->syncp); 1372 u64_stats_inc(&stats->cnt); 1373 u64_stats_add(&stats->nsecs, duration); 1374 u64_stats_update_end_irqrestore(&stats->syncp, flags); 1375 } 1376 1377 static __always_inline void notrace update_prog_stats(struct bpf_prog *prog, 1378 u64 start) 1379 { 1380 if (static_branch_unlikely(&bpf_stats_enabled_key)) 1381 __update_prog_stats(prog, start); 1382 } 1383 1384 static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start, 1385 struct bpf_tramp_run_ctx *run_ctx) 1386 __releases(RCU) 1387 { 1388 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1389 1390 update_prog_stats(prog, start); 1391 bpf_prog_put_recursion_context(prog); 1392 rcu_read_unlock_migrate(); 1393 } 1394 1395 static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog, 1396 struct bpf_tramp_run_ctx *run_ctx) 1397 __acquires(RCU) 1398 { 1399 /* Runtime stats are exported via actual BPF_LSM_CGROUP 1400 * programs, not the shims. 1401 */ 1402 rcu_read_lock_dont_migrate(); 1403 1404 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1405 1406 return NO_START_TIME; 1407 } 1408 1409 static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start, 1410 struct bpf_tramp_run_ctx *run_ctx) 1411 __releases(RCU) 1412 { 1413 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1414 1415 rcu_read_unlock_migrate(); 1416 } 1417 1418 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, 1419 struct bpf_tramp_run_ctx *run_ctx) 1420 { 1421 rcu_read_lock_trace(); 1422 migrate_disable(); 1423 might_fault(); 1424 1425 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1426 1427 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 1428 bpf_prog_inc_misses_counter(prog); 1429 if (prog->aux->recursion_detected) 1430 prog->aux->recursion_detected(prog); 1431 return 0; 1432 } 1433 return bpf_prog_start_time(); 1434 } 1435 1436 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start, 1437 struct bpf_tramp_run_ctx *run_ctx) 1438 { 1439 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1440 1441 update_prog_stats(prog, start); 1442 bpf_prog_put_recursion_context(prog); 1443 migrate_enable(); 1444 rcu_read_unlock_trace(); 1445 } 1446 1447 static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, 1448 struct bpf_tramp_run_ctx *run_ctx) 1449 { 1450 rcu_read_lock_trace(); 1451 migrate_disable(); 1452 might_fault(); 1453 1454 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1455 1456 return bpf_prog_start_time(); 1457 } 1458 1459 static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start, 1460 struct bpf_tramp_run_ctx *run_ctx) 1461 { 1462 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1463 1464 update_prog_stats(prog, start); 1465 migrate_enable(); 1466 rcu_read_unlock_trace(); 1467 } 1468 1469 static u64 notrace __bpf_prog_enter(struct bpf_prog *prog, 1470 struct bpf_tramp_run_ctx *run_ctx) 1471 __acquires(RCU) 1472 { 1473 rcu_read_lock_dont_migrate(); 1474 1475 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1476 1477 return bpf_prog_start_time(); 1478 } 1479 1480 static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, 1481 struct bpf_tramp_run_ctx *run_ctx) 1482 __releases(RCU) 1483 { 1484 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1485 1486 update_prog_stats(prog, start); 1487 rcu_read_unlock_migrate(); 1488 } 1489 1490 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr) 1491 { 1492 percpu_ref_get(&tr->pcref); 1493 } 1494 1495 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr) 1496 { 1497 percpu_ref_put(&tr->pcref); 1498 } 1499 1500 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog) 1501 { 1502 bool sleepable = prog->sleepable; 1503 1504 if (bpf_prog_check_recur(prog)) 1505 return sleepable ? __bpf_prog_enter_sleepable_recur : 1506 __bpf_prog_enter_recur; 1507 1508 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1509 prog->expected_attach_type == BPF_LSM_CGROUP) 1510 return __bpf_prog_enter_lsm_cgroup; 1511 1512 return sleepable ? __bpf_prog_enter_sleepable : __bpf_prog_enter; 1513 } 1514 1515 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog) 1516 { 1517 bool sleepable = prog->sleepable; 1518 1519 if (bpf_prog_check_recur(prog)) 1520 return sleepable ? __bpf_prog_exit_sleepable_recur : 1521 __bpf_prog_exit_recur; 1522 1523 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1524 prog->expected_attach_type == BPF_LSM_CGROUP) 1525 return __bpf_prog_exit_lsm_cgroup; 1526 1527 return sleepable ? __bpf_prog_exit_sleepable : __bpf_prog_exit; 1528 } 1529 1530 int __weak 1531 arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end, 1532 const struct btf_func_model *m, u32 flags, 1533 struct bpf_tramp_nodes *tnodes, 1534 void *func_addr) 1535 { 1536 return -ENOTSUPP; 1537 } 1538 1539 void * __weak arch_alloc_bpf_trampoline(unsigned int size) 1540 { 1541 void *image; 1542 1543 if (WARN_ON_ONCE(size > PAGE_SIZE)) 1544 return NULL; 1545 image = bpf_jit_alloc_exec(PAGE_SIZE); 1546 if (image) 1547 set_vm_flush_reset_perms(image); 1548 return image; 1549 } 1550 1551 void __weak arch_free_bpf_trampoline(void *image, unsigned int size) 1552 { 1553 WARN_ON_ONCE(size > PAGE_SIZE); 1554 /* bpf_jit_free_exec doesn't need "size", but 1555 * bpf_prog_pack_free() needs it. 1556 */ 1557 bpf_jit_free_exec(image); 1558 } 1559 1560 int __weak arch_protect_bpf_trampoline(void *image, unsigned int size) 1561 { 1562 WARN_ON_ONCE(size > PAGE_SIZE); 1563 return set_memory_rox((long)image, 1); 1564 } 1565 1566 int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, 1567 struct bpf_tramp_nodes *tnodes, void *func_addr) 1568 { 1569 return -ENOTSUPP; 1570 } 1571 1572 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) && \ 1573 defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) && \ 1574 defined(CONFIG_BPF_SYSCALL) 1575 1576 static void trampoline_lock_all(void) 1577 { 1578 int i; 1579 1580 for (i = 0; i < TRAMPOLINE_LOCKS_TABLE_SIZE; i++) 1581 mutex_lock(&trampoline_locks[i].mutex); 1582 } 1583 1584 static void trampoline_unlock_all(void) 1585 { 1586 int i; 1587 1588 for (i = 0; i < TRAMPOLINE_LOCKS_TABLE_SIZE; i++) 1589 mutex_unlock(&trampoline_locks[i].mutex); 1590 } 1591 1592 static void remove_tracing_multi_data(struct bpf_tracing_multi_data *data) 1593 { 1594 ftrace_hash_remove(data->reg); 1595 ftrace_hash_remove(data->unreg); 1596 ftrace_hash_remove(data->modify); 1597 } 1598 1599 static void clear_tracing_multi_data(struct bpf_tracing_multi_data *data) 1600 { 1601 remove_tracing_multi_data(data); 1602 1603 free_ftrace_hash(data->reg); 1604 free_ftrace_hash(data->unreg); 1605 free_ftrace_hash(data->modify); 1606 } 1607 1608 static int init_tracing_multi_data(struct bpf_tracing_multi_data *data) 1609 { 1610 data->reg = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 1611 data->unreg = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 1612 data->modify = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 1613 1614 if (!data->reg || !data->unreg || !data->modify) { 1615 clear_tracing_multi_data(data); 1616 return -ENOMEM; 1617 } 1618 return 0; 1619 } 1620 1621 static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry *entry, 1622 unsigned long ip, unsigned long direct) 1623 { 1624 entry->ip = ip; 1625 entry->direct = direct; 1626 add_ftrace_hash_entry(hash, entry); 1627 } 1628 1629 static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_image *im, void *ptr) 1630 { 1631 unsigned long addr = (unsigned long) im->image; 1632 struct bpf_tracing_multi_data *data = ptr; 1633 unsigned long ip = tr->ip; 1634 1635 /* 1636 * Different BTF ids get different trampolines, but they can still 1637 * resolve to the same address (aliases, functions of the same name). 1638 * There's only one call site to attach to. 1639 */ 1640 if (ftrace_lookup_ip(data->reg, ip)) 1641 return -EINVAL; 1642 1643 if (bpf_trampoline_use_jmp(tr->flags)) 1644 addr = ftrace_jmp_set(addr); 1645 1646 ftrace_hash_add(data->reg, data->entry, ip, addr); 1647 tr->cur_image = im; 1648 return 0; 1649 } 1650 1651 static int unregister_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, void *ptr) 1652 { 1653 unsigned long addr = (unsigned long) tr->cur_image->image; 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, tr->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 struct bpf_tracing_multi_data *data = ptr; 1669 1670 if (bpf_trampoline_use_jmp(tr->flags)) 1671 addr = ftrace_jmp_set(addr); 1672 1673 ftrace_hash_add(data->modify, data->entry, tr->ip, addr); 1674 tr->cur_image = im; 1675 return 0; 1676 } 1677 1678 static const struct bpf_trampoline_ops trampoline_multi_ops = { 1679 .register_fentry = register_fentry_multi, 1680 .unregister_fentry = unregister_fentry_multi, 1681 .modify_fentry = modify_fentry_multi, 1682 }; 1683 1684 static void bpf_trampoline_multi_attach_init(struct bpf_trampoline *tr) 1685 { 1686 tr->multi_attach.old_image = tr->cur_image; 1687 tr->multi_attach.old_flags = tr->flags; 1688 } 1689 1690 static void bpf_trampoline_multi_attach_free(struct bpf_trampoline *tr) 1691 { 1692 /* 1693 * Only free old_image if it is no longer the active image. 1694 * When bpf_trampoline_update() fails before modify_fentry_multi()/ 1695 * unregister_fentry_multi() is called, cur_image is unchanged 1696 * (cur_image == old_image) and ftrace still points to it. Freeing 1697 * it would cause a UAF when ftrace calls into the freed memory. 1698 * On success, cur_image is either a new image or NULL, so 1699 * old_image != cur_image means the image is stale. 1700 */ 1701 if (tr->multi_attach.old_image && 1702 tr->multi_attach.old_image != tr->cur_image) 1703 bpf_tramp_image_put(tr->multi_attach.old_image); 1704 1705 tr->multi_attach.old_image = NULL; 1706 tr->multi_attach.old_flags = 0; 1707 } 1708 1709 static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr) 1710 { 1711 if (tr->cur_image) 1712 bpf_tramp_image_put(tr->cur_image); 1713 tr->cur_image = tr->multi_attach.old_image; 1714 tr->flags = tr->multi_attach.old_flags; 1715 1716 tr->multi_attach.old_image = NULL; 1717 tr->multi_attach.old_flags = 0; 1718 } 1719 1720 #define for_each_mnode_cnt(mnode, link, cnt) \ 1721 for (i = 0, mnode = &link->nodes[i]; i < cnt; i++, mnode = &link->nodes[i]) 1722 1723 #define for_each_mnode(mnode, link) \ 1724 for_each_mnode_cnt(mnode, link, link->nodes_cnt) 1725 1726 int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, 1727 struct bpf_tracing_multi_link *link) 1728 { 1729 struct bpf_tracing_multi_data *data = &link->data; 1730 struct bpf_attach_target_info tgt_info = {}; 1731 struct btf *btf = prog->aux->attach_btf; 1732 struct bpf_tracing_multi_node *mnode; 1733 struct bpf_trampoline *tr; 1734 int i, err, rollback_cnt; 1735 u64 key; 1736 1737 for_each_mnode(mnode, link) { 1738 rollback_cnt = i; 1739 1740 err = bpf_check_attach_btf_id_multi(btf, prog, ids[i], &tgt_info); 1741 if (err) 1742 goto rollback_put; 1743 1744 key = bpf_trampoline_compute_key(NULL, btf, ids[i]); 1745 1746 tr = bpf_trampoline_get(key, &tgt_info); 1747 if (!tr) { 1748 err = -ENOMEM; 1749 goto rollback_put; 1750 } 1751 1752 mnode->trampoline = tr; 1753 mnode->node.link = &link->link; 1754 mnode->node.cookie = link->cookies ? link->cookies[i] : 0; 1755 1756 if (prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI) { 1757 link->fexits[i].link = &link->link; 1758 link->fexits[i].cookie = link->cookies ? link->cookies[i] : 0; 1759 } 1760 1761 cond_resched(); 1762 } 1763 1764 err = init_tracing_multi_data(data); 1765 if (err) { 1766 rollback_cnt = link->nodes_cnt; 1767 goto rollback_put; 1768 } 1769 1770 trampoline_lock_all(); 1771 1772 for_each_mnode(mnode, link) { 1773 bpf_trampoline_multi_attach_init(mnode->trampoline); 1774 1775 data->entry = &mnode->entry; 1776 err = __bpf_trampoline_link_prog(&mnode->node, mnode->trampoline, NULL, 1777 &trampoline_multi_ops, data); 1778 if (err) { 1779 rollback_cnt = i; 1780 goto rollback_unlink; 1781 } 1782 } 1783 1784 rollback_cnt = link->nodes_cnt; 1785 if (ftrace_hash_count(data->reg)) { 1786 err = update_ftrace_direct_add(&direct_ops, data->reg); 1787 if (err) 1788 goto rollback_unlink; 1789 } 1790 1791 if (ftrace_hash_count(data->modify)) { 1792 err = update_ftrace_direct_mod(&direct_ops, data->modify, true); 1793 if (err) { 1794 if (ftrace_hash_count(data->reg)) 1795 WARN_ON_ONCE(update_ftrace_direct_del(&direct_ops, data->reg)); 1796 goto rollback_unlink; 1797 } 1798 } 1799 1800 for_each_mnode(mnode, link) 1801 bpf_trampoline_multi_attach_free(mnode->trampoline); 1802 1803 trampoline_unlock_all(); 1804 1805 remove_tracing_multi_data(data); 1806 return 0; 1807 1808 rollback_unlink: 1809 for_each_mnode_cnt(mnode, link, rollback_cnt) { 1810 bpf_trampoline_remove_prog(mnode->trampoline, &mnode->node); 1811 bpf_trampoline_multi_attach_rollback(mnode->trampoline); 1812 } 1813 1814 trampoline_unlock_all(); 1815 1816 clear_tracing_multi_data(data); 1817 rollback_cnt = link->nodes_cnt; 1818 1819 rollback_put: 1820 for_each_mnode_cnt(mnode, link, rollback_cnt) 1821 bpf_trampoline_put(mnode->trampoline); 1822 1823 return err; 1824 } 1825 1826 void bpf_trampoline_multi_detach(struct bpf_prog *prog, 1827 struct bpf_tracing_multi_link *link) 1828 { 1829 struct bpf_tracing_multi_data *data = &link->data; 1830 struct bpf_tracing_multi_node *mnode; 1831 int i, err; 1832 1833 trampoline_lock_all(); 1834 1835 for_each_mnode(mnode, link) { 1836 data->entry = &mnode->entry; 1837 bpf_trampoline_multi_attach_init(mnode->trampoline); 1838 err = __bpf_trampoline_unlink_prog(&mnode->node, mnode->trampoline, NULL, 1839 &trampoline_multi_ops, data); 1840 WARN_ONCE(err, "__bpf_trampoline_unlink_prog failed: %d\n", err); 1841 } 1842 1843 if (ftrace_hash_count(data->unreg)) 1844 WARN_ON_ONCE(update_ftrace_direct_del(&direct_ops, data->unreg)); 1845 if (ftrace_hash_count(data->modify)) 1846 WARN_ON_ONCE(update_ftrace_direct_mod(&direct_ops, data->modify, true)); 1847 1848 for_each_mnode(mnode, link) 1849 bpf_trampoline_multi_attach_free(mnode->trampoline); 1850 1851 trampoline_unlock_all(); 1852 1853 for_each_mnode(mnode, link) 1854 bpf_trampoline_put(mnode->trampoline); 1855 1856 clear_tracing_multi_data(data); 1857 } 1858 1859 #undef for_each_mnode_cnt 1860 #undef for_each_mnode 1861 1862 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS && 1863 CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS && 1864 CONFIG_BPF_SYSCALL */ 1865 1866 static int __init init_trampolines(void) 1867 { 1868 int i; 1869 1870 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1871 INIT_HLIST_HEAD(&trampoline_key_table[i]); 1872 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1873 INIT_HLIST_HEAD(&trampoline_ip_table[i]); 1874 for (i = 0; i < TRAMPOLINE_LOCKS_TABLE_SIZE; i++) 1875 __mutex_init(&trampoline_locks[i].mutex, "trampoline_lock", &trampoline_locks[i].key); 1876 return 0; 1877 } 1878 late_initcall(init_trampolines); 1879