core.c 47 KB

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