core.c 36 KB

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