btf.c 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561
  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/skmsg.h>
  22. #include <linux/perf_event.h>
  23. #include <linux/bsearch.h>
  24. #include <linux/btf_ids.h>
  25. #include <net/sock.h>
  26. /* BTF (BPF Type Format) is the meta data format which describes
  27. * the data types of BPF program/map. Hence, it basically focus
  28. * on the C programming language which the modern BPF is primary
  29. * using.
  30. *
  31. * ELF Section:
  32. * ~~~~~~~~~~~
  33. * The BTF data is stored under the ".BTF" ELF section
  34. *
  35. * struct btf_type:
  36. * ~~~~~~~~~~~~~~~
  37. * Each 'struct btf_type' object describes a C data type.
  38. * Depending on the type it is describing, a 'struct btf_type'
  39. * object may be followed by more data. F.e.
  40. * To describe an array, 'struct btf_type' is followed by
  41. * 'struct btf_array'.
  42. *
  43. * 'struct btf_type' and any extra data following it are
  44. * 4 bytes aligned.
  45. *
  46. * Type section:
  47. * ~~~~~~~~~~~~~
  48. * The BTF type section contains a list of 'struct btf_type' objects.
  49. * Each one describes a C type. Recall from the above section
  50. * that a 'struct btf_type' object could be immediately followed by extra
  51. * data in order to desribe some particular C types.
  52. *
  53. * type_id:
  54. * ~~~~~~~
  55. * Each btf_type object is identified by a type_id. The type_id
  56. * is implicitly implied by the location of the btf_type object in
  57. * the BTF type section. The first one has type_id 1. The second
  58. * one has type_id 2...etc. Hence, an earlier btf_type has
  59. * a smaller type_id.
  60. *
  61. * A btf_type object may refer to another btf_type object by using
  62. * type_id (i.e. the "type" in the "struct btf_type").
  63. *
  64. * NOTE that we cannot assume any reference-order.
  65. * A btf_type object can refer to an earlier btf_type object
  66. * but it can also refer to a later btf_type object.
  67. *
  68. * For example, to describe "const void *". A btf_type
  69. * object describing "const" may refer to another btf_type
  70. * object describing "void *". This type-reference is done
  71. * by specifying type_id:
  72. *
  73. * [1] CONST (anon) type_id=2
  74. * [2] PTR (anon) type_id=0
  75. *
  76. * The above is the btf_verifier debug log:
  77. * - Each line started with "[?]" is a btf_type object
  78. * - [?] is the type_id of the btf_type object.
  79. * - CONST/PTR is the BTF_KIND_XXX
  80. * - "(anon)" is the name of the type. It just
  81. * happens that CONST and PTR has no name.
  82. * - type_id=XXX is the 'u32 type' in btf_type
  83. *
  84. * NOTE: "void" has type_id 0
  85. *
  86. * String section:
  87. * ~~~~~~~~~~~~~~
  88. * The BTF string section contains the names used by the type section.
  89. * Each string is referred by an "offset" from the beginning of the
  90. * string section.
  91. *
  92. * Each string is '\0' terminated.
  93. *
  94. * The first character in the string section must be '\0'
  95. * which is used to mean 'anonymous'. Some btf_type may not
  96. * have a name.
  97. */
  98. /* BTF verification:
  99. *
  100. * To verify BTF data, two passes are needed.
  101. *
  102. * Pass #1
  103. * ~~~~~~~
  104. * The first pass is to collect all btf_type objects to
  105. * an array: "btf->types".
  106. *
  107. * Depending on the C type that a btf_type is describing,
  108. * a btf_type may be followed by extra data. We don't know
  109. * how many btf_type is there, and more importantly we don't
  110. * know where each btf_type is located in the type section.
  111. *
  112. * Without knowing the location of each type_id, most verifications
  113. * cannot be done. e.g. an earlier btf_type may refer to a later
  114. * btf_type (recall the "const void *" above), so we cannot
  115. * check this type-reference in the first pass.
  116. *
  117. * In the first pass, it still does some verifications (e.g.
  118. * checking the name is a valid offset to the string section).
  119. *
  120. * Pass #2
  121. * ~~~~~~~
  122. * The main focus is to resolve a btf_type that is referring
  123. * to another type.
  124. *
  125. * We have to ensure the referring type:
  126. * 1) does exist in the BTF (i.e. in btf->types[])
  127. * 2) does not cause a loop:
  128. * struct A {
  129. * struct B b;
  130. * };
  131. *
  132. * struct B {
  133. * struct A a;
  134. * };
  135. *
  136. * btf_type_needs_resolve() decides if a btf_type needs
  137. * to be resolved.
  138. *
  139. * The needs_resolve type implements the "resolve()" ops which
  140. * essentially does a DFS and detects backedge.
  141. *
  142. * During resolve (or DFS), different C types have different
  143. * "RESOLVED" conditions.
  144. *
  145. * When resolving a BTF_KIND_STRUCT, we need to resolve all its
  146. * members because a member is always referring to another
  147. * type. A struct's member can be treated as "RESOLVED" if
  148. * it is referring to a BTF_KIND_PTR. Otherwise, the
  149. * following valid C struct would be rejected:
  150. *
  151. * struct A {
  152. * int m;
  153. * struct A *a;
  154. * };
  155. *
  156. * When resolving a BTF_KIND_PTR, it needs to keep resolving if
  157. * it is referring to another BTF_KIND_PTR. Otherwise, we cannot
  158. * detect a pointer loop, e.g.:
  159. * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR +
  160. * ^ |
  161. * +-----------------------------------------+
  162. *
  163. */
  164. #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)
  165. #define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)
  166. #define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)
  167. #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)
  168. #define BITS_ROUNDUP_BYTES(bits) \
  169. (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits))
  170. #define BTF_INFO_MASK 0x8f00ffff
  171. #define BTF_INT_MASK 0x0fffffff
  172. #define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)
  173. #define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET)
  174. /* 16MB for 64k structs and each has 16 members and
  175. * a few MB spaces for the string section.
  176. * The hard limit is S32_MAX.
  177. */
  178. #define BTF_MAX_SIZE (16 * 1024 * 1024)
  179. #define for_each_member_from(i, from, struct_type, member) \
  180. for (i = from, member = btf_type_member(struct_type) + from; \
  181. i < btf_type_vlen(struct_type); \
  182. i++, member++)
  183. #define for_each_vsi_from(i, from, struct_type, member) \
  184. for (i = from, member = btf_type_var_secinfo(struct_type) + from; \
  185. i < btf_type_vlen(struct_type); \
  186. i++, member++)
  187. DEFINE_IDR(btf_idr);
  188. DEFINE_SPINLOCK(btf_idr_lock);
  189. struct btf {
  190. void *data;
  191. struct btf_type **types;
  192. u32 *resolved_ids;
  193. u32 *resolved_sizes;
  194. const char *strings;
  195. void *nohdr_data;
  196. struct btf_header hdr;
  197. u32 nr_types;
  198. u32 types_size;
  199. u32 data_size;
  200. refcount_t refcnt;
  201. u32 id;
  202. struct rcu_head rcu;
  203. };
  204. enum verifier_phase {
  205. CHECK_META,
  206. CHECK_TYPE,
  207. };
  208. struct resolve_vertex {
  209. const struct btf_type *t;
  210. u32 type_id;
  211. u16 next_member;
  212. };
  213. enum visit_state {
  214. NOT_VISITED,
  215. VISITED,
  216. RESOLVED,
  217. };
  218. enum resolve_mode {
  219. RESOLVE_TBD, /* To Be Determined */
  220. RESOLVE_PTR, /* Resolving for Pointer */
  221. RESOLVE_STRUCT_OR_ARRAY, /* Resolving for struct/union
  222. * or array
  223. */
  224. };
  225. #define MAX_RESOLVE_DEPTH 32
  226. struct btf_sec_info {
  227. u32 off;
  228. u32 len;
  229. };
  230. struct btf_verifier_env {
  231. struct btf *btf;
  232. u8 *visit_states;
  233. struct resolve_vertex stack[MAX_RESOLVE_DEPTH];
  234. struct bpf_verifier_log log;
  235. u32 log_type_id;
  236. u32 top_stack;
  237. enum verifier_phase phase;
  238. enum resolve_mode resolve_mode;
  239. };
  240. static const char * const btf_kind_str[NR_BTF_KINDS] = {
  241. [BTF_KIND_UNKN] = "UNKNOWN",
  242. [BTF_KIND_INT] = "INT",
  243. [BTF_KIND_PTR] = "PTR",
  244. [BTF_KIND_ARRAY] = "ARRAY",
  245. [BTF_KIND_STRUCT] = "STRUCT",
  246. [BTF_KIND_UNION] = "UNION",
  247. [BTF_KIND_ENUM] = "ENUM",
  248. [BTF_KIND_FWD] = "FWD",
  249. [BTF_KIND_TYPEDEF] = "TYPEDEF",
  250. [BTF_KIND_VOLATILE] = "VOLATILE",
  251. [BTF_KIND_CONST] = "CONST",
  252. [BTF_KIND_RESTRICT] = "RESTRICT",
  253. [BTF_KIND_FUNC] = "FUNC",
  254. [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
  255. [BTF_KIND_VAR] = "VAR",
  256. [BTF_KIND_DATASEC] = "DATASEC",
  257. };
  258. static const char *btf_type_str(const struct btf_type *t)
  259. {
  260. return btf_kind_str[BTF_INFO_KIND(t->info)];
  261. }
  262. /* Chunk size we use in safe copy of data to be shown. */
  263. #define BTF_SHOW_OBJ_SAFE_SIZE 32
  264. /*
  265. * This is the maximum size of a base type value (equivalent to a
  266. * 128-bit int); if we are at the end of our safe buffer and have
  267. * less than 16 bytes space we can't be assured of being able
  268. * to copy the next type safely, so in such cases we will initiate
  269. * a new copy.
  270. */
  271. #define BTF_SHOW_OBJ_BASE_TYPE_SIZE 16
  272. /* Type name size */
  273. #define BTF_SHOW_NAME_SIZE 80
  274. /*
  275. * Common data to all BTF show operations. Private show functions can add
  276. * their own data to a structure containing a struct btf_show and consult it
  277. * in the show callback. See btf_type_show() below.
  278. *
  279. * One challenge with showing nested data is we want to skip 0-valued
  280. * data, but in order to figure out whether a nested object is all zeros
  281. * we need to walk through it. As a result, we need to make two passes
  282. * when handling structs, unions and arrays; the first path simply looks
  283. * for nonzero data, while the second actually does the display. The first
  284. * pass is signalled by show->state.depth_check being set, and if we
  285. * encounter a non-zero value we set show->state.depth_to_show to
  286. * the depth at which we encountered it. When we have completed the
  287. * first pass, we will know if anything needs to be displayed if
  288. * depth_to_show > depth. See btf_[struct,array]_show() for the
  289. * implementation of this.
  290. *
  291. * Another problem is we want to ensure the data for display is safe to
  292. * access. To support this, the anonymous "struct {} obj" tracks the data
  293. * object and our safe copy of it. We copy portions of the data needed
  294. * to the object "copy" buffer, but because its size is limited to
  295. * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we
  296. * traverse larger objects for display.
  297. *
  298. * The various data type show functions all start with a call to
  299. * btf_show_start_type() which returns a pointer to the safe copy
  300. * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the
  301. * raw data itself). btf_show_obj_safe() is responsible for
  302. * using copy_from_kernel_nofault() to update the safe data if necessary
  303. * as we traverse the object's data. skbuff-like semantics are
  304. * used:
  305. *
  306. * - obj.head points to the start of the toplevel object for display
  307. * - obj.size is the size of the toplevel object
  308. * - obj.data points to the current point in the original data at
  309. * which our safe data starts. obj.data will advance as we copy
  310. * portions of the data.
  311. *
  312. * In most cases a single copy will suffice, but larger data structures
  313. * such as "struct task_struct" will require many copies. The logic in
  314. * btf_show_obj_safe() handles the logic that determines if a new
  315. * copy_from_kernel_nofault() is needed.
  316. */
  317. struct btf_show {
  318. u64 flags;
  319. void *target; /* target of show operation (seq file, buffer) */
  320. void (*showfn)(struct btf_show *show, const char *fmt, va_list args);
  321. const struct btf *btf;
  322. /* below are used during iteration */
  323. struct {
  324. u8 depth;
  325. u8 depth_to_show;
  326. u8 depth_check;
  327. u8 array_member:1,
  328. array_terminated:1;
  329. u16 array_encoding;
  330. u32 type_id;
  331. int status; /* non-zero for error */
  332. const struct btf_type *type;
  333. const struct btf_member *member;
  334. char name[BTF_SHOW_NAME_SIZE]; /* space for member name/type */
  335. } state;
  336. struct {
  337. u32 size;
  338. void *head;
  339. void *data;
  340. u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];
  341. } obj;
  342. };
  343. struct btf_kind_operations {
  344. s32 (*check_meta)(struct btf_verifier_env *env,
  345. const struct btf_type *t,
  346. u32 meta_left);
  347. int (*resolve)(struct btf_verifier_env *env,
  348. const struct resolve_vertex *v);
  349. int (*check_member)(struct btf_verifier_env *env,
  350. const struct btf_type *struct_type,
  351. const struct btf_member *member,
  352. const struct btf_type *member_type);
  353. int (*check_kflag_member)(struct btf_verifier_env *env,
  354. const struct btf_type *struct_type,
  355. const struct btf_member *member,
  356. const struct btf_type *member_type);
  357. void (*log_details)(struct btf_verifier_env *env,
  358. const struct btf_type *t);
  359. void (*show)(const struct btf *btf, const struct btf_type *t,
  360. u32 type_id, void *data, u8 bits_offsets,
  361. struct btf_show *show);
  362. };
  363. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];
  364. static struct btf_type btf_void;
  365. static int btf_resolve(struct btf_verifier_env *env,
  366. const struct btf_type *t, u32 type_id);
  367. static bool btf_type_is_modifier(const struct btf_type *t)
  368. {
  369. /* Some of them is not strictly a C modifier
  370. * but they are grouped into the same bucket
  371. * for BTF concern:
  372. * A type (t) that refers to another
  373. * type through t->type AND its size cannot
  374. * be determined without following the t->type.
  375. *
  376. * ptr does not fall into this bucket
  377. * because its size is always sizeof(void *).
  378. */
  379. switch (BTF_INFO_KIND(t->info)) {
  380. case BTF_KIND_TYPEDEF:
  381. case BTF_KIND_VOLATILE:
  382. case BTF_KIND_CONST:
  383. case BTF_KIND_RESTRICT:
  384. return true;
  385. }
  386. return false;
  387. }
  388. bool btf_type_is_void(const struct btf_type *t)
  389. {
  390. return t == &btf_void;
  391. }
  392. static bool btf_type_is_fwd(const struct btf_type *t)
  393. {
  394. return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
  395. }
  396. static bool btf_type_nosize(const struct btf_type *t)
  397. {
  398. return btf_type_is_void(t) || btf_type_is_fwd(t) ||
  399. btf_type_is_func(t) || btf_type_is_func_proto(t);
  400. }
  401. static bool btf_type_nosize_or_null(const struct btf_type *t)
  402. {
  403. return !t || btf_type_nosize(t);
  404. }
  405. static bool __btf_type_is_struct(const struct btf_type *t)
  406. {
  407. return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
  408. }
  409. static bool btf_type_is_array(const struct btf_type *t)
  410. {
  411. return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
  412. }
  413. static bool btf_type_is_datasec(const struct btf_type *t)
  414. {
  415. return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;
  416. }
  417. s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
  418. {
  419. const struct btf_type *t;
  420. const char *tname;
  421. u32 i;
  422. for (i = 1; i <= btf->nr_types; i++) {
  423. t = btf->types[i];
  424. if (BTF_INFO_KIND(t->info) != kind)
  425. continue;
  426. tname = btf_name_by_offset(btf, t->name_off);
  427. if (!strcmp(tname, name))
  428. return i;
  429. }
  430. return -ENOENT;
  431. }
  432. const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
  433. u32 id, u32 *res_id)
  434. {
  435. const struct btf_type *t = btf_type_by_id(btf, id);
  436. while (btf_type_is_modifier(t)) {
  437. id = t->type;
  438. t = btf_type_by_id(btf, t->type);
  439. }
  440. if (res_id)
  441. *res_id = id;
  442. return t;
  443. }
  444. const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
  445. u32 id, u32 *res_id)
  446. {
  447. const struct btf_type *t;
  448. t = btf_type_skip_modifiers(btf, id, NULL);
  449. if (!btf_type_is_ptr(t))
  450. return NULL;
  451. return btf_type_skip_modifiers(btf, t->type, res_id);
  452. }
  453. const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
  454. u32 id, u32 *res_id)
  455. {
  456. const struct btf_type *ptype;
  457. ptype = btf_type_resolve_ptr(btf, id, res_id);
  458. if (ptype && btf_type_is_func_proto(ptype))
  459. return ptype;
  460. return NULL;
  461. }
  462. /* Types that act only as a source, not sink or intermediate
  463. * type when resolving.
  464. */
  465. static bool btf_type_is_resolve_source_only(const struct btf_type *t)
  466. {
  467. return btf_type_is_var(t) ||
  468. btf_type_is_datasec(t);
  469. }
  470. /* What types need to be resolved?
  471. *
  472. * btf_type_is_modifier() is an obvious one.
  473. *
  474. * btf_type_is_struct() because its member refers to
  475. * another type (through member->type).
  476. *
  477. * btf_type_is_var() because the variable refers to
  478. * another type. btf_type_is_datasec() holds multiple
  479. * btf_type_is_var() types that need resolving.
  480. *
  481. * btf_type_is_array() because its element (array->type)
  482. * refers to another type. Array can be thought of a
  483. * special case of struct while array just has the same
  484. * member-type repeated by array->nelems of times.
  485. */
  486. static bool btf_type_needs_resolve(const struct btf_type *t)
  487. {
  488. return btf_type_is_modifier(t) ||
  489. btf_type_is_ptr(t) ||
  490. btf_type_is_struct(t) ||
  491. btf_type_is_array(t) ||
  492. btf_type_is_var(t) ||
  493. btf_type_is_datasec(t);
  494. }
  495. /* t->size can be used */
  496. static bool btf_type_has_size(const struct btf_type *t)
  497. {
  498. switch (BTF_INFO_KIND(t->info)) {
  499. case BTF_KIND_INT:
  500. case BTF_KIND_STRUCT:
  501. case BTF_KIND_UNION:
  502. case BTF_KIND_ENUM:
  503. case BTF_KIND_DATASEC:
  504. return true;
  505. }
  506. return false;
  507. }
  508. static const char *btf_int_encoding_str(u8 encoding)
  509. {
  510. if (encoding == 0)
  511. return "(none)";
  512. else if (encoding == BTF_INT_SIGNED)
  513. return "SIGNED";
  514. else if (encoding == BTF_INT_CHAR)
  515. return "CHAR";
  516. else if (encoding == BTF_INT_BOOL)
  517. return "BOOL";
  518. else
  519. return "UNKN";
  520. }
  521. static u32 btf_type_int(const struct btf_type *t)
  522. {
  523. return *(u32 *)(t + 1);
  524. }
  525. static const struct btf_array *btf_type_array(const struct btf_type *t)
  526. {
  527. return (const struct btf_array *)(t + 1);
  528. }
  529. static const struct btf_enum *btf_type_enum(const struct btf_type *t)
  530. {
  531. return (const struct btf_enum *)(t + 1);
  532. }
  533. static const struct btf_var *btf_type_var(const struct btf_type *t)
  534. {
  535. return (const struct btf_var *)(t + 1);
  536. }
  537. static const struct btf_kind_operations *btf_type_ops(const struct btf_type *t)
  538. {
  539. return kind_ops[BTF_INFO_KIND(t->info)];
  540. }
  541. static bool btf_name_offset_valid(const struct btf *btf, u32 offset)
  542. {
  543. return BTF_STR_OFFSET_VALID(offset) &&
  544. offset < btf->hdr.str_len;
  545. }
  546. static bool __btf_name_char_ok(char c, bool first, bool dot_ok)
  547. {
  548. if ((first ? !isalpha(c) :
  549. !isalnum(c)) &&
  550. c != '_' &&
  551. ((c == '.' && !dot_ok) ||
  552. c != '.'))
  553. return false;
  554. return true;
  555. }
  556. static bool __btf_name_valid(const struct btf *btf, u32 offset, bool dot_ok)
  557. {
  558. /* offset must be valid */
  559. const char *src = &btf->strings[offset];
  560. const char *src_limit;
  561. if (!__btf_name_char_ok(*src, true, dot_ok))
  562. return false;
  563. /* set a limit on identifier length */
  564. src_limit = src + KSYM_NAME_LEN;
  565. src++;
  566. while (*src && src < src_limit) {
  567. if (!__btf_name_char_ok(*src, false, dot_ok))
  568. return false;
  569. src++;
  570. }
  571. return !*src;
  572. }
  573. /* Only C-style identifier is permitted. This can be relaxed if
  574. * necessary.
  575. */
  576. static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)
  577. {
  578. return __btf_name_valid(btf, offset, false);
  579. }
  580. static bool btf_name_valid_section(const struct btf *btf, u32 offset)
  581. {
  582. return __btf_name_valid(btf, offset, true);
  583. }
  584. static const char *__btf_name_by_offset(const struct btf *btf, u32 offset)
  585. {
  586. if (!offset)
  587. return "(anon)";
  588. else if (offset < btf->hdr.str_len)
  589. return &btf->strings[offset];
  590. else
  591. return "(invalid-name-offset)";
  592. }
  593. const char *btf_name_by_offset(const struct btf *btf, u32 offset)
  594. {
  595. if (offset < btf->hdr.str_len)
  596. return &btf->strings[offset];
  597. return NULL;
  598. }
  599. const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
  600. {
  601. if (type_id > btf->nr_types)
  602. return NULL;
  603. return btf->types[type_id];
  604. }
  605. /*
  606. * Regular int is not a bit field and it must be either
  607. * u8/u16/u32/u64 or __int128.
  608. */
  609. static bool btf_type_int_is_regular(const struct btf_type *t)
  610. {
  611. u8 nr_bits, nr_bytes;
  612. u32 int_data;
  613. int_data = btf_type_int(t);
  614. nr_bits = BTF_INT_BITS(int_data);
  615. nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
  616. if (BITS_PER_BYTE_MASKED(nr_bits) ||
  617. BTF_INT_OFFSET(int_data) ||
  618. (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
  619. nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
  620. nr_bytes != (2 * sizeof(u64)))) {
  621. return false;
  622. }
  623. return true;
  624. }
  625. /*
  626. * Check that given struct member is a regular int with expected
  627. * offset and size.
  628. */
  629. bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
  630. const struct btf_member *m,
  631. u32 expected_offset, u32 expected_size)
  632. {
  633. const struct btf_type *t;
  634. u32 id, int_data;
  635. u8 nr_bits;
  636. id = m->type;
  637. t = btf_type_id_size(btf, &id, NULL);
  638. if (!t || !btf_type_is_int(t))
  639. return false;
  640. int_data = btf_type_int(t);
  641. nr_bits = BTF_INT_BITS(int_data);
  642. if (btf_type_kflag(s)) {
  643. u32 bitfield_size = BTF_MEMBER_BITFIELD_SIZE(m->offset);
  644. u32 bit_offset = BTF_MEMBER_BIT_OFFSET(m->offset);
  645. /* if kflag set, int should be a regular int and
  646. * bit offset should be at byte boundary.
  647. */
  648. return !bitfield_size &&
  649. BITS_ROUNDUP_BYTES(bit_offset) == expected_offset &&
  650. BITS_ROUNDUP_BYTES(nr_bits) == expected_size;
  651. }
  652. if (BTF_INT_OFFSET(int_data) ||
  653. BITS_PER_BYTE_MASKED(m->offset) ||
  654. BITS_ROUNDUP_BYTES(m->offset) != expected_offset ||
  655. BITS_PER_BYTE_MASKED(nr_bits) ||
  656. BITS_ROUNDUP_BYTES(nr_bits) != expected_size)
  657. return false;
  658. return true;
  659. }
  660. /* Similar to btf_type_skip_modifiers() but does not skip typedefs. */
  661. static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf,
  662. u32 id)
  663. {
  664. const struct btf_type *t = btf_type_by_id(btf, id);
  665. while (btf_type_is_modifier(t) &&
  666. BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) {
  667. id = t->type;
  668. t = btf_type_by_id(btf, t->type);
  669. }
  670. return t;
  671. }
  672. #define BTF_SHOW_MAX_ITER 10
  673. #define BTF_KIND_BIT(kind) (1ULL << kind)
  674. /*
  675. * Populate show->state.name with type name information.
  676. * Format of type name is
  677. *
  678. * [.member_name = ] (type_name)
  679. */
  680. static const char *btf_show_name(struct btf_show *show)
  681. {
  682. /* BTF_MAX_ITER array suffixes "[]" */
  683. const char *array_suffixes = "[][][][][][][][][][]";
  684. const char *array_suffix = &array_suffixes[strlen(array_suffixes)];
  685. /* BTF_MAX_ITER pointer suffixes "*" */
  686. const char *ptr_suffixes = "**********";
  687. const char *ptr_suffix = &ptr_suffixes[strlen(ptr_suffixes)];
  688. const char *name = NULL, *prefix = "", *parens = "";
  689. const struct btf_member *m = show->state.member;
  690. const struct btf_type *t = show->state.type;
  691. const struct btf_array *array;
  692. u32 id = show->state.type_id;
  693. const char *member = NULL;
  694. bool show_member = false;
  695. u64 kinds = 0;
  696. int i;
  697. show->state.name[0] = '\0';
  698. /*
  699. * Don't show type name if we're showing an array member;
  700. * in that case we show the array type so don't need to repeat
  701. * ourselves for each member.
  702. */
  703. if (show->state.array_member)
  704. return "";
  705. /* Retrieve member name, if any. */
  706. if (m) {
  707. member = btf_name_by_offset(show->btf, m->name_off);
  708. show_member = strlen(member) > 0;
  709. id = m->type;
  710. }
  711. /*
  712. * Start with type_id, as we have resolved the struct btf_type *
  713. * via btf_modifier_show() past the parent typedef to the child
  714. * struct, int etc it is defined as. In such cases, the type_id
  715. * still represents the starting type while the struct btf_type *
  716. * in our show->state points at the resolved type of the typedef.
  717. */
  718. t = btf_type_by_id(show->btf, id);
  719. if (!t)
  720. return "";
  721. /*
  722. * The goal here is to build up the right number of pointer and
  723. * array suffixes while ensuring the type name for a typedef
  724. * is represented. Along the way we accumulate a list of
  725. * BTF kinds we have encountered, since these will inform later
  726. * display; for example, pointer types will not require an
  727. * opening "{" for struct, we will just display the pointer value.
  728. *
  729. * We also want to accumulate the right number of pointer or array
  730. * indices in the format string while iterating until we get to
  731. * the typedef/pointee/array member target type.
  732. *
  733. * We start by pointing at the end of pointer and array suffix
  734. * strings; as we accumulate pointers and arrays we move the pointer
  735. * or array string backwards so it will show the expected number of
  736. * '*' or '[]' for the type. BTF_SHOW_MAX_ITER of nesting of pointers
  737. * and/or arrays and typedefs are supported as a precaution.
  738. *
  739. * We also want to get typedef name while proceeding to resolve
  740. * type it points to so that we can add parentheses if it is a
  741. * "typedef struct" etc.
  742. */
  743. for (i = 0; i < BTF_SHOW_MAX_ITER; i++) {
  744. switch (BTF_INFO_KIND(t->info)) {
  745. case BTF_KIND_TYPEDEF:
  746. if (!name)
  747. name = btf_name_by_offset(show->btf,
  748. t->name_off);
  749. kinds |= BTF_KIND_BIT(BTF_KIND_TYPEDEF);
  750. id = t->type;
  751. break;
  752. case BTF_KIND_ARRAY:
  753. kinds |= BTF_KIND_BIT(BTF_KIND_ARRAY);
  754. parens = "[";
  755. if (!t)
  756. return "";
  757. array = btf_type_array(t);
  758. if (array_suffix > array_suffixes)
  759. array_suffix -= 2;
  760. id = array->type;
  761. break;
  762. case BTF_KIND_PTR:
  763. kinds |= BTF_KIND_BIT(BTF_KIND_PTR);
  764. if (ptr_suffix > ptr_suffixes)
  765. ptr_suffix -= 1;
  766. id = t->type;
  767. break;
  768. default:
  769. id = 0;
  770. break;
  771. }
  772. if (!id)
  773. break;
  774. t = btf_type_skip_qualifiers(show->btf, id);
  775. }
  776. /* We may not be able to represent this type; bail to be safe */
  777. if (i == BTF_SHOW_MAX_ITER)
  778. return "";
  779. if (!name)
  780. name = btf_name_by_offset(show->btf, t->name_off);
  781. switch (BTF_INFO_KIND(t->info)) {
  782. case BTF_KIND_STRUCT:
  783. case BTF_KIND_UNION:
  784. prefix = BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT ?
  785. "struct" : "union";
  786. /* if it's an array of struct/union, parens is already set */
  787. if (!(kinds & (BTF_KIND_BIT(BTF_KIND_ARRAY))))
  788. parens = "{";
  789. break;
  790. case BTF_KIND_ENUM:
  791. prefix = "enum";
  792. break;
  793. default:
  794. break;
  795. }
  796. /* pointer does not require parens */
  797. if (kinds & BTF_KIND_BIT(BTF_KIND_PTR))
  798. parens = "";
  799. /* typedef does not require struct/union/enum prefix */
  800. if (kinds & BTF_KIND_BIT(BTF_KIND_TYPEDEF))
  801. prefix = "";
  802. if (!name)
  803. name = "";
  804. /* Even if we don't want type name info, we want parentheses etc */
  805. if (show->flags & BTF_SHOW_NONAME)
  806. snprintf(show->state.name, sizeof(show->state.name), "%s",
  807. parens);
  808. else
  809. snprintf(show->state.name, sizeof(show->state.name),
  810. "%s%s%s(%s%s%s%s%s%s)%s",
  811. /* first 3 strings comprise ".member = " */
  812. show_member ? "." : "",
  813. show_member ? member : "",
  814. show_member ? " = " : "",
  815. /* ...next is our prefix (struct, enum, etc) */
  816. prefix,
  817. strlen(prefix) > 0 && strlen(name) > 0 ? " " : "",
  818. /* ...this is the type name itself */
  819. name,
  820. /* ...suffixed by the appropriate '*', '[]' suffixes */
  821. strlen(ptr_suffix) > 0 ? " " : "", ptr_suffix,
  822. array_suffix, parens);
  823. return show->state.name;
  824. }
  825. static const char *__btf_show_indent(struct btf_show *show)
  826. {
  827. const char *indents = " ";
  828. const char *indent = &indents[strlen(indents)];
  829. if ((indent - show->state.depth) >= indents)
  830. return indent - show->state.depth;
  831. return indents;
  832. }
  833. static const char *btf_show_indent(struct btf_show *show)
  834. {
  835. return show->flags & BTF_SHOW_COMPACT ? "" : __btf_show_indent(show);
  836. }
  837. static const char *btf_show_newline(struct btf_show *show)
  838. {
  839. return show->flags & BTF_SHOW_COMPACT ? "" : "\n";
  840. }
  841. static const char *btf_show_delim(struct btf_show *show)
  842. {
  843. if (show->state.depth == 0)
  844. return "";
  845. if ((show->flags & BTF_SHOW_COMPACT) && show->state.type &&
  846. BTF_INFO_KIND(show->state.type->info) == BTF_KIND_UNION)
  847. return "|";
  848. return ",";
  849. }
  850. __printf(2, 3) static void btf_show(struct btf_show *show, const char *fmt, ...)
  851. {
  852. va_list args;
  853. if (!show->state.depth_check) {
  854. va_start(args, fmt);
  855. show->showfn(show, fmt, args);
  856. va_end(args);
  857. }
  858. }
  859. /* Macros are used here as btf_show_type_value[s]() prepends and appends
  860. * format specifiers to the format specifier passed in; these do the work of
  861. * adding indentation, delimiters etc while the caller simply has to specify
  862. * the type value(s) in the format specifier + value(s).
  863. */
  864. #define btf_show_type_value(show, fmt, value) \
  865. do { \
  866. if ((value) != 0 || (show->flags & BTF_SHOW_ZERO) || \
  867. show->state.depth == 0) { \
  868. btf_show(show, "%s%s" fmt "%s%s", \
  869. btf_show_indent(show), \
  870. btf_show_name(show), \
  871. value, btf_show_delim(show), \
  872. btf_show_newline(show)); \
  873. if (show->state.depth > show->state.depth_to_show) \
  874. show->state.depth_to_show = show->state.depth; \
  875. } \
  876. } while (0)
  877. #define btf_show_type_values(show, fmt, ...) \
  878. do { \
  879. btf_show(show, "%s%s" fmt "%s%s", btf_show_indent(show), \
  880. btf_show_name(show), \
  881. __VA_ARGS__, btf_show_delim(show), \
  882. btf_show_newline(show)); \
  883. if (show->state.depth > show->state.depth_to_show) \
  884. show->state.depth_to_show = show->state.depth; \
  885. } while (0)
  886. /* How much is left to copy to safe buffer after @data? */
  887. static int btf_show_obj_size_left(struct btf_show *show, void *data)
  888. {
  889. return show->obj.head + show->obj.size - data;
  890. }
  891. /* Is object pointed to by @data of @size already copied to our safe buffer? */
  892. static bool btf_show_obj_is_safe(struct btf_show *show, void *data, int size)
  893. {
  894. return data >= show->obj.data &&
  895. (data + size) < (show->obj.data + BTF_SHOW_OBJ_SAFE_SIZE);
  896. }
  897. /*
  898. * If object pointed to by @data of @size falls within our safe buffer, return
  899. * the equivalent pointer to the same safe data. Assumes
  900. * copy_from_kernel_nofault() has already happened and our safe buffer is
  901. * populated.
  902. */
  903. static void *__btf_show_obj_safe(struct btf_show *show, void *data, int size)
  904. {
  905. if (btf_show_obj_is_safe(show, data, size))
  906. return show->obj.safe + (data - show->obj.data);
  907. return NULL;
  908. }
  909. /*
  910. * Return a safe-to-access version of data pointed to by @data.
  911. * We do this by copying the relevant amount of information
  912. * to the struct btf_show obj.safe buffer using copy_from_kernel_nofault().
  913. *
  914. * If BTF_SHOW_UNSAFE is specified, just return data as-is; no
  915. * safe copy is needed.
  916. *
  917. * Otherwise we need to determine if we have the required amount
  918. * of data (determined by the @data pointer and the size of the
  919. * largest base type we can encounter (represented by
  920. * BTF_SHOW_OBJ_BASE_TYPE_SIZE). Having that much data ensures
  921. * that we will be able to print some of the current object,
  922. * and if more is needed a copy will be triggered.
  923. * Some objects such as structs will not fit into the buffer;
  924. * in such cases additional copies when we iterate over their
  925. * members may be needed.
  926. *
  927. * btf_show_obj_safe() is used to return a safe buffer for
  928. * btf_show_start_type(); this ensures that as we recurse into
  929. * nested types we always have safe data for the given type.
  930. * This approach is somewhat wasteful; it's possible for example
  931. * that when iterating over a large union we'll end up copying the
  932. * same data repeatedly, but the goal is safety not performance.
  933. * We use stack data as opposed to per-CPU buffers because the
  934. * iteration over a type can take some time, and preemption handling
  935. * would greatly complicate use of the safe buffer.
  936. */
  937. static void *btf_show_obj_safe(struct btf_show *show,
  938. const struct btf_type *t,
  939. void *data)
  940. {
  941. const struct btf_type *rt;
  942. int size_left, size;
  943. void *safe = NULL;
  944. if (show->flags & BTF_SHOW_UNSAFE)
  945. return data;
  946. rt = btf_resolve_size(show->btf, t, &size);
  947. if (IS_ERR(rt)) {
  948. show->state.status = PTR_ERR(rt);
  949. return NULL;
  950. }
  951. /*
  952. * Is this toplevel object? If so, set total object size and
  953. * initialize pointers. Otherwise check if we still fall within
  954. * our safe object data.
  955. */
  956. if (show->state.depth == 0) {
  957. show->obj.size = size;
  958. show->obj.head = data;
  959. } else {
  960. /*
  961. * If the size of the current object is > our remaining
  962. * safe buffer we _may_ need to do a new copy. However
  963. * consider the case of a nested struct; it's size pushes
  964. * us over the safe buffer limit, but showing any individual
  965. * struct members does not. In such cases, we don't need
  966. * to initiate a fresh copy yet; however we definitely need
  967. * at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes left
  968. * in our buffer, regardless of the current object size.
  969. * The logic here is that as we resolve types we will
  970. * hit a base type at some point, and we need to be sure
  971. * the next chunk of data is safely available to display
  972. * that type info safely. We cannot rely on the size of
  973. * the current object here because it may be much larger
  974. * than our current buffer (e.g. task_struct is 8k).
  975. * All we want to do here is ensure that we can print the
  976. * next basic type, which we can if either
  977. * - the current type size is within the safe buffer; or
  978. * - at least BTF_SHOW_OBJ_BASE_TYPE_SIZE bytes are left in
  979. * the safe buffer.
  980. */
  981. safe = __btf_show_obj_safe(show, data,
  982. min(size,
  983. BTF_SHOW_OBJ_BASE_TYPE_SIZE));
  984. }
  985. /*
  986. * We need a new copy to our safe object, either because we haven't
  987. * yet copied and are intializing safe data, or because the data
  988. * we want falls outside the boundaries of the safe object.
  989. */
  990. if (!safe) {
  991. size_left = btf_show_obj_size_left(show, data);
  992. if (size_left > BTF_SHOW_OBJ_SAFE_SIZE)
  993. size_left = BTF_SHOW_OBJ_SAFE_SIZE;
  994. show->state.status = copy_from_kernel_nofault(show->obj.safe,
  995. data, size_left);
  996. if (!show->state.status) {
  997. show->obj.data = data;
  998. safe = show->obj.safe;
  999. }
  1000. }
  1001. return safe;
  1002. }
  1003. /*
  1004. * Set the type we are starting to show and return a safe data pointer
  1005. * to be used for showing the associated data.
  1006. */
  1007. static void *btf_show_start_type(struct btf_show *show,
  1008. const struct btf_type *t,
  1009. u32 type_id, void *data)
  1010. {
  1011. show->state.type = t;
  1012. show->state.type_id = type_id;
  1013. show->state.name[0] = '\0';
  1014. return btf_show_obj_safe(show, t, data);
  1015. }
  1016. static void btf_show_end_type(struct btf_show *show)
  1017. {
  1018. show->state.type = NULL;
  1019. show->state.type_id = 0;
  1020. show->state.name[0] = '\0';
  1021. }
  1022. static void *btf_show_start_aggr_type(struct btf_show *show,
  1023. const struct btf_type *t,
  1024. u32 type_id, void *data)
  1025. {
  1026. void *safe_data = btf_show_start_type(show, t, type_id, data);
  1027. if (!safe_data)
  1028. return safe_data;
  1029. btf_show(show, "%s%s%s", btf_show_indent(show),
  1030. btf_show_name(show),
  1031. btf_show_newline(show));
  1032. show->state.depth++;
  1033. return safe_data;
  1034. }
  1035. static void btf_show_end_aggr_type(struct btf_show *show,
  1036. const char *suffix)
  1037. {
  1038. show->state.depth--;
  1039. btf_show(show, "%s%s%s%s", btf_show_indent(show), suffix,
  1040. btf_show_delim(show), btf_show_newline(show));
  1041. btf_show_end_type(show);
  1042. }
  1043. static void btf_show_start_member(struct btf_show *show,
  1044. const struct btf_member *m)
  1045. {
  1046. show->state.member = m;
  1047. }
  1048. static void btf_show_start_array_member(struct btf_show *show)
  1049. {
  1050. show->state.array_member = 1;
  1051. btf_show_start_member(show, NULL);
  1052. }
  1053. static void btf_show_end_member(struct btf_show *show)
  1054. {
  1055. show->state.member = NULL;
  1056. }
  1057. static void btf_show_end_array_member(struct btf_show *show)
  1058. {
  1059. show->state.array_member = 0;
  1060. btf_show_end_member(show);
  1061. }
  1062. static void *btf_show_start_array_type(struct btf_show *show,
  1063. const struct btf_type *t,
  1064. u32 type_id,
  1065. u16 array_encoding,
  1066. void *data)
  1067. {
  1068. show->state.array_encoding = array_encoding;
  1069. show->state.array_terminated = 0;
  1070. return btf_show_start_aggr_type(show, t, type_id, data);
  1071. }
  1072. static void btf_show_end_array_type(struct btf_show *show)
  1073. {
  1074. show->state.array_encoding = 0;
  1075. show->state.array_terminated = 0;
  1076. btf_show_end_aggr_type(show, "]");
  1077. }
  1078. static void *btf_show_start_struct_type(struct btf_show *show,
  1079. const struct btf_type *t,
  1080. u32 type_id,
  1081. void *data)
  1082. {
  1083. return btf_show_start_aggr_type(show, t, type_id, data);
  1084. }
  1085. static void btf_show_end_struct_type(struct btf_show *show)
  1086. {
  1087. btf_show_end_aggr_type(show, "}");
  1088. }
  1089. __printf(2, 3) static void __btf_verifier_log(struct bpf_verifier_log *log,
  1090. const char *fmt, ...)
  1091. {
  1092. va_list args;
  1093. va_start(args, fmt);
  1094. bpf_verifier_vlog(log, fmt, args);
  1095. va_end(args);
  1096. }
  1097. __printf(2, 3) static void btf_verifier_log(struct btf_verifier_env *env,
  1098. const char *fmt, ...)
  1099. {
  1100. struct bpf_verifier_log *log = &env->log;
  1101. va_list args;
  1102. if (!bpf_verifier_log_needed(log))
  1103. return;
  1104. va_start(args, fmt);
  1105. bpf_verifier_vlog(log, fmt, args);
  1106. va_end(args);
  1107. }
  1108. __printf(4, 5) static void __btf_verifier_log_type(struct btf_verifier_env *env,
  1109. const struct btf_type *t,
  1110. bool log_details,
  1111. const char *fmt, ...)
  1112. {
  1113. struct bpf_verifier_log *log = &env->log;
  1114. u8 kind = BTF_INFO_KIND(t->info);
  1115. struct btf *btf = env->btf;
  1116. va_list args;
  1117. if (!bpf_verifier_log_needed(log))
  1118. return;
  1119. /* btf verifier prints all types it is processing via
  1120. * btf_verifier_log_type(..., fmt = NULL).
  1121. * Skip those prints for in-kernel BTF verification.
  1122. */
  1123. if (log->level == BPF_LOG_KERNEL && !fmt)
  1124. return;
  1125. __btf_verifier_log(log, "[%u] %s %s%s",
  1126. env->log_type_id,
  1127. btf_kind_str[kind],
  1128. __btf_name_by_offset(btf, t->name_off),
  1129. log_details ? " " : "");
  1130. if (log_details)
  1131. btf_type_ops(t)->log_details(env, t);
  1132. if (fmt && *fmt) {
  1133. __btf_verifier_log(log, " ");
  1134. va_start(args, fmt);
  1135. bpf_verifier_vlog(log, fmt, args);
  1136. va_end(args);
  1137. }
  1138. __btf_verifier_log(log, "\n");
  1139. }
  1140. #define btf_verifier_log_type(env, t, ...) \
  1141. __btf_verifier_log_type((env), (t), true, __VA_ARGS__)
  1142. #define btf_verifier_log_basic(env, t, ...) \
  1143. __btf_verifier_log_type((env), (t), false, __VA_ARGS__)
  1144. __printf(4, 5)
  1145. static void btf_verifier_log_member(struct btf_verifier_env *env,
  1146. const struct btf_type *struct_type,
  1147. const struct btf_member *member,
  1148. const char *fmt, ...)
  1149. {
  1150. struct bpf_verifier_log *log = &env->log;
  1151. struct btf *btf = env->btf;
  1152. va_list args;
  1153. if (!bpf_verifier_log_needed(log))
  1154. return;
  1155. if (log->level == BPF_LOG_KERNEL && !fmt)
  1156. return;
  1157. /* The CHECK_META phase already did a btf dump.
  1158. *
  1159. * If member is logged again, it must hit an error in
  1160. * parsing this member. It is useful to print out which
  1161. * struct this member belongs to.
  1162. */
  1163. if (env->phase != CHECK_META)
  1164. btf_verifier_log_type(env, struct_type, NULL);
  1165. if (btf_type_kflag(struct_type))
  1166. __btf_verifier_log(log,
  1167. "\t%s type_id=%u bitfield_size=%u bits_offset=%u",
  1168. __btf_name_by_offset(btf, member->name_off),
  1169. member->type,
  1170. BTF_MEMBER_BITFIELD_SIZE(member->offset),
  1171. BTF_MEMBER_BIT_OFFSET(member->offset));
  1172. else
  1173. __btf_verifier_log(log, "\t%s type_id=%u bits_offset=%u",
  1174. __btf_name_by_offset(btf, member->name_off),
  1175. member->type, member->offset);
  1176. if (fmt && *fmt) {
  1177. __btf_verifier_log(log, " ");
  1178. va_start(args, fmt);
  1179. bpf_verifier_vlog(log, fmt, args);
  1180. va_end(args);
  1181. }
  1182. __btf_verifier_log(log, "\n");
  1183. }
  1184. __printf(4, 5)
  1185. static void btf_verifier_log_vsi(struct btf_verifier_env *env,
  1186. const struct btf_type *datasec_type,
  1187. const struct btf_var_secinfo *vsi,
  1188. const char *fmt, ...)
  1189. {
  1190. struct bpf_verifier_log *log = &env->log;
  1191. va_list args;
  1192. if (!bpf_verifier_log_needed(log))
  1193. return;
  1194. if (log->level == BPF_LOG_KERNEL && !fmt)
  1195. return;
  1196. if (env->phase != CHECK_META)
  1197. btf_verifier_log_type(env, datasec_type, NULL);
  1198. __btf_verifier_log(log, "\t type_id=%u offset=%u size=%u",
  1199. vsi->type, vsi->offset, vsi->size);
  1200. if (fmt && *fmt) {
  1201. __btf_verifier_log(log, " ");
  1202. va_start(args, fmt);
  1203. bpf_verifier_vlog(log, fmt, args);
  1204. va_end(args);
  1205. }
  1206. __btf_verifier_log(log, "\n");
  1207. }
  1208. static void btf_verifier_log_hdr(struct btf_verifier_env *env,
  1209. u32 btf_data_size)
  1210. {
  1211. struct bpf_verifier_log *log = &env->log;
  1212. const struct btf *btf = env->btf;
  1213. const struct btf_header *hdr;
  1214. if (!bpf_verifier_log_needed(log))
  1215. return;
  1216. if (log->level == BPF_LOG_KERNEL)
  1217. return;
  1218. hdr = &btf->hdr;
  1219. __btf_verifier_log(log, "magic: 0x%x\n", hdr->magic);
  1220. __btf_verifier_log(log, "version: %u\n", hdr->version);
  1221. __btf_verifier_log(log, "flags: 0x%x\n", hdr->flags);
  1222. __btf_verifier_log(log, "hdr_len: %u\n", hdr->hdr_len);
  1223. __btf_verifier_log(log, "type_off: %u\n", hdr->type_off);
  1224. __btf_verifier_log(log, "type_len: %u\n", hdr->type_len);
  1225. __btf_verifier_log(log, "str_off: %u\n", hdr->str_off);
  1226. __btf_verifier_log(log, "str_len: %u\n", hdr->str_len);
  1227. __btf_verifier_log(log, "btf_total_size: %u\n", btf_data_size);
  1228. }
  1229. static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
  1230. {
  1231. struct btf *btf = env->btf;
  1232. /* < 2 because +1 for btf_void which is always in btf->types[0].
  1233. * btf_void is not accounted in btf->nr_types because btf_void
  1234. * does not come from the BTF file.
  1235. */
  1236. if (btf->types_size - btf->nr_types < 2) {
  1237. /* Expand 'types' array */
  1238. struct btf_type **new_types;
  1239. u32 expand_by, new_size;
  1240. if (btf->types_size == BTF_MAX_TYPE) {
  1241. btf_verifier_log(env, "Exceeded max num of types");
  1242. return -E2BIG;
  1243. }
  1244. expand_by = max_t(u32, btf->types_size >> 2, 16);
  1245. new_size = min_t(u32, BTF_MAX_TYPE,
  1246. btf->types_size + expand_by);
  1247. new_types = kvcalloc(new_size, sizeof(*new_types),
  1248. GFP_KERNEL | __GFP_NOWARN);
  1249. if (!new_types)
  1250. return -ENOMEM;
  1251. if (btf->nr_types == 0)
  1252. new_types[0] = &btf_void;
  1253. else
  1254. memcpy(new_types, btf->types,
  1255. sizeof(*btf->types) * (btf->nr_types + 1));
  1256. kvfree(btf->types);
  1257. btf->types = new_types;
  1258. btf->types_size = new_size;
  1259. }
  1260. btf->types[++(btf->nr_types)] = t;
  1261. return 0;
  1262. }
  1263. static int btf_alloc_id(struct btf *btf)
  1264. {
  1265. int id;
  1266. idr_preload(GFP_KERNEL);
  1267. spin_lock_bh(&btf_idr_lock);
  1268. id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
  1269. if (id > 0)
  1270. btf->id = id;
  1271. spin_unlock_bh(&btf_idr_lock);
  1272. idr_preload_end();
  1273. if (WARN_ON_ONCE(!id))
  1274. return -ENOSPC;
  1275. return id > 0 ? 0 : id;
  1276. }
  1277. static void btf_free_id(struct btf *btf)
  1278. {
  1279. unsigned long flags;
  1280. /*
  1281. * In map-in-map, calling map_delete_elem() on outer
  1282. * map will call bpf_map_put on the inner map.
  1283. * It will then eventually call btf_free_id()
  1284. * on the inner map. Some of the map_delete_elem()
  1285. * implementation may have irq disabled, so
  1286. * we need to use the _irqsave() version instead
  1287. * of the _bh() version.
  1288. */
  1289. spin_lock_irqsave(&btf_idr_lock, flags);
  1290. idr_remove(&btf_idr, btf->id);
  1291. spin_unlock_irqrestore(&btf_idr_lock, flags);
  1292. }
  1293. static void btf_free(struct btf *btf)
  1294. {
  1295. kvfree(btf->types);
  1296. kvfree(btf->resolved_sizes);
  1297. kvfree(btf->resolved_ids);
  1298. kvfree(btf->data);
  1299. kfree(btf);
  1300. }
  1301. static void btf_free_rcu(struct rcu_head *rcu)
  1302. {
  1303. struct btf *btf = container_of(rcu, struct btf, rcu);
  1304. btf_free(btf);
  1305. }
  1306. void btf_put(struct btf *btf)
  1307. {
  1308. if (btf && refcount_dec_and_test(&btf->refcnt)) {
  1309. btf_free_id(btf);
  1310. call_rcu(&btf->rcu, btf_free_rcu);
  1311. }
  1312. }
  1313. static int env_resolve_init(struct btf_verifier_env *env)
  1314. {
  1315. struct btf *btf = env->btf;
  1316. u32 nr_types = btf->nr_types;
  1317. u32 *resolved_sizes = NULL;
  1318. u32 *resolved_ids = NULL;
  1319. u8 *visit_states = NULL;
  1320. /* +1 for btf_void */
  1321. resolved_sizes = kvcalloc(nr_types + 1, sizeof(*resolved_sizes),
  1322. GFP_KERNEL | __GFP_NOWARN);
  1323. if (!resolved_sizes)
  1324. goto nomem;
  1325. resolved_ids = kvcalloc(nr_types + 1, sizeof(*resolved_ids),
  1326. GFP_KERNEL | __GFP_NOWARN);
  1327. if (!resolved_ids)
  1328. goto nomem;
  1329. visit_states = kvcalloc(nr_types + 1, sizeof(*visit_states),
  1330. GFP_KERNEL | __GFP_NOWARN);
  1331. if (!visit_states)
  1332. goto nomem;
  1333. btf->resolved_sizes = resolved_sizes;
  1334. btf->resolved_ids = resolved_ids;
  1335. env->visit_states = visit_states;
  1336. return 0;
  1337. nomem:
  1338. kvfree(resolved_sizes);
  1339. kvfree(resolved_ids);
  1340. kvfree(visit_states);
  1341. return -ENOMEM;
  1342. }
  1343. static void btf_verifier_env_free(struct btf_verifier_env *env)
  1344. {
  1345. kvfree(env->visit_states);
  1346. kfree(env);
  1347. }
  1348. static bool env_type_is_resolve_sink(const struct btf_verifier_env *env,
  1349. const struct btf_type *next_type)
  1350. {
  1351. switch (env->resolve_mode) {
  1352. case RESOLVE_TBD:
  1353. /* int, enum or void is a sink */
  1354. return !btf_type_needs_resolve(next_type);
  1355. case RESOLVE_PTR:
  1356. /* int, enum, void, struct, array, func or func_proto is a sink
  1357. * for ptr
  1358. */
  1359. return !btf_type_is_modifier(next_type) &&
  1360. !btf_type_is_ptr(next_type);
  1361. case RESOLVE_STRUCT_OR_ARRAY:
  1362. /* int, enum, void, ptr, func or func_proto is a sink
  1363. * for struct and array
  1364. */
  1365. return !btf_type_is_modifier(next_type) &&
  1366. !btf_type_is_array(next_type) &&
  1367. !btf_type_is_struct(next_type);
  1368. default:
  1369. BUG();
  1370. }
  1371. }
  1372. static bool env_type_is_resolved(const struct btf_verifier_env *env,
  1373. u32 type_id)
  1374. {
  1375. return env->visit_states[type_id] == RESOLVED;
  1376. }
  1377. static int env_stack_push(struct btf_verifier_env *env,
  1378. const struct btf_type *t, u32 type_id)
  1379. {
  1380. struct resolve_vertex *v;
  1381. if (env->top_stack == MAX_RESOLVE_DEPTH)
  1382. return -E2BIG;
  1383. if (env->visit_states[type_id] != NOT_VISITED)
  1384. return -EEXIST;
  1385. env->visit_states[type_id] = VISITED;
  1386. v = &env->stack[env->top_stack++];
  1387. v->t = t;
  1388. v->type_id = type_id;
  1389. v->next_member = 0;
  1390. if (env->resolve_mode == RESOLVE_TBD) {
  1391. if (btf_type_is_ptr(t))
  1392. env->resolve_mode = RESOLVE_PTR;
  1393. else if (btf_type_is_struct(t) || btf_type_is_array(t))
  1394. env->resolve_mode = RESOLVE_STRUCT_OR_ARRAY;
  1395. }
  1396. return 0;
  1397. }
  1398. static void env_stack_set_next_member(struct btf_verifier_env *env,
  1399. u16 next_member)
  1400. {
  1401. env->stack[env->top_stack - 1].next_member = next_member;
  1402. }
  1403. static void env_stack_pop_resolved(struct btf_verifier_env *env,
  1404. u32 resolved_type_id,
  1405. u32 resolved_size)
  1406. {
  1407. u32 type_id = env->stack[--(env->top_stack)].type_id;
  1408. struct btf *btf = env->btf;
  1409. btf->resolved_sizes[type_id] = resolved_size;
  1410. btf->resolved_ids[type_id] = resolved_type_id;
  1411. env->visit_states[type_id] = RESOLVED;
  1412. }
  1413. static const struct resolve_vertex *env_stack_peak(struct btf_verifier_env *env)
  1414. {
  1415. return env->top_stack ? &env->stack[env->top_stack - 1] : NULL;
  1416. }
  1417. /* Resolve the size of a passed-in "type"
  1418. *
  1419. * type: is an array (e.g. u32 array[x][y])
  1420. * return type: type "u32[x][y]", i.e. BTF_KIND_ARRAY,
  1421. * *type_size: (x * y * sizeof(u32)). Hence, *type_size always
  1422. * corresponds to the return type.
  1423. * *elem_type: u32
  1424. * *elem_id: id of u32
  1425. * *total_nelems: (x * y). Hence, individual elem size is
  1426. * (*type_size / *total_nelems)
  1427. * *type_id: id of type if it's changed within the function, 0 if not
  1428. *
  1429. * type: is not an array (e.g. const struct X)
  1430. * return type: type "struct X"
  1431. * *type_size: sizeof(struct X)
  1432. * *elem_type: same as return type ("struct X")
  1433. * *elem_id: 0
  1434. * *total_nelems: 1
  1435. * *type_id: id of type if it's changed within the function, 0 if not
  1436. */
  1437. static const struct btf_type *
  1438. __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
  1439. u32 *type_size, const struct btf_type **elem_type,
  1440. u32 *elem_id, u32 *total_nelems, u32 *type_id)
  1441. {
  1442. const struct btf_type *array_type = NULL;
  1443. const struct btf_array *array = NULL;
  1444. u32 i, size, nelems = 1, id = 0;
  1445. for (i = 0; i < MAX_RESOLVE_DEPTH; i++) {
  1446. switch (BTF_INFO_KIND(type->info)) {
  1447. /* type->size can be used */
  1448. case BTF_KIND_INT:
  1449. case BTF_KIND_STRUCT:
  1450. case BTF_KIND_UNION:
  1451. case BTF_KIND_ENUM:
  1452. size = type->size;
  1453. goto resolved;
  1454. case BTF_KIND_PTR:
  1455. size = sizeof(void *);
  1456. goto resolved;
  1457. /* Modifiers */
  1458. case BTF_KIND_TYPEDEF:
  1459. case BTF_KIND_VOLATILE:
  1460. case BTF_KIND_CONST:
  1461. case BTF_KIND_RESTRICT:
  1462. id = type->type;
  1463. type = btf_type_by_id(btf, type->type);
  1464. break;
  1465. case BTF_KIND_ARRAY:
  1466. if (!array_type)
  1467. array_type = type;
  1468. array = btf_type_array(type);
  1469. if (nelems && array->nelems > U32_MAX / nelems)
  1470. return ERR_PTR(-EINVAL);
  1471. nelems *= array->nelems;
  1472. type = btf_type_by_id(btf, array->type);
  1473. break;
  1474. /* type without size */
  1475. default:
  1476. return ERR_PTR(-EINVAL);
  1477. }
  1478. }
  1479. return ERR_PTR(-EINVAL);
  1480. resolved:
  1481. if (nelems && size > U32_MAX / nelems)
  1482. return ERR_PTR(-EINVAL);
  1483. *type_size = nelems * size;
  1484. if (total_nelems)
  1485. *total_nelems = nelems;
  1486. if (elem_type)
  1487. *elem_type = type;
  1488. if (elem_id)
  1489. *elem_id = array ? array->type : 0;
  1490. if (type_id && id)
  1491. *type_id = id;
  1492. return array_type ? : type;
  1493. }
  1494. const struct btf_type *
  1495. btf_resolve_size(const struct btf *btf, const struct btf_type *type,
  1496. u32 *type_size)
  1497. {
  1498. return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL);
  1499. }
  1500. /* The input param "type_id" must point to a needs_resolve type */
  1501. static const struct btf_type *btf_type_id_resolve(const struct btf *btf,
  1502. u32 *type_id)
  1503. {
  1504. *type_id = btf->resolved_ids[*type_id];
  1505. return btf_type_by_id(btf, *type_id);
  1506. }
  1507. const struct btf_type *btf_type_id_size(const struct btf *btf,
  1508. u32 *type_id, u32 *ret_size)
  1509. {
  1510. const struct btf_type *size_type;
  1511. u32 size_type_id = *type_id;
  1512. u32 size = 0;
  1513. size_type = btf_type_by_id(btf, size_type_id);
  1514. if (btf_type_nosize_or_null(size_type))
  1515. return NULL;
  1516. if (btf_type_has_size(size_type)) {
  1517. size = size_type->size;
  1518. } else if (btf_type_is_array(size_type)) {
  1519. size = btf->resolved_sizes[size_type_id];
  1520. } else if (btf_type_is_ptr(size_type)) {
  1521. size = sizeof(void *);
  1522. } else {
  1523. if (WARN_ON_ONCE(!btf_type_is_modifier(size_type) &&
  1524. !btf_type_is_var(size_type)))
  1525. return NULL;
  1526. size_type_id = btf->resolved_ids[size_type_id];
  1527. size_type = btf_type_by_id(btf, size_type_id);
  1528. if (btf_type_nosize_or_null(size_type))
  1529. return NULL;
  1530. else if (btf_type_has_size(size_type))
  1531. size = size_type->size;
  1532. else if (btf_type_is_array(size_type))
  1533. size = btf->resolved_sizes[size_type_id];
  1534. else if (btf_type_is_ptr(size_type))
  1535. size = sizeof(void *);
  1536. else
  1537. return NULL;
  1538. }
  1539. *type_id = size_type_id;
  1540. if (ret_size)
  1541. *ret_size = size;
  1542. return size_type;
  1543. }
  1544. static int btf_df_check_member(struct btf_verifier_env *env,
  1545. const struct btf_type *struct_type,
  1546. const struct btf_member *member,
  1547. const struct btf_type *member_type)
  1548. {
  1549. btf_verifier_log_basic(env, struct_type,
  1550. "Unsupported check_member");
  1551. return -EINVAL;
  1552. }
  1553. static int btf_df_check_kflag_member(struct btf_verifier_env *env,
  1554. const struct btf_type *struct_type,
  1555. const struct btf_member *member,
  1556. const struct btf_type *member_type)
  1557. {
  1558. btf_verifier_log_basic(env, struct_type,
  1559. "Unsupported check_kflag_member");
  1560. return -EINVAL;
  1561. }
  1562. /* Used for ptr, array and struct/union type members.
  1563. * int, enum and modifier types have their specific callback functions.
  1564. */
  1565. static int btf_generic_check_kflag_member(struct btf_verifier_env *env,
  1566. const struct btf_type *struct_type,
  1567. const struct btf_member *member,
  1568. const struct btf_type *member_type)
  1569. {
  1570. if (BTF_MEMBER_BITFIELD_SIZE(member->offset)) {
  1571. btf_verifier_log_member(env, struct_type, member,
  1572. "Invalid member bitfield_size");
  1573. return -EINVAL;
  1574. }
  1575. /* bitfield size is 0, so member->offset represents bit offset only.
  1576. * It is safe to call non kflag check_member variants.
  1577. */
  1578. return btf_type_ops(member_type)->check_member(env, struct_type,
  1579. member,
  1580. member_type);
  1581. }
  1582. static int btf_df_resolve(struct btf_verifier_env *env,
  1583. const struct resolve_vertex *v)
  1584. {
  1585. btf_verifier_log_basic(env, v->t, "Unsupported resolve");
  1586. return -EINVAL;
  1587. }
  1588. static void btf_df_show(const struct btf *btf, const struct btf_type *t,
  1589. u32 type_id, void *data, u8 bits_offsets,
  1590. struct btf_show *show)
  1591. {
  1592. btf_show(show, "<unsupported kind:%u>", BTF_INFO_KIND(t->info));
  1593. }
  1594. static int btf_int_check_member(struct btf_verifier_env *env,
  1595. const struct btf_type *struct_type,
  1596. const struct btf_member *member,
  1597. const struct btf_type *member_type)
  1598. {
  1599. u32 int_data = btf_type_int(member_type);
  1600. u32 struct_bits_off = member->offset;
  1601. u32 struct_size = struct_type->size;
  1602. u32 nr_copy_bits;
  1603. u32 bytes_offset;
  1604. if (U32_MAX - struct_bits_off < BTF_INT_OFFSET(int_data)) {
  1605. btf_verifier_log_member(env, struct_type, member,
  1606. "bits_offset exceeds U32_MAX");
  1607. return -EINVAL;
  1608. }
  1609. struct_bits_off += BTF_INT_OFFSET(int_data);
  1610. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1611. nr_copy_bits = BTF_INT_BITS(int_data) +
  1612. BITS_PER_BYTE_MASKED(struct_bits_off);
  1613. if (nr_copy_bits > BITS_PER_U128) {
  1614. btf_verifier_log_member(env, struct_type, member,
  1615. "nr_copy_bits exceeds 128");
  1616. return -EINVAL;
  1617. }
  1618. if (struct_size < bytes_offset ||
  1619. struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
  1620. btf_verifier_log_member(env, struct_type, member,
  1621. "Member exceeds struct_size");
  1622. return -EINVAL;
  1623. }
  1624. return 0;
  1625. }
  1626. static int btf_int_check_kflag_member(struct btf_verifier_env *env,
  1627. const struct btf_type *struct_type,
  1628. const struct btf_member *member,
  1629. const struct btf_type *member_type)
  1630. {
  1631. u32 struct_bits_off, nr_bits, nr_int_data_bits, bytes_offset;
  1632. u32 int_data = btf_type_int(member_type);
  1633. u32 struct_size = struct_type->size;
  1634. u32 nr_copy_bits;
  1635. /* a regular int type is required for the kflag int member */
  1636. if (!btf_type_int_is_regular(member_type)) {
  1637. btf_verifier_log_member(env, struct_type, member,
  1638. "Invalid member base type");
  1639. return -EINVAL;
  1640. }
  1641. /* check sanity of bitfield size */
  1642. nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
  1643. struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
  1644. nr_int_data_bits = BTF_INT_BITS(int_data);
  1645. if (!nr_bits) {
  1646. /* Not a bitfield member, member offset must be at byte
  1647. * boundary.
  1648. */
  1649. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1650. btf_verifier_log_member(env, struct_type, member,
  1651. "Invalid member offset");
  1652. return -EINVAL;
  1653. }
  1654. nr_bits = nr_int_data_bits;
  1655. } else if (nr_bits > nr_int_data_bits) {
  1656. btf_verifier_log_member(env, struct_type, member,
  1657. "Invalid member bitfield_size");
  1658. return -EINVAL;
  1659. }
  1660. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1661. nr_copy_bits = nr_bits + BITS_PER_BYTE_MASKED(struct_bits_off);
  1662. if (nr_copy_bits > BITS_PER_U128) {
  1663. btf_verifier_log_member(env, struct_type, member,
  1664. "nr_copy_bits exceeds 128");
  1665. return -EINVAL;
  1666. }
  1667. if (struct_size < bytes_offset ||
  1668. struct_size - bytes_offset < BITS_ROUNDUP_BYTES(nr_copy_bits)) {
  1669. btf_verifier_log_member(env, struct_type, member,
  1670. "Member exceeds struct_size");
  1671. return -EINVAL;
  1672. }
  1673. return 0;
  1674. }
  1675. static s32 btf_int_check_meta(struct btf_verifier_env *env,
  1676. const struct btf_type *t,
  1677. u32 meta_left)
  1678. {
  1679. u32 int_data, nr_bits, meta_needed = sizeof(int_data);
  1680. u16 encoding;
  1681. if (meta_left < meta_needed) {
  1682. btf_verifier_log_basic(env, t,
  1683. "meta_left:%u meta_needed:%u",
  1684. meta_left, meta_needed);
  1685. return -EINVAL;
  1686. }
  1687. if (btf_type_vlen(t)) {
  1688. btf_verifier_log_type(env, t, "vlen != 0");
  1689. return -EINVAL;
  1690. }
  1691. if (btf_type_kflag(t)) {
  1692. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  1693. return -EINVAL;
  1694. }
  1695. int_data = btf_type_int(t);
  1696. if (int_data & ~BTF_INT_MASK) {
  1697. btf_verifier_log_basic(env, t, "Invalid int_data:%x",
  1698. int_data);
  1699. return -EINVAL;
  1700. }
  1701. nr_bits = BTF_INT_BITS(int_data) + BTF_INT_OFFSET(int_data);
  1702. if (nr_bits > BITS_PER_U128) {
  1703. btf_verifier_log_type(env, t, "nr_bits exceeds %zu",
  1704. BITS_PER_U128);
  1705. return -EINVAL;
  1706. }
  1707. if (BITS_ROUNDUP_BYTES(nr_bits) > t->size) {
  1708. btf_verifier_log_type(env, t, "nr_bits exceeds type_size");
  1709. return -EINVAL;
  1710. }
  1711. /*
  1712. * Only one of the encoding bits is allowed and it
  1713. * should be sufficient for the pretty print purpose (i.e. decoding).
  1714. * Multiple bits can be allowed later if it is found
  1715. * to be insufficient.
  1716. */
  1717. encoding = BTF_INT_ENCODING(int_data);
  1718. if (encoding &&
  1719. encoding != BTF_INT_SIGNED &&
  1720. encoding != BTF_INT_CHAR &&
  1721. encoding != BTF_INT_BOOL) {
  1722. btf_verifier_log_type(env, t, "Unsupported encoding");
  1723. return -ENOTSUPP;
  1724. }
  1725. btf_verifier_log_type(env, t, NULL);
  1726. return meta_needed;
  1727. }
  1728. static void btf_int_log(struct btf_verifier_env *env,
  1729. const struct btf_type *t)
  1730. {
  1731. int int_data = btf_type_int(t);
  1732. btf_verifier_log(env,
  1733. "size=%u bits_offset=%u nr_bits=%u encoding=%s",
  1734. t->size, BTF_INT_OFFSET(int_data),
  1735. BTF_INT_BITS(int_data),
  1736. btf_int_encoding_str(BTF_INT_ENCODING(int_data)));
  1737. }
  1738. static void btf_int128_print(struct btf_show *show, void *data)
  1739. {
  1740. /* data points to a __int128 number.
  1741. * Suppose
  1742. * int128_num = *(__int128 *)data;
  1743. * The below formulas shows what upper_num and lower_num represents:
  1744. * upper_num = int128_num >> 64;
  1745. * lower_num = int128_num & 0xffffffffFFFFFFFFULL;
  1746. */
  1747. u64 upper_num, lower_num;
  1748. #ifdef __BIG_ENDIAN_BITFIELD
  1749. upper_num = *(u64 *)data;
  1750. lower_num = *(u64 *)(data + 8);
  1751. #else
  1752. upper_num = *(u64 *)(data + 8);
  1753. lower_num = *(u64 *)data;
  1754. #endif
  1755. if (upper_num == 0)
  1756. btf_show_type_value(show, "0x%llx", lower_num);
  1757. else
  1758. btf_show_type_values(show, "0x%llx%016llx", upper_num,
  1759. lower_num);
  1760. }
  1761. static void btf_int128_shift(u64 *print_num, u16 left_shift_bits,
  1762. u16 right_shift_bits)
  1763. {
  1764. u64 upper_num, lower_num;
  1765. #ifdef __BIG_ENDIAN_BITFIELD
  1766. upper_num = print_num[0];
  1767. lower_num = print_num[1];
  1768. #else
  1769. upper_num = print_num[1];
  1770. lower_num = print_num[0];
  1771. #endif
  1772. /* shake out un-needed bits by shift/or operations */
  1773. if (left_shift_bits >= 64) {
  1774. upper_num = lower_num << (left_shift_bits - 64);
  1775. lower_num = 0;
  1776. } else {
  1777. upper_num = (upper_num << left_shift_bits) |
  1778. (lower_num >> (64 - left_shift_bits));
  1779. lower_num = lower_num << left_shift_bits;
  1780. }
  1781. if (right_shift_bits >= 64) {
  1782. lower_num = upper_num >> (right_shift_bits - 64);
  1783. upper_num = 0;
  1784. } else {
  1785. lower_num = (lower_num >> right_shift_bits) |
  1786. (upper_num << (64 - right_shift_bits));
  1787. upper_num = upper_num >> right_shift_bits;
  1788. }
  1789. #ifdef __BIG_ENDIAN_BITFIELD
  1790. print_num[0] = upper_num;
  1791. print_num[1] = lower_num;
  1792. #else
  1793. print_num[0] = lower_num;
  1794. print_num[1] = upper_num;
  1795. #endif
  1796. }
  1797. static void btf_bitfield_show(void *data, u8 bits_offset,
  1798. u8 nr_bits, struct btf_show *show)
  1799. {
  1800. u16 left_shift_bits, right_shift_bits;
  1801. u8 nr_copy_bytes;
  1802. u8 nr_copy_bits;
  1803. u64 print_num[2] = {};
  1804. nr_copy_bits = nr_bits + bits_offset;
  1805. nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
  1806. memcpy(print_num, data, nr_copy_bytes);
  1807. #ifdef __BIG_ENDIAN_BITFIELD
  1808. left_shift_bits = bits_offset;
  1809. #else
  1810. left_shift_bits = BITS_PER_U128 - nr_copy_bits;
  1811. #endif
  1812. right_shift_bits = BITS_PER_U128 - nr_bits;
  1813. btf_int128_shift(print_num, left_shift_bits, right_shift_bits);
  1814. btf_int128_print(show, print_num);
  1815. }
  1816. static void btf_int_bits_show(const struct btf *btf,
  1817. const struct btf_type *t,
  1818. void *data, u8 bits_offset,
  1819. struct btf_show *show)
  1820. {
  1821. u32 int_data = btf_type_int(t);
  1822. u8 nr_bits = BTF_INT_BITS(int_data);
  1823. u8 total_bits_offset;
  1824. /*
  1825. * bits_offset is at most 7.
  1826. * BTF_INT_OFFSET() cannot exceed 128 bits.
  1827. */
  1828. total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
  1829. data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
  1830. bits_offset = BITS_PER_BYTE_MASKED(total_bits_offset);
  1831. btf_bitfield_show(data, bits_offset, nr_bits, show);
  1832. }
  1833. static void btf_int_show(const struct btf *btf, const struct btf_type *t,
  1834. u32 type_id, void *data, u8 bits_offset,
  1835. struct btf_show *show)
  1836. {
  1837. u32 int_data = btf_type_int(t);
  1838. u8 encoding = BTF_INT_ENCODING(int_data);
  1839. bool sign = encoding & BTF_INT_SIGNED;
  1840. u8 nr_bits = BTF_INT_BITS(int_data);
  1841. void *safe_data;
  1842. safe_data = btf_show_start_type(show, t, type_id, data);
  1843. if (!safe_data)
  1844. return;
  1845. if (bits_offset || BTF_INT_OFFSET(int_data) ||
  1846. BITS_PER_BYTE_MASKED(nr_bits)) {
  1847. btf_int_bits_show(btf, t, safe_data, bits_offset, show);
  1848. goto out;
  1849. }
  1850. switch (nr_bits) {
  1851. case 128:
  1852. btf_int128_print(show, safe_data);
  1853. break;
  1854. case 64:
  1855. if (sign)
  1856. btf_show_type_value(show, "%lld", *(s64 *)safe_data);
  1857. else
  1858. btf_show_type_value(show, "%llu", *(u64 *)safe_data);
  1859. break;
  1860. case 32:
  1861. if (sign)
  1862. btf_show_type_value(show, "%d", *(s32 *)safe_data);
  1863. else
  1864. btf_show_type_value(show, "%u", *(u32 *)safe_data);
  1865. break;
  1866. case 16:
  1867. if (sign)
  1868. btf_show_type_value(show, "%d", *(s16 *)safe_data);
  1869. else
  1870. btf_show_type_value(show, "%u", *(u16 *)safe_data);
  1871. break;
  1872. case 8:
  1873. if (show->state.array_encoding == BTF_INT_CHAR) {
  1874. /* check for null terminator */
  1875. if (show->state.array_terminated)
  1876. break;
  1877. if (*(char *)data == '\0') {
  1878. show->state.array_terminated = 1;
  1879. break;
  1880. }
  1881. if (isprint(*(char *)data)) {
  1882. btf_show_type_value(show, "'%c'",
  1883. *(char *)safe_data);
  1884. break;
  1885. }
  1886. }
  1887. if (sign)
  1888. btf_show_type_value(show, "%d", *(s8 *)safe_data);
  1889. else
  1890. btf_show_type_value(show, "%u", *(u8 *)safe_data);
  1891. break;
  1892. default:
  1893. btf_int_bits_show(btf, t, safe_data, bits_offset, show);
  1894. break;
  1895. }
  1896. out:
  1897. btf_show_end_type(show);
  1898. }
  1899. static const struct btf_kind_operations int_ops = {
  1900. .check_meta = btf_int_check_meta,
  1901. .resolve = btf_df_resolve,
  1902. .check_member = btf_int_check_member,
  1903. .check_kflag_member = btf_int_check_kflag_member,
  1904. .log_details = btf_int_log,
  1905. .show = btf_int_show,
  1906. };
  1907. static int btf_modifier_check_member(struct btf_verifier_env *env,
  1908. const struct btf_type *struct_type,
  1909. const struct btf_member *member,
  1910. const struct btf_type *member_type)
  1911. {
  1912. const struct btf_type *resolved_type;
  1913. u32 resolved_type_id = member->type;
  1914. struct btf_member resolved_member;
  1915. struct btf *btf = env->btf;
  1916. resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
  1917. if (!resolved_type) {
  1918. btf_verifier_log_member(env, struct_type, member,
  1919. "Invalid member");
  1920. return -EINVAL;
  1921. }
  1922. resolved_member = *member;
  1923. resolved_member.type = resolved_type_id;
  1924. return btf_type_ops(resolved_type)->check_member(env, struct_type,
  1925. &resolved_member,
  1926. resolved_type);
  1927. }
  1928. static int btf_modifier_check_kflag_member(struct btf_verifier_env *env,
  1929. const struct btf_type *struct_type,
  1930. const struct btf_member *member,
  1931. const struct btf_type *member_type)
  1932. {
  1933. const struct btf_type *resolved_type;
  1934. u32 resolved_type_id = member->type;
  1935. struct btf_member resolved_member;
  1936. struct btf *btf = env->btf;
  1937. resolved_type = btf_type_id_size(btf, &resolved_type_id, NULL);
  1938. if (!resolved_type) {
  1939. btf_verifier_log_member(env, struct_type, member,
  1940. "Invalid member");
  1941. return -EINVAL;
  1942. }
  1943. resolved_member = *member;
  1944. resolved_member.type = resolved_type_id;
  1945. return btf_type_ops(resolved_type)->check_kflag_member(env, struct_type,
  1946. &resolved_member,
  1947. resolved_type);
  1948. }
  1949. static int btf_ptr_check_member(struct btf_verifier_env *env,
  1950. const struct btf_type *struct_type,
  1951. const struct btf_member *member,
  1952. const struct btf_type *member_type)
  1953. {
  1954. u32 struct_size, struct_bits_off, bytes_offset;
  1955. struct_size = struct_type->size;
  1956. struct_bits_off = member->offset;
  1957. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  1958. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  1959. btf_verifier_log_member(env, struct_type, member,
  1960. "Member is not byte aligned");
  1961. return -EINVAL;
  1962. }
  1963. if (struct_size - bytes_offset < sizeof(void *)) {
  1964. btf_verifier_log_member(env, struct_type, member,
  1965. "Member exceeds struct_size");
  1966. return -EINVAL;
  1967. }
  1968. return 0;
  1969. }
  1970. static int btf_ref_type_check_meta(struct btf_verifier_env *env,
  1971. const struct btf_type *t,
  1972. u32 meta_left)
  1973. {
  1974. if (btf_type_vlen(t)) {
  1975. btf_verifier_log_type(env, t, "vlen != 0");
  1976. return -EINVAL;
  1977. }
  1978. if (btf_type_kflag(t)) {
  1979. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  1980. return -EINVAL;
  1981. }
  1982. if (!BTF_TYPE_ID_VALID(t->type)) {
  1983. btf_verifier_log_type(env, t, "Invalid type_id");
  1984. return -EINVAL;
  1985. }
  1986. /* typedef type must have a valid name, and other ref types,
  1987. * volatile, const, restrict, should have a null name.
  1988. */
  1989. if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
  1990. if (!t->name_off ||
  1991. !btf_name_valid_identifier(env->btf, t->name_off)) {
  1992. btf_verifier_log_type(env, t, "Invalid name");
  1993. return -EINVAL;
  1994. }
  1995. } else {
  1996. if (t->name_off) {
  1997. btf_verifier_log_type(env, t, "Invalid name");
  1998. return -EINVAL;
  1999. }
  2000. }
  2001. btf_verifier_log_type(env, t, NULL);
  2002. return 0;
  2003. }
  2004. static int btf_modifier_resolve(struct btf_verifier_env *env,
  2005. const struct resolve_vertex *v)
  2006. {
  2007. const struct btf_type *t = v->t;
  2008. const struct btf_type *next_type;
  2009. u32 next_type_id = t->type;
  2010. struct btf *btf = env->btf;
  2011. next_type = btf_type_by_id(btf, next_type_id);
  2012. if (!next_type || btf_type_is_resolve_source_only(next_type)) {
  2013. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2014. return -EINVAL;
  2015. }
  2016. if (!env_type_is_resolve_sink(env, next_type) &&
  2017. !env_type_is_resolved(env, next_type_id))
  2018. return env_stack_push(env, next_type, next_type_id);
  2019. /* Figure out the resolved next_type_id with size.
  2020. * They will be stored in the current modifier's
  2021. * resolved_ids and resolved_sizes such that it can
  2022. * save us a few type-following when we use it later (e.g. in
  2023. * pretty print).
  2024. */
  2025. if (!btf_type_id_size(btf, &next_type_id, NULL)) {
  2026. if (env_type_is_resolved(env, next_type_id))
  2027. next_type = btf_type_id_resolve(btf, &next_type_id);
  2028. /* "typedef void new_void", "const void"...etc */
  2029. if (!btf_type_is_void(next_type) &&
  2030. !btf_type_is_fwd(next_type) &&
  2031. !btf_type_is_func_proto(next_type)) {
  2032. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2033. return -EINVAL;
  2034. }
  2035. }
  2036. env_stack_pop_resolved(env, next_type_id, 0);
  2037. return 0;
  2038. }
  2039. static int btf_var_resolve(struct btf_verifier_env *env,
  2040. const struct resolve_vertex *v)
  2041. {
  2042. const struct btf_type *next_type;
  2043. const struct btf_type *t = v->t;
  2044. u32 next_type_id = t->type;
  2045. struct btf *btf = env->btf;
  2046. next_type = btf_type_by_id(btf, next_type_id);
  2047. if (!next_type || btf_type_is_resolve_source_only(next_type)) {
  2048. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2049. return -EINVAL;
  2050. }
  2051. if (!env_type_is_resolve_sink(env, next_type) &&
  2052. !env_type_is_resolved(env, next_type_id))
  2053. return env_stack_push(env, next_type, next_type_id);
  2054. if (btf_type_is_modifier(next_type)) {
  2055. const struct btf_type *resolved_type;
  2056. u32 resolved_type_id;
  2057. resolved_type_id = next_type_id;
  2058. resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
  2059. if (btf_type_is_ptr(resolved_type) &&
  2060. !env_type_is_resolve_sink(env, resolved_type) &&
  2061. !env_type_is_resolved(env, resolved_type_id))
  2062. return env_stack_push(env, resolved_type,
  2063. resolved_type_id);
  2064. }
  2065. /* We must resolve to something concrete at this point, no
  2066. * forward types or similar that would resolve to size of
  2067. * zero is allowed.
  2068. */
  2069. if (!btf_type_id_size(btf, &next_type_id, NULL)) {
  2070. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2071. return -EINVAL;
  2072. }
  2073. env_stack_pop_resolved(env, next_type_id, 0);
  2074. return 0;
  2075. }
  2076. static int btf_ptr_resolve(struct btf_verifier_env *env,
  2077. const struct resolve_vertex *v)
  2078. {
  2079. const struct btf_type *next_type;
  2080. const struct btf_type *t = v->t;
  2081. u32 next_type_id = t->type;
  2082. struct btf *btf = env->btf;
  2083. next_type = btf_type_by_id(btf, next_type_id);
  2084. if (!next_type || btf_type_is_resolve_source_only(next_type)) {
  2085. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2086. return -EINVAL;
  2087. }
  2088. if (!env_type_is_resolve_sink(env, next_type) &&
  2089. !env_type_is_resolved(env, next_type_id))
  2090. return env_stack_push(env, next_type, next_type_id);
  2091. /* If the modifier was RESOLVED during RESOLVE_STRUCT_OR_ARRAY,
  2092. * the modifier may have stopped resolving when it was resolved
  2093. * to a ptr (last-resolved-ptr).
  2094. *
  2095. * We now need to continue from the last-resolved-ptr to
  2096. * ensure the last-resolved-ptr will not referring back to
  2097. * the currenct ptr (t).
  2098. */
  2099. if (btf_type_is_modifier(next_type)) {
  2100. const struct btf_type *resolved_type;
  2101. u32 resolved_type_id;
  2102. resolved_type_id = next_type_id;
  2103. resolved_type = btf_type_id_resolve(btf, &resolved_type_id);
  2104. if (btf_type_is_ptr(resolved_type) &&
  2105. !env_type_is_resolve_sink(env, resolved_type) &&
  2106. !env_type_is_resolved(env, resolved_type_id))
  2107. return env_stack_push(env, resolved_type,
  2108. resolved_type_id);
  2109. }
  2110. if (!btf_type_id_size(btf, &next_type_id, NULL)) {
  2111. if (env_type_is_resolved(env, next_type_id))
  2112. next_type = btf_type_id_resolve(btf, &next_type_id);
  2113. if (!btf_type_is_void(next_type) &&
  2114. !btf_type_is_fwd(next_type) &&
  2115. !btf_type_is_func_proto(next_type)) {
  2116. btf_verifier_log_type(env, v->t, "Invalid type_id");
  2117. return -EINVAL;
  2118. }
  2119. }
  2120. env_stack_pop_resolved(env, next_type_id, 0);
  2121. return 0;
  2122. }
  2123. static void btf_modifier_show(const struct btf *btf,
  2124. const struct btf_type *t,
  2125. u32 type_id, void *data,
  2126. u8 bits_offset, struct btf_show *show)
  2127. {
  2128. if (btf->resolved_ids)
  2129. t = btf_type_id_resolve(btf, &type_id);
  2130. else
  2131. t = btf_type_skip_modifiers(btf, type_id, NULL);
  2132. btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
  2133. }
  2134. static void btf_var_show(const struct btf *btf, const struct btf_type *t,
  2135. u32 type_id, void *data, u8 bits_offset,
  2136. struct btf_show *show)
  2137. {
  2138. t = btf_type_id_resolve(btf, &type_id);
  2139. btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show);
  2140. }
  2141. static void btf_ptr_show(const struct btf *btf, const struct btf_type *t,
  2142. u32 type_id, void *data, u8 bits_offset,
  2143. struct btf_show *show)
  2144. {
  2145. void *safe_data;
  2146. safe_data = btf_show_start_type(show, t, type_id, data);
  2147. if (!safe_data)
  2148. return;
  2149. /* It is a hashed value unless BTF_SHOW_PTR_RAW is specified */
  2150. if (show->flags & BTF_SHOW_PTR_RAW)
  2151. btf_show_type_value(show, "0x%px", *(void **)safe_data);
  2152. else
  2153. btf_show_type_value(show, "0x%p", *(void **)safe_data);
  2154. btf_show_end_type(show);
  2155. }
  2156. static void btf_ref_type_log(struct btf_verifier_env *env,
  2157. const struct btf_type *t)
  2158. {
  2159. btf_verifier_log(env, "type_id=%u", t->type);
  2160. }
  2161. static struct btf_kind_operations modifier_ops = {
  2162. .check_meta = btf_ref_type_check_meta,
  2163. .resolve = btf_modifier_resolve,
  2164. .check_member = btf_modifier_check_member,
  2165. .check_kflag_member = btf_modifier_check_kflag_member,
  2166. .log_details = btf_ref_type_log,
  2167. .show = btf_modifier_show,
  2168. };
  2169. static struct btf_kind_operations ptr_ops = {
  2170. .check_meta = btf_ref_type_check_meta,
  2171. .resolve = btf_ptr_resolve,
  2172. .check_member = btf_ptr_check_member,
  2173. .check_kflag_member = btf_generic_check_kflag_member,
  2174. .log_details = btf_ref_type_log,
  2175. .show = btf_ptr_show,
  2176. };
  2177. static s32 btf_fwd_check_meta(struct btf_verifier_env *env,
  2178. const struct btf_type *t,
  2179. u32 meta_left)
  2180. {
  2181. if (btf_type_vlen(t)) {
  2182. btf_verifier_log_type(env, t, "vlen != 0");
  2183. return -EINVAL;
  2184. }
  2185. if (t->type) {
  2186. btf_verifier_log_type(env, t, "type != 0");
  2187. return -EINVAL;
  2188. }
  2189. /* fwd type must have a valid name */
  2190. if (!t->name_off ||
  2191. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2192. btf_verifier_log_type(env, t, "Invalid name");
  2193. return -EINVAL;
  2194. }
  2195. btf_verifier_log_type(env, t, NULL);
  2196. return 0;
  2197. }
  2198. static void btf_fwd_type_log(struct btf_verifier_env *env,
  2199. const struct btf_type *t)
  2200. {
  2201. btf_verifier_log(env, "%s", btf_type_kflag(t) ? "union" : "struct");
  2202. }
  2203. static struct btf_kind_operations fwd_ops = {
  2204. .check_meta = btf_fwd_check_meta,
  2205. .resolve = btf_df_resolve,
  2206. .check_member = btf_df_check_member,
  2207. .check_kflag_member = btf_df_check_kflag_member,
  2208. .log_details = btf_fwd_type_log,
  2209. .show = btf_df_show,
  2210. };
  2211. static int btf_array_check_member(struct btf_verifier_env *env,
  2212. const struct btf_type *struct_type,
  2213. const struct btf_member *member,
  2214. const struct btf_type *member_type)
  2215. {
  2216. u32 struct_bits_off = member->offset;
  2217. u32 struct_size, bytes_offset;
  2218. u32 array_type_id, array_size;
  2219. struct btf *btf = env->btf;
  2220. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2221. btf_verifier_log_member(env, struct_type, member,
  2222. "Member is not byte aligned");
  2223. return -EINVAL;
  2224. }
  2225. array_type_id = member->type;
  2226. btf_type_id_size(btf, &array_type_id, &array_size);
  2227. struct_size = struct_type->size;
  2228. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  2229. if (struct_size - bytes_offset < array_size) {
  2230. btf_verifier_log_member(env, struct_type, member,
  2231. "Member exceeds struct_size");
  2232. return -EINVAL;
  2233. }
  2234. return 0;
  2235. }
  2236. static s32 btf_array_check_meta(struct btf_verifier_env *env,
  2237. const struct btf_type *t,
  2238. u32 meta_left)
  2239. {
  2240. const struct btf_array *array = btf_type_array(t);
  2241. u32 meta_needed = sizeof(*array);
  2242. if (meta_left < meta_needed) {
  2243. btf_verifier_log_basic(env, t,
  2244. "meta_left:%u meta_needed:%u",
  2245. meta_left, meta_needed);
  2246. return -EINVAL;
  2247. }
  2248. /* array type should not have a name */
  2249. if (t->name_off) {
  2250. btf_verifier_log_type(env, t, "Invalid name");
  2251. return -EINVAL;
  2252. }
  2253. if (btf_type_vlen(t)) {
  2254. btf_verifier_log_type(env, t, "vlen != 0");
  2255. return -EINVAL;
  2256. }
  2257. if (btf_type_kflag(t)) {
  2258. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2259. return -EINVAL;
  2260. }
  2261. if (t->size) {
  2262. btf_verifier_log_type(env, t, "size != 0");
  2263. return -EINVAL;
  2264. }
  2265. /* Array elem type and index type cannot be in type void,
  2266. * so !array->type and !array->index_type are not allowed.
  2267. */
  2268. if (!array->type || !BTF_TYPE_ID_VALID(array->type)) {
  2269. btf_verifier_log_type(env, t, "Invalid elem");
  2270. return -EINVAL;
  2271. }
  2272. if (!array->index_type || !BTF_TYPE_ID_VALID(array->index_type)) {
  2273. btf_verifier_log_type(env, t, "Invalid index");
  2274. return -EINVAL;
  2275. }
  2276. btf_verifier_log_type(env, t, NULL);
  2277. return meta_needed;
  2278. }
  2279. static int btf_array_resolve(struct btf_verifier_env *env,
  2280. const struct resolve_vertex *v)
  2281. {
  2282. const struct btf_array *array = btf_type_array(v->t);
  2283. const struct btf_type *elem_type, *index_type;
  2284. u32 elem_type_id, index_type_id;
  2285. struct btf *btf = env->btf;
  2286. u32 elem_size;
  2287. /* Check array->index_type */
  2288. index_type_id = array->index_type;
  2289. index_type = btf_type_by_id(btf, index_type_id);
  2290. if (btf_type_nosize_or_null(index_type) ||
  2291. btf_type_is_resolve_source_only(index_type)) {
  2292. btf_verifier_log_type(env, v->t, "Invalid index");
  2293. return -EINVAL;
  2294. }
  2295. if (!env_type_is_resolve_sink(env, index_type) &&
  2296. !env_type_is_resolved(env, index_type_id))
  2297. return env_stack_push(env, index_type, index_type_id);
  2298. index_type = btf_type_id_size(btf, &index_type_id, NULL);
  2299. if (!index_type || !btf_type_is_int(index_type) ||
  2300. !btf_type_int_is_regular(index_type)) {
  2301. btf_verifier_log_type(env, v->t, "Invalid index");
  2302. return -EINVAL;
  2303. }
  2304. /* Check array->type */
  2305. elem_type_id = array->type;
  2306. elem_type = btf_type_by_id(btf, elem_type_id);
  2307. if (btf_type_nosize_or_null(elem_type) ||
  2308. btf_type_is_resolve_source_only(elem_type)) {
  2309. btf_verifier_log_type(env, v->t,
  2310. "Invalid elem");
  2311. return -EINVAL;
  2312. }
  2313. if (!env_type_is_resolve_sink(env, elem_type) &&
  2314. !env_type_is_resolved(env, elem_type_id))
  2315. return env_stack_push(env, elem_type, elem_type_id);
  2316. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  2317. if (!elem_type) {
  2318. btf_verifier_log_type(env, v->t, "Invalid elem");
  2319. return -EINVAL;
  2320. }
  2321. if (btf_type_is_int(elem_type) && !btf_type_int_is_regular(elem_type)) {
  2322. btf_verifier_log_type(env, v->t, "Invalid array of int");
  2323. return -EINVAL;
  2324. }
  2325. if (array->nelems && elem_size > U32_MAX / array->nelems) {
  2326. btf_verifier_log_type(env, v->t,
  2327. "Array size overflows U32_MAX");
  2328. return -EINVAL;
  2329. }
  2330. env_stack_pop_resolved(env, elem_type_id, elem_size * array->nelems);
  2331. return 0;
  2332. }
  2333. static void btf_array_log(struct btf_verifier_env *env,
  2334. const struct btf_type *t)
  2335. {
  2336. const struct btf_array *array = btf_type_array(t);
  2337. btf_verifier_log(env, "type_id=%u index_type_id=%u nr_elems=%u",
  2338. array->type, array->index_type, array->nelems);
  2339. }
  2340. static void __btf_array_show(const struct btf *btf, const struct btf_type *t,
  2341. u32 type_id, void *data, u8 bits_offset,
  2342. struct btf_show *show)
  2343. {
  2344. const struct btf_array *array = btf_type_array(t);
  2345. const struct btf_kind_operations *elem_ops;
  2346. const struct btf_type *elem_type;
  2347. u32 i, elem_size = 0, elem_type_id;
  2348. u16 encoding = 0;
  2349. elem_type_id = array->type;
  2350. elem_type = btf_type_skip_modifiers(btf, elem_type_id, NULL);
  2351. if (elem_type && btf_type_has_size(elem_type))
  2352. elem_size = elem_type->size;
  2353. if (elem_type && btf_type_is_int(elem_type)) {
  2354. u32 int_type = btf_type_int(elem_type);
  2355. encoding = BTF_INT_ENCODING(int_type);
  2356. /*
  2357. * BTF_INT_CHAR encoding never seems to be set for
  2358. * char arrays, so if size is 1 and element is
  2359. * printable as a char, we'll do that.
  2360. */
  2361. if (elem_size == 1)
  2362. encoding = BTF_INT_CHAR;
  2363. }
  2364. if (!btf_show_start_array_type(show, t, type_id, encoding, data))
  2365. return;
  2366. if (!elem_type)
  2367. goto out;
  2368. elem_ops = btf_type_ops(elem_type);
  2369. for (i = 0; i < array->nelems; i++) {
  2370. btf_show_start_array_member(show);
  2371. elem_ops->show(btf, elem_type, elem_type_id, data,
  2372. bits_offset, show);
  2373. data += elem_size;
  2374. btf_show_end_array_member(show);
  2375. if (show->state.array_terminated)
  2376. break;
  2377. }
  2378. out:
  2379. btf_show_end_array_type(show);
  2380. }
  2381. static void btf_array_show(const struct btf *btf, const struct btf_type *t,
  2382. u32 type_id, void *data, u8 bits_offset,
  2383. struct btf_show *show)
  2384. {
  2385. const struct btf_member *m = show->state.member;
  2386. /*
  2387. * First check if any members would be shown (are non-zero).
  2388. * See comments above "struct btf_show" definition for more
  2389. * details on how this works at a high-level.
  2390. */
  2391. if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
  2392. if (!show->state.depth_check) {
  2393. show->state.depth_check = show->state.depth + 1;
  2394. show->state.depth_to_show = 0;
  2395. }
  2396. __btf_array_show(btf, t, type_id, data, bits_offset, show);
  2397. show->state.member = m;
  2398. if (show->state.depth_check != show->state.depth + 1)
  2399. return;
  2400. show->state.depth_check = 0;
  2401. if (show->state.depth_to_show <= show->state.depth)
  2402. return;
  2403. /*
  2404. * Reaching here indicates we have recursed and found
  2405. * non-zero array member(s).
  2406. */
  2407. }
  2408. __btf_array_show(btf, t, type_id, data, bits_offset, show);
  2409. }
  2410. static struct btf_kind_operations array_ops = {
  2411. .check_meta = btf_array_check_meta,
  2412. .resolve = btf_array_resolve,
  2413. .check_member = btf_array_check_member,
  2414. .check_kflag_member = btf_generic_check_kflag_member,
  2415. .log_details = btf_array_log,
  2416. .show = btf_array_show,
  2417. };
  2418. static int btf_struct_check_member(struct btf_verifier_env *env,
  2419. const struct btf_type *struct_type,
  2420. const struct btf_member *member,
  2421. const struct btf_type *member_type)
  2422. {
  2423. u32 struct_bits_off = member->offset;
  2424. u32 struct_size, bytes_offset;
  2425. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2426. btf_verifier_log_member(env, struct_type, member,
  2427. "Member is not byte aligned");
  2428. return -EINVAL;
  2429. }
  2430. struct_size = struct_type->size;
  2431. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  2432. if (struct_size - bytes_offset < member_type->size) {
  2433. btf_verifier_log_member(env, struct_type, member,
  2434. "Member exceeds struct_size");
  2435. return -EINVAL;
  2436. }
  2437. return 0;
  2438. }
  2439. static s32 btf_struct_check_meta(struct btf_verifier_env *env,
  2440. const struct btf_type *t,
  2441. u32 meta_left)
  2442. {
  2443. bool is_union = BTF_INFO_KIND(t->info) == BTF_KIND_UNION;
  2444. const struct btf_member *member;
  2445. u32 meta_needed, last_offset;
  2446. struct btf *btf = env->btf;
  2447. u32 struct_size = t->size;
  2448. u32 offset;
  2449. u16 i;
  2450. meta_needed = btf_type_vlen(t) * sizeof(*member);
  2451. if (meta_left < meta_needed) {
  2452. btf_verifier_log_basic(env, t,
  2453. "meta_left:%u meta_needed:%u",
  2454. meta_left, meta_needed);
  2455. return -EINVAL;
  2456. }
  2457. /* struct type either no name or a valid one */
  2458. if (t->name_off &&
  2459. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2460. btf_verifier_log_type(env, t, "Invalid name");
  2461. return -EINVAL;
  2462. }
  2463. btf_verifier_log_type(env, t, NULL);
  2464. last_offset = 0;
  2465. for_each_member(i, t, member) {
  2466. if (!btf_name_offset_valid(btf, member->name_off)) {
  2467. btf_verifier_log_member(env, t, member,
  2468. "Invalid member name_offset:%u",
  2469. member->name_off);
  2470. return -EINVAL;
  2471. }
  2472. /* struct member either no name or a valid one */
  2473. if (member->name_off &&
  2474. !btf_name_valid_identifier(btf, member->name_off)) {
  2475. btf_verifier_log_member(env, t, member, "Invalid name");
  2476. return -EINVAL;
  2477. }
  2478. /* A member cannot be in type void */
  2479. if (!member->type || !BTF_TYPE_ID_VALID(member->type)) {
  2480. btf_verifier_log_member(env, t, member,
  2481. "Invalid type_id");
  2482. return -EINVAL;
  2483. }
  2484. offset = btf_member_bit_offset(t, member);
  2485. if (is_union && offset) {
  2486. btf_verifier_log_member(env, t, member,
  2487. "Invalid member bits_offset");
  2488. return -EINVAL;
  2489. }
  2490. /*
  2491. * ">" instead of ">=" because the last member could be
  2492. * "char a[0];"
  2493. */
  2494. if (last_offset > offset) {
  2495. btf_verifier_log_member(env, t, member,
  2496. "Invalid member bits_offset");
  2497. return -EINVAL;
  2498. }
  2499. if (BITS_ROUNDUP_BYTES(offset) > struct_size) {
  2500. btf_verifier_log_member(env, t, member,
  2501. "Member bits_offset exceeds its struct size");
  2502. return -EINVAL;
  2503. }
  2504. btf_verifier_log_member(env, t, member, NULL);
  2505. last_offset = offset;
  2506. }
  2507. return meta_needed;
  2508. }
  2509. static int btf_struct_resolve(struct btf_verifier_env *env,
  2510. const struct resolve_vertex *v)
  2511. {
  2512. const struct btf_member *member;
  2513. int err;
  2514. u16 i;
  2515. /* Before continue resolving the next_member,
  2516. * ensure the last member is indeed resolved to a
  2517. * type with size info.
  2518. */
  2519. if (v->next_member) {
  2520. const struct btf_type *last_member_type;
  2521. const struct btf_member *last_member;
  2522. u16 last_member_type_id;
  2523. last_member = btf_type_member(v->t) + v->next_member - 1;
  2524. last_member_type_id = last_member->type;
  2525. if (WARN_ON_ONCE(!env_type_is_resolved(env,
  2526. last_member_type_id)))
  2527. return -EINVAL;
  2528. last_member_type = btf_type_by_id(env->btf,
  2529. last_member_type_id);
  2530. if (btf_type_kflag(v->t))
  2531. err = btf_type_ops(last_member_type)->check_kflag_member(env, v->t,
  2532. last_member,
  2533. last_member_type);
  2534. else
  2535. err = btf_type_ops(last_member_type)->check_member(env, v->t,
  2536. last_member,
  2537. last_member_type);
  2538. if (err)
  2539. return err;
  2540. }
  2541. for_each_member_from(i, v->next_member, v->t, member) {
  2542. u32 member_type_id = member->type;
  2543. const struct btf_type *member_type = btf_type_by_id(env->btf,
  2544. member_type_id);
  2545. if (btf_type_nosize_or_null(member_type) ||
  2546. btf_type_is_resolve_source_only(member_type)) {
  2547. btf_verifier_log_member(env, v->t, member,
  2548. "Invalid member");
  2549. return -EINVAL;
  2550. }
  2551. if (!env_type_is_resolve_sink(env, member_type) &&
  2552. !env_type_is_resolved(env, member_type_id)) {
  2553. env_stack_set_next_member(env, i + 1);
  2554. return env_stack_push(env, member_type, member_type_id);
  2555. }
  2556. if (btf_type_kflag(v->t))
  2557. err = btf_type_ops(member_type)->check_kflag_member(env, v->t,
  2558. member,
  2559. member_type);
  2560. else
  2561. err = btf_type_ops(member_type)->check_member(env, v->t,
  2562. member,
  2563. member_type);
  2564. if (err)
  2565. return err;
  2566. }
  2567. env_stack_pop_resolved(env, 0, 0);
  2568. return 0;
  2569. }
  2570. static void btf_struct_log(struct btf_verifier_env *env,
  2571. const struct btf_type *t)
  2572. {
  2573. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  2574. }
  2575. /* find 'struct bpf_spin_lock' in map value.
  2576. * return >= 0 offset if found
  2577. * and < 0 in case of error
  2578. */
  2579. int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t)
  2580. {
  2581. const struct btf_member *member;
  2582. u32 i, off = -ENOENT;
  2583. if (!__btf_type_is_struct(t))
  2584. return -EINVAL;
  2585. for_each_member(i, t, member) {
  2586. const struct btf_type *member_type = btf_type_by_id(btf,
  2587. member->type);
  2588. if (!__btf_type_is_struct(member_type))
  2589. continue;
  2590. if (member_type->size != sizeof(struct bpf_spin_lock))
  2591. continue;
  2592. if (strcmp(__btf_name_by_offset(btf, member_type->name_off),
  2593. "bpf_spin_lock"))
  2594. continue;
  2595. if (off != -ENOENT)
  2596. /* only one 'struct bpf_spin_lock' is allowed */
  2597. return -E2BIG;
  2598. off = btf_member_bit_offset(t, member);
  2599. if (off % 8)
  2600. /* valid C code cannot generate such BTF */
  2601. return -EINVAL;
  2602. off /= 8;
  2603. if (off % __alignof__(struct bpf_spin_lock))
  2604. /* valid struct bpf_spin_lock will be 4 byte aligned */
  2605. return -EINVAL;
  2606. }
  2607. return off;
  2608. }
  2609. static void __btf_struct_show(const struct btf *btf, const struct btf_type *t,
  2610. u32 type_id, void *data, u8 bits_offset,
  2611. struct btf_show *show)
  2612. {
  2613. const struct btf_member *member;
  2614. void *safe_data;
  2615. u32 i;
  2616. safe_data = btf_show_start_struct_type(show, t, type_id, data);
  2617. if (!safe_data)
  2618. return;
  2619. for_each_member(i, t, member) {
  2620. const struct btf_type *member_type = btf_type_by_id(btf,
  2621. member->type);
  2622. const struct btf_kind_operations *ops;
  2623. u32 member_offset, bitfield_size;
  2624. u32 bytes_offset;
  2625. u8 bits8_offset;
  2626. btf_show_start_member(show, member);
  2627. member_offset = btf_member_bit_offset(t, member);
  2628. bitfield_size = btf_member_bitfield_size(t, member);
  2629. bytes_offset = BITS_ROUNDDOWN_BYTES(member_offset);
  2630. bits8_offset = BITS_PER_BYTE_MASKED(member_offset);
  2631. if (bitfield_size) {
  2632. safe_data = btf_show_start_type(show, member_type,
  2633. member->type,
  2634. data + bytes_offset);
  2635. if (safe_data)
  2636. btf_bitfield_show(safe_data,
  2637. bits8_offset,
  2638. bitfield_size, show);
  2639. btf_show_end_type(show);
  2640. } else {
  2641. ops = btf_type_ops(member_type);
  2642. ops->show(btf, member_type, member->type,
  2643. data + bytes_offset, bits8_offset, show);
  2644. }
  2645. btf_show_end_member(show);
  2646. }
  2647. btf_show_end_struct_type(show);
  2648. }
  2649. static void btf_struct_show(const struct btf *btf, const struct btf_type *t,
  2650. u32 type_id, void *data, u8 bits_offset,
  2651. struct btf_show *show)
  2652. {
  2653. const struct btf_member *m = show->state.member;
  2654. /*
  2655. * First check if any members would be shown (are non-zero).
  2656. * See comments above "struct btf_show" definition for more
  2657. * details on how this works at a high-level.
  2658. */
  2659. if (show->state.depth > 0 && !(show->flags & BTF_SHOW_ZERO)) {
  2660. if (!show->state.depth_check) {
  2661. show->state.depth_check = show->state.depth + 1;
  2662. show->state.depth_to_show = 0;
  2663. }
  2664. __btf_struct_show(btf, t, type_id, data, bits_offset, show);
  2665. /* Restore saved member data here */
  2666. show->state.member = m;
  2667. if (show->state.depth_check != show->state.depth + 1)
  2668. return;
  2669. show->state.depth_check = 0;
  2670. if (show->state.depth_to_show <= show->state.depth)
  2671. return;
  2672. /*
  2673. * Reaching here indicates we have recursed and found
  2674. * non-zero child values.
  2675. */
  2676. }
  2677. __btf_struct_show(btf, t, type_id, data, bits_offset, show);
  2678. }
  2679. static struct btf_kind_operations struct_ops = {
  2680. .check_meta = btf_struct_check_meta,
  2681. .resolve = btf_struct_resolve,
  2682. .check_member = btf_struct_check_member,
  2683. .check_kflag_member = btf_generic_check_kflag_member,
  2684. .log_details = btf_struct_log,
  2685. .show = btf_struct_show,
  2686. };
  2687. static int btf_enum_check_member(struct btf_verifier_env *env,
  2688. const struct btf_type *struct_type,
  2689. const struct btf_member *member,
  2690. const struct btf_type *member_type)
  2691. {
  2692. u32 struct_bits_off = member->offset;
  2693. u32 struct_size, bytes_offset;
  2694. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2695. btf_verifier_log_member(env, struct_type, member,
  2696. "Member is not byte aligned");
  2697. return -EINVAL;
  2698. }
  2699. struct_size = struct_type->size;
  2700. bytes_offset = BITS_ROUNDDOWN_BYTES(struct_bits_off);
  2701. if (struct_size - bytes_offset < member_type->size) {
  2702. btf_verifier_log_member(env, struct_type, member,
  2703. "Member exceeds struct_size");
  2704. return -EINVAL;
  2705. }
  2706. return 0;
  2707. }
  2708. static int btf_enum_check_kflag_member(struct btf_verifier_env *env,
  2709. const struct btf_type *struct_type,
  2710. const struct btf_member *member,
  2711. const struct btf_type *member_type)
  2712. {
  2713. u32 struct_bits_off, nr_bits, bytes_end, struct_size;
  2714. u32 int_bitsize = sizeof(int) * BITS_PER_BYTE;
  2715. struct_bits_off = BTF_MEMBER_BIT_OFFSET(member->offset);
  2716. nr_bits = BTF_MEMBER_BITFIELD_SIZE(member->offset);
  2717. if (!nr_bits) {
  2718. if (BITS_PER_BYTE_MASKED(struct_bits_off)) {
  2719. btf_verifier_log_member(env, struct_type, member,
  2720. "Member is not byte aligned");
  2721. return -EINVAL;
  2722. }
  2723. nr_bits = int_bitsize;
  2724. } else if (nr_bits > int_bitsize) {
  2725. btf_verifier_log_member(env, struct_type, member,
  2726. "Invalid member bitfield_size");
  2727. return -EINVAL;
  2728. }
  2729. struct_size = struct_type->size;
  2730. bytes_end = BITS_ROUNDUP_BYTES(struct_bits_off + nr_bits);
  2731. if (struct_size < bytes_end) {
  2732. btf_verifier_log_member(env, struct_type, member,
  2733. "Member exceeds struct_size");
  2734. return -EINVAL;
  2735. }
  2736. return 0;
  2737. }
  2738. static s32 btf_enum_check_meta(struct btf_verifier_env *env,
  2739. const struct btf_type *t,
  2740. u32 meta_left)
  2741. {
  2742. const struct btf_enum *enums = btf_type_enum(t);
  2743. struct btf *btf = env->btf;
  2744. u16 i, nr_enums;
  2745. u32 meta_needed;
  2746. nr_enums = btf_type_vlen(t);
  2747. meta_needed = nr_enums * sizeof(*enums);
  2748. if (meta_left < meta_needed) {
  2749. btf_verifier_log_basic(env, t,
  2750. "meta_left:%u meta_needed:%u",
  2751. meta_left, meta_needed);
  2752. return -EINVAL;
  2753. }
  2754. if (btf_type_kflag(t)) {
  2755. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2756. return -EINVAL;
  2757. }
  2758. if (t->size > 8 || !is_power_of_2(t->size)) {
  2759. btf_verifier_log_type(env, t, "Unexpected size");
  2760. return -EINVAL;
  2761. }
  2762. /* enum type either no name or a valid one */
  2763. if (t->name_off &&
  2764. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2765. btf_verifier_log_type(env, t, "Invalid name");
  2766. return -EINVAL;
  2767. }
  2768. btf_verifier_log_type(env, t, NULL);
  2769. for (i = 0; i < nr_enums; i++) {
  2770. if (!btf_name_offset_valid(btf, enums[i].name_off)) {
  2771. btf_verifier_log(env, "\tInvalid name_offset:%u",
  2772. enums[i].name_off);
  2773. return -EINVAL;
  2774. }
  2775. /* enum member must have a valid name */
  2776. if (!enums[i].name_off ||
  2777. !btf_name_valid_identifier(btf, enums[i].name_off)) {
  2778. btf_verifier_log_type(env, t, "Invalid name");
  2779. return -EINVAL;
  2780. }
  2781. if (env->log.level == BPF_LOG_KERNEL)
  2782. continue;
  2783. btf_verifier_log(env, "\t%s val=%d\n",
  2784. __btf_name_by_offset(btf, enums[i].name_off),
  2785. enums[i].val);
  2786. }
  2787. return meta_needed;
  2788. }
  2789. static void btf_enum_log(struct btf_verifier_env *env,
  2790. const struct btf_type *t)
  2791. {
  2792. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  2793. }
  2794. static void btf_enum_show(const struct btf *btf, const struct btf_type *t,
  2795. u32 type_id, void *data, u8 bits_offset,
  2796. struct btf_show *show)
  2797. {
  2798. const struct btf_enum *enums = btf_type_enum(t);
  2799. u32 i, nr_enums = btf_type_vlen(t);
  2800. void *safe_data;
  2801. int v;
  2802. safe_data = btf_show_start_type(show, t, type_id, data);
  2803. if (!safe_data)
  2804. return;
  2805. v = *(int *)safe_data;
  2806. for (i = 0; i < nr_enums; i++) {
  2807. if (v != enums[i].val)
  2808. continue;
  2809. btf_show_type_value(show, "%s",
  2810. __btf_name_by_offset(btf,
  2811. enums[i].name_off));
  2812. btf_show_end_type(show);
  2813. return;
  2814. }
  2815. btf_show_type_value(show, "%d", v);
  2816. btf_show_end_type(show);
  2817. }
  2818. static struct btf_kind_operations enum_ops = {
  2819. .check_meta = btf_enum_check_meta,
  2820. .resolve = btf_df_resolve,
  2821. .check_member = btf_enum_check_member,
  2822. .check_kflag_member = btf_enum_check_kflag_member,
  2823. .log_details = btf_enum_log,
  2824. .show = btf_enum_show,
  2825. };
  2826. static s32 btf_func_proto_check_meta(struct btf_verifier_env *env,
  2827. const struct btf_type *t,
  2828. u32 meta_left)
  2829. {
  2830. u32 meta_needed = btf_type_vlen(t) * sizeof(struct btf_param);
  2831. if (meta_left < meta_needed) {
  2832. btf_verifier_log_basic(env, t,
  2833. "meta_left:%u meta_needed:%u",
  2834. meta_left, meta_needed);
  2835. return -EINVAL;
  2836. }
  2837. if (t->name_off) {
  2838. btf_verifier_log_type(env, t, "Invalid name");
  2839. return -EINVAL;
  2840. }
  2841. if (btf_type_kflag(t)) {
  2842. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2843. return -EINVAL;
  2844. }
  2845. btf_verifier_log_type(env, t, NULL);
  2846. return meta_needed;
  2847. }
  2848. static void btf_func_proto_log(struct btf_verifier_env *env,
  2849. const struct btf_type *t)
  2850. {
  2851. const struct btf_param *args = (const struct btf_param *)(t + 1);
  2852. u16 nr_args = btf_type_vlen(t), i;
  2853. btf_verifier_log(env, "return=%u args=(", t->type);
  2854. if (!nr_args) {
  2855. btf_verifier_log(env, "void");
  2856. goto done;
  2857. }
  2858. if (nr_args == 1 && !args[0].type) {
  2859. /* Only one vararg */
  2860. btf_verifier_log(env, "vararg");
  2861. goto done;
  2862. }
  2863. btf_verifier_log(env, "%u %s", args[0].type,
  2864. __btf_name_by_offset(env->btf,
  2865. args[0].name_off));
  2866. for (i = 1; i < nr_args - 1; i++)
  2867. btf_verifier_log(env, ", %u %s", args[i].type,
  2868. __btf_name_by_offset(env->btf,
  2869. args[i].name_off));
  2870. if (nr_args > 1) {
  2871. const struct btf_param *last_arg = &args[nr_args - 1];
  2872. if (last_arg->type)
  2873. btf_verifier_log(env, ", %u %s", last_arg->type,
  2874. __btf_name_by_offset(env->btf,
  2875. last_arg->name_off));
  2876. else
  2877. btf_verifier_log(env, ", vararg");
  2878. }
  2879. done:
  2880. btf_verifier_log(env, ")");
  2881. }
  2882. static struct btf_kind_operations func_proto_ops = {
  2883. .check_meta = btf_func_proto_check_meta,
  2884. .resolve = btf_df_resolve,
  2885. /*
  2886. * BTF_KIND_FUNC_PROTO cannot be directly referred by
  2887. * a struct's member.
  2888. *
  2889. * It should be a funciton pointer instead.
  2890. * (i.e. struct's member -> BTF_KIND_PTR -> BTF_KIND_FUNC_PROTO)
  2891. *
  2892. * Hence, there is no btf_func_check_member().
  2893. */
  2894. .check_member = btf_df_check_member,
  2895. .check_kflag_member = btf_df_check_kflag_member,
  2896. .log_details = btf_func_proto_log,
  2897. .show = btf_df_show,
  2898. };
  2899. static s32 btf_func_check_meta(struct btf_verifier_env *env,
  2900. const struct btf_type *t,
  2901. u32 meta_left)
  2902. {
  2903. if (!t->name_off ||
  2904. !btf_name_valid_identifier(env->btf, t->name_off)) {
  2905. btf_verifier_log_type(env, t, "Invalid name");
  2906. return -EINVAL;
  2907. }
  2908. if (btf_type_vlen(t) > BTF_FUNC_GLOBAL) {
  2909. btf_verifier_log_type(env, t, "Invalid func linkage");
  2910. return -EINVAL;
  2911. }
  2912. if (btf_type_kflag(t)) {
  2913. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2914. return -EINVAL;
  2915. }
  2916. btf_verifier_log_type(env, t, NULL);
  2917. return 0;
  2918. }
  2919. static struct btf_kind_operations func_ops = {
  2920. .check_meta = btf_func_check_meta,
  2921. .resolve = btf_df_resolve,
  2922. .check_member = btf_df_check_member,
  2923. .check_kflag_member = btf_df_check_kflag_member,
  2924. .log_details = btf_ref_type_log,
  2925. .show = btf_df_show,
  2926. };
  2927. static s32 btf_var_check_meta(struct btf_verifier_env *env,
  2928. const struct btf_type *t,
  2929. u32 meta_left)
  2930. {
  2931. const struct btf_var *var;
  2932. u32 meta_needed = sizeof(*var);
  2933. if (meta_left < meta_needed) {
  2934. btf_verifier_log_basic(env, t,
  2935. "meta_left:%u meta_needed:%u",
  2936. meta_left, meta_needed);
  2937. return -EINVAL;
  2938. }
  2939. if (btf_type_vlen(t)) {
  2940. btf_verifier_log_type(env, t, "vlen != 0");
  2941. return -EINVAL;
  2942. }
  2943. if (btf_type_kflag(t)) {
  2944. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  2945. return -EINVAL;
  2946. }
  2947. if (!t->name_off ||
  2948. !__btf_name_valid(env->btf, t->name_off, true)) {
  2949. btf_verifier_log_type(env, t, "Invalid name");
  2950. return -EINVAL;
  2951. }
  2952. /* A var cannot be in type void */
  2953. if (!t->type || !BTF_TYPE_ID_VALID(t->type)) {
  2954. btf_verifier_log_type(env, t, "Invalid type_id");
  2955. return -EINVAL;
  2956. }
  2957. var = btf_type_var(t);
  2958. if (var->linkage != BTF_VAR_STATIC &&
  2959. var->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
  2960. btf_verifier_log_type(env, t, "Linkage not supported");
  2961. return -EINVAL;
  2962. }
  2963. btf_verifier_log_type(env, t, NULL);
  2964. return meta_needed;
  2965. }
  2966. static void btf_var_log(struct btf_verifier_env *env, const struct btf_type *t)
  2967. {
  2968. const struct btf_var *var = btf_type_var(t);
  2969. btf_verifier_log(env, "type_id=%u linkage=%u", t->type, var->linkage);
  2970. }
  2971. static const struct btf_kind_operations var_ops = {
  2972. .check_meta = btf_var_check_meta,
  2973. .resolve = btf_var_resolve,
  2974. .check_member = btf_df_check_member,
  2975. .check_kflag_member = btf_df_check_kflag_member,
  2976. .log_details = btf_var_log,
  2977. .show = btf_var_show,
  2978. };
  2979. static s32 btf_datasec_check_meta(struct btf_verifier_env *env,
  2980. const struct btf_type *t,
  2981. u32 meta_left)
  2982. {
  2983. const struct btf_var_secinfo *vsi;
  2984. u64 last_vsi_end_off = 0, sum = 0;
  2985. u32 i, meta_needed;
  2986. meta_needed = btf_type_vlen(t) * sizeof(*vsi);
  2987. if (meta_left < meta_needed) {
  2988. btf_verifier_log_basic(env, t,
  2989. "meta_left:%u meta_needed:%u",
  2990. meta_left, meta_needed);
  2991. return -EINVAL;
  2992. }
  2993. if (!btf_type_vlen(t)) {
  2994. btf_verifier_log_type(env, t, "vlen == 0");
  2995. return -EINVAL;
  2996. }
  2997. if (!t->size) {
  2998. btf_verifier_log_type(env, t, "size == 0");
  2999. return -EINVAL;
  3000. }
  3001. if (btf_type_kflag(t)) {
  3002. btf_verifier_log_type(env, t, "Invalid btf_info kind_flag");
  3003. return -EINVAL;
  3004. }
  3005. if (!t->name_off ||
  3006. !btf_name_valid_section(env->btf, t->name_off)) {
  3007. btf_verifier_log_type(env, t, "Invalid name");
  3008. return -EINVAL;
  3009. }
  3010. btf_verifier_log_type(env, t, NULL);
  3011. for_each_vsi(i, t, vsi) {
  3012. /* A var cannot be in type void */
  3013. if (!vsi->type || !BTF_TYPE_ID_VALID(vsi->type)) {
  3014. btf_verifier_log_vsi(env, t, vsi,
  3015. "Invalid type_id");
  3016. return -EINVAL;
  3017. }
  3018. if (vsi->offset < last_vsi_end_off || vsi->offset >= t->size) {
  3019. btf_verifier_log_vsi(env, t, vsi,
  3020. "Invalid offset");
  3021. return -EINVAL;
  3022. }
  3023. if (!vsi->size || vsi->size > t->size) {
  3024. btf_verifier_log_vsi(env, t, vsi,
  3025. "Invalid size");
  3026. return -EINVAL;
  3027. }
  3028. last_vsi_end_off = vsi->offset + vsi->size;
  3029. if (last_vsi_end_off > t->size) {
  3030. btf_verifier_log_vsi(env, t, vsi,
  3031. "Invalid offset+size");
  3032. return -EINVAL;
  3033. }
  3034. btf_verifier_log_vsi(env, t, vsi, NULL);
  3035. sum += vsi->size;
  3036. }
  3037. if (t->size < sum) {
  3038. btf_verifier_log_type(env, t, "Invalid btf_info size");
  3039. return -EINVAL;
  3040. }
  3041. return meta_needed;
  3042. }
  3043. static int btf_datasec_resolve(struct btf_verifier_env *env,
  3044. const struct resolve_vertex *v)
  3045. {
  3046. const struct btf_var_secinfo *vsi;
  3047. struct btf *btf = env->btf;
  3048. u16 i;
  3049. for_each_vsi_from(i, v->next_member, v->t, vsi) {
  3050. u32 var_type_id = vsi->type, type_id, type_size = 0;
  3051. const struct btf_type *var_type = btf_type_by_id(env->btf,
  3052. var_type_id);
  3053. if (!var_type || !btf_type_is_var(var_type)) {
  3054. btf_verifier_log_vsi(env, v->t, vsi,
  3055. "Not a VAR kind member");
  3056. return -EINVAL;
  3057. }
  3058. if (!env_type_is_resolve_sink(env, var_type) &&
  3059. !env_type_is_resolved(env, var_type_id)) {
  3060. env_stack_set_next_member(env, i + 1);
  3061. return env_stack_push(env, var_type, var_type_id);
  3062. }
  3063. type_id = var_type->type;
  3064. if (!btf_type_id_size(btf, &type_id, &type_size)) {
  3065. btf_verifier_log_vsi(env, v->t, vsi, "Invalid type");
  3066. return -EINVAL;
  3067. }
  3068. if (vsi->size < type_size) {
  3069. btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
  3070. return -EINVAL;
  3071. }
  3072. }
  3073. env_stack_pop_resolved(env, 0, 0);
  3074. return 0;
  3075. }
  3076. static void btf_datasec_log(struct btf_verifier_env *env,
  3077. const struct btf_type *t)
  3078. {
  3079. btf_verifier_log(env, "size=%u vlen=%u", t->size, btf_type_vlen(t));
  3080. }
  3081. static void btf_datasec_show(const struct btf *btf,
  3082. const struct btf_type *t, u32 type_id,
  3083. void *data, u8 bits_offset,
  3084. struct btf_show *show)
  3085. {
  3086. const struct btf_var_secinfo *vsi;
  3087. const struct btf_type *var;
  3088. u32 i;
  3089. if (!btf_show_start_type(show, t, type_id, data))
  3090. return;
  3091. btf_show_type_value(show, "section (\"%s\") = {",
  3092. __btf_name_by_offset(btf, t->name_off));
  3093. for_each_vsi(i, t, vsi) {
  3094. var = btf_type_by_id(btf, vsi->type);
  3095. if (i)
  3096. btf_show(show, ",");
  3097. btf_type_ops(var)->show(btf, var, vsi->type,
  3098. data + vsi->offset, bits_offset, show);
  3099. }
  3100. btf_show_end_type(show);
  3101. }
  3102. static const struct btf_kind_operations datasec_ops = {
  3103. .check_meta = btf_datasec_check_meta,
  3104. .resolve = btf_datasec_resolve,
  3105. .check_member = btf_df_check_member,
  3106. .check_kflag_member = btf_df_check_kflag_member,
  3107. .log_details = btf_datasec_log,
  3108. .show = btf_datasec_show,
  3109. };
  3110. static int btf_func_proto_check(struct btf_verifier_env *env,
  3111. const struct btf_type *t)
  3112. {
  3113. const struct btf_type *ret_type;
  3114. const struct btf_param *args;
  3115. const struct btf *btf;
  3116. u16 nr_args, i;
  3117. int err;
  3118. btf = env->btf;
  3119. args = (const struct btf_param *)(t + 1);
  3120. nr_args = btf_type_vlen(t);
  3121. /* Check func return type which could be "void" (t->type == 0) */
  3122. if (t->type) {
  3123. u32 ret_type_id = t->type;
  3124. ret_type = btf_type_by_id(btf, ret_type_id);
  3125. if (!ret_type) {
  3126. btf_verifier_log_type(env, t, "Invalid return type");
  3127. return -EINVAL;
  3128. }
  3129. if (btf_type_needs_resolve(ret_type) &&
  3130. !env_type_is_resolved(env, ret_type_id)) {
  3131. err = btf_resolve(env, ret_type, ret_type_id);
  3132. if (err)
  3133. return err;
  3134. }
  3135. /* Ensure the return type is a type that has a size */
  3136. if (!btf_type_id_size(btf, &ret_type_id, NULL)) {
  3137. btf_verifier_log_type(env, t, "Invalid return type");
  3138. return -EINVAL;
  3139. }
  3140. }
  3141. if (!nr_args)
  3142. return 0;
  3143. /* Last func arg type_id could be 0 if it is a vararg */
  3144. if (!args[nr_args - 1].type) {
  3145. if (args[nr_args - 1].name_off) {
  3146. btf_verifier_log_type(env, t, "Invalid arg#%u",
  3147. nr_args);
  3148. return -EINVAL;
  3149. }
  3150. nr_args--;
  3151. }
  3152. err = 0;
  3153. for (i = 0; i < nr_args; i++) {
  3154. const struct btf_type *arg_type;
  3155. u32 arg_type_id;
  3156. arg_type_id = args[i].type;
  3157. arg_type = btf_type_by_id(btf, arg_type_id);
  3158. if (!arg_type) {
  3159. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  3160. err = -EINVAL;
  3161. break;
  3162. }
  3163. if (args[i].name_off &&
  3164. (!btf_name_offset_valid(btf, args[i].name_off) ||
  3165. !btf_name_valid_identifier(btf, args[i].name_off))) {
  3166. btf_verifier_log_type(env, t,
  3167. "Invalid arg#%u", i + 1);
  3168. err = -EINVAL;
  3169. break;
  3170. }
  3171. if (btf_type_needs_resolve(arg_type) &&
  3172. !env_type_is_resolved(env, arg_type_id)) {
  3173. err = btf_resolve(env, arg_type, arg_type_id);
  3174. if (err)
  3175. break;
  3176. }
  3177. if (!btf_type_id_size(btf, &arg_type_id, NULL)) {
  3178. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  3179. err = -EINVAL;
  3180. break;
  3181. }
  3182. }
  3183. return err;
  3184. }
  3185. static int btf_func_check(struct btf_verifier_env *env,
  3186. const struct btf_type *t)
  3187. {
  3188. const struct btf_type *proto_type;
  3189. const struct btf_param *args;
  3190. const struct btf *btf;
  3191. u16 nr_args, i;
  3192. btf = env->btf;
  3193. proto_type = btf_type_by_id(btf, t->type);
  3194. if (!proto_type || !btf_type_is_func_proto(proto_type)) {
  3195. btf_verifier_log_type(env, t, "Invalid type_id");
  3196. return -EINVAL;
  3197. }
  3198. args = (const struct btf_param *)(proto_type + 1);
  3199. nr_args = btf_type_vlen(proto_type);
  3200. for (i = 0; i < nr_args; i++) {
  3201. if (!args[i].name_off && args[i].type) {
  3202. btf_verifier_log_type(env, t, "Invalid arg#%u", i + 1);
  3203. return -EINVAL;
  3204. }
  3205. }
  3206. return 0;
  3207. }
  3208. static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
  3209. [BTF_KIND_INT] = &int_ops,
  3210. [BTF_KIND_PTR] = &ptr_ops,
  3211. [BTF_KIND_ARRAY] = &array_ops,
  3212. [BTF_KIND_STRUCT] = &struct_ops,
  3213. [BTF_KIND_UNION] = &struct_ops,
  3214. [BTF_KIND_ENUM] = &enum_ops,
  3215. [BTF_KIND_FWD] = &fwd_ops,
  3216. [BTF_KIND_TYPEDEF] = &modifier_ops,
  3217. [BTF_KIND_VOLATILE] = &modifier_ops,
  3218. [BTF_KIND_CONST] = &modifier_ops,
  3219. [BTF_KIND_RESTRICT] = &modifier_ops,
  3220. [BTF_KIND_FUNC] = &func_ops,
  3221. [BTF_KIND_FUNC_PROTO] = &func_proto_ops,
  3222. [BTF_KIND_VAR] = &var_ops,
  3223. [BTF_KIND_DATASEC] = &datasec_ops,
  3224. };
  3225. static s32 btf_check_meta(struct btf_verifier_env *env,
  3226. const struct btf_type *t,
  3227. u32 meta_left)
  3228. {
  3229. u32 saved_meta_left = meta_left;
  3230. s32 var_meta_size;
  3231. if (meta_left < sizeof(*t)) {
  3232. btf_verifier_log(env, "[%u] meta_left:%u meta_needed:%zu",
  3233. env->log_type_id, meta_left, sizeof(*t));
  3234. return -EINVAL;
  3235. }
  3236. meta_left -= sizeof(*t);
  3237. if (t->info & ~BTF_INFO_MASK) {
  3238. btf_verifier_log(env, "[%u] Invalid btf_info:%x",
  3239. env->log_type_id, t->info);
  3240. return -EINVAL;
  3241. }
  3242. if (BTF_INFO_KIND(t->info) > BTF_KIND_MAX ||
  3243. BTF_INFO_KIND(t->info) == BTF_KIND_UNKN) {
  3244. btf_verifier_log(env, "[%u] Invalid kind:%u",
  3245. env->log_type_id, BTF_INFO_KIND(t->info));
  3246. return -EINVAL;
  3247. }
  3248. if (!btf_name_offset_valid(env->btf, t->name_off)) {
  3249. btf_verifier_log(env, "[%u] Invalid name_offset:%u",
  3250. env->log_type_id, t->name_off);
  3251. return -EINVAL;
  3252. }
  3253. var_meta_size = btf_type_ops(t)->check_meta(env, t, meta_left);
  3254. if (var_meta_size < 0)
  3255. return var_meta_size;
  3256. meta_left -= var_meta_size;
  3257. return saved_meta_left - meta_left;
  3258. }
  3259. static int btf_check_all_metas(struct btf_verifier_env *env)
  3260. {
  3261. struct btf *btf = env->btf;
  3262. struct btf_header *hdr;
  3263. void *cur, *end;
  3264. hdr = &btf->hdr;
  3265. cur = btf->nohdr_data + hdr->type_off;
  3266. end = cur + hdr->type_len;
  3267. env->log_type_id = 1;
  3268. while (cur < end) {
  3269. struct btf_type *t = cur;
  3270. s32 meta_size;
  3271. meta_size = btf_check_meta(env, t, end - cur);
  3272. if (meta_size < 0)
  3273. return meta_size;
  3274. btf_add_type(env, t);
  3275. cur += meta_size;
  3276. env->log_type_id++;
  3277. }
  3278. return 0;
  3279. }
  3280. static bool btf_resolve_valid(struct btf_verifier_env *env,
  3281. const struct btf_type *t,
  3282. u32 type_id)
  3283. {
  3284. struct btf *btf = env->btf;
  3285. if (!env_type_is_resolved(env, type_id))
  3286. return false;
  3287. if (btf_type_is_struct(t) || btf_type_is_datasec(t))
  3288. return !btf->resolved_ids[type_id] &&
  3289. !btf->resolved_sizes[type_id];
  3290. if (btf_type_is_modifier(t) || btf_type_is_ptr(t) ||
  3291. btf_type_is_var(t)) {
  3292. t = btf_type_id_resolve(btf, &type_id);
  3293. return t &&
  3294. !btf_type_is_modifier(t) &&
  3295. !btf_type_is_var(t) &&
  3296. !btf_type_is_datasec(t);
  3297. }
  3298. if (btf_type_is_array(t)) {
  3299. const struct btf_array *array = btf_type_array(t);
  3300. const struct btf_type *elem_type;
  3301. u32 elem_type_id = array->type;
  3302. u32 elem_size;
  3303. elem_type = btf_type_id_size(btf, &elem_type_id, &elem_size);
  3304. return elem_type && !btf_type_is_modifier(elem_type) &&
  3305. (array->nelems * elem_size ==
  3306. btf->resolved_sizes[type_id]);
  3307. }
  3308. return false;
  3309. }
  3310. static int btf_resolve(struct btf_verifier_env *env,
  3311. const struct btf_type *t, u32 type_id)
  3312. {
  3313. u32 save_log_type_id = env->log_type_id;
  3314. const struct resolve_vertex *v;
  3315. int err = 0;
  3316. env->resolve_mode = RESOLVE_TBD;
  3317. env_stack_push(env, t, type_id);
  3318. while (!err && (v = env_stack_peak(env))) {
  3319. env->log_type_id = v->type_id;
  3320. err = btf_type_ops(v->t)->resolve(env, v);
  3321. }
  3322. env->log_type_id = type_id;
  3323. if (err == -E2BIG) {
  3324. btf_verifier_log_type(env, t,
  3325. "Exceeded max resolving depth:%u",
  3326. MAX_RESOLVE_DEPTH);
  3327. } else if (err == -EEXIST) {
  3328. btf_verifier_log_type(env, t, "Loop detected");
  3329. }
  3330. /* Final sanity check */
  3331. if (!err && !btf_resolve_valid(env, t, type_id)) {
  3332. btf_verifier_log_type(env, t, "Invalid resolve state");
  3333. err = -EINVAL;
  3334. }
  3335. env->log_type_id = save_log_type_id;
  3336. return err;
  3337. }
  3338. static int btf_check_all_types(struct btf_verifier_env *env)
  3339. {
  3340. struct btf *btf = env->btf;
  3341. u32 type_id;
  3342. int err;
  3343. err = env_resolve_init(env);
  3344. if (err)
  3345. return err;
  3346. env->phase++;
  3347. for (type_id = 1; type_id <= btf->nr_types; type_id++) {
  3348. const struct btf_type *t = btf_type_by_id(btf, type_id);
  3349. env->log_type_id = type_id;
  3350. if (btf_type_needs_resolve(t) &&
  3351. !env_type_is_resolved(env, type_id)) {
  3352. err = btf_resolve(env, t, type_id);
  3353. if (err)
  3354. return err;
  3355. }
  3356. if (btf_type_is_func_proto(t)) {
  3357. err = btf_func_proto_check(env, t);
  3358. if (err)
  3359. return err;
  3360. }
  3361. if (btf_type_is_func(t)) {
  3362. err = btf_func_check(env, t);
  3363. if (err)
  3364. return err;
  3365. }
  3366. }
  3367. return 0;
  3368. }
  3369. static int btf_parse_type_sec(struct btf_verifier_env *env)
  3370. {
  3371. const struct btf_header *hdr = &env->btf->hdr;
  3372. int err;
  3373. /* Type section must align to 4 bytes */
  3374. if (hdr->type_off & (sizeof(u32) - 1)) {
  3375. btf_verifier_log(env, "Unaligned type_off");
  3376. return -EINVAL;
  3377. }
  3378. if (!hdr->type_len) {
  3379. btf_verifier_log(env, "No type found");
  3380. return -EINVAL;
  3381. }
  3382. err = btf_check_all_metas(env);
  3383. if (err)
  3384. return err;
  3385. return btf_check_all_types(env);
  3386. }
  3387. static int btf_parse_str_sec(struct btf_verifier_env *env)
  3388. {
  3389. const struct btf_header *hdr;
  3390. struct btf *btf = env->btf;
  3391. const char *start, *end;
  3392. hdr = &btf->hdr;
  3393. start = btf->nohdr_data + hdr->str_off;
  3394. end = start + hdr->str_len;
  3395. if (end != btf->data + btf->data_size) {
  3396. btf_verifier_log(env, "String section is not at the end");
  3397. return -EINVAL;
  3398. }
  3399. if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
  3400. start[0] || end[-1]) {
  3401. btf_verifier_log(env, "Invalid string section");
  3402. return -EINVAL;
  3403. }
  3404. btf->strings = start;
  3405. return 0;
  3406. }
  3407. static const size_t btf_sec_info_offset[] = {
  3408. offsetof(struct btf_header, type_off),
  3409. offsetof(struct btf_header, str_off),
  3410. };
  3411. static int btf_sec_info_cmp(const void *a, const void *b)
  3412. {
  3413. const struct btf_sec_info *x = a;
  3414. const struct btf_sec_info *y = b;
  3415. return (int)(x->off - y->off) ? : (int)(x->len - y->len);
  3416. }
  3417. static int btf_check_sec_info(struct btf_verifier_env *env,
  3418. u32 btf_data_size)
  3419. {
  3420. struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
  3421. u32 total, expected_total, i;
  3422. const struct btf_header *hdr;
  3423. const struct btf *btf;
  3424. btf = env->btf;
  3425. hdr = &btf->hdr;
  3426. /* Populate the secs from hdr */
  3427. for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
  3428. secs[i] = *(struct btf_sec_info *)((void *)hdr +
  3429. btf_sec_info_offset[i]);
  3430. sort(secs, ARRAY_SIZE(btf_sec_info_offset),
  3431. sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
  3432. /* Check for gaps and overlap among sections */
  3433. total = 0;
  3434. expected_total = btf_data_size - hdr->hdr_len;
  3435. for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
  3436. if (expected_total < secs[i].off) {
  3437. btf_verifier_log(env, "Invalid section offset");
  3438. return -EINVAL;
  3439. }
  3440. if (total < secs[i].off) {
  3441. /* gap */
  3442. btf_verifier_log(env, "Unsupported section found");
  3443. return -EINVAL;
  3444. }
  3445. if (total > secs[i].off) {
  3446. btf_verifier_log(env, "Section overlap found");
  3447. return -EINVAL;
  3448. }
  3449. if (expected_total - total < secs[i].len) {
  3450. btf_verifier_log(env,
  3451. "Total section length too long");
  3452. return -EINVAL;
  3453. }
  3454. total += secs[i].len;
  3455. }
  3456. /* There is data other than hdr and known sections */
  3457. if (expected_total != total) {
  3458. btf_verifier_log(env, "Unsupported section found");
  3459. return -EINVAL;
  3460. }
  3461. return 0;
  3462. }
  3463. static int btf_parse_hdr(struct btf_verifier_env *env)
  3464. {
  3465. u32 hdr_len, hdr_copy, btf_data_size;
  3466. const struct btf_header *hdr;
  3467. struct btf *btf;
  3468. int err;
  3469. btf = env->btf;
  3470. btf_data_size = btf->data_size;
  3471. if (btf_data_size <
  3472. offsetof(struct btf_header, hdr_len) + sizeof(hdr->hdr_len)) {
  3473. btf_verifier_log(env, "hdr_len not found");
  3474. return -EINVAL;
  3475. }
  3476. hdr = btf->data;
  3477. hdr_len = hdr->hdr_len;
  3478. if (btf_data_size < hdr_len) {
  3479. btf_verifier_log(env, "btf_header not found");
  3480. return -EINVAL;
  3481. }
  3482. /* Ensure the unsupported header fields are zero */
  3483. if (hdr_len > sizeof(btf->hdr)) {
  3484. u8 *expected_zero = btf->data + sizeof(btf->hdr);
  3485. u8 *end = btf->data + hdr_len;
  3486. for (; expected_zero < end; expected_zero++) {
  3487. if (*expected_zero) {
  3488. btf_verifier_log(env, "Unsupported btf_header");
  3489. return -E2BIG;
  3490. }
  3491. }
  3492. }
  3493. hdr_copy = min_t(u32, hdr_len, sizeof(btf->hdr));
  3494. memcpy(&btf->hdr, btf->data, hdr_copy);
  3495. hdr = &btf->hdr;
  3496. btf_verifier_log_hdr(env, btf_data_size);
  3497. if (hdr->magic != BTF_MAGIC) {
  3498. btf_verifier_log(env, "Invalid magic");
  3499. return -EINVAL;
  3500. }
  3501. if (hdr->version != BTF_VERSION) {
  3502. btf_verifier_log(env, "Unsupported version");
  3503. return -ENOTSUPP;
  3504. }
  3505. if (hdr->flags) {
  3506. btf_verifier_log(env, "Unsupported flags");
  3507. return -ENOTSUPP;
  3508. }
  3509. if (btf_data_size == hdr->hdr_len) {
  3510. btf_verifier_log(env, "No data");
  3511. return -EINVAL;
  3512. }
  3513. err = btf_check_sec_info(env, btf_data_size);
  3514. if (err)
  3515. return err;
  3516. return 0;
  3517. }
  3518. static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
  3519. u32 log_level, char __user *log_ubuf, u32 log_size)
  3520. {
  3521. struct btf_verifier_env *env = NULL;
  3522. struct bpf_verifier_log *log;
  3523. struct btf *btf = NULL;
  3524. u8 *data;
  3525. int err;
  3526. if (btf_data_size > BTF_MAX_SIZE)
  3527. return ERR_PTR(-E2BIG);
  3528. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  3529. if (!env)
  3530. return ERR_PTR(-ENOMEM);
  3531. log = &env->log;
  3532. if (log_level || log_ubuf || log_size) {
  3533. /* user requested verbose verifier output
  3534. * and supplied buffer to store the verification trace
  3535. */
  3536. log->level = log_level;
  3537. log->ubuf = log_ubuf;
  3538. log->len_total = log_size;
  3539. /* log attributes have to be sane */
  3540. if (!bpf_verifier_log_attr_valid(log)) {
  3541. err = -EINVAL;
  3542. goto errout;
  3543. }
  3544. }
  3545. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  3546. if (!btf) {
  3547. err = -ENOMEM;
  3548. goto errout;
  3549. }
  3550. env->btf = btf;
  3551. data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
  3552. if (!data) {
  3553. err = -ENOMEM;
  3554. goto errout;
  3555. }
  3556. btf->data = data;
  3557. btf->data_size = btf_data_size;
  3558. if (copy_from_user(data, btf_data, btf_data_size)) {
  3559. err = -EFAULT;
  3560. goto errout;
  3561. }
  3562. err = btf_parse_hdr(env);
  3563. if (err)
  3564. goto errout;
  3565. btf->nohdr_data = btf->data + btf->hdr.hdr_len;
  3566. err = btf_parse_str_sec(env);
  3567. if (err)
  3568. goto errout;
  3569. err = btf_parse_type_sec(env);
  3570. if (err)
  3571. goto errout;
  3572. if (log->level && bpf_verifier_log_full(log)) {
  3573. err = -ENOSPC;
  3574. goto errout;
  3575. }
  3576. btf_verifier_env_free(env);
  3577. refcount_set(&btf->refcnt, 1);
  3578. return btf;
  3579. errout:
  3580. btf_verifier_env_free(env);
  3581. if (btf)
  3582. btf_free(btf);
  3583. return ERR_PTR(err);
  3584. }
  3585. extern char __weak __start_BTF[];
  3586. extern char __weak __stop_BTF[];
  3587. extern struct btf *btf_vmlinux;
  3588. #define BPF_MAP_TYPE(_id, _ops)
  3589. #define BPF_LINK_TYPE(_id, _name)
  3590. static union {
  3591. struct bpf_ctx_convert {
  3592. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  3593. prog_ctx_type _id##_prog; \
  3594. kern_ctx_type _id##_kern;
  3595. #include <linux/bpf_types.h>
  3596. #undef BPF_PROG_TYPE
  3597. } *__t;
  3598. /* 't' is written once under lock. Read many times. */
  3599. const struct btf_type *t;
  3600. } bpf_ctx_convert;
  3601. enum {
  3602. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  3603. __ctx_convert##_id,
  3604. #include <linux/bpf_types.h>
  3605. #undef BPF_PROG_TYPE
  3606. __ctx_convert_unused, /* to avoid empty enum in extreme .config */
  3607. };
  3608. static u8 bpf_ctx_convert_map[] = {
  3609. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
  3610. [_id] = __ctx_convert##_id,
  3611. #include <linux/bpf_types.h>
  3612. #undef BPF_PROG_TYPE
  3613. 0, /* avoid empty array */
  3614. };
  3615. #undef BPF_MAP_TYPE
  3616. #undef BPF_LINK_TYPE
  3617. static const struct btf_member *
  3618. btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,
  3619. const struct btf_type *t, enum bpf_prog_type prog_type,
  3620. int arg)
  3621. {
  3622. const struct btf_type *conv_struct;
  3623. const struct btf_type *ctx_struct;
  3624. const struct btf_member *ctx_type;
  3625. const char *tname, *ctx_tname;
  3626. conv_struct = bpf_ctx_convert.t;
  3627. if (!conv_struct) {
  3628. bpf_log(log, "btf_vmlinux is malformed\n");
  3629. return NULL;
  3630. }
  3631. t = btf_type_by_id(btf, t->type);
  3632. while (btf_type_is_modifier(t))
  3633. t = btf_type_by_id(btf, t->type);
  3634. if (!btf_type_is_struct(t)) {
  3635. /* Only pointer to struct is supported for now.
  3636. * That means that BPF_PROG_TYPE_TRACEPOINT with BTF
  3637. * is not supported yet.
  3638. * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
  3639. */
  3640. if (log->level & BPF_LOG_LEVEL)
  3641. bpf_log(log, "arg#%d type is not a struct\n", arg);
  3642. return NULL;
  3643. }
  3644. tname = btf_name_by_offset(btf, t->name_off);
  3645. if (!tname) {
  3646. bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
  3647. return NULL;
  3648. }
  3649. /* prog_type is valid bpf program type. No need for bounds check. */
  3650. ctx_type = btf_type_member(conv_struct) + bpf_ctx_convert_map[prog_type] * 2;
  3651. /* ctx_struct is a pointer to prog_ctx_type in vmlinux.
  3652. * Like 'struct __sk_buff'
  3653. */
  3654. ctx_struct = btf_type_by_id(btf_vmlinux, ctx_type->type);
  3655. if (!ctx_struct)
  3656. /* should not happen */
  3657. return NULL;
  3658. ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_struct->name_off);
  3659. if (!ctx_tname) {
  3660. /* should not happen */
  3661. bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
  3662. return NULL;
  3663. }
  3664. /* only compare that prog's ctx type name is the same as
  3665. * kernel expects. No need to compare field by field.
  3666. * It's ok for bpf prog to do:
  3667. * struct __sk_buff {};
  3668. * int socket_filter_bpf_prog(struct __sk_buff *skb)
  3669. * { // no fields of skb are ever used }
  3670. */
  3671. if (strcmp(ctx_tname, tname))
  3672. return NULL;
  3673. return ctx_type;
  3674. }
  3675. static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = {
  3676. #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
  3677. #define BPF_LINK_TYPE(_id, _name)
  3678. #define BPF_MAP_TYPE(_id, _ops) \
  3679. [_id] = &_ops,
  3680. #include <linux/bpf_types.h>
  3681. #undef BPF_PROG_TYPE
  3682. #undef BPF_LINK_TYPE
  3683. #undef BPF_MAP_TYPE
  3684. };
  3685. static int btf_vmlinux_map_ids_init(const struct btf *btf,
  3686. struct bpf_verifier_log *log)
  3687. {
  3688. const struct bpf_map_ops *ops;
  3689. int i, btf_id;
  3690. for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) {
  3691. ops = btf_vmlinux_map_ops[i];
  3692. if (!ops || (!ops->map_btf_name && !ops->map_btf_id))
  3693. continue;
  3694. if (!ops->map_btf_name || !ops->map_btf_id) {
  3695. bpf_log(log, "map type %d is misconfigured\n", i);
  3696. return -EINVAL;
  3697. }
  3698. btf_id = btf_find_by_name_kind(btf, ops->map_btf_name,
  3699. BTF_KIND_STRUCT);
  3700. if (btf_id < 0)
  3701. return btf_id;
  3702. *ops->map_btf_id = btf_id;
  3703. }
  3704. return 0;
  3705. }
  3706. static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
  3707. struct btf *btf,
  3708. const struct btf_type *t,
  3709. enum bpf_prog_type prog_type,
  3710. int arg)
  3711. {
  3712. const struct btf_member *prog_ctx_type, *kern_ctx_type;
  3713. prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
  3714. if (!prog_ctx_type)
  3715. return -ENOENT;
  3716. kern_ctx_type = prog_ctx_type + 1;
  3717. return kern_ctx_type->type;
  3718. }
  3719. BTF_ID_LIST(bpf_ctx_convert_btf_id)
  3720. BTF_ID(struct, bpf_ctx_convert)
  3721. struct btf *btf_parse_vmlinux(void)
  3722. {
  3723. struct btf_verifier_env *env = NULL;
  3724. struct bpf_verifier_log *log;
  3725. struct btf *btf = NULL;
  3726. int err;
  3727. env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
  3728. if (!env)
  3729. return ERR_PTR(-ENOMEM);
  3730. log = &env->log;
  3731. log->level = BPF_LOG_KERNEL;
  3732. btf = kzalloc(sizeof(*btf), GFP_KERNEL | __GFP_NOWARN);
  3733. if (!btf) {
  3734. err = -ENOMEM;
  3735. goto errout;
  3736. }
  3737. env->btf = btf;
  3738. btf->data = __start_BTF;
  3739. btf->data_size = __stop_BTF - __start_BTF;
  3740. err = btf_parse_hdr(env);
  3741. if (err)
  3742. goto errout;
  3743. btf->nohdr_data = btf->data + btf->hdr.hdr_len;
  3744. err = btf_parse_str_sec(env);
  3745. if (err)
  3746. goto errout;
  3747. err = btf_check_all_metas(env);
  3748. if (err)
  3749. goto errout;
  3750. /* btf_parse_vmlinux() runs under bpf_verifier_lock */
  3751. bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
  3752. /* find bpf map structs for map_ptr access checking */
  3753. err = btf_vmlinux_map_ids_init(btf, log);
  3754. if (err < 0)
  3755. goto errout;
  3756. bpf_struct_ops_init(btf, log);
  3757. btf_verifier_env_free(env);
  3758. refcount_set(&btf->refcnt, 1);
  3759. return btf;
  3760. errout:
  3761. btf_verifier_env_free(env);
  3762. if (btf) {
  3763. kvfree(btf->types);
  3764. kfree(btf);
  3765. }
  3766. return ERR_PTR(err);
  3767. }
  3768. struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
  3769. {
  3770. struct bpf_prog *tgt_prog = prog->aux->dst_prog;
  3771. if (tgt_prog) {
  3772. return tgt_prog->aux->btf;
  3773. } else {
  3774. return btf_vmlinux;
  3775. }
  3776. }
  3777. static bool is_string_ptr(struct btf *btf, const struct btf_type *t)
  3778. {
  3779. /* t comes in already as a pointer */
  3780. t = btf_type_by_id(btf, t->type);
  3781. /* allow const */
  3782. if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST)
  3783. t = btf_type_by_id(btf, t->type);
  3784. /* char, signed char, unsigned char */
  3785. return btf_type_is_int(t) && t->size == 1;
  3786. }
  3787. bool btf_ctx_access(int off, int size, enum bpf_access_type type,
  3788. const struct bpf_prog *prog,
  3789. struct bpf_insn_access_aux *info)
  3790. {
  3791. const struct btf_type *t = prog->aux->attach_func_proto;
  3792. struct bpf_prog *tgt_prog = prog->aux->dst_prog;
  3793. struct btf *btf = bpf_prog_get_target_btf(prog);
  3794. const char *tname = prog->aux->attach_func_name;
  3795. struct bpf_verifier_log *log = info->log;
  3796. const struct btf_param *args;
  3797. u32 nr_args, arg;
  3798. int i, ret;
  3799. if (off % 8) {
  3800. bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
  3801. tname, off);
  3802. return false;
  3803. }
  3804. arg = off / 8;
  3805. args = (const struct btf_param *)(t + 1);
  3806. /* if (t == NULL) Fall back to default BPF prog with 5 u64 arguments */
  3807. nr_args = t ? btf_type_vlen(t) : 5;
  3808. if (prog->aux->attach_btf_trace) {
  3809. /* skip first 'void *__data' argument in btf_trace_##name typedef */
  3810. args++;
  3811. nr_args--;
  3812. }
  3813. if (arg > nr_args) {
  3814. bpf_log(log, "func '%s' doesn't have %d-th argument\n",
  3815. tname, arg + 1);
  3816. return false;
  3817. }
  3818. if (arg == nr_args) {
  3819. switch (prog->expected_attach_type) {
  3820. case BPF_LSM_MAC:
  3821. case BPF_TRACE_FEXIT:
  3822. /* When LSM programs are attached to void LSM hooks
  3823. * they use FEXIT trampolines and when attached to
  3824. * int LSM hooks, they use MODIFY_RETURN trampolines.
  3825. *
  3826. * While the LSM programs are BPF_MODIFY_RETURN-like
  3827. * the check:
  3828. *
  3829. * if (ret_type != 'int')
  3830. * return -EINVAL;
  3831. *
  3832. * is _not_ done here. This is still safe as LSM hooks
  3833. * have only void and int return types.
  3834. */
  3835. if (!t)
  3836. return true;
  3837. t = btf_type_by_id(btf, t->type);
  3838. break;
  3839. case BPF_MODIFY_RETURN:
  3840. /* For now the BPF_MODIFY_RETURN can only be attached to
  3841. * functions that return an int.
  3842. */
  3843. if (!t)
  3844. return false;
  3845. t = btf_type_skip_modifiers(btf, t->type, NULL);
  3846. if (!btf_type_is_small_int(t)) {
  3847. bpf_log(log,
  3848. "ret type %s not allowed for fmod_ret\n",
  3849. btf_kind_str[BTF_INFO_KIND(t->info)]);
  3850. return false;
  3851. }
  3852. break;
  3853. default:
  3854. bpf_log(log, "func '%s' doesn't have %d-th argument\n",
  3855. tname, arg + 1);
  3856. return false;
  3857. }
  3858. } else {
  3859. if (!t)
  3860. /* Default prog with 5 args */
  3861. return true;
  3862. t = btf_type_by_id(btf, args[arg].type);
  3863. }
  3864. /* skip modifiers */
  3865. while (btf_type_is_modifier(t))
  3866. t = btf_type_by_id(btf, t->type);
  3867. if (btf_type_is_small_int(t) || btf_type_is_enum(t))
  3868. /* accessing a scalar */
  3869. return true;
  3870. if (!btf_type_is_ptr(t)) {
  3871. bpf_log(log,
  3872. "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
  3873. tname, arg,
  3874. __btf_name_by_offset(btf, t->name_off),
  3875. btf_kind_str[BTF_INFO_KIND(t->info)]);
  3876. return false;
  3877. }
  3878. /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
  3879. for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
  3880. const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
  3881. if (ctx_arg_info->offset == off &&
  3882. (ctx_arg_info->reg_type == PTR_TO_RDONLY_BUF_OR_NULL ||
  3883. ctx_arg_info->reg_type == PTR_TO_RDWR_BUF_OR_NULL)) {
  3884. info->reg_type = ctx_arg_info->reg_type;
  3885. return true;
  3886. }
  3887. }
  3888. if (t->type == 0)
  3889. /* This is a pointer to void.
  3890. * It is the same as scalar from the verifier safety pov.
  3891. * No further pointer walking is allowed.
  3892. */
  3893. return true;
  3894. if (is_string_ptr(btf, t))
  3895. return true;
  3896. /* this is a pointer to another type */
  3897. for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
  3898. const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
  3899. if (ctx_arg_info->offset == off) {
  3900. info->reg_type = ctx_arg_info->reg_type;
  3901. info->btf_id = ctx_arg_info->btf_id;
  3902. return true;
  3903. }
  3904. }
  3905. info->reg_type = PTR_TO_BTF_ID;
  3906. if (tgt_prog) {
  3907. enum bpf_prog_type tgt_type;
  3908. if (tgt_prog->type == BPF_PROG_TYPE_EXT)
  3909. tgt_type = tgt_prog->aux->saved_dst_prog_type;
  3910. else
  3911. tgt_type = tgt_prog->type;
  3912. ret = btf_translate_to_vmlinux(log, btf, t, tgt_type, arg);
  3913. if (ret > 0) {
  3914. info->btf_id = ret;
  3915. return true;
  3916. } else {
  3917. return false;
  3918. }
  3919. }
  3920. info->btf_id = t->type;
  3921. t = btf_type_by_id(btf, t->type);
  3922. /* skip modifiers */
  3923. while (btf_type_is_modifier(t)) {
  3924. info->btf_id = t->type;
  3925. t = btf_type_by_id(btf, t->type);
  3926. }
  3927. if (!btf_type_is_struct(t)) {
  3928. bpf_log(log,
  3929. "func '%s' arg%d type %s is not a struct\n",
  3930. tname, arg, btf_kind_str[BTF_INFO_KIND(t->info)]);
  3931. return false;
  3932. }
  3933. bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
  3934. tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)],
  3935. __btf_name_by_offset(btf, t->name_off));
  3936. return true;
  3937. }
  3938. enum bpf_struct_walk_result {
  3939. /* < 0 error */
  3940. WALK_SCALAR = 0,
  3941. WALK_PTR,
  3942. WALK_STRUCT,
  3943. };
  3944. static int btf_struct_walk(struct bpf_verifier_log *log,
  3945. const struct btf_type *t, int off, int size,
  3946. u32 *next_btf_id)
  3947. {
  3948. u32 i, moff, mtrue_end, msize = 0, total_nelems = 0;
  3949. const struct btf_type *mtype, *elem_type = NULL;
  3950. const struct btf_member *member;
  3951. const char *tname, *mname;
  3952. u32 vlen, elem_id, mid;
  3953. again:
  3954. tname = __btf_name_by_offset(btf_vmlinux, t->name_off);
  3955. if (!btf_type_is_struct(t)) {
  3956. bpf_log(log, "Type '%s' is not a struct\n", tname);
  3957. return -EINVAL;
  3958. }
  3959. vlen = btf_type_vlen(t);
  3960. if (off + size > t->size) {
  3961. /* If the last element is a variable size array, we may
  3962. * need to relax the rule.
  3963. */
  3964. struct btf_array *array_elem;
  3965. if (vlen == 0)
  3966. goto error;
  3967. member = btf_type_member(t) + vlen - 1;
  3968. mtype = btf_type_skip_modifiers(btf_vmlinux, member->type,
  3969. NULL);
  3970. if (!btf_type_is_array(mtype))
  3971. goto error;
  3972. array_elem = (struct btf_array *)(mtype + 1);
  3973. if (array_elem->nelems != 0)
  3974. goto error;
  3975. moff = btf_member_bit_offset(t, member) / 8;
  3976. if (off < moff)
  3977. goto error;
  3978. /* Only allow structure for now, can be relaxed for
  3979. * other types later.
  3980. */
  3981. t = btf_type_skip_modifiers(btf_vmlinux, array_elem->type,
  3982. NULL);
  3983. if (!btf_type_is_struct(t))
  3984. goto error;
  3985. off = (off - moff) % t->size;
  3986. goto again;
  3987. error:
  3988. bpf_log(log, "access beyond struct %s at off %u size %u\n",
  3989. tname, off, size);
  3990. return -EACCES;
  3991. }
  3992. for_each_member(i, t, member) {
  3993. /* offset of the field in bytes */
  3994. moff = btf_member_bit_offset(t, member) / 8;
  3995. if (off + size <= moff)
  3996. /* won't find anything, field is already too far */
  3997. break;
  3998. if (btf_member_bitfield_size(t, member)) {
  3999. u32 end_bit = btf_member_bit_offset(t, member) +
  4000. btf_member_bitfield_size(t, member);
  4001. /* off <= moff instead of off == moff because clang
  4002. * does not generate a BTF member for anonymous
  4003. * bitfield like the ":16" here:
  4004. * struct {
  4005. * int :16;
  4006. * int x:8;
  4007. * };
  4008. */
  4009. if (off <= moff &&
  4010. BITS_ROUNDUP_BYTES(end_bit) <= off + size)
  4011. return WALK_SCALAR;
  4012. /* off may be accessing a following member
  4013. *
  4014. * or
  4015. *
  4016. * Doing partial access at either end of this
  4017. * bitfield. Continue on this case also to
  4018. * treat it as not accessing this bitfield
  4019. * and eventually error out as field not
  4020. * found to keep it simple.
  4021. * It could be relaxed if there was a legit
  4022. * partial access case later.
  4023. */
  4024. continue;
  4025. }
  4026. /* In case of "off" is pointing to holes of a struct */
  4027. if (off < moff)
  4028. break;
  4029. /* type of the field */
  4030. mid = member->type;
  4031. mtype = btf_type_by_id(btf_vmlinux, member->type);
  4032. mname = __btf_name_by_offset(btf_vmlinux, member->name_off);
  4033. mtype = __btf_resolve_size(btf_vmlinux, mtype, &msize,
  4034. &elem_type, &elem_id, &total_nelems,
  4035. &mid);
  4036. if (IS_ERR(mtype)) {
  4037. bpf_log(log, "field %s doesn't have size\n", mname);
  4038. return -EFAULT;
  4039. }
  4040. mtrue_end = moff + msize;
  4041. if (off >= mtrue_end)
  4042. /* no overlap with member, keep iterating */
  4043. continue;
  4044. if (btf_type_is_array(mtype)) {
  4045. u32 elem_idx;
  4046. /* __btf_resolve_size() above helps to
  4047. * linearize a multi-dimensional array.
  4048. *
  4049. * The logic here is treating an array
  4050. * in a struct as the following way:
  4051. *
  4052. * struct outer {
  4053. * struct inner array[2][2];
  4054. * };
  4055. *
  4056. * looks like:
  4057. *
  4058. * struct outer {
  4059. * struct inner array_elem0;
  4060. * struct inner array_elem1;
  4061. * struct inner array_elem2;
  4062. * struct inner array_elem3;
  4063. * };
  4064. *
  4065. * When accessing outer->array[1][0], it moves
  4066. * moff to "array_elem2", set mtype to
  4067. * "struct inner", and msize also becomes
  4068. * sizeof(struct inner). Then most of the
  4069. * remaining logic will fall through without
  4070. * caring the current member is an array or
  4071. * not.
  4072. *
  4073. * Unlike mtype/msize/moff, mtrue_end does not
  4074. * change. The naming difference ("_true") tells
  4075. * that it is not always corresponding to
  4076. * the current mtype/msize/moff.
  4077. * It is the true end of the current
  4078. * member (i.e. array in this case). That
  4079. * will allow an int array to be accessed like
  4080. * a scratch space,
  4081. * i.e. allow access beyond the size of
  4082. * the array's element as long as it is
  4083. * within the mtrue_end boundary.
  4084. */
  4085. /* skip empty array */
  4086. if (moff == mtrue_end)
  4087. continue;
  4088. msize /= total_nelems;
  4089. elem_idx = (off - moff) / msize;
  4090. moff += elem_idx * msize;
  4091. mtype = elem_type;
  4092. mid = elem_id;
  4093. }
  4094. /* the 'off' we're looking for is either equal to start
  4095. * of this field or inside of this struct
  4096. */
  4097. if (btf_type_is_struct(mtype)) {
  4098. /* our field must be inside that union or struct */
  4099. t = mtype;
  4100. /* return if the offset matches the member offset */
  4101. if (off == moff) {
  4102. *next_btf_id = mid;
  4103. return WALK_STRUCT;
  4104. }
  4105. /* adjust offset we're looking for */
  4106. off -= moff;
  4107. goto again;
  4108. }
  4109. if (btf_type_is_ptr(mtype)) {
  4110. const struct btf_type *stype;
  4111. u32 id;
  4112. if (msize != size || off != moff) {
  4113. bpf_log(log,
  4114. "cannot access ptr member %s with moff %u in struct %s with off %u size %u\n",
  4115. mname, moff, tname, off, size);
  4116. return -EACCES;
  4117. }
  4118. stype = btf_type_skip_modifiers(btf_vmlinux, mtype->type, &id);
  4119. if (btf_type_is_struct(stype)) {
  4120. *next_btf_id = id;
  4121. return WALK_PTR;
  4122. }
  4123. }
  4124. /* Allow more flexible access within an int as long as
  4125. * it is within mtrue_end.
  4126. * Since mtrue_end could be the end of an array,
  4127. * that also allows using an array of int as a scratch
  4128. * space. e.g. skb->cb[].
  4129. */
  4130. if (off + size > mtrue_end) {
  4131. bpf_log(log,
  4132. "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n",
  4133. mname, mtrue_end, tname, off, size);
  4134. return -EACCES;
  4135. }
  4136. return WALK_SCALAR;
  4137. }
  4138. bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
  4139. return -EINVAL;
  4140. }
  4141. int btf_struct_access(struct bpf_verifier_log *log,
  4142. const struct btf_type *t, int off, int size,
  4143. enum bpf_access_type atype __maybe_unused,
  4144. u32 *next_btf_id)
  4145. {
  4146. int err;
  4147. u32 id;
  4148. do {
  4149. err = btf_struct_walk(log, t, off, size, &id);
  4150. switch (err) {
  4151. case WALK_PTR:
  4152. /* If we found the pointer or scalar on t+off,
  4153. * we're done.
  4154. */
  4155. *next_btf_id = id;
  4156. return PTR_TO_BTF_ID;
  4157. case WALK_SCALAR:
  4158. return SCALAR_VALUE;
  4159. case WALK_STRUCT:
  4160. /* We found nested struct, so continue the search
  4161. * by diving in it. At this point the offset is
  4162. * aligned with the new type, so set it to 0.
  4163. */
  4164. t = btf_type_by_id(btf_vmlinux, id);
  4165. off = 0;
  4166. break;
  4167. default:
  4168. /* It's either error or unknown return value..
  4169. * scream and leave.
  4170. */
  4171. if (WARN_ONCE(err > 0, "unknown btf_struct_walk return value"))
  4172. return -EINVAL;
  4173. return err;
  4174. }
  4175. } while (t);
  4176. return -EINVAL;
  4177. }
  4178. bool btf_struct_ids_match(struct bpf_verifier_log *log,
  4179. int off, u32 id, u32 need_type_id)
  4180. {
  4181. const struct btf_type *type;
  4182. int err;
  4183. /* Are we already done? */
  4184. if (need_type_id == id && off == 0)
  4185. return true;
  4186. again:
  4187. type = btf_type_by_id(btf_vmlinux, id);
  4188. if (!type)
  4189. return false;
  4190. err = btf_struct_walk(log, type, off, 1, &id);
  4191. if (err != WALK_STRUCT)
  4192. return false;
  4193. /* We found nested struct object. If it matches
  4194. * the requested ID, we're done. Otherwise let's
  4195. * continue the search with offset 0 in the new
  4196. * type.
  4197. */
  4198. if (need_type_id != id) {
  4199. off = 0;
  4200. goto again;
  4201. }
  4202. return true;
  4203. }
  4204. static int __get_type_size(struct btf *btf, u32 btf_id,
  4205. const struct btf_type **bad_type)
  4206. {
  4207. const struct btf_type *t;
  4208. if (!btf_id)
  4209. /* void */
  4210. return 0;
  4211. t = btf_type_by_id(btf, btf_id);
  4212. while (t && btf_type_is_modifier(t))
  4213. t = btf_type_by_id(btf, t->type);
  4214. if (!t) {
  4215. *bad_type = btf->types[0];
  4216. return -EINVAL;
  4217. }
  4218. if (btf_type_is_ptr(t))
  4219. /* kernel size of pointer. Not BPF's size of pointer*/
  4220. return sizeof(void *);
  4221. if (btf_type_is_int(t) || btf_type_is_enum(t))
  4222. return t->size;
  4223. *bad_type = t;
  4224. return -EINVAL;
  4225. }
  4226. int btf_distill_func_proto(struct bpf_verifier_log *log,
  4227. struct btf *btf,
  4228. const struct btf_type *func,
  4229. const char *tname,
  4230. struct btf_func_model *m)
  4231. {
  4232. const struct btf_param *args;
  4233. const struct btf_type *t;
  4234. u32 i, nargs;
  4235. int ret;
  4236. if (!func) {
  4237. /* BTF function prototype doesn't match the verifier types.
  4238. * Fall back to 5 u64 args.
  4239. */
  4240. for (i = 0; i < 5; i++)
  4241. m->arg_size[i] = 8;
  4242. m->ret_size = 8;
  4243. m->nr_args = 5;
  4244. return 0;
  4245. }
  4246. args = (const struct btf_param *)(func + 1);
  4247. nargs = btf_type_vlen(func);
  4248. if (nargs >= MAX_BPF_FUNC_ARGS) {
  4249. bpf_log(log,
  4250. "The function %s has %d arguments. Too many.\n",
  4251. tname, nargs);
  4252. return -EINVAL;
  4253. }
  4254. ret = __get_type_size(btf, func->type, &t);
  4255. if (ret < 0) {
  4256. bpf_log(log,
  4257. "The function %s return type %s is unsupported.\n",
  4258. tname, btf_kind_str[BTF_INFO_KIND(t->info)]);
  4259. return -EINVAL;
  4260. }
  4261. m->ret_size = ret;
  4262. for (i = 0; i < nargs; i++) {
  4263. if (i == nargs - 1 && args[i].type == 0) {
  4264. bpf_log(log,
  4265. "The function %s with variable args is unsupported.\n",
  4266. tname);
  4267. return -EINVAL;
  4268. }
  4269. ret = __get_type_size(btf, args[i].type, &t);
  4270. if (ret < 0) {
  4271. bpf_log(log,
  4272. "The function %s arg%d type %s is unsupported.\n",
  4273. tname, i, btf_kind_str[BTF_INFO_KIND(t->info)]);
  4274. return -EINVAL;
  4275. }
  4276. if (ret == 0) {
  4277. bpf_log(log,
  4278. "The function %s has malformed void argument.\n",
  4279. tname);
  4280. return -EINVAL;
  4281. }
  4282. m->arg_size[i] = ret;
  4283. }
  4284. m->nr_args = nargs;
  4285. return 0;
  4286. }
  4287. /* Compare BTFs of two functions assuming only scalars and pointers to context.
  4288. * t1 points to BTF_KIND_FUNC in btf1
  4289. * t2 points to BTF_KIND_FUNC in btf2
  4290. * Returns:
  4291. * EINVAL - function prototype mismatch
  4292. * EFAULT - verifier bug
  4293. * 0 - 99% match. The last 1% is validated by the verifier.
  4294. */
  4295. static int btf_check_func_type_match(struct bpf_verifier_log *log,
  4296. struct btf *btf1, const struct btf_type *t1,
  4297. struct btf *btf2, const struct btf_type *t2)
  4298. {
  4299. const struct btf_param *args1, *args2;
  4300. const char *fn1, *fn2, *s1, *s2;
  4301. u32 nargs1, nargs2, i;
  4302. fn1 = btf_name_by_offset(btf1, t1->name_off);
  4303. fn2 = btf_name_by_offset(btf2, t2->name_off);
  4304. if (btf_func_linkage(t1) != BTF_FUNC_GLOBAL) {
  4305. bpf_log(log, "%s() is not a global function\n", fn1);
  4306. return -EINVAL;
  4307. }
  4308. if (btf_func_linkage(t2) != BTF_FUNC_GLOBAL) {
  4309. bpf_log(log, "%s() is not a global function\n", fn2);
  4310. return -EINVAL;
  4311. }
  4312. t1 = btf_type_by_id(btf1, t1->type);
  4313. if (!t1 || !btf_type_is_func_proto(t1))
  4314. return -EFAULT;
  4315. t2 = btf_type_by_id(btf2, t2->type);
  4316. if (!t2 || !btf_type_is_func_proto(t2))
  4317. return -EFAULT;
  4318. args1 = (const struct btf_param *)(t1 + 1);
  4319. nargs1 = btf_type_vlen(t1);
  4320. args2 = (const struct btf_param *)(t2 + 1);
  4321. nargs2 = btf_type_vlen(t2);
  4322. if (nargs1 != nargs2) {
  4323. bpf_log(log, "%s() has %d args while %s() has %d args\n",
  4324. fn1, nargs1, fn2, nargs2);
  4325. return -EINVAL;
  4326. }
  4327. t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
  4328. t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
  4329. if (t1->info != t2->info) {
  4330. bpf_log(log,
  4331. "Return type %s of %s() doesn't match type %s of %s()\n",
  4332. btf_type_str(t1), fn1,
  4333. btf_type_str(t2), fn2);
  4334. return -EINVAL;
  4335. }
  4336. for (i = 0; i < nargs1; i++) {
  4337. t1 = btf_type_skip_modifiers(btf1, args1[i].type, NULL);
  4338. t2 = btf_type_skip_modifiers(btf2, args2[i].type, NULL);
  4339. if (t1->info != t2->info) {
  4340. bpf_log(log, "arg%d in %s() is %s while %s() has %s\n",
  4341. i, fn1, btf_type_str(t1),
  4342. fn2, btf_type_str(t2));
  4343. return -EINVAL;
  4344. }
  4345. if (btf_type_has_size(t1) && t1->size != t2->size) {
  4346. bpf_log(log,
  4347. "arg%d in %s() has size %d while %s() has %d\n",
  4348. i, fn1, t1->size,
  4349. fn2, t2->size);
  4350. return -EINVAL;
  4351. }
  4352. /* global functions are validated with scalars and pointers
  4353. * to context only. And only global functions can be replaced.
  4354. * Hence type check only those types.
  4355. */
  4356. if (btf_type_is_int(t1) || btf_type_is_enum(t1))
  4357. continue;
  4358. if (!btf_type_is_ptr(t1)) {
  4359. bpf_log(log,
  4360. "arg%d in %s() has unrecognized type\n",
  4361. i, fn1);
  4362. return -EINVAL;
  4363. }
  4364. t1 = btf_type_skip_modifiers(btf1, t1->type, NULL);
  4365. t2 = btf_type_skip_modifiers(btf2, t2->type, NULL);
  4366. if (!btf_type_is_struct(t1)) {
  4367. bpf_log(log,
  4368. "arg%d in %s() is not a pointer to context\n",
  4369. i, fn1);
  4370. return -EINVAL;
  4371. }
  4372. if (!btf_type_is_struct(t2)) {
  4373. bpf_log(log,
  4374. "arg%d in %s() is not a pointer to context\n",
  4375. i, fn2);
  4376. return -EINVAL;
  4377. }
  4378. /* This is an optional check to make program writing easier.
  4379. * Compare names of structs and report an error to the user.
  4380. * btf_prepare_func_args() already checked that t2 struct
  4381. * is a context type. btf_prepare_func_args() will check
  4382. * later that t1 struct is a context type as well.
  4383. */
  4384. s1 = btf_name_by_offset(btf1, t1->name_off);
  4385. s2 = btf_name_by_offset(btf2, t2->name_off);
  4386. if (strcmp(s1, s2)) {
  4387. bpf_log(log,
  4388. "arg%d %s(struct %s *) doesn't match %s(struct %s *)\n",
  4389. i, fn1, s1, fn2, s2);
  4390. return -EINVAL;
  4391. }
  4392. }
  4393. return 0;
  4394. }
  4395. /* Compare BTFs of given program with BTF of target program */
  4396. int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
  4397. struct btf *btf2, const struct btf_type *t2)
  4398. {
  4399. struct btf *btf1 = prog->aux->btf;
  4400. const struct btf_type *t1;
  4401. u32 btf_id = 0;
  4402. if (!prog->aux->func_info) {
  4403. bpf_log(log, "Program extension requires BTF\n");
  4404. return -EINVAL;
  4405. }
  4406. btf_id = prog->aux->func_info[0].type_id;
  4407. if (!btf_id)
  4408. return -EFAULT;
  4409. t1 = btf_type_by_id(btf1, btf_id);
  4410. if (!t1 || !btf_type_is_func(t1))
  4411. return -EFAULT;
  4412. return btf_check_func_type_match(log, btf1, t1, btf2, t2);
  4413. }
  4414. /* Compare BTF of a function with given bpf_reg_state.
  4415. * Returns:
  4416. * EFAULT - there is a verifier bug. Abort verification.
  4417. * EINVAL - there is a type mismatch or BTF is not available.
  4418. * 0 - BTF matches with what bpf_reg_state expects.
  4419. * Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
  4420. */
  4421. int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
  4422. struct bpf_reg_state *reg)
  4423. {
  4424. struct bpf_verifier_log *log = &env->log;
  4425. struct bpf_prog *prog = env->prog;
  4426. struct btf *btf = prog->aux->btf;
  4427. const struct btf_param *args;
  4428. const struct btf_type *t;
  4429. u32 i, nargs, btf_id;
  4430. const char *tname;
  4431. if (!prog->aux->func_info)
  4432. return -EINVAL;
  4433. btf_id = prog->aux->func_info[subprog].type_id;
  4434. if (!btf_id)
  4435. return -EFAULT;
  4436. if (prog->aux->func_info_aux[subprog].unreliable)
  4437. return -EINVAL;
  4438. t = btf_type_by_id(btf, btf_id);
  4439. if (!t || !btf_type_is_func(t)) {
  4440. /* These checks were already done by the verifier while loading
  4441. * struct bpf_func_info
  4442. */
  4443. bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
  4444. subprog);
  4445. return -EFAULT;
  4446. }
  4447. tname = btf_name_by_offset(btf, t->name_off);
  4448. t = btf_type_by_id(btf, t->type);
  4449. if (!t || !btf_type_is_func_proto(t)) {
  4450. bpf_log(log, "Invalid BTF of func %s\n", tname);
  4451. return -EFAULT;
  4452. }
  4453. args = (const struct btf_param *)(t + 1);
  4454. nargs = btf_type_vlen(t);
  4455. if (nargs > 5) {
  4456. bpf_log(log, "Function %s has %d > 5 args\n", tname, nargs);
  4457. goto out;
  4458. }
  4459. /* check that BTF function arguments match actual types that the
  4460. * verifier sees.
  4461. */
  4462. for (i = 0; i < nargs; i++) {
  4463. t = btf_type_by_id(btf, args[i].type);
  4464. while (btf_type_is_modifier(t))
  4465. t = btf_type_by_id(btf, t->type);
  4466. if (btf_type_is_int(t) || btf_type_is_enum(t)) {
  4467. if (reg[i + 1].type == SCALAR_VALUE)
  4468. continue;
  4469. bpf_log(log, "R%d is not a scalar\n", i + 1);
  4470. goto out;
  4471. }
  4472. if (btf_type_is_ptr(t)) {
  4473. if (reg[i + 1].type == SCALAR_VALUE) {
  4474. bpf_log(log, "R%d is not a pointer\n", i + 1);
  4475. goto out;
  4476. }
  4477. /* If function expects ctx type in BTF check that caller
  4478. * is passing PTR_TO_CTX.
  4479. */
  4480. if (btf_get_prog_ctx_type(log, btf, t, prog->type, i)) {
  4481. if (reg[i + 1].type != PTR_TO_CTX) {
  4482. bpf_log(log,
  4483. "arg#%d expected pointer to ctx, but got %s\n",
  4484. i, btf_kind_str[BTF_INFO_KIND(t->info)]);
  4485. goto out;
  4486. }
  4487. if (check_ctx_reg(env, &reg[i + 1], i + 1))
  4488. goto out;
  4489. continue;
  4490. }
  4491. }
  4492. bpf_log(log, "Unrecognized arg#%d type %s\n",
  4493. i, btf_kind_str[BTF_INFO_KIND(t->info)]);
  4494. goto out;
  4495. }
  4496. return 0;
  4497. out:
  4498. /* Compiler optimizations can remove arguments from static functions
  4499. * or mismatched type can be passed into a global function.
  4500. * In such cases mark the function as unreliable from BTF point of view.
  4501. */
  4502. prog->aux->func_info_aux[subprog].unreliable = true;
  4503. return -EINVAL;
  4504. }
  4505. /* Convert BTF of a function into bpf_reg_state if possible
  4506. * Returns:
  4507. * EFAULT - there is a verifier bug. Abort verification.
  4508. * EINVAL - cannot convert BTF.
  4509. * 0 - Successfully converted BTF into bpf_reg_state
  4510. * (either PTR_TO_CTX or SCALAR_VALUE).
  4511. */
  4512. int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
  4513. struct bpf_reg_state *reg)
  4514. {
  4515. struct bpf_verifier_log *log = &env->log;
  4516. struct bpf_prog *prog = env->prog;
  4517. enum bpf_prog_type prog_type = prog->type;
  4518. struct btf *btf = prog->aux->btf;
  4519. const struct btf_param *args;
  4520. const struct btf_type *t;
  4521. u32 i, nargs, btf_id;
  4522. const char *tname;
  4523. if (!prog->aux->func_info ||
  4524. prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
  4525. bpf_log(log, "Verifier bug\n");
  4526. return -EFAULT;
  4527. }
  4528. btf_id = prog->aux->func_info[subprog].type_id;
  4529. if (!btf_id) {
  4530. bpf_log(log, "Global functions need valid BTF\n");
  4531. return -EFAULT;
  4532. }
  4533. t = btf_type_by_id(btf, btf_id);
  4534. if (!t || !btf_type_is_func(t)) {
  4535. /* These checks were already done by the verifier while loading
  4536. * struct bpf_func_info
  4537. */
  4538. bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
  4539. subprog);
  4540. return -EFAULT;
  4541. }
  4542. tname = btf_name_by_offset(btf, t->name_off);
  4543. if (log->level & BPF_LOG_LEVEL)
  4544. bpf_log(log, "Validating %s() func#%d...\n",
  4545. tname, subprog);
  4546. if (prog->aux->func_info_aux[subprog].unreliable) {
  4547. bpf_log(log, "Verifier bug in function %s()\n", tname);
  4548. return -EFAULT;
  4549. }
  4550. if (prog_type == BPF_PROG_TYPE_EXT)
  4551. prog_type = prog->aux->dst_prog->type;
  4552. t = btf_type_by_id(btf, t->type);
  4553. if (!t || !btf_type_is_func_proto(t)) {
  4554. bpf_log(log, "Invalid type of function %s()\n", tname);
  4555. return -EFAULT;
  4556. }
  4557. args = (const struct btf_param *)(t + 1);
  4558. nargs = btf_type_vlen(t);
  4559. if (nargs > 5) {
  4560. bpf_log(log, "Global function %s() with %d > 5 args. Buggy compiler.\n",
  4561. tname, nargs);
  4562. return -EINVAL;
  4563. }
  4564. /* check that function returns int */
  4565. t = btf_type_by_id(btf, t->type);
  4566. while (btf_type_is_modifier(t))
  4567. t = btf_type_by_id(btf, t->type);
  4568. if (!btf_type_is_int(t) && !btf_type_is_enum(t)) {
  4569. bpf_log(log,
  4570. "Global function %s() doesn't return scalar. Only those are supported.\n",
  4571. tname);
  4572. return -EINVAL;
  4573. }
  4574. /* Convert BTF function arguments into verifier types.
  4575. * Only PTR_TO_CTX and SCALAR are supported atm.
  4576. */
  4577. for (i = 0; i < nargs; i++) {
  4578. t = btf_type_by_id(btf, args[i].type);
  4579. while (btf_type_is_modifier(t))
  4580. t = btf_type_by_id(btf, t->type);
  4581. if (btf_type_is_int(t) || btf_type_is_enum(t)) {
  4582. reg[i + 1].type = SCALAR_VALUE;
  4583. continue;
  4584. }
  4585. if (btf_type_is_ptr(t) &&
  4586. btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
  4587. reg[i + 1].type = PTR_TO_CTX;
  4588. continue;
  4589. }
  4590. bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
  4591. i, btf_kind_str[BTF_INFO_KIND(t->info)], tname);
  4592. return -EINVAL;
  4593. }
  4594. return 0;
  4595. }
  4596. static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
  4597. struct btf_show *show)
  4598. {
  4599. const struct btf_type *t = btf_type_by_id(btf, type_id);
  4600. show->btf = btf;
  4601. memset(&show->state, 0, sizeof(show->state));
  4602. memset(&show->obj, 0, sizeof(show->obj));
  4603. btf_type_ops(t)->show(btf, t, type_id, obj, 0, show);
  4604. }
  4605. static void btf_seq_show(struct btf_show *show, const char *fmt,
  4606. va_list args)
  4607. {
  4608. seq_vprintf((struct seq_file *)show->target, fmt, args);
  4609. }
  4610. int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
  4611. void *obj, struct seq_file *m, u64 flags)
  4612. {
  4613. struct btf_show sseq;
  4614. sseq.target = m;
  4615. sseq.showfn = btf_seq_show;
  4616. sseq.flags = flags;
  4617. btf_type_show(btf, type_id, obj, &sseq);
  4618. return sseq.state.status;
  4619. }
  4620. void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  4621. struct seq_file *m)
  4622. {
  4623. (void) btf_type_seq_show_flags(btf, type_id, obj, m,
  4624. BTF_SHOW_NONAME | BTF_SHOW_COMPACT |
  4625. BTF_SHOW_ZERO | BTF_SHOW_UNSAFE);
  4626. }
  4627. struct btf_show_snprintf {
  4628. struct btf_show show;
  4629. int len_left; /* space left in string */
  4630. int len; /* length we would have written */
  4631. };
  4632. static void btf_snprintf_show(struct btf_show *show, const char *fmt,
  4633. va_list args)
  4634. {
  4635. struct btf_show_snprintf *ssnprintf = (struct btf_show_snprintf *)show;
  4636. int len;
  4637. len = vsnprintf(show->target, ssnprintf->len_left, fmt, args);
  4638. if (len < 0) {
  4639. ssnprintf->len_left = 0;
  4640. ssnprintf->len = len;
  4641. } else if (len > ssnprintf->len_left) {
  4642. /* no space, drive on to get length we would have written */
  4643. ssnprintf->len_left = 0;
  4644. ssnprintf->len += len;
  4645. } else {
  4646. ssnprintf->len_left -= len;
  4647. ssnprintf->len += len;
  4648. show->target += len;
  4649. }
  4650. }
  4651. int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
  4652. char *buf, int len, u64 flags)
  4653. {
  4654. struct btf_show_snprintf ssnprintf;
  4655. ssnprintf.show.target = buf;
  4656. ssnprintf.show.flags = flags;
  4657. ssnprintf.show.showfn = btf_snprintf_show;
  4658. ssnprintf.len_left = len;
  4659. ssnprintf.len = 0;
  4660. btf_type_show(btf, type_id, obj, (struct btf_show *)&ssnprintf);
  4661. /* If we encontered an error, return it. */
  4662. if (ssnprintf.show.state.status)
  4663. return ssnprintf.show.state.status;
  4664. /* Otherwise return length we would have written */
  4665. return ssnprintf.len;
  4666. }
  4667. #ifdef CONFIG_PROC_FS
  4668. static void bpf_btf_show_fdinfo(struct seq_file *m, struct file *filp)
  4669. {
  4670. const struct btf *btf = filp->private_data;
  4671. seq_printf(m, "btf_id:\t%u\n", btf->id);
  4672. }
  4673. #endif
  4674. static int btf_release(struct inode *inode, struct file *filp)
  4675. {
  4676. btf_put(filp->private_data);
  4677. return 0;
  4678. }
  4679. const struct file_operations btf_fops = {
  4680. #ifdef CONFIG_PROC_FS
  4681. .show_fdinfo = bpf_btf_show_fdinfo,
  4682. #endif
  4683. .release = btf_release,
  4684. };
  4685. static int __btf_new_fd(struct btf *btf)
  4686. {
  4687. return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
  4688. }
  4689. int btf_new_fd(const union bpf_attr *attr)
  4690. {
  4691. struct btf *btf;
  4692. int ret;
  4693. btf = btf_parse(u64_to_user_ptr(attr->btf),
  4694. attr->btf_size, attr->btf_log_level,
  4695. u64_to_user_ptr(attr->btf_log_buf),
  4696. attr->btf_log_size);
  4697. if (IS_ERR(btf))
  4698. return PTR_ERR(btf);
  4699. ret = btf_alloc_id(btf);
  4700. if (ret) {
  4701. btf_free(btf);
  4702. return ret;
  4703. }
  4704. /*
  4705. * The BTF ID is published to the userspace.
  4706. * All BTF free must go through call_rcu() from
  4707. * now on (i.e. free by calling btf_put()).
  4708. */
  4709. ret = __btf_new_fd(btf);
  4710. if (ret < 0)
  4711. btf_put(btf);
  4712. return ret;
  4713. }
  4714. struct btf *btf_get_by_fd(int fd)
  4715. {
  4716. struct btf *btf;
  4717. struct fd f;
  4718. f = fdget(fd);
  4719. if (!f.file)
  4720. return ERR_PTR(-EBADF);
  4721. if (f.file->f_op != &btf_fops) {
  4722. fdput(f);
  4723. return ERR_PTR(-EINVAL);
  4724. }
  4725. btf = f.file->private_data;
  4726. refcount_inc(&btf->refcnt);
  4727. fdput(f);
  4728. return btf;
  4729. }
  4730. int btf_get_info_by_fd(const struct btf *btf,
  4731. const union bpf_attr *attr,
  4732. union bpf_attr __user *uattr)
  4733. {
  4734. struct bpf_btf_info __user *uinfo;
  4735. struct bpf_btf_info info;
  4736. u32 info_copy, btf_copy;
  4737. void __user *ubtf;
  4738. u32 uinfo_len;
  4739. uinfo = u64_to_user_ptr(attr->info.info);
  4740. uinfo_len = attr->info.info_len;
  4741. info_copy = min_t(u32, uinfo_len, sizeof(info));
  4742. memset(&info, 0, sizeof(info));
  4743. if (copy_from_user(&info, uinfo, info_copy))
  4744. return -EFAULT;
  4745. info.id = btf->id;
  4746. ubtf = u64_to_user_ptr(info.btf);
  4747. btf_copy = min_t(u32, btf->data_size, info.btf_size);
  4748. if (copy_to_user(ubtf, btf->data, btf_copy))
  4749. return -EFAULT;
  4750. info.btf_size = btf->data_size;
  4751. if (copy_to_user(uinfo, &info, info_copy) ||
  4752. put_user(info_copy, &uattr->info.info_len))
  4753. return -EFAULT;
  4754. return 0;
  4755. }
  4756. int btf_get_fd_by_id(u32 id)
  4757. {
  4758. struct btf *btf;
  4759. int fd;
  4760. rcu_read_lock();
  4761. btf = idr_find(&btf_idr, id);
  4762. if (!btf || !refcount_inc_not_zero(&btf->refcnt))
  4763. btf = ERR_PTR(-ENOENT);
  4764. rcu_read_unlock();
  4765. if (IS_ERR(btf))
  4766. return PTR_ERR(btf);
  4767. fd = __btf_new_fd(btf);
  4768. if (fd < 0)
  4769. btf_put(btf);
  4770. return fd;
  4771. }
  4772. u32 btf_id(const struct btf *btf)
  4773. {
  4774. return btf->id;
  4775. }
  4776. static int btf_id_cmp_func(const void *a, const void *b)
  4777. {
  4778. const int *pa = a, *pb = b;
  4779. return *pa - *pb;
  4780. }
  4781. bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
  4782. {
  4783. return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
  4784. }