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