core.c 47 KB

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