bpf_testmod.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020 Facebook */
  3. #include <linux/bpf.h>
  4. #include <linux/btf.h>
  5. #include <linux/btf_ids.h>
  6. #include <linux/delay.h>
  7. #include <linux/error-injection.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/percpu-defs.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/tracepoint.h>
  13. #include <linux/net.h>
  14. #include <linux/socket.h>
  15. #include <linux/nsproxy.h>
  16. #include <linux/inet.h>
  17. #include <linux/in.h>
  18. #include <linux/in6.h>
  19. #include <linux/un.h>
  20. #include <linux/filter.h>
  21. #include <net/sock.h>
  22. #include <linux/namei.h>
  23. #include "bpf_testmod.h"
  24. #include "bpf_testmod_kfunc.h"
  25. #define CREATE_TRACE_POINTS
  26. #include "bpf_testmod-events.h"
  27. #define CONNECT_TIMEOUT_SEC 1
  28. typedef int (*func_proto_typedef)(long);
  29. typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
  30. typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
  31. DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
  32. long bpf_testmod_test_struct_arg_result;
  33. static DEFINE_MUTEX(sock_lock);
  34. static struct socket *sock;
  35. struct bpf_testmod_struct_arg_1 {
  36. int a;
  37. };
  38. struct bpf_testmod_struct_arg_2 {
  39. long a;
  40. long b;
  41. };
  42. struct bpf_testmod_struct_arg_3 {
  43. int a;
  44. int b[];
  45. };
  46. struct bpf_testmod_struct_arg_4 {
  47. u64 a;
  48. int b;
  49. };
  50. struct bpf_testmod_struct_arg_5 {
  51. char a;
  52. short b;
  53. int c;
  54. long d;
  55. };
  56. __bpf_hook_start();
  57. noinline int
  58. bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
  59. bpf_testmod_test_struct_arg_result = a.a + a.b + b + c;
  60. return bpf_testmod_test_struct_arg_result;
  61. }
  62. noinline int
  63. bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) {
  64. bpf_testmod_test_struct_arg_result = a + b.a + b.b + c;
  65. return bpf_testmod_test_struct_arg_result;
  66. }
  67. noinline int
  68. bpf_testmod_test_struct_arg_3(int a, int b, struct bpf_testmod_struct_arg_2 c) {
  69. bpf_testmod_test_struct_arg_result = a + b + c.a + c.b;
  70. return bpf_testmod_test_struct_arg_result;
  71. }
  72. noinline int
  73. bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a, int b,
  74. int c, int d, struct bpf_testmod_struct_arg_2 e) {
  75. bpf_testmod_test_struct_arg_result = a.a + b + c + d + e.a + e.b;
  76. return bpf_testmod_test_struct_arg_result;
  77. }
  78. noinline int
  79. bpf_testmod_test_struct_arg_5(void) {
  80. bpf_testmod_test_struct_arg_result = 1;
  81. return bpf_testmod_test_struct_arg_result;
  82. }
  83. noinline int
  84. bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) {
  85. bpf_testmod_test_struct_arg_result = a->b[0];
  86. return bpf_testmod_test_struct_arg_result;
  87. }
  88. noinline int
  89. bpf_testmod_test_struct_arg_7(u64 a, void *b, short c, int d, void *e,
  90. struct bpf_testmod_struct_arg_4 f)
  91. {
  92. bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
  93. (long)e + f.a + f.b;
  94. return bpf_testmod_test_struct_arg_result;
  95. }
  96. noinline int
  97. bpf_testmod_test_struct_arg_8(u64 a, void *b, short c, int d, void *e,
  98. struct bpf_testmod_struct_arg_4 f, int g)
  99. {
  100. bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
  101. (long)e + f.a + f.b + g;
  102. return bpf_testmod_test_struct_arg_result;
  103. }
  104. noinline int
  105. bpf_testmod_test_struct_arg_9(u64 a, void *b, short c, int d, void *e, char f,
  106. short g, struct bpf_testmod_struct_arg_5 h, long i)
  107. {
  108. bpf_testmod_test_struct_arg_result = a + (long)b + c + d + (long)e +
  109. f + g + h.a + h.b + h.c + h.d + i;
  110. return bpf_testmod_test_struct_arg_result;
  111. }
  112. noinline int
  113. bpf_testmod_test_arg_ptr_to_struct(struct bpf_testmod_struct_arg_1 *a) {
  114. bpf_testmod_test_struct_arg_result = a->a;
  115. return bpf_testmod_test_struct_arg_result;
  116. }
  117. __bpf_kfunc void
  118. bpf_testmod_test_mod_kfunc(int i)
  119. {
  120. *(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
  121. }
  122. __bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
  123. {
  124. it->cnt = cnt;
  125. if (cnt < 0)
  126. return -EINVAL;
  127. it->value = value;
  128. return 0;
  129. }
  130. __bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
  131. {
  132. if (it->cnt <= 0)
  133. return NULL;
  134. it->cnt--;
  135. return &it->value;
  136. }
  137. __bpf_kfunc s64 bpf_iter_testmod_seq_value(int val, struct bpf_iter_testmod_seq* it__iter)
  138. {
  139. if (it__iter->cnt < 0)
  140. return 0;
  141. return val + it__iter->value;
  142. }
  143. __bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
  144. {
  145. it->cnt = 0;
  146. }
  147. __bpf_kfunc void bpf_kfunc_common_test(void)
  148. {
  149. }
  150. __bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr,
  151. struct bpf_dynptr *ptr__nullable)
  152. {
  153. }
  154. __bpf_kfunc struct sk_buff *bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head *ptr)
  155. {
  156. return NULL;
  157. }
  158. __bpf_kfunc struct sk_buff *bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common *ptr)
  159. {
  160. return NULL;
  161. }
  162. __bpf_kfunc void bpf_kfunc_nested_release_test(struct sk_buff *ptr)
  163. {
  164. }
  165. __bpf_kfunc void bpf_kfunc_trusted_vma_test(struct vm_area_struct *ptr)
  166. {
  167. }
  168. __bpf_kfunc void bpf_kfunc_trusted_task_test(struct task_struct *ptr)
  169. {
  170. }
  171. __bpf_kfunc void bpf_kfunc_trusted_num_test(int *ptr)
  172. {
  173. }
  174. __bpf_kfunc void bpf_kfunc_rcu_task_test(struct task_struct *ptr)
  175. {
  176. }
  177. __bpf_kfunc struct bpf_testmod_ctx *
  178. bpf_testmod_ctx_create(int *err)
  179. {
  180. struct bpf_testmod_ctx *ctx;
  181. ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
  182. if (!ctx) {
  183. *err = -ENOMEM;
  184. return NULL;
  185. }
  186. refcount_set(&ctx->usage, 1);
  187. return ctx;
  188. }
  189. static void testmod_free_cb(struct rcu_head *head)
  190. {
  191. struct bpf_testmod_ctx *ctx;
  192. ctx = container_of(head, struct bpf_testmod_ctx, rcu);
  193. kfree(ctx);
  194. }
  195. __bpf_kfunc void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx)
  196. {
  197. if (!ctx)
  198. return;
  199. if (refcount_dec_and_test(&ctx->usage))
  200. call_rcu(&ctx->rcu, testmod_free_cb);
  201. }
  202. struct bpf_testmod_btf_type_tag_1 {
  203. int a;
  204. };
  205. struct bpf_testmod_btf_type_tag_2 {
  206. struct bpf_testmod_btf_type_tag_1 __user *p;
  207. };
  208. struct bpf_testmod_btf_type_tag_3 {
  209. struct bpf_testmod_btf_type_tag_1 __percpu *p;
  210. };
  211. noinline int
  212. bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
  213. BTF_TYPE_EMIT(func_proto_typedef);
  214. BTF_TYPE_EMIT(func_proto_typedef_nested1);
  215. BTF_TYPE_EMIT(func_proto_typedef_nested2);
  216. return arg->a;
  217. }
  218. noinline int
  219. bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
  220. return arg->p->a;
  221. }
  222. noinline int
  223. bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
  224. return arg->a;
  225. }
  226. noinline int
  227. bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
  228. return arg->p->a;
  229. }
  230. noinline int bpf_testmod_loop_test(int n)
  231. {
  232. /* Make sum volatile, so smart compilers, such as clang, will not
  233. * optimize the code by removing the loop.
  234. */
  235. volatile int sum = 0;
  236. int i;
  237. /* the primary goal of this test is to test LBR. Create a lot of
  238. * branches in the function, so we can catch it easily.
  239. */
  240. for (i = 0; i < n; i++)
  241. sum += i;
  242. return sum;
  243. }
  244. __weak noinline struct file *bpf_testmod_return_ptr(int arg)
  245. {
  246. static struct file f = {};
  247. switch (arg) {
  248. case 1: return (void *)EINVAL; /* user addr */
  249. case 2: return (void *)0xcafe4a11; /* user addr */
  250. case 3: return (void *)-EINVAL; /* canonical, but invalid */
  251. case 4: return (void *)(1ull << 60); /* non-canonical and invalid */
  252. case 5: return (void *)~(1ull << 30); /* trigger extable */
  253. case 6: return &f; /* valid addr */
  254. case 7: return (void *)((long)&f | 1); /* kernel tricks */
  255. #ifdef CONFIG_X86_64
  256. case 8: return (void *)VSYSCALL_ADDR; /* vsyscall page address */
  257. #endif
  258. default: return NULL;
  259. }
  260. }
  261. noinline int bpf_testmod_fentry_test1(int a)
  262. {
  263. return a + 1;
  264. }
  265. noinline int bpf_testmod_fentry_test2(int a, u64 b)
  266. {
  267. return a + b;
  268. }
  269. noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
  270. {
  271. return a + b + c;
  272. }
  273. noinline int bpf_testmod_fentry_test7(u64 a, void *b, short c, int d,
  274. void *e, char f, int g)
  275. {
  276. return a + (long)b + c + d + (long)e + f + g;
  277. }
  278. noinline int bpf_testmod_fentry_test11(u64 a, void *b, short c, int d,
  279. void *e, char f, int g,
  280. unsigned int h, long i, __u64 j,
  281. unsigned long k)
  282. {
  283. return a + (long)b + c + d + (long)e + f + g + h + i + j + k;
  284. }
  285. int bpf_testmod_fentry_ok;
  286. noinline ssize_t
  287. bpf_testmod_test_read(struct file *file, struct kobject *kobj,
  288. struct bin_attribute *bin_attr,
  289. char *buf, loff_t off, size_t len)
  290. {
  291. struct bpf_testmod_test_read_ctx ctx = {
  292. .buf = buf,
  293. .off = off,
  294. .len = len,
  295. };
  296. struct bpf_testmod_struct_arg_1 struct_arg1 = {10}, struct_arg1_2 = {-1};
  297. struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
  298. struct bpf_testmod_struct_arg_3 *struct_arg3;
  299. struct bpf_testmod_struct_arg_4 struct_arg4 = {21, 22};
  300. struct bpf_testmod_struct_arg_5 struct_arg5 = {23, 24, 25, 26};
  301. int i = 1;
  302. while (bpf_testmod_return_ptr(i))
  303. i++;
  304. (void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
  305. (void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
  306. (void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
  307. (void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
  308. (void)bpf_testmod_test_struct_arg_5();
  309. (void)bpf_testmod_test_struct_arg_7(16, (void *)17, 18, 19,
  310. (void *)20, struct_arg4);
  311. (void)bpf_testmod_test_struct_arg_8(16, (void *)17, 18, 19,
  312. (void *)20, struct_arg4, 23);
  313. (void)bpf_testmod_test_struct_arg_9(16, (void *)17, 18, 19, (void *)20,
  314. 21, 22, struct_arg5, 27);
  315. (void)bpf_testmod_test_arg_ptr_to_struct(&struct_arg1_2);
  316. (void)trace_bpf_testmod_test_raw_tp_null(NULL);
  317. struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
  318. sizeof(int)), GFP_KERNEL);
  319. if (struct_arg3 != NULL) {
  320. struct_arg3->b[0] = 1;
  321. (void)bpf_testmod_test_struct_arg_6(struct_arg3);
  322. kfree(struct_arg3);
  323. }
  324. /* This is always true. Use the check to make sure the compiler
  325. * doesn't remove bpf_testmod_loop_test.
  326. */
  327. if (bpf_testmod_loop_test(101) > 100)
  328. trace_bpf_testmod_test_read(current, &ctx);
  329. trace_bpf_testmod_test_nullable_bare(NULL);
  330. /* Magic number to enable writable tp */
  331. if (len == 64) {
  332. struct bpf_testmod_test_writable_ctx writable = {
  333. .val = 1024,
  334. };
  335. trace_bpf_testmod_test_writable_bare(&writable);
  336. if (writable.early_ret)
  337. return snprintf(buf, len, "%d\n", writable.val);
  338. }
  339. if (bpf_testmod_fentry_test1(1) != 2 ||
  340. bpf_testmod_fentry_test2(2, 3) != 5 ||
  341. bpf_testmod_fentry_test3(4, 5, 6) != 15 ||
  342. bpf_testmod_fentry_test7(16, (void *)17, 18, 19, (void *)20,
  343. 21, 22) != 133 ||
  344. bpf_testmod_fentry_test11(16, (void *)17, 18, 19, (void *)20,
  345. 21, 22, 23, 24, 25, 26) != 231)
  346. goto out;
  347. bpf_testmod_fentry_ok = 1;
  348. out:
  349. return -EIO; /* always fail */
  350. }
  351. EXPORT_SYMBOL(bpf_testmod_test_read);
  352. ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
  353. noinline ssize_t
  354. bpf_testmod_test_write(struct file *file, struct kobject *kobj,
  355. struct bin_attribute *bin_attr,
  356. char *buf, loff_t off, size_t len)
  357. {
  358. struct bpf_testmod_test_write_ctx ctx = {
  359. .buf = buf,
  360. .off = off,
  361. .len = len,
  362. };
  363. trace_bpf_testmod_test_write_bare(current, &ctx);
  364. return -EIO; /* always fail */
  365. }
  366. EXPORT_SYMBOL(bpf_testmod_test_write);
  367. ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
  368. noinline int bpf_fentry_shadow_test(int a)
  369. {
  370. return a + 2;
  371. }
  372. EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test);
  373. __bpf_hook_end();
  374. static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
  375. .attr = { .name = "bpf_testmod", .mode = 0666, },
  376. .read = bpf_testmod_test_read,
  377. .write = bpf_testmod_test_write,
  378. };
  379. /* bpf_testmod_uprobe sysfs attribute is so far enabled for x86_64 only,
  380. * please see test_uretprobe_regs_change test
  381. */
  382. #ifdef __x86_64__
  383. static int
  384. uprobe_ret_handler(struct uprobe_consumer *self, unsigned long func,
  385. struct pt_regs *regs)
  386. {
  387. regs->ax = 0x12345678deadbeef;
  388. regs->cx = 0x87654321feebdaed;
  389. regs->r11 = (u64) -1;
  390. return true;
  391. }
  392. struct testmod_uprobe {
  393. struct path path;
  394. struct uprobe *uprobe;
  395. struct uprobe_consumer consumer;
  396. };
  397. static DEFINE_MUTEX(testmod_uprobe_mutex);
  398. static struct testmod_uprobe uprobe = {
  399. .consumer.ret_handler = uprobe_ret_handler,
  400. };
  401. static int testmod_register_uprobe(loff_t offset)
  402. {
  403. int err = -EBUSY;
  404. if (uprobe.uprobe)
  405. return -EBUSY;
  406. mutex_lock(&testmod_uprobe_mutex);
  407. if (uprobe.uprobe)
  408. goto out;
  409. err = kern_path("/proc/self/exe", LOOKUP_FOLLOW, &uprobe.path);
  410. if (err)
  411. goto out;
  412. uprobe.uprobe = uprobe_register(d_real_inode(uprobe.path.dentry),
  413. offset, 0, &uprobe.consumer);
  414. if (IS_ERR(uprobe.uprobe)) {
  415. err = PTR_ERR(uprobe.uprobe);
  416. path_put(&uprobe.path);
  417. uprobe.uprobe = NULL;
  418. }
  419. out:
  420. mutex_unlock(&testmod_uprobe_mutex);
  421. return err;
  422. }
  423. static void testmod_unregister_uprobe(void)
  424. {
  425. mutex_lock(&testmod_uprobe_mutex);
  426. if (uprobe.uprobe) {
  427. uprobe_unregister_nosync(uprobe.uprobe, &uprobe.consumer);
  428. uprobe_unregister_sync();
  429. path_put(&uprobe.path);
  430. uprobe.uprobe = NULL;
  431. }
  432. mutex_unlock(&testmod_uprobe_mutex);
  433. }
  434. static ssize_t
  435. bpf_testmod_uprobe_write(struct file *file, struct kobject *kobj,
  436. struct bin_attribute *bin_attr,
  437. char *buf, loff_t off, size_t len)
  438. {
  439. unsigned long offset = 0;
  440. int err = 0;
  441. if (kstrtoul(buf, 0, &offset))
  442. return -EINVAL;
  443. if (offset)
  444. err = testmod_register_uprobe(offset);
  445. else
  446. testmod_unregister_uprobe();
  447. return err ?: strlen(buf);
  448. }
  449. static struct bin_attribute bin_attr_bpf_testmod_uprobe_file __ro_after_init = {
  450. .attr = { .name = "bpf_testmod_uprobe", .mode = 0666, },
  451. .write = bpf_testmod_uprobe_write,
  452. };
  453. static int register_bpf_testmod_uprobe(void)
  454. {
  455. return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
  456. }
  457. static void unregister_bpf_testmod_uprobe(void)
  458. {
  459. testmod_unregister_uprobe();
  460. sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
  461. }
  462. #else
  463. static int register_bpf_testmod_uprobe(void)
  464. {
  465. return 0;
  466. }
  467. static void unregister_bpf_testmod_uprobe(void) { }
  468. #endif
  469. BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
  470. BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
  471. BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
  472. BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
  473. BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value)
  474. BTF_ID_FLAGS(func, bpf_kfunc_common_test)
  475. BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
  476. BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_nonzero_offset_test, KF_ACQUIRE)
  477. BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_zero_offset_test, KF_ACQUIRE)
  478. BTF_ID_FLAGS(func, bpf_kfunc_nested_release_test, KF_RELEASE)
  479. BTF_ID_FLAGS(func, bpf_kfunc_trusted_vma_test, KF_TRUSTED_ARGS)
  480. BTF_ID_FLAGS(func, bpf_kfunc_trusted_task_test, KF_TRUSTED_ARGS)
  481. BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test, KF_TRUSTED_ARGS)
  482. BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU)
  483. BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
  484. BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
  485. BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
  486. BTF_ID_LIST(bpf_testmod_dtor_ids)
  487. BTF_ID(struct, bpf_testmod_ctx)
  488. BTF_ID(func, bpf_testmod_ctx_release)
  489. static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {
  490. .owner = THIS_MODULE,
  491. .set = &bpf_testmod_common_kfunc_ids,
  492. };
  493. __bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
  494. {
  495. return a + b + c + d;
  496. }
  497. __bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
  498. {
  499. return a + b;
  500. }
  501. __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk)
  502. {
  503. return sk;
  504. }
  505. __bpf_kfunc long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d)
  506. {
  507. /* Provoke the compiler to assume that the caller has sign-extended a,
  508. * b and c on platforms where this is required (e.g. s390x).
  509. */
  510. return (long)a + (long)b + (long)c + d;
  511. }
  512. static struct prog_test_ref_kfunc prog_test_struct = {
  513. .a = 42,
  514. .b = 108,
  515. .next = &prog_test_struct,
  516. .cnt = REFCOUNT_INIT(1),
  517. };
  518. __bpf_kfunc struct prog_test_ref_kfunc *
  519. bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
  520. {
  521. refcount_inc(&prog_test_struct.cnt);
  522. return &prog_test_struct;
  523. }
  524. __bpf_kfunc void bpf_kfunc_call_test_offset(struct prog_test_ref_kfunc *p)
  525. {
  526. WARN_ON_ONCE(1);
  527. }
  528. __bpf_kfunc struct prog_test_member *
  529. bpf_kfunc_call_memb_acquire(void)
  530. {
  531. WARN_ON_ONCE(1);
  532. return NULL;
  533. }
  534. __bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
  535. {
  536. WARN_ON_ONCE(1);
  537. }
  538. static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size)
  539. {
  540. if (size > 2 * sizeof(int))
  541. return NULL;
  542. return (int *)p;
  543. }
  544. __bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p,
  545. const int rdwr_buf_size)
  546. {
  547. return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size);
  548. }
  549. __bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p,
  550. const int rdonly_buf_size)
  551. {
  552. return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
  553. }
  554. /* the next 2 ones can't be really used for testing expect to ensure
  555. * that the verifier rejects the call.
  556. * Acquire functions must return struct pointers, so these ones are
  557. * failing.
  558. */
  559. __bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p,
  560. const int rdonly_buf_size)
  561. {
  562. return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
  563. }
  564. __bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p)
  565. {
  566. }
  567. __bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb)
  568. {
  569. }
  570. __bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p)
  571. {
  572. }
  573. __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
  574. {
  575. }
  576. __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)
  577. {
  578. }
  579. __bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p)
  580. {
  581. }
  582. __bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p)
  583. {
  584. }
  585. __bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz)
  586. {
  587. }
  588. __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len)
  589. {
  590. }
  591. __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
  592. {
  593. }
  594. __bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
  595. {
  596. /* p != NULL, but p->cnt could be 0 */
  597. }
  598. __bpf_kfunc void bpf_kfunc_call_test_destructive(void)
  599. {
  600. }
  601. __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused)
  602. {
  603. return arg;
  604. }
  605. __bpf_kfunc void bpf_kfunc_call_test_sleepable(void)
  606. {
  607. }
  608. __bpf_kfunc int bpf_kfunc_init_sock(struct init_sock_args *args)
  609. {
  610. int proto;
  611. int err;
  612. mutex_lock(&sock_lock);
  613. if (sock) {
  614. pr_err("%s called without releasing old sock", __func__);
  615. err = -EPERM;
  616. goto out;
  617. }
  618. switch (args->af) {
  619. case AF_INET:
  620. case AF_INET6:
  621. proto = args->type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;
  622. break;
  623. case AF_UNIX:
  624. proto = PF_UNIX;
  625. break;
  626. default:
  627. pr_err("invalid address family %d\n", args->af);
  628. err = -EINVAL;
  629. goto out;
  630. }
  631. err = sock_create_kern(current->nsproxy->net_ns, args->af, args->type,
  632. proto, &sock);
  633. if (!err)
  634. /* Set timeout for call to kernel_connect() to prevent it from hanging,
  635. * and consider the connection attempt failed if it returns
  636. * -EINPROGRESS.
  637. */
  638. sock->sk->sk_sndtimeo = CONNECT_TIMEOUT_SEC * HZ;
  639. out:
  640. mutex_unlock(&sock_lock);
  641. return err;
  642. }
  643. __bpf_kfunc void bpf_kfunc_close_sock(void)
  644. {
  645. mutex_lock(&sock_lock);
  646. if (sock) {
  647. sock_release(sock);
  648. sock = NULL;
  649. }
  650. mutex_unlock(&sock_lock);
  651. }
  652. __bpf_kfunc int bpf_kfunc_call_kernel_connect(struct addr_args *args)
  653. {
  654. int err;
  655. if (args->addrlen > sizeof(args->addr))
  656. return -EINVAL;
  657. mutex_lock(&sock_lock);
  658. if (!sock) {
  659. pr_err("%s called without initializing sock", __func__);
  660. err = -EPERM;
  661. goto out;
  662. }
  663. err = kernel_connect(sock, (struct sockaddr *)&args->addr,
  664. args->addrlen, 0);
  665. out:
  666. mutex_unlock(&sock_lock);
  667. return err;
  668. }
  669. __bpf_kfunc int bpf_kfunc_call_kernel_bind(struct addr_args *args)
  670. {
  671. int err;
  672. if (args->addrlen > sizeof(args->addr))
  673. return -EINVAL;
  674. mutex_lock(&sock_lock);
  675. if (!sock) {
  676. pr_err("%s called without initializing sock", __func__);
  677. err = -EPERM;
  678. goto out;
  679. }
  680. err = kernel_bind(sock, (struct sockaddr *)&args->addr, args->addrlen);
  681. out:
  682. mutex_unlock(&sock_lock);
  683. return err;
  684. }
  685. __bpf_kfunc int bpf_kfunc_call_kernel_listen(void)
  686. {
  687. int err;
  688. mutex_lock(&sock_lock);
  689. if (!sock) {
  690. pr_err("%s called without initializing sock", __func__);
  691. err = -EPERM;
  692. goto out;
  693. }
  694. err = kernel_listen(sock, 128);
  695. out:
  696. mutex_unlock(&sock_lock);
  697. return err;
  698. }
  699. __bpf_kfunc int bpf_kfunc_call_kernel_sendmsg(struct sendmsg_args *args)
  700. {
  701. struct msghdr msg = {
  702. .msg_name = &args->addr.addr,
  703. .msg_namelen = args->addr.addrlen,
  704. };
  705. struct kvec iov;
  706. int err;
  707. if (args->addr.addrlen > sizeof(args->addr.addr) ||
  708. args->msglen > sizeof(args->msg))
  709. return -EINVAL;
  710. iov.iov_base = args->msg;
  711. iov.iov_len = args->msglen;
  712. mutex_lock(&sock_lock);
  713. if (!sock) {
  714. pr_err("%s called without initializing sock", __func__);
  715. err = -EPERM;
  716. goto out;
  717. }
  718. err = kernel_sendmsg(sock, &msg, &iov, 1, args->msglen);
  719. args->addr.addrlen = msg.msg_namelen;
  720. out:
  721. mutex_unlock(&sock_lock);
  722. return err;
  723. }
  724. __bpf_kfunc int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args)
  725. {
  726. struct msghdr msg = {
  727. .msg_name = &args->addr.addr,
  728. .msg_namelen = args->addr.addrlen,
  729. };
  730. struct kvec iov;
  731. int err;
  732. if (args->addr.addrlen > sizeof(args->addr.addr) ||
  733. args->msglen > sizeof(args->msg))
  734. return -EINVAL;
  735. iov.iov_base = args->msg;
  736. iov.iov_len = args->msglen;
  737. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, args->msglen);
  738. mutex_lock(&sock_lock);
  739. if (!sock) {
  740. pr_err("%s called without initializing sock", __func__);
  741. err = -EPERM;
  742. goto out;
  743. }
  744. err = sock_sendmsg(sock, &msg);
  745. args->addr.addrlen = msg.msg_namelen;
  746. out:
  747. mutex_unlock(&sock_lock);
  748. return err;
  749. }
  750. __bpf_kfunc int bpf_kfunc_call_kernel_getsockname(struct addr_args *args)
  751. {
  752. int err;
  753. mutex_lock(&sock_lock);
  754. if (!sock) {
  755. pr_err("%s called without initializing sock", __func__);
  756. err = -EPERM;
  757. goto out;
  758. }
  759. err = kernel_getsockname(sock, (struct sockaddr *)&args->addr);
  760. if (err < 0)
  761. goto out;
  762. args->addrlen = err;
  763. err = 0;
  764. out:
  765. mutex_unlock(&sock_lock);
  766. return err;
  767. }
  768. __bpf_kfunc int bpf_kfunc_call_kernel_getpeername(struct addr_args *args)
  769. {
  770. int err;
  771. mutex_lock(&sock_lock);
  772. if (!sock) {
  773. pr_err("%s called without initializing sock", __func__);
  774. err = -EPERM;
  775. goto out;
  776. }
  777. err = kernel_getpeername(sock, (struct sockaddr *)&args->addr);
  778. if (err < 0)
  779. goto out;
  780. args->addrlen = err;
  781. err = 0;
  782. out:
  783. mutex_unlock(&sock_lock);
  784. return err;
  785. }
  786. static DEFINE_MUTEX(st_ops_mutex);
  787. static struct bpf_testmod_st_ops *st_ops;
  788. __bpf_kfunc int bpf_kfunc_st_ops_test_prologue(struct st_ops_args *args)
  789. {
  790. int ret = -1;
  791. mutex_lock(&st_ops_mutex);
  792. if (st_ops && st_ops->test_prologue)
  793. ret = st_ops->test_prologue(args);
  794. mutex_unlock(&st_ops_mutex);
  795. return ret;
  796. }
  797. __bpf_kfunc int bpf_kfunc_st_ops_test_epilogue(struct st_ops_args *args)
  798. {
  799. int ret = -1;
  800. mutex_lock(&st_ops_mutex);
  801. if (st_ops && st_ops->test_epilogue)
  802. ret = st_ops->test_epilogue(args);
  803. mutex_unlock(&st_ops_mutex);
  804. return ret;
  805. }
  806. __bpf_kfunc int bpf_kfunc_st_ops_test_pro_epilogue(struct st_ops_args *args)
  807. {
  808. int ret = -1;
  809. mutex_lock(&st_ops_mutex);
  810. if (st_ops && st_ops->test_pro_epilogue)
  811. ret = st_ops->test_pro_epilogue(args);
  812. mutex_unlock(&st_ops_mutex);
  813. return ret;
  814. }
  815. __bpf_kfunc int bpf_kfunc_st_ops_inc10(struct st_ops_args *args)
  816. {
  817. args->a += 10;
  818. return args->a;
  819. }
  820. BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids)
  821. BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
  822. BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
  823. BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
  824. BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
  825. BTF_ID_FLAGS(func, bpf_kfunc_call_test4)
  826. BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
  827. BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
  828. BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
  829. BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
  830. BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
  831. BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
  832. BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL)
  833. BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL)
  834. BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL)
  835. BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)
  836. BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
  837. BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
  838. BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
  839. BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
  840. BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
  841. BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
  842. BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU)
  843. BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
  844. BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
  845. BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
  846. BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE)
  847. BTF_ID_FLAGS(func, bpf_kfunc_init_sock, KF_SLEEPABLE)
  848. BTF_ID_FLAGS(func, bpf_kfunc_close_sock, KF_SLEEPABLE)
  849. BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_connect, KF_SLEEPABLE)
  850. BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_bind, KF_SLEEPABLE)
  851. BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_listen, KF_SLEEPABLE)
  852. BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_sendmsg, KF_SLEEPABLE)
  853. BTF_ID_FLAGS(func, bpf_kfunc_call_sock_sendmsg, KF_SLEEPABLE)
  854. BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getsockname, KF_SLEEPABLE)
  855. BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getpeername, KF_SLEEPABLE)
  856. BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_prologue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
  857. BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
  858. BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_pro_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
  859. BTF_ID_FLAGS(func, bpf_kfunc_st_ops_inc10, KF_TRUSTED_ARGS)
  860. BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
  861. static int bpf_testmod_ops_init(struct btf *btf)
  862. {
  863. return 0;
  864. }
  865. static bool bpf_testmod_ops_is_valid_access(int off, int size,
  866. enum bpf_access_type type,
  867. const struct bpf_prog *prog,
  868. struct bpf_insn_access_aux *info)
  869. {
  870. return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
  871. }
  872. static int bpf_testmod_ops_init_member(const struct btf_type *t,
  873. const struct btf_member *member,
  874. void *kdata, const void *udata)
  875. {
  876. if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) {
  877. /* For data fields, this function has to copy it and return
  878. * 1 to indicate that the data has been handled by the
  879. * struct_ops type, or the verifier will reject the map if
  880. * the value of the data field is not zero.
  881. */
  882. ((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
  883. return 1;
  884. }
  885. return 0;
  886. }
  887. static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
  888. .owner = THIS_MODULE,
  889. .set = &bpf_testmod_check_kfunc_ids,
  890. };
  891. static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
  892. .is_valid_access = bpf_testmod_ops_is_valid_access,
  893. };
  894. static int bpf_dummy_reg(void *kdata, struct bpf_link *link)
  895. {
  896. struct bpf_testmod_ops *ops = kdata;
  897. if (ops->test_1)
  898. ops->test_1();
  899. /* Some test cases (ex. struct_ops_maybe_null) may not have test_2
  900. * initialized, so we need to check for NULL.
  901. */
  902. if (ops->test_2)
  903. ops->test_2(4, ops->data);
  904. return 0;
  905. }
  906. static void bpf_dummy_unreg(void *kdata, struct bpf_link *link)
  907. {
  908. }
  909. static int bpf_testmod_test_1(void)
  910. {
  911. return 0;
  912. }
  913. static void bpf_testmod_test_2(int a, int b)
  914. {
  915. }
  916. static int bpf_testmod_tramp(int value)
  917. {
  918. return 0;
  919. }
  920. static int bpf_testmod_ops__test_maybe_null(int dummy,
  921. struct task_struct *task__nullable)
  922. {
  923. return 0;
  924. }
  925. static struct bpf_testmod_ops __bpf_testmod_ops = {
  926. .test_1 = bpf_testmod_test_1,
  927. .test_2 = bpf_testmod_test_2,
  928. .test_maybe_null = bpf_testmod_ops__test_maybe_null,
  929. };
  930. struct bpf_struct_ops bpf_bpf_testmod_ops = {
  931. .verifier_ops = &bpf_testmod_verifier_ops,
  932. .init = bpf_testmod_ops_init,
  933. .init_member = bpf_testmod_ops_init_member,
  934. .reg = bpf_dummy_reg,
  935. .unreg = bpf_dummy_unreg,
  936. .cfi_stubs = &__bpf_testmod_ops,
  937. .name = "bpf_testmod_ops",
  938. .owner = THIS_MODULE,
  939. };
  940. static int bpf_dummy_reg2(void *kdata, struct bpf_link *link)
  941. {
  942. struct bpf_testmod_ops2 *ops = kdata;
  943. ops->test_1();
  944. return 0;
  945. }
  946. static struct bpf_testmod_ops2 __bpf_testmod_ops2 = {
  947. .test_1 = bpf_testmod_test_1,
  948. };
  949. struct bpf_struct_ops bpf_testmod_ops2 = {
  950. .verifier_ops = &bpf_testmod_verifier_ops,
  951. .init = bpf_testmod_ops_init,
  952. .init_member = bpf_testmod_ops_init_member,
  953. .reg = bpf_dummy_reg2,
  954. .unreg = bpf_dummy_unreg,
  955. .cfi_stubs = &__bpf_testmod_ops2,
  956. .name = "bpf_testmod_ops2",
  957. .owner = THIS_MODULE,
  958. };
  959. static int bpf_test_mod_st_ops__test_prologue(struct st_ops_args *args)
  960. {
  961. return 0;
  962. }
  963. static int bpf_test_mod_st_ops__test_epilogue(struct st_ops_args *args)
  964. {
  965. return 0;
  966. }
  967. static int bpf_test_mod_st_ops__test_pro_epilogue(struct st_ops_args *args)
  968. {
  969. return 0;
  970. }
  971. static int st_ops_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
  972. const struct bpf_prog *prog)
  973. {
  974. struct bpf_insn *insn = insn_buf;
  975. if (strcmp(prog->aux->attach_func_name, "test_prologue") &&
  976. strcmp(prog->aux->attach_func_name, "test_pro_epilogue"))
  977. return 0;
  978. /* r6 = r1[0]; // r6 will be "struct st_ops *args". r1 is "u64 *ctx".
  979. * r7 = r6->a;
  980. * r7 += 1000;
  981. * r6->a = r7;
  982. */
  983. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0);
  984. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, offsetof(struct st_ops_args, a));
  985. *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 1000);
  986. *insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_7, offsetof(struct st_ops_args, a));
  987. *insn++ = prog->insnsi[0];
  988. return insn - insn_buf;
  989. }
  990. static int st_ops_gen_epilogue(struct bpf_insn *insn_buf, const struct bpf_prog *prog,
  991. s16 ctx_stack_off)
  992. {
  993. struct bpf_insn *insn = insn_buf;
  994. if (strcmp(prog->aux->attach_func_name, "test_epilogue") &&
  995. strcmp(prog->aux->attach_func_name, "test_pro_epilogue"))
  996. return 0;
  997. /* r1 = stack[ctx_stack_off]; // r1 will be "u64 *ctx"
  998. * r1 = r1[0]; // r1 will be "struct st_ops *args"
  999. * r6 = r1->a;
  1000. * r6 += 10000;
  1001. * r1->a = r6;
  1002. * r0 = r6;
  1003. * r0 *= 2;
  1004. * BPF_EXIT;
  1005. */
  1006. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_FP, ctx_stack_off);
  1007. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
  1008. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct st_ops_args, a));
  1009. *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 10000);
  1010. *insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, offsetof(struct st_ops_args, a));
  1011. *insn++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_6);
  1012. *insn++ = BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, 2);
  1013. *insn++ = BPF_EXIT_INSN();
  1014. return insn - insn_buf;
  1015. }
  1016. static int st_ops_btf_struct_access(struct bpf_verifier_log *log,
  1017. const struct bpf_reg_state *reg,
  1018. int off, int size)
  1019. {
  1020. if (off < 0 || off + size > sizeof(struct st_ops_args))
  1021. return -EACCES;
  1022. return 0;
  1023. }
  1024. static const struct bpf_verifier_ops st_ops_verifier_ops = {
  1025. .is_valid_access = bpf_testmod_ops_is_valid_access,
  1026. .btf_struct_access = st_ops_btf_struct_access,
  1027. .gen_prologue = st_ops_gen_prologue,
  1028. .gen_epilogue = st_ops_gen_epilogue,
  1029. .get_func_proto = bpf_base_func_proto,
  1030. };
  1031. static struct bpf_testmod_st_ops st_ops_cfi_stubs = {
  1032. .test_prologue = bpf_test_mod_st_ops__test_prologue,
  1033. .test_epilogue = bpf_test_mod_st_ops__test_epilogue,
  1034. .test_pro_epilogue = bpf_test_mod_st_ops__test_pro_epilogue,
  1035. };
  1036. static int st_ops_reg(void *kdata, struct bpf_link *link)
  1037. {
  1038. int err = 0;
  1039. mutex_lock(&st_ops_mutex);
  1040. if (st_ops) {
  1041. pr_err("st_ops has already been registered\n");
  1042. err = -EEXIST;
  1043. goto unlock;
  1044. }
  1045. st_ops = kdata;
  1046. unlock:
  1047. mutex_unlock(&st_ops_mutex);
  1048. return err;
  1049. }
  1050. static void st_ops_unreg(void *kdata, struct bpf_link *link)
  1051. {
  1052. mutex_lock(&st_ops_mutex);
  1053. st_ops = NULL;
  1054. mutex_unlock(&st_ops_mutex);
  1055. }
  1056. static int st_ops_init(struct btf *btf)
  1057. {
  1058. return 0;
  1059. }
  1060. static int st_ops_init_member(const struct btf_type *t,
  1061. const struct btf_member *member,
  1062. void *kdata, const void *udata)
  1063. {
  1064. return 0;
  1065. }
  1066. static struct bpf_struct_ops testmod_st_ops = {
  1067. .verifier_ops = &st_ops_verifier_ops,
  1068. .init = st_ops_init,
  1069. .init_member = st_ops_init_member,
  1070. .reg = st_ops_reg,
  1071. .unreg = st_ops_unreg,
  1072. .cfi_stubs = &st_ops_cfi_stubs,
  1073. .name = "bpf_testmod_st_ops",
  1074. .owner = THIS_MODULE,
  1075. };
  1076. extern int bpf_fentry_test1(int a);
  1077. static int bpf_testmod_init(void)
  1078. {
  1079. const struct btf_id_dtor_kfunc bpf_testmod_dtors[] = {
  1080. {
  1081. .btf_id = bpf_testmod_dtor_ids[0],
  1082. .kfunc_btf_id = bpf_testmod_dtor_ids[1]
  1083. },
  1084. };
  1085. void **tramp;
  1086. int ret;
  1087. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_testmod_common_kfunc_set);
  1088. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
  1089. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set);
  1090. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set);
  1091. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_testmod_kfunc_set);
  1092. ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops);
  1093. ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2);
  1094. ret = ret ?: register_bpf_struct_ops(&testmod_st_ops, bpf_testmod_st_ops);
  1095. ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
  1096. ARRAY_SIZE(bpf_testmod_dtors),
  1097. THIS_MODULE);
  1098. if (ret < 0)
  1099. return ret;
  1100. if (bpf_fentry_test1(0) < 0)
  1101. return -EINVAL;
  1102. sock = NULL;
  1103. mutex_init(&sock_lock);
  1104. ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
  1105. if (ret < 0)
  1106. return ret;
  1107. ret = register_bpf_testmod_uprobe();
  1108. if (ret < 0)
  1109. return ret;
  1110. /* Ensure nothing is between tramp_1..tramp_40 */
  1111. BUILD_BUG_ON(offsetof(struct bpf_testmod_ops, tramp_1) + 40 * sizeof(long) !=
  1112. offsetofend(struct bpf_testmod_ops, tramp_40));
  1113. tramp = (void **)&__bpf_testmod_ops.tramp_1;
  1114. while (tramp <= (void **)&__bpf_testmod_ops.tramp_40)
  1115. *tramp++ = bpf_testmod_tramp;
  1116. return 0;
  1117. }
  1118. static void bpf_testmod_exit(void)
  1119. {
  1120. /* Need to wait for all references to be dropped because
  1121. * bpf_kfunc_call_test_release() which currently resides in kernel can
  1122. * be called after bpf_testmod is unloaded. Once release function is
  1123. * moved into the module this wait can be removed.
  1124. */
  1125. while (refcount_read(&prog_test_struct.cnt) > 1)
  1126. msleep(20);
  1127. bpf_kfunc_close_sock();
  1128. sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
  1129. unregister_bpf_testmod_uprobe();
  1130. }
  1131. module_init(bpf_testmod_init);
  1132. module_exit(bpf_testmod_exit);
  1133. MODULE_AUTHOR("Andrii Nakryiko");
  1134. MODULE_DESCRIPTION("BPF selftests module");
  1135. MODULE_LICENSE("Dual BSD/GPL");