core.c 47 KB

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