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_table[TRAMPOLINE_TABLE_SIZE]; 28 29 /* serializes access to trampoline_table */ 30 static DEFINE_MUTEX(trampoline_mutex); 31 32 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 33 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex); 34 35 static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, enum ftrace_ops_cmd cmd) 36 { 37 struct bpf_trampoline *tr = ops->private; 38 int ret = 0; 39 40 if (cmd == FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF) { 41 /* This is called inside register_ftrace_direct_multi(), so 42 * tr->mutex is already locked. 43 */ 44 lockdep_assert_held_once(&tr->mutex); 45 46 /* Instead of updating the trampoline here, we propagate 47 * -EAGAIN to register_ftrace_direct(). Then we can 48 * retry register_ftrace_direct() after updating the 49 * trampoline. 50 */ 51 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && 52 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) { 53 if (WARN_ON_ONCE(tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY)) 54 return -EBUSY; 55 56 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; 57 return -EAGAIN; 58 } 59 60 return 0; 61 } 62 63 /* The normal locking order is 64 * tr->mutex => direct_mutex (ftrace.c) => ftrace_lock (ftrace.c) 65 * 66 * The following two commands are called from 67 * 68 * prepare_direct_functions_for_ipmodify 69 * cleanup_direct_functions_after_ipmodify 70 * 71 * In both cases, direct_mutex is already locked. Use 72 * mutex_trylock(&tr->mutex) to avoid deadlock in race condition 73 * (something else is making changes to this same trampoline). 74 */ 75 if (!mutex_trylock(&tr->mutex)) { 76 /* sleep 1 ms to make sure whatever holding tr->mutex makes 77 * some progress. 78 */ 79 msleep(1); 80 return -EAGAIN; 81 } 82 83 switch (cmd) { 84 case FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER: 85 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; 86 87 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && 88 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) 89 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */); 90 break; 91 case FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER: 92 tr->flags &= ~BPF_TRAMP_F_SHARE_IPMODIFY; 93 94 if (tr->flags & BPF_TRAMP_F_ORIG_STACK) 95 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */); 96 break; 97 default: 98 ret = -EINVAL; 99 break; 100 } 101 102 mutex_unlock(&tr->mutex); 103 return ret; 104 } 105 #endif 106 107 bool bpf_prog_has_trampoline(const struct bpf_prog *prog) 108 { 109 enum bpf_attach_type eatype = prog->expected_attach_type; 110 enum bpf_prog_type ptype = prog->type; 111 112 return (ptype == BPF_PROG_TYPE_TRACING && 113 (eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT || 114 eatype == BPF_MODIFY_RETURN)) || 115 (ptype == BPF_PROG_TYPE_LSM && eatype == BPF_LSM_MAC); 116 } 117 118 void bpf_image_ksym_init(void *data, unsigned int size, struct bpf_ksym *ksym) 119 { 120 ksym->start = (unsigned long) data; 121 ksym->end = ksym->start + size; 122 } 123 124 void bpf_image_ksym_add(struct bpf_ksym *ksym) 125 { 126 bpf_ksym_add(ksym); 127 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 128 PAGE_SIZE, false, ksym->name); 129 } 130 131 void bpf_image_ksym_del(struct bpf_ksym *ksym) 132 { 133 bpf_ksym_del(ksym); 134 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 135 PAGE_SIZE, true, ksym->name); 136 } 137 138 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key) 139 { 140 struct bpf_trampoline *tr; 141 struct hlist_head *head; 142 int i; 143 144 mutex_lock(&trampoline_mutex); 145 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)]; 146 hlist_for_each_entry(tr, head, hlist) { 147 if (tr->key == key) { 148 refcount_inc(&tr->refcnt); 149 goto out; 150 } 151 } 152 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 153 if (!tr) 154 goto out; 155 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 156 tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL); 157 if (!tr->fops) { 158 kfree(tr); 159 tr = NULL; 160 goto out; 161 } 162 tr->fops->private = tr; 163 tr->fops->ops_func = bpf_tramp_ftrace_ops_func; 164 #endif 165 166 tr->key = key; 167 INIT_HLIST_NODE(&tr->hlist); 168 hlist_add_head(&tr->hlist, head); 169 refcount_set(&tr->refcnt, 1); 170 mutex_init(&tr->mutex); 171 for (i = 0; i < BPF_TRAMP_MAX; i++) 172 INIT_HLIST_HEAD(&tr->progs_hlist[i]); 173 out: 174 mutex_unlock(&trampoline_mutex); 175 return tr; 176 } 177 178 static int bpf_trampoline_update_fentry(struct bpf_trampoline *tr, u32 orig_flags, 179 void *old_addr, void *new_addr) 180 { 181 enum bpf_text_poke_type new_t = BPF_MOD_CALL, old_t = BPF_MOD_CALL; 182 void *ip = tr->func.addr; 183 184 if (!new_addr) 185 new_t = BPF_MOD_NOP; 186 else if (bpf_trampoline_use_jmp(tr->flags)) 187 new_t = BPF_MOD_JUMP; 188 189 if (!old_addr) 190 old_t = BPF_MOD_NOP; 191 else if (bpf_trampoline_use_jmp(orig_flags)) 192 old_t = BPF_MOD_JUMP; 193 194 return bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr); 195 } 196 197 static int unregister_fentry(struct bpf_trampoline *tr, u32 orig_flags, 198 void *old_addr) 199 { 200 int ret; 201 202 if (tr->func.ftrace_managed) 203 ret = unregister_ftrace_direct(tr->fops, (long)old_addr, false); 204 else 205 ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, NULL); 206 207 return ret; 208 } 209 210 static int modify_fentry(struct bpf_trampoline *tr, u32 orig_flags, 211 void *old_addr, void *new_addr, 212 bool lock_direct_mutex) 213 { 214 int ret; 215 216 if (tr->func.ftrace_managed) { 217 if (lock_direct_mutex) 218 ret = modify_ftrace_direct(tr->fops, (long)new_addr); 219 else 220 ret = modify_ftrace_direct_nolock(tr->fops, (long)new_addr); 221 } else { 222 ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, 223 new_addr); 224 } 225 return ret; 226 } 227 228 /* first time registering */ 229 static int register_fentry(struct bpf_trampoline *tr, void *new_addr) 230 { 231 void *ip = tr->func.addr; 232 unsigned long faddr; 233 int ret; 234 235 faddr = ftrace_location((unsigned long)ip); 236 if (faddr) { 237 if (!tr->fops) 238 return -ENOTSUPP; 239 tr->func.ftrace_managed = true; 240 } 241 242 if (tr->func.ftrace_managed) { 243 ret = ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1); 244 if (ret) 245 return ret; 246 ret = register_ftrace_direct(tr->fops, (long)new_addr); 247 } else { 248 ret = bpf_trampoline_update_fentry(tr, 0, NULL, new_addr); 249 } 250 251 return ret; 252 } 253 254 static struct bpf_tramp_links * 255 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) 256 { 257 struct bpf_tramp_link *link; 258 struct bpf_tramp_links *tlinks; 259 struct bpf_tramp_link **links; 260 int kind; 261 262 *total = 0; 263 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL); 264 if (!tlinks) 265 return ERR_PTR(-ENOMEM); 266 267 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 268 tlinks[kind].nr_links = tr->progs_cnt[kind]; 269 *total += tr->progs_cnt[kind]; 270 links = tlinks[kind].links; 271 272 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) { 273 *ip_arg |= link->link.prog->call_get_func_ip; 274 *links++ = link; 275 } 276 } 277 return tlinks; 278 } 279 280 static void bpf_tramp_image_free(struct bpf_tramp_image *im) 281 { 282 bpf_image_ksym_del(&im->ksym); 283 arch_free_bpf_trampoline(im->image, im->size); 284 bpf_jit_uncharge_modmem(im->size); 285 percpu_ref_exit(&im->pcref); 286 kfree_rcu(im, rcu); 287 } 288 289 static void __bpf_tramp_image_put_deferred(struct work_struct *work) 290 { 291 struct bpf_tramp_image *im; 292 293 im = container_of(work, struct bpf_tramp_image, work); 294 bpf_tramp_image_free(im); 295 } 296 297 /* callback, fexit step 3 or fentry step 2 */ 298 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) 299 { 300 struct bpf_tramp_image *im; 301 302 im = container_of(rcu, struct bpf_tramp_image, rcu); 303 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred); 304 schedule_work(&im->work); 305 } 306 307 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */ 308 static void __bpf_tramp_image_release(struct percpu_ref *pcref) 309 { 310 struct bpf_tramp_image *im; 311 312 im = container_of(pcref, struct bpf_tramp_image, pcref); 313 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 314 } 315 316 /* callback, fexit or fentry step 1 */ 317 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu) 318 { 319 struct bpf_tramp_image *im; 320 321 im = container_of(rcu, struct bpf_tramp_image, rcu); 322 if (im->ip_after_call) 323 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */ 324 percpu_ref_kill(&im->pcref); 325 else 326 /* the case of fentry trampoline */ 327 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 328 } 329 330 static void bpf_tramp_image_put(struct bpf_tramp_image *im) 331 { 332 /* The trampoline image that calls original function is using: 333 * rcu_read_lock_trace to protect sleepable bpf progs 334 * rcu_read_lock to protect normal bpf progs 335 * percpu_ref to protect trampoline itself 336 * rcu tasks to protect trampoline asm not covered by percpu_ref 337 * (which are few asm insns before __bpf_tramp_enter and 338 * after __bpf_tramp_exit) 339 * 340 * The trampoline is unreachable before bpf_tramp_image_put(). 341 * 342 * First, patch the trampoline to avoid calling into fexit progs. 343 * The progs will be freed even if the original function is still 344 * executing or sleeping. 345 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on 346 * first few asm instructions to execute and call into 347 * __bpf_tramp_enter->percpu_ref_get. 348 * Then use percpu_ref_kill to wait for the trampoline and the original 349 * function to finish. 350 * Then use call_rcu_tasks() to make sure few asm insns in 351 * the trampoline epilogue are done as well. 352 * 353 * In !PREEMPT case the task that got interrupted in the first asm 354 * insns won't go through an RCU quiescent state which the 355 * percpu_ref_kill will be waiting for. Hence the first 356 * call_rcu_tasks() is not necessary. 357 */ 358 if (im->ip_after_call) { 359 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_NOP, 360 BPF_MOD_JUMP, NULL, 361 im->ip_epilogue); 362 WARN_ON(err); 363 if (IS_ENABLED(CONFIG_TASKS_RCU)) 364 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 365 else 366 percpu_ref_kill(&im->pcref); 367 return; 368 } 369 370 /* The trampoline without fexit and fmod_ret progs doesn't call original 371 * function and doesn't use percpu_ref. 372 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. 373 * Then use call_rcu_tasks() to wait for the rest of trampoline asm 374 * and normal progs. 375 */ 376 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 377 } 378 379 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size) 380 { 381 struct bpf_tramp_image *im; 382 struct bpf_ksym *ksym; 383 void *image; 384 int err = -ENOMEM; 385 386 im = kzalloc(sizeof(*im), GFP_KERNEL); 387 if (!im) 388 goto out; 389 390 err = bpf_jit_charge_modmem(size); 391 if (err) 392 goto out_free_im; 393 im->size = size; 394 395 err = -ENOMEM; 396 im->image = image = arch_alloc_bpf_trampoline(size); 397 if (!image) 398 goto out_uncharge; 399 400 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL); 401 if (err) 402 goto out_free_image; 403 404 ksym = &im->ksym; 405 INIT_LIST_HEAD_RCU(&ksym->lnode); 406 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key); 407 bpf_image_ksym_init(image, size, ksym); 408 bpf_image_ksym_add(ksym); 409 return im; 410 411 out_free_image: 412 arch_free_bpf_trampoline(im->image, im->size); 413 out_uncharge: 414 bpf_jit_uncharge_modmem(size); 415 out_free_im: 416 kfree(im); 417 out: 418 return ERR_PTR(err); 419 } 420 421 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex) 422 { 423 struct bpf_tramp_image *im; 424 struct bpf_tramp_links *tlinks; 425 u32 orig_flags = tr->flags; 426 bool ip_arg = false; 427 int err, total, size; 428 429 tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg); 430 if (IS_ERR(tlinks)) 431 return PTR_ERR(tlinks); 432 433 if (total == 0) { 434 err = unregister_fentry(tr, orig_flags, tr->cur_image->image); 435 bpf_tramp_image_put(tr->cur_image); 436 tr->cur_image = NULL; 437 goto out; 438 } 439 440 /* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */ 441 tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX); 442 443 if (tlinks[BPF_TRAMP_FEXIT].nr_links || 444 tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links) { 445 /* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME 446 * should not be set together. 447 */ 448 tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 449 } else { 450 tr->flags |= BPF_TRAMP_F_RESTORE_REGS; 451 } 452 453 if (ip_arg) 454 tr->flags |= BPF_TRAMP_F_IP_ARG; 455 456 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 457 again: 458 if (tr->flags & BPF_TRAMP_F_CALL_ORIG) { 459 if (tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) { 460 /* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the 461 * first try, reset it in the second try. 462 */ 463 tr->flags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME; 464 } else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) { 465 /* Use "jmp" instead of "call" for the trampoline 466 * in the origin call case, and we don't need to 467 * skip the frame. 468 */ 469 tr->flags &= ~BPF_TRAMP_F_SKIP_FRAME; 470 } 471 } 472 #endif 473 474 size = arch_bpf_trampoline_size(&tr->func.model, tr->flags, 475 tlinks, tr->func.addr); 476 if (size < 0) { 477 err = size; 478 goto out; 479 } 480 481 if (size > PAGE_SIZE) { 482 err = -E2BIG; 483 goto out; 484 } 485 486 im = bpf_tramp_image_alloc(tr->key, size); 487 if (IS_ERR(im)) { 488 err = PTR_ERR(im); 489 goto out; 490 } 491 492 err = arch_prepare_bpf_trampoline(im, im->image, im->image + size, 493 &tr->func.model, tr->flags, tlinks, 494 tr->func.addr); 495 if (err < 0) 496 goto out_free; 497 498 err = arch_protect_bpf_trampoline(im->image, im->size); 499 if (err) 500 goto out_free; 501 502 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP 503 if (bpf_trampoline_use_jmp(tr->flags)) 504 tr->fops->flags |= FTRACE_OPS_FL_JMP; 505 else 506 tr->fops->flags &= ~FTRACE_OPS_FL_JMP; 507 #endif 508 509 WARN_ON(tr->cur_image && total == 0); 510 if (tr->cur_image) 511 /* progs already running at this address */ 512 err = modify_fentry(tr, orig_flags, tr->cur_image->image, 513 im->image, lock_direct_mutex); 514 else 515 /* first time registering */ 516 err = register_fentry(tr, im->image); 517 518 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 519 if (err == -EAGAIN) { 520 /* -EAGAIN from bpf_tramp_ftrace_ops_func. Now 521 * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the 522 * trampoline again, and retry register. 523 */ 524 bpf_tramp_image_free(im); 525 goto again; 526 } 527 #endif 528 if (err) 529 goto out_free; 530 531 if (tr->cur_image) 532 bpf_tramp_image_put(tr->cur_image); 533 tr->cur_image = im; 534 out: 535 /* If any error happens, restore previous flags */ 536 if (err) { 537 tr->flags = orig_flags; 538 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP 539 if (bpf_trampoline_use_jmp(tr->flags)) 540 tr->fops->flags |= FTRACE_OPS_FL_JMP; 541 else 542 tr->fops->flags &= ~FTRACE_OPS_FL_JMP; 543 #endif 544 } 545 kfree(tlinks); 546 return err; 547 548 out_free: 549 bpf_tramp_image_free(im); 550 goto out; 551 } 552 553 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) 554 { 555 switch (prog->expected_attach_type) { 556 case BPF_TRACE_FENTRY: 557 return BPF_TRAMP_FENTRY; 558 case BPF_MODIFY_RETURN: 559 return BPF_TRAMP_MODIFY_RETURN; 560 case BPF_TRACE_FEXIT: 561 return BPF_TRAMP_FEXIT; 562 case BPF_LSM_MAC: 563 if (!prog->aux->attach_func_proto->type) 564 /* The function returns void, we cannot modify its 565 * return value. 566 */ 567 return BPF_TRAMP_FEXIT; 568 else 569 return BPF_TRAMP_MODIFY_RETURN; 570 default: 571 return BPF_TRAMP_REPLACE; 572 } 573 } 574 575 static int bpf_freplace_check_tgt_prog(struct bpf_prog *tgt_prog) 576 { 577 struct bpf_prog_aux *aux = tgt_prog->aux; 578 579 guard(mutex)(&aux->ext_mutex); 580 if (aux->prog_array_member_cnt) 581 /* Program extensions can not extend target prog when the target 582 * prog has been updated to any prog_array map as tail callee. 583 * It's to prevent a potential infinite loop like: 584 * tgt prog entry -> tgt prog subprog -> freplace prog entry 585 * --tailcall-> tgt prog entry. 586 */ 587 return -EBUSY; 588 589 aux->is_extended = true; 590 return 0; 591 } 592 593 static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, 594 struct bpf_trampoline *tr, 595 struct bpf_prog *tgt_prog) 596 { 597 enum bpf_tramp_prog_type kind; 598 struct bpf_tramp_link *link_exiting; 599 int err = 0; 600 int cnt = 0, i; 601 602 kind = bpf_attach_type_to_tramp(link->link.prog); 603 if (tr->extension_prog) 604 /* cannot attach fentry/fexit if extension prog is attached. 605 * cannot overwrite extension prog either. 606 */ 607 return -EBUSY; 608 609 for (i = 0; i < BPF_TRAMP_MAX; i++) 610 cnt += tr->progs_cnt[i]; 611 612 if (kind == BPF_TRAMP_REPLACE) { 613 /* Cannot attach extension if fentry/fexit are in use. */ 614 if (cnt) 615 return -EBUSY; 616 err = bpf_freplace_check_tgt_prog(tgt_prog); 617 if (err) 618 return err; 619 tr->extension_prog = link->link.prog; 620 return bpf_arch_text_poke(tr->func.addr, BPF_MOD_NOP, 621 BPF_MOD_JUMP, NULL, 622 link->link.prog->bpf_func); 623 } 624 if (cnt >= BPF_MAX_TRAMP_LINKS) 625 return -E2BIG; 626 if (!hlist_unhashed(&link->tramp_hlist)) 627 /* prog already linked */ 628 return -EBUSY; 629 hlist_for_each_entry(link_exiting, &tr->progs_hlist[kind], tramp_hlist) { 630 if (link_exiting->link.prog != link->link.prog) 631 continue; 632 /* prog already linked */ 633 return -EBUSY; 634 } 635 636 hlist_add_head(&link->tramp_hlist, &tr->progs_hlist[kind]); 637 tr->progs_cnt[kind]++; 638 err = bpf_trampoline_update(tr, true /* lock_direct_mutex */); 639 if (err) { 640 hlist_del_init(&link->tramp_hlist); 641 tr->progs_cnt[kind]--; 642 } 643 return err; 644 } 645 646 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, 647 struct bpf_trampoline *tr, 648 struct bpf_prog *tgt_prog) 649 { 650 int err; 651 652 mutex_lock(&tr->mutex); 653 err = __bpf_trampoline_link_prog(link, tr, tgt_prog); 654 mutex_unlock(&tr->mutex); 655 return err; 656 } 657 658 static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, 659 struct bpf_trampoline *tr, 660 struct bpf_prog *tgt_prog) 661 { 662 enum bpf_tramp_prog_type kind; 663 int err; 664 665 kind = bpf_attach_type_to_tramp(link->link.prog); 666 if (kind == BPF_TRAMP_REPLACE) { 667 WARN_ON_ONCE(!tr->extension_prog); 668 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, 669 BPF_MOD_NOP, 670 tr->extension_prog->bpf_func, NULL); 671 tr->extension_prog = NULL; 672 guard(mutex)(&tgt_prog->aux->ext_mutex); 673 tgt_prog->aux->is_extended = false; 674 return err; 675 } 676 hlist_del_init(&link->tramp_hlist); 677 tr->progs_cnt[kind]--; 678 return bpf_trampoline_update(tr, true /* lock_direct_mutex */); 679 } 680 681 /* bpf_trampoline_unlink_prog() should never fail. */ 682 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, 683 struct bpf_trampoline *tr, 684 struct bpf_prog *tgt_prog) 685 { 686 int err; 687 688 mutex_lock(&tr->mutex); 689 err = __bpf_trampoline_unlink_prog(link, tr, tgt_prog); 690 mutex_unlock(&tr->mutex); 691 return err; 692 } 693 694 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM) 695 static void bpf_shim_tramp_link_release(struct bpf_link *link) 696 { 697 struct bpf_shim_tramp_link *shim_link = 698 container_of(link, struct bpf_shim_tramp_link, link.link); 699 700 /* paired with 'shim_link->trampoline = tr' in bpf_trampoline_link_cgroup_shim */ 701 if (!shim_link->trampoline) 702 return; 703 704 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&shim_link->link, shim_link->trampoline, NULL)); 705 bpf_trampoline_put(shim_link->trampoline); 706 } 707 708 static void bpf_shim_tramp_link_dealloc(struct bpf_link *link) 709 { 710 struct bpf_shim_tramp_link *shim_link = 711 container_of(link, struct bpf_shim_tramp_link, link.link); 712 713 kfree(shim_link); 714 } 715 716 static const struct bpf_link_ops bpf_shim_tramp_link_lops = { 717 .release = bpf_shim_tramp_link_release, 718 .dealloc = bpf_shim_tramp_link_dealloc, 719 }; 720 721 static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog, 722 bpf_func_t bpf_func, 723 int cgroup_atype, 724 enum bpf_attach_type attach_type) 725 { 726 struct bpf_shim_tramp_link *shim_link = NULL; 727 struct bpf_prog *p; 728 729 shim_link = kzalloc(sizeof(*shim_link), GFP_USER); 730 if (!shim_link) 731 return NULL; 732 733 p = bpf_prog_alloc(1, 0); 734 if (!p) { 735 kfree(shim_link); 736 return NULL; 737 } 738 739 p->jited = false; 740 p->bpf_func = bpf_func; 741 742 p->aux->cgroup_atype = cgroup_atype; 743 p->aux->attach_func_proto = prog->aux->attach_func_proto; 744 p->aux->attach_btf_id = prog->aux->attach_btf_id; 745 p->aux->attach_btf = prog->aux->attach_btf; 746 btf_get(p->aux->attach_btf); 747 p->type = BPF_PROG_TYPE_LSM; 748 p->expected_attach_type = BPF_LSM_MAC; 749 bpf_prog_inc(p); 750 bpf_link_init(&shim_link->link.link, BPF_LINK_TYPE_UNSPEC, 751 &bpf_shim_tramp_link_lops, p, attach_type); 752 bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype); 753 754 return shim_link; 755 } 756 757 static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr, 758 bpf_func_t bpf_func) 759 { 760 struct bpf_tramp_link *link; 761 int kind; 762 763 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 764 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) { 765 struct bpf_prog *p = link->link.prog; 766 767 if (p->bpf_func == bpf_func) 768 return container_of(link, struct bpf_shim_tramp_link, link); 769 } 770 } 771 772 return NULL; 773 } 774 775 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 776 int cgroup_atype, 777 enum bpf_attach_type attach_type) 778 { 779 struct bpf_shim_tramp_link *shim_link = NULL; 780 struct bpf_attach_target_info tgt_info = {}; 781 struct bpf_trampoline *tr; 782 bpf_func_t bpf_func; 783 u64 key; 784 int err; 785 786 err = bpf_check_attach_target(NULL, prog, NULL, 787 prog->aux->attach_btf_id, 788 &tgt_info); 789 if (err) 790 return err; 791 792 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 793 prog->aux->attach_btf_id); 794 795 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 796 tr = bpf_trampoline_get(key, &tgt_info); 797 if (!tr) 798 return -ENOMEM; 799 800 mutex_lock(&tr->mutex); 801 802 shim_link = cgroup_shim_find(tr, bpf_func); 803 if (shim_link) { 804 /* Reusing existing shim attached by the other program. */ 805 bpf_link_inc(&shim_link->link.link); 806 807 mutex_unlock(&tr->mutex); 808 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 809 return 0; 810 } 811 812 /* Allocate and install new shim. */ 813 814 shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype, attach_type); 815 if (!shim_link) { 816 err = -ENOMEM; 817 goto err; 818 } 819 820 err = __bpf_trampoline_link_prog(&shim_link->link, tr, NULL); 821 if (err) 822 goto err; 823 824 shim_link->trampoline = tr; 825 /* note, we're still holding tr refcnt from above */ 826 827 mutex_unlock(&tr->mutex); 828 829 return 0; 830 err: 831 mutex_unlock(&tr->mutex); 832 833 if (shim_link) 834 bpf_link_put(&shim_link->link.link); 835 836 /* have to release tr while _not_ holding its mutex */ 837 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 838 839 return err; 840 } 841 842 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog) 843 { 844 struct bpf_shim_tramp_link *shim_link = NULL; 845 struct bpf_trampoline *tr; 846 bpf_func_t bpf_func; 847 u64 key; 848 849 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 850 prog->aux->attach_btf_id); 851 852 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 853 tr = bpf_trampoline_lookup(key); 854 if (WARN_ON_ONCE(!tr)) 855 return; 856 857 mutex_lock(&tr->mutex); 858 shim_link = cgroup_shim_find(tr, bpf_func); 859 mutex_unlock(&tr->mutex); 860 861 if (shim_link) 862 bpf_link_put(&shim_link->link.link); 863 864 bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */ 865 } 866 #endif 867 868 struct bpf_trampoline *bpf_trampoline_get(u64 key, 869 struct bpf_attach_target_info *tgt_info) 870 { 871 struct bpf_trampoline *tr; 872 873 tr = bpf_trampoline_lookup(key); 874 if (!tr) 875 return NULL; 876 877 mutex_lock(&tr->mutex); 878 if (tr->func.addr) 879 goto out; 880 881 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); 882 tr->func.addr = (void *)tgt_info->tgt_addr; 883 out: 884 mutex_unlock(&tr->mutex); 885 return tr; 886 } 887 888 void bpf_trampoline_put(struct bpf_trampoline *tr) 889 { 890 int i; 891 892 if (!tr) 893 return; 894 mutex_lock(&trampoline_mutex); 895 if (!refcount_dec_and_test(&tr->refcnt)) 896 goto out; 897 WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); 898 899 for (i = 0; i < BPF_TRAMP_MAX; i++) 900 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i]))) 901 goto out; 902 903 /* This code will be executed even when the last bpf_tramp_image 904 * is alive. All progs are detached from the trampoline and the 905 * trampoline image is patched with jmp into epilogue to skip 906 * fexit progs. The fentry-only trampoline will be freed via 907 * multiple rcu callbacks. 908 */ 909 hlist_del(&tr->hlist); 910 if (tr->fops) { 911 ftrace_free_filter(tr->fops); 912 kfree(tr->fops); 913 } 914 kfree(tr); 915 out: 916 mutex_unlock(&trampoline_mutex); 917 } 918 919 #define NO_START_TIME 1 920 static __always_inline u64 notrace bpf_prog_start_time(void) 921 { 922 u64 start = NO_START_TIME; 923 924 if (static_branch_unlikely(&bpf_stats_enabled_key)) { 925 start = sched_clock(); 926 if (unlikely(!start)) 927 start = NO_START_TIME; 928 } 929 return start; 930 } 931 932 /* The logic is similar to bpf_prog_run(), but with an explicit 933 * rcu_read_lock() and migrate_disable() which are required 934 * for the trampoline. The macro is split into 935 * call __bpf_prog_enter 936 * call prog->bpf_func 937 * call __bpf_prog_exit 938 * 939 * __bpf_prog_enter returns: 940 * 0 - skip execution of the bpf prog 941 * 1 - execute bpf prog 942 * [2..MAX_U64] - execute bpf prog and record execution time. 943 * This is start time. 944 */ 945 static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) 946 __acquires(RCU) 947 { 948 rcu_read_lock_dont_migrate(); 949 950 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 951 952 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) { 953 bpf_prog_inc_misses_counter(prog); 954 if (prog->aux->recursion_detected) 955 prog->aux->recursion_detected(prog); 956 return 0; 957 } 958 return bpf_prog_start_time(); 959 } 960 961 static void notrace __update_prog_stats(struct bpf_prog *prog, u64 start) 962 { 963 struct bpf_prog_stats *stats; 964 unsigned long flags; 965 u64 duration; 966 967 /* 968 * static_key could be enabled in __bpf_prog_enter* and disabled in 969 * __bpf_prog_exit*. And vice versa. Check that 'start' is valid. 970 */ 971 if (start <= NO_START_TIME) 972 return; 973 974 duration = sched_clock() - start; 975 stats = this_cpu_ptr(prog->stats); 976 flags = u64_stats_update_begin_irqsave(&stats->syncp); 977 u64_stats_inc(&stats->cnt); 978 u64_stats_add(&stats->nsecs, duration); 979 u64_stats_update_end_irqrestore(&stats->syncp, flags); 980 } 981 982 static __always_inline void notrace update_prog_stats(struct bpf_prog *prog, 983 u64 start) 984 { 985 if (static_branch_unlikely(&bpf_stats_enabled_key)) 986 __update_prog_stats(prog, start); 987 } 988 989 static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start, 990 struct bpf_tramp_run_ctx *run_ctx) 991 __releases(RCU) 992 { 993 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 994 995 update_prog_stats(prog, start); 996 this_cpu_dec(*(prog->active)); 997 rcu_read_unlock_migrate(); 998 } 999 1000 static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog, 1001 struct bpf_tramp_run_ctx *run_ctx) 1002 __acquires(RCU) 1003 { 1004 /* Runtime stats are exported via actual BPF_LSM_CGROUP 1005 * programs, not the shims. 1006 */ 1007 rcu_read_lock_dont_migrate(); 1008 1009 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1010 1011 return NO_START_TIME; 1012 } 1013 1014 static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start, 1015 struct bpf_tramp_run_ctx *run_ctx) 1016 __releases(RCU) 1017 { 1018 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1019 1020 rcu_read_unlock_migrate(); 1021 } 1022 1023 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, 1024 struct bpf_tramp_run_ctx *run_ctx) 1025 { 1026 rcu_read_lock_trace(); 1027 migrate_disable(); 1028 might_fault(); 1029 1030 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1031 1032 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) { 1033 bpf_prog_inc_misses_counter(prog); 1034 if (prog->aux->recursion_detected) 1035 prog->aux->recursion_detected(prog); 1036 return 0; 1037 } 1038 return bpf_prog_start_time(); 1039 } 1040 1041 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start, 1042 struct bpf_tramp_run_ctx *run_ctx) 1043 { 1044 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1045 1046 update_prog_stats(prog, start); 1047 this_cpu_dec(*(prog->active)); 1048 migrate_enable(); 1049 rcu_read_unlock_trace(); 1050 } 1051 1052 static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, 1053 struct bpf_tramp_run_ctx *run_ctx) 1054 { 1055 rcu_read_lock_trace(); 1056 migrate_disable(); 1057 might_fault(); 1058 1059 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1060 1061 return bpf_prog_start_time(); 1062 } 1063 1064 static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start, 1065 struct bpf_tramp_run_ctx *run_ctx) 1066 { 1067 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1068 1069 update_prog_stats(prog, start); 1070 migrate_enable(); 1071 rcu_read_unlock_trace(); 1072 } 1073 1074 static u64 notrace __bpf_prog_enter(struct bpf_prog *prog, 1075 struct bpf_tramp_run_ctx *run_ctx) 1076 __acquires(RCU) 1077 { 1078 rcu_read_lock_dont_migrate(); 1079 1080 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1081 1082 return bpf_prog_start_time(); 1083 } 1084 1085 static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, 1086 struct bpf_tramp_run_ctx *run_ctx) 1087 __releases(RCU) 1088 { 1089 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1090 1091 update_prog_stats(prog, start); 1092 rcu_read_unlock_migrate(); 1093 } 1094 1095 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr) 1096 { 1097 percpu_ref_get(&tr->pcref); 1098 } 1099 1100 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr) 1101 { 1102 percpu_ref_put(&tr->pcref); 1103 } 1104 1105 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog) 1106 { 1107 bool sleepable = prog->sleepable; 1108 1109 if (bpf_prog_check_recur(prog)) 1110 return sleepable ? __bpf_prog_enter_sleepable_recur : 1111 __bpf_prog_enter_recur; 1112 1113 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1114 prog->expected_attach_type == BPF_LSM_CGROUP) 1115 return __bpf_prog_enter_lsm_cgroup; 1116 1117 return sleepable ? __bpf_prog_enter_sleepable : __bpf_prog_enter; 1118 } 1119 1120 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog) 1121 { 1122 bool sleepable = prog->sleepable; 1123 1124 if (bpf_prog_check_recur(prog)) 1125 return sleepable ? __bpf_prog_exit_sleepable_recur : 1126 __bpf_prog_exit_recur; 1127 1128 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1129 prog->expected_attach_type == BPF_LSM_CGROUP) 1130 return __bpf_prog_exit_lsm_cgroup; 1131 1132 return sleepable ? __bpf_prog_exit_sleepable : __bpf_prog_exit; 1133 } 1134 1135 int __weak 1136 arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end, 1137 const struct btf_func_model *m, u32 flags, 1138 struct bpf_tramp_links *tlinks, 1139 void *func_addr) 1140 { 1141 return -ENOTSUPP; 1142 } 1143 1144 void * __weak arch_alloc_bpf_trampoline(unsigned int size) 1145 { 1146 void *image; 1147 1148 if (WARN_ON_ONCE(size > PAGE_SIZE)) 1149 return NULL; 1150 image = bpf_jit_alloc_exec(PAGE_SIZE); 1151 if (image) 1152 set_vm_flush_reset_perms(image); 1153 return image; 1154 } 1155 1156 void __weak arch_free_bpf_trampoline(void *image, unsigned int size) 1157 { 1158 WARN_ON_ONCE(size > PAGE_SIZE); 1159 /* bpf_jit_free_exec doesn't need "size", but 1160 * bpf_prog_pack_free() needs it. 1161 */ 1162 bpf_jit_free_exec(image); 1163 } 1164 1165 int __weak arch_protect_bpf_trampoline(void *image, unsigned int size) 1166 { 1167 WARN_ON_ONCE(size > PAGE_SIZE); 1168 return set_memory_rox((long)image, 1); 1169 } 1170 1171 int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, 1172 struct bpf_tramp_links *tlinks, void *func_addr) 1173 { 1174 return -ENOTSUPP; 1175 } 1176 1177 static int __init init_trampolines(void) 1178 { 1179 int i; 1180 1181 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1182 INIT_HLIST_HEAD(&trampoline_table[i]); 1183 return 0; 1184 } 1185 late_initcall(init_trampolines); 1186