test_verifier.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Testsuite for eBPF verifier
  4. *
  5. * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
  6. * Copyright (c) 2017 Facebook
  7. * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
  8. */
  9. #include <endian.h>
  10. #include <asm/types.h>
  11. #include <linux/types.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <stddef.h>
  19. #include <stdbool.h>
  20. #include <sched.h>
  21. #include <limits.h>
  22. #include <assert.h>
  23. #include <linux/unistd.h>
  24. #include <linux/filter.h>
  25. #include <linux/bpf_perf_event.h>
  26. #include <linux/bpf.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/btf.h>
  29. #include <bpf/btf.h>
  30. #include <bpf/bpf.h>
  31. #include <bpf/libbpf.h>
  32. #include "autoconf_helper.h"
  33. #include "unpriv_helpers.h"
  34. #include "cap_helpers.h"
  35. #include "bpf_rand.h"
  36. #include "bpf_util.h"
  37. #include "test_btf.h"
  38. #include "../../../include/linux/filter.h"
  39. #include "testing_helpers.h"
  40. #ifndef ENOTSUPP
  41. #define ENOTSUPP 524
  42. #endif
  43. #define MAX_INSNS BPF_MAXINSNS
  44. #define MAX_EXPECTED_INSNS 32
  45. #define MAX_UNEXPECTED_INSNS 32
  46. #define MAX_TEST_INSNS 1000000
  47. #define MAX_FIXUPS 8
  48. #define MAX_NR_MAPS 23
  49. #define MAX_TEST_RUNS 8
  50. #define POINTER_VALUE 0xcafe4all
  51. #define TEST_DATA_LEN 64
  52. #define MAX_FUNC_INFOS 8
  53. #define MAX_BTF_STRINGS 256
  54. #define MAX_BTF_TYPES 256
  55. #define INSN_OFF_MASK ((__s16)0xFFFF)
  56. #define INSN_IMM_MASK ((__s32)0xFFFFFFFF)
  57. #define SKIP_INSNS() BPF_RAW_INSN(0xde, 0xa, 0xd, 0xbeef, 0xdeadbeef)
  58. #define DEFAULT_LIBBPF_LOG_LEVEL 4
  59. #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0)
  60. #define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1)
  61. #define F_NEEDS_JIT_ENABLED (1 << 2)
  62. /* need CAP_BPF, CAP_NET_ADMIN, CAP_PERFMON to load progs */
  63. #define ADMIN_CAPS (1ULL << CAP_NET_ADMIN | \
  64. 1ULL << CAP_PERFMON | \
  65. 1ULL << CAP_BPF)
  66. #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"
  67. static bool unpriv_disabled = false;
  68. static bool jit_disabled;
  69. static int skips;
  70. static bool verbose = false;
  71. static int verif_log_level = 0;
  72. struct kfunc_btf_id_pair {
  73. const char *kfunc;
  74. int insn_idx;
  75. };
  76. struct bpf_test {
  77. const char *descr;
  78. struct bpf_insn insns[MAX_INSNS];
  79. struct bpf_insn *fill_insns;
  80. /* If specified, test engine looks for this sequence of
  81. * instructions in the BPF program after loading. Allows to
  82. * test rewrites applied by verifier. Use values
  83. * INSN_OFF_MASK and INSN_IMM_MASK to mask `off` and `imm`
  84. * fields if content does not matter. The test case fails if
  85. * specified instructions are not found.
  86. *
  87. * The sequence could be split into sub-sequences by adding
  88. * SKIP_INSNS instruction at the end of each sub-sequence. In
  89. * such case sub-sequences are searched for one after another.
  90. */
  91. struct bpf_insn expected_insns[MAX_EXPECTED_INSNS];
  92. /* If specified, test engine applies same pattern matching
  93. * logic as for `expected_insns`. If the specified pattern is
  94. * matched test case is marked as failed.
  95. */
  96. struct bpf_insn unexpected_insns[MAX_UNEXPECTED_INSNS];
  97. int fixup_map_hash_8b[MAX_FIXUPS];
  98. int fixup_map_hash_48b[MAX_FIXUPS];
  99. int fixup_map_hash_16b[MAX_FIXUPS];
  100. int fixup_map_array_48b[MAX_FIXUPS];
  101. int fixup_map_sockmap[MAX_FIXUPS];
  102. int fixup_map_sockhash[MAX_FIXUPS];
  103. int fixup_map_xskmap[MAX_FIXUPS];
  104. int fixup_map_stacktrace[MAX_FIXUPS];
  105. int fixup_prog1[MAX_FIXUPS];
  106. int fixup_prog2[MAX_FIXUPS];
  107. int fixup_map_in_map[MAX_FIXUPS];
  108. int fixup_cgroup_storage[MAX_FIXUPS];
  109. int fixup_percpu_cgroup_storage[MAX_FIXUPS];
  110. int fixup_map_spin_lock[MAX_FIXUPS];
  111. int fixup_map_array_ro[MAX_FIXUPS];
  112. int fixup_map_array_wo[MAX_FIXUPS];
  113. int fixup_map_array_small[MAX_FIXUPS];
  114. int fixup_sk_storage_map[MAX_FIXUPS];
  115. int fixup_map_event_output[MAX_FIXUPS];
  116. int fixup_map_reuseport_array[MAX_FIXUPS];
  117. int fixup_map_ringbuf[MAX_FIXUPS];
  118. int fixup_map_timer[MAX_FIXUPS];
  119. int fixup_map_kptr[MAX_FIXUPS];
  120. struct kfunc_btf_id_pair fixup_kfunc_btf_id[MAX_FIXUPS];
  121. /* Expected verifier log output for result REJECT or VERBOSE_ACCEPT.
  122. * Can be a tab-separated sequence of expected strings. An empty string
  123. * means no log verification.
  124. */
  125. const char *errstr;
  126. const char *errstr_unpriv;
  127. uint32_t insn_processed;
  128. int prog_len;
  129. enum {
  130. UNDEF,
  131. ACCEPT,
  132. REJECT,
  133. VERBOSE_ACCEPT,
  134. } result, result_unpriv;
  135. enum bpf_prog_type prog_type;
  136. uint8_t flags;
  137. void (*fill_helper)(struct bpf_test *self);
  138. int runs;
  139. #define bpf_testdata_struct_t \
  140. struct { \
  141. uint32_t retval, retval_unpriv; \
  142. union { \
  143. __u8 data[TEST_DATA_LEN]; \
  144. __u64 data64[TEST_DATA_LEN / 8]; \
  145. }; \
  146. }
  147. union {
  148. bpf_testdata_struct_t;
  149. bpf_testdata_struct_t retvals[MAX_TEST_RUNS];
  150. };
  151. enum bpf_attach_type expected_attach_type;
  152. const char *kfunc;
  153. struct bpf_func_info func_info[MAX_FUNC_INFOS];
  154. int func_info_cnt;
  155. char btf_strings[MAX_BTF_STRINGS];
  156. /* A set of BTF types to load when specified,
  157. * use macro definitions from test_btf.h,
  158. * must end with BTF_END_RAW
  159. */
  160. __u32 btf_types[MAX_BTF_TYPES];
  161. };
  162. /* Note we want this to be 64 bit aligned so that the end of our array is
  163. * actually the end of the structure.
  164. */
  165. #define MAX_ENTRIES 11
  166. struct test_val {
  167. unsigned int index;
  168. int foo[MAX_ENTRIES];
  169. };
  170. struct other_val {
  171. long long foo;
  172. long long bar;
  173. };
  174. static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self)
  175. {
  176. /* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */
  177. #define PUSH_CNT 51
  178. /* jump range is limited to 16 bit. PUSH_CNT of ld_abs needs room */
  179. unsigned int len = (1 << 15) - PUSH_CNT * 2 * 5 * 6;
  180. struct bpf_insn *insn = self->fill_insns;
  181. int i = 0, j, k = 0;
  182. insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  183. loop:
  184. for (j = 0; j < PUSH_CNT; j++) {
  185. insn[i++] = BPF_LD_ABS(BPF_B, 0);
  186. /* jump to error label */
  187. insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3);
  188. i++;
  189. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
  190. insn[i++] = BPF_MOV64_IMM(BPF_REG_2, 1);
  191. insn[i++] = BPF_MOV64_IMM(BPF_REG_3, 2);
  192. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  193. BPF_FUNC_skb_vlan_push);
  194. insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3);
  195. i++;
  196. }
  197. for (j = 0; j < PUSH_CNT; j++) {
  198. insn[i++] = BPF_LD_ABS(BPF_B, 0);
  199. insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3);
  200. i++;
  201. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
  202. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  203. BPF_FUNC_skb_vlan_pop);
  204. insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3);
  205. i++;
  206. }
  207. if (++k < 5)
  208. goto loop;
  209. for (; i < len - 3; i++)
  210. insn[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0xbef);
  211. insn[len - 3] = BPF_JMP_A(1);
  212. /* error label */
  213. insn[len - 2] = BPF_MOV32_IMM(BPF_REG_0, 0);
  214. insn[len - 1] = BPF_EXIT_INSN();
  215. self->prog_len = len;
  216. }
  217. static void bpf_fill_jump_around_ld_abs(struct bpf_test *self)
  218. {
  219. struct bpf_insn *insn = self->fill_insns;
  220. /* jump range is limited to 16 bit. every ld_abs is replaced by 6 insns,
  221. * but on arches like arm, ppc etc, there will be one BPF_ZEXT inserted
  222. * to extend the error value of the inlined ld_abs sequence which then
  223. * contains 7 insns. so, set the dividend to 7 so the testcase could
  224. * work on all arches.
  225. */
  226. unsigned int len = (1 << 15) / 7;
  227. int i = 0;
  228. insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  229. insn[i++] = BPF_LD_ABS(BPF_B, 0);
  230. insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2);
  231. i++;
  232. while (i < len - 1)
  233. insn[i++] = BPF_LD_ABS(BPF_B, 1);
  234. insn[i] = BPF_EXIT_INSN();
  235. self->prog_len = i + 1;
  236. }
  237. static void bpf_fill_rand_ld_dw(struct bpf_test *self)
  238. {
  239. struct bpf_insn *insn = self->fill_insns;
  240. uint64_t res = 0;
  241. int i = 0;
  242. insn[i++] = BPF_MOV32_IMM(BPF_REG_0, 0);
  243. while (i < self->retval) {
  244. uint64_t val = bpf_semi_rand_get();
  245. struct bpf_insn tmp[2] = { BPF_LD_IMM64(BPF_REG_1, val) };
  246. res ^= val;
  247. insn[i++] = tmp[0];
  248. insn[i++] = tmp[1];
  249. insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
  250. }
  251. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
  252. insn[i++] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32);
  253. insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
  254. insn[i] = BPF_EXIT_INSN();
  255. self->prog_len = i + 1;
  256. res ^= (res >> 32);
  257. self->retval = (uint32_t)res;
  258. }
  259. #define MAX_JMP_SEQ 8192
  260. /* test the sequence of 8k jumps */
  261. static void bpf_fill_scale1(struct bpf_test *self)
  262. {
  263. struct bpf_insn *insn = self->fill_insns;
  264. int i = 0, k = 0;
  265. insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  266. /* test to check that the long sequence of jumps is acceptable */
  267. while (k++ < MAX_JMP_SEQ) {
  268. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  269. BPF_FUNC_get_prandom_u32);
  270. insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2);
  271. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10);
  272. insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6,
  273. -8 * (k % 64 + 1));
  274. }
  275. /* is_state_visited() doesn't allocate state for pruning for every jump.
  276. * Hence multiply jmps by 4 to accommodate that heuristic
  277. */
  278. while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4)
  279. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42);
  280. insn[i] = BPF_EXIT_INSN();
  281. self->prog_len = i + 1;
  282. self->retval = 42;
  283. }
  284. /* test the sequence of 8k jumps in inner most function (function depth 8)*/
  285. static void bpf_fill_scale2(struct bpf_test *self)
  286. {
  287. struct bpf_insn *insn = self->fill_insns;
  288. int i = 0, k = 0;
  289. #define FUNC_NEST 7
  290. for (k = 0; k < FUNC_NEST; k++) {
  291. insn[i++] = BPF_CALL_REL(1);
  292. insn[i++] = BPF_EXIT_INSN();
  293. }
  294. insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  295. /* test to check that the long sequence of jumps is acceptable */
  296. k = 0;
  297. while (k++ < MAX_JMP_SEQ) {
  298. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  299. BPF_FUNC_get_prandom_u32);
  300. insn[i++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, bpf_semi_rand_get(), 2);
  301. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_10);
  302. insn[i++] = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6,
  303. -8 * (k % (64 - 4 * FUNC_NEST) + 1));
  304. }
  305. while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4)
  306. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 42);
  307. insn[i] = BPF_EXIT_INSN();
  308. self->prog_len = i + 1;
  309. self->retval = 42;
  310. }
  311. static void bpf_fill_scale(struct bpf_test *self)
  312. {
  313. switch (self->retval) {
  314. case 1:
  315. return bpf_fill_scale1(self);
  316. case 2:
  317. return bpf_fill_scale2(self);
  318. default:
  319. self->prog_len = 0;
  320. break;
  321. }
  322. }
  323. static int bpf_fill_torturous_jumps_insn_1(struct bpf_insn *insn)
  324. {
  325. unsigned int len = 259, hlen = 128;
  326. int i;
  327. insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32);
  328. for (i = 1; i <= hlen; i++) {
  329. insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, hlen);
  330. insn[i + hlen] = BPF_JMP_A(hlen - i);
  331. }
  332. insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 1);
  333. insn[len - 1] = BPF_EXIT_INSN();
  334. return len;
  335. }
  336. static int bpf_fill_torturous_jumps_insn_2(struct bpf_insn *insn)
  337. {
  338. unsigned int len = 4100, jmp_off = 2048;
  339. int i, j;
  340. insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32);
  341. for (i = 1; i <= jmp_off; i++) {
  342. insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, jmp_off);
  343. }
  344. insn[i++] = BPF_JMP_A(jmp_off);
  345. for (; i <= jmp_off * 2 + 1; i+=16) {
  346. for (j = 0; j < 16; j++) {
  347. insn[i + j] = BPF_JMP_A(16 - j - 1);
  348. }
  349. }
  350. insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 2);
  351. insn[len - 1] = BPF_EXIT_INSN();
  352. return len;
  353. }
  354. static void bpf_fill_torturous_jumps(struct bpf_test *self)
  355. {
  356. struct bpf_insn *insn = self->fill_insns;
  357. int i = 0;
  358. switch (self->retval) {
  359. case 1:
  360. self->prog_len = bpf_fill_torturous_jumps_insn_1(insn);
  361. return;
  362. case 2:
  363. self->prog_len = bpf_fill_torturous_jumps_insn_2(insn);
  364. return;
  365. case 3:
  366. /* main */
  367. insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4);
  368. insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 262);
  369. insn[i++] = BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0);
  370. insn[i++] = BPF_MOV64_IMM(BPF_REG_0, 3);
  371. insn[i++] = BPF_EXIT_INSN();
  372. /* subprog 1 */
  373. i += bpf_fill_torturous_jumps_insn_1(insn + i);
  374. /* subprog 2 */
  375. i += bpf_fill_torturous_jumps_insn_2(insn + i);
  376. self->prog_len = i;
  377. return;
  378. default:
  379. self->prog_len = 0;
  380. break;
  381. }
  382. }
  383. static void bpf_fill_big_prog_with_loop_1(struct bpf_test *self)
  384. {
  385. struct bpf_insn *insn = self->fill_insns;
  386. /* This test was added to catch a specific use after free
  387. * error, which happened upon BPF program reallocation.
  388. * Reallocation is handled by core.c:bpf_prog_realloc, which
  389. * reuses old memory if page boundary is not crossed. The
  390. * value of `len` is chosen to cross this boundary on bpf_loop
  391. * patching.
  392. */
  393. const int len = getpagesize() - 25;
  394. int callback_load_idx;
  395. int callback_idx;
  396. int i = 0;
  397. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 1);
  398. callback_load_idx = i;
  399. insn[i++] = BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW,
  400. BPF_REG_2, BPF_PSEUDO_FUNC, 0,
  401. 777 /* filled below */);
  402. insn[i++] = BPF_RAW_INSN(0, 0, 0, 0, 0);
  403. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_3, 0);
  404. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_4, 0);
  405. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_loop);
  406. while (i < len - 3)
  407. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0);
  408. insn[i++] = BPF_EXIT_INSN();
  409. callback_idx = i;
  410. insn[i++] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0);
  411. insn[i++] = BPF_EXIT_INSN();
  412. insn[callback_load_idx].imm = callback_idx - callback_load_idx - 1;
  413. self->func_info[1].insn_off = callback_idx;
  414. self->prog_len = i;
  415. assert(i == len);
  416. }
  417. /* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */
  418. #define BPF_SK_LOOKUP(func) \
  419. /* struct bpf_sock_tuple tuple = {} */ \
  420. BPF_MOV64_IMM(BPF_REG_2, 0), \
  421. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8), \
  422. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16), \
  423. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24), \
  424. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32), \
  425. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40), \
  426. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48), \
  427. /* sk = func(ctx, &tuple, sizeof tuple, 0, 0) */ \
  428. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), \
  429. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48), \
  430. BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)), \
  431. BPF_MOV64_IMM(BPF_REG_4, 0), \
  432. BPF_MOV64_IMM(BPF_REG_5, 0), \
  433. BPF_EMIT_CALL(BPF_FUNC_ ## func)
  434. /* BPF_DIRECT_PKT_R2 contains 7 instructions, it initializes default return
  435. * value into 0 and does necessary preparation for direct packet access
  436. * through r2. The allowed access range is 8 bytes.
  437. */
  438. #define BPF_DIRECT_PKT_R2 \
  439. BPF_MOV64_IMM(BPF_REG_0, 0), \
  440. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, \
  441. offsetof(struct __sk_buff, data)), \
  442. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1, \
  443. offsetof(struct __sk_buff, data_end)), \
  444. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2), \
  445. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8), \
  446. BPF_JMP_REG(BPF_JLE, BPF_REG_4, BPF_REG_3, 1), \
  447. BPF_EXIT_INSN()
  448. /* BPF_RAND_UEXT_R7 contains 4 instructions, it initializes R7 into a random
  449. * positive u32, and zero-extend it into 64-bit.
  450. */
  451. #define BPF_RAND_UEXT_R7 \
  452. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \
  453. BPF_FUNC_get_prandom_u32), \
  454. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \
  455. BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 33), \
  456. BPF_ALU64_IMM(BPF_RSH, BPF_REG_7, 33)
  457. /* BPF_RAND_SEXT_R7 contains 5 instructions, it initializes R7 into a random
  458. * negative u32, and sign-extend it into 64-bit.
  459. */
  460. #define BPF_RAND_SEXT_R7 \
  461. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, \
  462. BPF_FUNC_get_prandom_u32), \
  463. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), \
  464. BPF_ALU64_IMM(BPF_OR, BPF_REG_7, 0x80000000), \
  465. BPF_ALU64_IMM(BPF_LSH, BPF_REG_7, 32), \
  466. BPF_ALU64_IMM(BPF_ARSH, BPF_REG_7, 32)
  467. static struct bpf_test tests[] = {
  468. #define FILL_ARRAY
  469. #include <verifier/tests.h>
  470. #undef FILL_ARRAY
  471. };
  472. static int probe_filter_length(const struct bpf_insn *fp)
  473. {
  474. int len;
  475. for (len = MAX_INSNS - 1; len > 0; --len)
  476. if (fp[len].code != 0 || fp[len].imm != 0)
  477. break;
  478. return len + 1;
  479. }
  480. static bool skip_unsupported_map(enum bpf_map_type map_type)
  481. {
  482. if (!libbpf_probe_bpf_map_type(map_type, NULL)) {
  483. printf("SKIP (unsupported map type %d)\n", map_type);
  484. skips++;
  485. return true;
  486. }
  487. return false;
  488. }
  489. static int __create_map(uint32_t type, uint32_t size_key,
  490. uint32_t size_value, uint32_t max_elem,
  491. uint32_t extra_flags)
  492. {
  493. LIBBPF_OPTS(bpf_map_create_opts, opts);
  494. int fd;
  495. opts.map_flags = (type == BPF_MAP_TYPE_HASH ? BPF_F_NO_PREALLOC : 0) | extra_flags;
  496. fd = bpf_map_create(type, NULL, size_key, size_value, max_elem, &opts);
  497. if (fd < 0) {
  498. if (skip_unsupported_map(type))
  499. return -1;
  500. printf("Failed to create hash map '%s'!\n", strerror(errno));
  501. }
  502. return fd;
  503. }
  504. static int create_map(uint32_t type, uint32_t size_key,
  505. uint32_t size_value, uint32_t max_elem)
  506. {
  507. return __create_map(type, size_key, size_value, max_elem, 0);
  508. }
  509. static void update_map(int fd, int index)
  510. {
  511. struct test_val value = {
  512. .index = (6 + 1) * sizeof(int),
  513. .foo[6] = 0xabcdef12,
  514. };
  515. assert(!bpf_map_update_elem(fd, &index, &value, 0));
  516. }
  517. static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret)
  518. {
  519. struct bpf_insn prog[] = {
  520. BPF_MOV64_IMM(BPF_REG_0, ret),
  521. BPF_EXIT_INSN(),
  522. };
  523. return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL);
  524. }
  525. static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd,
  526. int idx, int ret)
  527. {
  528. struct bpf_insn prog[] = {
  529. BPF_MOV64_IMM(BPF_REG_3, idx),
  530. BPF_LD_MAP_FD(BPF_REG_2, mfd),
  531. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  532. BPF_FUNC_tail_call),
  533. BPF_MOV64_IMM(BPF_REG_0, ret),
  534. BPF_EXIT_INSN(),
  535. };
  536. return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL);
  537. }
  538. static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem,
  539. int p1key, int p2key, int p3key)
  540. {
  541. int mfd, p1fd, p2fd, p3fd;
  542. mfd = bpf_map_create(BPF_MAP_TYPE_PROG_ARRAY, NULL, sizeof(int),
  543. sizeof(int), max_elem, NULL);
  544. if (mfd < 0) {
  545. if (skip_unsupported_map(BPF_MAP_TYPE_PROG_ARRAY))
  546. return -1;
  547. printf("Failed to create prog array '%s'!\n", strerror(errno));
  548. return -1;
  549. }
  550. p1fd = create_prog_dummy_simple(prog_type, 42);
  551. p2fd = create_prog_dummy_loop(prog_type, mfd, p2key, 41);
  552. p3fd = create_prog_dummy_simple(prog_type, 24);
  553. if (p1fd < 0 || p2fd < 0 || p3fd < 0)
  554. goto err;
  555. if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0)
  556. goto err;
  557. if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0)
  558. goto err;
  559. if (bpf_map_update_elem(mfd, &p3key, &p3fd, BPF_ANY) < 0) {
  560. err:
  561. close(mfd);
  562. mfd = -1;
  563. }
  564. close(p3fd);
  565. close(p2fd);
  566. close(p1fd);
  567. return mfd;
  568. }
  569. static int create_map_in_map(void)
  570. {
  571. LIBBPF_OPTS(bpf_map_create_opts, opts);
  572. int inner_map_fd, outer_map_fd;
  573. inner_map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int),
  574. sizeof(int), 1, NULL);
  575. if (inner_map_fd < 0) {
  576. if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY))
  577. return -1;
  578. printf("Failed to create array '%s'!\n", strerror(errno));
  579. return inner_map_fd;
  580. }
  581. opts.inner_map_fd = inner_map_fd;
  582. outer_map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, NULL,
  583. sizeof(int), sizeof(int), 1, &opts);
  584. if (outer_map_fd < 0) {
  585. if (skip_unsupported_map(BPF_MAP_TYPE_ARRAY_OF_MAPS))
  586. return -1;
  587. printf("Failed to create array of maps '%s'!\n",
  588. strerror(errno));
  589. }
  590. close(inner_map_fd);
  591. return outer_map_fd;
  592. }
  593. static int create_cgroup_storage(bool percpu)
  594. {
  595. enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE :
  596. BPF_MAP_TYPE_CGROUP_STORAGE;
  597. int fd;
  598. fd = bpf_map_create(type, NULL, sizeof(struct bpf_cgroup_storage_key),
  599. TEST_DATA_LEN, 0, NULL);
  600. if (fd < 0) {
  601. if (skip_unsupported_map(type))
  602. return -1;
  603. printf("Failed to create cgroup storage '%s'!\n",
  604. strerror(errno));
  605. }
  606. return fd;
  607. }
  608. /* struct bpf_spin_lock {
  609. * int val;
  610. * };
  611. * struct val {
  612. * int cnt;
  613. * struct bpf_spin_lock l;
  614. * };
  615. * struct bpf_timer {
  616. * __u64 :64;
  617. * __u64 :64;
  618. * } __attribute__((aligned(8)));
  619. * struct timer {
  620. * struct bpf_timer t;
  621. * };
  622. * struct btf_ptr {
  623. * struct prog_test_ref_kfunc __kptr_untrusted *ptr;
  624. * struct prog_test_ref_kfunc __kptr *ptr;
  625. * struct prog_test_member __kptr *ptr;
  626. * }
  627. */
  628. static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t"
  629. "\0btf_ptr\0prog_test_ref_kfunc\0ptr\0kptr\0kptr_untrusted"
  630. "\0prog_test_member";
  631. static __u32 btf_raw_types[] = {
  632. /* int */
  633. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  634. /* struct bpf_spin_lock */ /* [2] */
  635. BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
  636. BTF_MEMBER_ENC(15, 1, 0), /* int val; */
  637. /* struct val */ /* [3] */
  638. BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
  639. BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
  640. BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
  641. /* struct bpf_timer */ /* [4] */
  642. BTF_TYPE_ENC(25, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0), 16),
  643. /* struct timer */ /* [5] */
  644. BTF_TYPE_ENC(35, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
  645. BTF_MEMBER_ENC(41, 4, 0), /* struct bpf_timer t; */
  646. /* struct prog_test_ref_kfunc */ /* [6] */
  647. BTF_STRUCT_ENC(51, 0, 0),
  648. BTF_STRUCT_ENC(95, 0, 0), /* [7] */
  649. /* type tag "kptr_untrusted" */
  650. BTF_TYPE_TAG_ENC(80, 6), /* [8] */
  651. /* type tag "kptr" */
  652. BTF_TYPE_TAG_ENC(75, 6), /* [9] */
  653. BTF_TYPE_TAG_ENC(75, 7), /* [10] */
  654. BTF_PTR_ENC(8), /* [11] */
  655. BTF_PTR_ENC(9), /* [12] */
  656. BTF_PTR_ENC(10), /* [13] */
  657. /* struct btf_ptr */ /* [14] */
  658. BTF_STRUCT_ENC(43, 3, 24),
  659. BTF_MEMBER_ENC(71, 11, 0), /* struct prog_test_ref_kfunc __kptr_untrusted *ptr; */
  660. BTF_MEMBER_ENC(71, 12, 64), /* struct prog_test_ref_kfunc __kptr *ptr; */
  661. BTF_MEMBER_ENC(71, 13, 128), /* struct prog_test_member __kptr *ptr; */
  662. };
  663. static char bpf_vlog[UINT_MAX >> 8];
  664. static int load_btf_spec(__u32 *types, int types_len,
  665. const char *strings, int strings_len)
  666. {
  667. struct btf_header hdr = {
  668. .magic = BTF_MAGIC,
  669. .version = BTF_VERSION,
  670. .hdr_len = sizeof(struct btf_header),
  671. .type_len = types_len,
  672. .str_off = types_len,
  673. .str_len = strings_len,
  674. };
  675. void *ptr, *raw_btf;
  676. int btf_fd;
  677. LIBBPF_OPTS(bpf_btf_load_opts, opts,
  678. .log_buf = bpf_vlog,
  679. .log_size = sizeof(bpf_vlog),
  680. .log_level = (verbose
  681. ? verif_log_level
  682. : DEFAULT_LIBBPF_LOG_LEVEL),
  683. );
  684. raw_btf = malloc(sizeof(hdr) + types_len + strings_len);
  685. ptr = raw_btf;
  686. memcpy(ptr, &hdr, sizeof(hdr));
  687. ptr += sizeof(hdr);
  688. memcpy(ptr, types, hdr.type_len);
  689. ptr += hdr.type_len;
  690. memcpy(ptr, strings, hdr.str_len);
  691. ptr += hdr.str_len;
  692. btf_fd = bpf_btf_load(raw_btf, ptr - raw_btf, &opts);
  693. if (btf_fd < 0)
  694. printf("Failed to load BTF spec: '%s'\n", strerror(errno));
  695. free(raw_btf);
  696. return btf_fd < 0 ? -1 : btf_fd;
  697. }
  698. static int load_btf(void)
  699. {
  700. return load_btf_spec(btf_raw_types, sizeof(btf_raw_types),
  701. btf_str_sec, sizeof(btf_str_sec));
  702. }
  703. static int load_btf_for_test(struct bpf_test *test)
  704. {
  705. int types_num = 0;
  706. while (types_num < MAX_BTF_TYPES &&
  707. test->btf_types[types_num] != BTF_END_RAW)
  708. ++types_num;
  709. int types_len = types_num * sizeof(test->btf_types[0]);
  710. return load_btf_spec(test->btf_types, types_len,
  711. test->btf_strings, sizeof(test->btf_strings));
  712. }
  713. static int create_map_spin_lock(void)
  714. {
  715. LIBBPF_OPTS(bpf_map_create_opts, opts,
  716. .btf_key_type_id = 1,
  717. .btf_value_type_id = 3,
  718. );
  719. int fd, btf_fd;
  720. btf_fd = load_btf();
  721. if (btf_fd < 0)
  722. return -1;
  723. opts.btf_fd = btf_fd;
  724. fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 8, 1, &opts);
  725. if (fd < 0)
  726. printf("Failed to create map with spin_lock\n");
  727. return fd;
  728. }
  729. static int create_sk_storage_map(void)
  730. {
  731. LIBBPF_OPTS(bpf_map_create_opts, opts,
  732. .map_flags = BPF_F_NO_PREALLOC,
  733. .btf_key_type_id = 1,
  734. .btf_value_type_id = 3,
  735. );
  736. int fd, btf_fd;
  737. btf_fd = load_btf();
  738. if (btf_fd < 0)
  739. return -1;
  740. opts.btf_fd = btf_fd;
  741. fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "test_map", 4, 8, 0, &opts);
  742. close(opts.btf_fd);
  743. if (fd < 0)
  744. printf("Failed to create sk_storage_map\n");
  745. return fd;
  746. }
  747. static int create_map_timer(void)
  748. {
  749. LIBBPF_OPTS(bpf_map_create_opts, opts,
  750. .btf_key_type_id = 1,
  751. .btf_value_type_id = 5,
  752. );
  753. int fd, btf_fd;
  754. btf_fd = load_btf();
  755. if (btf_fd < 0)
  756. return -1;
  757. opts.btf_fd = btf_fd;
  758. fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 16, 1, &opts);
  759. if (fd < 0)
  760. printf("Failed to create map with timer\n");
  761. return fd;
  762. }
  763. static int create_map_kptr(void)
  764. {
  765. LIBBPF_OPTS(bpf_map_create_opts, opts,
  766. .btf_key_type_id = 1,
  767. .btf_value_type_id = 14,
  768. );
  769. int fd, btf_fd;
  770. btf_fd = load_btf();
  771. if (btf_fd < 0)
  772. return -1;
  773. opts.btf_fd = btf_fd;
  774. fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 24, 1, &opts);
  775. if (fd < 0)
  776. printf("Failed to create map with btf_id pointer\n");
  777. return fd;
  778. }
  779. static void set_root(bool set)
  780. {
  781. __u64 caps;
  782. if (set) {
  783. if (cap_enable_effective(1ULL << CAP_SYS_ADMIN, &caps))
  784. perror("cap_disable_effective(CAP_SYS_ADMIN)");
  785. } else {
  786. if (cap_disable_effective(1ULL << CAP_SYS_ADMIN, &caps))
  787. perror("cap_disable_effective(CAP_SYS_ADMIN)");
  788. }
  789. }
  790. static __u64 ptr_to_u64(const void *ptr)
  791. {
  792. return (uintptr_t) ptr;
  793. }
  794. static struct btf *btf__load_testmod_btf(struct btf *vmlinux)
  795. {
  796. struct bpf_btf_info info;
  797. __u32 len = sizeof(info);
  798. struct btf *btf = NULL;
  799. char name[64];
  800. __u32 id = 0;
  801. int err, fd;
  802. /* Iterate all loaded BTF objects and find bpf_testmod,
  803. * we need SYS_ADMIN cap for that.
  804. */
  805. set_root(true);
  806. while (true) {
  807. err = bpf_btf_get_next_id(id, &id);
  808. if (err) {
  809. if (errno == ENOENT)
  810. break;
  811. perror("bpf_btf_get_next_id failed");
  812. break;
  813. }
  814. fd = bpf_btf_get_fd_by_id(id);
  815. if (fd < 0) {
  816. if (errno == ENOENT)
  817. continue;
  818. perror("bpf_btf_get_fd_by_id failed");
  819. break;
  820. }
  821. memset(&info, 0, sizeof(info));
  822. info.name_len = sizeof(name);
  823. info.name = ptr_to_u64(name);
  824. len = sizeof(info);
  825. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  826. if (err) {
  827. close(fd);
  828. perror("bpf_obj_get_info_by_fd failed");
  829. break;
  830. }
  831. if (strcmp("bpf_testmod", name)) {
  832. close(fd);
  833. continue;
  834. }
  835. btf = btf__load_from_kernel_by_id_split(id, vmlinux);
  836. if (!btf) {
  837. close(fd);
  838. break;
  839. }
  840. /* We need the fd to stay open so it can be used in fd_array.
  841. * The final cleanup call to btf__free will free btf object
  842. * and close the file descriptor.
  843. */
  844. btf__set_fd(btf, fd);
  845. break;
  846. }
  847. set_root(false);
  848. return btf;
  849. }
  850. static struct btf *testmod_btf;
  851. static struct btf *vmlinux_btf;
  852. static void kfuncs_cleanup(void)
  853. {
  854. btf__free(testmod_btf);
  855. btf__free(vmlinux_btf);
  856. }
  857. static void fixup_prog_kfuncs(struct bpf_insn *prog, int *fd_array,
  858. struct kfunc_btf_id_pair *fixup_kfunc_btf_id)
  859. {
  860. /* Patch in kfunc BTF IDs */
  861. while (fixup_kfunc_btf_id->kfunc) {
  862. int btf_id = 0;
  863. /* try to find kfunc in kernel BTF */
  864. vmlinux_btf = vmlinux_btf ?: btf__load_vmlinux_btf();
  865. if (vmlinux_btf) {
  866. btf_id = btf__find_by_name_kind(vmlinux_btf,
  867. fixup_kfunc_btf_id->kfunc,
  868. BTF_KIND_FUNC);
  869. btf_id = btf_id < 0 ? 0 : btf_id;
  870. }
  871. /* kfunc not found in kernel BTF, try bpf_testmod BTF */
  872. if (!btf_id) {
  873. testmod_btf = testmod_btf ?: btf__load_testmod_btf(vmlinux_btf);
  874. if (testmod_btf) {
  875. btf_id = btf__find_by_name_kind(testmod_btf,
  876. fixup_kfunc_btf_id->kfunc,
  877. BTF_KIND_FUNC);
  878. btf_id = btf_id < 0 ? 0 : btf_id;
  879. if (btf_id) {
  880. /* We put bpf_testmod module fd into fd_array
  881. * and its index 1 into instruction 'off'.
  882. */
  883. *fd_array = btf__fd(testmod_btf);
  884. prog[fixup_kfunc_btf_id->insn_idx].off = 1;
  885. }
  886. }
  887. }
  888. prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id;
  889. fixup_kfunc_btf_id++;
  890. }
  891. }
  892. static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
  893. struct bpf_insn *prog, int *map_fds, int *fd_array)
  894. {
  895. int *fixup_map_hash_8b = test->fixup_map_hash_8b;
  896. int *fixup_map_hash_48b = test->fixup_map_hash_48b;
  897. int *fixup_map_hash_16b = test->fixup_map_hash_16b;
  898. int *fixup_map_array_48b = test->fixup_map_array_48b;
  899. int *fixup_map_sockmap = test->fixup_map_sockmap;
  900. int *fixup_map_sockhash = test->fixup_map_sockhash;
  901. int *fixup_map_xskmap = test->fixup_map_xskmap;
  902. int *fixup_map_stacktrace = test->fixup_map_stacktrace;
  903. int *fixup_prog1 = test->fixup_prog1;
  904. int *fixup_prog2 = test->fixup_prog2;
  905. int *fixup_map_in_map = test->fixup_map_in_map;
  906. int *fixup_cgroup_storage = test->fixup_cgroup_storage;
  907. int *fixup_percpu_cgroup_storage = test->fixup_percpu_cgroup_storage;
  908. int *fixup_map_spin_lock = test->fixup_map_spin_lock;
  909. int *fixup_map_array_ro = test->fixup_map_array_ro;
  910. int *fixup_map_array_wo = test->fixup_map_array_wo;
  911. int *fixup_map_array_small = test->fixup_map_array_small;
  912. int *fixup_sk_storage_map = test->fixup_sk_storage_map;
  913. int *fixup_map_event_output = test->fixup_map_event_output;
  914. int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;
  915. int *fixup_map_ringbuf = test->fixup_map_ringbuf;
  916. int *fixup_map_timer = test->fixup_map_timer;
  917. int *fixup_map_kptr = test->fixup_map_kptr;
  918. if (test->fill_helper) {
  919. test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn));
  920. test->fill_helper(test);
  921. }
  922. /* Allocating HTs with 1 elem is fine here, since we only test
  923. * for verifier and not do a runtime lookup, so the only thing
  924. * that really matters is value size in this case.
  925. */
  926. if (*fixup_map_hash_8b) {
  927. map_fds[0] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
  928. sizeof(long long), 1);
  929. do {
  930. prog[*fixup_map_hash_8b].imm = map_fds[0];
  931. fixup_map_hash_8b++;
  932. } while (*fixup_map_hash_8b);
  933. }
  934. if (*fixup_map_hash_48b) {
  935. map_fds[1] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
  936. sizeof(struct test_val), 1);
  937. do {
  938. prog[*fixup_map_hash_48b].imm = map_fds[1];
  939. fixup_map_hash_48b++;
  940. } while (*fixup_map_hash_48b);
  941. }
  942. if (*fixup_map_hash_16b) {
  943. map_fds[2] = create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
  944. sizeof(struct other_val), 1);
  945. do {
  946. prog[*fixup_map_hash_16b].imm = map_fds[2];
  947. fixup_map_hash_16b++;
  948. } while (*fixup_map_hash_16b);
  949. }
  950. if (*fixup_map_array_48b) {
  951. map_fds[3] = create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
  952. sizeof(struct test_val), 1);
  953. update_map(map_fds[3], 0);
  954. do {
  955. prog[*fixup_map_array_48b].imm = map_fds[3];
  956. fixup_map_array_48b++;
  957. } while (*fixup_map_array_48b);
  958. }
  959. if (*fixup_prog1) {
  960. map_fds[4] = create_prog_array(prog_type, 4, 0, 1, 2);
  961. do {
  962. prog[*fixup_prog1].imm = map_fds[4];
  963. fixup_prog1++;
  964. } while (*fixup_prog1);
  965. }
  966. if (*fixup_prog2) {
  967. map_fds[5] = create_prog_array(prog_type, 8, 7, 1, 2);
  968. do {
  969. prog[*fixup_prog2].imm = map_fds[5];
  970. fixup_prog2++;
  971. } while (*fixup_prog2);
  972. }
  973. if (*fixup_map_in_map) {
  974. map_fds[6] = create_map_in_map();
  975. do {
  976. prog[*fixup_map_in_map].imm = map_fds[6];
  977. fixup_map_in_map++;
  978. } while (*fixup_map_in_map);
  979. }
  980. if (*fixup_cgroup_storage) {
  981. map_fds[7] = create_cgroup_storage(false);
  982. do {
  983. prog[*fixup_cgroup_storage].imm = map_fds[7];
  984. fixup_cgroup_storage++;
  985. } while (*fixup_cgroup_storage);
  986. }
  987. if (*fixup_percpu_cgroup_storage) {
  988. map_fds[8] = create_cgroup_storage(true);
  989. do {
  990. prog[*fixup_percpu_cgroup_storage].imm = map_fds[8];
  991. fixup_percpu_cgroup_storage++;
  992. } while (*fixup_percpu_cgroup_storage);
  993. }
  994. if (*fixup_map_sockmap) {
  995. map_fds[9] = create_map(BPF_MAP_TYPE_SOCKMAP, sizeof(int),
  996. sizeof(int), 1);
  997. do {
  998. prog[*fixup_map_sockmap].imm = map_fds[9];
  999. fixup_map_sockmap++;
  1000. } while (*fixup_map_sockmap);
  1001. }
  1002. if (*fixup_map_sockhash) {
  1003. map_fds[10] = create_map(BPF_MAP_TYPE_SOCKHASH, sizeof(int),
  1004. sizeof(int), 1);
  1005. do {
  1006. prog[*fixup_map_sockhash].imm = map_fds[10];
  1007. fixup_map_sockhash++;
  1008. } while (*fixup_map_sockhash);
  1009. }
  1010. if (*fixup_map_xskmap) {
  1011. map_fds[11] = create_map(BPF_MAP_TYPE_XSKMAP, sizeof(int),
  1012. sizeof(int), 1);
  1013. do {
  1014. prog[*fixup_map_xskmap].imm = map_fds[11];
  1015. fixup_map_xskmap++;
  1016. } while (*fixup_map_xskmap);
  1017. }
  1018. if (*fixup_map_stacktrace) {
  1019. map_fds[12] = create_map(BPF_MAP_TYPE_STACK_TRACE, sizeof(u32),
  1020. sizeof(u64), 1);
  1021. do {
  1022. prog[*fixup_map_stacktrace].imm = map_fds[12];
  1023. fixup_map_stacktrace++;
  1024. } while (*fixup_map_stacktrace);
  1025. }
  1026. if (*fixup_map_spin_lock) {
  1027. map_fds[13] = create_map_spin_lock();
  1028. do {
  1029. prog[*fixup_map_spin_lock].imm = map_fds[13];
  1030. fixup_map_spin_lock++;
  1031. } while (*fixup_map_spin_lock);
  1032. }
  1033. if (*fixup_map_array_ro) {
  1034. map_fds[14] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
  1035. sizeof(struct test_val), 1,
  1036. BPF_F_RDONLY_PROG);
  1037. update_map(map_fds[14], 0);
  1038. do {
  1039. prog[*fixup_map_array_ro].imm = map_fds[14];
  1040. fixup_map_array_ro++;
  1041. } while (*fixup_map_array_ro);
  1042. }
  1043. if (*fixup_map_array_wo) {
  1044. map_fds[15] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
  1045. sizeof(struct test_val), 1,
  1046. BPF_F_WRONLY_PROG);
  1047. update_map(map_fds[15], 0);
  1048. do {
  1049. prog[*fixup_map_array_wo].imm = map_fds[15];
  1050. fixup_map_array_wo++;
  1051. } while (*fixup_map_array_wo);
  1052. }
  1053. if (*fixup_map_array_small) {
  1054. map_fds[16] = __create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
  1055. 1, 1, 0);
  1056. update_map(map_fds[16], 0);
  1057. do {
  1058. prog[*fixup_map_array_small].imm = map_fds[16];
  1059. fixup_map_array_small++;
  1060. } while (*fixup_map_array_small);
  1061. }
  1062. if (*fixup_sk_storage_map) {
  1063. map_fds[17] = create_sk_storage_map();
  1064. do {
  1065. prog[*fixup_sk_storage_map].imm = map_fds[17];
  1066. fixup_sk_storage_map++;
  1067. } while (*fixup_sk_storage_map);
  1068. }
  1069. if (*fixup_map_event_output) {
  1070. map_fds[18] = __create_map(BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  1071. sizeof(int), sizeof(int), 1, 0);
  1072. do {
  1073. prog[*fixup_map_event_output].imm = map_fds[18];
  1074. fixup_map_event_output++;
  1075. } while (*fixup_map_event_output);
  1076. }
  1077. if (*fixup_map_reuseport_array) {
  1078. map_fds[19] = __create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
  1079. sizeof(u32), sizeof(u64), 1, 0);
  1080. do {
  1081. prog[*fixup_map_reuseport_array].imm = map_fds[19];
  1082. fixup_map_reuseport_array++;
  1083. } while (*fixup_map_reuseport_array);
  1084. }
  1085. if (*fixup_map_ringbuf) {
  1086. map_fds[20] = create_map(BPF_MAP_TYPE_RINGBUF, 0,
  1087. 0, getpagesize());
  1088. do {
  1089. prog[*fixup_map_ringbuf].imm = map_fds[20];
  1090. fixup_map_ringbuf++;
  1091. } while (*fixup_map_ringbuf);
  1092. }
  1093. if (*fixup_map_timer) {
  1094. map_fds[21] = create_map_timer();
  1095. do {
  1096. prog[*fixup_map_timer].imm = map_fds[21];
  1097. fixup_map_timer++;
  1098. } while (*fixup_map_timer);
  1099. }
  1100. if (*fixup_map_kptr) {
  1101. map_fds[22] = create_map_kptr();
  1102. do {
  1103. prog[*fixup_map_kptr].imm = map_fds[22];
  1104. fixup_map_kptr++;
  1105. } while (*fixup_map_kptr);
  1106. }
  1107. fixup_prog_kfuncs(prog, fd_array, test->fixup_kfunc_btf_id);
  1108. }
  1109. static int set_admin(bool admin)
  1110. {
  1111. int err;
  1112. if (admin) {
  1113. err = cap_enable_effective(ADMIN_CAPS, NULL);
  1114. if (err)
  1115. perror("cap_enable_effective(ADMIN_CAPS)");
  1116. } else {
  1117. err = cap_disable_effective(ADMIN_CAPS, NULL);
  1118. if (err)
  1119. perror("cap_disable_effective(ADMIN_CAPS)");
  1120. }
  1121. return err;
  1122. }
  1123. static int do_prog_test_run(int fd_prog, bool unpriv, uint32_t expected_val,
  1124. void *data, size_t size_data)
  1125. {
  1126. __u8 tmp[TEST_DATA_LEN << 2];
  1127. __u32 size_tmp = sizeof(tmp);
  1128. int err, saved_errno;
  1129. LIBBPF_OPTS(bpf_test_run_opts, topts,
  1130. .data_in = data,
  1131. .data_size_in = size_data,
  1132. .data_out = tmp,
  1133. .data_size_out = size_tmp,
  1134. .repeat = 1,
  1135. );
  1136. if (unpriv)
  1137. set_admin(true);
  1138. err = bpf_prog_test_run_opts(fd_prog, &topts);
  1139. saved_errno = errno;
  1140. if (unpriv)
  1141. set_admin(false);
  1142. if (err) {
  1143. switch (saved_errno) {
  1144. case ENOTSUPP:
  1145. printf("Did not run the program (not supported) ");
  1146. return 0;
  1147. case EPERM:
  1148. if (unpriv) {
  1149. printf("Did not run the program (no permission) ");
  1150. return 0;
  1151. }
  1152. /* fallthrough; */
  1153. default:
  1154. printf("FAIL: Unexpected bpf_prog_test_run error (%s) ",
  1155. strerror(saved_errno));
  1156. return err;
  1157. }
  1158. }
  1159. if (topts.retval != expected_val && expected_val != POINTER_VALUE) {
  1160. printf("FAIL retval %d != %d ", topts.retval, expected_val);
  1161. return 1;
  1162. }
  1163. return 0;
  1164. }
  1165. /* Returns true if every part of exp (tab-separated) appears in log, in order.
  1166. *
  1167. * If exp is an empty string, returns true.
  1168. */
  1169. static bool cmp_str_seq(const char *log, const char *exp)
  1170. {
  1171. char needle[200];
  1172. const char *p, *q;
  1173. int len;
  1174. do {
  1175. if (!strlen(exp))
  1176. break;
  1177. p = strchr(exp, '\t');
  1178. if (!p)
  1179. p = exp + strlen(exp);
  1180. len = p - exp;
  1181. if (len >= sizeof(needle) || !len) {
  1182. printf("FAIL\nTestcase bug\n");
  1183. return false;
  1184. }
  1185. strncpy(needle, exp, len);
  1186. needle[len] = 0;
  1187. q = strstr(log, needle);
  1188. if (!q) {
  1189. printf("FAIL\nUnexpected verifier log!\n"
  1190. "EXP: %s\nRES:\n", needle);
  1191. return false;
  1192. }
  1193. log = q + len;
  1194. exp = p + 1;
  1195. } while (*p);
  1196. return true;
  1197. }
  1198. static bool is_null_insn(struct bpf_insn *insn)
  1199. {
  1200. struct bpf_insn null_insn = {};
  1201. return memcmp(insn, &null_insn, sizeof(null_insn)) == 0;
  1202. }
  1203. static bool is_skip_insn(struct bpf_insn *insn)
  1204. {
  1205. struct bpf_insn skip_insn = SKIP_INSNS();
  1206. return memcmp(insn, &skip_insn, sizeof(skip_insn)) == 0;
  1207. }
  1208. static int null_terminated_insn_len(struct bpf_insn *seq, int max_len)
  1209. {
  1210. int i;
  1211. for (i = 0; i < max_len; ++i) {
  1212. if (is_null_insn(&seq[i]))
  1213. return i;
  1214. }
  1215. return max_len;
  1216. }
  1217. static bool compare_masked_insn(struct bpf_insn *orig, struct bpf_insn *masked)
  1218. {
  1219. struct bpf_insn orig_masked;
  1220. memcpy(&orig_masked, orig, sizeof(orig_masked));
  1221. if (masked->imm == INSN_IMM_MASK)
  1222. orig_masked.imm = INSN_IMM_MASK;
  1223. if (masked->off == INSN_OFF_MASK)
  1224. orig_masked.off = INSN_OFF_MASK;
  1225. return memcmp(&orig_masked, masked, sizeof(orig_masked)) == 0;
  1226. }
  1227. static int find_insn_subseq(struct bpf_insn *seq, struct bpf_insn *subseq,
  1228. int seq_len, int subseq_len)
  1229. {
  1230. int i, j;
  1231. if (subseq_len > seq_len)
  1232. return -1;
  1233. for (i = 0; i < seq_len - subseq_len + 1; ++i) {
  1234. bool found = true;
  1235. for (j = 0; j < subseq_len; ++j) {
  1236. if (!compare_masked_insn(&seq[i + j], &subseq[j])) {
  1237. found = false;
  1238. break;
  1239. }
  1240. }
  1241. if (found)
  1242. return i;
  1243. }
  1244. return -1;
  1245. }
  1246. static int find_skip_insn_marker(struct bpf_insn *seq, int len)
  1247. {
  1248. int i;
  1249. for (i = 0; i < len; ++i)
  1250. if (is_skip_insn(&seq[i]))
  1251. return i;
  1252. return -1;
  1253. }
  1254. /* Return true if all sub-sequences in `subseqs` could be found in
  1255. * `seq` one after another. Sub-sequences are separated by a single
  1256. * nil instruction.
  1257. */
  1258. static bool find_all_insn_subseqs(struct bpf_insn *seq, struct bpf_insn *subseqs,
  1259. int seq_len, int max_subseqs_len)
  1260. {
  1261. int subseqs_len = null_terminated_insn_len(subseqs, max_subseqs_len);
  1262. while (subseqs_len > 0) {
  1263. int skip_idx = find_skip_insn_marker(subseqs, subseqs_len);
  1264. int cur_subseq_len = skip_idx < 0 ? subseqs_len : skip_idx;
  1265. int subseq_idx = find_insn_subseq(seq, subseqs,
  1266. seq_len, cur_subseq_len);
  1267. if (subseq_idx < 0)
  1268. return false;
  1269. seq += subseq_idx + cur_subseq_len;
  1270. seq_len -= subseq_idx + cur_subseq_len;
  1271. subseqs += cur_subseq_len + 1;
  1272. subseqs_len -= cur_subseq_len + 1;
  1273. }
  1274. return true;
  1275. }
  1276. static void print_insn(struct bpf_insn *buf, int cnt)
  1277. {
  1278. int i;
  1279. printf(" addr op d s off imm\n");
  1280. for (i = 0; i < cnt; ++i) {
  1281. struct bpf_insn *insn = &buf[i];
  1282. if (is_null_insn(insn))
  1283. break;
  1284. if (is_skip_insn(insn))
  1285. printf(" ...\n");
  1286. else
  1287. printf(" %04x: %02x %1x %x %04hx %08x\n",
  1288. i, insn->code, insn->dst_reg,
  1289. insn->src_reg, insn->off, insn->imm);
  1290. }
  1291. }
  1292. static bool check_xlated_program(struct bpf_test *test, int fd_prog)
  1293. {
  1294. struct bpf_insn *buf;
  1295. unsigned int cnt;
  1296. bool result = true;
  1297. bool check_expected = !is_null_insn(test->expected_insns);
  1298. bool check_unexpected = !is_null_insn(test->unexpected_insns);
  1299. if (!check_expected && !check_unexpected)
  1300. goto out;
  1301. if (get_xlated_program(fd_prog, &buf, &cnt)) {
  1302. printf("FAIL: can't get xlated program\n");
  1303. result = false;
  1304. goto out;
  1305. }
  1306. if (check_expected &&
  1307. !find_all_insn_subseqs(buf, test->expected_insns,
  1308. cnt, MAX_EXPECTED_INSNS)) {
  1309. printf("FAIL: can't find expected subsequence of instructions\n");
  1310. result = false;
  1311. if (verbose) {
  1312. printf("Program:\n");
  1313. print_insn(buf, cnt);
  1314. printf("Expected subsequence:\n");
  1315. print_insn(test->expected_insns, MAX_EXPECTED_INSNS);
  1316. }
  1317. }
  1318. if (check_unexpected &&
  1319. find_all_insn_subseqs(buf, test->unexpected_insns,
  1320. cnt, MAX_UNEXPECTED_INSNS)) {
  1321. printf("FAIL: found unexpected subsequence of instructions\n");
  1322. result = false;
  1323. if (verbose) {
  1324. printf("Program:\n");
  1325. print_insn(buf, cnt);
  1326. printf("Un-expected subsequence:\n");
  1327. print_insn(test->unexpected_insns, MAX_UNEXPECTED_INSNS);
  1328. }
  1329. }
  1330. free(buf);
  1331. out:
  1332. return result;
  1333. }
  1334. static void do_test_single(struct bpf_test *test, bool unpriv,
  1335. int *passes, int *errors)
  1336. {
  1337. int fd_prog, btf_fd, expected_ret, alignment_prevented_execution;
  1338. int prog_len, prog_type = test->prog_type;
  1339. struct bpf_insn *prog = test->insns;
  1340. LIBBPF_OPTS(bpf_prog_load_opts, opts);
  1341. int run_errs, run_successes;
  1342. int map_fds[MAX_NR_MAPS];
  1343. const char *expected_err;
  1344. int fd_array[2] = { -1, -1 };
  1345. int saved_errno;
  1346. int fixup_skips;
  1347. __u32 pflags;
  1348. int i, err;
  1349. if ((test->flags & F_NEEDS_JIT_ENABLED) && jit_disabled) {
  1350. printf("SKIP (requires BPF JIT)\n");
  1351. skips++;
  1352. sched_yield();
  1353. return;
  1354. }
  1355. fd_prog = -1;
  1356. for (i = 0; i < MAX_NR_MAPS; i++)
  1357. map_fds[i] = -1;
  1358. btf_fd = -1;
  1359. if (!prog_type)
  1360. prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  1361. fixup_skips = skips;
  1362. do_test_fixup(test, prog_type, prog, map_fds, &fd_array[1]);
  1363. if (test->fill_insns) {
  1364. prog = test->fill_insns;
  1365. prog_len = test->prog_len;
  1366. } else {
  1367. prog_len = probe_filter_length(prog);
  1368. }
  1369. /* If there were some map skips during fixup due to missing bpf
  1370. * features, skip this test.
  1371. */
  1372. if (fixup_skips != skips)
  1373. return;
  1374. pflags = testing_prog_flags();
  1375. if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT)
  1376. pflags |= BPF_F_STRICT_ALIGNMENT;
  1377. if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
  1378. pflags |= BPF_F_ANY_ALIGNMENT;
  1379. if (test->flags & ~3)
  1380. pflags |= test->flags;
  1381. expected_ret = unpriv && test->result_unpriv != UNDEF ?
  1382. test->result_unpriv : test->result;
  1383. expected_err = unpriv && test->errstr_unpriv ?
  1384. test->errstr_unpriv : test->errstr;
  1385. opts.expected_attach_type = test->expected_attach_type;
  1386. if (verbose)
  1387. opts.log_level = verif_log_level | 4; /* force stats */
  1388. else if (expected_ret == VERBOSE_ACCEPT)
  1389. opts.log_level = 2;
  1390. else
  1391. opts.log_level = DEFAULT_LIBBPF_LOG_LEVEL;
  1392. opts.prog_flags = pflags;
  1393. if (fd_array[1] != -1)
  1394. opts.fd_array = &fd_array[0];
  1395. if ((prog_type == BPF_PROG_TYPE_TRACING ||
  1396. prog_type == BPF_PROG_TYPE_LSM) && test->kfunc) {
  1397. int attach_btf_id;
  1398. attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
  1399. opts.expected_attach_type);
  1400. if (attach_btf_id < 0) {
  1401. printf("FAIL\nFailed to find BTF ID for '%s'!\n",
  1402. test->kfunc);
  1403. (*errors)++;
  1404. return;
  1405. }
  1406. opts.attach_btf_id = attach_btf_id;
  1407. }
  1408. if (test->btf_types[0] != 0) {
  1409. btf_fd = load_btf_for_test(test);
  1410. if (btf_fd < 0)
  1411. goto fail_log;
  1412. opts.prog_btf_fd = btf_fd;
  1413. }
  1414. if (test->func_info_cnt != 0) {
  1415. opts.func_info = test->func_info;
  1416. opts.func_info_cnt = test->func_info_cnt;
  1417. opts.func_info_rec_size = sizeof(test->func_info[0]);
  1418. }
  1419. opts.log_buf = bpf_vlog;
  1420. opts.log_size = sizeof(bpf_vlog);
  1421. fd_prog = bpf_prog_load(prog_type, NULL, "GPL", prog, prog_len, &opts);
  1422. saved_errno = errno;
  1423. /* BPF_PROG_TYPE_TRACING requires more setup and
  1424. * bpf_probe_prog_type won't give correct answer
  1425. */
  1426. if (fd_prog < 0 && prog_type != BPF_PROG_TYPE_TRACING &&
  1427. !libbpf_probe_bpf_prog_type(prog_type, NULL)) {
  1428. printf("SKIP (unsupported program type %d)\n", prog_type);
  1429. skips++;
  1430. goto close_fds;
  1431. }
  1432. if (fd_prog < 0 && saved_errno == ENOTSUPP) {
  1433. printf("SKIP (program uses an unsupported feature)\n");
  1434. skips++;
  1435. goto close_fds;
  1436. }
  1437. alignment_prevented_execution = 0;
  1438. if (expected_ret == ACCEPT || expected_ret == VERBOSE_ACCEPT) {
  1439. if (fd_prog < 0) {
  1440. printf("FAIL\nFailed to load prog '%s'!\n",
  1441. strerror(saved_errno));
  1442. goto fail_log;
  1443. }
  1444. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1445. if (fd_prog >= 0 &&
  1446. (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS))
  1447. alignment_prevented_execution = 1;
  1448. #endif
  1449. if (expected_ret == VERBOSE_ACCEPT && !cmp_str_seq(bpf_vlog, expected_err)) {
  1450. goto fail_log;
  1451. }
  1452. } else {
  1453. if (fd_prog >= 0) {
  1454. printf("FAIL\nUnexpected success to load!\n");
  1455. goto fail_log;
  1456. }
  1457. if (!expected_err || !cmp_str_seq(bpf_vlog, expected_err)) {
  1458. printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
  1459. expected_err, bpf_vlog);
  1460. goto fail_log;
  1461. }
  1462. }
  1463. if (!unpriv && test->insn_processed) {
  1464. uint32_t insn_processed;
  1465. char *proc;
  1466. proc = strstr(bpf_vlog, "processed ");
  1467. insn_processed = atoi(proc + 10);
  1468. if (test->insn_processed != insn_processed) {
  1469. printf("FAIL\nUnexpected insn_processed %u vs %u\n",
  1470. insn_processed, test->insn_processed);
  1471. goto fail_log;
  1472. }
  1473. }
  1474. if (verbose)
  1475. printf(", verifier log:\n%s", bpf_vlog);
  1476. if (!check_xlated_program(test, fd_prog))
  1477. goto fail_log;
  1478. run_errs = 0;
  1479. run_successes = 0;
  1480. if (!alignment_prevented_execution && fd_prog >= 0 && test->runs >= 0) {
  1481. uint32_t expected_val;
  1482. int i;
  1483. if (!test->runs)
  1484. test->runs = 1;
  1485. for (i = 0; i < test->runs; i++) {
  1486. if (unpriv && test->retvals[i].retval_unpriv)
  1487. expected_val = test->retvals[i].retval_unpriv;
  1488. else
  1489. expected_val = test->retvals[i].retval;
  1490. err = do_prog_test_run(fd_prog, unpriv, expected_val,
  1491. test->retvals[i].data,
  1492. sizeof(test->retvals[i].data));
  1493. if (err) {
  1494. printf("(run %d/%d) ", i + 1, test->runs);
  1495. run_errs++;
  1496. } else {
  1497. run_successes++;
  1498. }
  1499. }
  1500. }
  1501. if (!run_errs) {
  1502. (*passes)++;
  1503. if (run_successes > 1)
  1504. printf("%d cases ", run_successes);
  1505. printf("OK");
  1506. if (alignment_prevented_execution)
  1507. printf(" (NOTE: not executed due to unknown alignment)");
  1508. printf("\n");
  1509. } else {
  1510. printf("\n");
  1511. goto fail_log;
  1512. }
  1513. close_fds:
  1514. if (test->fill_insns)
  1515. free(test->fill_insns);
  1516. close(fd_prog);
  1517. close(btf_fd);
  1518. for (i = 0; i < MAX_NR_MAPS; i++)
  1519. close(map_fds[i]);
  1520. sched_yield();
  1521. return;
  1522. fail_log:
  1523. (*errors)++;
  1524. printf("%s", bpf_vlog);
  1525. goto close_fds;
  1526. }
  1527. static bool is_admin(void)
  1528. {
  1529. __u64 caps;
  1530. /* The test checks for finer cap as CAP_NET_ADMIN,
  1531. * CAP_PERFMON, and CAP_BPF instead of CAP_SYS_ADMIN.
  1532. * Thus, disable CAP_SYS_ADMIN at the beginning.
  1533. */
  1534. if (cap_disable_effective(1ULL << CAP_SYS_ADMIN, &caps)) {
  1535. perror("cap_disable_effective(CAP_SYS_ADMIN)");
  1536. return false;
  1537. }
  1538. return (caps & ADMIN_CAPS) == ADMIN_CAPS;
  1539. }
  1540. static bool test_as_unpriv(struct bpf_test *test)
  1541. {
  1542. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1543. /* Some architectures have strict alignment requirements. In
  1544. * that case, the BPF verifier detects if a program has
  1545. * unaligned accesses and rejects them. A user can pass
  1546. * BPF_F_ANY_ALIGNMENT to a program to override this
  1547. * check. That, however, will only work when a privileged user
  1548. * loads a program. An unprivileged user loading a program
  1549. * with this flag will be rejected prior entering the
  1550. * verifier.
  1551. */
  1552. if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
  1553. return false;
  1554. #endif
  1555. return !test->prog_type ||
  1556. test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
  1557. test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
  1558. }
  1559. static int do_test(bool unpriv, unsigned int from, unsigned int to)
  1560. {
  1561. int i, passes = 0, errors = 0;
  1562. /* ensure previous instance of the module is unloaded */
  1563. unload_bpf_testmod(verbose);
  1564. if (load_bpf_testmod(verbose))
  1565. return EXIT_FAILURE;
  1566. for (i = from; i < to; i++) {
  1567. struct bpf_test *test = &tests[i];
  1568. /* Program types that are not supported by non-root we
  1569. * skip right away.
  1570. */
  1571. if (test_as_unpriv(test) && unpriv_disabled) {
  1572. printf("#%d/u %s SKIP\n", i, test->descr);
  1573. skips++;
  1574. } else if (test_as_unpriv(test)) {
  1575. if (!unpriv)
  1576. set_admin(false);
  1577. printf("#%d/u %s ", i, test->descr);
  1578. do_test_single(test, true, &passes, &errors);
  1579. if (!unpriv)
  1580. set_admin(true);
  1581. }
  1582. if (unpriv) {
  1583. printf("#%d/p %s SKIP\n", i, test->descr);
  1584. skips++;
  1585. } else {
  1586. printf("#%d/p %s ", i, test->descr);
  1587. do_test_single(test, false, &passes, &errors);
  1588. }
  1589. }
  1590. unload_bpf_testmod(verbose);
  1591. kfuncs_cleanup();
  1592. printf("Summary: %d PASSED, %d SKIPPED, %d FAILED\n", passes,
  1593. skips, errors);
  1594. return errors ? EXIT_FAILURE : EXIT_SUCCESS;
  1595. }
  1596. int main(int argc, char **argv)
  1597. {
  1598. unsigned int from = 0, to = ARRAY_SIZE(tests);
  1599. bool unpriv = !is_admin();
  1600. int arg = 1;
  1601. if (argc > 1 && strcmp(argv[1], "-v") == 0) {
  1602. arg++;
  1603. verbose = true;
  1604. verif_log_level = 1;
  1605. argc--;
  1606. }
  1607. if (argc > 1 && strcmp(argv[1], "-vv") == 0) {
  1608. arg++;
  1609. verbose = true;
  1610. verif_log_level = 2;
  1611. argc--;
  1612. }
  1613. if (argc == 3) {
  1614. unsigned int l = atoi(argv[arg]);
  1615. unsigned int u = atoi(argv[arg + 1]);
  1616. if (l < to && u < to) {
  1617. from = l;
  1618. to = u + 1;
  1619. }
  1620. } else if (argc == 2) {
  1621. unsigned int t = atoi(argv[arg]);
  1622. if (t < to) {
  1623. from = t;
  1624. to = t + 1;
  1625. }
  1626. }
  1627. unpriv_disabled = get_unpriv_disabled();
  1628. if (unpriv && unpriv_disabled) {
  1629. printf("Cannot run as unprivileged user with sysctl %s.\n",
  1630. UNPRIV_SYSCTL);
  1631. return EXIT_FAILURE;
  1632. }
  1633. jit_disabled = !is_jit_enabled();
  1634. /* Use libbpf 1.0 API mode */
  1635. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  1636. bpf_semi_rand_init();
  1637. return do_test(unpriv, from, to);
  1638. }