core.c 45 KB

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