core.c 23 KB

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