core.c 20 KB

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