core.c 19 KB

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