verifier.c 129 KB

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