core.c 80 KB

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