core.c 29 KB

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