verifier.c 112 KB

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