core.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux Socket Filter - Kernel level socket filtering
  4. *
  5. * Based on the design of the Berkeley Packet Filter. The new
  6. * internal format has been designed by PLUMgrid:
  7. *
  8. * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
  9. *
  10. * Authors:
  11. *
  12. * Jay Schulist <[email protected]>
  13. * Alexei Starovoitov <[email protected]>
  14. * Daniel Borkmann <[email protected]>
  15. *
  16. * Andi Kleen - Fix a few bad bugs and races.
  17. * Kris Katterjohn - Added many additional checks in bpf_check_classic()
  18. */
  19. #include <uapi/linux/btf.h>
  20. #include <linux/filter.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/random.h>
  24. #include <linux/moduleloader.h>
  25. #include <linux/bpf.h>
  26. #include <linux/btf.h>
  27. #include <linux/objtool.h>
  28. #include <linux/rbtree_latch.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/extable.h>
  33. #include <linux/log2.h>
  34. #include <linux/bpf_verifier.h>
  35. #include <linux/nodemask.h>
  36. #include <linux/nospec.h>
  37. #include <asm/barrier.h>
  38. #include <asm/unaligned.h>
  39. #ifdef CONFIG_RKP
  40. #include <linux/rkp.h>
  41. #endif
  42. /* Registers */
  43. #define BPF_R0 regs[BPF_REG_0]
  44. #define BPF_R1 regs[BPF_REG_1]
  45. #define BPF_R2 regs[BPF_REG_2]
  46. #define BPF_R3 regs[BPF_REG_3]
  47. #define BPF_R4 regs[BPF_REG_4]
  48. #define BPF_R5 regs[BPF_REG_5]
  49. #define BPF_R6 regs[BPF_REG_6]
  50. #define BPF_R7 regs[BPF_REG_7]
  51. #define BPF_R8 regs[BPF_REG_8]
  52. #define BPF_R9 regs[BPF_REG_9]
  53. #define BPF_R10 regs[BPF_REG_10]
  54. /* Named registers */
  55. #define DST regs[insn->dst_reg]
  56. #define SRC regs[insn->src_reg]
  57. #define FP regs[BPF_REG_FP]
  58. #define AX regs[BPF_REG_AX]
  59. #define ARG1 regs[BPF_REG_ARG1]
  60. #define CTX regs[BPF_REG_CTX]
  61. #define IMM insn->imm
  62. /* No hurry in this branch
  63. *
  64. * Exported for the bpf jit load helper.
  65. */
  66. void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
  67. {
  68. u8 *ptr = NULL;
  69. if (k >= SKF_NET_OFF) {
  70. ptr = skb_network_header(skb) + k - SKF_NET_OFF;
  71. } else if (k >= SKF_LL_OFF) {
  72. if (unlikely(!skb_mac_header_was_set(skb)))
  73. return NULL;
  74. ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
  75. }
  76. if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
  77. return ptr;
  78. return NULL;
  79. }
  80. struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)
  81. {
  82. gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
  83. struct bpf_prog_aux *aux;
  84. struct bpf_prog *fp;
  85. size = round_up(size, PAGE_SIZE);
  86. fp = __vmalloc(size, gfp_flags);
  87. if (fp == NULL)
  88. return NULL;
  89. aux = kzalloc(sizeof(*aux), GFP_KERNEL_ACCOUNT | gfp_extra_flags);
  90. if (aux == NULL) {
  91. vfree(fp);
  92. return NULL;
  93. }
  94. fp->active = alloc_percpu_gfp(int, GFP_KERNEL_ACCOUNT | gfp_extra_flags);
  95. if (!fp->active) {
  96. vfree(fp);
  97. kfree(aux);
  98. return NULL;
  99. }
  100. fp->pages = size / PAGE_SIZE;
  101. fp->aux = aux;
  102. fp->aux->prog = fp;
  103. fp->jit_requested = ebpf_jit_enabled();
  104. fp->blinding_requested = bpf_jit_blinding_enabled(fp);
  105. #ifdef CONFIG_CGROUP_BPF
  106. aux->cgroup_atype = CGROUP_BPF_ATTACH_TYPE_INVALID;
  107. #endif
  108. INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode);
  109. mutex_init(&fp->aux->used_maps_mutex);
  110. mutex_init(&fp->aux->dst_mutex);
  111. return fp;
  112. }
  113. struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
  114. {
  115. gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
  116. struct bpf_prog *prog;
  117. int cpu;
  118. prog = bpf_prog_alloc_no_stats(size, gfp_extra_flags);
  119. if (!prog)
  120. return NULL;
  121. prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);
  122. if (!prog->stats) {
  123. free_percpu(prog->active);
  124. kfree(prog->aux);
  125. vfree(prog);
  126. return NULL;
  127. }
  128. for_each_possible_cpu(cpu) {
  129. struct bpf_prog_stats *pstats;
  130. pstats = per_cpu_ptr(prog->stats, cpu);
  131. u64_stats_init(&pstats->syncp);
  132. }
  133. return prog;
  134. }
  135. EXPORT_SYMBOL_GPL(bpf_prog_alloc);
  136. int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog)
  137. {
  138. if (!prog->aux->nr_linfo || !prog->jit_requested)
  139. return 0;
  140. prog->aux->jited_linfo = kvcalloc(prog->aux->nr_linfo,
  141. sizeof(*prog->aux->jited_linfo),
  142. GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
  143. if (!prog->aux->jited_linfo)
  144. return -ENOMEM;
  145. return 0;
  146. }
  147. void bpf_prog_jit_attempt_done(struct bpf_prog *prog)
  148. {
  149. if (prog->aux->jited_linfo &&
  150. (!prog->jited || !prog->aux->jited_linfo[0])) {
  151. kvfree(prog->aux->jited_linfo);
  152. prog->aux->jited_linfo = NULL;
  153. }
  154. kfree(prog->aux->kfunc_tab);
  155. prog->aux->kfunc_tab = NULL;
  156. }
  157. /* The jit engine is responsible to provide an array
  158. * for insn_off to the jited_off mapping (insn_to_jit_off).
  159. *
  160. * The idx to this array is the insn_off. Hence, the insn_off
  161. * here is relative to the prog itself instead of the main prog.
  162. * This array has one entry for each xlated bpf insn.
  163. *
  164. * jited_off is the byte off to the end of the jited insn.
  165. *
  166. * Hence, with
  167. * insn_start:
  168. * The first bpf insn off of the prog. The insn off
  169. * here is relative to the main prog.
  170. * e.g. if prog is a subprog, insn_start > 0
  171. * linfo_idx:
  172. * The prog's idx to prog->aux->linfo and jited_linfo
  173. *
  174. * jited_linfo[linfo_idx] = prog->bpf_func
  175. *
  176. * For i > linfo_idx,
  177. *
  178. * jited_linfo[i] = prog->bpf_func +
  179. * insn_to_jit_off[linfo[i].insn_off - insn_start - 1]
  180. */
  181. void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
  182. const u32 *insn_to_jit_off)
  183. {
  184. u32 linfo_idx, insn_start, insn_end, nr_linfo, i;
  185. const struct bpf_line_info *linfo;
  186. void **jited_linfo;
  187. if (!prog->aux->jited_linfo)
  188. /* Userspace did not provide linfo */
  189. return;
  190. linfo_idx = prog->aux->linfo_idx;
  191. linfo = &prog->aux->linfo[linfo_idx];
  192. insn_start = linfo[0].insn_off;
  193. insn_end = insn_start + prog->len;
  194. jited_linfo = &prog->aux->jited_linfo[linfo_idx];
  195. jited_linfo[0] = prog->bpf_func;
  196. nr_linfo = prog->aux->nr_linfo - linfo_idx;
  197. for (i = 1; i < nr_linfo && linfo[i].insn_off < insn_end; i++)
  198. /* The verifier ensures that linfo[i].insn_off is
  199. * strictly increasing
  200. */
  201. jited_linfo[i] = prog->bpf_func +
  202. insn_to_jit_off[linfo[i].insn_off - insn_start - 1];
  203. }
  204. struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
  205. gfp_t gfp_extra_flags)
  206. {
  207. gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
  208. struct bpf_prog *fp;
  209. u32 pages;
  210. size = round_up(size, PAGE_SIZE);
  211. pages = size / PAGE_SIZE;
  212. if (pages <= fp_old->pages)
  213. return fp_old;
  214. fp = __vmalloc(size, gfp_flags);
  215. if (fp) {
  216. memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
  217. fp->pages = pages;
  218. fp->aux->prog = fp;
  219. /* We keep fp->aux from fp_old around in the new
  220. * reallocated structure.
  221. */
  222. fp_old->aux = NULL;
  223. fp_old->stats = NULL;
  224. fp_old->active = NULL;
  225. __bpf_prog_free(fp_old);
  226. }
  227. return fp;
  228. }
  229. void __bpf_prog_free(struct bpf_prog *fp)
  230. {
  231. if (fp->aux) {
  232. mutex_destroy(&fp->aux->used_maps_mutex);
  233. mutex_destroy(&fp->aux->dst_mutex);
  234. kfree(fp->aux->poke_tab);
  235. kfree(fp->aux);
  236. }
  237. free_percpu(fp->stats);
  238. free_percpu(fp->active);
  239. vfree(fp);
  240. }
  241. int bpf_prog_calc_tag(struct bpf_prog *fp)
  242. {
  243. const u32 bits_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
  244. u32 raw_size = bpf_prog_tag_scratch_size(fp);
  245. u32 digest[SHA1_DIGEST_WORDS];
  246. u32 ws[SHA1_WORKSPACE_WORDS];
  247. u32 i, bsize, psize, blocks;
  248. struct bpf_insn *dst;
  249. bool was_ld_map;
  250. u8 *raw, *todo;
  251. __be32 *result;
  252. __be64 *bits;
  253. raw = vmalloc(raw_size);
  254. if (!raw)
  255. return -ENOMEM;
  256. sha1_init(digest);
  257. memset(ws, 0, sizeof(ws));
  258. /* We need to take out the map fd for the digest calculation
  259. * since they are unstable from user space side.
  260. */
  261. dst = (void *)raw;
  262. for (i = 0, was_ld_map = false; i < fp->len; i++) {
  263. dst[i] = fp->insnsi[i];
  264. if (!was_ld_map &&
  265. dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  266. (dst[i].src_reg == BPF_PSEUDO_MAP_FD ||
  267. dst[i].src_reg == BPF_PSEUDO_MAP_VALUE)) {
  268. was_ld_map = true;
  269. dst[i].imm = 0;
  270. } else if (was_ld_map &&
  271. dst[i].code == 0 &&
  272. dst[i].dst_reg == 0 &&
  273. dst[i].src_reg == 0 &&
  274. dst[i].off == 0) {
  275. was_ld_map = false;
  276. dst[i].imm = 0;
  277. } else {
  278. was_ld_map = false;
  279. }
  280. }
  281. psize = bpf_prog_insn_size(fp);
  282. memset(&raw[psize], 0, raw_size - psize);
  283. raw[psize++] = 0x80;
  284. bsize = round_up(psize, SHA1_BLOCK_SIZE);
  285. blocks = bsize / SHA1_BLOCK_SIZE;
  286. todo = raw;
  287. if (bsize - psize >= sizeof(__be64)) {
  288. bits = (__be64 *)(todo + bsize - sizeof(__be64));
  289. } else {
  290. bits = (__be64 *)(todo + bsize + bits_offset);
  291. blocks++;
  292. }
  293. *bits = cpu_to_be64((psize - 1) << 3);
  294. while (blocks--) {
  295. sha1_transform(digest, todo, ws);
  296. todo += SHA1_BLOCK_SIZE;
  297. }
  298. result = (__force __be32 *)digest;
  299. for (i = 0; i < SHA1_DIGEST_WORDS; i++)
  300. result[i] = cpu_to_be32(digest[i]);
  301. memcpy(fp->tag, result, sizeof(fp->tag));
  302. vfree(raw);
  303. return 0;
  304. }
  305. static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,
  306. s32 end_new, s32 curr, const bool probe_pass)
  307. {
  308. const s64 imm_min = S32_MIN, imm_max = S32_MAX;
  309. s32 delta = end_new - end_old;
  310. s64 imm = insn->imm;
  311. if (curr < pos && curr + imm + 1 >= end_old)
  312. imm += delta;
  313. else if (curr >= end_new && curr + imm + 1 < end_new)
  314. imm -= delta;
  315. if (imm < imm_min || imm > imm_max)
  316. return -ERANGE;
  317. if (!probe_pass)
  318. insn->imm = imm;
  319. return 0;
  320. }
  321. static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,
  322. s32 end_new, s32 curr, const bool probe_pass)
  323. {
  324. const s32 off_min = S16_MIN, off_max = S16_MAX;
  325. s32 delta = end_new - end_old;
  326. s32 off = insn->off;
  327. if (curr < pos && curr + off + 1 >= end_old)
  328. off += delta;
  329. else if (curr >= end_new && curr + off + 1 < end_new)
  330. off -= delta;
  331. if (off < off_min || off > off_max)
  332. return -ERANGE;
  333. if (!probe_pass)
  334. insn->off = off;
  335. return 0;
  336. }
  337. static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, s32 end_old,
  338. s32 end_new, const bool probe_pass)
  339. {
  340. u32 i, insn_cnt = prog->len + (probe_pass ? end_new - end_old : 0);
  341. struct bpf_insn *insn = prog->insnsi;
  342. int ret = 0;
  343. for (i = 0; i < insn_cnt; i++, insn++) {
  344. u8 code;
  345. /* In the probing pass we still operate on the original,
  346. * unpatched image in order to check overflows before we
  347. * do any other adjustments. Therefore skip the patchlet.
  348. */
  349. if (probe_pass && i == pos) {
  350. i = end_new;
  351. insn = prog->insnsi + end_old;
  352. }
  353. if (bpf_pseudo_func(insn)) {
  354. ret = bpf_adj_delta_to_imm(insn, pos, end_old,
  355. end_new, i, probe_pass);
  356. if (ret)
  357. return ret;
  358. continue;
  359. }
  360. code = insn->code;
  361. if ((BPF_CLASS(code) != BPF_JMP &&
  362. BPF_CLASS(code) != BPF_JMP32) ||
  363. BPF_OP(code) == BPF_EXIT)
  364. continue;
  365. /* Adjust offset of jmps if we cross patch boundaries. */
  366. if (BPF_OP(code) == BPF_CALL) {
  367. if (insn->src_reg != BPF_PSEUDO_CALL)
  368. continue;
  369. ret = bpf_adj_delta_to_imm(insn, pos, end_old,
  370. end_new, i, probe_pass);
  371. } else {
  372. ret = bpf_adj_delta_to_off(insn, pos, end_old,
  373. end_new, i, probe_pass);
  374. }
  375. if (ret)
  376. break;
  377. }
  378. return ret;
  379. }
  380. static void bpf_adj_linfo(struct bpf_prog *prog, u32 off, u32 delta)
  381. {
  382. struct bpf_line_info *linfo;
  383. u32 i, nr_linfo;
  384. nr_linfo = prog->aux->nr_linfo;
  385. if (!nr_linfo || !delta)
  386. return;
  387. linfo = prog->aux->linfo;
  388. for (i = 0; i < nr_linfo; i++)
  389. if (off < linfo[i].insn_off)
  390. break;
  391. /* Push all off < linfo[i].insn_off by delta */
  392. for (; i < nr_linfo; i++)
  393. linfo[i].insn_off += delta;
  394. }
  395. struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
  396. const struct bpf_insn *patch, u32 len)
  397. {
  398. u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;
  399. const u32 cnt_max = S16_MAX;
  400. struct bpf_prog *prog_adj;
  401. int err;
  402. /* Since our patchlet doesn't expand the image, we're done. */
  403. if (insn_delta == 0) {
  404. memcpy(prog->insnsi + off, patch, sizeof(*patch));
  405. return prog;
  406. }
  407. insn_adj_cnt = prog->len + insn_delta;
  408. /* Reject anything that would potentially let the insn->off
  409. * target overflow when we have excessive program expansions.
  410. * We need to probe here before we do any reallocation where
  411. * we afterwards may not fail anymore.
  412. */
  413. if (insn_adj_cnt > cnt_max &&
  414. (err = bpf_adj_branches(prog, off, off + 1, off + len, true)))
  415. return ERR_PTR(err);
  416. /* Several new instructions need to be inserted. Make room
  417. * for them. Likely, there's no need for a new allocation as
  418. * last page could have large enough tailroom.
  419. */
  420. prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
  421. GFP_USER);
  422. if (!prog_adj)
  423. return ERR_PTR(-ENOMEM);
  424. prog_adj->len = insn_adj_cnt;
  425. /* Patching happens in 3 steps:
  426. *
  427. * 1) Move over tail of insnsi from next instruction onwards,
  428. * so we can patch the single target insn with one or more
  429. * new ones (patching is always from 1 to n insns, n > 0).
  430. * 2) Inject new instructions at the target location.
  431. * 3) Adjust branch offsets if necessary.
  432. */
  433. insn_rest = insn_adj_cnt - off - len;
  434. memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,
  435. sizeof(*patch) * insn_rest);
  436. memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len);
  437. /* We are guaranteed to not fail at this point, otherwise
  438. * the ship has sailed to reverse to the original state. An
  439. * overflow cannot happen at this point.
  440. */
  441. BUG_ON(bpf_adj_branches(prog_adj, off, off + 1, off + len, false));
  442. bpf_adj_linfo(prog_adj, off, insn_delta);
  443. return prog_adj;
  444. }
  445. int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt)
  446. {
  447. /* Branch offsets can't overflow when program is shrinking, no need
  448. * to call bpf_adj_branches(..., true) here
  449. */
  450. memmove(prog->insnsi + off, prog->insnsi + off + cnt,
  451. sizeof(struct bpf_insn) * (prog->len - off - cnt));
  452. prog->len -= cnt;
  453. return WARN_ON_ONCE(bpf_adj_branches(prog, off, off + cnt, off, false));
  454. }
  455. static void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp)
  456. {
  457. int i;
  458. for (i = 0; i < fp->aux->func_cnt; i++)
  459. bpf_prog_kallsyms_del(fp->aux->func[i]);
  460. }
  461. void bpf_prog_kallsyms_del_all(struct bpf_prog *fp)
  462. {
  463. bpf_prog_kallsyms_del_subprogs(fp);
  464. bpf_prog_kallsyms_del(fp);
  465. }
  466. #ifdef CONFIG_BPF_JIT
  467. /* All BPF JIT sysctl knobs here. */
  468. int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
  469. int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
  470. int bpf_jit_harden __read_mostly;
  471. long bpf_jit_limit __read_mostly;
  472. long bpf_jit_limit_max __read_mostly;
  473. static void
  474. bpf_prog_ksym_set_addr(struct bpf_prog *prog)
  475. {
  476. WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog));
  477. prog->aux->ksym.start = (unsigned long) prog->bpf_func;
  478. prog->aux->ksym.end = prog->aux->ksym.start + prog->jited_len;
  479. }
  480. static void
  481. bpf_prog_ksym_set_name(struct bpf_prog *prog)
  482. {
  483. char *sym = prog->aux->ksym.name;
  484. const char *end = sym + KSYM_NAME_LEN;
  485. const struct btf_type *type;
  486. const char *func_name;
  487. BUILD_BUG_ON(sizeof("bpf_prog_") +
  488. sizeof(prog->tag) * 2 +
  489. /* name has been null terminated.
  490. * We should need +1 for the '_' preceding
  491. * the name. However, the null character
  492. * is double counted between the name and the
  493. * sizeof("bpf_prog_") above, so we omit
  494. * the +1 here.
  495. */
  496. sizeof(prog->aux->name) > KSYM_NAME_LEN);
  497. sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
  498. sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
  499. /* prog->aux->name will be ignored if full btf name is available */
  500. if (prog->aux->func_info_cnt) {
  501. type = btf_type_by_id(prog->aux->btf,
  502. prog->aux->func_info[prog->aux->func_idx].type_id);
  503. func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
  504. snprintf(sym, (size_t)(end - sym), "_%s", func_name);
  505. return;
  506. }
  507. if (prog->aux->name[0])
  508. snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
  509. else
  510. *sym = 0;
  511. }
  512. static unsigned long bpf_get_ksym_start(struct latch_tree_node *n)
  513. {
  514. return container_of(n, struct bpf_ksym, tnode)->start;
  515. }
  516. static __always_inline bool bpf_tree_less(struct latch_tree_node *a,
  517. struct latch_tree_node *b)
  518. {
  519. return bpf_get_ksym_start(a) < bpf_get_ksym_start(b);
  520. }
  521. static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n)
  522. {
  523. unsigned long val = (unsigned long)key;
  524. const struct bpf_ksym *ksym;
  525. ksym = container_of(n, struct bpf_ksym, tnode);
  526. if (val < ksym->start)
  527. return -1;
  528. /* Ensure that we detect return addresses as part of the program, when
  529. * the final instruction is a call for a program part of the stack
  530. * trace. Therefore, do val > ksym->end instead of val >= ksym->end.
  531. */
  532. if (val > ksym->end)
  533. return 1;
  534. return 0;
  535. }
  536. static const struct latch_tree_ops bpf_tree_ops = {
  537. .less = bpf_tree_less,
  538. .comp = bpf_tree_comp,
  539. };
  540. static DEFINE_SPINLOCK(bpf_lock);
  541. static LIST_HEAD(bpf_kallsyms);
  542. static struct latch_tree_root bpf_tree __cacheline_aligned;
  543. void bpf_ksym_add(struct bpf_ksym *ksym)
  544. {
  545. spin_lock_bh(&bpf_lock);
  546. WARN_ON_ONCE(!list_empty(&ksym->lnode));
  547. list_add_tail_rcu(&ksym->lnode, &bpf_kallsyms);
  548. latch_tree_insert(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
  549. spin_unlock_bh(&bpf_lock);
  550. }
  551. static void __bpf_ksym_del(struct bpf_ksym *ksym)
  552. {
  553. if (list_empty(&ksym->lnode))
  554. return;
  555. latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
  556. list_del_rcu(&ksym->lnode);
  557. }
  558. void bpf_ksym_del(struct bpf_ksym *ksym)
  559. {
  560. spin_lock_bh(&bpf_lock);
  561. __bpf_ksym_del(ksym);
  562. spin_unlock_bh(&bpf_lock);
  563. }
  564. static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
  565. {
  566. return fp->jited && !bpf_prog_was_classic(fp);
  567. }
  568. void bpf_prog_kallsyms_add(struct bpf_prog *fp)
  569. {
  570. if (!bpf_prog_kallsyms_candidate(fp) ||
  571. !bpf_capable())
  572. return;
  573. bpf_prog_ksym_set_addr(fp);
  574. bpf_prog_ksym_set_name(fp);
  575. fp->aux->ksym.prog = true;
  576. bpf_ksym_add(&fp->aux->ksym);
  577. }
  578. void bpf_prog_kallsyms_del(struct bpf_prog *fp)
  579. {
  580. if (!bpf_prog_kallsyms_candidate(fp))
  581. return;
  582. bpf_ksym_del(&fp->aux->ksym);
  583. }
  584. static struct bpf_ksym *bpf_ksym_find(unsigned long addr)
  585. {
  586. struct latch_tree_node *n;
  587. n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops);
  588. return n ? container_of(n, struct bpf_ksym, tnode) : NULL;
  589. }
  590. const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
  591. unsigned long *off, char *sym)
  592. {
  593. struct bpf_ksym *ksym;
  594. char *ret = NULL;
  595. rcu_read_lock();
  596. ksym = bpf_ksym_find(addr);
  597. if (ksym) {
  598. unsigned long symbol_start = ksym->start;
  599. unsigned long symbol_end = ksym->end;
  600. strncpy(sym, ksym->name, KSYM_NAME_LEN);
  601. ret = sym;
  602. if (size)
  603. *size = symbol_end - symbol_start;
  604. if (off)
  605. *off = addr - symbol_start;
  606. }
  607. rcu_read_unlock();
  608. return ret;
  609. }
  610. bool is_bpf_text_address(unsigned long addr)
  611. {
  612. bool ret;
  613. rcu_read_lock();
  614. ret = bpf_ksym_find(addr) != NULL;
  615. rcu_read_unlock();
  616. return ret;
  617. }
  618. static struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)
  619. {
  620. struct bpf_ksym *ksym = bpf_ksym_find(addr);
  621. return ksym && ksym->prog ?
  622. container_of(ksym, struct bpf_prog_aux, ksym)->prog :
  623. NULL;
  624. }
  625. const struct exception_table_entry *search_bpf_extables(unsigned long addr)
  626. {
  627. const struct exception_table_entry *e = NULL;
  628. struct bpf_prog *prog;
  629. rcu_read_lock();
  630. prog = bpf_prog_ksym_find(addr);
  631. if (!prog)
  632. goto out;
  633. if (!prog->aux->num_exentries)
  634. goto out;
  635. e = search_extable(prog->aux->extable, prog->aux->num_exentries, addr);
  636. out:
  637. rcu_read_unlock();
  638. return e;
  639. }
  640. int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  641. char *sym)
  642. {
  643. struct bpf_ksym *ksym;
  644. unsigned int it = 0;
  645. int ret = -ERANGE;
  646. if (!bpf_jit_kallsyms_enabled())
  647. return ret;
  648. rcu_read_lock();
  649. list_for_each_entry_rcu(ksym, &bpf_kallsyms, lnode) {
  650. if (it++ != symnum)
  651. continue;
  652. strncpy(sym, ksym->name, KSYM_NAME_LEN);
  653. *value = ksym->start;
  654. *type = BPF_SYM_ELF_TYPE;
  655. ret = 0;
  656. break;
  657. }
  658. rcu_read_unlock();
  659. return ret;
  660. }
  661. int bpf_jit_add_poke_descriptor(struct bpf_prog *prog,
  662. struct bpf_jit_poke_descriptor *poke)
  663. {
  664. struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab;
  665. static const u32 poke_tab_max = 1024;
  666. u32 slot = prog->aux->size_poke_tab;
  667. u32 size = slot + 1;
  668. if (size > poke_tab_max)
  669. return -ENOSPC;
  670. if (poke->tailcall_target || poke->tailcall_target_stable ||
  671. poke->tailcall_bypass || poke->adj_off || poke->bypass_addr)
  672. return -EINVAL;
  673. switch (poke->reason) {
  674. case BPF_POKE_REASON_TAIL_CALL:
  675. if (!poke->tail_call.map)
  676. return -EINVAL;
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. tab = krealloc(tab, size * sizeof(*poke), GFP_KERNEL);
  682. if (!tab)
  683. return -ENOMEM;
  684. memcpy(&tab[slot], poke, sizeof(*poke));
  685. prog->aux->size_poke_tab = size;
  686. prog->aux->poke_tab = tab;
  687. return slot;
  688. }
  689. /*
  690. * BPF program pack allocator.
  691. *
  692. * Most BPF programs are pretty small. Allocating a hole page for each
  693. * program is sometime a waste. Many small bpf program also adds pressure
  694. * to instruction TLB. To solve this issue, we introduce a BPF program pack
  695. * allocator. The prog_pack allocator uses HPAGE_PMD_SIZE page (2MB on x86)
  696. * to host BPF programs.
  697. */
  698. #define BPF_PROG_CHUNK_SHIFT 6
  699. #define BPF_PROG_CHUNK_SIZE (1 << BPF_PROG_CHUNK_SHIFT)
  700. #define BPF_PROG_CHUNK_MASK (~(BPF_PROG_CHUNK_SIZE - 1))
  701. struct bpf_prog_pack {
  702. struct list_head list;
  703. void *ptr;
  704. unsigned long bitmap[];
  705. };
  706. void bpf_jit_fill_hole_with_zero(void *area, unsigned int size)
  707. {
  708. memset(area, 0, size);
  709. }
  710. #define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
  711. static DEFINE_MUTEX(pack_mutex);
  712. static LIST_HEAD(pack_list);
  713. /* PMD_SIZE is not available in some special config, e.g. ARCH=arm with
  714. * CONFIG_MMU=n. Use PAGE_SIZE in these cases.
  715. */
  716. #ifdef PMD_SIZE
  717. #define BPF_PROG_PACK_SIZE (PMD_SIZE * num_possible_nodes())
  718. #else
  719. #define BPF_PROG_PACK_SIZE PAGE_SIZE
  720. #endif
  721. #define BPF_PROG_CHUNK_COUNT (BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE)
  722. static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns)
  723. {
  724. struct bpf_prog_pack *pack;
  725. pack = kzalloc(struct_size(pack, bitmap, BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT)),
  726. GFP_KERNEL);
  727. if (!pack)
  728. return NULL;
  729. pack->ptr = module_alloc(BPF_PROG_PACK_SIZE);
  730. if (!pack->ptr) {
  731. kfree(pack);
  732. return NULL;
  733. }
  734. bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE);
  735. bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
  736. list_add_tail(&pack->list, &pack_list);
  737. set_vm_flush_reset_perms(pack->ptr);
  738. set_memory_ro((unsigned long)pack->ptr, BPF_PROG_PACK_SIZE / PAGE_SIZE);
  739. set_memory_x((unsigned long)pack->ptr, BPF_PROG_PACK_SIZE / PAGE_SIZE);
  740. return pack;
  741. }
  742. void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
  743. {
  744. unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
  745. struct bpf_prog_pack *pack;
  746. unsigned long pos;
  747. void *ptr = NULL;
  748. mutex_lock(&pack_mutex);
  749. if (size > BPF_PROG_PACK_SIZE) {
  750. size = round_up(size, PAGE_SIZE);
  751. ptr = module_alloc(size);
  752. if (ptr) {
  753. bpf_fill_ill_insns(ptr, size);
  754. set_vm_flush_reset_perms(ptr);
  755. set_memory_ro((unsigned long)ptr, size / PAGE_SIZE);
  756. set_memory_x((unsigned long)ptr, size / PAGE_SIZE);
  757. }
  758. goto out;
  759. }
  760. list_for_each_entry(pack, &pack_list, list) {
  761. pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
  762. nbits, 0);
  763. if (pos < BPF_PROG_CHUNK_COUNT)
  764. goto found_free_area;
  765. }
  766. pack = alloc_new_pack(bpf_fill_ill_insns);
  767. if (!pack)
  768. goto out;
  769. pos = 0;
  770. found_free_area:
  771. bitmap_set(pack->bitmap, pos, nbits);
  772. ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT);
  773. out:
  774. mutex_unlock(&pack_mutex);
  775. return ptr;
  776. }
  777. void bpf_prog_pack_free(struct bpf_binary_header *hdr)
  778. {
  779. struct bpf_prog_pack *pack = NULL, *tmp;
  780. unsigned int nbits;
  781. unsigned long pos;
  782. mutex_lock(&pack_mutex);
  783. if (hdr->size > BPF_PROG_PACK_SIZE) {
  784. module_memfree(hdr);
  785. goto out;
  786. }
  787. list_for_each_entry(tmp, &pack_list, list) {
  788. if ((void *)hdr >= tmp->ptr && (tmp->ptr + BPF_PROG_PACK_SIZE) > (void *)hdr) {
  789. pack = tmp;
  790. break;
  791. }
  792. }
  793. if (WARN_ONCE(!pack, "bpf_prog_pack bug\n"))
  794. goto out;
  795. nbits = BPF_PROG_SIZE_TO_NBITS(hdr->size);
  796. pos = ((unsigned long)hdr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT;
  797. WARN_ONCE(bpf_arch_text_invalidate(hdr, hdr->size),
  798. "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
  799. bitmap_clear(pack->bitmap, pos, nbits);
  800. if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
  801. BPF_PROG_CHUNK_COUNT, 0) == 0) {
  802. list_del(&pack->list);
  803. module_memfree(pack->ptr);
  804. kfree(pack);
  805. }
  806. out:
  807. mutex_unlock(&pack_mutex);
  808. }
  809. static atomic_long_t bpf_jit_current;
  810. /* Can be overridden by an arch's JIT compiler if it has a custom,
  811. * dedicated BPF backend memory area, or if neither of the two
  812. * below apply.
  813. */
  814. u64 __weak bpf_jit_alloc_exec_limit(void)
  815. {
  816. #if defined(MODULES_VADDR)
  817. return MODULES_END - MODULES_VADDR;
  818. #else
  819. return VMALLOC_END - VMALLOC_START;
  820. #endif
  821. }
  822. static int __init bpf_jit_charge_init(void)
  823. {
  824. /* Only used as heuristic here to derive limit. */
  825. bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
  826. bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 1,
  827. PAGE_SIZE), LONG_MAX);
  828. return 0;
  829. }
  830. pure_initcall(bpf_jit_charge_init);
  831. int bpf_jit_charge_modmem(u32 size)
  832. {
  833. if (atomic_long_add_return(size, &bpf_jit_current) > READ_ONCE(bpf_jit_limit)) {
  834. if (!bpf_capable()) {
  835. atomic_long_sub(size, &bpf_jit_current);
  836. return -EPERM;
  837. }
  838. }
  839. return 0;
  840. }
  841. void bpf_jit_uncharge_modmem(u32 size)
  842. {
  843. atomic_long_sub(size, &bpf_jit_current);
  844. }
  845. void *__weak bpf_jit_alloc_exec(unsigned long size)
  846. {
  847. return module_alloc(size);
  848. }
  849. void __weak bpf_jit_free_exec(void *addr)
  850. {
  851. module_memfree(addr);
  852. }
  853. struct bpf_binary_header *
  854. bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
  855. unsigned int alignment,
  856. bpf_jit_fill_hole_t bpf_fill_ill_insns)
  857. {
  858. struct bpf_binary_header *hdr;
  859. u32 size, hole, start;
  860. WARN_ON_ONCE(!is_power_of_2(alignment) ||
  861. alignment > BPF_IMAGE_ALIGNMENT);
  862. /* Most of BPF filters are really small, but if some of them
  863. * fill a page, allow at least 128 extra bytes to insert a
  864. * random section of illegal instructions.
  865. */
  866. size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
  867. if (bpf_jit_charge_modmem(size))
  868. return NULL;
  869. hdr = bpf_jit_alloc_exec(size);
  870. if (!hdr) {
  871. bpf_jit_uncharge_modmem(size);
  872. return NULL;
  873. }
  874. /* Fill space with illegal/arch-dep instructions. */
  875. bpf_fill_ill_insns(hdr, size);
  876. hdr->size = size;
  877. hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),
  878. PAGE_SIZE - sizeof(*hdr));
  879. start = prandom_u32_max(hole) & ~(alignment - 1);
  880. /* Leave a random number of instructions before BPF code. */
  881. *image_ptr = &hdr->image[start];
  882. return hdr;
  883. }
  884. void bpf_jit_binary_free(struct bpf_binary_header *hdr)
  885. {
  886. u32 size = hdr->size;
  887. #ifdef CONFIG_RKP
  888. uh_call(UH_APP_RKP, RKP_BPF_LOAD, (u64)hdr, (u64)hdr->size, 1, 0);
  889. #endif
  890. bpf_jit_free_exec(hdr);
  891. bpf_jit_uncharge_modmem(size);
  892. }
  893. /* Allocate jit binary from bpf_prog_pack allocator.
  894. * Since the allocated memory is RO+X, the JIT engine cannot write directly
  895. * to the memory. To solve this problem, a RW buffer is also allocated at
  896. * as the same time. The JIT engine should calculate offsets based on the
  897. * RO memory address, but write JITed program to the RW buffer. Once the
  898. * JIT engine finishes, it calls bpf_jit_binary_pack_finalize, which copies
  899. * the JITed program to the RO memory.
  900. */
  901. struct bpf_binary_header *
  902. bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr,
  903. unsigned int alignment,
  904. struct bpf_binary_header **rw_header,
  905. u8 **rw_image,
  906. bpf_jit_fill_hole_t bpf_fill_ill_insns)
  907. {
  908. struct bpf_binary_header *ro_header;
  909. u32 size, hole, start;
  910. WARN_ON_ONCE(!is_power_of_2(alignment) ||
  911. alignment > BPF_IMAGE_ALIGNMENT);
  912. /* add 16 bytes for a random section of illegal instructions */
  913. size = round_up(proglen + sizeof(*ro_header) + 16, BPF_PROG_CHUNK_SIZE);
  914. if (bpf_jit_charge_modmem(size))
  915. return NULL;
  916. ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
  917. if (!ro_header) {
  918. bpf_jit_uncharge_modmem(size);
  919. return NULL;
  920. }
  921. *rw_header = kvmalloc(size, GFP_KERNEL);
  922. if (!*rw_header) {
  923. bpf_arch_text_copy(&ro_header->size, &size, sizeof(size));
  924. bpf_prog_pack_free(ro_header);
  925. bpf_jit_uncharge_modmem(size);
  926. return NULL;
  927. }
  928. /* Fill space with illegal/arch-dep instructions. */
  929. bpf_fill_ill_insns(*rw_header, size);
  930. (*rw_header)->size = size;
  931. hole = min_t(unsigned int, size - (proglen + sizeof(*ro_header)),
  932. BPF_PROG_CHUNK_SIZE - sizeof(*ro_header));
  933. start = prandom_u32_max(hole) & ~(alignment - 1);
  934. *image_ptr = &ro_header->image[start];
  935. *rw_image = &(*rw_header)->image[start];
  936. return ro_header;
  937. }
  938. /* Copy JITed text from rw_header to its final location, the ro_header. */
  939. int bpf_jit_binary_pack_finalize(struct bpf_prog *prog,
  940. struct bpf_binary_header *ro_header,
  941. struct bpf_binary_header *rw_header)
  942. {
  943. void *ptr;
  944. ptr = bpf_arch_text_copy(ro_header, rw_header, rw_header->size);
  945. kvfree(rw_header);
  946. if (IS_ERR(ptr)) {
  947. bpf_prog_pack_free(ro_header);
  948. return PTR_ERR(ptr);
  949. }
  950. return 0;
  951. }
  952. /* bpf_jit_binary_pack_free is called in two different scenarios:
  953. * 1) when the program is freed after;
  954. * 2) when the JIT engine fails (before bpf_jit_binary_pack_finalize).
  955. * For case 2), we need to free both the RO memory and the RW buffer.
  956. *
  957. * bpf_jit_binary_pack_free requires proper ro_header->size. However,
  958. * bpf_jit_binary_pack_alloc does not set it. Therefore, ro_header->size
  959. * must be set with either bpf_jit_binary_pack_finalize (normal path) or
  960. * bpf_arch_text_copy (when jit fails).
  961. */
  962. void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
  963. struct bpf_binary_header *rw_header)
  964. {
  965. u32 size = ro_header->size;
  966. bpf_prog_pack_free(ro_header);
  967. kvfree(rw_header);
  968. bpf_jit_uncharge_modmem(size);
  969. }
  970. struct bpf_binary_header *
  971. bpf_jit_binary_pack_hdr(const struct bpf_prog *fp)
  972. {
  973. unsigned long real_start = (unsigned long)fp->bpf_func;
  974. unsigned long addr;
  975. addr = real_start & BPF_PROG_CHUNK_MASK;
  976. return (void *)addr;
  977. }
  978. static inline struct bpf_binary_header *
  979. bpf_jit_binary_hdr(const struct bpf_prog *fp)
  980. {
  981. unsigned long real_start = (unsigned long)fp->bpf_func;
  982. unsigned long addr;
  983. addr = real_start & PAGE_MASK;
  984. return (void *)addr;
  985. }
  986. /* This symbol is only overridden by archs that have different
  987. * requirements than the usual eBPF JITs, f.e. when they only
  988. * implement cBPF JIT, do not set images read-only, etc.
  989. */
  990. void __weak bpf_jit_free(struct bpf_prog *fp)
  991. {
  992. if (fp->jited) {
  993. struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
  994. bpf_jit_binary_free(hdr);
  995. WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
  996. }
  997. bpf_prog_unlock_free(fp);
  998. }
  999. int bpf_jit_get_func_addr(const struct bpf_prog *prog,
  1000. const struct bpf_insn *insn, bool extra_pass,
  1001. u64 *func_addr, bool *func_addr_fixed)
  1002. {
  1003. s16 off = insn->off;
  1004. s32 imm = insn->imm;
  1005. u8 *addr;
  1006. *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
  1007. if (!*func_addr_fixed) {
  1008. /* Place-holder address till the last pass has collected
  1009. * all addresses for JITed subprograms in which case we
  1010. * can pick them up from prog->aux.
  1011. */
  1012. if (!extra_pass)
  1013. addr = NULL;
  1014. else if (prog->aux->func &&
  1015. off >= 0 && off < prog->aux->func_cnt)
  1016. addr = (u8 *)prog->aux->func[off]->bpf_func;
  1017. else
  1018. return -EINVAL;
  1019. } else {
  1020. /* Address of a BPF helper call. Since part of the core
  1021. * kernel, it's always at a fixed location. __bpf_call_base
  1022. * and the helper with imm relative to it are both in core
  1023. * kernel.
  1024. */
  1025. addr = (u8 *)__bpf_call_base + imm;
  1026. }
  1027. *func_addr = (unsigned long)addr;
  1028. return 0;
  1029. }
  1030. static int bpf_jit_blind_insn(const struct bpf_insn *from,
  1031. const struct bpf_insn *aux,
  1032. struct bpf_insn *to_buff,
  1033. bool emit_zext)
  1034. {
  1035. struct bpf_insn *to = to_buff;
  1036. u32 imm_rnd = get_random_u32();
  1037. s16 off;
  1038. BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
  1039. BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG);
  1040. /* Constraints on AX register:
  1041. *
  1042. * AX register is inaccessible from user space. It is mapped in
  1043. * all JITs, and used here for constant blinding rewrites. It is
  1044. * typically "stateless" meaning its contents are only valid within
  1045. * the executed instruction, but not across several instructions.
  1046. * There are a few exceptions however which are further detailed
  1047. * below.
  1048. *
  1049. * Constant blinding is only used by JITs, not in the interpreter.
  1050. * The interpreter uses AX in some occasions as a local temporary
  1051. * register e.g. in DIV or MOD instructions.
  1052. *
  1053. * In restricted circumstances, the verifier can also use the AX
  1054. * register for rewrites as long as they do not interfere with
  1055. * the above cases!
  1056. */
  1057. if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX)
  1058. goto out;
  1059. if (from->imm == 0 &&
  1060. (from->code == (BPF_ALU | BPF_MOV | BPF_K) ||
  1061. from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {
  1062. *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);
  1063. goto out;
  1064. }
  1065. switch (from->code) {
  1066. case BPF_ALU | BPF_ADD | BPF_K:
  1067. case BPF_ALU | BPF_SUB | BPF_K:
  1068. case BPF_ALU | BPF_AND | BPF_K:
  1069. case BPF_ALU | BPF_OR | BPF_K:
  1070. case BPF_ALU | BPF_XOR | BPF_K:
  1071. case BPF_ALU | BPF_MUL | BPF_K:
  1072. case BPF_ALU | BPF_MOV | BPF_K:
  1073. case BPF_ALU | BPF_DIV | BPF_K:
  1074. case BPF_ALU | BPF_MOD | BPF_K:
  1075. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  1076. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1077. *to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);
  1078. break;
  1079. case BPF_ALU64 | BPF_ADD | BPF_K:
  1080. case BPF_ALU64 | BPF_SUB | BPF_K:
  1081. case BPF_ALU64 | BPF_AND | BPF_K:
  1082. case BPF_ALU64 | BPF_OR | BPF_K:
  1083. case BPF_ALU64 | BPF_XOR | BPF_K:
  1084. case BPF_ALU64 | BPF_MUL | BPF_K:
  1085. case BPF_ALU64 | BPF_MOV | BPF_K:
  1086. case BPF_ALU64 | BPF_DIV | BPF_K:
  1087. case BPF_ALU64 | BPF_MOD | BPF_K:
  1088. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  1089. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1090. *to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);
  1091. break;
  1092. case BPF_JMP | BPF_JEQ | BPF_K:
  1093. case BPF_JMP | BPF_JNE | BPF_K:
  1094. case BPF_JMP | BPF_JGT | BPF_K:
  1095. case BPF_JMP | BPF_JLT | BPF_K:
  1096. case BPF_JMP | BPF_JGE | BPF_K:
  1097. case BPF_JMP | BPF_JLE | BPF_K:
  1098. case BPF_JMP | BPF_JSGT | BPF_K:
  1099. case BPF_JMP | BPF_JSLT | BPF_K:
  1100. case BPF_JMP | BPF_JSGE | BPF_K:
  1101. case BPF_JMP | BPF_JSLE | BPF_K:
  1102. case BPF_JMP | BPF_JSET | BPF_K:
  1103. /* Accommodate for extra offset in case of a backjump. */
  1104. off = from->off;
  1105. if (off < 0)
  1106. off -= 2;
  1107. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  1108. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1109. *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);
  1110. break;
  1111. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1112. case BPF_JMP32 | BPF_JNE | BPF_K:
  1113. case BPF_JMP32 | BPF_JGT | BPF_K:
  1114. case BPF_JMP32 | BPF_JLT | BPF_K:
  1115. case BPF_JMP32 | BPF_JGE | BPF_K:
  1116. case BPF_JMP32 | BPF_JLE | BPF_K:
  1117. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1118. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1119. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1120. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1121. case BPF_JMP32 | BPF_JSET | BPF_K:
  1122. /* Accommodate for extra offset in case of a backjump. */
  1123. off = from->off;
  1124. if (off < 0)
  1125. off -= 2;
  1126. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  1127. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1128. *to++ = BPF_JMP32_REG(from->code, from->dst_reg, BPF_REG_AX,
  1129. off);
  1130. break;
  1131. case BPF_LD | BPF_IMM | BPF_DW:
  1132. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);
  1133. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1134. *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
  1135. *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);
  1136. break;
  1137. case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */
  1138. *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);
  1139. *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1140. if (emit_zext)
  1141. *to++ = BPF_ZEXT_REG(BPF_REG_AX);
  1142. *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX);
  1143. break;
  1144. case BPF_ST | BPF_MEM | BPF_DW:
  1145. case BPF_ST | BPF_MEM | BPF_W:
  1146. case BPF_ST | BPF_MEM | BPF_H:
  1147. case BPF_ST | BPF_MEM | BPF_B:
  1148. *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);
  1149. *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);
  1150. *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);
  1151. break;
  1152. }
  1153. out:
  1154. return to - to_buff;
  1155. }
  1156. static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
  1157. gfp_t gfp_extra_flags)
  1158. {
  1159. gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags;
  1160. struct bpf_prog *fp;
  1161. fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags);
  1162. if (fp != NULL) {
  1163. /* aux->prog still points to the fp_other one, so
  1164. * when promoting the clone to the real program,
  1165. * this still needs to be adapted.
  1166. */
  1167. memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);
  1168. }
  1169. return fp;
  1170. }
  1171. static void bpf_prog_clone_free(struct bpf_prog *fp)
  1172. {
  1173. /* aux was stolen by the other clone, so we cannot free
  1174. * it from this path! It will be freed eventually by the
  1175. * other program on release.
  1176. *
  1177. * At this point, we don't need a deferred release since
  1178. * clone is guaranteed to not be locked.
  1179. */
  1180. fp->aux = NULL;
  1181. fp->stats = NULL;
  1182. fp->active = NULL;
  1183. __bpf_prog_free(fp);
  1184. }
  1185. void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
  1186. {
  1187. /* We have to repoint aux->prog to self, as we don't
  1188. * know whether fp here is the clone or the original.
  1189. */
  1190. fp->aux->prog = fp;
  1191. bpf_prog_clone_free(fp_other);
  1192. }
  1193. struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
  1194. {
  1195. struct bpf_insn insn_buff[16], aux[2];
  1196. struct bpf_prog *clone, *tmp;
  1197. int insn_delta, insn_cnt;
  1198. struct bpf_insn *insn;
  1199. int i, rewritten;
  1200. if (!prog->blinding_requested || prog->blinded)
  1201. return prog;
  1202. clone = bpf_prog_clone_create(prog, GFP_USER);
  1203. if (!clone)
  1204. return ERR_PTR(-ENOMEM);
  1205. insn_cnt = clone->len;
  1206. insn = clone->insnsi;
  1207. for (i = 0; i < insn_cnt; i++, insn++) {
  1208. if (bpf_pseudo_func(insn)) {
  1209. /* ld_imm64 with an address of bpf subprog is not
  1210. * a user controlled constant. Don't randomize it,
  1211. * since it will conflict with jit_subprogs() logic.
  1212. */
  1213. insn++;
  1214. i++;
  1215. continue;
  1216. }
  1217. /* We temporarily need to hold the original ld64 insn
  1218. * so that we can still access the first part in the
  1219. * second blinding run.
  1220. */
  1221. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&
  1222. insn[1].code == 0)
  1223. memcpy(aux, insn, sizeof(aux));
  1224. rewritten = bpf_jit_blind_insn(insn, aux, insn_buff,
  1225. clone->aux->verifier_zext);
  1226. if (!rewritten)
  1227. continue;
  1228. tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
  1229. if (IS_ERR(tmp)) {
  1230. /* Patching may have repointed aux->prog during
  1231. * realloc from the original one, so we need to
  1232. * fix it up here on error.
  1233. */
  1234. bpf_jit_prog_release_other(prog, clone);
  1235. return tmp;
  1236. }
  1237. clone = tmp;
  1238. insn_delta = rewritten - 1;
  1239. /* Walk new program and skip insns we just inserted. */
  1240. insn = clone->insnsi + i + insn_delta;
  1241. insn_cnt += insn_delta;
  1242. i += insn_delta;
  1243. }
  1244. clone->blinded = 1;
  1245. return clone;
  1246. }
  1247. #endif /* CONFIG_BPF_JIT */
  1248. /* Base function for offset calculation. Needs to go into .text section,
  1249. * therefore keeping it non-static as well; will also be used by JITs
  1250. * anyway later on, so do not let the compiler omit it. This also needs
  1251. * to go into kallsyms for correlation from e.g. bpftool, so naming
  1252. * must not change.
  1253. */
  1254. noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  1255. {
  1256. return 0;
  1257. }
  1258. EXPORT_SYMBOL_GPL(__bpf_call_base);
  1259. /* All UAPI available opcodes. */
  1260. #define BPF_INSN_MAP(INSN_2, INSN_3) \
  1261. /* 32 bit ALU operations. */ \
  1262. /* Register based. */ \
  1263. INSN_3(ALU, ADD, X), \
  1264. INSN_3(ALU, SUB, X), \
  1265. INSN_3(ALU, AND, X), \
  1266. INSN_3(ALU, OR, X), \
  1267. INSN_3(ALU, LSH, X), \
  1268. INSN_3(ALU, RSH, X), \
  1269. INSN_3(ALU, XOR, X), \
  1270. INSN_3(ALU, MUL, X), \
  1271. INSN_3(ALU, MOV, X), \
  1272. INSN_3(ALU, ARSH, X), \
  1273. INSN_3(ALU, DIV, X), \
  1274. INSN_3(ALU, MOD, X), \
  1275. INSN_2(ALU, NEG), \
  1276. INSN_3(ALU, END, TO_BE), \
  1277. INSN_3(ALU, END, TO_LE), \
  1278. /* Immediate based. */ \
  1279. INSN_3(ALU, ADD, K), \
  1280. INSN_3(ALU, SUB, K), \
  1281. INSN_3(ALU, AND, K), \
  1282. INSN_3(ALU, OR, K), \
  1283. INSN_3(ALU, LSH, K), \
  1284. INSN_3(ALU, RSH, K), \
  1285. INSN_3(ALU, XOR, K), \
  1286. INSN_3(ALU, MUL, K), \
  1287. INSN_3(ALU, MOV, K), \
  1288. INSN_3(ALU, ARSH, K), \
  1289. INSN_3(ALU, DIV, K), \
  1290. INSN_3(ALU, MOD, K), \
  1291. /* 64 bit ALU operations. */ \
  1292. /* Register based. */ \
  1293. INSN_3(ALU64, ADD, X), \
  1294. INSN_3(ALU64, SUB, X), \
  1295. INSN_3(ALU64, AND, X), \
  1296. INSN_3(ALU64, OR, X), \
  1297. INSN_3(ALU64, LSH, X), \
  1298. INSN_3(ALU64, RSH, X), \
  1299. INSN_3(ALU64, XOR, X), \
  1300. INSN_3(ALU64, MUL, X), \
  1301. INSN_3(ALU64, MOV, X), \
  1302. INSN_3(ALU64, ARSH, X), \
  1303. INSN_3(ALU64, DIV, X), \
  1304. INSN_3(ALU64, MOD, X), \
  1305. INSN_2(ALU64, NEG), \
  1306. /* Immediate based. */ \
  1307. INSN_3(ALU64, ADD, K), \
  1308. INSN_3(ALU64, SUB, K), \
  1309. INSN_3(ALU64, AND, K), \
  1310. INSN_3(ALU64, OR, K), \
  1311. INSN_3(ALU64, LSH, K), \
  1312. INSN_3(ALU64, RSH, K), \
  1313. INSN_3(ALU64, XOR, K), \
  1314. INSN_3(ALU64, MUL, K), \
  1315. INSN_3(ALU64, MOV, K), \
  1316. INSN_3(ALU64, ARSH, K), \
  1317. INSN_3(ALU64, DIV, K), \
  1318. INSN_3(ALU64, MOD, K), \
  1319. /* Call instruction. */ \
  1320. INSN_2(JMP, CALL), \
  1321. /* Exit instruction. */ \
  1322. INSN_2(JMP, EXIT), \
  1323. /* 32-bit Jump instructions. */ \
  1324. /* Register based. */ \
  1325. INSN_3(JMP32, JEQ, X), \
  1326. INSN_3(JMP32, JNE, X), \
  1327. INSN_3(JMP32, JGT, X), \
  1328. INSN_3(JMP32, JLT, X), \
  1329. INSN_3(JMP32, JGE, X), \
  1330. INSN_3(JMP32, JLE, X), \
  1331. INSN_3(JMP32, JSGT, X), \
  1332. INSN_3(JMP32, JSLT, X), \
  1333. INSN_3(JMP32, JSGE, X), \
  1334. INSN_3(JMP32, JSLE, X), \
  1335. INSN_3(JMP32, JSET, X), \
  1336. /* Immediate based. */ \
  1337. INSN_3(JMP32, JEQ, K), \
  1338. INSN_3(JMP32, JNE, K), \
  1339. INSN_3(JMP32, JGT, K), \
  1340. INSN_3(JMP32, JLT, K), \
  1341. INSN_3(JMP32, JGE, K), \
  1342. INSN_3(JMP32, JLE, K), \
  1343. INSN_3(JMP32, JSGT, K), \
  1344. INSN_3(JMP32, JSLT, K), \
  1345. INSN_3(JMP32, JSGE, K), \
  1346. INSN_3(JMP32, JSLE, K), \
  1347. INSN_3(JMP32, JSET, K), \
  1348. /* Jump instructions. */ \
  1349. /* Register based. */ \
  1350. INSN_3(JMP, JEQ, X), \
  1351. INSN_3(JMP, JNE, X), \
  1352. INSN_3(JMP, JGT, X), \
  1353. INSN_3(JMP, JLT, X), \
  1354. INSN_3(JMP, JGE, X), \
  1355. INSN_3(JMP, JLE, X), \
  1356. INSN_3(JMP, JSGT, X), \
  1357. INSN_3(JMP, JSLT, X), \
  1358. INSN_3(JMP, JSGE, X), \
  1359. INSN_3(JMP, JSLE, X), \
  1360. INSN_3(JMP, JSET, X), \
  1361. /* Immediate based. */ \
  1362. INSN_3(JMP, JEQ, K), \
  1363. INSN_3(JMP, JNE, K), \
  1364. INSN_3(JMP, JGT, K), \
  1365. INSN_3(JMP, JLT, K), \
  1366. INSN_3(JMP, JGE, K), \
  1367. INSN_3(JMP, JLE, K), \
  1368. INSN_3(JMP, JSGT, K), \
  1369. INSN_3(JMP, JSLT, K), \
  1370. INSN_3(JMP, JSGE, K), \
  1371. INSN_3(JMP, JSLE, K), \
  1372. INSN_3(JMP, JSET, K), \
  1373. INSN_2(JMP, JA), \
  1374. /* Store instructions. */ \
  1375. /* Register based. */ \
  1376. INSN_3(STX, MEM, B), \
  1377. INSN_3(STX, MEM, H), \
  1378. INSN_3(STX, MEM, W), \
  1379. INSN_3(STX, MEM, DW), \
  1380. INSN_3(STX, ATOMIC, W), \
  1381. INSN_3(STX, ATOMIC, DW), \
  1382. /* Immediate based. */ \
  1383. INSN_3(ST, MEM, B), \
  1384. INSN_3(ST, MEM, H), \
  1385. INSN_3(ST, MEM, W), \
  1386. INSN_3(ST, MEM, DW), \
  1387. /* Load instructions. */ \
  1388. /* Register based. */ \
  1389. INSN_3(LDX, MEM, B), \
  1390. INSN_3(LDX, MEM, H), \
  1391. INSN_3(LDX, MEM, W), \
  1392. INSN_3(LDX, MEM, DW), \
  1393. /* Immediate based. */ \
  1394. INSN_3(LD, IMM, DW)
  1395. bool bpf_opcode_in_insntable(u8 code)
  1396. {
  1397. #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true
  1398. #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true
  1399. static const bool public_insntable[256] = {
  1400. [0 ... 255] = false,
  1401. /* Now overwrite non-defaults ... */
  1402. BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL),
  1403. /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */
  1404. [BPF_LD | BPF_ABS | BPF_B] = true,
  1405. [BPF_LD | BPF_ABS | BPF_H] = true,
  1406. [BPF_LD | BPF_ABS | BPF_W] = true,
  1407. [BPF_LD | BPF_IND | BPF_B] = true,
  1408. [BPF_LD | BPF_IND | BPF_H] = true,
  1409. [BPF_LD | BPF_IND | BPF_W] = true,
  1410. };
  1411. #undef BPF_INSN_3_TBL
  1412. #undef BPF_INSN_2_TBL
  1413. return public_insntable[code];
  1414. }
  1415. #ifndef CONFIG_BPF_JIT_ALWAYS_ON
  1416. u64 __weak bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr)
  1417. {
  1418. memset(dst, 0, size);
  1419. return -EFAULT;
  1420. }
  1421. /**
  1422. * ___bpf_prog_run - run eBPF program on a given context
  1423. * @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers
  1424. * @insn: is the array of eBPF instructions
  1425. *
  1426. * Decode and execute eBPF instructions.
  1427. *
  1428. * Return: whatever value is in %BPF_R0 at program exit
  1429. */
  1430. static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
  1431. {
  1432. #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
  1433. #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z
  1434. static const void * const jumptable[256] __annotate_jump_table = {
  1435. [0 ... 255] = &&default_label,
  1436. /* Now overwrite non-defaults ... */
  1437. BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL),
  1438. /* Non-UAPI available opcodes. */
  1439. [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS,
  1440. [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL,
  1441. [BPF_ST | BPF_NOSPEC] = &&ST_NOSPEC,
  1442. [BPF_LDX | BPF_PROBE_MEM | BPF_B] = &&LDX_PROBE_MEM_B,
  1443. [BPF_LDX | BPF_PROBE_MEM | BPF_H] = &&LDX_PROBE_MEM_H,
  1444. [BPF_LDX | BPF_PROBE_MEM | BPF_W] = &&LDX_PROBE_MEM_W,
  1445. [BPF_LDX | BPF_PROBE_MEM | BPF_DW] = &&LDX_PROBE_MEM_DW,
  1446. };
  1447. #undef BPF_INSN_3_LBL
  1448. #undef BPF_INSN_2_LBL
  1449. u32 tail_call_cnt = 0;
  1450. #define CONT ({ insn++; goto select_insn; })
  1451. #define CONT_JMP ({ insn++; goto select_insn; })
  1452. select_insn:
  1453. goto *jumptable[insn->code];
  1454. /* Explicitly mask the register-based shift amounts with 63 or 31
  1455. * to avoid undefined behavior. Normally this won't affect the
  1456. * generated code, for example, in case of native 64 bit archs such
  1457. * as x86-64 or arm64, the compiler is optimizing the AND away for
  1458. * the interpreter. In case of JITs, each of the JIT backends compiles
  1459. * the BPF shift operations to machine instructions which produce
  1460. * implementation-defined results in such a case; the resulting
  1461. * contents of the register may be arbitrary, but program behaviour
  1462. * as a whole remains defined. In other words, in case of JIT backends,
  1463. * the AND must /not/ be added to the emitted LSH/RSH/ARSH translation.
  1464. */
  1465. /* ALU (shifts) */
  1466. #define SHT(OPCODE, OP) \
  1467. ALU64_##OPCODE##_X: \
  1468. DST = DST OP (SRC & 63); \
  1469. CONT; \
  1470. ALU_##OPCODE##_X: \
  1471. DST = (u32) DST OP ((u32) SRC & 31); \
  1472. CONT; \
  1473. ALU64_##OPCODE##_K: \
  1474. DST = DST OP IMM; \
  1475. CONT; \
  1476. ALU_##OPCODE##_K: \
  1477. DST = (u32) DST OP (u32) IMM; \
  1478. CONT;
  1479. /* ALU (rest) */
  1480. #define ALU(OPCODE, OP) \
  1481. ALU64_##OPCODE##_X: \
  1482. DST = DST OP SRC; \
  1483. CONT; \
  1484. ALU_##OPCODE##_X: \
  1485. DST = (u32) DST OP (u32) SRC; \
  1486. CONT; \
  1487. ALU64_##OPCODE##_K: \
  1488. DST = DST OP IMM; \
  1489. CONT; \
  1490. ALU_##OPCODE##_K: \
  1491. DST = (u32) DST OP (u32) IMM; \
  1492. CONT;
  1493. ALU(ADD, +)
  1494. ALU(SUB, -)
  1495. ALU(AND, &)
  1496. ALU(OR, |)
  1497. ALU(XOR, ^)
  1498. ALU(MUL, *)
  1499. SHT(LSH, <<)
  1500. SHT(RSH, >>)
  1501. #undef SHT
  1502. #undef ALU
  1503. ALU_NEG:
  1504. DST = (u32) -DST;
  1505. CONT;
  1506. ALU64_NEG:
  1507. DST = -DST;
  1508. CONT;
  1509. ALU_MOV_X:
  1510. DST = (u32) SRC;
  1511. CONT;
  1512. ALU_MOV_K:
  1513. DST = (u32) IMM;
  1514. CONT;
  1515. ALU64_MOV_X:
  1516. DST = SRC;
  1517. CONT;
  1518. ALU64_MOV_K:
  1519. DST = IMM;
  1520. CONT;
  1521. LD_IMM_DW:
  1522. DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
  1523. insn++;
  1524. CONT;
  1525. ALU_ARSH_X:
  1526. DST = (u64) (u32) (((s32) DST) >> (SRC & 31));
  1527. CONT;
  1528. ALU_ARSH_K:
  1529. DST = (u64) (u32) (((s32) DST) >> IMM);
  1530. CONT;
  1531. ALU64_ARSH_X:
  1532. (*(s64 *) &DST) >>= (SRC & 63);
  1533. CONT;
  1534. ALU64_ARSH_K:
  1535. (*(s64 *) &DST) >>= IMM;
  1536. CONT;
  1537. ALU64_MOD_X:
  1538. div64_u64_rem(DST, SRC, &AX);
  1539. DST = AX;
  1540. CONT;
  1541. ALU_MOD_X:
  1542. AX = (u32) DST;
  1543. DST = do_div(AX, (u32) SRC);
  1544. CONT;
  1545. ALU64_MOD_K:
  1546. div64_u64_rem(DST, IMM, &AX);
  1547. DST = AX;
  1548. CONT;
  1549. ALU_MOD_K:
  1550. AX = (u32) DST;
  1551. DST = do_div(AX, (u32) IMM);
  1552. CONT;
  1553. ALU64_DIV_X:
  1554. DST = div64_u64(DST, SRC);
  1555. CONT;
  1556. ALU_DIV_X:
  1557. AX = (u32) DST;
  1558. do_div(AX, (u32) SRC);
  1559. DST = (u32) AX;
  1560. CONT;
  1561. ALU64_DIV_K:
  1562. DST = div64_u64(DST, IMM);
  1563. CONT;
  1564. ALU_DIV_K:
  1565. AX = (u32) DST;
  1566. do_div(AX, (u32) IMM);
  1567. DST = (u32) AX;
  1568. CONT;
  1569. ALU_END_TO_BE:
  1570. switch (IMM) {
  1571. case 16:
  1572. DST = (__force u16) cpu_to_be16(DST);
  1573. break;
  1574. case 32:
  1575. DST = (__force u32) cpu_to_be32(DST);
  1576. break;
  1577. case 64:
  1578. DST = (__force u64) cpu_to_be64(DST);
  1579. break;
  1580. }
  1581. CONT;
  1582. ALU_END_TO_LE:
  1583. switch (IMM) {
  1584. case 16:
  1585. DST = (__force u16) cpu_to_le16(DST);
  1586. break;
  1587. case 32:
  1588. DST = (__force u32) cpu_to_le32(DST);
  1589. break;
  1590. case 64:
  1591. DST = (__force u64) cpu_to_le64(DST);
  1592. break;
  1593. }
  1594. CONT;
  1595. /* CALL */
  1596. JMP_CALL:
  1597. /* Function call scratches BPF_R1-BPF_R5 registers,
  1598. * preserves BPF_R6-BPF_R9, and stores return value
  1599. * into BPF_R0.
  1600. */
  1601. BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
  1602. BPF_R4, BPF_R5);
  1603. CONT;
  1604. JMP_CALL_ARGS:
  1605. BPF_R0 = (__bpf_call_base_args + insn->imm)(BPF_R1, BPF_R2,
  1606. BPF_R3, BPF_R4,
  1607. BPF_R5,
  1608. insn + insn->off + 1);
  1609. CONT;
  1610. JMP_TAIL_CALL: {
  1611. struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2;
  1612. struct bpf_array *array = container_of(map, struct bpf_array, map);
  1613. struct bpf_prog *prog;
  1614. u32 index = BPF_R3;
  1615. if (unlikely(index >= array->map.max_entries))
  1616. goto out;
  1617. if (unlikely(tail_call_cnt >= MAX_TAIL_CALL_CNT))
  1618. goto out;
  1619. tail_call_cnt++;
  1620. prog = READ_ONCE(array->ptrs[index]);
  1621. if (!prog)
  1622. goto out;
  1623. /* ARG1 at this point is guaranteed to point to CTX from
  1624. * the verifier side due to the fact that the tail call is
  1625. * handled like a helper, that is, bpf_tail_call_proto,
  1626. * where arg1_type is ARG_PTR_TO_CTX.
  1627. */
  1628. insn = prog->insnsi;
  1629. goto select_insn;
  1630. out:
  1631. CONT;
  1632. }
  1633. JMP_JA:
  1634. insn += insn->off;
  1635. CONT;
  1636. JMP_EXIT:
  1637. return BPF_R0;
  1638. /* JMP */
  1639. #define COND_JMP(SIGN, OPCODE, CMP_OP) \
  1640. JMP_##OPCODE##_X: \
  1641. if ((SIGN##64) DST CMP_OP (SIGN##64) SRC) { \
  1642. insn += insn->off; \
  1643. CONT_JMP; \
  1644. } \
  1645. CONT; \
  1646. JMP32_##OPCODE##_X: \
  1647. if ((SIGN##32) DST CMP_OP (SIGN##32) SRC) { \
  1648. insn += insn->off; \
  1649. CONT_JMP; \
  1650. } \
  1651. CONT; \
  1652. JMP_##OPCODE##_K: \
  1653. if ((SIGN##64) DST CMP_OP (SIGN##64) IMM) { \
  1654. insn += insn->off; \
  1655. CONT_JMP; \
  1656. } \
  1657. CONT; \
  1658. JMP32_##OPCODE##_K: \
  1659. if ((SIGN##32) DST CMP_OP (SIGN##32) IMM) { \
  1660. insn += insn->off; \
  1661. CONT_JMP; \
  1662. } \
  1663. CONT;
  1664. COND_JMP(u, JEQ, ==)
  1665. COND_JMP(u, JNE, !=)
  1666. COND_JMP(u, JGT, >)
  1667. COND_JMP(u, JLT, <)
  1668. COND_JMP(u, JGE, >=)
  1669. COND_JMP(u, JLE, <=)
  1670. COND_JMP(u, JSET, &)
  1671. COND_JMP(s, JSGT, >)
  1672. COND_JMP(s, JSLT, <)
  1673. COND_JMP(s, JSGE, >=)
  1674. COND_JMP(s, JSLE, <=)
  1675. #undef COND_JMP
  1676. /* ST, STX and LDX*/
  1677. ST_NOSPEC:
  1678. /* Speculation barrier for mitigating Speculative Store Bypass.
  1679. * In case of arm64, we rely on the firmware mitigation as
  1680. * controlled via the ssbd kernel parameter. Whenever the
  1681. * mitigation is enabled, it works for all of the kernel code
  1682. * with no need to provide any additional instructions here.
  1683. * In case of x86, we use 'lfence' insn for mitigation. We
  1684. * reuse preexisting logic from Spectre v1 mitigation that
  1685. * happens to produce the required code on x86 for v4 as well.
  1686. */
  1687. barrier_nospec();
  1688. CONT;
  1689. #define LDST(SIZEOP, SIZE) \
  1690. STX_MEM_##SIZEOP: \
  1691. *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \
  1692. CONT; \
  1693. ST_MEM_##SIZEOP: \
  1694. *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \
  1695. CONT; \
  1696. LDX_MEM_##SIZEOP: \
  1697. DST = *(SIZE *)(unsigned long) (SRC + insn->off); \
  1698. CONT; \
  1699. LDX_PROBE_MEM_##SIZEOP: \
  1700. bpf_probe_read_kernel(&DST, sizeof(SIZE), \
  1701. (const void *)(long) (SRC + insn->off)); \
  1702. DST = *((SIZE *)&DST); \
  1703. CONT;
  1704. LDST(B, u8)
  1705. LDST(H, u16)
  1706. LDST(W, u32)
  1707. LDST(DW, u64)
  1708. #undef LDST
  1709. #define ATOMIC_ALU_OP(BOP, KOP) \
  1710. case BOP: \
  1711. if (BPF_SIZE(insn->code) == BPF_W) \
  1712. atomic_##KOP((u32) SRC, (atomic_t *)(unsigned long) \
  1713. (DST + insn->off)); \
  1714. else \
  1715. atomic64_##KOP((u64) SRC, (atomic64_t *)(unsigned long) \
  1716. (DST + insn->off)); \
  1717. break; \
  1718. case BOP | BPF_FETCH: \
  1719. if (BPF_SIZE(insn->code) == BPF_W) \
  1720. SRC = (u32) atomic_fetch_##KOP( \
  1721. (u32) SRC, \
  1722. (atomic_t *)(unsigned long) (DST + insn->off)); \
  1723. else \
  1724. SRC = (u64) atomic64_fetch_##KOP( \
  1725. (u64) SRC, \
  1726. (atomic64_t *)(unsigned long) (DST + insn->off)); \
  1727. break;
  1728. STX_ATOMIC_DW:
  1729. STX_ATOMIC_W:
  1730. switch (IMM) {
  1731. ATOMIC_ALU_OP(BPF_ADD, add)
  1732. ATOMIC_ALU_OP(BPF_AND, and)
  1733. ATOMIC_ALU_OP(BPF_OR, or)
  1734. ATOMIC_ALU_OP(BPF_XOR, xor)
  1735. #undef ATOMIC_ALU_OP
  1736. case BPF_XCHG:
  1737. if (BPF_SIZE(insn->code) == BPF_W)
  1738. SRC = (u32) atomic_xchg(
  1739. (atomic_t *)(unsigned long) (DST + insn->off),
  1740. (u32) SRC);
  1741. else
  1742. SRC = (u64) atomic64_xchg(
  1743. (atomic64_t *)(unsigned long) (DST + insn->off),
  1744. (u64) SRC);
  1745. break;
  1746. case BPF_CMPXCHG:
  1747. if (BPF_SIZE(insn->code) == BPF_W)
  1748. BPF_R0 = (u32) atomic_cmpxchg(
  1749. (atomic_t *)(unsigned long) (DST + insn->off),
  1750. (u32) BPF_R0, (u32) SRC);
  1751. else
  1752. BPF_R0 = (u64) atomic64_cmpxchg(
  1753. (atomic64_t *)(unsigned long) (DST + insn->off),
  1754. (u64) BPF_R0, (u64) SRC);
  1755. break;
  1756. default:
  1757. goto default_label;
  1758. }
  1759. CONT;
  1760. default_label:
  1761. /* If we ever reach this, we have a bug somewhere. Die hard here
  1762. * instead of just returning 0; we could be somewhere in a subprog,
  1763. * so execution could continue otherwise which we do /not/ want.
  1764. *
  1765. * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable().
  1766. */
  1767. pr_warn("BPF interpreter: unknown opcode %02x (imm: 0x%x)\n",
  1768. insn->code, insn->imm);
  1769. BUG_ON(1);
  1770. return 0;
  1771. }
  1772. #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
  1773. #define DEFINE_BPF_PROG_RUN(stack_size) \
  1774. static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
  1775. { \
  1776. u64 stack[stack_size / sizeof(u64)]; \
  1777. u64 regs[MAX_BPF_EXT_REG] = {}; \
  1778. \
  1779. FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
  1780. ARG1 = (u64) (unsigned long) ctx; \
  1781. return ___bpf_prog_run(regs, insn); \
  1782. }
  1783. #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size
  1784. #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \
  1785. static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \
  1786. const struct bpf_insn *insn) \
  1787. { \
  1788. u64 stack[stack_size / sizeof(u64)]; \
  1789. u64 regs[MAX_BPF_EXT_REG]; \
  1790. \
  1791. FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \
  1792. BPF_R1 = r1; \
  1793. BPF_R2 = r2; \
  1794. BPF_R3 = r3; \
  1795. BPF_R4 = r4; \
  1796. BPF_R5 = r5; \
  1797. return ___bpf_prog_run(regs, insn); \
  1798. }
  1799. #define EVAL1(FN, X) FN(X)
  1800. #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y)
  1801. #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y)
  1802. #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y)
  1803. #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y)
  1804. #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y)
  1805. EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192);
  1806. EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384);
  1807. EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512);
  1808. EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192);
  1809. EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384);
  1810. EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512);
  1811. #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size),
  1812. static unsigned int (*interpreters[])(const void *ctx,
  1813. const struct bpf_insn *insn) = {
  1814. EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
  1815. EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
  1816. EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
  1817. };
  1818. #undef PROG_NAME_LIST
  1819. #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size),
  1820. static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5,
  1821. const struct bpf_insn *insn) = {
  1822. EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192)
  1823. EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384)
  1824. EVAL4(PROG_NAME_LIST, 416, 448, 480, 512)
  1825. };
  1826. #undef PROG_NAME_LIST
  1827. void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
  1828. {
  1829. stack_depth = max_t(u32, stack_depth, 1);
  1830. insn->off = (s16) insn->imm;
  1831. insn->imm = interpreters_args[(round_up(stack_depth, 32) / 32) - 1] -
  1832. __bpf_call_base_args;
  1833. insn->code = BPF_JMP | BPF_CALL_ARGS;
  1834. }
  1835. #else
  1836. static unsigned int __bpf_prog_ret0_warn(const void *ctx,
  1837. const struct bpf_insn *insn)
  1838. {
  1839. /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
  1840. * is not working properly, so warn about it!
  1841. */
  1842. WARN_ON_ONCE(1);
  1843. return 0;
  1844. }
  1845. #endif
  1846. bool bpf_prog_map_compatible(struct bpf_map *map,
  1847. const struct bpf_prog *fp)
  1848. {
  1849. enum bpf_prog_type prog_type = resolve_prog_type(fp);
  1850. bool ret;
  1851. if (fp->kprobe_override)
  1852. return false;
  1853. spin_lock(&map->owner.lock);
  1854. if (!map->owner.type) {
  1855. /* There's no owner yet where we could check for
  1856. * compatibility.
  1857. */
  1858. map->owner.type = prog_type;
  1859. map->owner.jited = fp->jited;
  1860. map->owner.xdp_has_frags = fp->aux->xdp_has_frags;
  1861. ret = true;
  1862. } else {
  1863. ret = map->owner.type == prog_type &&
  1864. map->owner.jited == fp->jited &&
  1865. map->owner.xdp_has_frags == fp->aux->xdp_has_frags;
  1866. }
  1867. spin_unlock(&map->owner.lock);
  1868. return ret;
  1869. }
  1870. static int bpf_check_tail_call(const struct bpf_prog *fp)
  1871. {
  1872. struct bpf_prog_aux *aux = fp->aux;
  1873. int i, ret = 0;
  1874. mutex_lock(&aux->used_maps_mutex);
  1875. for (i = 0; i < aux->used_map_cnt; i++) {
  1876. struct bpf_map *map = aux->used_maps[i];
  1877. if (!map_type_contains_progs(map))
  1878. continue;
  1879. if (!bpf_prog_map_compatible(map, fp)) {
  1880. ret = -EINVAL;
  1881. goto out;
  1882. }
  1883. }
  1884. out:
  1885. mutex_unlock(&aux->used_maps_mutex);
  1886. return ret;
  1887. }
  1888. static void bpf_prog_select_func(struct bpf_prog *fp)
  1889. {
  1890. #ifndef CONFIG_BPF_JIT_ALWAYS_ON
  1891. u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
  1892. fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
  1893. #else
  1894. fp->bpf_func = __bpf_prog_ret0_warn;
  1895. #endif
  1896. }
  1897. /**
  1898. * bpf_prog_select_runtime - select exec runtime for BPF program
  1899. * @fp: bpf_prog populated with BPF program
  1900. * @err: pointer to error variable
  1901. *
  1902. * Try to JIT eBPF program, if JIT is not available, use interpreter.
  1903. * The BPF program will be executed via bpf_prog_run() function.
  1904. *
  1905. * Return: the &fp argument along with &err set to 0 for success or
  1906. * a negative errno code on failure
  1907. */
  1908. struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
  1909. {
  1910. /* In case of BPF to BPF calls, verifier did all the prep
  1911. * work with regards to JITing, etc.
  1912. */
  1913. bool jit_needed = false;
  1914. if (fp->bpf_func)
  1915. goto finalize;
  1916. if (IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) ||
  1917. bpf_prog_has_kfunc_call(fp))
  1918. jit_needed = true;
  1919. bpf_prog_select_func(fp);
  1920. /* eBPF JITs can rewrite the program in case constant
  1921. * blinding is active. However, in case of error during
  1922. * blinding, bpf_int_jit_compile() must always return a
  1923. * valid program, which in this case would simply not
  1924. * be JITed, but falls back to the interpreter.
  1925. */
  1926. if (!bpf_prog_is_dev_bound(fp->aux)) {
  1927. *err = bpf_prog_alloc_jited_linfo(fp);
  1928. if (*err)
  1929. return fp;
  1930. fp = bpf_int_jit_compile(fp);
  1931. bpf_prog_jit_attempt_done(fp);
  1932. if (!fp->jited && jit_needed) {
  1933. *err = -ENOTSUPP;
  1934. return fp;
  1935. }
  1936. } else {
  1937. *err = bpf_prog_offload_compile(fp);
  1938. if (*err)
  1939. return fp;
  1940. }
  1941. finalize:
  1942. bpf_prog_lock_ro(fp);
  1943. /* The tail call compatibility check can only be done at
  1944. * this late stage as we need to determine, if we deal
  1945. * with JITed or non JITed program concatenations and not
  1946. * all eBPF JITs might immediately support all features.
  1947. */
  1948. *err = bpf_check_tail_call(fp);
  1949. return fp;
  1950. }
  1951. EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
  1952. static unsigned int __bpf_prog_ret1(const void *ctx,
  1953. const struct bpf_insn *insn)
  1954. {
  1955. return 1;
  1956. }
  1957. static struct bpf_prog_dummy {
  1958. struct bpf_prog prog;
  1959. } dummy_bpf_prog = {
  1960. .prog = {
  1961. .bpf_func = __bpf_prog_ret1,
  1962. },
  1963. };
  1964. struct bpf_empty_prog_array bpf_empty_prog_array = {
  1965. .null_prog = NULL,
  1966. };
  1967. EXPORT_SYMBOL(bpf_empty_prog_array);
  1968. struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
  1969. {
  1970. if (prog_cnt)
  1971. return kzalloc(sizeof(struct bpf_prog_array) +
  1972. sizeof(struct bpf_prog_array_item) *
  1973. (prog_cnt + 1),
  1974. flags);
  1975. return &bpf_empty_prog_array.hdr;
  1976. }
  1977. void bpf_prog_array_free(struct bpf_prog_array *progs)
  1978. {
  1979. if (!progs || progs == &bpf_empty_prog_array.hdr)
  1980. return;
  1981. kfree_rcu(progs, rcu);
  1982. }
  1983. static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu)
  1984. {
  1985. struct bpf_prog_array *progs;
  1986. progs = container_of(rcu, struct bpf_prog_array, rcu);
  1987. kfree_rcu(progs, rcu);
  1988. }
  1989. void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs)
  1990. {
  1991. if (!progs || progs == &bpf_empty_prog_array.hdr)
  1992. return;
  1993. call_rcu_tasks_trace(&progs->rcu, __bpf_prog_array_free_sleepable_cb);
  1994. }
  1995. int bpf_prog_array_length(struct bpf_prog_array *array)
  1996. {
  1997. struct bpf_prog_array_item *item;
  1998. u32 cnt = 0;
  1999. for (item = array->items; item->prog; item++)
  2000. if (item->prog != &dummy_bpf_prog.prog)
  2001. cnt++;
  2002. return cnt;
  2003. }
  2004. bool bpf_prog_array_is_empty(struct bpf_prog_array *array)
  2005. {
  2006. struct bpf_prog_array_item *item;
  2007. for (item = array->items; item->prog; item++)
  2008. if (item->prog != &dummy_bpf_prog.prog)
  2009. return false;
  2010. return true;
  2011. }
  2012. static bool bpf_prog_array_copy_core(struct bpf_prog_array *array,
  2013. u32 *prog_ids,
  2014. u32 request_cnt)
  2015. {
  2016. struct bpf_prog_array_item *item;
  2017. int i = 0;
  2018. for (item = array->items; item->prog; item++) {
  2019. if (item->prog == &dummy_bpf_prog.prog)
  2020. continue;
  2021. prog_ids[i] = item->prog->aux->id;
  2022. if (++i == request_cnt) {
  2023. item++;
  2024. break;
  2025. }
  2026. }
  2027. return !!(item->prog);
  2028. }
  2029. int bpf_prog_array_copy_to_user(struct bpf_prog_array *array,
  2030. __u32 __user *prog_ids, u32 cnt)
  2031. {
  2032. unsigned long err = 0;
  2033. bool nospc;
  2034. u32 *ids;
  2035. /* users of this function are doing:
  2036. * cnt = bpf_prog_array_length();
  2037. * if (cnt > 0)
  2038. * bpf_prog_array_copy_to_user(..., cnt);
  2039. * so below kcalloc doesn't need extra cnt > 0 check.
  2040. */
  2041. ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
  2042. if (!ids)
  2043. return -ENOMEM;
  2044. nospc = bpf_prog_array_copy_core(array, ids, cnt);
  2045. err = copy_to_user(prog_ids, ids, cnt * sizeof(u32));
  2046. kfree(ids);
  2047. if (err)
  2048. return -EFAULT;
  2049. if (nospc)
  2050. return -ENOSPC;
  2051. return 0;
  2052. }
  2053. void bpf_prog_array_delete_safe(struct bpf_prog_array *array,
  2054. struct bpf_prog *old_prog)
  2055. {
  2056. struct bpf_prog_array_item *item;
  2057. for (item = array->items; item->prog; item++)
  2058. if (item->prog == old_prog) {
  2059. WRITE_ONCE(item->prog, &dummy_bpf_prog.prog);
  2060. break;
  2061. }
  2062. }
  2063. /**
  2064. * bpf_prog_array_delete_safe_at() - Replaces the program at the given
  2065. * index into the program array with
  2066. * a dummy no-op program.
  2067. * @array: a bpf_prog_array
  2068. * @index: the index of the program to replace
  2069. *
  2070. * Skips over dummy programs, by not counting them, when calculating
  2071. * the position of the program to replace.
  2072. *
  2073. * Return:
  2074. * * 0 - Success
  2075. * * -EINVAL - Invalid index value. Must be a non-negative integer.
  2076. * * -ENOENT - Index out of range
  2077. */
  2078. int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index)
  2079. {
  2080. return bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog);
  2081. }
  2082. /**
  2083. * bpf_prog_array_update_at() - Updates the program at the given index
  2084. * into the program array.
  2085. * @array: a bpf_prog_array
  2086. * @index: the index of the program to update
  2087. * @prog: the program to insert into the array
  2088. *
  2089. * Skips over dummy programs, by not counting them, when calculating
  2090. * the position of the program to update.
  2091. *
  2092. * Return:
  2093. * * 0 - Success
  2094. * * -EINVAL - Invalid index value. Must be a non-negative integer.
  2095. * * -ENOENT - Index out of range
  2096. */
  2097. int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
  2098. struct bpf_prog *prog)
  2099. {
  2100. struct bpf_prog_array_item *item;
  2101. if (unlikely(index < 0))
  2102. return -EINVAL;
  2103. for (item = array->items; item->prog; item++) {
  2104. if (item->prog == &dummy_bpf_prog.prog)
  2105. continue;
  2106. if (!index) {
  2107. WRITE_ONCE(item->prog, prog);
  2108. return 0;
  2109. }
  2110. index--;
  2111. }
  2112. return -ENOENT;
  2113. }
  2114. int bpf_prog_array_copy(struct bpf_prog_array *old_array,
  2115. struct bpf_prog *exclude_prog,
  2116. struct bpf_prog *include_prog,
  2117. u64 bpf_cookie,
  2118. struct bpf_prog_array **new_array)
  2119. {
  2120. int new_prog_cnt, carry_prog_cnt = 0;
  2121. struct bpf_prog_array_item *existing, *new;
  2122. struct bpf_prog_array *array;
  2123. bool found_exclude = false;
  2124. /* Figure out how many existing progs we need to carry over to
  2125. * the new array.
  2126. */
  2127. if (old_array) {
  2128. existing = old_array->items;
  2129. for (; existing->prog; existing++) {
  2130. if (existing->prog == exclude_prog) {
  2131. found_exclude = true;
  2132. continue;
  2133. }
  2134. if (existing->prog != &dummy_bpf_prog.prog)
  2135. carry_prog_cnt++;
  2136. if (existing->prog == include_prog)
  2137. return -EEXIST;
  2138. }
  2139. }
  2140. if (exclude_prog && !found_exclude)
  2141. return -ENOENT;
  2142. /* How many progs (not NULL) will be in the new array? */
  2143. new_prog_cnt = carry_prog_cnt;
  2144. if (include_prog)
  2145. new_prog_cnt += 1;
  2146. /* Do we have any prog (not NULL) in the new array? */
  2147. if (!new_prog_cnt) {
  2148. *new_array = NULL;
  2149. return 0;
  2150. }
  2151. /* +1 as the end of prog_array is marked with NULL */
  2152. array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL);
  2153. if (!array)
  2154. return -ENOMEM;
  2155. new = array->items;
  2156. /* Fill in the new prog array */
  2157. if (carry_prog_cnt) {
  2158. existing = old_array->items;
  2159. for (; existing->prog; existing++) {
  2160. if (existing->prog == exclude_prog ||
  2161. existing->prog == &dummy_bpf_prog.prog)
  2162. continue;
  2163. new->prog = existing->prog;
  2164. new->bpf_cookie = existing->bpf_cookie;
  2165. new++;
  2166. }
  2167. }
  2168. if (include_prog) {
  2169. new->prog = include_prog;
  2170. new->bpf_cookie = bpf_cookie;
  2171. new++;
  2172. }
  2173. new->prog = NULL;
  2174. *new_array = array;
  2175. return 0;
  2176. }
  2177. int bpf_prog_array_copy_info(struct bpf_prog_array *array,
  2178. u32 *prog_ids, u32 request_cnt,
  2179. u32 *prog_cnt)
  2180. {
  2181. u32 cnt = 0;
  2182. if (array)
  2183. cnt = bpf_prog_array_length(array);
  2184. *prog_cnt = cnt;
  2185. /* return early if user requested only program count or nothing to copy */
  2186. if (!request_cnt || !cnt)
  2187. return 0;
  2188. /* this function is called under trace/bpf_trace.c: bpf_event_mutex */
  2189. return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC
  2190. : 0;
  2191. }
  2192. void __bpf_free_used_maps(struct bpf_prog_aux *aux,
  2193. struct bpf_map **used_maps, u32 len)
  2194. {
  2195. struct bpf_map *map;
  2196. u32 i;
  2197. for (i = 0; i < len; i++) {
  2198. map = used_maps[i];
  2199. if (map->ops->map_poke_untrack)
  2200. map->ops->map_poke_untrack(map, aux);
  2201. bpf_map_put(map);
  2202. }
  2203. }
  2204. static void bpf_free_used_maps(struct bpf_prog_aux *aux)
  2205. {
  2206. __bpf_free_used_maps(aux, aux->used_maps, aux->used_map_cnt);
  2207. kfree(aux->used_maps);
  2208. }
  2209. void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
  2210. struct btf_mod_pair *used_btfs, u32 len)
  2211. {
  2212. #ifdef CONFIG_BPF_SYSCALL
  2213. struct btf_mod_pair *btf_mod;
  2214. u32 i;
  2215. for (i = 0; i < len; i++) {
  2216. btf_mod = &used_btfs[i];
  2217. if (btf_mod->module)
  2218. module_put(btf_mod->module);
  2219. btf_put(btf_mod->btf);
  2220. }
  2221. #endif
  2222. }
  2223. static void bpf_free_used_btfs(struct bpf_prog_aux *aux)
  2224. {
  2225. __bpf_free_used_btfs(aux, aux->used_btfs, aux->used_btf_cnt);
  2226. kfree(aux->used_btfs);
  2227. }
  2228. static void bpf_prog_free_deferred(struct work_struct *work)
  2229. {
  2230. struct bpf_prog_aux *aux;
  2231. int i;
  2232. aux = container_of(work, struct bpf_prog_aux, work);
  2233. #ifdef CONFIG_BPF_SYSCALL
  2234. bpf_free_kfunc_btf_tab(aux->kfunc_btf_tab);
  2235. #endif
  2236. #ifdef CONFIG_CGROUP_BPF
  2237. if (aux->cgroup_atype != CGROUP_BPF_ATTACH_TYPE_INVALID)
  2238. bpf_cgroup_atype_put(aux->cgroup_atype);
  2239. #endif
  2240. bpf_free_used_maps(aux);
  2241. bpf_free_used_btfs(aux);
  2242. if (bpf_prog_is_dev_bound(aux))
  2243. bpf_prog_offload_destroy(aux->prog);
  2244. #ifdef CONFIG_PERF_EVENTS
  2245. if (aux->prog->has_callchain_buf)
  2246. put_callchain_buffers();
  2247. #endif
  2248. if (aux->dst_trampoline)
  2249. bpf_trampoline_put(aux->dst_trampoline);
  2250. for (i = 0; i < aux->func_cnt; i++) {
  2251. /* We can just unlink the subprog poke descriptor table as
  2252. * it was originally linked to the main program and is also
  2253. * released along with it.
  2254. */
  2255. aux->func[i]->aux->poke_tab = NULL;
  2256. bpf_jit_free(aux->func[i]);
  2257. }
  2258. if (aux->func_cnt) {
  2259. kfree(aux->func);
  2260. bpf_prog_unlock_free(aux->prog);
  2261. } else {
  2262. bpf_jit_free(aux->prog);
  2263. }
  2264. }
  2265. void bpf_prog_free(struct bpf_prog *fp)
  2266. {
  2267. struct bpf_prog_aux *aux = fp->aux;
  2268. if (aux->dst_prog)
  2269. bpf_prog_put(aux->dst_prog);
  2270. INIT_WORK(&aux->work, bpf_prog_free_deferred);
  2271. schedule_work(&aux->work);
  2272. }
  2273. EXPORT_SYMBOL_GPL(bpf_prog_free);
  2274. /* RNG for unpriviledged user space with separated state from prandom_u32(). */
  2275. static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state);
  2276. void bpf_user_rnd_init_once(void)
  2277. {
  2278. prandom_init_once(&bpf_user_rnd_state);
  2279. }
  2280. BPF_CALL_0(bpf_user_rnd_u32)
  2281. {
  2282. /* Should someone ever have the rather unwise idea to use some
  2283. * of the registers passed into this function, then note that
  2284. * this function is called from native eBPF and classic-to-eBPF
  2285. * transformations. Register assignments from both sides are
  2286. * different, f.e. classic always sets fn(ctx, A, X) here.
  2287. */
  2288. struct rnd_state *state;
  2289. u32 res;
  2290. state = &get_cpu_var(bpf_user_rnd_state);
  2291. res = prandom_u32_state(state);
  2292. put_cpu_var(bpf_user_rnd_state);
  2293. return res;
  2294. }
  2295. BPF_CALL_0(bpf_get_raw_cpu_id)
  2296. {
  2297. return raw_smp_processor_id();
  2298. }
  2299. /* Weak definitions of helper functions in case we don't have bpf syscall. */
  2300. const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
  2301. const struct bpf_func_proto bpf_map_update_elem_proto __weak;
  2302. const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
  2303. const struct bpf_func_proto bpf_map_push_elem_proto __weak;
  2304. const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
  2305. const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
  2306. const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto __weak;
  2307. const struct bpf_func_proto bpf_spin_lock_proto __weak;
  2308. const struct bpf_func_proto bpf_spin_unlock_proto __weak;
  2309. const struct bpf_func_proto bpf_jiffies64_proto __weak;
  2310. const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
  2311. const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
  2312. const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
  2313. const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
  2314. const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak;
  2315. const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto __weak;
  2316. const struct bpf_func_proto bpf_ktime_get_tai_ns_proto __weak;
  2317. const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
  2318. const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
  2319. const struct bpf_func_proto bpf_get_current_comm_proto __weak;
  2320. const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
  2321. const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak;
  2322. const struct bpf_func_proto bpf_get_local_storage_proto __weak;
  2323. const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak;
  2324. const struct bpf_func_proto bpf_snprintf_btf_proto __weak;
  2325. const struct bpf_func_proto bpf_seq_printf_btf_proto __weak;
  2326. const struct bpf_func_proto bpf_set_retval_proto __weak;
  2327. const struct bpf_func_proto bpf_get_retval_proto __weak;
  2328. const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
  2329. {
  2330. return NULL;
  2331. }
  2332. const struct bpf_func_proto * __weak bpf_get_trace_vprintk_proto(void)
  2333. {
  2334. return NULL;
  2335. }
  2336. u64 __weak
  2337. bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  2338. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
  2339. {
  2340. return -ENOTSUPP;
  2341. }
  2342. EXPORT_SYMBOL_GPL(bpf_event_output);
  2343. /* Always built-in helper functions. */
  2344. const struct bpf_func_proto bpf_tail_call_proto = {
  2345. .func = NULL,
  2346. .gpl_only = false,
  2347. .ret_type = RET_VOID,
  2348. .arg1_type = ARG_PTR_TO_CTX,
  2349. .arg2_type = ARG_CONST_MAP_PTR,
  2350. .arg3_type = ARG_ANYTHING,
  2351. };
  2352. /* Stub for JITs that only support cBPF. eBPF programs are interpreted.
  2353. * It is encouraged to implement bpf_int_jit_compile() instead, so that
  2354. * eBPF and implicitly also cBPF can get JITed!
  2355. */
  2356. struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
  2357. {
  2358. return prog;
  2359. }
  2360. /* Stub for JITs that support eBPF. All cBPF code gets transformed into
  2361. * eBPF by the kernel and is later compiled by bpf_int_jit_compile().
  2362. */
  2363. void __weak bpf_jit_compile(struct bpf_prog *prog)
  2364. {
  2365. }
  2366. bool __weak bpf_helper_changes_pkt_data(void *func)
  2367. {
  2368. return false;
  2369. }
  2370. /* Return TRUE if the JIT backend wants verifier to enable sub-register usage
  2371. * analysis code and wants explicit zero extension inserted by verifier.
  2372. * Otherwise, return FALSE.
  2373. *
  2374. * The verifier inserts an explicit zero extension after BPF_CMPXCHGs even if
  2375. * you don't override this. JITs that don't want these extra insns can detect
  2376. * them using insn_is_zext.
  2377. */
  2378. bool __weak bpf_jit_needs_zext(void)
  2379. {
  2380. return false;
  2381. }
  2382. /* Return TRUE if the JIT backend supports mixing bpf2bpf and tailcalls. */
  2383. bool __weak bpf_jit_supports_subprog_tailcalls(void)
  2384. {
  2385. return false;
  2386. }
  2387. bool __weak bpf_jit_supports_kfunc_call(void)
  2388. {
  2389. return false;
  2390. }
  2391. /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  2392. * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  2393. */
  2394. int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
  2395. int len)
  2396. {
  2397. return -EFAULT;
  2398. }
  2399. int __weak bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
  2400. void *addr1, void *addr2)
  2401. {
  2402. return -ENOTSUPP;
  2403. }
  2404. void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len)
  2405. {
  2406. return ERR_PTR(-ENOTSUPP);
  2407. }
  2408. int __weak bpf_arch_text_invalidate(void *dst, size_t len)
  2409. {
  2410. return -ENOTSUPP;
  2411. }
  2412. DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
  2413. EXPORT_SYMBOL(bpf_stats_enabled_key);
  2414. /* All definitions of tracepoints related to BPF. */
  2415. #define CREATE_TRACE_POINTS
  2416. #include <linux/bpf_trace.h>
  2417. EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception);
  2418. EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx);