core.c 20 KB

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