core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /*
  2. * Linux Socket Filter - Kernel level socket filtering
  3. *
  4. * Based on the design of the Berkeley Packet Filter. The new
  5. * internal format has been designed by PLUMgrid:
  6. *
  7. * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
  8. *
  9. * Authors:
  10. *
  11. * Jay Schulist <jschlst@samba.org>
  12. * Alexei Starovoitov <ast@plumgrid.com>
  13. * Daniel Borkmann <dborkman@redhat.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. * Andi Kleen - Fix a few bad bugs and races.
  21. * Kris Katterjohn - Added many additional checks in bpf_check_classic()
  22. */
  23. #include <linux/filter.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/random.h>
  27. #include <linux/moduleloader.h>
  28. #include <linux/bpf.h>
  29. #include <linux/frame.h>
  30. #include <asm/unaligned.h>
  31. /* Registers */
  32. #define BPF_R0 regs[BPF_REG_0]
  33. #define BPF_R1 regs[BPF_REG_1]
  34. #define BPF_R2 regs[BPF_REG_2]
  35. #define BPF_R3 regs[BPF_REG_3]
  36. #define BPF_R4 regs[BPF_REG_4]
  37. #define BPF_R5 regs[BPF_REG_5]
  38. #define BPF_R6 regs[BPF_REG_6]
  39. #define BPF_R7 regs[BPF_REG_7]
  40. #define BPF_R8 regs[BPF_REG_8]
  41. #define BPF_R9 regs[BPF_REG_9]
  42. #define BPF_R10 regs[BPF_REG_10]
  43. /* Named registers */
  44. #define DST regs[insn->dst_reg]
  45. #define SRC regs[insn->src_reg]
  46. #define FP regs[BPF_REG_FP]
  47. #define ARG1 regs[BPF_REG_ARG1]
  48. #define CTX regs[BPF_REG_CTX]
  49. #define IMM insn->imm
  50. /* No hurry in this branch
  51. *
  52. * Exported for the bpf jit load helper.
  53. */
  54. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
  55. {
  56. u8 *ptr = NULL;
  57. if (k >= SKF_NET_OFF)
  58. ptr = skb_network_header(skb) + k - SKF_NET_OFF;
  59. else if (k >= SKF_LL_OFF)
  60. ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
  61. if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
  62. return ptr;
  63. return NULL;
  64. }
  65. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
  66. {
  67. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  68. gfp_extra_flags;
  69. struct bpf_prog_aux *aux;
  70. struct bpf_prog *fp;
  71. size = round_up(size, PAGE_SIZE);
  72. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  73. if (fp == NULL)
  74. return NULL;
  75. kmemcheck_annotate_bitfield(fp, meta);
  76. aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
  77. if (aux == NULL) {
  78. vfree(fp);
  79. return NULL;
  80. }
  81. fp->pages = size / PAGE_SIZE;
  82. fp->aux = aux;
  83. fp->aux->prog = fp;
  84. return fp;
  85. }
  86. EXPORT_SYMBOL_GPL(bpf_prog_alloc);
  87. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  88. gfp_t gfp_extra_flags)
  89. {
  90. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  91. gfp_extra_flags;
  92. struct bpf_prog *fp;
  93. u32 pages, delta;
  94. int ret;
  95. BUG_ON(fp_old == NULL);
  96. size = round_up(size, PAGE_SIZE);
  97. pages = size / PAGE_SIZE;
  98. if (pages <= fp_old->pages)
  99. return fp_old;
  100. delta = pages - fp_old->pages;
  101. ret = __bpf_prog_charge(fp_old->aux->user, delta);
  102. if (ret)
  103. return NULL;
  104. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  105. if (fp == NULL) {
  106. __bpf_prog_uncharge(fp_old->aux->user, delta);
  107. } else {
  108. kmemcheck_annotate_bitfield(fp, meta);
  109. memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
  110. fp->pages = pages;
  111. fp->aux->prog = fp;
  112. /* We keep fp->aux from fp_old around in the new
  113. * reallocated structure.
  114. */
  115. fp_old->aux = NULL;
  116. __bpf_prog_free(fp_old);
  117. }
  118. return fp;
  119. }
  120. void __bpf_prog_free(struct bpf_prog *fp)
  121. {
  122. kfree(fp->aux);
  123. vfree(fp);
  124. }
  125. int bpf_prog_calc_digest(struct bpf_prog *fp)
  126. {
  127. const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
  128. u32 raw_size = bpf_prog_digest_scratch_size(fp);
  129. u32 ws[SHA_WORKSPACE_WORDS];
  130. u32 i, bsize, psize, blocks;
  131. struct bpf_insn *dst;
  132. bool was_ld_map;
  133. u8 *raw, *todo;
  134. __be32 *result;
  135. __be64 *bits;
  136. raw = vmalloc(raw_size);
  137. if (!raw)
  138. return -ENOMEM;
  139. sha_init(fp->digest);
  140. memset(ws, 0, sizeof(ws));
  141. /* We need to take out the map fd for the digest calculation
  142. * since they are unstable from user space side.
  143. */
  144. dst = (void *)raw;
  145. for (i = 0, was_ld_map = false; i < fp->len; i++) {
  146. dst[i] = fp->insnsi[i];
  147. if (!was_ld_map &&
  148. dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  149. dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
  150. was_ld_map = true;
  151. dst[i].imm = 0;
  152. } else if (was_ld_map &&
  153. dst[i].code == 0 &&
  154. dst[i].dst_reg == 0 &&
  155. dst[i].src_reg == 0 &&
  156. dst[i].off == 0) {
  157. was_ld_map = false;
  158. dst[i].imm = 0;
  159. } else {
  160. was_ld_map = false;
  161. }
  162. }
  163. psize = bpf_prog_insn_size(fp);
  164. memset(&raw[psize], 0, raw_size - psize);
  165. raw[psize++] = 0x80;
  166. bsize = round_up(psize, SHA_MESSAGE_BYTES);
  167. blocks = bsize / SHA_MESSAGE_BYTES;
  168. todo = raw;
  169. if (bsize - psize >= sizeof(__be64)) {
  170. bits = (__be64 *)(todo + bsize - sizeof(__be64));
  171. } else {
  172. bits = (__be64 *)(todo + bsize + bits_offset);
  173. blocks++;
  174. }
  175. *bits = cpu_to_be64((psize - 1) << 3);
  176. while (blocks--) {
  177. sha_transform(fp->digest, todo, ws);
  178. todo += SHA_MESSAGE_BYTES;
  179. }
  180. result = (__force __be32 *)fp->digest;
  181. for (i = 0; i < SHA_DIGEST_WORDS; i++)
  182. result[i] = cpu_to_be32(fp->digest[i]);
  183. vfree(raw);
  184. return 0;
  185. }
  186. static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
  187. {
  188. return BPF_CLASS(insn->code) == BPF_JMP &&
  189. /* Call and Exit are both special jumps with no
  190. * target inside the BPF instruction image.
  191. */
  192. BPF_OP(insn->code) != BPF_CALL &&
  193. BPF_OP(insn->code) != BPF_EXIT;
  194. }
  195. static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
  196. {
  197. struct bpf_insn *insn = prog->insnsi;
  198. u32 i, insn_cnt = prog->len;
  199. for (i = 0; i < insn_cnt; i++, insn++) {
  200. if (!bpf_is_jmp_and_has_target(insn))
  201. continue;
  202. /* Adjust offset of jmps if we cross boundaries. */
  203. if (i < pos && i + insn->off + 1 > pos)
  204. insn->off += delta;
  205. else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
  206. insn->off -= delta;
  207. }
  208. }
  209. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  210. const struct bpf_insn *patch, u32 len)
  211. {
  212. u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
  213. struct bpf_prog *prog_adj;
  214. /* Since our patchlet doesn't expand the image, we're done. */
  215. if (insn_delta == 0) {
  216. memcpy(prog->insnsi + off, patch, sizeof(*patch));
  217. return prog;
  218. }
  219. insn_adj_cnt = prog->len + insn_delta;
  220. /* Several new instructions need to be inserted. Make room
  221. * for them. Likely, there's no need for a new allocation as
  222. * last page could have large enough tailroom.
  223. */
  224. prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
  225. GFP_USER);
  226. if (!prog_adj)
  227. return NULL;
  228. prog_adj->len = insn_adj_cnt;
  229. /* Patching happens in 3 steps:
  230. *
  231. * 1) Move over tail of insnsi from next instruction onwards,
  232. * so we can patch the single target insn with one or more
  233. * new ones (patching is always from 1 to n insns, n > 0).
  234. * 2) Inject new instructions at the target location.
  235. * 3) Adjust branch offsets if necessary.
  236. */
  237. insn_rest = insn_adj_cnt - off - len;
  238. memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
  239. sizeof(*patch) * insn_rest);
  240. memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
  241. bpf_adj_branches(prog_adj, off, insn_delta);
  242. return prog_adj;
  243. }
  244. #ifdef CONFIG_BPF_JIT
  245. struct bpf_binary_header *
  246. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  247. unsigned int alignment,
  248. bpf_jit_fill_hole_t bpf_fill_ill_insns)
  249. {
  250. struct bpf_binary_header *hdr;
  251. unsigned int size, hole, start;
  252. /* Most of BPF filters are really small, but if some of them
  253. * fill a page, allow at least 128 extra bytes to insert a
  254. * random section of illegal instructions.
  255. */
  256. size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
  257. hdr = module_alloc(size);
  258. if (hdr == NULL)
  259. return NULL;
  260. /* Fill space with illegal/arch-dep instructions. */
  261. bpf_fill_ill_insns(hdr, size);
  262. hdr->pages = size / PAGE_SIZE;
  263. hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
  264. PAGE_SIZE - sizeof(*hdr));
  265. start = (get_random_int() % hole) & ~(alignment - 1);
  266. /* Leave a random number of instructions before BPF code. */
  267. *image_ptr = &hdr->image[start];
  268. return hdr;
  269. }
  270. void bpf_jit_binary_free(struct bpf_binary_header *hdr)
  271. {
  272. module_memfree(hdr);
  273. }
  274. int bpf_jit_harden __read_mostly;
  275. static int bpf_jit_blind_insn(const struct bpf_insn *from,
  276. const struct bpf_insn *aux,
  277. struct bpf_insn *to_buff)
  278. {
  279. struct bpf_insn *to = to_buff;
  280. u32 imm_rnd = get_random_int();
  281. s16 off;
  282. BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
  283. BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
  284. if (from->imm == 0 &&
  285. (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
  286. from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
  287. *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
  288. goto out;
  289. }
  290. switch (from->code) {
  291. case BPF_ALU | BPF_ADD | BPF_K:
  292. case BPF_ALU | BPF_SUB | BPF_K:
  293. case BPF_ALU | BPF_AND | BPF_K:
  294. case BPF_ALU | BPF_OR | BPF_K:
  295. case BPF_ALU | BPF_XOR | BPF_K:
  296. case BPF_ALU | BPF_MUL | BPF_K:
  297. case BPF_ALU | BPF_MOV | BPF_K:
  298. case BPF_ALU | BPF_DIV | BPF_K:
  299. case BPF_ALU | BPF_MOD | BPF_K:
  300. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  301. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  302. *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
  303. break;
  304. case BPF_ALU64 | BPF_ADD | BPF_K:
  305. case BPF_ALU64 | BPF_SUB | BPF_K:
  306. case BPF_ALU64 | BPF_AND | BPF_K:
  307. case BPF_ALU64 | BPF_OR | BPF_K:
  308. case BPF_ALU64 | BPF_XOR | BPF_K:
  309. case BPF_ALU64 | BPF_MUL | BPF_K:
  310. case BPF_ALU64 | BPF_MOV | BPF_K:
  311. case BPF_ALU64 | BPF_DIV | BPF_K:
  312. case BPF_ALU64 | BPF_MOD | BPF_K:
  313. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  314. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  315. *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
  316. break;
  317. case BPF_JMP | BPF_JEQ | BPF_K:
  318. case BPF_JMP | BPF_JNE | BPF_K:
  319. case BPF_JMP | BPF_JGT | BPF_K:
  320. case BPF_JMP | BPF_JGE | BPF_K:
  321. case BPF_JMP | BPF_JSGT | BPF_K:
  322. case BPF_JMP | BPF_JSGE | BPF_K:
  323. case BPF_JMP | BPF_JSET | BPF_K:
  324. /* Accommodate for extra offset in case of a backjump. */
  325. off = from->off;
  326. if (off < 0)
  327. off -= 2;
  328. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  329. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  330. *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
  331. break;
  332. case BPF_LD | BPF_ABS | BPF_W:
  333. case BPF_LD | BPF_ABS | BPF_H:
  334. case BPF_LD | BPF_ABS | BPF_B:
  335. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  336. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  337. *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
  338. break;
  339. case BPF_LD | BPF_IND | BPF_W:
  340. case BPF_LD | BPF_IND | BPF_H:
  341. case BPF_LD | BPF_IND | BPF_B:
  342. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  343. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  344. *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
  345. *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
  346. break;
  347. case BPF_LD | BPF_IMM | BPF_DW:
  348. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
  349. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  350. *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
  351. *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
  352. break;
  353. case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
  354. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
  355. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  356. *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
  357. break;
  358. case BPF_ST | BPF_MEM | BPF_DW:
  359. case BPF_ST | BPF_MEM | BPF_W:
  360. case BPF_ST | BPF_MEM | BPF_H:
  361. case BPF_ST | BPF_MEM | BPF_B:
  362. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  363. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  364. *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
  365. break;
  366. }
  367. out:
  368. return to - to_buff;
  369. }
  370. static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
  371. gfp_t gfp_extra_flags)
  372. {
  373. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  374. gfp_extra_flags;
  375. struct bpf_prog *fp;
  376. fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
  377. if (fp != NULL) {
  378. kmemcheck_annotate_bitfield(fp, meta);
  379. /* aux->prog still points to the fp_other one, so
  380. * when promoting the clone to the real program,
  381. * this still needs to be adapted.
  382. */
  383. memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
  384. }
  385. return fp;
  386. }
  387. static void bpf_prog_clone_free(struct bpf_prog *fp)
  388. {
  389. /* aux was stolen by the other clone, so we cannot free
  390. * it from this path! It will be freed eventually by the
  391. * other program on release.
  392. *
  393. * At this point, we don't need a deferred release since
  394. * clone is guaranteed to not be locked.
  395. */
  396. fp->aux = NULL;
  397. __bpf_prog_free(fp);
  398. }
  399. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
  400. {
  401. /* We have to repoint aux->prog to self, as we don't
  402. * know whether fp here is the clone or the original.
  403. */
  404. fp->aux->prog = fp;
  405. bpf_prog_clone_free(fp_other);
  406. }
  407. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
  408. {
  409. struct bpf_insn insn_buff[16], aux[2];
  410. struct bpf_prog *clone, *tmp;
  411. int insn_delta, insn_cnt;
  412. struct bpf_insn *insn;
  413. int i, rewritten;
  414. if (!bpf_jit_blinding_enabled())
  415. return prog;
  416. clone = bpf_prog_clone_create(prog, GFP_USER);
  417. if (!clone)
  418. return ERR_PTR(-ENOMEM);
  419. insn_cnt = clone->len;
  420. insn = clone->insnsi;
  421. for (i = 0; i < insn_cnt; i++, insn++) {
  422. /* We temporarily need to hold the original ld64 insn
  423. * so that we can still access the first part in the
  424. * second blinding run.
  425. */
  426. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  427. insn[1].code == 0)
  428. memcpy(aux, insn, sizeof(aux));
  429. rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
  430. if (!rewritten)
  431. continue;
  432. tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
  433. if (!tmp) {
  434. /* Patching may have repointed aux->prog during
  435. * realloc from the original one, so we need to
  436. * fix it up here on error.
  437. */
  438. bpf_jit_prog_release_other(prog, clone);
  439. return ERR_PTR(-ENOMEM);
  440. }
  441. clone = tmp;
  442. insn_delta = rewritten - 1;
  443. /* Walk new program and skip insns we just inserted. */
  444. insn = clone->insnsi + i + insn_delta;
  445. insn_cnt += insn_delta;
  446. i += insn_delta;
  447. }
  448. return clone;
  449. }
  450. #endif /* CONFIG_BPF_JIT */
  451. /* Base function for offset calculation. Needs to go into .text section,
  452. * therefore keeping it non-static as well; will also be used by JITs
  453. * anyway later on, so do not let the compiler omit it.
  454. */
  455. noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  456. {
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(__bpf_call_base);
  460. /**
  461. * __bpf_prog_run - run eBPF program on a given context
  462. * @ctx: is the data we are operating on
  463. * @insn: is the array of eBPF instructions
  464. *
  465. * Decode and execute eBPF instructions.
  466. */
  467. static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
  468. {
  469. u64 stack[MAX_BPF_STACK / sizeof(u64)];
  470. u64 regs[MAX_BPF_REG], tmp;
  471. static const void *jumptable[256] = {
  472. [0 ... 255] = &&default_label,
  473. /* Now overwrite non-defaults ... */
  474. /* 32 bit ALU operations */
  475. [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
  476. [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
  477. [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
  478. [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
  479. [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
  480. [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
  481. [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
  482. [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
  483. [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
  484. [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
  485. [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
  486. [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
  487. [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
  488. [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
  489. [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
  490. [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
  491. [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
  492. [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
  493. [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
  494. [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
  495. [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
  496. [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
  497. [BPF_ALU | BPF_NEG] = &&ALU_NEG,
  498. [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
  499. [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
  500. /* 64 bit ALU operations */
  501. [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
  502. [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
  503. [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
  504. [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
  505. [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
  506. [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
  507. [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
  508. [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
  509. [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
  510. [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
  511. [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
  512. [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
  513. [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
  514. [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
  515. [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
  516. [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
  517. [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
  518. [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
  519. [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
  520. [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
  521. [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
  522. [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
  523. [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
  524. [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
  525. [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
  526. /* Call instruction */
  527. [BPF_JMP | BPF_CALL] = &&JMP_CALL,
  528. [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
  529. /* Jumps */
  530. [BPF_JMP | BPF_JA] = &&JMP_JA,
  531. [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
  532. [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
  533. [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
  534. [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
  535. [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
  536. [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
  537. [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
  538. [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
  539. [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
  540. [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
  541. [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
  542. [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
  543. [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
  544. [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
  545. /* Program return */
  546. [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
  547. /* Store instructions */
  548. [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
  549. [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
  550. [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
  551. [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
  552. [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
  553. [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
  554. [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
  555. [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
  556. [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
  557. [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
  558. /* Load instructions */
  559. [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
  560. [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
  561. [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
  562. [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
  563. [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
  564. [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
  565. [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
  566. [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
  567. [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
  568. [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
  569. [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
  570. };
  571. u32 tail_call_cnt = 0;
  572. void *ptr;
  573. int off;
  574. #define CONT ({ insn++; goto select_insn; })
  575. #define CONT_JMP ({ insn++; goto select_insn; })
  576. FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
  577. ARG1 = (u64) (unsigned long) ctx;
  578. select_insn:
  579. goto *jumptable[insn->code];
  580. /* ALU */
  581. #define ALU(OPCODE, OP) \
  582. ALU64_##OPCODE##_X: \
  583. DST = DST OP SRC; \
  584. CONT; \
  585. ALU_##OPCODE##_X: \
  586. DST = (u32) DST OP (u32) SRC; \
  587. CONT; \
  588. ALU64_##OPCODE##_K: \
  589. DST = DST OP IMM; \
  590. CONT; \
  591. ALU_##OPCODE##_K: \
  592. DST = (u32) DST OP (u32) IMM; \
  593. CONT;
  594. ALU(ADD, +)
  595. ALU(SUB, -)
  596. ALU(AND, &)
  597. ALU(OR, |)
  598. ALU(LSH, <<)
  599. ALU(RSH, >>)
  600. ALU(XOR, ^)
  601. ALU(MUL, *)
  602. #undef ALU
  603. ALU_NEG:
  604. DST = (u32) -DST;
  605. CONT;
  606. ALU64_NEG:
  607. DST = -DST;
  608. CONT;
  609. ALU_MOV_X:
  610. DST = (u32) SRC;
  611. CONT;
  612. ALU_MOV_K:
  613. DST = (u32) IMM;
  614. CONT;
  615. ALU64_MOV_X:
  616. DST = SRC;
  617. CONT;
  618. ALU64_MOV_K:
  619. DST = IMM;
  620. CONT;
  621. LD_IMM_DW:
  622. DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
  623. insn++;
  624. CONT;
  625. ALU64_ARSH_X:
  626. (*(s64 *) &DST) >>= SRC;
  627. CONT;
  628. ALU64_ARSH_K:
  629. (*(s64 *) &DST) >>= IMM;
  630. CONT;
  631. ALU64_MOD_X:
  632. if (unlikely(SRC == 0))
  633. return 0;
  634. div64_u64_rem(DST, SRC, &tmp);
  635. DST = tmp;
  636. CONT;
  637. ALU_MOD_X:
  638. if (unlikely(SRC == 0))
  639. return 0;
  640. tmp = (u32) DST;
  641. DST = do_div(tmp, (u32) SRC);
  642. CONT;
  643. ALU64_MOD_K:
  644. div64_u64_rem(DST, IMM, &tmp);
  645. DST = tmp;
  646. CONT;
  647. ALU_MOD_K:
  648. tmp = (u32) DST;
  649. DST = do_div(tmp, (u32) IMM);
  650. CONT;
  651. ALU64_DIV_X:
  652. if (unlikely(SRC == 0))
  653. return 0;
  654. DST = div64_u64(DST, SRC);
  655. CONT;
  656. ALU_DIV_X:
  657. if (unlikely(SRC == 0))
  658. return 0;
  659. tmp = (u32) DST;
  660. do_div(tmp, (u32) SRC);
  661. DST = (u32) tmp;
  662. CONT;
  663. ALU64_DIV_K:
  664. DST = div64_u64(DST, IMM);
  665. CONT;
  666. ALU_DIV_K:
  667. tmp = (u32) DST;
  668. do_div(tmp, (u32) IMM);
  669. DST = (u32) tmp;
  670. CONT;
  671. ALU_END_TO_BE:
  672. switch (IMM) {
  673. case 16:
  674. DST = (__force u16) cpu_to_be16(DST);
  675. break;
  676. case 32:
  677. DST = (__force u32) cpu_to_be32(DST);
  678. break;
  679. case 64:
  680. DST = (__force u64) cpu_to_be64(DST);
  681. break;
  682. }
  683. CONT;
  684. ALU_END_TO_LE:
  685. switch (IMM) {
  686. case 16:
  687. DST = (__force u16) cpu_to_le16(DST);
  688. break;
  689. case 32:
  690. DST = (__force u32) cpu_to_le32(DST);
  691. break;
  692. case 64:
  693. DST = (__force u64) cpu_to_le64(DST);
  694. break;
  695. }
  696. CONT;
  697. /* CALL */
  698. JMP_CALL:
  699. /* Function call scratches BPF_R1-BPF_R5 registers,
  700. * preserves BPF_R6-BPF_R9, and stores return value
  701. * into BPF_R0.
  702. */
  703. BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
  704. BPF_R4, BPF_R5);
  705. CONT;
  706. JMP_TAIL_CALL: {
  707. struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
  708. struct bpf_array *array = container_of(map, struct bpf_array, map);
  709. struct bpf_prog *prog;
  710. u64 index = BPF_R3;
  711. if (unlikely(index >= array->map.max_entries))
  712. goto out;
  713. if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
  714. goto out;
  715. tail_call_cnt++;
  716. prog = READ_ONCE(array->ptrs[index]);
  717. if (!prog)
  718. goto out;
  719. /* ARG1 at this point is guaranteed to point to CTX from
  720. * the verifier side due to the fact that the tail call is
  721. * handeled like a helper, that is, bpf_tail_call_proto,
  722. * where arg1_type is ARG_PTR_TO_CTX.
  723. */
  724. insn = prog->insnsi;
  725. goto select_insn;
  726. out:
  727. CONT;
  728. }
  729. /* JMP */
  730. JMP_JA:
  731. insn += insn->off;
  732. CONT;
  733. JMP_JEQ_X:
  734. if (DST == SRC) {
  735. insn += insn->off;
  736. CONT_JMP;
  737. }
  738. CONT;
  739. JMP_JEQ_K:
  740. if (DST == IMM) {
  741. insn += insn->off;
  742. CONT_JMP;
  743. }
  744. CONT;
  745. JMP_JNE_X:
  746. if (DST != SRC) {
  747. insn += insn->off;
  748. CONT_JMP;
  749. }
  750. CONT;
  751. JMP_JNE_K:
  752. if (DST != IMM) {
  753. insn += insn->off;
  754. CONT_JMP;
  755. }
  756. CONT;
  757. JMP_JGT_X:
  758. if (DST > SRC) {
  759. insn += insn->off;
  760. CONT_JMP;
  761. }
  762. CONT;
  763. JMP_JGT_K:
  764. if (DST > IMM) {
  765. insn += insn->off;
  766. CONT_JMP;
  767. }
  768. CONT;
  769. JMP_JGE_X:
  770. if (DST >= SRC) {
  771. insn += insn->off;
  772. CONT_JMP;
  773. }
  774. CONT;
  775. JMP_JGE_K:
  776. if (DST >= IMM) {
  777. insn += insn->off;
  778. CONT_JMP;
  779. }
  780. CONT;
  781. JMP_JSGT_X:
  782. if (((s64) DST) > ((s64) SRC)) {
  783. insn += insn->off;
  784. CONT_JMP;
  785. }
  786. CONT;
  787. JMP_JSGT_K:
  788. if (((s64) DST) > ((s64) IMM)) {
  789. insn += insn->off;
  790. CONT_JMP;
  791. }
  792. CONT;
  793. JMP_JSGE_X:
  794. if (((s64) DST) >= ((s64) SRC)) {
  795. insn += insn->off;
  796. CONT_JMP;
  797. }
  798. CONT;
  799. JMP_JSGE_K:
  800. if (((s64) DST) >= ((s64) IMM)) {
  801. insn += insn->off;
  802. CONT_JMP;
  803. }
  804. CONT;
  805. JMP_JSET_X:
  806. if (DST & SRC) {
  807. insn += insn->off;
  808. CONT_JMP;
  809. }
  810. CONT;
  811. JMP_JSET_K:
  812. if (DST & IMM) {
  813. insn += insn->off;
  814. CONT_JMP;
  815. }
  816. CONT;
  817. JMP_EXIT:
  818. return BPF_R0;
  819. /* STX and ST and LDX*/
  820. #define LDST(SIZEOP, SIZE) \
  821. STX_MEM_##SIZEOP: \
  822. *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
  823. CONT; \
  824. ST_MEM_##SIZEOP: \
  825. *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
  826. CONT; \
  827. LDX_MEM_##SIZEOP: \
  828. DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
  829. CONT;
  830. LDST(B, u8)
  831. LDST(H, u16)
  832. LDST(W, u32)
  833. LDST(DW, u64)
  834. #undef LDST
  835. STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
  836. atomic_add((u32) SRC, (atomic_t *)(unsigned long)
  837. (DST + insn->off));
  838. CONT;
  839. STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
  840. atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
  841. (DST + insn->off));
  842. CONT;
  843. LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
  844. off = IMM;
  845. load_word:
  846. /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
  847. * only appearing in the programs where ctx ==
  848. * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
  849. * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
  850. * internal BPF verifier will check that BPF_R6 ==
  851. * ctx.
  852. *
  853. * BPF_ABS and BPF_IND are wrappers of function calls,
  854. * so they scratch BPF_R1-BPF_R5 registers, preserve
  855. * BPF_R6-BPF_R9, and store return value into BPF_R0.
  856. *
  857. * Implicit input:
  858. * ctx == skb == BPF_R6 == CTX
  859. *
  860. * Explicit input:
  861. * SRC == any register
  862. * IMM == 32-bit immediate
  863. *
  864. * Output:
  865. * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
  866. */
  867. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
  868. if (likely(ptr != NULL)) {
  869. BPF_R0 = get_unaligned_be32(ptr);
  870. CONT;
  871. }
  872. return 0;
  873. LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
  874. off = IMM;
  875. load_half:
  876. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
  877. if (likely(ptr != NULL)) {
  878. BPF_R0 = get_unaligned_be16(ptr);
  879. CONT;
  880. }
  881. return 0;
  882. LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
  883. off = IMM;
  884. load_byte:
  885. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
  886. if (likely(ptr != NULL)) {
  887. BPF_R0 = *(u8 *)ptr;
  888. CONT;
  889. }
  890. return 0;
  891. LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
  892. off = IMM + SRC;
  893. goto load_word;
  894. LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
  895. off = IMM + SRC;
  896. goto load_half;
  897. LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
  898. off = IMM + SRC;
  899. goto load_byte;
  900. default_label:
  901. /* If we ever reach this, we have a bug somewhere. */
  902. WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
  903. return 0;
  904. }
  905. STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
  906. bool bpf_prog_array_compatible(struct bpf_array *array,
  907. const struct bpf_prog *fp)
  908. {
  909. if (!array->owner_prog_type) {
  910. /* There's no owner yet where we could check for
  911. * compatibility.
  912. */
  913. array->owner_prog_type = fp->type;
  914. array->owner_jited = fp->jited;
  915. return true;
  916. }
  917. return array->owner_prog_type == fp->type &&
  918. array->owner_jited == fp->jited;
  919. }
  920. static int bpf_check_tail_call(const struct bpf_prog *fp)
  921. {
  922. struct bpf_prog_aux *aux = fp->aux;
  923. int i;
  924. for (i = 0; i < aux->used_map_cnt; i++) {
  925. struct bpf_map *map = aux->used_maps[i];
  926. struct bpf_array *array;
  927. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  928. continue;
  929. array = container_of(map, struct bpf_array, map);
  930. if (!bpf_prog_array_compatible(array, fp))
  931. return -EINVAL;
  932. }
  933. return 0;
  934. }
  935. /**
  936. * bpf_prog_select_runtime - select exec runtime for BPF program
  937. * @fp: bpf_prog populated with internal BPF program
  938. * @err: pointer to error variable
  939. *
  940. * Try to JIT eBPF program, if JIT is not available, use interpreter.
  941. * The BPF program will be executed via BPF_PROG_RUN() macro.
  942. */
  943. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
  944. {
  945. fp->bpf_func = (void *) __bpf_prog_run;
  946. /* eBPF JITs can rewrite the program in case constant
  947. * blinding is active. However, in case of error during
  948. * blinding, bpf_int_jit_compile() must always return a
  949. * valid program, which in this case would simply not
  950. * be JITed, but falls back to the interpreter.
  951. */
  952. fp = bpf_int_jit_compile(fp);
  953. bpf_prog_lock_ro(fp);
  954. /* The tail call compatibility check can only be done at
  955. * this late stage as we need to determine, if we deal
  956. * with JITed or non JITed program concatenations and not
  957. * all eBPF JITs might immediately support all features.
  958. */
  959. *err = bpf_check_tail_call(fp);
  960. return fp;
  961. }
  962. EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
  963. static void bpf_prog_free_deferred(struct work_struct *work)
  964. {
  965. struct bpf_prog_aux *aux;
  966. aux = container_of(work, struct bpf_prog_aux, work);
  967. bpf_jit_free(aux->prog);
  968. }
  969. /* Free internal BPF program */
  970. void bpf_prog_free(struct bpf_prog *fp)
  971. {
  972. struct bpf_prog_aux *aux = fp->aux;
  973. INIT_WORK(&aux->work, bpf_prog_free_deferred);
  974. schedule_work(&aux->work);
  975. }
  976. EXPORT_SYMBOL_GPL(bpf_prog_free);
  977. /* RNG for unpriviledged user space with separated state from prandom_u32(). */
  978. static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
  979. void bpf_user_rnd_init_once(void)
  980. {
  981. prandom_init_once(&bpf_user_rnd_state);
  982. }
  983. BPF_CALL_0(bpf_user_rnd_u32)
  984. {
  985. /* Should someone ever have the rather unwise idea to use some
  986. * of the registers passed into this function, then note that
  987. * this function is called from native eBPF and classic-to-eBPF
  988. * transformations. Register assignments from both sides are
  989. * different, f.e. classic always sets fn(ctx, A, X) here.
  990. */
  991. struct rnd_state *state;
  992. u32 res;
  993. state = &get_cpu_var(bpf_user_rnd_state);
  994. res = prandom_u32_state(state);
  995. put_cpu_var(bpf_user_rnd_state);
  996. return res;
  997. }
  998. /* Weak definitions of helper functions in case we don't have bpf syscall. */
  999. const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
  1000. const struct bpf_func_proto bpf_map_update_elem_proto __weak;
  1001. const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
  1002. const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
  1003. const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
  1004. const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
  1005. const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
  1006. const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
  1007. const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
  1008. const struct bpf_func_proto bpf_get_current_comm_proto __weak;
  1009. const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
  1010. {
  1011. return NULL;
  1012. }
  1013. u64 __weak
  1014. bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  1015. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  1016. {
  1017. return -ENOTSUPP;
  1018. }
  1019. /* Always built-in helper functions. */
  1020. const struct bpf_func_proto bpf_tail_call_proto = {
  1021. .func = NULL,
  1022. .gpl_only = false,
  1023. .ret_type = RET_VOID,
  1024. .arg1_type = ARG_PTR_TO_CTX,
  1025. .arg2_type = ARG_CONST_MAP_PTR,
  1026. .arg3_type = ARG_ANYTHING,
  1027. };
  1028. /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
  1029. struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
  1030. {
  1031. return prog;
  1032. }
  1033. bool __weak bpf_helper_changes_pkt_data(void *func)
  1034. {
  1035. return false;
  1036. }
  1037. /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  1038. * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  1039. */
  1040. int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
  1041. int len)
  1042. {
  1043. return -EFAULT;
  1044. }