core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 <asm/unaligned.h>
  30. /* Registers */
  31. #define BPF_R0 regs[BPF_REG_0]
  32. #define BPF_R1 regs[BPF_REG_1]
  33. #define BPF_R2 regs[BPF_REG_2]
  34. #define BPF_R3 regs[BPF_REG_3]
  35. #define BPF_R4 regs[BPF_REG_4]
  36. #define BPF_R5 regs[BPF_REG_5]
  37. #define BPF_R6 regs[BPF_REG_6]
  38. #define BPF_R7 regs[BPF_REG_7]
  39. #define BPF_R8 regs[BPF_REG_8]
  40. #define BPF_R9 regs[BPF_REG_9]
  41. #define BPF_R10 regs[BPF_REG_10]
  42. /* Named registers */
  43. #define DST regs[insn->dst_reg]
  44. #define SRC regs[insn->src_reg]
  45. #define FP regs[BPF_REG_FP]
  46. #define ARG1 regs[BPF_REG_ARG1]
  47. #define CTX regs[BPF_REG_CTX]
  48. #define IMM insn->imm
  49. /* No hurry in this branch
  50. *
  51. * Exported for the bpf jit load helper.
  52. */
  53. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
  54. {
  55. u8 *ptr = NULL;
  56. if (k >= SKF_NET_OFF)
  57. ptr = skb_network_header(skb) + k - SKF_NET_OFF;
  58. else if (k >= SKF_LL_OFF)
  59. ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
  60. if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
  61. return ptr;
  62. return NULL;
  63. }
  64. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
  65. {
  66. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  67. gfp_extra_flags;
  68. struct bpf_prog_aux *aux;
  69. struct bpf_prog *fp;
  70. size = round_up(size, PAGE_SIZE);
  71. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  72. if (fp == NULL)
  73. return NULL;
  74. aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
  75. if (aux == NULL) {
  76. vfree(fp);
  77. return NULL;
  78. }
  79. fp->pages = size / PAGE_SIZE;
  80. fp->aux = aux;
  81. return fp;
  82. }
  83. EXPORT_SYMBOL_GPL(bpf_prog_alloc);
  84. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  85. gfp_t gfp_extra_flags)
  86. {
  87. gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
  88. gfp_extra_flags;
  89. struct bpf_prog *fp;
  90. BUG_ON(fp_old == NULL);
  91. size = round_up(size, PAGE_SIZE);
  92. if (size <= fp_old->pages * PAGE_SIZE)
  93. return fp_old;
  94. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  95. if (fp != NULL) {
  96. memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
  97. fp->pages = size / PAGE_SIZE;
  98. /* We keep fp->aux from fp_old around in the new
  99. * reallocated structure.
  100. */
  101. fp_old->aux = NULL;
  102. __bpf_prog_free(fp_old);
  103. }
  104. return fp;
  105. }
  106. EXPORT_SYMBOL_GPL(bpf_prog_realloc);
  107. void __bpf_prog_free(struct bpf_prog *fp)
  108. {
  109. kfree(fp->aux);
  110. vfree(fp);
  111. }
  112. EXPORT_SYMBOL_GPL(__bpf_prog_free);
  113. #ifdef CONFIG_BPF_JIT
  114. struct bpf_binary_header *
  115. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  116. unsigned int alignment,
  117. bpf_jit_fill_hole_t bpf_fill_ill_insns)
  118. {
  119. struct bpf_binary_header *hdr;
  120. unsigned int size, hole, start;
  121. /* Most of BPF filters are really small, but if some of them
  122. * fill a page, allow at least 128 extra bytes to insert a
  123. * random section of illegal instructions.
  124. */
  125. size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
  126. hdr = module_alloc(size);
  127. if (hdr == NULL)
  128. return NULL;
  129. /* Fill space with illegal/arch-dep instructions. */
  130. bpf_fill_ill_insns(hdr, size);
  131. hdr->pages = size / PAGE_SIZE;
  132. hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
  133. PAGE_SIZE - sizeof(*hdr));
  134. start = (prandom_u32() % hole) & ~(alignment - 1);
  135. /* Leave a random number of instructions before BPF code. */
  136. *image_ptr = &hdr->image[start];
  137. return hdr;
  138. }
  139. void bpf_jit_binary_free(struct bpf_binary_header *hdr)
  140. {
  141. module_memfree(hdr);
  142. }
  143. #endif /* CONFIG_BPF_JIT */
  144. /* Base function for offset calculation. Needs to go into .text section,
  145. * therefore keeping it non-static as well; will also be used by JITs
  146. * anyway later on, so do not let the compiler omit it.
  147. */
  148. noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  149. {
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(__bpf_call_base);
  153. /**
  154. * __bpf_prog_run - run eBPF program on a given context
  155. * @ctx: is the data we are operating on
  156. * @insn: is the array of eBPF instructions
  157. *
  158. * Decode and execute eBPF instructions.
  159. */
  160. static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
  161. {
  162. u64 stack[MAX_BPF_STACK / sizeof(u64)];
  163. u64 regs[MAX_BPF_REG], tmp;
  164. static const void *jumptable[256] = {
  165. [0 ... 255] = &&default_label,
  166. /* Now overwrite non-defaults ... */
  167. /* 32 bit ALU operations */
  168. [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
  169. [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
  170. [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
  171. [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
  172. [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
  173. [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
  174. [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
  175. [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
  176. [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
  177. [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
  178. [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
  179. [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
  180. [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
  181. [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
  182. [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
  183. [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
  184. [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
  185. [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
  186. [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
  187. [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
  188. [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
  189. [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
  190. [BPF_ALU | BPF_NEG] = &&ALU_NEG,
  191. [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
  192. [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
  193. /* 64 bit ALU operations */
  194. [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
  195. [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
  196. [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
  197. [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
  198. [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
  199. [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
  200. [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
  201. [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
  202. [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
  203. [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
  204. [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
  205. [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
  206. [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
  207. [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
  208. [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
  209. [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
  210. [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
  211. [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
  212. [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
  213. [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
  214. [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
  215. [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
  216. [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
  217. [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
  218. [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
  219. /* Call instruction */
  220. [BPF_JMP | BPF_CALL] = &&JMP_CALL,
  221. [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
  222. /* Jumps */
  223. [BPF_JMP | BPF_JA] = &&JMP_JA,
  224. [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
  225. [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
  226. [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
  227. [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
  228. [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
  229. [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
  230. [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
  231. [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
  232. [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
  233. [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
  234. [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
  235. [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
  236. [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
  237. [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
  238. /* Program return */
  239. [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
  240. /* Store instructions */
  241. [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
  242. [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
  243. [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
  244. [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
  245. [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
  246. [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
  247. [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
  248. [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
  249. [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
  250. [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
  251. /* Load instructions */
  252. [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
  253. [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
  254. [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
  255. [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
  256. [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
  257. [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
  258. [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
  259. [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
  260. [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
  261. [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
  262. [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
  263. };
  264. u32 tail_call_cnt = 0;
  265. void *ptr;
  266. int off;
  267. #define CONT ({ insn++; goto select_insn; })
  268. #define CONT_JMP ({ insn++; goto select_insn; })
  269. FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
  270. ARG1 = (u64) (unsigned long) ctx;
  271. /* Registers used in classic BPF programs need to be reset first. */
  272. regs[BPF_REG_A] = 0;
  273. regs[BPF_REG_X] = 0;
  274. select_insn:
  275. goto *jumptable[insn->code];
  276. /* ALU */
  277. #define ALU(OPCODE, OP) \
  278. ALU64_##OPCODE##_X: \
  279. DST = DST OP SRC; \
  280. CONT; \
  281. ALU_##OPCODE##_X: \
  282. DST = (u32) DST OP (u32) SRC; \
  283. CONT; \
  284. ALU64_##OPCODE##_K: \
  285. DST = DST OP IMM; \
  286. CONT; \
  287. ALU_##OPCODE##_K: \
  288. DST = (u32) DST OP (u32) IMM; \
  289. CONT;
  290. ALU(ADD, +)
  291. ALU(SUB, -)
  292. ALU(AND, &)
  293. ALU(OR, |)
  294. ALU(LSH, <<)
  295. ALU(RSH, >>)
  296. ALU(XOR, ^)
  297. ALU(MUL, *)
  298. #undef ALU
  299. ALU_NEG:
  300. DST = (u32) -DST;
  301. CONT;
  302. ALU64_NEG:
  303. DST = -DST;
  304. CONT;
  305. ALU_MOV_X:
  306. DST = (u32) SRC;
  307. CONT;
  308. ALU_MOV_K:
  309. DST = (u32) IMM;
  310. CONT;
  311. ALU64_MOV_X:
  312. DST = SRC;
  313. CONT;
  314. ALU64_MOV_K:
  315. DST = IMM;
  316. CONT;
  317. LD_IMM_DW:
  318. DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
  319. insn++;
  320. CONT;
  321. ALU64_ARSH_X:
  322. (*(s64 *) &DST) >>= SRC;
  323. CONT;
  324. ALU64_ARSH_K:
  325. (*(s64 *) &DST) >>= IMM;
  326. CONT;
  327. ALU64_MOD_X:
  328. if (unlikely(SRC == 0))
  329. return 0;
  330. div64_u64_rem(DST, SRC, &tmp);
  331. DST = tmp;
  332. CONT;
  333. ALU_MOD_X:
  334. if (unlikely(SRC == 0))
  335. return 0;
  336. tmp = (u32) DST;
  337. DST = do_div(tmp, (u32) SRC);
  338. CONT;
  339. ALU64_MOD_K:
  340. div64_u64_rem(DST, IMM, &tmp);
  341. DST = tmp;
  342. CONT;
  343. ALU_MOD_K:
  344. tmp = (u32) DST;
  345. DST = do_div(tmp, (u32) IMM);
  346. CONT;
  347. ALU64_DIV_X:
  348. if (unlikely(SRC == 0))
  349. return 0;
  350. DST = div64_u64(DST, SRC);
  351. CONT;
  352. ALU_DIV_X:
  353. if (unlikely(SRC == 0))
  354. return 0;
  355. tmp = (u32) DST;
  356. do_div(tmp, (u32) SRC);
  357. DST = (u32) tmp;
  358. CONT;
  359. ALU64_DIV_K:
  360. DST = div64_u64(DST, IMM);
  361. CONT;
  362. ALU_DIV_K:
  363. tmp = (u32) DST;
  364. do_div(tmp, (u32) IMM);
  365. DST = (u32) tmp;
  366. CONT;
  367. ALU_END_TO_BE:
  368. switch (IMM) {
  369. case 16:
  370. DST = (__force u16) cpu_to_be16(DST);
  371. break;
  372. case 32:
  373. DST = (__force u32) cpu_to_be32(DST);
  374. break;
  375. case 64:
  376. DST = (__force u64) cpu_to_be64(DST);
  377. break;
  378. }
  379. CONT;
  380. ALU_END_TO_LE:
  381. switch (IMM) {
  382. case 16:
  383. DST = (__force u16) cpu_to_le16(DST);
  384. break;
  385. case 32:
  386. DST = (__force u32) cpu_to_le32(DST);
  387. break;
  388. case 64:
  389. DST = (__force u64) cpu_to_le64(DST);
  390. break;
  391. }
  392. CONT;
  393. /* CALL */
  394. JMP_CALL:
  395. /* Function call scratches BPF_R1-BPF_R5 registers,
  396. * preserves BPF_R6-BPF_R9, and stores return value
  397. * into BPF_R0.
  398. */
  399. BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
  400. BPF_R4, BPF_R5);
  401. CONT;
  402. JMP_TAIL_CALL: {
  403. struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
  404. struct bpf_array *array = container_of(map, struct bpf_array, map);
  405. struct bpf_prog *prog;
  406. u64 index = BPF_R3;
  407. if (unlikely(index >= array->map.max_entries))
  408. goto out;
  409. if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
  410. goto out;
  411. tail_call_cnt++;
  412. prog = READ_ONCE(array->ptrs[index]);
  413. if (unlikely(!prog))
  414. goto out;
  415. /* ARG1 at this point is guaranteed to point to CTX from
  416. * the verifier side due to the fact that the tail call is
  417. * handeled like a helper, that is, bpf_tail_call_proto,
  418. * where arg1_type is ARG_PTR_TO_CTX.
  419. */
  420. insn = prog->insnsi;
  421. goto select_insn;
  422. out:
  423. CONT;
  424. }
  425. /* JMP */
  426. JMP_JA:
  427. insn += insn->off;
  428. CONT;
  429. JMP_JEQ_X:
  430. if (DST == SRC) {
  431. insn += insn->off;
  432. CONT_JMP;
  433. }
  434. CONT;
  435. JMP_JEQ_K:
  436. if (DST == IMM) {
  437. insn += insn->off;
  438. CONT_JMP;
  439. }
  440. CONT;
  441. JMP_JNE_X:
  442. if (DST != SRC) {
  443. insn += insn->off;
  444. CONT_JMP;
  445. }
  446. CONT;
  447. JMP_JNE_K:
  448. if (DST != IMM) {
  449. insn += insn->off;
  450. CONT_JMP;
  451. }
  452. CONT;
  453. JMP_JGT_X:
  454. if (DST > SRC) {
  455. insn += insn->off;
  456. CONT_JMP;
  457. }
  458. CONT;
  459. JMP_JGT_K:
  460. if (DST > IMM) {
  461. insn += insn->off;
  462. CONT_JMP;
  463. }
  464. CONT;
  465. JMP_JGE_X:
  466. if (DST >= SRC) {
  467. insn += insn->off;
  468. CONT_JMP;
  469. }
  470. CONT;
  471. JMP_JGE_K:
  472. if (DST >= IMM) {
  473. insn += insn->off;
  474. CONT_JMP;
  475. }
  476. CONT;
  477. JMP_JSGT_X:
  478. if (((s64) DST) > ((s64) SRC)) {
  479. insn += insn->off;
  480. CONT_JMP;
  481. }
  482. CONT;
  483. JMP_JSGT_K:
  484. if (((s64) DST) > ((s64) IMM)) {
  485. insn += insn->off;
  486. CONT_JMP;
  487. }
  488. CONT;
  489. JMP_JSGE_X:
  490. if (((s64) DST) >= ((s64) SRC)) {
  491. insn += insn->off;
  492. CONT_JMP;
  493. }
  494. CONT;
  495. JMP_JSGE_K:
  496. if (((s64) DST) >= ((s64) IMM)) {
  497. insn += insn->off;
  498. CONT_JMP;
  499. }
  500. CONT;
  501. JMP_JSET_X:
  502. if (DST & SRC) {
  503. insn += insn->off;
  504. CONT_JMP;
  505. }
  506. CONT;
  507. JMP_JSET_K:
  508. if (DST & IMM) {
  509. insn += insn->off;
  510. CONT_JMP;
  511. }
  512. CONT;
  513. JMP_EXIT:
  514. return BPF_R0;
  515. /* STX and ST and LDX*/
  516. #define LDST(SIZEOP, SIZE) \
  517. STX_MEM_##SIZEOP: \
  518. *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
  519. CONT; \
  520. ST_MEM_##SIZEOP: \
  521. *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
  522. CONT; \
  523. LDX_MEM_##SIZEOP: \
  524. DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
  525. CONT;
  526. LDST(B, u8)
  527. LDST(H, u16)
  528. LDST(W, u32)
  529. LDST(DW, u64)
  530. #undef LDST
  531. STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
  532. atomic_add((u32) SRC, (atomic_t *)(unsigned long)
  533. (DST + insn->off));
  534. CONT;
  535. STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
  536. atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
  537. (DST + insn->off));
  538. CONT;
  539. LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
  540. off = IMM;
  541. load_word:
  542. /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
  543. * only appearing in the programs where ctx ==
  544. * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
  545. * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
  546. * internal BPF verifier will check that BPF_R6 ==
  547. * ctx.
  548. *
  549. * BPF_ABS and BPF_IND are wrappers of function calls,
  550. * so they scratch BPF_R1-BPF_R5 registers, preserve
  551. * BPF_R6-BPF_R9, and store return value into BPF_R0.
  552. *
  553. * Implicit input:
  554. * ctx == skb == BPF_R6 == CTX
  555. *
  556. * Explicit input:
  557. * SRC == any register
  558. * IMM == 32-bit immediate
  559. *
  560. * Output:
  561. * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
  562. */
  563. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
  564. if (likely(ptr != NULL)) {
  565. BPF_R0 = get_unaligned_be32(ptr);
  566. CONT;
  567. }
  568. return 0;
  569. LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
  570. off = IMM;
  571. load_half:
  572. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
  573. if (likely(ptr != NULL)) {
  574. BPF_R0 = get_unaligned_be16(ptr);
  575. CONT;
  576. }
  577. return 0;
  578. LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
  579. off = IMM;
  580. load_byte:
  581. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
  582. if (likely(ptr != NULL)) {
  583. BPF_R0 = *(u8 *)ptr;
  584. CONT;
  585. }
  586. return 0;
  587. LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
  588. off = IMM + SRC;
  589. goto load_word;
  590. LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
  591. off = IMM + SRC;
  592. goto load_half;
  593. LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
  594. off = IMM + SRC;
  595. goto load_byte;
  596. default_label:
  597. /* If we ever reach this, we have a bug somewhere. */
  598. WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
  599. return 0;
  600. }
  601. bool bpf_prog_array_compatible(struct bpf_array *array,
  602. const struct bpf_prog *fp)
  603. {
  604. if (!array->owner_prog_type) {
  605. /* There's no owner yet where we could check for
  606. * compatibility.
  607. */
  608. array->owner_prog_type = fp->type;
  609. array->owner_jited = fp->jited;
  610. return true;
  611. }
  612. return array->owner_prog_type == fp->type &&
  613. array->owner_jited == fp->jited;
  614. }
  615. static int bpf_check_tail_call(const struct bpf_prog *fp)
  616. {
  617. struct bpf_prog_aux *aux = fp->aux;
  618. int i;
  619. for (i = 0; i < aux->used_map_cnt; i++) {
  620. struct bpf_map *map = aux->used_maps[i];
  621. struct bpf_array *array;
  622. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  623. continue;
  624. array = container_of(map, struct bpf_array, map);
  625. if (!bpf_prog_array_compatible(array, fp))
  626. return -EINVAL;
  627. }
  628. return 0;
  629. }
  630. /**
  631. * bpf_prog_select_runtime - select exec runtime for BPF program
  632. * @fp: bpf_prog populated with internal BPF program
  633. *
  634. * Try to JIT eBPF program, if JIT is not available, use interpreter.
  635. * The BPF program will be executed via BPF_PROG_RUN() macro.
  636. */
  637. int bpf_prog_select_runtime(struct bpf_prog *fp)
  638. {
  639. fp->bpf_func = (void *) __bpf_prog_run;
  640. bpf_int_jit_compile(fp);
  641. bpf_prog_lock_ro(fp);
  642. /* The tail call compatibility check can only be done at
  643. * this late stage as we need to determine, if we deal
  644. * with JITed or non JITed program concatenations and not
  645. * all eBPF JITs might immediately support all features.
  646. */
  647. return bpf_check_tail_call(fp);
  648. }
  649. EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
  650. static void bpf_prog_free_deferred(struct work_struct *work)
  651. {
  652. struct bpf_prog_aux *aux;
  653. aux = container_of(work, struct bpf_prog_aux, work);
  654. bpf_jit_free(aux->prog);
  655. }
  656. /* Free internal BPF program */
  657. void bpf_prog_free(struct bpf_prog *fp)
  658. {
  659. struct bpf_prog_aux *aux = fp->aux;
  660. INIT_WORK(&aux->work, bpf_prog_free_deferred);
  661. aux->prog = fp;
  662. schedule_work(&aux->work);
  663. }
  664. EXPORT_SYMBOL_GPL(bpf_prog_free);
  665. /* Weak definitions of helper functions in case we don't have bpf syscall. */
  666. const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
  667. const struct bpf_func_proto bpf_map_update_elem_proto __weak;
  668. const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
  669. const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
  670. const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
  671. const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
  672. const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
  673. const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
  674. const struct bpf_func_proto bpf_get_current_comm_proto __weak;
  675. const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
  676. {
  677. return NULL;
  678. }
  679. /* Always built-in helper functions. */
  680. const struct bpf_func_proto bpf_tail_call_proto = {
  681. .func = NULL,
  682. .gpl_only = false,
  683. .ret_type = RET_VOID,
  684. .arg1_type = ARG_PTR_TO_CTX,
  685. .arg2_type = ARG_CONST_MAP_PTR,
  686. .arg3_type = ARG_ANYTHING,
  687. };
  688. /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
  689. void __weak bpf_int_jit_compile(struct bpf_prog *prog)
  690. {
  691. }
  692. /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  693. * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  694. */
  695. int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
  696. int len)
  697. {
  698. return -EFAULT;
  699. }