verifier.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/bpf.h>
  17. #include <linux/bpf_verifier.h>
  18. #include <linux/filter.h>
  19. #include <net/netlink.h>
  20. #include <linux/file.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/stringify.h>
  23. /* bpf_check() is a static code analyzer that walks eBPF program
  24. * instruction by instruction and updates register/stack state.
  25. * All paths of conditional branches are analyzed until 'bpf_exit' insn.
  26. *
  27. * The first pass is depth-first-search to check that the program is a DAG.
  28. * It rejects the following programs:
  29. * - larger than BPF_MAXINSNS insns
  30. * - if loop is present (detected via back-edge)
  31. * - unreachable insns exist (shouldn't be a forest. program = one function)
  32. * - out of bounds or malformed jumps
  33. * The second pass is all possible path descent from the 1st insn.
  34. * Since it's analyzing all pathes through the program, the length of the
  35. * analysis is limited to 64k insn, which may be hit even if total number of
  36. * insn is less then 4K, but there are too many branches that change stack/regs.
  37. * Number of 'branches to be analyzed' is limited to 1k
  38. *
  39. * On entry to each instruction, each register has a type, and the instruction
  40. * changes the types of the registers depending on instruction semantics.
  41. * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
  42. * copied to R1.
  43. *
  44. * All registers are 64-bit.
  45. * R0 - return register
  46. * R1-R5 argument passing registers
  47. * R6-R9 callee saved registers
  48. * R10 - frame pointer read-only
  49. *
  50. * At the start of BPF program the register R1 contains a pointer to bpf_context
  51. * and has type PTR_TO_CTX.
  52. *
  53. * Verifier tracks arithmetic operations on pointers in case:
  54. * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  55. * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
  56. * 1st insn copies R10 (which has FRAME_PTR) type into R1
  57. * and 2nd arithmetic instruction is pattern matched to recognize
  58. * that it wants to construct a pointer to some element within stack.
  59. * So after 2nd insn, the register R1 has type PTR_TO_STACK
  60. * (and -20 constant is saved for further stack bounds checking).
  61. * Meaning that this reg is a pointer to stack plus known immediate constant.
  62. *
  63. * Most of the time the registers have SCALAR_VALUE type, which
  64. * means the register has some value, but it's not a valid pointer.
  65. * (like pointer plus pointer becomes SCALAR_VALUE type)
  66. *
  67. * When verifier sees load or store instructions the type of base register
  68. * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
  69. * types recognized by check_mem_access() function.
  70. *
  71. * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
  72. * and the range of [ptr, ptr + map's value_size) is accessible.
  73. *
  74. * registers used to pass values to function calls are checked against
  75. * function argument constraints.
  76. *
  77. * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
  78. * It means that the register type passed to this function must be
  79. * PTR_TO_STACK and it will be used inside the function as
  80. * 'pointer to map element key'
  81. *
  82. * For example the argument constraints for bpf_map_lookup_elem():
  83. * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  84. * .arg1_type = ARG_CONST_MAP_PTR,
  85. * .arg2_type = ARG_PTR_TO_MAP_KEY,
  86. *
  87. * ret_type says that this function returns 'pointer to map elem value or null'
  88. * function expects 1st argument to be a const pointer to 'struct bpf_map' and
  89. * 2nd argument should be a pointer to stack, which will be used inside
  90. * the helper function as a pointer to map element key.
  91. *
  92. * On the kernel side the helper function looks like:
  93. * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  94. * {
  95. * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  96. * void *key = (void *) (unsigned long) r2;
  97. * void *value;
  98. *
  99. * here kernel can access 'key' and 'map' pointers safely, knowing that
  100. * [key, key + map->key_size) bytes are valid and were initialized on
  101. * the stack of eBPF program.
  102. * }
  103. *
  104. * Corresponding eBPF program may look like:
  105. * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
  106. * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
  107. * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
  108. * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  109. * here verifier looks at prototype of map_lookup_elem() and sees:
  110. * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
  111. * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
  112. *
  113. * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
  114. * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
  115. * and were initialized prior to this call.
  116. * If it's ok, then verifier allows this BPF_CALL insn and looks at
  117. * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
  118. * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
  119. * returns ether pointer to map value or NULL.
  120. *
  121. * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
  122. * insn, the register holding that pointer in the true branch changes state to
  123. * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
  124. * branch. See check_cond_jmp_op().
  125. *
  126. * After the call R0 is set to return type of the function and registers R1-R5
  127. * are set to NOT_INIT to indicate that they are no longer readable.
  128. */
  129. /* verifier_state + insn_idx are pushed to stack when branch is encountered */
  130. struct bpf_verifier_stack_elem {
  131. /* verifer state is 'st'
  132. * before processing instruction 'insn_idx'
  133. * and after processing instruction 'prev_insn_idx'
  134. */
  135. struct bpf_verifier_state st;
  136. int insn_idx;
  137. int prev_insn_idx;
  138. struct bpf_verifier_stack_elem *next;
  139. };
  140. #define BPF_COMPLEXITY_LIMIT_INSNS 131072
  141. #define BPF_COMPLEXITY_LIMIT_STACK 1024
  142. #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
  143. struct bpf_call_arg_meta {
  144. struct bpf_map *map_ptr;
  145. bool raw_mode;
  146. bool pkt_access;
  147. int regno;
  148. int access_size;
  149. };
  150. /* verbose verifier prints what it's seeing
  151. * bpf_check() is called under lock, so no race to access these global vars
  152. */
  153. static u32 log_level, log_size, log_len;
  154. static char *log_buf;
  155. static DEFINE_MUTEX(bpf_verifier_lock);
  156. /* log_level controls verbosity level of eBPF verifier.
  157. * verbose() is used to dump the verification trace to the log, so the user
  158. * can figure out what's wrong with the program
  159. */
  160. static __printf(1, 2) void verbose(const char *fmt, ...)
  161. {
  162. va_list args;
  163. if (log_level == 0 || log_len >= log_size - 1)
  164. return;
  165. va_start(args, fmt);
  166. log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
  167. va_end(args);
  168. }
  169. /* string representation of 'enum bpf_reg_type' */
  170. static const char * const reg_type_str[] = {
  171. [NOT_INIT] = "?",
  172. [SCALAR_VALUE] = "inv",
  173. [PTR_TO_CTX] = "ctx",
  174. [CONST_PTR_TO_MAP] = "map_ptr",
  175. [PTR_TO_MAP_VALUE] = "map_value",
  176. [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
  177. [PTR_TO_STACK] = "fp",
  178. [PTR_TO_PACKET] = "pkt",
  179. [PTR_TO_PACKET_END] = "pkt_end",
  180. };
  181. #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
  182. static const char * const func_id_str[] = {
  183. __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
  184. };
  185. #undef __BPF_FUNC_STR_FN
  186. static const char *func_id_name(int id)
  187. {
  188. BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
  189. if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
  190. return func_id_str[id];
  191. else
  192. return "unknown";
  193. }
  194. static void print_verifier_state(struct bpf_verifier_state *state)
  195. {
  196. struct bpf_reg_state *reg;
  197. enum bpf_reg_type t;
  198. int i;
  199. for (i = 0; i < MAX_BPF_REG; i++) {
  200. reg = &state->regs[i];
  201. t = reg->type;
  202. if (t == NOT_INIT)
  203. continue;
  204. verbose(" R%d=%s", i, reg_type_str[t]);
  205. if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
  206. tnum_is_const(reg->var_off)) {
  207. /* reg->off should be 0 for SCALAR_VALUE */
  208. verbose("%lld", reg->var_off.value + reg->off);
  209. } else {
  210. verbose("(id=%d", reg->id);
  211. if (t != SCALAR_VALUE)
  212. verbose(",off=%d", reg->off);
  213. if (t == PTR_TO_PACKET)
  214. verbose(",r=%d", reg->range);
  215. else if (t == CONST_PTR_TO_MAP ||
  216. t == PTR_TO_MAP_VALUE ||
  217. t == PTR_TO_MAP_VALUE_OR_NULL)
  218. verbose(",ks=%d,vs=%d",
  219. reg->map_ptr->key_size,
  220. reg->map_ptr->value_size);
  221. if (tnum_is_const(reg->var_off)) {
  222. /* Typically an immediate SCALAR_VALUE, but
  223. * could be a pointer whose offset is too big
  224. * for reg->off
  225. */
  226. verbose(",imm=%llx", reg->var_off.value);
  227. } else {
  228. if (reg->smin_value != reg->umin_value &&
  229. reg->smin_value != S64_MIN)
  230. verbose(",smin_value=%lld",
  231. (long long)reg->smin_value);
  232. if (reg->smax_value != reg->umax_value &&
  233. reg->smax_value != S64_MAX)
  234. verbose(",smax_value=%lld",
  235. (long long)reg->smax_value);
  236. if (reg->umin_value != 0)
  237. verbose(",umin_value=%llu",
  238. (unsigned long long)reg->umin_value);
  239. if (reg->umax_value != U64_MAX)
  240. verbose(",umax_value=%llu",
  241. (unsigned long long)reg->umax_value);
  242. if (!tnum_is_unknown(reg->var_off)) {
  243. char tn_buf[48];
  244. tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
  245. verbose(",var_off=%s", tn_buf);
  246. }
  247. }
  248. verbose(")");
  249. }
  250. }
  251. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  252. if (state->stack_slot_type[i] == STACK_SPILL)
  253. verbose(" fp%d=%s", -MAX_BPF_STACK + i,
  254. reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
  255. }
  256. verbose("\n");
  257. }
  258. static const char *const bpf_class_string[] = {
  259. [BPF_LD] = "ld",
  260. [BPF_LDX] = "ldx",
  261. [BPF_ST] = "st",
  262. [BPF_STX] = "stx",
  263. [BPF_ALU] = "alu",
  264. [BPF_JMP] = "jmp",
  265. [BPF_RET] = "BUG",
  266. [BPF_ALU64] = "alu64",
  267. };
  268. static const char *const bpf_alu_string[16] = {
  269. [BPF_ADD >> 4] = "+=",
  270. [BPF_SUB >> 4] = "-=",
  271. [BPF_MUL >> 4] = "*=",
  272. [BPF_DIV >> 4] = "/=",
  273. [BPF_OR >> 4] = "|=",
  274. [BPF_AND >> 4] = "&=",
  275. [BPF_LSH >> 4] = "<<=",
  276. [BPF_RSH >> 4] = ">>=",
  277. [BPF_NEG >> 4] = "neg",
  278. [BPF_MOD >> 4] = "%=",
  279. [BPF_XOR >> 4] = "^=",
  280. [BPF_MOV >> 4] = "=",
  281. [BPF_ARSH >> 4] = "s>>=",
  282. [BPF_END >> 4] = "endian",
  283. };
  284. static const char *const bpf_ldst_string[] = {
  285. [BPF_W >> 3] = "u32",
  286. [BPF_H >> 3] = "u16",
  287. [BPF_B >> 3] = "u8",
  288. [BPF_DW >> 3] = "u64",
  289. };
  290. static const char *const bpf_jmp_string[16] = {
  291. [BPF_JA >> 4] = "jmp",
  292. [BPF_JEQ >> 4] = "==",
  293. [BPF_JGT >> 4] = ">",
  294. [BPF_JLT >> 4] = "<",
  295. [BPF_JGE >> 4] = ">=",
  296. [BPF_JLE >> 4] = "<=",
  297. [BPF_JSET >> 4] = "&",
  298. [BPF_JNE >> 4] = "!=",
  299. [BPF_JSGT >> 4] = "s>",
  300. [BPF_JSLT >> 4] = "s<",
  301. [BPF_JSGE >> 4] = "s>=",
  302. [BPF_JSLE >> 4] = "s<=",
  303. [BPF_CALL >> 4] = "call",
  304. [BPF_EXIT >> 4] = "exit",
  305. };
  306. static void print_bpf_insn(const struct bpf_verifier_env *env,
  307. const struct bpf_insn *insn)
  308. {
  309. u8 class = BPF_CLASS(insn->code);
  310. if (class == BPF_ALU || class == BPF_ALU64) {
  311. if (BPF_SRC(insn->code) == BPF_X)
  312. verbose("(%02x) %sr%d %s %sr%d\n",
  313. insn->code, class == BPF_ALU ? "(u32) " : "",
  314. insn->dst_reg,
  315. bpf_alu_string[BPF_OP(insn->code) >> 4],
  316. class == BPF_ALU ? "(u32) " : "",
  317. insn->src_reg);
  318. else
  319. verbose("(%02x) %sr%d %s %s%d\n",
  320. insn->code, class == BPF_ALU ? "(u32) " : "",
  321. insn->dst_reg,
  322. bpf_alu_string[BPF_OP(insn->code) >> 4],
  323. class == BPF_ALU ? "(u32) " : "",
  324. insn->imm);
  325. } else if (class == BPF_STX) {
  326. if (BPF_MODE(insn->code) == BPF_MEM)
  327. verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
  328. insn->code,
  329. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  330. insn->dst_reg,
  331. insn->off, insn->src_reg);
  332. else if (BPF_MODE(insn->code) == BPF_XADD)
  333. verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
  334. insn->code,
  335. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  336. insn->dst_reg, insn->off,
  337. insn->src_reg);
  338. else
  339. verbose("BUG_%02x\n", insn->code);
  340. } else if (class == BPF_ST) {
  341. if (BPF_MODE(insn->code) != BPF_MEM) {
  342. verbose("BUG_st_%02x\n", insn->code);
  343. return;
  344. }
  345. verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
  346. insn->code,
  347. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  348. insn->dst_reg,
  349. insn->off, insn->imm);
  350. } else if (class == BPF_LDX) {
  351. if (BPF_MODE(insn->code) != BPF_MEM) {
  352. verbose("BUG_ldx_%02x\n", insn->code);
  353. return;
  354. }
  355. verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
  356. insn->code, insn->dst_reg,
  357. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  358. insn->src_reg, insn->off);
  359. } else if (class == BPF_LD) {
  360. if (BPF_MODE(insn->code) == BPF_ABS) {
  361. verbose("(%02x) r0 = *(%s *)skb[%d]\n",
  362. insn->code,
  363. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  364. insn->imm);
  365. } else if (BPF_MODE(insn->code) == BPF_IND) {
  366. verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
  367. insn->code,
  368. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  369. insn->src_reg, insn->imm);
  370. } else if (BPF_MODE(insn->code) == BPF_IMM &&
  371. BPF_SIZE(insn->code) == BPF_DW) {
  372. /* At this point, we already made sure that the second
  373. * part of the ldimm64 insn is accessible.
  374. */
  375. u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
  376. bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
  377. if (map_ptr && !env->allow_ptr_leaks)
  378. imm = 0;
  379. verbose("(%02x) r%d = 0x%llx\n", insn->code,
  380. insn->dst_reg, (unsigned long long)imm);
  381. } else {
  382. verbose("BUG_ld_%02x\n", insn->code);
  383. return;
  384. }
  385. } else if (class == BPF_JMP) {
  386. u8 opcode = BPF_OP(insn->code);
  387. if (opcode == BPF_CALL) {
  388. verbose("(%02x) call %s#%d\n", insn->code,
  389. func_id_name(insn->imm), insn->imm);
  390. } else if (insn->code == (BPF_JMP | BPF_JA)) {
  391. verbose("(%02x) goto pc%+d\n",
  392. insn->code, insn->off);
  393. } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
  394. verbose("(%02x) exit\n", insn->code);
  395. } else if (BPF_SRC(insn->code) == BPF_X) {
  396. verbose("(%02x) if r%d %s r%d goto pc%+d\n",
  397. insn->code, insn->dst_reg,
  398. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  399. insn->src_reg, insn->off);
  400. } else {
  401. verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
  402. insn->code, insn->dst_reg,
  403. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  404. insn->imm, insn->off);
  405. }
  406. } else {
  407. verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
  408. }
  409. }
  410. static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
  411. {
  412. struct bpf_verifier_stack_elem *elem;
  413. int insn_idx;
  414. if (env->head == NULL)
  415. return -1;
  416. memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
  417. insn_idx = env->head->insn_idx;
  418. if (prev_insn_idx)
  419. *prev_insn_idx = env->head->prev_insn_idx;
  420. elem = env->head->next;
  421. kfree(env->head);
  422. env->head = elem;
  423. env->stack_size--;
  424. return insn_idx;
  425. }
  426. static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
  427. int insn_idx, int prev_insn_idx)
  428. {
  429. struct bpf_verifier_stack_elem *elem;
  430. elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
  431. if (!elem)
  432. goto err;
  433. memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
  434. elem->insn_idx = insn_idx;
  435. elem->prev_insn_idx = prev_insn_idx;
  436. elem->next = env->head;
  437. env->head = elem;
  438. env->stack_size++;
  439. if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
  440. verbose("BPF program is too complex\n");
  441. goto err;
  442. }
  443. return &elem->st;
  444. err:
  445. /* pop all elements and return */
  446. while (pop_stack(env, NULL) >= 0);
  447. return NULL;
  448. }
  449. #define CALLER_SAVED_REGS 6
  450. static const int caller_saved[CALLER_SAVED_REGS] = {
  451. BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
  452. };
  453. static void __mark_reg_not_init(struct bpf_reg_state *reg);
  454. /* Mark the unknown part of a register (variable offset or scalar value) as
  455. * known to have the value @imm.
  456. */
  457. static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
  458. {
  459. reg->id = 0;
  460. reg->var_off = tnum_const(imm);
  461. reg->smin_value = (s64)imm;
  462. reg->smax_value = (s64)imm;
  463. reg->umin_value = imm;
  464. reg->umax_value = imm;
  465. }
  466. /* Mark the 'variable offset' part of a register as zero. This should be
  467. * used only on registers holding a pointer type.
  468. */
  469. static void __mark_reg_known_zero(struct bpf_reg_state *reg)
  470. {
  471. __mark_reg_known(reg, 0);
  472. }
  473. static void mark_reg_known_zero(struct bpf_reg_state *regs, u32 regno)
  474. {
  475. if (WARN_ON(regno >= MAX_BPF_REG)) {
  476. verbose("mark_reg_known_zero(regs, %u)\n", regno);
  477. /* Something bad happened, let's kill all regs */
  478. for (regno = 0; regno < MAX_BPF_REG; regno++)
  479. __mark_reg_not_init(regs + regno);
  480. return;
  481. }
  482. __mark_reg_known_zero(regs + regno);
  483. }
  484. /* Attempts to improve min/max values based on var_off information */
  485. static void __update_reg_bounds(struct bpf_reg_state *reg)
  486. {
  487. /* min signed is max(sign bit) | min(other bits) */
  488. reg->smin_value = max_t(s64, reg->smin_value,
  489. reg->var_off.value | (reg->var_off.mask & S64_MIN));
  490. /* max signed is min(sign bit) | max(other bits) */
  491. reg->smax_value = min_t(s64, reg->smax_value,
  492. reg->var_off.value | (reg->var_off.mask & S64_MAX));
  493. reg->umin_value = max(reg->umin_value, reg->var_off.value);
  494. reg->umax_value = min(reg->umax_value,
  495. reg->var_off.value | reg->var_off.mask);
  496. }
  497. /* Uses signed min/max values to inform unsigned, and vice-versa */
  498. static void __reg_deduce_bounds(struct bpf_reg_state *reg)
  499. {
  500. /* Learn sign from signed bounds.
  501. * If we cannot cross the sign boundary, then signed and unsigned bounds
  502. * are the same, so combine. This works even in the negative case, e.g.
  503. * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
  504. */
  505. if (reg->smin_value >= 0 || reg->smax_value < 0) {
  506. reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
  507. reg->umin_value);
  508. reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
  509. reg->umax_value);
  510. return;
  511. }
  512. /* Learn sign from unsigned bounds. Signed bounds cross the sign
  513. * boundary, so we must be careful.
  514. */
  515. if ((s64)reg->umax_value >= 0) {
  516. /* Positive. We can't learn anything from the smin, but smax
  517. * is positive, hence safe.
  518. */
  519. reg->smin_value = reg->umin_value;
  520. reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
  521. reg->umax_value);
  522. } else if ((s64)reg->umin_value < 0) {
  523. /* Negative. We can't learn anything from the smax, but smin
  524. * is negative, hence safe.
  525. */
  526. reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
  527. reg->umin_value);
  528. reg->smax_value = reg->umax_value;
  529. }
  530. }
  531. /* Attempts to improve var_off based on unsigned min/max information */
  532. static void __reg_bound_offset(struct bpf_reg_state *reg)
  533. {
  534. reg->var_off = tnum_intersect(reg->var_off,
  535. tnum_range(reg->umin_value,
  536. reg->umax_value));
  537. }
  538. /* Reset the min/max bounds of a register */
  539. static void __mark_reg_unbounded(struct bpf_reg_state *reg)
  540. {
  541. reg->smin_value = S64_MIN;
  542. reg->smax_value = S64_MAX;
  543. reg->umin_value = 0;
  544. reg->umax_value = U64_MAX;
  545. }
  546. /* Mark a register as having a completely unknown (scalar) value. */
  547. static void __mark_reg_unknown(struct bpf_reg_state *reg)
  548. {
  549. reg->type = SCALAR_VALUE;
  550. reg->id = 0;
  551. reg->off = 0;
  552. reg->var_off = tnum_unknown;
  553. __mark_reg_unbounded(reg);
  554. }
  555. static void mark_reg_unknown(struct bpf_reg_state *regs, u32 regno)
  556. {
  557. if (WARN_ON(regno >= MAX_BPF_REG)) {
  558. verbose("mark_reg_unknown(regs, %u)\n", regno);
  559. /* Something bad happened, let's kill all regs */
  560. for (regno = 0; regno < MAX_BPF_REG; regno++)
  561. __mark_reg_not_init(regs + regno);
  562. return;
  563. }
  564. __mark_reg_unknown(regs + regno);
  565. }
  566. static void __mark_reg_not_init(struct bpf_reg_state *reg)
  567. {
  568. __mark_reg_unknown(reg);
  569. reg->type = NOT_INIT;
  570. }
  571. static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno)
  572. {
  573. if (WARN_ON(regno >= MAX_BPF_REG)) {
  574. verbose("mark_reg_not_init(regs, %u)\n", regno);
  575. /* Something bad happened, let's kill all regs */
  576. for (regno = 0; regno < MAX_BPF_REG; regno++)
  577. __mark_reg_not_init(regs + regno);
  578. return;
  579. }
  580. __mark_reg_not_init(regs + regno);
  581. }
  582. static void init_reg_state(struct bpf_reg_state *regs)
  583. {
  584. int i;
  585. for (i = 0; i < MAX_BPF_REG; i++) {
  586. mark_reg_not_init(regs, i);
  587. regs[i].live = REG_LIVE_NONE;
  588. }
  589. /* frame pointer */
  590. regs[BPF_REG_FP].type = PTR_TO_STACK;
  591. mark_reg_known_zero(regs, BPF_REG_FP);
  592. /* 1st arg to a function */
  593. regs[BPF_REG_1].type = PTR_TO_CTX;
  594. mark_reg_known_zero(regs, BPF_REG_1);
  595. }
  596. enum reg_arg_type {
  597. SRC_OP, /* register is used as source operand */
  598. DST_OP, /* register is used as destination operand */
  599. DST_OP_NO_MARK /* same as above, check only, don't mark */
  600. };
  601. static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno)
  602. {
  603. struct bpf_verifier_state *parent = state->parent;
  604. if (regno == BPF_REG_FP)
  605. /* We don't need to worry about FP liveness because it's read-only */
  606. return;
  607. while (parent) {
  608. /* if read wasn't screened by an earlier write ... */
  609. if (state->regs[regno].live & REG_LIVE_WRITTEN)
  610. break;
  611. /* ... then we depend on parent's value */
  612. parent->regs[regno].live |= REG_LIVE_READ;
  613. state = parent;
  614. parent = state->parent;
  615. }
  616. }
  617. static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
  618. enum reg_arg_type t)
  619. {
  620. struct bpf_reg_state *regs = env->cur_state.regs;
  621. if (regno >= MAX_BPF_REG) {
  622. verbose("R%d is invalid\n", regno);
  623. return -EINVAL;
  624. }
  625. if (t == SRC_OP) {
  626. /* check whether register used as source operand can be read */
  627. if (regs[regno].type == NOT_INIT) {
  628. verbose("R%d !read_ok\n", regno);
  629. return -EACCES;
  630. }
  631. mark_reg_read(&env->cur_state, regno);
  632. } else {
  633. /* check whether register used as dest operand can be written to */
  634. if (regno == BPF_REG_FP) {
  635. verbose("frame pointer is read only\n");
  636. return -EACCES;
  637. }
  638. regs[regno].live |= REG_LIVE_WRITTEN;
  639. if (t == DST_OP)
  640. mark_reg_unknown(regs, regno);
  641. }
  642. return 0;
  643. }
  644. static bool is_spillable_regtype(enum bpf_reg_type type)
  645. {
  646. switch (type) {
  647. case PTR_TO_MAP_VALUE:
  648. case PTR_TO_MAP_VALUE_OR_NULL:
  649. case PTR_TO_STACK:
  650. case PTR_TO_CTX:
  651. case PTR_TO_PACKET:
  652. case PTR_TO_PACKET_END:
  653. case CONST_PTR_TO_MAP:
  654. return true;
  655. default:
  656. return false;
  657. }
  658. }
  659. /* check_stack_read/write functions track spill/fill of registers,
  660. * stack boundary and alignment are checked in check_mem_access()
  661. */
  662. static int check_stack_write(struct bpf_verifier_state *state, int off,
  663. int size, int value_regno)
  664. {
  665. int i, spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
  666. /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
  667. * so it's aligned access and [off, off + size) are within stack limits
  668. */
  669. if (value_regno >= 0 &&
  670. is_spillable_regtype(state->regs[value_regno].type)) {
  671. /* register containing pointer is being spilled into stack */
  672. if (size != BPF_REG_SIZE) {
  673. verbose("invalid size of register spill\n");
  674. return -EACCES;
  675. }
  676. /* save register state */
  677. state->spilled_regs[spi] = state->regs[value_regno];
  678. state->spilled_regs[spi].live |= REG_LIVE_WRITTEN;
  679. for (i = 0; i < BPF_REG_SIZE; i++)
  680. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
  681. } else {
  682. /* regular write of data into stack */
  683. state->spilled_regs[spi] = (struct bpf_reg_state) {};
  684. for (i = 0; i < size; i++)
  685. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
  686. }
  687. return 0;
  688. }
  689. static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slot)
  690. {
  691. struct bpf_verifier_state *parent = state->parent;
  692. while (parent) {
  693. /* if read wasn't screened by an earlier write ... */
  694. if (state->spilled_regs[slot].live & REG_LIVE_WRITTEN)
  695. break;
  696. /* ... then we depend on parent's value */
  697. parent->spilled_regs[slot].live |= REG_LIVE_READ;
  698. state = parent;
  699. parent = state->parent;
  700. }
  701. }
  702. static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
  703. int value_regno)
  704. {
  705. u8 *slot_type;
  706. int i, spi;
  707. slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
  708. if (slot_type[0] == STACK_SPILL) {
  709. if (size != BPF_REG_SIZE) {
  710. verbose("invalid size of register spill\n");
  711. return -EACCES;
  712. }
  713. for (i = 1; i < BPF_REG_SIZE; i++) {
  714. if (slot_type[i] != STACK_SPILL) {
  715. verbose("corrupted spill memory\n");
  716. return -EACCES;
  717. }
  718. }
  719. spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
  720. if (value_regno >= 0) {
  721. /* restore register state from stack */
  722. state->regs[value_regno] = state->spilled_regs[spi];
  723. mark_stack_slot_read(state, spi);
  724. }
  725. return 0;
  726. } else {
  727. for (i = 0; i < size; i++) {
  728. if (slot_type[i] != STACK_MISC) {
  729. verbose("invalid read from stack off %d+%d size %d\n",
  730. off, i, size);
  731. return -EACCES;
  732. }
  733. }
  734. if (value_regno >= 0)
  735. /* have read misc data from the stack */
  736. mark_reg_unknown(state->regs, value_regno);
  737. return 0;
  738. }
  739. }
  740. /* check read/write into map element returned by bpf_map_lookup_elem() */
  741. static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
  742. int size)
  743. {
  744. struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
  745. if (off < 0 || size <= 0 || off + size > map->value_size) {
  746. verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
  747. map->value_size, off, size);
  748. return -EACCES;
  749. }
  750. return 0;
  751. }
  752. /* check read/write into a map element with possible variable offset */
  753. static int check_map_access(struct bpf_verifier_env *env, u32 regno,
  754. int off, int size)
  755. {
  756. struct bpf_verifier_state *state = &env->cur_state;
  757. struct bpf_reg_state *reg = &state->regs[regno];
  758. int err;
  759. /* We may have adjusted the register to this map value, so we
  760. * need to try adding each of min_value and max_value to off
  761. * to make sure our theoretical access will be safe.
  762. */
  763. if (log_level)
  764. print_verifier_state(state);
  765. /* The minimum value is only important with signed
  766. * comparisons where we can't assume the floor of a
  767. * value is 0. If we are using signed variables for our
  768. * index'es we need to make sure that whatever we use
  769. * will have a set floor within our range.
  770. */
  771. if (reg->smin_value < 0) {
  772. verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
  773. regno);
  774. return -EACCES;
  775. }
  776. err = __check_map_access(env, regno, reg->smin_value + off, size);
  777. if (err) {
  778. verbose("R%d min value is outside of the array range\n", regno);
  779. return err;
  780. }
  781. /* If we haven't set a max value then we need to bail since we can't be
  782. * sure we won't do bad things.
  783. * If reg->umax_value + off could overflow, treat that as unbounded too.
  784. */
  785. if (reg->umax_value >= BPF_MAX_VAR_OFF) {
  786. verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
  787. regno);
  788. return -EACCES;
  789. }
  790. err = __check_map_access(env, regno, reg->umax_value + off, size);
  791. if (err)
  792. verbose("R%d max value is outside of the array range\n", regno);
  793. return err;
  794. }
  795. #define MAX_PACKET_OFF 0xffff
  796. static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
  797. const struct bpf_call_arg_meta *meta,
  798. enum bpf_access_type t)
  799. {
  800. switch (env->prog->type) {
  801. case BPF_PROG_TYPE_LWT_IN:
  802. case BPF_PROG_TYPE_LWT_OUT:
  803. /* dst_input() and dst_output() can't write for now */
  804. if (t == BPF_WRITE)
  805. return false;
  806. /* fallthrough */
  807. case BPF_PROG_TYPE_SCHED_CLS:
  808. case BPF_PROG_TYPE_SCHED_ACT:
  809. case BPF_PROG_TYPE_XDP:
  810. case BPF_PROG_TYPE_LWT_XMIT:
  811. case BPF_PROG_TYPE_SK_SKB:
  812. if (meta)
  813. return meta->pkt_access;
  814. env->seen_direct_write = true;
  815. return true;
  816. default:
  817. return false;
  818. }
  819. }
  820. static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
  821. int off, int size)
  822. {
  823. struct bpf_reg_state *regs = env->cur_state.regs;
  824. struct bpf_reg_state *reg = &regs[regno];
  825. if (off < 0 || size <= 0 || (u64)off + size > reg->range) {
  826. verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
  827. off, size, regno, reg->id, reg->off, reg->range);
  828. return -EACCES;
  829. }
  830. return 0;
  831. }
  832. static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
  833. int size)
  834. {
  835. struct bpf_reg_state *regs = env->cur_state.regs;
  836. struct bpf_reg_state *reg = &regs[regno];
  837. int err;
  838. /* We may have added a variable offset to the packet pointer; but any
  839. * reg->range we have comes after that. We are only checking the fixed
  840. * offset.
  841. */
  842. /* We don't allow negative numbers, because we aren't tracking enough
  843. * detail to prove they're safe.
  844. */
  845. if (reg->smin_value < 0) {
  846. verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
  847. regno);
  848. return -EACCES;
  849. }
  850. err = __check_packet_access(env, regno, off, size);
  851. if (err) {
  852. verbose("R%d offset is outside of the packet\n", regno);
  853. return err;
  854. }
  855. return err;
  856. }
  857. /* check access to 'struct bpf_context' fields. Supports fixed offsets only */
  858. static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
  859. enum bpf_access_type t, enum bpf_reg_type *reg_type)
  860. {
  861. struct bpf_insn_access_aux info = {
  862. .reg_type = *reg_type,
  863. };
  864. /* for analyzer ctx accesses are already validated and converted */
  865. if (env->analyzer_ops)
  866. return 0;
  867. if (env->prog->aux->ops->is_valid_access &&
  868. env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
  869. /* A non zero info.ctx_field_size indicates that this field is a
  870. * candidate for later verifier transformation to load the whole
  871. * field and then apply a mask when accessed with a narrower
  872. * access than actual ctx access size. A zero info.ctx_field_size
  873. * will only allow for whole field access and rejects any other
  874. * type of narrower access.
  875. */
  876. env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
  877. *reg_type = info.reg_type;
  878. /* remember the offset of last byte accessed in ctx */
  879. if (env->prog->aux->max_ctx_offset < off + size)
  880. env->prog->aux->max_ctx_offset = off + size;
  881. return 0;
  882. }
  883. verbose("invalid bpf_context access off=%d size=%d\n", off, size);
  884. return -EACCES;
  885. }
  886. static bool __is_pointer_value(bool allow_ptr_leaks,
  887. const struct bpf_reg_state *reg)
  888. {
  889. if (allow_ptr_leaks)
  890. return false;
  891. return reg->type != SCALAR_VALUE;
  892. }
  893. static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
  894. {
  895. return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]);
  896. }
  897. static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
  898. int off, int size, bool strict)
  899. {
  900. struct tnum reg_off;
  901. int ip_align;
  902. /* Byte size accesses are always allowed. */
  903. if (!strict || size == 1)
  904. return 0;
  905. /* For platforms that do not have a Kconfig enabling
  906. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
  907. * NET_IP_ALIGN is universally set to '2'. And on platforms
  908. * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
  909. * to this code only in strict mode where we want to emulate
  910. * the NET_IP_ALIGN==2 checking. Therefore use an
  911. * unconditional IP align value of '2'.
  912. */
  913. ip_align = 2;
  914. reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
  915. if (!tnum_is_aligned(reg_off, size)) {
  916. char tn_buf[48];
  917. tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
  918. verbose("misaligned packet access off %d+%s+%d+%d size %d\n",
  919. ip_align, tn_buf, reg->off, off, size);
  920. return -EACCES;
  921. }
  922. return 0;
  923. }
  924. static int check_generic_ptr_alignment(const struct bpf_reg_state *reg,
  925. const char *pointer_desc,
  926. int off, int size, bool strict)
  927. {
  928. struct tnum reg_off;
  929. /* Byte size accesses are always allowed. */
  930. if (!strict || size == 1)
  931. return 0;
  932. reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
  933. if (!tnum_is_aligned(reg_off, size)) {
  934. char tn_buf[48];
  935. tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
  936. verbose("misaligned %saccess off %s+%d+%d size %d\n",
  937. pointer_desc, tn_buf, reg->off, off, size);
  938. return -EACCES;
  939. }
  940. return 0;
  941. }
  942. static int check_ptr_alignment(struct bpf_verifier_env *env,
  943. const struct bpf_reg_state *reg,
  944. int off, int size)
  945. {
  946. bool strict = env->strict_alignment;
  947. const char *pointer_desc = "";
  948. switch (reg->type) {
  949. case PTR_TO_PACKET:
  950. /* special case, because of NET_IP_ALIGN */
  951. return check_pkt_ptr_alignment(reg, off, size, strict);
  952. case PTR_TO_MAP_VALUE:
  953. pointer_desc = "value ";
  954. break;
  955. case PTR_TO_CTX:
  956. pointer_desc = "context ";
  957. break;
  958. case PTR_TO_STACK:
  959. pointer_desc = "stack ";
  960. break;
  961. default:
  962. break;
  963. }
  964. return check_generic_ptr_alignment(reg, pointer_desc, off, size, strict);
  965. }
  966. /* check whether memory at (regno + off) is accessible for t = (read | write)
  967. * if t==write, value_regno is a register which value is stored into memory
  968. * if t==read, value_regno is a register which will receive the value from memory
  969. * if t==write && value_regno==-1, some unknown value is stored into memory
  970. * if t==read && value_regno==-1, don't care what we read from memory
  971. */
  972. static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off,
  973. int bpf_size, enum bpf_access_type t,
  974. int value_regno)
  975. {
  976. struct bpf_verifier_state *state = &env->cur_state;
  977. struct bpf_reg_state *reg = &state->regs[regno];
  978. int size, err = 0;
  979. size = bpf_size_to_bytes(bpf_size);
  980. if (size < 0)
  981. return size;
  982. /* alignment checks will add in reg->off themselves */
  983. err = check_ptr_alignment(env, reg, off, size);
  984. if (err)
  985. return err;
  986. /* for access checks, reg->off is just part of off */
  987. off += reg->off;
  988. if (reg->type == PTR_TO_MAP_VALUE) {
  989. if (t == BPF_WRITE && value_regno >= 0 &&
  990. is_pointer_value(env, value_regno)) {
  991. verbose("R%d leaks addr into map\n", value_regno);
  992. return -EACCES;
  993. }
  994. err = check_map_access(env, regno, off, size);
  995. if (!err && t == BPF_READ && value_regno >= 0)
  996. mark_reg_unknown(state->regs, value_regno);
  997. } else if (reg->type == PTR_TO_CTX) {
  998. enum bpf_reg_type reg_type = SCALAR_VALUE;
  999. if (t == BPF_WRITE && value_regno >= 0 &&
  1000. is_pointer_value(env, value_regno)) {
  1001. verbose("R%d leaks addr into ctx\n", value_regno);
  1002. return -EACCES;
  1003. }
  1004. /* ctx accesses must be at a fixed offset, so that we can
  1005. * determine what type of data were returned.
  1006. */
  1007. if (!tnum_is_const(reg->var_off)) {
  1008. char tn_buf[48];
  1009. tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
  1010. verbose("variable ctx access var_off=%s off=%d size=%d",
  1011. tn_buf, off, size);
  1012. return -EACCES;
  1013. }
  1014. off += reg->var_off.value;
  1015. err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
  1016. if (!err && t == BPF_READ && value_regno >= 0) {
  1017. /* ctx access returns either a scalar, or a
  1018. * PTR_TO_PACKET[_END]. In the latter case, we know
  1019. * the offset is zero.
  1020. */
  1021. if (reg_type == SCALAR_VALUE)
  1022. mark_reg_unknown(state->regs, value_regno);
  1023. else
  1024. mark_reg_known_zero(state->regs, value_regno);
  1025. state->regs[value_regno].id = 0;
  1026. state->regs[value_regno].off = 0;
  1027. state->regs[value_regno].range = 0;
  1028. state->regs[value_regno].type = reg_type;
  1029. }
  1030. } else if (reg->type == PTR_TO_STACK) {
  1031. /* stack accesses must be at a fixed offset, so that we can
  1032. * determine what type of data were returned.
  1033. * See check_stack_read().
  1034. */
  1035. if (!tnum_is_const(reg->var_off)) {
  1036. char tn_buf[48];
  1037. tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
  1038. verbose("variable stack access var_off=%s off=%d size=%d",
  1039. tn_buf, off, size);
  1040. return -EACCES;
  1041. }
  1042. off += reg->var_off.value;
  1043. if (off >= 0 || off < -MAX_BPF_STACK) {
  1044. verbose("invalid stack off=%d size=%d\n", off, size);
  1045. return -EACCES;
  1046. }
  1047. if (env->prog->aux->stack_depth < -off)
  1048. env->prog->aux->stack_depth = -off;
  1049. if (t == BPF_WRITE) {
  1050. if (!env->allow_ptr_leaks &&
  1051. state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
  1052. size != BPF_REG_SIZE) {
  1053. verbose("attempt to corrupt spilled pointer on stack\n");
  1054. return -EACCES;
  1055. }
  1056. err = check_stack_write(state, off, size, value_regno);
  1057. } else {
  1058. err = check_stack_read(state, off, size, value_regno);
  1059. }
  1060. } else if (reg->type == PTR_TO_PACKET) {
  1061. if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
  1062. verbose("cannot write into packet\n");
  1063. return -EACCES;
  1064. }
  1065. if (t == BPF_WRITE && value_regno >= 0 &&
  1066. is_pointer_value(env, value_regno)) {
  1067. verbose("R%d leaks addr into packet\n", value_regno);
  1068. return -EACCES;
  1069. }
  1070. err = check_packet_access(env, regno, off, size);
  1071. if (!err && t == BPF_READ && value_regno >= 0)
  1072. mark_reg_unknown(state->regs, value_regno);
  1073. } else {
  1074. verbose("R%d invalid mem access '%s'\n",
  1075. regno, reg_type_str[reg->type]);
  1076. return -EACCES;
  1077. }
  1078. if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
  1079. state->regs[value_regno].type == SCALAR_VALUE) {
  1080. /* b/h/w load zero-extends, mark upper bits as known 0 */
  1081. state->regs[value_regno].var_off = tnum_cast(
  1082. state->regs[value_regno].var_off, size);
  1083. __update_reg_bounds(&state->regs[value_regno]);
  1084. }
  1085. return err;
  1086. }
  1087. static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
  1088. {
  1089. int err;
  1090. if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
  1091. insn->imm != 0) {
  1092. verbose("BPF_XADD uses reserved fields\n");
  1093. return -EINVAL;
  1094. }
  1095. /* check src1 operand */
  1096. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  1097. if (err)
  1098. return err;
  1099. /* check src2 operand */
  1100. err = check_reg_arg(env, insn->dst_reg, SRC_OP);
  1101. if (err)
  1102. return err;
  1103. if (is_pointer_value(env, insn->src_reg)) {
  1104. verbose("R%d leaks addr into mem\n", insn->src_reg);
  1105. return -EACCES;
  1106. }
  1107. /* check whether atomic_add can read the memory */
  1108. err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
  1109. BPF_SIZE(insn->code), BPF_READ, -1);
  1110. if (err)
  1111. return err;
  1112. /* check whether atomic_add can write into the same memory */
  1113. return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
  1114. BPF_SIZE(insn->code), BPF_WRITE, -1);
  1115. }
  1116. /* Does this register contain a constant zero? */
  1117. static bool register_is_null(struct bpf_reg_state reg)
  1118. {
  1119. return reg.type == SCALAR_VALUE && tnum_equals_const(reg.var_off, 0);
  1120. }
  1121. /* when register 'regno' is passed into function that will read 'access_size'
  1122. * bytes from that pointer, make sure that it's within stack boundary
  1123. * and all elements of stack are initialized.
  1124. * Unlike most pointer bounds-checking functions, this one doesn't take an
  1125. * 'off' argument, so it has to add in reg->off itself.
  1126. */
  1127. static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
  1128. int access_size, bool zero_size_allowed,
  1129. struct bpf_call_arg_meta *meta)
  1130. {
  1131. struct bpf_verifier_state *state = &env->cur_state;
  1132. struct bpf_reg_state *regs = state->regs;
  1133. int off, i;
  1134. if (regs[regno].type != PTR_TO_STACK) {
  1135. /* Allow zero-byte read from NULL, regardless of pointer type */
  1136. if (zero_size_allowed && access_size == 0 &&
  1137. register_is_null(regs[regno]))
  1138. return 0;
  1139. verbose("R%d type=%s expected=%s\n", regno,
  1140. reg_type_str[regs[regno].type],
  1141. reg_type_str[PTR_TO_STACK]);
  1142. return -EACCES;
  1143. }
  1144. /* Only allow fixed-offset stack reads */
  1145. if (!tnum_is_const(regs[regno].var_off)) {
  1146. char tn_buf[48];
  1147. tnum_strn(tn_buf, sizeof(tn_buf), regs[regno].var_off);
  1148. verbose("invalid variable stack read R%d var_off=%s\n",
  1149. regno, tn_buf);
  1150. }
  1151. off = regs[regno].off + regs[regno].var_off.value;
  1152. if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
  1153. access_size <= 0) {
  1154. verbose("invalid stack type R%d off=%d access_size=%d\n",
  1155. regno, off, access_size);
  1156. return -EACCES;
  1157. }
  1158. if (env->prog->aux->stack_depth < -off)
  1159. env->prog->aux->stack_depth = -off;
  1160. if (meta && meta->raw_mode) {
  1161. meta->access_size = access_size;
  1162. meta->regno = regno;
  1163. return 0;
  1164. }
  1165. for (i = 0; i < access_size; i++) {
  1166. if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
  1167. verbose("invalid indirect read from stack off %d+%d size %d\n",
  1168. off, i, access_size);
  1169. return -EACCES;
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
  1175. int access_size, bool zero_size_allowed,
  1176. struct bpf_call_arg_meta *meta)
  1177. {
  1178. struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
  1179. switch (reg->type) {
  1180. case PTR_TO_PACKET:
  1181. return check_packet_access(env, regno, reg->off, access_size);
  1182. case PTR_TO_MAP_VALUE:
  1183. return check_map_access(env, regno, reg->off, access_size);
  1184. default: /* scalar_value|ptr_to_stack or invalid ptr */
  1185. return check_stack_boundary(env, regno, access_size,
  1186. zero_size_allowed, meta);
  1187. }
  1188. }
  1189. static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
  1190. enum bpf_arg_type arg_type,
  1191. struct bpf_call_arg_meta *meta)
  1192. {
  1193. struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
  1194. enum bpf_reg_type expected_type, type = reg->type;
  1195. int err = 0;
  1196. if (arg_type == ARG_DONTCARE)
  1197. return 0;
  1198. err = check_reg_arg(env, regno, SRC_OP);
  1199. if (err)
  1200. return err;
  1201. if (arg_type == ARG_ANYTHING) {
  1202. if (is_pointer_value(env, regno)) {
  1203. verbose("R%d leaks addr into helper function\n", regno);
  1204. return -EACCES;
  1205. }
  1206. return 0;
  1207. }
  1208. if (type == PTR_TO_PACKET &&
  1209. !may_access_direct_pkt_data(env, meta, BPF_READ)) {
  1210. verbose("helper access to the packet is not allowed\n");
  1211. return -EACCES;
  1212. }
  1213. if (arg_type == ARG_PTR_TO_MAP_KEY ||
  1214. arg_type == ARG_PTR_TO_MAP_VALUE) {
  1215. expected_type = PTR_TO_STACK;
  1216. if (type != PTR_TO_PACKET && type != expected_type)
  1217. goto err_type;
  1218. } else if (arg_type == ARG_CONST_SIZE ||
  1219. arg_type == ARG_CONST_SIZE_OR_ZERO) {
  1220. expected_type = SCALAR_VALUE;
  1221. if (type != expected_type)
  1222. goto err_type;
  1223. } else if (arg_type == ARG_CONST_MAP_PTR) {
  1224. expected_type = CONST_PTR_TO_MAP;
  1225. if (type != expected_type)
  1226. goto err_type;
  1227. } else if (arg_type == ARG_PTR_TO_CTX) {
  1228. expected_type = PTR_TO_CTX;
  1229. if (type != expected_type)
  1230. goto err_type;
  1231. } else if (arg_type == ARG_PTR_TO_MEM ||
  1232. arg_type == ARG_PTR_TO_UNINIT_MEM) {
  1233. expected_type = PTR_TO_STACK;
  1234. /* One exception here. In case function allows for NULL to be
  1235. * passed in as argument, it's a SCALAR_VALUE type. Final test
  1236. * happens during stack boundary checking.
  1237. */
  1238. if (register_is_null(*reg))
  1239. /* final test in check_stack_boundary() */;
  1240. else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE &&
  1241. type != expected_type)
  1242. goto err_type;
  1243. meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
  1244. } else {
  1245. verbose("unsupported arg_type %d\n", arg_type);
  1246. return -EFAULT;
  1247. }
  1248. if (arg_type == ARG_CONST_MAP_PTR) {
  1249. /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
  1250. meta->map_ptr = reg->map_ptr;
  1251. } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
  1252. /* bpf_map_xxx(..., map_ptr, ..., key) call:
  1253. * check that [key, key + map->key_size) are within
  1254. * stack limits and initialized
  1255. */
  1256. if (!meta->map_ptr) {
  1257. /* in function declaration map_ptr must come before
  1258. * map_key, so that it's verified and known before
  1259. * we have to check map_key here. Otherwise it means
  1260. * that kernel subsystem misconfigured verifier
  1261. */
  1262. verbose("invalid map_ptr to access map->key\n");
  1263. return -EACCES;
  1264. }
  1265. if (type == PTR_TO_PACKET)
  1266. err = check_packet_access(env, regno, reg->off,
  1267. meta->map_ptr->key_size);
  1268. else
  1269. err = check_stack_boundary(env, regno,
  1270. meta->map_ptr->key_size,
  1271. false, NULL);
  1272. } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
  1273. /* bpf_map_xxx(..., map_ptr, ..., value) call:
  1274. * check [value, value + map->value_size) validity
  1275. */
  1276. if (!meta->map_ptr) {
  1277. /* kernel subsystem misconfigured verifier */
  1278. verbose("invalid map_ptr to access map->value\n");
  1279. return -EACCES;
  1280. }
  1281. if (type == PTR_TO_PACKET)
  1282. err = check_packet_access(env, regno, reg->off,
  1283. meta->map_ptr->value_size);
  1284. else
  1285. err = check_stack_boundary(env, regno,
  1286. meta->map_ptr->value_size,
  1287. false, NULL);
  1288. } else if (arg_type == ARG_CONST_SIZE ||
  1289. arg_type == ARG_CONST_SIZE_OR_ZERO) {
  1290. bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
  1291. /* bpf_xxx(..., buf, len) call will access 'len' bytes
  1292. * from stack pointer 'buf'. Check it
  1293. * note: regno == len, regno - 1 == buf
  1294. */
  1295. if (regno == 0) {
  1296. /* kernel subsystem misconfigured verifier */
  1297. verbose("ARG_CONST_SIZE cannot be first argument\n");
  1298. return -EACCES;
  1299. }
  1300. /* The register is SCALAR_VALUE; the access check
  1301. * happens using its boundaries.
  1302. */
  1303. if (!tnum_is_const(reg->var_off))
  1304. /* For unprivileged variable accesses, disable raw
  1305. * mode so that the program is required to
  1306. * initialize all the memory that the helper could
  1307. * just partially fill up.
  1308. */
  1309. meta = NULL;
  1310. if (reg->smin_value < 0) {
  1311. verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
  1312. regno);
  1313. return -EACCES;
  1314. }
  1315. if (reg->umin_value == 0) {
  1316. err = check_helper_mem_access(env, regno - 1, 0,
  1317. zero_size_allowed,
  1318. meta);
  1319. if (err)
  1320. return err;
  1321. }
  1322. if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
  1323. verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
  1324. regno);
  1325. return -EACCES;
  1326. }
  1327. err = check_helper_mem_access(env, regno - 1,
  1328. reg->umax_value,
  1329. zero_size_allowed, meta);
  1330. }
  1331. return err;
  1332. err_type:
  1333. verbose("R%d type=%s expected=%s\n", regno,
  1334. reg_type_str[type], reg_type_str[expected_type]);
  1335. return -EACCES;
  1336. }
  1337. static int check_map_func_compatibility(struct bpf_map *map, int func_id)
  1338. {
  1339. if (!map)
  1340. return 0;
  1341. /* We need a two way check, first is from map perspective ... */
  1342. switch (map->map_type) {
  1343. case BPF_MAP_TYPE_PROG_ARRAY:
  1344. if (func_id != BPF_FUNC_tail_call)
  1345. goto error;
  1346. break;
  1347. case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
  1348. if (func_id != BPF_FUNC_perf_event_read &&
  1349. func_id != BPF_FUNC_perf_event_output)
  1350. goto error;
  1351. break;
  1352. case BPF_MAP_TYPE_STACK_TRACE:
  1353. if (func_id != BPF_FUNC_get_stackid)
  1354. goto error;
  1355. break;
  1356. case BPF_MAP_TYPE_CGROUP_ARRAY:
  1357. if (func_id != BPF_FUNC_skb_under_cgroup &&
  1358. func_id != BPF_FUNC_current_task_under_cgroup)
  1359. goto error;
  1360. break;
  1361. /* devmap returns a pointer to a live net_device ifindex that we cannot
  1362. * allow to be modified from bpf side. So do not allow lookup elements
  1363. * for now.
  1364. */
  1365. case BPF_MAP_TYPE_DEVMAP:
  1366. if (func_id != BPF_FUNC_redirect_map)
  1367. goto error;
  1368. break;
  1369. case BPF_MAP_TYPE_ARRAY_OF_MAPS:
  1370. case BPF_MAP_TYPE_HASH_OF_MAPS:
  1371. if (func_id != BPF_FUNC_map_lookup_elem)
  1372. goto error;
  1373. break;
  1374. case BPF_MAP_TYPE_SOCKMAP:
  1375. if (func_id != BPF_FUNC_sk_redirect_map &&
  1376. func_id != BPF_FUNC_sock_map_update &&
  1377. func_id != BPF_FUNC_map_delete_elem)
  1378. goto error;
  1379. break;
  1380. default:
  1381. break;
  1382. }
  1383. /* ... and second from the function itself. */
  1384. switch (func_id) {
  1385. case BPF_FUNC_tail_call:
  1386. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  1387. goto error;
  1388. break;
  1389. case BPF_FUNC_perf_event_read:
  1390. case BPF_FUNC_perf_event_output:
  1391. if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
  1392. goto error;
  1393. break;
  1394. case BPF_FUNC_get_stackid:
  1395. if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
  1396. goto error;
  1397. break;
  1398. case BPF_FUNC_current_task_under_cgroup:
  1399. case BPF_FUNC_skb_under_cgroup:
  1400. if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
  1401. goto error;
  1402. break;
  1403. case BPF_FUNC_redirect_map:
  1404. if (map->map_type != BPF_MAP_TYPE_DEVMAP)
  1405. goto error;
  1406. break;
  1407. case BPF_FUNC_sk_redirect_map:
  1408. if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
  1409. goto error;
  1410. break;
  1411. case BPF_FUNC_sock_map_update:
  1412. if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
  1413. goto error;
  1414. break;
  1415. default:
  1416. break;
  1417. }
  1418. return 0;
  1419. error:
  1420. verbose("cannot pass map_type %d into func %s#%d\n",
  1421. map->map_type, func_id_name(func_id), func_id);
  1422. return -EINVAL;
  1423. }
  1424. static int check_raw_mode(const struct bpf_func_proto *fn)
  1425. {
  1426. int count = 0;
  1427. if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
  1428. count++;
  1429. if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
  1430. count++;
  1431. if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
  1432. count++;
  1433. if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
  1434. count++;
  1435. if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
  1436. count++;
  1437. return count > 1 ? -EINVAL : 0;
  1438. }
  1439. /* Packet data might have moved, any old PTR_TO_PACKET[_END] are now invalid,
  1440. * so turn them into unknown SCALAR_VALUE.
  1441. */
  1442. static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
  1443. {
  1444. struct bpf_verifier_state *state = &env->cur_state;
  1445. struct bpf_reg_state *regs = state->regs, *reg;
  1446. int i;
  1447. for (i = 0; i < MAX_BPF_REG; i++)
  1448. if (regs[i].type == PTR_TO_PACKET ||
  1449. regs[i].type == PTR_TO_PACKET_END)
  1450. mark_reg_unknown(regs, i);
  1451. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1452. if (state->stack_slot_type[i] != STACK_SPILL)
  1453. continue;
  1454. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1455. if (reg->type != PTR_TO_PACKET &&
  1456. reg->type != PTR_TO_PACKET_END)
  1457. continue;
  1458. __mark_reg_unknown(reg);
  1459. }
  1460. }
  1461. static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
  1462. {
  1463. struct bpf_verifier_state *state = &env->cur_state;
  1464. const struct bpf_func_proto *fn = NULL;
  1465. struct bpf_reg_state *regs = state->regs;
  1466. struct bpf_call_arg_meta meta;
  1467. bool changes_data;
  1468. int i, err;
  1469. /* find function prototype */
  1470. if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
  1471. verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
  1472. return -EINVAL;
  1473. }
  1474. if (env->prog->aux->ops->get_func_proto)
  1475. fn = env->prog->aux->ops->get_func_proto(func_id);
  1476. if (!fn) {
  1477. verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
  1478. return -EINVAL;
  1479. }
  1480. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  1481. if (!env->prog->gpl_compatible && fn->gpl_only) {
  1482. verbose("cannot call GPL only function from proprietary program\n");
  1483. return -EINVAL;
  1484. }
  1485. changes_data = bpf_helper_changes_pkt_data(fn->func);
  1486. memset(&meta, 0, sizeof(meta));
  1487. meta.pkt_access = fn->pkt_access;
  1488. /* We only support one arg being in raw mode at the moment, which
  1489. * is sufficient for the helper functions we have right now.
  1490. */
  1491. err = check_raw_mode(fn);
  1492. if (err) {
  1493. verbose("kernel subsystem misconfigured func %s#%d\n",
  1494. func_id_name(func_id), func_id);
  1495. return err;
  1496. }
  1497. /* check args */
  1498. err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
  1499. if (err)
  1500. return err;
  1501. err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
  1502. if (err)
  1503. return err;
  1504. err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
  1505. if (err)
  1506. return err;
  1507. err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
  1508. if (err)
  1509. return err;
  1510. err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
  1511. if (err)
  1512. return err;
  1513. /* Mark slots with STACK_MISC in case of raw mode, stack offset
  1514. * is inferred from register state.
  1515. */
  1516. for (i = 0; i < meta.access_size; i++) {
  1517. err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1);
  1518. if (err)
  1519. return err;
  1520. }
  1521. /* reset caller saved regs */
  1522. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  1523. mark_reg_not_init(regs, caller_saved[i]);
  1524. check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
  1525. }
  1526. /* update return register (already marked as written above) */
  1527. if (fn->ret_type == RET_INTEGER) {
  1528. /* sets type to SCALAR_VALUE */
  1529. mark_reg_unknown(regs, BPF_REG_0);
  1530. } else if (fn->ret_type == RET_VOID) {
  1531. regs[BPF_REG_0].type = NOT_INIT;
  1532. } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
  1533. struct bpf_insn_aux_data *insn_aux;
  1534. regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
  1535. /* There is no offset yet applied, variable or fixed */
  1536. mark_reg_known_zero(regs, BPF_REG_0);
  1537. regs[BPF_REG_0].off = 0;
  1538. /* remember map_ptr, so that check_map_access()
  1539. * can check 'value_size' boundary of memory access
  1540. * to map element returned from bpf_map_lookup_elem()
  1541. */
  1542. if (meta.map_ptr == NULL) {
  1543. verbose("kernel subsystem misconfigured verifier\n");
  1544. return -EINVAL;
  1545. }
  1546. regs[BPF_REG_0].map_ptr = meta.map_ptr;
  1547. regs[BPF_REG_0].id = ++env->id_gen;
  1548. insn_aux = &env->insn_aux_data[insn_idx];
  1549. if (!insn_aux->map_ptr)
  1550. insn_aux->map_ptr = meta.map_ptr;
  1551. else if (insn_aux->map_ptr != meta.map_ptr)
  1552. insn_aux->map_ptr = BPF_MAP_PTR_POISON;
  1553. } else {
  1554. verbose("unknown return type %d of func %s#%d\n",
  1555. fn->ret_type, func_id_name(func_id), func_id);
  1556. return -EINVAL;
  1557. }
  1558. err = check_map_func_compatibility(meta.map_ptr, func_id);
  1559. if (err)
  1560. return err;
  1561. if (changes_data)
  1562. clear_all_pkt_pointers(env);
  1563. return 0;
  1564. }
  1565. static void coerce_reg_to_32(struct bpf_reg_state *reg)
  1566. {
  1567. /* clear high 32 bits */
  1568. reg->var_off = tnum_cast(reg->var_off, 4);
  1569. /* Update bounds */
  1570. __update_reg_bounds(reg);
  1571. }
  1572. static bool signed_add_overflows(s64 a, s64 b)
  1573. {
  1574. /* Do the add in u64, where overflow is well-defined */
  1575. s64 res = (s64)((u64)a + (u64)b);
  1576. if (b < 0)
  1577. return res > a;
  1578. return res < a;
  1579. }
  1580. static bool signed_sub_overflows(s64 a, s64 b)
  1581. {
  1582. /* Do the sub in u64, where overflow is well-defined */
  1583. s64 res = (s64)((u64)a - (u64)b);
  1584. if (b < 0)
  1585. return res < a;
  1586. return res > a;
  1587. }
  1588. /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
  1589. * Caller should also handle BPF_MOV case separately.
  1590. * If we return -EACCES, caller may want to try again treating pointer as a
  1591. * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
  1592. */
  1593. static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
  1594. struct bpf_insn *insn,
  1595. const struct bpf_reg_state *ptr_reg,
  1596. const struct bpf_reg_state *off_reg)
  1597. {
  1598. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1599. bool known = tnum_is_const(off_reg->var_off);
  1600. s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
  1601. smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
  1602. u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
  1603. umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
  1604. u8 opcode = BPF_OP(insn->code);
  1605. u32 dst = insn->dst_reg;
  1606. dst_reg = &regs[dst];
  1607. if (WARN_ON_ONCE(known && (smin_val != smax_val))) {
  1608. print_verifier_state(&env->cur_state);
  1609. verbose("verifier internal error: known but bad sbounds\n");
  1610. return -EINVAL;
  1611. }
  1612. if (WARN_ON_ONCE(known && (umin_val != umax_val))) {
  1613. print_verifier_state(&env->cur_state);
  1614. verbose("verifier internal error: known but bad ubounds\n");
  1615. return -EINVAL;
  1616. }
  1617. if (BPF_CLASS(insn->code) != BPF_ALU64) {
  1618. /* 32-bit ALU ops on pointers produce (meaningless) scalars */
  1619. if (!env->allow_ptr_leaks)
  1620. verbose("R%d 32-bit pointer arithmetic prohibited\n",
  1621. dst);
  1622. return -EACCES;
  1623. }
  1624. if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
  1625. if (!env->allow_ptr_leaks)
  1626. verbose("R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
  1627. dst);
  1628. return -EACCES;
  1629. }
  1630. if (ptr_reg->type == CONST_PTR_TO_MAP) {
  1631. if (!env->allow_ptr_leaks)
  1632. verbose("R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
  1633. dst);
  1634. return -EACCES;
  1635. }
  1636. if (ptr_reg->type == PTR_TO_PACKET_END) {
  1637. if (!env->allow_ptr_leaks)
  1638. verbose("R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
  1639. dst);
  1640. return -EACCES;
  1641. }
  1642. /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
  1643. * The id may be overwritten later if we create a new variable offset.
  1644. */
  1645. dst_reg->type = ptr_reg->type;
  1646. dst_reg->id = ptr_reg->id;
  1647. switch (opcode) {
  1648. case BPF_ADD:
  1649. /* We can take a fixed offset as long as it doesn't overflow
  1650. * the s32 'off' field
  1651. */
  1652. if (known && (ptr_reg->off + smin_val ==
  1653. (s64)(s32)(ptr_reg->off + smin_val))) {
  1654. /* pointer += K. Accumulate it into fixed offset */
  1655. dst_reg->smin_value = smin_ptr;
  1656. dst_reg->smax_value = smax_ptr;
  1657. dst_reg->umin_value = umin_ptr;
  1658. dst_reg->umax_value = umax_ptr;
  1659. dst_reg->var_off = ptr_reg->var_off;
  1660. dst_reg->off = ptr_reg->off + smin_val;
  1661. dst_reg->range = ptr_reg->range;
  1662. break;
  1663. }
  1664. /* A new variable offset is created. Note that off_reg->off
  1665. * == 0, since it's a scalar.
  1666. * dst_reg gets the pointer type and since some positive
  1667. * integer value was added to the pointer, give it a new 'id'
  1668. * if it's a PTR_TO_PACKET.
  1669. * this creates a new 'base' pointer, off_reg (variable) gets
  1670. * added into the variable offset, and we copy the fixed offset
  1671. * from ptr_reg.
  1672. */
  1673. if (signed_add_overflows(smin_ptr, smin_val) ||
  1674. signed_add_overflows(smax_ptr, smax_val)) {
  1675. dst_reg->smin_value = S64_MIN;
  1676. dst_reg->smax_value = S64_MAX;
  1677. } else {
  1678. dst_reg->smin_value = smin_ptr + smin_val;
  1679. dst_reg->smax_value = smax_ptr + smax_val;
  1680. }
  1681. if (umin_ptr + umin_val < umin_ptr ||
  1682. umax_ptr + umax_val < umax_ptr) {
  1683. dst_reg->umin_value = 0;
  1684. dst_reg->umax_value = U64_MAX;
  1685. } else {
  1686. dst_reg->umin_value = umin_ptr + umin_val;
  1687. dst_reg->umax_value = umax_ptr + umax_val;
  1688. }
  1689. dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
  1690. dst_reg->off = ptr_reg->off;
  1691. if (ptr_reg->type == PTR_TO_PACKET) {
  1692. dst_reg->id = ++env->id_gen;
  1693. /* something was added to pkt_ptr, set range to zero */
  1694. dst_reg->range = 0;
  1695. }
  1696. break;
  1697. case BPF_SUB:
  1698. if (dst_reg == off_reg) {
  1699. /* scalar -= pointer. Creates an unknown scalar */
  1700. if (!env->allow_ptr_leaks)
  1701. verbose("R%d tried to subtract pointer from scalar\n",
  1702. dst);
  1703. return -EACCES;
  1704. }
  1705. /* We don't allow subtraction from FP, because (according to
  1706. * test_verifier.c test "invalid fp arithmetic", JITs might not
  1707. * be able to deal with it.
  1708. */
  1709. if (ptr_reg->type == PTR_TO_STACK) {
  1710. if (!env->allow_ptr_leaks)
  1711. verbose("R%d subtraction from stack pointer prohibited\n",
  1712. dst);
  1713. return -EACCES;
  1714. }
  1715. if (known && (ptr_reg->off - smin_val ==
  1716. (s64)(s32)(ptr_reg->off - smin_val))) {
  1717. /* pointer -= K. Subtract it from fixed offset */
  1718. dst_reg->smin_value = smin_ptr;
  1719. dst_reg->smax_value = smax_ptr;
  1720. dst_reg->umin_value = umin_ptr;
  1721. dst_reg->umax_value = umax_ptr;
  1722. dst_reg->var_off = ptr_reg->var_off;
  1723. dst_reg->id = ptr_reg->id;
  1724. dst_reg->off = ptr_reg->off - smin_val;
  1725. dst_reg->range = ptr_reg->range;
  1726. break;
  1727. }
  1728. /* A new variable offset is created. If the subtrahend is known
  1729. * nonnegative, then any reg->range we had before is still good.
  1730. */
  1731. if (signed_sub_overflows(smin_ptr, smax_val) ||
  1732. signed_sub_overflows(smax_ptr, smin_val)) {
  1733. /* Overflow possible, we know nothing */
  1734. dst_reg->smin_value = S64_MIN;
  1735. dst_reg->smax_value = S64_MAX;
  1736. } else {
  1737. dst_reg->smin_value = smin_ptr - smax_val;
  1738. dst_reg->smax_value = smax_ptr - smin_val;
  1739. }
  1740. if (umin_ptr < umax_val) {
  1741. /* Overflow possible, we know nothing */
  1742. dst_reg->umin_value = 0;
  1743. dst_reg->umax_value = U64_MAX;
  1744. } else {
  1745. /* Cannot overflow (as long as bounds are consistent) */
  1746. dst_reg->umin_value = umin_ptr - umax_val;
  1747. dst_reg->umax_value = umax_ptr - umin_val;
  1748. }
  1749. dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
  1750. dst_reg->off = ptr_reg->off;
  1751. if (ptr_reg->type == PTR_TO_PACKET) {
  1752. dst_reg->id = ++env->id_gen;
  1753. /* something was added to pkt_ptr, set range to zero */
  1754. if (smin_val < 0)
  1755. dst_reg->range = 0;
  1756. }
  1757. break;
  1758. case BPF_AND:
  1759. case BPF_OR:
  1760. case BPF_XOR:
  1761. /* bitwise ops on pointers are troublesome, prohibit for now.
  1762. * (However, in principle we could allow some cases, e.g.
  1763. * ptr &= ~3 which would reduce min_value by 3.)
  1764. */
  1765. if (!env->allow_ptr_leaks)
  1766. verbose("R%d bitwise operator %s on pointer prohibited\n",
  1767. dst, bpf_alu_string[opcode >> 4]);
  1768. return -EACCES;
  1769. default:
  1770. /* other operators (e.g. MUL,LSH) produce non-pointer results */
  1771. if (!env->allow_ptr_leaks)
  1772. verbose("R%d pointer arithmetic with %s operator prohibited\n",
  1773. dst, bpf_alu_string[opcode >> 4]);
  1774. return -EACCES;
  1775. }
  1776. __update_reg_bounds(dst_reg);
  1777. __reg_deduce_bounds(dst_reg);
  1778. __reg_bound_offset(dst_reg);
  1779. return 0;
  1780. }
  1781. static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
  1782. struct bpf_insn *insn,
  1783. struct bpf_reg_state *dst_reg,
  1784. struct bpf_reg_state src_reg)
  1785. {
  1786. struct bpf_reg_state *regs = env->cur_state.regs;
  1787. u8 opcode = BPF_OP(insn->code);
  1788. bool src_known, dst_known;
  1789. s64 smin_val, smax_val;
  1790. u64 umin_val, umax_val;
  1791. if (BPF_CLASS(insn->code) != BPF_ALU64) {
  1792. /* 32-bit ALU ops are (32,32)->64 */
  1793. coerce_reg_to_32(dst_reg);
  1794. coerce_reg_to_32(&src_reg);
  1795. }
  1796. smin_val = src_reg.smin_value;
  1797. smax_val = src_reg.smax_value;
  1798. umin_val = src_reg.umin_value;
  1799. umax_val = src_reg.umax_value;
  1800. src_known = tnum_is_const(src_reg.var_off);
  1801. dst_known = tnum_is_const(dst_reg->var_off);
  1802. switch (opcode) {
  1803. case BPF_ADD:
  1804. if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
  1805. signed_add_overflows(dst_reg->smax_value, smax_val)) {
  1806. dst_reg->smin_value = S64_MIN;
  1807. dst_reg->smax_value = S64_MAX;
  1808. } else {
  1809. dst_reg->smin_value += smin_val;
  1810. dst_reg->smax_value += smax_val;
  1811. }
  1812. if (dst_reg->umin_value + umin_val < umin_val ||
  1813. dst_reg->umax_value + umax_val < umax_val) {
  1814. dst_reg->umin_value = 0;
  1815. dst_reg->umax_value = U64_MAX;
  1816. } else {
  1817. dst_reg->umin_value += umin_val;
  1818. dst_reg->umax_value += umax_val;
  1819. }
  1820. dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
  1821. break;
  1822. case BPF_SUB:
  1823. if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
  1824. signed_sub_overflows(dst_reg->smax_value, smin_val)) {
  1825. /* Overflow possible, we know nothing */
  1826. dst_reg->smin_value = S64_MIN;
  1827. dst_reg->smax_value = S64_MAX;
  1828. } else {
  1829. dst_reg->smin_value -= smax_val;
  1830. dst_reg->smax_value -= smin_val;
  1831. }
  1832. if (dst_reg->umin_value < umax_val) {
  1833. /* Overflow possible, we know nothing */
  1834. dst_reg->umin_value = 0;
  1835. dst_reg->umax_value = U64_MAX;
  1836. } else {
  1837. /* Cannot overflow (as long as bounds are consistent) */
  1838. dst_reg->umin_value -= umax_val;
  1839. dst_reg->umax_value -= umin_val;
  1840. }
  1841. dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
  1842. break;
  1843. case BPF_MUL:
  1844. dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
  1845. if (smin_val < 0 || dst_reg->smin_value < 0) {
  1846. /* Ain't nobody got time to multiply that sign */
  1847. __mark_reg_unbounded(dst_reg);
  1848. __update_reg_bounds(dst_reg);
  1849. break;
  1850. }
  1851. /* Both values are positive, so we can work with unsigned and
  1852. * copy the result to signed (unless it exceeds S64_MAX).
  1853. */
  1854. if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
  1855. /* Potential overflow, we know nothing */
  1856. __mark_reg_unbounded(dst_reg);
  1857. /* (except what we can learn from the var_off) */
  1858. __update_reg_bounds(dst_reg);
  1859. break;
  1860. }
  1861. dst_reg->umin_value *= umin_val;
  1862. dst_reg->umax_value *= umax_val;
  1863. if (dst_reg->umax_value > S64_MAX) {
  1864. /* Overflow possible, we know nothing */
  1865. dst_reg->smin_value = S64_MIN;
  1866. dst_reg->smax_value = S64_MAX;
  1867. } else {
  1868. dst_reg->smin_value = dst_reg->umin_value;
  1869. dst_reg->smax_value = dst_reg->umax_value;
  1870. }
  1871. break;
  1872. case BPF_AND:
  1873. if (src_known && dst_known) {
  1874. __mark_reg_known(dst_reg, dst_reg->var_off.value &
  1875. src_reg.var_off.value);
  1876. break;
  1877. }
  1878. /* We get our minimum from the var_off, since that's inherently
  1879. * bitwise. Our maximum is the minimum of the operands' maxima.
  1880. */
  1881. dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
  1882. dst_reg->umin_value = dst_reg->var_off.value;
  1883. dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
  1884. if (dst_reg->smin_value < 0 || smin_val < 0) {
  1885. /* Lose signed bounds when ANDing negative numbers,
  1886. * ain't nobody got time for that.
  1887. */
  1888. dst_reg->smin_value = S64_MIN;
  1889. dst_reg->smax_value = S64_MAX;
  1890. } else {
  1891. /* ANDing two positives gives a positive, so safe to
  1892. * cast result into s64.
  1893. */
  1894. dst_reg->smin_value = dst_reg->umin_value;
  1895. dst_reg->smax_value = dst_reg->umax_value;
  1896. }
  1897. /* We may learn something more from the var_off */
  1898. __update_reg_bounds(dst_reg);
  1899. break;
  1900. case BPF_OR:
  1901. if (src_known && dst_known) {
  1902. __mark_reg_known(dst_reg, dst_reg->var_off.value |
  1903. src_reg.var_off.value);
  1904. break;
  1905. }
  1906. /* We get our maximum from the var_off, and our minimum is the
  1907. * maximum of the operands' minima
  1908. */
  1909. dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
  1910. dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
  1911. dst_reg->umax_value = dst_reg->var_off.value |
  1912. dst_reg->var_off.mask;
  1913. if (dst_reg->smin_value < 0 || smin_val < 0) {
  1914. /* Lose signed bounds when ORing negative numbers,
  1915. * ain't nobody got time for that.
  1916. */
  1917. dst_reg->smin_value = S64_MIN;
  1918. dst_reg->smax_value = S64_MAX;
  1919. } else {
  1920. /* ORing two positives gives a positive, so safe to
  1921. * cast result into s64.
  1922. */
  1923. dst_reg->smin_value = dst_reg->umin_value;
  1924. dst_reg->smax_value = dst_reg->umax_value;
  1925. }
  1926. /* We may learn something more from the var_off */
  1927. __update_reg_bounds(dst_reg);
  1928. break;
  1929. case BPF_LSH:
  1930. if (umax_val > 63) {
  1931. /* Shifts greater than 63 are undefined. This includes
  1932. * shifts by a negative number.
  1933. */
  1934. mark_reg_unknown(regs, insn->dst_reg);
  1935. break;
  1936. }
  1937. /* We lose all sign bit information (except what we can pick
  1938. * up from var_off)
  1939. */
  1940. dst_reg->smin_value = S64_MIN;
  1941. dst_reg->smax_value = S64_MAX;
  1942. /* If we might shift our top bit out, then we know nothing */
  1943. if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
  1944. dst_reg->umin_value = 0;
  1945. dst_reg->umax_value = U64_MAX;
  1946. } else {
  1947. dst_reg->umin_value <<= umin_val;
  1948. dst_reg->umax_value <<= umax_val;
  1949. }
  1950. if (src_known)
  1951. dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
  1952. else
  1953. dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val);
  1954. /* We may learn something more from the var_off */
  1955. __update_reg_bounds(dst_reg);
  1956. break;
  1957. case BPF_RSH:
  1958. if (umax_val > 63) {
  1959. /* Shifts greater than 63 are undefined. This includes
  1960. * shifts by a negative number.
  1961. */
  1962. mark_reg_unknown(regs, insn->dst_reg);
  1963. break;
  1964. }
  1965. /* BPF_RSH is an unsigned shift, so make the appropriate casts */
  1966. if (dst_reg->smin_value < 0) {
  1967. if (umin_val) {
  1968. /* Sign bit will be cleared */
  1969. dst_reg->smin_value = 0;
  1970. } else {
  1971. /* Lost sign bit information */
  1972. dst_reg->smin_value = S64_MIN;
  1973. dst_reg->smax_value = S64_MAX;
  1974. }
  1975. } else {
  1976. dst_reg->smin_value =
  1977. (u64)(dst_reg->smin_value) >> umax_val;
  1978. }
  1979. if (src_known)
  1980. dst_reg->var_off = tnum_rshift(dst_reg->var_off,
  1981. umin_val);
  1982. else
  1983. dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val);
  1984. dst_reg->umin_value >>= umax_val;
  1985. dst_reg->umax_value >>= umin_val;
  1986. /* We may learn something more from the var_off */
  1987. __update_reg_bounds(dst_reg);
  1988. break;
  1989. default:
  1990. mark_reg_unknown(regs, insn->dst_reg);
  1991. break;
  1992. }
  1993. __reg_deduce_bounds(dst_reg);
  1994. __reg_bound_offset(dst_reg);
  1995. return 0;
  1996. }
  1997. /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
  1998. * and var_off.
  1999. */
  2000. static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
  2001. struct bpf_insn *insn)
  2002. {
  2003. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg, *src_reg;
  2004. struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
  2005. u8 opcode = BPF_OP(insn->code);
  2006. int rc;
  2007. dst_reg = &regs[insn->dst_reg];
  2008. src_reg = NULL;
  2009. if (dst_reg->type != SCALAR_VALUE)
  2010. ptr_reg = dst_reg;
  2011. if (BPF_SRC(insn->code) == BPF_X) {
  2012. src_reg = &regs[insn->src_reg];
  2013. if (src_reg->type != SCALAR_VALUE) {
  2014. if (dst_reg->type != SCALAR_VALUE) {
  2015. /* Combining two pointers by any ALU op yields
  2016. * an arbitrary scalar.
  2017. */
  2018. if (!env->allow_ptr_leaks) {
  2019. verbose("R%d pointer %s pointer prohibited\n",
  2020. insn->dst_reg,
  2021. bpf_alu_string[opcode >> 4]);
  2022. return -EACCES;
  2023. }
  2024. mark_reg_unknown(regs, insn->dst_reg);
  2025. return 0;
  2026. } else {
  2027. /* scalar += pointer
  2028. * This is legal, but we have to reverse our
  2029. * src/dest handling in computing the range
  2030. */
  2031. rc = adjust_ptr_min_max_vals(env, insn,
  2032. src_reg, dst_reg);
  2033. if (rc == -EACCES && env->allow_ptr_leaks) {
  2034. /* scalar += unknown scalar */
  2035. __mark_reg_unknown(&off_reg);
  2036. return adjust_scalar_min_max_vals(
  2037. env, insn,
  2038. dst_reg, off_reg);
  2039. }
  2040. return rc;
  2041. }
  2042. } else if (ptr_reg) {
  2043. /* pointer += scalar */
  2044. rc = adjust_ptr_min_max_vals(env, insn,
  2045. dst_reg, src_reg);
  2046. if (rc == -EACCES && env->allow_ptr_leaks) {
  2047. /* unknown scalar += scalar */
  2048. __mark_reg_unknown(dst_reg);
  2049. return adjust_scalar_min_max_vals(
  2050. env, insn, dst_reg, *src_reg);
  2051. }
  2052. return rc;
  2053. }
  2054. } else {
  2055. /* Pretend the src is a reg with a known value, since we only
  2056. * need to be able to read from this state.
  2057. */
  2058. off_reg.type = SCALAR_VALUE;
  2059. __mark_reg_known(&off_reg, insn->imm);
  2060. src_reg = &off_reg;
  2061. if (ptr_reg) { /* pointer += K */
  2062. rc = adjust_ptr_min_max_vals(env, insn,
  2063. ptr_reg, src_reg);
  2064. if (rc == -EACCES && env->allow_ptr_leaks) {
  2065. /* unknown scalar += K */
  2066. __mark_reg_unknown(dst_reg);
  2067. return adjust_scalar_min_max_vals(
  2068. env, insn, dst_reg, off_reg);
  2069. }
  2070. return rc;
  2071. }
  2072. }
  2073. /* Got here implies adding two SCALAR_VALUEs */
  2074. if (WARN_ON_ONCE(ptr_reg)) {
  2075. print_verifier_state(&env->cur_state);
  2076. verbose("verifier internal error: unexpected ptr_reg\n");
  2077. return -EINVAL;
  2078. }
  2079. if (WARN_ON(!src_reg)) {
  2080. print_verifier_state(&env->cur_state);
  2081. verbose("verifier internal error: no src_reg\n");
  2082. return -EINVAL;
  2083. }
  2084. return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
  2085. }
  2086. /* check validity of 32-bit and 64-bit arithmetic operations */
  2087. static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2088. {
  2089. struct bpf_reg_state *regs = env->cur_state.regs;
  2090. u8 opcode = BPF_OP(insn->code);
  2091. int err;
  2092. if (opcode == BPF_END || opcode == BPF_NEG) {
  2093. if (opcode == BPF_NEG) {
  2094. if (BPF_SRC(insn->code) != 0 ||
  2095. insn->src_reg != BPF_REG_0 ||
  2096. insn->off != 0 || insn->imm != 0) {
  2097. verbose("BPF_NEG uses reserved fields\n");
  2098. return -EINVAL;
  2099. }
  2100. } else {
  2101. if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
  2102. (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
  2103. BPF_CLASS(insn->code) == BPF_ALU64) {
  2104. verbose("BPF_END uses reserved fields\n");
  2105. return -EINVAL;
  2106. }
  2107. }
  2108. /* check src operand */
  2109. err = check_reg_arg(env, insn->dst_reg, SRC_OP);
  2110. if (err)
  2111. return err;
  2112. if (is_pointer_value(env, insn->dst_reg)) {
  2113. verbose("R%d pointer arithmetic prohibited\n",
  2114. insn->dst_reg);
  2115. return -EACCES;
  2116. }
  2117. /* check dest operand */
  2118. err = check_reg_arg(env, insn->dst_reg, DST_OP);
  2119. if (err)
  2120. return err;
  2121. } else if (opcode == BPF_MOV) {
  2122. if (BPF_SRC(insn->code) == BPF_X) {
  2123. if (insn->imm != 0 || insn->off != 0) {
  2124. verbose("BPF_MOV uses reserved fields\n");
  2125. return -EINVAL;
  2126. }
  2127. /* check src operand */
  2128. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  2129. if (err)
  2130. return err;
  2131. } else {
  2132. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  2133. verbose("BPF_MOV uses reserved fields\n");
  2134. return -EINVAL;
  2135. }
  2136. }
  2137. /* check dest operand */
  2138. err = check_reg_arg(env, insn->dst_reg, DST_OP);
  2139. if (err)
  2140. return err;
  2141. if (BPF_SRC(insn->code) == BPF_X) {
  2142. if (BPF_CLASS(insn->code) == BPF_ALU64) {
  2143. /* case: R1 = R2
  2144. * copy register state to dest reg
  2145. */
  2146. regs[insn->dst_reg] = regs[insn->src_reg];
  2147. regs[insn->dst_reg].live |= REG_LIVE_WRITTEN;
  2148. } else {
  2149. /* R1 = (u32) R2 */
  2150. if (is_pointer_value(env, insn->src_reg)) {
  2151. verbose("R%d partial copy of pointer\n",
  2152. insn->src_reg);
  2153. return -EACCES;
  2154. }
  2155. mark_reg_unknown(regs, insn->dst_reg);
  2156. /* high 32 bits are known zero. */
  2157. regs[insn->dst_reg].var_off = tnum_cast(
  2158. regs[insn->dst_reg].var_off, 4);
  2159. __update_reg_bounds(&regs[insn->dst_reg]);
  2160. }
  2161. } else {
  2162. /* case: R = imm
  2163. * remember the value we stored into this reg
  2164. */
  2165. regs[insn->dst_reg].type = SCALAR_VALUE;
  2166. __mark_reg_known(regs + insn->dst_reg, insn->imm);
  2167. }
  2168. } else if (opcode > BPF_END) {
  2169. verbose("invalid BPF_ALU opcode %x\n", opcode);
  2170. return -EINVAL;
  2171. } else { /* all other ALU ops: and, sub, xor, add, ... */
  2172. if (BPF_SRC(insn->code) == BPF_X) {
  2173. if (insn->imm != 0 || insn->off != 0) {
  2174. verbose("BPF_ALU uses reserved fields\n");
  2175. return -EINVAL;
  2176. }
  2177. /* check src1 operand */
  2178. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  2179. if (err)
  2180. return err;
  2181. } else {
  2182. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  2183. verbose("BPF_ALU uses reserved fields\n");
  2184. return -EINVAL;
  2185. }
  2186. }
  2187. /* check src2 operand */
  2188. err = check_reg_arg(env, insn->dst_reg, SRC_OP);
  2189. if (err)
  2190. return err;
  2191. if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
  2192. BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
  2193. verbose("div by zero\n");
  2194. return -EINVAL;
  2195. }
  2196. if ((opcode == BPF_LSH || opcode == BPF_RSH ||
  2197. opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
  2198. int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
  2199. if (insn->imm < 0 || insn->imm >= size) {
  2200. verbose("invalid shift %d\n", insn->imm);
  2201. return -EINVAL;
  2202. }
  2203. }
  2204. /* check dest operand */
  2205. err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
  2206. if (err)
  2207. return err;
  2208. return adjust_reg_min_max_vals(env, insn);
  2209. }
  2210. return 0;
  2211. }
  2212. static void find_good_pkt_pointers(struct bpf_verifier_state *state,
  2213. struct bpf_reg_state *dst_reg)
  2214. {
  2215. struct bpf_reg_state *regs = state->regs, *reg;
  2216. int i;
  2217. if (dst_reg->off < 0)
  2218. /* This doesn't give us any range */
  2219. return;
  2220. if (dst_reg->umax_value > MAX_PACKET_OFF ||
  2221. dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
  2222. /* Risk of overflow. For instance, ptr + (1<<63) may be less
  2223. * than pkt_end, but that's because it's also less than pkt.
  2224. */
  2225. return;
  2226. /* LLVM can generate four kind of checks:
  2227. *
  2228. * Type 1/2:
  2229. *
  2230. * r2 = r3;
  2231. * r2 += 8;
  2232. * if (r2 > pkt_end) goto <handle exception>
  2233. * <access okay>
  2234. *
  2235. * r2 = r3;
  2236. * r2 += 8;
  2237. * if (r2 < pkt_end) goto <access okay>
  2238. * <handle exception>
  2239. *
  2240. * Where:
  2241. * r2 == dst_reg, pkt_end == src_reg
  2242. * r2=pkt(id=n,off=8,r=0)
  2243. * r3=pkt(id=n,off=0,r=0)
  2244. *
  2245. * Type 3/4:
  2246. *
  2247. * r2 = r3;
  2248. * r2 += 8;
  2249. * if (pkt_end >= r2) goto <access okay>
  2250. * <handle exception>
  2251. *
  2252. * r2 = r3;
  2253. * r2 += 8;
  2254. * if (pkt_end <= r2) goto <handle exception>
  2255. * <access okay>
  2256. *
  2257. * Where:
  2258. * pkt_end == dst_reg, r2 == src_reg
  2259. * r2=pkt(id=n,off=8,r=0)
  2260. * r3=pkt(id=n,off=0,r=0)
  2261. *
  2262. * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
  2263. * so that range of bytes [r3, r3 + 8) is safe to access.
  2264. */
  2265. /* If our ids match, then we must have the same max_value. And we
  2266. * don't care about the other reg's fixed offset, since if it's too big
  2267. * the range won't allow anything.
  2268. * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
  2269. */
  2270. for (i = 0; i < MAX_BPF_REG; i++)
  2271. if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
  2272. /* keep the maximum range already checked */
  2273. regs[i].range = max_t(u16, regs[i].range, dst_reg->off);
  2274. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  2275. if (state->stack_slot_type[i] != STACK_SPILL)
  2276. continue;
  2277. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  2278. if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
  2279. reg->range = max_t(u16, reg->range, dst_reg->off);
  2280. }
  2281. }
  2282. /* Adjusts the register min/max values in the case that the dst_reg is the
  2283. * variable register that we are working on, and src_reg is a constant or we're
  2284. * simply doing a BPF_K check.
  2285. * In JEQ/JNE cases we also adjust the var_off values.
  2286. */
  2287. static void reg_set_min_max(struct bpf_reg_state *true_reg,
  2288. struct bpf_reg_state *false_reg, u64 val,
  2289. u8 opcode)
  2290. {
  2291. /* If the dst_reg is a pointer, we can't learn anything about its
  2292. * variable offset from the compare (unless src_reg were a pointer into
  2293. * the same object, but we don't bother with that.
  2294. * Since false_reg and true_reg have the same type by construction, we
  2295. * only need to check one of them for pointerness.
  2296. */
  2297. if (__is_pointer_value(false, false_reg))
  2298. return;
  2299. switch (opcode) {
  2300. case BPF_JEQ:
  2301. /* If this is false then we know nothing Jon Snow, but if it is
  2302. * true then we know for sure.
  2303. */
  2304. __mark_reg_known(true_reg, val);
  2305. break;
  2306. case BPF_JNE:
  2307. /* If this is true we know nothing Jon Snow, but if it is false
  2308. * we know the value for sure;
  2309. */
  2310. __mark_reg_known(false_reg, val);
  2311. break;
  2312. case BPF_JGT:
  2313. false_reg->umax_value = min(false_reg->umax_value, val);
  2314. true_reg->umin_value = max(true_reg->umin_value, val + 1);
  2315. break;
  2316. case BPF_JSGT:
  2317. false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
  2318. true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
  2319. break;
  2320. case BPF_JLT:
  2321. false_reg->umin_value = max(false_reg->umin_value, val);
  2322. true_reg->umax_value = min(true_reg->umax_value, val - 1);
  2323. break;
  2324. case BPF_JSLT:
  2325. false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
  2326. true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
  2327. break;
  2328. case BPF_JGE:
  2329. false_reg->umax_value = min(false_reg->umax_value, val - 1);
  2330. true_reg->umin_value = max(true_reg->umin_value, val);
  2331. break;
  2332. case BPF_JSGE:
  2333. false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
  2334. true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
  2335. break;
  2336. case BPF_JLE:
  2337. false_reg->umin_value = max(false_reg->umin_value, val + 1);
  2338. true_reg->umax_value = min(true_reg->umax_value, val);
  2339. break;
  2340. case BPF_JSLE:
  2341. false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
  2342. true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
  2343. break;
  2344. default:
  2345. break;
  2346. }
  2347. __reg_deduce_bounds(false_reg);
  2348. __reg_deduce_bounds(true_reg);
  2349. /* We might have learned some bits from the bounds. */
  2350. __reg_bound_offset(false_reg);
  2351. __reg_bound_offset(true_reg);
  2352. /* Intersecting with the old var_off might have improved our bounds
  2353. * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
  2354. * then new var_off is (0; 0x7f...fc) which improves our umax.
  2355. */
  2356. __update_reg_bounds(false_reg);
  2357. __update_reg_bounds(true_reg);
  2358. }
  2359. /* Same as above, but for the case that dst_reg holds a constant and src_reg is
  2360. * the variable reg.
  2361. */
  2362. static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
  2363. struct bpf_reg_state *false_reg, u64 val,
  2364. u8 opcode)
  2365. {
  2366. if (__is_pointer_value(false, false_reg))
  2367. return;
  2368. switch (opcode) {
  2369. case BPF_JEQ:
  2370. /* If this is false then we know nothing Jon Snow, but if it is
  2371. * true then we know for sure.
  2372. */
  2373. __mark_reg_known(true_reg, val);
  2374. break;
  2375. case BPF_JNE:
  2376. /* If this is true we know nothing Jon Snow, but if it is false
  2377. * we know the value for sure;
  2378. */
  2379. __mark_reg_known(false_reg, val);
  2380. break;
  2381. case BPF_JGT:
  2382. true_reg->umax_value = min(true_reg->umax_value, val - 1);
  2383. false_reg->umin_value = max(false_reg->umin_value, val);
  2384. break;
  2385. case BPF_JSGT:
  2386. true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1);
  2387. false_reg->smin_value = max_t(s64, false_reg->smin_value, val);
  2388. break;
  2389. case BPF_JLT:
  2390. true_reg->umin_value = max(true_reg->umin_value, val + 1);
  2391. false_reg->umax_value = min(false_reg->umax_value, val);
  2392. break;
  2393. case BPF_JSLT:
  2394. true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1);
  2395. false_reg->smax_value = min_t(s64, false_reg->smax_value, val);
  2396. break;
  2397. case BPF_JGE:
  2398. true_reg->umax_value = min(true_reg->umax_value, val);
  2399. false_reg->umin_value = max(false_reg->umin_value, val + 1);
  2400. break;
  2401. case BPF_JSGE:
  2402. true_reg->smax_value = min_t(s64, true_reg->smax_value, val);
  2403. false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1);
  2404. break;
  2405. case BPF_JLE:
  2406. true_reg->umin_value = max(true_reg->umin_value, val);
  2407. false_reg->umax_value = min(false_reg->umax_value, val - 1);
  2408. break;
  2409. case BPF_JSLE:
  2410. true_reg->smin_value = max_t(s64, true_reg->smin_value, val);
  2411. false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1);
  2412. break;
  2413. default:
  2414. break;
  2415. }
  2416. __reg_deduce_bounds(false_reg);
  2417. __reg_deduce_bounds(true_reg);
  2418. /* We might have learned some bits from the bounds. */
  2419. __reg_bound_offset(false_reg);
  2420. __reg_bound_offset(true_reg);
  2421. /* Intersecting with the old var_off might have improved our bounds
  2422. * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
  2423. * then new var_off is (0; 0x7f...fc) which improves our umax.
  2424. */
  2425. __update_reg_bounds(false_reg);
  2426. __update_reg_bounds(true_reg);
  2427. }
  2428. /* Regs are known to be equal, so intersect their min/max/var_off */
  2429. static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
  2430. struct bpf_reg_state *dst_reg)
  2431. {
  2432. src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
  2433. dst_reg->umin_value);
  2434. src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
  2435. dst_reg->umax_value);
  2436. src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
  2437. dst_reg->smin_value);
  2438. src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
  2439. dst_reg->smax_value);
  2440. src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
  2441. dst_reg->var_off);
  2442. /* We might have learned new bounds from the var_off. */
  2443. __update_reg_bounds(src_reg);
  2444. __update_reg_bounds(dst_reg);
  2445. /* We might have learned something about the sign bit. */
  2446. __reg_deduce_bounds(src_reg);
  2447. __reg_deduce_bounds(dst_reg);
  2448. /* We might have learned some bits from the bounds. */
  2449. __reg_bound_offset(src_reg);
  2450. __reg_bound_offset(dst_reg);
  2451. /* Intersecting with the old var_off might have improved our bounds
  2452. * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
  2453. * then new var_off is (0; 0x7f...fc) which improves our umax.
  2454. */
  2455. __update_reg_bounds(src_reg);
  2456. __update_reg_bounds(dst_reg);
  2457. }
  2458. static void reg_combine_min_max(struct bpf_reg_state *true_src,
  2459. struct bpf_reg_state *true_dst,
  2460. struct bpf_reg_state *false_src,
  2461. struct bpf_reg_state *false_dst,
  2462. u8 opcode)
  2463. {
  2464. switch (opcode) {
  2465. case BPF_JEQ:
  2466. __reg_combine_min_max(true_src, true_dst);
  2467. break;
  2468. case BPF_JNE:
  2469. __reg_combine_min_max(false_src, false_dst);
  2470. break;
  2471. }
  2472. }
  2473. static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
  2474. bool is_null)
  2475. {
  2476. struct bpf_reg_state *reg = &regs[regno];
  2477. if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
  2478. /* Old offset (both fixed and variable parts) should
  2479. * have been known-zero, because we don't allow pointer
  2480. * arithmetic on pointers that might be NULL.
  2481. */
  2482. if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
  2483. !tnum_equals_const(reg->var_off, 0) ||
  2484. reg->off)) {
  2485. __mark_reg_known_zero(reg);
  2486. reg->off = 0;
  2487. }
  2488. if (is_null) {
  2489. reg->type = SCALAR_VALUE;
  2490. } else if (reg->map_ptr->inner_map_meta) {
  2491. reg->type = CONST_PTR_TO_MAP;
  2492. reg->map_ptr = reg->map_ptr->inner_map_meta;
  2493. } else {
  2494. reg->type = PTR_TO_MAP_VALUE;
  2495. }
  2496. /* We don't need id from this point onwards anymore, thus we
  2497. * should better reset it, so that state pruning has chances
  2498. * to take effect.
  2499. */
  2500. reg->id = 0;
  2501. }
  2502. }
  2503. /* The logic is similar to find_good_pkt_pointers(), both could eventually
  2504. * be folded together at some point.
  2505. */
  2506. static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
  2507. bool is_null)
  2508. {
  2509. struct bpf_reg_state *regs = state->regs;
  2510. u32 id = regs[regno].id;
  2511. int i;
  2512. for (i = 0; i < MAX_BPF_REG; i++)
  2513. mark_map_reg(regs, i, id, is_null);
  2514. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  2515. if (state->stack_slot_type[i] != STACK_SPILL)
  2516. continue;
  2517. mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, is_null);
  2518. }
  2519. }
  2520. static int check_cond_jmp_op(struct bpf_verifier_env *env,
  2521. struct bpf_insn *insn, int *insn_idx)
  2522. {
  2523. struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
  2524. struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
  2525. u8 opcode = BPF_OP(insn->code);
  2526. int err;
  2527. if (opcode > BPF_JSLE) {
  2528. verbose("invalid BPF_JMP opcode %x\n", opcode);
  2529. return -EINVAL;
  2530. }
  2531. if (BPF_SRC(insn->code) == BPF_X) {
  2532. if (insn->imm != 0) {
  2533. verbose("BPF_JMP uses reserved fields\n");
  2534. return -EINVAL;
  2535. }
  2536. /* check src1 operand */
  2537. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  2538. if (err)
  2539. return err;
  2540. if (is_pointer_value(env, insn->src_reg)) {
  2541. verbose("R%d pointer comparison prohibited\n",
  2542. insn->src_reg);
  2543. return -EACCES;
  2544. }
  2545. } else {
  2546. if (insn->src_reg != BPF_REG_0) {
  2547. verbose("BPF_JMP uses reserved fields\n");
  2548. return -EINVAL;
  2549. }
  2550. }
  2551. /* check src2 operand */
  2552. err = check_reg_arg(env, insn->dst_reg, SRC_OP);
  2553. if (err)
  2554. return err;
  2555. dst_reg = &regs[insn->dst_reg];
  2556. /* detect if R == 0 where R was initialized to zero earlier */
  2557. if (BPF_SRC(insn->code) == BPF_K &&
  2558. (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  2559. dst_reg->type == SCALAR_VALUE &&
  2560. tnum_equals_const(dst_reg->var_off, insn->imm)) {
  2561. if (opcode == BPF_JEQ) {
  2562. /* if (imm == imm) goto pc+off;
  2563. * only follow the goto, ignore fall-through
  2564. */
  2565. *insn_idx += insn->off;
  2566. return 0;
  2567. } else {
  2568. /* if (imm != imm) goto pc+off;
  2569. * only follow fall-through branch, since
  2570. * that's where the program will go
  2571. */
  2572. return 0;
  2573. }
  2574. }
  2575. other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
  2576. if (!other_branch)
  2577. return -EFAULT;
  2578. /* detect if we are comparing against a constant value so we can adjust
  2579. * our min/max values for our dst register.
  2580. * this is only legit if both are scalars (or pointers to the same
  2581. * object, I suppose, but we don't support that right now), because
  2582. * otherwise the different base pointers mean the offsets aren't
  2583. * comparable.
  2584. */
  2585. if (BPF_SRC(insn->code) == BPF_X) {
  2586. if (dst_reg->type == SCALAR_VALUE &&
  2587. regs[insn->src_reg].type == SCALAR_VALUE) {
  2588. if (tnum_is_const(regs[insn->src_reg].var_off))
  2589. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  2590. dst_reg, regs[insn->src_reg].var_off.value,
  2591. opcode);
  2592. else if (tnum_is_const(dst_reg->var_off))
  2593. reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
  2594. &regs[insn->src_reg],
  2595. dst_reg->var_off.value, opcode);
  2596. else if (opcode == BPF_JEQ || opcode == BPF_JNE)
  2597. /* Comparing for equality, we can combine knowledge */
  2598. reg_combine_min_max(&other_branch->regs[insn->src_reg],
  2599. &other_branch->regs[insn->dst_reg],
  2600. &regs[insn->src_reg],
  2601. &regs[insn->dst_reg], opcode);
  2602. }
  2603. } else if (dst_reg->type == SCALAR_VALUE) {
  2604. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  2605. dst_reg, insn->imm, opcode);
  2606. }
  2607. /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
  2608. if (BPF_SRC(insn->code) == BPF_K &&
  2609. insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  2610. dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
  2611. /* Mark all identical map registers in each branch as either
  2612. * safe or unknown depending R == 0 or R != 0 conditional.
  2613. */
  2614. mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
  2615. mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
  2616. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
  2617. dst_reg->type == PTR_TO_PACKET &&
  2618. regs[insn->src_reg].type == PTR_TO_PACKET_END) {
  2619. find_good_pkt_pointers(this_branch, dst_reg);
  2620. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT &&
  2621. dst_reg->type == PTR_TO_PACKET &&
  2622. regs[insn->src_reg].type == PTR_TO_PACKET_END) {
  2623. find_good_pkt_pointers(other_branch, dst_reg);
  2624. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
  2625. dst_reg->type == PTR_TO_PACKET_END &&
  2626. regs[insn->src_reg].type == PTR_TO_PACKET) {
  2627. find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
  2628. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE &&
  2629. dst_reg->type == PTR_TO_PACKET_END &&
  2630. regs[insn->src_reg].type == PTR_TO_PACKET) {
  2631. find_good_pkt_pointers(this_branch, &regs[insn->src_reg]);
  2632. } else if (is_pointer_value(env, insn->dst_reg)) {
  2633. verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
  2634. return -EACCES;
  2635. }
  2636. if (log_level)
  2637. print_verifier_state(this_branch);
  2638. return 0;
  2639. }
  2640. /* return the map pointer stored inside BPF_LD_IMM64 instruction */
  2641. static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
  2642. {
  2643. u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
  2644. return (struct bpf_map *) (unsigned long) imm64;
  2645. }
  2646. /* verify BPF_LD_IMM64 instruction */
  2647. static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2648. {
  2649. struct bpf_reg_state *regs = env->cur_state.regs;
  2650. int err;
  2651. if (BPF_SIZE(insn->code) != BPF_DW) {
  2652. verbose("invalid BPF_LD_IMM insn\n");
  2653. return -EINVAL;
  2654. }
  2655. if (insn->off != 0) {
  2656. verbose("BPF_LD_IMM64 uses reserved fields\n");
  2657. return -EINVAL;
  2658. }
  2659. err = check_reg_arg(env, insn->dst_reg, DST_OP);
  2660. if (err)
  2661. return err;
  2662. if (insn->src_reg == 0) {
  2663. u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
  2664. regs[insn->dst_reg].type = SCALAR_VALUE;
  2665. __mark_reg_known(&regs[insn->dst_reg], imm);
  2666. return 0;
  2667. }
  2668. /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
  2669. BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
  2670. regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
  2671. regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
  2672. return 0;
  2673. }
  2674. static bool may_access_skb(enum bpf_prog_type type)
  2675. {
  2676. switch (type) {
  2677. case BPF_PROG_TYPE_SOCKET_FILTER:
  2678. case BPF_PROG_TYPE_SCHED_CLS:
  2679. case BPF_PROG_TYPE_SCHED_ACT:
  2680. return true;
  2681. default:
  2682. return false;
  2683. }
  2684. }
  2685. /* verify safety of LD_ABS|LD_IND instructions:
  2686. * - they can only appear in the programs where ctx == skb
  2687. * - since they are wrappers of function calls, they scratch R1-R5 registers,
  2688. * preserve R6-R9, and store return value into R0
  2689. *
  2690. * Implicit input:
  2691. * ctx == skb == R6 == CTX
  2692. *
  2693. * Explicit input:
  2694. * SRC == any register
  2695. * IMM == 32-bit immediate
  2696. *
  2697. * Output:
  2698. * R0 - 8/16/32-bit skb data converted to cpu endianness
  2699. */
  2700. static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2701. {
  2702. struct bpf_reg_state *regs = env->cur_state.regs;
  2703. u8 mode = BPF_MODE(insn->code);
  2704. int i, err;
  2705. if (!may_access_skb(env->prog->type)) {
  2706. verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
  2707. return -EINVAL;
  2708. }
  2709. if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
  2710. BPF_SIZE(insn->code) == BPF_DW ||
  2711. (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
  2712. verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
  2713. return -EINVAL;
  2714. }
  2715. /* check whether implicit source operand (register R6) is readable */
  2716. err = check_reg_arg(env, BPF_REG_6, SRC_OP);
  2717. if (err)
  2718. return err;
  2719. if (regs[BPF_REG_6].type != PTR_TO_CTX) {
  2720. verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
  2721. return -EINVAL;
  2722. }
  2723. if (mode == BPF_IND) {
  2724. /* check explicit source operand */
  2725. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  2726. if (err)
  2727. return err;
  2728. }
  2729. /* reset caller saved regs to unreadable */
  2730. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  2731. mark_reg_not_init(regs, caller_saved[i]);
  2732. check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
  2733. }
  2734. /* mark destination R0 register as readable, since it contains
  2735. * the value fetched from the packet.
  2736. * Already marked as written above.
  2737. */
  2738. mark_reg_unknown(regs, BPF_REG_0);
  2739. return 0;
  2740. }
  2741. /* non-recursive DFS pseudo code
  2742. * 1 procedure DFS-iterative(G,v):
  2743. * 2 label v as discovered
  2744. * 3 let S be a stack
  2745. * 4 S.push(v)
  2746. * 5 while S is not empty
  2747. * 6 t <- S.pop()
  2748. * 7 if t is what we're looking for:
  2749. * 8 return t
  2750. * 9 for all edges e in G.adjacentEdges(t) do
  2751. * 10 if edge e is already labelled
  2752. * 11 continue with the next edge
  2753. * 12 w <- G.adjacentVertex(t,e)
  2754. * 13 if vertex w is not discovered and not explored
  2755. * 14 label e as tree-edge
  2756. * 15 label w as discovered
  2757. * 16 S.push(w)
  2758. * 17 continue at 5
  2759. * 18 else if vertex w is discovered
  2760. * 19 label e as back-edge
  2761. * 20 else
  2762. * 21 // vertex w is explored
  2763. * 22 label e as forward- or cross-edge
  2764. * 23 label t as explored
  2765. * 24 S.pop()
  2766. *
  2767. * convention:
  2768. * 0x10 - discovered
  2769. * 0x11 - discovered and fall-through edge labelled
  2770. * 0x12 - discovered and fall-through and branch edges labelled
  2771. * 0x20 - explored
  2772. */
  2773. enum {
  2774. DISCOVERED = 0x10,
  2775. EXPLORED = 0x20,
  2776. FALLTHROUGH = 1,
  2777. BRANCH = 2,
  2778. };
  2779. #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
  2780. static int *insn_stack; /* stack of insns to process */
  2781. static int cur_stack; /* current stack index */
  2782. static int *insn_state;
  2783. /* t, w, e - match pseudo-code above:
  2784. * t - index of current instruction
  2785. * w - next instruction
  2786. * e - edge
  2787. */
  2788. static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
  2789. {
  2790. if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
  2791. return 0;
  2792. if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
  2793. return 0;
  2794. if (w < 0 || w >= env->prog->len) {
  2795. verbose("jump out of range from insn %d to %d\n", t, w);
  2796. return -EINVAL;
  2797. }
  2798. if (e == BRANCH)
  2799. /* mark branch target for state pruning */
  2800. env->explored_states[w] = STATE_LIST_MARK;
  2801. if (insn_state[w] == 0) {
  2802. /* tree-edge */
  2803. insn_state[t] = DISCOVERED | e;
  2804. insn_state[w] = DISCOVERED;
  2805. if (cur_stack >= env->prog->len)
  2806. return -E2BIG;
  2807. insn_stack[cur_stack++] = w;
  2808. return 1;
  2809. } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
  2810. verbose("back-edge from insn %d to %d\n", t, w);
  2811. return -EINVAL;
  2812. } else if (insn_state[w] == EXPLORED) {
  2813. /* forward- or cross-edge */
  2814. insn_state[t] = DISCOVERED | e;
  2815. } else {
  2816. verbose("insn state internal bug\n");
  2817. return -EFAULT;
  2818. }
  2819. return 0;
  2820. }
  2821. /* non-recursive depth-first-search to detect loops in BPF program
  2822. * loop == back-edge in directed graph
  2823. */
  2824. static int check_cfg(struct bpf_verifier_env *env)
  2825. {
  2826. struct bpf_insn *insns = env->prog->insnsi;
  2827. int insn_cnt = env->prog->len;
  2828. int ret = 0;
  2829. int i, t;
  2830. insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2831. if (!insn_state)
  2832. return -ENOMEM;
  2833. insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2834. if (!insn_stack) {
  2835. kfree(insn_state);
  2836. return -ENOMEM;
  2837. }
  2838. insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
  2839. insn_stack[0] = 0; /* 0 is the first instruction */
  2840. cur_stack = 1;
  2841. peek_stack:
  2842. if (cur_stack == 0)
  2843. goto check_state;
  2844. t = insn_stack[cur_stack - 1];
  2845. if (BPF_CLASS(insns[t].code) == BPF_JMP) {
  2846. u8 opcode = BPF_OP(insns[t].code);
  2847. if (opcode == BPF_EXIT) {
  2848. goto mark_explored;
  2849. } else if (opcode == BPF_CALL) {
  2850. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2851. if (ret == 1)
  2852. goto peek_stack;
  2853. else if (ret < 0)
  2854. goto err_free;
  2855. if (t + 1 < insn_cnt)
  2856. env->explored_states[t + 1] = STATE_LIST_MARK;
  2857. } else if (opcode == BPF_JA) {
  2858. if (BPF_SRC(insns[t].code) != BPF_K) {
  2859. ret = -EINVAL;
  2860. goto err_free;
  2861. }
  2862. /* unconditional jump with single edge */
  2863. ret = push_insn(t, t + insns[t].off + 1,
  2864. FALLTHROUGH, env);
  2865. if (ret == 1)
  2866. goto peek_stack;
  2867. else if (ret < 0)
  2868. goto err_free;
  2869. /* tell verifier to check for equivalent states
  2870. * after every call and jump
  2871. */
  2872. if (t + 1 < insn_cnt)
  2873. env->explored_states[t + 1] = STATE_LIST_MARK;
  2874. } else {
  2875. /* conditional jump with two edges */
  2876. env->explored_states[t] = STATE_LIST_MARK;
  2877. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2878. if (ret == 1)
  2879. goto peek_stack;
  2880. else if (ret < 0)
  2881. goto err_free;
  2882. ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
  2883. if (ret == 1)
  2884. goto peek_stack;
  2885. else if (ret < 0)
  2886. goto err_free;
  2887. }
  2888. } else {
  2889. /* all other non-branch instructions with single
  2890. * fall-through edge
  2891. */
  2892. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2893. if (ret == 1)
  2894. goto peek_stack;
  2895. else if (ret < 0)
  2896. goto err_free;
  2897. }
  2898. mark_explored:
  2899. insn_state[t] = EXPLORED;
  2900. if (cur_stack-- <= 0) {
  2901. verbose("pop stack internal bug\n");
  2902. ret = -EFAULT;
  2903. goto err_free;
  2904. }
  2905. goto peek_stack;
  2906. check_state:
  2907. for (i = 0; i < insn_cnt; i++) {
  2908. if (insn_state[i] != EXPLORED) {
  2909. verbose("unreachable insn %d\n", i);
  2910. ret = -EINVAL;
  2911. goto err_free;
  2912. }
  2913. }
  2914. ret = 0; /* cfg looks good */
  2915. err_free:
  2916. kfree(insn_state);
  2917. kfree(insn_stack);
  2918. return ret;
  2919. }
  2920. /* check %cur's range satisfies %old's */
  2921. static bool range_within(struct bpf_reg_state *old,
  2922. struct bpf_reg_state *cur)
  2923. {
  2924. return old->umin_value <= cur->umin_value &&
  2925. old->umax_value >= cur->umax_value &&
  2926. old->smin_value <= cur->smin_value &&
  2927. old->smax_value >= cur->smax_value;
  2928. }
  2929. /* Maximum number of register states that can exist at once */
  2930. #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
  2931. struct idpair {
  2932. u32 old;
  2933. u32 cur;
  2934. };
  2935. /* If in the old state two registers had the same id, then they need to have
  2936. * the same id in the new state as well. But that id could be different from
  2937. * the old state, so we need to track the mapping from old to new ids.
  2938. * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
  2939. * regs with old id 5 must also have new id 9 for the new state to be safe. But
  2940. * regs with a different old id could still have new id 9, we don't care about
  2941. * that.
  2942. * So we look through our idmap to see if this old id has been seen before. If
  2943. * so, we require the new id to match; otherwise, we add the id pair to the map.
  2944. */
  2945. static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
  2946. {
  2947. unsigned int i;
  2948. for (i = 0; i < ID_MAP_SIZE; i++) {
  2949. if (!idmap[i].old) {
  2950. /* Reached an empty slot; haven't seen this id before */
  2951. idmap[i].old = old_id;
  2952. idmap[i].cur = cur_id;
  2953. return true;
  2954. }
  2955. if (idmap[i].old == old_id)
  2956. return idmap[i].cur == cur_id;
  2957. }
  2958. /* We ran out of idmap slots, which should be impossible */
  2959. WARN_ON_ONCE(1);
  2960. return false;
  2961. }
  2962. /* Returns true if (rold safe implies rcur safe) */
  2963. static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
  2964. struct idpair *idmap)
  2965. {
  2966. if (!(rold->live & REG_LIVE_READ))
  2967. /* explored state didn't use this */
  2968. return true;
  2969. if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, live)) == 0)
  2970. return true;
  2971. if (rold->type == NOT_INIT)
  2972. /* explored state can't have used this */
  2973. return true;
  2974. if (rcur->type == NOT_INIT)
  2975. return false;
  2976. switch (rold->type) {
  2977. case SCALAR_VALUE:
  2978. if (rcur->type == SCALAR_VALUE) {
  2979. /* new val must satisfy old val knowledge */
  2980. return range_within(rold, rcur) &&
  2981. tnum_in(rold->var_off, rcur->var_off);
  2982. } else {
  2983. /* if we knew anything about the old value, we're not
  2984. * equal, because we can't know anything about the
  2985. * scalar value of the pointer in the new value.
  2986. */
  2987. return rold->umin_value == 0 &&
  2988. rold->umax_value == U64_MAX &&
  2989. rold->smin_value == S64_MIN &&
  2990. rold->smax_value == S64_MAX &&
  2991. tnum_is_unknown(rold->var_off);
  2992. }
  2993. case PTR_TO_MAP_VALUE:
  2994. /* If the new min/max/var_off satisfy the old ones and
  2995. * everything else matches, we are OK.
  2996. * We don't care about the 'id' value, because nothing
  2997. * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
  2998. */
  2999. return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
  3000. range_within(rold, rcur) &&
  3001. tnum_in(rold->var_off, rcur->var_off);
  3002. case PTR_TO_MAP_VALUE_OR_NULL:
  3003. /* a PTR_TO_MAP_VALUE could be safe to use as a
  3004. * PTR_TO_MAP_VALUE_OR_NULL into the same map.
  3005. * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
  3006. * checked, doing so could have affected others with the same
  3007. * id, and we can't check for that because we lost the id when
  3008. * we converted to a PTR_TO_MAP_VALUE.
  3009. */
  3010. if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
  3011. return false;
  3012. if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
  3013. return false;
  3014. /* Check our ids match any regs they're supposed to */
  3015. return check_ids(rold->id, rcur->id, idmap);
  3016. case PTR_TO_PACKET:
  3017. if (rcur->type != PTR_TO_PACKET)
  3018. return false;
  3019. /* We must have at least as much range as the old ptr
  3020. * did, so that any accesses which were safe before are
  3021. * still safe. This is true even if old range < old off,
  3022. * since someone could have accessed through (ptr - k), or
  3023. * even done ptr -= k in a register, to get a safe access.
  3024. */
  3025. if (rold->range > rcur->range)
  3026. return false;
  3027. /* If the offsets don't match, we can't trust our alignment;
  3028. * nor can we be sure that we won't fall out of range.
  3029. */
  3030. if (rold->off != rcur->off)
  3031. return false;
  3032. /* id relations must be preserved */
  3033. if (rold->id && !check_ids(rold->id, rcur->id, idmap))
  3034. return false;
  3035. /* new val must satisfy old val knowledge */
  3036. return range_within(rold, rcur) &&
  3037. tnum_in(rold->var_off, rcur->var_off);
  3038. case PTR_TO_CTX:
  3039. case CONST_PTR_TO_MAP:
  3040. case PTR_TO_STACK:
  3041. case PTR_TO_PACKET_END:
  3042. /* Only valid matches are exact, which memcmp() above
  3043. * would have accepted
  3044. */
  3045. default:
  3046. /* Don't know what's going on, just say it's not safe */
  3047. return false;
  3048. }
  3049. /* Shouldn't get here; if we do, say it's not safe */
  3050. WARN_ON_ONCE(1);
  3051. return false;
  3052. }
  3053. /* compare two verifier states
  3054. *
  3055. * all states stored in state_list are known to be valid, since
  3056. * verifier reached 'bpf_exit' instruction through them
  3057. *
  3058. * this function is called when verifier exploring different branches of
  3059. * execution popped from the state stack. If it sees an old state that has
  3060. * more strict register state and more strict stack state then this execution
  3061. * branch doesn't need to be explored further, since verifier already
  3062. * concluded that more strict state leads to valid finish.
  3063. *
  3064. * Therefore two states are equivalent if register state is more conservative
  3065. * and explored stack state is more conservative than the current one.
  3066. * Example:
  3067. * explored current
  3068. * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
  3069. * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
  3070. *
  3071. * In other words if current stack state (one being explored) has more
  3072. * valid slots than old one that already passed validation, it means
  3073. * the verifier can stop exploring and conclude that current state is valid too
  3074. *
  3075. * Similarly with registers. If explored state has register type as invalid
  3076. * whereas register type in current state is meaningful, it means that
  3077. * the current state will reach 'bpf_exit' instruction safely
  3078. */
  3079. static bool states_equal(struct bpf_verifier_env *env,
  3080. struct bpf_verifier_state *old,
  3081. struct bpf_verifier_state *cur)
  3082. {
  3083. struct idpair *idmap;
  3084. bool ret = false;
  3085. int i;
  3086. idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
  3087. /* If we failed to allocate the idmap, just say it's not safe */
  3088. if (!idmap)
  3089. return false;
  3090. for (i = 0; i < MAX_BPF_REG; i++) {
  3091. if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
  3092. goto out_free;
  3093. }
  3094. for (i = 0; i < MAX_BPF_STACK; i++) {
  3095. if (old->stack_slot_type[i] == STACK_INVALID)
  3096. continue;
  3097. if (old->stack_slot_type[i] != cur->stack_slot_type[i])
  3098. /* Ex: old explored (safe) state has STACK_SPILL in
  3099. * this stack slot, but current has has STACK_MISC ->
  3100. * this verifier states are not equivalent,
  3101. * return false to continue verification of this path
  3102. */
  3103. goto out_free;
  3104. if (i % BPF_REG_SIZE)
  3105. continue;
  3106. if (old->stack_slot_type[i] != STACK_SPILL)
  3107. continue;
  3108. if (!regsafe(&old->spilled_regs[i / BPF_REG_SIZE],
  3109. &cur->spilled_regs[i / BPF_REG_SIZE],
  3110. idmap))
  3111. /* when explored and current stack slot are both storing
  3112. * spilled registers, check that stored pointers types
  3113. * are the same as well.
  3114. * Ex: explored safe path could have stored
  3115. * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
  3116. * but current path has stored:
  3117. * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
  3118. * such verifier states are not equivalent.
  3119. * return false to continue verification of this path
  3120. */
  3121. goto out_free;
  3122. else
  3123. continue;
  3124. }
  3125. ret = true;
  3126. out_free:
  3127. kfree(idmap);
  3128. return ret;
  3129. }
  3130. /* A write screens off any subsequent reads; but write marks come from the
  3131. * straight-line code between a state and its parent. When we arrive at a
  3132. * jump target (in the first iteration of the propagate_liveness() loop),
  3133. * we didn't arrive by the straight-line code, so read marks in state must
  3134. * propagate to parent regardless of state's write marks.
  3135. */
  3136. static bool do_propagate_liveness(const struct bpf_verifier_state *state,
  3137. struct bpf_verifier_state *parent)
  3138. {
  3139. bool writes = parent == state->parent; /* Observe write marks */
  3140. bool touched = false; /* any changes made? */
  3141. int i;
  3142. if (!parent)
  3143. return touched;
  3144. /* Propagate read liveness of registers... */
  3145. BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
  3146. /* We don't need to worry about FP liveness because it's read-only */
  3147. for (i = 0; i < BPF_REG_FP; i++) {
  3148. if (parent->regs[i].live & REG_LIVE_READ)
  3149. continue;
  3150. if (writes && (state->regs[i].live & REG_LIVE_WRITTEN))
  3151. continue;
  3152. if (state->regs[i].live & REG_LIVE_READ) {
  3153. parent->regs[i].live |= REG_LIVE_READ;
  3154. touched = true;
  3155. }
  3156. }
  3157. /* ... and stack slots */
  3158. for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++) {
  3159. if (parent->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL)
  3160. continue;
  3161. if (state->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL)
  3162. continue;
  3163. if (parent->spilled_regs[i].live & REG_LIVE_READ)
  3164. continue;
  3165. if (writes && (state->spilled_regs[i].live & REG_LIVE_WRITTEN))
  3166. continue;
  3167. if (state->spilled_regs[i].live & REG_LIVE_READ) {
  3168. parent->spilled_regs[i].live |= REG_LIVE_READ;
  3169. touched = true;
  3170. }
  3171. }
  3172. return touched;
  3173. }
  3174. /* "parent" is "a state from which we reach the current state", but initially
  3175. * it is not the state->parent (i.e. "the state whose straight-line code leads
  3176. * to the current state"), instead it is the state that happened to arrive at
  3177. * a (prunable) equivalent of the current state. See comment above
  3178. * do_propagate_liveness() for consequences of this.
  3179. * This function is just a more efficient way of calling mark_reg_read() or
  3180. * mark_stack_slot_read() on each reg in "parent" that is read in "state",
  3181. * though it requires that parent != state->parent in the call arguments.
  3182. */
  3183. static void propagate_liveness(const struct bpf_verifier_state *state,
  3184. struct bpf_verifier_state *parent)
  3185. {
  3186. while (do_propagate_liveness(state, parent)) {
  3187. /* Something changed, so we need to feed those changes onward */
  3188. state = parent;
  3189. parent = state->parent;
  3190. }
  3191. }
  3192. static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
  3193. {
  3194. struct bpf_verifier_state_list *new_sl;
  3195. struct bpf_verifier_state_list *sl;
  3196. int i;
  3197. sl = env->explored_states[insn_idx];
  3198. if (!sl)
  3199. /* this 'insn_idx' instruction wasn't marked, so we will not
  3200. * be doing state search here
  3201. */
  3202. return 0;
  3203. while (sl != STATE_LIST_MARK) {
  3204. if (states_equal(env, &sl->state, &env->cur_state)) {
  3205. /* reached equivalent register/stack state,
  3206. * prune the search.
  3207. * Registers read by the continuation are read by us.
  3208. * If we have any write marks in env->cur_state, they
  3209. * will prevent corresponding reads in the continuation
  3210. * from reaching our parent (an explored_state). Our
  3211. * own state will get the read marks recorded, but
  3212. * they'll be immediately forgotten as we're pruning
  3213. * this state and will pop a new one.
  3214. */
  3215. propagate_liveness(&sl->state, &env->cur_state);
  3216. return 1;
  3217. }
  3218. sl = sl->next;
  3219. }
  3220. /* there were no equivalent states, remember current one.
  3221. * technically the current state is not proven to be safe yet,
  3222. * but it will either reach bpf_exit (which means it's safe) or
  3223. * it will be rejected. Since there are no loops, we won't be
  3224. * seeing this 'insn_idx' instruction again on the way to bpf_exit
  3225. */
  3226. new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
  3227. if (!new_sl)
  3228. return -ENOMEM;
  3229. /* add new state to the head of linked list */
  3230. memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
  3231. new_sl->next = env->explored_states[insn_idx];
  3232. env->explored_states[insn_idx] = new_sl;
  3233. /* connect new state to parentage chain */
  3234. env->cur_state.parent = &new_sl->state;
  3235. /* clear write marks in current state: the writes we did are not writes
  3236. * our child did, so they don't screen off its reads from us.
  3237. * (There are no read marks in current state, because reads always mark
  3238. * their parent and current state never has children yet. Only
  3239. * explored_states can get read marks.)
  3240. */
  3241. for (i = 0; i < BPF_REG_FP; i++)
  3242. env->cur_state.regs[i].live = REG_LIVE_NONE;
  3243. for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++)
  3244. if (env->cur_state.stack_slot_type[i * BPF_REG_SIZE] == STACK_SPILL)
  3245. env->cur_state.spilled_regs[i].live = REG_LIVE_NONE;
  3246. return 0;
  3247. }
  3248. static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
  3249. int insn_idx, int prev_insn_idx)
  3250. {
  3251. if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
  3252. return 0;
  3253. return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
  3254. }
  3255. static int do_check(struct bpf_verifier_env *env)
  3256. {
  3257. struct bpf_verifier_state *state = &env->cur_state;
  3258. struct bpf_insn *insns = env->prog->insnsi;
  3259. struct bpf_reg_state *regs = state->regs;
  3260. int insn_cnt = env->prog->len;
  3261. int insn_idx, prev_insn_idx = 0;
  3262. int insn_processed = 0;
  3263. bool do_print_state = false;
  3264. init_reg_state(regs);
  3265. state->parent = NULL;
  3266. insn_idx = 0;
  3267. for (;;) {
  3268. struct bpf_insn *insn;
  3269. u8 class;
  3270. int err;
  3271. if (insn_idx >= insn_cnt) {
  3272. verbose("invalid insn idx %d insn_cnt %d\n",
  3273. insn_idx, insn_cnt);
  3274. return -EFAULT;
  3275. }
  3276. insn = &insns[insn_idx];
  3277. class = BPF_CLASS(insn->code);
  3278. if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
  3279. verbose("BPF program is too large. Processed %d insn\n",
  3280. insn_processed);
  3281. return -E2BIG;
  3282. }
  3283. err = is_state_visited(env, insn_idx);
  3284. if (err < 0)
  3285. return err;
  3286. if (err == 1) {
  3287. /* found equivalent state, can prune the search */
  3288. if (log_level) {
  3289. if (do_print_state)
  3290. verbose("\nfrom %d to %d: safe\n",
  3291. prev_insn_idx, insn_idx);
  3292. else
  3293. verbose("%d: safe\n", insn_idx);
  3294. }
  3295. goto process_bpf_exit;
  3296. }
  3297. if (need_resched())
  3298. cond_resched();
  3299. if (log_level > 1 || (log_level && do_print_state)) {
  3300. if (log_level > 1)
  3301. verbose("%d:", insn_idx);
  3302. else
  3303. verbose("\nfrom %d to %d:",
  3304. prev_insn_idx, insn_idx);
  3305. print_verifier_state(&env->cur_state);
  3306. do_print_state = false;
  3307. }
  3308. if (log_level) {
  3309. verbose("%d: ", insn_idx);
  3310. print_bpf_insn(env, insn);
  3311. }
  3312. err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
  3313. if (err)
  3314. return err;
  3315. if (class == BPF_ALU || class == BPF_ALU64) {
  3316. err = check_alu_op(env, insn);
  3317. if (err)
  3318. return err;
  3319. } else if (class == BPF_LDX) {
  3320. enum bpf_reg_type *prev_src_type, src_reg_type;
  3321. /* check for reserved fields is already done */
  3322. /* check src operand */
  3323. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  3324. if (err)
  3325. return err;
  3326. err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
  3327. if (err)
  3328. return err;
  3329. src_reg_type = regs[insn->src_reg].type;
  3330. /* check that memory (src_reg + off) is readable,
  3331. * the state of dst_reg will be updated by this func
  3332. */
  3333. err = check_mem_access(env, insn_idx, insn->src_reg, insn->off,
  3334. BPF_SIZE(insn->code), BPF_READ,
  3335. insn->dst_reg);
  3336. if (err)
  3337. return err;
  3338. prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
  3339. if (*prev_src_type == NOT_INIT) {
  3340. /* saw a valid insn
  3341. * dst_reg = *(u32 *)(src_reg + off)
  3342. * save type to validate intersecting paths
  3343. */
  3344. *prev_src_type = src_reg_type;
  3345. } else if (src_reg_type != *prev_src_type &&
  3346. (src_reg_type == PTR_TO_CTX ||
  3347. *prev_src_type == PTR_TO_CTX)) {
  3348. /* ABuser program is trying to use the same insn
  3349. * dst_reg = *(u32*) (src_reg + off)
  3350. * with different pointer types:
  3351. * src_reg == ctx in one branch and
  3352. * src_reg == stack|map in some other branch.
  3353. * Reject it.
  3354. */
  3355. verbose("same insn cannot be used with different pointers\n");
  3356. return -EINVAL;
  3357. }
  3358. } else if (class == BPF_STX) {
  3359. enum bpf_reg_type *prev_dst_type, dst_reg_type;
  3360. if (BPF_MODE(insn->code) == BPF_XADD) {
  3361. err = check_xadd(env, insn_idx, insn);
  3362. if (err)
  3363. return err;
  3364. insn_idx++;
  3365. continue;
  3366. }
  3367. /* check src1 operand */
  3368. err = check_reg_arg(env, insn->src_reg, SRC_OP);
  3369. if (err)
  3370. return err;
  3371. /* check src2 operand */
  3372. err = check_reg_arg(env, insn->dst_reg, SRC_OP);
  3373. if (err)
  3374. return err;
  3375. dst_reg_type = regs[insn->dst_reg].type;
  3376. /* check that memory (dst_reg + off) is writeable */
  3377. err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
  3378. BPF_SIZE(insn->code), BPF_WRITE,
  3379. insn->src_reg);
  3380. if (err)
  3381. return err;
  3382. prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
  3383. if (*prev_dst_type == NOT_INIT) {
  3384. *prev_dst_type = dst_reg_type;
  3385. } else if (dst_reg_type != *prev_dst_type &&
  3386. (dst_reg_type == PTR_TO_CTX ||
  3387. *prev_dst_type == PTR_TO_CTX)) {
  3388. verbose("same insn cannot be used with different pointers\n");
  3389. return -EINVAL;
  3390. }
  3391. } else if (class == BPF_ST) {
  3392. if (BPF_MODE(insn->code) != BPF_MEM ||
  3393. insn->src_reg != BPF_REG_0) {
  3394. verbose("BPF_ST uses reserved fields\n");
  3395. return -EINVAL;
  3396. }
  3397. /* check src operand */
  3398. err = check_reg_arg(env, insn->dst_reg, SRC_OP);
  3399. if (err)
  3400. return err;
  3401. /* check that memory (dst_reg + off) is writeable */
  3402. err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
  3403. BPF_SIZE(insn->code), BPF_WRITE,
  3404. -1);
  3405. if (err)
  3406. return err;
  3407. } else if (class == BPF_JMP) {
  3408. u8 opcode = BPF_OP(insn->code);
  3409. if (opcode == BPF_CALL) {
  3410. if (BPF_SRC(insn->code) != BPF_K ||
  3411. insn->off != 0 ||
  3412. insn->src_reg != BPF_REG_0 ||
  3413. insn->dst_reg != BPF_REG_0) {
  3414. verbose("BPF_CALL uses reserved fields\n");
  3415. return -EINVAL;
  3416. }
  3417. err = check_call(env, insn->imm, insn_idx);
  3418. if (err)
  3419. return err;
  3420. } else if (opcode == BPF_JA) {
  3421. if (BPF_SRC(insn->code) != BPF_K ||
  3422. insn->imm != 0 ||
  3423. insn->src_reg != BPF_REG_0 ||
  3424. insn->dst_reg != BPF_REG_0) {
  3425. verbose("BPF_JA uses reserved fields\n");
  3426. return -EINVAL;
  3427. }
  3428. insn_idx += insn->off + 1;
  3429. continue;
  3430. } else if (opcode == BPF_EXIT) {
  3431. if (BPF_SRC(insn->code) != BPF_K ||
  3432. insn->imm != 0 ||
  3433. insn->src_reg != BPF_REG_0 ||
  3434. insn->dst_reg != BPF_REG_0) {
  3435. verbose("BPF_EXIT uses reserved fields\n");
  3436. return -EINVAL;
  3437. }
  3438. /* eBPF calling convetion is such that R0 is used
  3439. * to return the value from eBPF program.
  3440. * Make sure that it's readable at this time
  3441. * of bpf_exit, which means that program wrote
  3442. * something into it earlier
  3443. */
  3444. err = check_reg_arg(env, BPF_REG_0, SRC_OP);
  3445. if (err)
  3446. return err;
  3447. if (is_pointer_value(env, BPF_REG_0)) {
  3448. verbose("R0 leaks addr as return value\n");
  3449. return -EACCES;
  3450. }
  3451. process_bpf_exit:
  3452. insn_idx = pop_stack(env, &prev_insn_idx);
  3453. if (insn_idx < 0) {
  3454. break;
  3455. } else {
  3456. do_print_state = true;
  3457. continue;
  3458. }
  3459. } else {
  3460. err = check_cond_jmp_op(env, insn, &insn_idx);
  3461. if (err)
  3462. return err;
  3463. }
  3464. } else if (class == BPF_LD) {
  3465. u8 mode = BPF_MODE(insn->code);
  3466. if (mode == BPF_ABS || mode == BPF_IND) {
  3467. err = check_ld_abs(env, insn);
  3468. if (err)
  3469. return err;
  3470. } else if (mode == BPF_IMM) {
  3471. err = check_ld_imm(env, insn);
  3472. if (err)
  3473. return err;
  3474. insn_idx++;
  3475. } else {
  3476. verbose("invalid BPF_LD mode\n");
  3477. return -EINVAL;
  3478. }
  3479. } else {
  3480. verbose("unknown insn class %d\n", class);
  3481. return -EINVAL;
  3482. }
  3483. insn_idx++;
  3484. }
  3485. verbose("processed %d insns, stack depth %d\n",
  3486. insn_processed, env->prog->aux->stack_depth);
  3487. return 0;
  3488. }
  3489. static int check_map_prealloc(struct bpf_map *map)
  3490. {
  3491. return (map->map_type != BPF_MAP_TYPE_HASH &&
  3492. map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
  3493. map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
  3494. !(map->map_flags & BPF_F_NO_PREALLOC);
  3495. }
  3496. static int check_map_prog_compatibility(struct bpf_map *map,
  3497. struct bpf_prog *prog)
  3498. {
  3499. /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
  3500. * preallocated hash maps, since doing memory allocation
  3501. * in overflow_handler can crash depending on where nmi got
  3502. * triggered.
  3503. */
  3504. if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
  3505. if (!check_map_prealloc(map)) {
  3506. verbose("perf_event programs can only use preallocated hash map\n");
  3507. return -EINVAL;
  3508. }
  3509. if (map->inner_map_meta &&
  3510. !check_map_prealloc(map->inner_map_meta)) {
  3511. verbose("perf_event programs can only use preallocated inner hash map\n");
  3512. return -EINVAL;
  3513. }
  3514. }
  3515. return 0;
  3516. }
  3517. /* look for pseudo eBPF instructions that access map FDs and
  3518. * replace them with actual map pointers
  3519. */
  3520. static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
  3521. {
  3522. struct bpf_insn *insn = env->prog->insnsi;
  3523. int insn_cnt = env->prog->len;
  3524. int i, j, err;
  3525. err = bpf_prog_calc_tag(env->prog);
  3526. if (err)
  3527. return err;
  3528. for (i = 0; i < insn_cnt; i++, insn++) {
  3529. if (BPF_CLASS(insn->code) == BPF_LDX &&
  3530. (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
  3531. verbose("BPF_LDX uses reserved fields\n");
  3532. return -EINVAL;
  3533. }
  3534. if (BPF_CLASS(insn->code) == BPF_STX &&
  3535. ((BPF_MODE(insn->code) != BPF_MEM &&
  3536. BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
  3537. verbose("BPF_STX uses reserved fields\n");
  3538. return -EINVAL;
  3539. }
  3540. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  3541. struct bpf_map *map;
  3542. struct fd f;
  3543. if (i == insn_cnt - 1 || insn[1].code != 0 ||
  3544. insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
  3545. insn[1].off != 0) {
  3546. verbose("invalid bpf_ld_imm64 insn\n");
  3547. return -EINVAL;
  3548. }
  3549. if (insn->src_reg == 0)
  3550. /* valid generic load 64-bit imm */
  3551. goto next_insn;
  3552. if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
  3553. verbose("unrecognized bpf_ld_imm64 insn\n");
  3554. return -EINVAL;
  3555. }
  3556. f = fdget(insn->imm);
  3557. map = __bpf_map_get(f);
  3558. if (IS_ERR(map)) {
  3559. verbose("fd %d is not pointing to valid bpf_map\n",
  3560. insn->imm);
  3561. return PTR_ERR(map);
  3562. }
  3563. err = check_map_prog_compatibility(map, env->prog);
  3564. if (err) {
  3565. fdput(f);
  3566. return err;
  3567. }
  3568. /* store map pointer inside BPF_LD_IMM64 instruction */
  3569. insn[0].imm = (u32) (unsigned long) map;
  3570. insn[1].imm = ((u64) (unsigned long) map) >> 32;
  3571. /* check whether we recorded this map already */
  3572. for (j = 0; j < env->used_map_cnt; j++)
  3573. if (env->used_maps[j] == map) {
  3574. fdput(f);
  3575. goto next_insn;
  3576. }
  3577. if (env->used_map_cnt >= MAX_USED_MAPS) {
  3578. fdput(f);
  3579. return -E2BIG;
  3580. }
  3581. /* hold the map. If the program is rejected by verifier,
  3582. * the map will be released by release_maps() or it
  3583. * will be used by the valid program until it's unloaded
  3584. * and all maps are released in free_bpf_prog_info()
  3585. */
  3586. map = bpf_map_inc(map, false);
  3587. if (IS_ERR(map)) {
  3588. fdput(f);
  3589. return PTR_ERR(map);
  3590. }
  3591. env->used_maps[env->used_map_cnt++] = map;
  3592. fdput(f);
  3593. next_insn:
  3594. insn++;
  3595. i++;
  3596. }
  3597. }
  3598. /* now all pseudo BPF_LD_IMM64 instructions load valid
  3599. * 'struct bpf_map *' into a register instead of user map_fd.
  3600. * These pointers will be used later by verifier to validate map access.
  3601. */
  3602. return 0;
  3603. }
  3604. /* drop refcnt of maps used by the rejected program */
  3605. static void release_maps(struct bpf_verifier_env *env)
  3606. {
  3607. int i;
  3608. for (i = 0; i < env->used_map_cnt; i++)
  3609. bpf_map_put(env->used_maps[i]);
  3610. }
  3611. /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
  3612. static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
  3613. {
  3614. struct bpf_insn *insn = env->prog->insnsi;
  3615. int insn_cnt = env->prog->len;
  3616. int i;
  3617. for (i = 0; i < insn_cnt; i++, insn++)
  3618. if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
  3619. insn->src_reg = 0;
  3620. }
  3621. /* single env->prog->insni[off] instruction was replaced with the range
  3622. * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
  3623. * [0, off) and [off, end) to new locations, so the patched range stays zero
  3624. */
  3625. static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
  3626. u32 off, u32 cnt)
  3627. {
  3628. struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
  3629. if (cnt == 1)
  3630. return 0;
  3631. new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len);
  3632. if (!new_data)
  3633. return -ENOMEM;
  3634. memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
  3635. memcpy(new_data + off + cnt - 1, old_data + off,
  3636. sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
  3637. env->insn_aux_data = new_data;
  3638. vfree(old_data);
  3639. return 0;
  3640. }
  3641. static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
  3642. const struct bpf_insn *patch, u32 len)
  3643. {
  3644. struct bpf_prog *new_prog;
  3645. new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
  3646. if (!new_prog)
  3647. return NULL;
  3648. if (adjust_insn_aux_data(env, new_prog->len, off, len))
  3649. return NULL;
  3650. return new_prog;
  3651. }
  3652. /* convert load instructions that access fields of 'struct __sk_buff'
  3653. * into sequence of instructions that access fields of 'struct sk_buff'
  3654. */
  3655. static int convert_ctx_accesses(struct bpf_verifier_env *env)
  3656. {
  3657. const struct bpf_verifier_ops *ops = env->prog->aux->ops;
  3658. int i, cnt, size, ctx_field_size, delta = 0;
  3659. const int insn_cnt = env->prog->len;
  3660. struct bpf_insn insn_buf[16], *insn;
  3661. struct bpf_prog *new_prog;
  3662. enum bpf_access_type type;
  3663. bool is_narrower_load;
  3664. u32 target_size;
  3665. if (ops->gen_prologue) {
  3666. cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
  3667. env->prog);
  3668. if (cnt >= ARRAY_SIZE(insn_buf)) {
  3669. verbose("bpf verifier is misconfigured\n");
  3670. return -EINVAL;
  3671. } else if (cnt) {
  3672. new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
  3673. if (!new_prog)
  3674. return -ENOMEM;
  3675. env->prog = new_prog;
  3676. delta += cnt - 1;
  3677. }
  3678. }
  3679. if (!ops->convert_ctx_access)
  3680. return 0;
  3681. insn = env->prog->insnsi + delta;
  3682. for (i = 0; i < insn_cnt; i++, insn++) {
  3683. if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
  3684. insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
  3685. insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
  3686. insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
  3687. type = BPF_READ;
  3688. else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
  3689. insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
  3690. insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
  3691. insn->code == (BPF_STX | BPF_MEM | BPF_DW))
  3692. type = BPF_WRITE;
  3693. else
  3694. continue;
  3695. if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX)
  3696. continue;
  3697. ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
  3698. size = BPF_LDST_BYTES(insn);
  3699. /* If the read access is a narrower load of the field,
  3700. * convert to a 4/8-byte load, to minimum program type specific
  3701. * convert_ctx_access changes. If conversion is successful,
  3702. * we will apply proper mask to the result.
  3703. */
  3704. is_narrower_load = size < ctx_field_size;
  3705. if (is_narrower_load) {
  3706. u32 off = insn->off;
  3707. u8 size_code;
  3708. if (type == BPF_WRITE) {
  3709. verbose("bpf verifier narrow ctx access misconfigured\n");
  3710. return -EINVAL;
  3711. }
  3712. size_code = BPF_H;
  3713. if (ctx_field_size == 4)
  3714. size_code = BPF_W;
  3715. else if (ctx_field_size == 8)
  3716. size_code = BPF_DW;
  3717. insn->off = off & ~(ctx_field_size - 1);
  3718. insn->code = BPF_LDX | BPF_MEM | size_code;
  3719. }
  3720. target_size = 0;
  3721. cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog,
  3722. &target_size);
  3723. if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
  3724. (ctx_field_size && !target_size)) {
  3725. verbose("bpf verifier is misconfigured\n");
  3726. return -EINVAL;
  3727. }
  3728. if (is_narrower_load && size < target_size) {
  3729. if (ctx_field_size <= 4)
  3730. insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
  3731. (1 << size * 8) - 1);
  3732. else
  3733. insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
  3734. (1 << size * 8) - 1);
  3735. }
  3736. new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
  3737. if (!new_prog)
  3738. return -ENOMEM;
  3739. delta += cnt - 1;
  3740. /* keep walking new program and skip insns we just inserted */
  3741. env->prog = new_prog;
  3742. insn = new_prog->insnsi + i + delta;
  3743. }
  3744. return 0;
  3745. }
  3746. /* fixup insn->imm field of bpf_call instructions
  3747. * and inline eligible helpers as explicit sequence of BPF instructions
  3748. *
  3749. * this function is called after eBPF program passed verification
  3750. */
  3751. static int fixup_bpf_calls(struct bpf_verifier_env *env)
  3752. {
  3753. struct bpf_prog *prog = env->prog;
  3754. struct bpf_insn *insn = prog->insnsi;
  3755. const struct bpf_func_proto *fn;
  3756. const int insn_cnt = prog->len;
  3757. struct bpf_insn insn_buf[16];
  3758. struct bpf_prog *new_prog;
  3759. struct bpf_map *map_ptr;
  3760. int i, cnt, delta = 0;
  3761. for (i = 0; i < insn_cnt; i++, insn++) {
  3762. if (insn->code != (BPF_JMP | BPF_CALL))
  3763. continue;
  3764. if (insn->imm == BPF_FUNC_get_route_realm)
  3765. prog->dst_needed = 1;
  3766. if (insn->imm == BPF_FUNC_get_prandom_u32)
  3767. bpf_user_rnd_init_once();
  3768. if (insn->imm == BPF_FUNC_tail_call) {
  3769. /* If we tail call into other programs, we
  3770. * cannot make any assumptions since they can
  3771. * be replaced dynamically during runtime in
  3772. * the program array.
  3773. */
  3774. prog->cb_access = 1;
  3775. env->prog->aux->stack_depth = MAX_BPF_STACK;
  3776. /* mark bpf_tail_call as different opcode to avoid
  3777. * conditional branch in the interpeter for every normal
  3778. * call and to prevent accidental JITing by JIT compiler
  3779. * that doesn't support bpf_tail_call yet
  3780. */
  3781. insn->imm = 0;
  3782. insn->code = BPF_JMP | BPF_TAIL_CALL;
  3783. continue;
  3784. }
  3785. /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
  3786. * handlers are currently limited to 64 bit only.
  3787. */
  3788. if (ebpf_jit_enabled() && BITS_PER_LONG == 64 &&
  3789. insn->imm == BPF_FUNC_map_lookup_elem) {
  3790. map_ptr = env->insn_aux_data[i + delta].map_ptr;
  3791. if (map_ptr == BPF_MAP_PTR_POISON ||
  3792. !map_ptr->ops->map_gen_lookup)
  3793. goto patch_call_imm;
  3794. cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
  3795. if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
  3796. verbose("bpf verifier is misconfigured\n");
  3797. return -EINVAL;
  3798. }
  3799. new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
  3800. cnt);
  3801. if (!new_prog)
  3802. return -ENOMEM;
  3803. delta += cnt - 1;
  3804. /* keep walking new program and skip insns we just inserted */
  3805. env->prog = prog = new_prog;
  3806. insn = new_prog->insnsi + i + delta;
  3807. continue;
  3808. }
  3809. if (insn->imm == BPF_FUNC_redirect_map) {
  3810. /* Note, we cannot use prog directly as imm as subsequent
  3811. * rewrites would still change the prog pointer. The only
  3812. * stable address we can use is aux, which also works with
  3813. * prog clones during blinding.
  3814. */
  3815. u64 addr = (unsigned long)prog->aux;
  3816. struct bpf_insn r4_ld[] = {
  3817. BPF_LD_IMM64(BPF_REG_4, addr),
  3818. *insn,
  3819. };
  3820. cnt = ARRAY_SIZE(r4_ld);
  3821. new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt);
  3822. if (!new_prog)
  3823. return -ENOMEM;
  3824. delta += cnt - 1;
  3825. env->prog = prog = new_prog;
  3826. insn = new_prog->insnsi + i + delta;
  3827. }
  3828. patch_call_imm:
  3829. fn = prog->aux->ops->get_func_proto(insn->imm);
  3830. /* all functions that have prototype and verifier allowed
  3831. * programs to call them, must be real in-kernel functions
  3832. */
  3833. if (!fn->func) {
  3834. verbose("kernel subsystem misconfigured func %s#%d\n",
  3835. func_id_name(insn->imm), insn->imm);
  3836. return -EFAULT;
  3837. }
  3838. insn->imm = fn->func - __bpf_call_base;
  3839. }
  3840. return 0;
  3841. }
  3842. static void free_states(struct bpf_verifier_env *env)
  3843. {
  3844. struct bpf_verifier_state_list *sl, *sln;
  3845. int i;
  3846. if (!env->explored_states)
  3847. return;
  3848. for (i = 0; i < env->prog->len; i++) {
  3849. sl = env->explored_states[i];
  3850. if (sl)
  3851. while (sl != STATE_LIST_MARK) {
  3852. sln = sl->next;
  3853. kfree(sl);
  3854. sl = sln;
  3855. }
  3856. }
  3857. kfree(env->explored_states);
  3858. }
  3859. int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
  3860. {
  3861. char __user *log_ubuf = NULL;
  3862. struct bpf_verifier_env *env;
  3863. int ret = -EINVAL;
  3864. /* 'struct bpf_verifier_env' can be global, but since it's not small,
  3865. * allocate/free it every time bpf_check() is called
  3866. */
  3867. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  3868. if (!env)
  3869. return -ENOMEM;
  3870. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  3871. (*prog)->len);
  3872. ret = -ENOMEM;
  3873. if (!env->insn_aux_data)
  3874. goto err_free_env;
  3875. env->prog = *prog;
  3876. /* grab the mutex to protect few globals used by verifier */
  3877. mutex_lock(&bpf_verifier_lock);
  3878. if (attr->log_level || attr->log_buf || attr->log_size) {
  3879. /* user requested verbose verifier output
  3880. * and supplied buffer to store the verification trace
  3881. */
  3882. log_level = attr->log_level;
  3883. log_ubuf = (char __user *) (unsigned long) attr->log_buf;
  3884. log_size = attr->log_size;
  3885. log_len = 0;
  3886. ret = -EINVAL;
  3887. /* log_* values have to be sane */
  3888. if (log_size < 128 || log_size > UINT_MAX >> 8 ||
  3889. log_level == 0 || log_ubuf == NULL)
  3890. goto err_unlock;
  3891. ret = -ENOMEM;
  3892. log_buf = vmalloc(log_size);
  3893. if (!log_buf)
  3894. goto err_unlock;
  3895. } else {
  3896. log_level = 0;
  3897. }
  3898. env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
  3899. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  3900. env->strict_alignment = true;
  3901. ret = replace_map_fd_with_map_ptr(env);
  3902. if (ret < 0)
  3903. goto skip_full_check;
  3904. env->explored_states = kcalloc(env->prog->len,
  3905. sizeof(struct bpf_verifier_state_list *),
  3906. GFP_USER);
  3907. ret = -ENOMEM;
  3908. if (!env->explored_states)
  3909. goto skip_full_check;
  3910. ret = check_cfg(env);
  3911. if (ret < 0)
  3912. goto skip_full_check;
  3913. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  3914. ret = do_check(env);
  3915. skip_full_check:
  3916. while (pop_stack(env, NULL) >= 0);
  3917. free_states(env);
  3918. if (ret == 0)
  3919. /* program is valid, convert *(u32*)(ctx + off) accesses */
  3920. ret = convert_ctx_accesses(env);
  3921. if (ret == 0)
  3922. ret = fixup_bpf_calls(env);
  3923. if (log_level && log_len >= log_size - 1) {
  3924. BUG_ON(log_len >= log_size);
  3925. /* verifier log exceeded user supplied buffer */
  3926. ret = -ENOSPC;
  3927. /* fall through to return what was recorded */
  3928. }
  3929. /* copy verifier log back to user space including trailing zero */
  3930. if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
  3931. ret = -EFAULT;
  3932. goto free_log_buf;
  3933. }
  3934. if (ret == 0 && env->used_map_cnt) {
  3935. /* if program passed verifier, update used_maps in bpf_prog_info */
  3936. env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
  3937. sizeof(env->used_maps[0]),
  3938. GFP_KERNEL);
  3939. if (!env->prog->aux->used_maps) {
  3940. ret = -ENOMEM;
  3941. goto free_log_buf;
  3942. }
  3943. memcpy(env->prog->aux->used_maps, env->used_maps,
  3944. sizeof(env->used_maps[0]) * env->used_map_cnt);
  3945. env->prog->aux->used_map_cnt = env->used_map_cnt;
  3946. /* program is valid. Convert pseudo bpf_ld_imm64 into generic
  3947. * bpf_ld_imm64 instructions
  3948. */
  3949. convert_pseudo_ld_imm64(env);
  3950. }
  3951. free_log_buf:
  3952. if (log_level)
  3953. vfree(log_buf);
  3954. if (!env->prog->aux->used_maps)
  3955. /* if we didn't copy map pointers into bpf_prog_info, release
  3956. * them now. Otherwise free_bpf_prog_info() will release them.
  3957. */
  3958. release_maps(env);
  3959. *prog = env->prog;
  3960. err_unlock:
  3961. mutex_unlock(&bpf_verifier_lock);
  3962. vfree(env->insn_aux_data);
  3963. err_free_env:
  3964. kfree(env);
  3965. return ret;
  3966. }
  3967. int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
  3968. void *priv)
  3969. {
  3970. struct bpf_verifier_env *env;
  3971. int ret;
  3972. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  3973. if (!env)
  3974. return -ENOMEM;
  3975. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  3976. prog->len);
  3977. ret = -ENOMEM;
  3978. if (!env->insn_aux_data)
  3979. goto err_free_env;
  3980. env->prog = prog;
  3981. env->analyzer_ops = ops;
  3982. env->analyzer_priv = priv;
  3983. /* grab the mutex to protect few globals used by verifier */
  3984. mutex_lock(&bpf_verifier_lock);
  3985. log_level = 0;
  3986. env->strict_alignment = false;
  3987. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  3988. env->strict_alignment = true;
  3989. env->explored_states = kcalloc(env->prog->len,
  3990. sizeof(struct bpf_verifier_state_list *),
  3991. GFP_KERNEL);
  3992. ret = -ENOMEM;
  3993. if (!env->explored_states)
  3994. goto skip_full_check;
  3995. ret = check_cfg(env);
  3996. if (ret < 0)
  3997. goto skip_full_check;
  3998. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  3999. ret = do_check(env);
  4000. skip_full_check:
  4001. while (pop_stack(env, NULL) >= 0);
  4002. free_states(env);
  4003. mutex_unlock(&bpf_verifier_lock);
  4004. vfree(env->insn_aux_data);
  4005. err_free_env:
  4006. kfree(env);
  4007. return ret;
  4008. }
  4009. EXPORT_SYMBOL_GPL(bpf_analyzer);