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