core.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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 <linux/rbtree_latch.h>
  31. #include <linux/kallsyms.h>
  32. #include <linux/rcupdate.h>
  33. #include <asm/unaligned.h>
  34. /* Registers */
  35. #define BPF_R0 regs[BPF_REG_0]
  36. #define BPF_R1 regs[BPF_REG_1]
  37. #define BPF_R2 regs[BPF_REG_2]
  38. #define BPF_R3 regs[BPF_REG_3]
  39. #define BPF_R4 regs[BPF_REG_4]
  40. #define BPF_R5 regs[BPF_REG_5]
  41. #define BPF_R6 regs[BPF_REG_6]
  42. #define BPF_R7 regs[BPF_REG_7]
  43. #define BPF_R8 regs[BPF_REG_8]
  44. #define BPF_R9 regs[BPF_REG_9]
  45. #define BPF_R10 regs[BPF_REG_10]
  46. /* Named registers */
  47. #define DST regs[insn->dst_reg]
  48. #define SRC regs[insn->src_reg]
  49. #define FP regs[BPF_REG_FP]
  50. #define ARG1 regs[BPF_REG_ARG1]
  51. #define CTX regs[BPF_REG_CTX]
  52. #define IMM insn->imm
  53. /* No hurry in this branch
  54. *
  55. * Exported for the bpf jit load helper.
  56. */
  57. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
  58. {
  59. u8 *ptr = NULL;
  60. if (k >= SKF_NET_OFF)
  61. ptr = skb_network_header(skb) + k - SKF_NET_OFF;
  62. else if (k >= SKF_LL_OFF)
  63. ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
  64. if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
  65. return ptr;
  66. return NULL;
  67. }
  68. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
  69. {
  70. gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
  71. struct bpf_prog_aux *aux;
  72. struct bpf_prog *fp;
  73. size = round_up(size, PAGE_SIZE);
  74. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  75. if (fp == NULL)
  76. return NULL;
  77. kmemcheck_annotate_bitfield(fp, meta);
  78. aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
  79. if (aux == NULL) {
  80. vfree(fp);
  81. return NULL;
  82. }
  83. fp->pages = size / PAGE_SIZE;
  84. fp->aux = aux;
  85. fp->aux->prog = fp;
  86. INIT_LIST_HEAD_RCU(&fp->aux->ksym_lnode);
  87. return fp;
  88. }
  89. EXPORT_SYMBOL_GPL(bpf_prog_alloc);
  90. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  91. gfp_t gfp_extra_flags)
  92. {
  93. gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
  94. struct bpf_prog *fp;
  95. u32 pages, delta;
  96. int ret;
  97. BUG_ON(fp_old == NULL);
  98. size = round_up(size, PAGE_SIZE);
  99. pages = size / PAGE_SIZE;
  100. if (pages <= fp_old->pages)
  101. return fp_old;
  102. delta = pages - fp_old->pages;
  103. ret = __bpf_prog_charge(fp_old->aux->user, delta);
  104. if (ret)
  105. return NULL;
  106. fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  107. if (fp == NULL) {
  108. __bpf_prog_uncharge(fp_old->aux->user, delta);
  109. } else {
  110. kmemcheck_annotate_bitfield(fp, meta);
  111. memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
  112. fp->pages = pages;
  113. fp->aux->prog = fp;
  114. /* We keep fp->aux from fp_old around in the new
  115. * reallocated structure.
  116. */
  117. fp_old->aux = NULL;
  118. __bpf_prog_free(fp_old);
  119. }
  120. return fp;
  121. }
  122. void __bpf_prog_free(struct bpf_prog *fp)
  123. {
  124. kfree(fp->aux);
  125. vfree(fp);
  126. }
  127. int bpf_prog_calc_tag(struct bpf_prog *fp)
  128. {
  129. const u32 bits_offset = SHA_MESSAGE_BYTES - sizeof(__be64);
  130. u32 raw_size = bpf_prog_tag_scratch_size(fp);
  131. u32 digest[SHA_DIGEST_WORDS];
  132. u32 ws[SHA_WORKSPACE_WORDS];
  133. u32 i, bsize, psize, blocks;
  134. struct bpf_insn *dst;
  135. bool was_ld_map;
  136. u8 *raw, *todo;
  137. __be32 *result;
  138. __be64 *bits;
  139. raw = vmalloc(raw_size);
  140. if (!raw)
  141. return -ENOMEM;
  142. sha_init(digest);
  143. memset(ws, 0, sizeof(ws));
  144. /* We need to take out the map fd for the digest calculation
  145. * since they are unstable from user space side.
  146. */
  147. dst = (void *)raw;
  148. for (i = 0, was_ld_map = false; i < fp->len; i++) {
  149. dst[i] = fp->insnsi[i];
  150. if (!was_ld_map &&
  151. dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  152. dst[i].src_reg == BPF_PSEUDO_MAP_FD) {
  153. was_ld_map = true;
  154. dst[i].imm = 0;
  155. } else if (was_ld_map &&
  156. dst[i].code == 0 &&
  157. dst[i].dst_reg == 0 &&
  158. dst[i].src_reg == 0 &&
  159. dst[i].off == 0) {
  160. was_ld_map = false;
  161. dst[i].imm = 0;
  162. } else {
  163. was_ld_map = false;
  164. }
  165. }
  166. psize = bpf_prog_insn_size(fp);
  167. memset(&raw[psize], 0, raw_size - psize);
  168. raw[psize++] = 0x80;
  169. bsize = round_up(psize, SHA_MESSAGE_BYTES);
  170. blocks = bsize / SHA_MESSAGE_BYTES;
  171. todo = raw;
  172. if (bsize - psize >= sizeof(__be64)) {
  173. bits = (__be64 *)(todo + bsize - sizeof(__be64));
  174. } else {
  175. bits = (__be64 *)(todo + bsize + bits_offset);
  176. blocks++;
  177. }
  178. *bits = cpu_to_be64((psize - 1) << 3);
  179. while (blocks--) {
  180. sha_transform(digest, todo, ws);
  181. todo += SHA_MESSAGE_BYTES;
  182. }
  183. result = (__force __be32 *)digest;
  184. for (i = 0; i < SHA_DIGEST_WORDS; i++)
  185. result[i] = cpu_to_be32(digest[i]);
  186. memcpy(fp->tag, result, sizeof(fp->tag));
  187. vfree(raw);
  188. return 0;
  189. }
  190. static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn)
  191. {
  192. return BPF_CLASS(insn->code) == BPF_JMP &&
  193. /* Call and Exit are both special jumps with no
  194. * target inside the BPF instruction image.
  195. */
  196. BPF_OP(insn->code) != BPF_CALL &&
  197. BPF_OP(insn->code) != BPF_EXIT;
  198. }
  199. static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta)
  200. {
  201. struct bpf_insn *insn = prog->insnsi;
  202. u32 i, insn_cnt = prog->len;
  203. for (i = 0; i < insn_cnt; i++, insn++) {
  204. if (!bpf_is_jmp_and_has_target(insn))
  205. continue;
  206. /* Adjust offset of jmps if we cross boundaries. */
  207. if (i < pos && i + insn->off + 1 > pos)
  208. insn->off += delta;
  209. else if (i > pos + delta && i + insn->off + 1 <= pos + delta)
  210. insn->off -= delta;
  211. }
  212. }
  213. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  214. const struct bpf_insn *patch, u32 len)
  215. {
  216. u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
  217. struct bpf_prog *prog_adj;
  218. /* Since our patchlet doesn't expand the image, we're done. */
  219. if (insn_delta == 0) {
  220. memcpy(prog->insnsi + off, patch, sizeof(*patch));
  221. return prog;
  222. }
  223. insn_adj_cnt = prog->len + insn_delta;
  224. /* Several new instructions need to be inserted. Make room
  225. * for them. Likely, there's no need for a new allocation as
  226. * last page could have large enough tailroom.
  227. */
  228. prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
  229. GFP_USER);
  230. if (!prog_adj)
  231. return NULL;
  232. prog_adj->len = insn_adj_cnt;
  233. /* Patching happens in 3 steps:
  234. *
  235. * 1) Move over tail of insnsi from next instruction onwards,
  236. * so we can patch the single target insn with one or more
  237. * new ones (patching is always from 1 to n insns, n > 0).
  238. * 2) Inject new instructions at the target location.
  239. * 3) Adjust branch offsets if necessary.
  240. */
  241. insn_rest = insn_adj_cnt - off - len;
  242. memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
  243. sizeof(*patch) * insn_rest);
  244. memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
  245. bpf_adj_branches(prog_adj, off, insn_delta);
  246. return prog_adj;
  247. }
  248. #ifdef CONFIG_BPF_JIT
  249. static __always_inline void
  250. bpf_get_prog_addr_region(const struct bpf_prog *prog,
  251. unsigned long *symbol_start,
  252. unsigned long *symbol_end)
  253. {
  254. const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog);
  255. unsigned long addr = (unsigned long)hdr;
  256. WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
  257. *symbol_start = addr;
  258. *symbol_end = addr + hdr->pages * PAGE_SIZE;
  259. }
  260. static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
  261. {
  262. BUILD_BUG_ON(sizeof("bpf_prog_") +
  263. sizeof(prog->tag) * 2 + 1 > KSYM_NAME_LEN);
  264. sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
  265. sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
  266. *sym = 0;
  267. }
  268. static __always_inline unsigned long
  269. bpf_get_prog_addr_start(struct latch_tree_node *n)
  270. {
  271. unsigned long symbol_start, symbol_end;
  272. const struct bpf_prog_aux *aux;
  273. aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
  274. bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
  275. return symbol_start;
  276. }
  277. static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
  278. struct latch_tree_node *b)
  279. {
  280. return bpf_get_prog_addr_start(a) < bpf_get_prog_addr_start(b);
  281. }
  282. static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
  283. {
  284. unsigned long val = (unsigned long)key;
  285. unsigned long symbol_start, symbol_end;
  286. const struct bpf_prog_aux *aux;
  287. aux = container_of(n, struct bpf_prog_aux, ksym_tnode);
  288. bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
  289. if (val < symbol_start)
  290. return -1;
  291. if (val >= symbol_end)
  292. return 1;
  293. return 0;
  294. }
  295. static const struct latch_tree_ops bpf_tree_ops = {
  296. .less = bpf_tree_less,
  297. .comp = bpf_tree_comp,
  298. };
  299. static DEFINE_SPINLOCK(bpf_lock);
  300. static LIST_HEAD(bpf_kallsyms);
  301. static struct latch_tree_root bpf_tree __cacheline_aligned;
  302. int bpf_jit_kallsyms __read_mostly;
  303. static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)
  304. {
  305. WARN_ON_ONCE(!list_empty(&aux->ksym_lnode));
  306. list_add_tail_rcu(&aux->ksym_lnode, &bpf_kallsyms);
  307. latch_tree_insert(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
  308. }
  309. static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)
  310. {
  311. if (list_empty(&aux->ksym_lnode))
  312. return;
  313. latch_tree_erase(&aux->ksym_tnode, &bpf_tree, &bpf_tree_ops);
  314. list_del_rcu(&aux->ksym_lnode);
  315. }
  316. static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
  317. {
  318. return fp->jited && !bpf_prog_was_classic(fp);
  319. }
  320. static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
  321. {
  322. return list_empty(&fp->aux->ksym_lnode) ||
  323. fp->aux->ksym_lnode.prev == LIST_POISON2;
  324. }
  325. void bpf_prog_kallsyms_add(struct bpf_prog *fp)
  326. {
  327. if (!bpf_prog_kallsyms_candidate(fp) ||
  328. !capable(CAP_SYS_ADMIN))
  329. return;
  330. spin_lock_bh(&bpf_lock);
  331. bpf_prog_ksym_node_add(fp->aux);
  332. spin_unlock_bh(&bpf_lock);
  333. }
  334. void bpf_prog_kallsyms_del(struct bpf_prog *fp)
  335. {
  336. if (!bpf_prog_kallsyms_candidate(fp))
  337. return;
  338. spin_lock_bh(&bpf_lock);
  339. bpf_prog_ksym_node_del(fp->aux);
  340. spin_unlock_bh(&bpf_lock);
  341. }
  342. static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)
  343. {
  344. struct latch_tree_node *n;
  345. if (!bpf_jit_kallsyms_enabled())
  346. return NULL;
  347. n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
  348. return n ?
  349. container_of(n, struct bpf_prog_aux, ksym_tnode)->prog :
  350. NULL;
  351. }
  352. const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
  353. unsigned long *off, char *sym)
  354. {
  355. unsigned long symbol_start, symbol_end;
  356. struct bpf_prog *prog;
  357. char *ret = NULL;
  358. rcu_read_lock();
  359. prog = bpf_prog_kallsyms_find(addr);
  360. if (prog) {
  361. bpf_get_prog_addr_region(prog, &symbol_start, &symbol_end);
  362. bpf_get_prog_name(prog, sym);
  363. ret = sym;
  364. if (size)
  365. *size = symbol_end - symbol_start;
  366. if (off)
  367. *off = addr - symbol_start;
  368. }
  369. rcu_read_unlock();
  370. return ret;
  371. }
  372. bool is_bpf_text_address(unsigned long addr)
  373. {
  374. bool ret;
  375. rcu_read_lock();
  376. ret = bpf_prog_kallsyms_find(addr) != NULL;
  377. rcu_read_unlock();
  378. return ret;
  379. }
  380. int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  381. char *sym)
  382. {
  383. unsigned long symbol_start, symbol_end;
  384. struct bpf_prog_aux *aux;
  385. unsigned int it = 0;
  386. int ret = -ERANGE;
  387. if (!bpf_jit_kallsyms_enabled())
  388. return ret;
  389. rcu_read_lock();
  390. list_for_each_entry_rcu(aux, &bpf_kallsyms, ksym_lnode) {
  391. if (it++ != symnum)
  392. continue;
  393. bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
  394. bpf_get_prog_name(aux->prog, sym);
  395. *value = symbol_start;
  396. *type = BPF_SYM_ELF_TYPE;
  397. ret = 0;
  398. break;
  399. }
  400. rcu_read_unlock();
  401. return ret;
  402. }
  403. struct bpf_binary_header *
  404. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  405. unsigned int alignment,
  406. bpf_jit_fill_hole_t bpf_fill_ill_insns)
  407. {
  408. struct bpf_binary_header *hdr;
  409. unsigned int size, hole, start;
  410. /* Most of BPF filters are really small, but if some of them
  411. * fill a page, allow at least 128 extra bytes to insert a
  412. * random section of illegal instructions.
  413. */
  414. size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
  415. hdr = module_alloc(size);
  416. if (hdr == NULL)
  417. return NULL;
  418. /* Fill space with illegal/arch-dep instructions. */
  419. bpf_fill_ill_insns(hdr, size);
  420. hdr->pages = size / PAGE_SIZE;
  421. hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
  422. PAGE_SIZE - sizeof(*hdr));
  423. start = (get_random_int() % hole) & ~(alignment - 1);
  424. /* Leave a random number of instructions before BPF code. */
  425. *image_ptr = &hdr->image[start];
  426. return hdr;
  427. }
  428. void bpf_jit_binary_free(struct bpf_binary_header *hdr)
  429. {
  430. module_memfree(hdr);
  431. }
  432. /* This symbol is only overridden by archs that have different
  433. * requirements than the usual eBPF JITs, f.e. when they only
  434. * implement cBPF JIT, do not set images read-only, etc.
  435. */
  436. void __weak bpf_jit_free(struct bpf_prog *fp)
  437. {
  438. if (fp->jited) {
  439. struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
  440. bpf_jit_binary_unlock_ro(hdr);
  441. bpf_jit_binary_free(hdr);
  442. WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
  443. }
  444. bpf_prog_unlock_free(fp);
  445. }
  446. int bpf_jit_harden __read_mostly;
  447. static int bpf_jit_blind_insn(const struct bpf_insn *from,
  448. const struct bpf_insn *aux,
  449. struct bpf_insn *to_buff)
  450. {
  451. struct bpf_insn *to = to_buff;
  452. u32 imm_rnd = get_random_int();
  453. s16 off;
  454. BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
  455. BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
  456. if (from->imm == 0 &&
  457. (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
  458. from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
  459. *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
  460. goto out;
  461. }
  462. switch (from->code) {
  463. case BPF_ALU | BPF_ADD | BPF_K:
  464. case BPF_ALU | BPF_SUB | BPF_K:
  465. case BPF_ALU | BPF_AND | BPF_K:
  466. case BPF_ALU | BPF_OR | BPF_K:
  467. case BPF_ALU | BPF_XOR | BPF_K:
  468. case BPF_ALU | BPF_MUL | BPF_K:
  469. case BPF_ALU | BPF_MOV | BPF_K:
  470. case BPF_ALU | BPF_DIV | BPF_K:
  471. case BPF_ALU | BPF_MOD | BPF_K:
  472. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  473. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  474. *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
  475. break;
  476. case BPF_ALU64 | BPF_ADD | BPF_K:
  477. case BPF_ALU64 | BPF_SUB | BPF_K:
  478. case BPF_ALU64 | BPF_AND | BPF_K:
  479. case BPF_ALU64 | BPF_OR | BPF_K:
  480. case BPF_ALU64 | BPF_XOR | BPF_K:
  481. case BPF_ALU64 | BPF_MUL | BPF_K:
  482. case BPF_ALU64 | BPF_MOV | BPF_K:
  483. case BPF_ALU64 | BPF_DIV | BPF_K:
  484. case BPF_ALU64 | BPF_MOD | BPF_K:
  485. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  486. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  487. *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
  488. break;
  489. case BPF_JMP | BPF_JEQ | BPF_K:
  490. case BPF_JMP | BPF_JNE | BPF_K:
  491. case BPF_JMP | BPF_JGT | BPF_K:
  492. case BPF_JMP | BPF_JGE | BPF_K:
  493. case BPF_JMP | BPF_JSGT | BPF_K:
  494. case BPF_JMP | BPF_JSGE | BPF_K:
  495. case BPF_JMP | BPF_JSET | BPF_K:
  496. /* Accommodate for extra offset in case of a backjump. */
  497. off = from->off;
  498. if (off < 0)
  499. off -= 2;
  500. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  501. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  502. *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
  503. break;
  504. case BPF_LD | BPF_ABS | BPF_W:
  505. case BPF_LD | BPF_ABS | BPF_H:
  506. case BPF_LD | BPF_ABS | BPF_B:
  507. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  508. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  509. *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
  510. break;
  511. case BPF_LD | BPF_IND | BPF_W:
  512. case BPF_LD | BPF_IND | BPF_H:
  513. case BPF_LD | BPF_IND | BPF_B:
  514. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  515. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  516. *to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);
  517. *to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);
  518. break;
  519. case BPF_LD | BPF_IMM | BPF_DW:
  520. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
  521. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  522. *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
  523. *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
  524. break;
  525. case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
  526. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
  527. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  528. *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
  529. break;
  530. case BPF_ST | BPF_MEM | BPF_DW:
  531. case BPF_ST | BPF_MEM | BPF_W:
  532. case BPF_ST | BPF_MEM | BPF_H:
  533. case BPF_ST | BPF_MEM | BPF_B:
  534. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  535. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  536. *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
  537. break;
  538. }
  539. out:
  540. return to - to_buff;
  541. }
  542. static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
  543. gfp_t gfp_extra_flags)
  544. {
  545. gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
  546. struct bpf_prog *fp;
  547. fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);
  548. if (fp != NULL) {
  549. kmemcheck_annotate_bitfield(fp, meta);
  550. /* aux->prog still points to the fp_other one, so
  551. * when promoting the clone to the real program,
  552. * this still needs to be adapted.
  553. */
  554. memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
  555. }
  556. return fp;
  557. }
  558. static void bpf_prog_clone_free(struct bpf_prog *fp)
  559. {
  560. /* aux was stolen by the other clone, so we cannot free
  561. * it from this path! It will be freed eventually by the
  562. * other program on release.
  563. *
  564. * At this point, we don't need a deferred release since
  565. * clone is guaranteed to not be locked.
  566. */
  567. fp->aux = NULL;
  568. __bpf_prog_free(fp);
  569. }
  570. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
  571. {
  572. /* We have to repoint aux->prog to self, as we don't
  573. * know whether fp here is the clone or the original.
  574. */
  575. fp->aux->prog = fp;
  576. bpf_prog_clone_free(fp_other);
  577. }
  578. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
  579. {
  580. struct bpf_insn insn_buff[16], aux[2];
  581. struct bpf_prog *clone, *tmp;
  582. int insn_delta, insn_cnt;
  583. struct bpf_insn *insn;
  584. int i, rewritten;
  585. if (!bpf_jit_blinding_enabled())
  586. return prog;
  587. clone = bpf_prog_clone_create(prog, GFP_USER);
  588. if (!clone)
  589. return ERR_PTR(-ENOMEM);
  590. insn_cnt = clone->len;
  591. insn = clone->insnsi;
  592. for (i = 0; i < insn_cnt; i++, insn++) {
  593. /* We temporarily need to hold the original ld64 insn
  594. * so that we can still access the first part in the
  595. * second blinding run.
  596. */
  597. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  598. insn[1].code == 0)
  599. memcpy(aux, insn, sizeof(aux));
  600. rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);
  601. if (!rewritten)
  602. continue;
  603. tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
  604. if (!tmp) {
  605. /* Patching may have repointed aux->prog during
  606. * realloc from the original one, so we need to
  607. * fix it up here on error.
  608. */
  609. bpf_jit_prog_release_other(prog, clone);
  610. return ERR_PTR(-ENOMEM);
  611. }
  612. clone = tmp;
  613. insn_delta = rewritten - 1;
  614. /* Walk new program and skip insns we just inserted. */
  615. insn = clone->insnsi + i + insn_delta;
  616. insn_cnt += insn_delta;
  617. i += insn_delta;
  618. }
  619. return clone;
  620. }
  621. #endif /* CONFIG_BPF_JIT */
  622. /* Base function for offset calculation. Needs to go into .text section,
  623. * therefore keeping it non-static as well; will also be used by JITs
  624. * anyway later on, so do not let the compiler omit it.
  625. */
  626. noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  627. {
  628. return 0;
  629. }
  630. EXPORT_SYMBOL_GPL(__bpf_call_base);
  631. /**
  632. * __bpf_prog_run - run eBPF program on a given context
  633. * @ctx: is the data we are operating on
  634. * @insn: is the array of eBPF instructions
  635. *
  636. * Decode and execute eBPF instructions.
  637. */
  638. static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
  639. {
  640. u64 stack[MAX_BPF_STACK / sizeof(u64)];
  641. u64 regs[MAX_BPF_REG], tmp;
  642. static const void *jumptable[256] = {
  643. [0 ... 255] = &&default_label,
  644. /* Now overwrite non-defaults ... */
  645. /* 32 bit ALU operations */
  646. [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
  647. [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
  648. [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
  649. [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
  650. [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
  651. [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
  652. [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
  653. [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
  654. [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
  655. [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
  656. [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
  657. [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
  658. [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
  659. [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
  660. [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
  661. [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
  662. [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
  663. [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
  664. [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
  665. [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
  666. [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
  667. [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
  668. [BPF_ALU | BPF_NEG] = &&ALU_NEG,
  669. [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
  670. [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
  671. /* 64 bit ALU operations */
  672. [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
  673. [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
  674. [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
  675. [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
  676. [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
  677. [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
  678. [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
  679. [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
  680. [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
  681. [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
  682. [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
  683. [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
  684. [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
  685. [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
  686. [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
  687. [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
  688. [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
  689. [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
  690. [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
  691. [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
  692. [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
  693. [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
  694. [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
  695. [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
  696. [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
  697. /* Call instruction */
  698. [BPF_JMP | BPF_CALL] = &&JMP_CALL,
  699. [BPF_JMP | BPF_CALL | BPF_X] = &&JMP_TAIL_CALL,
  700. /* Jumps */
  701. [BPF_JMP | BPF_JA] = &&JMP_JA,
  702. [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
  703. [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
  704. [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
  705. [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
  706. [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
  707. [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
  708. [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
  709. [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
  710. [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
  711. [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
  712. [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
  713. [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
  714. [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
  715. [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
  716. /* Program return */
  717. [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
  718. /* Store instructions */
  719. [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
  720. [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
  721. [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
  722. [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
  723. [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
  724. [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
  725. [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
  726. [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
  727. [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
  728. [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
  729. /* Load instructions */
  730. [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
  731. [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
  732. [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
  733. [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
  734. [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
  735. [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
  736. [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
  737. [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
  738. [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
  739. [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
  740. [BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
  741. };
  742. u32 tail_call_cnt = 0;
  743. void *ptr;
  744. int off;
  745. #define CONT ({ insn++; goto select_insn; })
  746. #define CONT_JMP ({ insn++; goto select_insn; })
  747. FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
  748. ARG1 = (u64) (unsigned long) ctx;
  749. select_insn:
  750. goto *jumptable[insn->code];
  751. /* ALU */
  752. #define ALU(OPCODE, OP) \
  753. ALU64_##OPCODE##_X: \
  754. DST = DST OP SRC; \
  755. CONT; \
  756. ALU_##OPCODE##_X: \
  757. DST = (u32) DST OP (u32) SRC; \
  758. CONT; \
  759. ALU64_##OPCODE##_K: \
  760. DST = DST OP IMM; \
  761. CONT; \
  762. ALU_##OPCODE##_K: \
  763. DST = (u32) DST OP (u32) IMM; \
  764. CONT;
  765. ALU(ADD, +)
  766. ALU(SUB, -)
  767. ALU(AND, &)
  768. ALU(OR, |)
  769. ALU(LSH, <<)
  770. ALU(RSH, >>)
  771. ALU(XOR, ^)
  772. ALU(MUL, *)
  773. #undef ALU
  774. ALU_NEG:
  775. DST = (u32) -DST;
  776. CONT;
  777. ALU64_NEG:
  778. DST = -DST;
  779. CONT;
  780. ALU_MOV_X:
  781. DST = (u32) SRC;
  782. CONT;
  783. ALU_MOV_K:
  784. DST = (u32) IMM;
  785. CONT;
  786. ALU64_MOV_X:
  787. DST = SRC;
  788. CONT;
  789. ALU64_MOV_K:
  790. DST = IMM;
  791. CONT;
  792. LD_IMM_DW:
  793. DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
  794. insn++;
  795. CONT;
  796. ALU64_ARSH_X:
  797. (*(s64 *) &DST) >>= SRC;
  798. CONT;
  799. ALU64_ARSH_K:
  800. (*(s64 *) &DST) >>= IMM;
  801. CONT;
  802. ALU64_MOD_X:
  803. if (unlikely(SRC == 0))
  804. return 0;
  805. div64_u64_rem(DST, SRC, &tmp);
  806. DST = tmp;
  807. CONT;
  808. ALU_MOD_X:
  809. if (unlikely(SRC == 0))
  810. return 0;
  811. tmp = (u32) DST;
  812. DST = do_div(tmp, (u32) SRC);
  813. CONT;
  814. ALU64_MOD_K:
  815. div64_u64_rem(DST, IMM, &tmp);
  816. DST = tmp;
  817. CONT;
  818. ALU_MOD_K:
  819. tmp = (u32) DST;
  820. DST = do_div(tmp, (u32) IMM);
  821. CONT;
  822. ALU64_DIV_X:
  823. if (unlikely(SRC == 0))
  824. return 0;
  825. DST = div64_u64(DST, SRC);
  826. CONT;
  827. ALU_DIV_X:
  828. if (unlikely(SRC == 0))
  829. return 0;
  830. tmp = (u32) DST;
  831. do_div(tmp, (u32) SRC);
  832. DST = (u32) tmp;
  833. CONT;
  834. ALU64_DIV_K:
  835. DST = div64_u64(DST, IMM);
  836. CONT;
  837. ALU_DIV_K:
  838. tmp = (u32) DST;
  839. do_div(tmp, (u32) IMM);
  840. DST = (u32) tmp;
  841. CONT;
  842. ALU_END_TO_BE:
  843. switch (IMM) {
  844. case 16:
  845. DST = (__force u16) cpu_to_be16(DST);
  846. break;
  847. case 32:
  848. DST = (__force u32) cpu_to_be32(DST);
  849. break;
  850. case 64:
  851. DST = (__force u64) cpu_to_be64(DST);
  852. break;
  853. }
  854. CONT;
  855. ALU_END_TO_LE:
  856. switch (IMM) {
  857. case 16:
  858. DST = (__force u16) cpu_to_le16(DST);
  859. break;
  860. case 32:
  861. DST = (__force u32) cpu_to_le32(DST);
  862. break;
  863. case 64:
  864. DST = (__force u64) cpu_to_le64(DST);
  865. break;
  866. }
  867. CONT;
  868. /* CALL */
  869. JMP_CALL:
  870. /* Function call scratches BPF_R1-BPF_R5 registers,
  871. * preserves BPF_R6-BPF_R9, and stores return value
  872. * into BPF_R0.
  873. */
  874. BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
  875. BPF_R4, BPF_R5);
  876. CONT;
  877. JMP_TAIL_CALL: {
  878. struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
  879. struct bpf_array *array = container_of(map, struct bpf_array, map);
  880. struct bpf_prog *prog;
  881. u64 index = BPF_R3;
  882. if (unlikely(index >= array->map.max_entries))
  883. goto out;
  884. if (unlikely(tail_call_cnt > MAX_TAIL_CALL_CNT))
  885. goto out;
  886. tail_call_cnt++;
  887. prog = READ_ONCE(array->ptrs[index]);
  888. if (!prog)
  889. goto out;
  890. /* ARG1 at this point is guaranteed to point to CTX from
  891. * the verifier side due to the fact that the tail call is
  892. * handeled like a helper, that is, bpf_tail_call_proto,
  893. * where arg1_type is ARG_PTR_TO_CTX.
  894. */
  895. insn = prog->insnsi;
  896. goto select_insn;
  897. out:
  898. CONT;
  899. }
  900. /* JMP */
  901. JMP_JA:
  902. insn += insn->off;
  903. CONT;
  904. JMP_JEQ_X:
  905. if (DST == SRC) {
  906. insn += insn->off;
  907. CONT_JMP;
  908. }
  909. CONT;
  910. JMP_JEQ_K:
  911. if (DST == IMM) {
  912. insn += insn->off;
  913. CONT_JMP;
  914. }
  915. CONT;
  916. JMP_JNE_X:
  917. if (DST != SRC) {
  918. insn += insn->off;
  919. CONT_JMP;
  920. }
  921. CONT;
  922. JMP_JNE_K:
  923. if (DST != IMM) {
  924. insn += insn->off;
  925. CONT_JMP;
  926. }
  927. CONT;
  928. JMP_JGT_X:
  929. if (DST > SRC) {
  930. insn += insn->off;
  931. CONT_JMP;
  932. }
  933. CONT;
  934. JMP_JGT_K:
  935. if (DST > IMM) {
  936. insn += insn->off;
  937. CONT_JMP;
  938. }
  939. CONT;
  940. JMP_JGE_X:
  941. if (DST >= SRC) {
  942. insn += insn->off;
  943. CONT_JMP;
  944. }
  945. CONT;
  946. JMP_JGE_K:
  947. if (DST >= IMM) {
  948. insn += insn->off;
  949. CONT_JMP;
  950. }
  951. CONT;
  952. JMP_JSGT_X:
  953. if (((s64) DST) > ((s64) SRC)) {
  954. insn += insn->off;
  955. CONT_JMP;
  956. }
  957. CONT;
  958. JMP_JSGT_K:
  959. if (((s64) DST) > ((s64) IMM)) {
  960. insn += insn->off;
  961. CONT_JMP;
  962. }
  963. CONT;
  964. JMP_JSGE_X:
  965. if (((s64) DST) >= ((s64) SRC)) {
  966. insn += insn->off;
  967. CONT_JMP;
  968. }
  969. CONT;
  970. JMP_JSGE_K:
  971. if (((s64) DST) >= ((s64) IMM)) {
  972. insn += insn->off;
  973. CONT_JMP;
  974. }
  975. CONT;
  976. JMP_JSET_X:
  977. if (DST & SRC) {
  978. insn += insn->off;
  979. CONT_JMP;
  980. }
  981. CONT;
  982. JMP_JSET_K:
  983. if (DST & IMM) {
  984. insn += insn->off;
  985. CONT_JMP;
  986. }
  987. CONT;
  988. JMP_EXIT:
  989. return BPF_R0;
  990. /* STX and ST and LDX*/
  991. #define LDST(SIZEOP, SIZE) \
  992. STX_MEM_##SIZEOP: \
  993. *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
  994. CONT; \
  995. ST_MEM_##SIZEOP: \
  996. *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
  997. CONT; \
  998. LDX_MEM_##SIZEOP: \
  999. DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
  1000. CONT;
  1001. LDST(B, u8)
  1002. LDST(H, u16)
  1003. LDST(W, u32)
  1004. LDST(DW, u64)
  1005. #undef LDST
  1006. STX_XADD_W: /* lock xadd *(u32 *)(dst_reg + off16) += src_reg */
  1007. atomic_add((u32) SRC, (atomic_t *)(unsigned long)
  1008. (DST + insn->off));
  1009. CONT;
  1010. STX_XADD_DW: /* lock xadd *(u64 *)(dst_reg + off16) += src_reg */
  1011. atomic64_add((u64) SRC, (atomic64_t *)(unsigned long)
  1012. (DST + insn->off));
  1013. CONT;
  1014. LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + imm32)) */
  1015. off = IMM;
  1016. load_word:
  1017. /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are only
  1018. * appearing in the programs where ctx == skb
  1019. * (see may_access_skb() in the verifier). All programs
  1020. * keep 'ctx' in regs[BPF_REG_CTX] == BPF_R6,
  1021. * bpf_convert_filter() saves it in BPF_R6, internal BPF
  1022. * verifier will check that BPF_R6 == ctx.
  1023. *
  1024. * BPF_ABS and BPF_IND are wrappers of function calls,
  1025. * so they scratch BPF_R1-BPF_R5 registers, preserve
  1026. * BPF_R6-BPF_R9, and store return value into BPF_R0.
  1027. *
  1028. * Implicit input:
  1029. * ctx == skb == BPF_R6 == CTX
  1030. *
  1031. * Explicit input:
  1032. * SRC == any register
  1033. * IMM == 32-bit immediate
  1034. *
  1035. * Output:
  1036. * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
  1037. */
  1038. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 4, &tmp);
  1039. if (likely(ptr != NULL)) {
  1040. BPF_R0 = get_unaligned_be32(ptr);
  1041. CONT;
  1042. }
  1043. return 0;
  1044. LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + imm32)) */
  1045. off = IMM;
  1046. load_half:
  1047. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 2, &tmp);
  1048. if (likely(ptr != NULL)) {
  1049. BPF_R0 = get_unaligned_be16(ptr);
  1050. CONT;
  1051. }
  1052. return 0;
  1053. LD_ABS_B: /* BPF_R0 = *(u8 *) (skb->data + imm32) */
  1054. off = IMM;
  1055. load_byte:
  1056. ptr = bpf_load_pointer((struct sk_buff *) (unsigned long) CTX, off, 1, &tmp);
  1057. if (likely(ptr != NULL)) {
  1058. BPF_R0 = *(u8 *)ptr;
  1059. CONT;
  1060. }
  1061. return 0;
  1062. LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + src_reg + imm32)) */
  1063. off = IMM + SRC;
  1064. goto load_word;
  1065. LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + src_reg + imm32)) */
  1066. off = IMM + SRC;
  1067. goto load_half;
  1068. LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + src_reg + imm32) */
  1069. off = IMM + SRC;
  1070. goto load_byte;
  1071. default_label:
  1072. /* If we ever reach this, we have a bug somewhere. */
  1073. WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
  1074. return 0;
  1075. }
  1076. STACK_FRAME_NON_STANDARD(__bpf_prog_run); /* jump table */
  1077. bool bpf_prog_array_compatible(struct bpf_array *array,
  1078. const struct bpf_prog *fp)
  1079. {
  1080. if (!array->owner_prog_type) {
  1081. /* There's no owner yet where we could check for
  1082. * compatibility.
  1083. */
  1084. array->owner_prog_type = fp->type;
  1085. array->owner_jited = fp->jited;
  1086. return true;
  1087. }
  1088. return array->owner_prog_type == fp->type &&
  1089. array->owner_jited == fp->jited;
  1090. }
  1091. static int bpf_check_tail_call(const struct bpf_prog *fp)
  1092. {
  1093. struct bpf_prog_aux *aux = fp->aux;
  1094. int i;
  1095. for (i = 0; i < aux->used_map_cnt; i++) {
  1096. struct bpf_map *map = aux->used_maps[i];
  1097. struct bpf_array *array;
  1098. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  1099. continue;
  1100. array = container_of(map, struct bpf_array, map);
  1101. if (!bpf_prog_array_compatible(array, fp))
  1102. return -EINVAL;
  1103. }
  1104. return 0;
  1105. }
  1106. /**
  1107. * bpf_prog_select_runtime - select exec runtime for BPF program
  1108. * @fp: bpf_prog populated with internal BPF program
  1109. * @err: pointer to error variable
  1110. *
  1111. * Try to JIT eBPF program, if JIT is not available, use interpreter.
  1112. * The BPF program will be executed via BPF_PROG_RUN() macro.
  1113. */
  1114. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
  1115. {
  1116. fp->bpf_func = (void *) __bpf_prog_run;
  1117. /* eBPF JITs can rewrite the program in case constant
  1118. * blinding is active. However, in case of error during
  1119. * blinding, bpf_int_jit_compile() must always return a
  1120. * valid program, which in this case would simply not
  1121. * be JITed, but falls back to the interpreter.
  1122. */
  1123. fp = bpf_int_jit_compile(fp);
  1124. bpf_prog_lock_ro(fp);
  1125. /* The tail call compatibility check can only be done at
  1126. * this late stage as we need to determine, if we deal
  1127. * with JITed or non JITed program concatenations and not
  1128. * all eBPF JITs might immediately support all features.
  1129. */
  1130. *err = bpf_check_tail_call(fp);
  1131. return fp;
  1132. }
  1133. EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
  1134. static void bpf_prog_free_deferred(struct work_struct *work)
  1135. {
  1136. struct bpf_prog_aux *aux;
  1137. aux = container_of(work, struct bpf_prog_aux, work);
  1138. bpf_jit_free(aux->prog);
  1139. }
  1140. /* Free internal BPF program */
  1141. void bpf_prog_free(struct bpf_prog *fp)
  1142. {
  1143. struct bpf_prog_aux *aux = fp->aux;
  1144. INIT_WORK(&aux->work, bpf_prog_free_deferred);
  1145. schedule_work(&aux->work);
  1146. }
  1147. EXPORT_SYMBOL_GPL(bpf_prog_free);
  1148. /* RNG for unpriviledged user space with separated state from prandom_u32(). */
  1149. static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
  1150. void bpf_user_rnd_init_once(void)
  1151. {
  1152. prandom_init_once(&bpf_user_rnd_state);
  1153. }
  1154. BPF_CALL_0(bpf_user_rnd_u32)
  1155. {
  1156. /* Should someone ever have the rather unwise idea to use some
  1157. * of the registers passed into this function, then note that
  1158. * this function is called from native eBPF and classic-to-eBPF
  1159. * transformations. Register assignments from both sides are
  1160. * different, f.e. classic always sets fn(ctx, A, X) here.
  1161. */
  1162. struct rnd_state *state;
  1163. u32 res;
  1164. state = &get_cpu_var(bpf_user_rnd_state);
  1165. res = prandom_u32_state(state);
  1166. put_cpu_var(bpf_user_rnd_state);
  1167. return res;
  1168. }
  1169. /* Weak definitions of helper functions in case we don't have bpf syscall. */
  1170. const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
  1171. const struct bpf_func_proto bpf_map_update_elem_proto __weak;
  1172. const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
  1173. const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
  1174. const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
  1175. const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
  1176. const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
  1177. const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
  1178. const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
  1179. const struct bpf_func_proto bpf_get_current_comm_proto __weak;
  1180. const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
  1181. {
  1182. return NULL;
  1183. }
  1184. u64 __weak
  1185. bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  1186. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  1187. {
  1188. return -ENOTSUPP;
  1189. }
  1190. /* Always built-in helper functions. */
  1191. const struct bpf_func_proto bpf_tail_call_proto = {
  1192. .func = NULL,
  1193. .gpl_only = false,
  1194. .ret_type = RET_VOID,
  1195. .arg1_type = ARG_PTR_TO_CTX,
  1196. .arg2_type = ARG_CONST_MAP_PTR,
  1197. .arg3_type = ARG_ANYTHING,
  1198. };
  1199. /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
  1200. * It is encouraged to implement bpf_int_jit_compile() instead, so that
  1201. * eBPF and implicitly also cBPF can get JITed!
  1202. */
  1203. struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
  1204. {
  1205. return prog;
  1206. }
  1207. /* Stub for JITs that support eBPF. All cBPF code gets transformed into
  1208. * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
  1209. */
  1210. void __weak bpf_jit_compile(struct bpf_prog *prog)
  1211. {
  1212. }
  1213. bool __weak bpf_helper_changes_pkt_data(void *func)
  1214. {
  1215. return false;
  1216. }
  1217. /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  1218. * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  1219. */
  1220. int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
  1221. int len)
  1222. {
  1223. return -EFAULT;
  1224. }
  1225. /* All definitions of tracepoints related to BPF. */
  1226. #define CREATE_TRACE_POINTS
  1227. #include <linux/bpf_trace.h>
  1228. EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
  1229. EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_get_type);
  1230. EXPORT_TRACEPOINT_SYMBOL_GPL(bpf_prog_put_rcu);