core.c 37 KB

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