core.c 36 KB

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