core.c 46 KB

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