trampoline.c 29 KB

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