core.c 46 KB

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