verifier.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426
  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 32k 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 UNKNOWN_VALUE type, which
  64. * means the register has some value, but it's not a valid pointer.
  65. * (like pointer plus pointer becomes UNKNOWN_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, FRAME_PTR. 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 65536
  141. #define BPF_COMPLEXITY_LIMIT_STACK 1024
  142. struct bpf_call_arg_meta {
  143. struct bpf_map *map_ptr;
  144. bool raw_mode;
  145. bool pkt_access;
  146. int regno;
  147. int access_size;
  148. };
  149. /* verbose verifier prints what it's seeing
  150. * bpf_check() is called under lock, so no race to access these global vars
  151. */
  152. static u32 log_level, log_size, log_len;
  153. static char *log_buf;
  154. static DEFINE_MUTEX(bpf_verifier_lock);
  155. /* log_level controls verbosity level of eBPF verifier.
  156. * verbose() is used to dump the verification trace to the log, so the user
  157. * can figure out what's wrong with the program
  158. */
  159. static __printf(1, 2) void verbose(const char *fmt, ...)
  160. {
  161. va_list args;
  162. if (log_level == 0 || log_len >= log_size - 1)
  163. return;
  164. va_start(args, fmt);
  165. log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
  166. va_end(args);
  167. }
  168. /* string representation of 'enum bpf_reg_type' */
  169. static const char * const reg_type_str[] = {
  170. [NOT_INIT] = "?",
  171. [UNKNOWN_VALUE] = "inv",
  172. [PTR_TO_CTX] = "ctx",
  173. [CONST_PTR_TO_MAP] = "map_ptr",
  174. [PTR_TO_MAP_VALUE] = "map_value",
  175. [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
  176. [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj",
  177. [FRAME_PTR] = "fp",
  178. [PTR_TO_STACK] = "fp",
  179. [CONST_IMM] = "imm",
  180. [PTR_TO_PACKET] = "pkt",
  181. [PTR_TO_PACKET_END] = "pkt_end",
  182. };
  183. #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
  184. static const char * const func_id_str[] = {
  185. __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
  186. };
  187. #undef __BPF_FUNC_STR_FN
  188. static const char *func_id_name(int id)
  189. {
  190. BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
  191. if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
  192. return func_id_str[id];
  193. else
  194. return "unknown";
  195. }
  196. static void print_verifier_state(struct bpf_verifier_state *state)
  197. {
  198. struct bpf_reg_state *reg;
  199. enum bpf_reg_type t;
  200. int i;
  201. for (i = 0; i < MAX_BPF_REG; i++) {
  202. reg = &state->regs[i];
  203. t = reg->type;
  204. if (t == NOT_INIT)
  205. continue;
  206. verbose(" R%d=%s", i, reg_type_str[t]);
  207. if (t == CONST_IMM || t == PTR_TO_STACK)
  208. verbose("%lld", reg->imm);
  209. else if (t == PTR_TO_PACKET)
  210. verbose("(id=%d,off=%d,r=%d)",
  211. reg->id, reg->off, reg->range);
  212. else if (t == UNKNOWN_VALUE && reg->imm)
  213. verbose("%lld", reg->imm);
  214. else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
  215. t == PTR_TO_MAP_VALUE_OR_NULL ||
  216. t == PTR_TO_MAP_VALUE_ADJ)
  217. verbose("(ks=%d,vs=%d,id=%u)",
  218. reg->map_ptr->key_size,
  219. reg->map_ptr->value_size,
  220. reg->id);
  221. if (reg->min_value != BPF_REGISTER_MIN_RANGE)
  222. verbose(",min_value=%lld",
  223. (long long)reg->min_value);
  224. if (reg->max_value != BPF_REGISTER_MAX_RANGE)
  225. verbose(",max_value=%llu",
  226. (unsigned long long)reg->max_value);
  227. }
  228. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  229. if (state->stack_slot_type[i] == STACK_SPILL)
  230. verbose(" fp%d=%s", -MAX_BPF_STACK + i,
  231. reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
  232. }
  233. verbose("\n");
  234. }
  235. static const char *const bpf_class_string[] = {
  236. [BPF_LD] = "ld",
  237. [BPF_LDX] = "ldx",
  238. [BPF_ST] = "st",
  239. [BPF_STX] = "stx",
  240. [BPF_ALU] = "alu",
  241. [BPF_JMP] = "jmp",
  242. [BPF_RET] = "BUG",
  243. [BPF_ALU64] = "alu64",
  244. };
  245. static const char *const bpf_alu_string[16] = {
  246. [BPF_ADD >> 4] = "+=",
  247. [BPF_SUB >> 4] = "-=",
  248. [BPF_MUL >> 4] = "*=",
  249. [BPF_DIV >> 4] = "/=",
  250. [BPF_OR >> 4] = "|=",
  251. [BPF_AND >> 4] = "&=",
  252. [BPF_LSH >> 4] = "<<=",
  253. [BPF_RSH >> 4] = ">>=",
  254. [BPF_NEG >> 4] = "neg",
  255. [BPF_MOD >> 4] = "%=",
  256. [BPF_XOR >> 4] = "^=",
  257. [BPF_MOV >> 4] = "=",
  258. [BPF_ARSH >> 4] = "s>>=",
  259. [BPF_END >> 4] = "endian",
  260. };
  261. static const char *const bpf_ldst_string[] = {
  262. [BPF_W >> 3] = "u32",
  263. [BPF_H >> 3] = "u16",
  264. [BPF_B >> 3] = "u8",
  265. [BPF_DW >> 3] = "u64",
  266. };
  267. static const char *const bpf_jmp_string[16] = {
  268. [BPF_JA >> 4] = "jmp",
  269. [BPF_JEQ >> 4] = "==",
  270. [BPF_JGT >> 4] = ">",
  271. [BPF_JGE >> 4] = ">=",
  272. [BPF_JSET >> 4] = "&",
  273. [BPF_JNE >> 4] = "!=",
  274. [BPF_JSGT >> 4] = "s>",
  275. [BPF_JSGE >> 4] = "s>=",
  276. [BPF_CALL >> 4] = "call",
  277. [BPF_EXIT >> 4] = "exit",
  278. };
  279. static void print_bpf_insn(struct bpf_insn *insn)
  280. {
  281. u8 class = BPF_CLASS(insn->code);
  282. if (class == BPF_ALU || class == BPF_ALU64) {
  283. if (BPF_SRC(insn->code) == BPF_X)
  284. verbose("(%02x) %sr%d %s %sr%d\n",
  285. insn->code, class == BPF_ALU ? "(u32) " : "",
  286. insn->dst_reg,
  287. bpf_alu_string[BPF_OP(insn->code) >> 4],
  288. class == BPF_ALU ? "(u32) " : "",
  289. insn->src_reg);
  290. else
  291. verbose("(%02x) %sr%d %s %s%d\n",
  292. insn->code, class == BPF_ALU ? "(u32) " : "",
  293. insn->dst_reg,
  294. bpf_alu_string[BPF_OP(insn->code) >> 4],
  295. class == BPF_ALU ? "(u32) " : "",
  296. insn->imm);
  297. } else if (class == BPF_STX) {
  298. if (BPF_MODE(insn->code) == BPF_MEM)
  299. verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
  300. insn->code,
  301. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  302. insn->dst_reg,
  303. insn->off, insn->src_reg);
  304. else if (BPF_MODE(insn->code) == BPF_XADD)
  305. verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
  306. insn->code,
  307. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  308. insn->dst_reg, insn->off,
  309. insn->src_reg);
  310. else
  311. verbose("BUG_%02x\n", insn->code);
  312. } else if (class == BPF_ST) {
  313. if (BPF_MODE(insn->code) != BPF_MEM) {
  314. verbose("BUG_st_%02x\n", insn->code);
  315. return;
  316. }
  317. verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
  318. insn->code,
  319. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  320. insn->dst_reg,
  321. insn->off, insn->imm);
  322. } else if (class == BPF_LDX) {
  323. if (BPF_MODE(insn->code) != BPF_MEM) {
  324. verbose("BUG_ldx_%02x\n", insn->code);
  325. return;
  326. }
  327. verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
  328. insn->code, insn->dst_reg,
  329. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  330. insn->src_reg, insn->off);
  331. } else if (class == BPF_LD) {
  332. if (BPF_MODE(insn->code) == BPF_ABS) {
  333. verbose("(%02x) r0 = *(%s *)skb[%d]\n",
  334. insn->code,
  335. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  336. insn->imm);
  337. } else if (BPF_MODE(insn->code) == BPF_IND) {
  338. verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
  339. insn->code,
  340. bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
  341. insn->src_reg, insn->imm);
  342. } else if (BPF_MODE(insn->code) == BPF_IMM) {
  343. verbose("(%02x) r%d = 0x%x\n",
  344. insn->code, insn->dst_reg, insn->imm);
  345. } else {
  346. verbose("BUG_ld_%02x\n", insn->code);
  347. return;
  348. }
  349. } else if (class == BPF_JMP) {
  350. u8 opcode = BPF_OP(insn->code);
  351. if (opcode == BPF_CALL) {
  352. verbose("(%02x) call %s#%d\n", insn->code,
  353. func_id_name(insn->imm), insn->imm);
  354. } else if (insn->code == (BPF_JMP | BPF_JA)) {
  355. verbose("(%02x) goto pc%+d\n",
  356. insn->code, insn->off);
  357. } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
  358. verbose("(%02x) exit\n", insn->code);
  359. } else if (BPF_SRC(insn->code) == BPF_X) {
  360. verbose("(%02x) if r%d %s r%d goto pc%+d\n",
  361. insn->code, insn->dst_reg,
  362. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  363. insn->src_reg, insn->off);
  364. } else {
  365. verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
  366. insn->code, insn->dst_reg,
  367. bpf_jmp_string[BPF_OP(insn->code) >> 4],
  368. insn->imm, insn->off);
  369. }
  370. } else {
  371. verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
  372. }
  373. }
  374. static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
  375. {
  376. struct bpf_verifier_stack_elem *elem;
  377. int insn_idx;
  378. if (env->head == NULL)
  379. return -1;
  380. memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
  381. insn_idx = env->head->insn_idx;
  382. if (prev_insn_idx)
  383. *prev_insn_idx = env->head->prev_insn_idx;
  384. elem = env->head->next;
  385. kfree(env->head);
  386. env->head = elem;
  387. env->stack_size--;
  388. return insn_idx;
  389. }
  390. static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
  391. int insn_idx, int prev_insn_idx)
  392. {
  393. struct bpf_verifier_stack_elem *elem;
  394. elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
  395. if (!elem)
  396. goto err;
  397. memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
  398. elem->insn_idx = insn_idx;
  399. elem->prev_insn_idx = prev_insn_idx;
  400. elem->next = env->head;
  401. env->head = elem;
  402. env->stack_size++;
  403. if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
  404. verbose("BPF program is too complex\n");
  405. goto err;
  406. }
  407. return &elem->st;
  408. err:
  409. /* pop all elements and return */
  410. while (pop_stack(env, NULL) >= 0);
  411. return NULL;
  412. }
  413. #define CALLER_SAVED_REGS 6
  414. static const int caller_saved[CALLER_SAVED_REGS] = {
  415. BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
  416. };
  417. static void init_reg_state(struct bpf_reg_state *regs)
  418. {
  419. int i;
  420. for (i = 0; i < MAX_BPF_REG; i++) {
  421. regs[i].type = NOT_INIT;
  422. regs[i].imm = 0;
  423. regs[i].min_value = BPF_REGISTER_MIN_RANGE;
  424. regs[i].max_value = BPF_REGISTER_MAX_RANGE;
  425. }
  426. /* frame pointer */
  427. regs[BPF_REG_FP].type = FRAME_PTR;
  428. /* 1st arg to a function */
  429. regs[BPF_REG_1].type = PTR_TO_CTX;
  430. }
  431. static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
  432. {
  433. regs[regno].type = UNKNOWN_VALUE;
  434. regs[regno].id = 0;
  435. regs[regno].imm = 0;
  436. }
  437. static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
  438. {
  439. BUG_ON(regno >= MAX_BPF_REG);
  440. __mark_reg_unknown_value(regs, regno);
  441. }
  442. static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno)
  443. {
  444. regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
  445. regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
  446. }
  447. static void mark_reg_unknown_value_and_range(struct bpf_reg_state *regs,
  448. u32 regno)
  449. {
  450. mark_reg_unknown_value(regs, regno);
  451. reset_reg_range_values(regs, regno);
  452. }
  453. enum reg_arg_type {
  454. SRC_OP, /* register is used as source operand */
  455. DST_OP, /* register is used as destination operand */
  456. DST_OP_NO_MARK /* same as above, check only, don't mark */
  457. };
  458. static int check_reg_arg(struct bpf_reg_state *regs, u32 regno,
  459. enum reg_arg_type t)
  460. {
  461. if (regno >= MAX_BPF_REG) {
  462. verbose("R%d is invalid\n", regno);
  463. return -EINVAL;
  464. }
  465. if (t == SRC_OP) {
  466. /* check whether register used as source operand can be read */
  467. if (regs[regno].type == NOT_INIT) {
  468. verbose("R%d !read_ok\n", regno);
  469. return -EACCES;
  470. }
  471. } else {
  472. /* check whether register used as dest operand can be written to */
  473. if (regno == BPF_REG_FP) {
  474. verbose("frame pointer is read only\n");
  475. return -EACCES;
  476. }
  477. if (t == DST_OP)
  478. mark_reg_unknown_value(regs, regno);
  479. }
  480. return 0;
  481. }
  482. static int bpf_size_to_bytes(int bpf_size)
  483. {
  484. if (bpf_size == BPF_W)
  485. return 4;
  486. else if (bpf_size == BPF_H)
  487. return 2;
  488. else if (bpf_size == BPF_B)
  489. return 1;
  490. else if (bpf_size == BPF_DW)
  491. return 8;
  492. else
  493. return -EINVAL;
  494. }
  495. static bool is_spillable_regtype(enum bpf_reg_type type)
  496. {
  497. switch (type) {
  498. case PTR_TO_MAP_VALUE:
  499. case PTR_TO_MAP_VALUE_OR_NULL:
  500. case PTR_TO_MAP_VALUE_ADJ:
  501. case PTR_TO_STACK:
  502. case PTR_TO_CTX:
  503. case PTR_TO_PACKET:
  504. case PTR_TO_PACKET_END:
  505. case FRAME_PTR:
  506. case CONST_PTR_TO_MAP:
  507. return true;
  508. default:
  509. return false;
  510. }
  511. }
  512. /* check_stack_read/write functions track spill/fill of registers,
  513. * stack boundary and alignment are checked in check_mem_access()
  514. */
  515. static int check_stack_write(struct bpf_verifier_state *state, int off,
  516. int size, int value_regno)
  517. {
  518. int i;
  519. /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
  520. * so it's aligned access and [off, off + size) are within stack limits
  521. */
  522. if (value_regno >= 0 &&
  523. is_spillable_regtype(state->regs[value_regno].type)) {
  524. /* register containing pointer is being spilled into stack */
  525. if (size != BPF_REG_SIZE) {
  526. verbose("invalid size of register spill\n");
  527. return -EACCES;
  528. }
  529. /* save register state */
  530. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
  531. state->regs[value_regno];
  532. for (i = 0; i < BPF_REG_SIZE; i++)
  533. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
  534. } else {
  535. /* regular write of data into stack */
  536. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
  537. (struct bpf_reg_state) {};
  538. for (i = 0; i < size; i++)
  539. state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
  540. }
  541. return 0;
  542. }
  543. static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
  544. int value_regno)
  545. {
  546. u8 *slot_type;
  547. int i;
  548. slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
  549. if (slot_type[0] == STACK_SPILL) {
  550. if (size != BPF_REG_SIZE) {
  551. verbose("invalid size of register spill\n");
  552. return -EACCES;
  553. }
  554. for (i = 1; i < BPF_REG_SIZE; i++) {
  555. if (slot_type[i] != STACK_SPILL) {
  556. verbose("corrupted spill memory\n");
  557. return -EACCES;
  558. }
  559. }
  560. if (value_regno >= 0)
  561. /* restore register state from stack */
  562. state->regs[value_regno] =
  563. state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
  564. return 0;
  565. } else {
  566. for (i = 0; i < size; i++) {
  567. if (slot_type[i] != STACK_MISC) {
  568. verbose("invalid read from stack off %d+%d size %d\n",
  569. off, i, size);
  570. return -EACCES;
  571. }
  572. }
  573. if (value_regno >= 0)
  574. /* have read misc data from the stack */
  575. mark_reg_unknown_value_and_range(state->regs,
  576. value_regno);
  577. return 0;
  578. }
  579. }
  580. /* check read/write into map element returned by bpf_map_lookup_elem() */
  581. static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
  582. int size)
  583. {
  584. struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
  585. if (off < 0 || size <= 0 || off + size > map->value_size) {
  586. verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
  587. map->value_size, off, size);
  588. return -EACCES;
  589. }
  590. return 0;
  591. }
  592. /* check read/write into an adjusted map element */
  593. static int check_map_access_adj(struct bpf_verifier_env *env, u32 regno,
  594. int off, int size)
  595. {
  596. struct bpf_verifier_state *state = &env->cur_state;
  597. struct bpf_reg_state *reg = &state->regs[regno];
  598. int err;
  599. /* We adjusted the register to this map value, so we
  600. * need to change off and size to min_value and max_value
  601. * respectively to make sure our theoretical access will be
  602. * safe.
  603. */
  604. if (log_level)
  605. print_verifier_state(state);
  606. env->varlen_map_value_access = true;
  607. /* The minimum value is only important with signed
  608. * comparisons where we can't assume the floor of a
  609. * value is 0. If we are using signed variables for our
  610. * index'es we need to make sure that whatever we use
  611. * will have a set floor within our range.
  612. */
  613. if (reg->min_value < 0) {
  614. verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
  615. regno);
  616. return -EACCES;
  617. }
  618. err = check_map_access(env, regno, reg->min_value + off, size);
  619. if (err) {
  620. verbose("R%d min value is outside of the array range\n",
  621. regno);
  622. return err;
  623. }
  624. /* If we haven't set a max value then we need to bail
  625. * since we can't be sure we won't do bad things.
  626. */
  627. if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
  628. verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
  629. regno);
  630. return -EACCES;
  631. }
  632. return check_map_access(env, regno, reg->max_value + off, size);
  633. }
  634. #define MAX_PACKET_OFF 0xffff
  635. static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
  636. const struct bpf_call_arg_meta *meta,
  637. enum bpf_access_type t)
  638. {
  639. switch (env->prog->type) {
  640. case BPF_PROG_TYPE_LWT_IN:
  641. case BPF_PROG_TYPE_LWT_OUT:
  642. /* dst_input() and dst_output() can't write for now */
  643. if (t == BPF_WRITE)
  644. return false;
  645. case BPF_PROG_TYPE_SCHED_CLS:
  646. case BPF_PROG_TYPE_SCHED_ACT:
  647. case BPF_PROG_TYPE_XDP:
  648. case BPF_PROG_TYPE_LWT_XMIT:
  649. if (meta)
  650. return meta->pkt_access;
  651. env->seen_direct_write = true;
  652. return true;
  653. default:
  654. return false;
  655. }
  656. }
  657. static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
  658. int size)
  659. {
  660. struct bpf_reg_state *regs = env->cur_state.regs;
  661. struct bpf_reg_state *reg = &regs[regno];
  662. off += reg->off;
  663. if (off < 0 || size <= 0 || off + size > reg->range) {
  664. verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
  665. off, size, regno, reg->id, reg->off, reg->range);
  666. return -EACCES;
  667. }
  668. return 0;
  669. }
  670. /* check access to 'struct bpf_context' fields */
  671. static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,
  672. enum bpf_access_type t, enum bpf_reg_type *reg_type)
  673. {
  674. /* for analyzer ctx accesses are already validated and converted */
  675. if (env->analyzer_ops)
  676. return 0;
  677. if (env->prog->aux->ops->is_valid_access &&
  678. env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) {
  679. /* remember the offset of last byte accessed in ctx */
  680. if (env->prog->aux->max_ctx_offset < off + size)
  681. env->prog->aux->max_ctx_offset = off + size;
  682. return 0;
  683. }
  684. verbose("invalid bpf_context access off=%d size=%d\n", off, size);
  685. return -EACCES;
  686. }
  687. static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
  688. {
  689. if (env->allow_ptr_leaks)
  690. return false;
  691. switch (env->cur_state.regs[regno].type) {
  692. case UNKNOWN_VALUE:
  693. case CONST_IMM:
  694. return false;
  695. default:
  696. return true;
  697. }
  698. }
  699. static int check_ptr_alignment(struct bpf_verifier_env *env,
  700. struct bpf_reg_state *reg, int off, int size)
  701. {
  702. if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) {
  703. if (off % size != 0) {
  704. verbose("misaligned access off %d size %d\n",
  705. off, size);
  706. return -EACCES;
  707. } else {
  708. return 0;
  709. }
  710. }
  711. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  712. /* misaligned access to packet is ok on x86,arm,arm64 */
  713. return 0;
  714. if (reg->id && size != 1) {
  715. verbose("Unknown packet alignment. Only byte-sized access allowed\n");
  716. return -EACCES;
  717. }
  718. /* skb->data is NET_IP_ALIGN-ed */
  719. if (reg->type == PTR_TO_PACKET &&
  720. (NET_IP_ALIGN + reg->off + off) % size != 0) {
  721. verbose("misaligned packet access off %d+%d+%d size %d\n",
  722. NET_IP_ALIGN, reg->off, off, size);
  723. return -EACCES;
  724. }
  725. return 0;
  726. }
  727. /* check whether memory at (regno + off) is accessible for t = (read | write)
  728. * if t==write, value_regno is a register which value is stored into memory
  729. * if t==read, value_regno is a register which will receive the value from memory
  730. * if t==write && value_regno==-1, some unknown value is stored into memory
  731. * if t==read && value_regno==-1, don't care what we read from memory
  732. */
  733. static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
  734. int bpf_size, enum bpf_access_type t,
  735. int value_regno)
  736. {
  737. struct bpf_verifier_state *state = &env->cur_state;
  738. struct bpf_reg_state *reg = &state->regs[regno];
  739. int size, err = 0;
  740. if (reg->type == PTR_TO_STACK)
  741. off += reg->imm;
  742. size = bpf_size_to_bytes(bpf_size);
  743. if (size < 0)
  744. return size;
  745. err = check_ptr_alignment(env, reg, off, size);
  746. if (err)
  747. return err;
  748. if (reg->type == PTR_TO_MAP_VALUE ||
  749. reg->type == PTR_TO_MAP_VALUE_ADJ) {
  750. if (t == BPF_WRITE && value_regno >= 0 &&
  751. is_pointer_value(env, value_regno)) {
  752. verbose("R%d leaks addr into map\n", value_regno);
  753. return -EACCES;
  754. }
  755. if (reg->type == PTR_TO_MAP_VALUE_ADJ)
  756. err = check_map_access_adj(env, regno, off, size);
  757. else
  758. err = check_map_access(env, regno, off, size);
  759. if (!err && t == BPF_READ && value_regno >= 0)
  760. mark_reg_unknown_value_and_range(state->regs,
  761. value_regno);
  762. } else if (reg->type == PTR_TO_CTX) {
  763. enum bpf_reg_type reg_type = UNKNOWN_VALUE;
  764. if (t == BPF_WRITE && value_regno >= 0 &&
  765. is_pointer_value(env, value_regno)) {
  766. verbose("R%d leaks addr into ctx\n", value_regno);
  767. return -EACCES;
  768. }
  769. err = check_ctx_access(env, off, size, t, &reg_type);
  770. if (!err && t == BPF_READ && value_regno >= 0) {
  771. mark_reg_unknown_value_and_range(state->regs,
  772. value_regno);
  773. /* note that reg.[id|off|range] == 0 */
  774. state->regs[value_regno].type = reg_type;
  775. }
  776. } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) {
  777. if (off >= 0 || off < -MAX_BPF_STACK) {
  778. verbose("invalid stack off=%d size=%d\n", off, size);
  779. return -EACCES;
  780. }
  781. if (t == BPF_WRITE) {
  782. if (!env->allow_ptr_leaks &&
  783. state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
  784. size != BPF_REG_SIZE) {
  785. verbose("attempt to corrupt spilled pointer on stack\n");
  786. return -EACCES;
  787. }
  788. err = check_stack_write(state, off, size, value_regno);
  789. } else {
  790. err = check_stack_read(state, off, size, value_regno);
  791. }
  792. } else if (state->regs[regno].type == PTR_TO_PACKET) {
  793. if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
  794. verbose("cannot write into packet\n");
  795. return -EACCES;
  796. }
  797. if (t == BPF_WRITE && value_regno >= 0 &&
  798. is_pointer_value(env, value_regno)) {
  799. verbose("R%d leaks addr into packet\n", value_regno);
  800. return -EACCES;
  801. }
  802. err = check_packet_access(env, regno, off, size);
  803. if (!err && t == BPF_READ && value_regno >= 0)
  804. mark_reg_unknown_value_and_range(state->regs,
  805. value_regno);
  806. } else {
  807. verbose("R%d invalid mem access '%s'\n",
  808. regno, reg_type_str[reg->type]);
  809. return -EACCES;
  810. }
  811. if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks &&
  812. state->regs[value_regno].type == UNKNOWN_VALUE) {
  813. /* 1 or 2 byte load zero-extends, determine the number of
  814. * zero upper bits. Not doing it fo 4 byte load, since
  815. * such values cannot be added to ptr_to_packet anyway.
  816. */
  817. state->regs[value_regno].imm = 64 - size * 8;
  818. }
  819. return err;
  820. }
  821. static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn)
  822. {
  823. struct bpf_reg_state *regs = env->cur_state.regs;
  824. int err;
  825. if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
  826. insn->imm != 0) {
  827. verbose("BPF_XADD uses reserved fields\n");
  828. return -EINVAL;
  829. }
  830. /* check src1 operand */
  831. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  832. if (err)
  833. return err;
  834. /* check src2 operand */
  835. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  836. if (err)
  837. return err;
  838. /* check whether atomic_add can read the memory */
  839. err = check_mem_access(env, insn->dst_reg, insn->off,
  840. BPF_SIZE(insn->code), BPF_READ, -1);
  841. if (err)
  842. return err;
  843. /* check whether atomic_add can write into the same memory */
  844. return check_mem_access(env, insn->dst_reg, insn->off,
  845. BPF_SIZE(insn->code), BPF_WRITE, -1);
  846. }
  847. /* when register 'regno' is passed into function that will read 'access_size'
  848. * bytes from that pointer, make sure that it's within stack boundary
  849. * and all elements of stack are initialized
  850. */
  851. static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
  852. int access_size, bool zero_size_allowed,
  853. struct bpf_call_arg_meta *meta)
  854. {
  855. struct bpf_verifier_state *state = &env->cur_state;
  856. struct bpf_reg_state *regs = state->regs;
  857. int off, i;
  858. if (regs[regno].type != PTR_TO_STACK) {
  859. if (zero_size_allowed && access_size == 0 &&
  860. regs[regno].type == CONST_IMM &&
  861. regs[regno].imm == 0)
  862. return 0;
  863. verbose("R%d type=%s expected=%s\n", regno,
  864. reg_type_str[regs[regno].type],
  865. reg_type_str[PTR_TO_STACK]);
  866. return -EACCES;
  867. }
  868. off = regs[regno].imm;
  869. if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
  870. access_size <= 0) {
  871. verbose("invalid stack type R%d off=%d access_size=%d\n",
  872. regno, off, access_size);
  873. return -EACCES;
  874. }
  875. if (meta && meta->raw_mode) {
  876. meta->access_size = access_size;
  877. meta->regno = regno;
  878. return 0;
  879. }
  880. for (i = 0; i < access_size; i++) {
  881. if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
  882. verbose("invalid indirect read from stack off %d+%d size %d\n",
  883. off, i, access_size);
  884. return -EACCES;
  885. }
  886. }
  887. return 0;
  888. }
  889. static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
  890. int access_size, bool zero_size_allowed,
  891. struct bpf_call_arg_meta *meta)
  892. {
  893. struct bpf_reg_state *regs = env->cur_state.regs;
  894. switch (regs[regno].type) {
  895. case PTR_TO_PACKET:
  896. return check_packet_access(env, regno, 0, access_size);
  897. case PTR_TO_MAP_VALUE:
  898. return check_map_access(env, regno, 0, access_size);
  899. case PTR_TO_MAP_VALUE_ADJ:
  900. return check_map_access_adj(env, regno, 0, access_size);
  901. default: /* const_imm|ptr_to_stack or invalid ptr */
  902. return check_stack_boundary(env, regno, access_size,
  903. zero_size_allowed, meta);
  904. }
  905. }
  906. static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
  907. enum bpf_arg_type arg_type,
  908. struct bpf_call_arg_meta *meta)
  909. {
  910. struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
  911. enum bpf_reg_type expected_type, type = reg->type;
  912. int err = 0;
  913. if (arg_type == ARG_DONTCARE)
  914. return 0;
  915. if (type == NOT_INIT) {
  916. verbose("R%d !read_ok\n", regno);
  917. return -EACCES;
  918. }
  919. if (arg_type == ARG_ANYTHING) {
  920. if (is_pointer_value(env, regno)) {
  921. verbose("R%d leaks addr into helper function\n", regno);
  922. return -EACCES;
  923. }
  924. return 0;
  925. }
  926. if (type == PTR_TO_PACKET &&
  927. !may_access_direct_pkt_data(env, meta, BPF_READ)) {
  928. verbose("helper access to the packet is not allowed\n");
  929. return -EACCES;
  930. }
  931. if (arg_type == ARG_PTR_TO_MAP_KEY ||
  932. arg_type == ARG_PTR_TO_MAP_VALUE) {
  933. expected_type = PTR_TO_STACK;
  934. if (type != PTR_TO_PACKET && type != expected_type)
  935. goto err_type;
  936. } else if (arg_type == ARG_CONST_SIZE ||
  937. arg_type == ARG_CONST_SIZE_OR_ZERO) {
  938. expected_type = CONST_IMM;
  939. /* One exception. Allow UNKNOWN_VALUE registers when the
  940. * boundaries are known and don't cause unsafe memory accesses
  941. */
  942. if (type != UNKNOWN_VALUE && type != expected_type)
  943. goto err_type;
  944. } else if (arg_type == ARG_CONST_MAP_PTR) {
  945. expected_type = CONST_PTR_TO_MAP;
  946. if (type != expected_type)
  947. goto err_type;
  948. } else if (arg_type == ARG_PTR_TO_CTX) {
  949. expected_type = PTR_TO_CTX;
  950. if (type != expected_type)
  951. goto err_type;
  952. } else if (arg_type == ARG_PTR_TO_MEM ||
  953. arg_type == ARG_PTR_TO_UNINIT_MEM) {
  954. expected_type = PTR_TO_STACK;
  955. /* One exception here. In case function allows for NULL to be
  956. * passed in as argument, it's a CONST_IMM type. Final test
  957. * happens during stack boundary checking.
  958. */
  959. if (type == CONST_IMM && reg->imm == 0)
  960. /* final test in check_stack_boundary() */;
  961. else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE &&
  962. type != PTR_TO_MAP_VALUE_ADJ && type != expected_type)
  963. goto err_type;
  964. meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
  965. } else {
  966. verbose("unsupported arg_type %d\n", arg_type);
  967. return -EFAULT;
  968. }
  969. if (arg_type == ARG_CONST_MAP_PTR) {
  970. /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
  971. meta->map_ptr = reg->map_ptr;
  972. } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
  973. /* bpf_map_xxx(..., map_ptr, ..., key) call:
  974. * check that [key, key + map->key_size) are within
  975. * stack limits and initialized
  976. */
  977. if (!meta->map_ptr) {
  978. /* in function declaration map_ptr must come before
  979. * map_key, so that it's verified and known before
  980. * we have to check map_key here. Otherwise it means
  981. * that kernel subsystem misconfigured verifier
  982. */
  983. verbose("invalid map_ptr to access map->key\n");
  984. return -EACCES;
  985. }
  986. if (type == PTR_TO_PACKET)
  987. err = check_packet_access(env, regno, 0,
  988. meta->map_ptr->key_size);
  989. else
  990. err = check_stack_boundary(env, regno,
  991. meta->map_ptr->key_size,
  992. false, NULL);
  993. } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
  994. /* bpf_map_xxx(..., map_ptr, ..., value) call:
  995. * check [value, value + map->value_size) validity
  996. */
  997. if (!meta->map_ptr) {
  998. /* kernel subsystem misconfigured verifier */
  999. verbose("invalid map_ptr to access map->value\n");
  1000. return -EACCES;
  1001. }
  1002. if (type == PTR_TO_PACKET)
  1003. err = check_packet_access(env, regno, 0,
  1004. meta->map_ptr->value_size);
  1005. else
  1006. err = check_stack_boundary(env, regno,
  1007. meta->map_ptr->value_size,
  1008. false, NULL);
  1009. } else if (arg_type == ARG_CONST_SIZE ||
  1010. arg_type == ARG_CONST_SIZE_OR_ZERO) {
  1011. bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
  1012. /* bpf_xxx(..., buf, len) call will access 'len' bytes
  1013. * from stack pointer 'buf'. Check it
  1014. * note: regno == len, regno - 1 == buf
  1015. */
  1016. if (regno == 0) {
  1017. /* kernel subsystem misconfigured verifier */
  1018. verbose("ARG_CONST_SIZE cannot be first argument\n");
  1019. return -EACCES;
  1020. }
  1021. /* If the register is UNKNOWN_VALUE, the access check happens
  1022. * using its boundaries. Otherwise, just use its imm
  1023. */
  1024. if (type == UNKNOWN_VALUE) {
  1025. /* For unprivileged variable accesses, disable raw
  1026. * mode so that the program is required to
  1027. * initialize all the memory that the helper could
  1028. * just partially fill up.
  1029. */
  1030. meta = NULL;
  1031. if (reg->min_value < 0) {
  1032. verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
  1033. regno);
  1034. return -EACCES;
  1035. }
  1036. if (reg->min_value == 0) {
  1037. err = check_helper_mem_access(env, regno - 1, 0,
  1038. zero_size_allowed,
  1039. meta);
  1040. if (err)
  1041. return err;
  1042. }
  1043. if (reg->max_value == BPF_REGISTER_MAX_RANGE) {
  1044. verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
  1045. regno);
  1046. return -EACCES;
  1047. }
  1048. err = check_helper_mem_access(env, regno - 1,
  1049. reg->max_value,
  1050. zero_size_allowed, meta);
  1051. if (err)
  1052. return err;
  1053. } else {
  1054. /* register is CONST_IMM */
  1055. err = check_helper_mem_access(env, regno - 1, reg->imm,
  1056. zero_size_allowed, meta);
  1057. }
  1058. }
  1059. return err;
  1060. err_type:
  1061. verbose("R%d type=%s expected=%s\n", regno,
  1062. reg_type_str[type], reg_type_str[expected_type]);
  1063. return -EACCES;
  1064. }
  1065. static int check_map_func_compatibility(struct bpf_map *map, int func_id)
  1066. {
  1067. if (!map)
  1068. return 0;
  1069. /* We need a two way check, first is from map perspective ... */
  1070. switch (map->map_type) {
  1071. case BPF_MAP_TYPE_PROG_ARRAY:
  1072. if (func_id != BPF_FUNC_tail_call)
  1073. goto error;
  1074. break;
  1075. case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
  1076. if (func_id != BPF_FUNC_perf_event_read &&
  1077. func_id != BPF_FUNC_perf_event_output)
  1078. goto error;
  1079. break;
  1080. case BPF_MAP_TYPE_STACK_TRACE:
  1081. if (func_id != BPF_FUNC_get_stackid)
  1082. goto error;
  1083. break;
  1084. case BPF_MAP_TYPE_CGROUP_ARRAY:
  1085. if (func_id != BPF_FUNC_skb_under_cgroup &&
  1086. func_id != BPF_FUNC_current_task_under_cgroup)
  1087. goto error;
  1088. break;
  1089. default:
  1090. break;
  1091. }
  1092. /* ... and second from the function itself. */
  1093. switch (func_id) {
  1094. case BPF_FUNC_tail_call:
  1095. if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
  1096. goto error;
  1097. break;
  1098. case BPF_FUNC_perf_event_read:
  1099. case BPF_FUNC_perf_event_output:
  1100. if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
  1101. goto error;
  1102. break;
  1103. case BPF_FUNC_get_stackid:
  1104. if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
  1105. goto error;
  1106. break;
  1107. case BPF_FUNC_current_task_under_cgroup:
  1108. case BPF_FUNC_skb_under_cgroup:
  1109. if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
  1110. goto error;
  1111. break;
  1112. default:
  1113. break;
  1114. }
  1115. return 0;
  1116. error:
  1117. verbose("cannot pass map_type %d into func %s#%d\n",
  1118. map->map_type, func_id_name(func_id), func_id);
  1119. return -EINVAL;
  1120. }
  1121. static int check_raw_mode(const struct bpf_func_proto *fn)
  1122. {
  1123. int count = 0;
  1124. if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
  1125. count++;
  1126. if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
  1127. count++;
  1128. if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
  1129. count++;
  1130. if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
  1131. count++;
  1132. if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
  1133. count++;
  1134. return count > 1 ? -EINVAL : 0;
  1135. }
  1136. static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
  1137. {
  1138. struct bpf_verifier_state *state = &env->cur_state;
  1139. struct bpf_reg_state *regs = state->regs, *reg;
  1140. int i;
  1141. for (i = 0; i < MAX_BPF_REG; i++)
  1142. if (regs[i].type == PTR_TO_PACKET ||
  1143. regs[i].type == PTR_TO_PACKET_END)
  1144. mark_reg_unknown_value(regs, i);
  1145. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1146. if (state->stack_slot_type[i] != STACK_SPILL)
  1147. continue;
  1148. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1149. if (reg->type != PTR_TO_PACKET &&
  1150. reg->type != PTR_TO_PACKET_END)
  1151. continue;
  1152. reg->type = UNKNOWN_VALUE;
  1153. reg->imm = 0;
  1154. }
  1155. }
  1156. static int check_call(struct bpf_verifier_env *env, int func_id)
  1157. {
  1158. struct bpf_verifier_state *state = &env->cur_state;
  1159. const struct bpf_func_proto *fn = NULL;
  1160. struct bpf_reg_state *regs = state->regs;
  1161. struct bpf_reg_state *reg;
  1162. struct bpf_call_arg_meta meta;
  1163. bool changes_data;
  1164. int i, err;
  1165. /* find function prototype */
  1166. if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
  1167. verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
  1168. return -EINVAL;
  1169. }
  1170. if (env->prog->aux->ops->get_func_proto)
  1171. fn = env->prog->aux->ops->get_func_proto(func_id);
  1172. if (!fn) {
  1173. verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
  1174. return -EINVAL;
  1175. }
  1176. /* eBPF programs must be GPL compatible to use GPL-ed functions */
  1177. if (!env->prog->gpl_compatible && fn->gpl_only) {
  1178. verbose("cannot call GPL only function from proprietary program\n");
  1179. return -EINVAL;
  1180. }
  1181. changes_data = bpf_helper_changes_pkt_data(fn->func);
  1182. memset(&meta, 0, sizeof(meta));
  1183. meta.pkt_access = fn->pkt_access;
  1184. /* We only support one arg being in raw mode at the moment, which
  1185. * is sufficient for the helper functions we have right now.
  1186. */
  1187. err = check_raw_mode(fn);
  1188. if (err) {
  1189. verbose("kernel subsystem misconfigured func %s#%d\n",
  1190. func_id_name(func_id), func_id);
  1191. return err;
  1192. }
  1193. /* check args */
  1194. err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
  1195. if (err)
  1196. return err;
  1197. err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
  1198. if (err)
  1199. return err;
  1200. err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
  1201. if (err)
  1202. return err;
  1203. err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
  1204. if (err)
  1205. return err;
  1206. err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
  1207. if (err)
  1208. return err;
  1209. /* Mark slots with STACK_MISC in case of raw mode, stack offset
  1210. * is inferred from register state.
  1211. */
  1212. for (i = 0; i < meta.access_size; i++) {
  1213. err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1);
  1214. if (err)
  1215. return err;
  1216. }
  1217. /* reset caller saved regs */
  1218. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  1219. reg = regs + caller_saved[i];
  1220. reg->type = NOT_INIT;
  1221. reg->imm = 0;
  1222. }
  1223. /* update return register */
  1224. if (fn->ret_type == RET_INTEGER) {
  1225. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  1226. } else if (fn->ret_type == RET_VOID) {
  1227. regs[BPF_REG_0].type = NOT_INIT;
  1228. } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
  1229. regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
  1230. regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0;
  1231. /* remember map_ptr, so that check_map_access()
  1232. * can check 'value_size' boundary of memory access
  1233. * to map element returned from bpf_map_lookup_elem()
  1234. */
  1235. if (meta.map_ptr == NULL) {
  1236. verbose("kernel subsystem misconfigured verifier\n");
  1237. return -EINVAL;
  1238. }
  1239. regs[BPF_REG_0].map_ptr = meta.map_ptr;
  1240. regs[BPF_REG_0].id = ++env->id_gen;
  1241. } else {
  1242. verbose("unknown return type %d of func %s#%d\n",
  1243. fn->ret_type, func_id_name(func_id), func_id);
  1244. return -EINVAL;
  1245. }
  1246. err = check_map_func_compatibility(meta.map_ptr, func_id);
  1247. if (err)
  1248. return err;
  1249. if (changes_data)
  1250. clear_all_pkt_pointers(env);
  1251. return 0;
  1252. }
  1253. static int check_packet_ptr_add(struct bpf_verifier_env *env,
  1254. struct bpf_insn *insn)
  1255. {
  1256. struct bpf_reg_state *regs = env->cur_state.regs;
  1257. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1258. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1259. struct bpf_reg_state tmp_reg;
  1260. s32 imm;
  1261. if (BPF_SRC(insn->code) == BPF_K) {
  1262. /* pkt_ptr += imm */
  1263. imm = insn->imm;
  1264. add_imm:
  1265. if (imm <= 0) {
  1266. verbose("addition of negative constant to packet pointer is not allowed\n");
  1267. return -EACCES;
  1268. }
  1269. if (imm >= MAX_PACKET_OFF ||
  1270. imm + dst_reg->off >= MAX_PACKET_OFF) {
  1271. verbose("constant %d is too large to add to packet pointer\n",
  1272. imm);
  1273. return -EACCES;
  1274. }
  1275. /* a constant was added to pkt_ptr.
  1276. * Remember it while keeping the same 'id'
  1277. */
  1278. dst_reg->off += imm;
  1279. } else {
  1280. if (src_reg->type == PTR_TO_PACKET) {
  1281. /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
  1282. tmp_reg = *dst_reg; /* save r7 state */
  1283. *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */
  1284. src_reg = &tmp_reg; /* pretend it's src_reg state */
  1285. /* if the checks below reject it, the copy won't matter,
  1286. * since we're rejecting the whole program. If all ok,
  1287. * then imm22 state will be added to r7
  1288. * and r7 will be pkt(id=0,off=22,r=62) while
  1289. * r6 will stay as pkt(id=0,off=0,r=62)
  1290. */
  1291. }
  1292. if (src_reg->type == CONST_IMM) {
  1293. /* pkt_ptr += reg where reg is known constant */
  1294. imm = src_reg->imm;
  1295. goto add_imm;
  1296. }
  1297. /* disallow pkt_ptr += reg
  1298. * if reg is not uknown_value with guaranteed zero upper bits
  1299. * otherwise pkt_ptr may overflow and addition will become
  1300. * subtraction which is not allowed
  1301. */
  1302. if (src_reg->type != UNKNOWN_VALUE) {
  1303. verbose("cannot add '%s' to ptr_to_packet\n",
  1304. reg_type_str[src_reg->type]);
  1305. return -EACCES;
  1306. }
  1307. if (src_reg->imm < 48) {
  1308. verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
  1309. src_reg->imm);
  1310. return -EACCES;
  1311. }
  1312. /* dst_reg stays as pkt_ptr type and since some positive
  1313. * integer value was added to the pointer, increment its 'id'
  1314. */
  1315. dst_reg->id = ++env->id_gen;
  1316. /* something was added to pkt_ptr, set range and off to zero */
  1317. dst_reg->off = 0;
  1318. dst_reg->range = 0;
  1319. }
  1320. return 0;
  1321. }
  1322. static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn)
  1323. {
  1324. struct bpf_reg_state *regs = env->cur_state.regs;
  1325. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1326. u8 opcode = BPF_OP(insn->code);
  1327. s64 imm_log2;
  1328. /* for type == UNKNOWN_VALUE:
  1329. * imm > 0 -> number of zero upper bits
  1330. * imm == 0 -> don't track which is the same as all bits can be non-zero
  1331. */
  1332. if (BPF_SRC(insn->code) == BPF_X) {
  1333. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1334. if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 &&
  1335. dst_reg->imm && opcode == BPF_ADD) {
  1336. /* dreg += sreg
  1337. * where both have zero upper bits. Adding them
  1338. * can only result making one more bit non-zero
  1339. * in the larger value.
  1340. * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
  1341. * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
  1342. */
  1343. dst_reg->imm = min(dst_reg->imm, src_reg->imm);
  1344. dst_reg->imm--;
  1345. return 0;
  1346. }
  1347. if (src_reg->type == CONST_IMM && src_reg->imm > 0 &&
  1348. dst_reg->imm && opcode == BPF_ADD) {
  1349. /* dreg += sreg
  1350. * where dreg has zero upper bits and sreg is const.
  1351. * Adding them can only result making one more bit
  1352. * non-zero in the larger value.
  1353. */
  1354. imm_log2 = __ilog2_u64((long long)src_reg->imm);
  1355. dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
  1356. dst_reg->imm--;
  1357. return 0;
  1358. }
  1359. /* all other cases non supported yet, just mark dst_reg */
  1360. dst_reg->imm = 0;
  1361. return 0;
  1362. }
  1363. /* sign extend 32-bit imm into 64-bit to make sure that
  1364. * negative values occupy bit 63. Note ilog2() would have
  1365. * been incorrect, since sizeof(insn->imm) == 4
  1366. */
  1367. imm_log2 = __ilog2_u64((long long)insn->imm);
  1368. if (dst_reg->imm && opcode == BPF_LSH) {
  1369. /* reg <<= imm
  1370. * if reg was a result of 2 byte load, then its imm == 48
  1371. * which means that upper 48 bits are zero and shifting this reg
  1372. * left by 4 would mean that upper 44 bits are still zero
  1373. */
  1374. dst_reg->imm -= insn->imm;
  1375. } else if (dst_reg->imm && opcode == BPF_MUL) {
  1376. /* reg *= imm
  1377. * if multiplying by 14 subtract 4
  1378. * This is conservative calculation of upper zero bits.
  1379. * It's not trying to special case insn->imm == 1 or 0 cases
  1380. */
  1381. dst_reg->imm -= imm_log2 + 1;
  1382. } else if (opcode == BPF_AND) {
  1383. /* reg &= imm */
  1384. dst_reg->imm = 63 - imm_log2;
  1385. } else if (dst_reg->imm && opcode == BPF_ADD) {
  1386. /* reg += imm */
  1387. dst_reg->imm = min(dst_reg->imm, 63 - imm_log2);
  1388. dst_reg->imm--;
  1389. } else if (opcode == BPF_RSH) {
  1390. /* reg >>= imm
  1391. * which means that after right shift, upper bits will be zero
  1392. * note that verifier already checked that
  1393. * 0 <= imm < 64 for shift insn
  1394. */
  1395. dst_reg->imm += insn->imm;
  1396. if (unlikely(dst_reg->imm > 64))
  1397. /* some dumb code did:
  1398. * r2 = *(u32 *)mem;
  1399. * r2 >>= 32;
  1400. * and all bits are zero now */
  1401. dst_reg->imm = 64;
  1402. } else {
  1403. /* all other alu ops, means that we don't know what will
  1404. * happen to the value, mark it with unknown number of zero bits
  1405. */
  1406. dst_reg->imm = 0;
  1407. }
  1408. if (dst_reg->imm < 0) {
  1409. /* all 64 bits of the register can contain non-zero bits
  1410. * and such value cannot be added to ptr_to_packet, since it
  1411. * may overflow, mark it as unknown to avoid further eval
  1412. */
  1413. dst_reg->imm = 0;
  1414. }
  1415. return 0;
  1416. }
  1417. static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
  1418. struct bpf_insn *insn)
  1419. {
  1420. struct bpf_reg_state *regs = env->cur_state.regs;
  1421. struct bpf_reg_state *dst_reg = &regs[insn->dst_reg];
  1422. struct bpf_reg_state *src_reg = &regs[insn->src_reg];
  1423. u8 opcode = BPF_OP(insn->code);
  1424. u64 dst_imm = dst_reg->imm;
  1425. /* dst_reg->type == CONST_IMM here. Simulate execution of insns
  1426. * containing ALU ops. Don't care about overflow or negative
  1427. * values, just add/sub/... them; registers are in u64.
  1428. */
  1429. if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) {
  1430. dst_imm += insn->imm;
  1431. } else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
  1432. src_reg->type == CONST_IMM) {
  1433. dst_imm += src_reg->imm;
  1434. } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_K) {
  1435. dst_imm -= insn->imm;
  1436. } else if (opcode == BPF_SUB && BPF_SRC(insn->code) == BPF_X &&
  1437. src_reg->type == CONST_IMM) {
  1438. dst_imm -= src_reg->imm;
  1439. } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_K) {
  1440. dst_imm *= insn->imm;
  1441. } else if (opcode == BPF_MUL && BPF_SRC(insn->code) == BPF_X &&
  1442. src_reg->type == CONST_IMM) {
  1443. dst_imm *= src_reg->imm;
  1444. } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_K) {
  1445. dst_imm |= insn->imm;
  1446. } else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_X &&
  1447. src_reg->type == CONST_IMM) {
  1448. dst_imm |= src_reg->imm;
  1449. } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_K) {
  1450. dst_imm &= insn->imm;
  1451. } else if (opcode == BPF_AND && BPF_SRC(insn->code) == BPF_X &&
  1452. src_reg->type == CONST_IMM) {
  1453. dst_imm &= src_reg->imm;
  1454. } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_K) {
  1455. dst_imm >>= insn->imm;
  1456. } else if (opcode == BPF_RSH && BPF_SRC(insn->code) == BPF_X &&
  1457. src_reg->type == CONST_IMM) {
  1458. dst_imm >>= src_reg->imm;
  1459. } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_K) {
  1460. dst_imm <<= insn->imm;
  1461. } else if (opcode == BPF_LSH && BPF_SRC(insn->code) == BPF_X &&
  1462. src_reg->type == CONST_IMM) {
  1463. dst_imm <<= src_reg->imm;
  1464. } else {
  1465. mark_reg_unknown_value(regs, insn->dst_reg);
  1466. goto out;
  1467. }
  1468. dst_reg->imm = dst_imm;
  1469. out:
  1470. return 0;
  1471. }
  1472. static void check_reg_overflow(struct bpf_reg_state *reg)
  1473. {
  1474. if (reg->max_value > BPF_REGISTER_MAX_RANGE)
  1475. reg->max_value = BPF_REGISTER_MAX_RANGE;
  1476. if (reg->min_value < BPF_REGISTER_MIN_RANGE ||
  1477. reg->min_value > BPF_REGISTER_MAX_RANGE)
  1478. reg->min_value = BPF_REGISTER_MIN_RANGE;
  1479. }
  1480. static void adjust_reg_min_max_vals(struct bpf_verifier_env *env,
  1481. struct bpf_insn *insn)
  1482. {
  1483. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1484. s64 min_val = BPF_REGISTER_MIN_RANGE;
  1485. u64 max_val = BPF_REGISTER_MAX_RANGE;
  1486. u8 opcode = BPF_OP(insn->code);
  1487. dst_reg = &regs[insn->dst_reg];
  1488. if (BPF_SRC(insn->code) == BPF_X) {
  1489. check_reg_overflow(&regs[insn->src_reg]);
  1490. min_val = regs[insn->src_reg].min_value;
  1491. max_val = regs[insn->src_reg].max_value;
  1492. /* If the source register is a random pointer then the
  1493. * min_value/max_value values represent the range of the known
  1494. * accesses into that value, not the actual min/max value of the
  1495. * register itself. In this case we have to reset the reg range
  1496. * values so we know it is not safe to look at.
  1497. */
  1498. if (regs[insn->src_reg].type != CONST_IMM &&
  1499. regs[insn->src_reg].type != UNKNOWN_VALUE) {
  1500. min_val = BPF_REGISTER_MIN_RANGE;
  1501. max_val = BPF_REGISTER_MAX_RANGE;
  1502. }
  1503. } else if (insn->imm < BPF_REGISTER_MAX_RANGE &&
  1504. (s64)insn->imm > BPF_REGISTER_MIN_RANGE) {
  1505. min_val = max_val = insn->imm;
  1506. }
  1507. /* We don't know anything about what was done to this register, mark it
  1508. * as unknown.
  1509. */
  1510. if (min_val == BPF_REGISTER_MIN_RANGE &&
  1511. max_val == BPF_REGISTER_MAX_RANGE) {
  1512. reset_reg_range_values(regs, insn->dst_reg);
  1513. return;
  1514. }
  1515. /* If one of our values was at the end of our ranges then we can't just
  1516. * do our normal operations to the register, we need to set the values
  1517. * to the min/max since they are undefined.
  1518. */
  1519. if (min_val == BPF_REGISTER_MIN_RANGE)
  1520. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1521. if (max_val == BPF_REGISTER_MAX_RANGE)
  1522. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1523. switch (opcode) {
  1524. case BPF_ADD:
  1525. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1526. dst_reg->min_value += min_val;
  1527. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1528. dst_reg->max_value += max_val;
  1529. break;
  1530. case BPF_SUB:
  1531. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1532. dst_reg->min_value -= min_val;
  1533. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1534. dst_reg->max_value -= max_val;
  1535. break;
  1536. case BPF_MUL:
  1537. if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1538. dst_reg->min_value *= min_val;
  1539. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1540. dst_reg->max_value *= max_val;
  1541. break;
  1542. case BPF_AND:
  1543. /* Disallow AND'ing of negative numbers, ain't nobody got time
  1544. * for that. Otherwise the minimum is 0 and the max is the max
  1545. * value we could AND against.
  1546. */
  1547. if (min_val < 0)
  1548. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1549. else
  1550. dst_reg->min_value = 0;
  1551. dst_reg->max_value = max_val;
  1552. break;
  1553. case BPF_LSH:
  1554. /* Gotta have special overflow logic here, if we're shifting
  1555. * more than MAX_RANGE then just assume we have an invalid
  1556. * range.
  1557. */
  1558. if (min_val > ilog2(BPF_REGISTER_MAX_RANGE))
  1559. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1560. else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE)
  1561. dst_reg->min_value <<= min_val;
  1562. if (max_val > ilog2(BPF_REGISTER_MAX_RANGE))
  1563. dst_reg->max_value = BPF_REGISTER_MAX_RANGE;
  1564. else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1565. dst_reg->max_value <<= max_val;
  1566. break;
  1567. case BPF_RSH:
  1568. /* RSH by a negative number is undefined, and the BPF_RSH is an
  1569. * unsigned shift, so make the appropriate casts.
  1570. */
  1571. if (min_val < 0 || dst_reg->min_value < 0)
  1572. dst_reg->min_value = BPF_REGISTER_MIN_RANGE;
  1573. else
  1574. dst_reg->min_value =
  1575. (u64)(dst_reg->min_value) >> min_val;
  1576. if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE)
  1577. dst_reg->max_value >>= max_val;
  1578. break;
  1579. default:
  1580. reset_reg_range_values(regs, insn->dst_reg);
  1581. break;
  1582. }
  1583. check_reg_overflow(dst_reg);
  1584. }
  1585. /* check validity of 32-bit and 64-bit arithmetic operations */
  1586. static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
  1587. {
  1588. struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
  1589. u8 opcode = BPF_OP(insn->code);
  1590. int err;
  1591. if (opcode == BPF_END || opcode == BPF_NEG) {
  1592. if (opcode == BPF_NEG) {
  1593. if (BPF_SRC(insn->code) != 0 ||
  1594. insn->src_reg != BPF_REG_0 ||
  1595. insn->off != 0 || insn->imm != 0) {
  1596. verbose("BPF_NEG uses reserved fields\n");
  1597. return -EINVAL;
  1598. }
  1599. } else {
  1600. if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
  1601. (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
  1602. verbose("BPF_END uses reserved fields\n");
  1603. return -EINVAL;
  1604. }
  1605. }
  1606. /* check src operand */
  1607. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1608. if (err)
  1609. return err;
  1610. if (is_pointer_value(env, insn->dst_reg)) {
  1611. verbose("R%d pointer arithmetic prohibited\n",
  1612. insn->dst_reg);
  1613. return -EACCES;
  1614. }
  1615. /* check dest operand */
  1616. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1617. if (err)
  1618. return err;
  1619. } else if (opcode == BPF_MOV) {
  1620. if (BPF_SRC(insn->code) == BPF_X) {
  1621. if (insn->imm != 0 || insn->off != 0) {
  1622. verbose("BPF_MOV uses reserved fields\n");
  1623. return -EINVAL;
  1624. }
  1625. /* check src operand */
  1626. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1627. if (err)
  1628. return err;
  1629. } else {
  1630. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1631. verbose("BPF_MOV uses reserved fields\n");
  1632. return -EINVAL;
  1633. }
  1634. }
  1635. /* check dest operand */
  1636. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  1637. if (err)
  1638. return err;
  1639. /* we are setting our register to something new, we need to
  1640. * reset its range values.
  1641. */
  1642. reset_reg_range_values(regs, insn->dst_reg);
  1643. if (BPF_SRC(insn->code) == BPF_X) {
  1644. if (BPF_CLASS(insn->code) == BPF_ALU64) {
  1645. /* case: R1 = R2
  1646. * copy register state to dest reg
  1647. */
  1648. regs[insn->dst_reg] = regs[insn->src_reg];
  1649. } else {
  1650. if (is_pointer_value(env, insn->src_reg)) {
  1651. verbose("R%d partial copy of pointer\n",
  1652. insn->src_reg);
  1653. return -EACCES;
  1654. }
  1655. mark_reg_unknown_value(regs, insn->dst_reg);
  1656. }
  1657. } else {
  1658. /* case: R = imm
  1659. * remember the value we stored into this reg
  1660. */
  1661. regs[insn->dst_reg].type = CONST_IMM;
  1662. regs[insn->dst_reg].imm = insn->imm;
  1663. regs[insn->dst_reg].max_value = insn->imm;
  1664. regs[insn->dst_reg].min_value = insn->imm;
  1665. }
  1666. } else if (opcode > BPF_END) {
  1667. verbose("invalid BPF_ALU opcode %x\n", opcode);
  1668. return -EINVAL;
  1669. } else { /* all other ALU ops: and, sub, xor, add, ... */
  1670. if (BPF_SRC(insn->code) == BPF_X) {
  1671. if (insn->imm != 0 || insn->off != 0) {
  1672. verbose("BPF_ALU uses reserved fields\n");
  1673. return -EINVAL;
  1674. }
  1675. /* check src1 operand */
  1676. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1677. if (err)
  1678. return err;
  1679. } else {
  1680. if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
  1681. verbose("BPF_ALU uses reserved fields\n");
  1682. return -EINVAL;
  1683. }
  1684. }
  1685. /* check src2 operand */
  1686. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1687. if (err)
  1688. return err;
  1689. if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
  1690. BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
  1691. verbose("div by zero\n");
  1692. return -EINVAL;
  1693. }
  1694. if ((opcode == BPF_LSH || opcode == BPF_RSH ||
  1695. opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
  1696. int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
  1697. if (insn->imm < 0 || insn->imm >= size) {
  1698. verbose("invalid shift %d\n", insn->imm);
  1699. return -EINVAL;
  1700. }
  1701. }
  1702. /* check dest operand */
  1703. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  1704. if (err)
  1705. return err;
  1706. dst_reg = &regs[insn->dst_reg];
  1707. /* first we want to adjust our ranges. */
  1708. adjust_reg_min_max_vals(env, insn);
  1709. /* pattern match 'bpf_add Rx, imm' instruction */
  1710. if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
  1711. dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) {
  1712. dst_reg->type = PTR_TO_STACK;
  1713. dst_reg->imm = insn->imm;
  1714. return 0;
  1715. } else if (opcode == BPF_ADD &&
  1716. BPF_CLASS(insn->code) == BPF_ALU64 &&
  1717. (dst_reg->type == PTR_TO_PACKET ||
  1718. (BPF_SRC(insn->code) == BPF_X &&
  1719. regs[insn->src_reg].type == PTR_TO_PACKET))) {
  1720. /* ptr_to_packet += K|X */
  1721. return check_packet_ptr_add(env, insn);
  1722. } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  1723. dst_reg->type == UNKNOWN_VALUE &&
  1724. env->allow_ptr_leaks) {
  1725. /* unknown += K|X */
  1726. return evaluate_reg_alu(env, insn);
  1727. } else if (BPF_CLASS(insn->code) == BPF_ALU64 &&
  1728. dst_reg->type == CONST_IMM &&
  1729. env->allow_ptr_leaks) {
  1730. /* reg_imm += K|X */
  1731. return evaluate_reg_imm_alu(env, insn);
  1732. } else if (is_pointer_value(env, insn->dst_reg)) {
  1733. verbose("R%d pointer arithmetic prohibited\n",
  1734. insn->dst_reg);
  1735. return -EACCES;
  1736. } else if (BPF_SRC(insn->code) == BPF_X &&
  1737. is_pointer_value(env, insn->src_reg)) {
  1738. verbose("R%d pointer arithmetic prohibited\n",
  1739. insn->src_reg);
  1740. return -EACCES;
  1741. }
  1742. /* If we did pointer math on a map value then just set it to our
  1743. * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
  1744. * loads to this register appropriately, otherwise just mark the
  1745. * register as unknown.
  1746. */
  1747. if (env->allow_ptr_leaks &&
  1748. (dst_reg->type == PTR_TO_MAP_VALUE ||
  1749. dst_reg->type == PTR_TO_MAP_VALUE_ADJ))
  1750. dst_reg->type = PTR_TO_MAP_VALUE_ADJ;
  1751. else
  1752. mark_reg_unknown_value(regs, insn->dst_reg);
  1753. }
  1754. return 0;
  1755. }
  1756. static void find_good_pkt_pointers(struct bpf_verifier_state *state,
  1757. struct bpf_reg_state *dst_reg)
  1758. {
  1759. struct bpf_reg_state *regs = state->regs, *reg;
  1760. int i;
  1761. /* LLVM can generate two kind of checks:
  1762. *
  1763. * Type 1:
  1764. *
  1765. * r2 = r3;
  1766. * r2 += 8;
  1767. * if (r2 > pkt_end) goto <handle exception>
  1768. * <access okay>
  1769. *
  1770. * Where:
  1771. * r2 == dst_reg, pkt_end == src_reg
  1772. * r2=pkt(id=n,off=8,r=0)
  1773. * r3=pkt(id=n,off=0,r=0)
  1774. *
  1775. * Type 2:
  1776. *
  1777. * r2 = r3;
  1778. * r2 += 8;
  1779. * if (pkt_end >= r2) goto <access okay>
  1780. * <handle exception>
  1781. *
  1782. * Where:
  1783. * pkt_end == dst_reg, r2 == src_reg
  1784. * r2=pkt(id=n,off=8,r=0)
  1785. * r3=pkt(id=n,off=0,r=0)
  1786. *
  1787. * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
  1788. * so that range of bytes [r3, r3 + 8) is safe to access.
  1789. */
  1790. for (i = 0; i < MAX_BPF_REG; i++)
  1791. if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
  1792. regs[i].range = dst_reg->off;
  1793. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1794. if (state->stack_slot_type[i] != STACK_SPILL)
  1795. continue;
  1796. reg = &state->spilled_regs[i / BPF_REG_SIZE];
  1797. if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
  1798. reg->range = dst_reg->off;
  1799. }
  1800. }
  1801. /* Adjusts the register min/max values in the case that the dst_reg is the
  1802. * variable register that we are working on, and src_reg is a constant or we're
  1803. * simply doing a BPF_K check.
  1804. */
  1805. static void reg_set_min_max(struct bpf_reg_state *true_reg,
  1806. struct bpf_reg_state *false_reg, u64 val,
  1807. u8 opcode)
  1808. {
  1809. switch (opcode) {
  1810. case BPF_JEQ:
  1811. /* If this is false then we know nothing Jon Snow, but if it is
  1812. * true then we know for sure.
  1813. */
  1814. true_reg->max_value = true_reg->min_value = val;
  1815. break;
  1816. case BPF_JNE:
  1817. /* If this is true we know nothing Jon Snow, but if it is false
  1818. * we know the value for sure;
  1819. */
  1820. false_reg->max_value = false_reg->min_value = val;
  1821. break;
  1822. case BPF_JGT:
  1823. /* Unsigned comparison, the minimum value is 0. */
  1824. false_reg->min_value = 0;
  1825. case BPF_JSGT:
  1826. /* If this is false then we know the maximum val is val,
  1827. * otherwise we know the min val is val+1.
  1828. */
  1829. false_reg->max_value = val;
  1830. true_reg->min_value = val + 1;
  1831. break;
  1832. case BPF_JGE:
  1833. /* Unsigned comparison, the minimum value is 0. */
  1834. false_reg->min_value = 0;
  1835. case BPF_JSGE:
  1836. /* If this is false then we know the maximum value is val - 1,
  1837. * otherwise we know the mimimum value is val.
  1838. */
  1839. false_reg->max_value = val - 1;
  1840. true_reg->min_value = val;
  1841. break;
  1842. default:
  1843. break;
  1844. }
  1845. check_reg_overflow(false_reg);
  1846. check_reg_overflow(true_reg);
  1847. }
  1848. /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
  1849. * is the variable reg.
  1850. */
  1851. static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
  1852. struct bpf_reg_state *false_reg, u64 val,
  1853. u8 opcode)
  1854. {
  1855. switch (opcode) {
  1856. case BPF_JEQ:
  1857. /* If this is false then we know nothing Jon Snow, but if it is
  1858. * true then we know for sure.
  1859. */
  1860. true_reg->max_value = true_reg->min_value = val;
  1861. break;
  1862. case BPF_JNE:
  1863. /* If this is true we know nothing Jon Snow, but if it is false
  1864. * we know the value for sure;
  1865. */
  1866. false_reg->max_value = false_reg->min_value = val;
  1867. break;
  1868. case BPF_JGT:
  1869. /* Unsigned comparison, the minimum value is 0. */
  1870. true_reg->min_value = 0;
  1871. case BPF_JSGT:
  1872. /*
  1873. * If this is false, then the val is <= the register, if it is
  1874. * true the register <= to the val.
  1875. */
  1876. false_reg->min_value = val;
  1877. true_reg->max_value = val - 1;
  1878. break;
  1879. case BPF_JGE:
  1880. /* Unsigned comparison, the minimum value is 0. */
  1881. true_reg->min_value = 0;
  1882. case BPF_JSGE:
  1883. /* If this is false then constant < register, if it is true then
  1884. * the register < constant.
  1885. */
  1886. false_reg->min_value = val + 1;
  1887. true_reg->max_value = val;
  1888. break;
  1889. default:
  1890. break;
  1891. }
  1892. check_reg_overflow(false_reg);
  1893. check_reg_overflow(true_reg);
  1894. }
  1895. static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
  1896. enum bpf_reg_type type)
  1897. {
  1898. struct bpf_reg_state *reg = &regs[regno];
  1899. if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
  1900. reg->type = type;
  1901. /* We don't need id from this point onwards anymore, thus we
  1902. * should better reset it, so that state pruning has chances
  1903. * to take effect.
  1904. */
  1905. reg->id = 0;
  1906. if (type == UNKNOWN_VALUE)
  1907. __mark_reg_unknown_value(regs, regno);
  1908. }
  1909. }
  1910. /* The logic is similar to find_good_pkt_pointers(), both could eventually
  1911. * be folded together at some point.
  1912. */
  1913. static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
  1914. enum bpf_reg_type type)
  1915. {
  1916. struct bpf_reg_state *regs = state->regs;
  1917. u32 id = regs[regno].id;
  1918. int i;
  1919. for (i = 0; i < MAX_BPF_REG; i++)
  1920. mark_map_reg(regs, i, id, type);
  1921. for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
  1922. if (state->stack_slot_type[i] != STACK_SPILL)
  1923. continue;
  1924. mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type);
  1925. }
  1926. }
  1927. static int check_cond_jmp_op(struct bpf_verifier_env *env,
  1928. struct bpf_insn *insn, int *insn_idx)
  1929. {
  1930. struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
  1931. struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
  1932. u8 opcode = BPF_OP(insn->code);
  1933. int err;
  1934. if (opcode > BPF_EXIT) {
  1935. verbose("invalid BPF_JMP opcode %x\n", opcode);
  1936. return -EINVAL;
  1937. }
  1938. if (BPF_SRC(insn->code) == BPF_X) {
  1939. if (insn->imm != 0) {
  1940. verbose("BPF_JMP uses reserved fields\n");
  1941. return -EINVAL;
  1942. }
  1943. /* check src1 operand */
  1944. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  1945. if (err)
  1946. return err;
  1947. if (is_pointer_value(env, insn->src_reg)) {
  1948. verbose("R%d pointer comparison prohibited\n",
  1949. insn->src_reg);
  1950. return -EACCES;
  1951. }
  1952. } else {
  1953. if (insn->src_reg != BPF_REG_0) {
  1954. verbose("BPF_JMP uses reserved fields\n");
  1955. return -EINVAL;
  1956. }
  1957. }
  1958. /* check src2 operand */
  1959. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  1960. if (err)
  1961. return err;
  1962. dst_reg = &regs[insn->dst_reg];
  1963. /* detect if R == 0 where R was initialized to zero earlier */
  1964. if (BPF_SRC(insn->code) == BPF_K &&
  1965. (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  1966. dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) {
  1967. if (opcode == BPF_JEQ) {
  1968. /* if (imm == imm) goto pc+off;
  1969. * only follow the goto, ignore fall-through
  1970. */
  1971. *insn_idx += insn->off;
  1972. return 0;
  1973. } else {
  1974. /* if (imm != imm) goto pc+off;
  1975. * only follow fall-through branch, since
  1976. * that's where the program will go
  1977. */
  1978. return 0;
  1979. }
  1980. }
  1981. other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
  1982. if (!other_branch)
  1983. return -EFAULT;
  1984. /* detect if we are comparing against a constant value so we can adjust
  1985. * our min/max values for our dst register.
  1986. */
  1987. if (BPF_SRC(insn->code) == BPF_X) {
  1988. if (regs[insn->src_reg].type == CONST_IMM)
  1989. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  1990. dst_reg, regs[insn->src_reg].imm,
  1991. opcode);
  1992. else if (dst_reg->type == CONST_IMM)
  1993. reg_set_min_max_inv(&other_branch->regs[insn->src_reg],
  1994. &regs[insn->src_reg], dst_reg->imm,
  1995. opcode);
  1996. } else {
  1997. reg_set_min_max(&other_branch->regs[insn->dst_reg],
  1998. dst_reg, insn->imm, opcode);
  1999. }
  2000. /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
  2001. if (BPF_SRC(insn->code) == BPF_K &&
  2002. insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
  2003. dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
  2004. /* Mark all identical map registers in each branch as either
  2005. * safe or unknown depending R == 0 or R != 0 conditional.
  2006. */
  2007. mark_map_regs(this_branch, insn->dst_reg,
  2008. opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
  2009. mark_map_regs(other_branch, insn->dst_reg,
  2010. opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
  2011. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
  2012. dst_reg->type == PTR_TO_PACKET &&
  2013. regs[insn->src_reg].type == PTR_TO_PACKET_END) {
  2014. find_good_pkt_pointers(this_branch, dst_reg);
  2015. } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
  2016. dst_reg->type == PTR_TO_PACKET_END &&
  2017. regs[insn->src_reg].type == PTR_TO_PACKET) {
  2018. find_good_pkt_pointers(other_branch, &regs[insn->src_reg]);
  2019. } else if (is_pointer_value(env, insn->dst_reg)) {
  2020. verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
  2021. return -EACCES;
  2022. }
  2023. if (log_level)
  2024. print_verifier_state(this_branch);
  2025. return 0;
  2026. }
  2027. /* return the map pointer stored inside BPF_LD_IMM64 instruction */
  2028. static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
  2029. {
  2030. u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
  2031. return (struct bpf_map *) (unsigned long) imm64;
  2032. }
  2033. /* verify BPF_LD_IMM64 instruction */
  2034. static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2035. {
  2036. struct bpf_reg_state *regs = env->cur_state.regs;
  2037. int err;
  2038. if (BPF_SIZE(insn->code) != BPF_DW) {
  2039. verbose("invalid BPF_LD_IMM insn\n");
  2040. return -EINVAL;
  2041. }
  2042. if (insn->off != 0) {
  2043. verbose("BPF_LD_IMM64 uses reserved fields\n");
  2044. return -EINVAL;
  2045. }
  2046. err = check_reg_arg(regs, insn->dst_reg, DST_OP);
  2047. if (err)
  2048. return err;
  2049. if (insn->src_reg == 0) {
  2050. u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
  2051. regs[insn->dst_reg].type = CONST_IMM;
  2052. regs[insn->dst_reg].imm = imm;
  2053. return 0;
  2054. }
  2055. /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
  2056. BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
  2057. regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
  2058. regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
  2059. return 0;
  2060. }
  2061. static bool may_access_skb(enum bpf_prog_type type)
  2062. {
  2063. switch (type) {
  2064. case BPF_PROG_TYPE_SOCKET_FILTER:
  2065. case BPF_PROG_TYPE_SCHED_CLS:
  2066. case BPF_PROG_TYPE_SCHED_ACT:
  2067. return true;
  2068. default:
  2069. return false;
  2070. }
  2071. }
  2072. /* verify safety of LD_ABS|LD_IND instructions:
  2073. * - they can only appear in the programs where ctx == skb
  2074. * - since they are wrappers of function calls, they scratch R1-R5 registers,
  2075. * preserve R6-R9, and store return value into R0
  2076. *
  2077. * Implicit input:
  2078. * ctx == skb == R6 == CTX
  2079. *
  2080. * Explicit input:
  2081. * SRC == any register
  2082. * IMM == 32-bit immediate
  2083. *
  2084. * Output:
  2085. * R0 - 8/16/32-bit skb data converted to cpu endianness
  2086. */
  2087. static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
  2088. {
  2089. struct bpf_reg_state *regs = env->cur_state.regs;
  2090. u8 mode = BPF_MODE(insn->code);
  2091. struct bpf_reg_state *reg;
  2092. int i, err;
  2093. if (!may_access_skb(env->prog->type)) {
  2094. verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
  2095. return -EINVAL;
  2096. }
  2097. if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
  2098. BPF_SIZE(insn->code) == BPF_DW ||
  2099. (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
  2100. verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
  2101. return -EINVAL;
  2102. }
  2103. /* check whether implicit source operand (register R6) is readable */
  2104. err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
  2105. if (err)
  2106. return err;
  2107. if (regs[BPF_REG_6].type != PTR_TO_CTX) {
  2108. verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
  2109. return -EINVAL;
  2110. }
  2111. if (mode == BPF_IND) {
  2112. /* check explicit source operand */
  2113. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2114. if (err)
  2115. return err;
  2116. }
  2117. /* reset caller saved regs to unreadable */
  2118. for (i = 0; i < CALLER_SAVED_REGS; i++) {
  2119. reg = regs + caller_saved[i];
  2120. reg->type = NOT_INIT;
  2121. reg->imm = 0;
  2122. }
  2123. /* mark destination R0 register as readable, since it contains
  2124. * the value fetched from the packet
  2125. */
  2126. regs[BPF_REG_0].type = UNKNOWN_VALUE;
  2127. return 0;
  2128. }
  2129. /* non-recursive DFS pseudo code
  2130. * 1 procedure DFS-iterative(G,v):
  2131. * 2 label v as discovered
  2132. * 3 let S be a stack
  2133. * 4 S.push(v)
  2134. * 5 while S is not empty
  2135. * 6 t <- S.pop()
  2136. * 7 if t is what we're looking for:
  2137. * 8 return t
  2138. * 9 for all edges e in G.adjacentEdges(t) do
  2139. * 10 if edge e is already labelled
  2140. * 11 continue with the next edge
  2141. * 12 w <- G.adjacentVertex(t,e)
  2142. * 13 if vertex w is not discovered and not explored
  2143. * 14 label e as tree-edge
  2144. * 15 label w as discovered
  2145. * 16 S.push(w)
  2146. * 17 continue at 5
  2147. * 18 else if vertex w is discovered
  2148. * 19 label e as back-edge
  2149. * 20 else
  2150. * 21 // vertex w is explored
  2151. * 22 label e as forward- or cross-edge
  2152. * 23 label t as explored
  2153. * 24 S.pop()
  2154. *
  2155. * convention:
  2156. * 0x10 - discovered
  2157. * 0x11 - discovered and fall-through edge labelled
  2158. * 0x12 - discovered and fall-through and branch edges labelled
  2159. * 0x20 - explored
  2160. */
  2161. enum {
  2162. DISCOVERED = 0x10,
  2163. EXPLORED = 0x20,
  2164. FALLTHROUGH = 1,
  2165. BRANCH = 2,
  2166. };
  2167. #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
  2168. static int *insn_stack; /* stack of insns to process */
  2169. static int cur_stack; /* current stack index */
  2170. static int *insn_state;
  2171. /* t, w, e - match pseudo-code above:
  2172. * t - index of current instruction
  2173. * w - next instruction
  2174. * e - edge
  2175. */
  2176. static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
  2177. {
  2178. if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
  2179. return 0;
  2180. if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
  2181. return 0;
  2182. if (w < 0 || w >= env->prog->len) {
  2183. verbose("jump out of range from insn %d to %d\n", t, w);
  2184. return -EINVAL;
  2185. }
  2186. if (e == BRANCH)
  2187. /* mark branch target for state pruning */
  2188. env->explored_states[w] = STATE_LIST_MARK;
  2189. if (insn_state[w] == 0) {
  2190. /* tree-edge */
  2191. insn_state[t] = DISCOVERED | e;
  2192. insn_state[w] = DISCOVERED;
  2193. if (cur_stack >= env->prog->len)
  2194. return -E2BIG;
  2195. insn_stack[cur_stack++] = w;
  2196. return 1;
  2197. } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
  2198. verbose("back-edge from insn %d to %d\n", t, w);
  2199. return -EINVAL;
  2200. } else if (insn_state[w] == EXPLORED) {
  2201. /* forward- or cross-edge */
  2202. insn_state[t] = DISCOVERED | e;
  2203. } else {
  2204. verbose("insn state internal bug\n");
  2205. return -EFAULT;
  2206. }
  2207. return 0;
  2208. }
  2209. /* non-recursive depth-first-search to detect loops in BPF program
  2210. * loop == back-edge in directed graph
  2211. */
  2212. static int check_cfg(struct bpf_verifier_env *env)
  2213. {
  2214. struct bpf_insn *insns = env->prog->insnsi;
  2215. int insn_cnt = env->prog->len;
  2216. int ret = 0;
  2217. int i, t;
  2218. insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2219. if (!insn_state)
  2220. return -ENOMEM;
  2221. insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
  2222. if (!insn_stack) {
  2223. kfree(insn_state);
  2224. return -ENOMEM;
  2225. }
  2226. insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
  2227. insn_stack[0] = 0; /* 0 is the first instruction */
  2228. cur_stack = 1;
  2229. peek_stack:
  2230. if (cur_stack == 0)
  2231. goto check_state;
  2232. t = insn_stack[cur_stack - 1];
  2233. if (BPF_CLASS(insns[t].code) == BPF_JMP) {
  2234. u8 opcode = BPF_OP(insns[t].code);
  2235. if (opcode == BPF_EXIT) {
  2236. goto mark_explored;
  2237. } else if (opcode == BPF_CALL) {
  2238. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2239. if (ret == 1)
  2240. goto peek_stack;
  2241. else if (ret < 0)
  2242. goto err_free;
  2243. if (t + 1 < insn_cnt)
  2244. env->explored_states[t + 1] = STATE_LIST_MARK;
  2245. } else if (opcode == BPF_JA) {
  2246. if (BPF_SRC(insns[t].code) != BPF_K) {
  2247. ret = -EINVAL;
  2248. goto err_free;
  2249. }
  2250. /* unconditional jump with single edge */
  2251. ret = push_insn(t, t + insns[t].off + 1,
  2252. FALLTHROUGH, env);
  2253. if (ret == 1)
  2254. goto peek_stack;
  2255. else if (ret < 0)
  2256. goto err_free;
  2257. /* tell verifier to check for equivalent states
  2258. * after every call and jump
  2259. */
  2260. if (t + 1 < insn_cnt)
  2261. env->explored_states[t + 1] = STATE_LIST_MARK;
  2262. } else {
  2263. /* conditional jump with two edges */
  2264. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2265. if (ret == 1)
  2266. goto peek_stack;
  2267. else if (ret < 0)
  2268. goto err_free;
  2269. ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
  2270. if (ret == 1)
  2271. goto peek_stack;
  2272. else if (ret < 0)
  2273. goto err_free;
  2274. }
  2275. } else {
  2276. /* all other non-branch instructions with single
  2277. * fall-through edge
  2278. */
  2279. ret = push_insn(t, t + 1, FALLTHROUGH, env);
  2280. if (ret == 1)
  2281. goto peek_stack;
  2282. else if (ret < 0)
  2283. goto err_free;
  2284. }
  2285. mark_explored:
  2286. insn_state[t] = EXPLORED;
  2287. if (cur_stack-- <= 0) {
  2288. verbose("pop stack internal bug\n");
  2289. ret = -EFAULT;
  2290. goto err_free;
  2291. }
  2292. goto peek_stack;
  2293. check_state:
  2294. for (i = 0; i < insn_cnt; i++) {
  2295. if (insn_state[i] != EXPLORED) {
  2296. verbose("unreachable insn %d\n", i);
  2297. ret = -EINVAL;
  2298. goto err_free;
  2299. }
  2300. }
  2301. ret = 0; /* cfg looks good */
  2302. err_free:
  2303. kfree(insn_state);
  2304. kfree(insn_stack);
  2305. return ret;
  2306. }
  2307. /* the following conditions reduce the number of explored insns
  2308. * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
  2309. */
  2310. static bool compare_ptrs_to_packet(struct bpf_reg_state *old,
  2311. struct bpf_reg_state *cur)
  2312. {
  2313. if (old->id != cur->id)
  2314. return false;
  2315. /* old ptr_to_packet is more conservative, since it allows smaller
  2316. * range. Ex:
  2317. * old(off=0,r=10) is equal to cur(off=0,r=20), because
  2318. * old(off=0,r=10) means that with range=10 the verifier proceeded
  2319. * further and found no issues with the program. Now we're in the same
  2320. * spot with cur(off=0,r=20), so we're safe too, since anything further
  2321. * will only be looking at most 10 bytes after this pointer.
  2322. */
  2323. if (old->off == cur->off && old->range < cur->range)
  2324. return true;
  2325. /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
  2326. * since both cannot be used for packet access and safe(old)
  2327. * pointer has smaller off that could be used for further
  2328. * 'if (ptr > data_end)' check
  2329. * Ex:
  2330. * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
  2331. * that we cannot access the packet.
  2332. * The safe range is:
  2333. * [ptr, ptr + range - off)
  2334. * so whenever off >=range, it means no safe bytes from this pointer.
  2335. * When comparing old->off <= cur->off, it means that older code
  2336. * went with smaller offset and that offset was later
  2337. * used to figure out the safe range after 'if (ptr > data_end)' check
  2338. * Say, 'old' state was explored like:
  2339. * ... R3(off=0, r=0)
  2340. * R4 = R3 + 20
  2341. * ... now R4(off=20,r=0) <-- here
  2342. * if (R4 > data_end)
  2343. * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
  2344. * ... the code further went all the way to bpf_exit.
  2345. * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
  2346. * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
  2347. * goes further, such cur_R4 will give larger safe packet range after
  2348. * 'if (R4 > data_end)' and all further insn were already good with r=20,
  2349. * so they will be good with r=30 and we can prune the search.
  2350. */
  2351. if (old->off <= cur->off &&
  2352. old->off >= old->range && cur->off >= cur->range)
  2353. return true;
  2354. return false;
  2355. }
  2356. /* compare two verifier states
  2357. *
  2358. * all states stored in state_list are known to be valid, since
  2359. * verifier reached 'bpf_exit' instruction through them
  2360. *
  2361. * this function is called when verifier exploring different branches of
  2362. * execution popped from the state stack. If it sees an old state that has
  2363. * more strict register state and more strict stack state then this execution
  2364. * branch doesn't need to be explored further, since verifier already
  2365. * concluded that more strict state leads to valid finish.
  2366. *
  2367. * Therefore two states are equivalent if register state is more conservative
  2368. * and explored stack state is more conservative than the current one.
  2369. * Example:
  2370. * explored current
  2371. * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
  2372. * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
  2373. *
  2374. * In other words if current stack state (one being explored) has more
  2375. * valid slots than old one that already passed validation, it means
  2376. * the verifier can stop exploring and conclude that current state is valid too
  2377. *
  2378. * Similarly with registers. If explored state has register type as invalid
  2379. * whereas register type in current state is meaningful, it means that
  2380. * the current state will reach 'bpf_exit' instruction safely
  2381. */
  2382. static bool states_equal(struct bpf_verifier_env *env,
  2383. struct bpf_verifier_state *old,
  2384. struct bpf_verifier_state *cur)
  2385. {
  2386. bool varlen_map_access = env->varlen_map_value_access;
  2387. struct bpf_reg_state *rold, *rcur;
  2388. int i;
  2389. for (i = 0; i < MAX_BPF_REG; i++) {
  2390. rold = &old->regs[i];
  2391. rcur = &cur->regs[i];
  2392. if (memcmp(rold, rcur, sizeof(*rold)) == 0)
  2393. continue;
  2394. /* If the ranges were not the same, but everything else was and
  2395. * we didn't do a variable access into a map then we are a-ok.
  2396. */
  2397. if (!varlen_map_access &&
  2398. memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0)
  2399. continue;
  2400. /* If we didn't map access then again we don't care about the
  2401. * mismatched range values and it's ok if our old type was
  2402. * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
  2403. */
  2404. if (rold->type == NOT_INIT ||
  2405. (!varlen_map_access && rold->type == UNKNOWN_VALUE &&
  2406. rcur->type != NOT_INIT))
  2407. continue;
  2408. if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET &&
  2409. compare_ptrs_to_packet(rold, rcur))
  2410. continue;
  2411. return false;
  2412. }
  2413. for (i = 0; i < MAX_BPF_STACK; i++) {
  2414. if (old->stack_slot_type[i] == STACK_INVALID)
  2415. continue;
  2416. if (old->stack_slot_type[i] != cur->stack_slot_type[i])
  2417. /* Ex: old explored (safe) state has STACK_SPILL in
  2418. * this stack slot, but current has has STACK_MISC ->
  2419. * this verifier states are not equivalent,
  2420. * return false to continue verification of this path
  2421. */
  2422. return false;
  2423. if (i % BPF_REG_SIZE)
  2424. continue;
  2425. if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
  2426. &cur->spilled_regs[i / BPF_REG_SIZE],
  2427. sizeof(old->spilled_regs[0])))
  2428. /* when explored and current stack slot types are
  2429. * the same, check that stored pointers types
  2430. * are the same as well.
  2431. * Ex: explored safe path could have stored
  2432. * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
  2433. * but current path has stored:
  2434. * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
  2435. * such verifier states are not equivalent.
  2436. * return false to continue verification of this path
  2437. */
  2438. return false;
  2439. else
  2440. continue;
  2441. }
  2442. return true;
  2443. }
  2444. static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
  2445. {
  2446. struct bpf_verifier_state_list *new_sl;
  2447. struct bpf_verifier_state_list *sl;
  2448. sl = env->explored_states[insn_idx];
  2449. if (!sl)
  2450. /* this 'insn_idx' instruction wasn't marked, so we will not
  2451. * be doing state search here
  2452. */
  2453. return 0;
  2454. while (sl != STATE_LIST_MARK) {
  2455. if (states_equal(env, &sl->state, &env->cur_state))
  2456. /* reached equivalent register/stack state,
  2457. * prune the search
  2458. */
  2459. return 1;
  2460. sl = sl->next;
  2461. }
  2462. /* there were no equivalent states, remember current one.
  2463. * technically the current state is not proven to be safe yet,
  2464. * but it will either reach bpf_exit (which means it's safe) or
  2465. * it will be rejected. Since there are no loops, we won't be
  2466. * seeing this 'insn_idx' instruction again on the way to bpf_exit
  2467. */
  2468. new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
  2469. if (!new_sl)
  2470. return -ENOMEM;
  2471. /* add new state to the head of linked list */
  2472. memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
  2473. new_sl->next = env->explored_states[insn_idx];
  2474. env->explored_states[insn_idx] = new_sl;
  2475. return 0;
  2476. }
  2477. static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
  2478. int insn_idx, int prev_insn_idx)
  2479. {
  2480. if (!env->analyzer_ops || !env->analyzer_ops->insn_hook)
  2481. return 0;
  2482. return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx);
  2483. }
  2484. static int do_check(struct bpf_verifier_env *env)
  2485. {
  2486. struct bpf_verifier_state *state = &env->cur_state;
  2487. struct bpf_insn *insns = env->prog->insnsi;
  2488. struct bpf_reg_state *regs = state->regs;
  2489. int insn_cnt = env->prog->len;
  2490. int insn_idx, prev_insn_idx = 0;
  2491. int insn_processed = 0;
  2492. bool do_print_state = false;
  2493. init_reg_state(regs);
  2494. insn_idx = 0;
  2495. env->varlen_map_value_access = false;
  2496. for (;;) {
  2497. struct bpf_insn *insn;
  2498. u8 class;
  2499. int err;
  2500. if (insn_idx >= insn_cnt) {
  2501. verbose("invalid insn idx %d insn_cnt %d\n",
  2502. insn_idx, insn_cnt);
  2503. return -EFAULT;
  2504. }
  2505. insn = &insns[insn_idx];
  2506. class = BPF_CLASS(insn->code);
  2507. if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
  2508. verbose("BPF program is too large. Proccessed %d insn\n",
  2509. insn_processed);
  2510. return -E2BIG;
  2511. }
  2512. err = is_state_visited(env, insn_idx);
  2513. if (err < 0)
  2514. return err;
  2515. if (err == 1) {
  2516. /* found equivalent state, can prune the search */
  2517. if (log_level) {
  2518. if (do_print_state)
  2519. verbose("\nfrom %d to %d: safe\n",
  2520. prev_insn_idx, insn_idx);
  2521. else
  2522. verbose("%d: safe\n", insn_idx);
  2523. }
  2524. goto process_bpf_exit;
  2525. }
  2526. if (log_level && do_print_state) {
  2527. verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
  2528. print_verifier_state(&env->cur_state);
  2529. do_print_state = false;
  2530. }
  2531. if (log_level) {
  2532. verbose("%d: ", insn_idx);
  2533. print_bpf_insn(insn);
  2534. }
  2535. err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
  2536. if (err)
  2537. return err;
  2538. if (class == BPF_ALU || class == BPF_ALU64) {
  2539. err = check_alu_op(env, insn);
  2540. if (err)
  2541. return err;
  2542. } else if (class == BPF_LDX) {
  2543. enum bpf_reg_type *prev_src_type, src_reg_type;
  2544. /* check for reserved fields is already done */
  2545. /* check src operand */
  2546. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2547. if (err)
  2548. return err;
  2549. err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
  2550. if (err)
  2551. return err;
  2552. src_reg_type = regs[insn->src_reg].type;
  2553. /* check that memory (src_reg + off) is readable,
  2554. * the state of dst_reg will be updated by this func
  2555. */
  2556. err = check_mem_access(env, insn->src_reg, insn->off,
  2557. BPF_SIZE(insn->code), BPF_READ,
  2558. insn->dst_reg);
  2559. if (err)
  2560. return err;
  2561. if (BPF_SIZE(insn->code) != BPF_W &&
  2562. BPF_SIZE(insn->code) != BPF_DW) {
  2563. insn_idx++;
  2564. continue;
  2565. }
  2566. prev_src_type = &env->insn_aux_data[insn_idx].ptr_type;
  2567. if (*prev_src_type == NOT_INIT) {
  2568. /* saw a valid insn
  2569. * dst_reg = *(u32 *)(src_reg + off)
  2570. * save type to validate intersecting paths
  2571. */
  2572. *prev_src_type = src_reg_type;
  2573. } else if (src_reg_type != *prev_src_type &&
  2574. (src_reg_type == PTR_TO_CTX ||
  2575. *prev_src_type == PTR_TO_CTX)) {
  2576. /* ABuser program is trying to use the same insn
  2577. * dst_reg = *(u32*) (src_reg + off)
  2578. * with different pointer types:
  2579. * src_reg == ctx in one branch and
  2580. * src_reg == stack|map in some other branch.
  2581. * Reject it.
  2582. */
  2583. verbose("same insn cannot be used with different pointers\n");
  2584. return -EINVAL;
  2585. }
  2586. } else if (class == BPF_STX) {
  2587. enum bpf_reg_type *prev_dst_type, dst_reg_type;
  2588. if (BPF_MODE(insn->code) == BPF_XADD) {
  2589. err = check_xadd(env, insn);
  2590. if (err)
  2591. return err;
  2592. insn_idx++;
  2593. continue;
  2594. }
  2595. /* check src1 operand */
  2596. err = check_reg_arg(regs, insn->src_reg, SRC_OP);
  2597. if (err)
  2598. return err;
  2599. /* check src2 operand */
  2600. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2601. if (err)
  2602. return err;
  2603. dst_reg_type = regs[insn->dst_reg].type;
  2604. /* check that memory (dst_reg + off) is writeable */
  2605. err = check_mem_access(env, insn->dst_reg, insn->off,
  2606. BPF_SIZE(insn->code), BPF_WRITE,
  2607. insn->src_reg);
  2608. if (err)
  2609. return err;
  2610. prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type;
  2611. if (*prev_dst_type == NOT_INIT) {
  2612. *prev_dst_type = dst_reg_type;
  2613. } else if (dst_reg_type != *prev_dst_type &&
  2614. (dst_reg_type == PTR_TO_CTX ||
  2615. *prev_dst_type == PTR_TO_CTX)) {
  2616. verbose("same insn cannot be used with different pointers\n");
  2617. return -EINVAL;
  2618. }
  2619. } else if (class == BPF_ST) {
  2620. if (BPF_MODE(insn->code) != BPF_MEM ||
  2621. insn->src_reg != BPF_REG_0) {
  2622. verbose("BPF_ST uses reserved fields\n");
  2623. return -EINVAL;
  2624. }
  2625. /* check src operand */
  2626. err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
  2627. if (err)
  2628. return err;
  2629. /* check that memory (dst_reg + off) is writeable */
  2630. err = check_mem_access(env, insn->dst_reg, insn->off,
  2631. BPF_SIZE(insn->code), BPF_WRITE,
  2632. -1);
  2633. if (err)
  2634. return err;
  2635. } else if (class == BPF_JMP) {
  2636. u8 opcode = BPF_OP(insn->code);
  2637. if (opcode == BPF_CALL) {
  2638. if (BPF_SRC(insn->code) != BPF_K ||
  2639. insn->off != 0 ||
  2640. insn->src_reg != BPF_REG_0 ||
  2641. insn->dst_reg != BPF_REG_0) {
  2642. verbose("BPF_CALL uses reserved fields\n");
  2643. return -EINVAL;
  2644. }
  2645. err = check_call(env, insn->imm);
  2646. if (err)
  2647. return err;
  2648. } else if (opcode == BPF_JA) {
  2649. if (BPF_SRC(insn->code) != BPF_K ||
  2650. insn->imm != 0 ||
  2651. insn->src_reg != BPF_REG_0 ||
  2652. insn->dst_reg != BPF_REG_0) {
  2653. verbose("BPF_JA uses reserved fields\n");
  2654. return -EINVAL;
  2655. }
  2656. insn_idx += insn->off + 1;
  2657. continue;
  2658. } else if (opcode == BPF_EXIT) {
  2659. if (BPF_SRC(insn->code) != BPF_K ||
  2660. insn->imm != 0 ||
  2661. insn->src_reg != BPF_REG_0 ||
  2662. insn->dst_reg != BPF_REG_0) {
  2663. verbose("BPF_EXIT uses reserved fields\n");
  2664. return -EINVAL;
  2665. }
  2666. /* eBPF calling convetion is such that R0 is used
  2667. * to return the value from eBPF program.
  2668. * Make sure that it's readable at this time
  2669. * of bpf_exit, which means that program wrote
  2670. * something into it earlier
  2671. */
  2672. err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
  2673. if (err)
  2674. return err;
  2675. if (is_pointer_value(env, BPF_REG_0)) {
  2676. verbose("R0 leaks addr as return value\n");
  2677. return -EACCES;
  2678. }
  2679. process_bpf_exit:
  2680. insn_idx = pop_stack(env, &prev_insn_idx);
  2681. if (insn_idx < 0) {
  2682. break;
  2683. } else {
  2684. do_print_state = true;
  2685. continue;
  2686. }
  2687. } else {
  2688. err = check_cond_jmp_op(env, insn, &insn_idx);
  2689. if (err)
  2690. return err;
  2691. }
  2692. } else if (class == BPF_LD) {
  2693. u8 mode = BPF_MODE(insn->code);
  2694. if (mode == BPF_ABS || mode == BPF_IND) {
  2695. err = check_ld_abs(env, insn);
  2696. if (err)
  2697. return err;
  2698. } else if (mode == BPF_IMM) {
  2699. err = check_ld_imm(env, insn);
  2700. if (err)
  2701. return err;
  2702. insn_idx++;
  2703. } else {
  2704. verbose("invalid BPF_LD mode\n");
  2705. return -EINVAL;
  2706. }
  2707. reset_reg_range_values(regs, insn->dst_reg);
  2708. } else {
  2709. verbose("unknown insn class %d\n", class);
  2710. return -EINVAL;
  2711. }
  2712. insn_idx++;
  2713. }
  2714. verbose("processed %d insns\n", insn_processed);
  2715. return 0;
  2716. }
  2717. static int check_map_prog_compatibility(struct bpf_map *map,
  2718. struct bpf_prog *prog)
  2719. {
  2720. if (prog->type == BPF_PROG_TYPE_PERF_EVENT &&
  2721. (map->map_type == BPF_MAP_TYPE_HASH ||
  2722. map->map_type == BPF_MAP_TYPE_PERCPU_HASH) &&
  2723. (map->map_flags & BPF_F_NO_PREALLOC)) {
  2724. verbose("perf_event programs can only use preallocated hash map\n");
  2725. return -EINVAL;
  2726. }
  2727. return 0;
  2728. }
  2729. /* look for pseudo eBPF instructions that access map FDs and
  2730. * replace them with actual map pointers
  2731. */
  2732. static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
  2733. {
  2734. struct bpf_insn *insn = env->prog->insnsi;
  2735. int insn_cnt = env->prog->len;
  2736. int i, j, err;
  2737. err = bpf_prog_calc_tag(env->prog);
  2738. if (err)
  2739. return err;
  2740. for (i = 0; i < insn_cnt; i++, insn++) {
  2741. if (BPF_CLASS(insn->code) == BPF_LDX &&
  2742. (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
  2743. verbose("BPF_LDX uses reserved fields\n");
  2744. return -EINVAL;
  2745. }
  2746. if (BPF_CLASS(insn->code) == BPF_STX &&
  2747. ((BPF_MODE(insn->code) != BPF_MEM &&
  2748. BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
  2749. verbose("BPF_STX uses reserved fields\n");
  2750. return -EINVAL;
  2751. }
  2752. if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
  2753. struct bpf_map *map;
  2754. struct fd f;
  2755. if (i == insn_cnt - 1 || insn[1].code != 0 ||
  2756. insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
  2757. insn[1].off != 0) {
  2758. verbose("invalid bpf_ld_imm64 insn\n");
  2759. return -EINVAL;
  2760. }
  2761. if (insn->src_reg == 0)
  2762. /* valid generic load 64-bit imm */
  2763. goto next_insn;
  2764. if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
  2765. verbose("unrecognized bpf_ld_imm64 insn\n");
  2766. return -EINVAL;
  2767. }
  2768. f = fdget(insn->imm);
  2769. map = __bpf_map_get(f);
  2770. if (IS_ERR(map)) {
  2771. verbose("fd %d is not pointing to valid bpf_map\n",
  2772. insn->imm);
  2773. return PTR_ERR(map);
  2774. }
  2775. err = check_map_prog_compatibility(map, env->prog);
  2776. if (err) {
  2777. fdput(f);
  2778. return err;
  2779. }
  2780. /* store map pointer inside BPF_LD_IMM64 instruction */
  2781. insn[0].imm = (u32) (unsigned long) map;
  2782. insn[1].imm = ((u64) (unsigned long) map) >> 32;
  2783. /* check whether we recorded this map already */
  2784. for (j = 0; j < env->used_map_cnt; j++)
  2785. if (env->used_maps[j] == map) {
  2786. fdput(f);
  2787. goto next_insn;
  2788. }
  2789. if (env->used_map_cnt >= MAX_USED_MAPS) {
  2790. fdput(f);
  2791. return -E2BIG;
  2792. }
  2793. /* hold the map. If the program is rejected by verifier,
  2794. * the map will be released by release_maps() or it
  2795. * will be used by the valid program until it's unloaded
  2796. * and all maps are released in free_bpf_prog_info()
  2797. */
  2798. map = bpf_map_inc(map, false);
  2799. if (IS_ERR(map)) {
  2800. fdput(f);
  2801. return PTR_ERR(map);
  2802. }
  2803. env->used_maps[env->used_map_cnt++] = map;
  2804. fdput(f);
  2805. next_insn:
  2806. insn++;
  2807. i++;
  2808. }
  2809. }
  2810. /* now all pseudo BPF_LD_IMM64 instructions load valid
  2811. * 'struct bpf_map *' into a register instead of user map_fd.
  2812. * These pointers will be used later by verifier to validate map access.
  2813. */
  2814. return 0;
  2815. }
  2816. /* drop refcnt of maps used by the rejected program */
  2817. static void release_maps(struct bpf_verifier_env *env)
  2818. {
  2819. int i;
  2820. for (i = 0; i < env->used_map_cnt; i++)
  2821. bpf_map_put(env->used_maps[i]);
  2822. }
  2823. /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
  2824. static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
  2825. {
  2826. struct bpf_insn *insn = env->prog->insnsi;
  2827. int insn_cnt = env->prog->len;
  2828. int i;
  2829. for (i = 0; i < insn_cnt; i++, insn++)
  2830. if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
  2831. insn->src_reg = 0;
  2832. }
  2833. /* convert load instructions that access fields of 'struct __sk_buff'
  2834. * into sequence of instructions that access fields of 'struct sk_buff'
  2835. */
  2836. static int convert_ctx_accesses(struct bpf_verifier_env *env)
  2837. {
  2838. const struct bpf_verifier_ops *ops = env->prog->aux->ops;
  2839. const int insn_cnt = env->prog->len;
  2840. struct bpf_insn insn_buf[16], *insn;
  2841. struct bpf_prog *new_prog;
  2842. enum bpf_access_type type;
  2843. int i, cnt, delta = 0;
  2844. if (ops->gen_prologue) {
  2845. cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
  2846. env->prog);
  2847. if (cnt >= ARRAY_SIZE(insn_buf)) {
  2848. verbose("bpf verifier is misconfigured\n");
  2849. return -EINVAL;
  2850. } else if (cnt) {
  2851. new_prog = bpf_patch_insn_single(env->prog, 0,
  2852. insn_buf, cnt);
  2853. if (!new_prog)
  2854. return -ENOMEM;
  2855. env->prog = new_prog;
  2856. delta += cnt - 1;
  2857. }
  2858. }
  2859. if (!ops->convert_ctx_access)
  2860. return 0;
  2861. insn = env->prog->insnsi + delta;
  2862. for (i = 0; i < insn_cnt; i++, insn++) {
  2863. if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
  2864. insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
  2865. insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
  2866. insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
  2867. type = BPF_READ;
  2868. else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
  2869. insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
  2870. insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
  2871. insn->code == (BPF_STX | BPF_MEM | BPF_DW))
  2872. type = BPF_WRITE;
  2873. else
  2874. continue;
  2875. if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX)
  2876. continue;
  2877. cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog);
  2878. if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
  2879. verbose("bpf verifier is misconfigured\n");
  2880. return -EINVAL;
  2881. }
  2882. new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf,
  2883. cnt);
  2884. if (!new_prog)
  2885. return -ENOMEM;
  2886. delta += cnt - 1;
  2887. /* keep walking new program and skip insns we just inserted */
  2888. env->prog = new_prog;
  2889. insn = new_prog->insnsi + i + delta;
  2890. }
  2891. return 0;
  2892. }
  2893. static void free_states(struct bpf_verifier_env *env)
  2894. {
  2895. struct bpf_verifier_state_list *sl, *sln;
  2896. int i;
  2897. if (!env->explored_states)
  2898. return;
  2899. for (i = 0; i < env->prog->len; i++) {
  2900. sl = env->explored_states[i];
  2901. if (sl)
  2902. while (sl != STATE_LIST_MARK) {
  2903. sln = sl->next;
  2904. kfree(sl);
  2905. sl = sln;
  2906. }
  2907. }
  2908. kfree(env->explored_states);
  2909. }
  2910. int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
  2911. {
  2912. char __user *log_ubuf = NULL;
  2913. struct bpf_verifier_env *env;
  2914. int ret = -EINVAL;
  2915. /* 'struct bpf_verifier_env' can be global, but since it's not small,
  2916. * allocate/free it every time bpf_check() is called
  2917. */
  2918. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  2919. if (!env)
  2920. return -ENOMEM;
  2921. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  2922. (*prog)->len);
  2923. ret = -ENOMEM;
  2924. if (!env->insn_aux_data)
  2925. goto err_free_env;
  2926. env->prog = *prog;
  2927. /* grab the mutex to protect few globals used by verifier */
  2928. mutex_lock(&bpf_verifier_lock);
  2929. if (attr->log_level || attr->log_buf || attr->log_size) {
  2930. /* user requested verbose verifier output
  2931. * and supplied buffer to store the verification trace
  2932. */
  2933. log_level = attr->log_level;
  2934. log_ubuf = (char __user *) (unsigned long) attr->log_buf;
  2935. log_size = attr->log_size;
  2936. log_len = 0;
  2937. ret = -EINVAL;
  2938. /* log_* values have to be sane */
  2939. if (log_size < 128 || log_size > UINT_MAX >> 8 ||
  2940. log_level == 0 || log_ubuf == NULL)
  2941. goto err_unlock;
  2942. ret = -ENOMEM;
  2943. log_buf = vmalloc(log_size);
  2944. if (!log_buf)
  2945. goto err_unlock;
  2946. } else {
  2947. log_level = 0;
  2948. }
  2949. ret = replace_map_fd_with_map_ptr(env);
  2950. if (ret < 0)
  2951. goto skip_full_check;
  2952. env->explored_states = kcalloc(env->prog->len,
  2953. sizeof(struct bpf_verifier_state_list *),
  2954. GFP_USER);
  2955. ret = -ENOMEM;
  2956. if (!env->explored_states)
  2957. goto skip_full_check;
  2958. ret = check_cfg(env);
  2959. if (ret < 0)
  2960. goto skip_full_check;
  2961. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  2962. ret = do_check(env);
  2963. skip_full_check:
  2964. while (pop_stack(env, NULL) >= 0);
  2965. free_states(env);
  2966. if (ret == 0)
  2967. /* program is valid, convert *(u32*)(ctx + off) accesses */
  2968. ret = convert_ctx_accesses(env);
  2969. if (log_level && log_len >= log_size - 1) {
  2970. BUG_ON(log_len >= log_size);
  2971. /* verifier log exceeded user supplied buffer */
  2972. ret = -ENOSPC;
  2973. /* fall through to return what was recorded */
  2974. }
  2975. /* copy verifier log back to user space including trailing zero */
  2976. if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
  2977. ret = -EFAULT;
  2978. goto free_log_buf;
  2979. }
  2980. if (ret == 0 && env->used_map_cnt) {
  2981. /* if program passed verifier, update used_maps in bpf_prog_info */
  2982. env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
  2983. sizeof(env->used_maps[0]),
  2984. GFP_KERNEL);
  2985. if (!env->prog->aux->used_maps) {
  2986. ret = -ENOMEM;
  2987. goto free_log_buf;
  2988. }
  2989. memcpy(env->prog->aux->used_maps, env->used_maps,
  2990. sizeof(env->used_maps[0]) * env->used_map_cnt);
  2991. env->prog->aux->used_map_cnt = env->used_map_cnt;
  2992. /* program is valid. Convert pseudo bpf_ld_imm64 into generic
  2993. * bpf_ld_imm64 instructions
  2994. */
  2995. convert_pseudo_ld_imm64(env);
  2996. }
  2997. free_log_buf:
  2998. if (log_level)
  2999. vfree(log_buf);
  3000. if (!env->prog->aux->used_maps)
  3001. /* if we didn't copy map pointers into bpf_prog_info, release
  3002. * them now. Otherwise free_bpf_prog_info() will release them.
  3003. */
  3004. release_maps(env);
  3005. *prog = env->prog;
  3006. err_unlock:
  3007. mutex_unlock(&bpf_verifier_lock);
  3008. vfree(env->insn_aux_data);
  3009. err_free_env:
  3010. kfree(env);
  3011. return ret;
  3012. }
  3013. int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
  3014. void *priv)
  3015. {
  3016. struct bpf_verifier_env *env;
  3017. int ret;
  3018. env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
  3019. if (!env)
  3020. return -ENOMEM;
  3021. env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
  3022. prog->len);
  3023. ret = -ENOMEM;
  3024. if (!env->insn_aux_data)
  3025. goto err_free_env;
  3026. env->prog = prog;
  3027. env->analyzer_ops = ops;
  3028. env->analyzer_priv = priv;
  3029. /* grab the mutex to protect few globals used by verifier */
  3030. mutex_lock(&bpf_verifier_lock);
  3031. log_level = 0;
  3032. env->explored_states = kcalloc(env->prog->len,
  3033. sizeof(struct bpf_verifier_state_list *),
  3034. GFP_KERNEL);
  3035. ret = -ENOMEM;
  3036. if (!env->explored_states)
  3037. goto skip_full_check;
  3038. ret = check_cfg(env);
  3039. if (ret < 0)
  3040. goto skip_full_check;
  3041. env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
  3042. ret = do_check(env);
  3043. skip_full_check:
  3044. while (pop_stack(env, NULL) >= 0);
  3045. free_states(env);
  3046. mutex_unlock(&bpf_verifier_lock);
  3047. vfree(env->insn_aux_data);
  3048. err_free_env:
  3049. kfree(env);
  3050. return ret;
  3051. }
  3052. EXPORT_SYMBOL_GPL(bpf_analyzer);