core.c 42 KB

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