core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. select_insn:
  276. goto *jumptable[insn->code];
  277. /* ALU */
  278. #define ALU(OPCODE, OP) \
  279. ALU64_##OPCODE##_X: \
  280. DST = DST OP SRC; \
  281. CONT; \
  282. ALU_##OPCODE##_X: \
  283. DST = (u32) DST OP (u32) SRC; \
  284. CONT; \
  285. ALU64_##OPCODE##_K: \
  286. DST = DST OP IMM; \
  287. CONT; \
  288. ALU_##OPCODE##_K: \
  289. DST = (u32) DST OP (u32) IMM; \
  290. CONT;
  291. ALU(ADD, +)
  292. ALU(SUB, -)
  293. ALU(AND, &)
  294. ALU(OR, |)
  295. ALU(LSH, <<)
  296. ALU(RSH, >>)
  297. ALU(XOR, ^)
  298. ALU(MUL, *)
  299. #undef ALU
  300. ALU_NEG:
  301. DST = (u32) -DST;
  302. CONT;
  303. ALU64_NEG:
  304. DST = -DST;
  305. CONT;
  306. ALU_MOV_X:
  307. DST = (u32) SRC;
  308. CONT;
  309. ALU_MOV_K:
  310. DST = (u32) IMM;
  311. CONT;
  312. ALU64_MOV_X:
  313. DST = SRC;
  314. CONT;
  315. ALU64_MOV_K:
  316. DST = IMM;
  317. CONT;
  318. LD_IMM_DW:
  319. DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
  320. insn++;
  321. CONT;
  322. ALU64_ARSH_X:
  323. (*(s64 *) &DST) >>= SRC;
  324. CONT;
  325. ALU64_ARSH_K:
  326. (*(s64 *) &DST) >>= IMM;
  327. CONT;
  328. ALU64_MOD_X:
  329. if (unlikely(SRC == 0))
  330. return 0;
  331. div64_u64_rem(DST, SRC, &tmp);
  332. DST = tmp;
  333. CONT;
  334. ALU_MOD_X:
  335. if (unlikely(SRC == 0))
  336. return 0;
  337. tmp = (u32) DST;
  338. DST = do_div(tmp, (u32) SRC);
  339. CONT;
  340. ALU64_MOD_K:
  341. div64_u64_rem(DST, IMM, &tmp);
  342. DST = tmp;
  343. CONT;
  344. ALU_MOD_K:
  345. tmp = (u32) DST;
  346. DST = do_div(tmp, (u32) IMM);
  347. CONT;
  348. ALU64_DIV_X:
  349. if (unlikely(SRC == 0))
  350. return 0;
  351. DST = div64_u64(DST, SRC);
  352. CONT;
  353. ALU_DIV_X:
  354. if (unlikely(SRC == 0))
  355. return 0;
  356. tmp = (u32) DST;
  357. do_div(tmp, (u32) SRC);
  358. DST = (u32) tmp;
  359. CONT;
  360. ALU64_DIV_K:
  361. DST = div64_u64(DST, IMM);
  362. CONT;
  363. ALU_DIV_K:
  364. tmp = (u32) DST;
  365. do_div(tmp, (u32) IMM);
  366. DST = (u32) tmp;
  367. CONT;
  368. ALU_END_TO_BE:
  369. switch (IMM) {
  370. case 16:
  371. DST = (__force u16) cpu_to_be16(DST);
  372. break;
  373. case 32:
  374. DST = (__force u32) cpu_to_be32(DST);
  375. break;
  376. case 64:
  377. DST = (__force u64) cpu_to_be64(DST);
  378. break;
  379. }
  380. CONT;
  381. ALU_END_TO_LE:
  382. switch (IMM) {
  383. case 16:
  384. DST = (__force u16) cpu_to_le16(DST);
  385. break;
  386. case 32:
  387. DST = (__force u32) cpu_to_le32(DST);
  388. break;
  389. case 64:
  390. DST = (__force u64) cpu_to_le64(DST);
  391. break;
  392. }
  393. CONT;
  394. /* CALL */
  395. JMP_CALL:
  396. /* Function call scratches BPF_R1-BPF_R5 registers,
  397. * preserves BPF_R6-BPF_R9, and stores return value
  398. * into BPF_R0.
  399. */
  400. BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
  401. BPF_R4, BPF_R5);
  402. CONT;
  403. JMP_TAIL_CALL: {
  404. struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
  405. struct bpf_array *array = container_of(map, struct bpf_array, map);
  406. struct bpf_prog *prog;
  407. u64 index = BPF_R3;
  408. if (unlikely(index >= array->map.max_entries))
  409. goto out;
  410. if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
  411. goto out;
  412. tail_call_cnt++;
  413. prog = READ_ONCE(array->ptrs[index]);
  414. if (unlikely(!prog))
  415. goto out;
  416. /* ARG1 at this point is guaranteed to point to CTX from
  417. * the verifier side due to the fact that the tail call is
  418. * handeled like a helper, that is, bpf_tail_call_proto,
  419. * where arg1_type is ARG_PTR_TO_CTX.
  420. */
  421. insn = prog->insnsi;
  422. goto select_insn;
  423. out:
  424. CONT;
  425. }
  426. /* JMP */
  427. JMP_JA:
  428. insn += insn->off;
  429. CONT;
  430. JMP_JEQ_X:
  431. if (DST == SRC) {
  432. insn += insn->off;
  433. CONT_JMP;
  434. }
  435. CONT;
  436. JMP_JEQ_K:
  437. if (DST == IMM) {
  438. insn += insn->off;
  439. CONT_JMP;
  440. }
  441. CONT;
  442. JMP_JNE_X:
  443. if (DST != SRC) {
  444. insn += insn->off;
  445. CONT_JMP;
  446. }
  447. CONT;
  448. JMP_JNE_K:
  449. if (DST != IMM) {
  450. insn += insn->off;
  451. CONT_JMP;
  452. }
  453. CONT;
  454. JMP_JGT_X:
  455. if (DST > SRC) {
  456. insn += insn->off;
  457. CONT_JMP;
  458. }
  459. CONT;
  460. JMP_JGT_K:
  461. if (DST > IMM) {
  462. insn += insn->off;
  463. CONT_JMP;
  464. }
  465. CONT;
  466. JMP_JGE_X:
  467. if (DST >= SRC) {
  468. insn += insn->off;
  469. CONT_JMP;
  470. }
  471. CONT;
  472. JMP_JGE_K:
  473. if (DST >= IMM) {
  474. insn += insn->off;
  475. CONT_JMP;
  476. }
  477. CONT;
  478. JMP_JSGT_X:
  479. if (((s64) DST) > ((s64) SRC)) {
  480. insn += insn->off;
  481. CONT_JMP;
  482. }
  483. CONT;
  484. JMP_JSGT_K:
  485. if (((s64) DST) > ((s64) IMM)) {
  486. insn += insn->off;
  487. CONT_JMP;
  488. }
  489. CONT;
  490. JMP_JSGE_X:
  491. if (((s64) DST) >= ((s64) SRC)) {
  492. insn += insn->off;
  493. CONT_JMP;
  494. }
  495. CONT;
  496. JMP_JSGE_K:
  497. if (((s64) DST) >= ((s64) IMM)) {
  498. insn += insn->off;
  499. CONT_JMP;
  500. }
  501. CONT;
  502. JMP_JSET_X:
  503. if (DST & SRC) {
  504. insn += insn->off;
  505. CONT_JMP;
  506. }
  507. CONT;
  508. JMP_JSET_K:
  509. if (DST & IMM) {
  510. insn += insn->off;
  511. CONT_JMP;
  512. }
  513. CONT;
  514. JMP_EXIT:
  515. return BPF_R0;
  516. /* STX and ST and LDX*/
  517. #define LDST(SIZEOP, SIZE) \
  518. STX_MEM_##SIZEOP: \
  519. *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
  520. CONT; \
  521. ST_MEM_##SIZEOP: \
  522. *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
  523. CONT; \
  524. LDX_MEM_##SIZEOP: \
  525. DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
  526. CONT;
  527. LDST(B, u8)
  528. LDST(H, u16)
  529. LDST(W, u32)
  530. LDST(DW, u64)
  531. #undef LDST
  532. STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
  533. atomic_add((u32) SRC, (atomic_t *)(unsigned long)
  534. (DST + insn->off));
  535. CONT;
  536. STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
  537. atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
  538. (DST + insn->off));
  539. CONT;
  540. LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
  541. off = IMM;
  542. load_word:
  543. /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
  544. * only appearing in the programs where ctx ==
  545. * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
  546. * == BPF_R6, bpf_convert_filter() saves it in BPF_R6,
  547. * internal BPF verifier will check that BPF_R6 ==
  548. * ctx.
  549. *
  550. * BPF_ABS and BPF_IND are wrappers of function calls,
  551. * so they scratch BPF_R1-BPF_R5 registers, preserve
  552. * BPF_R6-BPF_R9, and store return value into BPF_R0.
  553. *
  554. * Implicit input:
  555. * ctx == skb == BPF_R6 == CTX
  556. *
  557. * Explicit input:
  558. * SRC == any register
  559. * IMM == 32-bit immediate
  560. *
  561. * Output:
  562. * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
  563. */
  564. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
  565. if (likely(ptr != NULL)) {
  566. BPF_R0 = get_unaligned_be32(ptr);
  567. CONT;
  568. }
  569. return 0;
  570. LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
  571. off = IMM;
  572. load_half:
  573. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
  574. if (likely(ptr != NULL)) {
  575. BPF_R0 = get_unaligned_be16(ptr);
  576. CONT;
  577. }
  578. return 0;
  579. LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
  580. off = IMM;
  581. load_byte:
  582. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
  583. if (likely(ptr != NULL)) {
  584. BPF_R0 = *(u8 *)ptr;
  585. CONT;
  586. }
  587. return 0;
  588. LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
  589. off = IMM + SRC;
  590. goto load_word;
  591. LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
  592. off = IMM + SRC;
  593. goto load_half;
  594. LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
  595. off = IMM + SRC;
  596. goto load_byte;
  597. default_label:
  598. /* If we ever reach this, we have a bug somewhere. */
  599. WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
  600. return 0;
  601. }
  602. bool bpf_prog_array_compatible(struct bpf_array *array,
  603. const struct bpf_prog *fp)
  604. {
  605. if (!array->owner_prog_type) {
  606. /* There's no owner yet where we could check for
  607. * compatibility.
  608. */
  609. array->owner_prog_type = fp->type;
  610. array->owner_jited = fp->jited;
  611. return true;
  612. }
  613. return array->owner_prog_type == fp->type &&
  614. array->owner_jited == fp->jited;
  615. }
  616. static int bpf_check_tail_call(const struct bpf_prog *fp)
  617. {
  618. struct bpf_prog_aux *aux = fp->aux;
  619. int i;
  620. for (i = 0; i < aux->used_map_cnt; i++) {
  621. struct bpf_map *map = aux->used_maps[i];
  622. struct bpf_array *array;
  623. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  624. continue;
  625. array = container_of(map, struct bpf_array, map);
  626. if (!bpf_prog_array_compatible(array, fp))
  627. return -EINVAL;
  628. }
  629. return 0;
  630. }
  631. /**
  632. * bpf_prog_select_runtime - select exec runtime for BPF program
  633. * @fp: bpf_prog populated with internal BPF program
  634. *
  635. * Try to JIT eBPF program, if JIT is not available, use interpreter.
  636. * The BPF program will be executed via BPF_PROG_RUN() macro.
  637. */
  638. int bpf_prog_select_runtime(struct bpf_prog *fp)
  639. {
  640. fp->bpf_func = (void *) __bpf_prog_run;
  641. bpf_int_jit_compile(fp);
  642. bpf_prog_lock_ro(fp);
  643. /* The tail call compatibility check can only be done at
  644. * this late stage as we need to determine, if we deal
  645. * with JITed or non JITed program concatenations and not
  646. * all eBPF JITs might immediately support all features.
  647. */
  648. return bpf_check_tail_call(fp);
  649. }
  650. EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
  651. static void bpf_prog_free_deferred(struct work_struct *work)
  652. {
  653. struct bpf_prog_aux *aux;
  654. aux = container_of(work, struct bpf_prog_aux, work);
  655. bpf_jit_free(aux->prog);
  656. }
  657. /* Free internal BPF program */
  658. void bpf_prog_free(struct bpf_prog *fp)
  659. {
  660. struct bpf_prog_aux *aux = fp->aux;
  661. INIT_WORK(&aux->work, bpf_prog_free_deferred);
  662. schedule_work(&aux->work);
  663. }
  664. EXPORT_SYMBOL_GPL(bpf_prog_free);
  665. /* RNG for unpriviledged user space with separated state from prandom_u32(). */
  666. static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
  667. void bpf_user_rnd_init_once(void)
  668. {
  669. prandom_init_once(&bpf_user_rnd_state);
  670. }
  671. u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  672. {
  673. /* Should someone ever have the rather unwise idea to use some
  674. * of the registers passed into this function, then note that
  675. * this function is called from native eBPF and classic-to-eBPF
  676. * transformations. Register assignments from both sides are
  677. * different, f.e. classic always sets fn(ctx, A, X) here.
  678. */
  679. struct rnd_state *state;
  680. u32 res;
  681. state = &get_cpu_var(bpf_user_rnd_state);
  682. res = prandom_u32_state(state);
  683. put_cpu_var(state);
  684. return res;
  685. }
  686. /* Weak definitions of helper functions in case we don't have bpf syscall. */
  687. const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
  688. const struct bpf_func_proto bpf_map_update_elem_proto __weak;
  689. const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
  690. const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
  691. const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
  692. const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
  693. const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
  694. const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
  695. const struct bpf_func_proto bpf_get_current_comm_proto __weak;
  696. const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
  697. {
  698. return NULL;
  699. }
  700. /* Always built-in helper functions. */
  701. const struct bpf_func_proto bpf_tail_call_proto = {
  702. .func = NULL,
  703. .gpl_only = false,
  704. .ret_type = RET_VOID,
  705. .arg1_type = ARG_PTR_TO_CTX,
  706. .arg2_type = ARG_CONST_MAP_PTR,
  707. .arg3_type = ARG_ANYTHING,
  708. };
  709. /* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
  710. void __weak bpf_int_jit_compile(struct bpf_prog *prog)
  711. {
  712. }
  713. /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  714. * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  715. */
  716. int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
  717. int len)
  718. {
  719. return -EFAULT;
  720. }