test_verifier.c 374 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532
  1. /*
  2. * Testsuite for eBPF verifier
  3. *
  4. * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
  5. * Copyright (c) 2017 Facebook
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of version 2 of the GNU General Public
  9. * License as published by the Free Software Foundation.
  10. */
  11. #include <endian.h>
  12. #include <asm/types.h>
  13. #include <linux/types.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <stddef.h>
  21. #include <stdbool.h>
  22. #include <sched.h>
  23. #include <limits.h>
  24. #include <sys/capability.h>
  25. #include <linux/unistd.h>
  26. #include <linux/filter.h>
  27. #include <linux/bpf_perf_event.h>
  28. #include <linux/bpf.h>
  29. #include <linux/if_ether.h>
  30. #include <bpf/bpf.h>
  31. #ifdef HAVE_GENHDR
  32. # include "autoconf.h"
  33. #else
  34. # if defined(__i386) || defined(__x86_64) || defined(__s390x__) || defined(__aarch64__)
  35. # define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1
  36. # endif
  37. #endif
  38. #include "bpf_rlimit.h"
  39. #include "bpf_rand.h"
  40. #include "../../../include/linux/filter.h"
  41. #ifndef ARRAY_SIZE
  42. # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  43. #endif
  44. #define MAX_INSNS BPF_MAXINSNS
  45. #define MAX_FIXUPS 8
  46. #define MAX_NR_MAPS 4
  47. #define POINTER_VALUE 0xcafe4all
  48. #define TEST_DATA_LEN 64
  49. #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0)
  50. #define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1)
  51. #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"
  52. static bool unpriv_disabled = false;
  53. struct bpf_test {
  54. const char *descr;
  55. struct bpf_insn insns[MAX_INSNS];
  56. int fixup_map1[MAX_FIXUPS];
  57. int fixup_map2[MAX_FIXUPS];
  58. int fixup_map3[MAX_FIXUPS];
  59. int fixup_prog[MAX_FIXUPS];
  60. int fixup_map_in_map[MAX_FIXUPS];
  61. const char *errstr;
  62. const char *errstr_unpriv;
  63. uint32_t retval;
  64. enum {
  65. UNDEF,
  66. ACCEPT,
  67. REJECT
  68. } result, result_unpriv;
  69. enum bpf_prog_type prog_type;
  70. uint8_t flags;
  71. __u8 data[TEST_DATA_LEN];
  72. void (*fill_helper)(struct bpf_test *self);
  73. };
  74. /* Note we want this to be 64 bit aligned so that the end of our array is
  75. * actually the end of the structure.
  76. */
  77. #define MAX_ENTRIES 11
  78. struct test_val {
  79. unsigned int index;
  80. int foo[MAX_ENTRIES];
  81. };
  82. struct other_val {
  83. long long foo;
  84. long long bar;
  85. };
  86. static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self)
  87. {
  88. /* test: {skb->data[0], vlan_push} x 68 + {skb->data[0], vlan_pop} x 68 */
  89. #define PUSH_CNT 51
  90. unsigned int len = BPF_MAXINSNS;
  91. struct bpf_insn *insn = self->insns;
  92. int i = 0, j, k = 0;
  93. insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  94. loop:
  95. for (j = 0; j < PUSH_CNT; j++) {
  96. insn[i++] = BPF_LD_ABS(BPF_B, 0);
  97. insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 2);
  98. i++;
  99. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
  100. insn[i++] = BPF_MOV64_IMM(BPF_REG_2, 1);
  101. insn[i++] = BPF_MOV64_IMM(BPF_REG_3, 2);
  102. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  103. BPF_FUNC_skb_vlan_push),
  104. insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 2);
  105. i++;
  106. }
  107. for (j = 0; j < PUSH_CNT; j++) {
  108. insn[i++] = BPF_LD_ABS(BPF_B, 0);
  109. insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 2);
  110. i++;
  111. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
  112. insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  113. BPF_FUNC_skb_vlan_pop),
  114. insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 2);
  115. i++;
  116. }
  117. if (++k < 5)
  118. goto loop;
  119. for (; i < len - 1; i++)
  120. insn[i] = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, 0xbef);
  121. insn[len - 1] = BPF_EXIT_INSN();
  122. }
  123. static void bpf_fill_jump_around_ld_abs(struct bpf_test *self)
  124. {
  125. struct bpf_insn *insn = self->insns;
  126. unsigned int len = BPF_MAXINSNS;
  127. int i = 0;
  128. insn[i++] = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  129. insn[i++] = BPF_LD_ABS(BPF_B, 0);
  130. insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2);
  131. i++;
  132. while (i < len - 1)
  133. insn[i++] = BPF_LD_ABS(BPF_B, 1);
  134. insn[i] = BPF_EXIT_INSN();
  135. }
  136. static void bpf_fill_rand_ld_dw(struct bpf_test *self)
  137. {
  138. struct bpf_insn *insn = self->insns;
  139. uint64_t res = 0;
  140. int i = 0;
  141. insn[i++] = BPF_MOV32_IMM(BPF_REG_0, 0);
  142. while (i < self->retval) {
  143. uint64_t val = bpf_semi_rand_get();
  144. struct bpf_insn tmp[2] = { BPF_LD_IMM64(BPF_REG_1, val) };
  145. res ^= val;
  146. insn[i++] = tmp[0];
  147. insn[i++] = tmp[1];
  148. insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
  149. }
  150. insn[i++] = BPF_MOV64_REG(BPF_REG_1, BPF_REG_0);
  151. insn[i++] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32);
  152. insn[i++] = BPF_ALU64_REG(BPF_XOR, BPF_REG_0, BPF_REG_1);
  153. insn[i] = BPF_EXIT_INSN();
  154. res ^= (res >> 32);
  155. self->retval = (uint32_t)res;
  156. }
  157. static struct bpf_test tests[] = {
  158. {
  159. "add+sub+mul",
  160. .insns = {
  161. BPF_MOV64_IMM(BPF_REG_1, 1),
  162. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 2),
  163. BPF_MOV64_IMM(BPF_REG_2, 3),
  164. BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_2),
  165. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -1),
  166. BPF_ALU64_IMM(BPF_MUL, BPF_REG_1, 3),
  167. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  168. BPF_EXIT_INSN(),
  169. },
  170. .result = ACCEPT,
  171. .retval = -3,
  172. },
  173. {
  174. "DIV32 by 0, zero check 1",
  175. .insns = {
  176. BPF_MOV32_IMM(BPF_REG_0, 42),
  177. BPF_MOV32_IMM(BPF_REG_1, 0),
  178. BPF_MOV32_IMM(BPF_REG_2, 1),
  179. BPF_ALU32_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
  180. BPF_EXIT_INSN(),
  181. },
  182. .result = ACCEPT,
  183. .retval = 42,
  184. },
  185. {
  186. "DIV32 by 0, zero check 2",
  187. .insns = {
  188. BPF_MOV32_IMM(BPF_REG_0, 42),
  189. BPF_LD_IMM64(BPF_REG_1, 0xffffffff00000000LL),
  190. BPF_MOV32_IMM(BPF_REG_2, 1),
  191. BPF_ALU32_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
  192. BPF_EXIT_INSN(),
  193. },
  194. .result = ACCEPT,
  195. .retval = 42,
  196. },
  197. {
  198. "DIV64 by 0, zero check",
  199. .insns = {
  200. BPF_MOV32_IMM(BPF_REG_0, 42),
  201. BPF_MOV32_IMM(BPF_REG_1, 0),
  202. BPF_MOV32_IMM(BPF_REG_2, 1),
  203. BPF_ALU64_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
  204. BPF_EXIT_INSN(),
  205. },
  206. .result = ACCEPT,
  207. .retval = 42,
  208. },
  209. {
  210. "MOD32 by 0, zero check 1",
  211. .insns = {
  212. BPF_MOV32_IMM(BPF_REG_0, 42),
  213. BPF_MOV32_IMM(BPF_REG_1, 0),
  214. BPF_MOV32_IMM(BPF_REG_2, 1),
  215. BPF_ALU32_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
  216. BPF_EXIT_INSN(),
  217. },
  218. .result = ACCEPT,
  219. .retval = 42,
  220. },
  221. {
  222. "MOD32 by 0, zero check 2",
  223. .insns = {
  224. BPF_MOV32_IMM(BPF_REG_0, 42),
  225. BPF_LD_IMM64(BPF_REG_1, 0xffffffff00000000LL),
  226. BPF_MOV32_IMM(BPF_REG_2, 1),
  227. BPF_ALU32_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
  228. BPF_EXIT_INSN(),
  229. },
  230. .result = ACCEPT,
  231. .retval = 42,
  232. },
  233. {
  234. "MOD64 by 0, zero check",
  235. .insns = {
  236. BPF_MOV32_IMM(BPF_REG_0, 42),
  237. BPF_MOV32_IMM(BPF_REG_1, 0),
  238. BPF_MOV32_IMM(BPF_REG_2, 1),
  239. BPF_ALU64_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
  240. BPF_EXIT_INSN(),
  241. },
  242. .result = ACCEPT,
  243. .retval = 42,
  244. },
  245. {
  246. "DIV32 by 0, zero check ok, cls",
  247. .insns = {
  248. BPF_MOV32_IMM(BPF_REG_0, 42),
  249. BPF_MOV32_IMM(BPF_REG_1, 2),
  250. BPF_MOV32_IMM(BPF_REG_2, 16),
  251. BPF_ALU32_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
  252. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  253. BPF_EXIT_INSN(),
  254. },
  255. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  256. .result = ACCEPT,
  257. .retval = 8,
  258. },
  259. {
  260. "DIV32 by 0, zero check 1, cls",
  261. .insns = {
  262. BPF_MOV32_IMM(BPF_REG_1, 0),
  263. BPF_MOV32_IMM(BPF_REG_0, 1),
  264. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
  265. BPF_EXIT_INSN(),
  266. },
  267. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  268. .result = ACCEPT,
  269. .retval = 0,
  270. },
  271. {
  272. "DIV32 by 0, zero check 2, cls",
  273. .insns = {
  274. BPF_LD_IMM64(BPF_REG_1, 0xffffffff00000000LL),
  275. BPF_MOV32_IMM(BPF_REG_0, 1),
  276. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
  277. BPF_EXIT_INSN(),
  278. },
  279. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  280. .result = ACCEPT,
  281. .retval = 0,
  282. },
  283. {
  284. "DIV64 by 0, zero check, cls",
  285. .insns = {
  286. BPF_MOV32_IMM(BPF_REG_1, 0),
  287. BPF_MOV32_IMM(BPF_REG_0, 1),
  288. BPF_ALU64_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
  289. BPF_EXIT_INSN(),
  290. },
  291. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  292. .result = ACCEPT,
  293. .retval = 0,
  294. },
  295. {
  296. "MOD32 by 0, zero check ok, cls",
  297. .insns = {
  298. BPF_MOV32_IMM(BPF_REG_0, 42),
  299. BPF_MOV32_IMM(BPF_REG_1, 3),
  300. BPF_MOV32_IMM(BPF_REG_2, 5),
  301. BPF_ALU32_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
  302. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  303. BPF_EXIT_INSN(),
  304. },
  305. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  306. .result = ACCEPT,
  307. .retval = 2,
  308. },
  309. {
  310. "MOD32 by 0, zero check 1, cls",
  311. .insns = {
  312. BPF_MOV32_IMM(BPF_REG_1, 0),
  313. BPF_MOV32_IMM(BPF_REG_0, 1),
  314. BPF_ALU32_REG(BPF_MOD, BPF_REG_0, BPF_REG_1),
  315. BPF_EXIT_INSN(),
  316. },
  317. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  318. .result = ACCEPT,
  319. .retval = 1,
  320. },
  321. {
  322. "MOD32 by 0, zero check 2, cls",
  323. .insns = {
  324. BPF_LD_IMM64(BPF_REG_1, 0xffffffff00000000LL),
  325. BPF_MOV32_IMM(BPF_REG_0, 1),
  326. BPF_ALU32_REG(BPF_MOD, BPF_REG_0, BPF_REG_1),
  327. BPF_EXIT_INSN(),
  328. },
  329. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  330. .result = ACCEPT,
  331. .retval = 1,
  332. },
  333. {
  334. "MOD64 by 0, zero check 1, cls",
  335. .insns = {
  336. BPF_MOV32_IMM(BPF_REG_1, 0),
  337. BPF_MOV32_IMM(BPF_REG_0, 2),
  338. BPF_ALU64_REG(BPF_MOD, BPF_REG_0, BPF_REG_1),
  339. BPF_EXIT_INSN(),
  340. },
  341. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  342. .result = ACCEPT,
  343. .retval = 2,
  344. },
  345. {
  346. "MOD64 by 0, zero check 2, cls",
  347. .insns = {
  348. BPF_MOV32_IMM(BPF_REG_1, 0),
  349. BPF_MOV32_IMM(BPF_REG_0, -1),
  350. BPF_ALU64_REG(BPF_MOD, BPF_REG_0, BPF_REG_1),
  351. BPF_EXIT_INSN(),
  352. },
  353. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  354. .result = ACCEPT,
  355. .retval = -1,
  356. },
  357. /* Just make sure that JITs used udiv/umod as otherwise we get
  358. * an exception from INT_MIN/-1 overflow similarly as with div
  359. * by zero.
  360. */
  361. {
  362. "DIV32 overflow, check 1",
  363. .insns = {
  364. BPF_MOV32_IMM(BPF_REG_1, -1),
  365. BPF_MOV32_IMM(BPF_REG_0, INT_MIN),
  366. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
  367. BPF_EXIT_INSN(),
  368. },
  369. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  370. .result = ACCEPT,
  371. .retval = 0,
  372. },
  373. {
  374. "DIV32 overflow, check 2",
  375. .insns = {
  376. BPF_MOV32_IMM(BPF_REG_0, INT_MIN),
  377. BPF_ALU32_IMM(BPF_DIV, BPF_REG_0, -1),
  378. BPF_EXIT_INSN(),
  379. },
  380. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  381. .result = ACCEPT,
  382. .retval = 0,
  383. },
  384. {
  385. "DIV64 overflow, check 1",
  386. .insns = {
  387. BPF_MOV64_IMM(BPF_REG_1, -1),
  388. BPF_LD_IMM64(BPF_REG_0, LLONG_MIN),
  389. BPF_ALU64_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
  390. BPF_EXIT_INSN(),
  391. },
  392. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  393. .result = ACCEPT,
  394. .retval = 0,
  395. },
  396. {
  397. "DIV64 overflow, check 2",
  398. .insns = {
  399. BPF_LD_IMM64(BPF_REG_0, LLONG_MIN),
  400. BPF_ALU64_IMM(BPF_DIV, BPF_REG_0, -1),
  401. BPF_EXIT_INSN(),
  402. },
  403. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  404. .result = ACCEPT,
  405. .retval = 0,
  406. },
  407. {
  408. "MOD32 overflow, check 1",
  409. .insns = {
  410. BPF_MOV32_IMM(BPF_REG_1, -1),
  411. BPF_MOV32_IMM(BPF_REG_0, INT_MIN),
  412. BPF_ALU32_REG(BPF_MOD, BPF_REG_0, BPF_REG_1),
  413. BPF_EXIT_INSN(),
  414. },
  415. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  416. .result = ACCEPT,
  417. .retval = INT_MIN,
  418. },
  419. {
  420. "MOD32 overflow, check 2",
  421. .insns = {
  422. BPF_MOV32_IMM(BPF_REG_0, INT_MIN),
  423. BPF_ALU32_IMM(BPF_MOD, BPF_REG_0, -1),
  424. BPF_EXIT_INSN(),
  425. },
  426. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  427. .result = ACCEPT,
  428. .retval = INT_MIN,
  429. },
  430. {
  431. "MOD64 overflow, check 1",
  432. .insns = {
  433. BPF_MOV64_IMM(BPF_REG_1, -1),
  434. BPF_LD_IMM64(BPF_REG_2, LLONG_MIN),
  435. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  436. BPF_ALU64_REG(BPF_MOD, BPF_REG_2, BPF_REG_1),
  437. BPF_MOV32_IMM(BPF_REG_0, 0),
  438. BPF_JMP_REG(BPF_JNE, BPF_REG_3, BPF_REG_2, 1),
  439. BPF_MOV32_IMM(BPF_REG_0, 1),
  440. BPF_EXIT_INSN(),
  441. },
  442. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  443. .result = ACCEPT,
  444. .retval = 1,
  445. },
  446. {
  447. "MOD64 overflow, check 2",
  448. .insns = {
  449. BPF_LD_IMM64(BPF_REG_2, LLONG_MIN),
  450. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  451. BPF_ALU64_IMM(BPF_MOD, BPF_REG_2, -1),
  452. BPF_MOV32_IMM(BPF_REG_0, 0),
  453. BPF_JMP_REG(BPF_JNE, BPF_REG_3, BPF_REG_2, 1),
  454. BPF_MOV32_IMM(BPF_REG_0, 1),
  455. BPF_EXIT_INSN(),
  456. },
  457. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  458. .result = ACCEPT,
  459. .retval = 1,
  460. },
  461. {
  462. "xor32 zero extend check",
  463. .insns = {
  464. BPF_MOV32_IMM(BPF_REG_2, -1),
  465. BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 32),
  466. BPF_ALU64_IMM(BPF_OR, BPF_REG_2, 0xffff),
  467. BPF_ALU32_REG(BPF_XOR, BPF_REG_2, BPF_REG_2),
  468. BPF_MOV32_IMM(BPF_REG_0, 2),
  469. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 0, 1),
  470. BPF_MOV32_IMM(BPF_REG_0, 1),
  471. BPF_EXIT_INSN(),
  472. },
  473. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  474. .result = ACCEPT,
  475. .retval = 1,
  476. },
  477. {
  478. "empty prog",
  479. .insns = {
  480. },
  481. .errstr = "unknown opcode 00",
  482. .result = REJECT,
  483. },
  484. {
  485. "only exit insn",
  486. .insns = {
  487. BPF_EXIT_INSN(),
  488. },
  489. .errstr = "R0 !read_ok",
  490. .result = REJECT,
  491. },
  492. {
  493. "unreachable",
  494. .insns = {
  495. BPF_EXIT_INSN(),
  496. BPF_EXIT_INSN(),
  497. },
  498. .errstr = "unreachable",
  499. .result = REJECT,
  500. },
  501. {
  502. "unreachable2",
  503. .insns = {
  504. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  505. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  506. BPF_EXIT_INSN(),
  507. },
  508. .errstr = "unreachable",
  509. .result = REJECT,
  510. },
  511. {
  512. "out of range jump",
  513. .insns = {
  514. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  515. BPF_EXIT_INSN(),
  516. },
  517. .errstr = "jump out of range",
  518. .result = REJECT,
  519. },
  520. {
  521. "out of range jump2",
  522. .insns = {
  523. BPF_JMP_IMM(BPF_JA, 0, 0, -2),
  524. BPF_EXIT_INSN(),
  525. },
  526. .errstr = "jump out of range",
  527. .result = REJECT,
  528. },
  529. {
  530. "test1 ld_imm64",
  531. .insns = {
  532. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
  533. BPF_LD_IMM64(BPF_REG_0, 0),
  534. BPF_LD_IMM64(BPF_REG_0, 0),
  535. BPF_LD_IMM64(BPF_REG_0, 1),
  536. BPF_LD_IMM64(BPF_REG_0, 1),
  537. BPF_MOV64_IMM(BPF_REG_0, 2),
  538. BPF_EXIT_INSN(),
  539. },
  540. .errstr = "invalid BPF_LD_IMM insn",
  541. .errstr_unpriv = "R1 pointer comparison",
  542. .result = REJECT,
  543. },
  544. {
  545. "test2 ld_imm64",
  546. .insns = {
  547. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
  548. BPF_LD_IMM64(BPF_REG_0, 0),
  549. BPF_LD_IMM64(BPF_REG_0, 0),
  550. BPF_LD_IMM64(BPF_REG_0, 1),
  551. BPF_LD_IMM64(BPF_REG_0, 1),
  552. BPF_EXIT_INSN(),
  553. },
  554. .errstr = "invalid BPF_LD_IMM insn",
  555. .errstr_unpriv = "R1 pointer comparison",
  556. .result = REJECT,
  557. },
  558. {
  559. "test3 ld_imm64",
  560. .insns = {
  561. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
  562. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 0),
  563. BPF_LD_IMM64(BPF_REG_0, 0),
  564. BPF_LD_IMM64(BPF_REG_0, 0),
  565. BPF_LD_IMM64(BPF_REG_0, 1),
  566. BPF_LD_IMM64(BPF_REG_0, 1),
  567. BPF_EXIT_INSN(),
  568. },
  569. .errstr = "invalid bpf_ld_imm64 insn",
  570. .result = REJECT,
  571. },
  572. {
  573. "test4 ld_imm64",
  574. .insns = {
  575. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 0),
  576. BPF_EXIT_INSN(),
  577. },
  578. .errstr = "invalid bpf_ld_imm64 insn",
  579. .result = REJECT,
  580. },
  581. {
  582. "test5 ld_imm64",
  583. .insns = {
  584. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 0),
  585. },
  586. .errstr = "invalid bpf_ld_imm64 insn",
  587. .result = REJECT,
  588. },
  589. {
  590. "test6 ld_imm64",
  591. .insns = {
  592. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 0),
  593. BPF_RAW_INSN(0, 0, 0, 0, 0),
  594. BPF_EXIT_INSN(),
  595. },
  596. .result = ACCEPT,
  597. },
  598. {
  599. "test7 ld_imm64",
  600. .insns = {
  601. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 1),
  602. BPF_RAW_INSN(0, 0, 0, 0, 1),
  603. BPF_EXIT_INSN(),
  604. },
  605. .result = ACCEPT,
  606. .retval = 1,
  607. },
  608. {
  609. "test8 ld_imm64",
  610. .insns = {
  611. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 1, 1),
  612. BPF_RAW_INSN(0, 0, 0, 0, 1),
  613. BPF_EXIT_INSN(),
  614. },
  615. .errstr = "uses reserved fields",
  616. .result = REJECT,
  617. },
  618. {
  619. "test9 ld_imm64",
  620. .insns = {
  621. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 1),
  622. BPF_RAW_INSN(0, 0, 0, 1, 1),
  623. BPF_EXIT_INSN(),
  624. },
  625. .errstr = "invalid bpf_ld_imm64 insn",
  626. .result = REJECT,
  627. },
  628. {
  629. "test10 ld_imm64",
  630. .insns = {
  631. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 1),
  632. BPF_RAW_INSN(0, BPF_REG_1, 0, 0, 1),
  633. BPF_EXIT_INSN(),
  634. },
  635. .errstr = "invalid bpf_ld_imm64 insn",
  636. .result = REJECT,
  637. },
  638. {
  639. "test11 ld_imm64",
  640. .insns = {
  641. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, 0, 0, 1),
  642. BPF_RAW_INSN(0, 0, BPF_REG_1, 0, 1),
  643. BPF_EXIT_INSN(),
  644. },
  645. .errstr = "invalid bpf_ld_imm64 insn",
  646. .result = REJECT,
  647. },
  648. {
  649. "test12 ld_imm64",
  650. .insns = {
  651. BPF_MOV64_IMM(BPF_REG_1, 0),
  652. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, BPF_REG_1, 0, 1),
  653. BPF_RAW_INSN(0, 0, 0, 0, 1),
  654. BPF_EXIT_INSN(),
  655. },
  656. .errstr = "not pointing to valid bpf_map",
  657. .result = REJECT,
  658. },
  659. {
  660. "test13 ld_imm64",
  661. .insns = {
  662. BPF_MOV64_IMM(BPF_REG_1, 0),
  663. BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, 0, BPF_REG_1, 0, 1),
  664. BPF_RAW_INSN(0, 0, BPF_REG_1, 0, 1),
  665. BPF_EXIT_INSN(),
  666. },
  667. .errstr = "invalid bpf_ld_imm64 insn",
  668. .result = REJECT,
  669. },
  670. {
  671. "arsh32 on imm",
  672. .insns = {
  673. BPF_MOV64_IMM(BPF_REG_0, 1),
  674. BPF_ALU32_IMM(BPF_ARSH, BPF_REG_0, 5),
  675. BPF_EXIT_INSN(),
  676. },
  677. .result = REJECT,
  678. .errstr = "unknown opcode c4",
  679. },
  680. {
  681. "arsh32 on reg",
  682. .insns = {
  683. BPF_MOV64_IMM(BPF_REG_0, 1),
  684. BPF_MOV64_IMM(BPF_REG_1, 5),
  685. BPF_ALU32_REG(BPF_ARSH, BPF_REG_0, BPF_REG_1),
  686. BPF_EXIT_INSN(),
  687. },
  688. .result = REJECT,
  689. .errstr = "unknown opcode cc",
  690. },
  691. {
  692. "arsh64 on imm",
  693. .insns = {
  694. BPF_MOV64_IMM(BPF_REG_0, 1),
  695. BPF_ALU64_IMM(BPF_ARSH, BPF_REG_0, 5),
  696. BPF_EXIT_INSN(),
  697. },
  698. .result = ACCEPT,
  699. },
  700. {
  701. "arsh64 on reg",
  702. .insns = {
  703. BPF_MOV64_IMM(BPF_REG_0, 1),
  704. BPF_MOV64_IMM(BPF_REG_1, 5),
  705. BPF_ALU64_REG(BPF_ARSH, BPF_REG_0, BPF_REG_1),
  706. BPF_EXIT_INSN(),
  707. },
  708. .result = ACCEPT,
  709. },
  710. {
  711. "no bpf_exit",
  712. .insns = {
  713. BPF_ALU64_REG(BPF_MOV, BPF_REG_0, BPF_REG_2),
  714. },
  715. .errstr = "not an exit",
  716. .result = REJECT,
  717. },
  718. {
  719. "loop (back-edge)",
  720. .insns = {
  721. BPF_JMP_IMM(BPF_JA, 0, 0, -1),
  722. BPF_EXIT_INSN(),
  723. },
  724. .errstr = "back-edge",
  725. .result = REJECT,
  726. },
  727. {
  728. "loop2 (back-edge)",
  729. .insns = {
  730. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  731. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  732. BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
  733. BPF_JMP_IMM(BPF_JA, 0, 0, -4),
  734. BPF_EXIT_INSN(),
  735. },
  736. .errstr = "back-edge",
  737. .result = REJECT,
  738. },
  739. {
  740. "conditional loop",
  741. .insns = {
  742. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  743. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  744. BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
  745. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, -3),
  746. BPF_EXIT_INSN(),
  747. },
  748. .errstr = "back-edge",
  749. .result = REJECT,
  750. },
  751. {
  752. "read uninitialized register",
  753. .insns = {
  754. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  755. BPF_EXIT_INSN(),
  756. },
  757. .errstr = "R2 !read_ok",
  758. .result = REJECT,
  759. },
  760. {
  761. "read invalid register",
  762. .insns = {
  763. BPF_MOV64_REG(BPF_REG_0, -1),
  764. BPF_EXIT_INSN(),
  765. },
  766. .errstr = "R15 is invalid",
  767. .result = REJECT,
  768. },
  769. {
  770. "program doesn't init R0 before exit",
  771. .insns = {
  772. BPF_ALU64_REG(BPF_MOV, BPF_REG_2, BPF_REG_1),
  773. BPF_EXIT_INSN(),
  774. },
  775. .errstr = "R0 !read_ok",
  776. .result = REJECT,
  777. },
  778. {
  779. "program doesn't init R0 before exit in all branches",
  780. .insns = {
  781. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  782. BPF_MOV64_IMM(BPF_REG_0, 1),
  783. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2),
  784. BPF_EXIT_INSN(),
  785. },
  786. .errstr = "R0 !read_ok",
  787. .errstr_unpriv = "R1 pointer comparison",
  788. .result = REJECT,
  789. },
  790. {
  791. "stack out of bounds",
  792. .insns = {
  793. BPF_ST_MEM(BPF_DW, BPF_REG_10, 8, 0),
  794. BPF_EXIT_INSN(),
  795. },
  796. .errstr = "invalid stack",
  797. .result = REJECT,
  798. },
  799. {
  800. "invalid call insn1",
  801. .insns = {
  802. BPF_RAW_INSN(BPF_JMP | BPF_CALL | BPF_X, 0, 0, 0, 0),
  803. BPF_EXIT_INSN(),
  804. },
  805. .errstr = "unknown opcode 8d",
  806. .result = REJECT,
  807. },
  808. {
  809. "invalid call insn2",
  810. .insns = {
  811. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 1, 0),
  812. BPF_EXIT_INSN(),
  813. },
  814. .errstr = "BPF_CALL uses reserved",
  815. .result = REJECT,
  816. },
  817. {
  818. "invalid function call",
  819. .insns = {
  820. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, 1234567),
  821. BPF_EXIT_INSN(),
  822. },
  823. .errstr = "invalid func unknown#1234567",
  824. .result = REJECT,
  825. },
  826. {
  827. "uninitialized stack1",
  828. .insns = {
  829. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  830. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  831. BPF_LD_MAP_FD(BPF_REG_1, 0),
  832. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  833. BPF_FUNC_map_lookup_elem),
  834. BPF_EXIT_INSN(),
  835. },
  836. .fixup_map1 = { 2 },
  837. .errstr = "invalid indirect read from stack",
  838. .result = REJECT,
  839. },
  840. {
  841. "uninitialized stack2",
  842. .insns = {
  843. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  844. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, -8),
  845. BPF_EXIT_INSN(),
  846. },
  847. .errstr = "invalid read from stack",
  848. .result = REJECT,
  849. },
  850. {
  851. "invalid fp arithmetic",
  852. /* If this gets ever changed, make sure JITs can deal with it. */
  853. .insns = {
  854. BPF_MOV64_IMM(BPF_REG_0, 0),
  855. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  856. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 8),
  857. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
  858. BPF_EXIT_INSN(),
  859. },
  860. .errstr = "R1 subtraction from stack pointer",
  861. .result = REJECT,
  862. },
  863. {
  864. "non-invalid fp arithmetic",
  865. .insns = {
  866. BPF_MOV64_IMM(BPF_REG_0, 0),
  867. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  868. BPF_EXIT_INSN(),
  869. },
  870. .result = ACCEPT,
  871. },
  872. {
  873. "invalid argument register",
  874. .insns = {
  875. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  876. BPF_FUNC_get_cgroup_classid),
  877. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  878. BPF_FUNC_get_cgroup_classid),
  879. BPF_EXIT_INSN(),
  880. },
  881. .errstr = "R1 !read_ok",
  882. .result = REJECT,
  883. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  884. },
  885. {
  886. "non-invalid argument register",
  887. .insns = {
  888. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
  889. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  890. BPF_FUNC_get_cgroup_classid),
  891. BPF_ALU64_REG(BPF_MOV, BPF_REG_1, BPF_REG_6),
  892. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  893. BPF_FUNC_get_cgroup_classid),
  894. BPF_EXIT_INSN(),
  895. },
  896. .result = ACCEPT,
  897. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  898. },
  899. {
  900. "check valid spill/fill",
  901. .insns = {
  902. /* spill R1(ctx) into stack */
  903. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  904. /* fill it back into R2 */
  905. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -8),
  906. /* should be able to access R0 = *(R2 + 8) */
  907. /* BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 8), */
  908. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  909. BPF_EXIT_INSN(),
  910. },
  911. .errstr_unpriv = "R0 leaks addr",
  912. .result = ACCEPT,
  913. .result_unpriv = REJECT,
  914. .retval = POINTER_VALUE,
  915. },
  916. {
  917. "check valid spill/fill, skb mark",
  918. .insns = {
  919. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
  920. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, -8),
  921. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  922. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0,
  923. offsetof(struct __sk_buff, mark)),
  924. BPF_EXIT_INSN(),
  925. },
  926. .result = ACCEPT,
  927. .result_unpriv = ACCEPT,
  928. },
  929. {
  930. "check corrupted spill/fill",
  931. .insns = {
  932. /* spill R1(ctx) into stack */
  933. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  934. /* mess up with R1 pointer on stack */
  935. BPF_ST_MEM(BPF_B, BPF_REG_10, -7, 0x23),
  936. /* fill back into R0 should fail */
  937. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  938. BPF_EXIT_INSN(),
  939. },
  940. .errstr_unpriv = "attempt to corrupt spilled",
  941. .errstr = "corrupted spill",
  942. .result = REJECT,
  943. },
  944. {
  945. "invalid src register in STX",
  946. .insns = {
  947. BPF_STX_MEM(BPF_B, BPF_REG_10, -1, -1),
  948. BPF_EXIT_INSN(),
  949. },
  950. .errstr = "R15 is invalid",
  951. .result = REJECT,
  952. },
  953. {
  954. "invalid dst register in STX",
  955. .insns = {
  956. BPF_STX_MEM(BPF_B, 14, BPF_REG_10, -1),
  957. BPF_EXIT_INSN(),
  958. },
  959. .errstr = "R14 is invalid",
  960. .result = REJECT,
  961. },
  962. {
  963. "invalid dst register in ST",
  964. .insns = {
  965. BPF_ST_MEM(BPF_B, 14, -1, -1),
  966. BPF_EXIT_INSN(),
  967. },
  968. .errstr = "R14 is invalid",
  969. .result = REJECT,
  970. },
  971. {
  972. "invalid src register in LDX",
  973. .insns = {
  974. BPF_LDX_MEM(BPF_B, BPF_REG_0, 12, 0),
  975. BPF_EXIT_INSN(),
  976. },
  977. .errstr = "R12 is invalid",
  978. .result = REJECT,
  979. },
  980. {
  981. "invalid dst register in LDX",
  982. .insns = {
  983. BPF_LDX_MEM(BPF_B, 11, BPF_REG_1, 0),
  984. BPF_EXIT_INSN(),
  985. },
  986. .errstr = "R11 is invalid",
  987. .result = REJECT,
  988. },
  989. {
  990. "junk insn",
  991. .insns = {
  992. BPF_RAW_INSN(0, 0, 0, 0, 0),
  993. BPF_EXIT_INSN(),
  994. },
  995. .errstr = "unknown opcode 00",
  996. .result = REJECT,
  997. },
  998. {
  999. "junk insn2",
  1000. .insns = {
  1001. BPF_RAW_INSN(1, 0, 0, 0, 0),
  1002. BPF_EXIT_INSN(),
  1003. },
  1004. .errstr = "BPF_LDX uses reserved fields",
  1005. .result = REJECT,
  1006. },
  1007. {
  1008. "junk insn3",
  1009. .insns = {
  1010. BPF_RAW_INSN(-1, 0, 0, 0, 0),
  1011. BPF_EXIT_INSN(),
  1012. },
  1013. .errstr = "unknown opcode ff",
  1014. .result = REJECT,
  1015. },
  1016. {
  1017. "junk insn4",
  1018. .insns = {
  1019. BPF_RAW_INSN(-1, -1, -1, -1, -1),
  1020. BPF_EXIT_INSN(),
  1021. },
  1022. .errstr = "unknown opcode ff",
  1023. .result = REJECT,
  1024. },
  1025. {
  1026. "junk insn5",
  1027. .insns = {
  1028. BPF_RAW_INSN(0x7f, -1, -1, -1, -1),
  1029. BPF_EXIT_INSN(),
  1030. },
  1031. .errstr = "BPF_ALU uses reserved fields",
  1032. .result = REJECT,
  1033. },
  1034. {
  1035. "misaligned read from stack",
  1036. .insns = {
  1037. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1038. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, -4),
  1039. BPF_EXIT_INSN(),
  1040. },
  1041. .errstr = "misaligned stack access",
  1042. .result = REJECT,
  1043. },
  1044. {
  1045. "invalid map_fd for function call",
  1046. .insns = {
  1047. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1048. BPF_ALU64_REG(BPF_MOV, BPF_REG_2, BPF_REG_10),
  1049. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1050. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1051. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1052. BPF_FUNC_map_delete_elem),
  1053. BPF_EXIT_INSN(),
  1054. },
  1055. .errstr = "fd 0 is not pointing to valid bpf_map",
  1056. .result = REJECT,
  1057. },
  1058. {
  1059. "don't check return value before access",
  1060. .insns = {
  1061. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1062. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1063. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1064. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1065. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1066. BPF_FUNC_map_lookup_elem),
  1067. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  1068. BPF_EXIT_INSN(),
  1069. },
  1070. .fixup_map1 = { 3 },
  1071. .errstr = "R0 invalid mem access 'map_value_or_null'",
  1072. .result = REJECT,
  1073. },
  1074. {
  1075. "access memory with incorrect alignment",
  1076. .insns = {
  1077. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1078. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1079. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1080. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1081. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1082. BPF_FUNC_map_lookup_elem),
  1083. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  1084. BPF_ST_MEM(BPF_DW, BPF_REG_0, 4, 0),
  1085. BPF_EXIT_INSN(),
  1086. },
  1087. .fixup_map1 = { 3 },
  1088. .errstr = "misaligned value access",
  1089. .result = REJECT,
  1090. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  1091. },
  1092. {
  1093. "sometimes access memory with incorrect alignment",
  1094. .insns = {
  1095. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1096. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1097. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1098. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1099. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1100. BPF_FUNC_map_lookup_elem),
  1101. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  1102. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  1103. BPF_EXIT_INSN(),
  1104. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 1),
  1105. BPF_EXIT_INSN(),
  1106. },
  1107. .fixup_map1 = { 3 },
  1108. .errstr = "R0 invalid mem access",
  1109. .errstr_unpriv = "R0 leaks addr",
  1110. .result = REJECT,
  1111. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  1112. },
  1113. {
  1114. "jump test 1",
  1115. .insns = {
  1116. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1117. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -8),
  1118. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
  1119. BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 0),
  1120. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 1, 1),
  1121. BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 1),
  1122. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 2, 1),
  1123. BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 2),
  1124. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 3, 1),
  1125. BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 3),
  1126. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 4, 1),
  1127. BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 4),
  1128. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 5, 1),
  1129. BPF_ST_MEM(BPF_DW, BPF_REG_2, -32, 5),
  1130. BPF_MOV64_IMM(BPF_REG_0, 0),
  1131. BPF_EXIT_INSN(),
  1132. },
  1133. .errstr_unpriv = "R1 pointer comparison",
  1134. .result_unpriv = REJECT,
  1135. .result = ACCEPT,
  1136. },
  1137. {
  1138. "jump test 2",
  1139. .insns = {
  1140. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1141. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
  1142. BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 0),
  1143. BPF_JMP_IMM(BPF_JA, 0, 0, 14),
  1144. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 1, 2),
  1145. BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 0),
  1146. BPF_JMP_IMM(BPF_JA, 0, 0, 11),
  1147. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 2, 2),
  1148. BPF_ST_MEM(BPF_DW, BPF_REG_2, -32, 0),
  1149. BPF_JMP_IMM(BPF_JA, 0, 0, 8),
  1150. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 3, 2),
  1151. BPF_ST_MEM(BPF_DW, BPF_REG_2, -40, 0),
  1152. BPF_JMP_IMM(BPF_JA, 0, 0, 5),
  1153. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 4, 2),
  1154. BPF_ST_MEM(BPF_DW, BPF_REG_2, -48, 0),
  1155. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  1156. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 5, 1),
  1157. BPF_ST_MEM(BPF_DW, BPF_REG_2, -56, 0),
  1158. BPF_MOV64_IMM(BPF_REG_0, 0),
  1159. BPF_EXIT_INSN(),
  1160. },
  1161. .errstr_unpriv = "R1 pointer comparison",
  1162. .result_unpriv = REJECT,
  1163. .result = ACCEPT,
  1164. },
  1165. {
  1166. "jump test 3",
  1167. .insns = {
  1168. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1169. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
  1170. BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 0),
  1171. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1172. BPF_JMP_IMM(BPF_JA, 0, 0, 19),
  1173. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 1, 3),
  1174. BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 0),
  1175. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  1176. BPF_JMP_IMM(BPF_JA, 0, 0, 15),
  1177. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 2, 3),
  1178. BPF_ST_MEM(BPF_DW, BPF_REG_2, -32, 0),
  1179. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -32),
  1180. BPF_JMP_IMM(BPF_JA, 0, 0, 11),
  1181. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 3, 3),
  1182. BPF_ST_MEM(BPF_DW, BPF_REG_2, -40, 0),
  1183. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -40),
  1184. BPF_JMP_IMM(BPF_JA, 0, 0, 7),
  1185. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 4, 3),
  1186. BPF_ST_MEM(BPF_DW, BPF_REG_2, -48, 0),
  1187. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48),
  1188. BPF_JMP_IMM(BPF_JA, 0, 0, 3),
  1189. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 5, 0),
  1190. BPF_ST_MEM(BPF_DW, BPF_REG_2, -56, 0),
  1191. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -56),
  1192. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1193. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1194. BPF_FUNC_map_delete_elem),
  1195. BPF_EXIT_INSN(),
  1196. },
  1197. .fixup_map1 = { 24 },
  1198. .errstr_unpriv = "R1 pointer comparison",
  1199. .result_unpriv = REJECT,
  1200. .result = ACCEPT,
  1201. .retval = -ENOENT,
  1202. },
  1203. {
  1204. "jump test 4",
  1205. .insns = {
  1206. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1207. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1208. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1209. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1210. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1211. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1212. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1213. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1214. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1215. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1216. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1217. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1218. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1219. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1220. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1221. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1222. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1223. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1224. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1225. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1226. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1227. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1228. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1229. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1230. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1231. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1232. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1233. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1234. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1235. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1236. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1237. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1238. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
  1239. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
  1240. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
  1241. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
  1242. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
  1243. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
  1244. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
  1245. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
  1246. BPF_MOV64_IMM(BPF_REG_0, 0),
  1247. BPF_EXIT_INSN(),
  1248. },
  1249. .errstr_unpriv = "R1 pointer comparison",
  1250. .result_unpriv = REJECT,
  1251. .result = ACCEPT,
  1252. },
  1253. {
  1254. "jump test 5",
  1255. .insns = {
  1256. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1257. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  1258. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  1259. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, -8),
  1260. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  1261. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_2, -8),
  1262. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  1263. BPF_MOV64_IMM(BPF_REG_0, 0),
  1264. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  1265. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, -8),
  1266. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  1267. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_2, -8),
  1268. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  1269. BPF_MOV64_IMM(BPF_REG_0, 0),
  1270. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  1271. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, -8),
  1272. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  1273. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_2, -8),
  1274. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  1275. BPF_MOV64_IMM(BPF_REG_0, 0),
  1276. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  1277. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, -8),
  1278. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  1279. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_2, -8),
  1280. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  1281. BPF_MOV64_IMM(BPF_REG_0, 0),
  1282. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  1283. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, -8),
  1284. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  1285. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_2, -8),
  1286. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  1287. BPF_MOV64_IMM(BPF_REG_0, 0),
  1288. BPF_EXIT_INSN(),
  1289. },
  1290. .errstr_unpriv = "R1 pointer comparison",
  1291. .result_unpriv = REJECT,
  1292. .result = ACCEPT,
  1293. },
  1294. {
  1295. "access skb fields ok",
  1296. .insns = {
  1297. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1298. offsetof(struct __sk_buff, len)),
  1299. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
  1300. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1301. offsetof(struct __sk_buff, mark)),
  1302. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
  1303. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1304. offsetof(struct __sk_buff, pkt_type)),
  1305. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
  1306. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1307. offsetof(struct __sk_buff, queue_mapping)),
  1308. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
  1309. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1310. offsetof(struct __sk_buff, protocol)),
  1311. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
  1312. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1313. offsetof(struct __sk_buff, vlan_present)),
  1314. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
  1315. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1316. offsetof(struct __sk_buff, vlan_tci)),
  1317. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
  1318. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1319. offsetof(struct __sk_buff, napi_id)),
  1320. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
  1321. BPF_EXIT_INSN(),
  1322. },
  1323. .result = ACCEPT,
  1324. },
  1325. {
  1326. "access skb fields bad1",
  1327. .insns = {
  1328. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -4),
  1329. BPF_EXIT_INSN(),
  1330. },
  1331. .errstr = "invalid bpf_context access",
  1332. .result = REJECT,
  1333. },
  1334. {
  1335. "access skb fields bad2",
  1336. .insns = {
  1337. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 9),
  1338. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1339. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1340. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1341. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1342. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1343. BPF_FUNC_map_lookup_elem),
  1344. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  1345. BPF_EXIT_INSN(),
  1346. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  1347. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1348. offsetof(struct __sk_buff, pkt_type)),
  1349. BPF_EXIT_INSN(),
  1350. },
  1351. .fixup_map1 = { 4 },
  1352. .errstr = "different pointers",
  1353. .errstr_unpriv = "R1 pointer comparison",
  1354. .result = REJECT,
  1355. },
  1356. {
  1357. "access skb fields bad3",
  1358. .insns = {
  1359. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
  1360. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1361. offsetof(struct __sk_buff, pkt_type)),
  1362. BPF_EXIT_INSN(),
  1363. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1364. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1365. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1366. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1367. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1368. BPF_FUNC_map_lookup_elem),
  1369. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  1370. BPF_EXIT_INSN(),
  1371. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  1372. BPF_JMP_IMM(BPF_JA, 0, 0, -12),
  1373. },
  1374. .fixup_map1 = { 6 },
  1375. .errstr = "different pointers",
  1376. .errstr_unpriv = "R1 pointer comparison",
  1377. .result = REJECT,
  1378. },
  1379. {
  1380. "access skb fields bad4",
  1381. .insns = {
  1382. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 3),
  1383. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  1384. offsetof(struct __sk_buff, len)),
  1385. BPF_MOV64_IMM(BPF_REG_0, 0),
  1386. BPF_EXIT_INSN(),
  1387. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  1388. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  1389. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  1390. BPF_LD_MAP_FD(BPF_REG_1, 0),
  1391. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  1392. BPF_FUNC_map_lookup_elem),
  1393. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  1394. BPF_EXIT_INSN(),
  1395. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  1396. BPF_JMP_IMM(BPF_JA, 0, 0, -13),
  1397. },
  1398. .fixup_map1 = { 7 },
  1399. .errstr = "different pointers",
  1400. .errstr_unpriv = "R1 pointer comparison",
  1401. .result = REJECT,
  1402. },
  1403. {
  1404. "invalid access __sk_buff family",
  1405. .insns = {
  1406. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1407. offsetof(struct __sk_buff, family)),
  1408. BPF_EXIT_INSN(),
  1409. },
  1410. .errstr = "invalid bpf_context access",
  1411. .result = REJECT,
  1412. },
  1413. {
  1414. "invalid access __sk_buff remote_ip4",
  1415. .insns = {
  1416. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1417. offsetof(struct __sk_buff, remote_ip4)),
  1418. BPF_EXIT_INSN(),
  1419. },
  1420. .errstr = "invalid bpf_context access",
  1421. .result = REJECT,
  1422. },
  1423. {
  1424. "invalid access __sk_buff local_ip4",
  1425. .insns = {
  1426. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1427. offsetof(struct __sk_buff, local_ip4)),
  1428. BPF_EXIT_INSN(),
  1429. },
  1430. .errstr = "invalid bpf_context access",
  1431. .result = REJECT,
  1432. },
  1433. {
  1434. "invalid access __sk_buff remote_ip6",
  1435. .insns = {
  1436. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1437. offsetof(struct __sk_buff, remote_ip6)),
  1438. BPF_EXIT_INSN(),
  1439. },
  1440. .errstr = "invalid bpf_context access",
  1441. .result = REJECT,
  1442. },
  1443. {
  1444. "invalid access __sk_buff local_ip6",
  1445. .insns = {
  1446. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1447. offsetof(struct __sk_buff, local_ip6)),
  1448. BPF_EXIT_INSN(),
  1449. },
  1450. .errstr = "invalid bpf_context access",
  1451. .result = REJECT,
  1452. },
  1453. {
  1454. "invalid access __sk_buff remote_port",
  1455. .insns = {
  1456. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1457. offsetof(struct __sk_buff, remote_port)),
  1458. BPF_EXIT_INSN(),
  1459. },
  1460. .errstr = "invalid bpf_context access",
  1461. .result = REJECT,
  1462. },
  1463. {
  1464. "invalid access __sk_buff remote_port",
  1465. .insns = {
  1466. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1467. offsetof(struct __sk_buff, local_port)),
  1468. BPF_EXIT_INSN(),
  1469. },
  1470. .errstr = "invalid bpf_context access",
  1471. .result = REJECT,
  1472. },
  1473. {
  1474. "valid access __sk_buff family",
  1475. .insns = {
  1476. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1477. offsetof(struct __sk_buff, family)),
  1478. BPF_EXIT_INSN(),
  1479. },
  1480. .result = ACCEPT,
  1481. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1482. },
  1483. {
  1484. "valid access __sk_buff remote_ip4",
  1485. .insns = {
  1486. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1487. offsetof(struct __sk_buff, remote_ip4)),
  1488. BPF_EXIT_INSN(),
  1489. },
  1490. .result = ACCEPT,
  1491. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1492. },
  1493. {
  1494. "valid access __sk_buff local_ip4",
  1495. .insns = {
  1496. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1497. offsetof(struct __sk_buff, local_ip4)),
  1498. BPF_EXIT_INSN(),
  1499. },
  1500. .result = ACCEPT,
  1501. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1502. },
  1503. {
  1504. "valid access __sk_buff remote_ip6",
  1505. .insns = {
  1506. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1507. offsetof(struct __sk_buff, remote_ip6[0])),
  1508. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1509. offsetof(struct __sk_buff, remote_ip6[1])),
  1510. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1511. offsetof(struct __sk_buff, remote_ip6[2])),
  1512. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1513. offsetof(struct __sk_buff, remote_ip6[3])),
  1514. BPF_EXIT_INSN(),
  1515. },
  1516. .result = ACCEPT,
  1517. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1518. },
  1519. {
  1520. "valid access __sk_buff local_ip6",
  1521. .insns = {
  1522. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1523. offsetof(struct __sk_buff, local_ip6[0])),
  1524. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1525. offsetof(struct __sk_buff, local_ip6[1])),
  1526. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1527. offsetof(struct __sk_buff, local_ip6[2])),
  1528. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1529. offsetof(struct __sk_buff, local_ip6[3])),
  1530. BPF_EXIT_INSN(),
  1531. },
  1532. .result = ACCEPT,
  1533. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1534. },
  1535. {
  1536. "valid access __sk_buff remote_port",
  1537. .insns = {
  1538. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1539. offsetof(struct __sk_buff, remote_port)),
  1540. BPF_EXIT_INSN(),
  1541. },
  1542. .result = ACCEPT,
  1543. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1544. },
  1545. {
  1546. "valid access __sk_buff remote_port",
  1547. .insns = {
  1548. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1549. offsetof(struct __sk_buff, local_port)),
  1550. BPF_EXIT_INSN(),
  1551. },
  1552. .result = ACCEPT,
  1553. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1554. },
  1555. {
  1556. "invalid access of tc_classid for SK_SKB",
  1557. .insns = {
  1558. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1559. offsetof(struct __sk_buff, tc_classid)),
  1560. BPF_EXIT_INSN(),
  1561. },
  1562. .result = REJECT,
  1563. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1564. .errstr = "invalid bpf_context access",
  1565. },
  1566. {
  1567. "invalid access of skb->mark for SK_SKB",
  1568. .insns = {
  1569. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1570. offsetof(struct __sk_buff, mark)),
  1571. BPF_EXIT_INSN(),
  1572. },
  1573. .result = REJECT,
  1574. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1575. .errstr = "invalid bpf_context access",
  1576. },
  1577. {
  1578. "check skb->mark is not writeable by SK_SKB",
  1579. .insns = {
  1580. BPF_MOV64_IMM(BPF_REG_0, 0),
  1581. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  1582. offsetof(struct __sk_buff, mark)),
  1583. BPF_EXIT_INSN(),
  1584. },
  1585. .result = REJECT,
  1586. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1587. .errstr = "invalid bpf_context access",
  1588. },
  1589. {
  1590. "check skb->tc_index is writeable by SK_SKB",
  1591. .insns = {
  1592. BPF_MOV64_IMM(BPF_REG_0, 0),
  1593. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  1594. offsetof(struct __sk_buff, tc_index)),
  1595. BPF_EXIT_INSN(),
  1596. },
  1597. .result = ACCEPT,
  1598. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1599. },
  1600. {
  1601. "check skb->priority is writeable by SK_SKB",
  1602. .insns = {
  1603. BPF_MOV64_IMM(BPF_REG_0, 0),
  1604. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  1605. offsetof(struct __sk_buff, priority)),
  1606. BPF_EXIT_INSN(),
  1607. },
  1608. .result = ACCEPT,
  1609. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1610. },
  1611. {
  1612. "direct packet read for SK_SKB",
  1613. .insns = {
  1614. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  1615. offsetof(struct __sk_buff, data)),
  1616. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  1617. offsetof(struct __sk_buff, data_end)),
  1618. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  1619. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  1620. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  1621. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  1622. BPF_MOV64_IMM(BPF_REG_0, 0),
  1623. BPF_EXIT_INSN(),
  1624. },
  1625. .result = ACCEPT,
  1626. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1627. },
  1628. {
  1629. "direct packet write for SK_SKB",
  1630. .insns = {
  1631. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  1632. offsetof(struct __sk_buff, data)),
  1633. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  1634. offsetof(struct __sk_buff, data_end)),
  1635. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  1636. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  1637. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  1638. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  1639. BPF_MOV64_IMM(BPF_REG_0, 0),
  1640. BPF_EXIT_INSN(),
  1641. },
  1642. .result = ACCEPT,
  1643. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1644. },
  1645. {
  1646. "overlapping checks for direct packet access SK_SKB",
  1647. .insns = {
  1648. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  1649. offsetof(struct __sk_buff, data)),
  1650. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  1651. offsetof(struct __sk_buff, data_end)),
  1652. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  1653. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  1654. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 4),
  1655. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  1656. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 6),
  1657. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  1658. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_2, 6),
  1659. BPF_MOV64_IMM(BPF_REG_0, 0),
  1660. BPF_EXIT_INSN(),
  1661. },
  1662. .result = ACCEPT,
  1663. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1664. },
  1665. {
  1666. "valid access family in SK_MSG",
  1667. .insns = {
  1668. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1669. offsetof(struct sk_msg_md, family)),
  1670. BPF_EXIT_INSN(),
  1671. },
  1672. .result = ACCEPT,
  1673. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1674. },
  1675. {
  1676. "valid access remote_ip4 in SK_MSG",
  1677. .insns = {
  1678. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1679. offsetof(struct sk_msg_md, remote_ip4)),
  1680. BPF_EXIT_INSN(),
  1681. },
  1682. .result = ACCEPT,
  1683. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1684. },
  1685. {
  1686. "valid access local_ip4 in SK_MSG",
  1687. .insns = {
  1688. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1689. offsetof(struct sk_msg_md, local_ip4)),
  1690. BPF_EXIT_INSN(),
  1691. },
  1692. .result = ACCEPT,
  1693. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1694. },
  1695. {
  1696. "valid access remote_port in SK_MSG",
  1697. .insns = {
  1698. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1699. offsetof(struct sk_msg_md, remote_port)),
  1700. BPF_EXIT_INSN(),
  1701. },
  1702. .result = ACCEPT,
  1703. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1704. },
  1705. {
  1706. "valid access local_port in SK_MSG",
  1707. .insns = {
  1708. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1709. offsetof(struct sk_msg_md, local_port)),
  1710. BPF_EXIT_INSN(),
  1711. },
  1712. .result = ACCEPT,
  1713. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1714. },
  1715. {
  1716. "valid access remote_ip6 in SK_MSG",
  1717. .insns = {
  1718. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1719. offsetof(struct sk_msg_md, remote_ip6[0])),
  1720. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1721. offsetof(struct sk_msg_md, remote_ip6[1])),
  1722. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1723. offsetof(struct sk_msg_md, remote_ip6[2])),
  1724. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1725. offsetof(struct sk_msg_md, remote_ip6[3])),
  1726. BPF_EXIT_INSN(),
  1727. },
  1728. .result = ACCEPT,
  1729. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1730. },
  1731. {
  1732. "valid access local_ip6 in SK_MSG",
  1733. .insns = {
  1734. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1735. offsetof(struct sk_msg_md, local_ip6[0])),
  1736. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1737. offsetof(struct sk_msg_md, local_ip6[1])),
  1738. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1739. offsetof(struct sk_msg_md, local_ip6[2])),
  1740. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  1741. offsetof(struct sk_msg_md, local_ip6[3])),
  1742. BPF_EXIT_INSN(),
  1743. },
  1744. .result = ACCEPT,
  1745. .prog_type = BPF_PROG_TYPE_SK_SKB,
  1746. },
  1747. {
  1748. "invalid 64B read of family in SK_MSG",
  1749. .insns = {
  1750. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1,
  1751. offsetof(struct sk_msg_md, family)),
  1752. BPF_EXIT_INSN(),
  1753. },
  1754. .errstr = "invalid bpf_context access",
  1755. .result = REJECT,
  1756. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1757. },
  1758. {
  1759. "invalid read past end of SK_MSG",
  1760. .insns = {
  1761. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  1762. offsetof(struct sk_msg_md, local_port) + 4),
  1763. BPF_EXIT_INSN(),
  1764. },
  1765. .errstr = "R0 !read_ok",
  1766. .result = REJECT,
  1767. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1768. },
  1769. {
  1770. "invalid read offset in SK_MSG",
  1771. .insns = {
  1772. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  1773. offsetof(struct sk_msg_md, family) + 1),
  1774. BPF_EXIT_INSN(),
  1775. },
  1776. .errstr = "invalid bpf_context access",
  1777. .result = REJECT,
  1778. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1779. },
  1780. {
  1781. "direct packet read for SK_MSG",
  1782. .insns = {
  1783. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1,
  1784. offsetof(struct sk_msg_md, data)),
  1785. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_1,
  1786. offsetof(struct sk_msg_md, data_end)),
  1787. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  1788. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  1789. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  1790. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  1791. BPF_MOV64_IMM(BPF_REG_0, 0),
  1792. BPF_EXIT_INSN(),
  1793. },
  1794. .result = ACCEPT,
  1795. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1796. },
  1797. {
  1798. "direct packet write for SK_MSG",
  1799. .insns = {
  1800. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1,
  1801. offsetof(struct sk_msg_md, data)),
  1802. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_1,
  1803. offsetof(struct sk_msg_md, data_end)),
  1804. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  1805. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  1806. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  1807. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  1808. BPF_MOV64_IMM(BPF_REG_0, 0),
  1809. BPF_EXIT_INSN(),
  1810. },
  1811. .result = ACCEPT,
  1812. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1813. },
  1814. {
  1815. "overlapping checks for direct packet access SK_MSG",
  1816. .insns = {
  1817. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1,
  1818. offsetof(struct sk_msg_md, data)),
  1819. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_1,
  1820. offsetof(struct sk_msg_md, data_end)),
  1821. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  1822. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  1823. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 4),
  1824. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  1825. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 6),
  1826. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  1827. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_2, 6),
  1828. BPF_MOV64_IMM(BPF_REG_0, 0),
  1829. BPF_EXIT_INSN(),
  1830. },
  1831. .result = ACCEPT,
  1832. .prog_type = BPF_PROG_TYPE_SK_MSG,
  1833. },
  1834. {
  1835. "check skb->mark is not writeable by sockets",
  1836. .insns = {
  1837. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  1838. offsetof(struct __sk_buff, mark)),
  1839. BPF_EXIT_INSN(),
  1840. },
  1841. .errstr = "invalid bpf_context access",
  1842. .errstr_unpriv = "R1 leaks addr",
  1843. .result = REJECT,
  1844. },
  1845. {
  1846. "check skb->tc_index is not writeable by sockets",
  1847. .insns = {
  1848. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  1849. offsetof(struct __sk_buff, tc_index)),
  1850. BPF_EXIT_INSN(),
  1851. },
  1852. .errstr = "invalid bpf_context access",
  1853. .errstr_unpriv = "R1 leaks addr",
  1854. .result = REJECT,
  1855. },
  1856. {
  1857. "check cb access: byte",
  1858. .insns = {
  1859. BPF_MOV64_IMM(BPF_REG_0, 0),
  1860. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1861. offsetof(struct __sk_buff, cb[0])),
  1862. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1863. offsetof(struct __sk_buff, cb[0]) + 1),
  1864. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1865. offsetof(struct __sk_buff, cb[0]) + 2),
  1866. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1867. offsetof(struct __sk_buff, cb[0]) + 3),
  1868. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1869. offsetof(struct __sk_buff, cb[1])),
  1870. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1871. offsetof(struct __sk_buff, cb[1]) + 1),
  1872. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1873. offsetof(struct __sk_buff, cb[1]) + 2),
  1874. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1875. offsetof(struct __sk_buff, cb[1]) + 3),
  1876. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1877. offsetof(struct __sk_buff, cb[2])),
  1878. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1879. offsetof(struct __sk_buff, cb[2]) + 1),
  1880. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1881. offsetof(struct __sk_buff, cb[2]) + 2),
  1882. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1883. offsetof(struct __sk_buff, cb[2]) + 3),
  1884. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1885. offsetof(struct __sk_buff, cb[3])),
  1886. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1887. offsetof(struct __sk_buff, cb[3]) + 1),
  1888. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1889. offsetof(struct __sk_buff, cb[3]) + 2),
  1890. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1891. offsetof(struct __sk_buff, cb[3]) + 3),
  1892. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1893. offsetof(struct __sk_buff, cb[4])),
  1894. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1895. offsetof(struct __sk_buff, cb[4]) + 1),
  1896. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1897. offsetof(struct __sk_buff, cb[4]) + 2),
  1898. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1899. offsetof(struct __sk_buff, cb[4]) + 3),
  1900. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1901. offsetof(struct __sk_buff, cb[0])),
  1902. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1903. offsetof(struct __sk_buff, cb[0]) + 1),
  1904. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1905. offsetof(struct __sk_buff, cb[0]) + 2),
  1906. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1907. offsetof(struct __sk_buff, cb[0]) + 3),
  1908. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1909. offsetof(struct __sk_buff, cb[1])),
  1910. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1911. offsetof(struct __sk_buff, cb[1]) + 1),
  1912. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1913. offsetof(struct __sk_buff, cb[1]) + 2),
  1914. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1915. offsetof(struct __sk_buff, cb[1]) + 3),
  1916. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1917. offsetof(struct __sk_buff, cb[2])),
  1918. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1919. offsetof(struct __sk_buff, cb[2]) + 1),
  1920. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1921. offsetof(struct __sk_buff, cb[2]) + 2),
  1922. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1923. offsetof(struct __sk_buff, cb[2]) + 3),
  1924. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1925. offsetof(struct __sk_buff, cb[3])),
  1926. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1927. offsetof(struct __sk_buff, cb[3]) + 1),
  1928. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1929. offsetof(struct __sk_buff, cb[3]) + 2),
  1930. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1931. offsetof(struct __sk_buff, cb[3]) + 3),
  1932. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1933. offsetof(struct __sk_buff, cb[4])),
  1934. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1935. offsetof(struct __sk_buff, cb[4]) + 1),
  1936. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1937. offsetof(struct __sk_buff, cb[4]) + 2),
  1938. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1939. offsetof(struct __sk_buff, cb[4]) + 3),
  1940. BPF_EXIT_INSN(),
  1941. },
  1942. .result = ACCEPT,
  1943. },
  1944. {
  1945. "__sk_buff->hash, offset 0, byte store not permitted",
  1946. .insns = {
  1947. BPF_MOV64_IMM(BPF_REG_0, 0),
  1948. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1949. offsetof(struct __sk_buff, hash)),
  1950. BPF_EXIT_INSN(),
  1951. },
  1952. .errstr = "invalid bpf_context access",
  1953. .result = REJECT,
  1954. },
  1955. {
  1956. "__sk_buff->tc_index, offset 3, byte store not permitted",
  1957. .insns = {
  1958. BPF_MOV64_IMM(BPF_REG_0, 0),
  1959. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  1960. offsetof(struct __sk_buff, tc_index) + 3),
  1961. BPF_EXIT_INSN(),
  1962. },
  1963. .errstr = "invalid bpf_context access",
  1964. .result = REJECT,
  1965. },
  1966. {
  1967. "check skb->hash byte load permitted",
  1968. .insns = {
  1969. BPF_MOV64_IMM(BPF_REG_0, 0),
  1970. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1971. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1972. offsetof(struct __sk_buff, hash)),
  1973. #else
  1974. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1975. offsetof(struct __sk_buff, hash) + 3),
  1976. #endif
  1977. BPF_EXIT_INSN(),
  1978. },
  1979. .result = ACCEPT,
  1980. },
  1981. {
  1982. "check skb->hash byte load not permitted 1",
  1983. .insns = {
  1984. BPF_MOV64_IMM(BPF_REG_0, 0),
  1985. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1986. offsetof(struct __sk_buff, hash) + 1),
  1987. BPF_EXIT_INSN(),
  1988. },
  1989. .errstr = "invalid bpf_context access",
  1990. .result = REJECT,
  1991. },
  1992. {
  1993. "check skb->hash byte load not permitted 2",
  1994. .insns = {
  1995. BPF_MOV64_IMM(BPF_REG_0, 0),
  1996. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  1997. offsetof(struct __sk_buff, hash) + 2),
  1998. BPF_EXIT_INSN(),
  1999. },
  2000. .errstr = "invalid bpf_context access",
  2001. .result = REJECT,
  2002. },
  2003. {
  2004. "check skb->hash byte load not permitted 3",
  2005. .insns = {
  2006. BPF_MOV64_IMM(BPF_REG_0, 0),
  2007. #if __BYTE_ORDER == __LITTLE_ENDIAN
  2008. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  2009. offsetof(struct __sk_buff, hash) + 3),
  2010. #else
  2011. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  2012. offsetof(struct __sk_buff, hash)),
  2013. #endif
  2014. BPF_EXIT_INSN(),
  2015. },
  2016. .errstr = "invalid bpf_context access",
  2017. .result = REJECT,
  2018. },
  2019. {
  2020. "check cb access: byte, wrong type",
  2021. .insns = {
  2022. BPF_MOV64_IMM(BPF_REG_0, 0),
  2023. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0,
  2024. offsetof(struct __sk_buff, cb[0])),
  2025. BPF_EXIT_INSN(),
  2026. },
  2027. .errstr = "invalid bpf_context access",
  2028. .result = REJECT,
  2029. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  2030. },
  2031. {
  2032. "check cb access: half",
  2033. .insns = {
  2034. BPF_MOV64_IMM(BPF_REG_0, 0),
  2035. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2036. offsetof(struct __sk_buff, cb[0])),
  2037. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2038. offsetof(struct __sk_buff, cb[0]) + 2),
  2039. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2040. offsetof(struct __sk_buff, cb[1])),
  2041. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2042. offsetof(struct __sk_buff, cb[1]) + 2),
  2043. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2044. offsetof(struct __sk_buff, cb[2])),
  2045. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2046. offsetof(struct __sk_buff, cb[2]) + 2),
  2047. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2048. offsetof(struct __sk_buff, cb[3])),
  2049. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2050. offsetof(struct __sk_buff, cb[3]) + 2),
  2051. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2052. offsetof(struct __sk_buff, cb[4])),
  2053. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2054. offsetof(struct __sk_buff, cb[4]) + 2),
  2055. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2056. offsetof(struct __sk_buff, cb[0])),
  2057. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2058. offsetof(struct __sk_buff, cb[0]) + 2),
  2059. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2060. offsetof(struct __sk_buff, cb[1])),
  2061. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2062. offsetof(struct __sk_buff, cb[1]) + 2),
  2063. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2064. offsetof(struct __sk_buff, cb[2])),
  2065. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2066. offsetof(struct __sk_buff, cb[2]) + 2),
  2067. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2068. offsetof(struct __sk_buff, cb[3])),
  2069. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2070. offsetof(struct __sk_buff, cb[3]) + 2),
  2071. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2072. offsetof(struct __sk_buff, cb[4])),
  2073. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2074. offsetof(struct __sk_buff, cb[4]) + 2),
  2075. BPF_EXIT_INSN(),
  2076. },
  2077. .result = ACCEPT,
  2078. },
  2079. {
  2080. "check cb access: half, unaligned",
  2081. .insns = {
  2082. BPF_MOV64_IMM(BPF_REG_0, 0),
  2083. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2084. offsetof(struct __sk_buff, cb[0]) + 1),
  2085. BPF_EXIT_INSN(),
  2086. },
  2087. .errstr = "misaligned context access",
  2088. .result = REJECT,
  2089. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2090. },
  2091. {
  2092. "check __sk_buff->hash, offset 0, half store not permitted",
  2093. .insns = {
  2094. BPF_MOV64_IMM(BPF_REG_0, 0),
  2095. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2096. offsetof(struct __sk_buff, hash)),
  2097. BPF_EXIT_INSN(),
  2098. },
  2099. .errstr = "invalid bpf_context access",
  2100. .result = REJECT,
  2101. },
  2102. {
  2103. "check __sk_buff->tc_index, offset 2, half store not permitted",
  2104. .insns = {
  2105. BPF_MOV64_IMM(BPF_REG_0, 0),
  2106. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2107. offsetof(struct __sk_buff, tc_index) + 2),
  2108. BPF_EXIT_INSN(),
  2109. },
  2110. .errstr = "invalid bpf_context access",
  2111. .result = REJECT,
  2112. },
  2113. {
  2114. "check skb->hash half load permitted",
  2115. .insns = {
  2116. BPF_MOV64_IMM(BPF_REG_0, 0),
  2117. #if __BYTE_ORDER == __LITTLE_ENDIAN
  2118. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2119. offsetof(struct __sk_buff, hash)),
  2120. #else
  2121. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2122. offsetof(struct __sk_buff, hash) + 2),
  2123. #endif
  2124. BPF_EXIT_INSN(),
  2125. },
  2126. .result = ACCEPT,
  2127. },
  2128. {
  2129. "check skb->hash half load not permitted",
  2130. .insns = {
  2131. BPF_MOV64_IMM(BPF_REG_0, 0),
  2132. #if __BYTE_ORDER == __LITTLE_ENDIAN
  2133. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2134. offsetof(struct __sk_buff, hash) + 2),
  2135. #else
  2136. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  2137. offsetof(struct __sk_buff, hash)),
  2138. #endif
  2139. BPF_EXIT_INSN(),
  2140. },
  2141. .errstr = "invalid bpf_context access",
  2142. .result = REJECT,
  2143. },
  2144. {
  2145. "check cb access: half, wrong type",
  2146. .insns = {
  2147. BPF_MOV64_IMM(BPF_REG_0, 0),
  2148. BPF_STX_MEM(BPF_H, BPF_REG_1, BPF_REG_0,
  2149. offsetof(struct __sk_buff, cb[0])),
  2150. BPF_EXIT_INSN(),
  2151. },
  2152. .errstr = "invalid bpf_context access",
  2153. .result = REJECT,
  2154. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  2155. },
  2156. {
  2157. "check cb access: word",
  2158. .insns = {
  2159. BPF_MOV64_IMM(BPF_REG_0, 0),
  2160. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2161. offsetof(struct __sk_buff, cb[0])),
  2162. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2163. offsetof(struct __sk_buff, cb[1])),
  2164. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2165. offsetof(struct __sk_buff, cb[2])),
  2166. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2167. offsetof(struct __sk_buff, cb[3])),
  2168. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2169. offsetof(struct __sk_buff, cb[4])),
  2170. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2171. offsetof(struct __sk_buff, cb[0])),
  2172. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2173. offsetof(struct __sk_buff, cb[1])),
  2174. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2175. offsetof(struct __sk_buff, cb[2])),
  2176. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2177. offsetof(struct __sk_buff, cb[3])),
  2178. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2179. offsetof(struct __sk_buff, cb[4])),
  2180. BPF_EXIT_INSN(),
  2181. },
  2182. .result = ACCEPT,
  2183. },
  2184. {
  2185. "check cb access: word, unaligned 1",
  2186. .insns = {
  2187. BPF_MOV64_IMM(BPF_REG_0, 0),
  2188. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2189. offsetof(struct __sk_buff, cb[0]) + 2),
  2190. BPF_EXIT_INSN(),
  2191. },
  2192. .errstr = "misaligned context access",
  2193. .result = REJECT,
  2194. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2195. },
  2196. {
  2197. "check cb access: word, unaligned 2",
  2198. .insns = {
  2199. BPF_MOV64_IMM(BPF_REG_0, 0),
  2200. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2201. offsetof(struct __sk_buff, cb[4]) + 1),
  2202. BPF_EXIT_INSN(),
  2203. },
  2204. .errstr = "misaligned context access",
  2205. .result = REJECT,
  2206. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2207. },
  2208. {
  2209. "check cb access: word, unaligned 3",
  2210. .insns = {
  2211. BPF_MOV64_IMM(BPF_REG_0, 0),
  2212. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2213. offsetof(struct __sk_buff, cb[4]) + 2),
  2214. BPF_EXIT_INSN(),
  2215. },
  2216. .errstr = "misaligned context access",
  2217. .result = REJECT,
  2218. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2219. },
  2220. {
  2221. "check cb access: word, unaligned 4",
  2222. .insns = {
  2223. BPF_MOV64_IMM(BPF_REG_0, 0),
  2224. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2225. offsetof(struct __sk_buff, cb[4]) + 3),
  2226. BPF_EXIT_INSN(),
  2227. },
  2228. .errstr = "misaligned context access",
  2229. .result = REJECT,
  2230. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2231. },
  2232. {
  2233. "check cb access: double",
  2234. .insns = {
  2235. BPF_MOV64_IMM(BPF_REG_0, 0),
  2236. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2237. offsetof(struct __sk_buff, cb[0])),
  2238. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2239. offsetof(struct __sk_buff, cb[2])),
  2240. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1,
  2241. offsetof(struct __sk_buff, cb[0])),
  2242. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1,
  2243. offsetof(struct __sk_buff, cb[2])),
  2244. BPF_EXIT_INSN(),
  2245. },
  2246. .result = ACCEPT,
  2247. },
  2248. {
  2249. "check cb access: double, unaligned 1",
  2250. .insns = {
  2251. BPF_MOV64_IMM(BPF_REG_0, 0),
  2252. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2253. offsetof(struct __sk_buff, cb[1])),
  2254. BPF_EXIT_INSN(),
  2255. },
  2256. .errstr = "misaligned context access",
  2257. .result = REJECT,
  2258. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2259. },
  2260. {
  2261. "check cb access: double, unaligned 2",
  2262. .insns = {
  2263. BPF_MOV64_IMM(BPF_REG_0, 0),
  2264. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2265. offsetof(struct __sk_buff, cb[3])),
  2266. BPF_EXIT_INSN(),
  2267. },
  2268. .errstr = "misaligned context access",
  2269. .result = REJECT,
  2270. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  2271. },
  2272. {
  2273. "check cb access: double, oob 1",
  2274. .insns = {
  2275. BPF_MOV64_IMM(BPF_REG_0, 0),
  2276. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2277. offsetof(struct __sk_buff, cb[4])),
  2278. BPF_EXIT_INSN(),
  2279. },
  2280. .errstr = "invalid bpf_context access",
  2281. .result = REJECT,
  2282. },
  2283. {
  2284. "check cb access: double, oob 2",
  2285. .insns = {
  2286. BPF_MOV64_IMM(BPF_REG_0, 0),
  2287. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1,
  2288. offsetof(struct __sk_buff, cb[4])),
  2289. BPF_EXIT_INSN(),
  2290. },
  2291. .errstr = "invalid bpf_context access",
  2292. .result = REJECT,
  2293. },
  2294. {
  2295. "check __sk_buff->ifindex dw store not permitted",
  2296. .insns = {
  2297. BPF_MOV64_IMM(BPF_REG_0, 0),
  2298. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2299. offsetof(struct __sk_buff, ifindex)),
  2300. BPF_EXIT_INSN(),
  2301. },
  2302. .errstr = "invalid bpf_context access",
  2303. .result = REJECT,
  2304. },
  2305. {
  2306. "check __sk_buff->ifindex dw load not permitted",
  2307. .insns = {
  2308. BPF_MOV64_IMM(BPF_REG_0, 0),
  2309. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1,
  2310. offsetof(struct __sk_buff, ifindex)),
  2311. BPF_EXIT_INSN(),
  2312. },
  2313. .errstr = "invalid bpf_context access",
  2314. .result = REJECT,
  2315. },
  2316. {
  2317. "check cb access: double, wrong type",
  2318. .insns = {
  2319. BPF_MOV64_IMM(BPF_REG_0, 0),
  2320. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  2321. offsetof(struct __sk_buff, cb[0])),
  2322. BPF_EXIT_INSN(),
  2323. },
  2324. .errstr = "invalid bpf_context access",
  2325. .result = REJECT,
  2326. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  2327. },
  2328. {
  2329. "check out of range skb->cb access",
  2330. .insns = {
  2331. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2332. offsetof(struct __sk_buff, cb[0]) + 256),
  2333. BPF_EXIT_INSN(),
  2334. },
  2335. .errstr = "invalid bpf_context access",
  2336. .errstr_unpriv = "",
  2337. .result = REJECT,
  2338. .prog_type = BPF_PROG_TYPE_SCHED_ACT,
  2339. },
  2340. {
  2341. "write skb fields from socket prog",
  2342. .insns = {
  2343. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2344. offsetof(struct __sk_buff, cb[4])),
  2345. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
  2346. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2347. offsetof(struct __sk_buff, mark)),
  2348. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2349. offsetof(struct __sk_buff, tc_index)),
  2350. BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 1),
  2351. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  2352. offsetof(struct __sk_buff, cb[0])),
  2353. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  2354. offsetof(struct __sk_buff, cb[2])),
  2355. BPF_EXIT_INSN(),
  2356. },
  2357. .result = ACCEPT,
  2358. .errstr_unpriv = "R1 leaks addr",
  2359. .result_unpriv = REJECT,
  2360. },
  2361. {
  2362. "write skb fields from tc_cls_act prog",
  2363. .insns = {
  2364. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2365. offsetof(struct __sk_buff, cb[0])),
  2366. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2367. offsetof(struct __sk_buff, mark)),
  2368. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  2369. offsetof(struct __sk_buff, tc_index)),
  2370. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2371. offsetof(struct __sk_buff, tc_index)),
  2372. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
  2373. offsetof(struct __sk_buff, cb[3])),
  2374. BPF_EXIT_INSN(),
  2375. },
  2376. .errstr_unpriv = "",
  2377. .result_unpriv = REJECT,
  2378. .result = ACCEPT,
  2379. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2380. },
  2381. {
  2382. "PTR_TO_STACK store/load",
  2383. .insns = {
  2384. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  2385. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -10),
  2386. BPF_ST_MEM(BPF_DW, BPF_REG_1, 2, 0xfaceb00c),
  2387. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 2),
  2388. BPF_EXIT_INSN(),
  2389. },
  2390. .result = ACCEPT,
  2391. .retval = 0xfaceb00c,
  2392. },
  2393. {
  2394. "PTR_TO_STACK store/load - bad alignment on off",
  2395. .insns = {
  2396. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  2397. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  2398. BPF_ST_MEM(BPF_DW, BPF_REG_1, 2, 0xfaceb00c),
  2399. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 2),
  2400. BPF_EXIT_INSN(),
  2401. },
  2402. .result = REJECT,
  2403. .errstr = "misaligned stack access off (0x0; 0x0)+-8+2 size 8",
  2404. },
  2405. {
  2406. "PTR_TO_STACK store/load - bad alignment on reg",
  2407. .insns = {
  2408. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  2409. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -10),
  2410. BPF_ST_MEM(BPF_DW, BPF_REG_1, 8, 0xfaceb00c),
  2411. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 8),
  2412. BPF_EXIT_INSN(),
  2413. },
  2414. .result = REJECT,
  2415. .errstr = "misaligned stack access off (0x0; 0x0)+-10+8 size 8",
  2416. },
  2417. {
  2418. "PTR_TO_STACK store/load - out of bounds low",
  2419. .insns = {
  2420. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  2421. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -80000),
  2422. BPF_ST_MEM(BPF_DW, BPF_REG_1, 8, 0xfaceb00c),
  2423. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 8),
  2424. BPF_EXIT_INSN(),
  2425. },
  2426. .result = REJECT,
  2427. .errstr = "invalid stack off=-79992 size=8",
  2428. },
  2429. {
  2430. "PTR_TO_STACK store/load - out of bounds high",
  2431. .insns = {
  2432. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  2433. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  2434. BPF_ST_MEM(BPF_DW, BPF_REG_1, 8, 0xfaceb00c),
  2435. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 8),
  2436. BPF_EXIT_INSN(),
  2437. },
  2438. .result = REJECT,
  2439. .errstr = "invalid stack off=0 size=8",
  2440. },
  2441. {
  2442. "unpriv: return pointer",
  2443. .insns = {
  2444. BPF_MOV64_REG(BPF_REG_0, BPF_REG_10),
  2445. BPF_EXIT_INSN(),
  2446. },
  2447. .result = ACCEPT,
  2448. .result_unpriv = REJECT,
  2449. .errstr_unpriv = "R0 leaks addr",
  2450. .retval = POINTER_VALUE,
  2451. },
  2452. {
  2453. "unpriv: add const to pointer",
  2454. .insns = {
  2455. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  2456. BPF_MOV64_IMM(BPF_REG_0, 0),
  2457. BPF_EXIT_INSN(),
  2458. },
  2459. .result = ACCEPT,
  2460. },
  2461. {
  2462. "unpriv: add pointer to pointer",
  2463. .insns = {
  2464. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_10),
  2465. BPF_MOV64_IMM(BPF_REG_0, 0),
  2466. BPF_EXIT_INSN(),
  2467. },
  2468. .result = REJECT,
  2469. .errstr = "R1 pointer += pointer",
  2470. },
  2471. {
  2472. "unpriv: neg pointer",
  2473. .insns = {
  2474. BPF_ALU64_IMM(BPF_NEG, BPF_REG_1, 0),
  2475. BPF_MOV64_IMM(BPF_REG_0, 0),
  2476. BPF_EXIT_INSN(),
  2477. },
  2478. .result = ACCEPT,
  2479. .result_unpriv = REJECT,
  2480. .errstr_unpriv = "R1 pointer arithmetic",
  2481. },
  2482. {
  2483. "unpriv: cmp pointer with const",
  2484. .insns = {
  2485. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 0),
  2486. BPF_MOV64_IMM(BPF_REG_0, 0),
  2487. BPF_EXIT_INSN(),
  2488. },
  2489. .result = ACCEPT,
  2490. .result_unpriv = REJECT,
  2491. .errstr_unpriv = "R1 pointer comparison",
  2492. },
  2493. {
  2494. "unpriv: cmp pointer with pointer",
  2495. .insns = {
  2496. BPF_JMP_REG(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
  2497. BPF_MOV64_IMM(BPF_REG_0, 0),
  2498. BPF_EXIT_INSN(),
  2499. },
  2500. .result = ACCEPT,
  2501. .result_unpriv = REJECT,
  2502. .errstr_unpriv = "R10 pointer comparison",
  2503. },
  2504. {
  2505. "unpriv: check that printk is disallowed",
  2506. .insns = {
  2507. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  2508. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  2509. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  2510. BPF_MOV64_IMM(BPF_REG_2, 8),
  2511. BPF_MOV64_REG(BPF_REG_3, BPF_REG_1),
  2512. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2513. BPF_FUNC_trace_printk),
  2514. BPF_MOV64_IMM(BPF_REG_0, 0),
  2515. BPF_EXIT_INSN(),
  2516. },
  2517. .errstr_unpriv = "unknown func bpf_trace_printk#6",
  2518. .result_unpriv = REJECT,
  2519. .result = ACCEPT,
  2520. },
  2521. {
  2522. "unpriv: pass pointer to helper function",
  2523. .insns = {
  2524. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  2525. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  2526. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  2527. BPF_LD_MAP_FD(BPF_REG_1, 0),
  2528. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  2529. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  2530. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2531. BPF_FUNC_map_update_elem),
  2532. BPF_MOV64_IMM(BPF_REG_0, 0),
  2533. BPF_EXIT_INSN(),
  2534. },
  2535. .fixup_map1 = { 3 },
  2536. .errstr_unpriv = "R4 leaks addr",
  2537. .result_unpriv = REJECT,
  2538. .result = ACCEPT,
  2539. },
  2540. {
  2541. "unpriv: indirectly pass pointer on stack to helper function",
  2542. .insns = {
  2543. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_10, -8),
  2544. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  2545. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  2546. BPF_LD_MAP_FD(BPF_REG_1, 0),
  2547. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2548. BPF_FUNC_map_lookup_elem),
  2549. BPF_MOV64_IMM(BPF_REG_0, 0),
  2550. BPF_EXIT_INSN(),
  2551. },
  2552. .fixup_map1 = { 3 },
  2553. .errstr = "invalid indirect read from stack off -8+0 size 8",
  2554. .result = REJECT,
  2555. },
  2556. {
  2557. "unpriv: mangle pointer on stack 1",
  2558. .insns = {
  2559. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_10, -8),
  2560. BPF_ST_MEM(BPF_W, BPF_REG_10, -8, 0),
  2561. BPF_MOV64_IMM(BPF_REG_0, 0),
  2562. BPF_EXIT_INSN(),
  2563. },
  2564. .errstr_unpriv = "attempt to corrupt spilled",
  2565. .result_unpriv = REJECT,
  2566. .result = ACCEPT,
  2567. },
  2568. {
  2569. "unpriv: mangle pointer on stack 2",
  2570. .insns = {
  2571. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_10, -8),
  2572. BPF_ST_MEM(BPF_B, BPF_REG_10, -1, 0),
  2573. BPF_MOV64_IMM(BPF_REG_0, 0),
  2574. BPF_EXIT_INSN(),
  2575. },
  2576. .errstr_unpriv = "attempt to corrupt spilled",
  2577. .result_unpriv = REJECT,
  2578. .result = ACCEPT,
  2579. },
  2580. {
  2581. "unpriv: read pointer from stack in small chunks",
  2582. .insns = {
  2583. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_10, -8),
  2584. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_10, -8),
  2585. BPF_MOV64_IMM(BPF_REG_0, 0),
  2586. BPF_EXIT_INSN(),
  2587. },
  2588. .errstr = "invalid size",
  2589. .result = REJECT,
  2590. },
  2591. {
  2592. "unpriv: write pointer into ctx",
  2593. .insns = {
  2594. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0),
  2595. BPF_MOV64_IMM(BPF_REG_0, 0),
  2596. BPF_EXIT_INSN(),
  2597. },
  2598. .errstr_unpriv = "R1 leaks addr",
  2599. .result_unpriv = REJECT,
  2600. .errstr = "invalid bpf_context access",
  2601. .result = REJECT,
  2602. },
  2603. {
  2604. "unpriv: spill/fill of ctx",
  2605. .insns = {
  2606. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2607. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2608. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  2609. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
  2610. BPF_MOV64_IMM(BPF_REG_0, 0),
  2611. BPF_EXIT_INSN(),
  2612. },
  2613. .result = ACCEPT,
  2614. },
  2615. {
  2616. "unpriv: spill/fill of ctx 2",
  2617. .insns = {
  2618. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2619. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2620. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  2621. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
  2622. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2623. BPF_FUNC_get_hash_recalc),
  2624. BPF_MOV64_IMM(BPF_REG_0, 0),
  2625. BPF_EXIT_INSN(),
  2626. },
  2627. .result = ACCEPT,
  2628. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2629. },
  2630. {
  2631. "unpriv: spill/fill of ctx 3",
  2632. .insns = {
  2633. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2634. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2635. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  2636. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, 0),
  2637. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
  2638. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2639. BPF_FUNC_get_hash_recalc),
  2640. BPF_EXIT_INSN(),
  2641. },
  2642. .result = REJECT,
  2643. .errstr = "R1 type=fp expected=ctx",
  2644. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2645. },
  2646. {
  2647. "unpriv: spill/fill of ctx 4",
  2648. .insns = {
  2649. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2650. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2651. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  2652. BPF_MOV64_IMM(BPF_REG_0, 1),
  2653. BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_10,
  2654. BPF_REG_0, -8, 0),
  2655. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
  2656. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2657. BPF_FUNC_get_hash_recalc),
  2658. BPF_EXIT_INSN(),
  2659. },
  2660. .result = REJECT,
  2661. .errstr = "R1 type=inv expected=ctx",
  2662. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2663. },
  2664. {
  2665. "unpriv: spill/fill of different pointers stx",
  2666. .insns = {
  2667. BPF_MOV64_IMM(BPF_REG_3, 42),
  2668. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2669. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2670. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
  2671. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  2672. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  2673. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
  2674. BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
  2675. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  2676. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
  2677. BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
  2678. offsetof(struct __sk_buff, mark)),
  2679. BPF_MOV64_IMM(BPF_REG_0, 0),
  2680. BPF_EXIT_INSN(),
  2681. },
  2682. .result = REJECT,
  2683. .errstr = "same insn cannot be used with different pointers",
  2684. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2685. },
  2686. {
  2687. "unpriv: spill/fill of different pointers ldx",
  2688. .insns = {
  2689. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2690. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2691. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
  2692. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  2693. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2,
  2694. -(__s32)offsetof(struct bpf_perf_event_data,
  2695. sample_period) - 8),
  2696. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
  2697. BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
  2698. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  2699. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
  2700. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1,
  2701. offsetof(struct bpf_perf_event_data,
  2702. sample_period)),
  2703. BPF_MOV64_IMM(BPF_REG_0, 0),
  2704. BPF_EXIT_INSN(),
  2705. },
  2706. .result = REJECT,
  2707. .errstr = "same insn cannot be used with different pointers",
  2708. .prog_type = BPF_PROG_TYPE_PERF_EVENT,
  2709. },
  2710. {
  2711. "unpriv: write pointer into map elem value",
  2712. .insns = {
  2713. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  2714. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  2715. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  2716. BPF_LD_MAP_FD(BPF_REG_1, 0),
  2717. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2718. BPF_FUNC_map_lookup_elem),
  2719. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  2720. BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
  2721. BPF_EXIT_INSN(),
  2722. },
  2723. .fixup_map1 = { 3 },
  2724. .errstr_unpriv = "R0 leaks addr",
  2725. .result_unpriv = REJECT,
  2726. .result = ACCEPT,
  2727. },
  2728. {
  2729. "unpriv: partial copy of pointer",
  2730. .insns = {
  2731. BPF_MOV32_REG(BPF_REG_1, BPF_REG_10),
  2732. BPF_MOV64_IMM(BPF_REG_0, 0),
  2733. BPF_EXIT_INSN(),
  2734. },
  2735. .errstr_unpriv = "R10 partial copy",
  2736. .result_unpriv = REJECT,
  2737. .result = ACCEPT,
  2738. },
  2739. {
  2740. "unpriv: pass pointer to tail_call",
  2741. .insns = {
  2742. BPF_MOV64_REG(BPF_REG_3, BPF_REG_1),
  2743. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2744. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2745. BPF_FUNC_tail_call),
  2746. BPF_MOV64_IMM(BPF_REG_0, 0),
  2747. BPF_EXIT_INSN(),
  2748. },
  2749. .fixup_prog = { 1 },
  2750. .errstr_unpriv = "R3 leaks addr into helper",
  2751. .result_unpriv = REJECT,
  2752. .result = ACCEPT,
  2753. },
  2754. {
  2755. "unpriv: cmp map pointer with zero",
  2756. .insns = {
  2757. BPF_MOV64_IMM(BPF_REG_1, 0),
  2758. BPF_LD_MAP_FD(BPF_REG_1, 0),
  2759. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 0),
  2760. BPF_MOV64_IMM(BPF_REG_0, 0),
  2761. BPF_EXIT_INSN(),
  2762. },
  2763. .fixup_map1 = { 1 },
  2764. .errstr_unpriv = "R1 pointer comparison",
  2765. .result_unpriv = REJECT,
  2766. .result = ACCEPT,
  2767. },
  2768. {
  2769. "unpriv: write into frame pointer",
  2770. .insns = {
  2771. BPF_MOV64_REG(BPF_REG_10, BPF_REG_1),
  2772. BPF_MOV64_IMM(BPF_REG_0, 0),
  2773. BPF_EXIT_INSN(),
  2774. },
  2775. .errstr = "frame pointer is read only",
  2776. .result = REJECT,
  2777. },
  2778. {
  2779. "unpriv: spill/fill frame pointer",
  2780. .insns = {
  2781. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2782. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2783. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, 0),
  2784. BPF_LDX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, 0),
  2785. BPF_MOV64_IMM(BPF_REG_0, 0),
  2786. BPF_EXIT_INSN(),
  2787. },
  2788. .errstr = "frame pointer is read only",
  2789. .result = REJECT,
  2790. },
  2791. {
  2792. "unpriv: cmp of frame pointer",
  2793. .insns = {
  2794. BPF_JMP_IMM(BPF_JEQ, BPF_REG_10, 0, 0),
  2795. BPF_MOV64_IMM(BPF_REG_0, 0),
  2796. BPF_EXIT_INSN(),
  2797. },
  2798. .errstr_unpriv = "R10 pointer comparison",
  2799. .result_unpriv = REJECT,
  2800. .result = ACCEPT,
  2801. },
  2802. {
  2803. "unpriv: adding of fp",
  2804. .insns = {
  2805. BPF_MOV64_IMM(BPF_REG_0, 0),
  2806. BPF_MOV64_IMM(BPF_REG_1, 0),
  2807. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_10),
  2808. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, -8),
  2809. BPF_EXIT_INSN(),
  2810. },
  2811. .result = ACCEPT,
  2812. },
  2813. {
  2814. "unpriv: cmp of stack pointer",
  2815. .insns = {
  2816. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  2817. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  2818. BPF_JMP_IMM(BPF_JEQ, BPF_REG_2, 0, 0),
  2819. BPF_MOV64_IMM(BPF_REG_0, 0),
  2820. BPF_EXIT_INSN(),
  2821. },
  2822. .errstr_unpriv = "R2 pointer comparison",
  2823. .result_unpriv = REJECT,
  2824. .result = ACCEPT,
  2825. },
  2826. {
  2827. "runtime/jit: tail_call within bounds, prog once",
  2828. .insns = {
  2829. BPF_MOV64_IMM(BPF_REG_3, 0),
  2830. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2831. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2832. BPF_FUNC_tail_call),
  2833. BPF_MOV64_IMM(BPF_REG_0, 1),
  2834. BPF_EXIT_INSN(),
  2835. },
  2836. .fixup_prog = { 1 },
  2837. .result = ACCEPT,
  2838. .retval = 42,
  2839. },
  2840. {
  2841. "runtime/jit: tail_call within bounds, prog loop",
  2842. .insns = {
  2843. BPF_MOV64_IMM(BPF_REG_3, 1),
  2844. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2845. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2846. BPF_FUNC_tail_call),
  2847. BPF_MOV64_IMM(BPF_REG_0, 1),
  2848. BPF_EXIT_INSN(),
  2849. },
  2850. .fixup_prog = { 1 },
  2851. .result = ACCEPT,
  2852. .retval = 41,
  2853. },
  2854. {
  2855. "runtime/jit: tail_call within bounds, no prog",
  2856. .insns = {
  2857. BPF_MOV64_IMM(BPF_REG_3, 2),
  2858. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2859. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2860. BPF_FUNC_tail_call),
  2861. BPF_MOV64_IMM(BPF_REG_0, 1),
  2862. BPF_EXIT_INSN(),
  2863. },
  2864. .fixup_prog = { 1 },
  2865. .result = ACCEPT,
  2866. .retval = 1,
  2867. },
  2868. {
  2869. "runtime/jit: tail_call out of bounds",
  2870. .insns = {
  2871. BPF_MOV64_IMM(BPF_REG_3, 256),
  2872. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2873. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2874. BPF_FUNC_tail_call),
  2875. BPF_MOV64_IMM(BPF_REG_0, 2),
  2876. BPF_EXIT_INSN(),
  2877. },
  2878. .fixup_prog = { 1 },
  2879. .result = ACCEPT,
  2880. .retval = 2,
  2881. },
  2882. {
  2883. "runtime/jit: pass negative index to tail_call",
  2884. .insns = {
  2885. BPF_MOV64_IMM(BPF_REG_3, -1),
  2886. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2887. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2888. BPF_FUNC_tail_call),
  2889. BPF_MOV64_IMM(BPF_REG_0, 2),
  2890. BPF_EXIT_INSN(),
  2891. },
  2892. .fixup_prog = { 1 },
  2893. .result = ACCEPT,
  2894. .retval = 2,
  2895. },
  2896. {
  2897. "runtime/jit: pass > 32bit index to tail_call",
  2898. .insns = {
  2899. BPF_LD_IMM64(BPF_REG_3, 0x100000000ULL),
  2900. BPF_LD_MAP_FD(BPF_REG_2, 0),
  2901. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2902. BPF_FUNC_tail_call),
  2903. BPF_MOV64_IMM(BPF_REG_0, 2),
  2904. BPF_EXIT_INSN(),
  2905. },
  2906. .fixup_prog = { 2 },
  2907. .result = ACCEPT,
  2908. .retval = 42,
  2909. },
  2910. {
  2911. "stack pointer arithmetic",
  2912. .insns = {
  2913. BPF_MOV64_IMM(BPF_REG_1, 4),
  2914. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  2915. BPF_MOV64_REG(BPF_REG_7, BPF_REG_10),
  2916. BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -10),
  2917. BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -10),
  2918. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  2919. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1),
  2920. BPF_ST_MEM(0, BPF_REG_2, 4, 0),
  2921. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  2922. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 8),
  2923. BPF_ST_MEM(0, BPF_REG_2, 4, 0),
  2924. BPF_MOV64_IMM(BPF_REG_0, 0),
  2925. BPF_EXIT_INSN(),
  2926. },
  2927. .result = ACCEPT,
  2928. },
  2929. {
  2930. "raw_stack: no skb_load_bytes",
  2931. .insns = {
  2932. BPF_MOV64_IMM(BPF_REG_2, 4),
  2933. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2934. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2935. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  2936. BPF_MOV64_IMM(BPF_REG_4, 8),
  2937. /* Call to skb_load_bytes() omitted. */
  2938. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  2939. BPF_EXIT_INSN(),
  2940. },
  2941. .result = REJECT,
  2942. .errstr = "invalid read from stack off -8+0 size 8",
  2943. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2944. },
  2945. {
  2946. "raw_stack: skb_load_bytes, negative len",
  2947. .insns = {
  2948. BPF_MOV64_IMM(BPF_REG_2, 4),
  2949. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2950. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2951. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  2952. BPF_MOV64_IMM(BPF_REG_4, -8),
  2953. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2954. BPF_FUNC_skb_load_bytes),
  2955. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  2956. BPF_EXIT_INSN(),
  2957. },
  2958. .result = REJECT,
  2959. .errstr = "R4 min value is negative",
  2960. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2961. },
  2962. {
  2963. "raw_stack: skb_load_bytes, negative len 2",
  2964. .insns = {
  2965. BPF_MOV64_IMM(BPF_REG_2, 4),
  2966. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2967. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2968. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  2969. BPF_MOV64_IMM(BPF_REG_4, ~0),
  2970. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2971. BPF_FUNC_skb_load_bytes),
  2972. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  2973. BPF_EXIT_INSN(),
  2974. },
  2975. .result = REJECT,
  2976. .errstr = "R4 min value is negative",
  2977. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2978. },
  2979. {
  2980. "raw_stack: skb_load_bytes, zero len",
  2981. .insns = {
  2982. BPF_MOV64_IMM(BPF_REG_2, 4),
  2983. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  2984. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  2985. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  2986. BPF_MOV64_IMM(BPF_REG_4, 0),
  2987. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  2988. BPF_FUNC_skb_load_bytes),
  2989. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  2990. BPF_EXIT_INSN(),
  2991. },
  2992. .result = REJECT,
  2993. .errstr = "invalid stack type R3",
  2994. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  2995. },
  2996. {
  2997. "raw_stack: skb_load_bytes, no init",
  2998. .insns = {
  2999. BPF_MOV64_IMM(BPF_REG_2, 4),
  3000. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3001. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  3002. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3003. BPF_MOV64_IMM(BPF_REG_4, 8),
  3004. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3005. BPF_FUNC_skb_load_bytes),
  3006. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3007. BPF_EXIT_INSN(),
  3008. },
  3009. .result = ACCEPT,
  3010. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3011. },
  3012. {
  3013. "raw_stack: skb_load_bytes, init",
  3014. .insns = {
  3015. BPF_MOV64_IMM(BPF_REG_2, 4),
  3016. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3017. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  3018. BPF_ST_MEM(BPF_DW, BPF_REG_6, 0, 0xcafe),
  3019. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3020. BPF_MOV64_IMM(BPF_REG_4, 8),
  3021. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3022. BPF_FUNC_skb_load_bytes),
  3023. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3024. BPF_EXIT_INSN(),
  3025. },
  3026. .result = ACCEPT,
  3027. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3028. },
  3029. {
  3030. "raw_stack: skb_load_bytes, spilled regs around bounds",
  3031. .insns = {
  3032. BPF_MOV64_IMM(BPF_REG_2, 4),
  3033. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3034. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -16),
  3035. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, -8),
  3036. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 8),
  3037. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3038. BPF_MOV64_IMM(BPF_REG_4, 8),
  3039. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3040. BPF_FUNC_skb_load_bytes),
  3041. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, -8),
  3042. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 8),
  3043. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0,
  3044. offsetof(struct __sk_buff, mark)),
  3045. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_2,
  3046. offsetof(struct __sk_buff, priority)),
  3047. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  3048. BPF_EXIT_INSN(),
  3049. },
  3050. .result = ACCEPT,
  3051. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3052. },
  3053. {
  3054. "raw_stack: skb_load_bytes, spilled regs corruption",
  3055. .insns = {
  3056. BPF_MOV64_IMM(BPF_REG_2, 4),
  3057. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3058. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
  3059. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  3060. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3061. BPF_MOV64_IMM(BPF_REG_4, 8),
  3062. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3063. BPF_FUNC_skb_load_bytes),
  3064. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3065. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0,
  3066. offsetof(struct __sk_buff, mark)),
  3067. BPF_EXIT_INSN(),
  3068. },
  3069. .result = REJECT,
  3070. .errstr = "R0 invalid mem access 'inv'",
  3071. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3072. },
  3073. {
  3074. "raw_stack: skb_load_bytes, spilled regs corruption 2",
  3075. .insns = {
  3076. BPF_MOV64_IMM(BPF_REG_2, 4),
  3077. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3078. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -16),
  3079. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, -8),
  3080. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  3081. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 8),
  3082. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3083. BPF_MOV64_IMM(BPF_REG_4, 8),
  3084. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3085. BPF_FUNC_skb_load_bytes),
  3086. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, -8),
  3087. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 8),
  3088. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6, 0),
  3089. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0,
  3090. offsetof(struct __sk_buff, mark)),
  3091. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_2,
  3092. offsetof(struct __sk_buff, priority)),
  3093. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  3094. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_3,
  3095. offsetof(struct __sk_buff, pkt_type)),
  3096. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_3),
  3097. BPF_EXIT_INSN(),
  3098. },
  3099. .result = REJECT,
  3100. .errstr = "R3 invalid mem access 'inv'",
  3101. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3102. },
  3103. {
  3104. "raw_stack: skb_load_bytes, spilled regs + data",
  3105. .insns = {
  3106. BPF_MOV64_IMM(BPF_REG_2, 4),
  3107. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3108. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -16),
  3109. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, -8),
  3110. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
  3111. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 8),
  3112. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3113. BPF_MOV64_IMM(BPF_REG_4, 8),
  3114. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3115. BPF_FUNC_skb_load_bytes),
  3116. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, -8),
  3117. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 8),
  3118. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6, 0),
  3119. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0,
  3120. offsetof(struct __sk_buff, mark)),
  3121. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_2,
  3122. offsetof(struct __sk_buff, priority)),
  3123. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  3124. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_3),
  3125. BPF_EXIT_INSN(),
  3126. },
  3127. .result = ACCEPT,
  3128. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3129. },
  3130. {
  3131. "raw_stack: skb_load_bytes, invalid access 1",
  3132. .insns = {
  3133. BPF_MOV64_IMM(BPF_REG_2, 4),
  3134. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3135. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -513),
  3136. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3137. BPF_MOV64_IMM(BPF_REG_4, 8),
  3138. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3139. BPF_FUNC_skb_load_bytes),
  3140. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3141. BPF_EXIT_INSN(),
  3142. },
  3143. .result = REJECT,
  3144. .errstr = "invalid stack type R3 off=-513 access_size=8",
  3145. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3146. },
  3147. {
  3148. "raw_stack: skb_load_bytes, invalid access 2",
  3149. .insns = {
  3150. BPF_MOV64_IMM(BPF_REG_2, 4),
  3151. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3152. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -1),
  3153. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3154. BPF_MOV64_IMM(BPF_REG_4, 8),
  3155. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3156. BPF_FUNC_skb_load_bytes),
  3157. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3158. BPF_EXIT_INSN(),
  3159. },
  3160. .result = REJECT,
  3161. .errstr = "invalid stack type R3 off=-1 access_size=8",
  3162. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3163. },
  3164. {
  3165. "raw_stack: skb_load_bytes, invalid access 3",
  3166. .insns = {
  3167. BPF_MOV64_IMM(BPF_REG_2, 4),
  3168. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3169. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 0xffffffff),
  3170. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3171. BPF_MOV64_IMM(BPF_REG_4, 0xffffffff),
  3172. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3173. BPF_FUNC_skb_load_bytes),
  3174. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3175. BPF_EXIT_INSN(),
  3176. },
  3177. .result = REJECT,
  3178. .errstr = "R4 min value is negative",
  3179. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3180. },
  3181. {
  3182. "raw_stack: skb_load_bytes, invalid access 4",
  3183. .insns = {
  3184. BPF_MOV64_IMM(BPF_REG_2, 4),
  3185. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3186. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -1),
  3187. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3188. BPF_MOV64_IMM(BPF_REG_4, 0x7fffffff),
  3189. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3190. BPF_FUNC_skb_load_bytes),
  3191. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3192. BPF_EXIT_INSN(),
  3193. },
  3194. .result = REJECT,
  3195. .errstr = "R4 unbounded memory access, use 'var &= const' or 'if (var < const)'",
  3196. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3197. },
  3198. {
  3199. "raw_stack: skb_load_bytes, invalid access 5",
  3200. .insns = {
  3201. BPF_MOV64_IMM(BPF_REG_2, 4),
  3202. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3203. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -512),
  3204. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3205. BPF_MOV64_IMM(BPF_REG_4, 0x7fffffff),
  3206. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3207. BPF_FUNC_skb_load_bytes),
  3208. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3209. BPF_EXIT_INSN(),
  3210. },
  3211. .result = REJECT,
  3212. .errstr = "R4 unbounded memory access, use 'var &= const' or 'if (var < const)'",
  3213. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3214. },
  3215. {
  3216. "raw_stack: skb_load_bytes, invalid access 6",
  3217. .insns = {
  3218. BPF_MOV64_IMM(BPF_REG_2, 4),
  3219. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3220. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -512),
  3221. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3222. BPF_MOV64_IMM(BPF_REG_4, 0),
  3223. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3224. BPF_FUNC_skb_load_bytes),
  3225. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3226. BPF_EXIT_INSN(),
  3227. },
  3228. .result = REJECT,
  3229. .errstr = "invalid stack type R3 off=-512 access_size=0",
  3230. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3231. },
  3232. {
  3233. "raw_stack: skb_load_bytes, large access",
  3234. .insns = {
  3235. BPF_MOV64_IMM(BPF_REG_2, 4),
  3236. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
  3237. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -512),
  3238. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  3239. BPF_MOV64_IMM(BPF_REG_4, 512),
  3240. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3241. BPF_FUNC_skb_load_bytes),
  3242. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  3243. BPF_EXIT_INSN(),
  3244. },
  3245. .result = ACCEPT,
  3246. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3247. },
  3248. {
  3249. "context stores via ST",
  3250. .insns = {
  3251. BPF_MOV64_IMM(BPF_REG_0, 0),
  3252. BPF_ST_MEM(BPF_DW, BPF_REG_1, offsetof(struct __sk_buff, mark), 0),
  3253. BPF_EXIT_INSN(),
  3254. },
  3255. .errstr = "BPF_ST stores into R1 context is not allowed",
  3256. .result = REJECT,
  3257. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3258. },
  3259. {
  3260. "context stores via XADD",
  3261. .insns = {
  3262. BPF_MOV64_IMM(BPF_REG_0, 0),
  3263. BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_W, BPF_REG_1,
  3264. BPF_REG_0, offsetof(struct __sk_buff, mark), 0),
  3265. BPF_EXIT_INSN(),
  3266. },
  3267. .errstr = "BPF_XADD stores into R1 context is not allowed",
  3268. .result = REJECT,
  3269. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3270. },
  3271. {
  3272. "direct packet access: test1",
  3273. .insns = {
  3274. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3275. offsetof(struct __sk_buff, data)),
  3276. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3277. offsetof(struct __sk_buff, data_end)),
  3278. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3279. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3280. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3281. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3282. BPF_MOV64_IMM(BPF_REG_0, 0),
  3283. BPF_EXIT_INSN(),
  3284. },
  3285. .result = ACCEPT,
  3286. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3287. },
  3288. {
  3289. "direct packet access: test2",
  3290. .insns = {
  3291. BPF_MOV64_IMM(BPF_REG_0, 1),
  3292. BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,
  3293. offsetof(struct __sk_buff, data_end)),
  3294. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3295. offsetof(struct __sk_buff, data)),
  3296. BPF_MOV64_REG(BPF_REG_5, BPF_REG_3),
  3297. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 14),
  3298. BPF_JMP_REG(BPF_JGT, BPF_REG_5, BPF_REG_4, 15),
  3299. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_3, 7),
  3300. BPF_LDX_MEM(BPF_B, BPF_REG_4, BPF_REG_3, 12),
  3301. BPF_ALU64_IMM(BPF_MUL, BPF_REG_4, 14),
  3302. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3303. offsetof(struct __sk_buff, data)),
  3304. BPF_ALU64_REG(BPF_ADD, BPF_REG_3, BPF_REG_4),
  3305. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3306. offsetof(struct __sk_buff, len)),
  3307. BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 49),
  3308. BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 49),
  3309. BPF_ALU64_REG(BPF_ADD, BPF_REG_3, BPF_REG_2),
  3310. BPF_MOV64_REG(BPF_REG_2, BPF_REG_3),
  3311. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 8),
  3312. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  3313. offsetof(struct __sk_buff, data_end)),
  3314. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 1),
  3315. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_3, 4),
  3316. BPF_MOV64_IMM(BPF_REG_0, 0),
  3317. BPF_EXIT_INSN(),
  3318. },
  3319. .result = ACCEPT,
  3320. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3321. },
  3322. {
  3323. "direct packet access: test3",
  3324. .insns = {
  3325. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3326. offsetof(struct __sk_buff, data)),
  3327. BPF_MOV64_IMM(BPF_REG_0, 0),
  3328. BPF_EXIT_INSN(),
  3329. },
  3330. .errstr = "invalid bpf_context access off=76",
  3331. .result = REJECT,
  3332. .prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
  3333. },
  3334. {
  3335. "direct packet access: test4 (write)",
  3336. .insns = {
  3337. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3338. offsetof(struct __sk_buff, data)),
  3339. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3340. offsetof(struct __sk_buff, data_end)),
  3341. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3342. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3343. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3344. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  3345. BPF_MOV64_IMM(BPF_REG_0, 0),
  3346. BPF_EXIT_INSN(),
  3347. },
  3348. .result = ACCEPT,
  3349. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3350. },
  3351. {
  3352. "direct packet access: test5 (pkt_end >= reg, good access)",
  3353. .insns = {
  3354. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3355. offsetof(struct __sk_buff, data)),
  3356. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3357. offsetof(struct __sk_buff, data_end)),
  3358. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3359. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3360. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_0, 2),
  3361. BPF_MOV64_IMM(BPF_REG_0, 1),
  3362. BPF_EXIT_INSN(),
  3363. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3364. BPF_MOV64_IMM(BPF_REG_0, 0),
  3365. BPF_EXIT_INSN(),
  3366. },
  3367. .result = ACCEPT,
  3368. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3369. },
  3370. {
  3371. "direct packet access: test6 (pkt_end >= reg, bad access)",
  3372. .insns = {
  3373. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3374. offsetof(struct __sk_buff, data)),
  3375. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3376. offsetof(struct __sk_buff, data_end)),
  3377. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3378. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3379. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_0, 3),
  3380. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3381. BPF_MOV64_IMM(BPF_REG_0, 1),
  3382. BPF_EXIT_INSN(),
  3383. BPF_MOV64_IMM(BPF_REG_0, 0),
  3384. BPF_EXIT_INSN(),
  3385. },
  3386. .errstr = "invalid access to packet",
  3387. .result = REJECT,
  3388. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3389. },
  3390. {
  3391. "direct packet access: test7 (pkt_end >= reg, both accesses)",
  3392. .insns = {
  3393. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3394. offsetof(struct __sk_buff, data)),
  3395. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3396. offsetof(struct __sk_buff, data_end)),
  3397. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3398. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3399. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_0, 3),
  3400. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3401. BPF_MOV64_IMM(BPF_REG_0, 1),
  3402. BPF_EXIT_INSN(),
  3403. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3404. BPF_MOV64_IMM(BPF_REG_0, 0),
  3405. BPF_EXIT_INSN(),
  3406. },
  3407. .errstr = "invalid access to packet",
  3408. .result = REJECT,
  3409. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3410. },
  3411. {
  3412. "direct packet access: test8 (double test, variant 1)",
  3413. .insns = {
  3414. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3415. offsetof(struct __sk_buff, data)),
  3416. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3417. offsetof(struct __sk_buff, data_end)),
  3418. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3419. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3420. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_0, 4),
  3421. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3422. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3423. BPF_MOV64_IMM(BPF_REG_0, 1),
  3424. BPF_EXIT_INSN(),
  3425. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3426. BPF_MOV64_IMM(BPF_REG_0, 0),
  3427. BPF_EXIT_INSN(),
  3428. },
  3429. .result = ACCEPT,
  3430. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3431. },
  3432. {
  3433. "direct packet access: test9 (double test, variant 2)",
  3434. .insns = {
  3435. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3436. offsetof(struct __sk_buff, data)),
  3437. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3438. offsetof(struct __sk_buff, data_end)),
  3439. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3440. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3441. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_0, 2),
  3442. BPF_MOV64_IMM(BPF_REG_0, 1),
  3443. BPF_EXIT_INSN(),
  3444. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3445. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3446. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3447. BPF_MOV64_IMM(BPF_REG_0, 0),
  3448. BPF_EXIT_INSN(),
  3449. },
  3450. .result = ACCEPT,
  3451. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3452. },
  3453. {
  3454. "direct packet access: test10 (write invalid)",
  3455. .insns = {
  3456. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3457. offsetof(struct __sk_buff, data)),
  3458. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3459. offsetof(struct __sk_buff, data_end)),
  3460. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3461. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3462. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
  3463. BPF_MOV64_IMM(BPF_REG_0, 0),
  3464. BPF_EXIT_INSN(),
  3465. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  3466. BPF_MOV64_IMM(BPF_REG_0, 0),
  3467. BPF_EXIT_INSN(),
  3468. },
  3469. .errstr = "invalid access to packet",
  3470. .result = REJECT,
  3471. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3472. },
  3473. {
  3474. "direct packet access: test11 (shift, good access)",
  3475. .insns = {
  3476. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3477. offsetof(struct __sk_buff, data)),
  3478. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3479. offsetof(struct __sk_buff, data_end)),
  3480. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3481. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 22),
  3482. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 8),
  3483. BPF_MOV64_IMM(BPF_REG_3, 144),
  3484. BPF_MOV64_REG(BPF_REG_5, BPF_REG_3),
  3485. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 23),
  3486. BPF_ALU64_IMM(BPF_RSH, BPF_REG_5, 3),
  3487. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  3488. BPF_ALU64_REG(BPF_ADD, BPF_REG_6, BPF_REG_5),
  3489. BPF_MOV64_IMM(BPF_REG_0, 1),
  3490. BPF_EXIT_INSN(),
  3491. BPF_MOV64_IMM(BPF_REG_0, 0),
  3492. BPF_EXIT_INSN(),
  3493. },
  3494. .result = ACCEPT,
  3495. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3496. .retval = 1,
  3497. },
  3498. {
  3499. "direct packet access: test12 (and, good access)",
  3500. .insns = {
  3501. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3502. offsetof(struct __sk_buff, data)),
  3503. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3504. offsetof(struct __sk_buff, data_end)),
  3505. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3506. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 22),
  3507. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 8),
  3508. BPF_MOV64_IMM(BPF_REG_3, 144),
  3509. BPF_MOV64_REG(BPF_REG_5, BPF_REG_3),
  3510. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 23),
  3511. BPF_ALU64_IMM(BPF_AND, BPF_REG_5, 15),
  3512. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  3513. BPF_ALU64_REG(BPF_ADD, BPF_REG_6, BPF_REG_5),
  3514. BPF_MOV64_IMM(BPF_REG_0, 1),
  3515. BPF_EXIT_INSN(),
  3516. BPF_MOV64_IMM(BPF_REG_0, 0),
  3517. BPF_EXIT_INSN(),
  3518. },
  3519. .result = ACCEPT,
  3520. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3521. .retval = 1,
  3522. },
  3523. {
  3524. "direct packet access: test13 (branches, good access)",
  3525. .insns = {
  3526. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3527. offsetof(struct __sk_buff, data)),
  3528. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3529. offsetof(struct __sk_buff, data_end)),
  3530. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3531. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 22),
  3532. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 13),
  3533. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3534. offsetof(struct __sk_buff, mark)),
  3535. BPF_MOV64_IMM(BPF_REG_4, 1),
  3536. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_4, 2),
  3537. BPF_MOV64_IMM(BPF_REG_3, 14),
  3538. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  3539. BPF_MOV64_IMM(BPF_REG_3, 24),
  3540. BPF_MOV64_REG(BPF_REG_5, BPF_REG_3),
  3541. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 23),
  3542. BPF_ALU64_IMM(BPF_AND, BPF_REG_5, 15),
  3543. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  3544. BPF_ALU64_REG(BPF_ADD, BPF_REG_6, BPF_REG_5),
  3545. BPF_MOV64_IMM(BPF_REG_0, 1),
  3546. BPF_EXIT_INSN(),
  3547. BPF_MOV64_IMM(BPF_REG_0, 0),
  3548. BPF_EXIT_INSN(),
  3549. },
  3550. .result = ACCEPT,
  3551. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3552. .retval = 1,
  3553. },
  3554. {
  3555. "direct packet access: test14 (pkt_ptr += 0, CONST_IMM, good access)",
  3556. .insns = {
  3557. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3558. offsetof(struct __sk_buff, data)),
  3559. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3560. offsetof(struct __sk_buff, data_end)),
  3561. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3562. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 22),
  3563. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 7),
  3564. BPF_MOV64_IMM(BPF_REG_5, 12),
  3565. BPF_ALU64_IMM(BPF_RSH, BPF_REG_5, 4),
  3566. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  3567. BPF_ALU64_REG(BPF_ADD, BPF_REG_6, BPF_REG_5),
  3568. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_6, 0),
  3569. BPF_MOV64_IMM(BPF_REG_0, 1),
  3570. BPF_EXIT_INSN(),
  3571. BPF_MOV64_IMM(BPF_REG_0, 0),
  3572. BPF_EXIT_INSN(),
  3573. },
  3574. .result = ACCEPT,
  3575. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3576. .retval = 1,
  3577. },
  3578. {
  3579. "direct packet access: test15 (spill with xadd)",
  3580. .insns = {
  3581. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3582. offsetof(struct __sk_buff, data)),
  3583. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3584. offsetof(struct __sk_buff, data_end)),
  3585. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3586. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3587. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 8),
  3588. BPF_MOV64_IMM(BPF_REG_5, 4096),
  3589. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  3590. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  3591. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  3592. BPF_STX_XADD(BPF_DW, BPF_REG_4, BPF_REG_5, 0),
  3593. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_4, 0),
  3594. BPF_STX_MEM(BPF_W, BPF_REG_2, BPF_REG_5, 0),
  3595. BPF_MOV64_IMM(BPF_REG_0, 0),
  3596. BPF_EXIT_INSN(),
  3597. },
  3598. .errstr = "R2 invalid mem access 'inv'",
  3599. .result = REJECT,
  3600. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3601. },
  3602. {
  3603. "direct packet access: test16 (arith on data_end)",
  3604. .insns = {
  3605. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3606. offsetof(struct __sk_buff, data)),
  3607. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3608. offsetof(struct __sk_buff, data_end)),
  3609. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3610. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3611. BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 16),
  3612. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3613. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  3614. BPF_MOV64_IMM(BPF_REG_0, 0),
  3615. BPF_EXIT_INSN(),
  3616. },
  3617. .errstr = "R3 pointer arithmetic on PTR_TO_PACKET_END",
  3618. .result = REJECT,
  3619. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3620. },
  3621. {
  3622. "direct packet access: test17 (pruning, alignment)",
  3623. .insns = {
  3624. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3625. offsetof(struct __sk_buff, data)),
  3626. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3627. offsetof(struct __sk_buff, data_end)),
  3628. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  3629. offsetof(struct __sk_buff, mark)),
  3630. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3631. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 14),
  3632. BPF_JMP_IMM(BPF_JGT, BPF_REG_7, 1, 4),
  3633. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3634. BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, -4),
  3635. BPF_MOV64_IMM(BPF_REG_0, 0),
  3636. BPF_EXIT_INSN(),
  3637. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
  3638. BPF_JMP_A(-6),
  3639. },
  3640. .errstr = "misaligned packet access off 2+(0x0; 0x0)+15+-4 size 4",
  3641. .result = REJECT,
  3642. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3643. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  3644. },
  3645. {
  3646. "direct packet access: test18 (imm += pkt_ptr, 1)",
  3647. .insns = {
  3648. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3649. offsetof(struct __sk_buff, data)),
  3650. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3651. offsetof(struct __sk_buff, data_end)),
  3652. BPF_MOV64_IMM(BPF_REG_0, 8),
  3653. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  3654. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3655. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  3656. BPF_MOV64_IMM(BPF_REG_0, 0),
  3657. BPF_EXIT_INSN(),
  3658. },
  3659. .result = ACCEPT,
  3660. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3661. },
  3662. {
  3663. "direct packet access: test19 (imm += pkt_ptr, 2)",
  3664. .insns = {
  3665. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3666. offsetof(struct __sk_buff, data)),
  3667. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3668. offsetof(struct __sk_buff, data_end)),
  3669. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3670. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3671. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
  3672. BPF_MOV64_IMM(BPF_REG_4, 4),
  3673. BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
  3674. BPF_STX_MEM(BPF_B, BPF_REG_4, BPF_REG_4, 0),
  3675. BPF_MOV64_IMM(BPF_REG_0, 0),
  3676. BPF_EXIT_INSN(),
  3677. },
  3678. .result = ACCEPT,
  3679. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3680. },
  3681. {
  3682. "direct packet access: test20 (x += pkt_ptr, 1)",
  3683. .insns = {
  3684. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3685. offsetof(struct __sk_buff, data)),
  3686. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3687. offsetof(struct __sk_buff, data_end)),
  3688. BPF_MOV64_IMM(BPF_REG_0, 0xffffffff),
  3689. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  3690. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  3691. BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0x7fff),
  3692. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  3693. BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
  3694. BPF_MOV64_REG(BPF_REG_5, BPF_REG_4),
  3695. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 0x7fff - 1),
  3696. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
  3697. BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_4, 0),
  3698. BPF_MOV64_IMM(BPF_REG_0, 0),
  3699. BPF_EXIT_INSN(),
  3700. },
  3701. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3702. .result = ACCEPT,
  3703. },
  3704. {
  3705. "direct packet access: test21 (x += pkt_ptr, 2)",
  3706. .insns = {
  3707. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3708. offsetof(struct __sk_buff, data)),
  3709. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3710. offsetof(struct __sk_buff, data_end)),
  3711. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3712. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3713. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 9),
  3714. BPF_MOV64_IMM(BPF_REG_4, 0xffffffff),
  3715. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_4, -8),
  3716. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  3717. BPF_ALU64_IMM(BPF_AND, BPF_REG_4, 0x7fff),
  3718. BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
  3719. BPF_MOV64_REG(BPF_REG_5, BPF_REG_4),
  3720. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 0x7fff - 1),
  3721. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
  3722. BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_4, 0),
  3723. BPF_MOV64_IMM(BPF_REG_0, 0),
  3724. BPF_EXIT_INSN(),
  3725. },
  3726. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3727. .result = ACCEPT,
  3728. },
  3729. {
  3730. "direct packet access: test22 (x += pkt_ptr, 3)",
  3731. .insns = {
  3732. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3733. offsetof(struct __sk_buff, data)),
  3734. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3735. offsetof(struct __sk_buff, data_end)),
  3736. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3737. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3738. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -8),
  3739. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_3, -16),
  3740. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_10, -16),
  3741. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 11),
  3742. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -8),
  3743. BPF_MOV64_IMM(BPF_REG_4, 0xffffffff),
  3744. BPF_STX_XADD(BPF_DW, BPF_REG_10, BPF_REG_4, -8),
  3745. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  3746. BPF_ALU64_IMM(BPF_RSH, BPF_REG_4, 49),
  3747. BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
  3748. BPF_MOV64_REG(BPF_REG_0, BPF_REG_4),
  3749. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2),
  3750. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
  3751. BPF_MOV64_IMM(BPF_REG_2, 1),
  3752. BPF_STX_MEM(BPF_H, BPF_REG_4, BPF_REG_2, 0),
  3753. BPF_MOV64_IMM(BPF_REG_0, 0),
  3754. BPF_EXIT_INSN(),
  3755. },
  3756. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3757. .result = ACCEPT,
  3758. },
  3759. {
  3760. "direct packet access: test23 (x += pkt_ptr, 4)",
  3761. .insns = {
  3762. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3763. offsetof(struct __sk_buff, data)),
  3764. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3765. offsetof(struct __sk_buff, data_end)),
  3766. BPF_MOV64_IMM(BPF_REG_0, 0xffffffff),
  3767. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  3768. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  3769. BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffff),
  3770. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  3771. BPF_MOV64_IMM(BPF_REG_0, 31),
  3772. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
  3773. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  3774. BPF_MOV64_REG(BPF_REG_5, BPF_REG_0),
  3775. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0xffff - 1),
  3776. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3777. BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_0, 0),
  3778. BPF_MOV64_IMM(BPF_REG_0, 0),
  3779. BPF_EXIT_INSN(),
  3780. },
  3781. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3782. .result = REJECT,
  3783. .errstr = "invalid access to packet, off=0 size=8, R5(id=1,off=0,r=0)",
  3784. },
  3785. {
  3786. "direct packet access: test24 (x += pkt_ptr, 5)",
  3787. .insns = {
  3788. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3789. offsetof(struct __sk_buff, data)),
  3790. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3791. offsetof(struct __sk_buff, data_end)),
  3792. BPF_MOV64_IMM(BPF_REG_0, 0xffffffff),
  3793. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  3794. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  3795. BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xff),
  3796. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  3797. BPF_MOV64_IMM(BPF_REG_0, 64),
  3798. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
  3799. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  3800. BPF_MOV64_REG(BPF_REG_5, BPF_REG_0),
  3801. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x7fff - 1),
  3802. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  3803. BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_0, 0),
  3804. BPF_MOV64_IMM(BPF_REG_0, 0),
  3805. BPF_EXIT_INSN(),
  3806. },
  3807. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3808. .result = ACCEPT,
  3809. },
  3810. {
  3811. "direct packet access: test25 (marking on <, good access)",
  3812. .insns = {
  3813. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3814. offsetof(struct __sk_buff, data)),
  3815. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3816. offsetof(struct __sk_buff, data_end)),
  3817. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3818. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3819. BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_3, 2),
  3820. BPF_MOV64_IMM(BPF_REG_0, 0),
  3821. BPF_EXIT_INSN(),
  3822. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3823. BPF_JMP_IMM(BPF_JA, 0, 0, -4),
  3824. },
  3825. .result = ACCEPT,
  3826. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3827. },
  3828. {
  3829. "direct packet access: test26 (marking on <, bad access)",
  3830. .insns = {
  3831. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3832. offsetof(struct __sk_buff, data)),
  3833. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3834. offsetof(struct __sk_buff, data_end)),
  3835. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3836. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3837. BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_3, 3),
  3838. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3839. BPF_MOV64_IMM(BPF_REG_0, 0),
  3840. BPF_EXIT_INSN(),
  3841. BPF_JMP_IMM(BPF_JA, 0, 0, -3),
  3842. },
  3843. .result = REJECT,
  3844. .errstr = "invalid access to packet",
  3845. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3846. },
  3847. {
  3848. "direct packet access: test27 (marking on <=, good access)",
  3849. .insns = {
  3850. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3851. offsetof(struct __sk_buff, data)),
  3852. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3853. offsetof(struct __sk_buff, data_end)),
  3854. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3855. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3856. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_0, 1),
  3857. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3858. BPF_MOV64_IMM(BPF_REG_0, 1),
  3859. BPF_EXIT_INSN(),
  3860. },
  3861. .result = ACCEPT,
  3862. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3863. .retval = 1,
  3864. },
  3865. {
  3866. "direct packet access: test28 (marking on <=, bad access)",
  3867. .insns = {
  3868. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3869. offsetof(struct __sk_buff, data)),
  3870. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3871. offsetof(struct __sk_buff, data_end)),
  3872. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  3873. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  3874. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_0, 2),
  3875. BPF_MOV64_IMM(BPF_REG_0, 1),
  3876. BPF_EXIT_INSN(),
  3877. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  3878. BPF_JMP_IMM(BPF_JA, 0, 0, -4),
  3879. },
  3880. .result = REJECT,
  3881. .errstr = "invalid access to packet",
  3882. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  3883. },
  3884. {
  3885. "helper access to packet: test1, valid packet_ptr range",
  3886. .insns = {
  3887. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3888. offsetof(struct xdp_md, data)),
  3889. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3890. offsetof(struct xdp_md, data_end)),
  3891. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  3892. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  3893. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 5),
  3894. BPF_LD_MAP_FD(BPF_REG_1, 0),
  3895. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  3896. BPF_MOV64_IMM(BPF_REG_4, 0),
  3897. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3898. BPF_FUNC_map_update_elem),
  3899. BPF_MOV64_IMM(BPF_REG_0, 0),
  3900. BPF_EXIT_INSN(),
  3901. },
  3902. .fixup_map1 = { 5 },
  3903. .result_unpriv = ACCEPT,
  3904. .result = ACCEPT,
  3905. .prog_type = BPF_PROG_TYPE_XDP,
  3906. },
  3907. {
  3908. "helper access to packet: test2, unchecked packet_ptr",
  3909. .insns = {
  3910. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3911. offsetof(struct xdp_md, data)),
  3912. BPF_LD_MAP_FD(BPF_REG_1, 0),
  3913. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3914. BPF_FUNC_map_lookup_elem),
  3915. BPF_MOV64_IMM(BPF_REG_0, 0),
  3916. BPF_EXIT_INSN(),
  3917. },
  3918. .fixup_map1 = { 1 },
  3919. .result = REJECT,
  3920. .errstr = "invalid access to packet",
  3921. .prog_type = BPF_PROG_TYPE_XDP,
  3922. },
  3923. {
  3924. "helper access to packet: test3, variable add",
  3925. .insns = {
  3926. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3927. offsetof(struct xdp_md, data)),
  3928. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3929. offsetof(struct xdp_md, data_end)),
  3930. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  3931. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8),
  3932. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 10),
  3933. BPF_LDX_MEM(BPF_B, BPF_REG_5, BPF_REG_2, 0),
  3934. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  3935. BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_5),
  3936. BPF_MOV64_REG(BPF_REG_5, BPF_REG_4),
  3937. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 8),
  3938. BPF_JMP_REG(BPF_JGT, BPF_REG_5, BPF_REG_3, 4),
  3939. BPF_LD_MAP_FD(BPF_REG_1, 0),
  3940. BPF_MOV64_REG(BPF_REG_2, BPF_REG_4),
  3941. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3942. BPF_FUNC_map_lookup_elem),
  3943. BPF_MOV64_IMM(BPF_REG_0, 0),
  3944. BPF_EXIT_INSN(),
  3945. },
  3946. .fixup_map1 = { 11 },
  3947. .result = ACCEPT,
  3948. .prog_type = BPF_PROG_TYPE_XDP,
  3949. },
  3950. {
  3951. "helper access to packet: test4, packet_ptr with bad range",
  3952. .insns = {
  3953. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3954. offsetof(struct xdp_md, data)),
  3955. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3956. offsetof(struct xdp_md, data_end)),
  3957. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  3958. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 4),
  3959. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 2),
  3960. BPF_MOV64_IMM(BPF_REG_0, 0),
  3961. BPF_EXIT_INSN(),
  3962. BPF_LD_MAP_FD(BPF_REG_1, 0),
  3963. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3964. BPF_FUNC_map_lookup_elem),
  3965. BPF_MOV64_IMM(BPF_REG_0, 0),
  3966. BPF_EXIT_INSN(),
  3967. },
  3968. .fixup_map1 = { 7 },
  3969. .result = REJECT,
  3970. .errstr = "invalid access to packet",
  3971. .prog_type = BPF_PROG_TYPE_XDP,
  3972. },
  3973. {
  3974. "helper access to packet: test5, packet_ptr with too short range",
  3975. .insns = {
  3976. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3977. offsetof(struct xdp_md, data)),
  3978. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  3979. offsetof(struct xdp_md, data_end)),
  3980. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
  3981. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  3982. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 7),
  3983. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 3),
  3984. BPF_LD_MAP_FD(BPF_REG_1, 0),
  3985. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  3986. BPF_FUNC_map_lookup_elem),
  3987. BPF_MOV64_IMM(BPF_REG_0, 0),
  3988. BPF_EXIT_INSN(),
  3989. },
  3990. .fixup_map1 = { 6 },
  3991. .result = REJECT,
  3992. .errstr = "invalid access to packet",
  3993. .prog_type = BPF_PROG_TYPE_XDP,
  3994. },
  3995. {
  3996. "helper access to packet: test6, cls valid packet_ptr range",
  3997. .insns = {
  3998. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  3999. offsetof(struct __sk_buff, data)),
  4000. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4001. offsetof(struct __sk_buff, data_end)),
  4002. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  4003. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  4004. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 5),
  4005. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4006. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  4007. BPF_MOV64_IMM(BPF_REG_4, 0),
  4008. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4009. BPF_FUNC_map_update_elem),
  4010. BPF_MOV64_IMM(BPF_REG_0, 0),
  4011. BPF_EXIT_INSN(),
  4012. },
  4013. .fixup_map1 = { 5 },
  4014. .result = ACCEPT,
  4015. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4016. },
  4017. {
  4018. "helper access to packet: test7, cls unchecked packet_ptr",
  4019. .insns = {
  4020. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4021. offsetof(struct __sk_buff, data)),
  4022. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4023. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4024. BPF_FUNC_map_lookup_elem),
  4025. BPF_MOV64_IMM(BPF_REG_0, 0),
  4026. BPF_EXIT_INSN(),
  4027. },
  4028. .fixup_map1 = { 1 },
  4029. .result = REJECT,
  4030. .errstr = "invalid access to packet",
  4031. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4032. },
  4033. {
  4034. "helper access to packet: test8, cls variable add",
  4035. .insns = {
  4036. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4037. offsetof(struct __sk_buff, data)),
  4038. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4039. offsetof(struct __sk_buff, data_end)),
  4040. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  4041. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8),
  4042. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 10),
  4043. BPF_LDX_MEM(BPF_B, BPF_REG_5, BPF_REG_2, 0),
  4044. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  4045. BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_5),
  4046. BPF_MOV64_REG(BPF_REG_5, BPF_REG_4),
  4047. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 8),
  4048. BPF_JMP_REG(BPF_JGT, BPF_REG_5, BPF_REG_3, 4),
  4049. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4050. BPF_MOV64_REG(BPF_REG_2, BPF_REG_4),
  4051. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4052. BPF_FUNC_map_lookup_elem),
  4053. BPF_MOV64_IMM(BPF_REG_0, 0),
  4054. BPF_EXIT_INSN(),
  4055. },
  4056. .fixup_map1 = { 11 },
  4057. .result = ACCEPT,
  4058. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4059. },
  4060. {
  4061. "helper access to packet: test9, cls packet_ptr with bad range",
  4062. .insns = {
  4063. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4064. offsetof(struct __sk_buff, data)),
  4065. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4066. offsetof(struct __sk_buff, data_end)),
  4067. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  4068. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 4),
  4069. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 2),
  4070. BPF_MOV64_IMM(BPF_REG_0, 0),
  4071. BPF_EXIT_INSN(),
  4072. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4073. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4074. BPF_FUNC_map_lookup_elem),
  4075. BPF_MOV64_IMM(BPF_REG_0, 0),
  4076. BPF_EXIT_INSN(),
  4077. },
  4078. .fixup_map1 = { 7 },
  4079. .result = REJECT,
  4080. .errstr = "invalid access to packet",
  4081. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4082. },
  4083. {
  4084. "helper access to packet: test10, cls packet_ptr with too short range",
  4085. .insns = {
  4086. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4087. offsetof(struct __sk_buff, data)),
  4088. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4089. offsetof(struct __sk_buff, data_end)),
  4090. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
  4091. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  4092. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 7),
  4093. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 3),
  4094. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4095. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4096. BPF_FUNC_map_lookup_elem),
  4097. BPF_MOV64_IMM(BPF_REG_0, 0),
  4098. BPF_EXIT_INSN(),
  4099. },
  4100. .fixup_map1 = { 6 },
  4101. .result = REJECT,
  4102. .errstr = "invalid access to packet",
  4103. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4104. },
  4105. {
  4106. "helper access to packet: test11, cls unsuitable helper 1",
  4107. .insns = {
  4108. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4109. offsetof(struct __sk_buff, data)),
  4110. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4111. offsetof(struct __sk_buff, data_end)),
  4112. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4113. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  4114. BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 7),
  4115. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_7, 4),
  4116. BPF_MOV64_IMM(BPF_REG_2, 0),
  4117. BPF_MOV64_IMM(BPF_REG_4, 42),
  4118. BPF_MOV64_IMM(BPF_REG_5, 0),
  4119. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4120. BPF_FUNC_skb_store_bytes),
  4121. BPF_MOV64_IMM(BPF_REG_0, 0),
  4122. BPF_EXIT_INSN(),
  4123. },
  4124. .result = REJECT,
  4125. .errstr = "helper access to the packet",
  4126. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4127. },
  4128. {
  4129. "helper access to packet: test12, cls unsuitable helper 2",
  4130. .insns = {
  4131. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4132. offsetof(struct __sk_buff, data)),
  4133. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4134. offsetof(struct __sk_buff, data_end)),
  4135. BPF_MOV64_REG(BPF_REG_3, BPF_REG_6),
  4136. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 8),
  4137. BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_7, 3),
  4138. BPF_MOV64_IMM(BPF_REG_2, 0),
  4139. BPF_MOV64_IMM(BPF_REG_4, 4),
  4140. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4141. BPF_FUNC_skb_load_bytes),
  4142. BPF_MOV64_IMM(BPF_REG_0, 0),
  4143. BPF_EXIT_INSN(),
  4144. },
  4145. .result = REJECT,
  4146. .errstr = "helper access to the packet",
  4147. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4148. },
  4149. {
  4150. "helper access to packet: test13, cls helper ok",
  4151. .insns = {
  4152. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4153. offsetof(struct __sk_buff, data)),
  4154. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4155. offsetof(struct __sk_buff, data_end)),
  4156. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4157. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4158. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4159. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4160. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4161. BPF_MOV64_IMM(BPF_REG_2, 4),
  4162. BPF_MOV64_IMM(BPF_REG_3, 0),
  4163. BPF_MOV64_IMM(BPF_REG_4, 0),
  4164. BPF_MOV64_IMM(BPF_REG_5, 0),
  4165. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4166. BPF_FUNC_csum_diff),
  4167. BPF_MOV64_IMM(BPF_REG_0, 0),
  4168. BPF_EXIT_INSN(),
  4169. },
  4170. .result = ACCEPT,
  4171. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4172. },
  4173. {
  4174. "helper access to packet: test14, cls helper ok sub",
  4175. .insns = {
  4176. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4177. offsetof(struct __sk_buff, data)),
  4178. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4179. offsetof(struct __sk_buff, data_end)),
  4180. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4181. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4182. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4183. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4184. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 4),
  4185. BPF_MOV64_IMM(BPF_REG_2, 4),
  4186. BPF_MOV64_IMM(BPF_REG_3, 0),
  4187. BPF_MOV64_IMM(BPF_REG_4, 0),
  4188. BPF_MOV64_IMM(BPF_REG_5, 0),
  4189. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4190. BPF_FUNC_csum_diff),
  4191. BPF_MOV64_IMM(BPF_REG_0, 0),
  4192. BPF_EXIT_INSN(),
  4193. },
  4194. .result = ACCEPT,
  4195. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4196. },
  4197. {
  4198. "helper access to packet: test15, cls helper fail sub",
  4199. .insns = {
  4200. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4201. offsetof(struct __sk_buff, data)),
  4202. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4203. offsetof(struct __sk_buff, data_end)),
  4204. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4205. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4206. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4207. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4208. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 12),
  4209. BPF_MOV64_IMM(BPF_REG_2, 4),
  4210. BPF_MOV64_IMM(BPF_REG_3, 0),
  4211. BPF_MOV64_IMM(BPF_REG_4, 0),
  4212. BPF_MOV64_IMM(BPF_REG_5, 0),
  4213. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4214. BPF_FUNC_csum_diff),
  4215. BPF_MOV64_IMM(BPF_REG_0, 0),
  4216. BPF_EXIT_INSN(),
  4217. },
  4218. .result = REJECT,
  4219. .errstr = "invalid access to packet",
  4220. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4221. },
  4222. {
  4223. "helper access to packet: test16, cls helper fail range 1",
  4224. .insns = {
  4225. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4226. offsetof(struct __sk_buff, data)),
  4227. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4228. offsetof(struct __sk_buff, data_end)),
  4229. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4230. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4231. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4232. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4233. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4234. BPF_MOV64_IMM(BPF_REG_2, 8),
  4235. BPF_MOV64_IMM(BPF_REG_3, 0),
  4236. BPF_MOV64_IMM(BPF_REG_4, 0),
  4237. BPF_MOV64_IMM(BPF_REG_5, 0),
  4238. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4239. BPF_FUNC_csum_diff),
  4240. BPF_MOV64_IMM(BPF_REG_0, 0),
  4241. BPF_EXIT_INSN(),
  4242. },
  4243. .result = REJECT,
  4244. .errstr = "invalid access to packet",
  4245. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4246. },
  4247. {
  4248. "helper access to packet: test17, cls helper fail range 2",
  4249. .insns = {
  4250. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4251. offsetof(struct __sk_buff, data)),
  4252. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4253. offsetof(struct __sk_buff, data_end)),
  4254. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4255. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4256. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4257. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4258. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4259. BPF_MOV64_IMM(BPF_REG_2, -9),
  4260. BPF_MOV64_IMM(BPF_REG_3, 0),
  4261. BPF_MOV64_IMM(BPF_REG_4, 0),
  4262. BPF_MOV64_IMM(BPF_REG_5, 0),
  4263. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4264. BPF_FUNC_csum_diff),
  4265. BPF_MOV64_IMM(BPF_REG_0, 0),
  4266. BPF_EXIT_INSN(),
  4267. },
  4268. .result = REJECT,
  4269. .errstr = "R2 min value is negative",
  4270. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4271. },
  4272. {
  4273. "helper access to packet: test18, cls helper fail range 3",
  4274. .insns = {
  4275. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4276. offsetof(struct __sk_buff, data)),
  4277. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4278. offsetof(struct __sk_buff, data_end)),
  4279. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4280. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4281. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4282. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4283. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4284. BPF_MOV64_IMM(BPF_REG_2, ~0),
  4285. BPF_MOV64_IMM(BPF_REG_3, 0),
  4286. BPF_MOV64_IMM(BPF_REG_4, 0),
  4287. BPF_MOV64_IMM(BPF_REG_5, 0),
  4288. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4289. BPF_FUNC_csum_diff),
  4290. BPF_MOV64_IMM(BPF_REG_0, 0),
  4291. BPF_EXIT_INSN(),
  4292. },
  4293. .result = REJECT,
  4294. .errstr = "R2 min value is negative",
  4295. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4296. },
  4297. {
  4298. "helper access to packet: test19, cls helper range zero",
  4299. .insns = {
  4300. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4301. offsetof(struct __sk_buff, data)),
  4302. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4303. offsetof(struct __sk_buff, data_end)),
  4304. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4305. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4306. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4307. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4308. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4309. BPF_MOV64_IMM(BPF_REG_2, 0),
  4310. BPF_MOV64_IMM(BPF_REG_3, 0),
  4311. BPF_MOV64_IMM(BPF_REG_4, 0),
  4312. BPF_MOV64_IMM(BPF_REG_5, 0),
  4313. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4314. BPF_FUNC_csum_diff),
  4315. BPF_MOV64_IMM(BPF_REG_0, 0),
  4316. BPF_EXIT_INSN(),
  4317. },
  4318. .result = ACCEPT,
  4319. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4320. },
  4321. {
  4322. "helper access to packet: test20, pkt end as input",
  4323. .insns = {
  4324. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4325. offsetof(struct __sk_buff, data)),
  4326. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4327. offsetof(struct __sk_buff, data_end)),
  4328. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4329. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4330. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4331. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4332. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  4333. BPF_MOV64_IMM(BPF_REG_2, 4),
  4334. BPF_MOV64_IMM(BPF_REG_3, 0),
  4335. BPF_MOV64_IMM(BPF_REG_4, 0),
  4336. BPF_MOV64_IMM(BPF_REG_5, 0),
  4337. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4338. BPF_FUNC_csum_diff),
  4339. BPF_MOV64_IMM(BPF_REG_0, 0),
  4340. BPF_EXIT_INSN(),
  4341. },
  4342. .result = REJECT,
  4343. .errstr = "R1 type=pkt_end expected=fp",
  4344. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4345. },
  4346. {
  4347. "helper access to packet: test21, wrong reg",
  4348. .insns = {
  4349. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  4350. offsetof(struct __sk_buff, data)),
  4351. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  4352. offsetof(struct __sk_buff, data_end)),
  4353. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 1),
  4354. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  4355. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 7),
  4356. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_7, 6),
  4357. BPF_MOV64_IMM(BPF_REG_2, 4),
  4358. BPF_MOV64_IMM(BPF_REG_3, 0),
  4359. BPF_MOV64_IMM(BPF_REG_4, 0),
  4360. BPF_MOV64_IMM(BPF_REG_5, 0),
  4361. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4362. BPF_FUNC_csum_diff),
  4363. BPF_MOV64_IMM(BPF_REG_0, 0),
  4364. BPF_EXIT_INSN(),
  4365. },
  4366. .result = REJECT,
  4367. .errstr = "invalid access to packet",
  4368. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  4369. },
  4370. {
  4371. "valid map access into an array with a constant",
  4372. .insns = {
  4373. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4374. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4375. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4376. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4377. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4378. BPF_FUNC_map_lookup_elem),
  4379. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4380. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4381. offsetof(struct test_val, foo)),
  4382. BPF_EXIT_INSN(),
  4383. },
  4384. .fixup_map2 = { 3 },
  4385. .errstr_unpriv = "R0 leaks addr",
  4386. .result_unpriv = REJECT,
  4387. .result = ACCEPT,
  4388. },
  4389. {
  4390. "valid map access into an array with a register",
  4391. .insns = {
  4392. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4393. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4394. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4395. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4396. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4397. BPF_FUNC_map_lookup_elem),
  4398. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  4399. BPF_MOV64_IMM(BPF_REG_1, 4),
  4400. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
  4401. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4402. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4403. offsetof(struct test_val, foo)),
  4404. BPF_EXIT_INSN(),
  4405. },
  4406. .fixup_map2 = { 3 },
  4407. .errstr_unpriv = "R0 leaks addr",
  4408. .result_unpriv = REJECT,
  4409. .result = ACCEPT,
  4410. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4411. },
  4412. {
  4413. "valid map access into an array with a variable",
  4414. .insns = {
  4415. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4416. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4417. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4418. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4419. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4420. BPF_FUNC_map_lookup_elem),
  4421. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  4422. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  4423. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, MAX_ENTRIES, 3),
  4424. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
  4425. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4426. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4427. offsetof(struct test_val, foo)),
  4428. BPF_EXIT_INSN(),
  4429. },
  4430. .fixup_map2 = { 3 },
  4431. .errstr_unpriv = "R0 leaks addr",
  4432. .result_unpriv = REJECT,
  4433. .result = ACCEPT,
  4434. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4435. },
  4436. {
  4437. "valid map access into an array with a signed variable",
  4438. .insns = {
  4439. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4440. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4441. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4442. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4443. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4444. BPF_FUNC_map_lookup_elem),
  4445. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  4446. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  4447. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 0xffffffff, 1),
  4448. BPF_MOV32_IMM(BPF_REG_1, 0),
  4449. BPF_MOV32_IMM(BPF_REG_2, MAX_ENTRIES),
  4450. BPF_JMP_REG(BPF_JSGT, BPF_REG_2, BPF_REG_1, 1),
  4451. BPF_MOV32_IMM(BPF_REG_1, 0),
  4452. BPF_ALU32_IMM(BPF_LSH, BPF_REG_1, 2),
  4453. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4454. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4455. offsetof(struct test_val, foo)),
  4456. BPF_EXIT_INSN(),
  4457. },
  4458. .fixup_map2 = { 3 },
  4459. .errstr_unpriv = "R0 leaks addr",
  4460. .result_unpriv = REJECT,
  4461. .result = ACCEPT,
  4462. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4463. },
  4464. {
  4465. "invalid map access into an array with a constant",
  4466. .insns = {
  4467. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4468. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4469. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4470. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4471. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4472. BPF_FUNC_map_lookup_elem),
  4473. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4474. BPF_ST_MEM(BPF_DW, BPF_REG_0, (MAX_ENTRIES + 1) << 2,
  4475. offsetof(struct test_val, foo)),
  4476. BPF_EXIT_INSN(),
  4477. },
  4478. .fixup_map2 = { 3 },
  4479. .errstr = "invalid access to map value, value_size=48 off=48 size=8",
  4480. .result = REJECT,
  4481. },
  4482. {
  4483. "invalid map access into an array with a register",
  4484. .insns = {
  4485. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4486. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4487. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4488. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4489. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4490. BPF_FUNC_map_lookup_elem),
  4491. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  4492. BPF_MOV64_IMM(BPF_REG_1, MAX_ENTRIES + 1),
  4493. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
  4494. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4495. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4496. offsetof(struct test_val, foo)),
  4497. BPF_EXIT_INSN(),
  4498. },
  4499. .fixup_map2 = { 3 },
  4500. .errstr = "R0 min value is outside of the array range",
  4501. .result = REJECT,
  4502. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4503. },
  4504. {
  4505. "invalid map access into an array with a variable",
  4506. .insns = {
  4507. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4508. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4509. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4510. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4511. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4512. BPF_FUNC_map_lookup_elem),
  4513. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  4514. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  4515. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
  4516. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4517. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4518. offsetof(struct test_val, foo)),
  4519. BPF_EXIT_INSN(),
  4520. },
  4521. .fixup_map2 = { 3 },
  4522. .errstr = "R0 unbounded memory access, make sure to bounds check any array access into a map",
  4523. .result = REJECT,
  4524. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4525. },
  4526. {
  4527. "invalid map access into an array with no floor check",
  4528. .insns = {
  4529. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4530. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4531. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4532. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4533. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4534. BPF_FUNC_map_lookup_elem),
  4535. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  4536. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
  4537. BPF_MOV32_IMM(BPF_REG_2, MAX_ENTRIES),
  4538. BPF_JMP_REG(BPF_JSGT, BPF_REG_2, BPF_REG_1, 1),
  4539. BPF_MOV32_IMM(BPF_REG_1, 0),
  4540. BPF_ALU32_IMM(BPF_LSH, BPF_REG_1, 2),
  4541. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4542. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4543. offsetof(struct test_val, foo)),
  4544. BPF_EXIT_INSN(),
  4545. },
  4546. .fixup_map2 = { 3 },
  4547. .errstr_unpriv = "R0 leaks addr",
  4548. .errstr = "R0 unbounded memory access",
  4549. .result_unpriv = REJECT,
  4550. .result = REJECT,
  4551. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4552. },
  4553. {
  4554. "invalid map access into an array with a invalid max check",
  4555. .insns = {
  4556. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4557. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4558. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4559. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4560. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4561. BPF_FUNC_map_lookup_elem),
  4562. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  4563. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  4564. BPF_MOV32_IMM(BPF_REG_2, MAX_ENTRIES + 1),
  4565. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 1),
  4566. BPF_MOV32_IMM(BPF_REG_1, 0),
  4567. BPF_ALU32_IMM(BPF_LSH, BPF_REG_1, 2),
  4568. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4569. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  4570. offsetof(struct test_val, foo)),
  4571. BPF_EXIT_INSN(),
  4572. },
  4573. .fixup_map2 = { 3 },
  4574. .errstr_unpriv = "R0 leaks addr",
  4575. .errstr = "invalid access to map value, value_size=48 off=44 size=8",
  4576. .result_unpriv = REJECT,
  4577. .result = REJECT,
  4578. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4579. },
  4580. {
  4581. "invalid map access into an array with a invalid max check",
  4582. .insns = {
  4583. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4584. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4585. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4586. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4587. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4588. BPF_FUNC_map_lookup_elem),
  4589. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 10),
  4590. BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
  4591. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4592. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4593. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4594. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4595. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4596. BPF_FUNC_map_lookup_elem),
  4597. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  4598. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_8),
  4599. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0,
  4600. offsetof(struct test_val, foo)),
  4601. BPF_EXIT_INSN(),
  4602. },
  4603. .fixup_map2 = { 3, 11 },
  4604. .errstr = "R0 pointer += pointer",
  4605. .result = REJECT,
  4606. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4607. },
  4608. {
  4609. "multiple registers share map_lookup_elem result",
  4610. .insns = {
  4611. BPF_MOV64_IMM(BPF_REG_1, 10),
  4612. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  4613. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4614. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4615. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4616. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4617. BPF_FUNC_map_lookup_elem),
  4618. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  4619. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4620. BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
  4621. BPF_EXIT_INSN(),
  4622. },
  4623. .fixup_map1 = { 4 },
  4624. .result = ACCEPT,
  4625. .prog_type = BPF_PROG_TYPE_SCHED_CLS
  4626. },
  4627. {
  4628. "alu ops on ptr_to_map_value_or_null, 1",
  4629. .insns = {
  4630. BPF_MOV64_IMM(BPF_REG_1, 10),
  4631. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  4632. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4633. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4634. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4635. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4636. BPF_FUNC_map_lookup_elem),
  4637. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  4638. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -2),
  4639. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 2),
  4640. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4641. BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
  4642. BPF_EXIT_INSN(),
  4643. },
  4644. .fixup_map1 = { 4 },
  4645. .errstr = "R4 pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL",
  4646. .result = REJECT,
  4647. .prog_type = BPF_PROG_TYPE_SCHED_CLS
  4648. },
  4649. {
  4650. "alu ops on ptr_to_map_value_or_null, 2",
  4651. .insns = {
  4652. BPF_MOV64_IMM(BPF_REG_1, 10),
  4653. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  4654. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4655. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4656. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4657. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4658. BPF_FUNC_map_lookup_elem),
  4659. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  4660. BPF_ALU64_IMM(BPF_AND, BPF_REG_4, -1),
  4661. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4662. BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
  4663. BPF_EXIT_INSN(),
  4664. },
  4665. .fixup_map1 = { 4 },
  4666. .errstr = "R4 pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL",
  4667. .result = REJECT,
  4668. .prog_type = BPF_PROG_TYPE_SCHED_CLS
  4669. },
  4670. {
  4671. "alu ops on ptr_to_map_value_or_null, 3",
  4672. .insns = {
  4673. BPF_MOV64_IMM(BPF_REG_1, 10),
  4674. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  4675. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4676. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4677. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4678. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4679. BPF_FUNC_map_lookup_elem),
  4680. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  4681. BPF_ALU64_IMM(BPF_LSH, BPF_REG_4, 1),
  4682. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4683. BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
  4684. BPF_EXIT_INSN(),
  4685. },
  4686. .fixup_map1 = { 4 },
  4687. .errstr = "R4 pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL",
  4688. .result = REJECT,
  4689. .prog_type = BPF_PROG_TYPE_SCHED_CLS
  4690. },
  4691. {
  4692. "invalid memory access with multiple map_lookup_elem calls",
  4693. .insns = {
  4694. BPF_MOV64_IMM(BPF_REG_1, 10),
  4695. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  4696. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4697. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4698. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4699. BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
  4700. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  4701. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4702. BPF_FUNC_map_lookup_elem),
  4703. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  4704. BPF_MOV64_REG(BPF_REG_1, BPF_REG_8),
  4705. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  4706. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4707. BPF_FUNC_map_lookup_elem),
  4708. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4709. BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
  4710. BPF_EXIT_INSN(),
  4711. },
  4712. .fixup_map1 = { 4 },
  4713. .result = REJECT,
  4714. .errstr = "R4 !read_ok",
  4715. .prog_type = BPF_PROG_TYPE_SCHED_CLS
  4716. },
  4717. {
  4718. "valid indirect map_lookup_elem access with 2nd lookup in branch",
  4719. .insns = {
  4720. BPF_MOV64_IMM(BPF_REG_1, 10),
  4721. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1, -8),
  4722. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4723. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4724. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4725. BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
  4726. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  4727. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4728. BPF_FUNC_map_lookup_elem),
  4729. BPF_MOV64_IMM(BPF_REG_2, 10),
  4730. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 0, 3),
  4731. BPF_MOV64_REG(BPF_REG_1, BPF_REG_8),
  4732. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  4733. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  4734. BPF_FUNC_map_lookup_elem),
  4735. BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
  4736. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  4737. BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
  4738. BPF_EXIT_INSN(),
  4739. },
  4740. .fixup_map1 = { 4 },
  4741. .result = ACCEPT,
  4742. .prog_type = BPF_PROG_TYPE_SCHED_CLS
  4743. },
  4744. {
  4745. "invalid map access from else condition",
  4746. .insns = {
  4747. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  4748. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  4749. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  4750. BPF_LD_MAP_FD(BPF_REG_1, 0),
  4751. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  4752. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  4753. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  4754. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, MAX_ENTRIES-1, 1),
  4755. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
  4756. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
  4757. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  4758. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, offsetof(struct test_val, foo)),
  4759. BPF_EXIT_INSN(),
  4760. },
  4761. .fixup_map2 = { 3 },
  4762. .errstr = "R0 unbounded memory access",
  4763. .result = REJECT,
  4764. .errstr_unpriv = "R0 leaks addr",
  4765. .result_unpriv = REJECT,
  4766. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  4767. },
  4768. {
  4769. "constant register |= constant should keep constant type",
  4770. .insns = {
  4771. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  4772. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -48),
  4773. BPF_MOV64_IMM(BPF_REG_2, 34),
  4774. BPF_ALU64_IMM(BPF_OR, BPF_REG_2, 13),
  4775. BPF_MOV64_IMM(BPF_REG_3, 0),
  4776. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  4777. BPF_EXIT_INSN(),
  4778. },
  4779. .result = ACCEPT,
  4780. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  4781. },
  4782. {
  4783. "constant register |= constant should not bypass stack boundary checks",
  4784. .insns = {
  4785. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  4786. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -48),
  4787. BPF_MOV64_IMM(BPF_REG_2, 34),
  4788. BPF_ALU64_IMM(BPF_OR, BPF_REG_2, 24),
  4789. BPF_MOV64_IMM(BPF_REG_3, 0),
  4790. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  4791. BPF_EXIT_INSN(),
  4792. },
  4793. .errstr = "invalid stack type R1 off=-48 access_size=58",
  4794. .result = REJECT,
  4795. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  4796. },
  4797. {
  4798. "constant register |= constant register should keep constant type",
  4799. .insns = {
  4800. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  4801. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -48),
  4802. BPF_MOV64_IMM(BPF_REG_2, 34),
  4803. BPF_MOV64_IMM(BPF_REG_4, 13),
  4804. BPF_ALU64_REG(BPF_OR, BPF_REG_2, BPF_REG_4),
  4805. BPF_MOV64_IMM(BPF_REG_3, 0),
  4806. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  4807. BPF_EXIT_INSN(),
  4808. },
  4809. .result = ACCEPT,
  4810. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  4811. },
  4812. {
  4813. "constant register |= constant register should not bypass stack boundary checks",
  4814. .insns = {
  4815. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  4816. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -48),
  4817. BPF_MOV64_IMM(BPF_REG_2, 34),
  4818. BPF_MOV64_IMM(BPF_REG_4, 24),
  4819. BPF_ALU64_REG(BPF_OR, BPF_REG_2, BPF_REG_4),
  4820. BPF_MOV64_IMM(BPF_REG_3, 0),
  4821. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  4822. BPF_EXIT_INSN(),
  4823. },
  4824. .errstr = "invalid stack type R1 off=-48 access_size=58",
  4825. .result = REJECT,
  4826. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  4827. },
  4828. {
  4829. "invalid direct packet write for LWT_IN",
  4830. .insns = {
  4831. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4832. offsetof(struct __sk_buff, data)),
  4833. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4834. offsetof(struct __sk_buff, data_end)),
  4835. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4836. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4837. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  4838. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  4839. BPF_MOV64_IMM(BPF_REG_0, 0),
  4840. BPF_EXIT_INSN(),
  4841. },
  4842. .errstr = "cannot write into packet",
  4843. .result = REJECT,
  4844. .prog_type = BPF_PROG_TYPE_LWT_IN,
  4845. },
  4846. {
  4847. "invalid direct packet write for LWT_OUT",
  4848. .insns = {
  4849. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4850. offsetof(struct __sk_buff, data)),
  4851. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4852. offsetof(struct __sk_buff, data_end)),
  4853. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4854. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4855. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  4856. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  4857. BPF_MOV64_IMM(BPF_REG_0, 0),
  4858. BPF_EXIT_INSN(),
  4859. },
  4860. .errstr = "cannot write into packet",
  4861. .result = REJECT,
  4862. .prog_type = BPF_PROG_TYPE_LWT_OUT,
  4863. },
  4864. {
  4865. "direct packet write for LWT_XMIT",
  4866. .insns = {
  4867. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4868. offsetof(struct __sk_buff, data)),
  4869. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4870. offsetof(struct __sk_buff, data_end)),
  4871. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4872. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4873. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  4874. BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  4875. BPF_MOV64_IMM(BPF_REG_0, 0),
  4876. BPF_EXIT_INSN(),
  4877. },
  4878. .result = ACCEPT,
  4879. .prog_type = BPF_PROG_TYPE_LWT_XMIT,
  4880. },
  4881. {
  4882. "direct packet read for LWT_IN",
  4883. .insns = {
  4884. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4885. offsetof(struct __sk_buff, data)),
  4886. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4887. offsetof(struct __sk_buff, data_end)),
  4888. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4889. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4890. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  4891. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  4892. BPF_MOV64_IMM(BPF_REG_0, 0),
  4893. BPF_EXIT_INSN(),
  4894. },
  4895. .result = ACCEPT,
  4896. .prog_type = BPF_PROG_TYPE_LWT_IN,
  4897. },
  4898. {
  4899. "direct packet read for LWT_OUT",
  4900. .insns = {
  4901. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4902. offsetof(struct __sk_buff, data)),
  4903. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4904. offsetof(struct __sk_buff, data_end)),
  4905. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4906. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4907. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  4908. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  4909. BPF_MOV64_IMM(BPF_REG_0, 0),
  4910. BPF_EXIT_INSN(),
  4911. },
  4912. .result = ACCEPT,
  4913. .prog_type = BPF_PROG_TYPE_LWT_OUT,
  4914. },
  4915. {
  4916. "direct packet read for LWT_XMIT",
  4917. .insns = {
  4918. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4919. offsetof(struct __sk_buff, data)),
  4920. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4921. offsetof(struct __sk_buff, data_end)),
  4922. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4923. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4924. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  4925. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  4926. BPF_MOV64_IMM(BPF_REG_0, 0),
  4927. BPF_EXIT_INSN(),
  4928. },
  4929. .result = ACCEPT,
  4930. .prog_type = BPF_PROG_TYPE_LWT_XMIT,
  4931. },
  4932. {
  4933. "overlapping checks for direct packet access",
  4934. .insns = {
  4935. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  4936. offsetof(struct __sk_buff, data)),
  4937. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  4938. offsetof(struct __sk_buff, data_end)),
  4939. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  4940. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  4941. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 4),
  4942. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  4943. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 6),
  4944. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  4945. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_2, 6),
  4946. BPF_MOV64_IMM(BPF_REG_0, 0),
  4947. BPF_EXIT_INSN(),
  4948. },
  4949. .result = ACCEPT,
  4950. .prog_type = BPF_PROG_TYPE_LWT_XMIT,
  4951. },
  4952. {
  4953. "invalid access of tc_classid for LWT_IN",
  4954. .insns = {
  4955. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  4956. offsetof(struct __sk_buff, tc_classid)),
  4957. BPF_EXIT_INSN(),
  4958. },
  4959. .result = REJECT,
  4960. .errstr = "invalid bpf_context access",
  4961. },
  4962. {
  4963. "invalid access of tc_classid for LWT_OUT",
  4964. .insns = {
  4965. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  4966. offsetof(struct __sk_buff, tc_classid)),
  4967. BPF_EXIT_INSN(),
  4968. },
  4969. .result = REJECT,
  4970. .errstr = "invalid bpf_context access",
  4971. },
  4972. {
  4973. "invalid access of tc_classid for LWT_XMIT",
  4974. .insns = {
  4975. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  4976. offsetof(struct __sk_buff, tc_classid)),
  4977. BPF_EXIT_INSN(),
  4978. },
  4979. .result = REJECT,
  4980. .errstr = "invalid bpf_context access",
  4981. },
  4982. {
  4983. "leak pointer into ctx 1",
  4984. .insns = {
  4985. BPF_MOV64_IMM(BPF_REG_0, 0),
  4986. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  4987. offsetof(struct __sk_buff, cb[0])),
  4988. BPF_LD_MAP_FD(BPF_REG_2, 0),
  4989. BPF_STX_XADD(BPF_DW, BPF_REG_1, BPF_REG_2,
  4990. offsetof(struct __sk_buff, cb[0])),
  4991. BPF_EXIT_INSN(),
  4992. },
  4993. .fixup_map1 = { 2 },
  4994. .errstr_unpriv = "R2 leaks addr into mem",
  4995. .result_unpriv = REJECT,
  4996. .result = REJECT,
  4997. .errstr = "BPF_XADD stores into R1 context is not allowed",
  4998. },
  4999. {
  5000. "leak pointer into ctx 2",
  5001. .insns = {
  5002. BPF_MOV64_IMM(BPF_REG_0, 0),
  5003. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0,
  5004. offsetof(struct __sk_buff, cb[0])),
  5005. BPF_STX_XADD(BPF_DW, BPF_REG_1, BPF_REG_10,
  5006. offsetof(struct __sk_buff, cb[0])),
  5007. BPF_EXIT_INSN(),
  5008. },
  5009. .errstr_unpriv = "R10 leaks addr into mem",
  5010. .result_unpriv = REJECT,
  5011. .result = REJECT,
  5012. .errstr = "BPF_XADD stores into R1 context is not allowed",
  5013. },
  5014. {
  5015. "leak pointer into ctx 3",
  5016. .insns = {
  5017. BPF_MOV64_IMM(BPF_REG_0, 0),
  5018. BPF_LD_MAP_FD(BPF_REG_2, 0),
  5019. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2,
  5020. offsetof(struct __sk_buff, cb[0])),
  5021. BPF_EXIT_INSN(),
  5022. },
  5023. .fixup_map1 = { 1 },
  5024. .errstr_unpriv = "R2 leaks addr into ctx",
  5025. .result_unpriv = REJECT,
  5026. .result = ACCEPT,
  5027. },
  5028. {
  5029. "leak pointer into map val",
  5030. .insns = {
  5031. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  5032. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  5033. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5034. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5035. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5036. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  5037. BPF_FUNC_map_lookup_elem),
  5038. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  5039. BPF_MOV64_IMM(BPF_REG_3, 0),
  5040. BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
  5041. BPF_STX_XADD(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  5042. BPF_MOV64_IMM(BPF_REG_0, 0),
  5043. BPF_EXIT_INSN(),
  5044. },
  5045. .fixup_map1 = { 4 },
  5046. .errstr_unpriv = "R6 leaks addr into mem",
  5047. .result_unpriv = REJECT,
  5048. .result = ACCEPT,
  5049. },
  5050. {
  5051. "helper access to map: full range",
  5052. .insns = {
  5053. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5054. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5055. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5056. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5057. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5058. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5059. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5060. BPF_MOV64_IMM(BPF_REG_2, sizeof(struct test_val)),
  5061. BPF_MOV64_IMM(BPF_REG_3, 0),
  5062. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5063. BPF_EXIT_INSN(),
  5064. },
  5065. .fixup_map2 = { 3 },
  5066. .result = ACCEPT,
  5067. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5068. },
  5069. {
  5070. "helper access to map: partial range",
  5071. .insns = {
  5072. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5073. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5074. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5075. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5076. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5077. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5078. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5079. BPF_MOV64_IMM(BPF_REG_2, 8),
  5080. BPF_MOV64_IMM(BPF_REG_3, 0),
  5081. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5082. BPF_EXIT_INSN(),
  5083. },
  5084. .fixup_map2 = { 3 },
  5085. .result = ACCEPT,
  5086. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5087. },
  5088. {
  5089. "helper access to map: empty range",
  5090. .insns = {
  5091. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5092. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5093. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5094. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5095. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5096. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  5097. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5098. BPF_MOV64_IMM(BPF_REG_2, 0),
  5099. BPF_EMIT_CALL(BPF_FUNC_trace_printk),
  5100. BPF_EXIT_INSN(),
  5101. },
  5102. .fixup_map2 = { 3 },
  5103. .errstr = "invalid access to map value, value_size=48 off=0 size=0",
  5104. .result = REJECT,
  5105. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5106. },
  5107. {
  5108. "helper access to map: out-of-bound range",
  5109. .insns = {
  5110. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5111. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5112. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5113. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5114. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5115. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5116. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5117. BPF_MOV64_IMM(BPF_REG_2, sizeof(struct test_val) + 8),
  5118. BPF_MOV64_IMM(BPF_REG_3, 0),
  5119. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5120. BPF_EXIT_INSN(),
  5121. },
  5122. .fixup_map2 = { 3 },
  5123. .errstr = "invalid access to map value, value_size=48 off=0 size=56",
  5124. .result = REJECT,
  5125. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5126. },
  5127. {
  5128. "helper access to map: negative range",
  5129. .insns = {
  5130. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5131. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5132. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5133. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5134. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5135. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5136. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5137. BPF_MOV64_IMM(BPF_REG_2, -8),
  5138. BPF_MOV64_IMM(BPF_REG_3, 0),
  5139. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5140. BPF_EXIT_INSN(),
  5141. },
  5142. .fixup_map2 = { 3 },
  5143. .errstr = "R2 min value is negative",
  5144. .result = REJECT,
  5145. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5146. },
  5147. {
  5148. "helper access to adjusted map (via const imm): full range",
  5149. .insns = {
  5150. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5151. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5152. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5153. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5154. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5155. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5156. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5157. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  5158. offsetof(struct test_val, foo)),
  5159. BPF_MOV64_IMM(BPF_REG_2,
  5160. sizeof(struct test_val) -
  5161. offsetof(struct test_val, foo)),
  5162. BPF_MOV64_IMM(BPF_REG_3, 0),
  5163. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5164. BPF_EXIT_INSN(),
  5165. },
  5166. .fixup_map2 = { 3 },
  5167. .result = ACCEPT,
  5168. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5169. },
  5170. {
  5171. "helper access to adjusted map (via const imm): partial range",
  5172. .insns = {
  5173. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5174. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5175. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5176. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5177. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5178. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5179. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5180. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  5181. offsetof(struct test_val, foo)),
  5182. BPF_MOV64_IMM(BPF_REG_2, 8),
  5183. BPF_MOV64_IMM(BPF_REG_3, 0),
  5184. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5185. BPF_EXIT_INSN(),
  5186. },
  5187. .fixup_map2 = { 3 },
  5188. .result = ACCEPT,
  5189. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5190. },
  5191. {
  5192. "helper access to adjusted map (via const imm): empty range",
  5193. .insns = {
  5194. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5195. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5196. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5197. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5198. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5199. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5200. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5201. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  5202. offsetof(struct test_val, foo)),
  5203. BPF_MOV64_IMM(BPF_REG_2, 0),
  5204. BPF_EMIT_CALL(BPF_FUNC_trace_printk),
  5205. BPF_EXIT_INSN(),
  5206. },
  5207. .fixup_map2 = { 3 },
  5208. .errstr = "invalid access to map value, value_size=48 off=4 size=0",
  5209. .result = REJECT,
  5210. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5211. },
  5212. {
  5213. "helper access to adjusted map (via const imm): out-of-bound range",
  5214. .insns = {
  5215. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5216. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5217. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5218. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5219. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5220. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5221. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5222. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  5223. offsetof(struct test_val, foo)),
  5224. BPF_MOV64_IMM(BPF_REG_2,
  5225. sizeof(struct test_val) -
  5226. offsetof(struct test_val, foo) + 8),
  5227. BPF_MOV64_IMM(BPF_REG_3, 0),
  5228. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5229. BPF_EXIT_INSN(),
  5230. },
  5231. .fixup_map2 = { 3 },
  5232. .errstr = "invalid access to map value, value_size=48 off=4 size=52",
  5233. .result = REJECT,
  5234. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5235. },
  5236. {
  5237. "helper access to adjusted map (via const imm): negative range (> adjustment)",
  5238. .insns = {
  5239. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5240. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5241. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5242. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5243. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5244. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5245. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5246. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  5247. offsetof(struct test_val, foo)),
  5248. BPF_MOV64_IMM(BPF_REG_2, -8),
  5249. BPF_MOV64_IMM(BPF_REG_3, 0),
  5250. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5251. BPF_EXIT_INSN(),
  5252. },
  5253. .fixup_map2 = { 3 },
  5254. .errstr = "R2 min value is negative",
  5255. .result = REJECT,
  5256. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5257. },
  5258. {
  5259. "helper access to adjusted map (via const imm): negative range (< adjustment)",
  5260. .insns = {
  5261. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5262. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5263. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5264. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5265. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5266. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5267. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5268. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  5269. offsetof(struct test_val, foo)),
  5270. BPF_MOV64_IMM(BPF_REG_2, -1),
  5271. BPF_MOV64_IMM(BPF_REG_3, 0),
  5272. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5273. BPF_EXIT_INSN(),
  5274. },
  5275. .fixup_map2 = { 3 },
  5276. .errstr = "R2 min value is negative",
  5277. .result = REJECT,
  5278. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5279. },
  5280. {
  5281. "helper access to adjusted map (via const reg): full range",
  5282. .insns = {
  5283. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5284. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5285. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5286. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5287. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5288. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5289. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5290. BPF_MOV64_IMM(BPF_REG_3,
  5291. offsetof(struct test_val, foo)),
  5292. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5293. BPF_MOV64_IMM(BPF_REG_2,
  5294. sizeof(struct test_val) -
  5295. offsetof(struct test_val, foo)),
  5296. BPF_MOV64_IMM(BPF_REG_3, 0),
  5297. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5298. BPF_EXIT_INSN(),
  5299. },
  5300. .fixup_map2 = { 3 },
  5301. .result = ACCEPT,
  5302. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5303. },
  5304. {
  5305. "helper access to adjusted map (via const reg): partial range",
  5306. .insns = {
  5307. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5308. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5309. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5310. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5311. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5312. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5313. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5314. BPF_MOV64_IMM(BPF_REG_3,
  5315. offsetof(struct test_val, foo)),
  5316. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5317. BPF_MOV64_IMM(BPF_REG_2, 8),
  5318. BPF_MOV64_IMM(BPF_REG_3, 0),
  5319. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5320. BPF_EXIT_INSN(),
  5321. },
  5322. .fixup_map2 = { 3 },
  5323. .result = ACCEPT,
  5324. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5325. },
  5326. {
  5327. "helper access to adjusted map (via const reg): empty range",
  5328. .insns = {
  5329. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5330. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5331. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5332. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5333. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5334. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5335. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5336. BPF_MOV64_IMM(BPF_REG_3, 0),
  5337. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5338. BPF_MOV64_IMM(BPF_REG_2, 0),
  5339. BPF_EMIT_CALL(BPF_FUNC_trace_printk),
  5340. BPF_EXIT_INSN(),
  5341. },
  5342. .fixup_map2 = { 3 },
  5343. .errstr = "R1 min value is outside of the array range",
  5344. .result = REJECT,
  5345. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5346. },
  5347. {
  5348. "helper access to adjusted map (via const reg): out-of-bound range",
  5349. .insns = {
  5350. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5351. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5352. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5353. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5354. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5355. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5356. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5357. BPF_MOV64_IMM(BPF_REG_3,
  5358. offsetof(struct test_val, foo)),
  5359. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5360. BPF_MOV64_IMM(BPF_REG_2,
  5361. sizeof(struct test_val) -
  5362. offsetof(struct test_val, foo) + 8),
  5363. BPF_MOV64_IMM(BPF_REG_3, 0),
  5364. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5365. BPF_EXIT_INSN(),
  5366. },
  5367. .fixup_map2 = { 3 },
  5368. .errstr = "invalid access to map value, value_size=48 off=4 size=52",
  5369. .result = REJECT,
  5370. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5371. },
  5372. {
  5373. "helper access to adjusted map (via const reg): negative range (> adjustment)",
  5374. .insns = {
  5375. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5376. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5377. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5378. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5379. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5380. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5381. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5382. BPF_MOV64_IMM(BPF_REG_3,
  5383. offsetof(struct test_val, foo)),
  5384. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5385. BPF_MOV64_IMM(BPF_REG_2, -8),
  5386. BPF_MOV64_IMM(BPF_REG_3, 0),
  5387. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5388. BPF_EXIT_INSN(),
  5389. },
  5390. .fixup_map2 = { 3 },
  5391. .errstr = "R2 min value is negative",
  5392. .result = REJECT,
  5393. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5394. },
  5395. {
  5396. "helper access to adjusted map (via const reg): negative range (< adjustment)",
  5397. .insns = {
  5398. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5399. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5400. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5401. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5402. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5403. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5404. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5405. BPF_MOV64_IMM(BPF_REG_3,
  5406. offsetof(struct test_val, foo)),
  5407. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5408. BPF_MOV64_IMM(BPF_REG_2, -1),
  5409. BPF_MOV64_IMM(BPF_REG_3, 0),
  5410. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5411. BPF_EXIT_INSN(),
  5412. },
  5413. .fixup_map2 = { 3 },
  5414. .errstr = "R2 min value is negative",
  5415. .result = REJECT,
  5416. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5417. },
  5418. {
  5419. "helper access to adjusted map (via variable): full range",
  5420. .insns = {
  5421. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5422. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5423. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5424. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5425. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5426. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  5427. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5428. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5429. BPF_JMP_IMM(BPF_JGT, BPF_REG_3,
  5430. offsetof(struct test_val, foo), 4),
  5431. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5432. BPF_MOV64_IMM(BPF_REG_2,
  5433. sizeof(struct test_val) -
  5434. offsetof(struct test_val, foo)),
  5435. BPF_MOV64_IMM(BPF_REG_3, 0),
  5436. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5437. BPF_EXIT_INSN(),
  5438. },
  5439. .fixup_map2 = { 3 },
  5440. .result = ACCEPT,
  5441. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5442. },
  5443. {
  5444. "helper access to adjusted map (via variable): partial range",
  5445. .insns = {
  5446. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5447. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5448. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5449. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5450. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5451. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  5452. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5453. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5454. BPF_JMP_IMM(BPF_JGT, BPF_REG_3,
  5455. offsetof(struct test_val, foo), 4),
  5456. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5457. BPF_MOV64_IMM(BPF_REG_2, 8),
  5458. BPF_MOV64_IMM(BPF_REG_3, 0),
  5459. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5460. BPF_EXIT_INSN(),
  5461. },
  5462. .fixup_map2 = { 3 },
  5463. .result = ACCEPT,
  5464. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5465. },
  5466. {
  5467. "helper access to adjusted map (via variable): empty range",
  5468. .insns = {
  5469. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5470. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5471. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5472. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5473. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5474. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5475. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5476. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5477. BPF_JMP_IMM(BPF_JGT, BPF_REG_3,
  5478. offsetof(struct test_val, foo), 3),
  5479. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5480. BPF_MOV64_IMM(BPF_REG_2, 0),
  5481. BPF_EMIT_CALL(BPF_FUNC_trace_printk),
  5482. BPF_EXIT_INSN(),
  5483. },
  5484. .fixup_map2 = { 3 },
  5485. .errstr = "R1 min value is outside of the array range",
  5486. .result = REJECT,
  5487. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5488. },
  5489. {
  5490. "helper access to adjusted map (via variable): no max check",
  5491. .insns = {
  5492. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5493. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5494. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5495. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5496. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5497. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5498. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5499. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5500. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5501. BPF_MOV64_IMM(BPF_REG_2, 1),
  5502. BPF_MOV64_IMM(BPF_REG_3, 0),
  5503. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5504. BPF_EXIT_INSN(),
  5505. },
  5506. .fixup_map2 = { 3 },
  5507. .errstr = "R1 unbounded memory access",
  5508. .result = REJECT,
  5509. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5510. },
  5511. {
  5512. "helper access to adjusted map (via variable): wrong max check",
  5513. .insns = {
  5514. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5515. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5516. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5517. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5518. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5519. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  5520. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5521. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5522. BPF_JMP_IMM(BPF_JGT, BPF_REG_3,
  5523. offsetof(struct test_val, foo), 4),
  5524. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5525. BPF_MOV64_IMM(BPF_REG_2,
  5526. sizeof(struct test_val) -
  5527. offsetof(struct test_val, foo) + 1),
  5528. BPF_MOV64_IMM(BPF_REG_3, 0),
  5529. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  5530. BPF_EXIT_INSN(),
  5531. },
  5532. .fixup_map2 = { 3 },
  5533. .errstr = "invalid access to map value, value_size=48 off=4 size=45",
  5534. .result = REJECT,
  5535. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5536. },
  5537. {
  5538. "helper access to map: bounds check using <, good access",
  5539. .insns = {
  5540. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5541. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5542. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5543. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5544. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5545. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5546. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5547. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5548. BPF_JMP_IMM(BPF_JLT, BPF_REG_3, 32, 2),
  5549. BPF_MOV64_IMM(BPF_REG_0, 0),
  5550. BPF_EXIT_INSN(),
  5551. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5552. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5553. BPF_MOV64_IMM(BPF_REG_0, 0),
  5554. BPF_EXIT_INSN(),
  5555. },
  5556. .fixup_map2 = { 3 },
  5557. .result = ACCEPT,
  5558. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5559. },
  5560. {
  5561. "helper access to map: bounds check using <, bad access",
  5562. .insns = {
  5563. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5564. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5565. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5566. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5567. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5568. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5569. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5570. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5571. BPF_JMP_IMM(BPF_JLT, BPF_REG_3, 32, 4),
  5572. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5573. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5574. BPF_MOV64_IMM(BPF_REG_0, 0),
  5575. BPF_EXIT_INSN(),
  5576. BPF_MOV64_IMM(BPF_REG_0, 0),
  5577. BPF_EXIT_INSN(),
  5578. },
  5579. .fixup_map2 = { 3 },
  5580. .result = REJECT,
  5581. .errstr = "R1 unbounded memory access",
  5582. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5583. },
  5584. {
  5585. "helper access to map: bounds check using <=, good access",
  5586. .insns = {
  5587. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5588. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5589. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5590. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5591. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5592. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5593. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5594. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5595. BPF_JMP_IMM(BPF_JLE, BPF_REG_3, 32, 2),
  5596. BPF_MOV64_IMM(BPF_REG_0, 0),
  5597. BPF_EXIT_INSN(),
  5598. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5599. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5600. BPF_MOV64_IMM(BPF_REG_0, 0),
  5601. BPF_EXIT_INSN(),
  5602. },
  5603. .fixup_map2 = { 3 },
  5604. .result = ACCEPT,
  5605. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5606. },
  5607. {
  5608. "helper access to map: bounds check using <=, bad access",
  5609. .insns = {
  5610. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5611. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5612. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5613. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5614. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5615. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5616. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5617. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5618. BPF_JMP_IMM(BPF_JLE, BPF_REG_3, 32, 4),
  5619. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5620. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5621. BPF_MOV64_IMM(BPF_REG_0, 0),
  5622. BPF_EXIT_INSN(),
  5623. BPF_MOV64_IMM(BPF_REG_0, 0),
  5624. BPF_EXIT_INSN(),
  5625. },
  5626. .fixup_map2 = { 3 },
  5627. .result = REJECT,
  5628. .errstr = "R1 unbounded memory access",
  5629. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5630. },
  5631. {
  5632. "helper access to map: bounds check using s<, good access",
  5633. .insns = {
  5634. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5635. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5636. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5637. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5638. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5639. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5640. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5641. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5642. BPF_JMP_IMM(BPF_JSLT, BPF_REG_3, 32, 2),
  5643. BPF_MOV64_IMM(BPF_REG_0, 0),
  5644. BPF_EXIT_INSN(),
  5645. BPF_JMP_IMM(BPF_JSLT, BPF_REG_3, 0, -3),
  5646. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5647. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5648. BPF_MOV64_IMM(BPF_REG_0, 0),
  5649. BPF_EXIT_INSN(),
  5650. },
  5651. .fixup_map2 = { 3 },
  5652. .result = ACCEPT,
  5653. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5654. },
  5655. {
  5656. "helper access to map: bounds check using s<, good access 2",
  5657. .insns = {
  5658. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5659. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5660. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5661. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5662. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5663. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5664. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5665. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5666. BPF_JMP_IMM(BPF_JSLT, BPF_REG_3, 32, 2),
  5667. BPF_MOV64_IMM(BPF_REG_0, 0),
  5668. BPF_EXIT_INSN(),
  5669. BPF_JMP_IMM(BPF_JSLT, BPF_REG_3, -3, -3),
  5670. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5671. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5672. BPF_MOV64_IMM(BPF_REG_0, 0),
  5673. BPF_EXIT_INSN(),
  5674. },
  5675. .fixup_map2 = { 3 },
  5676. .result = ACCEPT,
  5677. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5678. },
  5679. {
  5680. "helper access to map: bounds check using s<, bad access",
  5681. .insns = {
  5682. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5683. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5684. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5685. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5686. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5687. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5688. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5689. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0),
  5690. BPF_JMP_IMM(BPF_JSLT, BPF_REG_3, 32, 2),
  5691. BPF_MOV64_IMM(BPF_REG_0, 0),
  5692. BPF_EXIT_INSN(),
  5693. BPF_JMP_IMM(BPF_JSLT, BPF_REG_3, -3, -3),
  5694. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5695. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5696. BPF_MOV64_IMM(BPF_REG_0, 0),
  5697. BPF_EXIT_INSN(),
  5698. },
  5699. .fixup_map2 = { 3 },
  5700. .result = REJECT,
  5701. .errstr = "R1 min value is negative",
  5702. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5703. },
  5704. {
  5705. "helper access to map: bounds check using s<=, good access",
  5706. .insns = {
  5707. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5708. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5709. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5710. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5711. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5712. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5713. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5714. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5715. BPF_JMP_IMM(BPF_JSLE, BPF_REG_3, 32, 2),
  5716. BPF_MOV64_IMM(BPF_REG_0, 0),
  5717. BPF_EXIT_INSN(),
  5718. BPF_JMP_IMM(BPF_JSLE, BPF_REG_3, 0, -3),
  5719. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5720. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5721. BPF_MOV64_IMM(BPF_REG_0, 0),
  5722. BPF_EXIT_INSN(),
  5723. },
  5724. .fixup_map2 = { 3 },
  5725. .result = ACCEPT,
  5726. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5727. },
  5728. {
  5729. "helper access to map: bounds check using s<=, good access 2",
  5730. .insns = {
  5731. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5732. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5733. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5734. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5735. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5736. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5737. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5738. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5739. BPF_JMP_IMM(BPF_JSLE, BPF_REG_3, 32, 2),
  5740. BPF_MOV64_IMM(BPF_REG_0, 0),
  5741. BPF_EXIT_INSN(),
  5742. BPF_JMP_IMM(BPF_JSLE, BPF_REG_3, -3, -3),
  5743. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5744. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5745. BPF_MOV64_IMM(BPF_REG_0, 0),
  5746. BPF_EXIT_INSN(),
  5747. },
  5748. .fixup_map2 = { 3 },
  5749. .result = ACCEPT,
  5750. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5751. },
  5752. {
  5753. "helper access to map: bounds check using s<=, bad access",
  5754. .insns = {
  5755. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5756. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5757. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5758. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5759. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5760. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5761. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  5762. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0),
  5763. BPF_JMP_IMM(BPF_JSLE, BPF_REG_3, 32, 2),
  5764. BPF_MOV64_IMM(BPF_REG_0, 0),
  5765. BPF_EXIT_INSN(),
  5766. BPF_JMP_IMM(BPF_JSLE, BPF_REG_3, -3, -3),
  5767. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_3),
  5768. BPF_ST_MEM(BPF_B, BPF_REG_1, 0, 0),
  5769. BPF_MOV64_IMM(BPF_REG_0, 0),
  5770. BPF_EXIT_INSN(),
  5771. },
  5772. .fixup_map2 = { 3 },
  5773. .result = REJECT,
  5774. .errstr = "R1 min value is negative",
  5775. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5776. },
  5777. {
  5778. "map lookup helper access to map",
  5779. .insns = {
  5780. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5781. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5782. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5783. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5784. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5785. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  5786. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5787. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5788. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5789. BPF_EXIT_INSN(),
  5790. },
  5791. .fixup_map3 = { 3, 8 },
  5792. .result = ACCEPT,
  5793. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5794. },
  5795. {
  5796. "map update helper access to map",
  5797. .insns = {
  5798. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5799. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5800. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5801. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5802. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5803. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5804. BPF_MOV64_IMM(BPF_REG_4, 0),
  5805. BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
  5806. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5807. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5808. BPF_EMIT_CALL(BPF_FUNC_map_update_elem),
  5809. BPF_EXIT_INSN(),
  5810. },
  5811. .fixup_map3 = { 3, 10 },
  5812. .result = ACCEPT,
  5813. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5814. },
  5815. {
  5816. "map update helper access to map: wrong size",
  5817. .insns = {
  5818. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5819. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5820. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5821. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5822. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5823. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5824. BPF_MOV64_IMM(BPF_REG_4, 0),
  5825. BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
  5826. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5827. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5828. BPF_EMIT_CALL(BPF_FUNC_map_update_elem),
  5829. BPF_EXIT_INSN(),
  5830. },
  5831. .fixup_map1 = { 3 },
  5832. .fixup_map3 = { 10 },
  5833. .result = REJECT,
  5834. .errstr = "invalid access to map value, value_size=8 off=0 size=16",
  5835. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5836. },
  5837. {
  5838. "map helper access to adjusted map (via const imm)",
  5839. .insns = {
  5840. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5841. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5842. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5843. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5844. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5845. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5846. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5847. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2,
  5848. offsetof(struct other_val, bar)),
  5849. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5850. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5851. BPF_EXIT_INSN(),
  5852. },
  5853. .fixup_map3 = { 3, 9 },
  5854. .result = ACCEPT,
  5855. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5856. },
  5857. {
  5858. "map helper access to adjusted map (via const imm): out-of-bound 1",
  5859. .insns = {
  5860. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5861. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5862. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5863. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5864. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5865. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5866. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5867. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2,
  5868. sizeof(struct other_val) - 4),
  5869. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5870. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5871. BPF_EXIT_INSN(),
  5872. },
  5873. .fixup_map3 = { 3, 9 },
  5874. .result = REJECT,
  5875. .errstr = "invalid access to map value, value_size=16 off=12 size=8",
  5876. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5877. },
  5878. {
  5879. "map helper access to adjusted map (via const imm): out-of-bound 2",
  5880. .insns = {
  5881. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5882. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5883. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5884. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5885. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5886. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  5887. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5888. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  5889. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5890. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5891. BPF_EXIT_INSN(),
  5892. },
  5893. .fixup_map3 = { 3, 9 },
  5894. .result = REJECT,
  5895. .errstr = "invalid access to map value, value_size=16 off=-4 size=8",
  5896. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5897. },
  5898. {
  5899. "map helper access to adjusted map (via const reg)",
  5900. .insns = {
  5901. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5902. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5903. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5904. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5905. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5906. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5907. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5908. BPF_MOV64_IMM(BPF_REG_3,
  5909. offsetof(struct other_val, bar)),
  5910. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
  5911. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5912. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5913. BPF_EXIT_INSN(),
  5914. },
  5915. .fixup_map3 = { 3, 10 },
  5916. .result = ACCEPT,
  5917. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5918. },
  5919. {
  5920. "map helper access to adjusted map (via const reg): out-of-bound 1",
  5921. .insns = {
  5922. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5923. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5924. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5925. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5926. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5927. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5928. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5929. BPF_MOV64_IMM(BPF_REG_3,
  5930. sizeof(struct other_val) - 4),
  5931. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
  5932. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5933. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5934. BPF_EXIT_INSN(),
  5935. },
  5936. .fixup_map3 = { 3, 10 },
  5937. .result = REJECT,
  5938. .errstr = "invalid access to map value, value_size=16 off=12 size=8",
  5939. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5940. },
  5941. {
  5942. "map helper access to adjusted map (via const reg): out-of-bound 2",
  5943. .insns = {
  5944. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5945. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5946. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5947. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5948. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5949. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5950. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5951. BPF_MOV64_IMM(BPF_REG_3, -4),
  5952. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
  5953. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5954. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5955. BPF_EXIT_INSN(),
  5956. },
  5957. .fixup_map3 = { 3, 10 },
  5958. .result = REJECT,
  5959. .errstr = "invalid access to map value, value_size=16 off=-4 size=8",
  5960. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5961. },
  5962. {
  5963. "map helper access to adjusted map (via variable)",
  5964. .insns = {
  5965. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5966. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5967. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5968. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5969. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5970. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  5971. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5972. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5973. BPF_JMP_IMM(BPF_JGT, BPF_REG_3,
  5974. offsetof(struct other_val, bar), 4),
  5975. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
  5976. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5977. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5978. BPF_EXIT_INSN(),
  5979. },
  5980. .fixup_map3 = { 3, 11 },
  5981. .result = ACCEPT,
  5982. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  5983. },
  5984. {
  5985. "map helper access to adjusted map (via variable): no max check",
  5986. .insns = {
  5987. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  5988. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  5989. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  5990. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5991. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5992. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  5993. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  5994. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  5995. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
  5996. BPF_LD_MAP_FD(BPF_REG_1, 0),
  5997. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  5998. BPF_EXIT_INSN(),
  5999. },
  6000. .fixup_map3 = { 3, 10 },
  6001. .result = REJECT,
  6002. .errstr = "R2 unbounded memory access, make sure to bounds check any array access into a map",
  6003. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6004. },
  6005. {
  6006. "map helper access to adjusted map (via variable): wrong max check",
  6007. .insns = {
  6008. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6009. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6010. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6011. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6012. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6013. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  6014. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  6015. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0),
  6016. BPF_JMP_IMM(BPF_JGT, BPF_REG_3,
  6017. offsetof(struct other_val, bar) + 1, 4),
  6018. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_3),
  6019. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6020. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6021. BPF_EXIT_INSN(),
  6022. },
  6023. .fixup_map3 = { 3, 11 },
  6024. .result = REJECT,
  6025. .errstr = "invalid access to map value, value_size=16 off=9 size=8",
  6026. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6027. },
  6028. {
  6029. "map element value is preserved across register spilling",
  6030. .insns = {
  6031. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6032. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6033. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6034. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6035. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6036. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  6037. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 42),
  6038. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6039. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -184),
  6040. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
  6041. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_1, 0),
  6042. BPF_ST_MEM(BPF_DW, BPF_REG_3, 0, 42),
  6043. BPF_EXIT_INSN(),
  6044. },
  6045. .fixup_map2 = { 3 },
  6046. .errstr_unpriv = "R0 leaks addr",
  6047. .result = ACCEPT,
  6048. .result_unpriv = REJECT,
  6049. },
  6050. {
  6051. "map element value or null is marked on register spilling",
  6052. .insns = {
  6053. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6054. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6055. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6056. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6057. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6058. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6059. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -152),
  6060. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
  6061. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  6062. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_1, 0),
  6063. BPF_ST_MEM(BPF_DW, BPF_REG_3, 0, 42),
  6064. BPF_EXIT_INSN(),
  6065. },
  6066. .fixup_map2 = { 3 },
  6067. .errstr_unpriv = "R0 leaks addr",
  6068. .result = ACCEPT,
  6069. .result_unpriv = REJECT,
  6070. },
  6071. {
  6072. "map element value store of cleared call register",
  6073. .insns = {
  6074. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6075. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6076. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6077. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6078. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6079. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  6080. BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  6081. BPF_EXIT_INSN(),
  6082. },
  6083. .fixup_map2 = { 3 },
  6084. .errstr_unpriv = "R1 !read_ok",
  6085. .errstr = "R1 !read_ok",
  6086. .result = REJECT,
  6087. .result_unpriv = REJECT,
  6088. },
  6089. {
  6090. "map element value with unaligned store",
  6091. .insns = {
  6092. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6093. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6094. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6095. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6096. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6097. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 17),
  6098. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 3),
  6099. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 42),
  6100. BPF_ST_MEM(BPF_DW, BPF_REG_0, 2, 43),
  6101. BPF_ST_MEM(BPF_DW, BPF_REG_0, -2, 44),
  6102. BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
  6103. BPF_ST_MEM(BPF_DW, BPF_REG_8, 0, 32),
  6104. BPF_ST_MEM(BPF_DW, BPF_REG_8, 2, 33),
  6105. BPF_ST_MEM(BPF_DW, BPF_REG_8, -2, 34),
  6106. BPF_ALU64_IMM(BPF_ADD, BPF_REG_8, 5),
  6107. BPF_ST_MEM(BPF_DW, BPF_REG_8, 0, 22),
  6108. BPF_ST_MEM(BPF_DW, BPF_REG_8, 4, 23),
  6109. BPF_ST_MEM(BPF_DW, BPF_REG_8, -7, 24),
  6110. BPF_MOV64_REG(BPF_REG_7, BPF_REG_8),
  6111. BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 3),
  6112. BPF_ST_MEM(BPF_DW, BPF_REG_7, 0, 22),
  6113. BPF_ST_MEM(BPF_DW, BPF_REG_7, 4, 23),
  6114. BPF_ST_MEM(BPF_DW, BPF_REG_7, -4, 24),
  6115. BPF_EXIT_INSN(),
  6116. },
  6117. .fixup_map2 = { 3 },
  6118. .errstr_unpriv = "R0 leaks addr",
  6119. .result = ACCEPT,
  6120. .result_unpriv = REJECT,
  6121. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  6122. },
  6123. {
  6124. "map element value with unaligned load",
  6125. .insns = {
  6126. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6127. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6128. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6129. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6130. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6131. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 11),
  6132. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  6133. BPF_JMP_IMM(BPF_JGE, BPF_REG_1, MAX_ENTRIES, 9),
  6134. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 3),
  6135. BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  6136. BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 2),
  6137. BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
  6138. BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 0),
  6139. BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 2),
  6140. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 5),
  6141. BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  6142. BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 4),
  6143. BPF_EXIT_INSN(),
  6144. },
  6145. .fixup_map2 = { 3 },
  6146. .errstr_unpriv = "R0 leaks addr",
  6147. .result = ACCEPT,
  6148. .result_unpriv = REJECT,
  6149. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  6150. },
  6151. {
  6152. "map element value illegal alu op, 1",
  6153. .insns = {
  6154. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6155. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6156. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6157. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6158. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6159. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  6160. BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 8),
  6161. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 22),
  6162. BPF_EXIT_INSN(),
  6163. },
  6164. .fixup_map2 = { 3 },
  6165. .errstr = "R0 bitwise operator &= on pointer",
  6166. .result = REJECT,
  6167. },
  6168. {
  6169. "map element value illegal alu op, 2",
  6170. .insns = {
  6171. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6172. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6173. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6174. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6175. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6176. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  6177. BPF_ALU32_IMM(BPF_ADD, BPF_REG_0, 0),
  6178. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 22),
  6179. BPF_EXIT_INSN(),
  6180. },
  6181. .fixup_map2 = { 3 },
  6182. .errstr = "R0 32-bit pointer arithmetic prohibited",
  6183. .result = REJECT,
  6184. },
  6185. {
  6186. "map element value illegal alu op, 3",
  6187. .insns = {
  6188. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6189. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6190. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6191. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6192. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6193. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  6194. BPF_ALU64_IMM(BPF_DIV, BPF_REG_0, 42),
  6195. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 22),
  6196. BPF_EXIT_INSN(),
  6197. },
  6198. .fixup_map2 = { 3 },
  6199. .errstr = "R0 pointer arithmetic with /= operator",
  6200. .result = REJECT,
  6201. },
  6202. {
  6203. "map element value illegal alu op, 4",
  6204. .insns = {
  6205. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6206. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6207. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6208. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6209. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6210. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  6211. BPF_ENDIAN(BPF_FROM_BE, BPF_REG_0, 64),
  6212. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 22),
  6213. BPF_EXIT_INSN(),
  6214. },
  6215. .fixup_map2 = { 3 },
  6216. .errstr_unpriv = "R0 pointer arithmetic prohibited",
  6217. .errstr = "invalid mem access 'inv'",
  6218. .result = REJECT,
  6219. .result_unpriv = REJECT,
  6220. },
  6221. {
  6222. "map element value illegal alu op, 5",
  6223. .insns = {
  6224. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6225. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6226. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6227. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6228. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6229. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  6230. BPF_MOV64_IMM(BPF_REG_3, 4096),
  6231. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6232. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6233. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  6234. BPF_STX_XADD(BPF_DW, BPF_REG_2, BPF_REG_3, 0),
  6235. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0),
  6236. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 22),
  6237. BPF_EXIT_INSN(),
  6238. },
  6239. .fixup_map2 = { 3 },
  6240. .errstr = "R0 invalid mem access 'inv'",
  6241. .result = REJECT,
  6242. },
  6243. {
  6244. "map element value is preserved across register spilling",
  6245. .insns = {
  6246. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6247. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6248. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6249. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6250. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6251. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  6252. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0,
  6253. offsetof(struct test_val, foo)),
  6254. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 42),
  6255. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6256. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -184),
  6257. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
  6258. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_1, 0),
  6259. BPF_ST_MEM(BPF_DW, BPF_REG_3, 0, 42),
  6260. BPF_EXIT_INSN(),
  6261. },
  6262. .fixup_map2 = { 3 },
  6263. .errstr_unpriv = "R0 leaks addr",
  6264. .result = ACCEPT,
  6265. .result_unpriv = REJECT,
  6266. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  6267. },
  6268. {
  6269. "helper access to variable memory: stack, bitwise AND + JMP, correct bounds",
  6270. .insns = {
  6271. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6272. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6273. BPF_MOV64_IMM(BPF_REG_0, 0),
  6274. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -64),
  6275. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -56),
  6276. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -48),
  6277. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -40),
  6278. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -32),
  6279. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -24),
  6280. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -16),
  6281. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  6282. BPF_MOV64_IMM(BPF_REG_2, 16),
  6283. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6284. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6285. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 64),
  6286. BPF_MOV64_IMM(BPF_REG_4, 0),
  6287. BPF_JMP_REG(BPF_JGE, BPF_REG_4, BPF_REG_2, 2),
  6288. BPF_MOV64_IMM(BPF_REG_3, 0),
  6289. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6290. BPF_MOV64_IMM(BPF_REG_0, 0),
  6291. BPF_EXIT_INSN(),
  6292. },
  6293. .result = ACCEPT,
  6294. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6295. },
  6296. {
  6297. "helper access to variable memory: stack, bitwise AND, zero included",
  6298. .insns = {
  6299. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6300. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6301. BPF_MOV64_IMM(BPF_REG_2, 16),
  6302. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6303. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6304. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 64),
  6305. BPF_MOV64_IMM(BPF_REG_3, 0),
  6306. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6307. BPF_EXIT_INSN(),
  6308. },
  6309. .errstr = "invalid indirect read from stack off -64+0 size 64",
  6310. .result = REJECT,
  6311. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6312. },
  6313. {
  6314. "helper access to variable memory: stack, bitwise AND + JMP, wrong max",
  6315. .insns = {
  6316. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6317. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6318. BPF_MOV64_IMM(BPF_REG_2, 16),
  6319. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6320. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6321. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 65),
  6322. BPF_MOV64_IMM(BPF_REG_4, 0),
  6323. BPF_JMP_REG(BPF_JGE, BPF_REG_4, BPF_REG_2, 2),
  6324. BPF_MOV64_IMM(BPF_REG_3, 0),
  6325. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6326. BPF_MOV64_IMM(BPF_REG_0, 0),
  6327. BPF_EXIT_INSN(),
  6328. },
  6329. .errstr = "invalid stack type R1 off=-64 access_size=65",
  6330. .result = REJECT,
  6331. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6332. },
  6333. {
  6334. "helper access to variable memory: stack, JMP, correct bounds",
  6335. .insns = {
  6336. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6337. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6338. BPF_MOV64_IMM(BPF_REG_0, 0),
  6339. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -64),
  6340. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -56),
  6341. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -48),
  6342. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -40),
  6343. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -32),
  6344. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -24),
  6345. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -16),
  6346. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  6347. BPF_MOV64_IMM(BPF_REG_2, 16),
  6348. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6349. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6350. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 64, 4),
  6351. BPF_MOV64_IMM(BPF_REG_4, 0),
  6352. BPF_JMP_REG(BPF_JGE, BPF_REG_4, BPF_REG_2, 2),
  6353. BPF_MOV64_IMM(BPF_REG_3, 0),
  6354. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6355. BPF_MOV64_IMM(BPF_REG_0, 0),
  6356. BPF_EXIT_INSN(),
  6357. },
  6358. .result = ACCEPT,
  6359. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6360. },
  6361. {
  6362. "helper access to variable memory: stack, JMP (signed), correct bounds",
  6363. .insns = {
  6364. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6365. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6366. BPF_MOV64_IMM(BPF_REG_0, 0),
  6367. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -64),
  6368. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -56),
  6369. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -48),
  6370. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -40),
  6371. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -32),
  6372. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -24),
  6373. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -16),
  6374. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  6375. BPF_MOV64_IMM(BPF_REG_2, 16),
  6376. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6377. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6378. BPF_JMP_IMM(BPF_JSGT, BPF_REG_2, 64, 4),
  6379. BPF_MOV64_IMM(BPF_REG_4, 0),
  6380. BPF_JMP_REG(BPF_JSGE, BPF_REG_4, BPF_REG_2, 2),
  6381. BPF_MOV64_IMM(BPF_REG_3, 0),
  6382. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6383. BPF_MOV64_IMM(BPF_REG_0, 0),
  6384. BPF_EXIT_INSN(),
  6385. },
  6386. .result = ACCEPT,
  6387. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6388. },
  6389. {
  6390. "helper access to variable memory: stack, JMP, bounds + offset",
  6391. .insns = {
  6392. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6393. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6394. BPF_MOV64_IMM(BPF_REG_2, 16),
  6395. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6396. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6397. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 64, 5),
  6398. BPF_MOV64_IMM(BPF_REG_4, 0),
  6399. BPF_JMP_REG(BPF_JGE, BPF_REG_4, BPF_REG_2, 3),
  6400. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
  6401. BPF_MOV64_IMM(BPF_REG_3, 0),
  6402. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6403. BPF_MOV64_IMM(BPF_REG_0, 0),
  6404. BPF_EXIT_INSN(),
  6405. },
  6406. .errstr = "invalid stack type R1 off=-64 access_size=65",
  6407. .result = REJECT,
  6408. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6409. },
  6410. {
  6411. "helper access to variable memory: stack, JMP, wrong max",
  6412. .insns = {
  6413. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6414. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6415. BPF_MOV64_IMM(BPF_REG_2, 16),
  6416. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6417. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6418. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 65, 4),
  6419. BPF_MOV64_IMM(BPF_REG_4, 0),
  6420. BPF_JMP_REG(BPF_JGE, BPF_REG_4, BPF_REG_2, 2),
  6421. BPF_MOV64_IMM(BPF_REG_3, 0),
  6422. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6423. BPF_MOV64_IMM(BPF_REG_0, 0),
  6424. BPF_EXIT_INSN(),
  6425. },
  6426. .errstr = "invalid stack type R1 off=-64 access_size=65",
  6427. .result = REJECT,
  6428. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6429. },
  6430. {
  6431. "helper access to variable memory: stack, JMP, no max check",
  6432. .insns = {
  6433. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6434. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6435. BPF_MOV64_IMM(BPF_REG_2, 16),
  6436. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6437. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6438. BPF_MOV64_IMM(BPF_REG_4, 0),
  6439. BPF_JMP_REG(BPF_JGE, BPF_REG_4, BPF_REG_2, 2),
  6440. BPF_MOV64_IMM(BPF_REG_3, 0),
  6441. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6442. BPF_MOV64_IMM(BPF_REG_0, 0),
  6443. BPF_EXIT_INSN(),
  6444. },
  6445. /* because max wasn't checked, signed min is negative */
  6446. .errstr = "R2 min value is negative, either use unsigned or 'var &= const'",
  6447. .result = REJECT,
  6448. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6449. },
  6450. {
  6451. "helper access to variable memory: stack, JMP, no min check",
  6452. .insns = {
  6453. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6454. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6455. BPF_MOV64_IMM(BPF_REG_2, 16),
  6456. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6457. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6458. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 64, 3),
  6459. BPF_MOV64_IMM(BPF_REG_3, 0),
  6460. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6461. BPF_MOV64_IMM(BPF_REG_0, 0),
  6462. BPF_EXIT_INSN(),
  6463. },
  6464. .errstr = "invalid indirect read from stack off -64+0 size 64",
  6465. .result = REJECT,
  6466. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6467. },
  6468. {
  6469. "helper access to variable memory: stack, JMP (signed), no min check",
  6470. .insns = {
  6471. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6472. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6473. BPF_MOV64_IMM(BPF_REG_2, 16),
  6474. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, -128),
  6475. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -128),
  6476. BPF_JMP_IMM(BPF_JSGT, BPF_REG_2, 64, 3),
  6477. BPF_MOV64_IMM(BPF_REG_3, 0),
  6478. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6479. BPF_MOV64_IMM(BPF_REG_0, 0),
  6480. BPF_EXIT_INSN(),
  6481. },
  6482. .errstr = "R2 min value is negative",
  6483. .result = REJECT,
  6484. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6485. },
  6486. {
  6487. "helper access to variable memory: map, JMP, correct bounds",
  6488. .insns = {
  6489. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6490. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6491. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6492. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6493. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6494. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 10),
  6495. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6496. BPF_MOV64_IMM(BPF_REG_2, sizeof(struct test_val)),
  6497. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -128),
  6498. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -128),
  6499. BPF_JMP_IMM(BPF_JSGT, BPF_REG_2,
  6500. sizeof(struct test_val), 4),
  6501. BPF_MOV64_IMM(BPF_REG_4, 0),
  6502. BPF_JMP_REG(BPF_JSGE, BPF_REG_4, BPF_REG_2, 2),
  6503. BPF_MOV64_IMM(BPF_REG_3, 0),
  6504. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6505. BPF_MOV64_IMM(BPF_REG_0, 0),
  6506. BPF_EXIT_INSN(),
  6507. },
  6508. .fixup_map2 = { 3 },
  6509. .result = ACCEPT,
  6510. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6511. },
  6512. {
  6513. "helper access to variable memory: map, JMP, wrong max",
  6514. .insns = {
  6515. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6516. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6517. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6518. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6519. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6520. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 10),
  6521. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6522. BPF_MOV64_IMM(BPF_REG_2, sizeof(struct test_val)),
  6523. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -128),
  6524. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -128),
  6525. BPF_JMP_IMM(BPF_JSGT, BPF_REG_2,
  6526. sizeof(struct test_val) + 1, 4),
  6527. BPF_MOV64_IMM(BPF_REG_4, 0),
  6528. BPF_JMP_REG(BPF_JSGE, BPF_REG_4, BPF_REG_2, 2),
  6529. BPF_MOV64_IMM(BPF_REG_3, 0),
  6530. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6531. BPF_MOV64_IMM(BPF_REG_0, 0),
  6532. BPF_EXIT_INSN(),
  6533. },
  6534. .fixup_map2 = { 3 },
  6535. .errstr = "invalid access to map value, value_size=48 off=0 size=49",
  6536. .result = REJECT,
  6537. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6538. },
  6539. {
  6540. "helper access to variable memory: map adjusted, JMP, correct bounds",
  6541. .insns = {
  6542. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6543. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6544. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6545. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6546. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6547. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 11),
  6548. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6549. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 20),
  6550. BPF_MOV64_IMM(BPF_REG_2, sizeof(struct test_val)),
  6551. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -128),
  6552. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -128),
  6553. BPF_JMP_IMM(BPF_JSGT, BPF_REG_2,
  6554. sizeof(struct test_val) - 20, 4),
  6555. BPF_MOV64_IMM(BPF_REG_4, 0),
  6556. BPF_JMP_REG(BPF_JSGE, BPF_REG_4, BPF_REG_2, 2),
  6557. BPF_MOV64_IMM(BPF_REG_3, 0),
  6558. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6559. BPF_MOV64_IMM(BPF_REG_0, 0),
  6560. BPF_EXIT_INSN(),
  6561. },
  6562. .fixup_map2 = { 3 },
  6563. .result = ACCEPT,
  6564. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6565. },
  6566. {
  6567. "helper access to variable memory: map adjusted, JMP, wrong max",
  6568. .insns = {
  6569. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6570. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6571. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  6572. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6573. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6574. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 11),
  6575. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6576. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 20),
  6577. BPF_MOV64_IMM(BPF_REG_2, sizeof(struct test_val)),
  6578. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -128),
  6579. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -128),
  6580. BPF_JMP_IMM(BPF_JSGT, BPF_REG_2,
  6581. sizeof(struct test_val) - 19, 4),
  6582. BPF_MOV64_IMM(BPF_REG_4, 0),
  6583. BPF_JMP_REG(BPF_JSGE, BPF_REG_4, BPF_REG_2, 2),
  6584. BPF_MOV64_IMM(BPF_REG_3, 0),
  6585. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6586. BPF_MOV64_IMM(BPF_REG_0, 0),
  6587. BPF_EXIT_INSN(),
  6588. },
  6589. .fixup_map2 = { 3 },
  6590. .errstr = "R1 min value is outside of the array range",
  6591. .result = REJECT,
  6592. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6593. },
  6594. {
  6595. "helper access to variable memory: size = 0 allowed on NULL (ARG_PTR_TO_MEM_OR_NULL)",
  6596. .insns = {
  6597. BPF_MOV64_IMM(BPF_REG_1, 0),
  6598. BPF_MOV64_IMM(BPF_REG_2, 0),
  6599. BPF_MOV64_IMM(BPF_REG_3, 0),
  6600. BPF_MOV64_IMM(BPF_REG_4, 0),
  6601. BPF_MOV64_IMM(BPF_REG_5, 0),
  6602. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6603. BPF_EXIT_INSN(),
  6604. },
  6605. .result = ACCEPT,
  6606. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6607. },
  6608. {
  6609. "helper access to variable memory: size > 0 not allowed on NULL (ARG_PTR_TO_MEM_OR_NULL)",
  6610. .insns = {
  6611. BPF_MOV64_IMM(BPF_REG_1, 0),
  6612. BPF_MOV64_IMM(BPF_REG_2, 1),
  6613. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -128),
  6614. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -128),
  6615. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 64),
  6616. BPF_MOV64_IMM(BPF_REG_3, 0),
  6617. BPF_MOV64_IMM(BPF_REG_4, 0),
  6618. BPF_MOV64_IMM(BPF_REG_5, 0),
  6619. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6620. BPF_EXIT_INSN(),
  6621. },
  6622. .errstr = "R1 type=inv expected=fp",
  6623. .result = REJECT,
  6624. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6625. },
  6626. {
  6627. "helper access to variable memory: size = 0 allowed on != NULL stack pointer (ARG_PTR_TO_MEM_OR_NULL)",
  6628. .insns = {
  6629. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6630. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  6631. BPF_MOV64_IMM(BPF_REG_2, 0),
  6632. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, 0),
  6633. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 8),
  6634. BPF_MOV64_IMM(BPF_REG_3, 0),
  6635. BPF_MOV64_IMM(BPF_REG_4, 0),
  6636. BPF_MOV64_IMM(BPF_REG_5, 0),
  6637. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6638. BPF_EXIT_INSN(),
  6639. },
  6640. .result = ACCEPT,
  6641. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6642. },
  6643. {
  6644. "helper access to variable memory: size = 0 allowed on != NULL map pointer (ARG_PTR_TO_MEM_OR_NULL)",
  6645. .insns = {
  6646. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6647. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6648. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6649. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6650. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6651. BPF_FUNC_map_lookup_elem),
  6652. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  6653. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6654. BPF_MOV64_IMM(BPF_REG_2, 0),
  6655. BPF_MOV64_IMM(BPF_REG_3, 0),
  6656. BPF_MOV64_IMM(BPF_REG_4, 0),
  6657. BPF_MOV64_IMM(BPF_REG_5, 0),
  6658. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6659. BPF_EXIT_INSN(),
  6660. },
  6661. .fixup_map1 = { 3 },
  6662. .result = ACCEPT,
  6663. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6664. },
  6665. {
  6666. "helper access to variable memory: size possible = 0 allowed on != NULL stack pointer (ARG_PTR_TO_MEM_OR_NULL)",
  6667. .insns = {
  6668. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6669. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6670. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6671. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6672. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6673. BPF_FUNC_map_lookup_elem),
  6674. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  6675. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  6676. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 8, 7),
  6677. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6678. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  6679. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_2, 0),
  6680. BPF_MOV64_IMM(BPF_REG_3, 0),
  6681. BPF_MOV64_IMM(BPF_REG_4, 0),
  6682. BPF_MOV64_IMM(BPF_REG_5, 0),
  6683. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6684. BPF_EXIT_INSN(),
  6685. },
  6686. .fixup_map1 = { 3 },
  6687. .result = ACCEPT,
  6688. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6689. },
  6690. {
  6691. "helper access to variable memory: size possible = 0 allowed on != NULL map pointer (ARG_PTR_TO_MEM_OR_NULL)",
  6692. .insns = {
  6693. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6694. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6695. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6696. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6697. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6698. BPF_FUNC_map_lookup_elem),
  6699. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  6700. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6701. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  6702. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 8, 4),
  6703. BPF_MOV64_IMM(BPF_REG_3, 0),
  6704. BPF_MOV64_IMM(BPF_REG_4, 0),
  6705. BPF_MOV64_IMM(BPF_REG_5, 0),
  6706. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6707. BPF_EXIT_INSN(),
  6708. },
  6709. .fixup_map1 = { 3 },
  6710. .result = ACCEPT,
  6711. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6712. },
  6713. {
  6714. "helper access to variable memory: size possible = 0 allowed on != NULL packet pointer (ARG_PTR_TO_MEM_OR_NULL)",
  6715. .insns = {
  6716. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  6717. offsetof(struct __sk_buff, data)),
  6718. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  6719. offsetof(struct __sk_buff, data_end)),
  6720. BPF_MOV64_REG(BPF_REG_0, BPF_REG_6),
  6721. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  6722. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 7),
  6723. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  6724. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 0),
  6725. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 8, 4),
  6726. BPF_MOV64_IMM(BPF_REG_3, 0),
  6727. BPF_MOV64_IMM(BPF_REG_4, 0),
  6728. BPF_MOV64_IMM(BPF_REG_5, 0),
  6729. BPF_EMIT_CALL(BPF_FUNC_csum_diff),
  6730. BPF_EXIT_INSN(),
  6731. },
  6732. .result = ACCEPT,
  6733. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  6734. .retval = 0 /* csum_diff of 64-byte packet */,
  6735. },
  6736. {
  6737. "helper access to variable memory: size = 0 not allowed on NULL (!ARG_PTR_TO_MEM_OR_NULL)",
  6738. .insns = {
  6739. BPF_MOV64_IMM(BPF_REG_1, 0),
  6740. BPF_MOV64_IMM(BPF_REG_2, 0),
  6741. BPF_MOV64_IMM(BPF_REG_3, 0),
  6742. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6743. BPF_EXIT_INSN(),
  6744. },
  6745. .errstr = "R1 type=inv expected=fp",
  6746. .result = REJECT,
  6747. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6748. },
  6749. {
  6750. "helper access to variable memory: size > 0 not allowed on NULL (!ARG_PTR_TO_MEM_OR_NULL)",
  6751. .insns = {
  6752. BPF_MOV64_IMM(BPF_REG_1, 0),
  6753. BPF_MOV64_IMM(BPF_REG_2, 1),
  6754. BPF_MOV64_IMM(BPF_REG_3, 0),
  6755. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6756. BPF_EXIT_INSN(),
  6757. },
  6758. .errstr = "R1 type=inv expected=fp",
  6759. .result = REJECT,
  6760. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6761. },
  6762. {
  6763. "helper access to variable memory: size = 0 allowed on != NULL stack pointer (!ARG_PTR_TO_MEM_OR_NULL)",
  6764. .insns = {
  6765. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6766. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  6767. BPF_MOV64_IMM(BPF_REG_2, 0),
  6768. BPF_MOV64_IMM(BPF_REG_3, 0),
  6769. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6770. BPF_EXIT_INSN(),
  6771. },
  6772. .result = ACCEPT,
  6773. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6774. },
  6775. {
  6776. "helper access to variable memory: size = 0 allowed on != NULL map pointer (!ARG_PTR_TO_MEM_OR_NULL)",
  6777. .insns = {
  6778. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6779. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6780. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6781. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6782. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6783. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  6784. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6785. BPF_MOV64_IMM(BPF_REG_2, 0),
  6786. BPF_MOV64_IMM(BPF_REG_3, 0),
  6787. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6788. BPF_EXIT_INSN(),
  6789. },
  6790. .fixup_map1 = { 3 },
  6791. .result = ACCEPT,
  6792. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6793. },
  6794. {
  6795. "helper access to variable memory: size possible = 0 allowed on != NULL stack pointer (!ARG_PTR_TO_MEM_OR_NULL)",
  6796. .insns = {
  6797. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6798. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6799. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6800. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6801. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6802. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  6803. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  6804. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 8, 4),
  6805. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6806. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  6807. BPF_MOV64_IMM(BPF_REG_3, 0),
  6808. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6809. BPF_EXIT_INSN(),
  6810. },
  6811. .fixup_map1 = { 3 },
  6812. .result = ACCEPT,
  6813. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6814. },
  6815. {
  6816. "helper access to variable memory: size possible = 0 allowed on != NULL map pointer (!ARG_PTR_TO_MEM_OR_NULL)",
  6817. .insns = {
  6818. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6819. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6820. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6821. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6822. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  6823. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  6824. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6825. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  6826. BPF_JMP_IMM(BPF_JGT, BPF_REG_2, 8, 2),
  6827. BPF_MOV64_IMM(BPF_REG_3, 0),
  6828. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6829. BPF_EXIT_INSN(),
  6830. },
  6831. .fixup_map1 = { 3 },
  6832. .result = ACCEPT,
  6833. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6834. },
  6835. {
  6836. "helper access to variable memory: 8 bytes leak",
  6837. .insns = {
  6838. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6839. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6840. BPF_MOV64_IMM(BPF_REG_0, 0),
  6841. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -64),
  6842. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -56),
  6843. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -48),
  6844. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -40),
  6845. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -24),
  6846. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -16),
  6847. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  6848. BPF_MOV64_IMM(BPF_REG_2, 1),
  6849. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -128),
  6850. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -128),
  6851. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 63),
  6852. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
  6853. BPF_MOV64_IMM(BPF_REG_3, 0),
  6854. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6855. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  6856. BPF_EXIT_INSN(),
  6857. },
  6858. .errstr = "invalid indirect read from stack off -64+32 size 64",
  6859. .result = REJECT,
  6860. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6861. },
  6862. {
  6863. "helper access to variable memory: 8 bytes no leak (init memory)",
  6864. .insns = {
  6865. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  6866. BPF_MOV64_IMM(BPF_REG_0, 0),
  6867. BPF_MOV64_IMM(BPF_REG_0, 0),
  6868. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -64),
  6869. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -56),
  6870. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -48),
  6871. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -40),
  6872. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -32),
  6873. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -24),
  6874. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -16),
  6875. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  6876. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -64),
  6877. BPF_MOV64_IMM(BPF_REG_2, 0),
  6878. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 32),
  6879. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 32),
  6880. BPF_MOV64_IMM(BPF_REG_3, 0),
  6881. BPF_EMIT_CALL(BPF_FUNC_probe_read),
  6882. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  6883. BPF_EXIT_INSN(),
  6884. },
  6885. .result = ACCEPT,
  6886. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  6887. },
  6888. {
  6889. "invalid and of negative number",
  6890. .insns = {
  6891. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6892. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6893. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6894. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6895. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6896. BPF_FUNC_map_lookup_elem),
  6897. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  6898. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  6899. BPF_ALU64_IMM(BPF_AND, BPF_REG_1, -4),
  6900. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 2),
  6901. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  6902. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  6903. offsetof(struct test_val, foo)),
  6904. BPF_EXIT_INSN(),
  6905. },
  6906. .fixup_map2 = { 3 },
  6907. .errstr = "R0 max value is outside of the array range",
  6908. .result = REJECT,
  6909. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  6910. },
  6911. {
  6912. "invalid range check",
  6913. .insns = {
  6914. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  6915. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6916. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  6917. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6918. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6919. BPF_FUNC_map_lookup_elem),
  6920. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 12),
  6921. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  6922. BPF_MOV64_IMM(BPF_REG_9, 1),
  6923. BPF_ALU32_IMM(BPF_MOD, BPF_REG_1, 2),
  6924. BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 1),
  6925. BPF_ALU32_REG(BPF_AND, BPF_REG_9, BPF_REG_1),
  6926. BPF_ALU32_IMM(BPF_ADD, BPF_REG_9, 1),
  6927. BPF_ALU32_IMM(BPF_RSH, BPF_REG_9, 1),
  6928. BPF_MOV32_IMM(BPF_REG_3, 1),
  6929. BPF_ALU32_REG(BPF_SUB, BPF_REG_3, BPF_REG_9),
  6930. BPF_ALU32_IMM(BPF_MUL, BPF_REG_3, 0x10000000),
  6931. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_3),
  6932. BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_3, 0),
  6933. BPF_MOV64_REG(BPF_REG_0, 0),
  6934. BPF_EXIT_INSN(),
  6935. },
  6936. .fixup_map2 = { 3 },
  6937. .errstr = "R0 max value is outside of the array range",
  6938. .result = REJECT,
  6939. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  6940. },
  6941. {
  6942. "map in map access",
  6943. .insns = {
  6944. BPF_ST_MEM(0, BPF_REG_10, -4, 0),
  6945. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6946. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  6947. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6948. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6949. BPF_FUNC_map_lookup_elem),
  6950. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  6951. BPF_ST_MEM(0, BPF_REG_10, -4, 0),
  6952. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6953. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  6954. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6955. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6956. BPF_FUNC_map_lookup_elem),
  6957. BPF_MOV64_REG(BPF_REG_0, 0),
  6958. BPF_EXIT_INSN(),
  6959. },
  6960. .fixup_map_in_map = { 3 },
  6961. .result = ACCEPT,
  6962. },
  6963. {
  6964. "invalid inner map pointer",
  6965. .insns = {
  6966. BPF_ST_MEM(0, BPF_REG_10, -4, 0),
  6967. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6968. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  6969. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6970. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6971. BPF_FUNC_map_lookup_elem),
  6972. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  6973. BPF_ST_MEM(0, BPF_REG_10, -4, 0),
  6974. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6975. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  6976. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  6977. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  6978. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6979. BPF_FUNC_map_lookup_elem),
  6980. BPF_MOV64_REG(BPF_REG_0, 0),
  6981. BPF_EXIT_INSN(),
  6982. },
  6983. .fixup_map_in_map = { 3 },
  6984. .errstr = "R1 pointer arithmetic on CONST_PTR_TO_MAP prohibited",
  6985. .result = REJECT,
  6986. },
  6987. {
  6988. "forgot null checking on the inner map pointer",
  6989. .insns = {
  6990. BPF_ST_MEM(0, BPF_REG_10, -4, 0),
  6991. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6992. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  6993. BPF_LD_MAP_FD(BPF_REG_1, 0),
  6994. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  6995. BPF_FUNC_map_lookup_elem),
  6996. BPF_ST_MEM(0, BPF_REG_10, -4, 0),
  6997. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  6998. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
  6999. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  7000. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7001. BPF_FUNC_map_lookup_elem),
  7002. BPF_MOV64_REG(BPF_REG_0, 0),
  7003. BPF_EXIT_INSN(),
  7004. },
  7005. .fixup_map_in_map = { 3 },
  7006. .errstr = "R1 type=map_value_or_null expected=map_ptr",
  7007. .result = REJECT,
  7008. },
  7009. {
  7010. "ld_abs: check calling conv, r1",
  7011. .insns = {
  7012. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7013. BPF_MOV64_IMM(BPF_REG_1, 0),
  7014. BPF_LD_ABS(BPF_W, -0x200000),
  7015. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  7016. BPF_EXIT_INSN(),
  7017. },
  7018. .errstr = "R1 !read_ok",
  7019. .result = REJECT,
  7020. },
  7021. {
  7022. "ld_abs: check calling conv, r2",
  7023. .insns = {
  7024. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7025. BPF_MOV64_IMM(BPF_REG_2, 0),
  7026. BPF_LD_ABS(BPF_W, -0x200000),
  7027. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  7028. BPF_EXIT_INSN(),
  7029. },
  7030. .errstr = "R2 !read_ok",
  7031. .result = REJECT,
  7032. },
  7033. {
  7034. "ld_abs: check calling conv, r3",
  7035. .insns = {
  7036. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7037. BPF_MOV64_IMM(BPF_REG_3, 0),
  7038. BPF_LD_ABS(BPF_W, -0x200000),
  7039. BPF_MOV64_REG(BPF_REG_0, BPF_REG_3),
  7040. BPF_EXIT_INSN(),
  7041. },
  7042. .errstr = "R3 !read_ok",
  7043. .result = REJECT,
  7044. },
  7045. {
  7046. "ld_abs: check calling conv, r4",
  7047. .insns = {
  7048. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7049. BPF_MOV64_IMM(BPF_REG_4, 0),
  7050. BPF_LD_ABS(BPF_W, -0x200000),
  7051. BPF_MOV64_REG(BPF_REG_0, BPF_REG_4),
  7052. BPF_EXIT_INSN(),
  7053. },
  7054. .errstr = "R4 !read_ok",
  7055. .result = REJECT,
  7056. },
  7057. {
  7058. "ld_abs: check calling conv, r5",
  7059. .insns = {
  7060. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7061. BPF_MOV64_IMM(BPF_REG_5, 0),
  7062. BPF_LD_ABS(BPF_W, -0x200000),
  7063. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  7064. BPF_EXIT_INSN(),
  7065. },
  7066. .errstr = "R5 !read_ok",
  7067. .result = REJECT,
  7068. },
  7069. {
  7070. "ld_abs: check calling conv, r7",
  7071. .insns = {
  7072. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7073. BPF_MOV64_IMM(BPF_REG_7, 0),
  7074. BPF_LD_ABS(BPF_W, -0x200000),
  7075. BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
  7076. BPF_EXIT_INSN(),
  7077. },
  7078. .result = ACCEPT,
  7079. },
  7080. {
  7081. "ld_abs: tests on r6 and skb data reload helper",
  7082. .insns = {
  7083. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7084. BPF_LD_ABS(BPF_B, 0),
  7085. BPF_LD_ABS(BPF_H, 0),
  7086. BPF_LD_ABS(BPF_W, 0),
  7087. BPF_MOV64_REG(BPF_REG_7, BPF_REG_6),
  7088. BPF_MOV64_IMM(BPF_REG_6, 0),
  7089. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  7090. BPF_MOV64_IMM(BPF_REG_2, 1),
  7091. BPF_MOV64_IMM(BPF_REG_3, 2),
  7092. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7093. BPF_FUNC_skb_vlan_push),
  7094. BPF_MOV64_REG(BPF_REG_6, BPF_REG_7),
  7095. BPF_LD_ABS(BPF_B, 0),
  7096. BPF_LD_ABS(BPF_H, 0),
  7097. BPF_LD_ABS(BPF_W, 0),
  7098. BPF_MOV64_IMM(BPF_REG_0, 42),
  7099. BPF_EXIT_INSN(),
  7100. },
  7101. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  7102. .result = ACCEPT,
  7103. .retval = 42 /* ultimate return value */,
  7104. },
  7105. {
  7106. "ld_ind: check calling conv, r1",
  7107. .insns = {
  7108. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7109. BPF_MOV64_IMM(BPF_REG_1, 1),
  7110. BPF_LD_IND(BPF_W, BPF_REG_1, -0x200000),
  7111. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  7112. BPF_EXIT_INSN(),
  7113. },
  7114. .errstr = "R1 !read_ok",
  7115. .result = REJECT,
  7116. },
  7117. {
  7118. "ld_ind: check calling conv, r2",
  7119. .insns = {
  7120. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7121. BPF_MOV64_IMM(BPF_REG_2, 1),
  7122. BPF_LD_IND(BPF_W, BPF_REG_2, -0x200000),
  7123. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  7124. BPF_EXIT_INSN(),
  7125. },
  7126. .errstr = "R2 !read_ok",
  7127. .result = REJECT,
  7128. },
  7129. {
  7130. "ld_ind: check calling conv, r3",
  7131. .insns = {
  7132. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7133. BPF_MOV64_IMM(BPF_REG_3, 1),
  7134. BPF_LD_IND(BPF_W, BPF_REG_3, -0x200000),
  7135. BPF_MOV64_REG(BPF_REG_0, BPF_REG_3),
  7136. BPF_EXIT_INSN(),
  7137. },
  7138. .errstr = "R3 !read_ok",
  7139. .result = REJECT,
  7140. },
  7141. {
  7142. "ld_ind: check calling conv, r4",
  7143. .insns = {
  7144. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7145. BPF_MOV64_IMM(BPF_REG_4, 1),
  7146. BPF_LD_IND(BPF_W, BPF_REG_4, -0x200000),
  7147. BPF_MOV64_REG(BPF_REG_0, BPF_REG_4),
  7148. BPF_EXIT_INSN(),
  7149. },
  7150. .errstr = "R4 !read_ok",
  7151. .result = REJECT,
  7152. },
  7153. {
  7154. "ld_ind: check calling conv, r5",
  7155. .insns = {
  7156. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7157. BPF_MOV64_IMM(BPF_REG_5, 1),
  7158. BPF_LD_IND(BPF_W, BPF_REG_5, -0x200000),
  7159. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  7160. BPF_EXIT_INSN(),
  7161. },
  7162. .errstr = "R5 !read_ok",
  7163. .result = REJECT,
  7164. },
  7165. {
  7166. "ld_ind: check calling conv, r7",
  7167. .insns = {
  7168. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  7169. BPF_MOV64_IMM(BPF_REG_7, 1),
  7170. BPF_LD_IND(BPF_W, BPF_REG_7, -0x200000),
  7171. BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
  7172. BPF_EXIT_INSN(),
  7173. },
  7174. .result = ACCEPT,
  7175. .retval = 1,
  7176. },
  7177. {
  7178. "check bpf_perf_event_data->sample_period byte load permitted",
  7179. .insns = {
  7180. BPF_MOV64_IMM(BPF_REG_0, 0),
  7181. #if __BYTE_ORDER == __LITTLE_ENDIAN
  7182. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  7183. offsetof(struct bpf_perf_event_data, sample_period)),
  7184. #else
  7185. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
  7186. offsetof(struct bpf_perf_event_data, sample_period) + 7),
  7187. #endif
  7188. BPF_EXIT_INSN(),
  7189. },
  7190. .result = ACCEPT,
  7191. .prog_type = BPF_PROG_TYPE_PERF_EVENT,
  7192. },
  7193. {
  7194. "check bpf_perf_event_data->sample_period half load permitted",
  7195. .insns = {
  7196. BPF_MOV64_IMM(BPF_REG_0, 0),
  7197. #if __BYTE_ORDER == __LITTLE_ENDIAN
  7198. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  7199. offsetof(struct bpf_perf_event_data, sample_period)),
  7200. #else
  7201. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  7202. offsetof(struct bpf_perf_event_data, sample_period) + 6),
  7203. #endif
  7204. BPF_EXIT_INSN(),
  7205. },
  7206. .result = ACCEPT,
  7207. .prog_type = BPF_PROG_TYPE_PERF_EVENT,
  7208. },
  7209. {
  7210. "check bpf_perf_event_data->sample_period word load permitted",
  7211. .insns = {
  7212. BPF_MOV64_IMM(BPF_REG_0, 0),
  7213. #if __BYTE_ORDER == __LITTLE_ENDIAN
  7214. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  7215. offsetof(struct bpf_perf_event_data, sample_period)),
  7216. #else
  7217. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  7218. offsetof(struct bpf_perf_event_data, sample_period) + 4),
  7219. #endif
  7220. BPF_EXIT_INSN(),
  7221. },
  7222. .result = ACCEPT,
  7223. .prog_type = BPF_PROG_TYPE_PERF_EVENT,
  7224. },
  7225. {
  7226. "check bpf_perf_event_data->sample_period dword load permitted",
  7227. .insns = {
  7228. BPF_MOV64_IMM(BPF_REG_0, 0),
  7229. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1,
  7230. offsetof(struct bpf_perf_event_data, sample_period)),
  7231. BPF_EXIT_INSN(),
  7232. },
  7233. .result = ACCEPT,
  7234. .prog_type = BPF_PROG_TYPE_PERF_EVENT,
  7235. },
  7236. {
  7237. "check skb->data half load not permitted",
  7238. .insns = {
  7239. BPF_MOV64_IMM(BPF_REG_0, 0),
  7240. #if __BYTE_ORDER == __LITTLE_ENDIAN
  7241. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  7242. offsetof(struct __sk_buff, data)),
  7243. #else
  7244. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  7245. offsetof(struct __sk_buff, data) + 2),
  7246. #endif
  7247. BPF_EXIT_INSN(),
  7248. },
  7249. .result = REJECT,
  7250. .errstr = "invalid bpf_context access",
  7251. },
  7252. {
  7253. "check skb->tc_classid half load not permitted for lwt prog",
  7254. .insns = {
  7255. BPF_MOV64_IMM(BPF_REG_0, 0),
  7256. #if __BYTE_ORDER == __LITTLE_ENDIAN
  7257. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  7258. offsetof(struct __sk_buff, tc_classid)),
  7259. #else
  7260. BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
  7261. offsetof(struct __sk_buff, tc_classid) + 2),
  7262. #endif
  7263. BPF_EXIT_INSN(),
  7264. },
  7265. .result = REJECT,
  7266. .errstr = "invalid bpf_context access",
  7267. .prog_type = BPF_PROG_TYPE_LWT_IN,
  7268. },
  7269. {
  7270. "bounds checks mixing signed and unsigned, positive bounds",
  7271. .insns = {
  7272. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7273. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7274. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7275. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7276. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7277. BPF_FUNC_map_lookup_elem),
  7278. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  7279. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7280. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7281. BPF_MOV64_IMM(BPF_REG_2, 2),
  7282. BPF_JMP_REG(BPF_JGE, BPF_REG_2, BPF_REG_1, 3),
  7283. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 4, 2),
  7284. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7285. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7286. BPF_MOV64_IMM(BPF_REG_0, 0),
  7287. BPF_EXIT_INSN(),
  7288. },
  7289. .fixup_map1 = { 3 },
  7290. .errstr = "unbounded min value",
  7291. .result = REJECT,
  7292. },
  7293. {
  7294. "bounds checks mixing signed and unsigned",
  7295. .insns = {
  7296. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7297. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7298. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7299. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7300. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7301. BPF_FUNC_map_lookup_elem),
  7302. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  7303. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7304. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7305. BPF_MOV64_IMM(BPF_REG_2, -1),
  7306. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 3),
  7307. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7308. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7309. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7310. BPF_MOV64_IMM(BPF_REG_0, 0),
  7311. BPF_EXIT_INSN(),
  7312. },
  7313. .fixup_map1 = { 3 },
  7314. .errstr = "unbounded min value",
  7315. .result = REJECT,
  7316. },
  7317. {
  7318. "bounds checks mixing signed and unsigned, variant 2",
  7319. .insns = {
  7320. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7321. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7322. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7323. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7324. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7325. BPF_FUNC_map_lookup_elem),
  7326. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7327. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7328. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7329. BPF_MOV64_IMM(BPF_REG_2, -1),
  7330. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 5),
  7331. BPF_MOV64_IMM(BPF_REG_8, 0),
  7332. BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_1),
  7333. BPF_JMP_IMM(BPF_JSGT, BPF_REG_8, 1, 2),
  7334. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_8),
  7335. BPF_ST_MEM(BPF_B, BPF_REG_8, 0, 0),
  7336. BPF_MOV64_IMM(BPF_REG_0, 0),
  7337. BPF_EXIT_INSN(),
  7338. },
  7339. .fixup_map1 = { 3 },
  7340. .errstr = "unbounded min value",
  7341. .result = REJECT,
  7342. },
  7343. {
  7344. "bounds checks mixing signed and unsigned, variant 3",
  7345. .insns = {
  7346. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7347. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7348. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7349. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7350. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7351. BPF_FUNC_map_lookup_elem),
  7352. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
  7353. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7354. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7355. BPF_MOV64_IMM(BPF_REG_2, -1),
  7356. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 4),
  7357. BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
  7358. BPF_JMP_IMM(BPF_JSGT, BPF_REG_8, 1, 2),
  7359. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_8),
  7360. BPF_ST_MEM(BPF_B, BPF_REG_8, 0, 0),
  7361. BPF_MOV64_IMM(BPF_REG_0, 0),
  7362. BPF_EXIT_INSN(),
  7363. },
  7364. .fixup_map1 = { 3 },
  7365. .errstr = "unbounded min value",
  7366. .result = REJECT,
  7367. },
  7368. {
  7369. "bounds checks mixing signed and unsigned, variant 4",
  7370. .insns = {
  7371. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7372. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7373. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7374. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7375. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7376. BPF_FUNC_map_lookup_elem),
  7377. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  7378. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7379. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7380. BPF_MOV64_IMM(BPF_REG_2, 1),
  7381. BPF_ALU64_REG(BPF_AND, BPF_REG_1, BPF_REG_2),
  7382. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7383. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7384. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7385. BPF_MOV64_IMM(BPF_REG_0, 0),
  7386. BPF_EXIT_INSN(),
  7387. },
  7388. .fixup_map1 = { 3 },
  7389. .result = ACCEPT,
  7390. },
  7391. {
  7392. "bounds checks mixing signed and unsigned, variant 5",
  7393. .insns = {
  7394. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7395. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7396. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7397. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7398. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7399. BPF_FUNC_map_lookup_elem),
  7400. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7401. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7402. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7403. BPF_MOV64_IMM(BPF_REG_2, -1),
  7404. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 5),
  7405. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 4),
  7406. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 4),
  7407. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  7408. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7409. BPF_MOV64_IMM(BPF_REG_0, 0),
  7410. BPF_EXIT_INSN(),
  7411. },
  7412. .fixup_map1 = { 3 },
  7413. .errstr = "unbounded min value",
  7414. .result = REJECT,
  7415. },
  7416. {
  7417. "bounds checks mixing signed and unsigned, variant 6",
  7418. .insns = {
  7419. BPF_MOV64_IMM(BPF_REG_2, 0),
  7420. BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
  7421. BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -512),
  7422. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7423. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -16),
  7424. BPF_MOV64_IMM(BPF_REG_6, -1),
  7425. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_6, 5),
  7426. BPF_JMP_IMM(BPF_JSGT, BPF_REG_4, 1, 4),
  7427. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 1),
  7428. BPF_MOV64_IMM(BPF_REG_5, 0),
  7429. BPF_ST_MEM(BPF_H, BPF_REG_10, -512, 0),
  7430. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7431. BPF_FUNC_skb_load_bytes),
  7432. BPF_MOV64_IMM(BPF_REG_0, 0),
  7433. BPF_EXIT_INSN(),
  7434. },
  7435. .errstr = "R4 min value is negative, either use unsigned",
  7436. .result = REJECT,
  7437. },
  7438. {
  7439. "bounds checks mixing signed and unsigned, variant 7",
  7440. .insns = {
  7441. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7442. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7443. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7444. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7445. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7446. BPF_FUNC_map_lookup_elem),
  7447. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
  7448. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7449. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7450. BPF_MOV64_IMM(BPF_REG_2, 1024 * 1024 * 1024),
  7451. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 3),
  7452. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7453. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7454. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7455. BPF_MOV64_IMM(BPF_REG_0, 0),
  7456. BPF_EXIT_INSN(),
  7457. },
  7458. .fixup_map1 = { 3 },
  7459. .result = ACCEPT,
  7460. },
  7461. {
  7462. "bounds checks mixing signed and unsigned, variant 8",
  7463. .insns = {
  7464. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7465. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7466. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7467. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7468. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7469. BPF_FUNC_map_lookup_elem),
  7470. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7471. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7472. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7473. BPF_MOV64_IMM(BPF_REG_2, -1),
  7474. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 2),
  7475. BPF_MOV64_IMM(BPF_REG_0, 0),
  7476. BPF_EXIT_INSN(),
  7477. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7478. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7479. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7480. BPF_MOV64_IMM(BPF_REG_0, 0),
  7481. BPF_EXIT_INSN(),
  7482. },
  7483. .fixup_map1 = { 3 },
  7484. .errstr = "unbounded min value",
  7485. .result = REJECT,
  7486. },
  7487. {
  7488. "bounds checks mixing signed and unsigned, variant 9",
  7489. .insns = {
  7490. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7491. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7492. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7493. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7494. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7495. BPF_FUNC_map_lookup_elem),
  7496. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 10),
  7497. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7498. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7499. BPF_LD_IMM64(BPF_REG_2, -9223372036854775808ULL),
  7500. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 2),
  7501. BPF_MOV64_IMM(BPF_REG_0, 0),
  7502. BPF_EXIT_INSN(),
  7503. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7504. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7505. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7506. BPF_MOV64_IMM(BPF_REG_0, 0),
  7507. BPF_EXIT_INSN(),
  7508. },
  7509. .fixup_map1 = { 3 },
  7510. .result = ACCEPT,
  7511. },
  7512. {
  7513. "bounds checks mixing signed and unsigned, variant 10",
  7514. .insns = {
  7515. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7516. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7517. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7518. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7519. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7520. BPF_FUNC_map_lookup_elem),
  7521. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7522. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7523. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7524. BPF_MOV64_IMM(BPF_REG_2, 0),
  7525. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 2),
  7526. BPF_MOV64_IMM(BPF_REG_0, 0),
  7527. BPF_EXIT_INSN(),
  7528. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7529. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7530. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7531. BPF_MOV64_IMM(BPF_REG_0, 0),
  7532. BPF_EXIT_INSN(),
  7533. },
  7534. .fixup_map1 = { 3 },
  7535. .errstr = "unbounded min value",
  7536. .result = REJECT,
  7537. },
  7538. {
  7539. "bounds checks mixing signed and unsigned, variant 11",
  7540. .insns = {
  7541. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7542. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7543. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7544. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7545. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7546. BPF_FUNC_map_lookup_elem),
  7547. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7548. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7549. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7550. BPF_MOV64_IMM(BPF_REG_2, -1),
  7551. BPF_JMP_REG(BPF_JGE, BPF_REG_2, BPF_REG_1, 2),
  7552. /* Dead branch. */
  7553. BPF_MOV64_IMM(BPF_REG_0, 0),
  7554. BPF_EXIT_INSN(),
  7555. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7556. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7557. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7558. BPF_MOV64_IMM(BPF_REG_0, 0),
  7559. BPF_EXIT_INSN(),
  7560. },
  7561. .fixup_map1 = { 3 },
  7562. .errstr = "unbounded min value",
  7563. .result = REJECT,
  7564. },
  7565. {
  7566. "bounds checks mixing signed and unsigned, variant 12",
  7567. .insns = {
  7568. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7569. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7570. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7571. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7572. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7573. BPF_FUNC_map_lookup_elem),
  7574. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7575. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7576. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7577. BPF_MOV64_IMM(BPF_REG_2, -6),
  7578. BPF_JMP_REG(BPF_JGE, BPF_REG_2, BPF_REG_1, 2),
  7579. BPF_MOV64_IMM(BPF_REG_0, 0),
  7580. BPF_EXIT_INSN(),
  7581. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7582. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7583. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7584. BPF_MOV64_IMM(BPF_REG_0, 0),
  7585. BPF_EXIT_INSN(),
  7586. },
  7587. .fixup_map1 = { 3 },
  7588. .errstr = "unbounded min value",
  7589. .result = REJECT,
  7590. },
  7591. {
  7592. "bounds checks mixing signed and unsigned, variant 13",
  7593. .insns = {
  7594. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7595. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7596. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7597. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7598. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7599. BPF_FUNC_map_lookup_elem),
  7600. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  7601. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7602. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7603. BPF_MOV64_IMM(BPF_REG_2, 2),
  7604. BPF_JMP_REG(BPF_JGE, BPF_REG_2, BPF_REG_1, 2),
  7605. BPF_MOV64_IMM(BPF_REG_7, 1),
  7606. BPF_JMP_IMM(BPF_JSGT, BPF_REG_7, 0, 2),
  7607. BPF_MOV64_IMM(BPF_REG_0, 0),
  7608. BPF_EXIT_INSN(),
  7609. BPF_ALU64_REG(BPF_ADD, BPF_REG_7, BPF_REG_1),
  7610. BPF_JMP_IMM(BPF_JSGT, BPF_REG_7, 4, 2),
  7611. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_7),
  7612. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7613. BPF_MOV64_IMM(BPF_REG_0, 0),
  7614. BPF_EXIT_INSN(),
  7615. },
  7616. .fixup_map1 = { 3 },
  7617. .errstr = "unbounded min value",
  7618. .result = REJECT,
  7619. },
  7620. {
  7621. "bounds checks mixing signed and unsigned, variant 14",
  7622. .insns = {
  7623. BPF_LDX_MEM(BPF_W, BPF_REG_9, BPF_REG_1,
  7624. offsetof(struct __sk_buff, mark)),
  7625. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7626. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7627. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7628. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7629. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7630. BPF_FUNC_map_lookup_elem),
  7631. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
  7632. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7633. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7634. BPF_MOV64_IMM(BPF_REG_2, -1),
  7635. BPF_MOV64_IMM(BPF_REG_8, 2),
  7636. BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 42, 6),
  7637. BPF_JMP_REG(BPF_JSGT, BPF_REG_8, BPF_REG_1, 3),
  7638. BPF_JMP_IMM(BPF_JSGT, BPF_REG_1, 1, 2),
  7639. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7640. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7641. BPF_MOV64_IMM(BPF_REG_0, 0),
  7642. BPF_EXIT_INSN(),
  7643. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, -3),
  7644. BPF_JMP_IMM(BPF_JA, 0, 0, -7),
  7645. },
  7646. .fixup_map1 = { 4 },
  7647. .errstr = "R0 invalid mem access 'inv'",
  7648. .result = REJECT,
  7649. },
  7650. {
  7651. "bounds checks mixing signed and unsigned, variant 15",
  7652. .insns = {
  7653. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7654. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7655. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7656. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7657. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7658. BPF_FUNC_map_lookup_elem),
  7659. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  7660. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, -8),
  7661. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -16),
  7662. BPF_MOV64_IMM(BPF_REG_2, -6),
  7663. BPF_JMP_REG(BPF_JGE, BPF_REG_2, BPF_REG_1, 2),
  7664. BPF_MOV64_IMM(BPF_REG_0, 0),
  7665. BPF_EXIT_INSN(),
  7666. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7667. BPF_JMP_IMM(BPF_JGT, BPF_REG_0, 1, 2),
  7668. BPF_MOV64_IMM(BPF_REG_0, 0),
  7669. BPF_EXIT_INSN(),
  7670. BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
  7671. BPF_MOV64_IMM(BPF_REG_0, 0),
  7672. BPF_EXIT_INSN(),
  7673. },
  7674. .fixup_map1 = { 3 },
  7675. .errstr = "unbounded min value",
  7676. .result = REJECT,
  7677. .result_unpriv = REJECT,
  7678. },
  7679. {
  7680. "subtraction bounds (map value) variant 1",
  7681. .insns = {
  7682. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7683. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7684. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7685. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7686. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7687. BPF_FUNC_map_lookup_elem),
  7688. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7689. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  7690. BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0xff, 7),
  7691. BPF_LDX_MEM(BPF_B, BPF_REG_3, BPF_REG_0, 1),
  7692. BPF_JMP_IMM(BPF_JGT, BPF_REG_3, 0xff, 5),
  7693. BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_3),
  7694. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 56),
  7695. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7696. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7697. BPF_EXIT_INSN(),
  7698. BPF_MOV64_IMM(BPF_REG_0, 0),
  7699. BPF_EXIT_INSN(),
  7700. },
  7701. .fixup_map1 = { 3 },
  7702. .errstr = "R0 max value is outside of the array range",
  7703. .result = REJECT,
  7704. },
  7705. {
  7706. "subtraction bounds (map value) variant 2",
  7707. .insns = {
  7708. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7709. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7710. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7711. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7712. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7713. BPF_FUNC_map_lookup_elem),
  7714. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
  7715. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  7716. BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0xff, 6),
  7717. BPF_LDX_MEM(BPF_B, BPF_REG_3, BPF_REG_0, 1),
  7718. BPF_JMP_IMM(BPF_JGT, BPF_REG_3, 0xff, 4),
  7719. BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_3),
  7720. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7721. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7722. BPF_EXIT_INSN(),
  7723. BPF_MOV64_IMM(BPF_REG_0, 0),
  7724. BPF_EXIT_INSN(),
  7725. },
  7726. .fixup_map1 = { 3 },
  7727. .errstr = "R0 min value is negative, either use unsigned index or do a if (index >=0) check.",
  7728. .result = REJECT,
  7729. },
  7730. {
  7731. "bounds check based on zero-extended MOV",
  7732. .insns = {
  7733. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7734. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7735. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7736. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7737. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7738. BPF_FUNC_map_lookup_elem),
  7739. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  7740. /* r2 = 0x0000'0000'ffff'ffff */
  7741. BPF_MOV32_IMM(BPF_REG_2, 0xffffffff),
  7742. /* r2 = 0 */
  7743. BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 32),
  7744. /* no-op */
  7745. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  7746. /* access at offset 0 */
  7747. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7748. /* exit */
  7749. BPF_MOV64_IMM(BPF_REG_0, 0),
  7750. BPF_EXIT_INSN(),
  7751. },
  7752. .fixup_map1 = { 3 },
  7753. .result = ACCEPT
  7754. },
  7755. {
  7756. "bounds check based on sign-extended MOV. test1",
  7757. .insns = {
  7758. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7759. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7760. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7761. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7762. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7763. BPF_FUNC_map_lookup_elem),
  7764. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  7765. /* r2 = 0xffff'ffff'ffff'ffff */
  7766. BPF_MOV64_IMM(BPF_REG_2, 0xffffffff),
  7767. /* r2 = 0xffff'ffff */
  7768. BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 32),
  7769. /* r0 = <oob pointer> */
  7770. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  7771. /* access to OOB pointer */
  7772. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7773. /* exit */
  7774. BPF_MOV64_IMM(BPF_REG_0, 0),
  7775. BPF_EXIT_INSN(),
  7776. },
  7777. .fixup_map1 = { 3 },
  7778. .errstr = "map_value pointer and 4294967295",
  7779. .result = REJECT
  7780. },
  7781. {
  7782. "bounds check based on sign-extended MOV. test2",
  7783. .insns = {
  7784. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7785. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7786. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7787. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7788. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7789. BPF_FUNC_map_lookup_elem),
  7790. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  7791. /* r2 = 0xffff'ffff'ffff'ffff */
  7792. BPF_MOV64_IMM(BPF_REG_2, 0xffffffff),
  7793. /* r2 = 0xfff'ffff */
  7794. BPF_ALU64_IMM(BPF_RSH, BPF_REG_2, 36),
  7795. /* r0 = <oob pointer> */
  7796. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  7797. /* access to OOB pointer */
  7798. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7799. /* exit */
  7800. BPF_MOV64_IMM(BPF_REG_0, 0),
  7801. BPF_EXIT_INSN(),
  7802. },
  7803. .fixup_map1 = { 3 },
  7804. .errstr = "R0 min value is outside of the array range",
  7805. .result = REJECT
  7806. },
  7807. {
  7808. "bounds check based on reg_off + var_off + insn_off. test1",
  7809. .insns = {
  7810. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  7811. offsetof(struct __sk_buff, mark)),
  7812. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7813. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7814. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7815. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7816. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7817. BPF_FUNC_map_lookup_elem),
  7818. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  7819. BPF_ALU64_IMM(BPF_AND, BPF_REG_6, 1),
  7820. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, (1 << 29) - 1),
  7821. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_6),
  7822. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, (1 << 29) - 1),
  7823. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 3),
  7824. BPF_MOV64_IMM(BPF_REG_0, 0),
  7825. BPF_EXIT_INSN(),
  7826. },
  7827. .fixup_map1 = { 4 },
  7828. .errstr = "value_size=8 off=1073741825",
  7829. .result = REJECT,
  7830. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  7831. },
  7832. {
  7833. "bounds check based on reg_off + var_off + insn_off. test2",
  7834. .insns = {
  7835. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  7836. offsetof(struct __sk_buff, mark)),
  7837. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7838. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7839. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7840. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7841. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7842. BPF_FUNC_map_lookup_elem),
  7843. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
  7844. BPF_ALU64_IMM(BPF_AND, BPF_REG_6, 1),
  7845. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, (1 << 30) - 1),
  7846. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_6),
  7847. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, (1 << 29) - 1),
  7848. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 3),
  7849. BPF_MOV64_IMM(BPF_REG_0, 0),
  7850. BPF_EXIT_INSN(),
  7851. },
  7852. .fixup_map1 = { 4 },
  7853. .errstr = "value 1073741823",
  7854. .result = REJECT,
  7855. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  7856. },
  7857. {
  7858. "bounds check after truncation of non-boundary-crossing range",
  7859. .insns = {
  7860. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7861. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7862. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7863. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7864. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7865. BPF_FUNC_map_lookup_elem),
  7866. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7867. /* r1 = [0x00, 0xff] */
  7868. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  7869. BPF_MOV64_IMM(BPF_REG_2, 1),
  7870. /* r2 = 0x10'0000'0000 */
  7871. BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 36),
  7872. /* r1 = [0x10'0000'0000, 0x10'0000'00ff] */
  7873. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2),
  7874. /* r1 = [0x10'7fff'ffff, 0x10'8000'00fe] */
  7875. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x7fffffff),
  7876. /* r1 = [0x00, 0xff] */
  7877. BPF_ALU32_IMM(BPF_SUB, BPF_REG_1, 0x7fffffff),
  7878. /* r1 = 0 */
  7879. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
  7880. /* no-op */
  7881. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7882. /* access at offset 0 */
  7883. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7884. /* exit */
  7885. BPF_MOV64_IMM(BPF_REG_0, 0),
  7886. BPF_EXIT_INSN(),
  7887. },
  7888. .fixup_map1 = { 3 },
  7889. .result = ACCEPT
  7890. },
  7891. {
  7892. "bounds check after truncation of boundary-crossing range (1)",
  7893. .insns = {
  7894. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7895. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7896. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7897. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7898. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7899. BPF_FUNC_map_lookup_elem),
  7900. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7901. /* r1 = [0x00, 0xff] */
  7902. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  7903. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
  7904. /* r1 = [0xffff'ff80, 0x1'0000'007f] */
  7905. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
  7906. /* r1 = [0xffff'ff80, 0xffff'ffff] or
  7907. * [0x0000'0000, 0x0000'007f]
  7908. */
  7909. BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 0),
  7910. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
  7911. /* r1 = [0x00, 0xff] or
  7912. * [0xffff'ffff'0000'0080, 0xffff'ffff'ffff'ffff]
  7913. */
  7914. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
  7915. /* r1 = 0 or
  7916. * [0x00ff'ffff'ff00'0000, 0x00ff'ffff'ffff'ffff]
  7917. */
  7918. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
  7919. /* no-op or OOB pointer computation */
  7920. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7921. /* potentially OOB access */
  7922. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7923. /* exit */
  7924. BPF_MOV64_IMM(BPF_REG_0, 0),
  7925. BPF_EXIT_INSN(),
  7926. },
  7927. .fixup_map1 = { 3 },
  7928. /* not actually fully unbounded, but the bound is very high */
  7929. .errstr = "R0 unbounded memory access",
  7930. .result = REJECT
  7931. },
  7932. {
  7933. "bounds check after truncation of boundary-crossing range (2)",
  7934. .insns = {
  7935. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7936. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7937. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7938. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7939. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7940. BPF_FUNC_map_lookup_elem),
  7941. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 9),
  7942. /* r1 = [0x00, 0xff] */
  7943. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  7944. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
  7945. /* r1 = [0xffff'ff80, 0x1'0000'007f] */
  7946. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0xffffff80 >> 1),
  7947. /* r1 = [0xffff'ff80, 0xffff'ffff] or
  7948. * [0x0000'0000, 0x0000'007f]
  7949. * difference to previous test: truncation via MOV32
  7950. * instead of ALU32.
  7951. */
  7952. BPF_MOV32_REG(BPF_REG_1, BPF_REG_1),
  7953. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
  7954. /* r1 = [0x00, 0xff] or
  7955. * [0xffff'ffff'0000'0080, 0xffff'ffff'ffff'ffff]
  7956. */
  7957. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 0xffffff80 >> 1),
  7958. /* r1 = 0 or
  7959. * [0x00ff'ffff'ff00'0000, 0x00ff'ffff'ffff'ffff]
  7960. */
  7961. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
  7962. /* no-op or OOB pointer computation */
  7963. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7964. /* potentially OOB access */
  7965. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7966. /* exit */
  7967. BPF_MOV64_IMM(BPF_REG_0, 0),
  7968. BPF_EXIT_INSN(),
  7969. },
  7970. .fixup_map1 = { 3 },
  7971. /* not actually fully unbounded, but the bound is very high */
  7972. .errstr = "R0 unbounded memory access",
  7973. .result = REJECT
  7974. },
  7975. {
  7976. "bounds check after wrapping 32-bit addition",
  7977. .insns = {
  7978. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  7979. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  7980. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  7981. BPF_LD_MAP_FD(BPF_REG_1, 0),
  7982. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  7983. BPF_FUNC_map_lookup_elem),
  7984. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
  7985. /* r1 = 0x7fff'ffff */
  7986. BPF_MOV64_IMM(BPF_REG_1, 0x7fffffff),
  7987. /* r1 = 0xffff'fffe */
  7988. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x7fffffff),
  7989. /* r1 = 0 */
  7990. BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 2),
  7991. /* no-op */
  7992. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  7993. /* access at offset 0 */
  7994. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  7995. /* exit */
  7996. BPF_MOV64_IMM(BPF_REG_0, 0),
  7997. BPF_EXIT_INSN(),
  7998. },
  7999. .fixup_map1 = { 3 },
  8000. .result = ACCEPT
  8001. },
  8002. {
  8003. "bounds check after shift with oversized count operand",
  8004. .insns = {
  8005. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8006. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8007. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8008. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8009. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8010. BPF_FUNC_map_lookup_elem),
  8011. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  8012. BPF_MOV64_IMM(BPF_REG_2, 32),
  8013. BPF_MOV64_IMM(BPF_REG_1, 1),
  8014. /* r1 = (u32)1 << (u32)32 = ? */
  8015. BPF_ALU32_REG(BPF_LSH, BPF_REG_1, BPF_REG_2),
  8016. /* r1 = [0x0000, 0xffff] */
  8017. BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0xffff),
  8018. /* computes unknown pointer, potentially OOB */
  8019. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  8020. /* potentially OOB access */
  8021. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  8022. /* exit */
  8023. BPF_MOV64_IMM(BPF_REG_0, 0),
  8024. BPF_EXIT_INSN(),
  8025. },
  8026. .fixup_map1 = { 3 },
  8027. .errstr = "R0 max value is outside of the array range",
  8028. .result = REJECT
  8029. },
  8030. {
  8031. "bounds check after right shift of maybe-negative number",
  8032. .insns = {
  8033. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8034. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8035. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8036. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8037. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8038. BPF_FUNC_map_lookup_elem),
  8039. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 6),
  8040. /* r1 = [0x00, 0xff] */
  8041. BPF_LDX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  8042. /* r1 = [-0x01, 0xfe] */
  8043. BPF_ALU64_IMM(BPF_SUB, BPF_REG_1, 1),
  8044. /* r1 = 0 or 0xff'ffff'ffff'ffff */
  8045. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
  8046. /* r1 = 0 or 0xffff'ffff'ffff */
  8047. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 8),
  8048. /* computes unknown pointer, potentially OOB */
  8049. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  8050. /* potentially OOB access */
  8051. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  8052. /* exit */
  8053. BPF_MOV64_IMM(BPF_REG_0, 0),
  8054. BPF_EXIT_INSN(),
  8055. },
  8056. .fixup_map1 = { 3 },
  8057. .errstr = "R0 unbounded memory access",
  8058. .result = REJECT
  8059. },
  8060. {
  8061. "bounds check map access with off+size signed 32bit overflow. test1",
  8062. .insns = {
  8063. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8064. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8065. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8066. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8067. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8068. BPF_FUNC_map_lookup_elem),
  8069. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  8070. BPF_EXIT_INSN(),
  8071. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x7ffffffe),
  8072. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
  8073. BPF_JMP_A(0),
  8074. BPF_EXIT_INSN(),
  8075. },
  8076. .fixup_map1 = { 3 },
  8077. .errstr = "map_value pointer and 2147483646",
  8078. .result = REJECT
  8079. },
  8080. {
  8081. "bounds check map access with off+size signed 32bit overflow. test2",
  8082. .insns = {
  8083. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8084. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8085. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8086. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8087. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8088. BPF_FUNC_map_lookup_elem),
  8089. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  8090. BPF_EXIT_INSN(),
  8091. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x1fffffff),
  8092. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x1fffffff),
  8093. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x1fffffff),
  8094. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
  8095. BPF_JMP_A(0),
  8096. BPF_EXIT_INSN(),
  8097. },
  8098. .fixup_map1 = { 3 },
  8099. .errstr = "pointer offset 1073741822",
  8100. .result = REJECT
  8101. },
  8102. {
  8103. "bounds check map access with off+size signed 32bit overflow. test3",
  8104. .insns = {
  8105. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8106. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8107. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8108. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8109. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8110. BPF_FUNC_map_lookup_elem),
  8111. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  8112. BPF_EXIT_INSN(),
  8113. BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 0x1fffffff),
  8114. BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 0x1fffffff),
  8115. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 2),
  8116. BPF_JMP_A(0),
  8117. BPF_EXIT_INSN(),
  8118. },
  8119. .fixup_map1 = { 3 },
  8120. .errstr = "pointer offset -1073741822",
  8121. .result = REJECT
  8122. },
  8123. {
  8124. "bounds check map access with off+size signed 32bit overflow. test4",
  8125. .insns = {
  8126. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8127. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8128. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8129. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8130. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8131. BPF_FUNC_map_lookup_elem),
  8132. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  8133. BPF_EXIT_INSN(),
  8134. BPF_MOV64_IMM(BPF_REG_1, 1000000),
  8135. BPF_ALU64_IMM(BPF_MUL, BPF_REG_1, 1000000),
  8136. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  8137. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 2),
  8138. BPF_JMP_A(0),
  8139. BPF_EXIT_INSN(),
  8140. },
  8141. .fixup_map1 = { 3 },
  8142. .errstr = "map_value pointer and 1000000000000",
  8143. .result = REJECT
  8144. },
  8145. {
  8146. "pointer/scalar confusion in state equality check (way 1)",
  8147. .insns = {
  8148. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8149. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8150. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8151. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8152. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8153. BPF_FUNC_map_lookup_elem),
  8154. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  8155. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
  8156. BPF_JMP_A(1),
  8157. BPF_MOV64_REG(BPF_REG_0, BPF_REG_10),
  8158. BPF_JMP_A(0),
  8159. BPF_EXIT_INSN(),
  8160. },
  8161. .fixup_map1 = { 3 },
  8162. .result = ACCEPT,
  8163. .retval = POINTER_VALUE,
  8164. .result_unpriv = REJECT,
  8165. .errstr_unpriv = "R0 leaks addr as return value"
  8166. },
  8167. {
  8168. "pointer/scalar confusion in state equality check (way 2)",
  8169. .insns = {
  8170. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8171. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8172. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8173. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8174. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8175. BPF_FUNC_map_lookup_elem),
  8176. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  8177. BPF_MOV64_REG(BPF_REG_0, BPF_REG_10),
  8178. BPF_JMP_A(1),
  8179. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
  8180. BPF_EXIT_INSN(),
  8181. },
  8182. .fixup_map1 = { 3 },
  8183. .result = ACCEPT,
  8184. .retval = POINTER_VALUE,
  8185. .result_unpriv = REJECT,
  8186. .errstr_unpriv = "R0 leaks addr as return value"
  8187. },
  8188. {
  8189. "variable-offset ctx access",
  8190. .insns = {
  8191. /* Get an unknown value */
  8192. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
  8193. /* Make it small and 4-byte aligned */
  8194. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
  8195. /* add it to skb. We now have either &skb->len or
  8196. * &skb->pkt_type, but we don't know which
  8197. */
  8198. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2),
  8199. /* dereference it */
  8200. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  8201. BPF_EXIT_INSN(),
  8202. },
  8203. .errstr = "variable ctx access var_off=(0x0; 0x4)",
  8204. .result = REJECT,
  8205. .prog_type = BPF_PROG_TYPE_LWT_IN,
  8206. },
  8207. {
  8208. "variable-offset stack access",
  8209. .insns = {
  8210. /* Fill the top 8 bytes of the stack */
  8211. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8212. /* Get an unknown value */
  8213. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
  8214. /* Make it small and 4-byte aligned */
  8215. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
  8216. BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 8),
  8217. /* add it to fp. We now have either fp-4 or fp-8, but
  8218. * we don't know which
  8219. */
  8220. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
  8221. /* dereference it */
  8222. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0),
  8223. BPF_EXIT_INSN(),
  8224. },
  8225. .errstr = "variable stack access var_off=(0xfffffffffffffff8; 0x4)",
  8226. .result = REJECT,
  8227. .prog_type = BPF_PROG_TYPE_LWT_IN,
  8228. },
  8229. {
  8230. "indirect variable-offset stack access",
  8231. .insns = {
  8232. /* Fill the top 8 bytes of the stack */
  8233. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8234. /* Get an unknown value */
  8235. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
  8236. /* Make it small and 4-byte aligned */
  8237. BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
  8238. BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 8),
  8239. /* add it to fp. We now have either fp-4 or fp-8, but
  8240. * we don't know which
  8241. */
  8242. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
  8243. /* dereference it indirectly */
  8244. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8245. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8246. BPF_FUNC_map_lookup_elem),
  8247. BPF_MOV64_IMM(BPF_REG_0, 0),
  8248. BPF_EXIT_INSN(),
  8249. },
  8250. .fixup_map1 = { 5 },
  8251. .errstr = "variable stack read R2",
  8252. .result = REJECT,
  8253. .prog_type = BPF_PROG_TYPE_LWT_IN,
  8254. },
  8255. {
  8256. "direct stack access with 32-bit wraparound. test1",
  8257. .insns = {
  8258. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  8259. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x7fffffff),
  8260. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x7fffffff),
  8261. BPF_MOV32_IMM(BPF_REG_0, 0),
  8262. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  8263. BPF_EXIT_INSN()
  8264. },
  8265. .errstr = "fp pointer and 2147483647",
  8266. .result = REJECT
  8267. },
  8268. {
  8269. "direct stack access with 32-bit wraparound. test2",
  8270. .insns = {
  8271. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  8272. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x3fffffff),
  8273. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x3fffffff),
  8274. BPF_MOV32_IMM(BPF_REG_0, 0),
  8275. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  8276. BPF_EXIT_INSN()
  8277. },
  8278. .errstr = "fp pointer and 1073741823",
  8279. .result = REJECT
  8280. },
  8281. {
  8282. "direct stack access with 32-bit wraparound. test3",
  8283. .insns = {
  8284. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  8285. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x1fffffff),
  8286. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 0x1fffffff),
  8287. BPF_MOV32_IMM(BPF_REG_0, 0),
  8288. BPF_STX_MEM(BPF_B, BPF_REG_1, BPF_REG_0, 0),
  8289. BPF_EXIT_INSN()
  8290. },
  8291. .errstr = "fp pointer offset 1073741822",
  8292. .result = REJECT
  8293. },
  8294. {
  8295. "liveness pruning and write screening",
  8296. .insns = {
  8297. /* Get an unknown value */
  8298. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
  8299. /* branch conditions teach us nothing about R2 */
  8300. BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
  8301. BPF_MOV64_IMM(BPF_REG_0, 0),
  8302. BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
  8303. BPF_MOV64_IMM(BPF_REG_0, 0),
  8304. BPF_EXIT_INSN(),
  8305. },
  8306. .errstr = "R0 !read_ok",
  8307. .result = REJECT,
  8308. .prog_type = BPF_PROG_TYPE_LWT_IN,
  8309. },
  8310. {
  8311. "varlen_map_value_access pruning",
  8312. .insns = {
  8313. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  8314. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  8315. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  8316. BPF_LD_MAP_FD(BPF_REG_1, 0),
  8317. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8318. BPF_FUNC_map_lookup_elem),
  8319. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
  8320. BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
  8321. BPF_MOV32_IMM(BPF_REG_2, MAX_ENTRIES),
  8322. BPF_JMP_REG(BPF_JSGT, BPF_REG_2, BPF_REG_1, 1),
  8323. BPF_MOV32_IMM(BPF_REG_1, 0),
  8324. BPF_ALU32_IMM(BPF_LSH, BPF_REG_1, 2),
  8325. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
  8326. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  8327. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0,
  8328. offsetof(struct test_val, foo)),
  8329. BPF_EXIT_INSN(),
  8330. },
  8331. .fixup_map2 = { 3 },
  8332. .errstr_unpriv = "R0 leaks addr",
  8333. .errstr = "R0 unbounded memory access",
  8334. .result_unpriv = REJECT,
  8335. .result = REJECT,
  8336. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8337. },
  8338. {
  8339. "invalid 64-bit BPF_END",
  8340. .insns = {
  8341. BPF_MOV32_IMM(BPF_REG_0, 0),
  8342. {
  8343. .code = BPF_ALU64 | BPF_END | BPF_TO_LE,
  8344. .dst_reg = BPF_REG_0,
  8345. .src_reg = 0,
  8346. .off = 0,
  8347. .imm = 32,
  8348. },
  8349. BPF_EXIT_INSN(),
  8350. },
  8351. .errstr = "unknown opcode d7",
  8352. .result = REJECT,
  8353. },
  8354. {
  8355. "XDP, using ifindex from netdev",
  8356. .insns = {
  8357. BPF_MOV64_IMM(BPF_REG_0, 0),
  8358. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8359. offsetof(struct xdp_md, ingress_ifindex)),
  8360. BPF_JMP_IMM(BPF_JLT, BPF_REG_2, 1, 1),
  8361. BPF_MOV64_IMM(BPF_REG_0, 1),
  8362. BPF_EXIT_INSN(),
  8363. },
  8364. .result = ACCEPT,
  8365. .prog_type = BPF_PROG_TYPE_XDP,
  8366. .retval = 1,
  8367. },
  8368. {
  8369. "meta access, test1",
  8370. .insns = {
  8371. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8372. offsetof(struct xdp_md, data_meta)),
  8373. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8374. offsetof(struct xdp_md, data)),
  8375. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  8376. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  8377. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  8378. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8379. BPF_MOV64_IMM(BPF_REG_0, 0),
  8380. BPF_EXIT_INSN(),
  8381. },
  8382. .result = ACCEPT,
  8383. .prog_type = BPF_PROG_TYPE_XDP,
  8384. },
  8385. {
  8386. "meta access, test2",
  8387. .insns = {
  8388. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8389. offsetof(struct xdp_md, data_meta)),
  8390. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8391. offsetof(struct xdp_md, data)),
  8392. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  8393. BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 8),
  8394. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  8395. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8),
  8396. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
  8397. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  8398. BPF_MOV64_IMM(BPF_REG_0, 0),
  8399. BPF_EXIT_INSN(),
  8400. },
  8401. .result = REJECT,
  8402. .errstr = "invalid access to packet, off=-8",
  8403. .prog_type = BPF_PROG_TYPE_XDP,
  8404. },
  8405. {
  8406. "meta access, test3",
  8407. .insns = {
  8408. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8409. offsetof(struct xdp_md, data_meta)),
  8410. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8411. offsetof(struct xdp_md, data_end)),
  8412. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  8413. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  8414. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  8415. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8416. BPF_MOV64_IMM(BPF_REG_0, 0),
  8417. BPF_EXIT_INSN(),
  8418. },
  8419. .result = REJECT,
  8420. .errstr = "invalid access to packet",
  8421. .prog_type = BPF_PROG_TYPE_XDP,
  8422. },
  8423. {
  8424. "meta access, test4",
  8425. .insns = {
  8426. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8427. offsetof(struct xdp_md, data_meta)),
  8428. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8429. offsetof(struct xdp_md, data_end)),
  8430. BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,
  8431. offsetof(struct xdp_md, data)),
  8432. BPF_MOV64_REG(BPF_REG_0, BPF_REG_4),
  8433. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  8434. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
  8435. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8436. BPF_MOV64_IMM(BPF_REG_0, 0),
  8437. BPF_EXIT_INSN(),
  8438. },
  8439. .result = REJECT,
  8440. .errstr = "invalid access to packet",
  8441. .prog_type = BPF_PROG_TYPE_XDP,
  8442. },
  8443. {
  8444. "meta access, test5",
  8445. .insns = {
  8446. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8447. offsetof(struct xdp_md, data_meta)),
  8448. BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,
  8449. offsetof(struct xdp_md, data)),
  8450. BPF_MOV64_REG(BPF_REG_0, BPF_REG_3),
  8451. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  8452. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_4, 3),
  8453. BPF_MOV64_IMM(BPF_REG_2, -8),
  8454. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  8455. BPF_FUNC_xdp_adjust_meta),
  8456. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_3, 0),
  8457. BPF_MOV64_IMM(BPF_REG_0, 0),
  8458. BPF_EXIT_INSN(),
  8459. },
  8460. .result = REJECT,
  8461. .errstr = "R3 !read_ok",
  8462. .prog_type = BPF_PROG_TYPE_XDP,
  8463. },
  8464. {
  8465. "meta access, test6",
  8466. .insns = {
  8467. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8468. offsetof(struct xdp_md, data_meta)),
  8469. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8470. offsetof(struct xdp_md, data)),
  8471. BPF_MOV64_REG(BPF_REG_0, BPF_REG_3),
  8472. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  8473. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  8474. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8),
  8475. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_0, 1),
  8476. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8477. BPF_MOV64_IMM(BPF_REG_0, 0),
  8478. BPF_EXIT_INSN(),
  8479. },
  8480. .result = REJECT,
  8481. .errstr = "invalid access to packet",
  8482. .prog_type = BPF_PROG_TYPE_XDP,
  8483. },
  8484. {
  8485. "meta access, test7",
  8486. .insns = {
  8487. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8488. offsetof(struct xdp_md, data_meta)),
  8489. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8490. offsetof(struct xdp_md, data)),
  8491. BPF_MOV64_REG(BPF_REG_0, BPF_REG_3),
  8492. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  8493. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  8494. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 8),
  8495. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
  8496. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8497. BPF_MOV64_IMM(BPF_REG_0, 0),
  8498. BPF_EXIT_INSN(),
  8499. },
  8500. .result = ACCEPT,
  8501. .prog_type = BPF_PROG_TYPE_XDP,
  8502. },
  8503. {
  8504. "meta access, test8",
  8505. .insns = {
  8506. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8507. offsetof(struct xdp_md, data_meta)),
  8508. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8509. offsetof(struct xdp_md, data)),
  8510. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  8511. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 0xFFFF),
  8512. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
  8513. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8514. BPF_MOV64_IMM(BPF_REG_0, 0),
  8515. BPF_EXIT_INSN(),
  8516. },
  8517. .result = ACCEPT,
  8518. .prog_type = BPF_PROG_TYPE_XDP,
  8519. },
  8520. {
  8521. "meta access, test9",
  8522. .insns = {
  8523. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8524. offsetof(struct xdp_md, data_meta)),
  8525. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8526. offsetof(struct xdp_md, data)),
  8527. BPF_MOV64_REG(BPF_REG_4, BPF_REG_2),
  8528. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 0xFFFF),
  8529. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 1),
  8530. BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
  8531. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8532. BPF_MOV64_IMM(BPF_REG_0, 0),
  8533. BPF_EXIT_INSN(),
  8534. },
  8535. .result = REJECT,
  8536. .errstr = "invalid access to packet",
  8537. .prog_type = BPF_PROG_TYPE_XDP,
  8538. },
  8539. {
  8540. "meta access, test10",
  8541. .insns = {
  8542. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8543. offsetof(struct xdp_md, data_meta)),
  8544. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8545. offsetof(struct xdp_md, data)),
  8546. BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,
  8547. offsetof(struct xdp_md, data_end)),
  8548. BPF_MOV64_IMM(BPF_REG_5, 42),
  8549. BPF_MOV64_IMM(BPF_REG_6, 24),
  8550. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_5, -8),
  8551. BPF_STX_XADD(BPF_DW, BPF_REG_10, BPF_REG_6, -8),
  8552. BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_10, -8),
  8553. BPF_JMP_IMM(BPF_JGT, BPF_REG_5, 100, 6),
  8554. BPF_ALU64_REG(BPF_ADD, BPF_REG_3, BPF_REG_5),
  8555. BPF_MOV64_REG(BPF_REG_5, BPF_REG_3),
  8556. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  8557. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 8),
  8558. BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_5, 1),
  8559. BPF_LDX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
  8560. BPF_MOV64_IMM(BPF_REG_0, 0),
  8561. BPF_EXIT_INSN(),
  8562. },
  8563. .result = REJECT,
  8564. .errstr = "invalid access to packet",
  8565. .prog_type = BPF_PROG_TYPE_XDP,
  8566. },
  8567. {
  8568. "meta access, test11",
  8569. .insns = {
  8570. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8571. offsetof(struct xdp_md, data_meta)),
  8572. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8573. offsetof(struct xdp_md, data)),
  8574. BPF_MOV64_IMM(BPF_REG_5, 42),
  8575. BPF_MOV64_IMM(BPF_REG_6, 24),
  8576. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_5, -8),
  8577. BPF_STX_XADD(BPF_DW, BPF_REG_10, BPF_REG_6, -8),
  8578. BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_10, -8),
  8579. BPF_JMP_IMM(BPF_JGT, BPF_REG_5, 100, 6),
  8580. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_5),
  8581. BPF_MOV64_REG(BPF_REG_5, BPF_REG_2),
  8582. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  8583. BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 8),
  8584. BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_3, 1),
  8585. BPF_LDX_MEM(BPF_B, BPF_REG_5, BPF_REG_5, 0),
  8586. BPF_MOV64_IMM(BPF_REG_0, 0),
  8587. BPF_EXIT_INSN(),
  8588. },
  8589. .result = ACCEPT,
  8590. .prog_type = BPF_PROG_TYPE_XDP,
  8591. },
  8592. {
  8593. "meta access, test12",
  8594. .insns = {
  8595. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8596. offsetof(struct xdp_md, data_meta)),
  8597. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8598. offsetof(struct xdp_md, data)),
  8599. BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,
  8600. offsetof(struct xdp_md, data_end)),
  8601. BPF_MOV64_REG(BPF_REG_5, BPF_REG_3),
  8602. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 16),
  8603. BPF_JMP_REG(BPF_JGT, BPF_REG_5, BPF_REG_4, 5),
  8604. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_3, 0),
  8605. BPF_MOV64_REG(BPF_REG_5, BPF_REG_2),
  8606. BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, 16),
  8607. BPF_JMP_REG(BPF_JGT, BPF_REG_5, BPF_REG_3, 1),
  8608. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_2, 0),
  8609. BPF_MOV64_IMM(BPF_REG_0, 0),
  8610. BPF_EXIT_INSN(),
  8611. },
  8612. .result = ACCEPT,
  8613. .prog_type = BPF_PROG_TYPE_XDP,
  8614. },
  8615. {
  8616. "arithmetic ops make PTR_TO_CTX unusable",
  8617. .insns = {
  8618. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1,
  8619. offsetof(struct __sk_buff, data) -
  8620. offsetof(struct __sk_buff, mark)),
  8621. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  8622. offsetof(struct __sk_buff, mark)),
  8623. BPF_EXIT_INSN(),
  8624. },
  8625. .errstr = "dereference of modified ctx ptr R1 off=68+8, ctx+const is allowed, ctx+const+const is not",
  8626. .result = REJECT,
  8627. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  8628. },
  8629. {
  8630. "pkt_end - pkt_start is allowed",
  8631. .insns = {
  8632. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  8633. offsetof(struct __sk_buff, data_end)),
  8634. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8635. offsetof(struct __sk_buff, data)),
  8636. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_2),
  8637. BPF_EXIT_INSN(),
  8638. },
  8639. .result = ACCEPT,
  8640. .retval = TEST_DATA_LEN,
  8641. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  8642. },
  8643. {
  8644. "XDP pkt read, pkt_end mangling, bad access 1",
  8645. .insns = {
  8646. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8647. offsetof(struct xdp_md, data)),
  8648. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8649. offsetof(struct xdp_md, data_end)),
  8650. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8651. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8652. BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 8),
  8653. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  8654. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8655. BPF_MOV64_IMM(BPF_REG_0, 0),
  8656. BPF_EXIT_INSN(),
  8657. },
  8658. .errstr = "R3 pointer arithmetic on PTR_TO_PACKET_END",
  8659. .result = REJECT,
  8660. .prog_type = BPF_PROG_TYPE_XDP,
  8661. },
  8662. {
  8663. "XDP pkt read, pkt_end mangling, bad access 2",
  8664. .insns = {
  8665. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8666. offsetof(struct xdp_md, data)),
  8667. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8668. offsetof(struct xdp_md, data_end)),
  8669. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8670. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8671. BPF_ALU64_IMM(BPF_SUB, BPF_REG_3, 8),
  8672. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  8673. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8674. BPF_MOV64_IMM(BPF_REG_0, 0),
  8675. BPF_EXIT_INSN(),
  8676. },
  8677. .errstr = "R3 pointer arithmetic on PTR_TO_PACKET_END",
  8678. .result = REJECT,
  8679. .prog_type = BPF_PROG_TYPE_XDP,
  8680. },
  8681. {
  8682. "XDP pkt read, pkt_data' > pkt_end, good access",
  8683. .insns = {
  8684. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8685. offsetof(struct xdp_md, data)),
  8686. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8687. offsetof(struct xdp_md, data_end)),
  8688. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8689. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8690. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  8691. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8692. BPF_MOV64_IMM(BPF_REG_0, 0),
  8693. BPF_EXIT_INSN(),
  8694. },
  8695. .result = ACCEPT,
  8696. .prog_type = BPF_PROG_TYPE_XDP,
  8697. },
  8698. {
  8699. "XDP pkt read, pkt_data' > pkt_end, bad access 1",
  8700. .insns = {
  8701. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8702. offsetof(struct xdp_md, data)),
  8703. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8704. offsetof(struct xdp_md, data_end)),
  8705. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8706. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8707. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  8708. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  8709. BPF_MOV64_IMM(BPF_REG_0, 0),
  8710. BPF_EXIT_INSN(),
  8711. },
  8712. .errstr = "R1 offset is outside of the packet",
  8713. .result = REJECT,
  8714. .prog_type = BPF_PROG_TYPE_XDP,
  8715. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8716. },
  8717. {
  8718. "XDP pkt read, pkt_data' > pkt_end, bad access 2",
  8719. .insns = {
  8720. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8721. offsetof(struct xdp_md, data)),
  8722. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8723. offsetof(struct xdp_md, data_end)),
  8724. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8725. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8726. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 0),
  8727. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8728. BPF_MOV64_IMM(BPF_REG_0, 0),
  8729. BPF_EXIT_INSN(),
  8730. },
  8731. .errstr = "R1 offset is outside of the packet",
  8732. .result = REJECT,
  8733. .prog_type = BPF_PROG_TYPE_XDP,
  8734. },
  8735. {
  8736. "XDP pkt read, pkt_end > pkt_data', good access",
  8737. .insns = {
  8738. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8739. offsetof(struct xdp_md, data)),
  8740. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8741. offsetof(struct xdp_md, data_end)),
  8742. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8743. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8744. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_1, 1),
  8745. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  8746. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  8747. BPF_MOV64_IMM(BPF_REG_0, 0),
  8748. BPF_EXIT_INSN(),
  8749. },
  8750. .result = ACCEPT,
  8751. .prog_type = BPF_PROG_TYPE_XDP,
  8752. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8753. },
  8754. {
  8755. "XDP pkt read, pkt_end > pkt_data', bad access 1",
  8756. .insns = {
  8757. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8758. offsetof(struct xdp_md, data)),
  8759. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8760. offsetof(struct xdp_md, data_end)),
  8761. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8762. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8763. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_1, 1),
  8764. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  8765. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8766. BPF_MOV64_IMM(BPF_REG_0, 0),
  8767. BPF_EXIT_INSN(),
  8768. },
  8769. .errstr = "R1 offset is outside of the packet",
  8770. .result = REJECT,
  8771. .prog_type = BPF_PROG_TYPE_XDP,
  8772. },
  8773. {
  8774. "XDP pkt read, pkt_end > pkt_data', bad access 2",
  8775. .insns = {
  8776. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8777. offsetof(struct xdp_md, data)),
  8778. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8779. offsetof(struct xdp_md, data_end)),
  8780. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8781. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8782. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_1, 1),
  8783. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8784. BPF_MOV64_IMM(BPF_REG_0, 0),
  8785. BPF_EXIT_INSN(),
  8786. },
  8787. .errstr = "R1 offset is outside of the packet",
  8788. .result = REJECT,
  8789. .prog_type = BPF_PROG_TYPE_XDP,
  8790. },
  8791. {
  8792. "XDP pkt read, pkt_data' < pkt_end, good access",
  8793. .insns = {
  8794. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8795. offsetof(struct xdp_md, data)),
  8796. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8797. offsetof(struct xdp_md, data_end)),
  8798. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8799. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8800. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 1),
  8801. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  8802. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  8803. BPF_MOV64_IMM(BPF_REG_0, 0),
  8804. BPF_EXIT_INSN(),
  8805. },
  8806. .result = ACCEPT,
  8807. .prog_type = BPF_PROG_TYPE_XDP,
  8808. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8809. },
  8810. {
  8811. "XDP pkt read, pkt_data' < pkt_end, bad access 1",
  8812. .insns = {
  8813. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8814. offsetof(struct xdp_md, data)),
  8815. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8816. offsetof(struct xdp_md, data_end)),
  8817. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8818. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8819. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 1),
  8820. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  8821. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8822. BPF_MOV64_IMM(BPF_REG_0, 0),
  8823. BPF_EXIT_INSN(),
  8824. },
  8825. .errstr = "R1 offset is outside of the packet",
  8826. .result = REJECT,
  8827. .prog_type = BPF_PROG_TYPE_XDP,
  8828. },
  8829. {
  8830. "XDP pkt read, pkt_data' < pkt_end, bad access 2",
  8831. .insns = {
  8832. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8833. offsetof(struct xdp_md, data)),
  8834. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8835. offsetof(struct xdp_md, data_end)),
  8836. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8837. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8838. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 1),
  8839. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8840. BPF_MOV64_IMM(BPF_REG_0, 0),
  8841. BPF_EXIT_INSN(),
  8842. },
  8843. .errstr = "R1 offset is outside of the packet",
  8844. .result = REJECT,
  8845. .prog_type = BPF_PROG_TYPE_XDP,
  8846. },
  8847. {
  8848. "XDP pkt read, pkt_end < pkt_data', good access",
  8849. .insns = {
  8850. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8851. offsetof(struct xdp_md, data)),
  8852. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8853. offsetof(struct xdp_md, data_end)),
  8854. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8855. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8856. BPF_JMP_REG(BPF_JLT, BPF_REG_3, BPF_REG_1, 1),
  8857. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8858. BPF_MOV64_IMM(BPF_REG_0, 0),
  8859. BPF_EXIT_INSN(),
  8860. },
  8861. .result = ACCEPT,
  8862. .prog_type = BPF_PROG_TYPE_XDP,
  8863. },
  8864. {
  8865. "XDP pkt read, pkt_end < pkt_data', bad access 1",
  8866. .insns = {
  8867. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8868. offsetof(struct xdp_md, data)),
  8869. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8870. offsetof(struct xdp_md, data_end)),
  8871. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8872. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8873. BPF_JMP_REG(BPF_JLT, BPF_REG_3, BPF_REG_1, 1),
  8874. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  8875. BPF_MOV64_IMM(BPF_REG_0, 0),
  8876. BPF_EXIT_INSN(),
  8877. },
  8878. .errstr = "R1 offset is outside of the packet",
  8879. .result = REJECT,
  8880. .prog_type = BPF_PROG_TYPE_XDP,
  8881. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8882. },
  8883. {
  8884. "XDP pkt read, pkt_end < pkt_data', bad access 2",
  8885. .insns = {
  8886. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8887. offsetof(struct xdp_md, data)),
  8888. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8889. offsetof(struct xdp_md, data_end)),
  8890. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8891. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8892. BPF_JMP_REG(BPF_JLT, BPF_REG_3, BPF_REG_1, 0),
  8893. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8894. BPF_MOV64_IMM(BPF_REG_0, 0),
  8895. BPF_EXIT_INSN(),
  8896. },
  8897. .errstr = "R1 offset is outside of the packet",
  8898. .result = REJECT,
  8899. .prog_type = BPF_PROG_TYPE_XDP,
  8900. },
  8901. {
  8902. "XDP pkt read, pkt_data' >= pkt_end, good access",
  8903. .insns = {
  8904. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8905. offsetof(struct xdp_md, data)),
  8906. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8907. offsetof(struct xdp_md, data_end)),
  8908. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8909. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8910. BPF_JMP_REG(BPF_JGE, BPF_REG_1, BPF_REG_3, 1),
  8911. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  8912. BPF_MOV64_IMM(BPF_REG_0, 0),
  8913. BPF_EXIT_INSN(),
  8914. },
  8915. .result = ACCEPT,
  8916. .prog_type = BPF_PROG_TYPE_XDP,
  8917. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8918. },
  8919. {
  8920. "XDP pkt read, pkt_data' >= pkt_end, bad access 1",
  8921. .insns = {
  8922. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8923. offsetof(struct xdp_md, data)),
  8924. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8925. offsetof(struct xdp_md, data_end)),
  8926. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8927. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8928. BPF_JMP_REG(BPF_JGE, BPF_REG_1, BPF_REG_3, 1),
  8929. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8930. BPF_MOV64_IMM(BPF_REG_0, 0),
  8931. BPF_EXIT_INSN(),
  8932. },
  8933. .errstr = "R1 offset is outside of the packet",
  8934. .result = REJECT,
  8935. .prog_type = BPF_PROG_TYPE_XDP,
  8936. },
  8937. {
  8938. "XDP pkt read, pkt_data' >= pkt_end, bad access 2",
  8939. .insns = {
  8940. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8941. offsetof(struct xdp_md, data)),
  8942. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8943. offsetof(struct xdp_md, data_end)),
  8944. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8945. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8946. BPF_JMP_REG(BPF_JGE, BPF_REG_1, BPF_REG_3, 0),
  8947. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  8948. BPF_MOV64_IMM(BPF_REG_0, 0),
  8949. BPF_EXIT_INSN(),
  8950. },
  8951. .errstr = "R1 offset is outside of the packet",
  8952. .result = REJECT,
  8953. .prog_type = BPF_PROG_TYPE_XDP,
  8954. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8955. },
  8956. {
  8957. "XDP pkt read, pkt_end >= pkt_data', good access",
  8958. .insns = {
  8959. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8960. offsetof(struct xdp_md, data)),
  8961. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8962. offsetof(struct xdp_md, data_end)),
  8963. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8964. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8965. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 1),
  8966. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  8967. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  8968. BPF_MOV64_IMM(BPF_REG_0, 0),
  8969. BPF_EXIT_INSN(),
  8970. },
  8971. .result = ACCEPT,
  8972. .prog_type = BPF_PROG_TYPE_XDP,
  8973. },
  8974. {
  8975. "XDP pkt read, pkt_end >= pkt_data', bad access 1",
  8976. .insns = {
  8977. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8978. offsetof(struct xdp_md, data)),
  8979. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  8980. offsetof(struct xdp_md, data_end)),
  8981. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  8982. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  8983. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 1),
  8984. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  8985. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  8986. BPF_MOV64_IMM(BPF_REG_0, 0),
  8987. BPF_EXIT_INSN(),
  8988. },
  8989. .errstr = "R1 offset is outside of the packet",
  8990. .result = REJECT,
  8991. .prog_type = BPF_PROG_TYPE_XDP,
  8992. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  8993. },
  8994. {
  8995. "XDP pkt read, pkt_end >= pkt_data', bad access 2",
  8996. .insns = {
  8997. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  8998. offsetof(struct xdp_md, data)),
  8999. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9000. offsetof(struct xdp_md, data_end)),
  9001. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9002. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9003. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 1),
  9004. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9005. BPF_MOV64_IMM(BPF_REG_0, 0),
  9006. BPF_EXIT_INSN(),
  9007. },
  9008. .errstr = "R1 offset is outside of the packet",
  9009. .result = REJECT,
  9010. .prog_type = BPF_PROG_TYPE_XDP,
  9011. },
  9012. {
  9013. "XDP pkt read, pkt_data' <= pkt_end, good access",
  9014. .insns = {
  9015. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9016. offsetof(struct xdp_md, data)),
  9017. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9018. offsetof(struct xdp_md, data_end)),
  9019. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9020. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9021. BPF_JMP_REG(BPF_JLE, BPF_REG_1, BPF_REG_3, 1),
  9022. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9023. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9024. BPF_MOV64_IMM(BPF_REG_0, 0),
  9025. BPF_EXIT_INSN(),
  9026. },
  9027. .result = ACCEPT,
  9028. .prog_type = BPF_PROG_TYPE_XDP,
  9029. },
  9030. {
  9031. "XDP pkt read, pkt_data' <= pkt_end, bad access 1",
  9032. .insns = {
  9033. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9034. offsetof(struct xdp_md, data)),
  9035. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9036. offsetof(struct xdp_md, data_end)),
  9037. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9038. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9039. BPF_JMP_REG(BPF_JLE, BPF_REG_1, BPF_REG_3, 1),
  9040. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9041. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  9042. BPF_MOV64_IMM(BPF_REG_0, 0),
  9043. BPF_EXIT_INSN(),
  9044. },
  9045. .errstr = "R1 offset is outside of the packet",
  9046. .result = REJECT,
  9047. .prog_type = BPF_PROG_TYPE_XDP,
  9048. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9049. },
  9050. {
  9051. "XDP pkt read, pkt_data' <= pkt_end, bad access 2",
  9052. .insns = {
  9053. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9054. offsetof(struct xdp_md, data)),
  9055. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9056. offsetof(struct xdp_md, data_end)),
  9057. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9058. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9059. BPF_JMP_REG(BPF_JLE, BPF_REG_1, BPF_REG_3, 1),
  9060. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9061. BPF_MOV64_IMM(BPF_REG_0, 0),
  9062. BPF_EXIT_INSN(),
  9063. },
  9064. .errstr = "R1 offset is outside of the packet",
  9065. .result = REJECT,
  9066. .prog_type = BPF_PROG_TYPE_XDP,
  9067. },
  9068. {
  9069. "XDP pkt read, pkt_end <= pkt_data', good access",
  9070. .insns = {
  9071. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9072. offsetof(struct xdp_md, data)),
  9073. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9074. offsetof(struct xdp_md, data_end)),
  9075. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9076. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9077. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_1, 1),
  9078. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9079. BPF_MOV64_IMM(BPF_REG_0, 0),
  9080. BPF_EXIT_INSN(),
  9081. },
  9082. .result = ACCEPT,
  9083. .prog_type = BPF_PROG_TYPE_XDP,
  9084. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9085. },
  9086. {
  9087. "XDP pkt read, pkt_end <= pkt_data', bad access 1",
  9088. .insns = {
  9089. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9090. offsetof(struct xdp_md, data)),
  9091. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9092. offsetof(struct xdp_md, data_end)),
  9093. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9094. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9095. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_1, 1),
  9096. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9097. BPF_MOV64_IMM(BPF_REG_0, 0),
  9098. BPF_EXIT_INSN(),
  9099. },
  9100. .errstr = "R1 offset is outside of the packet",
  9101. .result = REJECT,
  9102. .prog_type = BPF_PROG_TYPE_XDP,
  9103. },
  9104. {
  9105. "XDP pkt read, pkt_end <= pkt_data', bad access 2",
  9106. .insns = {
  9107. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9108. offsetof(struct xdp_md, data)),
  9109. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9110. offsetof(struct xdp_md, data_end)),
  9111. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9112. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9113. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_1, 0),
  9114. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9115. BPF_MOV64_IMM(BPF_REG_0, 0),
  9116. BPF_EXIT_INSN(),
  9117. },
  9118. .errstr = "R1 offset is outside of the packet",
  9119. .result = REJECT,
  9120. .prog_type = BPF_PROG_TYPE_XDP,
  9121. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9122. },
  9123. {
  9124. "XDP pkt read, pkt_meta' > pkt_data, good access",
  9125. .insns = {
  9126. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9127. offsetof(struct xdp_md, data_meta)),
  9128. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9129. offsetof(struct xdp_md, data)),
  9130. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9131. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9132. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  9133. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9134. BPF_MOV64_IMM(BPF_REG_0, 0),
  9135. BPF_EXIT_INSN(),
  9136. },
  9137. .result = ACCEPT,
  9138. .prog_type = BPF_PROG_TYPE_XDP,
  9139. },
  9140. {
  9141. "XDP pkt read, pkt_meta' > pkt_data, bad access 1",
  9142. .insns = {
  9143. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9144. offsetof(struct xdp_md, data_meta)),
  9145. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9146. offsetof(struct xdp_md, data)),
  9147. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9148. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9149. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 1),
  9150. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  9151. BPF_MOV64_IMM(BPF_REG_0, 0),
  9152. BPF_EXIT_INSN(),
  9153. },
  9154. .errstr = "R1 offset is outside of the packet",
  9155. .result = REJECT,
  9156. .prog_type = BPF_PROG_TYPE_XDP,
  9157. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9158. },
  9159. {
  9160. "XDP pkt read, pkt_meta' > pkt_data, bad access 2",
  9161. .insns = {
  9162. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9163. offsetof(struct xdp_md, data_meta)),
  9164. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9165. offsetof(struct xdp_md, data)),
  9166. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9167. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9168. BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_3, 0),
  9169. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9170. BPF_MOV64_IMM(BPF_REG_0, 0),
  9171. BPF_EXIT_INSN(),
  9172. },
  9173. .errstr = "R1 offset is outside of the packet",
  9174. .result = REJECT,
  9175. .prog_type = BPF_PROG_TYPE_XDP,
  9176. },
  9177. {
  9178. "XDP pkt read, pkt_data > pkt_meta', good access",
  9179. .insns = {
  9180. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9181. offsetof(struct xdp_md, data_meta)),
  9182. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9183. offsetof(struct xdp_md, data)),
  9184. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9185. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9186. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_1, 1),
  9187. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9188. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9189. BPF_MOV64_IMM(BPF_REG_0, 0),
  9190. BPF_EXIT_INSN(),
  9191. },
  9192. .result = ACCEPT,
  9193. .prog_type = BPF_PROG_TYPE_XDP,
  9194. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9195. },
  9196. {
  9197. "XDP pkt read, pkt_data > pkt_meta', bad access 1",
  9198. .insns = {
  9199. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9200. offsetof(struct xdp_md, data_meta)),
  9201. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9202. offsetof(struct xdp_md, data)),
  9203. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9204. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9205. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_1, 1),
  9206. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9207. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9208. BPF_MOV64_IMM(BPF_REG_0, 0),
  9209. BPF_EXIT_INSN(),
  9210. },
  9211. .errstr = "R1 offset is outside of the packet",
  9212. .result = REJECT,
  9213. .prog_type = BPF_PROG_TYPE_XDP,
  9214. },
  9215. {
  9216. "XDP pkt read, pkt_data > pkt_meta', bad access 2",
  9217. .insns = {
  9218. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9219. offsetof(struct xdp_md, data_meta)),
  9220. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9221. offsetof(struct xdp_md, data)),
  9222. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9223. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9224. BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_1, 1),
  9225. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9226. BPF_MOV64_IMM(BPF_REG_0, 0),
  9227. BPF_EXIT_INSN(),
  9228. },
  9229. .errstr = "R1 offset is outside of the packet",
  9230. .result = REJECT,
  9231. .prog_type = BPF_PROG_TYPE_XDP,
  9232. },
  9233. {
  9234. "XDP pkt read, pkt_meta' < pkt_data, good access",
  9235. .insns = {
  9236. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9237. offsetof(struct xdp_md, data_meta)),
  9238. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9239. offsetof(struct xdp_md, data)),
  9240. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9241. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9242. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 1),
  9243. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9244. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9245. BPF_MOV64_IMM(BPF_REG_0, 0),
  9246. BPF_EXIT_INSN(),
  9247. },
  9248. .result = ACCEPT,
  9249. .prog_type = BPF_PROG_TYPE_XDP,
  9250. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9251. },
  9252. {
  9253. "XDP pkt read, pkt_meta' < pkt_data, bad access 1",
  9254. .insns = {
  9255. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9256. offsetof(struct xdp_md, data_meta)),
  9257. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9258. offsetof(struct xdp_md, data)),
  9259. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9260. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9261. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 1),
  9262. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9263. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9264. BPF_MOV64_IMM(BPF_REG_0, 0),
  9265. BPF_EXIT_INSN(),
  9266. },
  9267. .errstr = "R1 offset is outside of the packet",
  9268. .result = REJECT,
  9269. .prog_type = BPF_PROG_TYPE_XDP,
  9270. },
  9271. {
  9272. "XDP pkt read, pkt_meta' < pkt_data, bad access 2",
  9273. .insns = {
  9274. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9275. offsetof(struct xdp_md, data_meta)),
  9276. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9277. offsetof(struct xdp_md, data)),
  9278. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9279. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9280. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 1),
  9281. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9282. BPF_MOV64_IMM(BPF_REG_0, 0),
  9283. BPF_EXIT_INSN(),
  9284. },
  9285. .errstr = "R1 offset is outside of the packet",
  9286. .result = REJECT,
  9287. .prog_type = BPF_PROG_TYPE_XDP,
  9288. },
  9289. {
  9290. "XDP pkt read, pkt_data < pkt_meta', good access",
  9291. .insns = {
  9292. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9293. offsetof(struct xdp_md, data_meta)),
  9294. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9295. offsetof(struct xdp_md, data)),
  9296. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9297. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9298. BPF_JMP_REG(BPF_JLT, BPF_REG_3, BPF_REG_1, 1),
  9299. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9300. BPF_MOV64_IMM(BPF_REG_0, 0),
  9301. BPF_EXIT_INSN(),
  9302. },
  9303. .result = ACCEPT,
  9304. .prog_type = BPF_PROG_TYPE_XDP,
  9305. },
  9306. {
  9307. "XDP pkt read, pkt_data < pkt_meta', bad access 1",
  9308. .insns = {
  9309. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9310. offsetof(struct xdp_md, data_meta)),
  9311. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9312. offsetof(struct xdp_md, data)),
  9313. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9314. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9315. BPF_JMP_REG(BPF_JLT, BPF_REG_3, BPF_REG_1, 1),
  9316. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  9317. BPF_MOV64_IMM(BPF_REG_0, 0),
  9318. BPF_EXIT_INSN(),
  9319. },
  9320. .errstr = "R1 offset is outside of the packet",
  9321. .result = REJECT,
  9322. .prog_type = BPF_PROG_TYPE_XDP,
  9323. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9324. },
  9325. {
  9326. "XDP pkt read, pkt_data < pkt_meta', bad access 2",
  9327. .insns = {
  9328. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9329. offsetof(struct xdp_md, data_meta)),
  9330. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9331. offsetof(struct xdp_md, data)),
  9332. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9333. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9334. BPF_JMP_REG(BPF_JLT, BPF_REG_3, BPF_REG_1, 0),
  9335. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9336. BPF_MOV64_IMM(BPF_REG_0, 0),
  9337. BPF_EXIT_INSN(),
  9338. },
  9339. .errstr = "R1 offset is outside of the packet",
  9340. .result = REJECT,
  9341. .prog_type = BPF_PROG_TYPE_XDP,
  9342. },
  9343. {
  9344. "XDP pkt read, pkt_meta' >= pkt_data, good access",
  9345. .insns = {
  9346. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9347. offsetof(struct xdp_md, data_meta)),
  9348. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9349. offsetof(struct xdp_md, data)),
  9350. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9351. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9352. BPF_JMP_REG(BPF_JGE, BPF_REG_1, BPF_REG_3, 1),
  9353. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9354. BPF_MOV64_IMM(BPF_REG_0, 0),
  9355. BPF_EXIT_INSN(),
  9356. },
  9357. .result = ACCEPT,
  9358. .prog_type = BPF_PROG_TYPE_XDP,
  9359. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9360. },
  9361. {
  9362. "XDP pkt read, pkt_meta' >= pkt_data, bad access 1",
  9363. .insns = {
  9364. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9365. offsetof(struct xdp_md, data_meta)),
  9366. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9367. offsetof(struct xdp_md, data)),
  9368. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9369. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9370. BPF_JMP_REG(BPF_JGE, BPF_REG_1, BPF_REG_3, 1),
  9371. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9372. BPF_MOV64_IMM(BPF_REG_0, 0),
  9373. BPF_EXIT_INSN(),
  9374. },
  9375. .errstr = "R1 offset is outside of the packet",
  9376. .result = REJECT,
  9377. .prog_type = BPF_PROG_TYPE_XDP,
  9378. },
  9379. {
  9380. "XDP pkt read, pkt_meta' >= pkt_data, bad access 2",
  9381. .insns = {
  9382. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9383. offsetof(struct xdp_md, data_meta)),
  9384. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9385. offsetof(struct xdp_md, data)),
  9386. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9387. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9388. BPF_JMP_REG(BPF_JGE, BPF_REG_1, BPF_REG_3, 0),
  9389. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9390. BPF_MOV64_IMM(BPF_REG_0, 0),
  9391. BPF_EXIT_INSN(),
  9392. },
  9393. .errstr = "R1 offset is outside of the packet",
  9394. .result = REJECT,
  9395. .prog_type = BPF_PROG_TYPE_XDP,
  9396. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9397. },
  9398. {
  9399. "XDP pkt read, pkt_data >= pkt_meta', good access",
  9400. .insns = {
  9401. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9402. offsetof(struct xdp_md, data_meta)),
  9403. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9404. offsetof(struct xdp_md, data)),
  9405. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9406. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9407. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 1),
  9408. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9409. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9410. BPF_MOV64_IMM(BPF_REG_0, 0),
  9411. BPF_EXIT_INSN(),
  9412. },
  9413. .result = ACCEPT,
  9414. .prog_type = BPF_PROG_TYPE_XDP,
  9415. },
  9416. {
  9417. "XDP pkt read, pkt_data >= pkt_meta', bad access 1",
  9418. .insns = {
  9419. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9420. offsetof(struct xdp_md, data_meta)),
  9421. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9422. offsetof(struct xdp_md, data)),
  9423. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9424. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9425. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 1),
  9426. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9427. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  9428. BPF_MOV64_IMM(BPF_REG_0, 0),
  9429. BPF_EXIT_INSN(),
  9430. },
  9431. .errstr = "R1 offset is outside of the packet",
  9432. .result = REJECT,
  9433. .prog_type = BPF_PROG_TYPE_XDP,
  9434. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9435. },
  9436. {
  9437. "XDP pkt read, pkt_data >= pkt_meta', bad access 2",
  9438. .insns = {
  9439. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9440. offsetof(struct xdp_md, data_meta)),
  9441. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9442. offsetof(struct xdp_md, data)),
  9443. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9444. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9445. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 1),
  9446. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9447. BPF_MOV64_IMM(BPF_REG_0, 0),
  9448. BPF_EXIT_INSN(),
  9449. },
  9450. .errstr = "R1 offset is outside of the packet",
  9451. .result = REJECT,
  9452. .prog_type = BPF_PROG_TYPE_XDP,
  9453. },
  9454. {
  9455. "XDP pkt read, pkt_meta' <= pkt_data, good access",
  9456. .insns = {
  9457. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9458. offsetof(struct xdp_md, data_meta)),
  9459. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9460. offsetof(struct xdp_md, data)),
  9461. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9462. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9463. BPF_JMP_REG(BPF_JLE, BPF_REG_1, BPF_REG_3, 1),
  9464. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9465. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9466. BPF_MOV64_IMM(BPF_REG_0, 0),
  9467. BPF_EXIT_INSN(),
  9468. },
  9469. .result = ACCEPT,
  9470. .prog_type = BPF_PROG_TYPE_XDP,
  9471. },
  9472. {
  9473. "XDP pkt read, pkt_meta' <= pkt_data, bad access 1",
  9474. .insns = {
  9475. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9476. offsetof(struct xdp_md, data_meta)),
  9477. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9478. offsetof(struct xdp_md, data)),
  9479. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9480. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9481. BPF_JMP_REG(BPF_JLE, BPF_REG_1, BPF_REG_3, 1),
  9482. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  9483. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -4),
  9484. BPF_MOV64_IMM(BPF_REG_0, 0),
  9485. BPF_EXIT_INSN(),
  9486. },
  9487. .errstr = "R1 offset is outside of the packet",
  9488. .result = REJECT,
  9489. .prog_type = BPF_PROG_TYPE_XDP,
  9490. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9491. },
  9492. {
  9493. "XDP pkt read, pkt_meta' <= pkt_data, bad access 2",
  9494. .insns = {
  9495. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9496. offsetof(struct xdp_md, data_meta)),
  9497. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9498. offsetof(struct xdp_md, data)),
  9499. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9500. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9501. BPF_JMP_REG(BPF_JLE, BPF_REG_1, BPF_REG_3, 1),
  9502. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9503. BPF_MOV64_IMM(BPF_REG_0, 0),
  9504. BPF_EXIT_INSN(),
  9505. },
  9506. .errstr = "R1 offset is outside of the packet",
  9507. .result = REJECT,
  9508. .prog_type = BPF_PROG_TYPE_XDP,
  9509. },
  9510. {
  9511. "XDP pkt read, pkt_data <= pkt_meta', good access",
  9512. .insns = {
  9513. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9514. offsetof(struct xdp_md, data_meta)),
  9515. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9516. offsetof(struct xdp_md, data)),
  9517. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9518. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9519. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_1, 1),
  9520. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9521. BPF_MOV64_IMM(BPF_REG_0, 0),
  9522. BPF_EXIT_INSN(),
  9523. },
  9524. .result = ACCEPT,
  9525. .prog_type = BPF_PROG_TYPE_XDP,
  9526. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9527. },
  9528. {
  9529. "XDP pkt read, pkt_data <= pkt_meta', bad access 1",
  9530. .insns = {
  9531. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9532. offsetof(struct xdp_md, data_meta)),
  9533. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9534. offsetof(struct xdp_md, data)),
  9535. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9536. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9537. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_1, 1),
  9538. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8),
  9539. BPF_MOV64_IMM(BPF_REG_0, 0),
  9540. BPF_EXIT_INSN(),
  9541. },
  9542. .errstr = "R1 offset is outside of the packet",
  9543. .result = REJECT,
  9544. .prog_type = BPF_PROG_TYPE_XDP,
  9545. },
  9546. {
  9547. "XDP pkt read, pkt_data <= pkt_meta', bad access 2",
  9548. .insns = {
  9549. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  9550. offsetof(struct xdp_md, data_meta)),
  9551. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  9552. offsetof(struct xdp_md, data)),
  9553. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  9554. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  9555. BPF_JMP_REG(BPF_JLE, BPF_REG_3, BPF_REG_1, 0),
  9556. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, -5),
  9557. BPF_MOV64_IMM(BPF_REG_0, 0),
  9558. BPF_EXIT_INSN(),
  9559. },
  9560. .errstr = "R1 offset is outside of the packet",
  9561. .result = REJECT,
  9562. .prog_type = BPF_PROG_TYPE_XDP,
  9563. .flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
  9564. },
  9565. {
  9566. "check deducing bounds from const, 1",
  9567. .insns = {
  9568. BPF_MOV64_IMM(BPF_REG_0, 1),
  9569. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 1, 0),
  9570. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  9571. BPF_EXIT_INSN(),
  9572. },
  9573. .result = REJECT,
  9574. .errstr = "R0 tried to subtract pointer from scalar",
  9575. },
  9576. {
  9577. "check deducing bounds from const, 2",
  9578. .insns = {
  9579. BPF_MOV64_IMM(BPF_REG_0, 1),
  9580. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 1, 1),
  9581. BPF_EXIT_INSN(),
  9582. BPF_JMP_IMM(BPF_JSLE, BPF_REG_0, 1, 1),
  9583. BPF_EXIT_INSN(),
  9584. BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_0),
  9585. BPF_EXIT_INSN(),
  9586. },
  9587. .result = ACCEPT,
  9588. .retval = 1,
  9589. },
  9590. {
  9591. "check deducing bounds from const, 3",
  9592. .insns = {
  9593. BPF_MOV64_IMM(BPF_REG_0, 0),
  9594. BPF_JMP_IMM(BPF_JSLE, BPF_REG_0, 0, 0),
  9595. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  9596. BPF_EXIT_INSN(),
  9597. },
  9598. .result = REJECT,
  9599. .errstr = "R0 tried to subtract pointer from scalar",
  9600. },
  9601. {
  9602. "check deducing bounds from const, 4",
  9603. .insns = {
  9604. BPF_MOV64_IMM(BPF_REG_0, 0),
  9605. BPF_JMP_IMM(BPF_JSLE, BPF_REG_0, 0, 1),
  9606. BPF_EXIT_INSN(),
  9607. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 1),
  9608. BPF_EXIT_INSN(),
  9609. BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_0),
  9610. BPF_EXIT_INSN(),
  9611. },
  9612. .result = ACCEPT,
  9613. },
  9614. {
  9615. "check deducing bounds from const, 5",
  9616. .insns = {
  9617. BPF_MOV64_IMM(BPF_REG_0, 0),
  9618. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 1),
  9619. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  9620. BPF_EXIT_INSN(),
  9621. },
  9622. .result = REJECT,
  9623. .errstr = "R0 tried to subtract pointer from scalar",
  9624. },
  9625. {
  9626. "check deducing bounds from const, 6",
  9627. .insns = {
  9628. BPF_MOV64_IMM(BPF_REG_0, 0),
  9629. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 1),
  9630. BPF_EXIT_INSN(),
  9631. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  9632. BPF_EXIT_INSN(),
  9633. },
  9634. .result = REJECT,
  9635. .errstr = "R0 tried to subtract pointer from scalar",
  9636. },
  9637. {
  9638. "check deducing bounds from const, 7",
  9639. .insns = {
  9640. BPF_MOV64_IMM(BPF_REG_0, ~0),
  9641. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 0),
  9642. BPF_ALU64_REG(BPF_SUB, BPF_REG_1, BPF_REG_0),
  9643. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9644. offsetof(struct __sk_buff, mark)),
  9645. BPF_EXIT_INSN(),
  9646. },
  9647. .result = REJECT,
  9648. .errstr = "dereference of modified ctx ptr",
  9649. },
  9650. {
  9651. "check deducing bounds from const, 8",
  9652. .insns = {
  9653. BPF_MOV64_IMM(BPF_REG_0, ~0),
  9654. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 1),
  9655. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_0),
  9656. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9657. offsetof(struct __sk_buff, mark)),
  9658. BPF_EXIT_INSN(),
  9659. },
  9660. .result = REJECT,
  9661. .errstr = "dereference of modified ctx ptr",
  9662. },
  9663. {
  9664. "check deducing bounds from const, 9",
  9665. .insns = {
  9666. BPF_MOV64_IMM(BPF_REG_0, 0),
  9667. BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 0),
  9668. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  9669. BPF_EXIT_INSN(),
  9670. },
  9671. .result = REJECT,
  9672. .errstr = "R0 tried to subtract pointer from scalar",
  9673. },
  9674. {
  9675. "check deducing bounds from const, 10",
  9676. .insns = {
  9677. BPF_MOV64_IMM(BPF_REG_0, 0),
  9678. BPF_JMP_IMM(BPF_JSLE, BPF_REG_0, 0, 0),
  9679. /* Marks reg as unknown. */
  9680. BPF_ALU64_IMM(BPF_NEG, BPF_REG_0, 0),
  9681. BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1),
  9682. BPF_EXIT_INSN(),
  9683. },
  9684. .result = REJECT,
  9685. .errstr = "math between ctx pointer and register with unbounded min value is not allowed",
  9686. },
  9687. {
  9688. "bpf_exit with invalid return code. test1",
  9689. .insns = {
  9690. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  9691. BPF_EXIT_INSN(),
  9692. },
  9693. .errstr = "R0 has value (0x0; 0xffffffff)",
  9694. .result = REJECT,
  9695. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9696. },
  9697. {
  9698. "bpf_exit with invalid return code. test2",
  9699. .insns = {
  9700. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  9701. BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1),
  9702. BPF_EXIT_INSN(),
  9703. },
  9704. .result = ACCEPT,
  9705. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9706. },
  9707. {
  9708. "bpf_exit with invalid return code. test3",
  9709. .insns = {
  9710. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  9711. BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 3),
  9712. BPF_EXIT_INSN(),
  9713. },
  9714. .errstr = "R0 has value (0x0; 0x3)",
  9715. .result = REJECT,
  9716. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9717. },
  9718. {
  9719. "bpf_exit with invalid return code. test4",
  9720. .insns = {
  9721. BPF_MOV64_IMM(BPF_REG_0, 1),
  9722. BPF_EXIT_INSN(),
  9723. },
  9724. .result = ACCEPT,
  9725. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9726. },
  9727. {
  9728. "bpf_exit with invalid return code. test5",
  9729. .insns = {
  9730. BPF_MOV64_IMM(BPF_REG_0, 2),
  9731. BPF_EXIT_INSN(),
  9732. },
  9733. .errstr = "R0 has value (0x2; 0x0)",
  9734. .result = REJECT,
  9735. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9736. },
  9737. {
  9738. "bpf_exit with invalid return code. test6",
  9739. .insns = {
  9740. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  9741. BPF_EXIT_INSN(),
  9742. },
  9743. .errstr = "R0 is not a known value (ctx)",
  9744. .result = REJECT,
  9745. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9746. },
  9747. {
  9748. "bpf_exit with invalid return code. test7",
  9749. .insns = {
  9750. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  9751. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 4),
  9752. BPF_ALU64_REG(BPF_MUL, BPF_REG_0, BPF_REG_2),
  9753. BPF_EXIT_INSN(),
  9754. },
  9755. .errstr = "R0 has unknown scalar value",
  9756. .result = REJECT,
  9757. .prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
  9758. },
  9759. {
  9760. "calls: basic sanity",
  9761. .insns = {
  9762. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  9763. BPF_MOV64_IMM(BPF_REG_0, 1),
  9764. BPF_EXIT_INSN(),
  9765. BPF_MOV64_IMM(BPF_REG_0, 2),
  9766. BPF_EXIT_INSN(),
  9767. },
  9768. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9769. .result = ACCEPT,
  9770. },
  9771. {
  9772. "calls: not on unpriviledged",
  9773. .insns = {
  9774. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  9775. BPF_MOV64_IMM(BPF_REG_0, 1),
  9776. BPF_EXIT_INSN(),
  9777. BPF_MOV64_IMM(BPF_REG_0, 2),
  9778. BPF_EXIT_INSN(),
  9779. },
  9780. .errstr_unpriv = "function calls to other bpf functions are allowed for root only",
  9781. .result_unpriv = REJECT,
  9782. .result = ACCEPT,
  9783. .retval = 1,
  9784. },
  9785. {
  9786. "calls: div by 0 in subprog",
  9787. .insns = {
  9788. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  9789. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 8),
  9790. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  9791. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  9792. offsetof(struct __sk_buff, data_end)),
  9793. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  9794. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 8),
  9795. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 1),
  9796. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  9797. BPF_MOV64_IMM(BPF_REG_0, 1),
  9798. BPF_EXIT_INSN(),
  9799. BPF_MOV32_IMM(BPF_REG_2, 0),
  9800. BPF_MOV32_IMM(BPF_REG_3, 1),
  9801. BPF_ALU32_REG(BPF_DIV, BPF_REG_3, BPF_REG_2),
  9802. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9803. offsetof(struct __sk_buff, data)),
  9804. BPF_EXIT_INSN(),
  9805. },
  9806. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  9807. .result = ACCEPT,
  9808. .retval = 1,
  9809. },
  9810. {
  9811. "calls: multiple ret types in subprog 1",
  9812. .insns = {
  9813. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  9814. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 8),
  9815. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  9816. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  9817. offsetof(struct __sk_buff, data_end)),
  9818. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  9819. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 8),
  9820. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 1),
  9821. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  9822. BPF_MOV64_IMM(BPF_REG_0, 1),
  9823. BPF_EXIT_INSN(),
  9824. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9825. offsetof(struct __sk_buff, data)),
  9826. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  9827. BPF_MOV32_IMM(BPF_REG_0, 42),
  9828. BPF_EXIT_INSN(),
  9829. },
  9830. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  9831. .result = REJECT,
  9832. .errstr = "R0 invalid mem access 'inv'",
  9833. },
  9834. {
  9835. "calls: multiple ret types in subprog 2",
  9836. .insns = {
  9837. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  9838. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 8),
  9839. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  9840. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1,
  9841. offsetof(struct __sk_buff, data_end)),
  9842. BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
  9843. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 8),
  9844. BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_1, 1),
  9845. BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_0, 0),
  9846. BPF_MOV64_IMM(BPF_REG_0, 1),
  9847. BPF_EXIT_INSN(),
  9848. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9849. offsetof(struct __sk_buff, data)),
  9850. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  9851. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 9),
  9852. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  9853. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  9854. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  9855. BPF_LD_MAP_FD(BPF_REG_1, 0),
  9856. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  9857. BPF_FUNC_map_lookup_elem),
  9858. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  9859. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
  9860. offsetof(struct __sk_buff, data)),
  9861. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 64),
  9862. BPF_EXIT_INSN(),
  9863. },
  9864. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  9865. .fixup_map1 = { 16 },
  9866. .result = REJECT,
  9867. .errstr = "R0 min value is outside of the array range",
  9868. },
  9869. {
  9870. "calls: overlapping caller/callee",
  9871. .insns = {
  9872. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 0),
  9873. BPF_MOV64_IMM(BPF_REG_0, 1),
  9874. BPF_EXIT_INSN(),
  9875. },
  9876. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9877. .errstr = "last insn is not an exit or jmp",
  9878. .result = REJECT,
  9879. },
  9880. {
  9881. "calls: wrong recursive calls",
  9882. .insns = {
  9883. BPF_JMP_IMM(BPF_JA, 0, 0, 4),
  9884. BPF_JMP_IMM(BPF_JA, 0, 0, 4),
  9885. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -2),
  9886. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -2),
  9887. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -2),
  9888. BPF_MOV64_IMM(BPF_REG_0, 1),
  9889. BPF_EXIT_INSN(),
  9890. },
  9891. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9892. .errstr = "jump out of range",
  9893. .result = REJECT,
  9894. },
  9895. {
  9896. "calls: wrong src reg",
  9897. .insns = {
  9898. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 2, 0, 0),
  9899. BPF_MOV64_IMM(BPF_REG_0, 1),
  9900. BPF_EXIT_INSN(),
  9901. },
  9902. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9903. .errstr = "BPF_CALL uses reserved fields",
  9904. .result = REJECT,
  9905. },
  9906. {
  9907. "calls: wrong off value",
  9908. .insns = {
  9909. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, -1, 2),
  9910. BPF_MOV64_IMM(BPF_REG_0, 1),
  9911. BPF_EXIT_INSN(),
  9912. BPF_MOV64_IMM(BPF_REG_0, 2),
  9913. BPF_EXIT_INSN(),
  9914. },
  9915. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9916. .errstr = "BPF_CALL uses reserved fields",
  9917. .result = REJECT,
  9918. },
  9919. {
  9920. "calls: jump back loop",
  9921. .insns = {
  9922. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -1),
  9923. BPF_MOV64_IMM(BPF_REG_0, 1),
  9924. BPF_EXIT_INSN(),
  9925. },
  9926. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9927. .errstr = "back-edge from insn 0 to 0",
  9928. .result = REJECT,
  9929. },
  9930. {
  9931. "calls: conditional call",
  9932. .insns = {
  9933. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9934. offsetof(struct __sk_buff, mark)),
  9935. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  9936. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  9937. BPF_MOV64_IMM(BPF_REG_0, 1),
  9938. BPF_EXIT_INSN(),
  9939. BPF_MOV64_IMM(BPF_REG_0, 2),
  9940. BPF_EXIT_INSN(),
  9941. },
  9942. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9943. .errstr = "jump out of range",
  9944. .result = REJECT,
  9945. },
  9946. {
  9947. "calls: conditional call 2",
  9948. .insns = {
  9949. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9950. offsetof(struct __sk_buff, mark)),
  9951. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  9952. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  9953. BPF_MOV64_IMM(BPF_REG_0, 1),
  9954. BPF_EXIT_INSN(),
  9955. BPF_MOV64_IMM(BPF_REG_0, 2),
  9956. BPF_EXIT_INSN(),
  9957. BPF_MOV64_IMM(BPF_REG_0, 3),
  9958. BPF_EXIT_INSN(),
  9959. },
  9960. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9961. .result = ACCEPT,
  9962. },
  9963. {
  9964. "calls: conditional call 3",
  9965. .insns = {
  9966. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9967. offsetof(struct __sk_buff, mark)),
  9968. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  9969. BPF_JMP_IMM(BPF_JA, 0, 0, 4),
  9970. BPF_MOV64_IMM(BPF_REG_0, 1),
  9971. BPF_EXIT_INSN(),
  9972. BPF_MOV64_IMM(BPF_REG_0, 1),
  9973. BPF_JMP_IMM(BPF_JA, 0, 0, -6),
  9974. BPF_MOV64_IMM(BPF_REG_0, 3),
  9975. BPF_JMP_IMM(BPF_JA, 0, 0, -6),
  9976. },
  9977. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9978. .errstr = "back-edge from insn",
  9979. .result = REJECT,
  9980. },
  9981. {
  9982. "calls: conditional call 4",
  9983. .insns = {
  9984. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  9985. offsetof(struct __sk_buff, mark)),
  9986. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  9987. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  9988. BPF_MOV64_IMM(BPF_REG_0, 1),
  9989. BPF_EXIT_INSN(),
  9990. BPF_MOV64_IMM(BPF_REG_0, 1),
  9991. BPF_JMP_IMM(BPF_JA, 0, 0, -5),
  9992. BPF_MOV64_IMM(BPF_REG_0, 3),
  9993. BPF_EXIT_INSN(),
  9994. },
  9995. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  9996. .result = ACCEPT,
  9997. },
  9998. {
  9999. "calls: conditional call 5",
  10000. .insns = {
  10001. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  10002. offsetof(struct __sk_buff, mark)),
  10003. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
  10004. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  10005. BPF_MOV64_IMM(BPF_REG_0, 1),
  10006. BPF_EXIT_INSN(),
  10007. BPF_MOV64_IMM(BPF_REG_0, 1),
  10008. BPF_JMP_IMM(BPF_JA, 0, 0, -6),
  10009. BPF_MOV64_IMM(BPF_REG_0, 3),
  10010. BPF_EXIT_INSN(),
  10011. },
  10012. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10013. .errstr = "back-edge from insn",
  10014. .result = REJECT,
  10015. },
  10016. {
  10017. "calls: conditional call 6",
  10018. .insns = {
  10019. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10020. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -2),
  10021. BPF_EXIT_INSN(),
  10022. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  10023. offsetof(struct __sk_buff, mark)),
  10024. BPF_EXIT_INSN(),
  10025. },
  10026. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10027. .errstr = "back-edge from insn",
  10028. .result = REJECT,
  10029. },
  10030. {
  10031. "calls: using r0 returned by callee",
  10032. .insns = {
  10033. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10034. BPF_EXIT_INSN(),
  10035. BPF_MOV64_IMM(BPF_REG_0, 2),
  10036. BPF_EXIT_INSN(),
  10037. },
  10038. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10039. .result = ACCEPT,
  10040. },
  10041. {
  10042. "calls: using uninit r0 from callee",
  10043. .insns = {
  10044. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10045. BPF_EXIT_INSN(),
  10046. BPF_EXIT_INSN(),
  10047. },
  10048. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10049. .errstr = "!read_ok",
  10050. .result = REJECT,
  10051. },
  10052. {
  10053. "calls: callee is using r1",
  10054. .insns = {
  10055. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10056. BPF_EXIT_INSN(),
  10057. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  10058. offsetof(struct __sk_buff, len)),
  10059. BPF_EXIT_INSN(),
  10060. },
  10061. .prog_type = BPF_PROG_TYPE_SCHED_ACT,
  10062. .result = ACCEPT,
  10063. .retval = TEST_DATA_LEN,
  10064. },
  10065. {
  10066. "calls: callee using args1",
  10067. .insns = {
  10068. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10069. BPF_EXIT_INSN(),
  10070. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  10071. BPF_EXIT_INSN(),
  10072. },
  10073. .errstr_unpriv = "allowed for root only",
  10074. .result_unpriv = REJECT,
  10075. .result = ACCEPT,
  10076. .retval = POINTER_VALUE,
  10077. },
  10078. {
  10079. "calls: callee using wrong args2",
  10080. .insns = {
  10081. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10082. BPF_EXIT_INSN(),
  10083. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  10084. BPF_EXIT_INSN(),
  10085. },
  10086. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10087. .errstr = "R2 !read_ok",
  10088. .result = REJECT,
  10089. },
  10090. {
  10091. "calls: callee using two args",
  10092. .insns = {
  10093. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10094. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
  10095. offsetof(struct __sk_buff, len)),
  10096. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_6,
  10097. offsetof(struct __sk_buff, len)),
  10098. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10099. BPF_EXIT_INSN(),
  10100. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  10101. BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
  10102. BPF_EXIT_INSN(),
  10103. },
  10104. .errstr_unpriv = "allowed for root only",
  10105. .result_unpriv = REJECT,
  10106. .result = ACCEPT,
  10107. .retval = TEST_DATA_LEN + TEST_DATA_LEN - ETH_HLEN - ETH_HLEN,
  10108. },
  10109. {
  10110. "calls: callee changing pkt pointers",
  10111. .insns = {
  10112. BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_1,
  10113. offsetof(struct xdp_md, data)),
  10114. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
  10115. offsetof(struct xdp_md, data_end)),
  10116. BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
  10117. BPF_ALU64_IMM(BPF_ADD, BPF_REG_8, 8),
  10118. BPF_JMP_REG(BPF_JGT, BPF_REG_8, BPF_REG_7, 2),
  10119. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10120. /* clear_all_pkt_pointers() has to walk all frames
  10121. * to make sure that pkt pointers in the caller
  10122. * are cleared when callee is calling a helper that
  10123. * adjusts packet size
  10124. */
  10125. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  10126. BPF_MOV32_IMM(BPF_REG_0, 0),
  10127. BPF_EXIT_INSN(),
  10128. BPF_MOV64_IMM(BPF_REG_2, 0),
  10129. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  10130. BPF_FUNC_xdp_adjust_head),
  10131. BPF_EXIT_INSN(),
  10132. },
  10133. .result = REJECT,
  10134. .errstr = "R6 invalid mem access 'inv'",
  10135. .prog_type = BPF_PROG_TYPE_XDP,
  10136. },
  10137. {
  10138. "calls: two calls with args",
  10139. .insns = {
  10140. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10141. BPF_EXIT_INSN(),
  10142. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10143. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 6),
  10144. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
  10145. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10146. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10147. BPF_ALU64_REG(BPF_ADD, BPF_REG_7, BPF_REG_0),
  10148. BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
  10149. BPF_EXIT_INSN(),
  10150. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  10151. offsetof(struct __sk_buff, len)),
  10152. BPF_EXIT_INSN(),
  10153. },
  10154. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10155. .result = ACCEPT,
  10156. .retval = TEST_DATA_LEN + TEST_DATA_LEN,
  10157. },
  10158. {
  10159. "calls: calls with stack arith",
  10160. .insns = {
  10161. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10162. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -64),
  10163. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10164. BPF_EXIT_INSN(),
  10165. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -64),
  10166. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10167. BPF_EXIT_INSN(),
  10168. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -64),
  10169. BPF_MOV64_IMM(BPF_REG_0, 42),
  10170. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  10171. BPF_EXIT_INSN(),
  10172. },
  10173. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10174. .result = ACCEPT,
  10175. .retval = 42,
  10176. },
  10177. {
  10178. "calls: calls with misaligned stack access",
  10179. .insns = {
  10180. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10181. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -63),
  10182. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10183. BPF_EXIT_INSN(),
  10184. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -61),
  10185. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10186. BPF_EXIT_INSN(),
  10187. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -63),
  10188. BPF_MOV64_IMM(BPF_REG_0, 42),
  10189. BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
  10190. BPF_EXIT_INSN(),
  10191. },
  10192. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10193. .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
  10194. .errstr = "misaligned stack access",
  10195. .result = REJECT,
  10196. },
  10197. {
  10198. "calls: calls control flow, jump test",
  10199. .insns = {
  10200. BPF_MOV64_IMM(BPF_REG_0, 42),
  10201. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  10202. BPF_MOV64_IMM(BPF_REG_0, 43),
  10203. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  10204. BPF_JMP_IMM(BPF_JA, 0, 0, -3),
  10205. BPF_EXIT_INSN(),
  10206. },
  10207. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10208. .result = ACCEPT,
  10209. .retval = 43,
  10210. },
  10211. {
  10212. "calls: calls control flow, jump test 2",
  10213. .insns = {
  10214. BPF_MOV64_IMM(BPF_REG_0, 42),
  10215. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  10216. BPF_MOV64_IMM(BPF_REG_0, 43),
  10217. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  10218. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -3),
  10219. BPF_EXIT_INSN(),
  10220. },
  10221. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10222. .errstr = "jump out of range from insn 1 to 4",
  10223. .result = REJECT,
  10224. },
  10225. {
  10226. "calls: two calls with bad jump",
  10227. .insns = {
  10228. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10229. BPF_EXIT_INSN(),
  10230. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10231. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 6),
  10232. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
  10233. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10234. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10235. BPF_ALU64_REG(BPF_ADD, BPF_REG_7, BPF_REG_0),
  10236. BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
  10237. BPF_EXIT_INSN(),
  10238. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  10239. offsetof(struct __sk_buff, len)),
  10240. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, -3),
  10241. BPF_EXIT_INSN(),
  10242. },
  10243. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10244. .errstr = "jump out of range from insn 11 to 9",
  10245. .result = REJECT,
  10246. },
  10247. {
  10248. "calls: recursive call. test1",
  10249. .insns = {
  10250. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10251. BPF_EXIT_INSN(),
  10252. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -1),
  10253. BPF_EXIT_INSN(),
  10254. },
  10255. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10256. .errstr = "back-edge",
  10257. .result = REJECT,
  10258. },
  10259. {
  10260. "calls: recursive call. test2",
  10261. .insns = {
  10262. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10263. BPF_EXIT_INSN(),
  10264. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -3),
  10265. BPF_EXIT_INSN(),
  10266. },
  10267. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10268. .errstr = "back-edge",
  10269. .result = REJECT,
  10270. },
  10271. {
  10272. "calls: unreachable code",
  10273. .insns = {
  10274. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10275. BPF_EXIT_INSN(),
  10276. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10277. BPF_EXIT_INSN(),
  10278. BPF_MOV64_IMM(BPF_REG_0, 0),
  10279. BPF_EXIT_INSN(),
  10280. BPF_MOV64_IMM(BPF_REG_0, 0),
  10281. BPF_EXIT_INSN(),
  10282. },
  10283. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10284. .errstr = "unreachable insn 6",
  10285. .result = REJECT,
  10286. },
  10287. {
  10288. "calls: invalid call",
  10289. .insns = {
  10290. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10291. BPF_EXIT_INSN(),
  10292. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, -4),
  10293. BPF_EXIT_INSN(),
  10294. },
  10295. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10296. .errstr = "invalid destination",
  10297. .result = REJECT,
  10298. },
  10299. {
  10300. "calls: invalid call 2",
  10301. .insns = {
  10302. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10303. BPF_EXIT_INSN(),
  10304. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 0x7fffffff),
  10305. BPF_EXIT_INSN(),
  10306. },
  10307. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10308. .errstr = "invalid destination",
  10309. .result = REJECT,
  10310. },
  10311. {
  10312. "calls: jumping across function bodies. test1",
  10313. .insns = {
  10314. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10315. BPF_MOV64_IMM(BPF_REG_0, 0),
  10316. BPF_EXIT_INSN(),
  10317. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, -3),
  10318. BPF_EXIT_INSN(),
  10319. },
  10320. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10321. .errstr = "jump out of range",
  10322. .result = REJECT,
  10323. },
  10324. {
  10325. "calls: jumping across function bodies. test2",
  10326. .insns = {
  10327. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
  10328. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10329. BPF_MOV64_IMM(BPF_REG_0, 0),
  10330. BPF_EXIT_INSN(),
  10331. BPF_EXIT_INSN(),
  10332. },
  10333. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10334. .errstr = "jump out of range",
  10335. .result = REJECT,
  10336. },
  10337. {
  10338. "calls: call without exit",
  10339. .insns = {
  10340. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10341. BPF_EXIT_INSN(),
  10342. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10343. BPF_EXIT_INSN(),
  10344. BPF_MOV64_IMM(BPF_REG_0, 0),
  10345. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, -2),
  10346. },
  10347. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10348. .errstr = "not an exit",
  10349. .result = REJECT,
  10350. },
  10351. {
  10352. "calls: call into middle of ld_imm64",
  10353. .insns = {
  10354. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10355. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10356. BPF_MOV64_IMM(BPF_REG_0, 0),
  10357. BPF_EXIT_INSN(),
  10358. BPF_LD_IMM64(BPF_REG_0, 0),
  10359. BPF_EXIT_INSN(),
  10360. },
  10361. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10362. .errstr = "last insn",
  10363. .result = REJECT,
  10364. },
  10365. {
  10366. "calls: call into middle of other call",
  10367. .insns = {
  10368. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10369. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10370. BPF_MOV64_IMM(BPF_REG_0, 0),
  10371. BPF_EXIT_INSN(),
  10372. BPF_MOV64_IMM(BPF_REG_0, 0),
  10373. BPF_MOV64_IMM(BPF_REG_0, 0),
  10374. BPF_EXIT_INSN(),
  10375. },
  10376. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10377. .errstr = "last insn",
  10378. .result = REJECT,
  10379. },
  10380. {
  10381. "calls: ld_abs with changing ctx data in callee",
  10382. .insns = {
  10383. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10384. BPF_LD_ABS(BPF_B, 0),
  10385. BPF_LD_ABS(BPF_H, 0),
  10386. BPF_LD_ABS(BPF_W, 0),
  10387. BPF_MOV64_REG(BPF_REG_7, BPF_REG_6),
  10388. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 5),
  10389. BPF_MOV64_REG(BPF_REG_6, BPF_REG_7),
  10390. BPF_LD_ABS(BPF_B, 0),
  10391. BPF_LD_ABS(BPF_H, 0),
  10392. BPF_LD_ABS(BPF_W, 0),
  10393. BPF_EXIT_INSN(),
  10394. BPF_MOV64_IMM(BPF_REG_2, 1),
  10395. BPF_MOV64_IMM(BPF_REG_3, 2),
  10396. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  10397. BPF_FUNC_skb_vlan_push),
  10398. BPF_EXIT_INSN(),
  10399. },
  10400. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10401. .errstr = "BPF_LD_[ABS|IND] instructions cannot be mixed",
  10402. .result = REJECT,
  10403. },
  10404. {
  10405. "calls: two calls with bad fallthrough",
  10406. .insns = {
  10407. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10408. BPF_EXIT_INSN(),
  10409. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10410. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 6),
  10411. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
  10412. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10413. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10414. BPF_ALU64_REG(BPF_ADD, BPF_REG_7, BPF_REG_0),
  10415. BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
  10416. BPF_MOV64_REG(BPF_REG_0, BPF_REG_0),
  10417. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
  10418. offsetof(struct __sk_buff, len)),
  10419. BPF_EXIT_INSN(),
  10420. },
  10421. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  10422. .errstr = "not an exit",
  10423. .result = REJECT,
  10424. },
  10425. {
  10426. "calls: two calls with stack read",
  10427. .insns = {
  10428. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10429. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10430. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10431. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10432. BPF_EXIT_INSN(),
  10433. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10434. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 6),
  10435. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
  10436. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10437. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10438. BPF_ALU64_REG(BPF_ADD, BPF_REG_7, BPF_REG_0),
  10439. BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
  10440. BPF_EXIT_INSN(),
  10441. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  10442. BPF_EXIT_INSN(),
  10443. },
  10444. .prog_type = BPF_PROG_TYPE_XDP,
  10445. .result = ACCEPT,
  10446. },
  10447. {
  10448. "calls: two calls with stack write",
  10449. .insns = {
  10450. /* main prog */
  10451. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10452. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10453. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10454. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10455. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10456. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10457. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -16),
  10458. BPF_EXIT_INSN(),
  10459. /* subprog 1 */
  10460. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10461. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10462. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 7),
  10463. BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
  10464. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10465. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  10466. BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_0),
  10467. BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
  10468. /* write into stack frame of main prog */
  10469. BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  10470. BPF_EXIT_INSN(),
  10471. /* subprog 2 */
  10472. /* read from stack frame of main prog */
  10473. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
  10474. BPF_EXIT_INSN(),
  10475. },
  10476. .prog_type = BPF_PROG_TYPE_XDP,
  10477. .result = ACCEPT,
  10478. },
  10479. {
  10480. "calls: stack overflow using two frames (pre-call access)",
  10481. .insns = {
  10482. /* prog 1 */
  10483. BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
  10484. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1),
  10485. BPF_EXIT_INSN(),
  10486. /* prog 2 */
  10487. BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
  10488. BPF_MOV64_IMM(BPF_REG_0, 0),
  10489. BPF_EXIT_INSN(),
  10490. },
  10491. .prog_type = BPF_PROG_TYPE_XDP,
  10492. .errstr = "combined stack size",
  10493. .result = REJECT,
  10494. },
  10495. {
  10496. "calls: stack overflow using two frames (post-call access)",
  10497. .insns = {
  10498. /* prog 1 */
  10499. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 2),
  10500. BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
  10501. BPF_EXIT_INSN(),
  10502. /* prog 2 */
  10503. BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
  10504. BPF_MOV64_IMM(BPF_REG_0, 0),
  10505. BPF_EXIT_INSN(),
  10506. },
  10507. .prog_type = BPF_PROG_TYPE_XDP,
  10508. .errstr = "combined stack size",
  10509. .result = REJECT,
  10510. },
  10511. {
  10512. "calls: stack depth check using three frames. test1",
  10513. .insns = {
  10514. /* main */
  10515. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4), /* call A */
  10516. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 5), /* call B */
  10517. BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0),
  10518. BPF_MOV64_IMM(BPF_REG_0, 0),
  10519. BPF_EXIT_INSN(),
  10520. /* A */
  10521. BPF_ST_MEM(BPF_B, BPF_REG_10, -256, 0),
  10522. BPF_EXIT_INSN(),
  10523. /* B */
  10524. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -3), /* call A */
  10525. BPF_ST_MEM(BPF_B, BPF_REG_10, -64, 0),
  10526. BPF_EXIT_INSN(),
  10527. },
  10528. .prog_type = BPF_PROG_TYPE_XDP,
  10529. /* stack_main=32, stack_A=256, stack_B=64
  10530. * and max(main+A, main+A+B) < 512
  10531. */
  10532. .result = ACCEPT,
  10533. },
  10534. {
  10535. "calls: stack depth check using three frames. test2",
  10536. .insns = {
  10537. /* main */
  10538. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4), /* call A */
  10539. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 5), /* call B */
  10540. BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0),
  10541. BPF_MOV64_IMM(BPF_REG_0, 0),
  10542. BPF_EXIT_INSN(),
  10543. /* A */
  10544. BPF_ST_MEM(BPF_B, BPF_REG_10, -64, 0),
  10545. BPF_EXIT_INSN(),
  10546. /* B */
  10547. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -3), /* call A */
  10548. BPF_ST_MEM(BPF_B, BPF_REG_10, -256, 0),
  10549. BPF_EXIT_INSN(),
  10550. },
  10551. .prog_type = BPF_PROG_TYPE_XDP,
  10552. /* stack_main=32, stack_A=64, stack_B=256
  10553. * and max(main+A, main+A+B) < 512
  10554. */
  10555. .result = ACCEPT,
  10556. },
  10557. {
  10558. "calls: stack depth check using three frames. test3",
  10559. .insns = {
  10560. /* main */
  10561. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10562. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 6), /* call A */
  10563. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10564. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 8), /* call B */
  10565. BPF_JMP_IMM(BPF_JGE, BPF_REG_6, 0, 1),
  10566. BPF_ST_MEM(BPF_B, BPF_REG_10, -64, 0),
  10567. BPF_MOV64_IMM(BPF_REG_0, 0),
  10568. BPF_EXIT_INSN(),
  10569. /* A */
  10570. BPF_JMP_IMM(BPF_JLT, BPF_REG_1, 10, 1),
  10571. BPF_EXIT_INSN(),
  10572. BPF_ST_MEM(BPF_B, BPF_REG_10, -224, 0),
  10573. BPF_JMP_IMM(BPF_JA, 0, 0, -3),
  10574. /* B */
  10575. BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 2, 1),
  10576. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, -6), /* call A */
  10577. BPF_ST_MEM(BPF_B, BPF_REG_10, -256, 0),
  10578. BPF_EXIT_INSN(),
  10579. },
  10580. .prog_type = BPF_PROG_TYPE_XDP,
  10581. /* stack_main=64, stack_A=224, stack_B=256
  10582. * and max(main+A, main+A+B) > 512
  10583. */
  10584. .errstr = "combined stack",
  10585. .result = REJECT,
  10586. },
  10587. {
  10588. "calls: stack depth check using three frames. test4",
  10589. /* void main(void) {
  10590. * func1(0);
  10591. * func1(1);
  10592. * func2(1);
  10593. * }
  10594. * void func1(int alloc_or_recurse) {
  10595. * if (alloc_or_recurse) {
  10596. * frame_pointer[-300] = 1;
  10597. * } else {
  10598. * func2(alloc_or_recurse);
  10599. * }
  10600. * }
  10601. * void func2(int alloc_or_recurse) {
  10602. * if (alloc_or_recurse) {
  10603. * frame_pointer[-300] = 1;
  10604. * }
  10605. * }
  10606. */
  10607. .insns = {
  10608. /* main */
  10609. BPF_MOV64_IMM(BPF_REG_1, 0),
  10610. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 6), /* call A */
  10611. BPF_MOV64_IMM(BPF_REG_1, 1),
  10612. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4), /* call A */
  10613. BPF_MOV64_IMM(BPF_REG_1, 1),
  10614. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 7), /* call B */
  10615. BPF_MOV64_IMM(BPF_REG_0, 0),
  10616. BPF_EXIT_INSN(),
  10617. /* A */
  10618. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
  10619. BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
  10620. BPF_EXIT_INSN(),
  10621. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call B */
  10622. BPF_EXIT_INSN(),
  10623. /* B */
  10624. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
  10625. BPF_ST_MEM(BPF_B, BPF_REG_10, -300, 0),
  10626. BPF_EXIT_INSN(),
  10627. },
  10628. .prog_type = BPF_PROG_TYPE_XDP,
  10629. .result = REJECT,
  10630. .errstr = "combined stack",
  10631. },
  10632. {
  10633. "calls: stack depth check using three frames. test5",
  10634. .insns = {
  10635. /* main */
  10636. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call A */
  10637. BPF_EXIT_INSN(),
  10638. /* A */
  10639. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call B */
  10640. BPF_EXIT_INSN(),
  10641. /* B */
  10642. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call C */
  10643. BPF_EXIT_INSN(),
  10644. /* C */
  10645. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call D */
  10646. BPF_EXIT_INSN(),
  10647. /* D */
  10648. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call E */
  10649. BPF_EXIT_INSN(),
  10650. /* E */
  10651. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call F */
  10652. BPF_EXIT_INSN(),
  10653. /* F */
  10654. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call G */
  10655. BPF_EXIT_INSN(),
  10656. /* G */
  10657. BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 1), /* call H */
  10658. BPF_EXIT_INSN(),
  10659. /* H */
  10660. BPF_MOV64_IMM(BPF_REG_0, 0),
  10661. BPF_EXIT_INSN(),
  10662. },
  10663. .prog_type = BPF_PROG_TYPE_XDP,
  10664. .errstr = "call stack",
  10665. .result = REJECT,
  10666. },
  10667. {
  10668. "calls: spill into caller stack frame",
  10669. .insns = {
  10670. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10671. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10672. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10673. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10674. BPF_EXIT_INSN(),
  10675. BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0),
  10676. BPF_MOV64_IMM(BPF_REG_0, 0),
  10677. BPF_EXIT_INSN(),
  10678. },
  10679. .prog_type = BPF_PROG_TYPE_XDP,
  10680. .errstr = "cannot spill",
  10681. .result = REJECT,
  10682. },
  10683. {
  10684. "calls: write into caller stack frame",
  10685. .insns = {
  10686. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10687. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10688. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10689. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10690. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  10691. BPF_EXIT_INSN(),
  10692. BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
  10693. BPF_MOV64_IMM(BPF_REG_0, 0),
  10694. BPF_EXIT_INSN(),
  10695. },
  10696. .prog_type = BPF_PROG_TYPE_XDP,
  10697. .result = ACCEPT,
  10698. .retval = 42,
  10699. },
  10700. {
  10701. "calls: write into callee stack frame",
  10702. .insns = {
  10703. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10704. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 42),
  10705. BPF_EXIT_INSN(),
  10706. BPF_MOV64_REG(BPF_REG_0, BPF_REG_10),
  10707. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, -8),
  10708. BPF_EXIT_INSN(),
  10709. },
  10710. .prog_type = BPF_PROG_TYPE_XDP,
  10711. .errstr = "cannot return stack pointer",
  10712. .result = REJECT,
  10713. },
  10714. {
  10715. "calls: two calls with stack write and void return",
  10716. .insns = {
  10717. /* main prog */
  10718. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10719. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10720. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10721. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10722. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10723. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10724. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -16),
  10725. BPF_EXIT_INSN(),
  10726. /* subprog 1 */
  10727. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10728. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10729. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10730. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  10731. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10732. BPF_EXIT_INSN(),
  10733. /* subprog 2 */
  10734. /* write into stack frame of main prog */
  10735. BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
  10736. BPF_EXIT_INSN(), /* void return */
  10737. },
  10738. .prog_type = BPF_PROG_TYPE_XDP,
  10739. .result = ACCEPT,
  10740. },
  10741. {
  10742. "calls: ambiguous return value",
  10743. .insns = {
  10744. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10745. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 5),
  10746. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  10747. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  10748. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10749. BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
  10750. BPF_EXIT_INSN(),
  10751. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
  10752. BPF_MOV64_IMM(BPF_REG_0, 0),
  10753. BPF_EXIT_INSN(),
  10754. },
  10755. .errstr_unpriv = "allowed for root only",
  10756. .result_unpriv = REJECT,
  10757. .errstr = "R0 !read_ok",
  10758. .result = REJECT,
  10759. },
  10760. {
  10761. "calls: two calls that return map_value",
  10762. .insns = {
  10763. /* main prog */
  10764. /* pass fp-16, fp-8 into a function */
  10765. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10766. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10767. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10768. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10769. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 8),
  10770. /* fetch map_value_ptr from the stack of this function */
  10771. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  10772. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  10773. /* write into map value */
  10774. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10775. /* fetch secound map_value_ptr from the stack */
  10776. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -16),
  10777. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  10778. /* write into map value */
  10779. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10780. BPF_MOV64_IMM(BPF_REG_0, 0),
  10781. BPF_EXIT_INSN(),
  10782. /* subprog 1 */
  10783. /* call 3rd function twice */
  10784. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10785. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10786. /* first time with fp-8 */
  10787. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  10788. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  10789. /* second time with fp-16 */
  10790. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  10791. BPF_EXIT_INSN(),
  10792. /* subprog 2 */
  10793. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10794. /* lookup from map */
  10795. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10796. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10797. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  10798. BPF_LD_MAP_FD(BPF_REG_1, 0),
  10799. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  10800. BPF_FUNC_map_lookup_elem),
  10801. /* write map_value_ptr into stack frame of main prog */
  10802. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  10803. BPF_MOV64_IMM(BPF_REG_0, 0),
  10804. BPF_EXIT_INSN(), /* return 0 */
  10805. },
  10806. .prog_type = BPF_PROG_TYPE_XDP,
  10807. .fixup_map1 = { 23 },
  10808. .result = ACCEPT,
  10809. },
  10810. {
  10811. "calls: two calls that return map_value with bool condition",
  10812. .insns = {
  10813. /* main prog */
  10814. /* pass fp-16, fp-8 into a function */
  10815. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10816. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10817. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10818. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10819. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10820. BPF_MOV64_IMM(BPF_REG_0, 0),
  10821. BPF_EXIT_INSN(),
  10822. /* subprog 1 */
  10823. /* call 3rd function twice */
  10824. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10825. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10826. /* first time with fp-8 */
  10827. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 9),
  10828. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 1, 2),
  10829. /* fetch map_value_ptr from the stack of this function */
  10830. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  10831. /* write into map value */
  10832. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10833. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  10834. /* second time with fp-16 */
  10835. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  10836. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 1, 2),
  10837. /* fetch secound map_value_ptr from the stack */
  10838. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_7, 0),
  10839. /* write into map value */
  10840. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10841. BPF_EXIT_INSN(),
  10842. /* subprog 2 */
  10843. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10844. /* lookup from map */
  10845. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10846. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10847. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  10848. BPF_LD_MAP_FD(BPF_REG_1, 0),
  10849. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  10850. BPF_FUNC_map_lookup_elem),
  10851. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  10852. BPF_MOV64_IMM(BPF_REG_0, 0),
  10853. BPF_EXIT_INSN(), /* return 0 */
  10854. /* write map_value_ptr into stack frame of main prog */
  10855. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  10856. BPF_MOV64_IMM(BPF_REG_0, 1),
  10857. BPF_EXIT_INSN(), /* return 1 */
  10858. },
  10859. .prog_type = BPF_PROG_TYPE_XDP,
  10860. .fixup_map1 = { 23 },
  10861. .result = ACCEPT,
  10862. },
  10863. {
  10864. "calls: two calls that return map_value with incorrect bool check",
  10865. .insns = {
  10866. /* main prog */
  10867. /* pass fp-16, fp-8 into a function */
  10868. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10869. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10870. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10871. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10872. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10873. BPF_MOV64_IMM(BPF_REG_0, 0),
  10874. BPF_EXIT_INSN(),
  10875. /* subprog 1 */
  10876. /* call 3rd function twice */
  10877. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10878. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10879. /* first time with fp-8 */
  10880. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 9),
  10881. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 1, 2),
  10882. /* fetch map_value_ptr from the stack of this function */
  10883. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_6, 0),
  10884. /* write into map value */
  10885. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10886. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  10887. /* second time with fp-16 */
  10888. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  10889. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  10890. /* fetch secound map_value_ptr from the stack */
  10891. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_7, 0),
  10892. /* write into map value */
  10893. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10894. BPF_EXIT_INSN(),
  10895. /* subprog 2 */
  10896. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10897. /* lookup from map */
  10898. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10899. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10900. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  10901. BPF_LD_MAP_FD(BPF_REG_1, 0),
  10902. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  10903. BPF_FUNC_map_lookup_elem),
  10904. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  10905. BPF_MOV64_IMM(BPF_REG_0, 0),
  10906. BPF_EXIT_INSN(), /* return 0 */
  10907. /* write map_value_ptr into stack frame of main prog */
  10908. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  10909. BPF_MOV64_IMM(BPF_REG_0, 1),
  10910. BPF_EXIT_INSN(), /* return 1 */
  10911. },
  10912. .prog_type = BPF_PROG_TYPE_XDP,
  10913. .fixup_map1 = { 23 },
  10914. .result = REJECT,
  10915. .errstr = "invalid read from stack off -16+0 size 8",
  10916. },
  10917. {
  10918. "calls: two calls that receive map_value via arg=ptr_stack_of_caller. test1",
  10919. .insns = {
  10920. /* main prog */
  10921. /* pass fp-16, fp-8 into a function */
  10922. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10923. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10924. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10925. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10926. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10927. BPF_MOV64_IMM(BPF_REG_0, 0),
  10928. BPF_EXIT_INSN(),
  10929. /* subprog 1 */
  10930. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10931. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10932. /* 1st lookup from map */
  10933. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  10934. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10935. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  10936. BPF_LD_MAP_FD(BPF_REG_1, 0),
  10937. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  10938. BPF_FUNC_map_lookup_elem),
  10939. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  10940. BPF_MOV64_IMM(BPF_REG_8, 0),
  10941. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  10942. /* write map_value_ptr into stack frame of main prog at fp-8 */
  10943. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  10944. BPF_MOV64_IMM(BPF_REG_8, 1),
  10945. /* 2nd lookup from map */
  10946. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* 20 */
  10947. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  10948. BPF_LD_MAP_FD(BPF_REG_1, 0),
  10949. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, /* 24 */
  10950. BPF_FUNC_map_lookup_elem),
  10951. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  10952. BPF_MOV64_IMM(BPF_REG_9, 0),
  10953. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  10954. /* write map_value_ptr into stack frame of main prog at fp-16 */
  10955. BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  10956. BPF_MOV64_IMM(BPF_REG_9, 1),
  10957. /* call 3rd func with fp-8, 0|1, fp-16, 0|1 */
  10958. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), /* 30 */
  10959. BPF_MOV64_REG(BPF_REG_2, BPF_REG_8),
  10960. BPF_MOV64_REG(BPF_REG_3, BPF_REG_7),
  10961. BPF_MOV64_REG(BPF_REG_4, BPF_REG_9),
  10962. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1), /* 34 */
  10963. BPF_EXIT_INSN(),
  10964. /* subprog 2 */
  10965. /* if arg2 == 1 do *arg1 = 0 */
  10966. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 1, 2),
  10967. /* fetch map_value_ptr from the stack of this function */
  10968. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  10969. /* write into map value */
  10970. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  10971. /* if arg4 == 1 do *arg3 = 0 */
  10972. BPF_JMP_IMM(BPF_JNE, BPF_REG_4, 1, 2),
  10973. /* fetch map_value_ptr from the stack of this function */
  10974. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
  10975. /* write into map value */
  10976. BPF_ST_MEM(BPF_DW, BPF_REG_0, 2, 0),
  10977. BPF_EXIT_INSN(),
  10978. },
  10979. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  10980. .fixup_map1 = { 12, 22 },
  10981. .result = REJECT,
  10982. .errstr = "invalid access to map value, value_size=8 off=2 size=8",
  10983. },
  10984. {
  10985. "calls: two calls that receive map_value via arg=ptr_stack_of_caller. test2",
  10986. .insns = {
  10987. /* main prog */
  10988. /* pass fp-16, fp-8 into a function */
  10989. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  10990. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  10991. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  10992. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  10993. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  10994. BPF_MOV64_IMM(BPF_REG_0, 0),
  10995. BPF_EXIT_INSN(),
  10996. /* subprog 1 */
  10997. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  10998. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  10999. /* 1st lookup from map */
  11000. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11001. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11002. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11003. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11004. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11005. BPF_FUNC_map_lookup_elem),
  11006. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11007. BPF_MOV64_IMM(BPF_REG_8, 0),
  11008. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  11009. /* write map_value_ptr into stack frame of main prog at fp-8 */
  11010. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  11011. BPF_MOV64_IMM(BPF_REG_8, 1),
  11012. /* 2nd lookup from map */
  11013. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* 20 */
  11014. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11015. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11016. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, /* 24 */
  11017. BPF_FUNC_map_lookup_elem),
  11018. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11019. BPF_MOV64_IMM(BPF_REG_9, 0),
  11020. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  11021. /* write map_value_ptr into stack frame of main prog at fp-16 */
  11022. BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  11023. BPF_MOV64_IMM(BPF_REG_9, 1),
  11024. /* call 3rd func with fp-8, 0|1, fp-16, 0|1 */
  11025. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), /* 30 */
  11026. BPF_MOV64_REG(BPF_REG_2, BPF_REG_8),
  11027. BPF_MOV64_REG(BPF_REG_3, BPF_REG_7),
  11028. BPF_MOV64_REG(BPF_REG_4, BPF_REG_9),
  11029. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1), /* 34 */
  11030. BPF_EXIT_INSN(),
  11031. /* subprog 2 */
  11032. /* if arg2 == 1 do *arg1 = 0 */
  11033. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 1, 2),
  11034. /* fetch map_value_ptr from the stack of this function */
  11035. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  11036. /* write into map value */
  11037. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11038. /* if arg4 == 1 do *arg3 = 0 */
  11039. BPF_JMP_IMM(BPF_JNE, BPF_REG_4, 1, 2),
  11040. /* fetch map_value_ptr from the stack of this function */
  11041. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
  11042. /* write into map value */
  11043. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11044. BPF_EXIT_INSN(),
  11045. },
  11046. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11047. .fixup_map1 = { 12, 22 },
  11048. .result = ACCEPT,
  11049. },
  11050. {
  11051. "calls: two jumps that receive map_value via arg=ptr_stack_of_jumper. test3",
  11052. .insns = {
  11053. /* main prog */
  11054. /* pass fp-16, fp-8 into a function */
  11055. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  11056. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  11057. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11058. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  11059. BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 2),
  11060. BPF_MOV64_IMM(BPF_REG_0, 0),
  11061. BPF_EXIT_INSN(),
  11062. /* subprog 1 */
  11063. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11064. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  11065. /* 1st lookup from map */
  11066. BPF_ST_MEM(BPF_DW, BPF_REG_10, -24, 0),
  11067. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11068. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -24),
  11069. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11070. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11071. BPF_FUNC_map_lookup_elem),
  11072. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11073. BPF_MOV64_IMM(BPF_REG_8, 0),
  11074. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  11075. /* write map_value_ptr into stack frame of main prog at fp-8 */
  11076. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  11077. BPF_MOV64_IMM(BPF_REG_8, 1),
  11078. /* 2nd lookup from map */
  11079. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11080. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -24),
  11081. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11082. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11083. BPF_FUNC_map_lookup_elem),
  11084. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11085. BPF_MOV64_IMM(BPF_REG_9, 0), // 26
  11086. BPF_JMP_IMM(BPF_JA, 0, 0, 2),
  11087. /* write map_value_ptr into stack frame of main prog at fp-16 */
  11088. BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  11089. BPF_MOV64_IMM(BPF_REG_9, 1),
  11090. /* call 3rd func with fp-8, 0|1, fp-16, 0|1 */
  11091. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), // 30
  11092. BPF_MOV64_REG(BPF_REG_2, BPF_REG_8),
  11093. BPF_MOV64_REG(BPF_REG_3, BPF_REG_7),
  11094. BPF_MOV64_REG(BPF_REG_4, BPF_REG_9),
  11095. BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1), // 34
  11096. BPF_JMP_IMM(BPF_JA, 0, 0, -30),
  11097. /* subprog 2 */
  11098. /* if arg2 == 1 do *arg1 = 0 */
  11099. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 1, 2),
  11100. /* fetch map_value_ptr from the stack of this function */
  11101. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  11102. /* write into map value */
  11103. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11104. /* if arg4 == 1 do *arg3 = 0 */
  11105. BPF_JMP_IMM(BPF_JNE, BPF_REG_4, 1, 2),
  11106. /* fetch map_value_ptr from the stack of this function */
  11107. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
  11108. /* write into map value */
  11109. BPF_ST_MEM(BPF_DW, BPF_REG_0, 2, 0),
  11110. BPF_JMP_IMM(BPF_JA, 0, 0, -8),
  11111. },
  11112. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11113. .fixup_map1 = { 12, 22 },
  11114. .result = REJECT,
  11115. .errstr = "invalid access to map value, value_size=8 off=2 size=8",
  11116. },
  11117. {
  11118. "calls: two calls that receive map_value_ptr_or_null via arg. test1",
  11119. .insns = {
  11120. /* main prog */
  11121. /* pass fp-16, fp-8 into a function */
  11122. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  11123. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  11124. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11125. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  11126. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  11127. BPF_MOV64_IMM(BPF_REG_0, 0),
  11128. BPF_EXIT_INSN(),
  11129. /* subprog 1 */
  11130. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11131. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  11132. /* 1st lookup from map */
  11133. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11134. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11135. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11136. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11137. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11138. BPF_FUNC_map_lookup_elem),
  11139. /* write map_value_ptr_or_null into stack frame of main prog at fp-8 */
  11140. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  11141. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11142. BPF_MOV64_IMM(BPF_REG_8, 0),
  11143. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  11144. BPF_MOV64_IMM(BPF_REG_8, 1),
  11145. /* 2nd lookup from map */
  11146. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11147. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11148. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11149. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11150. BPF_FUNC_map_lookup_elem),
  11151. /* write map_value_ptr_or_null into stack frame of main prog at fp-16 */
  11152. BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  11153. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11154. BPF_MOV64_IMM(BPF_REG_9, 0),
  11155. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  11156. BPF_MOV64_IMM(BPF_REG_9, 1),
  11157. /* call 3rd func with fp-8, 0|1, fp-16, 0|1 */
  11158. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  11159. BPF_MOV64_REG(BPF_REG_2, BPF_REG_8),
  11160. BPF_MOV64_REG(BPF_REG_3, BPF_REG_7),
  11161. BPF_MOV64_REG(BPF_REG_4, BPF_REG_9),
  11162. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  11163. BPF_EXIT_INSN(),
  11164. /* subprog 2 */
  11165. /* if arg2 == 1 do *arg1 = 0 */
  11166. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 1, 2),
  11167. /* fetch map_value_ptr from the stack of this function */
  11168. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  11169. /* write into map value */
  11170. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11171. /* if arg4 == 1 do *arg3 = 0 */
  11172. BPF_JMP_IMM(BPF_JNE, BPF_REG_4, 1, 2),
  11173. /* fetch map_value_ptr from the stack of this function */
  11174. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
  11175. /* write into map value */
  11176. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11177. BPF_EXIT_INSN(),
  11178. },
  11179. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11180. .fixup_map1 = { 12, 22 },
  11181. .result = ACCEPT,
  11182. },
  11183. {
  11184. "calls: two calls that receive map_value_ptr_or_null via arg. test2",
  11185. .insns = {
  11186. /* main prog */
  11187. /* pass fp-16, fp-8 into a function */
  11188. BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
  11189. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
  11190. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11191. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
  11192. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
  11193. BPF_MOV64_IMM(BPF_REG_0, 0),
  11194. BPF_EXIT_INSN(),
  11195. /* subprog 1 */
  11196. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11197. BPF_MOV64_REG(BPF_REG_7, BPF_REG_2),
  11198. /* 1st lookup from map */
  11199. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11200. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11201. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11202. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11203. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11204. BPF_FUNC_map_lookup_elem),
  11205. /* write map_value_ptr_or_null into stack frame of main prog at fp-8 */
  11206. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  11207. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11208. BPF_MOV64_IMM(BPF_REG_8, 0),
  11209. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  11210. BPF_MOV64_IMM(BPF_REG_8, 1),
  11211. /* 2nd lookup from map */
  11212. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11213. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11214. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11215. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11216. BPF_FUNC_map_lookup_elem),
  11217. /* write map_value_ptr_or_null into stack frame of main prog at fp-16 */
  11218. BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
  11219. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11220. BPF_MOV64_IMM(BPF_REG_9, 0),
  11221. BPF_JMP_IMM(BPF_JA, 0, 0, 1),
  11222. BPF_MOV64_IMM(BPF_REG_9, 1),
  11223. /* call 3rd func with fp-8, 0|1, fp-16, 0|1 */
  11224. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  11225. BPF_MOV64_REG(BPF_REG_2, BPF_REG_8),
  11226. BPF_MOV64_REG(BPF_REG_3, BPF_REG_7),
  11227. BPF_MOV64_REG(BPF_REG_4, BPF_REG_9),
  11228. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  11229. BPF_EXIT_INSN(),
  11230. /* subprog 2 */
  11231. /* if arg2 == 1 do *arg1 = 0 */
  11232. BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 1, 2),
  11233. /* fetch map_value_ptr from the stack of this function */
  11234. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  11235. /* write into map value */
  11236. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11237. /* if arg4 == 0 do *arg3 = 0 */
  11238. BPF_JMP_IMM(BPF_JNE, BPF_REG_4, 0, 2),
  11239. /* fetch map_value_ptr from the stack of this function */
  11240. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
  11241. /* write into map value */
  11242. BPF_ST_MEM(BPF_DW, BPF_REG_0, 0, 0),
  11243. BPF_EXIT_INSN(),
  11244. },
  11245. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11246. .fixup_map1 = { 12, 22 },
  11247. .result = REJECT,
  11248. .errstr = "R0 invalid mem access 'inv'",
  11249. },
  11250. {
  11251. "calls: pkt_ptr spill into caller stack",
  11252. .insns = {
  11253. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11254. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11255. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 1),
  11256. BPF_EXIT_INSN(),
  11257. /* subprog 1 */
  11258. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11259. offsetof(struct __sk_buff, data)),
  11260. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11261. offsetof(struct __sk_buff, data_end)),
  11262. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11263. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11264. /* spill unchecked pkt_ptr into stack of caller */
  11265. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11266. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
  11267. /* now the pkt range is verified, read pkt_ptr from stack */
  11268. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_4, 0),
  11269. /* write 4 bytes into packet */
  11270. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11271. BPF_EXIT_INSN(),
  11272. },
  11273. .result = ACCEPT,
  11274. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11275. .retval = POINTER_VALUE,
  11276. },
  11277. {
  11278. "calls: pkt_ptr spill into caller stack 2",
  11279. .insns = {
  11280. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11281. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11282. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  11283. /* Marking is still kept, but not in all cases safe. */
  11284. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11285. BPF_ST_MEM(BPF_W, BPF_REG_4, 0, 0),
  11286. BPF_EXIT_INSN(),
  11287. /* subprog 1 */
  11288. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11289. offsetof(struct __sk_buff, data)),
  11290. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11291. offsetof(struct __sk_buff, data_end)),
  11292. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11293. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11294. /* spill unchecked pkt_ptr into stack of caller */
  11295. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11296. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
  11297. /* now the pkt range is verified, read pkt_ptr from stack */
  11298. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_4, 0),
  11299. /* write 4 bytes into packet */
  11300. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11301. BPF_EXIT_INSN(),
  11302. },
  11303. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11304. .errstr = "invalid access to packet",
  11305. .result = REJECT,
  11306. },
  11307. {
  11308. "calls: pkt_ptr spill into caller stack 3",
  11309. .insns = {
  11310. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11311. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11312. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  11313. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  11314. /* Marking is still kept and safe here. */
  11315. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11316. BPF_ST_MEM(BPF_W, BPF_REG_4, 0, 0),
  11317. BPF_EXIT_INSN(),
  11318. /* subprog 1 */
  11319. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11320. offsetof(struct __sk_buff, data)),
  11321. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11322. offsetof(struct __sk_buff, data_end)),
  11323. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11324. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11325. /* spill unchecked pkt_ptr into stack of caller */
  11326. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11327. BPF_MOV64_IMM(BPF_REG_5, 0),
  11328. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
  11329. BPF_MOV64_IMM(BPF_REG_5, 1),
  11330. /* now the pkt range is verified, read pkt_ptr from stack */
  11331. BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_4, 0),
  11332. /* write 4 bytes into packet */
  11333. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11334. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11335. BPF_EXIT_INSN(),
  11336. },
  11337. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11338. .result = ACCEPT,
  11339. .retval = 1,
  11340. },
  11341. {
  11342. "calls: pkt_ptr spill into caller stack 4",
  11343. .insns = {
  11344. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11345. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11346. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  11347. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  11348. /* Check marking propagated. */
  11349. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11350. BPF_ST_MEM(BPF_W, BPF_REG_4, 0, 0),
  11351. BPF_EXIT_INSN(),
  11352. /* subprog 1 */
  11353. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11354. offsetof(struct __sk_buff, data)),
  11355. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11356. offsetof(struct __sk_buff, data_end)),
  11357. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11358. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11359. /* spill unchecked pkt_ptr into stack of caller */
  11360. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11361. BPF_MOV64_IMM(BPF_REG_5, 0),
  11362. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
  11363. BPF_MOV64_IMM(BPF_REG_5, 1),
  11364. /* don't read back pkt_ptr from stack here */
  11365. /* write 4 bytes into packet */
  11366. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11367. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11368. BPF_EXIT_INSN(),
  11369. },
  11370. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11371. .result = ACCEPT,
  11372. .retval = 1,
  11373. },
  11374. {
  11375. "calls: pkt_ptr spill into caller stack 5",
  11376. .insns = {
  11377. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11378. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11379. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_1, 0),
  11380. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  11381. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11382. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_4, 0),
  11383. BPF_EXIT_INSN(),
  11384. /* subprog 1 */
  11385. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11386. offsetof(struct __sk_buff, data)),
  11387. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11388. offsetof(struct __sk_buff, data_end)),
  11389. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11390. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11391. BPF_MOV64_IMM(BPF_REG_5, 0),
  11392. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
  11393. /* spill checked pkt_ptr into stack of caller */
  11394. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11395. BPF_MOV64_IMM(BPF_REG_5, 1),
  11396. /* don't read back pkt_ptr from stack here */
  11397. /* write 4 bytes into packet */
  11398. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11399. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11400. BPF_EXIT_INSN(),
  11401. },
  11402. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11403. .errstr = "same insn cannot be used with different",
  11404. .result = REJECT,
  11405. },
  11406. {
  11407. "calls: pkt_ptr spill into caller stack 6",
  11408. .insns = {
  11409. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11410. offsetof(struct __sk_buff, data_end)),
  11411. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11412. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11413. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11414. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  11415. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11416. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_4, 0),
  11417. BPF_EXIT_INSN(),
  11418. /* subprog 1 */
  11419. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11420. offsetof(struct __sk_buff, data)),
  11421. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11422. offsetof(struct __sk_buff, data_end)),
  11423. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11424. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11425. BPF_MOV64_IMM(BPF_REG_5, 0),
  11426. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
  11427. /* spill checked pkt_ptr into stack of caller */
  11428. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11429. BPF_MOV64_IMM(BPF_REG_5, 1),
  11430. /* don't read back pkt_ptr from stack here */
  11431. /* write 4 bytes into packet */
  11432. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11433. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11434. BPF_EXIT_INSN(),
  11435. },
  11436. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11437. .errstr = "R4 invalid mem access",
  11438. .result = REJECT,
  11439. },
  11440. {
  11441. "calls: pkt_ptr spill into caller stack 7",
  11442. .insns = {
  11443. BPF_MOV64_IMM(BPF_REG_2, 0),
  11444. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11445. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11446. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11447. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  11448. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11449. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_4, 0),
  11450. BPF_EXIT_INSN(),
  11451. /* subprog 1 */
  11452. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11453. offsetof(struct __sk_buff, data)),
  11454. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11455. offsetof(struct __sk_buff, data_end)),
  11456. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11457. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11458. BPF_MOV64_IMM(BPF_REG_5, 0),
  11459. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
  11460. /* spill checked pkt_ptr into stack of caller */
  11461. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11462. BPF_MOV64_IMM(BPF_REG_5, 1),
  11463. /* don't read back pkt_ptr from stack here */
  11464. /* write 4 bytes into packet */
  11465. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11466. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11467. BPF_EXIT_INSN(),
  11468. },
  11469. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11470. .errstr = "R4 invalid mem access",
  11471. .result = REJECT,
  11472. },
  11473. {
  11474. "calls: pkt_ptr spill into caller stack 8",
  11475. .insns = {
  11476. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11477. offsetof(struct __sk_buff, data)),
  11478. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11479. offsetof(struct __sk_buff, data_end)),
  11480. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11481. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11482. BPF_JMP_REG(BPF_JLE, BPF_REG_0, BPF_REG_3, 1),
  11483. BPF_EXIT_INSN(),
  11484. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11485. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11486. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11487. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  11488. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11489. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_4, 0),
  11490. BPF_EXIT_INSN(),
  11491. /* subprog 1 */
  11492. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11493. offsetof(struct __sk_buff, data)),
  11494. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11495. offsetof(struct __sk_buff, data_end)),
  11496. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11497. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11498. BPF_MOV64_IMM(BPF_REG_5, 0),
  11499. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
  11500. /* spill checked pkt_ptr into stack of caller */
  11501. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11502. BPF_MOV64_IMM(BPF_REG_5, 1),
  11503. /* don't read back pkt_ptr from stack here */
  11504. /* write 4 bytes into packet */
  11505. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11506. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11507. BPF_EXIT_INSN(),
  11508. },
  11509. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11510. .result = ACCEPT,
  11511. },
  11512. {
  11513. "calls: pkt_ptr spill into caller stack 9",
  11514. .insns = {
  11515. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11516. offsetof(struct __sk_buff, data)),
  11517. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11518. offsetof(struct __sk_buff, data_end)),
  11519. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11520. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11521. BPF_JMP_REG(BPF_JLE, BPF_REG_0, BPF_REG_3, 1),
  11522. BPF_EXIT_INSN(),
  11523. BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
  11524. BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
  11525. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11526. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
  11527. BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
  11528. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_4, 0),
  11529. BPF_EXIT_INSN(),
  11530. /* subprog 1 */
  11531. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11532. offsetof(struct __sk_buff, data)),
  11533. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11534. offsetof(struct __sk_buff, data_end)),
  11535. BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
  11536. BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 8),
  11537. BPF_MOV64_IMM(BPF_REG_5, 0),
  11538. /* spill unchecked pkt_ptr into stack of caller */
  11539. BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_2, 0),
  11540. BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
  11541. BPF_MOV64_IMM(BPF_REG_5, 1),
  11542. /* don't read back pkt_ptr from stack here */
  11543. /* write 4 bytes into packet */
  11544. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11545. BPF_MOV64_REG(BPF_REG_0, BPF_REG_5),
  11546. BPF_EXIT_INSN(),
  11547. },
  11548. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11549. .errstr = "invalid access to packet",
  11550. .result = REJECT,
  11551. },
  11552. {
  11553. "calls: caller stack init to zero or map_value_or_null",
  11554. .insns = {
  11555. BPF_MOV64_IMM(BPF_REG_0, 0),
  11556. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  11557. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11558. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11559. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
  11560. /* fetch map_value_or_null or const_zero from stack */
  11561. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  11562. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
  11563. /* store into map_value */
  11564. BPF_ST_MEM(BPF_W, BPF_REG_0, 0, 0),
  11565. BPF_EXIT_INSN(),
  11566. /* subprog 1 */
  11567. /* if (ctx == 0) return; */
  11568. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 8),
  11569. /* else bpf_map_lookup() and *(fp - 8) = r0 */
  11570. BPF_MOV64_REG(BPF_REG_6, BPF_REG_2),
  11571. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11572. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11573. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11574. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11575. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11576. BPF_FUNC_map_lookup_elem),
  11577. /* write map_value_ptr_or_null into stack frame of main prog at fp-8 */
  11578. BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
  11579. BPF_EXIT_INSN(),
  11580. },
  11581. .fixup_map1 = { 13 },
  11582. .result = ACCEPT,
  11583. .prog_type = BPF_PROG_TYPE_XDP,
  11584. },
  11585. {
  11586. "calls: stack init to zero and pruning",
  11587. .insns = {
  11588. /* first make allocated_stack 16 byte */
  11589. BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, 0),
  11590. /* now fork the execution such that the false branch
  11591. * of JGT insn will be verified second and it skisp zero
  11592. * init of fp-8 stack slot. If stack liveness marking
  11593. * is missing live_read marks from call map_lookup
  11594. * processing then pruning will incorrectly assume
  11595. * that fp-8 stack slot was unused in the fall-through
  11596. * branch and will accept the program incorrectly
  11597. */
  11598. BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 2, 2),
  11599. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11600. BPF_JMP_IMM(BPF_JA, 0, 0, 0),
  11601. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11602. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11603. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11604. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11605. BPF_FUNC_map_lookup_elem),
  11606. BPF_EXIT_INSN(),
  11607. },
  11608. .fixup_map2 = { 6 },
  11609. .errstr = "invalid indirect read from stack off -8+0 size 8",
  11610. .result = REJECT,
  11611. .prog_type = BPF_PROG_TYPE_XDP,
  11612. },
  11613. {
  11614. "search pruning: all branches should be verified (nop operation)",
  11615. .insns = {
  11616. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11617. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11618. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  11619. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11620. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  11621. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 11),
  11622. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0),
  11623. BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0xbeef, 2),
  11624. BPF_MOV64_IMM(BPF_REG_4, 0),
  11625. BPF_JMP_A(1),
  11626. BPF_MOV64_IMM(BPF_REG_4, 1),
  11627. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_4, -16),
  11628. BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
  11629. BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_10, -16),
  11630. BPF_JMP_IMM(BPF_JEQ, BPF_REG_5, 0, 2),
  11631. BPF_MOV64_IMM(BPF_REG_6, 0),
  11632. BPF_ST_MEM(BPF_DW, BPF_REG_6, 0, 0xdead),
  11633. BPF_EXIT_INSN(),
  11634. },
  11635. .fixup_map1 = { 3 },
  11636. .errstr = "R6 invalid mem access 'inv'",
  11637. .result = REJECT,
  11638. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  11639. },
  11640. {
  11641. "search pruning: all branches should be verified (invalid stack access)",
  11642. .insns = {
  11643. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11644. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11645. BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0),
  11646. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11647. BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
  11648. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 8),
  11649. BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0),
  11650. BPF_MOV64_IMM(BPF_REG_4, 0),
  11651. BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0xbeef, 2),
  11652. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_4, -16),
  11653. BPF_JMP_A(1),
  11654. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_4, -24),
  11655. BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
  11656. BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_10, -16),
  11657. BPF_EXIT_INSN(),
  11658. },
  11659. .fixup_map1 = { 3 },
  11660. .errstr = "invalid read from stack off -16+0 size 8",
  11661. .result = REJECT,
  11662. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  11663. },
  11664. {
  11665. "jit: lsh, rsh, arsh by 1",
  11666. .insns = {
  11667. BPF_MOV64_IMM(BPF_REG_0, 1),
  11668. BPF_MOV64_IMM(BPF_REG_1, 0xff),
  11669. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 1),
  11670. BPF_ALU32_IMM(BPF_LSH, BPF_REG_1, 1),
  11671. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x3fc, 1),
  11672. BPF_EXIT_INSN(),
  11673. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 1),
  11674. BPF_ALU32_IMM(BPF_RSH, BPF_REG_1, 1),
  11675. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0xff, 1),
  11676. BPF_EXIT_INSN(),
  11677. BPF_ALU64_IMM(BPF_ARSH, BPF_REG_1, 1),
  11678. BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x7f, 1),
  11679. BPF_EXIT_INSN(),
  11680. BPF_MOV64_IMM(BPF_REG_0, 2),
  11681. BPF_EXIT_INSN(),
  11682. },
  11683. .result = ACCEPT,
  11684. .retval = 2,
  11685. },
  11686. {
  11687. "jit: mov32 for ldimm64, 1",
  11688. .insns = {
  11689. BPF_MOV64_IMM(BPF_REG_0, 2),
  11690. BPF_LD_IMM64(BPF_REG_1, 0xfeffffffffffffffULL),
  11691. BPF_ALU64_IMM(BPF_RSH, BPF_REG_1, 32),
  11692. BPF_LD_IMM64(BPF_REG_2, 0xfeffffffULL),
  11693. BPF_JMP_REG(BPF_JEQ, BPF_REG_1, BPF_REG_2, 1),
  11694. BPF_MOV64_IMM(BPF_REG_0, 1),
  11695. BPF_EXIT_INSN(),
  11696. },
  11697. .result = ACCEPT,
  11698. .retval = 2,
  11699. },
  11700. {
  11701. "jit: mov32 for ldimm64, 2",
  11702. .insns = {
  11703. BPF_MOV64_IMM(BPF_REG_0, 1),
  11704. BPF_LD_IMM64(BPF_REG_1, 0x1ffffffffULL),
  11705. BPF_LD_IMM64(BPF_REG_2, 0xffffffffULL),
  11706. BPF_JMP_REG(BPF_JEQ, BPF_REG_1, BPF_REG_2, 1),
  11707. BPF_MOV64_IMM(BPF_REG_0, 2),
  11708. BPF_EXIT_INSN(),
  11709. },
  11710. .result = ACCEPT,
  11711. .retval = 2,
  11712. },
  11713. {
  11714. "jit: various mul tests",
  11715. .insns = {
  11716. BPF_LD_IMM64(BPF_REG_2, 0xeeff0d413122ULL),
  11717. BPF_LD_IMM64(BPF_REG_0, 0xfefefeULL),
  11718. BPF_LD_IMM64(BPF_REG_1, 0xefefefULL),
  11719. BPF_ALU64_REG(BPF_MUL, BPF_REG_0, BPF_REG_1),
  11720. BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_2, 2),
  11721. BPF_MOV64_IMM(BPF_REG_0, 1),
  11722. BPF_EXIT_INSN(),
  11723. BPF_LD_IMM64(BPF_REG_3, 0xfefefeULL),
  11724. BPF_ALU64_REG(BPF_MUL, BPF_REG_3, BPF_REG_1),
  11725. BPF_JMP_REG(BPF_JEQ, BPF_REG_3, BPF_REG_2, 2),
  11726. BPF_MOV64_IMM(BPF_REG_0, 1),
  11727. BPF_EXIT_INSN(),
  11728. BPF_MOV32_REG(BPF_REG_2, BPF_REG_2),
  11729. BPF_LD_IMM64(BPF_REG_0, 0xfefefeULL),
  11730. BPF_ALU32_REG(BPF_MUL, BPF_REG_0, BPF_REG_1),
  11731. BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_2, 2),
  11732. BPF_MOV64_IMM(BPF_REG_0, 1),
  11733. BPF_EXIT_INSN(),
  11734. BPF_LD_IMM64(BPF_REG_3, 0xfefefeULL),
  11735. BPF_ALU32_REG(BPF_MUL, BPF_REG_3, BPF_REG_1),
  11736. BPF_JMP_REG(BPF_JEQ, BPF_REG_3, BPF_REG_2, 2),
  11737. BPF_MOV64_IMM(BPF_REG_0, 1),
  11738. BPF_EXIT_INSN(),
  11739. BPF_LD_IMM64(BPF_REG_0, 0x952a7bbcULL),
  11740. BPF_LD_IMM64(BPF_REG_1, 0xfefefeULL),
  11741. BPF_LD_IMM64(BPF_REG_2, 0xeeff0d413122ULL),
  11742. BPF_ALU32_REG(BPF_MUL, BPF_REG_2, BPF_REG_1),
  11743. BPF_JMP_REG(BPF_JEQ, BPF_REG_2, BPF_REG_0, 2),
  11744. BPF_MOV64_IMM(BPF_REG_0, 1),
  11745. BPF_EXIT_INSN(),
  11746. BPF_MOV64_IMM(BPF_REG_0, 2),
  11747. BPF_EXIT_INSN(),
  11748. },
  11749. .result = ACCEPT,
  11750. .retval = 2,
  11751. },
  11752. {
  11753. "xadd/w check unaligned stack",
  11754. .insns = {
  11755. BPF_MOV64_IMM(BPF_REG_0, 1),
  11756. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  11757. BPF_STX_XADD(BPF_W, BPF_REG_10, BPF_REG_0, -7),
  11758. BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
  11759. BPF_EXIT_INSN(),
  11760. },
  11761. .result = REJECT,
  11762. .errstr = "misaligned stack access off",
  11763. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11764. },
  11765. {
  11766. "xadd/w check unaligned map",
  11767. .insns = {
  11768. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11769. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11770. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11771. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11772. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11773. BPF_FUNC_map_lookup_elem),
  11774. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
  11775. BPF_EXIT_INSN(),
  11776. BPF_MOV64_IMM(BPF_REG_1, 1),
  11777. BPF_STX_XADD(BPF_W, BPF_REG_0, BPF_REG_1, 3),
  11778. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 3),
  11779. BPF_EXIT_INSN(),
  11780. },
  11781. .fixup_map1 = { 3 },
  11782. .result = REJECT,
  11783. .errstr = "misaligned value access off",
  11784. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11785. },
  11786. {
  11787. "xadd/w check unaligned pkt",
  11788. .insns = {
  11789. BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
  11790. offsetof(struct xdp_md, data)),
  11791. BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
  11792. offsetof(struct xdp_md, data_end)),
  11793. BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
  11794. BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 8),
  11795. BPF_JMP_REG(BPF_JLT, BPF_REG_1, BPF_REG_3, 2),
  11796. BPF_MOV64_IMM(BPF_REG_0, 99),
  11797. BPF_JMP_IMM(BPF_JA, 0, 0, 6),
  11798. BPF_MOV64_IMM(BPF_REG_0, 1),
  11799. BPF_ST_MEM(BPF_W, BPF_REG_2, 0, 0),
  11800. BPF_ST_MEM(BPF_W, BPF_REG_2, 3, 0),
  11801. BPF_STX_XADD(BPF_W, BPF_REG_2, BPF_REG_0, 1),
  11802. BPF_STX_XADD(BPF_W, BPF_REG_2, BPF_REG_0, 2),
  11803. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 1),
  11804. BPF_EXIT_INSN(),
  11805. },
  11806. .result = REJECT,
  11807. .errstr = "BPF_XADD stores into R2 packet",
  11808. .prog_type = BPF_PROG_TYPE_XDP,
  11809. },
  11810. {
  11811. "bpf_get_stack return R0 within range",
  11812. .insns = {
  11813. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11814. BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
  11815. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  11816. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
  11817. BPF_LD_MAP_FD(BPF_REG_1, 0),
  11818. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  11819. BPF_FUNC_map_lookup_elem),
  11820. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 28),
  11821. BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
  11822. BPF_MOV64_IMM(BPF_REG_9, sizeof(struct test_val)),
  11823. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  11824. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  11825. BPF_MOV64_IMM(BPF_REG_3, sizeof(struct test_val)),
  11826. BPF_MOV64_IMM(BPF_REG_4, 256),
  11827. BPF_EMIT_CALL(BPF_FUNC_get_stack),
  11828. BPF_MOV64_IMM(BPF_REG_1, 0),
  11829. BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
  11830. BPF_ALU64_IMM(BPF_LSH, BPF_REG_8, 32),
  11831. BPF_ALU64_IMM(BPF_ARSH, BPF_REG_8, 32),
  11832. BPF_JMP_REG(BPF_JSLT, BPF_REG_1, BPF_REG_8, 16),
  11833. BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_8),
  11834. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  11835. BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_8),
  11836. BPF_MOV64_REG(BPF_REG_1, BPF_REG_9),
  11837. BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 32),
  11838. BPF_ALU64_IMM(BPF_ARSH, BPF_REG_1, 32),
  11839. BPF_MOV64_REG(BPF_REG_3, BPF_REG_2),
  11840. BPF_ALU64_REG(BPF_ADD, BPF_REG_3, BPF_REG_1),
  11841. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  11842. BPF_MOV64_IMM(BPF_REG_5, sizeof(struct test_val)),
  11843. BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_5),
  11844. BPF_JMP_REG(BPF_JGE, BPF_REG_3, BPF_REG_1, 4),
  11845. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  11846. BPF_MOV64_REG(BPF_REG_3, BPF_REG_9),
  11847. BPF_MOV64_IMM(BPF_REG_4, 0),
  11848. BPF_EMIT_CALL(BPF_FUNC_get_stack),
  11849. BPF_EXIT_INSN(),
  11850. },
  11851. .fixup_map2 = { 4 },
  11852. .result = ACCEPT,
  11853. .prog_type = BPF_PROG_TYPE_TRACEPOINT,
  11854. },
  11855. {
  11856. "ld_abs: invalid op 1",
  11857. .insns = {
  11858. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11859. BPF_LD_ABS(BPF_DW, 0),
  11860. BPF_EXIT_INSN(),
  11861. },
  11862. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11863. .result = REJECT,
  11864. .errstr = "unknown opcode",
  11865. },
  11866. {
  11867. "ld_abs: invalid op 2",
  11868. .insns = {
  11869. BPF_MOV32_IMM(BPF_REG_0, 256),
  11870. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11871. BPF_LD_IND(BPF_DW, BPF_REG_0, 0),
  11872. BPF_EXIT_INSN(),
  11873. },
  11874. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11875. .result = REJECT,
  11876. .errstr = "unknown opcode",
  11877. },
  11878. {
  11879. "ld_abs: nmap reduced",
  11880. .insns = {
  11881. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  11882. BPF_LD_ABS(BPF_H, 12),
  11883. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0x806, 28),
  11884. BPF_LD_ABS(BPF_H, 12),
  11885. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0x806, 26),
  11886. BPF_MOV32_IMM(BPF_REG_0, 18),
  11887. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -64),
  11888. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_10, -64),
  11889. BPF_LD_IND(BPF_W, BPF_REG_7, 14),
  11890. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -60),
  11891. BPF_MOV32_IMM(BPF_REG_0, 280971478),
  11892. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -56),
  11893. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_10, -56),
  11894. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_10, -60),
  11895. BPF_ALU32_REG(BPF_SUB, BPF_REG_0, BPF_REG_7),
  11896. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 15),
  11897. BPF_LD_ABS(BPF_H, 12),
  11898. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0x806, 13),
  11899. BPF_MOV32_IMM(BPF_REG_0, 22),
  11900. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -56),
  11901. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_10, -56),
  11902. BPF_LD_IND(BPF_H, BPF_REG_7, 14),
  11903. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -52),
  11904. BPF_MOV32_IMM(BPF_REG_0, 17366),
  11905. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -48),
  11906. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_10, -48),
  11907. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_10, -52),
  11908. BPF_ALU32_REG(BPF_SUB, BPF_REG_0, BPF_REG_7),
  11909. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
  11910. BPF_MOV32_IMM(BPF_REG_0, 256),
  11911. BPF_EXIT_INSN(),
  11912. BPF_MOV32_IMM(BPF_REG_0, 0),
  11913. BPF_EXIT_INSN(),
  11914. },
  11915. .data = {
  11916. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x06, 0,
  11917. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11918. 0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6,
  11919. },
  11920. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11921. .result = ACCEPT,
  11922. .retval = 256,
  11923. },
  11924. {
  11925. "ld_abs: div + abs, test 1",
  11926. .insns = {
  11927. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
  11928. BPF_LD_ABS(BPF_B, 3),
  11929. BPF_ALU64_IMM(BPF_MOV, BPF_REG_2, 2),
  11930. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_2),
  11931. BPF_ALU64_REG(BPF_MOV, BPF_REG_8, BPF_REG_0),
  11932. BPF_LD_ABS(BPF_B, 4),
  11933. BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_0),
  11934. BPF_LD_IND(BPF_B, BPF_REG_8, -70),
  11935. BPF_EXIT_INSN(),
  11936. },
  11937. .data = {
  11938. 10, 20, 30, 40, 50,
  11939. },
  11940. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11941. .result = ACCEPT,
  11942. .retval = 10,
  11943. },
  11944. {
  11945. "ld_abs: div + abs, test 2",
  11946. .insns = {
  11947. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
  11948. BPF_LD_ABS(BPF_B, 3),
  11949. BPF_ALU64_IMM(BPF_MOV, BPF_REG_2, 2),
  11950. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_2),
  11951. BPF_ALU64_REG(BPF_MOV, BPF_REG_8, BPF_REG_0),
  11952. BPF_LD_ABS(BPF_B, 128),
  11953. BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_0),
  11954. BPF_LD_IND(BPF_B, BPF_REG_8, -70),
  11955. BPF_EXIT_INSN(),
  11956. },
  11957. .data = {
  11958. 10, 20, 30, 40, 50,
  11959. },
  11960. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11961. .result = ACCEPT,
  11962. .retval = 0,
  11963. },
  11964. {
  11965. "ld_abs: div + abs, test 3",
  11966. .insns = {
  11967. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
  11968. BPF_ALU64_IMM(BPF_MOV, BPF_REG_7, 0),
  11969. BPF_LD_ABS(BPF_B, 3),
  11970. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_7),
  11971. BPF_EXIT_INSN(),
  11972. },
  11973. .data = {
  11974. 10, 20, 30, 40, 50,
  11975. },
  11976. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11977. .result = ACCEPT,
  11978. .retval = 0,
  11979. },
  11980. {
  11981. "ld_abs: div + abs, test 4",
  11982. .insns = {
  11983. BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_1),
  11984. BPF_ALU64_IMM(BPF_MOV, BPF_REG_7, 0),
  11985. BPF_LD_ABS(BPF_B, 256),
  11986. BPF_ALU32_REG(BPF_DIV, BPF_REG_0, BPF_REG_7),
  11987. BPF_EXIT_INSN(),
  11988. },
  11989. .data = {
  11990. 10, 20, 30, 40, 50,
  11991. },
  11992. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  11993. .result = ACCEPT,
  11994. .retval = 0,
  11995. },
  11996. {
  11997. "ld_abs: vlan + abs, test 1",
  11998. .insns = { },
  11999. .data = {
  12000. 0x34,
  12001. },
  12002. .fill_helper = bpf_fill_ld_abs_vlan_push_pop,
  12003. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12004. .result = ACCEPT,
  12005. .retval = 0xbef,
  12006. },
  12007. {
  12008. "ld_abs: vlan + abs, test 2",
  12009. .insns = {
  12010. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  12011. BPF_LD_ABS(BPF_B, 0),
  12012. BPF_LD_ABS(BPF_H, 0),
  12013. BPF_LD_ABS(BPF_W, 0),
  12014. BPF_MOV64_REG(BPF_REG_7, BPF_REG_6),
  12015. BPF_MOV64_IMM(BPF_REG_6, 0),
  12016. BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
  12017. BPF_MOV64_IMM(BPF_REG_2, 1),
  12018. BPF_MOV64_IMM(BPF_REG_3, 2),
  12019. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  12020. BPF_FUNC_skb_vlan_push),
  12021. BPF_MOV64_REG(BPF_REG_6, BPF_REG_7),
  12022. BPF_LD_ABS(BPF_B, 0),
  12023. BPF_LD_ABS(BPF_H, 0),
  12024. BPF_LD_ABS(BPF_W, 0),
  12025. BPF_MOV64_IMM(BPF_REG_0, 42),
  12026. BPF_EXIT_INSN(),
  12027. },
  12028. .data = {
  12029. 0x34,
  12030. },
  12031. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12032. .result = ACCEPT,
  12033. .retval = 42,
  12034. },
  12035. {
  12036. "ld_abs: jump around ld_abs",
  12037. .insns = { },
  12038. .data = {
  12039. 10, 11,
  12040. },
  12041. .fill_helper = bpf_fill_jump_around_ld_abs,
  12042. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12043. .result = ACCEPT,
  12044. .retval = 10,
  12045. },
  12046. {
  12047. "ld_dw: xor semi-random 64 bit imms, test 1",
  12048. .insns = { },
  12049. .data = { },
  12050. .fill_helper = bpf_fill_rand_ld_dw,
  12051. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12052. .result = ACCEPT,
  12053. .retval = 4090,
  12054. },
  12055. {
  12056. "ld_dw: xor semi-random 64 bit imms, test 2",
  12057. .insns = { },
  12058. .data = { },
  12059. .fill_helper = bpf_fill_rand_ld_dw,
  12060. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12061. .result = ACCEPT,
  12062. .retval = 2047,
  12063. },
  12064. {
  12065. "ld_dw: xor semi-random 64 bit imms, test 3",
  12066. .insns = { },
  12067. .data = { },
  12068. .fill_helper = bpf_fill_rand_ld_dw,
  12069. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12070. .result = ACCEPT,
  12071. .retval = 511,
  12072. },
  12073. {
  12074. "ld_dw: xor semi-random 64 bit imms, test 4",
  12075. .insns = { },
  12076. .data = { },
  12077. .fill_helper = bpf_fill_rand_ld_dw,
  12078. .prog_type = BPF_PROG_TYPE_SCHED_CLS,
  12079. .result = ACCEPT,
  12080. .retval = 5,
  12081. },
  12082. };
  12083. static int probe_filter_length(const struct bpf_insn *fp)
  12084. {
  12085. int len;
  12086. for (len = MAX_INSNS - 1; len > 0; --len)
  12087. if (fp[len].code != 0 || fp[len].imm != 0)
  12088. break;
  12089. return len + 1;
  12090. }
  12091. static int create_map(uint32_t size_value, uint32_t max_elem)
  12092. {
  12093. int fd;
  12094. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(long long),
  12095. size_value, max_elem, BPF_F_NO_PREALLOC);
  12096. if (fd < 0)
  12097. printf("Failed to create hash map '%s'!\n", strerror(errno));
  12098. return fd;
  12099. }
  12100. static int create_prog_dummy1(void)
  12101. {
  12102. struct bpf_insn prog[] = {
  12103. BPF_MOV64_IMM(BPF_REG_0, 42),
  12104. BPF_EXIT_INSN(),
  12105. };
  12106. return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
  12107. ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
  12108. }
  12109. static int create_prog_dummy2(int mfd, int idx)
  12110. {
  12111. struct bpf_insn prog[] = {
  12112. BPF_MOV64_IMM(BPF_REG_3, idx),
  12113. BPF_LD_MAP_FD(BPF_REG_2, mfd),
  12114. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  12115. BPF_FUNC_tail_call),
  12116. BPF_MOV64_IMM(BPF_REG_0, 41),
  12117. BPF_EXIT_INSN(),
  12118. };
  12119. return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
  12120. ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
  12121. }
  12122. static int create_prog_array(void)
  12123. {
  12124. int p1key = 0, p2key = 1;
  12125. int mfd, p1fd, p2fd;
  12126. mfd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(int),
  12127. sizeof(int), 4, 0);
  12128. if (mfd < 0) {
  12129. printf("Failed to create prog array '%s'!\n", strerror(errno));
  12130. return -1;
  12131. }
  12132. p1fd = create_prog_dummy1();
  12133. p2fd = create_prog_dummy2(mfd, p2key);
  12134. if (p1fd < 0 || p2fd < 0)
  12135. goto out;
  12136. if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0)
  12137. goto out;
  12138. if (bpf_map_update_elem(mfd, &p2key, &p2fd, BPF_ANY) < 0)
  12139. goto out;
  12140. close(p2fd);
  12141. close(p1fd);
  12142. return mfd;
  12143. out:
  12144. close(p2fd);
  12145. close(p1fd);
  12146. close(mfd);
  12147. return -1;
  12148. }
  12149. static int create_map_in_map(void)
  12150. {
  12151. int inner_map_fd, outer_map_fd;
  12152. inner_map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(int),
  12153. sizeof(int), 1, 0);
  12154. if (inner_map_fd < 0) {
  12155. printf("Failed to create array '%s'!\n", strerror(errno));
  12156. return inner_map_fd;
  12157. }
  12158. outer_map_fd = bpf_create_map_in_map(BPF_MAP_TYPE_ARRAY_OF_MAPS, NULL,
  12159. sizeof(int), inner_map_fd, 1, 0);
  12160. if (outer_map_fd < 0)
  12161. printf("Failed to create array of maps '%s'!\n",
  12162. strerror(errno));
  12163. close(inner_map_fd);
  12164. return outer_map_fd;
  12165. }
  12166. static char bpf_vlog[UINT_MAX >> 8];
  12167. static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
  12168. int *map_fds)
  12169. {
  12170. int *fixup_map1 = test->fixup_map1;
  12171. int *fixup_map2 = test->fixup_map2;
  12172. int *fixup_map3 = test->fixup_map3;
  12173. int *fixup_prog = test->fixup_prog;
  12174. int *fixup_map_in_map = test->fixup_map_in_map;
  12175. if (test->fill_helper)
  12176. test->fill_helper(test);
  12177. /* Allocating HTs with 1 elem is fine here, since we only test
  12178. * for verifier and not do a runtime lookup, so the only thing
  12179. * that really matters is value size in this case.
  12180. */
  12181. if (*fixup_map1) {
  12182. map_fds[0] = create_map(sizeof(long long), 1);
  12183. do {
  12184. prog[*fixup_map1].imm = map_fds[0];
  12185. fixup_map1++;
  12186. } while (*fixup_map1);
  12187. }
  12188. if (*fixup_map2) {
  12189. map_fds[1] = create_map(sizeof(struct test_val), 1);
  12190. do {
  12191. prog[*fixup_map2].imm = map_fds[1];
  12192. fixup_map2++;
  12193. } while (*fixup_map2);
  12194. }
  12195. if (*fixup_map3) {
  12196. map_fds[1] = create_map(sizeof(struct other_val), 1);
  12197. do {
  12198. prog[*fixup_map3].imm = map_fds[1];
  12199. fixup_map3++;
  12200. } while (*fixup_map3);
  12201. }
  12202. if (*fixup_prog) {
  12203. map_fds[2] = create_prog_array();
  12204. do {
  12205. prog[*fixup_prog].imm = map_fds[2];
  12206. fixup_prog++;
  12207. } while (*fixup_prog);
  12208. }
  12209. if (*fixup_map_in_map) {
  12210. map_fds[3] = create_map_in_map();
  12211. do {
  12212. prog[*fixup_map_in_map].imm = map_fds[3];
  12213. fixup_map_in_map++;
  12214. } while (*fixup_map_in_map);
  12215. }
  12216. }
  12217. static void do_test_single(struct bpf_test *test, bool unpriv,
  12218. int *passes, int *errors)
  12219. {
  12220. int fd_prog, expected_ret, reject_from_alignment;
  12221. int prog_len, prog_type = test->prog_type;
  12222. struct bpf_insn *prog = test->insns;
  12223. int map_fds[MAX_NR_MAPS];
  12224. const char *expected_err;
  12225. uint32_t retval;
  12226. int i, err;
  12227. for (i = 0; i < MAX_NR_MAPS; i++)
  12228. map_fds[i] = -1;
  12229. do_test_fixup(test, prog, map_fds);
  12230. prog_len = probe_filter_length(prog);
  12231. fd_prog = bpf_verify_program(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER,
  12232. prog, prog_len, test->flags & F_LOAD_WITH_STRICT_ALIGNMENT,
  12233. "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 1);
  12234. expected_ret = unpriv && test->result_unpriv != UNDEF ?
  12235. test->result_unpriv : test->result;
  12236. expected_err = unpriv && test->errstr_unpriv ?
  12237. test->errstr_unpriv : test->errstr;
  12238. reject_from_alignment = fd_prog < 0 &&
  12239. (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) &&
  12240. strstr(bpf_vlog, "Unknown alignment.");
  12241. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  12242. if (reject_from_alignment) {
  12243. printf("FAIL\nFailed due to alignment despite having efficient unaligned access: '%s'!\n",
  12244. strerror(errno));
  12245. goto fail_log;
  12246. }
  12247. #endif
  12248. if (expected_ret == ACCEPT) {
  12249. if (fd_prog < 0 && !reject_from_alignment) {
  12250. printf("FAIL\nFailed to load prog '%s'!\n",
  12251. strerror(errno));
  12252. goto fail_log;
  12253. }
  12254. } else {
  12255. if (fd_prog >= 0) {
  12256. printf("FAIL\nUnexpected success to load!\n");
  12257. goto fail_log;
  12258. }
  12259. if (!strstr(bpf_vlog, expected_err) && !reject_from_alignment) {
  12260. printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
  12261. expected_err, bpf_vlog);
  12262. goto fail_log;
  12263. }
  12264. }
  12265. if (fd_prog >= 0) {
  12266. err = bpf_prog_test_run(fd_prog, 1, test->data,
  12267. sizeof(test->data), NULL, NULL,
  12268. &retval, NULL);
  12269. if (err && errno != 524/*ENOTSUPP*/ && errno != EPERM) {
  12270. printf("Unexpected bpf_prog_test_run error\n");
  12271. goto fail_log;
  12272. }
  12273. if (!err && retval != test->retval &&
  12274. test->retval != POINTER_VALUE) {
  12275. printf("FAIL retval %d != %d\n", retval, test->retval);
  12276. goto fail_log;
  12277. }
  12278. }
  12279. (*passes)++;
  12280. printf("OK%s\n", reject_from_alignment ?
  12281. " (NOTE: reject due to unknown alignment)" : "");
  12282. close_fds:
  12283. close(fd_prog);
  12284. for (i = 0; i < MAX_NR_MAPS; i++)
  12285. close(map_fds[i]);
  12286. sched_yield();
  12287. return;
  12288. fail_log:
  12289. (*errors)++;
  12290. printf("%s", bpf_vlog);
  12291. goto close_fds;
  12292. }
  12293. static bool is_admin(void)
  12294. {
  12295. cap_t caps;
  12296. cap_flag_value_t sysadmin = CAP_CLEAR;
  12297. const cap_value_t cap_val = CAP_SYS_ADMIN;
  12298. #ifdef CAP_IS_SUPPORTED
  12299. if (!CAP_IS_SUPPORTED(CAP_SETFCAP)) {
  12300. perror("cap_get_flag");
  12301. return false;
  12302. }
  12303. #endif
  12304. caps = cap_get_proc();
  12305. if (!caps) {
  12306. perror("cap_get_proc");
  12307. return false;
  12308. }
  12309. if (cap_get_flag(caps, cap_val, CAP_EFFECTIVE, &sysadmin))
  12310. perror("cap_get_flag");
  12311. if (cap_free(caps))
  12312. perror("cap_free");
  12313. return (sysadmin == CAP_SET);
  12314. }
  12315. static int set_admin(bool admin)
  12316. {
  12317. cap_t caps;
  12318. const cap_value_t cap_val = CAP_SYS_ADMIN;
  12319. int ret = -1;
  12320. caps = cap_get_proc();
  12321. if (!caps) {
  12322. perror("cap_get_proc");
  12323. return -1;
  12324. }
  12325. if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_val,
  12326. admin ? CAP_SET : CAP_CLEAR)) {
  12327. perror("cap_set_flag");
  12328. goto out;
  12329. }
  12330. if (cap_set_proc(caps)) {
  12331. perror("cap_set_proc");
  12332. goto out;
  12333. }
  12334. ret = 0;
  12335. out:
  12336. if (cap_free(caps))
  12337. perror("cap_free");
  12338. return ret;
  12339. }
  12340. static void get_unpriv_disabled()
  12341. {
  12342. char buf[2];
  12343. FILE *fd;
  12344. fd = fopen("/proc/sys/"UNPRIV_SYSCTL, "r");
  12345. if (!fd) {
  12346. perror("fopen /proc/sys/"UNPRIV_SYSCTL);
  12347. unpriv_disabled = true;
  12348. return;
  12349. }
  12350. if (fgets(buf, 2, fd) == buf && atoi(buf))
  12351. unpriv_disabled = true;
  12352. fclose(fd);
  12353. }
  12354. static int do_test(bool unpriv, unsigned int from, unsigned int to)
  12355. {
  12356. int i, passes = 0, errors = 0, skips = 0;
  12357. for (i = from; i < to; i++) {
  12358. struct bpf_test *test = &tests[i];
  12359. /* Program types that are not supported by non-root we
  12360. * skip right away.
  12361. */
  12362. if (!test->prog_type && unpriv_disabled) {
  12363. printf("#%d/u %s SKIP\n", i, test->descr);
  12364. skips++;
  12365. } else if (!test->prog_type) {
  12366. if (!unpriv)
  12367. set_admin(false);
  12368. printf("#%d/u %s ", i, test->descr);
  12369. do_test_single(test, true, &passes, &errors);
  12370. if (!unpriv)
  12371. set_admin(true);
  12372. }
  12373. if (unpriv) {
  12374. printf("#%d/p %s SKIP\n", i, test->descr);
  12375. skips++;
  12376. } else {
  12377. printf("#%d/p %s ", i, test->descr);
  12378. do_test_single(test, false, &passes, &errors);
  12379. }
  12380. }
  12381. printf("Summary: %d PASSED, %d SKIPPED, %d FAILED\n", passes,
  12382. skips, errors);
  12383. return errors ? EXIT_FAILURE : EXIT_SUCCESS;
  12384. }
  12385. int main(int argc, char **argv)
  12386. {
  12387. unsigned int from = 0, to = ARRAY_SIZE(tests);
  12388. bool unpriv = !is_admin();
  12389. if (argc == 3) {
  12390. unsigned int l = atoi(argv[argc - 2]);
  12391. unsigned int u = atoi(argv[argc - 1]);
  12392. if (l < to && u < to) {
  12393. from = l;
  12394. to = u + 1;
  12395. }
  12396. } else if (argc == 2) {
  12397. unsigned int t = atoi(argv[argc - 1]);
  12398. if (t < to) {
  12399. from = t;
  12400. to = t + 1;
  12401. }
  12402. }
  12403. get_unpriv_disabled();
  12404. if (unpriv && unpriv_disabled) {
  12405. printf("Cannot run as unprivileged user with sysctl %s.\n",
  12406. UNPRIV_SYSCTL);
  12407. return EXIT_FAILURE;
  12408. }
  12409. bpf_semi_rand_init();
  12410. return do_test(unpriv, from, to);
  12411. }