core.c 23 KB

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