core.c 46 KB

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