btf.c 241 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018 Facebook */
  3. #include <uapi/linux/btf.h>
  4. #include <uapi/linux/bpf.h>
  5. #include <uapi/linux/bpf_perf_event.h>
  6. #include <uapi/linux/types.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/compiler.h>
  9. #include <linux/ctype.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/anon_inodes.h>
  13. #include <linux/file.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/kernel.h>
  16. #include <linux/idr.h>
  17. #include <linux/sort.h>
  18. #include <linux/bpf_verifier.h>
  19. #include <linux/btf.h>
  20. #include <linux/btf_ids.h>
  21. #include <linux/bpf.h>
  22. #include <linux/bpf_lsm.h>
  23. #include <linux/skmsg.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/bsearch.h>
  26. #include <linux/kobject.h>
  27. #include <linux/sysfs.h>
  28. #include <net/netfilter/nf_bpf_link.h>
  29. #include <net/sock.h>
  30. #include <net/xdp.h>
  31. #include "../tools/lib/bpf/relo_core.h"
  32. /* BTF (BPF Type Format) is the meta data format which describes
  33. * the data types of BPF program/map. Hence, it basically focus
  34. * on the C programming language which the modern BPF is primary
  35. * using.
  36. *
  37. * ELF Section:
  38. * ~~~~~~~~~~~
  39. * The BTF data is stored under the ".BTF" ELF section
  40. *
  41. * struct btf_type:
  42. * ~~~~~~~~~~~~~~~
  43. * Each 'struct btf_type' object describes a C data type.
  44. * Depending on the type it is describing, a 'struct btf_type'
  45. * object may be followed by more data. F.e.
  46. * To describe an array, 'struct btf_type' is followed by
  47. * 'struct btf_array'.
  48. *
  49. * 'struct btf_type' and any extra data following it are
  50. * 4 bytes aligned.
  51. *
  52. * Type section:
  53. * ~~~~~~~~~~~~~
  54. * The BTF type section contains a list of 'struct btf_type' objects.
  55. * Each one describes a C type. Recall from the above section
  56. * that a 'struct btf_type' object could be immediately followed by extra
  57. * data in order to describe some particular C types.
  58. *
  59. * type_id:
  60. * ~~~~~~~
  61. * Each btf_type object is identified by a type_id. The type_id
  62. * is implicitly implied by the location of the btf_type object in
  63. * the BTF type section. The first one has type_id 1. The second
  64. * one has type_id 2...etc. Hence, an earlier btf_type has
  65. * a smaller type_id.
  66. *
  67. * A btf_type object may refer to another btf_type object by using
  68. * type_id (i.e. the "type" in the "struct btf_type").
  69. *
  70. * NOTE that we cannot assume any reference-order.
  71. * A btf_type object can refer to an earlier btf_type object
  72. * but it can also refer to a later btf_type object.
  73. *
  74. * For example, to describe "const void *". A btf_type
  75. * object describing "const" may refer to another btf_type
  76. * object describing "void *". This type-reference is done
  77. * by specifying type_id:
  78. *
  79. * [1] CONST (anon) type_id=2
  80. * [2] PTR (anon) type_id=0
  81. *
  82. * The above is the btf_verifier debug log:
  83. * - Each line started with "[?]" is a btf_type object
  84. * - [?] is the type_id of the btf_type object.
  85. * - CONST/PTR is the BTF_KIND_XXX
  86. * - "(anon)" is the name of the type. It just
  87. * happens that CONST and PTR has no name.
  88. * - type_id=XXX is the 'u32 type' in btf_type
  89. *
  90. * NOTE: "void" has type_id 0
  91. *
  92. * String section:
  93. * ~~~~~~~~~~~~~~
  94. * The BTF string section contains the names used by the type section.
  95. * Each string is referred by an "offset" from the beginning of the
  96. * string section.
  97. *
  98. * Each string is '\0' terminated.
  99. *
  100. * The first character in the string section must be '\0'
  101. * which is used to mean 'anonymous'. Some btf_type may not
  102. * have a name.
  103. */
  104. /* BTF verification:
  105. *
  106. * To verify BTF data, two passes are needed.
  107. *
  108. * Pass #1
  109. * ~~~~~~~
  110. * The first pass is to collect all btf_type objects to
  111. * an array: "btf->types".
  112. *
  113. * Depending on the C type that a btf_type is describing,
  114. * a btf_type may be followed by extra data. We don't know
  115. * how many btf_type is there, and more importantly we don't
  116. * know where each btf_type is located in the type section.
  117. *
  118. * Without knowing the location of each type_id, most verifications
  119. * cannot be done. e.g. an earlier btf_type may refer to a later
  120. * btf_type (recall the "const void *" above), so we cannot
  121. * check this type-reference in the first pass.
  122. *
  123. * In the first pass, it still does some verifications (e.g.
  124. * checking the name is a valid offset to the string section).
  125. *
  126. * Pass #2
  127. * ~~~~~~~
  128. * The main focus is to resolve a btf_type that is referring
  129. * to another type.
  130. *
  131. * We have to ensure the referring type:
  132. * 1) does exist in the BTF (i.e. in btf->types[])
  133. * 2) does not cause a loop:
  134. * struct A {
  135. * struct B b;
  136. * };
  137. *
  138. * struct B {
  139. * struct A a;
  140. * };
  141. *
  142. * btf_type_needs_resolve() decides if a btf_type needs
  143. * to be resolved.
  144. *
  145. * The needs_resolve type implements the "resolve()" ops which
  146. * essentially does a DFS and detects backedge.
  147. *
  148. * During resolve (or DFS), different C types have different
  149. * "RESOLVED" conditions.
  150. *
  151. * When resolving a BTF_KIND_STRUCT, we need to resolve all its
  152. * members because a member is always referring to another
  153. * type. A struct's member can be treated as "RESOLVED" if
  154. * it is referring to a BTF_KIND_PTR. Otherwise, the
  155. * following valid C struct would be rejected:
  156. *
  157. * struct A {
  158. * int m;
  159. * struct A *a;
  160. * };
  161. *
  162. * When resolving a BTF_KIND_PTR, it needs to keep resolving if
  163. * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
  164. * detect a pointer loop, e.g.:
  165. * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
  166. * ^ |
  167. * +-----------------------------------------+
  168. *
  169. */
  170. #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
  171. #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
  172. #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
  173. #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
  174. #define BITS_ROUNDUP_BYTES(bits) \
  175. (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
  176. #define BTF_INFO_MASK 0x9f00ffff
  177. #define BTF_INT_MASK 0x0fffffff
  178. #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
  179. #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
  180. /* 16MB for 64k structs and each has 16 members and
  181. * a few MB spaces for the string section.
  182. * The hard limit is S32_MAX.
  183. */
  184. #define BTF_MAX_SIZE (16 * 1024 * 1024)
  185. #define for_each_member_from(i, from, struct_type, member) \
  186. for (i = from, member = btf_type_member(struct_type) + from; \
  187. i < btf_type_vlen(struct_type); \
  188. i++, member++)
  189. #define for_each_vsi_from(i, from, struct_type, member) \
  190. for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
  191. i < btf_type_vlen(struct_type); \
  192. i++, member++)
  193. DEFINE_IDR(btf_idr);
  194. DEFINE_SPINLOCK(btf_idr_lock);
  195. enum btf_kfunc_hook {
  196. BTF_KFUNC_HOOK_COMMON,
  197. BTF_KFUNC_HOOK_XDP,
  198. BTF_KFUNC_HOOK_TC,
  199. BTF_KFUNC_HOOK_STRUCT_OPS,
  200. BTF_KFUNC_HOOK_TRACING,
  201. BTF_KFUNC_HOOK_SYSCALL,
  202. BTF_KFUNC_HOOK_FMODRET,
  203. BTF_KFUNC_HOOK_CGROUP,
  204. BTF_KFUNC_HOOK_SCHED_ACT,
  205. BTF_KFUNC_HOOK_SK_SKB,
  206. BTF_KFUNC_HOOK_SOCKET_FILTER,
  207. BTF_KFUNC_HOOK_LWT,
  208. BTF_KFUNC_HOOK_NETFILTER,
  209. BTF_KFUNC_HOOK_KPROBE,
  210. BTF_KFUNC_HOOK_MAX,
  211. };
  212. enum {
  213. BTF_KFUNC_SET_MAX_CNT = 256,
  214. BTF_DTOR_KFUNC_MAX_CNT = 256,
  215. BTF_KFUNC_FILTER_MAX_CNT = 16,
  216. };
  217. struct btf_kfunc_hook_filter {
  218. btf_kfunc_filter_t filters[BTF_KFUNC_FILTER_MAX_CNT];
  219. u32 nr_filters;
  220. };
  221. struct btf_kfunc_set_tab {
  222. struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX];
  223. struct btf_kfunc_hook_filter hook_filters[BTF_KFUNC_HOOK_MAX];
  224. };
  225. struct btf_id_dtor_kfunc_tab {
  226. u32 cnt;
  227. struct btf_id_dtor_kfunc dtors[];
  228. };
  229. struct btf_struct_ops_tab {
  230. u32 cnt;
  231. u32 capacity;
  232. struct bpf_struct_ops_desc ops[];
  233. };
  234. struct btf {
  235. void *data;
  236. struct btf_type **types;
  237. u32 *resolved_ids;
  238. u32 *resolved_sizes;
  239. const char *strings;
  240. void *nohdr_data;
  241. struct btf_header hdr;
  242. u32 nr_types; /* includes VOID for base BTF */
  243. u32 types_size;
  244. u32 data_size;
  245. refcount_t refcnt;
  246. u32 id;
  247. struct rcu_head rcu;
  248. struct btf_kfunc_set_tab *kfunc_set_tab;
  249. struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab;
  250. struct btf_struct_metas *struct_meta_tab;
  251. struct btf_struct_ops_tab *struct_ops_tab;
  252. /* split BTF support */
  253. struct btf *base_btf;
  254. u32 start_id; /* first type ID in this BTF (0 for base BTF) */
  255. u32 start_str_off; /* first string offset (0 for base BTF) */
  256. char name[MODULE_NAME_LEN];
  257. bool kernel_btf;
  258. __u32 *base_id_map; /* map from distilled base BTF -> vmlinux BTF ids */
  259. };
  260. enum verifier_phase {
  261. CHECK_META,
  262. CHECK_TYPE,
  263. };
  264. struct resolve_vertex {
  265. const struct btf_type *t;
  266. u32 type_id;
  267. u16 next_member;
  268. };
  269. enum visit_state {
  270. NOT_VISITED,
  271. VISITED,
  272. RESOLVED,
  273. };
  274. enum resolve_mode {
  275. RESOLVE_TBD, /* To Be Determined */
  276. RESOLVE_PTR, /* Resolving for Pointer */
  277. RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
  278. * or array
  279. */
  280. };
  281. #define MAX_RESOLVE_DEPTH 32
  282. struct btf_sec_info {
  283. u32 off;
  284. u32 len;
  285. };
  286. struct btf_verifier_env {
  287. struct btf *btf;
  288. u8 *visit_states;
  289. struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
  290. struct bpf_verifier_log log;
  291. u32 log_type_id;
  292. u32 top_stack;
  293. enum verifier_phase phase;
  294. enum resolve_mode resolve_mode;
  295. };
  296. static const char * const btf_kind_str[NR_BTF_KINDS] = {
  297. [BTF_KIND_UNKN] = "UNKNOWN",
  298. [BTF_KIND_INT] = "INT",
  299. [BTF_KIND_PTR] = "PTR",
  300. [BTF_KIND_ARRAY] = "ARRAY",
  301. [BTF_KIND_STRUCT] = "STRUCT",
  302. [BTF_KIND_UNION] = "UNION",
  303. [BTF_KIND_ENUM] = "ENUM",
  304. [BTF_KIND_FWD] = "FWD",
  305. [BTF_KIND_TYPEDEF] = "TYPEDEF",
  306. [BTF_KIND_VOLATILE] = "VOLATILE",
  307. [BTF_KIND_CONST] = "CONST",
  308. [BTF_KIND_RESTRICT] = "RESTRICT",
  309. [BTF_KIND_FUNC] = "FUNC",
  310. [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
  311. [BTF_KIND_VAR] = "VAR",
  312. [BTF_KIND_DATASEC] = "DATASEC",
  313. [BTF_KIND_FLOAT] = "FLOAT",
  314. [BTF_KIND_DECL_TAG] = "DECL_TAG",
  315. [BTF_KIND_TYPE_TAG] = "TYPE_TAG",
  316. [BTF_KIND_ENUM64] = "ENUM64",
  317. };
  318. const char *btf_type_str(const struct btf_type *t)
  319. {
  320. return btf_kind_str[BTF_INFO_KIND(t->info)];
  321. }
  322. /* Chunk size we use in safe copy of data to be shown. */
  323. #define BTF_SHOW_OBJ_SAFE_SIZE 32
  324. /*
  325. * This is the maximum size of a base type value (equivalent to a
  326. * 128-bit int); if we are at the end of our safe buffer and have
  327. * less than 16 bytes space we can't be assured of being able
  328. * to copy the next type safely, so in such cases we will initiate
  329. * a new copy.
  330. */
  331. #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
  332. /* Type name size */
  333. #define BTF_SHOW_NAME_SIZE 80
  334. /*
  335. * The suffix of a type that indicates it cannot alias another type when
  336. * comparing BTF IDs for kfunc invocations.
  337. */
  338. #define NOCAST_ALIAS_SUFFIX "___init"
  339. /*
  340. * Common data to all BTF show operations. Private show functions can add
  341. * their own data to a structure containing a struct btf_show and consult it
  342. * in the show callback. See btf_type_show() below.
  343. *
  344. * One challenge with showing nested data is we want to skip 0-valued
  345. * data, but in order to figure out whether a nested object is all zeros
  346. * we need to walk through it. As a result, we need to make two passes
  347. * when handling structs, unions and arrays; the first path simply looks
  348. * for nonzero data, while the second actually does the display. The first
  349. * pass is signalled by show->state.depth_check being set, and if we
  350. * encounter a non-zero value we set show->state.depth_to_show to
  351. * the depth at which we encountered it. When we have completed the
  352. * first pass, we will know if anything needs to be displayed if
  353. * depth_to_show > depth. See btf_[struct,array]_show() for the
  354. * implementation of this.
  355. *
  356. * Another problem is we want to ensure the data for display is safe to
  357. * access. To support this, the anonymous "struct {} obj" tracks the data
  358. * object and our safe copy of it. We copy portions of the data needed
  359. * to the object "copy" buffer, but because its size is limited to
  360. * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
  361. * traverse larger objects for display.
  362. *
  363. * The various data type show functions all start with a call to
  364. * btf_show_start_type() which returns a pointer to the safe copy
  365. * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
  366. * raw data itself). btf_show_obj_safe() is responsible for
  367. * using copy_from_kernel_nofault() to update the safe data if necessary
  368. * as we traverse the object's data. skbuff-like semantics are
  369. * used:
  370. *
  371. * - obj.head points to the start of the toplevel object for display
  372. * - obj.size is the size of the toplevel object
  373. * - obj.data points to the current point in the original data at
  374. * which our safe data starts. obj.data will advance as we copy
  375. * portions of the data.
  376. *
  377. * In most cases a single copy will suffice, but larger data structures
  378. * such as "struct task_struct" will require many copies. The logic in
  379. * btf_show_obj_safe() handles the logic that determines if a new
  380. * copy_from_kernel_nofault() is needed.
  381. */
  382. struct btf_show {
  383. u64 flags;
  384. void *target; /* target of show operation (seq file, buffer) */
  385. __printf(2, 0) void (*showfn)(struct btf_show *show, const char *fmt, va_list args);
  386. const struct btf *btf;
  387. /* below are used during iteration */
  388. struct {
  389. u8 depth;
  390. u8 depth_to_show;
  391. u8 depth_check;
  392. u8 array_member:1,
  393. array_terminated:1;
  394. u16 array_encoding;
  395. u32 type_id;
  396. int status; /* non-zero for error */
  397. const struct btf_type *type;
  398. const struct btf_member *member;
  399. char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */
  400. } state;
  401. struct {
  402. u32 size;
  403. void *head;
  404. void *data;
  405. u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];
  406. } obj;
  407. };
  408. struct btf_kind_operations {
  409. s32 (*check_meta)(struct btf_verifier_env *env,
  410. const struct btf_type *t,
  411. u32 meta_left);
  412. int (*resolve)(struct btf_verifier_env *env,
  413. const struct resolve_vertex *v);
  414. int (*check_member)(struct btf_verifier_env *env,
  415. const struct btf_type *struct_type,
  416. const struct btf_member *member,
  417. const struct btf_type *member_type);
  418. int (*check_kflag_member)(struct btf_verifier_env *env,
  419. const struct btf_type *struct_type,
  420. const struct btf_member *member,
  421. const struct btf_type *member_type);
  422. void (*log_details)(struct btf_verifier_env *env,
  423. const struct btf_type *t);
  424. void (*show)(const struct btf *btf, const struct btf_type *t,
  425. u32 type_id, void *data, u8 bits_offsets,
  426. struct btf_show *show);
  427. };
  428. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
  429. static struct btf_type btf_void;
  430. static int btf_resolve(struct btf_verifier_env *env,
  431. const struct btf_type *t, u32 type_id);
  432. static int btf_func_check(struct btf_verifier_env *env,
  433. const struct btf_type *t);
  434. static bool btf_type_is_modifier(const struct btf_type *t)
  435. {
  436. /* Some of them is not strictly a C modifier
  437. * but they are grouped into the same bucket
  438. * for BTF concern:
  439. * A type (t) that refers to another
  440. * type through t->type AND its size cannot
  441. * be determined without following the t->type.
  442. *
  443. * ptr does not fall into this bucket
  444. * because its size is always sizeof(void *).
  445. */
  446. switch (BTF_INFO_KIND(t->info)) {
  447. case BTF_KIND_TYPEDEF:
  448. case BTF_KIND_VOLATILE:
  449. case BTF_KIND_CONST:
  450. case BTF_KIND_RESTRICT:
  451. case BTF_KIND_TYPE_TAG:
  452. return true;
  453. }
  454. return false;
  455. }
  456. bool btf_type_is_void(const struct btf_type *t)
  457. {
  458. return t == &btf_void;
  459. }
  460. static bool btf_type_is_datasec(const struct btf_type *t)
  461. {
  462. return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
  463. }
  464. static bool btf_type_is_decl_tag(const struct btf_type *t)
  465. {
  466. return BTF_INFO_KIND(t->info) == BTF_KIND_DECL_TAG;
  467. }
  468. static bool btf_type_nosize(const struct btf_type *t)
  469. {
  470. return btf_type_is_void(t) || btf_type_is_fwd(t) ||
  471. btf_type_is_func(t) || btf_type_is_func_proto(t) ||
  472. btf_type_is_decl_tag(t);
  473. }
  474. static bool btf_type_nosize_or_null(const struct btf_type *t)
  475. {
  476. return !t || btf_type_nosize(t);
  477. }
  478. static bool btf_type_is_decl_tag_target(const struct btf_type *t)
  479. {
  480. return btf_type_is_func(t) || btf_type_is_struct(t) ||
  481. btf_type_is_var(t) || btf_type_is_typedef(t);
  482. }
  483. bool btf_is_vmlinux(const struct btf *btf)
  484. {
  485. return btf->kernel_btf && !btf->base_btf;
  486. }
  487. u32 btf_nr_types(const struct btf *btf)
  488. {
  489. u32 total = 0;
  490. while (btf) {
  491. total += btf->nr_types;
  492. btf = btf->base_btf;
  493. }
  494. return total;
  495. }
  496. s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
  497. {
  498. const struct btf_type *t;
  499. const char *tname;
  500. u32 i, total;
  501. total = btf_nr_types(btf);
  502. for (i = 1; i < total; i++) {
  503. t = btf_type_by_id(btf, i);
  504. if (BTF_INFO_KIND(t->info) != kind)
  505. continue;
  506. tname = btf_name_by_offset(btf, t->name_off);
  507. if (!strcmp(tname, name))
  508. return i;
  509. }
  510. return -ENOENT;
  511. }
  512. s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)
  513. {
  514. struct btf *btf;
  515. s32 ret;
  516. int id;
  517. btf = bpf_get_btf_vmlinux();
  518. if (IS_ERR(btf))
  519. return PTR_ERR(btf);
  520. if (!btf)
  521. return -EINVAL;
  522. ret = btf_find_by_name_kind(btf, name, kind);
  523. /* ret is never zero, since btf_find_by_name_kind returns
  524. * positive btf_id or negative error.
  525. */
  526. if (ret > 0) {
  527. btf_get(btf);
  528. *btf_p = btf;
  529. return ret;
  530. }
  531. /* If name is not found in vmlinux's BTF then search in module's BTFs */
  532. spin_lock_bh(&btf_idr_lock);
  533. idr_for_each_entry(&btf_idr, btf, id) {
  534. if (!btf_is_module(btf))
  535. continue;
  536. /* linear search could be slow hence unlock/lock
  537. * the IDR to avoiding holding it for too long
  538. */
  539. btf_get(btf);
  540. spin_unlock_bh(&btf_idr_lock);
  541. ret = btf_find_by_name_kind(btf, name, kind);
  542. if (ret > 0) {
  543. *btf_p = btf;
  544. return ret;
  545. }
  546. btf_put(btf);
  547. spin_lock_bh(&btf_idr_lock);
  548. }
  549. spin_unlock_bh(&btf_idr_lock);
  550. return ret;
  551. }
  552. const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
  553. u32 id, u32 *res_id)
  554. {
  555. const struct btf_type *t = btf_type_by_id(btf, id);
  556. while (btf_type_is_modifier(t)) {
  557. id = t->type;
  558. t = btf_type_by_id(btf, t->type);
  559. }
  560. if (res_id)
  561. *res_id = id;
  562. return t;
  563. }
  564. const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
  565. u32 id, u32 *res_id)
  566. {
  567. const struct btf_type *t;
  568. t = btf_type_skip_modifiers(btf, id, NULL);
  569. if (!btf_type_is_ptr(t))
  570. return NULL;
  571. return btf_type_skip_modifiers(btf, t->type, res_id);
  572. }
  573. const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
  574. u32 id, u32 *res_id)
  575. {
  576. const struct btf_type *ptype;
  577. ptype = btf_type_resolve_ptr(btf, id, res_id);
  578. if (ptype && btf_type_is_func_proto(ptype))
  579. return ptype;
  580. return NULL;
  581. }
  582. /* Types that act only as a source, not sink or intermediate
  583. * type when resolving.
  584. */
  585. static bool btf_type_is_resolve_source_only(const struct btf_type *t)
  586. {
  587. return btf_type_is_var(t) ||
  588. btf_type_is_decl_tag(t) ||
  589. btf_type_is_datasec(t);
  590. }
  591. /* What types need to be resolved?
  592. *
  593. * btf_type_is_modifier() is an obvious one.
  594. *
  595. * btf_type_is_struct() because its member refers to
  596. * another type (through member->type).
  597. *
  598. * btf_type_is_var() because the variable refers to
  599. * another type. btf_type_is_datasec() holds multiple
  600. * btf_type_is_var() types that need resolving.
  601. *
  602. * btf_type_is_array() because its element (array->type)
  603. * refers to another type. Array can be thought of a
  604. * special case of struct while array just has the same
  605. * member-type repeated by array->nelems of times.
  606. */
  607. static bool btf_type_needs_resolve(const struct btf_type *t)
  608. {
  609. return btf_type_is_modifier(t) ||
  610. btf_type_is_ptr(t) ||
  611. btf_type_is_struct(t) ||
  612. btf_type_is_array(t) ||
  613. btf_type_is_var(t) ||
  614. btf_type_is_func(t) ||
  615. btf_type_is_decl_tag(t) ||
  616. btf_type_is_datasec(t);
  617. }
  618. /* t->size can be used */
  619. static bool btf_type_has_size(const struct btf_type *t)
  620. {
  621. switch (BTF_INFO_KIND(t->info)) {
  622. case BTF_KIND_INT:
  623. case BTF_KIND_STRUCT:
  624. case BTF_KIND_UNION:
  625. case BTF_KIND_ENUM:
  626. case BTF_KIND_DATASEC:
  627. case BTF_KIND_FLOAT:
  628. case BTF_KIND_ENUM64:
  629. return true;
  630. }
  631. return false;
  632. }
  633. static const char *btf_int_encoding_str(u8 encoding)
  634. {
  635. if (encoding == 0)
  636. return "(none)";
  637. else if (encoding == BTF_INT_SIGNED)
  638. return "SIGNED";
  639. else if (encoding == BTF_INT_CHAR)
  640. return "CHAR";
  641. else if (encoding == BTF_INT_BOOL)
  642. return "BOOL";
  643. else
  644. return "UNKN";
  645. }
  646. static u32 btf_type_int(const struct btf_type *t)
  647. {
  648. return *(u32 *)(t + 1);
  649. }
  650. static const struct btf_array *btf_type_array(const struct btf_type *t)
  651. {
  652. return (const struct btf_array *)(t + 1);
  653. }
  654. static const struct btf_enum *btf_type_enum(const struct btf_type *t)
  655. {
  656. return (const struct btf_enum *)(t + 1);
  657. }
  658. static const struct btf_var *btf_type_var(const struct btf_type *t)
  659. {
  660. return (const struct btf_var *)(t + 1);
  661. }
  662. static const struct btf_decl_tag *btf_type_decl_tag(const struct btf_type *t)
  663. {
  664. return (const struct btf_decl_tag *)(t + 1);
  665. }
  666. static const struct btf_enum64 *btf_type_enum64(const struct btf_type *t)
  667. {
  668. return (const struct btf_enum64 *)(t + 1);
  669. }
  670. static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
  671. {
  672. return kind_ops[BTF_INFO_KIND(t->info)];
  673. }
  674. static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
  675. {
  676. if (!BTF_STR_OFFSET_VALID(offset))
  677. return false;
  678. while (offset < btf->start_str_off)
  679. btf = btf->base_btf;
  680. offset -= btf->start_str_off;
  681. return offset < btf->hdr.str_len;
  682. }
  683. static bool __btf_name_char_ok(char c, bool first)
  684. {
  685. if ((first ? !isalpha(c) :
  686. !isalnum(c)) &&
  687. c != '_' &&
  688. c != '.')
  689. return false;
  690. return true;
  691. }
  692. const char *btf_str_by_offset(const struct btf *btf, u32 offset)
  693. {
  694. while (offset < btf->start_str_off)
  695. btf = btf->base_btf;
  696. offset -= btf->start_str_off;
  697. if (offset < btf->hdr.str_len)
  698. return &btf->strings[offset];
  699. return NULL;
  700. }
  701. static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
  702. {
  703. /* offset must be valid */
  704. const char *src = btf_str_by_offset(btf, offset);
  705. const char *src_limit;
  706. if (!__btf_name_char_ok(*src, true))
  707. return false;
  708. /* set a limit on identifier length */
  709. src_limit = src + KSYM_NAME_LEN;
  710. src++;
  711. while (*src && src < src_limit) {
  712. if (!__btf_name_char_ok(*src, false))
  713. return false;
  714. src++;
  715. }
  716. return !*src;
  717. }
  718. /* Allow any printable character in DATASEC names */
  719. static bool btf_name_valid_section(const struct btf *btf, u32 offset)
  720. {
  721. /* offset must be valid */
  722. const char *src = btf_str_by_offset(btf, offset);
  723. const char *src_limit;
  724. if (!*src)
  725. return false;
  726. /* set a limit on identifier length */
  727. src_limit = src + KSYM_NAME_LEN;
  728. while (*src && src < src_limit) {
  729. if (!isprint(*src))
  730. return false;
  731. src++;
  732. }
  733. return !*src;
  734. }
  735. static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
  736. {
  737. const char *name;
  738. if (!offset)
  739. return "(anon)";
  740. name = btf_str_by_offset(btf, offset);
  741. return name ?: "(invalid-name-offset)";
  742. }
  743. const char *btf_name_by_offset(const struct btf *btf, u32 offset)
  744. {
  745. return btf_str_by_offset(btf, offset);
  746. }
  747. const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
  748. {
  749. while (type_id < btf->start_id)
  750. btf = btf->base_btf;
  751. type_id -= btf->start_id;
  752. if (type_id >= btf->nr_types)
  753. return NULL;
  754. return btf->types[type_id];
  755. }
  756. EXPORT_SYMBOL_GPL(btf_type_by_id);
  757. /*
  758. * Regular int is not a bit field and it must be either
  759. * u8/u16/u32/u64 or __int128.
  760. */
  761. static bool btf_type_int_is_regular(const struct btf_type *t)
  762. {
  763. u8 nr_bits, nr_bytes;
  764. u32 int_data;
  765. int_data = btf_type_int(t);
  766. nr_bits = BTF_INT_BITS(int_data);
  767. nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
  768. if (BITS_PER_BYTE_MASKED(nr_bits) ||
  769. BTF_INT_OFFSET(int_data) ||
  770. (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
  771. nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
  772. nr_bytes != (2 * sizeof(u64)))) {
  773. return false;
  774. }
  775. return true;
  776. }
  777. /*
  778. * Check that given struct member is a regular int with expected
  779. * offset and size.
  780. */
  781. bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
  782. const struct btf_member *m,
  783. u32 expected_offset, u32 expected_size)
  784. {
  785. const struct btf_type *t;
  786. u32 id, int_data;
  787. u8 nr_bits;
  788. id = m->type;
  789. t = btf_type_id_size(btf, &id, NULL);
  790. if (!t || !btf_type_is_int(t))
  791. return false;
  792. int_data = btf_type_int(t);
  793. nr_bits = BTF_INT_BITS(int_data);
  794. if (btf_type_kflag(s)) {
  795. u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
  796. u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
  797. /* if kflag set, int should be a regular int and
  798. * bit offset should be at byte boundary.
  799. */
  800. return !bitfield_size &&
  801. BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
  802. BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
  803. }
  804. if (BTF_INT_OFFSET(int_data) ||
  805. BITS_PER_BYTE_MASKED(m->offset) ||
  806. BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
  807. BITS_PER_BYTE_MASKED(nr_bits) ||
  808. BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
  809. return false;
  810. return true;
  811. }
  812. /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
  813. static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,
  814. u32 id)
  815. {
  816. const struct btf_type *t = btf_type_by_id(btf, id);
  817. while (btf_type_is_modifier(t) &&
  818. BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
  819. t = btf_type_by_id(btf, t->type);
  820. }
  821. return t;
  822. }
  823. #define BTF_SHOW_MAX_ITER 10
  824. #define BTF_KIND_BIT(kind) (1ULL << kind)
  825. /*
  826. * Populate show->state.name with type name information.
  827. * Format of type name is
  828. *
  829. * [.member_name = ] (type_name)
  830. */
  831. static const char *btf_show_name(struct btf_show *show)
  832. {
  833. /* BTF_MAX_ITER array suffixes "[]" */
  834. const char *array_suffixes = "[][][][][][][][][][]";
  835. const char *array_suffix = &array_suffixes[strlen(array_suffixes)];
  836. /* BTF_MAX_ITER pointer suffixes "*" */
  837. const char *ptr_suffixes = "**********";
  838. const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)];
  839. const char *name = NULL, *prefix = "", *parens = "";
  840. const struct btf_member *m = show->state.member;
  841. const struct btf_type *t;
  842. const struct btf_array *array;
  843. u32 id = show->state.type_id;
  844. const char *member = NULL;
  845. bool show_member = false;
  846. u64 kinds = 0;
  847. int i;
  848. show->state.name[0] = '\0';
  849. /*
  850. * Don't show type name if we're showing an array member;
  851. * in that case we show the array type so don't need to repeat
  852. * ourselves for each member.
  853. */
  854. if (show->state.array_member)
  855. return "";
  856. /* Retrieve member name, if any. */
  857. if (m) {
  858. member = btf_name_by_offset(show->btf, m->name_off);
  859. show_member = strlen(member) > 0;
  860. id = m->type;
  861. }
  862. /*
  863. * Start with type_id, as we have resolved the struct btf_type *
  864. * via btf_modifier_show() past the parent typedef to the child
  865. * struct, int etc it is defined as. In such cases, the type_id
  866. * still represents the starting type while the struct btf_type *
  867. * in our show->state points at the resolved type of the typedef.
  868. */
  869. t = btf_type_by_id(show->btf, id);
  870. if (!t)
  871. return "";
  872. /*
  873. * The goal here is to build up the right number of pointer and
  874. * array suffixes while ensuring the type name for a typedef
  875. * is represented. Along the way we accumulate a list of
  876. * BTF kinds we have encountered, since these will inform later
  877. * display; for example, pointer types will not require an
  878. * opening "{" for struct, we will just display the pointer value.
  879. *
  880. * We also want to accumulate the right number of pointer or array
  881. * indices in the format string while iterating until we get to
  882. * the typedef/pointee/array member target type.
  883. *
  884. * We start by pointing at the end of pointer and array suffix
  885. * strings; as we accumulate pointers and arrays we move the pointer
  886. * or array string backwards so it will show the expected number of
  887. * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers
  888. * and/or arrays and typedefs are supported as a precaution.
  889. *
  890. * We also want to get typedef name while proceeding to resolve
  891. * type it points to so that we can add parentheses if it is a
  892. * "typedef struct" etc.
  893. */
  894. for (i = 0; i < BTF_SHOW_MAX_ITER; i++) {
  895. switch (BTF_INFO_KIND(t->info)) {
  896. case BTF_KIND_TYPEDEF:
  897. if (!name)
  898. name = btf_name_by_offset(show->btf,
  899. t->name_off);
  900. kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF);
  901. id = t->type;
  902. break;
  903. case BTF_KIND_ARRAY:
  904. kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY);
  905. parens = "[";
  906. if (!t)
  907. return "";
  908. array = btf_type_array(t);
  909. if (array_suffix > array_suffixes)
  910. array_suffix -= 2;
  911. id = array->type;
  912. break;
  913. case BTF_KIND_PTR:
  914. kinds |= BTF_KIND_BIT(BTF_KIND_PTR);
  915. if (ptr_suffix > ptr_suffixes)
  916. ptr_suffix -= 1;
  917. id = t->type;
  918. break;
  919. default:
  920. id = 0;
  921. break;
  922. }
  923. if (!id)
  924. break;
  925. t = btf_type_skip_qualifiers(show->btf, id);
  926. }
  927. /* We may not be able to represent this type; bail to be safe */
  928. if (i == BTF_SHOW_MAX_ITER)
  929. return "";
  930. if (!name)
  931. name = btf_name_by_offset(show->btf, t->name_off);
  932. switch (BTF_INFO_KIND(t->info)) {
  933. case BTF_KIND_STRUCT:
  934. case BTF_KIND_UNION:
  935. prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ?
  936. "struct" : "union";
  937. /* if it's an array of struct/union, parens is already set */
  938. if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY))))
  939. parens = "{";
  940. break;
  941. case BTF_KIND_ENUM:
  942. case BTF_KIND_ENUM64:
  943. prefix = "enum";
  944. break;
  945. default:
  946. break;
  947. }
  948. /* pointer does not require parens */
  949. if (kinds & BTF_KIND_BIT(BTF_KIND_PTR))
  950. parens = "";
  951. /* typedef does not require struct/union/enum prefix */
  952. if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF))
  953. prefix = "";
  954. if (!name)
  955. name = "";
  956. /* Even if we don't want type name info, we want parentheses etc */
  957. if (show->flags & BTF_SHOW_NONAME)
  958. snprintf(show->state.name, sizeof(show->state.name), "%s",
  959. parens);
  960. else
  961. snprintf(show->state.name, sizeof(show->state.name),
  962. "%s%s%s(%s%s%s%s%s%s)%s",
  963. /* first 3 strings comprise ".member = " */
  964. show_member ? "." : "",
  965. show_member ? member : "",
  966. show_member ? " = " : "",
  967. /* ...next is our prefix (struct, enum, etc) */
  968. prefix,
  969. strlen(prefix) > 0 && strlen(name) > 0 ? " " : "",
  970. /* ...this is the type name itself */
  971. name,
  972. /* ...suffixed by the appropriate '*', '[]' suffixes */
  973. strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix,
  974. array_suffix, parens);
  975. return show->state.name;
  976. }
  977. static const char *__btf_show_indent(struct btf_show *show)
  978. {
  979. const char *indents = " ";
  980. const char *indent = &indents[strlen(indents)];
  981. if ((indent - show->state.depth) >= indents)
  982. return indent - show->state.depth;
  983. return indents;
  984. }
  985. static const char *btf_show_indent(struct btf_show *show)
  986. {
  987. return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show);
  988. }
  989. static const char *btf_show_newline(struct btf_show *show)
  990. {
  991. return show->flags & BTF_SHOW_COMPACT ? "" : "\n";
  992. }
  993. static const char *btf_show_delim(struct btf_show *show)
  994. {
  995. if (show->state.depth == 0)
  996. return "";
  997. if ((show->flags & BTF_SHOW_COMPACT) && show->state.type &&
  998. BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION)
  999. return "|";
  1000. return ",";
  1001. }
  1002. __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...)
  1003. {
  1004. va_list args;
  1005. if (!show->state.depth_check) {
  1006. va_start(args, fmt);
  1007. show->showfn(show, fmt, args);
  1008. va_end(args);
  1009. }
  1010. }
  1011. /* Macros are used here as btf_show_type_value[s]() prepends and appends
  1012. * format specifiers to the format specifier passed in; these do the work of
  1013. * adding indentation, delimiters etc while the caller simply has to specify
  1014. * the type value(s) in the format specifier + value(s).
  1015. */
  1016. #define btf_show_type_value(show, fmt, value) \
  1017. do { \
  1018. if ((value) != (__typeof__(value))0 || \
  1019. (show->flags & BTF_SHOW_ZERO) || \
  1020. show->state.depth == 0) { \
  1021. btf_show(show, "%s%s" fmt "%s%s", \
  1022. btf_show_indent(show), \
  1023. btf_show_name(show), \
  1024. value, btf_show_delim(show), \
  1025. btf_show_newline(show)); \
  1026. if (show->state.depth > show->state.depth_to_show) \
  1027. show->state.depth_to_show = show->state.depth; \
  1028. } \
  1029. } while (0)
  1030. #define btf_show_type_values(show, fmt, ...) \
  1031. do { \
  1032. btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
  1033. btf_show_name(show), \
  1034. __VA_ARGS__, btf_show_delim(show), \
  1035. btf_show_newline(show)); \
  1036. if (show->state.depth > show->state.depth_to_show) \
  1037. show->state.depth_to_show = show->state.depth; \
  1038. } while (0)
  1039. /* How much is left to copy to safe buffer after @data? */
  1040. static int btf_show_obj_size_left(struct btf_show *show, void *data)
  1041. {
  1042. return show->obj.head + show->obj.size - data;
  1043. }
  1044. /* Is object pointed to by @data of @size already copied to our safe buffer? */
  1045. static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size)
  1046. {
  1047. return data >= show->obj.data &&
  1048. (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE);
  1049. }
  1050. /*
  1051. * If object pointed to by @data of @size falls within our safe buffer, return
  1052. * the equivalent pointer to the same safe data. Assumes
  1053. * copy_from_kernel_nofault() has already happened and our safe buffer is
  1054. * populated.
  1055. */
  1056. static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size)
  1057. {
  1058. if (btf_show_obj_is_safe(show, data, size))
  1059. return show->obj.safe + (data - show->obj.data);
  1060. return NULL;
  1061. }
  1062. /*
  1063. * Return a safe-to-access version of data pointed to by @data.
  1064. * We do this by copying the relevant amount of information
  1065. * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
  1066. *
  1067. * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
  1068. * safe copy is needed.
  1069. *
  1070. * Otherwise we need to determine if we have the required amount
  1071. * of data (determined by the @data pointer and the size of the
  1072. * largest base type we can encounter (represented by
  1073. * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
  1074. * that we will be able to print some of the current object,
  1075. * and if more is needed a copy will be triggered.
  1076. * Some objects such as structs will not fit into the buffer;
  1077. * in such cases additional copies when we iterate over their
  1078. * members may be needed.
  1079. *
  1080. * btf_show_obj_safe() is used to return a safe buffer for
  1081. * btf_show_start_type(); this ensures that as we recurse into
  1082. * nested types we always have safe data for the given type.
  1083. * This approach is somewhat wasteful; it's possible for example
  1084. * that when iterating over a large union we'll end up copying the
  1085. * same data repeatedly, but the goal is safety not performance.
  1086. * We use stack data as opposed to per-CPU buffers because the
  1087. * iteration over a type can take some time, and preemption handling
  1088. * would greatly complicate use of the safe buffer.
  1089. */
  1090. static void *btf_show_obj_safe(struct btf_show *show,
  1091. const struct btf_type *t,
  1092. void *data)
  1093. {
  1094. const struct btf_type *rt;
  1095. int size_left, size;
  1096. void *safe = NULL;
  1097. if (show->flags & BTF_SHOW_UNSAFE)
  1098. return data;
  1099. rt = btf_resolve_size(show->btf, t, &size);
  1100. if (IS_ERR(rt)) {
  1101. show->state.status = PTR_ERR(rt);
  1102. return NULL;
  1103. }
  1104. /*
  1105. * Is this toplevel object? If so, set total object size and
  1106. * initialize pointers. Otherwise check if we still fall within
  1107. * our safe object data.
  1108. */
  1109. if (show->state.depth == 0) {
  1110. show->obj.size = size;
  1111. show->obj.head = data;
  1112. } else {
  1113. /*
  1114. * If the size of the current object is > our remaining
  1115. * safe buffer we _may_ need to do a new copy. However
  1116. * consider the case of a nested struct; it's size pushes
  1117. * us over the safe buffer limit, but showing any individual
  1118. * struct members does not. In such cases, we don't need
  1119. * to initiate a fresh copy yet; however we definitely need
  1120. * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
  1121. * in our buffer, regardless of the current object size.
  1122. * The logic here is that as we resolve types we will
  1123. * hit a base type at some point, and we need to be sure
  1124. * the next chunk of data is safely available to display
  1125. * that type info safely. We cannot rely on the size of
  1126. * the current object here because it may be much larger
  1127. * than our current buffer (e.g. task_struct is 8k).
  1128. * All we want to do here is ensure that we can print the
  1129. * next basic type, which we can if either
  1130. * - the current type size is within the safe buffer; or
  1131. * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
  1132. * the safe buffer.
  1133. */
  1134. safe = __btf_show_obj_safe(show, data,
  1135. min(size,
  1136. BTF_SHOW_OBJ_BASE_TYPE_SIZE));
  1137. }
  1138. /*
  1139. * We need a new copy to our safe object, either because we haven't
  1140. * yet copied and are initializing safe data, or because the data
  1141. * we want falls outside the boundaries of the safe object.
  1142. */
  1143. if (!safe) {
  1144. size_left = btf_show_obj_size_left(show, data);
  1145. if (size_left > BTF_SHOW_OBJ_SAFE_SIZE)
  1146. size_left = BTF_SHOW_OBJ_SAFE_SIZE;
  1147. show->state.status = copy_from_kernel_nofault(show->obj.safe,
  1148. data, size_left);
  1149. if (!show->state.status) {
  1150. show->obj.data = data;
  1151. safe = show->obj.safe;
  1152. }
  1153. }
  1154. return safe;
  1155. }
  1156. /*
  1157. * Set the type we are starting to show and return a safe data pointer
  1158. * to be used for showing the associated data.
  1159. */
  1160. static void *btf_show_start_type(struct btf_show *show,
  1161. const struct btf_type *t,
  1162. u32 type_id, void *data)
  1163. {
  1164. show->state.type = t;
  1165. show->state.type_id = type_id;
  1166. show->state.name[0] = '\0';
  1167. return btf_show_obj_safe(show, t, data);
  1168. }
  1169. static void btf_show_end_type(struct btf_show *show)
  1170. {
  1171. show->state.type = NULL;
  1172. show->state.type_id = 0;
  1173. show->state.name[0] = '\0';
  1174. }
  1175. static void *btf_show_start_aggr_type(struct btf_show *show,
  1176. const struct btf_type *t,
  1177. u32 type_id, void *data)
  1178. {
  1179. void *safe_data = btf_show_start_type(show, t, type_id, data);
  1180. if (!safe_data)
  1181. return safe_data;
  1182. btf_show(show, "%s%s%s", btf_show_indent(show),
  1183. btf_show_name(show),
  1184. btf_show_newline(show));
  1185. show->state.depth++;
  1186. return safe_data;
  1187. }
  1188. static void btf_show_end_aggr_type(struct btf_show *show,
  1189. const char *suffix)
  1190. {
  1191. show->state.depth--;
  1192. btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix,
  1193. btf_show_delim(show), btf_show_newline(show));
  1194. btf_show_end_type(show);
  1195. }
  1196. static void btf_show_start_member(struct btf_show *show,
  1197. const struct btf_member *m)
  1198. {
  1199. show->state.member = m;
  1200. }
  1201. static void btf_show_start_array_member(struct btf_show *show)
  1202. {
  1203. show->state.array_member = 1;
  1204. btf_show_start_member(show, NULL);
  1205. }
  1206. static void btf_show_end_member(struct btf_show *show)
  1207. {
  1208. show->state.member = NULL;
  1209. }
  1210. static void btf_show_end_array_member(struct btf_show *show)
  1211. {
  1212. show->state.array_member = 0;
  1213. btf_show_end_member(show);
  1214. }
  1215. static void *btf_show_start_array_type(struct btf_show *show,
  1216. const struct btf_type *t,
  1217. u32 type_id,
  1218. u16 array_encoding,
  1219. void *data)
  1220. {
  1221. show->state.array_encoding = array_encoding;
  1222. show->state.array_terminated = 0;
  1223. return btf_show_start_aggr_type(show, t, type_id, data);
  1224. }
  1225. static void btf_show_end_array_type(struct btf_show *show)
  1226. {
  1227. show->state.array_encoding = 0;
  1228. show->state.array_terminated = 0;
  1229. btf_show_end_aggr_type(show, "]");
  1230. }
  1231. static void *btf_show_start_struct_type(struct btf_show *show,
  1232. const struct btf_type *t,
  1233. u32 type_id,
  1234. void *data)
  1235. {
  1236. return btf_show_start_aggr_type(show, t, type_id, data);
  1237. }
  1238. static void btf_show_end_struct_type(struct btf_show *show)
  1239. {
  1240. btf_show_end_aggr_type(show, "}");
  1241. }
  1242. __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
  1243. const char *fmt, ...)
  1244. {
  1245. va_list args;
  1246. va_start(args, fmt);
  1247. bpf_verifier_vlog(log, fmt, args);
  1248. va_end(args);
  1249. }
  1250. __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
  1251. const char *fmt, ...)
  1252. {
  1253. struct bpf_verifier_log *log = &env->log;
  1254. va_list args;
  1255. if (!bpf_verifier_log_needed(log))
  1256. return;
  1257. va_start(args, fmt);
  1258. bpf_verifier_vlog(log, fmt, args);
  1259. va_end(args);
  1260. }
  1261. __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
  1262. const struct btf_type *t,
  1263. bool log_details,
  1264. const char *fmt, ...)
  1265. {
  1266. struct bpf_verifier_log *log = &env->log;
  1267. struct btf *btf = env->btf;
  1268. va_list args;
  1269. if (!bpf_verifier_log_needed(log))
  1270. return;
  1271. if (log->level == BPF_LOG_KERNEL) {
  1272. /* btf verifier prints all types it is processing via
  1273. * btf_verifier_log_type(..., fmt = NULL).
  1274. * Skip those prints for in-kernel BTF verification.
  1275. */
  1276. if (!fmt)
  1277. return;
  1278. /* Skip logging when loading module BTF with mismatches permitted */
  1279. if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
  1280. return;
  1281. }
  1282. __btf_verifier_log(log, "[%u] %s %s%s",
  1283. env->log_type_id,
  1284. btf_type_str(t),
  1285. __btf_name_by_offset(btf, t->name_off),
  1286. log_details ? " " : "");
  1287. if (log_details)
  1288. btf_type_ops(t)->log_details(env, t);
  1289. if (fmt && *fmt) {
  1290. __btf_verifier_log(log, " ");
  1291. va_start(args, fmt);
  1292. bpf_verifier_vlog(log, fmt, args);
  1293. va_end(args);
  1294. }
  1295. __btf_verifier_log(log, "\n");
  1296. }
  1297. #define btf_verifier_log_type(env, t, ...) \
  1298. __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
  1299. #define btf_verifier_log_basic(env, t, ...) \
  1300. __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
  1301. __printf(4, 5)
  1302. static void btf_verifier_log_member(struct btf_verifier_env *env,
  1303. const struct btf_type *struct_type,
  1304. const struct btf_member *member,
  1305. const char *fmt, ...)
  1306. {
  1307. struct bpf_verifier_log *log = &env->log;
  1308. struct btf *btf = env->btf;
  1309. va_list args;
  1310. if (!bpf_verifier_log_needed(log))
  1311. return;
  1312. if (log->level == BPF_LOG_KERNEL) {
  1313. if (!fmt)
  1314. return;
  1315. /* Skip logging when loading module BTF with mismatches permitted */
  1316. if (env->btf->base_btf && IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
  1317. return;
  1318. }
  1319. /* The CHECK_META phase already did a btf dump.
  1320. *
  1321. * If member is logged again, it must hit an error in
  1322. * parsing this member. It is useful to print out which
  1323. * struct this member belongs to.
  1324. */
  1325. if (env->phase != CHECK_META)
  1326. btf_verifier_log_type(env, struct_type, NULL);
  1327. if (btf_type_kflag(struct_type))
  1328. __btf_verifier_log(log,
  1329. "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
  1330. __btf_name_by_offset(btf, member->name_off),
  1331. member->type,
  1332. BTF_MEMBER_BITFIELD_SIZE(member->offset),
  1333. BTF_MEMBER_BIT_OFFSET(member->offset));
  1334. else
  1335. __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
  1336. __btf_name_by_offset(btf, member->name_off),
  1337. member->type, member->offset);
  1338. if (fmt && *fmt) {
  1339. __btf_verifier_log(log, " ");
  1340. va_start(args, fmt);
  1341. bpf_verifier_vlog(log, fmt, args);
  1342. va_end(args);
  1343. }
  1344. __btf_verifier_log(log, "\n");
  1345. }
  1346. __printf(4, 5)
  1347. static void btf_verifier_log_vsi(struct btf_verifier_env *env,
  1348. const struct btf_type *datasec_type,
  1349. const struct btf_var_secinfo *vsi,
  1350. const char *fmt, ...)
  1351. {
  1352. struct bpf_verifier_log *log = &env->log;
  1353. va_list args;
  1354. if (!bpf_verifier_log_needed(log))
  1355. return;
  1356. if (log->level == BPF_LOG_KERNEL && !fmt)
  1357. return;
  1358. if (env->phase != CHECK_META)
  1359. btf_verifier_log_type(env, datasec_type, NULL);
  1360. __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
  1361. vsi->type, vsi->offset, vsi->size);
  1362. if (fmt && *fmt) {
  1363. __btf_verifier_log(log, " ");
  1364. va_start(args, fmt);
  1365. bpf_verifier_vlog(log, fmt, args);
  1366. va_end(args);
  1367. }
  1368. __btf_verifier_log(log, "\n");
  1369. }
  1370. static void btf_verifier_log_hdr(struct btf_verifier_env *env,
  1371. u32 btf_data_size)
  1372. {
  1373. struct bpf_verifier_log *log = &env->log;
  1374. const struct btf *btf = env->btf;
  1375. const struct btf_header *hdr;
  1376. if (!bpf_verifier_log_needed(log))
  1377. return;
  1378. if (log->level == BPF_LOG_KERNEL)
  1379. return;
  1380. hdr = &btf->hdr;
  1381. __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
  1382. __btf_verifier_log(log, "version: %u\n", hdr->version);
  1383. __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
  1384. __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
  1385. __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
  1386. __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
  1387. __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
  1388. __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
  1389. __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
  1390. }
  1391. static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
  1392. {
  1393. struct btf *btf = env->btf;
  1394. if (btf->types_size == btf->nr_types) {
  1395. /* Expand 'types' array */
  1396. struct btf_type **new_types;
  1397. u32 expand_by, new_size;
  1398. if (btf->start_id + btf->types_size == BTF_MAX_TYPE) {
  1399. btf_verifier_log(env, "Exceeded max num of types");
  1400. return -E2BIG;
  1401. }
  1402. expand_by = max_t(u32, btf->types_size >> 2, 16);
  1403. new_size = min_t(u32, BTF_MAX_TYPE,
  1404. btf->types_size + expand_by);
  1405. new_types = kvcalloc(new_size, sizeof(*new_types),
  1406. GFP_KERNEL | __GFP_NOWARN);
  1407. if (!new_types)
  1408. return -ENOMEM;
  1409. if (btf->nr_types == 0) {
  1410. if (!btf->base_btf) {
  1411. /* lazily init VOID type */
  1412. new_types[0] = &btf_void;
  1413. btf->nr_types++;
  1414. }
  1415. } else {
  1416. memcpy(new_types, btf->types,
  1417. sizeof(*btf->types) * btf->nr_types);
  1418. }
  1419. kvfree(btf->types);
  1420. btf->types = new_types;
  1421. btf->types_size = new_size;
  1422. }
  1423. btf->types[btf->nr_types++] = t;
  1424. return 0;
  1425. }
  1426. static int btf_alloc_id(struct btf *btf)
  1427. {
  1428. int id;
  1429. idr_preload(GFP_KERNEL);
  1430. spin_lock_bh(&btf_idr_lock);
  1431. id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
  1432. if (id > 0)
  1433. btf->id = id;
  1434. spin_unlock_bh(&btf_idr_lock);
  1435. idr_preload_end();
  1436. if (WARN_ON_ONCE(!id))
  1437. return -ENOSPC;
  1438. return id > 0 ? 0 : id;
  1439. }
  1440. static void btf_free_id(struct btf *btf)
  1441. {
  1442. unsigned long flags;
  1443. /*
  1444. * In map-in-map, calling map_delete_elem() on outer
  1445. * map will call bpf_map_put on the inner map.
  1446. * It will then eventually call btf_free_id()
  1447. * on the inner map. Some of the map_delete_elem()
  1448. * implementation may have irq disabled, so
  1449. * we need to use the _irqsave() version instead
  1450. * of the _bh() version.
  1451. */
  1452. spin_lock_irqsave(&btf_idr_lock, flags);
  1453. idr_remove(&btf_idr, btf->id);
  1454. spin_unlock_irqrestore(&btf_idr_lock, flags);
  1455. }
  1456. static void btf_free_kfunc_set_tab(struct btf *btf)
  1457. {
  1458. struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab;
  1459. int hook;
  1460. if (!tab)
  1461. return;
  1462. for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++)
  1463. kfree(tab->sets[hook]);
  1464. kfree(tab);
  1465. btf->kfunc_set_tab = NULL;
  1466. }
  1467. static void btf_free_dtor_kfunc_tab(struct btf *btf)
  1468. {
  1469. struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab;
  1470. if (!tab)
  1471. return;
  1472. kfree(tab);
  1473. btf->dtor_kfunc_tab = NULL;
  1474. }
  1475. static void btf_struct_metas_free(struct btf_struct_metas *tab)
  1476. {
  1477. int i;
  1478. if (!tab)
  1479. return;
  1480. for (i = 0; i < tab->cnt; i++)
  1481. btf_record_free(tab->types[i].record);
  1482. kfree(tab);
  1483. }
  1484. static void btf_free_struct_meta_tab(struct btf *btf)
  1485. {
  1486. struct btf_struct_metas *tab = btf->struct_meta_tab;
  1487. btf_struct_metas_free(tab);
  1488. btf->struct_meta_tab = NULL;
  1489. }
  1490. static void btf_free_struct_ops_tab(struct btf *btf)
  1491. {
  1492. struct btf_struct_ops_tab *tab = btf->struct_ops_tab;
  1493. u32 i;
  1494. if (!tab)
  1495. return;
  1496. for (i = 0; i < tab->cnt; i++)
  1497. bpf_struct_ops_desc_release(&tab->ops[i]);
  1498. kfree(tab);
  1499. btf->struct_ops_tab = NULL;
  1500. }
  1501. static void btf_free(struct btf *btf)
  1502. {
  1503. btf_free_struct_meta_tab(btf);
  1504. btf_free_dtor_kfunc_tab(btf);
  1505. btf_free_kfunc_set_tab(btf);
  1506. btf_free_struct_ops_tab(btf);
  1507. kvfree(btf->types);
  1508. kvfree(btf->resolved_sizes);
  1509. kvfree(btf->resolved_ids);
  1510. /* vmlinux does not allocate btf->data, it simply points it at
  1511. * __start_BTF.
  1512. */
  1513. if (!btf_is_vmlinux(btf))
  1514. kvfree(btf->data);
  1515. kvfree(btf->base_id_map);
  1516. kfree(btf);
  1517. }
  1518. static void btf_free_rcu(struct rcu_head *rcu)
  1519. {
  1520. struct btf *btf = container_of(rcu, struct btf, rcu);
  1521. btf_free(btf);
  1522. }
  1523. const char *btf_get_name(const struct btf *btf)
  1524. {
  1525. return btf->name;
  1526. }
  1527. void btf_get(struct btf *btf)
  1528. {
  1529. refcount_inc(&btf->refcnt);
  1530. }
  1531. void btf_put(struct btf *btf)
  1532. {
  1533. if (btf && refcount_dec_and_test(&btf->refcnt)) {
  1534. btf_free_id(btf);
  1535. call_rcu(&btf->rcu, btf_free_rcu);
  1536. }
  1537. }
  1538. struct btf *btf_base_btf(const struct btf *btf)
  1539. {
  1540. return btf->base_btf;
  1541. }
  1542. const struct btf_header *btf_header(const struct btf *btf)
  1543. {
  1544. return &btf->hdr;
  1545. }
  1546. void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
  1547. {
  1548. btf->base_btf = (struct btf *)base_btf;
  1549. btf->start_id = btf_nr_types(base_btf);
  1550. btf->start_str_off = base_btf->hdr.str_len;
  1551. }
  1552. static int env_resolve_init(struct btf_verifier_env *env)
  1553. {
  1554. struct btf *btf = env->btf;
  1555. u32 nr_types = btf->nr_types;
  1556. u32 *resolved_sizes = NULL;
  1557. u32 *resolved_ids = NULL;
  1558. u8 *visit_states = NULL;
  1559. resolved_sizes = kvcalloc(nr_types, sizeof(*resolved_sizes),
  1560. GFP_KERNEL | __GFP_NOWARN);
  1561. if (!resolved_sizes)
  1562. goto nomem;
  1563. resolved_ids = kvcalloc(nr_types, sizeof(*resolved_ids),
  1564. GFP_KERNEL | __GFP_NOWARN);
  1565. if (!resolved_ids)
  1566. goto nomem;
  1567. visit_states = kvcalloc(nr_types, sizeof(*visit_states),
  1568. GFP_KERNEL | __GFP_NOWARN);
  1569. if (!visit_states)
  1570. goto nomem;
  1571. btf->resolved_sizes = resolved_sizes;
  1572. btf->resolved_ids = resolved_ids;
  1573. env->visit_states = visit_states;
  1574. return 0;
  1575. nomem:
  1576. kvfree(resolved_sizes);
  1577. kvfree(resolved_ids);
  1578. kvfree(visit_states);
  1579. return -ENOMEM;
  1580. }
  1581. static void btf_verifier_env_free(struct btf_verifier_env *env)
  1582. {
  1583. kvfree(env->visit_states);
  1584. kfree(env);
  1585. }
  1586. static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
  1587. const struct btf_type *next_type)
  1588. {
  1589. switch (env->resolve_mode) {
  1590. case RESOLVE_TBD:
  1591. /* int, enum or void is a sink */
  1592. return !btf_type_needs_resolve(next_type);
  1593. case RESOLVE_PTR:
  1594. /* int, enum, void, struct, array, func or func_proto is a sink
  1595. * for ptr
  1596. */
  1597. return !btf_type_is_modifier(next_type) &&
  1598. !btf_type_is_ptr(next_type);
  1599. case RESOLVE_STRUCT_OR_ARRAY:
  1600. /* int, enum, void, ptr, func or func_proto is a sink
  1601. * for struct and array
  1602. */
  1603. return !btf_type_is_modifier(next_type) &&
  1604. !btf_type_is_array(next_type) &&
  1605. !btf_type_is_struct(next_type);
  1606. default:
  1607. BUG();
  1608. }
  1609. }
  1610. static bool env_type_is_resolved(const struct btf_verifier_env *env,
  1611. u32 type_id)
  1612. {
  1613. /* base BTF types should be resolved by now */
  1614. if (type_id < env->btf->start_id)
  1615. return true;
  1616. return env->visit_states[type_id - env->btf->start_id] == RESOLVED;
  1617. }
  1618. static int env_stack_push(struct btf_verifier_env *env,
  1619. const struct btf_type *t, u32 type_id)
  1620. {
  1621. const struct btf *btf = env->btf;
  1622. struct resolve_vertex *v;
  1623. if (env->top_stack == MAX_RESOLVE_DEPTH)
  1624. return -E2BIG;
  1625. if (type_id < btf->start_id
  1626. || env->visit_states[type_id - btf->start_id] != NOT_VISITED)
  1627. return -EEXIST;
  1628. env->visit_states[type_id - btf->start_id] = VISITED;
  1629. v = &env->stack[env->top_stack++];
  1630. v->t = t;
  1631. v->type_id = type_id;
  1632. v->next_member = 0;
  1633. if (env->resolve_mode == RESOLVE_TBD) {
  1634. if (btf_type_is_ptr(t))
  1635. env->resolve_mode = RESOLVE_PTR;
  1636. else if (btf_type_is_struct(t) || btf_type_is_array(t))
  1637. env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
  1638. }
  1639. return 0;
  1640. }
  1641. static void env_stack_set_next_member(struct btf_verifier_env *env,
  1642. u16 next_member)
  1643. {
  1644. env->stack[env->top_stack - 1].next_member = next_member;
  1645. }
  1646. static void env_stack_pop_resolved(struct btf_verifier_env *env,
  1647. u32 resolved_type_id,
  1648. u32 resolved_size)
  1649. {
  1650. u32 type_id = env->stack[--(env->top_stack)].type_id;
  1651. struct btf *btf = env->btf;
  1652. type_id -= btf->start_id; /* adjust to local type id */
  1653. btf->resolved_sizes[type_id] = resolved_size;
  1654. btf->resolved_ids[type_id] = resolved_type_id;
  1655. env->visit_states[type_id] = RESOLVED;
  1656. }
  1657. static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
  1658. {
  1659. return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
  1660. }
  1661. /* Resolve the size of a passed-in "type"
  1662. *
  1663. * type: is an array (e.g. u32 array[x][y])
  1664. * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
  1665. * *type_size: (x * y * sizeof(u32)). Hence, *type_size always
  1666. * corresponds to the return type.
  1667. * *elem_type: u32
  1668. * *elem_id: id of u32
  1669. * *total_nelems: (x * y). Hence, individual elem size is
  1670. * (*type_size / *total_nelems)
  1671. * *type_id: id of type if it's changed within the function, 0 if not
  1672. *
  1673. * type: is not an array (e.g. const struct X)
  1674. * return type: type "struct X"
  1675. * *type_size: sizeof(struct X)
  1676. * *elem_type: same as return type ("struct X")
  1677. * *elem_id: 0
  1678. * *total_nelems: 1
  1679. * *type_id: id of type if it's changed within the function, 0 if not
  1680. */
  1681. static const struct btf_type *
  1682. __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
  1683. u32 *type_size, const struct btf_type **elem_type,
  1684. u32 *elem_id, u32 *total_nelems, u32 *type_id)
  1685. {
  1686. const struct btf_type *array_type = NULL;
  1687. const struct btf_array *array = NULL;
  1688. u32 i, size, nelems = 1, id = 0;
  1689. for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
  1690. switch (BTF_INFO_KIND(type->info)) {
  1691. /* type->size can be used */
  1692. case BTF_KIND_INT:
  1693. case BTF_KIND_STRUCT:
  1694. case BTF_KIND_UNION:
  1695. case BTF_KIND_ENUM:
  1696. case BTF_KIND_FLOAT:
  1697. case BTF_KIND_ENUM64:
  1698. size = type->size;
  1699. goto resolved;
  1700. case BTF_KIND_PTR:
  1701. size = sizeof(void *);
  1702. goto resolved;
  1703. /* Modifiers */
  1704. case BTF_KIND_TYPEDEF:
  1705. case BTF_KIND_VOLATILE:
  1706. case BTF_KIND_CONST:
  1707. case BTF_KIND_RESTRICT:
  1708. case BTF_KIND_TYPE_TAG:
  1709. id = type->type;
  1710. type = btf_type_by_id(btf, type->type);
  1711. break;
  1712. case BTF_KIND_ARRAY:
  1713. if (!array_type)
  1714. array_type = type;
  1715. array = btf_type_array(type);
  1716. if (nelems && array->nelems > U32_MAX / nelems)
  1717. return ERR_PTR(-EINVAL);
  1718. nelems *= array->nelems;
  1719. type = btf_type_by_id(btf, array->type);
  1720. break;
  1721. /* type without size */
  1722. default:
  1723. return ERR_PTR(-EINVAL);
  1724. }
  1725. }
  1726. return ERR_PTR(-EINVAL);
  1727. resolved:
  1728. if (nelems && size > U32_MAX / nelems)
  1729. return ERR_PTR(-EINVAL);
  1730. *type_size = nelems * size;
  1731. if (total_nelems)
  1732. *total_nelems = nelems;
  1733. if (elem_type)
  1734. *elem_type = type;
  1735. if (elem_id)
  1736. *elem_id = array ? array->type : 0;
  1737. if (type_id && id)
  1738. *type_id = id;
  1739. return array_type ? : type;
  1740. }
  1741. const struct btf_type *
  1742. btf_resolve_size(const struct btf *btf, const struct btf_type *type,
  1743. u32 *type_size)
  1744. {
  1745. return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);
  1746. }
  1747. static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
  1748. {
  1749. while (type_id < btf->start_id)
  1750. btf = btf->base_btf;
  1751. return btf->resolved_ids[type_id - btf->start_id];
  1752. }
  1753. /* The input param "type_id" must point to a needs_resolve type */
  1754. static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
  1755. u32 *type_id)
  1756. {
  1757. *type_id = btf_resolved_type_id(btf, *type_id);
  1758. return btf_type_by_id(btf, *type_id);
  1759. }
  1760. static u32 btf_resolved_type_size(const struct btf *btf, u32 type_id)
  1761. {
  1762. while (type_id < btf->start_id)
  1763. btf = btf->base_btf;
  1764. return btf->resolved_sizes[type_id - btf->start_id];
  1765. }
  1766. const struct btf_type *btf_type_id_size(const struct btf *btf,
  1767. u32 *type_id, u32 *ret_size)
  1768. {
  1769. const struct btf_type *size_type;
  1770. u32 size_type_id = *type_id;
  1771. u32 size = 0;
  1772. size_type = btf_type_by_id(btf, size_type_id);
  1773. if (btf_type_nosize_or_null(size_type))
  1774. return NULL;
  1775. if (btf_type_has_size(size_type)) {
  1776. size = size_type->size;
  1777. } else if (btf_type_is_array(size_type)) {
  1778. size = btf_resolved_type_size(btf, size_type_id);
  1779. } else if (btf_type_is_ptr(size_type)) {
  1780. size = sizeof(void *);
  1781. } else {
  1782. if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
  1783. !btf_type_is_var(size_type)))
  1784. return NULL;
  1785. size_type_id = btf_resolved_type_id(btf, size_type_id);
  1786. size_type = btf_type_by_id(btf, size_type_id);
  1787. if (btf_type_nosize_or_null(size_type))
  1788. return NULL;
  1789. else if (btf_type_has_size(size_type))
  1790. size = size_type->size;
  1791. else if (btf_type_is_array(size_type))
  1792. size = btf_resolved_type_size(btf, size_type_id);
  1793. else if (btf_type_is_ptr(size_type))
  1794. size = sizeof(void *);
  1795. else
  1796. return NULL;
  1797. }
  1798. *type_id = size_type_id;
  1799. if (ret_size)
  1800. *ret_size = size;
  1801. return size_type;
  1802. }
  1803. static int btf_df_check_member(struct btf_verifier_env *env,
  1804. const struct btf_type *struct_type,
  1805. const struct btf_member *member,
  1806. const struct btf_type *member_type)
  1807. {
  1808. btf_verifier_log_basic(env, struct_type,
  1809. "Unsupported check_member");
  1810. return -EINVAL;
  1811. }
  1812. static int btf_df_check_kflag_member(struct btf_verifier_env *env,
  1813. const struct btf_type *struct_type,
  1814. const struct btf_member *member,
  1815. const struct btf_type *member_type)
  1816. {
  1817. btf_verifier_log_basic(env, struct_type,
  1818. "Unsupported check_kflag_member");
  1819. return -EINVAL;
  1820. }
  1821. /* Used for ptr, array struct/union and float type members.
  1822. * int, enum and modifier types have their specific callback functions.
  1823. */
  1824. static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
  1825. const struct btf_type *struct_type,
  1826. const struct btf_member *member,
  1827. const struct btf_type *member_type)
  1828. {
  1829. if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
  1830. btf_verifier_log_member(env, struct_type, member,
  1831. "Invalid member bitfield_size");
  1832. return -EINVAL;
  1833. }
  1834. /* bitfield size is 0, so member->offset represents bit offset only.
  1835. * It is safe to call non kflag check_member variants.
  1836. */
  1837. return btf_type_ops(member_type)->check_member(env, struct_type,
  1838. member,
  1839. member_type);
  1840. }
  1841. static int btf_df_resolve(struct btf_verifier_env *env,
  1842. const struct resolve_vertex *v)
  1843. {
  1844. btf_verifier_log_basic(env, v->t, "Unsupported resolve");
  1845. return -EINVAL;
  1846. }
  1847. static void btf_df_show(const struct btf *btf, const struct btf_type *t,
  1848. u32 type_id, void *data, u8 bits_offsets,
  1849. struct btf_show *show)
  1850. {
  1851. btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
  1852. }
  1853. static int btf_int_check_member(struct btf_verifier_env *env,
  1854. const struct btf_type *struct_type,
  1855. const struct btf_member *member,
  1856. const struct btf_type *member_type)
  1857. {
  1858. u32 int_data = btf_type_int(member_type);
  1859. u32 struct_bits_off = member->offset;
  1860. u32 struct_size = struct_type->size;
  1861. u32 nr_copy_bits;
  1862. u32 bytes_offset;
  1863. if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
  1864. btf_verifier_log_member(env, struct_type, member,
  1865. "bits_offset exceeds U32_MAX");
  1866. return -EINVAL;
  1867. }
  1868. struct_bits_off += BTF_INT_OFFSET(int_data);
  1869. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1870. nr_copy_bits = BTF_INT_BITS(int_data) +
  1871. BITS_PER_BYTE_MASKED(struct_bits_off);
  1872. if (nr_copy_bits > BITS_PER_U128) {
  1873. btf_verifier_log_member(env, struct_type, member,
  1874. "nr_copy_bits exceeds 128");
  1875. return -EINVAL;
  1876. }
  1877. if (struct_size < bytes_offset ||
  1878. struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
  1879. btf_verifier_log_member(env, struct_type, member,
  1880. "Member exceeds struct_size");
  1881. return -EINVAL;
  1882. }
  1883. return 0;
  1884. }
  1885. static int btf_int_check_kflag_member(struct btf_verifier_env *env,
  1886. const struct btf_type *struct_type,
  1887. const struct btf_member *member,
  1888. const struct btf_type *member_type)
  1889. {
  1890. u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
  1891. u32 int_data = btf_type_int(member_type);
  1892. u32 struct_size = struct_type->size;
  1893. u32 nr_copy_bits;
  1894. /* a regular int type is required for the kflag int member */
  1895. if (!btf_type_int_is_regular(member_type)) {
  1896. btf_verifier_log_member(env, struct_type, member,
  1897. "Invalid member base type");
  1898. return -EINVAL;
  1899. }
  1900. /* check sanity of bitfield size */
  1901. nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
  1902. struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
  1903. nr_int_data_bits = BTF_INT_BITS(int_data);
  1904. if (!nr_bits) {
  1905. /* Not a bitfield member, member offset must be at byte
  1906. * boundary.
  1907. */
  1908. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1909. btf_verifier_log_member(env, struct_type, member,
  1910. "Invalid member offset");
  1911. return -EINVAL;
  1912. }
  1913. nr_bits = nr_int_data_bits;
  1914. } else if (nr_bits > nr_int_data_bits) {
  1915. btf_verifier_log_member(env, struct_type, member,
  1916. "Invalid member bitfield_size");
  1917. return -EINVAL;
  1918. }
  1919. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1920. nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
  1921. if (nr_copy_bits > BITS_PER_U128) {
  1922. btf_verifier_log_member(env, struct_type, member,
  1923. "nr_copy_bits exceeds 128");
  1924. return -EINVAL;
  1925. }
  1926. if (struct_size < bytes_offset ||
  1927. struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
  1928. btf_verifier_log_member(env, struct_type, member,
  1929. "Member exceeds struct_size");
  1930. return -EINVAL;
  1931. }
  1932. return 0;
  1933. }
  1934. static s32 btf_int_check_meta(struct btf_verifier_env *env,
  1935. const struct btf_type *t,
  1936. u32 meta_left)
  1937. {
  1938. u32 int_data, nr_bits, meta_needed = sizeof(int_data);
  1939. u16 encoding;
  1940. if (meta_left < meta_needed) {
  1941. btf_verifier_log_basic(env, t,
  1942. "meta_left:%u meta_needed:%u",
  1943. meta_left, meta_needed);
  1944. return -EINVAL;
  1945. }
  1946. if (btf_type_vlen(t)) {
  1947. btf_verifier_log_type(env, t, "vlen != 0");
  1948. return -EINVAL;
  1949. }
  1950. if (btf_type_kflag(t)) {
  1951. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  1952. return -EINVAL;
  1953. }
  1954. int_data = btf_type_int(t);
  1955. if (int_data & ~BTF_INT_MASK) {
  1956. btf_verifier_log_basic(env, t, "Invalid int_data:%x",
  1957. int_data);
  1958. return -EINVAL;
  1959. }
  1960. nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
  1961. if (nr_bits > BITS_PER_U128) {
  1962. btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
  1963. BITS_PER_U128);
  1964. return -EINVAL;
  1965. }
  1966. if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
  1967. btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
  1968. return -EINVAL;
  1969. }
  1970. /*
  1971. * Only one of the encoding bits is allowed and it
  1972. * should be sufficient for the pretty print purpose (i.e. decoding).
  1973. * Multiple bits can be allowed later if it is found
  1974. * to be insufficient.
  1975. */
  1976. encoding = BTF_INT_ENCODING(int_data);
  1977. if (encoding &&
  1978. encoding != BTF_INT_SIGNED &&
  1979. encoding != BTF_INT_CHAR &&
  1980. encoding != BTF_INT_BOOL) {
  1981. btf_verifier_log_type(env, t, "Unsupported encoding");
  1982. return -ENOTSUPP;
  1983. }
  1984. btf_verifier_log_type(env, t, NULL);
  1985. return meta_needed;
  1986. }
  1987. static void btf_int_log(struct btf_verifier_env *env,
  1988. const struct btf_type *t)
  1989. {
  1990. int int_data = btf_type_int(t);
  1991. btf_verifier_log(env,
  1992. "size=%u bits_offset=%u nr_bits=%u encoding=%s",
  1993. t->size, BTF_INT_OFFSET(int_data),
  1994. BTF_INT_BITS(int_data),
  1995. btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
  1996. }
  1997. static void btf_int128_print(struct btf_show *show, void *data)
  1998. {
  1999. /* data points to a __int128 number.
  2000. * Suppose
  2001. * int128_num = *(__int128 *)data;
  2002. * The below formulas shows what upper_num and lower_num represents:
  2003. * upper_num = int128_num >> 64;
  2004. * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
  2005. */
  2006. u64 upper_num, lower_num;
  2007. #ifdef __BIG_ENDIAN_BITFIELD
  2008. upper_num = *(u64 *)data;
  2009. lower_num = *(u64 *)(data + 8);
  2010. #else
  2011. upper_num = *(u64 *)(data + 8);
  2012. lower_num = *(u64 *)data;
  2013. #endif
  2014. if (upper_num == 0)
  2015. btf_show_type_value(show, "0x%llx", lower_num);
  2016. else
  2017. btf_show_type_values(show, "0x%llx%016llx", upper_num,
  2018. lower_num);
  2019. }
  2020. static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
  2021. u16 right_shift_bits)
  2022. {
  2023. u64 upper_num, lower_num;
  2024. #ifdef __BIG_ENDIAN_BITFIELD
  2025. upper_num = print_num[0];
  2026. lower_num = print_num[1];
  2027. #else
  2028. upper_num = print_num[1];
  2029. lower_num = print_num[0];
  2030. #endif
  2031. /* shake out un-needed bits by shift/or operations */
  2032. if (left_shift_bits >= 64) {
  2033. upper_num = lower_num << (left_shift_bits - 64);
  2034. lower_num = 0;
  2035. } else {
  2036. upper_num = (upper_num << left_shift_bits) |
  2037. (lower_num >> (64 - left_shift_bits));
  2038. lower_num = lower_num << left_shift_bits;
  2039. }
  2040. if (right_shift_bits >= 64) {
  2041. lower_num = upper_num >> (right_shift_bits - 64);
  2042. upper_num = 0;
  2043. } else {
  2044. lower_num = (lower_num >> right_shift_bits) |
  2045. (upper_num << (64 - right_shift_bits));
  2046. upper_num = upper_num >> right_shift_bits;
  2047. }
  2048. #ifdef __BIG_ENDIAN_BITFIELD
  2049. print_num[0] = upper_num;
  2050. print_num[1] = lower_num;
  2051. #else
  2052. print_num[0] = lower_num;
  2053. print_num[1] = upper_num;
  2054. #endif
  2055. }
  2056. static void btf_bitfield_show(void *data, u8 bits_offset,
  2057. u8 nr_bits, struct btf_show *show)
  2058. {
  2059. u16 left_shift_bits, right_shift_bits;
  2060. u8 nr_copy_bytes;
  2061. u8 nr_copy_bits;
  2062. u64 print_num[2] = {};
  2063. nr_copy_bits = nr_bits + bits_offset;
  2064. nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
  2065. memcpy(print_num, data, nr_copy_bytes);
  2066. #ifdef __BIG_ENDIAN_BITFIELD
  2067. left_shift_bits = bits_offset;
  2068. #else
  2069. left_shift_bits = BITS_PER_U128 - nr_copy_bits;
  2070. #endif
  2071. right_shift_bits = BITS_PER_U128 - nr_bits;
  2072. btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
  2073. btf_int128_print(show, print_num);
  2074. }
  2075. static void btf_int_bits_show(const struct btf *btf,
  2076. const struct btf_type *t,
  2077. void *data, u8 bits_offset,
  2078. struct btf_show *show)
  2079. {
  2080. u32 int_data = btf_type_int(t);
  2081. u8 nr_bits = BTF_INT_BITS(int_data);
  2082. u8 total_bits_offset;
  2083. /*
  2084. * bits_offset is at most 7.
  2085. * BTF_INT_OFFSET() cannot exceed 128 bits.
  2086. */
  2087. total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
  2088. data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
  2089. bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
  2090. btf_bitfield_show(data, bits_offset, nr_bits, show);
  2091. }
  2092. static void btf_int_show(const struct btf *btf, const struct btf_type *t,
  2093. u32 type_id, void *data, u8 bits_offset,
  2094. struct btf_show *show)
  2095. {
  2096. u32 int_data = btf_type_int(t);
  2097. u8 encoding = BTF_INT_ENCODING(int_data);
  2098. bool sign = encoding & BTF_INT_SIGNED;
  2099. u8 nr_bits = BTF_INT_BITS(int_data);
  2100. void *safe_data;
  2101. safe_data = btf_show_start_type(show, t, type_id, data);
  2102. if (!safe_data)
  2103. return;
  2104. if (bits_offset || BTF_INT_OFFSET(int_data) ||
  2105. BITS_PER_BYTE_MASKED(nr_bits)) {
  2106. btf_int_bits_show(btf, t, safe_data, bits_offset, show);
  2107. goto out;
  2108. }
  2109. switch (nr_bits) {
  2110. case 128:
  2111. btf_int128_print(show, safe_data);
  2112. break;
  2113. case 64:
  2114. if (sign)
  2115. btf_show_type_value(show, "%lld", *(s64 *)safe_data);
  2116. else
  2117. btf_show_type_value(show, "%llu", *(u64 *)safe_data);
  2118. break;
  2119. case 32:
  2120. if (sign)
  2121. btf_show_type_value(show, "%d", *(s32 *)safe_data);
  2122. else
  2123. btf_show_type_value(show, "%u", *(u32 *)safe_data);
  2124. break;
  2125. case 16:
  2126. if (sign)
  2127. btf_show_type_value(show, "%d", *(s16 *)safe_data);
  2128. else
  2129. btf_show_type_value(show, "%u", *(u16 *)safe_data);
  2130. break;
  2131. case 8:
  2132. if (show->state.array_encoding == BTF_INT_CHAR) {
  2133. /* check for null terminator */
  2134. if (show->state.array_terminated)
  2135. break;
  2136. if (*(char *)data == '\0') {
  2137. show->state.array_terminated = 1;
  2138. break;
  2139. }
  2140. if (isprint(*(char *)data)) {
  2141. btf_show_type_value(show, "'%c'",
  2142. *(char *)safe_data);
  2143. break;
  2144. }
  2145. }
  2146. if (sign)
  2147. btf_show_type_value(show, "%d", *(s8 *)safe_data);
  2148. else
  2149. btf_show_type_value(show, "%u", *(u8 *)safe_data);
  2150. break;
  2151. default:
  2152. btf_int_bits_show(btf, t, safe_data, bits_offset, show);
  2153. break;
  2154. }
  2155. out:
  2156. btf_show_end_type(show);
  2157. }
  2158. static const struct btf_kind_operations int_ops = {
  2159. .check_meta = btf_int_check_meta,
  2160. .resolve = btf_df_resolve,
  2161. .check_member = btf_int_check_member,
  2162. .check_kflag_member = btf_int_check_kflag_member,
  2163. .log_details = btf_int_log,
  2164. .show = btf_int_show,
  2165. };
  2166. static int btf_modifier_check_member(struct btf_verifier_env *env,
  2167. const struct btf_type *struct_type,
  2168. const struct btf_member *member,
  2169. const struct btf_type *member_type)
  2170. {
  2171. const struct btf_type *resolved_type;
  2172. u32 resolved_type_id = member->type;
  2173. struct btf_member resolved_member;
  2174. struct btf *btf = env->btf;
  2175. resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
  2176. if (!resolved_type) {
  2177. btf_verifier_log_member(env, struct_type, member,
  2178. "Invalid member");
  2179. return -EINVAL;
  2180. }
  2181. resolved_member = *member;
  2182. resolved_member.type = resolved_type_id;
  2183. return btf_type_ops(resolved_type)->check_member(env, struct_type,
  2184. &resolved_member,
  2185. resolved_type);
  2186. }
  2187. static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
  2188. const struct btf_type *struct_type,
  2189. const struct btf_member *member,
  2190. const struct btf_type *member_type)
  2191. {
  2192. const struct btf_type *resolved_type;
  2193. u32 resolved_type_id = member->type;
  2194. struct btf_member resolved_member;
  2195. struct btf *btf = env->btf;
  2196. resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
  2197. if (!resolved_type) {
  2198. btf_verifier_log_member(env, struct_type, member,
  2199. "Invalid member");
  2200. return -EINVAL;
  2201. }
  2202. resolved_member = *member;
  2203. resolved_member.type = resolved_type_id;
  2204. return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
  2205. &resolved_member,
  2206. resolved_type);
  2207. }
  2208. static int btf_ptr_check_member(struct btf_verifier_env *env,
  2209. const struct btf_type *struct_type,
  2210. const struct btf_member *member,
  2211. const struct btf_type *member_type)
  2212. {
  2213. u32 struct_size, struct_bits_off, bytes_offset;
  2214. struct_size = struct_type->size;
  2215. struct_bits_off = member->offset;
  2216. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  2217. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2218. btf_verifier_log_member(env, struct_type, member,
  2219. "Member is not byte aligned");
  2220. return -EINVAL;
  2221. }
  2222. if (struct_size - bytes_offset < sizeof(void *)) {
  2223. btf_verifier_log_member(env, struct_type, member,
  2224. "Member exceeds struct_size");
  2225. return -EINVAL;
  2226. }
  2227. return 0;
  2228. }
  2229. static int btf_ref_type_check_meta(struct btf_verifier_env *env,
  2230. const struct btf_type *t,
  2231. u32 meta_left)
  2232. {
  2233. const char *value;
  2234. if (btf_type_vlen(t)) {
  2235. btf_verifier_log_type(env, t, "vlen != 0");
  2236. return -EINVAL;
  2237. }
  2238. if (btf_type_kflag(t)) {
  2239. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2240. return -EINVAL;
  2241. }
  2242. if (!BTF_TYPE_ID_VALID(t->type)) {
  2243. btf_verifier_log_type(env, t, "Invalid type_id");
  2244. return -EINVAL;
  2245. }
  2246. /* typedef/type_tag type must have a valid name, and other ref types,
  2247. * volatile, const, restrict, should have a null name.
  2248. */
  2249. if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
  2250. if (!t->name_off ||
  2251. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2252. btf_verifier_log_type(env, t, "Invalid name");
  2253. return -EINVAL;
  2254. }
  2255. } else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) {
  2256. value = btf_name_by_offset(env->btf, t->name_off);
  2257. if (!value || !value[0]) {
  2258. btf_verifier_log_type(env, t, "Invalid name");
  2259. return -EINVAL;
  2260. }
  2261. } else {
  2262. if (t->name_off) {
  2263. btf_verifier_log_type(env, t, "Invalid name");
  2264. return -EINVAL;
  2265. }
  2266. }
  2267. btf_verifier_log_type(env, t, NULL);
  2268. return 0;
  2269. }
  2270. static int btf_modifier_resolve(struct btf_verifier_env *env,
  2271. const struct resolve_vertex *v)
  2272. {
  2273. const struct btf_type *t = v->t;
  2274. const struct btf_type *next_type;
  2275. u32 next_type_id = t->type;
  2276. struct btf *btf = env->btf;
  2277. next_type = btf_type_by_id(btf, next_type_id);
  2278. if (!next_type || btf_type_is_resolve_source_only(next_type)) {
  2279. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2280. return -EINVAL;
  2281. }
  2282. if (!env_type_is_resolve_sink(env, next_type) &&
  2283. !env_type_is_resolved(env, next_type_id))
  2284. return env_stack_push(env, next_type, next_type_id);
  2285. /* Figure out the resolved next_type_id with size.
  2286. * They will be stored in the current modifier's
  2287. * resolved_ids and resolved_sizes such that it can
  2288. * save us a few type-following when we use it later (e.g. in
  2289. * pretty print).
  2290. */
  2291. if (!btf_type_id_size(btf, &next_type_id, NULL)) {
  2292. if (env_type_is_resolved(env, next_type_id))
  2293. next_type = btf_type_id_resolve(btf, &next_type_id);
  2294. /* "typedef void new_void", "const void"...etc */
  2295. if (!btf_type_is_void(next_type) &&
  2296. !btf_type_is_fwd(next_type) &&
  2297. !btf_type_is_func_proto(next_type)) {
  2298. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2299. return -EINVAL;
  2300. }
  2301. }
  2302. env_stack_pop_resolved(env, next_type_id, 0);
  2303. return 0;
  2304. }
  2305. static int btf_var_resolve(struct btf_verifier_env *env,
  2306. const struct resolve_vertex *v)
  2307. {
  2308. const struct btf_type *next_type;
  2309. const struct btf_type *t = v->t;
  2310. u32 next_type_id = t->type;
  2311. struct btf *btf = env->btf;
  2312. next_type = btf_type_by_id(btf, next_type_id);
  2313. if (!next_type || btf_type_is_resolve_source_only(next_type)) {
  2314. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2315. return -EINVAL;
  2316. }
  2317. if (!env_type_is_resolve_sink(env, next_type) &&
  2318. !env_type_is_resolved(env, next_type_id))
  2319. return env_stack_push(env, next_type, next_type_id);
  2320. if (btf_type_is_modifier(next_type)) {
  2321. const struct btf_type *resolved_type;
  2322. u32 resolved_type_id;
  2323. resolved_type_id = next_type_id;
  2324. resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
  2325. if (btf_type_is_ptr(resolved_type) &&
  2326. !env_type_is_resolve_sink(env, resolved_type) &&
  2327. !env_type_is_resolved(env, resolved_type_id))
  2328. return env_stack_push(env, resolved_type,
  2329. resolved_type_id);
  2330. }
  2331. /* We must resolve to something concrete at this point, no
  2332. * forward types or similar that would resolve to size of
  2333. * zero is allowed.
  2334. */
  2335. if (!btf_type_id_size(btf, &next_type_id, NULL)) {
  2336. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2337. return -EINVAL;
  2338. }
  2339. env_stack_pop_resolved(env, next_type_id, 0);
  2340. return 0;
  2341. }
  2342. static int btf_ptr_resolve(struct btf_verifier_env *env,
  2343. const struct resolve_vertex *v)
  2344. {
  2345. const struct btf_type *next_type;
  2346. const struct btf_type *t = v->t;
  2347. u32 next_type_id = t->type;
  2348. struct btf *btf = env->btf;
  2349. next_type = btf_type_by_id(btf, next_type_id);
  2350. if (!next_type || btf_type_is_resolve_source_only(next_type)) {
  2351. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2352. return -EINVAL;
  2353. }
  2354. if (!env_type_is_resolve_sink(env, next_type) &&
  2355. !env_type_is_resolved(env, next_type_id))
  2356. return env_stack_push(env, next_type, next_type_id);
  2357. /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
  2358. * the modifier may have stopped resolving when it was resolved
  2359. * to a ptr (last-resolved-ptr).
  2360. *
  2361. * We now need to continue from the last-resolved-ptr to
  2362. * ensure the last-resolved-ptr will not referring back to
  2363. * the current ptr (t).
  2364. */
  2365. if (btf_type_is_modifier(next_type)) {
  2366. const struct btf_type *resolved_type;
  2367. u32 resolved_type_id;
  2368. resolved_type_id = next_type_id;
  2369. resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
  2370. if (btf_type_is_ptr(resolved_type) &&
  2371. !env_type_is_resolve_sink(env, resolved_type) &&
  2372. !env_type_is_resolved(env, resolved_type_id))
  2373. return env_stack_push(env, resolved_type,
  2374. resolved_type_id);
  2375. }
  2376. if (!btf_type_id_size(btf, &next_type_id, NULL)) {
  2377. if (env_type_is_resolved(env, next_type_id))
  2378. next_type = btf_type_id_resolve(btf, &next_type_id);
  2379. if (!btf_type_is_void(next_type) &&
  2380. !btf_type_is_fwd(next_type) &&
  2381. !btf_type_is_func_proto(next_type)) {
  2382. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2383. return -EINVAL;
  2384. }
  2385. }
  2386. env_stack_pop_resolved(env, next_type_id, 0);
  2387. return 0;
  2388. }
  2389. static void btf_modifier_show(const struct btf *btf,
  2390. const struct btf_type *t,
  2391. u32 type_id, void *data,
  2392. u8 bits_offset, struct btf_show *show)
  2393. {
  2394. if (btf->resolved_ids)
  2395. t = btf_type_id_resolve(btf, &type_id);
  2396. else
  2397. t = btf_type_skip_modifiers(btf, type_id, NULL);
  2398. btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
  2399. }
  2400. static void btf_var_show(const struct btf *btf, const struct btf_type *t,
  2401. u32 type_id, void *data, u8 bits_offset,
  2402. struct btf_show *show)
  2403. {
  2404. t = btf_type_id_resolve(btf, &type_id);
  2405. btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
  2406. }
  2407. static void btf_ptr_show(const struct btf *btf, const struct btf_type *t,
  2408. u32 type_id, void *data, u8 bits_offset,
  2409. struct btf_show *show)
  2410. {
  2411. void *safe_data;
  2412. safe_data = btf_show_start_type(show, t, type_id, data);
  2413. if (!safe_data)
  2414. return;
  2415. /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
  2416. if (show->flags & BTF_SHOW_PTR_RAW)
  2417. btf_show_type_value(show, "0x%px", *(void **)safe_data);
  2418. else
  2419. btf_show_type_value(show, "0x%p", *(void **)safe_data);
  2420. btf_show_end_type(show);
  2421. }
  2422. static void btf_ref_type_log(struct btf_verifier_env *env,
  2423. const struct btf_type *t)
  2424. {
  2425. btf_verifier_log(env, "type_id=%u", t->type);
  2426. }
  2427. static struct btf_kind_operations modifier_ops = {
  2428. .check_meta = btf_ref_type_check_meta,
  2429. .resolve = btf_modifier_resolve,
  2430. .check_member = btf_modifier_check_member,
  2431. .check_kflag_member = btf_modifier_check_kflag_member,
  2432. .log_details = btf_ref_type_log,
  2433. .show = btf_modifier_show,
  2434. };
  2435. static struct btf_kind_operations ptr_ops = {
  2436. .check_meta = btf_ref_type_check_meta,
  2437. .resolve = btf_ptr_resolve,
  2438. .check_member = btf_ptr_check_member,
  2439. .check_kflag_member = btf_generic_check_kflag_member,
  2440. .log_details = btf_ref_type_log,
  2441. .show = btf_ptr_show,
  2442. };
  2443. static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
  2444. const struct btf_type *t,
  2445. u32 meta_left)
  2446. {
  2447. if (btf_type_vlen(t)) {
  2448. btf_verifier_log_type(env, t, "vlen != 0");
  2449. return -EINVAL;
  2450. }
  2451. if (t->type) {
  2452. btf_verifier_log_type(env, t, "type != 0");
  2453. return -EINVAL;
  2454. }
  2455. /* fwd type must have a valid name */
  2456. if (!t->name_off ||
  2457. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2458. btf_verifier_log_type(env, t, "Invalid name");
  2459. return -EINVAL;
  2460. }
  2461. btf_verifier_log_type(env, t, NULL);
  2462. return 0;
  2463. }
  2464. static void btf_fwd_type_log(struct btf_verifier_env *env,
  2465. const struct btf_type *t)
  2466. {
  2467. btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
  2468. }
  2469. static struct btf_kind_operations fwd_ops = {
  2470. .check_meta = btf_fwd_check_meta,
  2471. .resolve = btf_df_resolve,
  2472. .check_member = btf_df_check_member,
  2473. .check_kflag_member = btf_df_check_kflag_member,
  2474. .log_details = btf_fwd_type_log,
  2475. .show = btf_df_show,
  2476. };
  2477. static int btf_array_check_member(struct btf_verifier_env *env,
  2478. const struct btf_type *struct_type,
  2479. const struct btf_member *member,
  2480. const struct btf_type *member_type)
  2481. {
  2482. u32 struct_bits_off = member->offset;
  2483. u32 struct_size, bytes_offset;
  2484. u32 array_type_id, array_size;
  2485. struct btf *btf = env->btf;
  2486. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2487. btf_verifier_log_member(env, struct_type, member,
  2488. "Member is not byte aligned");
  2489. return -EINVAL;
  2490. }
  2491. array_type_id = member->type;
  2492. btf_type_id_size(btf, &array_type_id, &array_size);
  2493. struct_size = struct_type->size;
  2494. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  2495. if (struct_size - bytes_offset < array_size) {
  2496. btf_verifier_log_member(env, struct_type, member,
  2497. "Member exceeds struct_size");
  2498. return -EINVAL;
  2499. }
  2500. return 0;
  2501. }
  2502. static s32 btf_array_check_meta(struct btf_verifier_env *env,
  2503. const struct btf_type *t,
  2504. u32 meta_left)
  2505. {
  2506. const struct btf_array *array = btf_type_array(t);
  2507. u32 meta_needed = sizeof(*array);
  2508. if (meta_left < meta_needed) {
  2509. btf_verifier_log_basic(env, t,
  2510. "meta_left:%u meta_needed:%u",
  2511. meta_left, meta_needed);
  2512. return -EINVAL;
  2513. }
  2514. /* array type should not have a name */
  2515. if (t->name_off) {
  2516. btf_verifier_log_type(env, t, "Invalid name");
  2517. return -EINVAL;
  2518. }
  2519. if (btf_type_vlen(t)) {
  2520. btf_verifier_log_type(env, t, "vlen != 0");
  2521. return -EINVAL;
  2522. }
  2523. if (btf_type_kflag(t)) {
  2524. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2525. return -EINVAL;
  2526. }
  2527. if (t->size) {
  2528. btf_verifier_log_type(env, t, "size != 0");
  2529. return -EINVAL;
  2530. }
  2531. /* Array elem type and index type cannot be in type void,
  2532. * so !array->type and !array->index_type are not allowed.
  2533. */
  2534. if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
  2535. btf_verifier_log_type(env, t, "Invalid elem");
  2536. return -EINVAL;
  2537. }
  2538. if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
  2539. btf_verifier_log_type(env, t, "Invalid index");
  2540. return -EINVAL;
  2541. }
  2542. btf_verifier_log_type(env, t, NULL);
  2543. return meta_needed;
  2544. }
  2545. static int btf_array_resolve(struct btf_verifier_env *env,
  2546. const struct resolve_vertex *v)
  2547. {
  2548. const struct btf_array *array = btf_type_array(v->t);
  2549. const struct btf_type *elem_type, *index_type;
  2550. u32 elem_type_id, index_type_id;
  2551. struct btf *btf = env->btf;
  2552. u32 elem_size;
  2553. /* Check array->index_type */
  2554. index_type_id = array->index_type;
  2555. index_type = btf_type_by_id(btf, index_type_id);
  2556. if (btf_type_nosize_or_null(index_type) ||
  2557. btf_type_is_resolve_source_only(index_type)) {
  2558. btf_verifier_log_type(env, v->t, "Invalid index");
  2559. return -EINVAL;
  2560. }
  2561. if (!env_type_is_resolve_sink(env, index_type) &&
  2562. !env_type_is_resolved(env, index_type_id))
  2563. return env_stack_push(env, index_type, index_type_id);
  2564. index_type = btf_type_id_size(btf, &index_type_id, NULL);
  2565. if (!index_type || !btf_type_is_int(index_type) ||
  2566. !btf_type_int_is_regular(index_type)) {
  2567. btf_verifier_log_type(env, v->t, "Invalid index");
  2568. return -EINVAL;
  2569. }
  2570. /* Check array->type */
  2571. elem_type_id = array->type;
  2572. elem_type = btf_type_by_id(btf, elem_type_id);
  2573. if (btf_type_nosize_or_null(elem_type) ||
  2574. btf_type_is_resolve_source_only(elem_type)) {
  2575. btf_verifier_log_type(env, v->t,
  2576. "Invalid elem");
  2577. return -EINVAL;
  2578. }
  2579. if (!env_type_is_resolve_sink(env, elem_type) &&
  2580. !env_type_is_resolved(env, elem_type_id))
  2581. return env_stack_push(env, elem_type, elem_type_id);
  2582. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  2583. if (!elem_type) {
  2584. btf_verifier_log_type(env, v->t, "Invalid elem");
  2585. return -EINVAL;
  2586. }
  2587. if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
  2588. btf_verifier_log_type(env, v->t, "Invalid array of int");
  2589. return -EINVAL;
  2590. }
  2591. if (array->nelems && elem_size > U32_MAX / array->nelems) {
  2592. btf_verifier_log_type(env, v->t,
  2593. "Array size overflows U32_MAX");
  2594. return -EINVAL;
  2595. }
  2596. env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
  2597. return 0;
  2598. }
  2599. static void btf_array_log(struct btf_verifier_env *env,
  2600. const struct btf_type *t)
  2601. {
  2602. const struct btf_array *array = btf_type_array(t);
  2603. btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
  2604. array->type, array->index_type, array->nelems);
  2605. }
  2606. static void __btf_array_show(const struct btf *btf, const struct btf_type *t,
  2607. u32 type_id, void *data, u8 bits_offset,
  2608. struct btf_show *show)
  2609. {
  2610. const struct btf_array *array = btf_type_array(t);
  2611. const struct btf_kind_operations *elem_ops;
  2612. const struct btf_type *elem_type;
  2613. u32 i, elem_size = 0, elem_type_id;
  2614. u16 encoding = 0;
  2615. elem_type_id = array->type;
  2616. elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL);
  2617. if (elem_type && btf_type_has_size(elem_type))
  2618. elem_size = elem_type->size;
  2619. if (elem_type && btf_type_is_int(elem_type)) {
  2620. u32 int_type = btf_type_int(elem_type);
  2621. encoding = BTF_INT_ENCODING(int_type);
  2622. /*
  2623. * BTF_INT_CHAR encoding never seems to be set for
  2624. * char arrays, so if size is 1 and element is
  2625. * printable as a char, we'll do that.
  2626. */
  2627. if (elem_size == 1)
  2628. encoding = BTF_INT_CHAR;
  2629. }
  2630. if (!btf_show_start_array_type(show, t, type_id, encoding, data))
  2631. return;
  2632. if (!elem_type)
  2633. goto out;
  2634. elem_ops = btf_type_ops(elem_type);
  2635. for (i = 0; i < array->nelems; i++) {
  2636. btf_show_start_array_member(show);
  2637. elem_ops->show(btf, elem_type, elem_type_id, data,
  2638. bits_offset, show);
  2639. data += elem_size;
  2640. btf_show_end_array_member(show);
  2641. if (show->state.array_terminated)
  2642. break;
  2643. }
  2644. out:
  2645. btf_show_end_array_type(show);
  2646. }
  2647. static void btf_array_show(const struct btf *btf, const struct btf_type *t,
  2648. u32 type_id, void *data, u8 bits_offset,
  2649. struct btf_show *show)
  2650. {
  2651. const struct btf_member *m = show->state.member;
  2652. /*
  2653. * First check if any members would be shown (are non-zero).
  2654. * See comments above "struct btf_show" definition for more
  2655. * details on how this works at a high-level.
  2656. */
  2657. if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
  2658. if (!show->state.depth_check) {
  2659. show->state.depth_check = show->state.depth + 1;
  2660. show->state.depth_to_show = 0;
  2661. }
  2662. __btf_array_show(btf, t, type_id, data, bits_offset, show);
  2663. show->state.member = m;
  2664. if (show->state.depth_check != show->state.depth + 1)
  2665. return;
  2666. show->state.depth_check = 0;
  2667. if (show->state.depth_to_show <= show->state.depth)
  2668. return;
  2669. /*
  2670. * Reaching here indicates we have recursed and found
  2671. * non-zero array member(s).
  2672. */
  2673. }
  2674. __btf_array_show(btf, t, type_id, data, bits_offset, show);
  2675. }
  2676. static struct btf_kind_operations array_ops = {
  2677. .check_meta = btf_array_check_meta,
  2678. .resolve = btf_array_resolve,
  2679. .check_member = btf_array_check_member,
  2680. .check_kflag_member = btf_generic_check_kflag_member,
  2681. .log_details = btf_array_log,
  2682. .show = btf_array_show,
  2683. };
  2684. static int btf_struct_check_member(struct btf_verifier_env *env,
  2685. const struct btf_type *struct_type,
  2686. const struct btf_member *member,
  2687. const struct btf_type *member_type)
  2688. {
  2689. u32 struct_bits_off = member->offset;
  2690. u32 struct_size, bytes_offset;
  2691. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2692. btf_verifier_log_member(env, struct_type, member,
  2693. "Member is not byte aligned");
  2694. return -EINVAL;
  2695. }
  2696. struct_size = struct_type->size;
  2697. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  2698. if (struct_size - bytes_offset < member_type->size) {
  2699. btf_verifier_log_member(env, struct_type, member,
  2700. "Member exceeds struct_size");
  2701. return -EINVAL;
  2702. }
  2703. return 0;
  2704. }
  2705. static s32 btf_struct_check_meta(struct btf_verifier_env *env,
  2706. const struct btf_type *t,
  2707. u32 meta_left)
  2708. {
  2709. bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
  2710. const struct btf_member *member;
  2711. u32 meta_needed, last_offset;
  2712. struct btf *btf = env->btf;
  2713. u32 struct_size = t->size;
  2714. u32 offset;
  2715. u16 i;
  2716. meta_needed = btf_type_vlen(t) * sizeof(*member);
  2717. if (meta_left < meta_needed) {
  2718. btf_verifier_log_basic(env, t,
  2719. "meta_left:%u meta_needed:%u",
  2720. meta_left, meta_needed);
  2721. return -EINVAL;
  2722. }
  2723. /* struct type either no name or a valid one */
  2724. if (t->name_off &&
  2725. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2726. btf_verifier_log_type(env, t, "Invalid name");
  2727. return -EINVAL;
  2728. }
  2729. btf_verifier_log_type(env, t, NULL);
  2730. last_offset = 0;
  2731. for_each_member(i, t, member) {
  2732. if (!btf_name_offset_valid(btf, member->name_off)) {
  2733. btf_verifier_log_member(env, t, member,
  2734. "Invalid member name_offset:%u",
  2735. member->name_off);
  2736. return -EINVAL;
  2737. }
  2738. /* struct member either no name or a valid one */
  2739. if (member->name_off &&
  2740. !btf_name_valid_identifier(btf, member->name_off)) {
  2741. btf_verifier_log_member(env, t, member, "Invalid name");
  2742. return -EINVAL;
  2743. }
  2744. /* A member cannot be in type void */
  2745. if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
  2746. btf_verifier_log_member(env, t, member,
  2747. "Invalid type_id");
  2748. return -EINVAL;
  2749. }
  2750. offset = __btf_member_bit_offset(t, member);
  2751. if (is_union && offset) {
  2752. btf_verifier_log_member(env, t, member,
  2753. "Invalid member bits_offset");
  2754. return -EINVAL;
  2755. }
  2756. /*
  2757. * ">" instead of ">=" because the last member could be
  2758. * "char a[0];"
  2759. */
  2760. if (last_offset > offset) {
  2761. btf_verifier_log_member(env, t, member,
  2762. "Invalid member bits_offset");
  2763. return -EINVAL;
  2764. }
  2765. if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
  2766. btf_verifier_log_member(env, t, member,
  2767. "Member bits_offset exceeds its struct size");
  2768. return -EINVAL;
  2769. }
  2770. btf_verifier_log_member(env, t, member, NULL);
  2771. last_offset = offset;
  2772. }
  2773. return meta_needed;
  2774. }
  2775. static int btf_struct_resolve(struct btf_verifier_env *env,
  2776. const struct resolve_vertex *v)
  2777. {
  2778. const struct btf_member *member;
  2779. int err;
  2780. u16 i;
  2781. /* Before continue resolving the next_member,
  2782. * ensure the last member is indeed resolved to a
  2783. * type with size info.
  2784. */
  2785. if (v->next_member) {
  2786. const struct btf_type *last_member_type;
  2787. const struct btf_member *last_member;
  2788. u32 last_member_type_id;
  2789. last_member = btf_type_member(v->t) + v->next_member - 1;
  2790. last_member_type_id = last_member->type;
  2791. if (WARN_ON_ONCE(!env_type_is_resolved(env,
  2792. last_member_type_id)))
  2793. return -EINVAL;
  2794. last_member_type = btf_type_by_id(env->btf,
  2795. last_member_type_id);
  2796. if (btf_type_kflag(v->t))
  2797. err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
  2798. last_member,
  2799. last_member_type);
  2800. else
  2801. err = btf_type_ops(last_member_type)->check_member(env, v->t,
  2802. last_member,
  2803. last_member_type);
  2804. if (err)
  2805. return err;
  2806. }
  2807. for_each_member_from(i, v->next_member, v->t, member) {
  2808. u32 member_type_id = member->type;
  2809. const struct btf_type *member_type = btf_type_by_id(env->btf,
  2810. member_type_id);
  2811. if (btf_type_nosize_or_null(member_type) ||
  2812. btf_type_is_resolve_source_only(member_type)) {
  2813. btf_verifier_log_member(env, v->t, member,
  2814. "Invalid member");
  2815. return -EINVAL;
  2816. }
  2817. if (!env_type_is_resolve_sink(env, member_type) &&
  2818. !env_type_is_resolved(env, member_type_id)) {
  2819. env_stack_set_next_member(env, i + 1);
  2820. return env_stack_push(env, member_type, member_type_id);
  2821. }
  2822. if (btf_type_kflag(v->t))
  2823. err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
  2824. member,
  2825. member_type);
  2826. else
  2827. err = btf_type_ops(member_type)->check_member(env, v->t,
  2828. member,
  2829. member_type);
  2830. if (err)
  2831. return err;
  2832. }
  2833. env_stack_pop_resolved(env, 0, 0);
  2834. return 0;
  2835. }
  2836. static void btf_struct_log(struct btf_verifier_env *env,
  2837. const struct btf_type *t)
  2838. {
  2839. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  2840. }
  2841. enum {
  2842. BTF_FIELD_IGNORE = 0,
  2843. BTF_FIELD_FOUND = 1,
  2844. };
  2845. struct btf_field_info {
  2846. enum btf_field_type type;
  2847. u32 off;
  2848. union {
  2849. struct {
  2850. u32 type_id;
  2851. } kptr;
  2852. struct {
  2853. const char *node_name;
  2854. u32 value_btf_id;
  2855. } graph_root;
  2856. };
  2857. };
  2858. static int btf_find_struct(const struct btf *btf, const struct btf_type *t,
  2859. u32 off, int sz, enum btf_field_type field_type,
  2860. struct btf_field_info *info)
  2861. {
  2862. if (!__btf_type_is_struct(t))
  2863. return BTF_FIELD_IGNORE;
  2864. if (t->size != sz)
  2865. return BTF_FIELD_IGNORE;
  2866. info->type = field_type;
  2867. info->off = off;
  2868. return BTF_FIELD_FOUND;
  2869. }
  2870. static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,
  2871. u32 off, int sz, struct btf_field_info *info)
  2872. {
  2873. enum btf_field_type type;
  2874. u32 res_id;
  2875. /* Permit modifiers on the pointer itself */
  2876. if (btf_type_is_volatile(t))
  2877. t = btf_type_by_id(btf, t->type);
  2878. /* For PTR, sz is always == 8 */
  2879. if (!btf_type_is_ptr(t))
  2880. return BTF_FIELD_IGNORE;
  2881. t = btf_type_by_id(btf, t->type);
  2882. if (!btf_type_is_type_tag(t))
  2883. return BTF_FIELD_IGNORE;
  2884. /* Reject extra tags */
  2885. if (btf_type_is_type_tag(btf_type_by_id(btf, t->type)))
  2886. return -EINVAL;
  2887. if (!strcmp("kptr_untrusted", __btf_name_by_offset(btf, t->name_off)))
  2888. type = BPF_KPTR_UNREF;
  2889. else if (!strcmp("kptr", __btf_name_by_offset(btf, t->name_off)))
  2890. type = BPF_KPTR_REF;
  2891. else if (!strcmp("percpu_kptr", __btf_name_by_offset(btf, t->name_off)))
  2892. type = BPF_KPTR_PERCPU;
  2893. else
  2894. return -EINVAL;
  2895. /* Get the base type */
  2896. t = btf_type_skip_modifiers(btf, t->type, &res_id);
  2897. /* Only pointer to struct is allowed */
  2898. if (!__btf_type_is_struct(t))
  2899. return -EINVAL;
  2900. info->type = type;
  2901. info->off = off;
  2902. info->kptr.type_id = res_id;
  2903. return BTF_FIELD_FOUND;
  2904. }
  2905. int btf_find_next_decl_tag(const struct btf *btf, const struct btf_type *pt,
  2906. int comp_idx, const char *tag_key, int last_id)
  2907. {
  2908. int len = strlen(tag_key);
  2909. int i, n;
  2910. for (i = last_id + 1, n = btf_nr_types(btf); i < n; i++) {
  2911. const struct btf_type *t = btf_type_by_id(btf, i);
  2912. if (!btf_type_is_decl_tag(t))
  2913. continue;
  2914. if (pt != btf_type_by_id(btf, t->type))
  2915. continue;
  2916. if (btf_type_decl_tag(t)->component_idx != comp_idx)
  2917. continue;
  2918. if (strncmp(__btf_name_by_offset(btf, t->name_off), tag_key, len))
  2919. continue;
  2920. return i;
  2921. }
  2922. return -ENOENT;
  2923. }
  2924. const char *btf_find_decl_tag_value(const struct btf *btf, const struct btf_type *pt,
  2925. int comp_idx, const char *tag_key)
  2926. {
  2927. const char *value = NULL;
  2928. const struct btf_type *t;
  2929. int len, id;
  2930. id = btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, 0);
  2931. if (id < 0)
  2932. return ERR_PTR(id);
  2933. t = btf_type_by_id(btf, id);
  2934. len = strlen(tag_key);
  2935. value = __btf_name_by_offset(btf, t->name_off) + len;
  2936. /* Prevent duplicate entries for same type */
  2937. id = btf_find_next_decl_tag(btf, pt, comp_idx, tag_key, id);
  2938. if (id >= 0)
  2939. return ERR_PTR(-EEXIST);
  2940. return value;
  2941. }
  2942. static int
  2943. btf_find_graph_root(const struct btf *btf, const struct btf_type *pt,
  2944. const struct btf_type *t, int comp_idx, u32 off,
  2945. int sz, struct btf_field_info *info,
  2946. enum btf_field_type head_type)
  2947. {
  2948. const char *node_field_name;
  2949. const char *value_type;
  2950. s32 id;
  2951. if (!__btf_type_is_struct(t))
  2952. return BTF_FIELD_IGNORE;
  2953. if (t->size != sz)
  2954. return BTF_FIELD_IGNORE;
  2955. value_type = btf_find_decl_tag_value(btf, pt, comp_idx, "contains:");
  2956. if (IS_ERR(value_type))
  2957. return -EINVAL;
  2958. node_field_name = strstr(value_type, ":");
  2959. if (!node_field_name)
  2960. return -EINVAL;
  2961. value_type = kstrndup(value_type, node_field_name - value_type, GFP_KERNEL | __GFP_NOWARN);
  2962. if (!value_type)
  2963. return -ENOMEM;
  2964. id = btf_find_by_name_kind(btf, value_type, BTF_KIND_STRUCT);
  2965. kfree(value_type);
  2966. if (id < 0)
  2967. return id;
  2968. node_field_name++;
  2969. if (str_is_empty(node_field_name))
  2970. return -EINVAL;
  2971. info->type = head_type;
  2972. info->off = off;
  2973. info->graph_root.value_btf_id = id;
  2974. info->graph_root.node_name = node_field_name;
  2975. return BTF_FIELD_FOUND;
  2976. }
  2977. #define field_mask_test_name(field_type, field_type_str) \
  2978. if (field_mask & field_type && !strcmp(name, field_type_str)) { \
  2979. type = field_type; \
  2980. goto end; \
  2981. }
  2982. static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_type,
  2983. u32 field_mask, u32 *seen_mask,
  2984. int *align, int *sz)
  2985. {
  2986. int type = 0;
  2987. const char *name = __btf_name_by_offset(btf, var_type->name_off);
  2988. if (field_mask & BPF_SPIN_LOCK) {
  2989. if (!strcmp(name, "bpf_spin_lock")) {
  2990. if (*seen_mask & BPF_SPIN_LOCK)
  2991. return -E2BIG;
  2992. *seen_mask |= BPF_SPIN_LOCK;
  2993. type = BPF_SPIN_LOCK;
  2994. goto end;
  2995. }
  2996. }
  2997. if (field_mask & BPF_TIMER) {
  2998. if (!strcmp(name, "bpf_timer")) {
  2999. if (*seen_mask & BPF_TIMER)
  3000. return -E2BIG;
  3001. *seen_mask |= BPF_TIMER;
  3002. type = BPF_TIMER;
  3003. goto end;
  3004. }
  3005. }
  3006. if (field_mask & BPF_WORKQUEUE) {
  3007. if (!strcmp(name, "bpf_wq")) {
  3008. if (*seen_mask & BPF_WORKQUEUE)
  3009. return -E2BIG;
  3010. *seen_mask |= BPF_WORKQUEUE;
  3011. type = BPF_WORKQUEUE;
  3012. goto end;
  3013. }
  3014. }
  3015. field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");
  3016. field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");
  3017. field_mask_test_name(BPF_RB_ROOT, "bpf_rb_root");
  3018. field_mask_test_name(BPF_RB_NODE, "bpf_rb_node");
  3019. field_mask_test_name(BPF_REFCOUNT, "bpf_refcount");
  3020. /* Only return BPF_KPTR when all other types with matchable names fail */
  3021. if (field_mask & BPF_KPTR && !__btf_type_is_struct(var_type)) {
  3022. type = BPF_KPTR_REF;
  3023. goto end;
  3024. }
  3025. return 0;
  3026. end:
  3027. *sz = btf_field_type_size(type);
  3028. *align = btf_field_type_align(type);
  3029. return type;
  3030. }
  3031. #undef field_mask_test_name
  3032. /* Repeat a number of fields for a specified number of times.
  3033. *
  3034. * Copy the fields starting from the first field and repeat them for
  3035. * repeat_cnt times. The fields are repeated by adding the offset of each
  3036. * field with
  3037. * (i + 1) * elem_size
  3038. * where i is the repeat index and elem_size is the size of an element.
  3039. */
  3040. static int btf_repeat_fields(struct btf_field_info *info, int info_cnt,
  3041. u32 field_cnt, u32 repeat_cnt, u32 elem_size)
  3042. {
  3043. u32 i, j;
  3044. u32 cur;
  3045. /* Ensure not repeating fields that should not be repeated. */
  3046. for (i = 0; i < field_cnt; i++) {
  3047. switch (info[i].type) {
  3048. case BPF_KPTR_UNREF:
  3049. case BPF_KPTR_REF:
  3050. case BPF_KPTR_PERCPU:
  3051. case BPF_LIST_HEAD:
  3052. case BPF_RB_ROOT:
  3053. break;
  3054. default:
  3055. return -EINVAL;
  3056. }
  3057. }
  3058. /* The type of struct size or variable size is u32,
  3059. * so the multiplication will not overflow.
  3060. */
  3061. if (field_cnt * (repeat_cnt + 1) > info_cnt)
  3062. return -E2BIG;
  3063. cur = field_cnt;
  3064. for (i = 0; i < repeat_cnt; i++) {
  3065. memcpy(&info[cur], &info[0], field_cnt * sizeof(info[0]));
  3066. for (j = 0; j < field_cnt; j++)
  3067. info[cur++].off += (i + 1) * elem_size;
  3068. }
  3069. return 0;
  3070. }
  3071. static int btf_find_struct_field(const struct btf *btf,
  3072. const struct btf_type *t, u32 field_mask,
  3073. struct btf_field_info *info, int info_cnt,
  3074. u32 level);
  3075. /* Find special fields in the struct type of a field.
  3076. *
  3077. * This function is used to find fields of special types that is not a
  3078. * global variable or a direct field of a struct type. It also handles the
  3079. * repetition if it is the element type of an array.
  3080. */
  3081. static int btf_find_nested_struct(const struct btf *btf, const struct btf_type *t,
  3082. u32 off, u32 nelems,
  3083. u32 field_mask, struct btf_field_info *info,
  3084. int info_cnt, u32 level)
  3085. {
  3086. int ret, err, i;
  3087. level++;
  3088. if (level >= MAX_RESOLVE_DEPTH)
  3089. return -E2BIG;
  3090. ret = btf_find_struct_field(btf, t, field_mask, info, info_cnt, level);
  3091. if (ret <= 0)
  3092. return ret;
  3093. /* Shift the offsets of the nested struct fields to the offsets
  3094. * related to the container.
  3095. */
  3096. for (i = 0; i < ret; i++)
  3097. info[i].off += off;
  3098. if (nelems > 1) {
  3099. err = btf_repeat_fields(info, info_cnt, ret, nelems - 1, t->size);
  3100. if (err == 0)
  3101. ret *= nelems;
  3102. else
  3103. ret = err;
  3104. }
  3105. return ret;
  3106. }
  3107. static int btf_find_field_one(const struct btf *btf,
  3108. const struct btf_type *var,
  3109. const struct btf_type *var_type,
  3110. int var_idx,
  3111. u32 off, u32 expected_size,
  3112. u32 field_mask, u32 *seen_mask,
  3113. struct btf_field_info *info, int info_cnt,
  3114. u32 level)
  3115. {
  3116. int ret, align, sz, field_type;
  3117. struct btf_field_info tmp;
  3118. const struct btf_array *array;
  3119. u32 i, nelems = 1;
  3120. /* Walk into array types to find the element type and the number of
  3121. * elements in the (flattened) array.
  3122. */
  3123. for (i = 0; i < MAX_RESOLVE_DEPTH && btf_type_is_array(var_type); i++) {
  3124. array = btf_array(var_type);
  3125. nelems *= array->nelems;
  3126. var_type = btf_type_by_id(btf, array->type);
  3127. }
  3128. if (i == MAX_RESOLVE_DEPTH)
  3129. return -E2BIG;
  3130. if (nelems == 0)
  3131. return 0;
  3132. field_type = btf_get_field_type(btf, var_type,
  3133. field_mask, seen_mask, &align, &sz);
  3134. /* Look into variables of struct types */
  3135. if (!field_type && __btf_type_is_struct(var_type)) {
  3136. sz = var_type->size;
  3137. if (expected_size && expected_size != sz * nelems)
  3138. return 0;
  3139. ret = btf_find_nested_struct(btf, var_type, off, nelems, field_mask,
  3140. &info[0], info_cnt, level);
  3141. return ret;
  3142. }
  3143. if (field_type == 0)
  3144. return 0;
  3145. if (field_type < 0)
  3146. return field_type;
  3147. if (expected_size && expected_size != sz * nelems)
  3148. return 0;
  3149. if (off % align)
  3150. return 0;
  3151. switch (field_type) {
  3152. case BPF_SPIN_LOCK:
  3153. case BPF_TIMER:
  3154. case BPF_WORKQUEUE:
  3155. case BPF_LIST_NODE:
  3156. case BPF_RB_NODE:
  3157. case BPF_REFCOUNT:
  3158. ret = btf_find_struct(btf, var_type, off, sz, field_type,
  3159. info_cnt ? &info[0] : &tmp);
  3160. if (ret < 0)
  3161. return ret;
  3162. break;
  3163. case BPF_KPTR_UNREF:
  3164. case BPF_KPTR_REF:
  3165. case BPF_KPTR_PERCPU:
  3166. ret = btf_find_kptr(btf, var_type, off, sz,
  3167. info_cnt ? &info[0] : &tmp);
  3168. if (ret < 0)
  3169. return ret;
  3170. break;
  3171. case BPF_LIST_HEAD:
  3172. case BPF_RB_ROOT:
  3173. ret = btf_find_graph_root(btf, var, var_type,
  3174. var_idx, off, sz,
  3175. info_cnt ? &info[0] : &tmp,
  3176. field_type);
  3177. if (ret < 0)
  3178. return ret;
  3179. break;
  3180. default:
  3181. return -EFAULT;
  3182. }
  3183. if (ret == BTF_FIELD_IGNORE)
  3184. return 0;
  3185. if (!info_cnt)
  3186. return -E2BIG;
  3187. if (nelems > 1) {
  3188. ret = btf_repeat_fields(info, info_cnt, 1, nelems - 1, sz);
  3189. if (ret < 0)
  3190. return ret;
  3191. }
  3192. return nelems;
  3193. }
  3194. static int btf_find_struct_field(const struct btf *btf,
  3195. const struct btf_type *t, u32 field_mask,
  3196. struct btf_field_info *info, int info_cnt,
  3197. u32 level)
  3198. {
  3199. int ret, idx = 0;
  3200. const struct btf_member *member;
  3201. u32 i, off, seen_mask = 0;
  3202. for_each_member(i, t, member) {
  3203. const struct btf_type *member_type = btf_type_by_id(btf,
  3204. member->type);
  3205. off = __btf_member_bit_offset(t, member);
  3206. if (off % 8)
  3207. /* valid C code cannot generate such BTF */
  3208. return -EINVAL;
  3209. off /= 8;
  3210. ret = btf_find_field_one(btf, t, member_type, i,
  3211. off, 0,
  3212. field_mask, &seen_mask,
  3213. &info[idx], info_cnt - idx, level);
  3214. if (ret < 0)
  3215. return ret;
  3216. idx += ret;
  3217. }
  3218. return idx;
  3219. }
  3220. static int btf_find_datasec_var(const struct btf *btf, const struct btf_type *t,
  3221. u32 field_mask, struct btf_field_info *info,
  3222. int info_cnt, u32 level)
  3223. {
  3224. int ret, idx = 0;
  3225. const struct btf_var_secinfo *vsi;
  3226. u32 i, off, seen_mask = 0;
  3227. for_each_vsi(i, t, vsi) {
  3228. const struct btf_type *var = btf_type_by_id(btf, vsi->type);
  3229. const struct btf_type *var_type = btf_type_by_id(btf, var->type);
  3230. off = vsi->offset;
  3231. ret = btf_find_field_one(btf, var, var_type, -1, off, vsi->size,
  3232. field_mask, &seen_mask,
  3233. &info[idx], info_cnt - idx,
  3234. level);
  3235. if (ret < 0)
  3236. return ret;
  3237. idx += ret;
  3238. }
  3239. return idx;
  3240. }
  3241. static int btf_find_field(const struct btf *btf, const struct btf_type *t,
  3242. u32 field_mask, struct btf_field_info *info,
  3243. int info_cnt)
  3244. {
  3245. if (__btf_type_is_struct(t))
  3246. return btf_find_struct_field(btf, t, field_mask, info, info_cnt, 0);
  3247. else if (btf_type_is_datasec(t))
  3248. return btf_find_datasec_var(btf, t, field_mask, info, info_cnt, 0);
  3249. return -EINVAL;
  3250. }
  3251. /* Callers have to ensure the life cycle of btf if it is program BTF */
  3252. static int btf_parse_kptr(const struct btf *btf, struct btf_field *field,
  3253. struct btf_field_info *info)
  3254. {
  3255. struct module *mod = NULL;
  3256. const struct btf_type *t;
  3257. /* If a matching btf type is found in kernel or module BTFs, kptr_ref
  3258. * is that BTF, otherwise it's program BTF
  3259. */
  3260. struct btf *kptr_btf;
  3261. int ret;
  3262. s32 id;
  3263. /* Find type in map BTF, and use it to look up the matching type
  3264. * in vmlinux or module BTFs, by name and kind.
  3265. */
  3266. t = btf_type_by_id(btf, info->kptr.type_id);
  3267. id = bpf_find_btf_id(__btf_name_by_offset(btf, t->name_off), BTF_INFO_KIND(t->info),
  3268. &kptr_btf);
  3269. if (id == -ENOENT) {
  3270. /* btf_parse_kptr should only be called w/ btf = program BTF */
  3271. WARN_ON_ONCE(btf_is_kernel(btf));
  3272. /* Type exists only in program BTF. Assume that it's a MEM_ALLOC
  3273. * kptr allocated via bpf_obj_new
  3274. */
  3275. field->kptr.dtor = NULL;
  3276. id = info->kptr.type_id;
  3277. kptr_btf = (struct btf *)btf;
  3278. goto found_dtor;
  3279. }
  3280. if (id < 0)
  3281. return id;
  3282. /* Find and stash the function pointer for the destruction function that
  3283. * needs to be eventually invoked from the map free path.
  3284. */
  3285. if (info->type == BPF_KPTR_REF) {
  3286. const struct btf_type *dtor_func;
  3287. const char *dtor_func_name;
  3288. unsigned long addr;
  3289. s32 dtor_btf_id;
  3290. /* This call also serves as a whitelist of allowed objects that
  3291. * can be used as a referenced pointer and be stored in a map at
  3292. * the same time.
  3293. */
  3294. dtor_btf_id = btf_find_dtor_kfunc(kptr_btf, id);
  3295. if (dtor_btf_id < 0) {
  3296. ret = dtor_btf_id;
  3297. goto end_btf;
  3298. }
  3299. dtor_func = btf_type_by_id(kptr_btf, dtor_btf_id);
  3300. if (!dtor_func) {
  3301. ret = -ENOENT;
  3302. goto end_btf;
  3303. }
  3304. if (btf_is_module(kptr_btf)) {
  3305. mod = btf_try_get_module(kptr_btf);
  3306. if (!mod) {
  3307. ret = -ENXIO;
  3308. goto end_btf;
  3309. }
  3310. }
  3311. /* We already verified dtor_func to be btf_type_is_func
  3312. * in register_btf_id_dtor_kfuncs.
  3313. */
  3314. dtor_func_name = __btf_name_by_offset(kptr_btf, dtor_func->name_off);
  3315. addr = kallsyms_lookup_name(dtor_func_name);
  3316. if (!addr) {
  3317. ret = -EINVAL;
  3318. goto end_mod;
  3319. }
  3320. field->kptr.dtor = (void *)addr;
  3321. }
  3322. found_dtor:
  3323. field->kptr.btf_id = id;
  3324. field->kptr.btf = kptr_btf;
  3325. field->kptr.module = mod;
  3326. return 0;
  3327. end_mod:
  3328. module_put(mod);
  3329. end_btf:
  3330. btf_put(kptr_btf);
  3331. return ret;
  3332. }
  3333. static int btf_parse_graph_root(const struct btf *btf,
  3334. struct btf_field *field,
  3335. struct btf_field_info *info,
  3336. const char *node_type_name,
  3337. size_t node_type_align)
  3338. {
  3339. const struct btf_type *t, *n = NULL;
  3340. const struct btf_member *member;
  3341. u32 offset;
  3342. int i;
  3343. t = btf_type_by_id(btf, info->graph_root.value_btf_id);
  3344. /* We've already checked that value_btf_id is a struct type. We
  3345. * just need to figure out the offset of the list_node, and
  3346. * verify its type.
  3347. */
  3348. for_each_member(i, t, member) {
  3349. if (strcmp(info->graph_root.node_name,
  3350. __btf_name_by_offset(btf, member->name_off)))
  3351. continue;
  3352. /* Invalid BTF, two members with same name */
  3353. if (n)
  3354. return -EINVAL;
  3355. n = btf_type_by_id(btf, member->type);
  3356. if (!__btf_type_is_struct(n))
  3357. return -EINVAL;
  3358. if (strcmp(node_type_name, __btf_name_by_offset(btf, n->name_off)))
  3359. return -EINVAL;
  3360. offset = __btf_member_bit_offset(n, member);
  3361. if (offset % 8)
  3362. return -EINVAL;
  3363. offset /= 8;
  3364. if (offset % node_type_align)
  3365. return -EINVAL;
  3366. field->graph_root.btf = (struct btf *)btf;
  3367. field->graph_root.value_btf_id = info->graph_root.value_btf_id;
  3368. field->graph_root.node_offset = offset;
  3369. }
  3370. if (!n)
  3371. return -ENOENT;
  3372. return 0;
  3373. }
  3374. static int btf_parse_list_head(const struct btf *btf, struct btf_field *field,
  3375. struct btf_field_info *info)
  3376. {
  3377. return btf_parse_graph_root(btf, field, info, "bpf_list_node",
  3378. __alignof__(struct bpf_list_node));
  3379. }
  3380. static int btf_parse_rb_root(const struct btf *btf, struct btf_field *field,
  3381. struct btf_field_info *info)
  3382. {
  3383. return btf_parse_graph_root(btf, field, info, "bpf_rb_node",
  3384. __alignof__(struct bpf_rb_node));
  3385. }
  3386. static int btf_field_cmp(const void *_a, const void *_b, const void *priv)
  3387. {
  3388. const struct btf_field *a = (const struct btf_field *)_a;
  3389. const struct btf_field *b = (const struct btf_field *)_b;
  3390. if (a->offset < b->offset)
  3391. return -1;
  3392. else if (a->offset > b->offset)
  3393. return 1;
  3394. return 0;
  3395. }
  3396. struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
  3397. u32 field_mask, u32 value_size)
  3398. {
  3399. struct btf_field_info info_arr[BTF_FIELDS_MAX];
  3400. u32 next_off = 0, field_type_size;
  3401. struct btf_record *rec;
  3402. int ret, i, cnt;
  3403. ret = btf_find_field(btf, t, field_mask, info_arr, ARRAY_SIZE(info_arr));
  3404. if (ret < 0)
  3405. return ERR_PTR(ret);
  3406. if (!ret)
  3407. return NULL;
  3408. cnt = ret;
  3409. /* This needs to be kzalloc to zero out padding and unused fields, see
  3410. * comment in btf_record_equal.
  3411. */
  3412. rec = kzalloc(offsetof(struct btf_record, fields[cnt]), GFP_KERNEL | __GFP_NOWARN);
  3413. if (!rec)
  3414. return ERR_PTR(-ENOMEM);
  3415. rec->spin_lock_off = -EINVAL;
  3416. rec->timer_off = -EINVAL;
  3417. rec->wq_off = -EINVAL;
  3418. rec->refcount_off = -EINVAL;
  3419. for (i = 0; i < cnt; i++) {
  3420. field_type_size = btf_field_type_size(info_arr[i].type);
  3421. if (info_arr[i].off + field_type_size > value_size) {
  3422. WARN_ONCE(1, "verifier bug off %d size %d", info_arr[i].off, value_size);
  3423. ret = -EFAULT;
  3424. goto end;
  3425. }
  3426. if (info_arr[i].off < next_off) {
  3427. ret = -EEXIST;
  3428. goto end;
  3429. }
  3430. next_off = info_arr[i].off + field_type_size;
  3431. rec->field_mask |= info_arr[i].type;
  3432. rec->fields[i].offset = info_arr[i].off;
  3433. rec->fields[i].type = info_arr[i].type;
  3434. rec->fields[i].size = field_type_size;
  3435. switch (info_arr[i].type) {
  3436. case BPF_SPIN_LOCK:
  3437. WARN_ON_ONCE(rec->spin_lock_off >= 0);
  3438. /* Cache offset for faster lookup at runtime */
  3439. rec->spin_lock_off = rec->fields[i].offset;
  3440. break;
  3441. case BPF_TIMER:
  3442. WARN_ON_ONCE(rec->timer_off >= 0);
  3443. /* Cache offset for faster lookup at runtime */
  3444. rec->timer_off = rec->fields[i].offset;
  3445. break;
  3446. case BPF_WORKQUEUE:
  3447. WARN_ON_ONCE(rec->wq_off >= 0);
  3448. /* Cache offset for faster lookup at runtime */
  3449. rec->wq_off = rec->fields[i].offset;
  3450. break;
  3451. case BPF_REFCOUNT:
  3452. WARN_ON_ONCE(rec->refcount_off >= 0);
  3453. /* Cache offset for faster lookup at runtime */
  3454. rec->refcount_off = rec->fields[i].offset;
  3455. break;
  3456. case BPF_KPTR_UNREF:
  3457. case BPF_KPTR_REF:
  3458. case BPF_KPTR_PERCPU:
  3459. ret = btf_parse_kptr(btf, &rec->fields[i], &info_arr[i]);
  3460. if (ret < 0)
  3461. goto end;
  3462. break;
  3463. case BPF_LIST_HEAD:
  3464. ret = btf_parse_list_head(btf, &rec->fields[i], &info_arr[i]);
  3465. if (ret < 0)
  3466. goto end;
  3467. break;
  3468. case BPF_RB_ROOT:
  3469. ret = btf_parse_rb_root(btf, &rec->fields[i], &info_arr[i]);
  3470. if (ret < 0)
  3471. goto end;
  3472. break;
  3473. case BPF_LIST_NODE:
  3474. case BPF_RB_NODE:
  3475. break;
  3476. default:
  3477. ret = -EFAULT;
  3478. goto end;
  3479. }
  3480. rec->cnt++;
  3481. }
  3482. /* bpf_{list_head, rb_node} require bpf_spin_lock */
  3483. if ((btf_record_has_field(rec, BPF_LIST_HEAD) ||
  3484. btf_record_has_field(rec, BPF_RB_ROOT)) && rec->spin_lock_off < 0) {
  3485. ret = -EINVAL;
  3486. goto end;
  3487. }
  3488. if (rec->refcount_off < 0 &&
  3489. btf_record_has_field(rec, BPF_LIST_NODE) &&
  3490. btf_record_has_field(rec, BPF_RB_NODE)) {
  3491. ret = -EINVAL;
  3492. goto end;
  3493. }
  3494. sort_r(rec->fields, rec->cnt, sizeof(struct btf_field), btf_field_cmp,
  3495. NULL, rec);
  3496. return rec;
  3497. end:
  3498. btf_record_free(rec);
  3499. return ERR_PTR(ret);
  3500. }
  3501. int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec)
  3502. {
  3503. int i;
  3504. /* There are three types that signify ownership of some other type:
  3505. * kptr_ref, bpf_list_head, bpf_rb_root.
  3506. * kptr_ref only supports storing kernel types, which can't store
  3507. * references to program allocated local types.
  3508. *
  3509. * Hence we only need to ensure that bpf_{list_head,rb_root} ownership
  3510. * does not form cycles.
  3511. */
  3512. if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & BPF_GRAPH_ROOT))
  3513. return 0;
  3514. for (i = 0; i < rec->cnt; i++) {
  3515. struct btf_struct_meta *meta;
  3516. u32 btf_id;
  3517. if (!(rec->fields[i].type & BPF_GRAPH_ROOT))
  3518. continue;
  3519. btf_id = rec->fields[i].graph_root.value_btf_id;
  3520. meta = btf_find_struct_meta(btf, btf_id);
  3521. if (!meta)
  3522. return -EFAULT;
  3523. rec->fields[i].graph_root.value_rec = meta->record;
  3524. /* We need to set value_rec for all root types, but no need
  3525. * to check ownership cycle for a type unless it's also a
  3526. * node type.
  3527. */
  3528. if (!(rec->field_mask & BPF_GRAPH_NODE))
  3529. continue;
  3530. /* We need to ensure ownership acyclicity among all types. The
  3531. * proper way to do it would be to topologically sort all BTF
  3532. * IDs based on the ownership edges, since there can be multiple
  3533. * bpf_{list_head,rb_node} in a type. Instead, we use the
  3534. * following resaoning:
  3535. *
  3536. * - A type can only be owned by another type in user BTF if it
  3537. * has a bpf_{list,rb}_node. Let's call these node types.
  3538. * - A type can only _own_ another type in user BTF if it has a
  3539. * bpf_{list_head,rb_root}. Let's call these root types.
  3540. *
  3541. * We ensure that if a type is both a root and node, its
  3542. * element types cannot be root types.
  3543. *
  3544. * To ensure acyclicity:
  3545. *
  3546. * When A is an root type but not a node, its ownership
  3547. * chain can be:
  3548. * A -> B -> C
  3549. * Where:
  3550. * - A is an root, e.g. has bpf_rb_root.
  3551. * - B is both a root and node, e.g. has bpf_rb_node and
  3552. * bpf_list_head.
  3553. * - C is only an root, e.g. has bpf_list_node
  3554. *
  3555. * When A is both a root and node, some other type already
  3556. * owns it in the BTF domain, hence it can not own
  3557. * another root type through any of the ownership edges.
  3558. * A -> B
  3559. * Where:
  3560. * - A is both an root and node.
  3561. * - B is only an node.
  3562. */
  3563. if (meta->record->field_mask & BPF_GRAPH_ROOT)
  3564. return -ELOOP;
  3565. }
  3566. return 0;
  3567. }
  3568. static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
  3569. u32 type_id, void *data, u8 bits_offset,
  3570. struct btf_show *show)
  3571. {
  3572. const struct btf_member *member;
  3573. void *safe_data;
  3574. u32 i;
  3575. safe_data = btf_show_start_struct_type(show, t, type_id, data);
  3576. if (!safe_data)
  3577. return;
  3578. for_each_member(i, t, member) {
  3579. const struct btf_type *member_type = btf_type_by_id(btf,
  3580. member->type);
  3581. const struct btf_kind_operations *ops;
  3582. u32 member_offset, bitfield_size;
  3583. u32 bytes_offset;
  3584. u8 bits8_offset;
  3585. btf_show_start_member(show, member);
  3586. member_offset = __btf_member_bit_offset(t, member);
  3587. bitfield_size = __btf_member_bitfield_size(t, member);
  3588. bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
  3589. bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
  3590. if (bitfield_size) {
  3591. safe_data = btf_show_start_type(show, member_type,
  3592. member->type,
  3593. data + bytes_offset);
  3594. if (safe_data)
  3595. btf_bitfield_show(safe_data,
  3596. bits8_offset,
  3597. bitfield_size, show);
  3598. btf_show_end_type(show);
  3599. } else {
  3600. ops = btf_type_ops(member_type);
  3601. ops->show(btf, member_type, member->type,
  3602. data + bytes_offset, bits8_offset, show);
  3603. }
  3604. btf_show_end_member(show);
  3605. }
  3606. btf_show_end_struct_type(show);
  3607. }
  3608. static void btf_struct_show(const struct btf *btf, const struct btf_type *t,
  3609. u32 type_id, void *data, u8 bits_offset,
  3610. struct btf_show *show)
  3611. {
  3612. const struct btf_member *m = show->state.member;
  3613. /*
  3614. * First check if any members would be shown (are non-zero).
  3615. * See comments above "struct btf_show" definition for more
  3616. * details on how this works at a high-level.
  3617. */
  3618. if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
  3619. if (!show->state.depth_check) {
  3620. show->state.depth_check = show->state.depth + 1;
  3621. show->state.depth_to_show = 0;
  3622. }
  3623. __btf_struct_show(btf, t, type_id, data, bits_offset, show);
  3624. /* Restore saved member data here */
  3625. show->state.member = m;
  3626. if (show->state.depth_check != show->state.depth + 1)
  3627. return;
  3628. show->state.depth_check = 0;
  3629. if (show->state.depth_to_show <= show->state.depth)
  3630. return;
  3631. /*
  3632. * Reaching here indicates we have recursed and found
  3633. * non-zero child values.
  3634. */
  3635. }
  3636. __btf_struct_show(btf, t, type_id, data, bits_offset, show);
  3637. }
  3638. static struct btf_kind_operations struct_ops = {
  3639. .check_meta = btf_struct_check_meta,
  3640. .resolve = btf_struct_resolve,
  3641. .check_member = btf_struct_check_member,
  3642. .check_kflag_member = btf_generic_check_kflag_member,
  3643. .log_details = btf_struct_log,
  3644. .show = btf_struct_show,
  3645. };
  3646. static int btf_enum_check_member(struct btf_verifier_env *env,
  3647. const struct btf_type *struct_type,
  3648. const struct btf_member *member,
  3649. const struct btf_type *member_type)
  3650. {
  3651. u32 struct_bits_off = member->offset;
  3652. u32 struct_size, bytes_offset;
  3653. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  3654. btf_verifier_log_member(env, struct_type, member,
  3655. "Member is not byte aligned");
  3656. return -EINVAL;
  3657. }
  3658. struct_size = struct_type->size;
  3659. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  3660. if (struct_size - bytes_offset < member_type->size) {
  3661. btf_verifier_log_member(env, struct_type, member,
  3662. "Member exceeds struct_size");
  3663. return -EINVAL;
  3664. }
  3665. return 0;
  3666. }
  3667. static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
  3668. const struct btf_type *struct_type,
  3669. const struct btf_member *member,
  3670. const struct btf_type *member_type)
  3671. {
  3672. u32 struct_bits_off, nr_bits, bytes_end, struct_size;
  3673. u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
  3674. struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
  3675. nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
  3676. if (!nr_bits) {
  3677. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  3678. btf_verifier_log_member(env, struct_type, member,
  3679. "Member is not byte aligned");
  3680. return -EINVAL;
  3681. }
  3682. nr_bits = int_bitsize;
  3683. } else if (nr_bits > int_bitsize) {
  3684. btf_verifier_log_member(env, struct_type, member,
  3685. "Invalid member bitfield_size");
  3686. return -EINVAL;
  3687. }
  3688. struct_size = struct_type->size;
  3689. bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
  3690. if (struct_size < bytes_end) {
  3691. btf_verifier_log_member(env, struct_type, member,
  3692. "Member exceeds struct_size");
  3693. return -EINVAL;
  3694. }
  3695. return 0;
  3696. }
  3697. static s32 btf_enum_check_meta(struct btf_verifier_env *env,
  3698. const struct btf_type *t,
  3699. u32 meta_left)
  3700. {
  3701. const struct btf_enum *enums = btf_type_enum(t);
  3702. struct btf *btf = env->btf;
  3703. const char *fmt_str;
  3704. u16 i, nr_enums;
  3705. u32 meta_needed;
  3706. nr_enums = btf_type_vlen(t);
  3707. meta_needed = nr_enums * sizeof(*enums);
  3708. if (meta_left < meta_needed) {
  3709. btf_verifier_log_basic(env, t,
  3710. "meta_left:%u meta_needed:%u",
  3711. meta_left, meta_needed);
  3712. return -EINVAL;
  3713. }
  3714. if (t->size > 8 || !is_power_of_2(t->size)) {
  3715. btf_verifier_log_type(env, t, "Unexpected size");
  3716. return -EINVAL;
  3717. }
  3718. /* enum type either no name or a valid one */
  3719. if (t->name_off &&
  3720. !btf_name_valid_identifier(env->btf, t->name_off)) {
  3721. btf_verifier_log_type(env, t, "Invalid name");
  3722. return -EINVAL;
  3723. }
  3724. btf_verifier_log_type(env, t, NULL);
  3725. for (i = 0; i < nr_enums; i++) {
  3726. if (!btf_name_offset_valid(btf, enums[i].name_off)) {
  3727. btf_verifier_log(env, "\tInvalid name_offset:%u",
  3728. enums[i].name_off);
  3729. return -EINVAL;
  3730. }
  3731. /* enum member must have a valid name */
  3732. if (!enums[i].name_off ||
  3733. !btf_name_valid_identifier(btf, enums[i].name_off)) {
  3734. btf_verifier_log_type(env, t, "Invalid name");
  3735. return -EINVAL;
  3736. }
  3737. if (env->log.level == BPF_LOG_KERNEL)
  3738. continue;
  3739. fmt_str = btf_type_kflag(t) ? "\t%s val=%d\n" : "\t%s val=%u\n";
  3740. btf_verifier_log(env, fmt_str,
  3741. __btf_name_by_offset(btf, enums[i].name_off),
  3742. enums[i].val);
  3743. }
  3744. return meta_needed;
  3745. }
  3746. static void btf_enum_log(struct btf_verifier_env *env,
  3747. const struct btf_type *t)
  3748. {
  3749. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  3750. }
  3751. static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
  3752. u32 type_id, void *data, u8 bits_offset,
  3753. struct btf_show *show)
  3754. {
  3755. const struct btf_enum *enums = btf_type_enum(t);
  3756. u32 i, nr_enums = btf_type_vlen(t);
  3757. void *safe_data;
  3758. int v;
  3759. safe_data = btf_show_start_type(show, t, type_id, data);
  3760. if (!safe_data)
  3761. return;
  3762. v = *(int *)safe_data;
  3763. for (i = 0; i < nr_enums; i++) {
  3764. if (v != enums[i].val)
  3765. continue;
  3766. btf_show_type_value(show, "%s",
  3767. __btf_name_by_offset(btf,
  3768. enums[i].name_off));
  3769. btf_show_end_type(show);
  3770. return;
  3771. }
  3772. if (btf_type_kflag(t))
  3773. btf_show_type_value(show, "%d", v);
  3774. else
  3775. btf_show_type_value(show, "%u", v);
  3776. btf_show_end_type(show);
  3777. }
  3778. static struct btf_kind_operations enum_ops = {
  3779. .check_meta = btf_enum_check_meta,
  3780. .resolve = btf_df_resolve,
  3781. .check_member = btf_enum_check_member,
  3782. .check_kflag_member = btf_enum_check_kflag_member,
  3783. .log_details = btf_enum_log,
  3784. .show = btf_enum_show,
  3785. };
  3786. static s32 btf_enum64_check_meta(struct btf_verifier_env *env,
  3787. const struct btf_type *t,
  3788. u32 meta_left)
  3789. {
  3790. const struct btf_enum64 *enums = btf_type_enum64(t);
  3791. struct btf *btf = env->btf;
  3792. const char *fmt_str;
  3793. u16 i, nr_enums;
  3794. u32 meta_needed;
  3795. nr_enums = btf_type_vlen(t);
  3796. meta_needed = nr_enums * sizeof(*enums);
  3797. if (meta_left < meta_needed) {
  3798. btf_verifier_log_basic(env, t,
  3799. "meta_left:%u meta_needed:%u",
  3800. meta_left, meta_needed);
  3801. return -EINVAL;
  3802. }
  3803. if (t->size > 8 || !is_power_of_2(t->size)) {
  3804. btf_verifier_log_type(env, t, "Unexpected size");
  3805. return -EINVAL;
  3806. }
  3807. /* enum type either no name or a valid one */
  3808. if (t->name_off &&
  3809. !btf_name_valid_identifier(env->btf, t->name_off)) {
  3810. btf_verifier_log_type(env, t, "Invalid name");
  3811. return -EINVAL;
  3812. }
  3813. btf_verifier_log_type(env, t, NULL);
  3814. for (i = 0; i < nr_enums; i++) {
  3815. if (!btf_name_offset_valid(btf, enums[i].name_off)) {
  3816. btf_verifier_log(env, "\tInvalid name_offset:%u",
  3817. enums[i].name_off);
  3818. return -EINVAL;
  3819. }
  3820. /* enum member must have a valid name */
  3821. if (!enums[i].name_off ||
  3822. !btf_name_valid_identifier(btf, enums[i].name_off)) {
  3823. btf_verifier_log_type(env, t, "Invalid name");
  3824. return -EINVAL;
  3825. }
  3826. if (env->log.level == BPF_LOG_KERNEL)
  3827. continue;
  3828. fmt_str = btf_type_kflag(t) ? "\t%s val=%lld\n" : "\t%s val=%llu\n";
  3829. btf_verifier_log(env, fmt_str,
  3830. __btf_name_by_offset(btf, enums[i].name_off),
  3831. btf_enum64_value(enums + i));
  3832. }
  3833. return meta_needed;
  3834. }
  3835. static void btf_enum64_show(const struct btf *btf, const struct btf_type *t,
  3836. u32 type_id, void *data, u8 bits_offset,
  3837. struct btf_show *show)
  3838. {
  3839. const struct btf_enum64 *enums = btf_type_enum64(t);
  3840. u32 i, nr_enums = btf_type_vlen(t);
  3841. void *safe_data;
  3842. s64 v;
  3843. safe_data = btf_show_start_type(show, t, type_id, data);
  3844. if (!safe_data)
  3845. return;
  3846. v = *(u64 *)safe_data;
  3847. for (i = 0; i < nr_enums; i++) {
  3848. if (v != btf_enum64_value(enums + i))
  3849. continue;
  3850. btf_show_type_value(show, "%s",
  3851. __btf_name_by_offset(btf,
  3852. enums[i].name_off));
  3853. btf_show_end_type(show);
  3854. return;
  3855. }
  3856. if (btf_type_kflag(t))
  3857. btf_show_type_value(show, "%lld", v);
  3858. else
  3859. btf_show_type_value(show, "%llu", v);
  3860. btf_show_end_type(show);
  3861. }
  3862. static struct btf_kind_operations enum64_ops = {
  3863. .check_meta = btf_enum64_check_meta,
  3864. .resolve = btf_df_resolve,
  3865. .check_member = btf_enum_check_member,
  3866. .check_kflag_member = btf_enum_check_kflag_member,
  3867. .log_details = btf_enum_log,
  3868. .show = btf_enum64_show,
  3869. };
  3870. static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
  3871. const struct btf_type *t,
  3872. u32 meta_left)
  3873. {
  3874. u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
  3875. if (meta_left < meta_needed) {
  3876. btf_verifier_log_basic(env, t,
  3877. "meta_left:%u meta_needed:%u",
  3878. meta_left, meta_needed);
  3879. return -EINVAL;
  3880. }
  3881. if (t->name_off) {
  3882. btf_verifier_log_type(env, t, "Invalid name");
  3883. return -EINVAL;
  3884. }
  3885. if (btf_type_kflag(t)) {
  3886. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  3887. return -EINVAL;
  3888. }
  3889. btf_verifier_log_type(env, t, NULL);
  3890. return meta_needed;
  3891. }
  3892. static void btf_func_proto_log(struct btf_verifier_env *env,
  3893. const struct btf_type *t)
  3894. {
  3895. const struct btf_param *args = (const struct btf_param *)(t + 1);
  3896. u16 nr_args = btf_type_vlen(t), i;
  3897. btf_verifier_log(env, "return=%u args=(", t->type);
  3898. if (!nr_args) {
  3899. btf_verifier_log(env, "void");
  3900. goto done;
  3901. }
  3902. if (nr_args == 1 && !args[0].type) {
  3903. /* Only one vararg */
  3904. btf_verifier_log(env, "vararg");
  3905. goto done;
  3906. }
  3907. btf_verifier_log(env, "%u %s", args[0].type,
  3908. __btf_name_by_offset(env->btf,
  3909. args[0].name_off));
  3910. for (i = 1; i < nr_args - 1; i++)
  3911. btf_verifier_log(env, ", %u %s", args[i].type,
  3912. __btf_name_by_offset(env->btf,
  3913. args[i].name_off));
  3914. if (nr_args > 1) {
  3915. const struct btf_param *last_arg = &args[nr_args - 1];
  3916. if (last_arg->type)
  3917. btf_verifier_log(env, ", %u %s", last_arg->type,
  3918. __btf_name_by_offset(env->btf,
  3919. last_arg->name_off));
  3920. else
  3921. btf_verifier_log(env, ", vararg");
  3922. }
  3923. done:
  3924. btf_verifier_log(env, ")");
  3925. }
  3926. static struct btf_kind_operations func_proto_ops = {
  3927. .check_meta = btf_func_proto_check_meta,
  3928. .resolve = btf_df_resolve,
  3929. /*
  3930. * BTF_KIND_FUNC_PROTO cannot be directly referred by
  3931. * a struct's member.
  3932. *
  3933. * It should be a function pointer instead.
  3934. * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
  3935. *
  3936. * Hence, there is no btf_func_check_member().
  3937. */
  3938. .check_member = btf_df_check_member,
  3939. .check_kflag_member = btf_df_check_kflag_member,
  3940. .log_details = btf_func_proto_log,
  3941. .show = btf_df_show,
  3942. };
  3943. static s32 btf_func_check_meta(struct btf_verifier_env *env,
  3944. const struct btf_type *t,
  3945. u32 meta_left)
  3946. {
  3947. if (!t->name_off ||
  3948. !btf_name_valid_identifier(env->btf, t->name_off)) {
  3949. btf_verifier_log_type(env, t, "Invalid name");
  3950. return -EINVAL;
  3951. }
  3952. if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
  3953. btf_verifier_log_type(env, t, "Invalid func linkage");
  3954. return -EINVAL;
  3955. }
  3956. if (btf_type_kflag(t)) {
  3957. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  3958. return -EINVAL;
  3959. }
  3960. btf_verifier_log_type(env, t, NULL);
  3961. return 0;
  3962. }
  3963. static int btf_func_resolve(struct btf_verifier_env *env,
  3964. const struct resolve_vertex *v)
  3965. {
  3966. const struct btf_type *t = v->t;
  3967. u32 next_type_id = t->type;
  3968. int err;
  3969. err = btf_func_check(env, t);
  3970. if (err)
  3971. return err;
  3972. env_stack_pop_resolved(env, next_type_id, 0);
  3973. return 0;
  3974. }
  3975. static struct btf_kind_operations func_ops = {
  3976. .check_meta = btf_func_check_meta,
  3977. .resolve = btf_func_resolve,
  3978. .check_member = btf_df_check_member,
  3979. .check_kflag_member = btf_df_check_kflag_member,
  3980. .log_details = btf_ref_type_log,
  3981. .show = btf_df_show,
  3982. };
  3983. static s32 btf_var_check_meta(struct btf_verifier_env *env,
  3984. const struct btf_type *t,
  3985. u32 meta_left)
  3986. {
  3987. const struct btf_var *var;
  3988. u32 meta_needed = sizeof(*var);
  3989. if (meta_left < meta_needed) {
  3990. btf_verifier_log_basic(env, t,
  3991. "meta_left:%u meta_needed:%u",
  3992. meta_left, meta_needed);
  3993. return -EINVAL;
  3994. }
  3995. if (btf_type_vlen(t)) {
  3996. btf_verifier_log_type(env, t, "vlen != 0");
  3997. return -EINVAL;
  3998. }
  3999. if (btf_type_kflag(t)) {
  4000. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  4001. return -EINVAL;
  4002. }
  4003. if (!t->name_off ||
  4004. !btf_name_valid_identifier(env->btf, t->name_off)) {
  4005. btf_verifier_log_type(env, t, "Invalid name");
  4006. return -EINVAL;
  4007. }
  4008. /* A var cannot be in type void */
  4009. if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
  4010. btf_verifier_log_type(env, t, "Invalid type_id");
  4011. return -EINVAL;
  4012. }
  4013. var = btf_type_var(t);
  4014. if (var->linkage != BTF_VAR_STATIC &&
  4015. var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
  4016. btf_verifier_log_type(env, t, "Linkage not supported");
  4017. return -EINVAL;
  4018. }
  4019. btf_verifier_log_type(env, t, NULL);
  4020. return meta_needed;
  4021. }
  4022. static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
  4023. {
  4024. const struct btf_var *var = btf_type_var(t);
  4025. btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
  4026. }
  4027. static const struct btf_kind_operations var_ops = {
  4028. .check_meta = btf_var_check_meta,
  4029. .resolve = btf_var_resolve,
  4030. .check_member = btf_df_check_member,
  4031. .check_kflag_member = btf_df_check_kflag_member,
  4032. .log_details = btf_var_log,
  4033. .show = btf_var_show,
  4034. };
  4035. static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
  4036. const struct btf_type *t,
  4037. u32 meta_left)
  4038. {
  4039. const struct btf_var_secinfo *vsi;
  4040. u64 last_vsi_end_off = 0, sum = 0;
  4041. u32 i, meta_needed;
  4042. meta_needed = btf_type_vlen(t) * sizeof(*vsi);
  4043. if (meta_left < meta_needed) {
  4044. btf_verifier_log_basic(env, t,
  4045. "meta_left:%u meta_needed:%u",
  4046. meta_left, meta_needed);
  4047. return -EINVAL;
  4048. }
  4049. if (!t->size) {
  4050. btf_verifier_log_type(env, t, "size == 0");
  4051. return -EINVAL;
  4052. }
  4053. if (btf_type_kflag(t)) {
  4054. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  4055. return -EINVAL;
  4056. }
  4057. if (!t->name_off ||
  4058. !btf_name_valid_section(env->btf, t->name_off)) {
  4059. btf_verifier_log_type(env, t, "Invalid name");
  4060. return -EINVAL;
  4061. }
  4062. btf_verifier_log_type(env, t, NULL);
  4063. for_each_vsi(i, t, vsi) {
  4064. /* A var cannot be in type void */
  4065. if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
  4066. btf_verifier_log_vsi(env, t, vsi,
  4067. "Invalid type_id");
  4068. return -EINVAL;
  4069. }
  4070. if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
  4071. btf_verifier_log_vsi(env, t, vsi,
  4072. "Invalid offset");
  4073. return -EINVAL;
  4074. }
  4075. if (!vsi->size || vsi->size > t->size) {
  4076. btf_verifier_log_vsi(env, t, vsi,
  4077. "Invalid size");
  4078. return -EINVAL;
  4079. }
  4080. last_vsi_end_off = vsi->offset + vsi->size;
  4081. if (last_vsi_end_off > t->size) {
  4082. btf_verifier_log_vsi(env, t, vsi,
  4083. "Invalid offset+size");
  4084. return -EINVAL;
  4085. }
  4086. btf_verifier_log_vsi(env, t, vsi, NULL);
  4087. sum += vsi->size;
  4088. }
  4089. if (t->size < sum) {
  4090. btf_verifier_log_type(env, t, "Invalid btf_info size");
  4091. return -EINVAL;
  4092. }
  4093. return meta_needed;
  4094. }
  4095. static int btf_datasec_resolve(struct btf_verifier_env *env,
  4096. const struct resolve_vertex *v)
  4097. {
  4098. const struct btf_var_secinfo *vsi;
  4099. struct btf *btf = env->btf;
  4100. u16 i;
  4101. env->resolve_mode = RESOLVE_TBD;
  4102. for_each_vsi_from(i, v->next_member, v->t, vsi) {
  4103. u32 var_type_id = vsi->type, type_id, type_size = 0;
  4104. const struct btf_type *var_type = btf_type_by_id(env->btf,
  4105. var_type_id);
  4106. if (!var_type || !btf_type_is_var(var_type)) {
  4107. btf_verifier_log_vsi(env, v->t, vsi,
  4108. "Not a VAR kind member");
  4109. return -EINVAL;
  4110. }
  4111. if (!env_type_is_resolve_sink(env, var_type) &&
  4112. !env_type_is_resolved(env, var_type_id)) {
  4113. env_stack_set_next_member(env, i + 1);
  4114. return env_stack_push(env, var_type, var_type_id);
  4115. }
  4116. type_id = var_type->type;
  4117. if (!btf_type_id_size(btf, &type_id, &type_size)) {
  4118. btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
  4119. return -EINVAL;
  4120. }
  4121. if (vsi->size < type_size) {
  4122. btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
  4123. return -EINVAL;
  4124. }
  4125. }
  4126. env_stack_pop_resolved(env, 0, 0);
  4127. return 0;
  4128. }
  4129. static void btf_datasec_log(struct btf_verifier_env *env,
  4130. const struct btf_type *t)
  4131. {
  4132. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  4133. }
  4134. static void btf_datasec_show(const struct btf *btf,
  4135. const struct btf_type *t, u32 type_id,
  4136. void *data, u8 bits_offset,
  4137. struct btf_show *show)
  4138. {
  4139. const struct btf_var_secinfo *vsi;
  4140. const struct btf_type *var;
  4141. u32 i;
  4142. if (!btf_show_start_type(show, t, type_id, data))
  4143. return;
  4144. btf_show_type_value(show, "section (\"%s\") = {",
  4145. __btf_name_by_offset(btf, t->name_off));
  4146. for_each_vsi(i, t, vsi) {
  4147. var = btf_type_by_id(btf, vsi->type);
  4148. if (i)
  4149. btf_show(show, ",");
  4150. btf_type_ops(var)->show(btf, var, vsi->type,
  4151. data + vsi->offset, bits_offset, show);
  4152. }
  4153. btf_show_end_type(show);
  4154. }
  4155. static const struct btf_kind_operations datasec_ops = {
  4156. .check_meta = btf_datasec_check_meta,
  4157. .resolve = btf_datasec_resolve,
  4158. .check_member = btf_df_check_member,
  4159. .check_kflag_member = btf_df_check_kflag_member,
  4160. .log_details = btf_datasec_log,
  4161. .show = btf_datasec_show,
  4162. };
  4163. static s32 btf_float_check_meta(struct btf_verifier_env *env,
  4164. const struct btf_type *t,
  4165. u32 meta_left)
  4166. {
  4167. if (btf_type_vlen(t)) {
  4168. btf_verifier_log_type(env, t, "vlen != 0");
  4169. return -EINVAL;
  4170. }
  4171. if (btf_type_kflag(t)) {
  4172. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  4173. return -EINVAL;
  4174. }
  4175. if (t->size != 2 && t->size != 4 && t->size != 8 && t->size != 12 &&
  4176. t->size != 16) {
  4177. btf_verifier_log_type(env, t, "Invalid type_size");
  4178. return -EINVAL;
  4179. }
  4180. btf_verifier_log_type(env, t, NULL);
  4181. return 0;
  4182. }
  4183. static int btf_float_check_member(struct btf_verifier_env *env,
  4184. const struct btf_type *struct_type,
  4185. const struct btf_member *member,
  4186. const struct btf_type *member_type)
  4187. {
  4188. u64 start_offset_bytes;
  4189. u64 end_offset_bytes;
  4190. u64 misalign_bits;
  4191. u64 align_bytes;
  4192. u64 align_bits;
  4193. /* Different architectures have different alignment requirements, so
  4194. * here we check only for the reasonable minimum. This way we ensure
  4195. * that types after CO-RE can pass the kernel BTF verifier.
  4196. */
  4197. align_bytes = min_t(u64, sizeof(void *), member_type->size);
  4198. align_bits = align_bytes * BITS_PER_BYTE;
  4199. div64_u64_rem(member->offset, align_bits, &misalign_bits);
  4200. if (misalign_bits) {
  4201. btf_verifier_log_member(env, struct_type, member,
  4202. "Member is not properly aligned");
  4203. return -EINVAL;
  4204. }
  4205. start_offset_bytes = member->offset / BITS_PER_BYTE;
  4206. end_offset_bytes = start_offset_bytes + member_type->size;
  4207. if (end_offset_bytes > struct_type->size) {
  4208. btf_verifier_log_member(env, struct_type, member,
  4209. "Member exceeds struct_size");
  4210. return -EINVAL;
  4211. }
  4212. return 0;
  4213. }
  4214. static void btf_float_log(struct btf_verifier_env *env,
  4215. const struct btf_type *t)
  4216. {
  4217. btf_verifier_log(env, "size=%u", t->size);
  4218. }
  4219. static const struct btf_kind_operations float_ops = {
  4220. .check_meta = btf_float_check_meta,
  4221. .resolve = btf_df_resolve,
  4222. .check_member = btf_float_check_member,
  4223. .check_kflag_member = btf_generic_check_kflag_member,
  4224. .log_details = btf_float_log,
  4225. .show = btf_df_show,
  4226. };
  4227. static s32 btf_decl_tag_check_meta(struct btf_verifier_env *env,
  4228. const struct btf_type *t,
  4229. u32 meta_left)
  4230. {
  4231. const struct btf_decl_tag *tag;
  4232. u32 meta_needed = sizeof(*tag);
  4233. s32 component_idx;
  4234. const char *value;
  4235. if (meta_left < meta_needed) {
  4236. btf_verifier_log_basic(env, t,
  4237. "meta_left:%u meta_needed:%u",
  4238. meta_left, meta_needed);
  4239. return -EINVAL;
  4240. }
  4241. value = btf_name_by_offset(env->btf, t->name_off);
  4242. if (!value || !value[0]) {
  4243. btf_verifier_log_type(env, t, "Invalid value");
  4244. return -EINVAL;
  4245. }
  4246. if (btf_type_vlen(t)) {
  4247. btf_verifier_log_type(env, t, "vlen != 0");
  4248. return -EINVAL;
  4249. }
  4250. if (btf_type_kflag(t)) {
  4251. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  4252. return -EINVAL;
  4253. }
  4254. component_idx = btf_type_decl_tag(t)->component_idx;
  4255. if (component_idx < -1) {
  4256. btf_verifier_log_type(env, t, "Invalid component_idx");
  4257. return -EINVAL;
  4258. }
  4259. btf_verifier_log_type(env, t, NULL);
  4260. return meta_needed;
  4261. }
  4262. static int btf_decl_tag_resolve(struct btf_verifier_env *env,
  4263. const struct resolve_vertex *v)
  4264. {
  4265. const struct btf_type *next_type;
  4266. const struct btf_type *t = v->t;
  4267. u32 next_type_id = t->type;
  4268. struct btf *btf = env->btf;
  4269. s32 component_idx;
  4270. u32 vlen;
  4271. next_type = btf_type_by_id(btf, next_type_id);
  4272. if (!next_type || !btf_type_is_decl_tag_target(next_type)) {
  4273. btf_verifier_log_type(env, v->t, "Invalid type_id");
  4274. return -EINVAL;
  4275. }
  4276. if (!env_type_is_resolve_sink(env, next_type) &&
  4277. !env_type_is_resolved(env, next_type_id))
  4278. return env_stack_push(env, next_type, next_type_id);
  4279. component_idx = btf_type_decl_tag(t)->component_idx;
  4280. if (component_idx != -1) {
  4281. if (btf_type_is_var(next_type) || btf_type_is_typedef(next_type)) {
  4282. btf_verifier_log_type(env, v->t, "Invalid component_idx");
  4283. return -EINVAL;
  4284. }
  4285. if (btf_type_is_struct(next_type)) {
  4286. vlen = btf_type_vlen(next_type);
  4287. } else {
  4288. /* next_type should be a function */
  4289. next_type = btf_type_by_id(btf, next_type->type);
  4290. vlen = btf_type_vlen(next_type);
  4291. }
  4292. if ((u32)component_idx >= vlen) {
  4293. btf_verifier_log_type(env, v->t, "Invalid component_idx");
  4294. return -EINVAL;
  4295. }
  4296. }
  4297. env_stack_pop_resolved(env, next_type_id, 0);
  4298. return 0;
  4299. }
  4300. static void btf_decl_tag_log(struct btf_verifier_env *env, const struct btf_type *t)
  4301. {
  4302. btf_verifier_log(env, "type=%u component_idx=%d", t->type,
  4303. btf_type_decl_tag(t)->component_idx);
  4304. }
  4305. static const struct btf_kind_operations decl_tag_ops = {
  4306. .check_meta = btf_decl_tag_check_meta,
  4307. .resolve = btf_decl_tag_resolve,
  4308. .check_member = btf_df_check_member,
  4309. .check_kflag_member = btf_df_check_kflag_member,
  4310. .log_details = btf_decl_tag_log,
  4311. .show = btf_df_show,
  4312. };
  4313. static int btf_func_proto_check(struct btf_verifier_env *env,
  4314. const struct btf_type *t)
  4315. {
  4316. const struct btf_type *ret_type;
  4317. const struct btf_param *args;
  4318. const struct btf *btf;
  4319. u16 nr_args, i;
  4320. int err;
  4321. btf = env->btf;
  4322. args = (const struct btf_param *)(t + 1);
  4323. nr_args = btf_type_vlen(t);
  4324. /* Check func return type which could be "void" (t->type == 0) */
  4325. if (t->type) {
  4326. u32 ret_type_id = t->type;
  4327. ret_type = btf_type_by_id(btf, ret_type_id);
  4328. if (!ret_type) {
  4329. btf_verifier_log_type(env, t, "Invalid return type");
  4330. return -EINVAL;
  4331. }
  4332. if (btf_type_is_resolve_source_only(ret_type)) {
  4333. btf_verifier_log_type(env, t, "Invalid return type");
  4334. return -EINVAL;
  4335. }
  4336. if (btf_type_needs_resolve(ret_type) &&
  4337. !env_type_is_resolved(env, ret_type_id)) {
  4338. err = btf_resolve(env, ret_type, ret_type_id);
  4339. if (err)
  4340. return err;
  4341. }
  4342. /* Ensure the return type is a type that has a size */
  4343. if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
  4344. btf_verifier_log_type(env, t, "Invalid return type");
  4345. return -EINVAL;
  4346. }
  4347. }
  4348. if (!nr_args)
  4349. return 0;
  4350. /* Last func arg type_id could be 0 if it is a vararg */
  4351. if (!args[nr_args - 1].type) {
  4352. if (args[nr_args - 1].name_off) {
  4353. btf_verifier_log_type(env, t, "Invalid arg#%u",
  4354. nr_args);
  4355. return -EINVAL;
  4356. }
  4357. nr_args--;
  4358. }
  4359. for (i = 0; i < nr_args; i++) {
  4360. const struct btf_type *arg_type;
  4361. u32 arg_type_id;
  4362. arg_type_id = args[i].type;
  4363. arg_type = btf_type_by_id(btf, arg_type_id);
  4364. if (!arg_type) {
  4365. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  4366. return -EINVAL;
  4367. }
  4368. if (btf_type_is_resolve_source_only(arg_type)) {
  4369. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  4370. return -EINVAL;
  4371. }
  4372. if (args[i].name_off &&
  4373. (!btf_name_offset_valid(btf, args[i].name_off) ||
  4374. !btf_name_valid_identifier(btf, args[i].name_off))) {
  4375. btf_verifier_log_type(env, t,
  4376. "Invalid arg#%u", i + 1);
  4377. return -EINVAL;
  4378. }
  4379. if (btf_type_needs_resolve(arg_type) &&
  4380. !env_type_is_resolved(env, arg_type_id)) {
  4381. err = btf_resolve(env, arg_type, arg_type_id);
  4382. if (err)
  4383. return err;
  4384. }
  4385. if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
  4386. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  4387. return -EINVAL;
  4388. }
  4389. }
  4390. return 0;
  4391. }
  4392. static int btf_func_check(struct btf_verifier_env *env,
  4393. const struct btf_type *t)
  4394. {
  4395. const struct btf_type *proto_type;
  4396. const struct btf_param *args;
  4397. const struct btf *btf;
  4398. u16 nr_args, i;
  4399. btf = env->btf;
  4400. proto_type = btf_type_by_id(btf, t->type);
  4401. if (!proto_type || !btf_type_is_func_proto(proto_type)) {
  4402. btf_verifier_log_type(env, t, "Invalid type_id");
  4403. return -EINVAL;
  4404. }
  4405. args = (const struct btf_param *)(proto_type + 1);
  4406. nr_args = btf_type_vlen(proto_type);
  4407. for (i = 0; i < nr_args; i++) {
  4408. if (!args[i].name_off && args[i].type) {
  4409. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  4410. return -EINVAL;
  4411. }
  4412. }
  4413. return 0;
  4414. }
  4415. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
  4416. [BTF_KIND_INT] = &int_ops,
  4417. [BTF_KIND_PTR] = &ptr_ops,
  4418. [BTF_KIND_ARRAY] = &array_ops,
  4419. [BTF_KIND_STRUCT] = &struct_ops,
  4420. [BTF_KIND_UNION] = &struct_ops,
  4421. [BTF_KIND_ENUM] = &enum_ops,
  4422. [BTF_KIND_FWD] = &fwd_ops,
  4423. [BTF_KIND_TYPEDEF] = &modifier_ops,
  4424. [BTF_KIND_VOLATILE] = &modifier_ops,
  4425. [BTF_KIND_CONST] = &modifier_ops,
  4426. [BTF_KIND_RESTRICT] = &modifier_ops,
  4427. [BTF_KIND_FUNC] = &func_ops,
  4428. [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
  4429. [BTF_KIND_VAR] = &var_ops,
  4430. [BTF_KIND_DATASEC] = &datasec_ops,
  4431. [BTF_KIND_FLOAT] = &float_ops,
  4432. [BTF_KIND_DECL_TAG] = &decl_tag_ops,
  4433. [BTF_KIND_TYPE_TAG] = &modifier_ops,
  4434. [BTF_KIND_ENUM64] = &enum64_ops,
  4435. };
  4436. static s32 btf_check_meta(struct btf_verifier_env *env,
  4437. const struct btf_type *t,
  4438. u32 meta_left)
  4439. {
  4440. u32 saved_meta_left = meta_left;
  4441. s32 var_meta_size;
  4442. if (meta_left < sizeof(*t)) {
  4443. btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
  4444. env->log_type_id, meta_left, sizeof(*t));
  4445. return -EINVAL;
  4446. }
  4447. meta_left -= sizeof(*t);
  4448. if (t->info & ~BTF_INFO_MASK) {
  4449. btf_verifier_log(env, "[%u] Invalid btf_info:%x",
  4450. env->log_type_id, t->info);
  4451. return -EINVAL;
  4452. }
  4453. if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
  4454. BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
  4455. btf_verifier_log(env, "[%u] Invalid kind:%u",
  4456. env->log_type_id, BTF_INFO_KIND(t->info));
  4457. return -EINVAL;
  4458. }
  4459. if (!btf_name_offset_valid(env->btf, t->name_off)) {
  4460. btf_verifier_log(env, "[%u] Invalid name_offset:%u",
  4461. env->log_type_id, t->name_off);
  4462. return -EINVAL;
  4463. }
  4464. var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
  4465. if (var_meta_size < 0)
  4466. return var_meta_size;
  4467. meta_left -= var_meta_size;
  4468. return saved_meta_left - meta_left;
  4469. }
  4470. static int btf_check_all_metas(struct btf_verifier_env *env)
  4471. {
  4472. struct btf *btf = env->btf;
  4473. struct btf_header *hdr;
  4474. void *cur, *end;
  4475. hdr = &btf->hdr;
  4476. cur = btf->nohdr_data + hdr->type_off;
  4477. end = cur + hdr->type_len;
  4478. env->log_type_id = btf->base_btf ? btf->start_id : 1;
  4479. while (cur < end) {
  4480. struct btf_type *t = cur;
  4481. s32 meta_size;
  4482. meta_size = btf_check_meta(env, t, end - cur);
  4483. if (meta_size < 0)
  4484. return meta_size;
  4485. btf_add_type(env, t);
  4486. cur += meta_size;
  4487. env->log_type_id++;
  4488. }
  4489. return 0;
  4490. }
  4491. static bool btf_resolve_valid(struct btf_verifier_env *env,
  4492. const struct btf_type *t,
  4493. u32 type_id)
  4494. {
  4495. struct btf *btf = env->btf;
  4496. if (!env_type_is_resolved(env, type_id))
  4497. return false;
  4498. if (btf_type_is_struct(t) || btf_type_is_datasec(t))
  4499. return !btf_resolved_type_id(btf, type_id) &&
  4500. !btf_resolved_type_size(btf, type_id);
  4501. if (btf_type_is_decl_tag(t) || btf_type_is_func(t))
  4502. return btf_resolved_type_id(btf, type_id) &&
  4503. !btf_resolved_type_size(btf, type_id);
  4504. if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
  4505. btf_type_is_var(t)) {
  4506. t = btf_type_id_resolve(btf, &type_id);
  4507. return t &&
  4508. !btf_type_is_modifier(t) &&
  4509. !btf_type_is_var(t) &&
  4510. !btf_type_is_datasec(t);
  4511. }
  4512. if (btf_type_is_array(t)) {
  4513. const struct btf_array *array = btf_type_array(t);
  4514. const struct btf_type *elem_type;
  4515. u32 elem_type_id = array->type;
  4516. u32 elem_size;
  4517. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  4518. return elem_type && !btf_type_is_modifier(elem_type) &&
  4519. (array->nelems * elem_size ==
  4520. btf_resolved_type_size(btf, type_id));
  4521. }
  4522. return false;
  4523. }
  4524. static int btf_resolve(struct btf_verifier_env *env,
  4525. const struct btf_type *t, u32 type_id)
  4526. {
  4527. u32 save_log_type_id = env->log_type_id;
  4528. const struct resolve_vertex *v;
  4529. int err = 0;
  4530. env->resolve_mode = RESOLVE_TBD;
  4531. env_stack_push(env, t, type_id);
  4532. while (!err && (v = env_stack_peak(env))) {
  4533. env->log_type_id = v->type_id;
  4534. err = btf_type_ops(v->t)->resolve(env, v);
  4535. }
  4536. env->log_type_id = type_id;
  4537. if (err == -E2BIG) {
  4538. btf_verifier_log_type(env, t,
  4539. "Exceeded max resolving depth:%u",
  4540. MAX_RESOLVE_DEPTH);
  4541. } else if (err == -EEXIST) {
  4542. btf_verifier_log_type(env, t, "Loop detected");
  4543. }
  4544. /* Final sanity check */
  4545. if (!err && !btf_resolve_valid(env, t, type_id)) {
  4546. btf_verifier_log_type(env, t, "Invalid resolve state");
  4547. err = -EINVAL;
  4548. }
  4549. env->log_type_id = save_log_type_id;
  4550. return err;
  4551. }
  4552. static int btf_check_all_types(struct btf_verifier_env *env)
  4553. {
  4554. struct btf *btf = env->btf;
  4555. const struct btf_type *t;
  4556. u32 type_id, i;
  4557. int err;
  4558. err = env_resolve_init(env);
  4559. if (err)
  4560. return err;
  4561. env->phase++;
  4562. for (i = btf->base_btf ? 0 : 1; i < btf->nr_types; i++) {
  4563. type_id = btf->start_id + i;
  4564. t = btf_type_by_id(btf, type_id);
  4565. env->log_type_id = type_id;
  4566. if (btf_type_needs_resolve(t) &&
  4567. !env_type_is_resolved(env, type_id)) {
  4568. err = btf_resolve(env, t, type_id);
  4569. if (err)
  4570. return err;
  4571. }
  4572. if (btf_type_is_func_proto(t)) {
  4573. err = btf_func_proto_check(env, t);
  4574. if (err)
  4575. return err;
  4576. }
  4577. }
  4578. return 0;
  4579. }
  4580. static int btf_parse_type_sec(struct btf_verifier_env *env)
  4581. {
  4582. const struct btf_header *hdr = &env->btf->hdr;
  4583. int err;
  4584. /* Type section must align to 4 bytes */
  4585. if (hdr->type_off & (sizeof(u32) - 1)) {
  4586. btf_verifier_log(env, "Unaligned type_off");
  4587. return -EINVAL;
  4588. }
  4589. if (!env->btf->base_btf && !hdr->type_len) {
  4590. btf_verifier_log(env, "No type found");
  4591. return -EINVAL;
  4592. }
  4593. err = btf_check_all_metas(env);
  4594. if (err)
  4595. return err;
  4596. return btf_check_all_types(env);
  4597. }
  4598. static int btf_parse_str_sec(struct btf_verifier_env *env)
  4599. {
  4600. const struct btf_header *hdr;
  4601. struct btf *btf = env->btf;
  4602. const char *start, *end;
  4603. hdr = &btf->hdr;
  4604. start = btf->nohdr_data + hdr->str_off;
  4605. end = start + hdr->str_len;
  4606. if (end != btf->data + btf->data_size) {
  4607. btf_verifier_log(env, "String section is not at the end");
  4608. return -EINVAL;
  4609. }
  4610. btf->strings = start;
  4611. if (btf->base_btf && !hdr->str_len)
  4612. return 0;
  4613. if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET || end[-1]) {
  4614. btf_verifier_log(env, "Invalid string section");
  4615. return -EINVAL;
  4616. }
  4617. if (!btf->base_btf && start[0]) {
  4618. btf_verifier_log(env, "Invalid string section");
  4619. return -EINVAL;
  4620. }
  4621. return 0;
  4622. }
  4623. static const size_t btf_sec_info_offset[] = {
  4624. offsetof(struct btf_header, type_off),
  4625. offsetof(struct btf_header, str_off),
  4626. };
  4627. static int btf_sec_info_cmp(const void *a, const void *b)
  4628. {
  4629. const struct btf_sec_info *x = a;
  4630. const struct btf_sec_info *y = b;
  4631. return (int)(x->off - y->off) ? : (int)(x->len - y->len);
  4632. }
  4633. static int btf_check_sec_info(struct btf_verifier_env *env,
  4634. u32 btf_data_size)
  4635. {
  4636. struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
  4637. u32 total, expected_total, i;
  4638. const struct btf_header *hdr;
  4639. const struct btf *btf;
  4640. btf = env->btf;
  4641. hdr = &btf->hdr;
  4642. /* Populate the secs from hdr */
  4643. for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
  4644. secs[i] = *(struct btf_sec_info *)((void *)hdr +
  4645. btf_sec_info_offset[i]);
  4646. sort(secs, ARRAY_SIZE(btf_sec_info_offset),
  4647. sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
  4648. /* Check for gaps and overlap among sections */
  4649. total = 0;
  4650. expected_total = btf_data_size - hdr->hdr_len;
  4651. for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
  4652. if (expected_total < secs[i].off) {
  4653. btf_verifier_log(env, "Invalid section offset");
  4654. return -EINVAL;
  4655. }
  4656. if (total < secs[i].off) {
  4657. /* gap */
  4658. btf_verifier_log(env, "Unsupported section found");
  4659. return -EINVAL;
  4660. }
  4661. if (total > secs[i].off) {
  4662. btf_verifier_log(env, "Section overlap found");
  4663. return -EINVAL;
  4664. }
  4665. if (expected_total - total < secs[i].len) {
  4666. btf_verifier_log(env,
  4667. "Total section length too long");
  4668. return -EINVAL;
  4669. }
  4670. total += secs[i].len;
  4671. }
  4672. /* There is data other than hdr and known sections */
  4673. if (expected_total != total) {
  4674. btf_verifier_log(env, "Unsupported section found");
  4675. return -EINVAL;
  4676. }
  4677. return 0;
  4678. }
  4679. static int btf_parse_hdr(struct btf_verifier_env *env)
  4680. {
  4681. u32 hdr_len, hdr_copy, btf_data_size;
  4682. const struct btf_header *hdr;
  4683. struct btf *btf;
  4684. btf = env->btf;
  4685. btf_data_size = btf->data_size;
  4686. if (btf_data_size < offsetofend(struct btf_header, hdr_len)) {
  4687. btf_verifier_log(env, "hdr_len not found");
  4688. return -EINVAL;
  4689. }
  4690. hdr = btf->data;
  4691. hdr_len = hdr->hdr_len;
  4692. if (btf_data_size < hdr_len) {
  4693. btf_verifier_log(env, "btf_header not found");
  4694. return -EINVAL;
  4695. }
  4696. /* Ensure the unsupported header fields are zero */
  4697. if (hdr_len > sizeof(btf->hdr)) {
  4698. u8 *expected_zero = btf->data + sizeof(btf->hdr);
  4699. u8 *end = btf->data + hdr_len;
  4700. for (; expected_zero < end; expected_zero++) {
  4701. if (*expected_zero) {
  4702. btf_verifier_log(env, "Unsupported btf_header");
  4703. return -E2BIG;
  4704. }
  4705. }
  4706. }
  4707. hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
  4708. memcpy(&btf->hdr, btf->data, hdr_copy);
  4709. hdr = &btf->hdr;
  4710. btf_verifier_log_hdr(env, btf_data_size);
  4711. if (hdr->magic != BTF_MAGIC) {
  4712. btf_verifier_log(env, "Invalid magic");
  4713. return -EINVAL;
  4714. }
  4715. if (hdr->version != BTF_VERSION) {
  4716. btf_verifier_log(env, "Unsupported version");
  4717. return -ENOTSUPP;
  4718. }
  4719. if (hdr->flags) {
  4720. btf_verifier_log(env, "Unsupported flags");
  4721. return -ENOTSUPP;
  4722. }
  4723. if (!btf->base_btf && btf_data_size == hdr->hdr_len) {
  4724. btf_verifier_log(env, "No data");
  4725. return -EINVAL;
  4726. }
  4727. return btf_check_sec_info(env, btf_data_size);
  4728. }
  4729. static const char *alloc_obj_fields[] = {
  4730. "bpf_spin_lock",
  4731. "bpf_list_head",
  4732. "bpf_list_node",
  4733. "bpf_rb_root",
  4734. "bpf_rb_node",
  4735. "bpf_refcount",
  4736. };
  4737. static struct btf_struct_metas *
  4738. btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
  4739. {
  4740. struct btf_struct_metas *tab = NULL;
  4741. struct btf_id_set *aof;
  4742. int i, n, id, ret;
  4743. BUILD_BUG_ON(offsetof(struct btf_id_set, cnt) != 0);
  4744. BUILD_BUG_ON(sizeof(struct btf_id_set) != sizeof(u32));
  4745. aof = kmalloc(sizeof(*aof), GFP_KERNEL | __GFP_NOWARN);
  4746. if (!aof)
  4747. return ERR_PTR(-ENOMEM);
  4748. aof->cnt = 0;
  4749. for (i = 0; i < ARRAY_SIZE(alloc_obj_fields); i++) {
  4750. /* Try to find whether this special type exists in user BTF, and
  4751. * if so remember its ID so we can easily find it among members
  4752. * of structs that we iterate in the next loop.
  4753. */
  4754. struct btf_id_set *new_aof;
  4755. id = btf_find_by_name_kind(btf, alloc_obj_fields[i], BTF_KIND_STRUCT);
  4756. if (id < 0)
  4757. continue;
  4758. new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]),
  4759. GFP_KERNEL | __GFP_NOWARN);
  4760. if (!new_aof) {
  4761. ret = -ENOMEM;
  4762. goto free_aof;
  4763. }
  4764. aof = new_aof;
  4765. aof->ids[aof->cnt++] = id;
  4766. }
  4767. n = btf_nr_types(btf);
  4768. for (i = 1; i < n; i++) {
  4769. /* Try to find if there are kptrs in user BTF and remember their ID */
  4770. struct btf_id_set *new_aof;
  4771. struct btf_field_info tmp;
  4772. const struct btf_type *t;
  4773. t = btf_type_by_id(btf, i);
  4774. if (!t) {
  4775. ret = -EINVAL;
  4776. goto free_aof;
  4777. }
  4778. ret = btf_find_kptr(btf, t, 0, 0, &tmp);
  4779. if (ret != BTF_FIELD_FOUND)
  4780. continue;
  4781. new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]),
  4782. GFP_KERNEL | __GFP_NOWARN);
  4783. if (!new_aof) {
  4784. ret = -ENOMEM;
  4785. goto free_aof;
  4786. }
  4787. aof = new_aof;
  4788. aof->ids[aof->cnt++] = i;
  4789. }
  4790. if (!aof->cnt) {
  4791. kfree(aof);
  4792. return NULL;
  4793. }
  4794. sort(&aof->ids, aof->cnt, sizeof(aof->ids[0]), btf_id_cmp_func, NULL);
  4795. for (i = 1; i < n; i++) {
  4796. struct btf_struct_metas *new_tab;
  4797. const struct btf_member *member;
  4798. struct btf_struct_meta *type;
  4799. struct btf_record *record;
  4800. const struct btf_type *t;
  4801. int j, tab_cnt;
  4802. t = btf_type_by_id(btf, i);
  4803. if (!__btf_type_is_struct(t))
  4804. continue;
  4805. cond_resched();
  4806. for_each_member(j, t, member) {
  4807. if (btf_id_set_contains(aof, member->type))
  4808. goto parse;
  4809. }
  4810. continue;
  4811. parse:
  4812. tab_cnt = tab ? tab->cnt : 0;
  4813. new_tab = krealloc(tab, offsetof(struct btf_struct_metas, types[tab_cnt + 1]),
  4814. GFP_KERNEL | __GFP_NOWARN);
  4815. if (!new_tab) {
  4816. ret = -ENOMEM;
  4817. goto free;
  4818. }
  4819. if (!tab)
  4820. new_tab->cnt = 0;
  4821. tab = new_tab;
  4822. type = &tab->types[tab->cnt];
  4823. type->btf_id = i;
  4824. record = btf_parse_fields(btf, t, BPF_SPIN_LOCK | BPF_LIST_HEAD | BPF_LIST_NODE |
  4825. BPF_RB_ROOT | BPF_RB_NODE | BPF_REFCOUNT |
  4826. BPF_KPTR, t->size);
  4827. /* The record cannot be unset, treat it as an error if so */
  4828. if (IS_ERR_OR_NULL(record)) {
  4829. ret = PTR_ERR_OR_ZERO(record) ?: -EFAULT;
  4830. goto free;
  4831. }
  4832. type->record = record;
  4833. tab->cnt++;
  4834. }
  4835. kfree(aof);
  4836. return tab;
  4837. free:
  4838. btf_struct_metas_free(tab);
  4839. free_aof:
  4840. kfree(aof);
  4841. return ERR_PTR(ret);
  4842. }
  4843. struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
  4844. {
  4845. struct btf_struct_metas *tab;
  4846. BUILD_BUG_ON(offsetof(struct btf_struct_meta, btf_id) != 0);
  4847. tab = btf->struct_meta_tab;
  4848. if (!tab)
  4849. return NULL;
  4850. return bsearch(&btf_id, tab->types, tab->cnt, sizeof(tab->types[0]), btf_id_cmp_func);
  4851. }
  4852. static int btf_check_type_tags(struct btf_verifier_env *env,
  4853. struct btf *btf, int start_id)
  4854. {
  4855. int i, n, good_id = start_id - 1;
  4856. bool in_tags;
  4857. n = btf_nr_types(btf);
  4858. for (i = start_id; i < n; i++) {
  4859. const struct btf_type *t;
  4860. int chain_limit = 32;
  4861. u32 cur_id = i;
  4862. t = btf_type_by_id(btf, i);
  4863. if (!t)
  4864. return -EINVAL;
  4865. if (!btf_type_is_modifier(t))
  4866. continue;
  4867. cond_resched();
  4868. in_tags = btf_type_is_type_tag(t);
  4869. while (btf_type_is_modifier(t)) {
  4870. if (!chain_limit--) {
  4871. btf_verifier_log(env, "Max chain length or cycle detected");
  4872. return -ELOOP;
  4873. }
  4874. if (btf_type_is_type_tag(t)) {
  4875. if (!in_tags) {
  4876. btf_verifier_log(env, "Type tags don't precede modifiers");
  4877. return -EINVAL;
  4878. }
  4879. } else if (in_tags) {
  4880. in_tags = false;
  4881. }
  4882. if (cur_id <= good_id)
  4883. break;
  4884. /* Move to next type */
  4885. cur_id = t->type;
  4886. t = btf_type_by_id(btf, cur_id);
  4887. if (!t)
  4888. return -EINVAL;
  4889. }
  4890. good_id = i;
  4891. }
  4892. return 0;
  4893. }
  4894. static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 uattr_size)
  4895. {
  4896. u32 log_true_size;
  4897. int err;
  4898. err = bpf_vlog_finalize(log, &log_true_size);
  4899. if (uattr_size >= offsetofend(union bpf_attr, btf_log_true_size) &&
  4900. copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size),
  4901. &log_true_size, sizeof(log_true_size)))
  4902. err = -EFAULT;
  4903. return err;
  4904. }
  4905. static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
  4906. {
  4907. bpfptr_t btf_data = make_bpfptr(attr->btf, uattr.is_kernel);
  4908. char __user *log_ubuf = u64_to_user_ptr(attr->btf_log_buf);
  4909. struct btf_struct_metas *struct_meta_tab;
  4910. struct btf_verifier_env *env = NULL;
  4911. struct btf *btf = NULL;
  4912. u8 *data;
  4913. int err, ret;
  4914. if (attr->btf_size > BTF_MAX_SIZE)
  4915. return ERR_PTR(-E2BIG);
  4916. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  4917. if (!env)
  4918. return ERR_PTR(-ENOMEM);
  4919. /* user could have requested verbose verifier output
  4920. * and supplied buffer to store the verification trace
  4921. */
  4922. err = bpf_vlog_init(&env->log, attr->btf_log_level,
  4923. log_ubuf, attr->btf_log_size);
  4924. if (err)
  4925. goto errout_free;
  4926. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  4927. if (!btf) {
  4928. err = -ENOMEM;
  4929. goto errout;
  4930. }
  4931. env->btf = btf;
  4932. data = kvmalloc(attr->btf_size, GFP_KERNEL | __GFP_NOWARN);
  4933. if (!data) {
  4934. err = -ENOMEM;
  4935. goto errout;
  4936. }
  4937. btf->data = data;
  4938. btf->data_size = attr->btf_size;
  4939. if (copy_from_bpfptr(data, btf_data, attr->btf_size)) {
  4940. err = -EFAULT;
  4941. goto errout;
  4942. }
  4943. err = btf_parse_hdr(env);
  4944. if (err)
  4945. goto errout;
  4946. btf->nohdr_data = btf->data + btf->hdr.hdr_len;
  4947. err = btf_parse_str_sec(env);
  4948. if (err)
  4949. goto errout;
  4950. err = btf_parse_type_sec(env);
  4951. if (err)
  4952. goto errout;
  4953. err = btf_check_type_tags(env, btf, 1);
  4954. if (err)
  4955. goto errout;
  4956. struct_meta_tab = btf_parse_struct_metas(&env->log, btf);
  4957. if (IS_ERR(struct_meta_tab)) {
  4958. err = PTR_ERR(struct_meta_tab);
  4959. goto errout;
  4960. }
  4961. btf->struct_meta_tab = struct_meta_tab;
  4962. if (struct_meta_tab) {
  4963. int i;
  4964. for (i = 0; i < struct_meta_tab->cnt; i++) {
  4965. err = btf_check_and_fixup_fields(btf, struct_meta_tab->types[i].record);
  4966. if (err < 0)
  4967. goto errout_meta;
  4968. }
  4969. }
  4970. err = finalize_log(&env->log, uattr, uattr_size);
  4971. if (err)
  4972. goto errout_free;
  4973. btf_verifier_env_free(env);
  4974. refcount_set(&btf->refcnt, 1);
  4975. return btf;
  4976. errout_meta:
  4977. btf_free_struct_meta_tab(btf);
  4978. errout:
  4979. /* overwrite err with -ENOSPC or -EFAULT */
  4980. ret = finalize_log(&env->log, uattr, uattr_size);
  4981. if (ret)
  4982. err = ret;
  4983. errout_free:
  4984. btf_verifier_env_free(env);
  4985. if (btf)
  4986. btf_free(btf);
  4987. return ERR_PTR(err);
  4988. }
  4989. extern char __start_BTF[];
  4990. extern char __stop_BTF[];
  4991. extern struct btf *btf_vmlinux;
  4992. #define BPF_MAP_TYPE(_id, _ops)
  4993. #define BPF_LINK_TYPE(_id, _name)
  4994. static union {
  4995. struct bpf_ctx_convert {
  4996. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  4997. prog_ctx_type _id##_prog; \
  4998. kern_ctx_type _id##_kern;
  4999. #include <linux/bpf_types.h>
  5000. #undef BPF_PROG_TYPE
  5001. } *__t;
  5002. /* 't' is written once under lock. Read many times. */
  5003. const struct btf_type *t;
  5004. } bpf_ctx_convert;
  5005. enum {
  5006. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  5007. __ctx_convert##_id,
  5008. #include <linux/bpf_types.h>
  5009. #undef BPF_PROG_TYPE
  5010. __ctx_convert_unused, /* to avoid empty enum in extreme .config */
  5011. };
  5012. static u8 bpf_ctx_convert_map[] = {
  5013. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  5014. [_id] = __ctx_convert##_id,
  5015. #include <linux/bpf_types.h>
  5016. #undef BPF_PROG_TYPE
  5017. 0, /* avoid empty array */
  5018. };
  5019. #undef BPF_MAP_TYPE
  5020. #undef BPF_LINK_TYPE
  5021. static const struct btf_type *find_canonical_prog_ctx_type(enum bpf_prog_type prog_type)
  5022. {
  5023. const struct btf_type *conv_struct;
  5024. const struct btf_member *ctx_type;
  5025. conv_struct = bpf_ctx_convert.t;
  5026. if (!conv_struct)
  5027. return NULL;
  5028. /* prog_type is valid bpf program type. No need for bounds check. */
  5029. ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
  5030. /* ctx_type is a pointer to prog_ctx_type in vmlinux.
  5031. * Like 'struct __sk_buff'
  5032. */
  5033. return btf_type_by_id(btf_vmlinux, ctx_type->type);
  5034. }
  5035. static int find_kern_ctx_type_id(enum bpf_prog_type prog_type)
  5036. {
  5037. const struct btf_type *conv_struct;
  5038. const struct btf_member *ctx_type;
  5039. conv_struct = bpf_ctx_convert.t;
  5040. if (!conv_struct)
  5041. return -EFAULT;
  5042. /* prog_type is valid bpf program type. No need for bounds check. */
  5043. ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2 + 1;
  5044. /* ctx_type is a pointer to prog_ctx_type in vmlinux.
  5045. * Like 'struct sk_buff'
  5046. */
  5047. return ctx_type->type;
  5048. }
  5049. bool btf_is_projection_of(const char *pname, const char *tname)
  5050. {
  5051. if (strcmp(pname, "__sk_buff") == 0 && strcmp(tname, "sk_buff") == 0)
  5052. return true;
  5053. if (strcmp(pname, "xdp_md") == 0 && strcmp(tname, "xdp_buff") == 0)
  5054. return true;
  5055. return false;
  5056. }
  5057. bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
  5058. const struct btf_type *t, enum bpf_prog_type prog_type,
  5059. int arg)
  5060. {
  5061. const struct btf_type *ctx_type;
  5062. const char *tname, *ctx_tname;
  5063. t = btf_type_by_id(btf, t->type);
  5064. /* KPROBE programs allow bpf_user_pt_regs_t typedef, which we need to
  5065. * check before we skip all the typedef below.
  5066. */
  5067. if (prog_type == BPF_PROG_TYPE_KPROBE) {
  5068. while (btf_type_is_modifier(t) && !btf_type_is_typedef(t))
  5069. t = btf_type_by_id(btf, t->type);
  5070. if (btf_type_is_typedef(t)) {
  5071. tname = btf_name_by_offset(btf, t->name_off);
  5072. if (tname && strcmp(tname, "bpf_user_pt_regs_t") == 0)
  5073. return true;
  5074. }
  5075. }
  5076. while (btf_type_is_modifier(t))
  5077. t = btf_type_by_id(btf, t->type);
  5078. if (!btf_type_is_struct(t)) {
  5079. /* Only pointer to struct is supported for now.
  5080. * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
  5081. * is not supported yet.
  5082. * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
  5083. */
  5084. return false;
  5085. }
  5086. tname = btf_name_by_offset(btf, t->name_off);
  5087. if (!tname) {
  5088. bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
  5089. return false;
  5090. }
  5091. ctx_type = find_canonical_prog_ctx_type(prog_type);
  5092. if (!ctx_type) {
  5093. bpf_log(log, "btf_vmlinux is malformed\n");
  5094. /* should not happen */
  5095. return false;
  5096. }
  5097. again:
  5098. ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type->name_off);
  5099. if (!ctx_tname) {
  5100. /* should not happen */
  5101. bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
  5102. return false;
  5103. }
  5104. /* program types without named context types work only with arg:ctx tag */
  5105. if (ctx_tname[0] == '\0')
  5106. return false;
  5107. /* only compare that prog's ctx type name is the same as
  5108. * kernel expects. No need to compare field by field.
  5109. * It's ok for bpf prog to do:
  5110. * struct __sk_buff {};
  5111. * int socket_filter_bpf_prog(struct __sk_buff *skb)
  5112. * { // no fields of skb are ever used }
  5113. */
  5114. if (btf_is_projection_of(ctx_tname, tname))
  5115. return true;
  5116. if (strcmp(ctx_tname, tname)) {
  5117. /* bpf_user_pt_regs_t is a typedef, so resolve it to
  5118. * underlying struct and check name again
  5119. */
  5120. if (!btf_type_is_modifier(ctx_type))
  5121. return false;
  5122. while (btf_type_is_modifier(ctx_type))
  5123. ctx_type = btf_type_by_id(btf_vmlinux, ctx_type->type);
  5124. goto again;
  5125. }
  5126. return true;
  5127. }
  5128. /* forward declarations for arch-specific underlying types of
  5129. * bpf_user_pt_regs_t; this avoids the need for arch-specific #ifdef
  5130. * compilation guards below for BPF_PROG_TYPE_PERF_EVENT checks, but still
  5131. * works correctly with __builtin_types_compatible_p() on respective
  5132. * architectures
  5133. */
  5134. struct user_regs_struct;
  5135. struct user_pt_regs;
  5136. static int btf_validate_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
  5137. const struct btf_type *t, int arg,
  5138. enum bpf_prog_type prog_type,
  5139. enum bpf_attach_type attach_type)
  5140. {
  5141. const struct btf_type *ctx_type;
  5142. const char *tname, *ctx_tname;
  5143. if (!btf_is_ptr(t)) {
  5144. bpf_log(log, "arg#%d type isn't a pointer\n", arg);
  5145. return -EINVAL;
  5146. }
  5147. t = btf_type_by_id(btf, t->type);
  5148. /* KPROBE and PERF_EVENT programs allow bpf_user_pt_regs_t typedef */
  5149. if (prog_type == BPF_PROG_TYPE_KPROBE || prog_type == BPF_PROG_TYPE_PERF_EVENT) {
  5150. while (btf_type_is_modifier(t) && !btf_type_is_typedef(t))
  5151. t = btf_type_by_id(btf, t->type);
  5152. if (btf_type_is_typedef(t)) {
  5153. tname = btf_name_by_offset(btf, t->name_off);
  5154. if (tname && strcmp(tname, "bpf_user_pt_regs_t") == 0)
  5155. return 0;
  5156. }
  5157. }
  5158. /* all other program types don't use typedefs for context type */
  5159. while (btf_type_is_modifier(t))
  5160. t = btf_type_by_id(btf, t->type);
  5161. /* `void *ctx __arg_ctx` is always valid */
  5162. if (btf_type_is_void(t))
  5163. return 0;
  5164. tname = btf_name_by_offset(btf, t->name_off);
  5165. if (str_is_empty(tname)) {
  5166. bpf_log(log, "arg#%d type doesn't have a name\n", arg);
  5167. return -EINVAL;
  5168. }
  5169. /* special cases */
  5170. switch (prog_type) {
  5171. case BPF_PROG_TYPE_KPROBE:
  5172. if (__btf_type_is_struct(t) && strcmp(tname, "pt_regs") == 0)
  5173. return 0;
  5174. break;
  5175. case BPF_PROG_TYPE_PERF_EVENT:
  5176. if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&
  5177. __btf_type_is_struct(t) && strcmp(tname, "pt_regs") == 0)
  5178. return 0;
  5179. if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) &&
  5180. __btf_type_is_struct(t) && strcmp(tname, "user_pt_regs") == 0)
  5181. return 0;
  5182. if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&
  5183. __btf_type_is_struct(t) && strcmp(tname, "user_regs_struct") == 0)
  5184. return 0;
  5185. break;
  5186. case BPF_PROG_TYPE_RAW_TRACEPOINT:
  5187. case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
  5188. /* allow u64* as ctx */
  5189. if (btf_is_int(t) && t->size == 8)
  5190. return 0;
  5191. break;
  5192. case BPF_PROG_TYPE_TRACING:
  5193. switch (attach_type) {
  5194. case BPF_TRACE_RAW_TP:
  5195. /* tp_btf program is TRACING, so need special case here */
  5196. if (__btf_type_is_struct(t) &&
  5197. strcmp(tname, "bpf_raw_tracepoint_args") == 0)
  5198. return 0;
  5199. /* allow u64* as ctx */
  5200. if (btf_is_int(t) && t->size == 8)
  5201. return 0;
  5202. break;
  5203. case BPF_TRACE_ITER:
  5204. /* allow struct bpf_iter__xxx types only */
  5205. if (__btf_type_is_struct(t) &&
  5206. strncmp(tname, "bpf_iter__", sizeof("bpf_iter__") - 1) == 0)
  5207. return 0;
  5208. break;
  5209. case BPF_TRACE_FENTRY:
  5210. case BPF_TRACE_FEXIT:
  5211. case BPF_MODIFY_RETURN:
  5212. /* allow u64* as ctx */
  5213. if (btf_is_int(t) && t->size == 8)
  5214. return 0;
  5215. break;
  5216. default:
  5217. break;
  5218. }
  5219. break;
  5220. case BPF_PROG_TYPE_LSM:
  5221. case BPF_PROG_TYPE_STRUCT_OPS:
  5222. /* allow u64* as ctx */
  5223. if (btf_is_int(t) && t->size == 8)
  5224. return 0;
  5225. break;
  5226. case BPF_PROG_TYPE_TRACEPOINT:
  5227. case BPF_PROG_TYPE_SYSCALL:
  5228. case BPF_PROG_TYPE_EXT:
  5229. return 0; /* anything goes */
  5230. default:
  5231. break;
  5232. }
  5233. ctx_type = find_canonical_prog_ctx_type(prog_type);
  5234. if (!ctx_type) {
  5235. /* should not happen */
  5236. bpf_log(log, "btf_vmlinux is malformed\n");
  5237. return -EINVAL;
  5238. }
  5239. /* resolve typedefs and check that underlying structs are matching as well */
  5240. while (btf_type_is_modifier(ctx_type))
  5241. ctx_type = btf_type_by_id(btf_vmlinux, ctx_type->type);
  5242. /* if program type doesn't have distinctly named struct type for
  5243. * context, then __arg_ctx argument can only be `void *`, which we
  5244. * already checked above
  5245. */
  5246. if (!__btf_type_is_struct(ctx_type)) {
  5247. bpf_log(log, "arg#%d should be void pointer\n", arg);
  5248. return -EINVAL;
  5249. }
  5250. ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type->name_off);
  5251. if (!__btf_type_is_struct(t) || strcmp(ctx_tname, tname) != 0) {
  5252. bpf_log(log, "arg#%d should be `struct %s *`\n", arg, ctx_tname);
  5253. return -EINVAL;
  5254. }
  5255. return 0;
  5256. }
  5257. static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
  5258. struct btf *btf,
  5259. const struct btf_type *t,
  5260. enum bpf_prog_type prog_type,
  5261. int arg)
  5262. {
  5263. if (!btf_is_prog_ctx_type(log, btf, t, prog_type, arg))
  5264. return -ENOENT;
  5265. return find_kern_ctx_type_id(prog_type);
  5266. }
  5267. int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type)
  5268. {
  5269. const struct btf_member *kctx_member;
  5270. const struct btf_type *conv_struct;
  5271. const struct btf_type *kctx_type;
  5272. u32 kctx_type_id;
  5273. conv_struct = bpf_ctx_convert.t;
  5274. /* get member for kernel ctx type */
  5275. kctx_member = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2 + 1;
  5276. kctx_type_id = kctx_member->type;
  5277. kctx_type = btf_type_by_id(btf_vmlinux, kctx_type_id);
  5278. if (!btf_type_is_struct(kctx_type)) {
  5279. bpf_log(log, "kern ctx type id %u is not a struct\n", kctx_type_id);
  5280. return -EINVAL;
  5281. }
  5282. return kctx_type_id;
  5283. }
  5284. BTF_ID_LIST(bpf_ctx_convert_btf_id)
  5285. BTF_ID(struct, bpf_ctx_convert)
  5286. static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name,
  5287. void *data, unsigned int data_size)
  5288. {
  5289. struct btf *btf = NULL;
  5290. int err;
  5291. if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF))
  5292. return ERR_PTR(-ENOENT);
  5293. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  5294. if (!btf) {
  5295. err = -ENOMEM;
  5296. goto errout;
  5297. }
  5298. env->btf = btf;
  5299. btf->data = data;
  5300. btf->data_size = data_size;
  5301. btf->kernel_btf = true;
  5302. snprintf(btf->name, sizeof(btf->name), "%s", name);
  5303. err = btf_parse_hdr(env);
  5304. if (err)
  5305. goto errout;
  5306. btf->nohdr_data = btf->data + btf->hdr.hdr_len;
  5307. err = btf_parse_str_sec(env);
  5308. if (err)
  5309. goto errout;
  5310. err = btf_check_all_metas(env);
  5311. if (err)
  5312. goto errout;
  5313. err = btf_check_type_tags(env, btf, 1);
  5314. if (err)
  5315. goto errout;
  5316. refcount_set(&btf->refcnt, 1);
  5317. return btf;
  5318. errout:
  5319. if (btf) {
  5320. kvfree(btf->types);
  5321. kfree(btf);
  5322. }
  5323. return ERR_PTR(err);
  5324. }
  5325. struct btf *btf_parse_vmlinux(void)
  5326. {
  5327. struct btf_verifier_env *env = NULL;
  5328. struct bpf_verifier_log *log;
  5329. struct btf *btf;
  5330. int err;
  5331. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  5332. if (!env)
  5333. return ERR_PTR(-ENOMEM);
  5334. log = &env->log;
  5335. log->level = BPF_LOG_KERNEL;
  5336. btf = btf_parse_base(env, "vmlinux", __start_BTF, __stop_BTF - __start_BTF);
  5337. if (IS_ERR(btf))
  5338. goto err_out;
  5339. /* btf_parse_vmlinux() runs under bpf_verifier_lock */
  5340. bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
  5341. err = btf_alloc_id(btf);
  5342. if (err) {
  5343. btf_free(btf);
  5344. btf = ERR_PTR(err);
  5345. }
  5346. err_out:
  5347. btf_verifier_env_free(env);
  5348. return btf;
  5349. }
  5350. /* If .BTF_ids section was created with distilled base BTF, both base and
  5351. * split BTF ids will need to be mapped to actual base/split ids for
  5352. * BTF now that it has been relocated.
  5353. */
  5354. static __u32 btf_relocate_id(const struct btf *btf, __u32 id)
  5355. {
  5356. if (!btf->base_btf || !btf->base_id_map)
  5357. return id;
  5358. return btf->base_id_map[id];
  5359. }
  5360. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  5361. static struct btf *btf_parse_module(const char *module_name, const void *data,
  5362. unsigned int data_size, void *base_data,
  5363. unsigned int base_data_size)
  5364. {
  5365. struct btf *btf = NULL, *vmlinux_btf, *base_btf = NULL;
  5366. struct btf_verifier_env *env = NULL;
  5367. struct bpf_verifier_log *log;
  5368. int err = 0;
  5369. vmlinux_btf = bpf_get_btf_vmlinux();
  5370. if (IS_ERR(vmlinux_btf))
  5371. return vmlinux_btf;
  5372. if (!vmlinux_btf)
  5373. return ERR_PTR(-EINVAL);
  5374. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  5375. if (!env)
  5376. return ERR_PTR(-ENOMEM);
  5377. log = &env->log;
  5378. log->level = BPF_LOG_KERNEL;
  5379. if (base_data) {
  5380. base_btf = btf_parse_base(env, ".BTF.base", base_data, base_data_size);
  5381. if (IS_ERR(base_btf)) {
  5382. err = PTR_ERR(base_btf);
  5383. goto errout;
  5384. }
  5385. } else {
  5386. base_btf = vmlinux_btf;
  5387. }
  5388. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  5389. if (!btf) {
  5390. err = -ENOMEM;
  5391. goto errout;
  5392. }
  5393. env->btf = btf;
  5394. btf->base_btf = base_btf;
  5395. btf->start_id = base_btf->nr_types;
  5396. btf->start_str_off = base_btf->hdr.str_len;
  5397. btf->kernel_btf = true;
  5398. snprintf(btf->name, sizeof(btf->name), "%s", module_name);
  5399. btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
  5400. if (!btf->data) {
  5401. err = -ENOMEM;
  5402. goto errout;
  5403. }
  5404. btf->data_size = data_size;
  5405. err = btf_parse_hdr(env);
  5406. if (err)
  5407. goto errout;
  5408. btf->nohdr_data = btf->data + btf->hdr.hdr_len;
  5409. err = btf_parse_str_sec(env);
  5410. if (err)
  5411. goto errout;
  5412. err = btf_check_all_metas(env);
  5413. if (err)
  5414. goto errout;
  5415. err = btf_check_type_tags(env, btf, btf_nr_types(base_btf));
  5416. if (err)
  5417. goto errout;
  5418. if (base_btf != vmlinux_btf) {
  5419. err = btf_relocate(btf, vmlinux_btf, &btf->base_id_map);
  5420. if (err)
  5421. goto errout;
  5422. btf_free(base_btf);
  5423. base_btf = vmlinux_btf;
  5424. }
  5425. btf_verifier_env_free(env);
  5426. refcount_set(&btf->refcnt, 1);
  5427. return btf;
  5428. errout:
  5429. btf_verifier_env_free(env);
  5430. if (!IS_ERR(base_btf) && base_btf != vmlinux_btf)
  5431. btf_free(base_btf);
  5432. if (btf) {
  5433. kvfree(btf->data);
  5434. kvfree(btf->types);
  5435. kfree(btf);
  5436. }
  5437. return ERR_PTR(err);
  5438. }
  5439. #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
  5440. struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
  5441. {
  5442. struct bpf_prog *tgt_prog = prog->aux->dst_prog;
  5443. if (tgt_prog)
  5444. return tgt_prog->aux->btf;
  5445. else
  5446. return prog->aux->attach_btf;
  5447. }
  5448. static bool is_int_ptr(struct btf *btf, const struct btf_type *t)
  5449. {
  5450. /* skip modifiers */
  5451. t = btf_type_skip_modifiers(btf, t->type, NULL);
  5452. return btf_type_is_int(t);
  5453. }
  5454. static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto,
  5455. int off)
  5456. {
  5457. const struct btf_param *args;
  5458. const struct btf_type *t;
  5459. u32 offset = 0, nr_args;
  5460. int i;
  5461. if (!func_proto)
  5462. return off / 8;
  5463. nr_args = btf_type_vlen(func_proto);
  5464. args = (const struct btf_param *)(func_proto + 1);
  5465. for (i = 0; i < nr_args; i++) {
  5466. t = btf_type_skip_modifiers(btf, args[i].type, NULL);
  5467. offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8);
  5468. if (off < offset)
  5469. return i;
  5470. }
  5471. t = btf_type_skip_modifiers(btf, func_proto->type, NULL);
  5472. offset += btf_type_is_ptr(t) ? 8 : roundup(t->size, 8);
  5473. if (off < offset)
  5474. return nr_args;
  5475. return nr_args + 1;
  5476. }
  5477. static bool prog_args_trusted(const struct bpf_prog *prog)
  5478. {
  5479. enum bpf_attach_type atype = prog->expected_attach_type;
  5480. switch (prog->type) {
  5481. case BPF_PROG_TYPE_TRACING:
  5482. return atype == BPF_TRACE_RAW_TP || atype == BPF_TRACE_ITER;
  5483. case BPF_PROG_TYPE_LSM:
  5484. return bpf_lsm_is_trusted(prog);
  5485. case BPF_PROG_TYPE_STRUCT_OPS:
  5486. return true;
  5487. default:
  5488. return false;
  5489. }
  5490. }
  5491. int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto,
  5492. u32 arg_no)
  5493. {
  5494. const struct btf_param *args;
  5495. const struct btf_type *t;
  5496. int off = 0, i;
  5497. u32 sz;
  5498. args = btf_params(func_proto);
  5499. for (i = 0; i < arg_no; i++) {
  5500. t = btf_type_by_id(btf, args[i].type);
  5501. t = btf_resolve_size(btf, t, &sz);
  5502. if (IS_ERR(t))
  5503. return PTR_ERR(t);
  5504. off += roundup(sz, 8);
  5505. }
  5506. return off;
  5507. }
  5508. struct bpf_raw_tp_null_args {
  5509. const char *func;
  5510. u64 mask;
  5511. };
  5512. static const struct bpf_raw_tp_null_args raw_tp_null_args[] = {
  5513. /* sched */
  5514. { "sched_pi_setprio", 0x10 },
  5515. /* ... from sched_numa_pair_template event class */
  5516. { "sched_stick_numa", 0x100 },
  5517. { "sched_swap_numa", 0x100 },
  5518. /* afs */
  5519. { "afs_make_fs_call", 0x10 },
  5520. { "afs_make_fs_calli", 0x10 },
  5521. { "afs_make_fs_call1", 0x10 },
  5522. { "afs_make_fs_call2", 0x10 },
  5523. { "afs_protocol_error", 0x1 },
  5524. { "afs_flock_ev", 0x10 },
  5525. /* cachefiles */
  5526. { "cachefiles_lookup", 0x1 | 0x200 },
  5527. { "cachefiles_unlink", 0x1 },
  5528. { "cachefiles_rename", 0x1 },
  5529. { "cachefiles_prep_read", 0x1 },
  5530. { "cachefiles_mark_active", 0x1 },
  5531. { "cachefiles_mark_failed", 0x1 },
  5532. { "cachefiles_mark_inactive", 0x1 },
  5533. { "cachefiles_vfs_error", 0x1 },
  5534. { "cachefiles_io_error", 0x1 },
  5535. { "cachefiles_ondemand_open", 0x1 },
  5536. { "cachefiles_ondemand_copen", 0x1 },
  5537. { "cachefiles_ondemand_close", 0x1 },
  5538. { "cachefiles_ondemand_read", 0x1 },
  5539. { "cachefiles_ondemand_cread", 0x1 },
  5540. { "cachefiles_ondemand_fd_write", 0x1 },
  5541. { "cachefiles_ondemand_fd_release", 0x1 },
  5542. /* ext4, from ext4__mballoc event class */
  5543. { "ext4_mballoc_discard", 0x10 },
  5544. { "ext4_mballoc_free", 0x10 },
  5545. /* fib */
  5546. { "fib_table_lookup", 0x100 },
  5547. /* filelock */
  5548. /* ... from filelock_lock event class */
  5549. { "posix_lock_inode", 0x10 },
  5550. { "fcntl_setlk", 0x10 },
  5551. { "locks_remove_posix", 0x10 },
  5552. { "flock_lock_inode", 0x10 },
  5553. /* ... from filelock_lease event class */
  5554. { "break_lease_noblock", 0x10 },
  5555. { "break_lease_block", 0x10 },
  5556. { "break_lease_unblock", 0x10 },
  5557. { "generic_delete_lease", 0x10 },
  5558. { "time_out_leases", 0x10 },
  5559. /* host1x */
  5560. { "host1x_cdma_push_gather", 0x10000 },
  5561. /* huge_memory */
  5562. { "mm_khugepaged_scan_pmd", 0x10 },
  5563. { "mm_collapse_huge_page_isolate", 0x1 },
  5564. { "mm_khugepaged_scan_file", 0x10 },
  5565. { "mm_khugepaged_collapse_file", 0x10 },
  5566. /* kmem */
  5567. { "mm_page_alloc", 0x1 },
  5568. { "mm_page_pcpu_drain", 0x1 },
  5569. /* .. from mm_page event class */
  5570. { "mm_page_alloc_zone_locked", 0x1 },
  5571. /* netfs */
  5572. { "netfs_failure", 0x10 },
  5573. /* power */
  5574. { "device_pm_callback_start", 0x10 },
  5575. /* qdisc */
  5576. { "qdisc_dequeue", 0x1000 },
  5577. /* rxrpc */
  5578. { "rxrpc_recvdata", 0x1 },
  5579. { "rxrpc_resend", 0x10 },
  5580. /* skb */
  5581. {"kfree_skb", 0x1000},
  5582. /* sunrpc */
  5583. { "xs_stream_read_data", 0x1 },
  5584. /* ... from xprt_cong_event event class */
  5585. { "xprt_reserve_cong", 0x10 },
  5586. { "xprt_release_cong", 0x10 },
  5587. { "xprt_get_cong", 0x10 },
  5588. { "xprt_put_cong", 0x10 },
  5589. /* tcp */
  5590. { "tcp_send_reset", 0x11 },
  5591. /* tegra_apb_dma */
  5592. { "tegra_dma_tx_status", 0x100 },
  5593. /* timer_migration */
  5594. { "tmigr_update_events", 0x1 },
  5595. /* writeback, from writeback_folio_template event class */
  5596. { "writeback_dirty_folio", 0x10 },
  5597. { "folio_wait_writeback", 0x10 },
  5598. /* rdma */
  5599. { "mr_integ_alloc", 0x2000 },
  5600. /* bpf_testmod */
  5601. { "bpf_testmod_test_read", 0x0 },
  5602. };
  5603. bool btf_ctx_access(int off, int size, enum bpf_access_type type,
  5604. const struct bpf_prog *prog,
  5605. struct bpf_insn_access_aux *info)
  5606. {
  5607. const struct btf_type *t = prog->aux->attach_func_proto;
  5608. struct bpf_prog *tgt_prog = prog->aux->dst_prog;
  5609. struct btf *btf = bpf_prog_get_target_btf(prog);
  5610. const char *tname = prog->aux->attach_func_name;
  5611. struct bpf_verifier_log *log = info->log;
  5612. const struct btf_param *args;
  5613. bool ptr_err_raw_tp = false;
  5614. const char *tag_value;
  5615. u32 nr_args, arg;
  5616. int i, ret;
  5617. if (off % 8) {
  5618. bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
  5619. tname, off);
  5620. return false;
  5621. }
  5622. arg = get_ctx_arg_idx(btf, t, off);
  5623. args = (const struct btf_param *)(t + 1);
  5624. /* if (t == NULL) Fall back to default BPF prog with
  5625. * MAX_BPF_FUNC_REG_ARGS u64 arguments.
  5626. */
  5627. nr_args = t ? btf_type_vlen(t) : MAX_BPF_FUNC_REG_ARGS;
  5628. if (prog->aux->attach_btf_trace) {
  5629. /* skip first 'void *__data' argument in btf_trace_##name typedef */
  5630. args++;
  5631. nr_args--;
  5632. }
  5633. if (arg > nr_args) {
  5634. bpf_log(log, "func '%s' doesn't have %d-th argument\n",
  5635. tname, arg + 1);
  5636. return false;
  5637. }
  5638. if (arg == nr_args) {
  5639. switch (prog->expected_attach_type) {
  5640. case BPF_LSM_MAC:
  5641. /* mark we are accessing the return value */
  5642. info->is_retval = true;
  5643. fallthrough;
  5644. case BPF_LSM_CGROUP:
  5645. case BPF_TRACE_FEXIT:
  5646. /* When LSM programs are attached to void LSM hooks
  5647. * they use FEXIT trampolines and when attached to
  5648. * int LSM hooks, they use MODIFY_RETURN trampolines.
  5649. *
  5650. * While the LSM programs are BPF_MODIFY_RETURN-like
  5651. * the check:
  5652. *
  5653. * if (ret_type != 'int')
  5654. * return -EINVAL;
  5655. *
  5656. * is _not_ done here. This is still safe as LSM hooks
  5657. * have only void and int return types.
  5658. */
  5659. if (!t)
  5660. return true;
  5661. t = btf_type_by_id(btf, t->type);
  5662. break;
  5663. case BPF_MODIFY_RETURN:
  5664. /* For now the BPF_MODIFY_RETURN can only be attached to
  5665. * functions that return an int.
  5666. */
  5667. if (!t)
  5668. return false;
  5669. t = btf_type_skip_modifiers(btf, t->type, NULL);
  5670. if (!btf_type_is_small_int(t)) {
  5671. bpf_log(log,
  5672. "ret type %s not allowed for fmod_ret\n",
  5673. btf_type_str(t));
  5674. return false;
  5675. }
  5676. break;
  5677. default:
  5678. bpf_log(log, "func '%s' doesn't have %d-th argument\n",
  5679. tname, arg + 1);
  5680. return false;
  5681. }
  5682. } else {
  5683. if (!t)
  5684. /* Default prog with MAX_BPF_FUNC_REG_ARGS args */
  5685. return true;
  5686. t = btf_type_by_id(btf, args[arg].type);
  5687. }
  5688. /* skip modifiers */
  5689. while (btf_type_is_modifier(t))
  5690. t = btf_type_by_id(btf, t->type);
  5691. if (btf_type_is_small_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t))
  5692. /* accessing a scalar */
  5693. return true;
  5694. if (!btf_type_is_ptr(t)) {
  5695. bpf_log(log,
  5696. "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
  5697. tname, arg,
  5698. __btf_name_by_offset(btf, t->name_off),
  5699. btf_type_str(t));
  5700. return false;
  5701. }
  5702. if (size != sizeof(u64)) {
  5703. bpf_log(log, "func '%s' size %d must be 8\n",
  5704. tname, size);
  5705. return false;
  5706. }
  5707. /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
  5708. for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
  5709. const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
  5710. u32 type, flag;
  5711. type = base_type(ctx_arg_info->reg_type);
  5712. flag = type_flag(ctx_arg_info->reg_type);
  5713. if (ctx_arg_info->offset == off && type == PTR_TO_BUF &&
  5714. (flag & PTR_MAYBE_NULL)) {
  5715. info->reg_type = ctx_arg_info->reg_type;
  5716. return true;
  5717. }
  5718. }
  5719. if (t->type == 0)
  5720. /* This is a pointer to void.
  5721. * It is the same as scalar from the verifier safety pov.
  5722. * No further pointer walking is allowed.
  5723. */
  5724. return true;
  5725. if (is_int_ptr(btf, t))
  5726. return true;
  5727. /* this is a pointer to another type */
  5728. for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
  5729. const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
  5730. if (ctx_arg_info->offset == off) {
  5731. if (!ctx_arg_info->btf_id) {
  5732. bpf_log(log,"invalid btf_id for context argument offset %u\n", off);
  5733. return false;
  5734. }
  5735. info->reg_type = ctx_arg_info->reg_type;
  5736. info->btf = ctx_arg_info->btf ? : btf_vmlinux;
  5737. info->btf_id = ctx_arg_info->btf_id;
  5738. return true;
  5739. }
  5740. }
  5741. info->reg_type = PTR_TO_BTF_ID;
  5742. if (prog_args_trusted(prog))
  5743. info->reg_type |= PTR_TRUSTED;
  5744. if (btf_param_match_suffix(btf, &args[arg], "__nullable"))
  5745. info->reg_type |= PTR_MAYBE_NULL;
  5746. if (prog->expected_attach_type == BPF_TRACE_RAW_TP) {
  5747. struct btf *btf = prog->aux->attach_btf;
  5748. const struct btf_type *t;
  5749. const char *tname;
  5750. /* BTF lookups cannot fail, return false on error */
  5751. t = btf_type_by_id(btf, prog->aux->attach_btf_id);
  5752. if (!t)
  5753. return false;
  5754. tname = btf_name_by_offset(btf, t->name_off);
  5755. if (!tname)
  5756. return false;
  5757. /* Checked by bpf_check_attach_target */
  5758. tname += sizeof("btf_trace_") - 1;
  5759. for (i = 0; i < ARRAY_SIZE(raw_tp_null_args); i++) {
  5760. /* Is this a func with potential NULL args? */
  5761. if (strcmp(tname, raw_tp_null_args[i].func))
  5762. continue;
  5763. if (raw_tp_null_args[i].mask & (0x1ULL << (arg * 4)))
  5764. info->reg_type |= PTR_MAYBE_NULL;
  5765. /* Is the current arg IS_ERR? */
  5766. if (raw_tp_null_args[i].mask & (0x2ULL << (arg * 4)))
  5767. ptr_err_raw_tp = true;
  5768. break;
  5769. }
  5770. /* If we don't know NULL-ness specification and the tracepoint
  5771. * is coming from a loadable module, be conservative and mark
  5772. * argument as PTR_MAYBE_NULL.
  5773. */
  5774. if (i == ARRAY_SIZE(raw_tp_null_args) && btf_is_module(btf))
  5775. info->reg_type |= PTR_MAYBE_NULL;
  5776. }
  5777. if (tgt_prog) {
  5778. enum bpf_prog_type tgt_type;
  5779. if (tgt_prog->type == BPF_PROG_TYPE_EXT)
  5780. tgt_type = tgt_prog->aux->saved_dst_prog_type;
  5781. else
  5782. tgt_type = tgt_prog->type;
  5783. ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg);
  5784. if (ret > 0) {
  5785. info->btf = btf_vmlinux;
  5786. info->btf_id = ret;
  5787. return true;
  5788. } else {
  5789. return false;
  5790. }
  5791. }
  5792. info->btf = btf;
  5793. info->btf_id = t->type;
  5794. t = btf_type_by_id(btf, t->type);
  5795. if (btf_type_is_type_tag(t)) {
  5796. tag_value = __btf_name_by_offset(btf, t->name_off);
  5797. if (strcmp(tag_value, "user") == 0)
  5798. info->reg_type |= MEM_USER;
  5799. if (strcmp(tag_value, "percpu") == 0)
  5800. info->reg_type |= MEM_PERCPU;
  5801. }
  5802. /* skip modifiers */
  5803. while (btf_type_is_modifier(t)) {
  5804. info->btf_id = t->type;
  5805. t = btf_type_by_id(btf, t->type);
  5806. }
  5807. if (!btf_type_is_struct(t)) {
  5808. bpf_log(log,
  5809. "func '%s' arg%d type %s is not a struct\n",
  5810. tname, arg, btf_type_str(t));
  5811. return false;
  5812. }
  5813. bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
  5814. tname, arg, info->btf_id, btf_type_str(t),
  5815. __btf_name_by_offset(btf, t->name_off));
  5816. /* Perform all checks on the validity of type for this argument, but if
  5817. * we know it can be IS_ERR at runtime, scrub pointer type and mark as
  5818. * scalar.
  5819. */
  5820. if (ptr_err_raw_tp) {
  5821. bpf_log(log, "marking pointer arg%d as scalar as it may encode error", arg);
  5822. info->reg_type = SCALAR_VALUE;
  5823. }
  5824. return true;
  5825. }
  5826. EXPORT_SYMBOL_GPL(btf_ctx_access);
  5827. enum bpf_struct_walk_result {
  5828. /* < 0 error */
  5829. WALK_SCALAR = 0,
  5830. WALK_PTR,
  5831. WALK_STRUCT,
  5832. };
  5833. static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf,
  5834. const struct btf_type *t, int off, int size,
  5835. u32 *next_btf_id, enum bpf_type_flag *flag,
  5836. const char **field_name)
  5837. {
  5838. u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
  5839. const struct btf_type *mtype, *elem_type = NULL;
  5840. const struct btf_member *member;
  5841. const char *tname, *mname, *tag_value;
  5842. u32 vlen, elem_id, mid;
  5843. again:
  5844. if (btf_type_is_modifier(t))
  5845. t = btf_type_skip_modifiers(btf, t->type, NULL);
  5846. tname = __btf_name_by_offset(btf, t->name_off);
  5847. if (!btf_type_is_struct(t)) {
  5848. bpf_log(log, "Type '%s' is not a struct\n", tname);
  5849. return -EINVAL;
  5850. }
  5851. vlen = btf_type_vlen(t);
  5852. if (BTF_INFO_KIND(t->info) == BTF_KIND_UNION && vlen != 1 && !(*flag & PTR_UNTRUSTED))
  5853. /*
  5854. * walking unions yields untrusted pointers
  5855. * with exception of __bpf_md_ptr and other
  5856. * unions with a single member
  5857. */
  5858. *flag |= PTR_UNTRUSTED;
  5859. if (off + size > t->size) {
  5860. /* If the last element is a variable size array, we may
  5861. * need to relax the rule.
  5862. */
  5863. struct btf_array *array_elem;
  5864. if (vlen == 0)
  5865. goto error;
  5866. member = btf_type_member(t) + vlen - 1;
  5867. mtype = btf_type_skip_modifiers(btf, member->type,
  5868. NULL);
  5869. if (!btf_type_is_array(mtype))
  5870. goto error;
  5871. array_elem = (struct btf_array *)(mtype + 1);
  5872. if (array_elem->nelems != 0)
  5873. goto error;
  5874. moff = __btf_member_bit_offset(t, member) / 8;
  5875. if (off < moff)
  5876. goto error;
  5877. /* allow structure and integer */
  5878. t = btf_type_skip_modifiers(btf, array_elem->type,
  5879. NULL);
  5880. if (btf_type_is_int(t))
  5881. return WALK_SCALAR;
  5882. if (!btf_type_is_struct(t))
  5883. goto error;
  5884. off = (off - moff) % t->size;
  5885. goto again;
  5886. error:
  5887. bpf_log(log, "access beyond struct %s at off %u size %u\n",
  5888. tname, off, size);
  5889. return -EACCES;
  5890. }
  5891. for_each_member(i, t, member) {
  5892. /* offset of the field in bytes */
  5893. moff = __btf_member_bit_offset(t, member) / 8;
  5894. if (off + size <= moff)
  5895. /* won't find anything, field is already too far */
  5896. break;
  5897. if (__btf_member_bitfield_size(t, member)) {
  5898. u32 end_bit = __btf_member_bit_offset(t, member) +
  5899. __btf_member_bitfield_size(t, member);
  5900. /* off <= moff instead of off == moff because clang
  5901. * does not generate a BTF member for anonymous
  5902. * bitfield like the ":16" here:
  5903. * struct {
  5904. * int :16;
  5905. * int x:8;
  5906. * };
  5907. */
  5908. if (off <= moff &&
  5909. BITS_ROUNDUP_BYTES(end_bit) <= off + size)
  5910. return WALK_SCALAR;
  5911. /* off may be accessing a following member
  5912. *
  5913. * or
  5914. *
  5915. * Doing partial access at either end of this
  5916. * bitfield. Continue on this case also to
  5917. * treat it as not accessing this bitfield
  5918. * and eventually error out as field not
  5919. * found to keep it simple.
  5920. * It could be relaxed if there was a legit
  5921. * partial access case later.
  5922. */
  5923. continue;
  5924. }
  5925. /* In case of "off" is pointing to holes of a struct */
  5926. if (off < moff)
  5927. break;
  5928. /* type of the field */
  5929. mid = member->type;
  5930. mtype = btf_type_by_id(btf, member->type);
  5931. mname = __btf_name_by_offset(btf, member->name_off);
  5932. mtype = __btf_resolve_size(btf, mtype, &msize,
  5933. &elem_type, &elem_id, &total_nelems,
  5934. &mid);
  5935. if (IS_ERR(mtype)) {
  5936. bpf_log(log, "field %s doesn't have size\n", mname);
  5937. return -EFAULT;
  5938. }
  5939. mtrue_end = moff + msize;
  5940. if (off >= mtrue_end)
  5941. /* no overlap with member, keep iterating */
  5942. continue;
  5943. if (btf_type_is_array(mtype)) {
  5944. u32 elem_idx;
  5945. /* __btf_resolve_size() above helps to
  5946. * linearize a multi-dimensional array.
  5947. *
  5948. * The logic here is treating an array
  5949. * in a struct as the following way:
  5950. *
  5951. * struct outer {
  5952. * struct inner array[2][2];
  5953. * };
  5954. *
  5955. * looks like:
  5956. *
  5957. * struct outer {
  5958. * struct inner array_elem0;
  5959. * struct inner array_elem1;
  5960. * struct inner array_elem2;
  5961. * struct inner array_elem3;
  5962. * };
  5963. *
  5964. * When accessing outer->array[1][0], it moves
  5965. * moff to "array_elem2", set mtype to
  5966. * "struct inner", and msize also becomes
  5967. * sizeof(struct inner). Then most of the
  5968. * remaining logic will fall through without
  5969. * caring the current member is an array or
  5970. * not.
  5971. *
  5972. * Unlike mtype/msize/moff, mtrue_end does not
  5973. * change. The naming difference ("_true") tells
  5974. * that it is not always corresponding to
  5975. * the current mtype/msize/moff.
  5976. * It is the true end of the current
  5977. * member (i.e. array in this case). That
  5978. * will allow an int array to be accessed like
  5979. * a scratch space,
  5980. * i.e. allow access beyond the size of
  5981. * the array's element as long as it is
  5982. * within the mtrue_end boundary.
  5983. */
  5984. /* skip empty array */
  5985. if (moff == mtrue_end)
  5986. continue;
  5987. msize /= total_nelems;
  5988. elem_idx = (off - moff) / msize;
  5989. moff += elem_idx * msize;
  5990. mtype = elem_type;
  5991. mid = elem_id;
  5992. }
  5993. /* the 'off' we're looking for is either equal to start
  5994. * of this field or inside of this struct
  5995. */
  5996. if (btf_type_is_struct(mtype)) {
  5997. /* our field must be inside that union or struct */
  5998. t = mtype;
  5999. /* return if the offset matches the member offset */
  6000. if (off == moff) {
  6001. *next_btf_id = mid;
  6002. return WALK_STRUCT;
  6003. }
  6004. /* adjust offset we're looking for */
  6005. off -= moff;
  6006. goto again;
  6007. }
  6008. if (btf_type_is_ptr(mtype)) {
  6009. const struct btf_type *stype, *t;
  6010. enum bpf_type_flag tmp_flag = 0;
  6011. u32 id;
  6012. if (msize != size || off != moff) {
  6013. bpf_log(log,
  6014. "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
  6015. mname, moff, tname, off, size);
  6016. return -EACCES;
  6017. }
  6018. /* check type tag */
  6019. t = btf_type_by_id(btf, mtype->type);
  6020. if (btf_type_is_type_tag(t)) {
  6021. tag_value = __btf_name_by_offset(btf, t->name_off);
  6022. /* check __user tag */
  6023. if (strcmp(tag_value, "user") == 0)
  6024. tmp_flag = MEM_USER;
  6025. /* check __percpu tag */
  6026. if (strcmp(tag_value, "percpu") == 0)
  6027. tmp_flag = MEM_PERCPU;
  6028. /* check __rcu tag */
  6029. if (strcmp(tag_value, "rcu") == 0)
  6030. tmp_flag = MEM_RCU;
  6031. }
  6032. stype = btf_type_skip_modifiers(btf, mtype->type, &id);
  6033. if (btf_type_is_struct(stype)) {
  6034. *next_btf_id = id;
  6035. *flag |= tmp_flag;
  6036. if (field_name)
  6037. *field_name = mname;
  6038. return WALK_PTR;
  6039. }
  6040. }
  6041. /* Allow more flexible access within an int as long as
  6042. * it is within mtrue_end.
  6043. * Since mtrue_end could be the end of an array,
  6044. * that also allows using an array of int as a scratch
  6045. * space. e.g. skb->cb[].
  6046. */
  6047. if (off + size > mtrue_end && !(*flag & PTR_UNTRUSTED)) {
  6048. bpf_log(log,
  6049. "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
  6050. mname, mtrue_end, tname, off, size);
  6051. return -EACCES;
  6052. }
  6053. return WALK_SCALAR;
  6054. }
  6055. bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
  6056. return -EINVAL;
  6057. }
  6058. int btf_struct_access(struct bpf_verifier_log *log,
  6059. const struct bpf_reg_state *reg,
  6060. int off, int size, enum bpf_access_type atype __maybe_unused,
  6061. u32 *next_btf_id, enum bpf_type_flag *flag,
  6062. const char **field_name)
  6063. {
  6064. const struct btf *btf = reg->btf;
  6065. enum bpf_type_flag tmp_flag = 0;
  6066. const struct btf_type *t;
  6067. u32 id = reg->btf_id;
  6068. int err;
  6069. while (type_is_alloc(reg->type)) {
  6070. struct btf_struct_meta *meta;
  6071. struct btf_record *rec;
  6072. int i;
  6073. meta = btf_find_struct_meta(btf, id);
  6074. if (!meta)
  6075. break;
  6076. rec = meta->record;
  6077. for (i = 0; i < rec->cnt; i++) {
  6078. struct btf_field *field = &rec->fields[i];
  6079. u32 offset = field->offset;
  6080. if (off < offset + field->size && offset < off + size) {
  6081. bpf_log(log,
  6082. "direct access to %s is disallowed\n",
  6083. btf_field_type_name(field->type));
  6084. return -EACCES;
  6085. }
  6086. }
  6087. break;
  6088. }
  6089. t = btf_type_by_id(btf, id);
  6090. do {
  6091. err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag, field_name);
  6092. switch (err) {
  6093. case WALK_PTR:
  6094. /* For local types, the destination register cannot
  6095. * become a pointer again.
  6096. */
  6097. if (type_is_alloc(reg->type))
  6098. return SCALAR_VALUE;
  6099. /* If we found the pointer or scalar on t+off,
  6100. * we're done.
  6101. */
  6102. *next_btf_id = id;
  6103. *flag = tmp_flag;
  6104. return PTR_TO_BTF_ID;
  6105. case WALK_SCALAR:
  6106. return SCALAR_VALUE;
  6107. case WALK_STRUCT:
  6108. /* We found nested struct, so continue the search
  6109. * by diving in it. At this point the offset is
  6110. * aligned with the new type, so set it to 0.
  6111. */
  6112. t = btf_type_by_id(btf, id);
  6113. off = 0;
  6114. break;
  6115. default:
  6116. /* It's either error or unknown return value..
  6117. * scream and leave.
  6118. */
  6119. if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value"))
  6120. return -EINVAL;
  6121. return err;
  6122. }
  6123. } while (t);
  6124. return -EINVAL;
  6125. }
  6126. /* Check that two BTF types, each specified as an BTF object + id, are exactly
  6127. * the same. Trivial ID check is not enough due to module BTFs, because we can
  6128. * end up with two different module BTFs, but IDs point to the common type in
  6129. * vmlinux BTF.
  6130. */
  6131. bool btf_types_are_same(const struct btf *btf1, u32 id1,
  6132. const struct btf *btf2, u32 id2)
  6133. {
  6134. if (id1 != id2)
  6135. return false;
  6136. if (btf1 == btf2)
  6137. return true;
  6138. return btf_type_by_id(btf1, id1) == btf_type_by_id(btf2, id2);
  6139. }
  6140. bool btf_struct_ids_match(struct bpf_verifier_log *log,
  6141. const struct btf *btf, u32 id, int off,
  6142. const struct btf *need_btf, u32 need_type_id,
  6143. bool strict)
  6144. {
  6145. const struct btf_type *type;
  6146. enum bpf_type_flag flag = 0;
  6147. int err;
  6148. /* Are we already done? */
  6149. if (off == 0 && btf_types_are_same(btf, id, need_btf, need_type_id))
  6150. return true;
  6151. /* In case of strict type match, we do not walk struct, the top level
  6152. * type match must succeed. When strict is true, off should have already
  6153. * been 0.
  6154. */
  6155. if (strict)
  6156. return false;
  6157. again:
  6158. type = btf_type_by_id(btf, id);
  6159. if (!type)
  6160. return false;
  6161. err = btf_struct_walk(log, btf, type, off, 1, &id, &flag, NULL);
  6162. if (err != WALK_STRUCT)
  6163. return false;
  6164. /* We found nested struct object. If it matches
  6165. * the requested ID, we're done. Otherwise let's
  6166. * continue the search with offset 0 in the new
  6167. * type.
  6168. */
  6169. if (!btf_types_are_same(btf, id, need_btf, need_type_id)) {
  6170. off = 0;
  6171. goto again;
  6172. }
  6173. return true;
  6174. }
  6175. static int __get_type_size(struct btf *btf, u32 btf_id,
  6176. const struct btf_type **ret_type)
  6177. {
  6178. const struct btf_type *t;
  6179. *ret_type = btf_type_by_id(btf, 0);
  6180. if (!btf_id)
  6181. /* void */
  6182. return 0;
  6183. t = btf_type_by_id(btf, btf_id);
  6184. while (t && btf_type_is_modifier(t))
  6185. t = btf_type_by_id(btf, t->type);
  6186. if (!t)
  6187. return -EINVAL;
  6188. *ret_type = t;
  6189. if (btf_type_is_ptr(t))
  6190. /* kernel size of pointer. Not BPF's size of pointer*/
  6191. return sizeof(void *);
  6192. if (btf_type_is_int(t) || btf_is_any_enum(t) || __btf_type_is_struct(t))
  6193. return t->size;
  6194. return -EINVAL;
  6195. }
  6196. static u8 __get_type_fmodel_flags(const struct btf_type *t)
  6197. {
  6198. u8 flags = 0;
  6199. if (__btf_type_is_struct(t))
  6200. flags |= BTF_FMODEL_STRUCT_ARG;
  6201. if (btf_type_is_signed_int(t))
  6202. flags |= BTF_FMODEL_SIGNED_ARG;
  6203. return flags;
  6204. }
  6205. int btf_distill_func_proto(struct bpf_verifier_log *log,
  6206. struct btf *btf,
  6207. const struct btf_type *func,
  6208. const char *tname,
  6209. struct btf_func_model *m)
  6210. {
  6211. const struct btf_param *args;
  6212. const struct btf_type *t;
  6213. u32 i, nargs;
  6214. int ret;
  6215. if (!func) {
  6216. /* BTF function prototype doesn't match the verifier types.
  6217. * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args.
  6218. */
  6219. for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
  6220. m->arg_size[i] = 8;
  6221. m->arg_flags[i] = 0;
  6222. }
  6223. m->ret_size = 8;
  6224. m->ret_flags = 0;
  6225. m->nr_args = MAX_BPF_FUNC_REG_ARGS;
  6226. return 0;
  6227. }
  6228. args = (const struct btf_param *)(func + 1);
  6229. nargs = btf_type_vlen(func);
  6230. if (nargs > MAX_BPF_FUNC_ARGS) {
  6231. bpf_log(log,
  6232. "The function %s has %d arguments. Too many.\n",
  6233. tname, nargs);
  6234. return -EINVAL;
  6235. }
  6236. ret = __get_type_size(btf, func->type, &t);
  6237. if (ret < 0 || __btf_type_is_struct(t)) {
  6238. bpf_log(log,
  6239. "The function %s return type %s is unsupported.\n",
  6240. tname, btf_type_str(t));
  6241. return -EINVAL;
  6242. }
  6243. m->ret_size = ret;
  6244. m->ret_flags = __get_type_fmodel_flags(t);
  6245. for (i = 0; i < nargs; i++) {
  6246. if (i == nargs - 1 && args[i].type == 0) {
  6247. bpf_log(log,
  6248. "The function %s with variable args is unsupported.\n",
  6249. tname);
  6250. return -EINVAL;
  6251. }
  6252. ret = __get_type_size(btf, args[i].type, &t);
  6253. /* No support of struct argument size greater than 16 bytes */
  6254. if (ret < 0 || ret > 16) {
  6255. bpf_log(log,
  6256. "The function %s arg%d type %s is unsupported.\n",
  6257. tname, i, btf_type_str(t));
  6258. return -EINVAL;
  6259. }
  6260. if (ret == 0) {
  6261. bpf_log(log,
  6262. "The function %s has malformed void argument.\n",
  6263. tname);
  6264. return -EINVAL;
  6265. }
  6266. m->arg_size[i] = ret;
  6267. m->arg_flags[i] = __get_type_fmodel_flags(t);
  6268. }
  6269. m->nr_args = nargs;
  6270. return 0;
  6271. }
  6272. /* Compare BTFs of two functions assuming only scalars and pointers to context.
  6273. * t1 points to BTF_KIND_FUNC in btf1
  6274. * t2 points to BTF_KIND_FUNC in btf2
  6275. * Returns:
  6276. * EINVAL - function prototype mismatch
  6277. * EFAULT - verifier bug
  6278. * 0 - 99% match. The last 1% is validated by the verifier.
  6279. */
  6280. static int btf_check_func_type_match(struct bpf_verifier_log *log,
  6281. struct btf *btf1, const struct btf_type *t1,
  6282. struct btf *btf2, const struct btf_type *t2)
  6283. {
  6284. const struct btf_param *args1, *args2;
  6285. const char *fn1, *fn2, *s1, *s2;
  6286. u32 nargs1, nargs2, i;
  6287. fn1 = btf_name_by_offset(btf1, t1->name_off);
  6288. fn2 = btf_name_by_offset(btf2, t2->name_off);
  6289. if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
  6290. bpf_log(log, "%s() is not a global function\n", fn1);
  6291. return -EINVAL;
  6292. }
  6293. if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
  6294. bpf_log(log, "%s() is not a global function\n", fn2);
  6295. return -EINVAL;
  6296. }
  6297. t1 = btf_type_by_id(btf1, t1->type);
  6298. if (!t1 || !btf_type_is_func_proto(t1))
  6299. return -EFAULT;
  6300. t2 = btf_type_by_id(btf2, t2->type);
  6301. if (!t2 || !btf_type_is_func_proto(t2))
  6302. return -EFAULT;
  6303. args1 = (const struct btf_param *)(t1 + 1);
  6304. nargs1 = btf_type_vlen(t1);
  6305. args2 = (const struct btf_param *)(t2 + 1);
  6306. nargs2 = btf_type_vlen(t2);
  6307. if (nargs1 != nargs2) {
  6308. bpf_log(log, "%s() has %d args while %s() has %d args\n",
  6309. fn1, nargs1, fn2, nargs2);
  6310. return -EINVAL;
  6311. }
  6312. t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
  6313. t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
  6314. if (t1->info != t2->info) {
  6315. bpf_log(log,
  6316. "Return type %s of %s() doesn't match type %s of %s()\n",
  6317. btf_type_str(t1), fn1,
  6318. btf_type_str(t2), fn2);
  6319. return -EINVAL;
  6320. }
  6321. for (i = 0; i < nargs1; i++) {
  6322. t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
  6323. t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
  6324. if (t1->info != t2->info) {
  6325. bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
  6326. i, fn1, btf_type_str(t1),
  6327. fn2, btf_type_str(t2));
  6328. return -EINVAL;
  6329. }
  6330. if (btf_type_has_size(t1) && t1->size != t2->size) {
  6331. bpf_log(log,
  6332. "arg%d in %s() has size %d while %s() has %d\n",
  6333. i, fn1, t1->size,
  6334. fn2, t2->size);
  6335. return -EINVAL;
  6336. }
  6337. /* global functions are validated with scalars and pointers
  6338. * to context only. And only global functions can be replaced.
  6339. * Hence type check only those types.
  6340. */
  6341. if (btf_type_is_int(t1) || btf_is_any_enum(t1))
  6342. continue;
  6343. if (!btf_type_is_ptr(t1)) {
  6344. bpf_log(log,
  6345. "arg%d in %s() has unrecognized type\n",
  6346. i, fn1);
  6347. return -EINVAL;
  6348. }
  6349. t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
  6350. t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
  6351. if (!btf_type_is_struct(t1)) {
  6352. bpf_log(log,
  6353. "arg%d in %s() is not a pointer to context\n",
  6354. i, fn1);
  6355. return -EINVAL;
  6356. }
  6357. if (!btf_type_is_struct(t2)) {
  6358. bpf_log(log,
  6359. "arg%d in %s() is not a pointer to context\n",
  6360. i, fn2);
  6361. return -EINVAL;
  6362. }
  6363. /* This is an optional check to make program writing easier.
  6364. * Compare names of structs and report an error to the user.
  6365. * btf_prepare_func_args() already checked that t2 struct
  6366. * is a context type. btf_prepare_func_args() will check
  6367. * later that t1 struct is a context type as well.
  6368. */
  6369. s1 = btf_name_by_offset(btf1, t1->name_off);
  6370. s2 = btf_name_by_offset(btf2, t2->name_off);
  6371. if (strcmp(s1, s2)) {
  6372. bpf_log(log,
  6373. "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
  6374. i, fn1, s1, fn2, s2);
  6375. return -EINVAL;
  6376. }
  6377. }
  6378. return 0;
  6379. }
  6380. /* Compare BTFs of given program with BTF of target program */
  6381. int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
  6382. struct btf *btf2, const struct btf_type *t2)
  6383. {
  6384. struct btf *btf1 = prog->aux->btf;
  6385. const struct btf_type *t1;
  6386. u32 btf_id = 0;
  6387. if (!prog->aux->func_info) {
  6388. bpf_log(log, "Program extension requires BTF\n");
  6389. return -EINVAL;
  6390. }
  6391. btf_id = prog->aux->func_info[0].type_id;
  6392. if (!btf_id)
  6393. return -EFAULT;
  6394. t1 = btf_type_by_id(btf1, btf_id);
  6395. if (!t1 || !btf_type_is_func(t1))
  6396. return -EFAULT;
  6397. return btf_check_func_type_match(log, btf1, t1, btf2, t2);
  6398. }
  6399. static bool btf_is_dynptr_ptr(const struct btf *btf, const struct btf_type *t)
  6400. {
  6401. const char *name;
  6402. t = btf_type_by_id(btf, t->type); /* skip PTR */
  6403. while (btf_type_is_modifier(t))
  6404. t = btf_type_by_id(btf, t->type);
  6405. /* allow either struct or struct forward declaration */
  6406. if (btf_type_is_struct(t) ||
  6407. (btf_type_is_fwd(t) && btf_type_kflag(t) == 0)) {
  6408. name = btf_str_by_offset(btf, t->name_off);
  6409. return name && strcmp(name, "bpf_dynptr") == 0;
  6410. }
  6411. return false;
  6412. }
  6413. struct bpf_cand_cache {
  6414. const char *name;
  6415. u32 name_len;
  6416. u16 kind;
  6417. u16 cnt;
  6418. struct {
  6419. const struct btf *btf;
  6420. u32 id;
  6421. } cands[];
  6422. };
  6423. static DEFINE_MUTEX(cand_cache_mutex);
  6424. static struct bpf_cand_cache *
  6425. bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id);
  6426. static int btf_get_ptr_to_btf_id(struct bpf_verifier_log *log, int arg_idx,
  6427. const struct btf *btf, const struct btf_type *t)
  6428. {
  6429. struct bpf_cand_cache *cc;
  6430. struct bpf_core_ctx ctx = {
  6431. .btf = btf,
  6432. .log = log,
  6433. };
  6434. u32 kern_type_id, type_id;
  6435. int err = 0;
  6436. /* skip PTR and modifiers */
  6437. type_id = t->type;
  6438. t = btf_type_by_id(btf, t->type);
  6439. while (btf_type_is_modifier(t)) {
  6440. type_id = t->type;
  6441. t = btf_type_by_id(btf, t->type);
  6442. }
  6443. mutex_lock(&cand_cache_mutex);
  6444. cc = bpf_core_find_cands(&ctx, type_id);
  6445. if (IS_ERR(cc)) {
  6446. err = PTR_ERR(cc);
  6447. bpf_log(log, "arg#%d reference type('%s %s') candidate matching error: %d\n",
  6448. arg_idx, btf_type_str(t), __btf_name_by_offset(btf, t->name_off),
  6449. err);
  6450. goto cand_cache_unlock;
  6451. }
  6452. if (cc->cnt != 1) {
  6453. bpf_log(log, "arg#%d reference type('%s %s') %s\n",
  6454. arg_idx, btf_type_str(t), __btf_name_by_offset(btf, t->name_off),
  6455. cc->cnt == 0 ? "has no matches" : "is ambiguous");
  6456. err = cc->cnt == 0 ? -ENOENT : -ESRCH;
  6457. goto cand_cache_unlock;
  6458. }
  6459. if (btf_is_module(cc->cands[0].btf)) {
  6460. bpf_log(log, "arg#%d reference type('%s %s') points to kernel module type (unsupported)\n",
  6461. arg_idx, btf_type_str(t), __btf_name_by_offset(btf, t->name_off));
  6462. err = -EOPNOTSUPP;
  6463. goto cand_cache_unlock;
  6464. }
  6465. kern_type_id = cc->cands[0].id;
  6466. cand_cache_unlock:
  6467. mutex_unlock(&cand_cache_mutex);
  6468. if (err)
  6469. return err;
  6470. return kern_type_id;
  6471. }
  6472. enum btf_arg_tag {
  6473. ARG_TAG_CTX = BIT_ULL(0),
  6474. ARG_TAG_NONNULL = BIT_ULL(1),
  6475. ARG_TAG_TRUSTED = BIT_ULL(2),
  6476. ARG_TAG_NULLABLE = BIT_ULL(3),
  6477. ARG_TAG_ARENA = BIT_ULL(4),
  6478. };
  6479. /* Process BTF of a function to produce high-level expectation of function
  6480. * arguments (like ARG_PTR_TO_CTX, or ARG_PTR_TO_MEM, etc). This information
  6481. * is cached in subprog info for reuse.
  6482. * Returns:
  6483. * EFAULT - there is a verifier bug. Abort verification.
  6484. * EINVAL - cannot convert BTF.
  6485. * 0 - Successfully processed BTF and constructed argument expectations.
  6486. */
  6487. int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
  6488. {
  6489. bool is_global = subprog_aux(env, subprog)->linkage == BTF_FUNC_GLOBAL;
  6490. struct bpf_subprog_info *sub = subprog_info(env, subprog);
  6491. struct bpf_verifier_log *log = &env->log;
  6492. struct bpf_prog *prog = env->prog;
  6493. enum bpf_prog_type prog_type = prog->type;
  6494. struct btf *btf = prog->aux->btf;
  6495. const struct btf_param *args;
  6496. const struct btf_type *t, *ref_t, *fn_t;
  6497. u32 i, nargs, btf_id;
  6498. const char *tname;
  6499. if (sub->args_cached)
  6500. return 0;
  6501. if (!prog->aux->func_info) {
  6502. bpf_log(log, "Verifier bug\n");
  6503. return -EFAULT;
  6504. }
  6505. btf_id = prog->aux->func_info[subprog].type_id;
  6506. if (!btf_id) {
  6507. if (!is_global) /* not fatal for static funcs */
  6508. return -EINVAL;
  6509. bpf_log(log, "Global functions need valid BTF\n");
  6510. return -EFAULT;
  6511. }
  6512. fn_t = btf_type_by_id(btf, btf_id);
  6513. if (!fn_t || !btf_type_is_func(fn_t)) {
  6514. /* These checks were already done by the verifier while loading
  6515. * struct bpf_func_info
  6516. */
  6517. bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
  6518. subprog);
  6519. return -EFAULT;
  6520. }
  6521. tname = btf_name_by_offset(btf, fn_t->name_off);
  6522. if (prog->aux->func_info_aux[subprog].unreliable) {
  6523. bpf_log(log, "Verifier bug in function %s()\n", tname);
  6524. return -EFAULT;
  6525. }
  6526. if (prog_type == BPF_PROG_TYPE_EXT)
  6527. prog_type = prog->aux->dst_prog->type;
  6528. t = btf_type_by_id(btf, fn_t->type);
  6529. if (!t || !btf_type_is_func_proto(t)) {
  6530. bpf_log(log, "Invalid type of function %s()\n", tname);
  6531. return -EFAULT;
  6532. }
  6533. args = (const struct btf_param *)(t + 1);
  6534. nargs = btf_type_vlen(t);
  6535. if (nargs > MAX_BPF_FUNC_REG_ARGS) {
  6536. if (!is_global)
  6537. return -EINVAL;
  6538. bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n",
  6539. tname, nargs, MAX_BPF_FUNC_REG_ARGS);
  6540. return -EINVAL;
  6541. }
  6542. /* check that function returns int, exception cb also requires this */
  6543. t = btf_type_by_id(btf, t->type);
  6544. while (btf_type_is_modifier(t))
  6545. t = btf_type_by_id(btf, t->type);
  6546. if (!btf_type_is_int(t) && !btf_is_any_enum(t)) {
  6547. if (!is_global)
  6548. return -EINVAL;
  6549. bpf_log(log,
  6550. "Global function %s() doesn't return scalar. Only those are supported.\n",
  6551. tname);
  6552. return -EINVAL;
  6553. }
  6554. /* Convert BTF function arguments into verifier types.
  6555. * Only PTR_TO_CTX and SCALAR are supported atm.
  6556. */
  6557. for (i = 0; i < nargs; i++) {
  6558. u32 tags = 0;
  6559. int id = 0;
  6560. /* 'arg:<tag>' decl_tag takes precedence over derivation of
  6561. * register type from BTF type itself
  6562. */
  6563. while ((id = btf_find_next_decl_tag(btf, fn_t, i, "arg:", id)) > 0) {
  6564. const struct btf_type *tag_t = btf_type_by_id(btf, id);
  6565. const char *tag = __btf_name_by_offset(btf, tag_t->name_off) + 4;
  6566. /* disallow arg tags in static subprogs */
  6567. if (!is_global) {
  6568. bpf_log(log, "arg#%d type tag is not supported in static functions\n", i);
  6569. return -EOPNOTSUPP;
  6570. }
  6571. if (strcmp(tag, "ctx") == 0) {
  6572. tags |= ARG_TAG_CTX;
  6573. } else if (strcmp(tag, "trusted") == 0) {
  6574. tags |= ARG_TAG_TRUSTED;
  6575. } else if (strcmp(tag, "nonnull") == 0) {
  6576. tags |= ARG_TAG_NONNULL;
  6577. } else if (strcmp(tag, "nullable") == 0) {
  6578. tags |= ARG_TAG_NULLABLE;
  6579. } else if (strcmp(tag, "arena") == 0) {
  6580. tags |= ARG_TAG_ARENA;
  6581. } else {
  6582. bpf_log(log, "arg#%d has unsupported set of tags\n", i);
  6583. return -EOPNOTSUPP;
  6584. }
  6585. }
  6586. if (id != -ENOENT) {
  6587. bpf_log(log, "arg#%d type tag fetching failure: %d\n", i, id);
  6588. return id;
  6589. }
  6590. t = btf_type_by_id(btf, args[i].type);
  6591. while (btf_type_is_modifier(t))
  6592. t = btf_type_by_id(btf, t->type);
  6593. if (!btf_type_is_ptr(t))
  6594. goto skip_pointer;
  6595. if ((tags & ARG_TAG_CTX) || btf_is_prog_ctx_type(log, btf, t, prog_type, i)) {
  6596. if (tags & ~ARG_TAG_CTX) {
  6597. bpf_log(log, "arg#%d has invalid combination of tags\n", i);
  6598. return -EINVAL;
  6599. }
  6600. if ((tags & ARG_TAG_CTX) &&
  6601. btf_validate_prog_ctx_type(log, btf, t, i, prog_type,
  6602. prog->expected_attach_type))
  6603. return -EINVAL;
  6604. sub->args[i].arg_type = ARG_PTR_TO_CTX;
  6605. continue;
  6606. }
  6607. if (btf_is_dynptr_ptr(btf, t)) {
  6608. if (tags) {
  6609. bpf_log(log, "arg#%d has invalid combination of tags\n", i);
  6610. return -EINVAL;
  6611. }
  6612. sub->args[i].arg_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY;
  6613. continue;
  6614. }
  6615. if (tags & ARG_TAG_TRUSTED) {
  6616. int kern_type_id;
  6617. if (tags & ARG_TAG_NONNULL) {
  6618. bpf_log(log, "arg#%d has invalid combination of tags\n", i);
  6619. return -EINVAL;
  6620. }
  6621. kern_type_id = btf_get_ptr_to_btf_id(log, i, btf, t);
  6622. if (kern_type_id < 0)
  6623. return kern_type_id;
  6624. sub->args[i].arg_type = ARG_PTR_TO_BTF_ID | PTR_TRUSTED;
  6625. if (tags & ARG_TAG_NULLABLE)
  6626. sub->args[i].arg_type |= PTR_MAYBE_NULL;
  6627. sub->args[i].btf_id = kern_type_id;
  6628. continue;
  6629. }
  6630. if (tags & ARG_TAG_ARENA) {
  6631. if (tags & ~ARG_TAG_ARENA) {
  6632. bpf_log(log, "arg#%d arena cannot be combined with any other tags\n", i);
  6633. return -EINVAL;
  6634. }
  6635. sub->args[i].arg_type = ARG_PTR_TO_ARENA;
  6636. continue;
  6637. }
  6638. if (is_global) { /* generic user data pointer */
  6639. u32 mem_size;
  6640. if (tags & ARG_TAG_NULLABLE) {
  6641. bpf_log(log, "arg#%d has invalid combination of tags\n", i);
  6642. return -EINVAL;
  6643. }
  6644. t = btf_type_skip_modifiers(btf, t->type, NULL);
  6645. ref_t = btf_resolve_size(btf, t, &mem_size);
  6646. if (IS_ERR(ref_t)) {
  6647. bpf_log(log, "arg#%d reference type('%s %s') size cannot be determined: %ld\n",
  6648. i, btf_type_str(t), btf_name_by_offset(btf, t->name_off),
  6649. PTR_ERR(ref_t));
  6650. return -EINVAL;
  6651. }
  6652. sub->args[i].arg_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL;
  6653. if (tags & ARG_TAG_NONNULL)
  6654. sub->args[i].arg_type &= ~PTR_MAYBE_NULL;
  6655. sub->args[i].mem_size = mem_size;
  6656. continue;
  6657. }
  6658. skip_pointer:
  6659. if (tags) {
  6660. bpf_log(log, "arg#%d has pointer tag, but is not a pointer type\n", i);
  6661. return -EINVAL;
  6662. }
  6663. if (btf_type_is_int(t) || btf_is_any_enum(t)) {
  6664. sub->args[i].arg_type = ARG_ANYTHING;
  6665. continue;
  6666. }
  6667. if (!is_global)
  6668. return -EINVAL;
  6669. bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
  6670. i, btf_type_str(t), tname);
  6671. return -EINVAL;
  6672. }
  6673. sub->arg_cnt = nargs;
  6674. sub->args_cached = true;
  6675. return 0;
  6676. }
  6677. static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
  6678. struct btf_show *show)
  6679. {
  6680. const struct btf_type *t = btf_type_by_id(btf, type_id);
  6681. show->btf = btf;
  6682. memset(&show->state, 0, sizeof(show->state));
  6683. memset(&show->obj, 0, sizeof(show->obj));
  6684. btf_type_ops(t)->show(btf, t, type_id, obj, 0, show);
  6685. }
  6686. __printf(2, 0) static void btf_seq_show(struct btf_show *show, const char *fmt,
  6687. va_list args)
  6688. {
  6689. seq_vprintf((struct seq_file *)show->target, fmt, args);
  6690. }
  6691. int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
  6692. void *obj, struct seq_file *m, u64 flags)
  6693. {
  6694. struct btf_show sseq;
  6695. sseq.target = m;
  6696. sseq.showfn = btf_seq_show;
  6697. sseq.flags = flags;
  6698. btf_type_show(btf, type_id, obj, &sseq);
  6699. return sseq.state.status;
  6700. }
  6701. void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  6702. struct seq_file *m)
  6703. {
  6704. (void) btf_type_seq_show_flags(btf, type_id, obj, m,
  6705. BTF_SHOW_NONAME | BTF_SHOW_COMPACT |
  6706. BTF_SHOW_ZERO | BTF_SHOW_UNSAFE);
  6707. }
  6708. struct btf_show_snprintf {
  6709. struct btf_show show;
  6710. int len_left; /* space left in string */
  6711. int len; /* length we would have written */
  6712. };
  6713. __printf(2, 0) static void btf_snprintf_show(struct btf_show *show, const char *fmt,
  6714. va_list args)
  6715. {
  6716. struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show;
  6717. int len;
  6718. len = vsnprintf(show->target, ssnprintf->len_left, fmt, args);
  6719. if (len < 0) {
  6720. ssnprintf->len_left = 0;
  6721. ssnprintf->len = len;
  6722. } else if (len >= ssnprintf->len_left) {
  6723. /* no space, drive on to get length we would have written */
  6724. ssnprintf->len_left = 0;
  6725. ssnprintf->len += len;
  6726. } else {
  6727. ssnprintf->len_left -= len;
  6728. ssnprintf->len += len;
  6729. show->target += len;
  6730. }
  6731. }
  6732. int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
  6733. char *buf, int len, u64 flags)
  6734. {
  6735. struct btf_show_snprintf ssnprintf;
  6736. ssnprintf.show.target = buf;
  6737. ssnprintf.show.flags = flags;
  6738. ssnprintf.show.showfn = btf_snprintf_show;
  6739. ssnprintf.len_left = len;
  6740. ssnprintf.len = 0;
  6741. btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf);
  6742. /* If we encountered an error, return it. */
  6743. if (ssnprintf.show.state.status)
  6744. return ssnprintf.show.state.status;
  6745. /* Otherwise return length we would have written */
  6746. return ssnprintf.len;
  6747. }
  6748. #ifdef CONFIG_PROC_FS
  6749. static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
  6750. {
  6751. const struct btf *btf = filp->private_data;
  6752. seq_printf(m, "btf_id:\t%u\n", btf->id);
  6753. }
  6754. #endif
  6755. static int btf_release(struct inode *inode, struct file *filp)
  6756. {
  6757. btf_put(filp->private_data);
  6758. return 0;
  6759. }
  6760. const struct file_operations btf_fops = {
  6761. #ifdef CONFIG_PROC_FS
  6762. .show_fdinfo = bpf_btf_show_fdinfo,
  6763. #endif
  6764. .release = btf_release,
  6765. };
  6766. static int __btf_new_fd(struct btf *btf)
  6767. {
  6768. return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
  6769. }
  6770. int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
  6771. {
  6772. struct btf *btf;
  6773. int ret;
  6774. btf = btf_parse(attr, uattr, uattr_size);
  6775. if (IS_ERR(btf))
  6776. return PTR_ERR(btf);
  6777. ret = btf_alloc_id(btf);
  6778. if (ret) {
  6779. btf_free(btf);
  6780. return ret;
  6781. }
  6782. /*
  6783. * The BTF ID is published to the userspace.
  6784. * All BTF free must go through call_rcu() from
  6785. * now on (i.e. free by calling btf_put()).
  6786. */
  6787. ret = __btf_new_fd(btf);
  6788. if (ret < 0)
  6789. btf_put(btf);
  6790. return ret;
  6791. }
  6792. struct btf *btf_get_by_fd(int fd)
  6793. {
  6794. struct btf *btf;
  6795. CLASS(fd, f)(fd);
  6796. if (fd_empty(f))
  6797. return ERR_PTR(-EBADF);
  6798. if (fd_file(f)->f_op != &btf_fops)
  6799. return ERR_PTR(-EINVAL);
  6800. btf = fd_file(f)->private_data;
  6801. refcount_inc(&btf->refcnt);
  6802. return btf;
  6803. }
  6804. int btf_get_info_by_fd(const struct btf *btf,
  6805. const union bpf_attr *attr,
  6806. union bpf_attr __user *uattr)
  6807. {
  6808. struct bpf_btf_info __user *uinfo;
  6809. struct bpf_btf_info info;
  6810. u32 info_copy, btf_copy;
  6811. void __user *ubtf;
  6812. char __user *uname;
  6813. u32 uinfo_len, uname_len, name_len;
  6814. int ret = 0;
  6815. uinfo = u64_to_user_ptr(attr->info.info);
  6816. uinfo_len = attr->info.info_len;
  6817. info_copy = min_t(u32, uinfo_len, sizeof(info));
  6818. memset(&info, 0, sizeof(info));
  6819. if (copy_from_user(&info, uinfo, info_copy))
  6820. return -EFAULT;
  6821. info.id = btf->id;
  6822. ubtf = u64_to_user_ptr(info.btf);
  6823. btf_copy = min_t(u32, btf->data_size, info.btf_size);
  6824. if (copy_to_user(ubtf, btf->data, btf_copy))
  6825. return -EFAULT;
  6826. info.btf_size = btf->data_size;
  6827. info.kernel_btf = btf->kernel_btf;
  6828. uname = u64_to_user_ptr(info.name);
  6829. uname_len = info.name_len;
  6830. if (!uname ^ !uname_len)
  6831. return -EINVAL;
  6832. name_len = strlen(btf->name);
  6833. info.name_len = name_len;
  6834. if (uname) {
  6835. if (uname_len >= name_len + 1) {
  6836. if (copy_to_user(uname, btf->name, name_len + 1))
  6837. return -EFAULT;
  6838. } else {
  6839. char zero = '\0';
  6840. if (copy_to_user(uname, btf->name, uname_len - 1))
  6841. return -EFAULT;
  6842. if (put_user(zero, uname + uname_len - 1))
  6843. return -EFAULT;
  6844. /* let user-space know about too short buffer */
  6845. ret = -ENOSPC;
  6846. }
  6847. }
  6848. if (copy_to_user(uinfo, &info, info_copy) ||
  6849. put_user(info_copy, &uattr->info.info_len))
  6850. return -EFAULT;
  6851. return ret;
  6852. }
  6853. int btf_get_fd_by_id(u32 id)
  6854. {
  6855. struct btf *btf;
  6856. int fd;
  6857. rcu_read_lock();
  6858. btf = idr_find(&btf_idr, id);
  6859. if (!btf || !refcount_inc_not_zero(&btf->refcnt))
  6860. btf = ERR_PTR(-ENOENT);
  6861. rcu_read_unlock();
  6862. if (IS_ERR(btf))
  6863. return PTR_ERR(btf);
  6864. fd = __btf_new_fd(btf);
  6865. if (fd < 0)
  6866. btf_put(btf);
  6867. return fd;
  6868. }
  6869. u32 btf_obj_id(const struct btf *btf)
  6870. {
  6871. return btf->id;
  6872. }
  6873. bool btf_is_kernel(const struct btf *btf)
  6874. {
  6875. return btf->kernel_btf;
  6876. }
  6877. bool btf_is_module(const struct btf *btf)
  6878. {
  6879. return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
  6880. }
  6881. enum {
  6882. BTF_MODULE_F_LIVE = (1 << 0),
  6883. };
  6884. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  6885. struct btf_module {
  6886. struct list_head list;
  6887. struct module *module;
  6888. struct btf *btf;
  6889. struct bin_attribute *sysfs_attr;
  6890. int flags;
  6891. };
  6892. static LIST_HEAD(btf_modules);
  6893. static DEFINE_MUTEX(btf_module_mutex);
  6894. static ssize_t
  6895. btf_module_read(struct file *file, struct kobject *kobj,
  6896. struct bin_attribute *bin_attr,
  6897. char *buf, loff_t off, size_t len)
  6898. {
  6899. const struct btf *btf = bin_attr->private;
  6900. memcpy(buf, btf->data + off, len);
  6901. return len;
  6902. }
  6903. static void purge_cand_cache(struct btf *btf);
  6904. static int btf_module_notify(struct notifier_block *nb, unsigned long op,
  6905. void *module)
  6906. {
  6907. struct btf_module *btf_mod, *tmp;
  6908. struct module *mod = module;
  6909. struct btf *btf;
  6910. int err = 0;
  6911. if (mod->btf_data_size == 0 ||
  6912. (op != MODULE_STATE_COMING && op != MODULE_STATE_LIVE &&
  6913. op != MODULE_STATE_GOING))
  6914. goto out;
  6915. switch (op) {
  6916. case MODULE_STATE_COMING:
  6917. btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL);
  6918. if (!btf_mod) {
  6919. err = -ENOMEM;
  6920. goto out;
  6921. }
  6922. btf = btf_parse_module(mod->name, mod->btf_data, mod->btf_data_size,
  6923. mod->btf_base_data, mod->btf_base_data_size);
  6924. if (IS_ERR(btf)) {
  6925. kfree(btf_mod);
  6926. if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
  6927. pr_warn("failed to validate module [%s] BTF: %ld\n",
  6928. mod->name, PTR_ERR(btf));
  6929. err = PTR_ERR(btf);
  6930. } else {
  6931. pr_warn_once("Kernel module BTF mismatch detected, BTF debug info may be unavailable for some modules\n");
  6932. }
  6933. goto out;
  6934. }
  6935. err = btf_alloc_id(btf);
  6936. if (err) {
  6937. btf_free(btf);
  6938. kfree(btf_mod);
  6939. goto out;
  6940. }
  6941. purge_cand_cache(NULL);
  6942. mutex_lock(&btf_module_mutex);
  6943. btf_mod->module = module;
  6944. btf_mod->btf = btf;
  6945. list_add(&btf_mod->list, &btf_modules);
  6946. mutex_unlock(&btf_module_mutex);
  6947. if (IS_ENABLED(CONFIG_SYSFS)) {
  6948. struct bin_attribute *attr;
  6949. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  6950. if (!attr)
  6951. goto out;
  6952. sysfs_bin_attr_init(attr);
  6953. attr->attr.name = btf->name;
  6954. attr->attr.mode = 0444;
  6955. attr->size = btf->data_size;
  6956. attr->private = btf;
  6957. attr->read = btf_module_read;
  6958. err = sysfs_create_bin_file(btf_kobj, attr);
  6959. if (err) {
  6960. pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
  6961. mod->name, err);
  6962. kfree(attr);
  6963. err = 0;
  6964. goto out;
  6965. }
  6966. btf_mod->sysfs_attr = attr;
  6967. }
  6968. break;
  6969. case MODULE_STATE_LIVE:
  6970. mutex_lock(&btf_module_mutex);
  6971. list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
  6972. if (btf_mod->module != module)
  6973. continue;
  6974. btf_mod->flags |= BTF_MODULE_F_LIVE;
  6975. break;
  6976. }
  6977. mutex_unlock(&btf_module_mutex);
  6978. break;
  6979. case MODULE_STATE_GOING:
  6980. mutex_lock(&btf_module_mutex);
  6981. list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
  6982. if (btf_mod->module != module)
  6983. continue;
  6984. list_del(&btf_mod->list);
  6985. if (btf_mod->sysfs_attr)
  6986. sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
  6987. purge_cand_cache(btf_mod->btf);
  6988. btf_put(btf_mod->btf);
  6989. kfree(btf_mod->sysfs_attr);
  6990. kfree(btf_mod);
  6991. break;
  6992. }
  6993. mutex_unlock(&btf_module_mutex);
  6994. break;
  6995. }
  6996. out:
  6997. return notifier_from_errno(err);
  6998. }
  6999. static struct notifier_block btf_module_nb = {
  7000. .notifier_call = btf_module_notify,
  7001. };
  7002. static int __init btf_module_init(void)
  7003. {
  7004. register_module_notifier(&btf_module_nb);
  7005. return 0;
  7006. }
  7007. fs_initcall(btf_module_init);
  7008. #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */
  7009. struct module *btf_try_get_module(const struct btf *btf)
  7010. {
  7011. struct module *res = NULL;
  7012. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  7013. struct btf_module *btf_mod, *tmp;
  7014. mutex_lock(&btf_module_mutex);
  7015. list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
  7016. if (btf_mod->btf != btf)
  7017. continue;
  7018. /* We must only consider module whose __init routine has
  7019. * finished, hence we must check for BTF_MODULE_F_LIVE flag,
  7020. * which is set from the notifier callback for
  7021. * MODULE_STATE_LIVE.
  7022. */
  7023. if ((btf_mod->flags & BTF_MODULE_F_LIVE) && try_module_get(btf_mod->module))
  7024. res = btf_mod->module;
  7025. break;
  7026. }
  7027. mutex_unlock(&btf_module_mutex);
  7028. #endif
  7029. return res;
  7030. }
  7031. /* Returns struct btf corresponding to the struct module.
  7032. * This function can return NULL or ERR_PTR.
  7033. */
  7034. static struct btf *btf_get_module_btf(const struct module *module)
  7035. {
  7036. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  7037. struct btf_module *btf_mod, *tmp;
  7038. #endif
  7039. struct btf *btf = NULL;
  7040. if (!module) {
  7041. btf = bpf_get_btf_vmlinux();
  7042. if (!IS_ERR_OR_NULL(btf))
  7043. btf_get(btf);
  7044. return btf;
  7045. }
  7046. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  7047. mutex_lock(&btf_module_mutex);
  7048. list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) {
  7049. if (btf_mod->module != module)
  7050. continue;
  7051. btf_get(btf_mod->btf);
  7052. btf = btf_mod->btf;
  7053. break;
  7054. }
  7055. mutex_unlock(&btf_module_mutex);
  7056. #endif
  7057. return btf;
  7058. }
  7059. static int check_btf_kconfigs(const struct module *module, const char *feature)
  7060. {
  7061. if (!module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
  7062. pr_err("missing vmlinux BTF, cannot register %s\n", feature);
  7063. return -ENOENT;
  7064. }
  7065. if (module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
  7066. pr_warn("missing module BTF, cannot register %s\n", feature);
  7067. return 0;
  7068. }
  7069. BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
  7070. {
  7071. struct btf *btf = NULL;
  7072. int btf_obj_fd = 0;
  7073. long ret;
  7074. if (flags)
  7075. return -EINVAL;
  7076. if (name_sz <= 1 || name[name_sz - 1])
  7077. return -EINVAL;
  7078. ret = bpf_find_btf_id(name, kind, &btf);
  7079. if (ret > 0 && btf_is_module(btf)) {
  7080. btf_obj_fd = __btf_new_fd(btf);
  7081. if (btf_obj_fd < 0) {
  7082. btf_put(btf);
  7083. return btf_obj_fd;
  7084. }
  7085. return ret | (((u64)btf_obj_fd) << 32);
  7086. }
  7087. if (ret > 0)
  7088. btf_put(btf);
  7089. return ret;
  7090. }
  7091. const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = {
  7092. .func = bpf_btf_find_by_name_kind,
  7093. .gpl_only = false,
  7094. .ret_type = RET_INTEGER,
  7095. .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
  7096. .arg2_type = ARG_CONST_SIZE,
  7097. .arg3_type = ARG_ANYTHING,
  7098. .arg4_type = ARG_ANYTHING,
  7099. };
  7100. BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE)
  7101. #define BTF_TRACING_TYPE(name, type) BTF_ID(struct, type)
  7102. BTF_TRACING_TYPE_xxx
  7103. #undef BTF_TRACING_TYPE
  7104. /* Validate well-formedness of iter argument type.
  7105. * On success, return positive BTF ID of iter state's STRUCT type.
  7106. * On error, negative error is returned.
  7107. */
  7108. int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx)
  7109. {
  7110. const struct btf_param *arg;
  7111. const struct btf_type *t;
  7112. const char *name;
  7113. int btf_id;
  7114. if (btf_type_vlen(func) <= arg_idx)
  7115. return -EINVAL;
  7116. arg = &btf_params(func)[arg_idx];
  7117. t = btf_type_skip_modifiers(btf, arg->type, NULL);
  7118. if (!t || !btf_type_is_ptr(t))
  7119. return -EINVAL;
  7120. t = btf_type_skip_modifiers(btf, t->type, &btf_id);
  7121. if (!t || !__btf_type_is_struct(t))
  7122. return -EINVAL;
  7123. name = btf_name_by_offset(btf, t->name_off);
  7124. if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1))
  7125. return -EINVAL;
  7126. return btf_id;
  7127. }
  7128. static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,
  7129. const struct btf_type *func, u32 func_flags)
  7130. {
  7131. u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY);
  7132. const char *sfx, *iter_name;
  7133. const struct btf_type *t;
  7134. char exp_name[128];
  7135. u32 nr_args;
  7136. int btf_id;
  7137. /* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */
  7138. if (!flags || (flags & (flags - 1)))
  7139. return -EINVAL;
  7140. /* any BPF iter kfunc should have `struct bpf_iter_<type> *` first arg */
  7141. nr_args = btf_type_vlen(func);
  7142. if (nr_args < 1)
  7143. return -EINVAL;
  7144. btf_id = btf_check_iter_arg(btf, func, 0);
  7145. if (btf_id < 0)
  7146. return btf_id;
  7147. /* sizeof(struct bpf_iter_<type>) should be a multiple of 8 to
  7148. * fit nicely in stack slots
  7149. */
  7150. t = btf_type_by_id(btf, btf_id);
  7151. if (t->size == 0 || (t->size % 8))
  7152. return -EINVAL;
  7153. /* validate bpf_iter_<type>_{new,next,destroy}(struct bpf_iter_<type> *)
  7154. * naming pattern
  7155. */
  7156. iter_name = btf_name_by_offset(btf, t->name_off) + sizeof(ITER_PREFIX) - 1;
  7157. if (flags & KF_ITER_NEW)
  7158. sfx = "new";
  7159. else if (flags & KF_ITER_NEXT)
  7160. sfx = "next";
  7161. else /* (flags & KF_ITER_DESTROY) */
  7162. sfx = "destroy";
  7163. snprintf(exp_name, sizeof(exp_name), "bpf_iter_%s_%s", iter_name, sfx);
  7164. if (strcmp(func_name, exp_name))
  7165. return -EINVAL;
  7166. /* only iter constructor should have extra arguments */
  7167. if (!(flags & KF_ITER_NEW) && nr_args != 1)
  7168. return -EINVAL;
  7169. if (flags & KF_ITER_NEXT) {
  7170. /* bpf_iter_<type>_next() should return pointer */
  7171. t = btf_type_skip_modifiers(btf, func->type, NULL);
  7172. if (!t || !btf_type_is_ptr(t))
  7173. return -EINVAL;
  7174. }
  7175. if (flags & KF_ITER_DESTROY) {
  7176. /* bpf_iter_<type>_destroy() should return void */
  7177. t = btf_type_by_id(btf, func->type);
  7178. if (!t || !btf_type_is_void(t))
  7179. return -EINVAL;
  7180. }
  7181. return 0;
  7182. }
  7183. static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags)
  7184. {
  7185. const struct btf_type *func;
  7186. const char *func_name;
  7187. int err;
  7188. /* any kfunc should be FUNC -> FUNC_PROTO */
  7189. func = btf_type_by_id(btf, func_id);
  7190. if (!func || !btf_type_is_func(func))
  7191. return -EINVAL;
  7192. /* sanity check kfunc name */
  7193. func_name = btf_name_by_offset(btf, func->name_off);
  7194. if (!func_name || !func_name[0])
  7195. return -EINVAL;
  7196. func = btf_type_by_id(btf, func->type);
  7197. if (!func || !btf_type_is_func_proto(func))
  7198. return -EINVAL;
  7199. if (func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY)) {
  7200. err = btf_check_iter_kfuncs(btf, func_name, func, func_flags);
  7201. if (err)
  7202. return err;
  7203. }
  7204. return 0;
  7205. }
  7206. /* Kernel Function (kfunc) BTF ID set registration API */
  7207. static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
  7208. const struct btf_kfunc_id_set *kset)
  7209. {
  7210. struct btf_kfunc_hook_filter *hook_filter;
  7211. struct btf_id_set8 *add_set = kset->set;
  7212. bool vmlinux_set = !btf_is_module(btf);
  7213. bool add_filter = !!kset->filter;
  7214. struct btf_kfunc_set_tab *tab;
  7215. struct btf_id_set8 *set;
  7216. u32 set_cnt, i;
  7217. int ret;
  7218. if (hook >= BTF_KFUNC_HOOK_MAX) {
  7219. ret = -EINVAL;
  7220. goto end;
  7221. }
  7222. if (!add_set->cnt)
  7223. return 0;
  7224. tab = btf->kfunc_set_tab;
  7225. if (tab && add_filter) {
  7226. u32 i;
  7227. hook_filter = &tab->hook_filters[hook];
  7228. for (i = 0; i < hook_filter->nr_filters; i++) {
  7229. if (hook_filter->filters[i] == kset->filter) {
  7230. add_filter = false;
  7231. break;
  7232. }
  7233. }
  7234. if (add_filter && hook_filter->nr_filters == BTF_KFUNC_FILTER_MAX_CNT) {
  7235. ret = -E2BIG;
  7236. goto end;
  7237. }
  7238. }
  7239. if (!tab) {
  7240. tab = kzalloc(sizeof(*tab), GFP_KERNEL | __GFP_NOWARN);
  7241. if (!tab)
  7242. return -ENOMEM;
  7243. btf->kfunc_set_tab = tab;
  7244. }
  7245. set = tab->sets[hook];
  7246. /* Warn when register_btf_kfunc_id_set is called twice for the same hook
  7247. * for module sets.
  7248. */
  7249. if (WARN_ON_ONCE(set && !vmlinux_set)) {
  7250. ret = -EINVAL;
  7251. goto end;
  7252. }
  7253. /* In case of vmlinux sets, there may be more than one set being
  7254. * registered per hook. To create a unified set, we allocate a new set
  7255. * and concatenate all individual sets being registered. While each set
  7256. * is individually sorted, they may become unsorted when concatenated,
  7257. * hence re-sorting the final set again is required to make binary
  7258. * searching the set using btf_id_set8_contains function work.
  7259. *
  7260. * For module sets, we need to allocate as we may need to relocate
  7261. * BTF ids.
  7262. */
  7263. set_cnt = set ? set->cnt : 0;
  7264. if (set_cnt > U32_MAX - add_set->cnt) {
  7265. ret = -EOVERFLOW;
  7266. goto end;
  7267. }
  7268. if (set_cnt + add_set->cnt > BTF_KFUNC_SET_MAX_CNT) {
  7269. ret = -E2BIG;
  7270. goto end;
  7271. }
  7272. /* Grow set */
  7273. set = krealloc(tab->sets[hook],
  7274. offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]),
  7275. GFP_KERNEL | __GFP_NOWARN);
  7276. if (!set) {
  7277. ret = -ENOMEM;
  7278. goto end;
  7279. }
  7280. /* For newly allocated set, initialize set->cnt to 0 */
  7281. if (!tab->sets[hook])
  7282. set->cnt = 0;
  7283. tab->sets[hook] = set;
  7284. /* Concatenate the two sets */
  7285. memcpy(set->pairs + set->cnt, add_set->pairs, add_set->cnt * sizeof(set->pairs[0]));
  7286. /* Now that the set is copied, update with relocated BTF ids */
  7287. for (i = set->cnt; i < set->cnt + add_set->cnt; i++)
  7288. set->pairs[i].id = btf_relocate_id(btf, set->pairs[i].id);
  7289. set->cnt += add_set->cnt;
  7290. sort(set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func, NULL);
  7291. if (add_filter) {
  7292. hook_filter = &tab->hook_filters[hook];
  7293. hook_filter->filters[hook_filter->nr_filters++] = kset->filter;
  7294. }
  7295. return 0;
  7296. end:
  7297. btf_free_kfunc_set_tab(btf);
  7298. return ret;
  7299. }
  7300. static u32 *__btf_kfunc_id_set_contains(const struct btf *btf,
  7301. enum btf_kfunc_hook hook,
  7302. u32 kfunc_btf_id,
  7303. const struct bpf_prog *prog)
  7304. {
  7305. struct btf_kfunc_hook_filter *hook_filter;
  7306. struct btf_id_set8 *set;
  7307. u32 *id, i;
  7308. if (hook >= BTF_KFUNC_HOOK_MAX)
  7309. return NULL;
  7310. if (!btf->kfunc_set_tab)
  7311. return NULL;
  7312. hook_filter = &btf->kfunc_set_tab->hook_filters[hook];
  7313. for (i = 0; i < hook_filter->nr_filters; i++) {
  7314. if (hook_filter->filters[i](prog, kfunc_btf_id))
  7315. return NULL;
  7316. }
  7317. set = btf->kfunc_set_tab->sets[hook];
  7318. if (!set)
  7319. return NULL;
  7320. id = btf_id_set8_contains(set, kfunc_btf_id);
  7321. if (!id)
  7322. return NULL;
  7323. /* The flags for BTF ID are located next to it */
  7324. return id + 1;
  7325. }
  7326. static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
  7327. {
  7328. switch (prog_type) {
  7329. case BPF_PROG_TYPE_UNSPEC:
  7330. return BTF_KFUNC_HOOK_COMMON;
  7331. case BPF_PROG_TYPE_XDP:
  7332. return BTF_KFUNC_HOOK_XDP;
  7333. case BPF_PROG_TYPE_SCHED_CLS:
  7334. return BTF_KFUNC_HOOK_TC;
  7335. case BPF_PROG_TYPE_STRUCT_OPS:
  7336. return BTF_KFUNC_HOOK_STRUCT_OPS;
  7337. case BPF_PROG_TYPE_TRACING:
  7338. case BPF_PROG_TYPE_TRACEPOINT:
  7339. case BPF_PROG_TYPE_PERF_EVENT:
  7340. case BPF_PROG_TYPE_LSM:
  7341. return BTF_KFUNC_HOOK_TRACING;
  7342. case BPF_PROG_TYPE_SYSCALL:
  7343. return BTF_KFUNC_HOOK_SYSCALL;
  7344. case BPF_PROG_TYPE_CGROUP_SKB:
  7345. case BPF_PROG_TYPE_CGROUP_SOCK:
  7346. case BPF_PROG_TYPE_CGROUP_DEVICE:
  7347. case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
  7348. case BPF_PROG_TYPE_CGROUP_SOCKOPT:
  7349. case BPF_PROG_TYPE_CGROUP_SYSCTL:
  7350. return BTF_KFUNC_HOOK_CGROUP;
  7351. case BPF_PROG_TYPE_SCHED_ACT:
  7352. return BTF_KFUNC_HOOK_SCHED_ACT;
  7353. case BPF_PROG_TYPE_SK_SKB:
  7354. return BTF_KFUNC_HOOK_SK_SKB;
  7355. case BPF_PROG_TYPE_SOCKET_FILTER:
  7356. return BTF_KFUNC_HOOK_SOCKET_FILTER;
  7357. case BPF_PROG_TYPE_LWT_OUT:
  7358. case BPF_PROG_TYPE_LWT_IN:
  7359. case BPF_PROG_TYPE_LWT_XMIT:
  7360. case BPF_PROG_TYPE_LWT_SEG6LOCAL:
  7361. return BTF_KFUNC_HOOK_LWT;
  7362. case BPF_PROG_TYPE_NETFILTER:
  7363. return BTF_KFUNC_HOOK_NETFILTER;
  7364. case BPF_PROG_TYPE_KPROBE:
  7365. return BTF_KFUNC_HOOK_KPROBE;
  7366. default:
  7367. return BTF_KFUNC_HOOK_MAX;
  7368. }
  7369. }
  7370. /* Caution:
  7371. * Reference to the module (obtained using btf_try_get_module) corresponding to
  7372. * the struct btf *MUST* be held when calling this function from verifier
  7373. * context. This is usually true as we stash references in prog's kfunc_btf_tab;
  7374. * keeping the reference for the duration of the call provides the necessary
  7375. * protection for looking up a well-formed btf->kfunc_set_tab.
  7376. */
  7377. u32 *btf_kfunc_id_set_contains(const struct btf *btf,
  7378. u32 kfunc_btf_id,
  7379. const struct bpf_prog *prog)
  7380. {
  7381. enum bpf_prog_type prog_type = resolve_prog_type(prog);
  7382. enum btf_kfunc_hook hook;
  7383. u32 *kfunc_flags;
  7384. kfunc_flags = __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, kfunc_btf_id, prog);
  7385. if (kfunc_flags)
  7386. return kfunc_flags;
  7387. hook = bpf_prog_type_to_kfunc_hook(prog_type);
  7388. return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id, prog);
  7389. }
  7390. u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,
  7391. const struct bpf_prog *prog)
  7392. {
  7393. return __btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_FMODRET, kfunc_btf_id, prog);
  7394. }
  7395. static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,
  7396. const struct btf_kfunc_id_set *kset)
  7397. {
  7398. struct btf *btf;
  7399. int ret, i;
  7400. btf = btf_get_module_btf(kset->owner);
  7401. if (!btf)
  7402. return check_btf_kconfigs(kset->owner, "kfunc");
  7403. if (IS_ERR(btf))
  7404. return PTR_ERR(btf);
  7405. for (i = 0; i < kset->set->cnt; i++) {
  7406. ret = btf_check_kfunc_protos(btf, btf_relocate_id(btf, kset->set->pairs[i].id),
  7407. kset->set->pairs[i].flags);
  7408. if (ret)
  7409. goto err_out;
  7410. }
  7411. ret = btf_populate_kfunc_set(btf, hook, kset);
  7412. err_out:
  7413. btf_put(btf);
  7414. return ret;
  7415. }
  7416. /* This function must be invoked only from initcalls/module init functions */
  7417. int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
  7418. const struct btf_kfunc_id_set *kset)
  7419. {
  7420. enum btf_kfunc_hook hook;
  7421. /* All kfuncs need to be tagged as such in BTF.
  7422. * WARN() for initcall registrations that do not check errors.
  7423. */
  7424. if (!(kset->set->flags & BTF_SET8_KFUNCS)) {
  7425. WARN_ON(!kset->owner);
  7426. return -EINVAL;
  7427. }
  7428. hook = bpf_prog_type_to_kfunc_hook(prog_type);
  7429. return __register_btf_kfunc_id_set(hook, kset);
  7430. }
  7431. EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set);
  7432. /* This function must be invoked only from initcalls/module init functions */
  7433. int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset)
  7434. {
  7435. return __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET, kset);
  7436. }
  7437. EXPORT_SYMBOL_GPL(register_btf_fmodret_id_set);
  7438. s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
  7439. {
  7440. struct btf_id_dtor_kfunc_tab *tab = btf->dtor_kfunc_tab;
  7441. struct btf_id_dtor_kfunc *dtor;
  7442. if (!tab)
  7443. return -ENOENT;
  7444. /* Even though the size of tab->dtors[0] is > sizeof(u32), we only need
  7445. * to compare the first u32 with btf_id, so we can reuse btf_id_cmp_func.
  7446. */
  7447. BUILD_BUG_ON(offsetof(struct btf_id_dtor_kfunc, btf_id) != 0);
  7448. dtor = bsearch(&btf_id, tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func);
  7449. if (!dtor)
  7450. return -ENOENT;
  7451. return dtor->kfunc_btf_id;
  7452. }
  7453. static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc *dtors, u32 cnt)
  7454. {
  7455. const struct btf_type *dtor_func, *dtor_func_proto, *t;
  7456. const struct btf_param *args;
  7457. s32 dtor_btf_id;
  7458. u32 nr_args, i;
  7459. for (i = 0; i < cnt; i++) {
  7460. dtor_btf_id = btf_relocate_id(btf, dtors[i].kfunc_btf_id);
  7461. dtor_func = btf_type_by_id(btf, dtor_btf_id);
  7462. if (!dtor_func || !btf_type_is_func(dtor_func))
  7463. return -EINVAL;
  7464. dtor_func_proto = btf_type_by_id(btf, dtor_func->type);
  7465. if (!dtor_func_proto || !btf_type_is_func_proto(dtor_func_proto))
  7466. return -EINVAL;
  7467. /* Make sure the prototype of the destructor kfunc is 'void func(type *)' */
  7468. t = btf_type_by_id(btf, dtor_func_proto->type);
  7469. if (!t || !btf_type_is_void(t))
  7470. return -EINVAL;
  7471. nr_args = btf_type_vlen(dtor_func_proto);
  7472. if (nr_args != 1)
  7473. return -EINVAL;
  7474. args = btf_params(dtor_func_proto);
  7475. t = btf_type_by_id(btf, args[0].type);
  7476. /* Allow any pointer type, as width on targets Linux supports
  7477. * will be same for all pointer types (i.e. sizeof(void *))
  7478. */
  7479. if (!t || !btf_type_is_ptr(t))
  7480. return -EINVAL;
  7481. }
  7482. return 0;
  7483. }
  7484. /* This function must be invoked only from initcalls/module init functions */
  7485. int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
  7486. struct module *owner)
  7487. {
  7488. struct btf_id_dtor_kfunc_tab *tab;
  7489. struct btf *btf;
  7490. u32 tab_cnt, i;
  7491. int ret;
  7492. btf = btf_get_module_btf(owner);
  7493. if (!btf)
  7494. return check_btf_kconfigs(owner, "dtor kfuncs");
  7495. if (IS_ERR(btf))
  7496. return PTR_ERR(btf);
  7497. if (add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) {
  7498. pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT);
  7499. ret = -E2BIG;
  7500. goto end;
  7501. }
  7502. /* Ensure that the prototype of dtor kfuncs being registered is sane */
  7503. ret = btf_check_dtor_kfuncs(btf, dtors, add_cnt);
  7504. if (ret < 0)
  7505. goto end;
  7506. tab = btf->dtor_kfunc_tab;
  7507. /* Only one call allowed for modules */
  7508. if (WARN_ON_ONCE(tab && btf_is_module(btf))) {
  7509. ret = -EINVAL;
  7510. goto end;
  7511. }
  7512. tab_cnt = tab ? tab->cnt : 0;
  7513. if (tab_cnt > U32_MAX - add_cnt) {
  7514. ret = -EOVERFLOW;
  7515. goto end;
  7516. }
  7517. if (tab_cnt + add_cnt >= BTF_DTOR_KFUNC_MAX_CNT) {
  7518. pr_err("cannot register more than %d kfunc destructors\n", BTF_DTOR_KFUNC_MAX_CNT);
  7519. ret = -E2BIG;
  7520. goto end;
  7521. }
  7522. tab = krealloc(btf->dtor_kfunc_tab,
  7523. offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]),
  7524. GFP_KERNEL | __GFP_NOWARN);
  7525. if (!tab) {
  7526. ret = -ENOMEM;
  7527. goto end;
  7528. }
  7529. if (!btf->dtor_kfunc_tab)
  7530. tab->cnt = 0;
  7531. btf->dtor_kfunc_tab = tab;
  7532. memcpy(tab->dtors + tab->cnt, dtors, add_cnt * sizeof(tab->dtors[0]));
  7533. /* remap BTF ids based on BTF relocation (if any) */
  7534. for (i = tab_cnt; i < tab_cnt + add_cnt; i++) {
  7535. tab->dtors[i].btf_id = btf_relocate_id(btf, tab->dtors[i].btf_id);
  7536. tab->dtors[i].kfunc_btf_id = btf_relocate_id(btf, tab->dtors[i].kfunc_btf_id);
  7537. }
  7538. tab->cnt += add_cnt;
  7539. sort(tab->dtors, tab->cnt, sizeof(tab->dtors[0]), btf_id_cmp_func, NULL);
  7540. end:
  7541. if (ret)
  7542. btf_free_dtor_kfunc_tab(btf);
  7543. btf_put(btf);
  7544. return ret;
  7545. }
  7546. EXPORT_SYMBOL_GPL(register_btf_id_dtor_kfuncs);
  7547. #define MAX_TYPES_ARE_COMPAT_DEPTH 2
  7548. /* Check local and target types for compatibility. This check is used for
  7549. * type-based CO-RE relocations and follow slightly different rules than
  7550. * field-based relocations. This function assumes that root types were already
  7551. * checked for name match. Beyond that initial root-level name check, names
  7552. * are completely ignored. Compatibility rules are as follows:
  7553. * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs/ENUM64s are considered compatible, but
  7554. * kind should match for local and target types (i.e., STRUCT is not
  7555. * compatible with UNION);
  7556. * - for ENUMs/ENUM64s, the size is ignored;
  7557. * - for INT, size and signedness are ignored;
  7558. * - for ARRAY, dimensionality is ignored, element types are checked for
  7559. * compatibility recursively;
  7560. * - CONST/VOLATILE/RESTRICT modifiers are ignored;
  7561. * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
  7562. * - FUNC_PROTOs are compatible if they have compatible signature: same
  7563. * number of input args and compatible return and argument types.
  7564. * These rules are not set in stone and probably will be adjusted as we get
  7565. * more experience with using BPF CO-RE relocations.
  7566. */
  7567. int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
  7568. const struct btf *targ_btf, __u32 targ_id)
  7569. {
  7570. return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id,
  7571. MAX_TYPES_ARE_COMPAT_DEPTH);
  7572. }
  7573. #define MAX_TYPES_MATCH_DEPTH 2
  7574. int bpf_core_types_match(const struct btf *local_btf, u32 local_id,
  7575. const struct btf *targ_btf, u32 targ_id)
  7576. {
  7577. return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false,
  7578. MAX_TYPES_MATCH_DEPTH);
  7579. }
  7580. static bool bpf_core_is_flavor_sep(const char *s)
  7581. {
  7582. /* check X___Y name pattern, where X and Y are not underscores */
  7583. return s[0] != '_' && /* X */
  7584. s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
  7585. s[4] != '_'; /* Y */
  7586. }
  7587. size_t bpf_core_essential_name_len(const char *name)
  7588. {
  7589. size_t n = strlen(name);
  7590. int i;
  7591. for (i = n - 5; i >= 0; i--) {
  7592. if (bpf_core_is_flavor_sep(name + i))
  7593. return i + 1;
  7594. }
  7595. return n;
  7596. }
  7597. static void bpf_free_cands(struct bpf_cand_cache *cands)
  7598. {
  7599. if (!cands->cnt)
  7600. /* empty candidate array was allocated on stack */
  7601. return;
  7602. kfree(cands);
  7603. }
  7604. static void bpf_free_cands_from_cache(struct bpf_cand_cache *cands)
  7605. {
  7606. kfree(cands->name);
  7607. kfree(cands);
  7608. }
  7609. #define VMLINUX_CAND_CACHE_SIZE 31
  7610. static struct bpf_cand_cache *vmlinux_cand_cache[VMLINUX_CAND_CACHE_SIZE];
  7611. #define MODULE_CAND_CACHE_SIZE 31
  7612. static struct bpf_cand_cache *module_cand_cache[MODULE_CAND_CACHE_SIZE];
  7613. static void __print_cand_cache(struct bpf_verifier_log *log,
  7614. struct bpf_cand_cache **cache,
  7615. int cache_size)
  7616. {
  7617. struct bpf_cand_cache *cc;
  7618. int i, j;
  7619. for (i = 0; i < cache_size; i++) {
  7620. cc = cache[i];
  7621. if (!cc)
  7622. continue;
  7623. bpf_log(log, "[%d]%s(", i, cc->name);
  7624. for (j = 0; j < cc->cnt; j++) {
  7625. bpf_log(log, "%d", cc->cands[j].id);
  7626. if (j < cc->cnt - 1)
  7627. bpf_log(log, " ");
  7628. }
  7629. bpf_log(log, "), ");
  7630. }
  7631. }
  7632. static void print_cand_cache(struct bpf_verifier_log *log)
  7633. {
  7634. mutex_lock(&cand_cache_mutex);
  7635. bpf_log(log, "vmlinux_cand_cache:");
  7636. __print_cand_cache(log, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
  7637. bpf_log(log, "\nmodule_cand_cache:");
  7638. __print_cand_cache(log, module_cand_cache, MODULE_CAND_CACHE_SIZE);
  7639. bpf_log(log, "\n");
  7640. mutex_unlock(&cand_cache_mutex);
  7641. }
  7642. static u32 hash_cands(struct bpf_cand_cache *cands)
  7643. {
  7644. return jhash(cands->name, cands->name_len, 0);
  7645. }
  7646. static struct bpf_cand_cache *check_cand_cache(struct bpf_cand_cache *cands,
  7647. struct bpf_cand_cache **cache,
  7648. int cache_size)
  7649. {
  7650. struct bpf_cand_cache *cc = cache[hash_cands(cands) % cache_size];
  7651. if (cc && cc->name_len == cands->name_len &&
  7652. !strncmp(cc->name, cands->name, cands->name_len))
  7653. return cc;
  7654. return NULL;
  7655. }
  7656. static size_t sizeof_cands(int cnt)
  7657. {
  7658. return offsetof(struct bpf_cand_cache, cands[cnt]);
  7659. }
  7660. static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands,
  7661. struct bpf_cand_cache **cache,
  7662. int cache_size)
  7663. {
  7664. struct bpf_cand_cache **cc = &cache[hash_cands(cands) % cache_size], *new_cands;
  7665. if (*cc) {
  7666. bpf_free_cands_from_cache(*cc);
  7667. *cc = NULL;
  7668. }
  7669. new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL);
  7670. if (!new_cands) {
  7671. bpf_free_cands(cands);
  7672. return ERR_PTR(-ENOMEM);
  7673. }
  7674. /* strdup the name, since it will stay in cache.
  7675. * the cands->name points to strings in prog's BTF and the prog can be unloaded.
  7676. */
  7677. new_cands->name = kmemdup_nul(cands->name, cands->name_len, GFP_KERNEL);
  7678. bpf_free_cands(cands);
  7679. if (!new_cands->name) {
  7680. kfree(new_cands);
  7681. return ERR_PTR(-ENOMEM);
  7682. }
  7683. *cc = new_cands;
  7684. return new_cands;
  7685. }
  7686. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  7687. static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache,
  7688. int cache_size)
  7689. {
  7690. struct bpf_cand_cache *cc;
  7691. int i, j;
  7692. for (i = 0; i < cache_size; i++) {
  7693. cc = cache[i];
  7694. if (!cc)
  7695. continue;
  7696. if (!btf) {
  7697. /* when new module is loaded purge all of module_cand_cache,
  7698. * since new module might have candidates with the name
  7699. * that matches cached cands.
  7700. */
  7701. bpf_free_cands_from_cache(cc);
  7702. cache[i] = NULL;
  7703. continue;
  7704. }
  7705. /* when module is unloaded purge cache entries
  7706. * that match module's btf
  7707. */
  7708. for (j = 0; j < cc->cnt; j++)
  7709. if (cc->cands[j].btf == btf) {
  7710. bpf_free_cands_from_cache(cc);
  7711. cache[i] = NULL;
  7712. break;
  7713. }
  7714. }
  7715. }
  7716. static void purge_cand_cache(struct btf *btf)
  7717. {
  7718. mutex_lock(&cand_cache_mutex);
  7719. __purge_cand_cache(btf, module_cand_cache, MODULE_CAND_CACHE_SIZE);
  7720. mutex_unlock(&cand_cache_mutex);
  7721. }
  7722. #endif
  7723. static struct bpf_cand_cache *
  7724. bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
  7725. int targ_start_id)
  7726. {
  7727. struct bpf_cand_cache *new_cands;
  7728. const struct btf_type *t;
  7729. const char *targ_name;
  7730. size_t targ_essent_len;
  7731. int n, i;
  7732. n = btf_nr_types(targ_btf);
  7733. for (i = targ_start_id; i < n; i++) {
  7734. t = btf_type_by_id(targ_btf, i);
  7735. if (btf_kind(t) != cands->kind)
  7736. continue;
  7737. targ_name = btf_name_by_offset(targ_btf, t->name_off);
  7738. if (!targ_name)
  7739. continue;
  7740. /* the resched point is before strncmp to make sure that search
  7741. * for non-existing name will have a chance to schedule().
  7742. */
  7743. cond_resched();
  7744. if (strncmp(cands->name, targ_name, cands->name_len) != 0)
  7745. continue;
  7746. targ_essent_len = bpf_core_essential_name_len(targ_name);
  7747. if (targ_essent_len != cands->name_len)
  7748. continue;
  7749. /* most of the time there is only one candidate for a given kind+name pair */
  7750. new_cands = kmalloc(sizeof_cands(cands->cnt + 1), GFP_KERNEL);
  7751. if (!new_cands) {
  7752. bpf_free_cands(cands);
  7753. return ERR_PTR(-ENOMEM);
  7754. }
  7755. memcpy(new_cands, cands, sizeof_cands(cands->cnt));
  7756. bpf_free_cands(cands);
  7757. cands = new_cands;
  7758. cands->cands[cands->cnt].btf = targ_btf;
  7759. cands->cands[cands->cnt].id = i;
  7760. cands->cnt++;
  7761. }
  7762. return cands;
  7763. }
  7764. static struct bpf_cand_cache *
  7765. bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id)
  7766. {
  7767. struct bpf_cand_cache *cands, *cc, local_cand = {};
  7768. const struct btf *local_btf = ctx->btf;
  7769. const struct btf_type *local_type;
  7770. const struct btf *main_btf;
  7771. size_t local_essent_len;
  7772. struct btf *mod_btf;
  7773. const char *name;
  7774. int id;
  7775. main_btf = bpf_get_btf_vmlinux();
  7776. if (IS_ERR(main_btf))
  7777. return ERR_CAST(main_btf);
  7778. if (!main_btf)
  7779. return ERR_PTR(-EINVAL);
  7780. local_type = btf_type_by_id(local_btf, local_type_id);
  7781. if (!local_type)
  7782. return ERR_PTR(-EINVAL);
  7783. name = btf_name_by_offset(local_btf, local_type->name_off);
  7784. if (str_is_empty(name))
  7785. return ERR_PTR(-EINVAL);
  7786. local_essent_len = bpf_core_essential_name_len(name);
  7787. cands = &local_cand;
  7788. cands->name = name;
  7789. cands->kind = btf_kind(local_type);
  7790. cands->name_len = local_essent_len;
  7791. cc = check_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
  7792. /* cands is a pointer to stack here */
  7793. if (cc) {
  7794. if (cc->cnt)
  7795. return cc;
  7796. goto check_modules;
  7797. }
  7798. /* Attempt to find target candidates in vmlinux BTF first */
  7799. cands = bpf_core_add_cands(cands, main_btf, 1);
  7800. if (IS_ERR(cands))
  7801. return ERR_CAST(cands);
  7802. /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */
  7803. /* populate cache even when cands->cnt == 0 */
  7804. cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
  7805. if (IS_ERR(cc))
  7806. return ERR_CAST(cc);
  7807. /* if vmlinux BTF has any candidate, don't go for module BTFs */
  7808. if (cc->cnt)
  7809. return cc;
  7810. check_modules:
  7811. /* cands is a pointer to stack here and cands->cnt == 0 */
  7812. cc = check_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
  7813. if (cc)
  7814. /* if cache has it return it even if cc->cnt == 0 */
  7815. return cc;
  7816. /* If candidate is not found in vmlinux's BTF then search in module's BTFs */
  7817. spin_lock_bh(&btf_idr_lock);
  7818. idr_for_each_entry(&btf_idr, mod_btf, id) {
  7819. if (!btf_is_module(mod_btf))
  7820. continue;
  7821. /* linear search could be slow hence unlock/lock
  7822. * the IDR to avoiding holding it for too long
  7823. */
  7824. btf_get(mod_btf);
  7825. spin_unlock_bh(&btf_idr_lock);
  7826. cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf));
  7827. btf_put(mod_btf);
  7828. if (IS_ERR(cands))
  7829. return ERR_CAST(cands);
  7830. spin_lock_bh(&btf_idr_lock);
  7831. }
  7832. spin_unlock_bh(&btf_idr_lock);
  7833. /* cands is a pointer to kmalloced memory here if cands->cnt > 0
  7834. * or pointer to stack if cands->cnd == 0.
  7835. * Copy it into the cache even when cands->cnt == 0 and
  7836. * return the result.
  7837. */
  7838. return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
  7839. }
  7840. int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
  7841. int relo_idx, void *insn)
  7842. {
  7843. bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL;
  7844. struct bpf_core_cand_list cands = {};
  7845. struct bpf_core_relo_res targ_res;
  7846. struct bpf_core_spec *specs;
  7847. const struct btf_type *type;
  7848. int err;
  7849. /* ~4k of temp memory necessary to convert LLVM spec like "0:1:0:5"
  7850. * into arrays of btf_ids of struct fields and array indices.
  7851. */
  7852. specs = kcalloc(3, sizeof(*specs), GFP_KERNEL);
  7853. if (!specs)
  7854. return -ENOMEM;
  7855. type = btf_type_by_id(ctx->btf, relo->type_id);
  7856. if (!type) {
  7857. bpf_log(ctx->log, "relo #%u: bad type id %u\n",
  7858. relo_idx, relo->type_id);
  7859. kfree(specs);
  7860. return -EINVAL;
  7861. }
  7862. if (need_cands) {
  7863. struct bpf_cand_cache *cc;
  7864. int i;
  7865. mutex_lock(&cand_cache_mutex);
  7866. cc = bpf_core_find_cands(ctx, relo->type_id);
  7867. if (IS_ERR(cc)) {
  7868. bpf_log(ctx->log, "target candidate search failed for %d\n",
  7869. relo->type_id);
  7870. err = PTR_ERR(cc);
  7871. goto out;
  7872. }
  7873. if (cc->cnt) {
  7874. cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL);
  7875. if (!cands.cands) {
  7876. err = -ENOMEM;
  7877. goto out;
  7878. }
  7879. }
  7880. for (i = 0; i < cc->cnt; i++) {
  7881. bpf_log(ctx->log,
  7882. "CO-RE relocating %s %s: found target candidate [%d]\n",
  7883. btf_kind_str[cc->kind], cc->name, cc->cands[i].id);
  7884. cands.cands[i].btf = cc->cands[i].btf;
  7885. cands.cands[i].id = cc->cands[i].id;
  7886. }
  7887. cands.len = cc->cnt;
  7888. /* cand_cache_mutex needs to span the cache lookup and
  7889. * copy of btf pointer into bpf_core_cand_list,
  7890. * since module can be unloaded while bpf_core_calc_relo_insn
  7891. * is working with module's btf.
  7892. */
  7893. }
  7894. err = bpf_core_calc_relo_insn((void *)ctx->log, relo, relo_idx, ctx->btf, &cands, specs,
  7895. &targ_res);
  7896. if (err)
  7897. goto out;
  7898. err = bpf_core_patch_insn((void *)ctx->log, insn, relo->insn_off / 8, relo, relo_idx,
  7899. &targ_res);
  7900. out:
  7901. kfree(specs);
  7902. if (need_cands) {
  7903. kfree(cands.cands);
  7904. mutex_unlock(&cand_cache_mutex);
  7905. if (ctx->log->level & BPF_LOG_LEVEL2)
  7906. print_cand_cache(ctx->log);
  7907. }
  7908. return err;
  7909. }
  7910. bool btf_nested_type_is_trusted(struct bpf_verifier_log *log,
  7911. const struct bpf_reg_state *reg,
  7912. const char *field_name, u32 btf_id, const char *suffix)
  7913. {
  7914. struct btf *btf = reg->btf;
  7915. const struct btf_type *walk_type, *safe_type;
  7916. const char *tname;
  7917. char safe_tname[64];
  7918. long ret, safe_id;
  7919. const struct btf_member *member;
  7920. u32 i;
  7921. walk_type = btf_type_by_id(btf, reg->btf_id);
  7922. if (!walk_type)
  7923. return false;
  7924. tname = btf_name_by_offset(btf, walk_type->name_off);
  7925. ret = snprintf(safe_tname, sizeof(safe_tname), "%s%s", tname, suffix);
  7926. if (ret >= sizeof(safe_tname))
  7927. return false;
  7928. safe_id = btf_find_by_name_kind(btf, safe_tname, BTF_INFO_KIND(walk_type->info));
  7929. if (safe_id < 0)
  7930. return false;
  7931. safe_type = btf_type_by_id(btf, safe_id);
  7932. if (!safe_type)
  7933. return false;
  7934. for_each_member(i, safe_type, member) {
  7935. const char *m_name = __btf_name_by_offset(btf, member->name_off);
  7936. const struct btf_type *mtype = btf_type_by_id(btf, member->type);
  7937. u32 id;
  7938. if (!btf_type_is_ptr(mtype))
  7939. continue;
  7940. btf_type_skip_modifiers(btf, mtype->type, &id);
  7941. /* If we match on both type and name, the field is considered trusted. */
  7942. if (btf_id == id && !strcmp(field_name, m_name))
  7943. return true;
  7944. }
  7945. return false;
  7946. }
  7947. bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,
  7948. const struct btf *reg_btf, u32 reg_id,
  7949. const struct btf *arg_btf, u32 arg_id)
  7950. {
  7951. const char *reg_name, *arg_name, *search_needle;
  7952. const struct btf_type *reg_type, *arg_type;
  7953. int reg_len, arg_len, cmp_len;
  7954. size_t pattern_len = sizeof(NOCAST_ALIAS_SUFFIX) - sizeof(char);
  7955. reg_type = btf_type_by_id(reg_btf, reg_id);
  7956. if (!reg_type)
  7957. return false;
  7958. arg_type = btf_type_by_id(arg_btf, arg_id);
  7959. if (!arg_type)
  7960. return false;
  7961. reg_name = btf_name_by_offset(reg_btf, reg_type->name_off);
  7962. arg_name = btf_name_by_offset(arg_btf, arg_type->name_off);
  7963. reg_len = strlen(reg_name);
  7964. arg_len = strlen(arg_name);
  7965. /* Exactly one of the two type names may be suffixed with ___init, so
  7966. * if the strings are the same size, they can't possibly be no-cast
  7967. * aliases of one another. If you have two of the same type names, e.g.
  7968. * they're both nf_conn___init, it would be improper to return true
  7969. * because they are _not_ no-cast aliases, they are the same type.
  7970. */
  7971. if (reg_len == arg_len)
  7972. return false;
  7973. /* Either of the two names must be the other name, suffixed with ___init. */
  7974. if ((reg_len != arg_len + pattern_len) &&
  7975. (arg_len != reg_len + pattern_len))
  7976. return false;
  7977. if (reg_len < arg_len) {
  7978. search_needle = strstr(arg_name, NOCAST_ALIAS_SUFFIX);
  7979. cmp_len = reg_len;
  7980. } else {
  7981. search_needle = strstr(reg_name, NOCAST_ALIAS_SUFFIX);
  7982. cmp_len = arg_len;
  7983. }
  7984. if (!search_needle)
  7985. return false;
  7986. /* ___init suffix must come at the end of the name */
  7987. if (*(search_needle + pattern_len) != '\0')
  7988. return false;
  7989. return !strncmp(reg_name, arg_name, cmp_len);
  7990. }
  7991. #ifdef CONFIG_BPF_JIT
  7992. static int
  7993. btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
  7994. struct bpf_verifier_log *log)
  7995. {
  7996. struct btf_struct_ops_tab *tab, *new_tab;
  7997. int i, err;
  7998. tab = btf->struct_ops_tab;
  7999. if (!tab) {
  8000. tab = kzalloc(offsetof(struct btf_struct_ops_tab, ops[4]),
  8001. GFP_KERNEL);
  8002. if (!tab)
  8003. return -ENOMEM;
  8004. tab->capacity = 4;
  8005. btf->struct_ops_tab = tab;
  8006. }
  8007. for (i = 0; i < tab->cnt; i++)
  8008. if (tab->ops[i].st_ops == st_ops)
  8009. return -EEXIST;
  8010. if (tab->cnt == tab->capacity) {
  8011. new_tab = krealloc(tab,
  8012. offsetof(struct btf_struct_ops_tab,
  8013. ops[tab->capacity * 2]),
  8014. GFP_KERNEL);
  8015. if (!new_tab)
  8016. return -ENOMEM;
  8017. tab = new_tab;
  8018. tab->capacity *= 2;
  8019. btf->struct_ops_tab = tab;
  8020. }
  8021. tab->ops[btf->struct_ops_tab->cnt].st_ops = st_ops;
  8022. err = bpf_struct_ops_desc_init(&tab->ops[btf->struct_ops_tab->cnt], btf, log);
  8023. if (err)
  8024. return err;
  8025. btf->struct_ops_tab->cnt++;
  8026. return 0;
  8027. }
  8028. const struct bpf_struct_ops_desc *
  8029. bpf_struct_ops_find_value(struct btf *btf, u32 value_id)
  8030. {
  8031. const struct bpf_struct_ops_desc *st_ops_list;
  8032. unsigned int i;
  8033. u32 cnt;
  8034. if (!value_id)
  8035. return NULL;
  8036. if (!btf->struct_ops_tab)
  8037. return NULL;
  8038. cnt = btf->struct_ops_tab->cnt;
  8039. st_ops_list = btf->struct_ops_tab->ops;
  8040. for (i = 0; i < cnt; i++) {
  8041. if (st_ops_list[i].value_id == value_id)
  8042. return &st_ops_list[i];
  8043. }
  8044. return NULL;
  8045. }
  8046. const struct bpf_struct_ops_desc *
  8047. bpf_struct_ops_find(struct btf *btf, u32 type_id)
  8048. {
  8049. const struct bpf_struct_ops_desc *st_ops_list;
  8050. unsigned int i;
  8051. u32 cnt;
  8052. if (!type_id)
  8053. return NULL;
  8054. if (!btf->struct_ops_tab)
  8055. return NULL;
  8056. cnt = btf->struct_ops_tab->cnt;
  8057. st_ops_list = btf->struct_ops_tab->ops;
  8058. for (i = 0; i < cnt; i++) {
  8059. if (st_ops_list[i].type_id == type_id)
  8060. return &st_ops_list[i];
  8061. }
  8062. return NULL;
  8063. }
  8064. int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops)
  8065. {
  8066. struct bpf_verifier_log *log;
  8067. struct btf *btf;
  8068. int err = 0;
  8069. btf = btf_get_module_btf(st_ops->owner);
  8070. if (!btf)
  8071. return check_btf_kconfigs(st_ops->owner, "struct_ops");
  8072. if (IS_ERR(btf))
  8073. return PTR_ERR(btf);
  8074. log = kzalloc(sizeof(*log), GFP_KERNEL | __GFP_NOWARN);
  8075. if (!log) {
  8076. err = -ENOMEM;
  8077. goto errout;
  8078. }
  8079. log->level = BPF_LOG_KERNEL;
  8080. err = btf_add_struct_ops(btf, st_ops, log);
  8081. errout:
  8082. kfree(log);
  8083. btf_put(btf);
  8084. return err;
  8085. }
  8086. EXPORT_SYMBOL_GPL(__register_bpf_struct_ops);
  8087. #endif
  8088. bool btf_param_match_suffix(const struct btf *btf,
  8089. const struct btf_param *arg,
  8090. const char *suffix)
  8091. {
  8092. int suffix_len = strlen(suffix), len;
  8093. const char *param_name;
  8094. /* In the future, this can be ported to use BTF tagging */
  8095. param_name = btf_name_by_offset(btf, arg->name_off);
  8096. if (str_is_empty(param_name))
  8097. return false;
  8098. len = strlen(param_name);
  8099. if (len <= suffix_len)
  8100. return false;
  8101. param_name += len - suffix_len;
  8102. return !strncmp(param_name, suffix, suffix_len);
  8103. }