core.c 43 KB

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