core.c 45 KB

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