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/module.h> 13 #include <linux/static_call.h> 14 15 /* dummy _ops. The verifier will operate on target program's ops. */ 16 const struct bpf_verifier_ops bpf_extension_verifier_ops = { 17 }; 18 const struct bpf_prog_ops bpf_extension_prog_ops = { 19 }; 20 21 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */ 22 #define TRAMPOLINE_HASH_BITS 10 23 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS) 24 25 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE]; 26 27 /* serializes access to trampoline_table */ 28 static DEFINE_MUTEX(trampoline_mutex); 29 30 void *bpf_jit_alloc_exec_page(void) 31 { 32 void *image; 33 34 image = bpf_jit_alloc_exec(PAGE_SIZE); 35 if (!image) 36 return NULL; 37 38 set_vm_flush_reset_perms(image); 39 /* Keep image as writeable. The alternative is to keep flipping ro/rw 40 * everytime new program is attached or detached. 41 */ 42 set_memory_x((long)image, 1); 43 return image; 44 } 45 46 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym) 47 { 48 ksym->start = (unsigned long) data; 49 ksym->end = ksym->start + PAGE_SIZE; 50 bpf_ksym_add(ksym); 51 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 52 PAGE_SIZE, false, ksym->name); 53 } 54 55 void bpf_image_ksym_del(struct bpf_ksym *ksym) 56 { 57 bpf_ksym_del(ksym); 58 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 59 PAGE_SIZE, true, ksym->name); 60 } 61 62 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key) 63 { 64 struct bpf_trampoline *tr; 65 struct hlist_head *head; 66 int i; 67 68 mutex_lock(&trampoline_mutex); 69 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)]; 70 hlist_for_each_entry(tr, head, hlist) { 71 if (tr->key == key) { 72 refcount_inc(&tr->refcnt); 73 goto out; 74 } 75 } 76 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 77 if (!tr) 78 goto out; 79 80 tr->key = key; 81 INIT_HLIST_NODE(&tr->hlist); 82 hlist_add_head(&tr->hlist, head); 83 refcount_set(&tr->refcnt, 1); 84 mutex_init(&tr->mutex); 85 for (i = 0; i < BPF_TRAMP_MAX; i++) 86 INIT_HLIST_HEAD(&tr->progs_hlist[i]); 87 out: 88 mutex_unlock(&trampoline_mutex); 89 return tr; 90 } 91 92 static int bpf_trampoline_module_get(struct bpf_trampoline *tr) 93 { 94 struct module *mod; 95 int err = 0; 96 97 preempt_disable(); 98 mod = __module_text_address((unsigned long) tr->func.addr); 99 if (mod && !try_module_get(mod)) 100 err = -ENOENT; 101 preempt_enable(); 102 tr->mod = mod; 103 return err; 104 } 105 106 static void bpf_trampoline_module_put(struct bpf_trampoline *tr) 107 { 108 module_put(tr->mod); 109 tr->mod = NULL; 110 } 111 112 static int is_ftrace_location(void *ip) 113 { 114 long addr; 115 116 addr = ftrace_location((long)ip); 117 if (!addr) 118 return 0; 119 if (WARN_ON_ONCE(addr != (long)ip)) 120 return -EFAULT; 121 return 1; 122 } 123 124 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr) 125 { 126 void *ip = tr->func.addr; 127 int ret; 128 129 if (tr->func.ftrace_managed) 130 ret = unregister_ftrace_direct((long)ip, (long)old_addr); 131 else 132 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL); 133 134 if (!ret) 135 bpf_trampoline_module_put(tr); 136 return ret; 137 } 138 139 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr) 140 { 141 void *ip = tr->func.addr; 142 int ret; 143 144 if (tr->func.ftrace_managed) 145 ret = modify_ftrace_direct((long)ip, (long)old_addr, (long)new_addr); 146 else 147 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr); 148 return ret; 149 } 150 151 /* first time registering */ 152 static int register_fentry(struct bpf_trampoline *tr, void *new_addr) 153 { 154 void *ip = tr->func.addr; 155 int ret; 156 157 ret = is_ftrace_location(ip); 158 if (ret < 0) 159 return ret; 160 tr->func.ftrace_managed = ret; 161 162 if (bpf_trampoline_module_get(tr)) 163 return -ENOENT; 164 165 if (tr->func.ftrace_managed) 166 ret = register_ftrace_direct((long)ip, (long)new_addr); 167 else 168 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr); 169 170 if (ret) 171 bpf_trampoline_module_put(tr); 172 return ret; 173 } 174 175 static struct bpf_tramp_progs * 176 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) 177 { 178 const struct bpf_prog_aux *aux; 179 struct bpf_tramp_progs *tprogs; 180 struct bpf_prog **progs; 181 int kind; 182 183 *total = 0; 184 tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL); 185 if (!tprogs) 186 return ERR_PTR(-ENOMEM); 187 188 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 189 tprogs[kind].nr_progs = tr->progs_cnt[kind]; 190 *total += tr->progs_cnt[kind]; 191 progs = tprogs[kind].progs; 192 193 hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) { 194 *ip_arg |= aux->prog->call_get_func_ip; 195 *progs++ = aux->prog; 196 } 197 } 198 return tprogs; 199 } 200 201 static void __bpf_tramp_image_put_deferred(struct work_struct *work) 202 { 203 struct bpf_tramp_image *im; 204 205 im = container_of(work, struct bpf_tramp_image, work); 206 bpf_image_ksym_del(&im->ksym); 207 bpf_jit_free_exec(im->image); 208 bpf_jit_uncharge_modmem(1); 209 percpu_ref_exit(&im->pcref); 210 kfree_rcu(im, rcu); 211 } 212 213 /* callback, fexit step 3 or fentry step 2 */ 214 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) 215 { 216 struct bpf_tramp_image *im; 217 218 im = container_of(rcu, struct bpf_tramp_image, rcu); 219 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred); 220 schedule_work(&im->work); 221 } 222 223 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */ 224 static void __bpf_tramp_image_release(struct percpu_ref *pcref) 225 { 226 struct bpf_tramp_image *im; 227 228 im = container_of(pcref, struct bpf_tramp_image, pcref); 229 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 230 } 231 232 /* callback, fexit or fentry step 1 */ 233 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu) 234 { 235 struct bpf_tramp_image *im; 236 237 im = container_of(rcu, struct bpf_tramp_image, rcu); 238 if (im->ip_after_call) 239 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */ 240 percpu_ref_kill(&im->pcref); 241 else 242 /* the case of fentry trampoline */ 243 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 244 } 245 246 static void bpf_tramp_image_put(struct bpf_tramp_image *im) 247 { 248 /* The trampoline image that calls original function is using: 249 * rcu_read_lock_trace to protect sleepable bpf progs 250 * rcu_read_lock to protect normal bpf progs 251 * percpu_ref to protect trampoline itself 252 * rcu tasks to protect trampoline asm not covered by percpu_ref 253 * (which are few asm insns before __bpf_tramp_enter and 254 * after __bpf_tramp_exit) 255 * 256 * The trampoline is unreachable before bpf_tramp_image_put(). 257 * 258 * First, patch the trampoline to avoid calling into fexit progs. 259 * The progs will be freed even if the original function is still 260 * executing or sleeping. 261 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on 262 * first few asm instructions to execute and call into 263 * __bpf_tramp_enter->percpu_ref_get. 264 * Then use percpu_ref_kill to wait for the trampoline and the original 265 * function to finish. 266 * Then use call_rcu_tasks() to make sure few asm insns in 267 * the trampoline epilogue are done as well. 268 * 269 * In !PREEMPT case the task that got interrupted in the first asm 270 * insns won't go through an RCU quiescent state which the 271 * percpu_ref_kill will be waiting for. Hence the first 272 * call_rcu_tasks() is not necessary. 273 */ 274 if (im->ip_after_call) { 275 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP, 276 NULL, im->ip_epilogue); 277 WARN_ON(err); 278 if (IS_ENABLED(CONFIG_PREEMPTION)) 279 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 280 else 281 percpu_ref_kill(&im->pcref); 282 return; 283 } 284 285 /* The trampoline without fexit and fmod_ret progs doesn't call original 286 * function and doesn't use percpu_ref. 287 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. 288 * Then use call_rcu_tasks() to wait for the rest of trampoline asm 289 * and normal progs. 290 */ 291 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 292 } 293 294 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx) 295 { 296 struct bpf_tramp_image *im; 297 struct bpf_ksym *ksym; 298 void *image; 299 int err = -ENOMEM; 300 301 im = kzalloc(sizeof(*im), GFP_KERNEL); 302 if (!im) 303 goto out; 304 305 err = bpf_jit_charge_modmem(1); 306 if (err) 307 goto out_free_im; 308 309 err = -ENOMEM; 310 im->image = image = bpf_jit_alloc_exec_page(); 311 if (!image) 312 goto out_uncharge; 313 314 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL); 315 if (err) 316 goto out_free_image; 317 318 ksym = &im->ksym; 319 INIT_LIST_HEAD_RCU(&ksym->lnode); 320 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx); 321 bpf_image_ksym_add(image, ksym); 322 return im; 323 324 out_free_image: 325 bpf_jit_free_exec(im->image); 326 out_uncharge: 327 bpf_jit_uncharge_modmem(1); 328 out_free_im: 329 kfree(im); 330 out: 331 return ERR_PTR(err); 332 } 333 334 static int bpf_trampoline_update(struct bpf_trampoline *tr) 335 { 336 struct bpf_tramp_image *im; 337 struct bpf_tramp_progs *tprogs; 338 u32 flags = BPF_TRAMP_F_RESTORE_REGS; 339 bool ip_arg = false; 340 int err, total; 341 342 tprogs = bpf_trampoline_get_progs(tr, &total, &ip_arg); 343 if (IS_ERR(tprogs)) 344 return PTR_ERR(tprogs); 345 346 if (total == 0) { 347 err = unregister_fentry(tr, tr->cur_image->image); 348 bpf_tramp_image_put(tr->cur_image); 349 tr->cur_image = NULL; 350 tr->selector = 0; 351 goto out; 352 } 353 354 im = bpf_tramp_image_alloc(tr->key, tr->selector); 355 if (IS_ERR(im)) { 356 err = PTR_ERR(im); 357 goto out; 358 } 359 360 if (tprogs[BPF_TRAMP_FEXIT].nr_progs || 361 tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs) 362 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 363 364 if (ip_arg) 365 flags |= BPF_TRAMP_F_IP_ARG; 366 367 err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE, 368 &tr->func.model, flags, tprogs, 369 tr->func.addr); 370 if (err < 0) 371 goto out; 372 373 WARN_ON(tr->cur_image && tr->selector == 0); 374 WARN_ON(!tr->cur_image && tr->selector); 375 if (tr->cur_image) 376 /* progs already running at this address */ 377 err = modify_fentry(tr, tr->cur_image->image, im->image); 378 else 379 /* first time registering */ 380 err = register_fentry(tr, im->image); 381 if (err) 382 goto out; 383 if (tr->cur_image) 384 bpf_tramp_image_put(tr->cur_image); 385 tr->cur_image = im; 386 tr->selector++; 387 out: 388 kfree(tprogs); 389 return err; 390 } 391 392 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) 393 { 394 switch (prog->expected_attach_type) { 395 case BPF_TRACE_FENTRY: 396 return BPF_TRAMP_FENTRY; 397 case BPF_MODIFY_RETURN: 398 return BPF_TRAMP_MODIFY_RETURN; 399 case BPF_TRACE_FEXIT: 400 return BPF_TRAMP_FEXIT; 401 case BPF_LSM_MAC: 402 if (!prog->aux->attach_func_proto->type) 403 /* The function returns void, we cannot modify its 404 * return value. 405 */ 406 return BPF_TRAMP_FEXIT; 407 else 408 return BPF_TRAMP_MODIFY_RETURN; 409 default: 410 return BPF_TRAMP_REPLACE; 411 } 412 } 413 414 int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr) 415 { 416 enum bpf_tramp_prog_type kind; 417 int err = 0; 418 int cnt; 419 420 kind = bpf_attach_type_to_tramp(prog); 421 mutex_lock(&tr->mutex); 422 if (tr->extension_prog) { 423 /* cannot attach fentry/fexit if extension prog is attached. 424 * cannot overwrite extension prog either. 425 */ 426 err = -EBUSY; 427 goto out; 428 } 429 cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT]; 430 if (kind == BPF_TRAMP_REPLACE) { 431 /* Cannot attach extension if fentry/fexit are in use. */ 432 if (cnt) { 433 err = -EBUSY; 434 goto out; 435 } 436 tr->extension_prog = prog; 437 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL, 438 prog->bpf_func); 439 goto out; 440 } 441 if (cnt >= BPF_MAX_TRAMP_PROGS) { 442 err = -E2BIG; 443 goto out; 444 } 445 if (!hlist_unhashed(&prog->aux->tramp_hlist)) { 446 /* prog already linked */ 447 err = -EBUSY; 448 goto out; 449 } 450 hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]); 451 tr->progs_cnt[kind]++; 452 err = bpf_trampoline_update(tr); 453 if (err) { 454 hlist_del_init(&prog->aux->tramp_hlist); 455 tr->progs_cnt[kind]--; 456 } 457 out: 458 mutex_unlock(&tr->mutex); 459 return err; 460 } 461 462 /* bpf_trampoline_unlink_prog() should never fail. */ 463 int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr) 464 { 465 enum bpf_tramp_prog_type kind; 466 int err; 467 468 kind = bpf_attach_type_to_tramp(prog); 469 mutex_lock(&tr->mutex); 470 if (kind == BPF_TRAMP_REPLACE) { 471 WARN_ON_ONCE(!tr->extension_prog); 472 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, 473 tr->extension_prog->bpf_func, NULL); 474 tr->extension_prog = NULL; 475 goto out; 476 } 477 hlist_del_init(&prog->aux->tramp_hlist); 478 tr->progs_cnt[kind]--; 479 err = bpf_trampoline_update(tr); 480 out: 481 mutex_unlock(&tr->mutex); 482 return err; 483 } 484 485 struct bpf_trampoline *bpf_trampoline_get(u64 key, 486 struct bpf_attach_target_info *tgt_info) 487 { 488 struct bpf_trampoline *tr; 489 490 tr = bpf_trampoline_lookup(key); 491 if (!tr) 492 return NULL; 493 494 mutex_lock(&tr->mutex); 495 if (tr->func.addr) 496 goto out; 497 498 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); 499 tr->func.addr = (void *)tgt_info->tgt_addr; 500 out: 501 mutex_unlock(&tr->mutex); 502 return tr; 503 } 504 505 void bpf_trampoline_put(struct bpf_trampoline *tr) 506 { 507 if (!tr) 508 return; 509 mutex_lock(&trampoline_mutex); 510 if (!refcount_dec_and_test(&tr->refcnt)) 511 goto out; 512 WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); 513 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY]))) 514 goto out; 515 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT]))) 516 goto out; 517 /* This code will be executed even when the last bpf_tramp_image 518 * is alive. All progs are detached from the trampoline and the 519 * trampoline image is patched with jmp into epilogue to skip 520 * fexit progs. The fentry-only trampoline will be freed via 521 * multiple rcu callbacks. 522 */ 523 hlist_del(&tr->hlist); 524 kfree(tr); 525 out: 526 mutex_unlock(&trampoline_mutex); 527 } 528 529 #define NO_START_TIME 1 530 static __always_inline u64 notrace bpf_prog_start_time(void) 531 { 532 u64 start = NO_START_TIME; 533 534 if (static_branch_unlikely(&bpf_stats_enabled_key)) { 535 start = sched_clock(); 536 if (unlikely(!start)) 537 start = NO_START_TIME; 538 } 539 return start; 540 } 541 542 static void notrace inc_misses_counter(struct bpf_prog *prog) 543 { 544 struct bpf_prog_stats *stats; 545 546 stats = this_cpu_ptr(prog->stats); 547 u64_stats_update_begin(&stats->syncp); 548 u64_stats_inc(&stats->misses); 549 u64_stats_update_end(&stats->syncp); 550 } 551 552 /* The logic is similar to bpf_prog_run(), but with an explicit 553 * rcu_read_lock() and migrate_disable() which are required 554 * for the trampoline. The macro is split into 555 * call __bpf_prog_enter 556 * call prog->bpf_func 557 * call __bpf_prog_exit 558 * 559 * __bpf_prog_enter returns: 560 * 0 - skip execution of the bpf prog 561 * 1 - execute bpf prog 562 * [2..MAX_U64] - execute bpf prog and record execution time. 563 * This is start time. 564 */ 565 u64 notrace __bpf_prog_enter(struct bpf_prog *prog) 566 __acquires(RCU) 567 { 568 rcu_read_lock(); 569 migrate_disable(); 570 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) { 571 inc_misses_counter(prog); 572 return 0; 573 } 574 return bpf_prog_start_time(); 575 } 576 577 static void notrace update_prog_stats(struct bpf_prog *prog, 578 u64 start) 579 { 580 struct bpf_prog_stats *stats; 581 582 if (static_branch_unlikely(&bpf_stats_enabled_key) && 583 /* static_key could be enabled in __bpf_prog_enter* 584 * and disabled in __bpf_prog_exit*. 585 * And vice versa. 586 * Hence check that 'start' is valid. 587 */ 588 start > NO_START_TIME) { 589 unsigned long flags; 590 591 stats = this_cpu_ptr(prog->stats); 592 flags = u64_stats_update_begin_irqsave(&stats->syncp); 593 u64_stats_inc(&stats->cnt); 594 u64_stats_add(&stats->nsecs, sched_clock() - start); 595 u64_stats_update_end_irqrestore(&stats->syncp, flags); 596 } 597 } 598 599 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) 600 __releases(RCU) 601 { 602 update_prog_stats(prog, start); 603 __this_cpu_dec(*(prog->active)); 604 migrate_enable(); 605 rcu_read_unlock(); 606 } 607 608 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog) 609 { 610 rcu_read_lock_trace(); 611 migrate_disable(); 612 might_fault(); 613 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) { 614 inc_misses_counter(prog); 615 return 0; 616 } 617 return bpf_prog_start_time(); 618 } 619 620 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start) 621 { 622 update_prog_stats(prog, start); 623 __this_cpu_dec(*(prog->active)); 624 migrate_enable(); 625 rcu_read_unlock_trace(); 626 } 627 628 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr) 629 { 630 percpu_ref_get(&tr->pcref); 631 } 632 633 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr) 634 { 635 percpu_ref_put(&tr->pcref); 636 } 637 638 int __weak 639 arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end, 640 const struct btf_func_model *m, u32 flags, 641 struct bpf_tramp_progs *tprogs, 642 void *orig_call) 643 { 644 return -ENOTSUPP; 645 } 646 647 static int __init init_trampolines(void) 648 { 649 int i; 650 651 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 652 INIT_HLIST_HEAD(&trampoline_table[i]); 653 return 0; 654 } 655 late_initcall(init_trampolines); 656