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