verifier.c 131 KB

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