core.c 46 KB

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