fs_test.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Landlock tests - Filesystem
  4. *
  5. * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  6. * Copyright © 2020 ANSSI
  7. * Copyright © 2020-2022 Microsoft Corporation
  8. */
  9. #define _GNU_SOURCE
  10. #include <asm/termbits.h>
  11. #include <fcntl.h>
  12. #include <libgen.h>
  13. #include <linux/fiemap.h>
  14. #include <linux/landlock.h>
  15. #include <linux/magic.h>
  16. #include <sched.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/capability.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/mount.h>
  23. #include <sys/prctl.h>
  24. #include <sys/sendfile.h>
  25. #include <sys/socket.h>
  26. #include <sys/stat.h>
  27. #include <sys/sysmacros.h>
  28. #include <sys/un.h>
  29. #include <sys/vfs.h>
  30. #include <unistd.h>
  31. /*
  32. * Intentionally included last to work around header conflict.
  33. * See https://sourceware.org/glibc/wiki/Synchronizing_Headers.
  34. */
  35. #include <linux/fs.h>
  36. #include <linux/mount.h>
  37. #include "common.h"
  38. #ifndef renameat2
  39. int renameat2(int olddirfd, const char *oldpath, int newdirfd,
  40. const char *newpath, unsigned int flags)
  41. {
  42. return syscall(__NR_renameat2, olddirfd, oldpath, newdirfd, newpath,
  43. flags);
  44. }
  45. #endif
  46. #ifndef open_tree
  47. int open_tree(int dfd, const char *filename, unsigned int flags)
  48. {
  49. return syscall(__NR_open_tree, dfd, filename, flags);
  50. }
  51. #endif
  52. #ifndef RENAME_EXCHANGE
  53. #define RENAME_EXCHANGE (1 << 1)
  54. #endif
  55. #define BINARY_PATH "./true"
  56. /* Paths (sibling number and depth) */
  57. static const char dir_s1d1[] = TMP_DIR "/s1d1";
  58. static const char file1_s1d1[] = TMP_DIR "/s1d1/f1";
  59. static const char file2_s1d1[] = TMP_DIR "/s1d1/f2";
  60. static const char dir_s1d2[] = TMP_DIR "/s1d1/s1d2";
  61. static const char file1_s1d2[] = TMP_DIR "/s1d1/s1d2/f1";
  62. static const char file2_s1d2[] = TMP_DIR "/s1d1/s1d2/f2";
  63. static const char dir_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3";
  64. static const char file1_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f1";
  65. static const char file2_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f2";
  66. static const char dir_s2d1[] = TMP_DIR "/s2d1";
  67. static const char file1_s2d1[] = TMP_DIR "/s2d1/f1";
  68. static const char dir_s2d2[] = TMP_DIR "/s2d1/s2d2";
  69. static const char file1_s2d2[] = TMP_DIR "/s2d1/s2d2/f1";
  70. static const char dir_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3";
  71. static const char file1_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f1";
  72. static const char file2_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f2";
  73. static const char dir_s3d1[] = TMP_DIR "/s3d1";
  74. static const char file1_s3d1[] = TMP_DIR "/s3d1/f1";
  75. /* dir_s3d2 is a mount point. */
  76. static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
  77. static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
  78. /*
  79. * layout1 hierarchy:
  80. *
  81. * tmp
  82. * ├── s1d1
  83. * │   ├── f1
  84. * │   ├── f2
  85. * │   └── s1d2
  86. * │   ├── f1
  87. * │   ├── f2
  88. * │   └── s1d3
  89. * │   ├── f1
  90. * │   └── f2
  91. * ├── s2d1
  92. * │   ├── f1
  93. * │   └── s2d2
  94. * │   ├── f1
  95. * │   └── s2d3
  96. * │   ├── f1
  97. * │   └── f2
  98. * └── s3d1
  99. *    ├── f1
  100. * └── s3d2
  101. * └── s3d3
  102. */
  103. static bool fgrep(FILE *const inf, const char *const str)
  104. {
  105. char line[32];
  106. const int slen = strlen(str);
  107. while (!feof(inf)) {
  108. if (!fgets(line, sizeof(line), inf))
  109. break;
  110. if (strncmp(line, str, slen))
  111. continue;
  112. return true;
  113. }
  114. return false;
  115. }
  116. static bool supports_filesystem(const char *const filesystem)
  117. {
  118. char str[32];
  119. int len;
  120. bool res = true;
  121. FILE *const inf = fopen("/proc/filesystems", "r");
  122. /*
  123. * Consider that the filesystem is supported if we cannot get the
  124. * supported ones.
  125. */
  126. if (!inf)
  127. return true;
  128. /* filesystem can be null for bind mounts. */
  129. if (!filesystem)
  130. goto out;
  131. len = snprintf(str, sizeof(str), "nodev\t%s\n", filesystem);
  132. if (len >= sizeof(str))
  133. /* Ignores too-long filesystem names. */
  134. goto out;
  135. res = fgrep(inf, str);
  136. out:
  137. fclose(inf);
  138. return res;
  139. }
  140. static bool cwd_matches_fs(unsigned int fs_magic)
  141. {
  142. struct statfs statfs_buf;
  143. if (!fs_magic)
  144. return true;
  145. if (statfs(".", &statfs_buf))
  146. return true;
  147. return statfs_buf.f_type == fs_magic;
  148. }
  149. static void mkdir_parents(struct __test_metadata *const _metadata,
  150. const char *const path)
  151. {
  152. char *walker;
  153. const char *parent;
  154. int i, err;
  155. ASSERT_NE(path[0], '\0');
  156. walker = strdup(path);
  157. ASSERT_NE(NULL, walker);
  158. parent = walker;
  159. for (i = 1; walker[i]; i++) {
  160. if (walker[i] != '/')
  161. continue;
  162. walker[i] = '\0';
  163. err = mkdir(parent, 0700);
  164. ASSERT_FALSE(err && errno != EEXIST)
  165. {
  166. TH_LOG("Failed to create directory \"%s\": %s", parent,
  167. strerror(errno));
  168. }
  169. walker[i] = '/';
  170. }
  171. free(walker);
  172. }
  173. static void create_directory(struct __test_metadata *const _metadata,
  174. const char *const path)
  175. {
  176. mkdir_parents(_metadata, path);
  177. ASSERT_EQ(0, mkdir(path, 0700))
  178. {
  179. TH_LOG("Failed to create directory \"%s\": %s", path,
  180. strerror(errno));
  181. }
  182. }
  183. static void create_file(struct __test_metadata *const _metadata,
  184. const char *const path)
  185. {
  186. mkdir_parents(_metadata, path);
  187. ASSERT_EQ(0, mknod(path, S_IFREG | 0700, 0))
  188. {
  189. TH_LOG("Failed to create file \"%s\": %s", path,
  190. strerror(errno));
  191. }
  192. }
  193. static int remove_path(const char *const path)
  194. {
  195. char *walker;
  196. int i, ret, err = 0;
  197. walker = strdup(path);
  198. if (!walker) {
  199. err = ENOMEM;
  200. goto out;
  201. }
  202. if (unlink(path) && rmdir(path)) {
  203. if (errno != ENOENT && errno != ENOTDIR)
  204. err = errno;
  205. goto out;
  206. }
  207. for (i = strlen(walker); i > 0; i--) {
  208. if (walker[i] != '/')
  209. continue;
  210. walker[i] = '\0';
  211. ret = rmdir(walker);
  212. if (ret) {
  213. if (errno != ENOTEMPTY && errno != EBUSY)
  214. err = errno;
  215. goto out;
  216. }
  217. if (strcmp(walker, TMP_DIR) == 0)
  218. goto out;
  219. }
  220. out:
  221. free(walker);
  222. return err;
  223. }
  224. struct mnt_opt {
  225. const char *const source;
  226. const char *const type;
  227. const unsigned long flags;
  228. const char *const data;
  229. };
  230. #define MNT_TMP_DATA "size=4m,mode=700"
  231. static const struct mnt_opt mnt_tmp = {
  232. .type = "tmpfs",
  233. .data = MNT_TMP_DATA,
  234. };
  235. static int mount_opt(const struct mnt_opt *const mnt, const char *const target)
  236. {
  237. return mount(mnt->source ?: mnt->type, target, mnt->type, mnt->flags,
  238. mnt->data);
  239. }
  240. static void prepare_layout_opt(struct __test_metadata *const _metadata,
  241. const struct mnt_opt *const mnt)
  242. {
  243. disable_caps(_metadata);
  244. umask(0077);
  245. create_directory(_metadata, TMP_DIR);
  246. /*
  247. * Do not pollute the rest of the system: creates a private mount point
  248. * for tests relying on pivot_root(2) and move_mount(2).
  249. */
  250. set_cap(_metadata, CAP_SYS_ADMIN);
  251. ASSERT_EQ(0, unshare(CLONE_NEWNS | CLONE_NEWCGROUP));
  252. ASSERT_EQ(0, mount_opt(mnt, TMP_DIR))
  253. {
  254. TH_LOG("Failed to mount the %s filesystem: %s", mnt->type,
  255. strerror(errno));
  256. /*
  257. * FIXTURE_TEARDOWN() is not called when FIXTURE_SETUP()
  258. * failed, so we need to explicitly do a minimal cleanup to
  259. * avoid cascading errors with other tests that don't depend on
  260. * the same filesystem.
  261. */
  262. remove_path(TMP_DIR);
  263. }
  264. ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL));
  265. clear_cap(_metadata, CAP_SYS_ADMIN);
  266. }
  267. static void prepare_layout(struct __test_metadata *const _metadata)
  268. {
  269. prepare_layout_opt(_metadata, &mnt_tmp);
  270. }
  271. static void cleanup_layout(struct __test_metadata *const _metadata)
  272. {
  273. set_cap(_metadata, CAP_SYS_ADMIN);
  274. if (umount(TMP_DIR)) {
  275. /*
  276. * According to the test environment, the mount point of the
  277. * current directory may be shared or not, which changes the
  278. * visibility of the nested TMP_DIR mount point for the test's
  279. * parent process doing this cleanup.
  280. */
  281. ASSERT_EQ(EINVAL, errno);
  282. }
  283. clear_cap(_metadata, CAP_SYS_ADMIN);
  284. EXPECT_EQ(0, remove_path(TMP_DIR));
  285. }
  286. /* clang-format off */
  287. FIXTURE(layout0) {};
  288. /* clang-format on */
  289. FIXTURE_SETUP(layout0)
  290. {
  291. prepare_layout(_metadata);
  292. }
  293. FIXTURE_TEARDOWN_PARENT(layout0)
  294. {
  295. cleanup_layout(_metadata);
  296. }
  297. static void create_layout1(struct __test_metadata *const _metadata)
  298. {
  299. create_file(_metadata, file1_s1d1);
  300. create_file(_metadata, file1_s1d2);
  301. create_file(_metadata, file1_s1d3);
  302. create_file(_metadata, file2_s1d1);
  303. create_file(_metadata, file2_s1d2);
  304. create_file(_metadata, file2_s1d3);
  305. create_file(_metadata, file1_s2d1);
  306. create_file(_metadata, file1_s2d2);
  307. create_file(_metadata, file1_s2d3);
  308. create_file(_metadata, file2_s2d3);
  309. create_file(_metadata, file1_s3d1);
  310. create_directory(_metadata, dir_s3d2);
  311. set_cap(_metadata, CAP_SYS_ADMIN);
  312. ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2));
  313. clear_cap(_metadata, CAP_SYS_ADMIN);
  314. ASSERT_EQ(0, mkdir(dir_s3d3, 0700));
  315. }
  316. static void remove_layout1(struct __test_metadata *const _metadata)
  317. {
  318. EXPECT_EQ(0, remove_path(file2_s1d3));
  319. EXPECT_EQ(0, remove_path(file2_s1d2));
  320. EXPECT_EQ(0, remove_path(file2_s1d1));
  321. EXPECT_EQ(0, remove_path(file1_s1d3));
  322. EXPECT_EQ(0, remove_path(file1_s1d2));
  323. EXPECT_EQ(0, remove_path(file1_s1d1));
  324. EXPECT_EQ(0, remove_path(dir_s1d3));
  325. EXPECT_EQ(0, remove_path(file2_s2d3));
  326. EXPECT_EQ(0, remove_path(file1_s2d3));
  327. EXPECT_EQ(0, remove_path(file1_s2d2));
  328. EXPECT_EQ(0, remove_path(file1_s2d1));
  329. EXPECT_EQ(0, remove_path(dir_s2d2));
  330. EXPECT_EQ(0, remove_path(file1_s3d1));
  331. EXPECT_EQ(0, remove_path(dir_s3d3));
  332. set_cap(_metadata, CAP_SYS_ADMIN);
  333. umount(dir_s3d2);
  334. clear_cap(_metadata, CAP_SYS_ADMIN);
  335. EXPECT_EQ(0, remove_path(dir_s3d2));
  336. }
  337. /* clang-format off */
  338. FIXTURE(layout1) {};
  339. /* clang-format on */
  340. FIXTURE_SETUP(layout1)
  341. {
  342. prepare_layout(_metadata);
  343. create_layout1(_metadata);
  344. }
  345. FIXTURE_TEARDOWN_PARENT(layout1)
  346. {
  347. remove_layout1(_metadata);
  348. cleanup_layout(_metadata);
  349. }
  350. /*
  351. * This helper enables to use the ASSERT_* macros and print the line number
  352. * pointing to the test caller.
  353. */
  354. static int test_open_rel(const int dirfd, const char *const path,
  355. const int flags)
  356. {
  357. int fd;
  358. /* Works with file and directories. */
  359. fd = openat(dirfd, path, flags | O_CLOEXEC);
  360. if (fd < 0)
  361. return errno;
  362. /*
  363. * Mixing error codes from close(2) and open(2) should not lead to any
  364. * (access type) confusion for this test.
  365. */
  366. if (close(fd) != 0)
  367. return errno;
  368. return 0;
  369. }
  370. static int test_open(const char *const path, const int flags)
  371. {
  372. return test_open_rel(AT_FDCWD, path, flags);
  373. }
  374. TEST_F_FORK(layout1, no_restriction)
  375. {
  376. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  377. ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
  378. ASSERT_EQ(0, test_open(file2_s1d1, O_RDONLY));
  379. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
  380. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  381. ASSERT_EQ(0, test_open(file2_s1d2, O_RDONLY));
  382. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
  383. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  384. ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY));
  385. ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY));
  386. ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY));
  387. ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY));
  388. ASSERT_EQ(0, test_open(dir_s2d3, O_RDONLY));
  389. ASSERT_EQ(0, test_open(file1_s2d3, O_RDONLY));
  390. ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY));
  391. ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
  392. ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY));
  393. }
  394. TEST_F_FORK(layout1, inval)
  395. {
  396. struct landlock_path_beneath_attr path_beneath = {
  397. .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE |
  398. LANDLOCK_ACCESS_FS_WRITE_FILE,
  399. .parent_fd = -1,
  400. };
  401. struct landlock_ruleset_attr ruleset_attr = {
  402. .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE |
  403. LANDLOCK_ACCESS_FS_WRITE_FILE,
  404. };
  405. int ruleset_fd;
  406. path_beneath.parent_fd =
  407. open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC);
  408. ASSERT_LE(0, path_beneath.parent_fd);
  409. ruleset_fd = open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC);
  410. ASSERT_LE(0, ruleset_fd);
  411. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  412. &path_beneath, 0));
  413. /* Returns EBADF because ruleset_fd is not a landlock-ruleset FD. */
  414. ASSERT_EQ(EBADF, errno);
  415. ASSERT_EQ(0, close(ruleset_fd));
  416. ruleset_fd = open(dir_s1d1, O_DIRECTORY | O_CLOEXEC);
  417. ASSERT_LE(0, ruleset_fd);
  418. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  419. &path_beneath, 0));
  420. /* Returns EBADFD because ruleset_fd is not a valid ruleset. */
  421. ASSERT_EQ(EBADFD, errno);
  422. ASSERT_EQ(0, close(ruleset_fd));
  423. /* Gets a real ruleset. */
  424. ruleset_fd =
  425. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  426. ASSERT_LE(0, ruleset_fd);
  427. ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  428. &path_beneath, 0));
  429. ASSERT_EQ(0, close(path_beneath.parent_fd));
  430. /* Tests without O_PATH. */
  431. path_beneath.parent_fd = open(dir_s1d2, O_DIRECTORY | O_CLOEXEC);
  432. ASSERT_LE(0, path_beneath.parent_fd);
  433. ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  434. &path_beneath, 0));
  435. ASSERT_EQ(0, close(path_beneath.parent_fd));
  436. /* Tests with a ruleset FD. */
  437. path_beneath.parent_fd = ruleset_fd;
  438. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  439. &path_beneath, 0));
  440. ASSERT_EQ(EBADFD, errno);
  441. /* Checks unhandled allowed_access. */
  442. path_beneath.parent_fd =
  443. open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC);
  444. ASSERT_LE(0, path_beneath.parent_fd);
  445. /* Test with legitimate values. */
  446. path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_EXECUTE;
  447. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  448. &path_beneath, 0));
  449. ASSERT_EQ(EINVAL, errno);
  450. path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_EXECUTE;
  451. /* Tests with denied-by-default access right. */
  452. path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_REFER;
  453. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  454. &path_beneath, 0));
  455. ASSERT_EQ(EINVAL, errno);
  456. path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_REFER;
  457. /* Test with unknown (64-bits) value. */
  458. path_beneath.allowed_access |= (1ULL << 60);
  459. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  460. &path_beneath, 0));
  461. ASSERT_EQ(EINVAL, errno);
  462. path_beneath.allowed_access &= ~(1ULL << 60);
  463. /* Test with no access. */
  464. path_beneath.allowed_access = 0;
  465. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  466. &path_beneath, 0));
  467. ASSERT_EQ(ENOMSG, errno);
  468. path_beneath.allowed_access &= ~(1ULL << 60);
  469. ASSERT_EQ(0, close(path_beneath.parent_fd));
  470. /* Enforces the ruleset. */
  471. ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
  472. ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
  473. ASSERT_EQ(0, close(ruleset_fd));
  474. }
  475. /* clang-format off */
  476. #define ACCESS_FILE ( \
  477. LANDLOCK_ACCESS_FS_EXECUTE | \
  478. LANDLOCK_ACCESS_FS_WRITE_FILE | \
  479. LANDLOCK_ACCESS_FS_READ_FILE | \
  480. LANDLOCK_ACCESS_FS_TRUNCATE | \
  481. LANDLOCK_ACCESS_FS_IOCTL_DEV)
  482. #define ACCESS_LAST LANDLOCK_ACCESS_FS_IOCTL_DEV
  483. #define ACCESS_ALL ( \
  484. ACCESS_FILE | \
  485. LANDLOCK_ACCESS_FS_READ_DIR | \
  486. LANDLOCK_ACCESS_FS_REMOVE_DIR | \
  487. LANDLOCK_ACCESS_FS_REMOVE_FILE | \
  488. LANDLOCK_ACCESS_FS_MAKE_CHAR | \
  489. LANDLOCK_ACCESS_FS_MAKE_DIR | \
  490. LANDLOCK_ACCESS_FS_MAKE_REG | \
  491. LANDLOCK_ACCESS_FS_MAKE_SOCK | \
  492. LANDLOCK_ACCESS_FS_MAKE_FIFO | \
  493. LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
  494. LANDLOCK_ACCESS_FS_MAKE_SYM | \
  495. LANDLOCK_ACCESS_FS_REFER)
  496. /* clang-format on */
  497. TEST_F_FORK(layout1, file_and_dir_access_rights)
  498. {
  499. __u64 access;
  500. int err;
  501. struct landlock_path_beneath_attr path_beneath_file = {},
  502. path_beneath_dir = {};
  503. struct landlock_ruleset_attr ruleset_attr = {
  504. .handled_access_fs = ACCESS_ALL,
  505. };
  506. const int ruleset_fd =
  507. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  508. ASSERT_LE(0, ruleset_fd);
  509. /* Tests access rights for files. */
  510. path_beneath_file.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC);
  511. ASSERT_LE(0, path_beneath_file.parent_fd);
  512. /* Tests access rights for directories. */
  513. path_beneath_dir.parent_fd =
  514. open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC);
  515. ASSERT_LE(0, path_beneath_dir.parent_fd);
  516. for (access = 1; access <= ACCESS_LAST; access <<= 1) {
  517. path_beneath_dir.allowed_access = access;
  518. ASSERT_EQ(0, landlock_add_rule(ruleset_fd,
  519. LANDLOCK_RULE_PATH_BENEATH,
  520. &path_beneath_dir, 0));
  521. path_beneath_file.allowed_access = access;
  522. err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  523. &path_beneath_file, 0);
  524. if (access & ACCESS_FILE) {
  525. ASSERT_EQ(0, err);
  526. } else {
  527. ASSERT_EQ(-1, err);
  528. ASSERT_EQ(EINVAL, errno);
  529. }
  530. }
  531. ASSERT_EQ(0, close(path_beneath_file.parent_fd));
  532. ASSERT_EQ(0, close(path_beneath_dir.parent_fd));
  533. ASSERT_EQ(0, close(ruleset_fd));
  534. }
  535. TEST_F_FORK(layout0, ruleset_with_unknown_access)
  536. {
  537. __u64 access_mask;
  538. for (access_mask = 1ULL << 63; access_mask != ACCESS_LAST;
  539. access_mask >>= 1) {
  540. struct landlock_ruleset_attr ruleset_attr = {
  541. .handled_access_fs = access_mask,
  542. };
  543. ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr,
  544. sizeof(ruleset_attr), 0));
  545. ASSERT_EQ(EINVAL, errno);
  546. }
  547. }
  548. TEST_F_FORK(layout0, rule_with_unknown_access)
  549. {
  550. __u64 access;
  551. struct landlock_path_beneath_attr path_beneath = {};
  552. const struct landlock_ruleset_attr ruleset_attr = {
  553. .handled_access_fs = ACCESS_ALL,
  554. };
  555. const int ruleset_fd =
  556. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  557. ASSERT_LE(0, ruleset_fd);
  558. path_beneath.parent_fd =
  559. open(TMP_DIR, O_PATH | O_DIRECTORY | O_CLOEXEC);
  560. ASSERT_LE(0, path_beneath.parent_fd);
  561. for (access = 1ULL << 63; access != ACCESS_LAST; access >>= 1) {
  562. path_beneath.allowed_access = access;
  563. EXPECT_EQ(-1, landlock_add_rule(ruleset_fd,
  564. LANDLOCK_RULE_PATH_BENEATH,
  565. &path_beneath, 0));
  566. EXPECT_EQ(EINVAL, errno);
  567. }
  568. ASSERT_EQ(0, close(path_beneath.parent_fd));
  569. ASSERT_EQ(0, close(ruleset_fd));
  570. }
  571. TEST_F_FORK(layout1, rule_with_unhandled_access)
  572. {
  573. struct landlock_ruleset_attr ruleset_attr = {
  574. .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE,
  575. };
  576. struct landlock_path_beneath_attr path_beneath = {};
  577. int ruleset_fd;
  578. __u64 access;
  579. ruleset_fd =
  580. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  581. ASSERT_LE(0, ruleset_fd);
  582. path_beneath.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC);
  583. ASSERT_LE(0, path_beneath.parent_fd);
  584. for (access = 1; access > 0; access <<= 1) {
  585. int err;
  586. path_beneath.allowed_access = access;
  587. err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  588. &path_beneath, 0);
  589. if (access == ruleset_attr.handled_access_fs) {
  590. EXPECT_EQ(0, err);
  591. } else {
  592. EXPECT_EQ(-1, err);
  593. EXPECT_EQ(EINVAL, errno);
  594. }
  595. }
  596. EXPECT_EQ(0, close(path_beneath.parent_fd));
  597. EXPECT_EQ(0, close(ruleset_fd));
  598. }
  599. static void add_path_beneath(struct __test_metadata *const _metadata,
  600. const int ruleset_fd, const __u64 allowed_access,
  601. const char *const path)
  602. {
  603. struct landlock_path_beneath_attr path_beneath = {
  604. .allowed_access = allowed_access,
  605. };
  606. path_beneath.parent_fd = open(path, O_PATH | O_CLOEXEC);
  607. ASSERT_LE(0, path_beneath.parent_fd)
  608. {
  609. TH_LOG("Failed to open directory \"%s\": %s", path,
  610. strerror(errno));
  611. }
  612. ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  613. &path_beneath, 0))
  614. {
  615. TH_LOG("Failed to update the ruleset with \"%s\": %s", path,
  616. strerror(errno));
  617. }
  618. ASSERT_EQ(0, close(path_beneath.parent_fd));
  619. }
  620. struct rule {
  621. const char *path;
  622. __u64 access;
  623. };
  624. /* clang-format off */
  625. #define ACCESS_RO ( \
  626. LANDLOCK_ACCESS_FS_READ_FILE | \
  627. LANDLOCK_ACCESS_FS_READ_DIR)
  628. #define ACCESS_RW ( \
  629. ACCESS_RO | \
  630. LANDLOCK_ACCESS_FS_WRITE_FILE)
  631. /* clang-format on */
  632. static int create_ruleset(struct __test_metadata *const _metadata,
  633. const __u64 handled_access_fs,
  634. const struct rule rules[])
  635. {
  636. int ruleset_fd, i;
  637. struct landlock_ruleset_attr ruleset_attr = {
  638. .handled_access_fs = handled_access_fs,
  639. };
  640. ASSERT_NE(NULL, rules)
  641. {
  642. TH_LOG("No rule list");
  643. }
  644. ASSERT_NE(NULL, rules[0].path)
  645. {
  646. TH_LOG("Empty rule list");
  647. }
  648. ruleset_fd =
  649. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  650. ASSERT_LE(0, ruleset_fd)
  651. {
  652. TH_LOG("Failed to create a ruleset: %s", strerror(errno));
  653. }
  654. for (i = 0; rules[i].path; i++) {
  655. if (!rules[i].access)
  656. continue;
  657. add_path_beneath(_metadata, ruleset_fd, rules[i].access,
  658. rules[i].path);
  659. }
  660. return ruleset_fd;
  661. }
  662. TEST_F_FORK(layout0, proc_nsfs)
  663. {
  664. const struct rule rules[] = {
  665. {
  666. .path = "/dev/null",
  667. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  668. LANDLOCK_ACCESS_FS_WRITE_FILE,
  669. },
  670. {},
  671. };
  672. struct landlock_path_beneath_attr path_beneath;
  673. const int ruleset_fd = create_ruleset(
  674. _metadata, rules[0].access | LANDLOCK_ACCESS_FS_READ_DIR,
  675. rules);
  676. ASSERT_LE(0, ruleset_fd);
  677. ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY));
  678. enforce_ruleset(_metadata, ruleset_fd);
  679. ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
  680. ASSERT_EQ(EACCES, test_open("/dev", O_RDONLY));
  681. ASSERT_EQ(0, test_open("/dev/null", O_RDONLY));
  682. ASSERT_EQ(EACCES, test_open("/dev/full", O_RDONLY));
  683. ASSERT_EQ(EACCES, test_open("/proc", O_RDONLY));
  684. ASSERT_EQ(EACCES, test_open("/proc/self", O_RDONLY));
  685. ASSERT_EQ(EACCES, test_open("/proc/self/ns", O_RDONLY));
  686. /*
  687. * Because nsfs is an internal filesystem, /proc/self/ns/mnt is a
  688. * disconnected path. Such path cannot be identified and must then be
  689. * allowed.
  690. */
  691. ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY));
  692. /*
  693. * Checks that it is not possible to add nsfs-like filesystem
  694. * references to a ruleset.
  695. */
  696. path_beneath.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE |
  697. LANDLOCK_ACCESS_FS_WRITE_FILE,
  698. path_beneath.parent_fd = open("/proc/self/ns/mnt", O_PATH | O_CLOEXEC);
  699. ASSERT_LE(0, path_beneath.parent_fd);
  700. ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
  701. &path_beneath, 0));
  702. ASSERT_EQ(EBADFD, errno);
  703. ASSERT_EQ(0, close(path_beneath.parent_fd));
  704. }
  705. TEST_F_FORK(layout0, unpriv)
  706. {
  707. const struct rule rules[] = {
  708. {
  709. .path = TMP_DIR,
  710. .access = ACCESS_RO,
  711. },
  712. {},
  713. };
  714. int ruleset_fd;
  715. drop_caps(_metadata);
  716. ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules);
  717. ASSERT_LE(0, ruleset_fd);
  718. ASSERT_EQ(-1, landlock_restrict_self(ruleset_fd, 0));
  719. ASSERT_EQ(EPERM, errno);
  720. /* enforce_ruleset() calls prctl(no_new_privs). */
  721. enforce_ruleset(_metadata, ruleset_fd);
  722. ASSERT_EQ(0, close(ruleset_fd));
  723. }
  724. TEST_F_FORK(layout1, effective_access)
  725. {
  726. const struct rule rules[] = {
  727. {
  728. .path = dir_s1d2,
  729. .access = ACCESS_RO,
  730. },
  731. {
  732. .path = file1_s2d2,
  733. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  734. LANDLOCK_ACCESS_FS_WRITE_FILE,
  735. },
  736. {},
  737. };
  738. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  739. char buf;
  740. int reg_fd;
  741. ASSERT_LE(0, ruleset_fd);
  742. enforce_ruleset(_metadata, ruleset_fd);
  743. ASSERT_EQ(0, close(ruleset_fd));
  744. /* Tests on a directory (with or without O_PATH). */
  745. ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
  746. ASSERT_EQ(0, test_open("/", O_RDONLY | O_PATH));
  747. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
  748. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_PATH));
  749. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  750. ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY | O_PATH));
  751. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
  752. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  753. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
  754. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  755. /* Tests on a file (with or without O_PATH). */
  756. ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY));
  757. ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_PATH));
  758. ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY));
  759. /* Checks effective read and write actions. */
  760. reg_fd = open(file1_s2d2, O_RDWR | O_CLOEXEC);
  761. ASSERT_LE(0, reg_fd);
  762. ASSERT_EQ(1, write(reg_fd, ".", 1));
  763. ASSERT_LE(0, lseek(reg_fd, 0, SEEK_SET));
  764. ASSERT_EQ(1, read(reg_fd, &buf, 1));
  765. ASSERT_EQ('.', buf);
  766. ASSERT_EQ(0, close(reg_fd));
  767. /* Just in case, double-checks effective actions. */
  768. reg_fd = open(file1_s2d2, O_RDONLY | O_CLOEXEC);
  769. ASSERT_LE(0, reg_fd);
  770. ASSERT_EQ(-1, write(reg_fd, &buf, 1));
  771. ASSERT_EQ(EBADF, errno);
  772. ASSERT_EQ(0, close(reg_fd));
  773. }
  774. TEST_F_FORK(layout1, unhandled_access)
  775. {
  776. const struct rule rules[] = {
  777. {
  778. .path = dir_s1d2,
  779. .access = ACCESS_RO,
  780. },
  781. {},
  782. };
  783. /* Here, we only handle read accesses, not write accesses. */
  784. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules);
  785. ASSERT_LE(0, ruleset_fd);
  786. enforce_ruleset(_metadata, ruleset_fd);
  787. ASSERT_EQ(0, close(ruleset_fd));
  788. /*
  789. * Because the policy does not handle LANDLOCK_ACCESS_FS_WRITE_FILE,
  790. * opening for write-only should be allowed, but not read-write.
  791. */
  792. ASSERT_EQ(0, test_open(file1_s1d1, O_WRONLY));
  793. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
  794. ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY));
  795. ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR));
  796. }
  797. TEST_F_FORK(layout1, ruleset_overlap)
  798. {
  799. const struct rule rules[] = {
  800. /* These rules should be ORed among them. */
  801. {
  802. .path = dir_s1d2,
  803. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  804. LANDLOCK_ACCESS_FS_WRITE_FILE,
  805. },
  806. {
  807. .path = dir_s1d2,
  808. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  809. LANDLOCK_ACCESS_FS_READ_DIR,
  810. },
  811. {},
  812. };
  813. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  814. ASSERT_LE(0, ruleset_fd);
  815. enforce_ruleset(_metadata, ruleset_fd);
  816. ASSERT_EQ(0, close(ruleset_fd));
  817. /* Checks s1d1 hierarchy. */
  818. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  819. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  820. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
  821. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  822. /* Checks s1d2 hierarchy. */
  823. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  824. ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY));
  825. ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR));
  826. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  827. /* Checks s1d3 hierarchy. */
  828. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  829. ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
  830. ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
  831. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  832. }
  833. TEST_F_FORK(layout1, layer_rule_unions)
  834. {
  835. const struct rule layer1[] = {
  836. {
  837. .path = dir_s1d2,
  838. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  839. },
  840. /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */
  841. {
  842. .path = dir_s1d3,
  843. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  844. },
  845. {},
  846. };
  847. const struct rule layer2[] = {
  848. /* Doesn't change anything from layer1. */
  849. {
  850. .path = dir_s1d2,
  851. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  852. LANDLOCK_ACCESS_FS_WRITE_FILE,
  853. },
  854. {},
  855. };
  856. const struct rule layer3[] = {
  857. /* Only allows write (but not read) to dir_s1d3. */
  858. {
  859. .path = dir_s1d2,
  860. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  861. },
  862. {},
  863. };
  864. int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1);
  865. ASSERT_LE(0, ruleset_fd);
  866. enforce_ruleset(_metadata, ruleset_fd);
  867. ASSERT_EQ(0, close(ruleset_fd));
  868. /* Checks s1d1 hierarchy with layer1. */
  869. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  870. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  871. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
  872. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  873. /* Checks s1d2 hierarchy with layer1. */
  874. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  875. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  876. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
  877. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  878. /* Checks s1d3 hierarchy with layer1. */
  879. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  880. ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
  881. /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */
  882. ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
  883. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  884. /* Doesn't change anything from layer1. */
  885. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2);
  886. ASSERT_LE(0, ruleset_fd);
  887. enforce_ruleset(_metadata, ruleset_fd);
  888. ASSERT_EQ(0, close(ruleset_fd));
  889. /* Checks s1d1 hierarchy with layer2. */
  890. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  891. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  892. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
  893. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  894. /* Checks s1d2 hierarchy with layer2. */
  895. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  896. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  897. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
  898. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  899. /* Checks s1d3 hierarchy with layer2. */
  900. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  901. ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
  902. /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */
  903. ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
  904. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  905. /* Only allows write (but not read) to dir_s1d3. */
  906. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3);
  907. ASSERT_LE(0, ruleset_fd);
  908. enforce_ruleset(_metadata, ruleset_fd);
  909. ASSERT_EQ(0, close(ruleset_fd));
  910. /* Checks s1d1 hierarchy with layer3. */
  911. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  912. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  913. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
  914. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  915. /* Checks s1d2 hierarchy with layer3. */
  916. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY));
  917. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  918. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
  919. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  920. /* Checks s1d3 hierarchy with layer3. */
  921. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY));
  922. ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
  923. /* dir_s1d3 should now deny READ_FILE and WRITE_FILE (O_RDWR). */
  924. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDWR));
  925. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  926. }
  927. TEST_F_FORK(layout1, non_overlapping_accesses)
  928. {
  929. const struct rule layer1[] = {
  930. {
  931. .path = dir_s1d2,
  932. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  933. },
  934. {},
  935. };
  936. const struct rule layer2[] = {
  937. {
  938. .path = dir_s1d3,
  939. .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
  940. },
  941. {},
  942. };
  943. int ruleset_fd;
  944. ASSERT_EQ(0, unlink(file1_s1d1));
  945. ASSERT_EQ(0, unlink(file1_s1d2));
  946. ruleset_fd =
  947. create_ruleset(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, layer1);
  948. ASSERT_LE(0, ruleset_fd);
  949. enforce_ruleset(_metadata, ruleset_fd);
  950. ASSERT_EQ(0, close(ruleset_fd));
  951. ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0));
  952. ASSERT_EQ(EACCES, errno);
  953. ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0));
  954. ASSERT_EQ(0, unlink(file1_s1d2));
  955. ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_REMOVE_FILE,
  956. layer2);
  957. ASSERT_LE(0, ruleset_fd);
  958. enforce_ruleset(_metadata, ruleset_fd);
  959. ASSERT_EQ(0, close(ruleset_fd));
  960. /* Unchanged accesses for file creation. */
  961. ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0));
  962. ASSERT_EQ(EACCES, errno);
  963. ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0));
  964. /* Checks file removing. */
  965. ASSERT_EQ(-1, unlink(file1_s1d2));
  966. ASSERT_EQ(EACCES, errno);
  967. ASSERT_EQ(0, unlink(file1_s1d3));
  968. }
  969. TEST_F_FORK(layout1, interleaved_masked_accesses)
  970. {
  971. /*
  972. * Checks overly restrictive rules:
  973. * layer 1: allows R s1d1/s1d2/s1d3/file1
  974. * layer 2: allows RW s1d1/s1d2/s1d3
  975. * allows W s1d1/s1d2
  976. * denies R s1d1/s1d2
  977. * layer 3: allows R s1d1
  978. * layer 4: allows R s1d1/s1d2
  979. * denies W s1d1/s1d2
  980. * layer 5: allows R s1d1/s1d2
  981. * layer 6: allows X ----
  982. * layer 7: allows W s1d1/s1d2
  983. * denies R s1d1/s1d2
  984. */
  985. const struct rule layer1_read[] = {
  986. /* Allows read access to file1_s1d3 with the first layer. */
  987. {
  988. .path = file1_s1d3,
  989. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  990. },
  991. {},
  992. };
  993. /* First rule with write restrictions. */
  994. const struct rule layer2_read_write[] = {
  995. /* Start by granting read-write access via its parent directory... */
  996. {
  997. .path = dir_s1d3,
  998. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  999. LANDLOCK_ACCESS_FS_WRITE_FILE,
  1000. },
  1001. /* ...but also denies read access via its grandparent directory. */
  1002. {
  1003. .path = dir_s1d2,
  1004. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  1005. },
  1006. {},
  1007. };
  1008. const struct rule layer3_read[] = {
  1009. /* Allows read access via its great-grandparent directory. */
  1010. {
  1011. .path = dir_s1d1,
  1012. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  1013. },
  1014. {},
  1015. };
  1016. const struct rule layer4_read_write[] = {
  1017. /*
  1018. * Try to confuse the deny access by denying write (but not
  1019. * read) access via its grandparent directory.
  1020. */
  1021. {
  1022. .path = dir_s1d2,
  1023. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  1024. },
  1025. {},
  1026. };
  1027. const struct rule layer5_read[] = {
  1028. /*
  1029. * Try to override layer2's deny read access by explicitly
  1030. * allowing read access via file1_s1d3's grandparent.
  1031. */
  1032. {
  1033. .path = dir_s1d2,
  1034. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  1035. },
  1036. {},
  1037. };
  1038. const struct rule layer6_execute[] = {
  1039. /*
  1040. * Restricts an unrelated file hierarchy with a new access
  1041. * (non-overlapping) type.
  1042. */
  1043. {
  1044. .path = dir_s2d1,
  1045. .access = LANDLOCK_ACCESS_FS_EXECUTE,
  1046. },
  1047. {},
  1048. };
  1049. const struct rule layer7_read_write[] = {
  1050. /*
  1051. * Finally, denies read access to file1_s1d3 via its
  1052. * grandparent.
  1053. */
  1054. {
  1055. .path = dir_s1d2,
  1056. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  1057. },
  1058. {},
  1059. };
  1060. int ruleset_fd;
  1061. ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
  1062. layer1_read);
  1063. ASSERT_LE(0, ruleset_fd);
  1064. enforce_ruleset(_metadata, ruleset_fd);
  1065. ASSERT_EQ(0, close(ruleset_fd));
  1066. /* Checks that read access is granted for file1_s1d3 with layer 1. */
  1067. ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
  1068. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1069. ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY));
  1070. ruleset_fd = create_ruleset(_metadata,
  1071. LANDLOCK_ACCESS_FS_READ_FILE |
  1072. LANDLOCK_ACCESS_FS_WRITE_FILE,
  1073. layer2_read_write);
  1074. ASSERT_LE(0, ruleset_fd);
  1075. enforce_ruleset(_metadata, ruleset_fd);
  1076. ASSERT_EQ(0, close(ruleset_fd));
  1077. /* Checks that previous access rights are unchanged with layer 2. */
  1078. ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
  1079. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1080. ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY));
  1081. ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
  1082. layer3_read);
  1083. ASSERT_LE(0, ruleset_fd);
  1084. enforce_ruleset(_metadata, ruleset_fd);
  1085. ASSERT_EQ(0, close(ruleset_fd));
  1086. /* Checks that previous access rights are unchanged with layer 3. */
  1087. ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
  1088. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1089. ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY));
  1090. /* This time, denies write access for the file hierarchy. */
  1091. ruleset_fd = create_ruleset(_metadata,
  1092. LANDLOCK_ACCESS_FS_READ_FILE |
  1093. LANDLOCK_ACCESS_FS_WRITE_FILE,
  1094. layer4_read_write);
  1095. ASSERT_LE(0, ruleset_fd);
  1096. enforce_ruleset(_metadata, ruleset_fd);
  1097. ASSERT_EQ(0, close(ruleset_fd));
  1098. /*
  1099. * Checks that the only change with layer 4 is that write access is
  1100. * denied.
  1101. */
  1102. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  1103. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1104. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1105. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
  1106. ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
  1107. layer5_read);
  1108. ASSERT_LE(0, ruleset_fd);
  1109. enforce_ruleset(_metadata, ruleset_fd);
  1110. ASSERT_EQ(0, close(ruleset_fd));
  1111. /* Checks that previous access rights are unchanged with layer 5. */
  1112. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  1113. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1114. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
  1115. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1116. ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_EXECUTE,
  1117. layer6_execute);
  1118. ASSERT_LE(0, ruleset_fd);
  1119. enforce_ruleset(_metadata, ruleset_fd);
  1120. ASSERT_EQ(0, close(ruleset_fd));
  1121. /* Checks that previous access rights are unchanged with layer 6. */
  1122. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  1123. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1124. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
  1125. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1126. ruleset_fd = create_ruleset(_metadata,
  1127. LANDLOCK_ACCESS_FS_READ_FILE |
  1128. LANDLOCK_ACCESS_FS_WRITE_FILE,
  1129. layer7_read_write);
  1130. ASSERT_LE(0, ruleset_fd);
  1131. enforce_ruleset(_metadata, ruleset_fd);
  1132. ASSERT_EQ(0, close(ruleset_fd));
  1133. /* Checks read access is now denied with layer 7. */
  1134. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY));
  1135. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1136. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
  1137. ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
  1138. }
  1139. TEST_F_FORK(layout1, inherit_subset)
  1140. {
  1141. const struct rule rules[] = {
  1142. {
  1143. .path = dir_s1d2,
  1144. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  1145. LANDLOCK_ACCESS_FS_READ_DIR,
  1146. },
  1147. {},
  1148. };
  1149. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1150. ASSERT_LE(0, ruleset_fd);
  1151. enforce_ruleset(_metadata, ruleset_fd);
  1152. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  1153. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  1154. /* Write access is forbidden. */
  1155. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  1156. /* Readdir access is allowed. */
  1157. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  1158. /* Write access is forbidden. */
  1159. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1160. /* Readdir access is allowed. */
  1161. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  1162. /*
  1163. * Tests shared rule extension: the following rules should not grant
  1164. * any new access, only remove some. Once enforced, these rules are
  1165. * ANDed with the previous ones.
  1166. */
  1167. add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE,
  1168. dir_s1d2);
  1169. /*
  1170. * According to ruleset_fd, dir_s1d2 should now have the
  1171. * LANDLOCK_ACCESS_FS_READ_FILE and LANDLOCK_ACCESS_FS_WRITE_FILE
  1172. * access rights (even if this directory is opened a second time).
  1173. * However, when enforcing this updated ruleset, the ruleset tied to
  1174. * the current process (i.e. its domain) will still only have the
  1175. * dir_s1d2 with LANDLOCK_ACCESS_FS_READ_FILE and
  1176. * LANDLOCK_ACCESS_FS_READ_DIR accesses, but
  1177. * LANDLOCK_ACCESS_FS_WRITE_FILE must not be allowed because it would
  1178. * be a privilege escalation.
  1179. */
  1180. enforce_ruleset(_metadata, ruleset_fd);
  1181. /* Same tests and results as above. */
  1182. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  1183. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  1184. /* It is still forbidden to write in file1_s1d2. */
  1185. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  1186. /* Readdir access is still allowed. */
  1187. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  1188. /* It is still forbidden to write in file1_s1d3. */
  1189. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1190. /* Readdir access is still allowed. */
  1191. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  1192. /*
  1193. * Try to get more privileges by adding new access rights to the parent
  1194. * directory: dir_s1d1.
  1195. */
  1196. add_path_beneath(_metadata, ruleset_fd, ACCESS_RW, dir_s1d1);
  1197. enforce_ruleset(_metadata, ruleset_fd);
  1198. /* Same tests and results as above. */
  1199. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  1200. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  1201. /* It is still forbidden to write in file1_s1d2. */
  1202. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  1203. /* Readdir access is still allowed. */
  1204. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  1205. /* It is still forbidden to write in file1_s1d3. */
  1206. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1207. /* Readdir access is still allowed. */
  1208. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  1209. /*
  1210. * Now, dir_s1d3 get a new rule tied to it, only allowing
  1211. * LANDLOCK_ACCESS_FS_WRITE_FILE. The (kernel internal) difference is
  1212. * that there was no rule tied to it before.
  1213. */
  1214. add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE,
  1215. dir_s1d3);
  1216. enforce_ruleset(_metadata, ruleset_fd);
  1217. ASSERT_EQ(0, close(ruleset_fd));
  1218. /*
  1219. * Same tests and results as above, except for open(dir_s1d3) which is
  1220. * now denied because the new rule mask the rule previously inherited
  1221. * from dir_s1d2.
  1222. */
  1223. /* Same tests and results as above. */
  1224. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  1225. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  1226. /* It is still forbidden to write in file1_s1d2. */
  1227. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  1228. /* Readdir access is still allowed. */
  1229. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  1230. /* It is still forbidden to write in file1_s1d3. */
  1231. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  1232. /*
  1233. * Readdir of dir_s1d3 is still allowed because of the OR policy inside
  1234. * the same layer.
  1235. */
  1236. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  1237. }
  1238. TEST_F_FORK(layout1, inherit_superset)
  1239. {
  1240. const struct rule rules[] = {
  1241. {
  1242. .path = dir_s1d3,
  1243. .access = ACCESS_RO,
  1244. },
  1245. {},
  1246. };
  1247. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1248. ASSERT_LE(0, ruleset_fd);
  1249. enforce_ruleset(_metadata, ruleset_fd);
  1250. /* Readdir access is denied for dir_s1d2. */
  1251. ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  1252. /* Readdir access is allowed for dir_s1d3. */
  1253. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  1254. /* File access is allowed for file1_s1d3. */
  1255. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  1256. /* Now dir_s1d2, parent of dir_s1d3, gets a new rule tied to it. */
  1257. add_path_beneath(_metadata, ruleset_fd,
  1258. LANDLOCK_ACCESS_FS_READ_FILE |
  1259. LANDLOCK_ACCESS_FS_READ_DIR,
  1260. dir_s1d2);
  1261. enforce_ruleset(_metadata, ruleset_fd);
  1262. ASSERT_EQ(0, close(ruleset_fd));
  1263. /* Readdir access is still denied for dir_s1d2. */
  1264. ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  1265. /* Readdir access is still allowed for dir_s1d3. */
  1266. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  1267. /* File access is still allowed for file1_s1d3. */
  1268. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  1269. }
  1270. TEST_F_FORK(layout0, max_layers)
  1271. {
  1272. int i, err;
  1273. const struct rule rules[] = {
  1274. {
  1275. .path = TMP_DIR,
  1276. .access = ACCESS_RO,
  1277. },
  1278. {},
  1279. };
  1280. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1281. ASSERT_LE(0, ruleset_fd);
  1282. for (i = 0; i < 16; i++)
  1283. enforce_ruleset(_metadata, ruleset_fd);
  1284. for (i = 0; i < 2; i++) {
  1285. err = landlock_restrict_self(ruleset_fd, 0);
  1286. ASSERT_EQ(-1, err);
  1287. ASSERT_EQ(E2BIG, errno);
  1288. }
  1289. ASSERT_EQ(0, close(ruleset_fd));
  1290. }
  1291. TEST_F_FORK(layout1, empty_or_same_ruleset)
  1292. {
  1293. struct landlock_ruleset_attr ruleset_attr = {};
  1294. int ruleset_fd;
  1295. /* Tests empty handled_access_fs. */
  1296. ruleset_fd =
  1297. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  1298. ASSERT_LE(-1, ruleset_fd);
  1299. ASSERT_EQ(ENOMSG, errno);
  1300. /* Enforces policy which deny read access to all files. */
  1301. ruleset_attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE;
  1302. ruleset_fd =
  1303. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  1304. ASSERT_LE(0, ruleset_fd);
  1305. enforce_ruleset(_metadata, ruleset_fd);
  1306. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  1307. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  1308. /* Nests a policy which deny read access to all directories. */
  1309. ruleset_attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR;
  1310. ruleset_fd =
  1311. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  1312. ASSERT_LE(0, ruleset_fd);
  1313. enforce_ruleset(_metadata, ruleset_fd);
  1314. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  1315. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
  1316. /* Enforces a second time with the same ruleset. */
  1317. enforce_ruleset(_metadata, ruleset_fd);
  1318. ASSERT_EQ(0, close(ruleset_fd));
  1319. }
  1320. TEST_F_FORK(layout1, rule_on_mountpoint)
  1321. {
  1322. const struct rule rules[] = {
  1323. {
  1324. .path = dir_s1d1,
  1325. .access = ACCESS_RO,
  1326. },
  1327. {
  1328. /* dir_s3d2 is a mount point. */
  1329. .path = dir_s3d2,
  1330. .access = ACCESS_RO,
  1331. },
  1332. {},
  1333. };
  1334. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1335. ASSERT_LE(0, ruleset_fd);
  1336. enforce_ruleset(_metadata, ruleset_fd);
  1337. ASSERT_EQ(0, close(ruleset_fd));
  1338. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  1339. ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY));
  1340. ASSERT_EQ(EACCES, test_open(dir_s3d1, O_RDONLY));
  1341. ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
  1342. ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY));
  1343. }
  1344. TEST_F_FORK(layout1, rule_over_mountpoint)
  1345. {
  1346. const struct rule rules[] = {
  1347. {
  1348. .path = dir_s1d1,
  1349. .access = ACCESS_RO,
  1350. },
  1351. {
  1352. /* dir_s3d2 is a mount point. */
  1353. .path = dir_s3d1,
  1354. .access = ACCESS_RO,
  1355. },
  1356. {},
  1357. };
  1358. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1359. ASSERT_LE(0, ruleset_fd);
  1360. enforce_ruleset(_metadata, ruleset_fd);
  1361. ASSERT_EQ(0, close(ruleset_fd));
  1362. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  1363. ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY));
  1364. ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY));
  1365. ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
  1366. ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY));
  1367. }
  1368. /*
  1369. * This test verifies that we can apply a landlock rule on the root directory
  1370. * (which might require special handling).
  1371. */
  1372. TEST_F_FORK(layout1, rule_over_root_allow_then_deny)
  1373. {
  1374. struct rule rules[] = {
  1375. {
  1376. .path = "/",
  1377. .access = ACCESS_RO,
  1378. },
  1379. {},
  1380. };
  1381. int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1382. ASSERT_LE(0, ruleset_fd);
  1383. enforce_ruleset(_metadata, ruleset_fd);
  1384. ASSERT_EQ(0, close(ruleset_fd));
  1385. /* Checks allowed access. */
  1386. ASSERT_EQ(0, test_open("/", O_RDONLY));
  1387. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  1388. rules[0].access = LANDLOCK_ACCESS_FS_READ_FILE;
  1389. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1390. ASSERT_LE(0, ruleset_fd);
  1391. enforce_ruleset(_metadata, ruleset_fd);
  1392. ASSERT_EQ(0, close(ruleset_fd));
  1393. /* Checks denied access (on a directory). */
  1394. ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
  1395. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
  1396. }
  1397. TEST_F_FORK(layout1, rule_over_root_deny)
  1398. {
  1399. const struct rule rules[] = {
  1400. {
  1401. .path = "/",
  1402. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  1403. },
  1404. {},
  1405. };
  1406. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1407. ASSERT_LE(0, ruleset_fd);
  1408. enforce_ruleset(_metadata, ruleset_fd);
  1409. ASSERT_EQ(0, close(ruleset_fd));
  1410. /* Checks denied access (on a directory). */
  1411. ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
  1412. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
  1413. }
  1414. TEST_F_FORK(layout1, rule_inside_mount_ns)
  1415. {
  1416. const struct rule rules[] = {
  1417. {
  1418. .path = "s3d3",
  1419. .access = ACCESS_RO,
  1420. },
  1421. {},
  1422. };
  1423. int ruleset_fd;
  1424. set_cap(_metadata, CAP_SYS_ADMIN);
  1425. ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3))
  1426. {
  1427. TH_LOG("Failed to pivot root: %s", strerror(errno));
  1428. };
  1429. ASSERT_EQ(0, chdir("/"));
  1430. clear_cap(_metadata, CAP_SYS_ADMIN);
  1431. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1432. ASSERT_LE(0, ruleset_fd);
  1433. enforce_ruleset(_metadata, ruleset_fd);
  1434. ASSERT_EQ(0, close(ruleset_fd));
  1435. ASSERT_EQ(0, test_open("s3d3", O_RDONLY));
  1436. ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
  1437. }
  1438. TEST_F_FORK(layout1, mount_and_pivot)
  1439. {
  1440. const struct rule rules[] = {
  1441. {
  1442. .path = dir_s3d2,
  1443. .access = ACCESS_RO,
  1444. },
  1445. {},
  1446. };
  1447. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1448. ASSERT_LE(0, ruleset_fd);
  1449. enforce_ruleset(_metadata, ruleset_fd);
  1450. ASSERT_EQ(0, close(ruleset_fd));
  1451. set_cap(_metadata, CAP_SYS_ADMIN);
  1452. ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_RDONLY, NULL));
  1453. ASSERT_EQ(EPERM, errno);
  1454. ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3));
  1455. ASSERT_EQ(EPERM, errno);
  1456. clear_cap(_metadata, CAP_SYS_ADMIN);
  1457. }
  1458. TEST_F_FORK(layout1, move_mount)
  1459. {
  1460. const struct rule rules[] = {
  1461. {
  1462. .path = dir_s3d2,
  1463. .access = ACCESS_RO,
  1464. },
  1465. {},
  1466. };
  1467. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1468. ASSERT_LE(0, ruleset_fd);
  1469. set_cap(_metadata, CAP_SYS_ADMIN);
  1470. ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD,
  1471. dir_s1d2, 0))
  1472. {
  1473. TH_LOG("Failed to move mount: %s", strerror(errno));
  1474. }
  1475. ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD,
  1476. dir_s3d2, 0));
  1477. clear_cap(_metadata, CAP_SYS_ADMIN);
  1478. enforce_ruleset(_metadata, ruleset_fd);
  1479. ASSERT_EQ(0, close(ruleset_fd));
  1480. set_cap(_metadata, CAP_SYS_ADMIN);
  1481. ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD,
  1482. dir_s1d2, 0));
  1483. ASSERT_EQ(EPERM, errno);
  1484. clear_cap(_metadata, CAP_SYS_ADMIN);
  1485. }
  1486. TEST_F_FORK(layout1, topology_changes_with_net_only)
  1487. {
  1488. const struct landlock_ruleset_attr ruleset_net = {
  1489. .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
  1490. LANDLOCK_ACCESS_NET_CONNECT_TCP,
  1491. };
  1492. int ruleset_fd;
  1493. /* Add network restrictions. */
  1494. ruleset_fd =
  1495. landlock_create_ruleset(&ruleset_net, sizeof(ruleset_net), 0);
  1496. ASSERT_LE(0, ruleset_fd);
  1497. enforce_ruleset(_metadata, ruleset_fd);
  1498. ASSERT_EQ(0, close(ruleset_fd));
  1499. /* Mount, remount, move_mount, umount, and pivot_root checks. */
  1500. set_cap(_metadata, CAP_SYS_ADMIN);
  1501. ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s1d2));
  1502. ASSERT_EQ(0, mount(NULL, dir_s1d2, NULL, MS_PRIVATE | MS_REC, NULL));
  1503. ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD,
  1504. dir_s2d2, 0));
  1505. ASSERT_EQ(0, umount(dir_s2d2));
  1506. ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3));
  1507. ASSERT_EQ(0, chdir("/"));
  1508. clear_cap(_metadata, CAP_SYS_ADMIN);
  1509. }
  1510. TEST_F_FORK(layout1, topology_changes_with_net_and_fs)
  1511. {
  1512. const struct landlock_ruleset_attr ruleset_net_fs = {
  1513. .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
  1514. LANDLOCK_ACCESS_NET_CONNECT_TCP,
  1515. .handled_access_fs = LANDLOCK_ACCESS_FS_EXECUTE,
  1516. };
  1517. int ruleset_fd;
  1518. /* Add network and filesystem restrictions. */
  1519. ruleset_fd = landlock_create_ruleset(&ruleset_net_fs,
  1520. sizeof(ruleset_net_fs), 0);
  1521. ASSERT_LE(0, ruleset_fd);
  1522. enforce_ruleset(_metadata, ruleset_fd);
  1523. ASSERT_EQ(0, close(ruleset_fd));
  1524. /* Mount, remount, move_mount, umount, and pivot_root checks. */
  1525. set_cap(_metadata, CAP_SYS_ADMIN);
  1526. ASSERT_EQ(-1, mount_opt(&mnt_tmp, dir_s1d2));
  1527. ASSERT_EQ(EPERM, errno);
  1528. ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_PRIVATE | MS_REC, NULL));
  1529. ASSERT_EQ(EPERM, errno);
  1530. ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD,
  1531. dir_s2d2, 0));
  1532. ASSERT_EQ(EPERM, errno);
  1533. ASSERT_EQ(-1, umount(dir_s3d2));
  1534. ASSERT_EQ(EPERM, errno);
  1535. ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3));
  1536. ASSERT_EQ(EPERM, errno);
  1537. clear_cap(_metadata, CAP_SYS_ADMIN);
  1538. }
  1539. TEST_F_FORK(layout1, release_inodes)
  1540. {
  1541. const struct rule rules[] = {
  1542. {
  1543. .path = dir_s1d1,
  1544. .access = ACCESS_RO,
  1545. },
  1546. {
  1547. .path = dir_s3d2,
  1548. .access = ACCESS_RO,
  1549. },
  1550. {
  1551. .path = dir_s3d3,
  1552. .access = ACCESS_RO,
  1553. },
  1554. {},
  1555. };
  1556. const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
  1557. ASSERT_LE(0, ruleset_fd);
  1558. /* Unmount a file hierarchy while it is being used by a ruleset. */
  1559. set_cap(_metadata, CAP_SYS_ADMIN);
  1560. ASSERT_EQ(0, umount(dir_s3d2));
  1561. clear_cap(_metadata, CAP_SYS_ADMIN);
  1562. enforce_ruleset(_metadata, ruleset_fd);
  1563. ASSERT_EQ(0, close(ruleset_fd));
  1564. ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
  1565. ASSERT_EQ(EACCES, test_open(dir_s3d2, O_RDONLY));
  1566. /* This dir_s3d3 would not be allowed and does not exist anyway. */
  1567. ASSERT_EQ(ENOENT, test_open(dir_s3d3, O_RDONLY));
  1568. }
  1569. enum relative_access {
  1570. REL_OPEN,
  1571. REL_CHDIR,
  1572. REL_CHROOT_ONLY,
  1573. REL_CHROOT_CHDIR,
  1574. };
  1575. static void test_relative_path(struct __test_metadata *const _metadata,
  1576. const enum relative_access rel)
  1577. {
  1578. /*
  1579. * Common layer to check that chroot doesn't ignore it (i.e. a chroot
  1580. * is not a disconnected root directory).
  1581. */
  1582. const struct rule layer1_base[] = {
  1583. {
  1584. .path = TMP_DIR,
  1585. .access = ACCESS_RO,
  1586. },
  1587. {},
  1588. };
  1589. const struct rule layer2_subs[] = {
  1590. {
  1591. .path = dir_s1d2,
  1592. .access = ACCESS_RO,
  1593. },
  1594. {
  1595. .path = dir_s2d2,
  1596. .access = ACCESS_RO,
  1597. },
  1598. {},
  1599. };
  1600. int dirfd, ruleset_fd;
  1601. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
  1602. ASSERT_LE(0, ruleset_fd);
  1603. enforce_ruleset(_metadata, ruleset_fd);
  1604. ASSERT_EQ(0, close(ruleset_fd));
  1605. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_subs);
  1606. ASSERT_LE(0, ruleset_fd);
  1607. switch (rel) {
  1608. case REL_OPEN:
  1609. case REL_CHDIR:
  1610. break;
  1611. case REL_CHROOT_ONLY:
  1612. ASSERT_EQ(0, chdir(dir_s2d2));
  1613. break;
  1614. case REL_CHROOT_CHDIR:
  1615. ASSERT_EQ(0, chdir(dir_s1d2));
  1616. break;
  1617. default:
  1618. ASSERT_TRUE(false);
  1619. return;
  1620. }
  1621. set_cap(_metadata, CAP_SYS_CHROOT);
  1622. enforce_ruleset(_metadata, ruleset_fd);
  1623. switch (rel) {
  1624. case REL_OPEN:
  1625. dirfd = open(dir_s1d2, O_DIRECTORY);
  1626. ASSERT_LE(0, dirfd);
  1627. break;
  1628. case REL_CHDIR:
  1629. ASSERT_EQ(0, chdir(dir_s1d2));
  1630. dirfd = AT_FDCWD;
  1631. break;
  1632. case REL_CHROOT_ONLY:
  1633. /* Do chroot into dir_s1d2 (relative to dir_s2d2). */
  1634. ASSERT_EQ(0, chroot("../../s1d1/s1d2"))
  1635. {
  1636. TH_LOG("Failed to chroot: %s", strerror(errno));
  1637. }
  1638. dirfd = AT_FDCWD;
  1639. break;
  1640. case REL_CHROOT_CHDIR:
  1641. /* Do chroot into dir_s1d2. */
  1642. ASSERT_EQ(0, chroot("."))
  1643. {
  1644. TH_LOG("Failed to chroot: %s", strerror(errno));
  1645. }
  1646. dirfd = AT_FDCWD;
  1647. break;
  1648. }
  1649. ASSERT_EQ((rel == REL_CHROOT_CHDIR) ? 0 : EACCES,
  1650. test_open_rel(dirfd, "..", O_RDONLY));
  1651. ASSERT_EQ(0, test_open_rel(dirfd, ".", O_RDONLY));
  1652. if (rel == REL_CHROOT_ONLY) {
  1653. /* The current directory is dir_s2d2. */
  1654. ASSERT_EQ(0, test_open_rel(dirfd, "./s2d3", O_RDONLY));
  1655. } else {
  1656. /* The current directory is dir_s1d2. */
  1657. ASSERT_EQ(0, test_open_rel(dirfd, "./s1d3", O_RDONLY));
  1658. }
  1659. if (rel == REL_CHROOT_ONLY || rel == REL_CHROOT_CHDIR) {
  1660. /* Checks the root dir_s1d2. */
  1661. ASSERT_EQ(0, test_open_rel(dirfd, "/..", O_RDONLY));
  1662. ASSERT_EQ(0, test_open_rel(dirfd, "/", O_RDONLY));
  1663. ASSERT_EQ(0, test_open_rel(dirfd, "/f1", O_RDONLY));
  1664. ASSERT_EQ(0, test_open_rel(dirfd, "/s1d3", O_RDONLY));
  1665. }
  1666. if (rel != REL_CHROOT_CHDIR) {
  1667. ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s1d1", O_RDONLY));
  1668. ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2", O_RDONLY));
  1669. ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2/s1d3",
  1670. O_RDONLY));
  1671. ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s2d1", O_RDONLY));
  1672. ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2", O_RDONLY));
  1673. ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2/s2d3",
  1674. O_RDONLY));
  1675. }
  1676. if (rel == REL_OPEN)
  1677. ASSERT_EQ(0, close(dirfd));
  1678. ASSERT_EQ(0, close(ruleset_fd));
  1679. }
  1680. TEST_F_FORK(layout1, relative_open)
  1681. {
  1682. test_relative_path(_metadata, REL_OPEN);
  1683. }
  1684. TEST_F_FORK(layout1, relative_chdir)
  1685. {
  1686. test_relative_path(_metadata, REL_CHDIR);
  1687. }
  1688. TEST_F_FORK(layout1, relative_chroot_only)
  1689. {
  1690. test_relative_path(_metadata, REL_CHROOT_ONLY);
  1691. }
  1692. TEST_F_FORK(layout1, relative_chroot_chdir)
  1693. {
  1694. test_relative_path(_metadata, REL_CHROOT_CHDIR);
  1695. }
  1696. static void copy_binary(struct __test_metadata *const _metadata,
  1697. const char *const dst_path)
  1698. {
  1699. int dst_fd, src_fd;
  1700. struct stat statbuf;
  1701. dst_fd = open(dst_path, O_WRONLY | O_TRUNC | O_CLOEXEC);
  1702. ASSERT_LE(0, dst_fd)
  1703. {
  1704. TH_LOG("Failed to open \"%s\": %s", dst_path, strerror(errno));
  1705. }
  1706. src_fd = open(BINARY_PATH, O_RDONLY | O_CLOEXEC);
  1707. ASSERT_LE(0, src_fd)
  1708. {
  1709. TH_LOG("Failed to open \"" BINARY_PATH "\": %s",
  1710. strerror(errno));
  1711. }
  1712. ASSERT_EQ(0, fstat(src_fd, &statbuf));
  1713. ASSERT_EQ(statbuf.st_size,
  1714. sendfile(dst_fd, src_fd, 0, statbuf.st_size));
  1715. ASSERT_EQ(0, close(src_fd));
  1716. ASSERT_EQ(0, close(dst_fd));
  1717. }
  1718. static void test_execute(struct __test_metadata *const _metadata, const int err,
  1719. const char *const path)
  1720. {
  1721. int status;
  1722. char *const argv[] = { (char *)path, NULL };
  1723. const pid_t child = fork();
  1724. ASSERT_LE(0, child);
  1725. if (child == 0) {
  1726. ASSERT_EQ(err ? -1 : 0, execve(path, argv, NULL))
  1727. {
  1728. TH_LOG("Failed to execute \"%s\": %s", path,
  1729. strerror(errno));
  1730. };
  1731. ASSERT_EQ(err, errno);
  1732. _exit(__test_passed(_metadata) ? 2 : 1);
  1733. return;
  1734. }
  1735. ASSERT_EQ(child, waitpid(child, &status, 0));
  1736. ASSERT_EQ(1, WIFEXITED(status));
  1737. ASSERT_EQ(err ? 2 : 0, WEXITSTATUS(status))
  1738. {
  1739. TH_LOG("Unexpected return code for \"%s\"", path);
  1740. };
  1741. }
  1742. TEST_F_FORK(layout1, execute)
  1743. {
  1744. const struct rule rules[] = {
  1745. {
  1746. .path = dir_s1d2,
  1747. .access = LANDLOCK_ACCESS_FS_EXECUTE,
  1748. },
  1749. {},
  1750. };
  1751. const int ruleset_fd =
  1752. create_ruleset(_metadata, rules[0].access, rules);
  1753. ASSERT_LE(0, ruleset_fd);
  1754. copy_binary(_metadata, file1_s1d1);
  1755. copy_binary(_metadata, file1_s1d2);
  1756. copy_binary(_metadata, file1_s1d3);
  1757. enforce_ruleset(_metadata, ruleset_fd);
  1758. ASSERT_EQ(0, close(ruleset_fd));
  1759. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  1760. ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
  1761. test_execute(_metadata, EACCES, file1_s1d1);
  1762. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
  1763. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  1764. test_execute(_metadata, 0, file1_s1d2);
  1765. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
  1766. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  1767. test_execute(_metadata, 0, file1_s1d3);
  1768. }
  1769. TEST_F_FORK(layout1, link)
  1770. {
  1771. const struct rule layer1[] = {
  1772. {
  1773. .path = dir_s1d2,
  1774. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  1775. },
  1776. {},
  1777. };
  1778. const struct rule layer2[] = {
  1779. {
  1780. .path = dir_s1d3,
  1781. .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
  1782. },
  1783. {},
  1784. };
  1785. int ruleset_fd = create_ruleset(_metadata, layer1[0].access, layer1);
  1786. ASSERT_LE(0, ruleset_fd);
  1787. ASSERT_EQ(0, unlink(file1_s1d1));
  1788. ASSERT_EQ(0, unlink(file1_s1d2));
  1789. ASSERT_EQ(0, unlink(file1_s1d3));
  1790. enforce_ruleset(_metadata, ruleset_fd);
  1791. ASSERT_EQ(0, close(ruleset_fd));
  1792. ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
  1793. ASSERT_EQ(EACCES, errno);
  1794. /* Denies linking because of reparenting. */
  1795. ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2));
  1796. ASSERT_EQ(EXDEV, errno);
  1797. ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3));
  1798. ASSERT_EQ(EXDEV, errno);
  1799. ASSERT_EQ(-1, link(file2_s1d3, file1_s1d2));
  1800. ASSERT_EQ(EXDEV, errno);
  1801. ASSERT_EQ(0, link(file2_s1d2, file1_s1d2));
  1802. ASSERT_EQ(0, link(file2_s1d3, file1_s1d3));
  1803. /* Prepares for next unlinks. */
  1804. ASSERT_EQ(0, unlink(file2_s1d2));
  1805. ASSERT_EQ(0, unlink(file2_s1d3));
  1806. ruleset_fd = create_ruleset(_metadata, layer2[0].access, layer2);
  1807. ASSERT_LE(0, ruleset_fd);
  1808. enforce_ruleset(_metadata, ruleset_fd);
  1809. ASSERT_EQ(0, close(ruleset_fd));
  1810. /* Checks that linkind doesn't require the ability to delete a file. */
  1811. ASSERT_EQ(0, link(file1_s1d2, file2_s1d2));
  1812. ASSERT_EQ(0, link(file1_s1d3, file2_s1d3));
  1813. }
  1814. static int test_rename(const char *const oldpath, const char *const newpath)
  1815. {
  1816. if (rename(oldpath, newpath))
  1817. return errno;
  1818. return 0;
  1819. }
  1820. static int test_exchange(const char *const oldpath, const char *const newpath)
  1821. {
  1822. if (renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_EXCHANGE))
  1823. return errno;
  1824. return 0;
  1825. }
  1826. TEST_F_FORK(layout1, rename_file)
  1827. {
  1828. const struct rule rules[] = {
  1829. {
  1830. .path = dir_s1d3,
  1831. .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
  1832. },
  1833. {
  1834. .path = dir_s2d2,
  1835. .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
  1836. },
  1837. {},
  1838. };
  1839. const int ruleset_fd =
  1840. create_ruleset(_metadata, rules[0].access, rules);
  1841. ASSERT_LE(0, ruleset_fd);
  1842. ASSERT_EQ(0, unlink(file1_s1d2));
  1843. enforce_ruleset(_metadata, ruleset_fd);
  1844. ASSERT_EQ(0, close(ruleset_fd));
  1845. /*
  1846. * Tries to replace a file, from a directory that allows file removal,
  1847. * but to a different directory (which also allows file removal).
  1848. */
  1849. ASSERT_EQ(-1, rename(file1_s2d3, file1_s1d3));
  1850. ASSERT_EQ(EXDEV, errno);
  1851. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d3,
  1852. RENAME_EXCHANGE));
  1853. ASSERT_EQ(EXDEV, errno);
  1854. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3,
  1855. RENAME_EXCHANGE));
  1856. ASSERT_EQ(EXDEV, errno);
  1857. /*
  1858. * Tries to replace a file, from a directory that denies file removal,
  1859. * to a different directory (which allows file removal).
  1860. */
  1861. ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3));
  1862. ASSERT_EQ(EACCES, errno);
  1863. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file1_s1d3,
  1864. RENAME_EXCHANGE));
  1865. ASSERT_EQ(EACCES, errno);
  1866. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s1d3,
  1867. RENAME_EXCHANGE));
  1868. ASSERT_EQ(EXDEV, errno);
  1869. /* Exchanges files and directories that partially allow removal. */
  1870. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s2d1,
  1871. RENAME_EXCHANGE));
  1872. ASSERT_EQ(EACCES, errno);
  1873. /* Checks that file1_s2d1 cannot be removed (instead of ENOTDIR). */
  1874. ASSERT_EQ(-1, rename(dir_s2d2, file1_s2d1));
  1875. ASSERT_EQ(EACCES, errno);
  1876. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, dir_s2d2,
  1877. RENAME_EXCHANGE));
  1878. ASSERT_EQ(EACCES, errno);
  1879. /* Checks that file1_s1d1 cannot be removed (instead of EISDIR). */
  1880. ASSERT_EQ(-1, rename(file1_s1d1, dir_s1d2));
  1881. ASSERT_EQ(EACCES, errno);
  1882. /* Renames files with different parents. */
  1883. ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2));
  1884. ASSERT_EQ(EXDEV, errno);
  1885. ASSERT_EQ(0, unlink(file1_s1d3));
  1886. ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3));
  1887. ASSERT_EQ(EACCES, errno);
  1888. /* Exchanges and renames files with same parent. */
  1889. ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s2d3,
  1890. RENAME_EXCHANGE));
  1891. ASSERT_EQ(0, rename(file2_s2d3, file1_s2d3));
  1892. /* Exchanges files and directories with same parent, twice. */
  1893. ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3,
  1894. RENAME_EXCHANGE));
  1895. ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3,
  1896. RENAME_EXCHANGE));
  1897. }
  1898. TEST_F_FORK(layout1, rename_dir)
  1899. {
  1900. const struct rule rules[] = {
  1901. {
  1902. .path = dir_s1d2,
  1903. .access = LANDLOCK_ACCESS_FS_REMOVE_DIR,
  1904. },
  1905. {
  1906. .path = dir_s2d1,
  1907. .access = LANDLOCK_ACCESS_FS_REMOVE_DIR,
  1908. },
  1909. {},
  1910. };
  1911. const int ruleset_fd =
  1912. create_ruleset(_metadata, rules[0].access, rules);
  1913. ASSERT_LE(0, ruleset_fd);
  1914. /* Empties dir_s1d3 to allow renaming. */
  1915. ASSERT_EQ(0, unlink(file1_s1d3));
  1916. ASSERT_EQ(0, unlink(file2_s1d3));
  1917. enforce_ruleset(_metadata, ruleset_fd);
  1918. ASSERT_EQ(0, close(ruleset_fd));
  1919. /* Exchanges and renames directory to a different parent. */
  1920. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3,
  1921. RENAME_EXCHANGE));
  1922. ASSERT_EQ(EXDEV, errno);
  1923. ASSERT_EQ(-1, rename(dir_s2d3, dir_s1d3));
  1924. ASSERT_EQ(EXDEV, errno);
  1925. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3,
  1926. RENAME_EXCHANGE));
  1927. ASSERT_EQ(EXDEV, errno);
  1928. /*
  1929. * Exchanges directory to the same parent, which doesn't allow
  1930. * directory removal.
  1931. */
  1932. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d1, AT_FDCWD, dir_s2d1,
  1933. RENAME_EXCHANGE));
  1934. ASSERT_EQ(EACCES, errno);
  1935. /* Checks that dir_s1d2 cannot be removed (instead of ENOTDIR). */
  1936. ASSERT_EQ(-1, rename(dir_s1d2, file1_s1d1));
  1937. ASSERT_EQ(EACCES, errno);
  1938. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s1d2,
  1939. RENAME_EXCHANGE));
  1940. ASSERT_EQ(EACCES, errno);
  1941. /* Checks that dir_s1d2 cannot be removed (instead of EISDIR). */
  1942. ASSERT_EQ(-1, rename(file1_s1d1, dir_s1d2));
  1943. ASSERT_EQ(EACCES, errno);
  1944. /*
  1945. * Exchanges and renames directory to the same parent, which allows
  1946. * directory removal.
  1947. */
  1948. ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s1d2,
  1949. RENAME_EXCHANGE));
  1950. ASSERT_EQ(0, unlink(dir_s1d3));
  1951. ASSERT_EQ(0, mkdir(dir_s1d3, 0700));
  1952. ASSERT_EQ(0, rename(file1_s1d2, dir_s1d3));
  1953. ASSERT_EQ(0, rmdir(dir_s1d3));
  1954. }
  1955. TEST_F_FORK(layout1, reparent_refer)
  1956. {
  1957. const struct rule layer1[] = {
  1958. {
  1959. .path = dir_s1d2,
  1960. .access = LANDLOCK_ACCESS_FS_REFER,
  1961. },
  1962. {
  1963. .path = dir_s2d2,
  1964. .access = LANDLOCK_ACCESS_FS_REFER,
  1965. },
  1966. {},
  1967. };
  1968. int ruleset_fd =
  1969. create_ruleset(_metadata, LANDLOCK_ACCESS_FS_REFER, layer1);
  1970. ASSERT_LE(0, ruleset_fd);
  1971. enforce_ruleset(_metadata, ruleset_fd);
  1972. ASSERT_EQ(0, close(ruleset_fd));
  1973. ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d1));
  1974. ASSERT_EQ(EXDEV, errno);
  1975. ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d2));
  1976. ASSERT_EQ(EXDEV, errno);
  1977. ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d3));
  1978. ASSERT_EQ(EXDEV, errno);
  1979. ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d1));
  1980. ASSERT_EQ(EXDEV, errno);
  1981. ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d2));
  1982. ASSERT_EQ(EXDEV, errno);
  1983. /*
  1984. * Moving should only be allowed when the source and the destination
  1985. * parent directory have REFER.
  1986. */
  1987. ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d3));
  1988. ASSERT_EQ(ENOTEMPTY, errno);
  1989. ASSERT_EQ(0, unlink(file1_s2d3));
  1990. ASSERT_EQ(0, unlink(file2_s2d3));
  1991. ASSERT_EQ(0, rename(dir_s1d3, dir_s2d3));
  1992. }
  1993. /* Checks renames beneath dir_s1d1. */
  1994. static void refer_denied_by_default(struct __test_metadata *const _metadata,
  1995. const struct rule layer1[],
  1996. const int layer1_err,
  1997. const struct rule layer2[])
  1998. {
  1999. int ruleset_fd;
  2000. ASSERT_EQ(0, unlink(file1_s1d2));
  2001. ruleset_fd = create_ruleset(_metadata, layer1[0].access, layer1);
  2002. ASSERT_LE(0, ruleset_fd);
  2003. enforce_ruleset(_metadata, ruleset_fd);
  2004. ASSERT_EQ(0, close(ruleset_fd));
  2005. /*
  2006. * If the first layer handles LANDLOCK_ACCESS_FS_REFER (according to
  2007. * layer1_err), then it allows some different-parent renames and links.
  2008. */
  2009. ASSERT_EQ(layer1_err, test_rename(file1_s1d1, file1_s1d2));
  2010. if (layer1_err == 0)
  2011. ASSERT_EQ(layer1_err, test_rename(file1_s1d2, file1_s1d1));
  2012. ASSERT_EQ(layer1_err, test_exchange(file2_s1d1, file2_s1d2));
  2013. ASSERT_EQ(layer1_err, test_exchange(file2_s1d2, file2_s1d1));
  2014. ruleset_fd = create_ruleset(_metadata, layer2[0].access, layer2);
  2015. ASSERT_LE(0, ruleset_fd);
  2016. enforce_ruleset(_metadata, ruleset_fd);
  2017. ASSERT_EQ(0, close(ruleset_fd));
  2018. /*
  2019. * Now, either the first or the second layer does not handle
  2020. * LANDLOCK_ACCESS_FS_REFER, which means that any different-parent
  2021. * renames and links are denied, thus making the layer handling
  2022. * LANDLOCK_ACCESS_FS_REFER null and void.
  2023. */
  2024. ASSERT_EQ(EXDEV, test_rename(file1_s1d1, file1_s1d2));
  2025. ASSERT_EQ(EXDEV, test_exchange(file2_s1d1, file2_s1d2));
  2026. ASSERT_EQ(EXDEV, test_exchange(file2_s1d2, file2_s1d1));
  2027. }
  2028. const struct rule layer_dir_s1d1_refer[] = {
  2029. {
  2030. .path = dir_s1d1,
  2031. .access = LANDLOCK_ACCESS_FS_REFER,
  2032. },
  2033. {},
  2034. };
  2035. const struct rule layer_dir_s1d1_execute[] = {
  2036. {
  2037. /* Matches a parent directory. */
  2038. .path = dir_s1d1,
  2039. .access = LANDLOCK_ACCESS_FS_EXECUTE,
  2040. },
  2041. {},
  2042. };
  2043. const struct rule layer_dir_s2d1_execute[] = {
  2044. {
  2045. /* Does not match a parent directory. */
  2046. .path = dir_s2d1,
  2047. .access = LANDLOCK_ACCESS_FS_EXECUTE,
  2048. },
  2049. {},
  2050. };
  2051. /*
  2052. * Tests precedence over renames: denied by default for different parent
  2053. * directories, *with* a rule matching a parent directory, but not directly
  2054. * denying access (with MAKE_REG nor REMOVE).
  2055. */
  2056. TEST_F_FORK(layout1, refer_denied_by_default1)
  2057. {
  2058. refer_denied_by_default(_metadata, layer_dir_s1d1_refer, 0,
  2059. layer_dir_s1d1_execute);
  2060. }
  2061. /*
  2062. * Same test but this time turning around the ABI version order: the first
  2063. * layer does not handle LANDLOCK_ACCESS_FS_REFER.
  2064. */
  2065. TEST_F_FORK(layout1, refer_denied_by_default2)
  2066. {
  2067. refer_denied_by_default(_metadata, layer_dir_s1d1_execute, EXDEV,
  2068. layer_dir_s1d1_refer);
  2069. }
  2070. /*
  2071. * Tests precedence over renames: denied by default for different parent
  2072. * directories, *without* a rule matching a parent directory, but not directly
  2073. * denying access (with MAKE_REG nor REMOVE).
  2074. */
  2075. TEST_F_FORK(layout1, refer_denied_by_default3)
  2076. {
  2077. refer_denied_by_default(_metadata, layer_dir_s1d1_refer, 0,
  2078. layer_dir_s2d1_execute);
  2079. }
  2080. /*
  2081. * Same test but this time turning around the ABI version order: the first
  2082. * layer does not handle LANDLOCK_ACCESS_FS_REFER.
  2083. */
  2084. TEST_F_FORK(layout1, refer_denied_by_default4)
  2085. {
  2086. refer_denied_by_default(_metadata, layer_dir_s2d1_execute, EXDEV,
  2087. layer_dir_s1d1_refer);
  2088. }
  2089. /*
  2090. * Tests walking through a denied root mount.
  2091. */
  2092. TEST_F_FORK(layout1, refer_mount_root_deny)
  2093. {
  2094. const struct landlock_ruleset_attr ruleset_attr = {
  2095. .handled_access_fs = LANDLOCK_ACCESS_FS_MAKE_DIR,
  2096. };
  2097. int root_fd, ruleset_fd;
  2098. /* Creates a mount object from a non-mount point. */
  2099. set_cap(_metadata, CAP_SYS_ADMIN);
  2100. root_fd =
  2101. open_tree(AT_FDCWD, dir_s1d1,
  2102. AT_EMPTY_PATH | OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
  2103. clear_cap(_metadata, CAP_SYS_ADMIN);
  2104. ASSERT_LE(0, root_fd);
  2105. ruleset_fd =
  2106. landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
  2107. ASSERT_LE(0, ruleset_fd);
  2108. ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
  2109. ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
  2110. EXPECT_EQ(0, close(ruleset_fd));
  2111. /* Link denied by Landlock: EACCES. */
  2112. EXPECT_EQ(-1, linkat(root_fd, ".", root_fd, "does_not_exist", 0));
  2113. EXPECT_EQ(EACCES, errno);
  2114. /* renameat2() always returns EBUSY. */
  2115. EXPECT_EQ(-1, renameat2(root_fd, ".", root_fd, "does_not_exist", 0));
  2116. EXPECT_EQ(EBUSY, errno);
  2117. EXPECT_EQ(0, close(root_fd));
  2118. }
  2119. TEST_F_FORK(layout1, reparent_link)
  2120. {
  2121. const struct rule layer1[] = {
  2122. {
  2123. .path = dir_s1d2,
  2124. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  2125. },
  2126. {
  2127. .path = dir_s1d3,
  2128. .access = LANDLOCK_ACCESS_FS_REFER,
  2129. },
  2130. {
  2131. .path = dir_s2d2,
  2132. .access = LANDLOCK_ACCESS_FS_REFER,
  2133. },
  2134. {
  2135. .path = dir_s2d3,
  2136. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  2137. },
  2138. {},
  2139. };
  2140. const int ruleset_fd = create_ruleset(
  2141. _metadata,
  2142. LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, layer1);
  2143. ASSERT_LE(0, ruleset_fd);
  2144. enforce_ruleset(_metadata, ruleset_fd);
  2145. ASSERT_EQ(0, close(ruleset_fd));
  2146. ASSERT_EQ(0, unlink(file1_s1d1));
  2147. ASSERT_EQ(0, unlink(file1_s1d2));
  2148. ASSERT_EQ(0, unlink(file1_s1d3));
  2149. /* Denies linking because of missing MAKE_REG. */
  2150. ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
  2151. ASSERT_EQ(EACCES, errno);
  2152. /* Denies linking because of missing source and destination REFER. */
  2153. ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2));
  2154. ASSERT_EQ(EXDEV, errno);
  2155. /* Denies linking because of missing source REFER. */
  2156. ASSERT_EQ(-1, link(file1_s2d1, file1_s1d3));
  2157. ASSERT_EQ(EXDEV, errno);
  2158. /* Denies linking because of missing MAKE_REG. */
  2159. ASSERT_EQ(-1, link(file1_s2d2, file1_s1d1));
  2160. ASSERT_EQ(EACCES, errno);
  2161. /* Denies linking because of missing destination REFER. */
  2162. ASSERT_EQ(-1, link(file1_s2d2, file1_s1d2));
  2163. ASSERT_EQ(EXDEV, errno);
  2164. /* Allows linking because of REFER and MAKE_REG. */
  2165. ASSERT_EQ(0, link(file1_s2d2, file1_s1d3));
  2166. ASSERT_EQ(0, unlink(file1_s2d2));
  2167. /* Reverse linking denied because of missing MAKE_REG. */
  2168. ASSERT_EQ(-1, link(file1_s1d3, file1_s2d2));
  2169. ASSERT_EQ(EACCES, errno);
  2170. ASSERT_EQ(0, unlink(file1_s2d3));
  2171. /* Checks reverse linking. */
  2172. ASSERT_EQ(0, link(file1_s1d3, file1_s2d3));
  2173. ASSERT_EQ(0, unlink(file1_s1d3));
  2174. /*
  2175. * This is OK for a file link, but it should not be allowed for a
  2176. * directory rename (because of the superset of access rights.
  2177. */
  2178. ASSERT_EQ(0, link(file1_s2d3, file1_s1d3));
  2179. ASSERT_EQ(0, unlink(file1_s1d3));
  2180. ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3));
  2181. ASSERT_EQ(EXDEV, errno);
  2182. ASSERT_EQ(-1, link(file2_s1d3, file1_s1d2));
  2183. ASSERT_EQ(EXDEV, errno);
  2184. ASSERT_EQ(0, link(file2_s1d2, file1_s1d2));
  2185. ASSERT_EQ(0, link(file2_s1d3, file1_s1d3));
  2186. }
  2187. TEST_F_FORK(layout1, reparent_rename)
  2188. {
  2189. /* Same rules as for reparent_link. */
  2190. const struct rule layer1[] = {
  2191. {
  2192. .path = dir_s1d2,
  2193. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  2194. },
  2195. {
  2196. .path = dir_s1d3,
  2197. .access = LANDLOCK_ACCESS_FS_REFER,
  2198. },
  2199. {
  2200. .path = dir_s2d2,
  2201. .access = LANDLOCK_ACCESS_FS_REFER,
  2202. },
  2203. {
  2204. .path = dir_s2d3,
  2205. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  2206. },
  2207. {},
  2208. };
  2209. const int ruleset_fd = create_ruleset(
  2210. _metadata,
  2211. LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, layer1);
  2212. ASSERT_LE(0, ruleset_fd);
  2213. enforce_ruleset(_metadata, ruleset_fd);
  2214. ASSERT_EQ(0, close(ruleset_fd));
  2215. ASSERT_EQ(0, unlink(file1_s1d2));
  2216. ASSERT_EQ(0, unlink(file1_s1d3));
  2217. /* Denies renaming because of missing MAKE_REG. */
  2218. ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file1_s1d1,
  2219. RENAME_EXCHANGE));
  2220. ASSERT_EQ(EACCES, errno);
  2221. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file2_s1d1,
  2222. RENAME_EXCHANGE));
  2223. ASSERT_EQ(EACCES, errno);
  2224. ASSERT_EQ(0, unlink(file1_s1d1));
  2225. ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1));
  2226. ASSERT_EQ(EACCES, errno);
  2227. /* Even denies same file exchange. */
  2228. ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file2_s1d1,
  2229. RENAME_EXCHANGE));
  2230. ASSERT_EQ(EACCES, errno);
  2231. /* Denies renaming because of missing source and destination REFER. */
  2232. ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d2));
  2233. ASSERT_EQ(EXDEV, errno);
  2234. /*
  2235. * Denies renaming because of missing MAKE_REG, source and destination
  2236. * REFER.
  2237. */
  2238. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file2_s1d1,
  2239. RENAME_EXCHANGE));
  2240. ASSERT_EQ(EACCES, errno);
  2241. ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file1_s2d1,
  2242. RENAME_EXCHANGE));
  2243. ASSERT_EQ(EACCES, errno);
  2244. /* Denies renaming because of missing source REFER. */
  2245. ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3));
  2246. ASSERT_EQ(EXDEV, errno);
  2247. /* Denies renaming because of missing MAKE_REG. */
  2248. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file2_s1d3,
  2249. RENAME_EXCHANGE));
  2250. ASSERT_EQ(EACCES, errno);
  2251. /* Denies renaming because of missing MAKE_REG. */
  2252. ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d1));
  2253. ASSERT_EQ(EACCES, errno);
  2254. /* Denies renaming because of missing destination REFER*/
  2255. ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2));
  2256. ASSERT_EQ(EXDEV, errno);
  2257. /* Denies exchange because of one missing MAKE_REG. */
  2258. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, file2_s1d3,
  2259. RENAME_EXCHANGE));
  2260. ASSERT_EQ(EACCES, errno);
  2261. /* Allows renaming because of REFER and MAKE_REG. */
  2262. ASSERT_EQ(0, rename(file1_s2d2, file1_s1d3));
  2263. /* Reverse renaming denied because of missing MAKE_REG. */
  2264. ASSERT_EQ(-1, rename(file1_s1d3, file1_s2d2));
  2265. ASSERT_EQ(EACCES, errno);
  2266. ASSERT_EQ(0, unlink(file1_s2d3));
  2267. ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
  2268. /* Tests reverse renaming. */
  2269. ASSERT_EQ(0, rename(file1_s2d3, file1_s1d3));
  2270. ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s1d3,
  2271. RENAME_EXCHANGE));
  2272. ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
  2273. /*
  2274. * This is OK for a file rename, but it should not be allowed for a
  2275. * directory rename (because of the superset of access rights).
  2276. */
  2277. ASSERT_EQ(0, rename(file1_s2d3, file1_s1d3));
  2278. ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
  2279. /*
  2280. * Tests superset restrictions applied to directories. Not only the
  2281. * dir_s2d3's parent (dir_s2d2) should be taken into account but also
  2282. * access rights tied to dir_s2d3. dir_s2d2 is missing one access right
  2283. * compared to dir_s1d3/file1_s1d3 (MAKE_REG) but it is provided
  2284. * directly by the moved dir_s2d3.
  2285. */
  2286. ASSERT_EQ(0, rename(dir_s2d3, file1_s1d3));
  2287. ASSERT_EQ(0, rename(file1_s1d3, dir_s2d3));
  2288. /*
  2289. * The first rename is allowed but not the exchange because dir_s1d3's
  2290. * parent (dir_s1d2) doesn't have REFER.
  2291. */
  2292. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3,
  2293. RENAME_EXCHANGE));
  2294. ASSERT_EQ(EXDEV, errno);
  2295. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s2d3,
  2296. RENAME_EXCHANGE));
  2297. ASSERT_EQ(EXDEV, errno);
  2298. ASSERT_EQ(-1, rename(file1_s2d3, dir_s1d3));
  2299. ASSERT_EQ(EXDEV, errno);
  2300. ASSERT_EQ(-1, rename(file2_s1d2, file1_s1d3));
  2301. ASSERT_EQ(EXDEV, errno);
  2302. ASSERT_EQ(-1, rename(file2_s1d3, file1_s1d2));
  2303. ASSERT_EQ(EXDEV, errno);
  2304. /* Renaming in the same directory is always allowed. */
  2305. ASSERT_EQ(0, rename(file2_s1d2, file1_s1d2));
  2306. ASSERT_EQ(0, rename(file2_s1d3, file1_s1d3));
  2307. ASSERT_EQ(0, unlink(file1_s1d2));
  2308. /* Denies because of missing source MAKE_REG and destination REFER. */
  2309. ASSERT_EQ(-1, rename(dir_s2d3, file1_s1d2));
  2310. ASSERT_EQ(EXDEV, errno);
  2311. ASSERT_EQ(0, unlink(file1_s1d3));
  2312. /* Denies because of missing source MAKE_REG and REFER. */
  2313. ASSERT_EQ(-1, rename(dir_s2d2, file1_s1d3));
  2314. ASSERT_EQ(EXDEV, errno);
  2315. }
  2316. static void
  2317. reparent_exdev_layers_enforce1(struct __test_metadata *const _metadata)
  2318. {
  2319. const struct rule layer1[] = {
  2320. {
  2321. .path = dir_s1d2,
  2322. .access = LANDLOCK_ACCESS_FS_REFER,
  2323. },
  2324. {
  2325. /* Interesting for the layer2 tests. */
  2326. .path = dir_s1d3,
  2327. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  2328. },
  2329. {
  2330. .path = dir_s2d2,
  2331. .access = LANDLOCK_ACCESS_FS_REFER,
  2332. },
  2333. {
  2334. .path = dir_s2d3,
  2335. .access = LANDLOCK_ACCESS_FS_MAKE_REG,
  2336. },
  2337. {},
  2338. };
  2339. const int ruleset_fd = create_ruleset(
  2340. _metadata,
  2341. LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, layer1);
  2342. ASSERT_LE(0, ruleset_fd);
  2343. enforce_ruleset(_metadata, ruleset_fd);
  2344. ASSERT_EQ(0, close(ruleset_fd));
  2345. }
  2346. static void
  2347. reparent_exdev_layers_enforce2(struct __test_metadata *const _metadata)
  2348. {
  2349. const struct rule layer2[] = {
  2350. {
  2351. .path = dir_s2d3,
  2352. .access = LANDLOCK_ACCESS_FS_MAKE_DIR,
  2353. },
  2354. {},
  2355. };
  2356. /*
  2357. * Same checks as before but with a second layer and a new MAKE_DIR
  2358. * rule (and no explicit handling of REFER).
  2359. */
  2360. const int ruleset_fd =
  2361. create_ruleset(_metadata, LANDLOCK_ACCESS_FS_MAKE_DIR, layer2);
  2362. ASSERT_LE(0, ruleset_fd);
  2363. enforce_ruleset(_metadata, ruleset_fd);
  2364. ASSERT_EQ(0, close(ruleset_fd));
  2365. }
  2366. TEST_F_FORK(layout1, reparent_exdev_layers_rename1)
  2367. {
  2368. ASSERT_EQ(0, unlink(file1_s2d2));
  2369. ASSERT_EQ(0, unlink(file1_s2d3));
  2370. reparent_exdev_layers_enforce1(_metadata);
  2371. /*
  2372. * Moving the dir_s1d3 directory below dir_s2d2 is allowed by Landlock
  2373. * because it doesn't inherit new access rights.
  2374. */
  2375. ASSERT_EQ(0, rename(dir_s1d3, file1_s2d2));
  2376. ASSERT_EQ(0, rename(file1_s2d2, dir_s1d3));
  2377. /*
  2378. * Moving the dir_s1d3 directory below dir_s2d3 is allowed, even if it
  2379. * gets a new inherited access rights (MAKE_REG), because MAKE_REG is
  2380. * already allowed for dir_s1d3.
  2381. */
  2382. ASSERT_EQ(0, rename(dir_s1d3, file1_s2d3));
  2383. ASSERT_EQ(0, rename(file1_s2d3, dir_s1d3));
  2384. /*
  2385. * However, moving the file1_s1d3 file below dir_s2d3 is allowed
  2386. * because it cannot inherit MAKE_REG right (which is dedicated to
  2387. * directories).
  2388. */
  2389. ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
  2390. reparent_exdev_layers_enforce2(_metadata);
  2391. /*
  2392. * Moving the dir_s1d3 directory below dir_s2d2 is now denied because
  2393. * MAKE_DIR is not tied to dir_s2d2.
  2394. */
  2395. ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d2));
  2396. ASSERT_EQ(EACCES, errno);
  2397. /*
  2398. * Moving the dir_s1d3 directory below dir_s2d3 is forbidden because it
  2399. * would grants MAKE_REG and MAKE_DIR rights to it.
  2400. */
  2401. ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d3));
  2402. ASSERT_EQ(EXDEV, errno);
  2403. /*
  2404. * Moving the file2_s1d3 file below dir_s2d3 is denied because the
  2405. * second layer does not handle REFER, which is always denied by
  2406. * default.
  2407. */
  2408. ASSERT_EQ(-1, rename(file2_s1d3, file1_s2d3));
  2409. ASSERT_EQ(EXDEV, errno);
  2410. }
  2411. TEST_F_FORK(layout1, reparent_exdev_layers_rename2)
  2412. {
  2413. reparent_exdev_layers_enforce1(_metadata);
  2414. /* Checks EACCES predominance over EXDEV. */
  2415. ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d2));
  2416. ASSERT_EQ(EACCES, errno);
  2417. ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d2));
  2418. ASSERT_EQ(EACCES, errno);
  2419. ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d3));
  2420. ASSERT_EQ(EXDEV, errno);
  2421. /* Modify layout! */
  2422. ASSERT_EQ(0, rename(file1_s1d2, file1_s2d3));
  2423. /* Without REFER source. */
  2424. ASSERT_EQ(-1, rename(dir_s1d1, file1_s2d2));
  2425. ASSERT_EQ(EXDEV, errno);
  2426. ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d2));
  2427. ASSERT_EQ(EXDEV, errno);
  2428. reparent_exdev_layers_enforce2(_metadata);
  2429. /* Checks EACCES predominance over EXDEV. */
  2430. ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d2));
  2431. ASSERT_EQ(EACCES, errno);
  2432. /* Checks with actual file2_s1d2. */
  2433. ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d2));
  2434. ASSERT_EQ(EACCES, errno);
  2435. ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d3));
  2436. ASSERT_EQ(EXDEV, errno);
  2437. /*
  2438. * Modifying the layout is now denied because the second layer does not
  2439. * handle REFER, which is always denied by default.
  2440. */
  2441. ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d3));
  2442. ASSERT_EQ(EXDEV, errno);
  2443. /* Without REFER source, EACCES wins over EXDEV. */
  2444. ASSERT_EQ(-1, rename(dir_s1d1, file1_s2d2));
  2445. ASSERT_EQ(EACCES, errno);
  2446. ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d2));
  2447. ASSERT_EQ(EACCES, errno);
  2448. }
  2449. TEST_F_FORK(layout1, reparent_exdev_layers_exchange1)
  2450. {
  2451. const char *const dir_file1_s1d2 = file1_s1d2, *const dir_file2_s2d3 =
  2452. file2_s2d3;
  2453. ASSERT_EQ(0, unlink(file1_s1d2));
  2454. ASSERT_EQ(0, mkdir(file1_s1d2, 0700));
  2455. ASSERT_EQ(0, unlink(file2_s2d3));
  2456. ASSERT_EQ(0, mkdir(file2_s2d3, 0700));
  2457. reparent_exdev_layers_enforce1(_metadata);
  2458. /* Error predominance with file exchange: returns EXDEV and EACCES. */
  2459. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d3,
  2460. RENAME_EXCHANGE));
  2461. ASSERT_EQ(EACCES, errno);
  2462. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d1,
  2463. RENAME_EXCHANGE));
  2464. ASSERT_EQ(EACCES, errno);
  2465. /*
  2466. * Checks with directories which creation could be allowed, but denied
  2467. * because of access rights that would be inherited.
  2468. */
  2469. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD,
  2470. dir_file2_s2d3, RENAME_EXCHANGE));
  2471. ASSERT_EQ(EXDEV, errno);
  2472. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD,
  2473. dir_file1_s1d2, RENAME_EXCHANGE));
  2474. ASSERT_EQ(EXDEV, errno);
  2475. /* Checks with same access rights. */
  2476. ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s2d3,
  2477. RENAME_EXCHANGE));
  2478. ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3,
  2479. RENAME_EXCHANGE));
  2480. /* Checks with different (child-only) access rights. */
  2481. ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_file1_s1d2,
  2482. RENAME_EXCHANGE));
  2483. ASSERT_EQ(0, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, dir_s2d3,
  2484. RENAME_EXCHANGE));
  2485. /*
  2486. * Checks that exchange between file and directory are consistent.
  2487. *
  2488. * Moving a file (file1_s2d2) to a directory which only grants more
  2489. * directory-related access rights is allowed, and at the same time
  2490. * moving a directory (dir_file2_s2d3) to another directory which
  2491. * grants less access rights is allowed too.
  2492. *
  2493. * See layout1.reparent_exdev_layers_exchange3 for inverted arguments.
  2494. */
  2495. ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
  2496. RENAME_EXCHANGE));
  2497. /*
  2498. * However, moving back the directory is denied because it would get
  2499. * more access rights than the current state and because file creation
  2500. * is forbidden (in dir_s2d2).
  2501. */
  2502. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
  2503. RENAME_EXCHANGE));
  2504. ASSERT_EQ(EACCES, errno);
  2505. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
  2506. RENAME_EXCHANGE));
  2507. ASSERT_EQ(EACCES, errno);
  2508. reparent_exdev_layers_enforce2(_metadata);
  2509. /* Error predominance with file exchange: returns EXDEV and EACCES. */
  2510. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d3,
  2511. RENAME_EXCHANGE));
  2512. ASSERT_EQ(EACCES, errno);
  2513. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d1,
  2514. RENAME_EXCHANGE));
  2515. ASSERT_EQ(EACCES, errno);
  2516. /* Checks with directories which creation is now denied. */
  2517. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD,
  2518. dir_file2_s2d3, RENAME_EXCHANGE));
  2519. ASSERT_EQ(EACCES, errno);
  2520. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD,
  2521. dir_file1_s1d2, RENAME_EXCHANGE));
  2522. ASSERT_EQ(EACCES, errno);
  2523. /* Checks with different (child-only) access rights. */
  2524. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s2d3,
  2525. RENAME_EXCHANGE));
  2526. /* Denied because of MAKE_DIR. */
  2527. ASSERT_EQ(EACCES, errno);
  2528. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3,
  2529. RENAME_EXCHANGE));
  2530. ASSERT_EQ(EACCES, errno);
  2531. /* Checks with different (child-only) access rights. */
  2532. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_file1_s1d2,
  2533. RENAME_EXCHANGE));
  2534. /* Denied because of MAKE_DIR. */
  2535. ASSERT_EQ(EACCES, errno);
  2536. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, dir_s2d3,
  2537. RENAME_EXCHANGE));
  2538. ASSERT_EQ(EACCES, errno);
  2539. /* See layout1.reparent_exdev_layers_exchange2 for complement. */
  2540. }
  2541. TEST_F_FORK(layout1, reparent_exdev_layers_exchange2)
  2542. {
  2543. const char *const dir_file2_s2d3 = file2_s2d3;
  2544. ASSERT_EQ(0, unlink(file2_s2d3));
  2545. ASSERT_EQ(0, mkdir(file2_s2d3, 0700));
  2546. reparent_exdev_layers_enforce1(_metadata);
  2547. reparent_exdev_layers_enforce2(_metadata);
  2548. /* Checks that exchange between file and directory are consistent. */
  2549. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
  2550. RENAME_EXCHANGE));
  2551. ASSERT_EQ(EACCES, errno);
  2552. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
  2553. RENAME_EXCHANGE));
  2554. ASSERT_EQ(EACCES, errno);
  2555. }
  2556. TEST_F_FORK(layout1, reparent_exdev_layers_exchange3)
  2557. {
  2558. const char *const dir_file2_s2d3 = file2_s2d3;
  2559. ASSERT_EQ(0, unlink(file2_s2d3));
  2560. ASSERT_EQ(0, mkdir(file2_s2d3, 0700));
  2561. reparent_exdev_layers_enforce1(_metadata);
  2562. /*
  2563. * Checks that exchange between file and directory are consistent,
  2564. * including with inverted arguments (see
  2565. * layout1.reparent_exdev_layers_exchange1).
  2566. */
  2567. ASSERT_EQ(0, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
  2568. RENAME_EXCHANGE));
  2569. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
  2570. RENAME_EXCHANGE));
  2571. ASSERT_EQ(EACCES, errno);
  2572. ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
  2573. RENAME_EXCHANGE));
  2574. ASSERT_EQ(EACCES, errno);
  2575. }
  2576. TEST_F_FORK(layout1, reparent_remove)
  2577. {
  2578. const struct rule layer1[] = {
  2579. {
  2580. .path = dir_s1d1,
  2581. .access = LANDLOCK_ACCESS_FS_REFER |
  2582. LANDLOCK_ACCESS_FS_REMOVE_DIR,
  2583. },
  2584. {
  2585. .path = dir_s1d2,
  2586. .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
  2587. },
  2588. {
  2589. .path = dir_s2d1,
  2590. .access = LANDLOCK_ACCESS_FS_REFER |
  2591. LANDLOCK_ACCESS_FS_REMOVE_FILE,
  2592. },
  2593. {},
  2594. };
  2595. const int ruleset_fd = create_ruleset(
  2596. _metadata,
  2597. LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_REMOVE_DIR |
  2598. LANDLOCK_ACCESS_FS_REMOVE_FILE,
  2599. layer1);
  2600. ASSERT_LE(0, ruleset_fd);
  2601. enforce_ruleset(_metadata, ruleset_fd);
  2602. ASSERT_EQ(0, close(ruleset_fd));
  2603. /* Access denied because of wrong/swapped remove file/dir. */
  2604. ASSERT_EQ(-1, rename(file1_s1d1, dir_s2d2));
  2605. ASSERT_EQ(EACCES, errno);
  2606. ASSERT_EQ(-1, rename(dir_s2d2, file1_s1d1));
  2607. ASSERT_EQ(EACCES, errno);
  2608. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s2d2,
  2609. RENAME_EXCHANGE));
  2610. ASSERT_EQ(EACCES, errno);
  2611. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s2d3,
  2612. RENAME_EXCHANGE));
  2613. ASSERT_EQ(EACCES, errno);
  2614. /* Access allowed thanks to the matching rights. */
  2615. ASSERT_EQ(-1, rename(file1_s2d1, dir_s1d2));
  2616. ASSERT_EQ(EISDIR, errno);
  2617. ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d1));
  2618. ASSERT_EQ(ENOTDIR, errno);
  2619. ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d1));
  2620. ASSERT_EQ(ENOTDIR, errno);
  2621. ASSERT_EQ(0, unlink(file1_s2d1));
  2622. ASSERT_EQ(0, unlink(file1_s1d3));
  2623. ASSERT_EQ(0, unlink(file2_s1d3));
  2624. ASSERT_EQ(0, rename(dir_s1d3, file1_s2d1));
  2625. /* Effectively removes a file and a directory by exchanging them. */
  2626. ASSERT_EQ(0, mkdir(dir_s1d3, 0700));
  2627. ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3,
  2628. RENAME_EXCHANGE));
  2629. ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3,
  2630. RENAME_EXCHANGE));
  2631. ASSERT_EQ(EACCES, errno);
  2632. }
  2633. TEST_F_FORK(layout1, reparent_dom_superset)
  2634. {
  2635. const struct rule layer1[] = {
  2636. {
  2637. .path = dir_s1d2,
  2638. .access = LANDLOCK_ACCESS_FS_REFER,
  2639. },
  2640. {
  2641. .path = file1_s1d2,
  2642. .access = LANDLOCK_ACCESS_FS_EXECUTE,
  2643. },
  2644. {
  2645. .path = dir_s1d3,
  2646. .access = LANDLOCK_ACCESS_FS_MAKE_SOCK |
  2647. LANDLOCK_ACCESS_FS_EXECUTE,
  2648. },
  2649. {
  2650. .path = dir_s2d2,
  2651. .access = LANDLOCK_ACCESS_FS_REFER |
  2652. LANDLOCK_ACCESS_FS_EXECUTE |
  2653. LANDLOCK_ACCESS_FS_MAKE_SOCK,
  2654. },
  2655. {
  2656. .path = dir_s2d3,
  2657. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  2658. LANDLOCK_ACCESS_FS_MAKE_FIFO,
  2659. },
  2660. {},
  2661. };
  2662. int ruleset_fd = create_ruleset(_metadata,
  2663. LANDLOCK_ACCESS_FS_REFER |
  2664. LANDLOCK_ACCESS_FS_EXECUTE |
  2665. LANDLOCK_ACCESS_FS_MAKE_SOCK |
  2666. LANDLOCK_ACCESS_FS_READ_FILE |
  2667. LANDLOCK_ACCESS_FS_MAKE_FIFO,
  2668. layer1);
  2669. ASSERT_LE(0, ruleset_fd);
  2670. enforce_ruleset(_metadata, ruleset_fd);
  2671. ASSERT_EQ(0, close(ruleset_fd));
  2672. ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d1));
  2673. ASSERT_EQ(EXDEV, errno);
  2674. /*
  2675. * Moving file1_s1d2 beneath dir_s2d3 would grant it the READ_FILE
  2676. * access right.
  2677. */
  2678. ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d3));
  2679. ASSERT_EQ(EXDEV, errno);
  2680. /*
  2681. * Moving file1_s1d2 should be allowed even if dir_s2d2 grants a
  2682. * superset of access rights compared to dir_s1d2, because file1_s1d2
  2683. * already has these access rights anyway.
  2684. */
  2685. ASSERT_EQ(0, rename(file1_s1d2, file1_s2d2));
  2686. ASSERT_EQ(0, rename(file1_s2d2, file1_s1d2));
  2687. ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d1));
  2688. ASSERT_EQ(EXDEV, errno);
  2689. /*
  2690. * Moving dir_s1d3 beneath dir_s2d3 would grant it the MAKE_FIFO access
  2691. * right.
  2692. */
  2693. ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d3));
  2694. ASSERT_EQ(EXDEV, errno);
  2695. /*
  2696. * Moving dir_s1d3 should be allowed even if dir_s2d2 grants a superset
  2697. * of access rights compared to dir_s1d2, because dir_s1d3 already has
  2698. * these access rights anyway.
  2699. */
  2700. ASSERT_EQ(0, rename(dir_s1d3, file1_s2d2));
  2701. ASSERT_EQ(0, rename(file1_s2d2, dir_s1d3));
  2702. /*
  2703. * Moving file1_s2d3 beneath dir_s1d2 is allowed, but moving it back
  2704. * will be denied because the new inherited access rights from dir_s1d2
  2705. * will be less than the destination (original) dir_s2d3. This is a
  2706. * sinkhole scenario where we cannot move back files or directories.
  2707. */
  2708. ASSERT_EQ(0, rename(file1_s2d3, file2_s1d2));
  2709. ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d3));
  2710. ASSERT_EQ(EXDEV, errno);
  2711. ASSERT_EQ(0, unlink(file2_s1d2));
  2712. ASSERT_EQ(0, unlink(file2_s2d3));
  2713. /*
  2714. * Checks similar directory one-way move: dir_s2d3 loses EXECUTE and
  2715. * MAKE_SOCK which were inherited from dir_s1d3.
  2716. */
  2717. ASSERT_EQ(0, rename(dir_s2d3, file2_s1d2));
  2718. ASSERT_EQ(-1, rename(file2_s1d2, dir_s2d3));
  2719. ASSERT_EQ(EXDEV, errno);
  2720. }
  2721. TEST_F_FORK(layout1, remove_dir)
  2722. {
  2723. const struct rule rules[] = {
  2724. {
  2725. .path = dir_s1d2,
  2726. .access = LANDLOCK_ACCESS_FS_REMOVE_DIR,
  2727. },
  2728. {},
  2729. };
  2730. const int ruleset_fd =
  2731. create_ruleset(_metadata, rules[0].access, rules);
  2732. ASSERT_LE(0, ruleset_fd);
  2733. ASSERT_EQ(0, unlink(file1_s1d1));
  2734. ASSERT_EQ(0, unlink(file1_s1d2));
  2735. ASSERT_EQ(0, unlink(file1_s1d3));
  2736. ASSERT_EQ(0, unlink(file2_s1d3));
  2737. enforce_ruleset(_metadata, ruleset_fd);
  2738. ASSERT_EQ(0, close(ruleset_fd));
  2739. ASSERT_EQ(0, rmdir(dir_s1d3));
  2740. ASSERT_EQ(0, mkdir(dir_s1d3, 0700));
  2741. ASSERT_EQ(0, unlinkat(AT_FDCWD, dir_s1d3, AT_REMOVEDIR));
  2742. /* dir_s1d2 itself cannot be removed. */
  2743. ASSERT_EQ(-1, rmdir(dir_s1d2));
  2744. ASSERT_EQ(EACCES, errno);
  2745. ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d2, AT_REMOVEDIR));
  2746. ASSERT_EQ(EACCES, errno);
  2747. ASSERT_EQ(-1, rmdir(dir_s1d1));
  2748. ASSERT_EQ(EACCES, errno);
  2749. ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d1, AT_REMOVEDIR));
  2750. ASSERT_EQ(EACCES, errno);
  2751. }
  2752. TEST_F_FORK(layout1, remove_file)
  2753. {
  2754. const struct rule rules[] = {
  2755. {
  2756. .path = dir_s1d2,
  2757. .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
  2758. },
  2759. {},
  2760. };
  2761. const int ruleset_fd =
  2762. create_ruleset(_metadata, rules[0].access, rules);
  2763. ASSERT_LE(0, ruleset_fd);
  2764. enforce_ruleset(_metadata, ruleset_fd);
  2765. ASSERT_EQ(0, close(ruleset_fd));
  2766. ASSERT_EQ(-1, unlink(file1_s1d1));
  2767. ASSERT_EQ(EACCES, errno);
  2768. ASSERT_EQ(-1, unlinkat(AT_FDCWD, file1_s1d1, 0));
  2769. ASSERT_EQ(EACCES, errno);
  2770. ASSERT_EQ(0, unlink(file1_s1d2));
  2771. ASSERT_EQ(0, unlinkat(AT_FDCWD, file1_s1d3, 0));
  2772. }
  2773. static void test_make_file(struct __test_metadata *const _metadata,
  2774. const __u64 access, const mode_t mode,
  2775. const dev_t dev)
  2776. {
  2777. const struct rule rules[] = {
  2778. {
  2779. .path = dir_s1d2,
  2780. .access = access,
  2781. },
  2782. {},
  2783. };
  2784. const int ruleset_fd = create_ruleset(_metadata, access, rules);
  2785. ASSERT_LE(0, ruleset_fd);
  2786. ASSERT_EQ(0, unlink(file1_s1d1));
  2787. ASSERT_EQ(0, unlink(file2_s1d1));
  2788. ASSERT_EQ(0, mknod(file2_s1d1, mode | 0400, dev))
  2789. {
  2790. TH_LOG("Failed to make file \"%s\": %s", file2_s1d1,
  2791. strerror(errno));
  2792. };
  2793. ASSERT_EQ(0, unlink(file1_s1d2));
  2794. ASSERT_EQ(0, unlink(file2_s1d2));
  2795. ASSERT_EQ(0, unlink(file1_s1d3));
  2796. ASSERT_EQ(0, unlink(file2_s1d3));
  2797. enforce_ruleset(_metadata, ruleset_fd);
  2798. ASSERT_EQ(0, close(ruleset_fd));
  2799. ASSERT_EQ(-1, mknod(file1_s1d1, mode | 0400, dev));
  2800. ASSERT_EQ(EACCES, errno);
  2801. ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
  2802. ASSERT_EQ(EACCES, errno);
  2803. ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1));
  2804. ASSERT_EQ(EACCES, errno);
  2805. ASSERT_EQ(0, mknod(file1_s1d2, mode | 0400, dev))
  2806. {
  2807. TH_LOG("Failed to make file \"%s\": %s", file1_s1d2,
  2808. strerror(errno));
  2809. };
  2810. ASSERT_EQ(0, link(file1_s1d2, file2_s1d2));
  2811. ASSERT_EQ(0, unlink(file2_s1d2));
  2812. ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2));
  2813. ASSERT_EQ(0, mknod(file1_s1d3, mode | 0400, dev));
  2814. ASSERT_EQ(0, link(file1_s1d3, file2_s1d3));
  2815. ASSERT_EQ(0, unlink(file2_s1d3));
  2816. ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3));
  2817. }
  2818. TEST_F_FORK(layout1, make_char)
  2819. {
  2820. /* Creates a /dev/null device. */
  2821. set_cap(_metadata, CAP_MKNOD);
  2822. test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_CHAR, S_IFCHR,
  2823. makedev(1, 3));
  2824. }
  2825. TEST_F_FORK(layout1, make_block)
  2826. {
  2827. /* Creates a /dev/loop0 device. */
  2828. set_cap(_metadata, CAP_MKNOD);
  2829. test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_BLOCK, S_IFBLK,
  2830. makedev(7, 0));
  2831. }
  2832. TEST_F_FORK(layout1, make_reg_1)
  2833. {
  2834. test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, S_IFREG, 0);
  2835. }
  2836. TEST_F_FORK(layout1, make_reg_2)
  2837. {
  2838. test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, 0, 0);
  2839. }
  2840. TEST_F_FORK(layout1, make_sock)
  2841. {
  2842. test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_SOCK, S_IFSOCK, 0);
  2843. }
  2844. TEST_F_FORK(layout1, make_fifo)
  2845. {
  2846. test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_FIFO, S_IFIFO, 0);
  2847. }
  2848. TEST_F_FORK(layout1, make_sym)
  2849. {
  2850. const struct rule rules[] = {
  2851. {
  2852. .path = dir_s1d2,
  2853. .access = LANDLOCK_ACCESS_FS_MAKE_SYM,
  2854. },
  2855. {},
  2856. };
  2857. const int ruleset_fd =
  2858. create_ruleset(_metadata, rules[0].access, rules);
  2859. ASSERT_LE(0, ruleset_fd);
  2860. ASSERT_EQ(0, unlink(file1_s1d1));
  2861. ASSERT_EQ(0, unlink(file2_s1d1));
  2862. ASSERT_EQ(0, symlink("none", file2_s1d1));
  2863. ASSERT_EQ(0, unlink(file1_s1d2));
  2864. ASSERT_EQ(0, unlink(file2_s1d2));
  2865. ASSERT_EQ(0, unlink(file1_s1d3));
  2866. ASSERT_EQ(0, unlink(file2_s1d3));
  2867. enforce_ruleset(_metadata, ruleset_fd);
  2868. ASSERT_EQ(0, close(ruleset_fd));
  2869. ASSERT_EQ(-1, symlink("none", file1_s1d1));
  2870. ASSERT_EQ(EACCES, errno);
  2871. ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
  2872. ASSERT_EQ(EACCES, errno);
  2873. ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1));
  2874. ASSERT_EQ(EACCES, errno);
  2875. ASSERT_EQ(0, symlink("none", file1_s1d2));
  2876. ASSERT_EQ(0, link(file1_s1d2, file2_s1d2));
  2877. ASSERT_EQ(0, unlink(file2_s1d2));
  2878. ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2));
  2879. ASSERT_EQ(0, symlink("none", file1_s1d3));
  2880. ASSERT_EQ(0, link(file1_s1d3, file2_s1d3));
  2881. ASSERT_EQ(0, unlink(file2_s1d3));
  2882. ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3));
  2883. }
  2884. TEST_F_FORK(layout1, make_dir)
  2885. {
  2886. const struct rule rules[] = {
  2887. {
  2888. .path = dir_s1d2,
  2889. .access = LANDLOCK_ACCESS_FS_MAKE_DIR,
  2890. },
  2891. {},
  2892. };
  2893. const int ruleset_fd =
  2894. create_ruleset(_metadata, rules[0].access, rules);
  2895. ASSERT_LE(0, ruleset_fd);
  2896. ASSERT_EQ(0, unlink(file1_s1d1));
  2897. ASSERT_EQ(0, unlink(file1_s1d2));
  2898. ASSERT_EQ(0, unlink(file1_s1d3));
  2899. enforce_ruleset(_metadata, ruleset_fd);
  2900. ASSERT_EQ(0, close(ruleset_fd));
  2901. /* Uses file_* as directory names. */
  2902. ASSERT_EQ(-1, mkdir(file1_s1d1, 0700));
  2903. ASSERT_EQ(EACCES, errno);
  2904. ASSERT_EQ(0, mkdir(file1_s1d2, 0700));
  2905. ASSERT_EQ(0, mkdir(file1_s1d3, 0700));
  2906. }
  2907. static int open_proc_fd(struct __test_metadata *const _metadata, const int fd,
  2908. const int open_flags)
  2909. {
  2910. static const char path_template[] = "/proc/self/fd/%d";
  2911. char procfd_path[sizeof(path_template) + 10];
  2912. const int procfd_path_size =
  2913. snprintf(procfd_path, sizeof(procfd_path), path_template, fd);
  2914. ASSERT_LT(procfd_path_size, sizeof(procfd_path));
  2915. return open(procfd_path, open_flags);
  2916. }
  2917. TEST_F_FORK(layout1, proc_unlinked_file)
  2918. {
  2919. const struct rule rules[] = {
  2920. {
  2921. .path = file1_s1d2,
  2922. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  2923. },
  2924. {},
  2925. };
  2926. int reg_fd, proc_fd;
  2927. const int ruleset_fd = create_ruleset(
  2928. _metadata,
  2929. LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE,
  2930. rules);
  2931. ASSERT_LE(0, ruleset_fd);
  2932. enforce_ruleset(_metadata, ruleset_fd);
  2933. ASSERT_EQ(0, close(ruleset_fd));
  2934. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
  2935. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  2936. reg_fd = open(file1_s1d2, O_RDONLY | O_CLOEXEC);
  2937. ASSERT_LE(0, reg_fd);
  2938. ASSERT_EQ(0, unlink(file1_s1d2));
  2939. proc_fd = open_proc_fd(_metadata, reg_fd, O_RDONLY | O_CLOEXEC);
  2940. ASSERT_LE(0, proc_fd);
  2941. ASSERT_EQ(0, close(proc_fd));
  2942. proc_fd = open_proc_fd(_metadata, reg_fd, O_RDWR | O_CLOEXEC);
  2943. ASSERT_EQ(-1, proc_fd)
  2944. {
  2945. TH_LOG("Successfully opened /proc/self/fd/%d: %s", reg_fd,
  2946. strerror(errno));
  2947. }
  2948. ASSERT_EQ(EACCES, errno);
  2949. ASSERT_EQ(0, close(reg_fd));
  2950. }
  2951. TEST_F_FORK(layout1, proc_pipe)
  2952. {
  2953. int proc_fd;
  2954. int pipe_fds[2];
  2955. char buf = '\0';
  2956. const struct rule rules[] = {
  2957. {
  2958. .path = dir_s1d2,
  2959. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  2960. LANDLOCK_ACCESS_FS_WRITE_FILE,
  2961. },
  2962. {},
  2963. };
  2964. /* Limits read and write access to files tied to the filesystem. */
  2965. const int ruleset_fd =
  2966. create_ruleset(_metadata, rules[0].access, rules);
  2967. ASSERT_LE(0, ruleset_fd);
  2968. enforce_ruleset(_metadata, ruleset_fd);
  2969. ASSERT_EQ(0, close(ruleset_fd));
  2970. /* Checks enforcement for normal files. */
  2971. ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR));
  2972. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
  2973. /* Checks access to pipes through FD. */
  2974. ASSERT_EQ(0, pipe2(pipe_fds, O_CLOEXEC));
  2975. ASSERT_EQ(1, write(pipe_fds[1], ".", 1))
  2976. {
  2977. TH_LOG("Failed to write in pipe: %s", strerror(errno));
  2978. }
  2979. ASSERT_EQ(1, read(pipe_fds[0], &buf, 1));
  2980. ASSERT_EQ('.', buf);
  2981. /* Checks write access to pipe through /proc/self/fd . */
  2982. proc_fd = open_proc_fd(_metadata, pipe_fds[1], O_WRONLY | O_CLOEXEC);
  2983. ASSERT_LE(0, proc_fd);
  2984. ASSERT_EQ(1, write(proc_fd, ".", 1))
  2985. {
  2986. TH_LOG("Failed to write through /proc/self/fd/%d: %s",
  2987. pipe_fds[1], strerror(errno));
  2988. }
  2989. ASSERT_EQ(0, close(proc_fd));
  2990. /* Checks read access to pipe through /proc/self/fd . */
  2991. proc_fd = open_proc_fd(_metadata, pipe_fds[0], O_RDONLY | O_CLOEXEC);
  2992. ASSERT_LE(0, proc_fd);
  2993. buf = '\0';
  2994. ASSERT_EQ(1, read(proc_fd, &buf, 1))
  2995. {
  2996. TH_LOG("Failed to read through /proc/self/fd/%d: %s",
  2997. pipe_fds[1], strerror(errno));
  2998. }
  2999. ASSERT_EQ(0, close(proc_fd));
  3000. ASSERT_EQ(0, close(pipe_fds[0]));
  3001. ASSERT_EQ(0, close(pipe_fds[1]));
  3002. }
  3003. /* Invokes truncate(2) and returns its errno or 0. */
  3004. static int test_truncate(const char *const path)
  3005. {
  3006. if (truncate(path, 10) < 0)
  3007. return errno;
  3008. return 0;
  3009. }
  3010. /*
  3011. * Invokes creat(2) and returns its errno or 0.
  3012. * Closes the opened file descriptor on success.
  3013. */
  3014. static int test_creat(const char *const path)
  3015. {
  3016. int fd = creat(path, 0600);
  3017. if (fd < 0)
  3018. return errno;
  3019. /*
  3020. * Mixing error codes from close(2) and creat(2) should not lead to any
  3021. * (access type) confusion for this test.
  3022. */
  3023. if (close(fd) < 0)
  3024. return errno;
  3025. return 0;
  3026. }
  3027. /*
  3028. * Exercises file truncation when it's not restricted,
  3029. * as it was the case before LANDLOCK_ACCESS_FS_TRUNCATE existed.
  3030. */
  3031. TEST_F_FORK(layout1, truncate_unhandled)
  3032. {
  3033. const char *const file_r = file1_s1d1;
  3034. const char *const file_w = file2_s1d1;
  3035. const char *const file_none = file1_s1d2;
  3036. const struct rule rules[] = {
  3037. {
  3038. .path = file_r,
  3039. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  3040. },
  3041. {
  3042. .path = file_w,
  3043. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3044. },
  3045. /* Implicitly: No rights for file_none. */
  3046. {},
  3047. };
  3048. const __u64 handled = LANDLOCK_ACCESS_FS_READ_FILE |
  3049. LANDLOCK_ACCESS_FS_WRITE_FILE;
  3050. int ruleset_fd;
  3051. /* Enables Landlock. */
  3052. ruleset_fd = create_ruleset(_metadata, handled, rules);
  3053. ASSERT_LE(0, ruleset_fd);
  3054. enforce_ruleset(_metadata, ruleset_fd);
  3055. ASSERT_EQ(0, close(ruleset_fd));
  3056. /*
  3057. * Checks read right: truncate and open with O_TRUNC work, unless the
  3058. * file is attempted to be opened for writing.
  3059. */
  3060. EXPECT_EQ(0, test_truncate(file_r));
  3061. EXPECT_EQ(0, test_open(file_r, O_RDONLY | O_TRUNC));
  3062. EXPECT_EQ(EACCES, test_open(file_r, O_WRONLY | O_TRUNC));
  3063. EXPECT_EQ(EACCES, test_creat(file_r));
  3064. /*
  3065. * Checks write right: truncate and open with O_TRUNC work, unless the
  3066. * file is attempted to be opened for reading.
  3067. */
  3068. EXPECT_EQ(0, test_truncate(file_w));
  3069. EXPECT_EQ(EACCES, test_open(file_w, O_RDONLY | O_TRUNC));
  3070. EXPECT_EQ(0, test_open(file_w, O_WRONLY | O_TRUNC));
  3071. EXPECT_EQ(0, test_creat(file_w));
  3072. /*
  3073. * Checks "no rights" case: truncate works but all open attempts fail,
  3074. * including creat.
  3075. */
  3076. EXPECT_EQ(0, test_truncate(file_none));
  3077. EXPECT_EQ(EACCES, test_open(file_none, O_RDONLY | O_TRUNC));
  3078. EXPECT_EQ(EACCES, test_open(file_none, O_WRONLY | O_TRUNC));
  3079. EXPECT_EQ(EACCES, test_creat(file_none));
  3080. }
  3081. TEST_F_FORK(layout1, truncate)
  3082. {
  3083. const char *const file_rwt = file1_s1d1;
  3084. const char *const file_rw = file2_s1d1;
  3085. const char *const file_rt = file1_s1d2;
  3086. const char *const file_t = file2_s1d2;
  3087. const char *const file_none = file1_s1d3;
  3088. const char *const dir_t = dir_s2d1;
  3089. const char *const file_in_dir_t = file1_s2d1;
  3090. const char *const dir_w = dir_s3d1;
  3091. const char *const file_in_dir_w = file1_s3d1;
  3092. const struct rule rules[] = {
  3093. {
  3094. .path = file_rwt,
  3095. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  3096. LANDLOCK_ACCESS_FS_WRITE_FILE |
  3097. LANDLOCK_ACCESS_FS_TRUNCATE,
  3098. },
  3099. {
  3100. .path = file_rw,
  3101. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  3102. LANDLOCK_ACCESS_FS_WRITE_FILE,
  3103. },
  3104. {
  3105. .path = file_rt,
  3106. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  3107. LANDLOCK_ACCESS_FS_TRUNCATE,
  3108. },
  3109. {
  3110. .path = file_t,
  3111. .access = LANDLOCK_ACCESS_FS_TRUNCATE,
  3112. },
  3113. /* Implicitly: No access rights for file_none. */
  3114. {
  3115. .path = dir_t,
  3116. .access = LANDLOCK_ACCESS_FS_TRUNCATE,
  3117. },
  3118. {
  3119. .path = dir_w,
  3120. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3121. },
  3122. {},
  3123. };
  3124. const __u64 handled = LANDLOCK_ACCESS_FS_READ_FILE |
  3125. LANDLOCK_ACCESS_FS_WRITE_FILE |
  3126. LANDLOCK_ACCESS_FS_TRUNCATE;
  3127. int ruleset_fd;
  3128. /* Enables Landlock. */
  3129. ruleset_fd = create_ruleset(_metadata, handled, rules);
  3130. ASSERT_LE(0, ruleset_fd);
  3131. enforce_ruleset(_metadata, ruleset_fd);
  3132. ASSERT_EQ(0, close(ruleset_fd));
  3133. /* Checks read, write and truncate rights: truncation works. */
  3134. EXPECT_EQ(0, test_truncate(file_rwt));
  3135. EXPECT_EQ(0, test_open(file_rwt, O_RDONLY | O_TRUNC));
  3136. EXPECT_EQ(0, test_open(file_rwt, O_WRONLY | O_TRUNC));
  3137. /* Checks read and write rights: no truncate variant works. */
  3138. EXPECT_EQ(EACCES, test_truncate(file_rw));
  3139. EXPECT_EQ(EACCES, test_open(file_rw, O_RDONLY | O_TRUNC));
  3140. EXPECT_EQ(EACCES, test_open(file_rw, O_WRONLY | O_TRUNC));
  3141. /*
  3142. * Checks read and truncate rights: truncation works.
  3143. *
  3144. * Note: Files can get truncated using open() even with O_RDONLY.
  3145. */
  3146. EXPECT_EQ(0, test_truncate(file_rt));
  3147. EXPECT_EQ(0, test_open(file_rt, O_RDONLY | O_TRUNC));
  3148. EXPECT_EQ(EACCES, test_open(file_rt, O_WRONLY | O_TRUNC));
  3149. /* Checks truncate right: truncate works, but can't open file. */
  3150. EXPECT_EQ(0, test_truncate(file_t));
  3151. EXPECT_EQ(EACCES, test_open(file_t, O_RDONLY | O_TRUNC));
  3152. EXPECT_EQ(EACCES, test_open(file_t, O_WRONLY | O_TRUNC));
  3153. /* Checks "no rights" case: No form of truncation works. */
  3154. EXPECT_EQ(EACCES, test_truncate(file_none));
  3155. EXPECT_EQ(EACCES, test_open(file_none, O_RDONLY | O_TRUNC));
  3156. EXPECT_EQ(EACCES, test_open(file_none, O_WRONLY | O_TRUNC));
  3157. /*
  3158. * Checks truncate right on directory: truncate works on contained
  3159. * files.
  3160. */
  3161. EXPECT_EQ(0, test_truncate(file_in_dir_t));
  3162. EXPECT_EQ(EACCES, test_open(file_in_dir_t, O_RDONLY | O_TRUNC));
  3163. EXPECT_EQ(EACCES, test_open(file_in_dir_t, O_WRONLY | O_TRUNC));
  3164. /*
  3165. * Checks creat in dir_w: This requires the truncate right when
  3166. * overwriting an existing file, but does not require it when the file
  3167. * is new.
  3168. */
  3169. EXPECT_EQ(EACCES, test_creat(file_in_dir_w));
  3170. ASSERT_EQ(0, unlink(file_in_dir_w));
  3171. EXPECT_EQ(0, test_creat(file_in_dir_w));
  3172. }
  3173. /* Invokes ftruncate(2) and returns its errno or 0. */
  3174. static int test_ftruncate(int fd)
  3175. {
  3176. if (ftruncate(fd, 10) < 0)
  3177. return errno;
  3178. return 0;
  3179. }
  3180. TEST_F_FORK(layout1, ftruncate)
  3181. {
  3182. /*
  3183. * This test opens a new file descriptor at different stages of
  3184. * Landlock restriction:
  3185. *
  3186. * without restriction: ftruncate works
  3187. * something else but truncate restricted: ftruncate works
  3188. * truncate restricted and permitted: ftruncate works
  3189. * truncate restricted and not permitted: ftruncate fails
  3190. *
  3191. * Whether this works or not is expected to depend on the time when the
  3192. * FD was opened, not to depend on the time when ftruncate() was
  3193. * called.
  3194. */
  3195. const char *const path = file1_s1d1;
  3196. const __u64 handled1 = LANDLOCK_ACCESS_FS_READ_FILE |
  3197. LANDLOCK_ACCESS_FS_WRITE_FILE;
  3198. const struct rule layer1[] = {
  3199. {
  3200. .path = path,
  3201. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3202. },
  3203. {},
  3204. };
  3205. const __u64 handled2 = LANDLOCK_ACCESS_FS_TRUNCATE;
  3206. const struct rule layer2[] = {
  3207. {
  3208. .path = path,
  3209. .access = LANDLOCK_ACCESS_FS_TRUNCATE,
  3210. },
  3211. {},
  3212. };
  3213. const __u64 handled3 = LANDLOCK_ACCESS_FS_TRUNCATE |
  3214. LANDLOCK_ACCESS_FS_WRITE_FILE;
  3215. const struct rule layer3[] = {
  3216. {
  3217. .path = path,
  3218. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3219. },
  3220. {},
  3221. };
  3222. int fd_layer0, fd_layer1, fd_layer2, fd_layer3, ruleset_fd;
  3223. fd_layer0 = open(path, O_WRONLY);
  3224. EXPECT_EQ(0, test_ftruncate(fd_layer0));
  3225. ruleset_fd = create_ruleset(_metadata, handled1, layer1);
  3226. ASSERT_LE(0, ruleset_fd);
  3227. enforce_ruleset(_metadata, ruleset_fd);
  3228. ASSERT_EQ(0, close(ruleset_fd));
  3229. fd_layer1 = open(path, O_WRONLY);
  3230. EXPECT_EQ(0, test_ftruncate(fd_layer0));
  3231. EXPECT_EQ(0, test_ftruncate(fd_layer1));
  3232. ruleset_fd = create_ruleset(_metadata, handled2, layer2);
  3233. ASSERT_LE(0, ruleset_fd);
  3234. enforce_ruleset(_metadata, ruleset_fd);
  3235. ASSERT_EQ(0, close(ruleset_fd));
  3236. fd_layer2 = open(path, O_WRONLY);
  3237. EXPECT_EQ(0, test_ftruncate(fd_layer0));
  3238. EXPECT_EQ(0, test_ftruncate(fd_layer1));
  3239. EXPECT_EQ(0, test_ftruncate(fd_layer2));
  3240. ruleset_fd = create_ruleset(_metadata, handled3, layer3);
  3241. ASSERT_LE(0, ruleset_fd);
  3242. enforce_ruleset(_metadata, ruleset_fd);
  3243. ASSERT_EQ(0, close(ruleset_fd));
  3244. fd_layer3 = open(path, O_WRONLY);
  3245. EXPECT_EQ(0, test_ftruncate(fd_layer0));
  3246. EXPECT_EQ(0, test_ftruncate(fd_layer1));
  3247. EXPECT_EQ(0, test_ftruncate(fd_layer2));
  3248. EXPECT_EQ(EACCES, test_ftruncate(fd_layer3));
  3249. ASSERT_EQ(0, close(fd_layer0));
  3250. ASSERT_EQ(0, close(fd_layer1));
  3251. ASSERT_EQ(0, close(fd_layer2));
  3252. ASSERT_EQ(0, close(fd_layer3));
  3253. }
  3254. /* clang-format off */
  3255. FIXTURE(ftruncate) {};
  3256. /* clang-format on */
  3257. FIXTURE_SETUP(ftruncate)
  3258. {
  3259. prepare_layout(_metadata);
  3260. create_file(_metadata, file1_s1d1);
  3261. }
  3262. FIXTURE_TEARDOWN_PARENT(ftruncate)
  3263. {
  3264. EXPECT_EQ(0, remove_path(file1_s1d1));
  3265. cleanup_layout(_metadata);
  3266. }
  3267. FIXTURE_VARIANT(ftruncate)
  3268. {
  3269. const __u64 handled;
  3270. const __u64 allowed;
  3271. const int expected_open_result;
  3272. const int expected_ftruncate_result;
  3273. };
  3274. /* clang-format off */
  3275. FIXTURE_VARIANT_ADD(ftruncate, w_w) {
  3276. /* clang-format on */
  3277. .handled = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3278. .allowed = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3279. .expected_open_result = 0,
  3280. .expected_ftruncate_result = 0,
  3281. };
  3282. /* clang-format off */
  3283. FIXTURE_VARIANT_ADD(ftruncate, t_t) {
  3284. /* clang-format on */
  3285. .handled = LANDLOCK_ACCESS_FS_TRUNCATE,
  3286. .allowed = LANDLOCK_ACCESS_FS_TRUNCATE,
  3287. .expected_open_result = 0,
  3288. .expected_ftruncate_result = 0,
  3289. };
  3290. /* clang-format off */
  3291. FIXTURE_VARIANT_ADD(ftruncate, wt_w) {
  3292. /* clang-format on */
  3293. .handled = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE,
  3294. .allowed = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3295. .expected_open_result = 0,
  3296. .expected_ftruncate_result = EACCES,
  3297. };
  3298. /* clang-format off */
  3299. FIXTURE_VARIANT_ADD(ftruncate, wt_wt) {
  3300. /* clang-format on */
  3301. .handled = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE,
  3302. .allowed = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE,
  3303. .expected_open_result = 0,
  3304. .expected_ftruncate_result = 0,
  3305. };
  3306. /* clang-format off */
  3307. FIXTURE_VARIANT_ADD(ftruncate, wt_t) {
  3308. /* clang-format on */
  3309. .handled = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_TRUNCATE,
  3310. .allowed = LANDLOCK_ACCESS_FS_TRUNCATE,
  3311. .expected_open_result = EACCES,
  3312. };
  3313. TEST_F_FORK(ftruncate, open_and_ftruncate)
  3314. {
  3315. const char *const path = file1_s1d1;
  3316. const struct rule rules[] = {
  3317. {
  3318. .path = path,
  3319. .access = variant->allowed,
  3320. },
  3321. {},
  3322. };
  3323. int fd, ruleset_fd;
  3324. /* Enables Landlock. */
  3325. ruleset_fd = create_ruleset(_metadata, variant->handled, rules);
  3326. ASSERT_LE(0, ruleset_fd);
  3327. enforce_ruleset(_metadata, ruleset_fd);
  3328. ASSERT_EQ(0, close(ruleset_fd));
  3329. fd = open(path, O_WRONLY);
  3330. EXPECT_EQ(variant->expected_open_result, (fd < 0 ? errno : 0));
  3331. if (fd >= 0) {
  3332. EXPECT_EQ(variant->expected_ftruncate_result,
  3333. test_ftruncate(fd));
  3334. ASSERT_EQ(0, close(fd));
  3335. }
  3336. }
  3337. TEST_F_FORK(ftruncate, open_and_ftruncate_in_different_processes)
  3338. {
  3339. int child, fd, status;
  3340. int socket_fds[2];
  3341. ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
  3342. socket_fds));
  3343. child = fork();
  3344. ASSERT_LE(0, child);
  3345. if (child == 0) {
  3346. /*
  3347. * Enables Landlock in the child process, open a file descriptor
  3348. * where truncation is forbidden and send it to the
  3349. * non-landlocked parent process.
  3350. */
  3351. const char *const path = file1_s1d1;
  3352. const struct rule rules[] = {
  3353. {
  3354. .path = path,
  3355. .access = variant->allowed,
  3356. },
  3357. {},
  3358. };
  3359. int fd, ruleset_fd;
  3360. ruleset_fd = create_ruleset(_metadata, variant->handled, rules);
  3361. ASSERT_LE(0, ruleset_fd);
  3362. enforce_ruleset(_metadata, ruleset_fd);
  3363. ASSERT_EQ(0, close(ruleset_fd));
  3364. fd = open(path, O_WRONLY);
  3365. ASSERT_EQ(variant->expected_open_result, (fd < 0 ? errno : 0));
  3366. if (fd >= 0) {
  3367. ASSERT_EQ(0, send_fd(socket_fds[0], fd));
  3368. ASSERT_EQ(0, close(fd));
  3369. }
  3370. ASSERT_EQ(0, close(socket_fds[0]));
  3371. _exit(_metadata->exit_code);
  3372. return;
  3373. }
  3374. if (variant->expected_open_result == 0) {
  3375. fd = recv_fd(socket_fds[1]);
  3376. ASSERT_LE(0, fd);
  3377. EXPECT_EQ(variant->expected_ftruncate_result,
  3378. test_ftruncate(fd));
  3379. ASSERT_EQ(0, close(fd));
  3380. }
  3381. ASSERT_EQ(child, waitpid(child, &status, 0));
  3382. ASSERT_EQ(1, WIFEXITED(status));
  3383. ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
  3384. ASSERT_EQ(0, close(socket_fds[0]));
  3385. ASSERT_EQ(0, close(socket_fds[1]));
  3386. }
  3387. /* Invokes the FS_IOC_GETFLAGS IOCTL and returns its errno or 0. */
  3388. static int test_fs_ioc_getflags_ioctl(int fd)
  3389. {
  3390. uint32_t flags;
  3391. if (ioctl(fd, FS_IOC_GETFLAGS, &flags) < 0)
  3392. return errno;
  3393. return 0;
  3394. }
  3395. TEST(memfd_ftruncate_and_ioctl)
  3396. {
  3397. const struct landlock_ruleset_attr attr = {
  3398. .handled_access_fs = ACCESS_ALL,
  3399. };
  3400. int ruleset_fd, fd, i;
  3401. /*
  3402. * We exercise the same test both with and without Landlock enabled, to
  3403. * ensure that it behaves the same in both cases.
  3404. */
  3405. for (i = 0; i < 2; i++) {
  3406. /* Creates a new memfd. */
  3407. fd = memfd_create("name", MFD_CLOEXEC);
  3408. ASSERT_LE(0, fd);
  3409. /*
  3410. * Checks that operations associated with the opened file
  3411. * (ftruncate, ioctl) are permitted on file descriptors that are
  3412. * created in ways other than open(2).
  3413. */
  3414. EXPECT_EQ(0, test_ftruncate(fd));
  3415. EXPECT_EQ(0, test_fs_ioc_getflags_ioctl(fd));
  3416. ASSERT_EQ(0, close(fd));
  3417. /* Enables Landlock. */
  3418. ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
  3419. ASSERT_LE(0, ruleset_fd);
  3420. enforce_ruleset(_metadata, ruleset_fd);
  3421. ASSERT_EQ(0, close(ruleset_fd));
  3422. }
  3423. }
  3424. static int test_fionread_ioctl(int fd)
  3425. {
  3426. size_t sz = 0;
  3427. if (ioctl(fd, FIONREAD, &sz) < 0 && errno == EACCES)
  3428. return errno;
  3429. return 0;
  3430. }
  3431. TEST_F_FORK(layout1, o_path_ftruncate_and_ioctl)
  3432. {
  3433. const struct landlock_ruleset_attr attr = {
  3434. .handled_access_fs = ACCESS_ALL,
  3435. };
  3436. int ruleset_fd, fd;
  3437. /*
  3438. * Checks that for files opened with O_PATH, both ioctl(2) and
  3439. * ftruncate(2) yield EBADF, as it is documented in open(2) for the
  3440. * O_PATH flag.
  3441. */
  3442. fd = open(dir_s1d1, O_PATH | O_CLOEXEC);
  3443. ASSERT_LE(0, fd);
  3444. EXPECT_EQ(EBADF, test_ftruncate(fd));
  3445. EXPECT_EQ(EBADF, test_fs_ioc_getflags_ioctl(fd));
  3446. ASSERT_EQ(0, close(fd));
  3447. /* Enables Landlock. */
  3448. ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
  3449. ASSERT_LE(0, ruleset_fd);
  3450. enforce_ruleset(_metadata, ruleset_fd);
  3451. ASSERT_EQ(0, close(ruleset_fd));
  3452. /*
  3453. * Checks that after enabling Landlock,
  3454. * - the file can still be opened with O_PATH
  3455. * - both ioctl and truncate still yield EBADF (not EACCES).
  3456. */
  3457. fd = open(dir_s1d1, O_PATH | O_CLOEXEC);
  3458. ASSERT_LE(0, fd);
  3459. EXPECT_EQ(EBADF, test_ftruncate(fd));
  3460. EXPECT_EQ(EBADF, test_fs_ioc_getflags_ioctl(fd));
  3461. ASSERT_EQ(0, close(fd));
  3462. }
  3463. /*
  3464. * ioctl_error - generically call the given ioctl with a pointer to a
  3465. * sufficiently large zeroed-out memory region.
  3466. *
  3467. * Returns the IOCTLs error, or 0.
  3468. */
  3469. static int ioctl_error(struct __test_metadata *const _metadata, int fd,
  3470. unsigned int cmd)
  3471. {
  3472. char buf[128]; /* sufficiently large */
  3473. int res, stdinbak_fd;
  3474. /*
  3475. * Depending on the IOCTL command, parts of the zeroed-out buffer might
  3476. * be interpreted as file descriptor numbers. We do not want to
  3477. * accidentally operate on file descriptor 0 (stdin), so we temporarily
  3478. * move stdin to a different FD and close FD 0 for the IOCTL call.
  3479. */
  3480. stdinbak_fd = dup(0);
  3481. ASSERT_LT(0, stdinbak_fd);
  3482. ASSERT_EQ(0, close(0));
  3483. /* Invokes the IOCTL with a zeroed-out buffer. */
  3484. bzero(&buf, sizeof(buf));
  3485. res = ioctl(fd, cmd, &buf);
  3486. /* Restores the old FD 0 and closes the backup FD. */
  3487. ASSERT_EQ(0, dup2(stdinbak_fd, 0));
  3488. ASSERT_EQ(0, close(stdinbak_fd));
  3489. if (res < 0)
  3490. return errno;
  3491. return 0;
  3492. }
  3493. /* Define some linux/falloc.h IOCTL commands which are not available in uapi headers. */
  3494. struct space_resv {
  3495. __s16 l_type;
  3496. __s16 l_whence;
  3497. __s64 l_start;
  3498. __s64 l_len; /* len == 0 means until end of file */
  3499. __s32 l_sysid;
  3500. __u32 l_pid;
  3501. __s32 l_pad[4]; /* reserved area */
  3502. };
  3503. #define FS_IOC_RESVSP _IOW('X', 40, struct space_resv)
  3504. #define FS_IOC_UNRESVSP _IOW('X', 41, struct space_resv)
  3505. #define FS_IOC_RESVSP64 _IOW('X', 42, struct space_resv)
  3506. #define FS_IOC_UNRESVSP64 _IOW('X', 43, struct space_resv)
  3507. #define FS_IOC_ZERO_RANGE _IOW('X', 57, struct space_resv)
  3508. /*
  3509. * Tests a series of blanket-permitted and denied IOCTLs.
  3510. */
  3511. TEST_F_FORK(layout1, blanket_permitted_ioctls)
  3512. {
  3513. const struct landlock_ruleset_attr attr = {
  3514. .handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV,
  3515. };
  3516. int ruleset_fd, fd;
  3517. /* Enables Landlock. */
  3518. ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
  3519. ASSERT_LE(0, ruleset_fd);
  3520. enforce_ruleset(_metadata, ruleset_fd);
  3521. ASSERT_EQ(0, close(ruleset_fd));
  3522. fd = open("/dev/null", O_RDWR | O_CLOEXEC);
  3523. ASSERT_LE(0, fd);
  3524. /*
  3525. * Checks permitted commands.
  3526. * These ones may return errors, but should not be blocked by Landlock.
  3527. */
  3528. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIOCLEX));
  3529. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIONCLEX));
  3530. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIONBIO));
  3531. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIOASYNC));
  3532. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIOQSIZE));
  3533. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIFREEZE));
  3534. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FITHAW));
  3535. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FS_IOC_FIEMAP));
  3536. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIGETBSZ));
  3537. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FICLONE));
  3538. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FICLONERANGE));
  3539. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FIDEDUPERANGE));
  3540. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FS_IOC_GETFSUUID));
  3541. EXPECT_NE(EACCES, ioctl_error(_metadata, fd, FS_IOC_GETFSSYSFSPATH));
  3542. /*
  3543. * Checks blocked commands.
  3544. * A call to a blocked IOCTL command always returns EACCES.
  3545. */
  3546. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FIONREAD));
  3547. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_GETFLAGS));
  3548. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_SETFLAGS));
  3549. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_FSGETXATTR));
  3550. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_FSSETXATTR));
  3551. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FIBMAP));
  3552. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_RESVSP));
  3553. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_RESVSP64));
  3554. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_UNRESVSP));
  3555. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_UNRESVSP64));
  3556. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, FS_IOC_ZERO_RANGE));
  3557. /* Default case is also blocked. */
  3558. EXPECT_EQ(EACCES, ioctl_error(_metadata, fd, 0xc00ffeee));
  3559. ASSERT_EQ(0, close(fd));
  3560. }
  3561. /*
  3562. * Named pipes are not governed by the LANDLOCK_ACCESS_FS_IOCTL_DEV right,
  3563. * because they are not character or block devices.
  3564. */
  3565. TEST_F_FORK(layout1, named_pipe_ioctl)
  3566. {
  3567. pid_t child_pid;
  3568. int fd, ruleset_fd;
  3569. const char *const path = file1_s1d1;
  3570. const struct landlock_ruleset_attr attr = {
  3571. .handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV,
  3572. };
  3573. ASSERT_EQ(0, unlink(path));
  3574. ASSERT_EQ(0, mkfifo(path, 0600));
  3575. /* Enables Landlock. */
  3576. ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
  3577. ASSERT_LE(0, ruleset_fd);
  3578. enforce_ruleset(_metadata, ruleset_fd);
  3579. ASSERT_EQ(0, close(ruleset_fd));
  3580. /* The child process opens the pipe for writing. */
  3581. child_pid = fork();
  3582. ASSERT_NE(-1, child_pid);
  3583. if (child_pid == 0) {
  3584. fd = open(path, O_WRONLY);
  3585. close(fd);
  3586. exit(0);
  3587. }
  3588. fd = open(path, O_RDONLY);
  3589. ASSERT_LE(0, fd);
  3590. /* FIONREAD is implemented by pipefifo_fops. */
  3591. EXPECT_EQ(0, test_fionread_ioctl(fd));
  3592. ASSERT_EQ(0, close(fd));
  3593. ASSERT_EQ(0, unlink(path));
  3594. ASSERT_EQ(child_pid, waitpid(child_pid, NULL, 0));
  3595. }
  3596. /* For named UNIX domain sockets, no IOCTL restrictions apply. */
  3597. TEST_F_FORK(layout1, named_unix_domain_socket_ioctl)
  3598. {
  3599. const char *const path = file1_s1d1;
  3600. int srv_fd, cli_fd, ruleset_fd;
  3601. socklen_t size;
  3602. struct sockaddr_un srv_un, cli_un;
  3603. const struct landlock_ruleset_attr attr = {
  3604. .handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV,
  3605. };
  3606. /* Sets up a server */
  3607. srv_un.sun_family = AF_UNIX;
  3608. strncpy(srv_un.sun_path, path, sizeof(srv_un.sun_path));
  3609. ASSERT_EQ(0, unlink(path));
  3610. srv_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  3611. ASSERT_LE(0, srv_fd);
  3612. size = offsetof(struct sockaddr_un, sun_path) + strlen(srv_un.sun_path);
  3613. ASSERT_EQ(0, bind(srv_fd, (struct sockaddr *)&srv_un, size));
  3614. ASSERT_EQ(0, listen(srv_fd, 10 /* qlen */));
  3615. /* Enables Landlock. */
  3616. ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
  3617. ASSERT_LE(0, ruleset_fd);
  3618. enforce_ruleset(_metadata, ruleset_fd);
  3619. ASSERT_EQ(0, close(ruleset_fd));
  3620. /* Sets up a client connection to it */
  3621. cli_un.sun_family = AF_UNIX;
  3622. cli_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  3623. ASSERT_LE(0, cli_fd);
  3624. size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path);
  3625. ASSERT_EQ(0, bind(cli_fd, (struct sockaddr *)&cli_un, size));
  3626. bzero(&cli_un, sizeof(cli_un));
  3627. cli_un.sun_family = AF_UNIX;
  3628. strncpy(cli_un.sun_path, path, sizeof(cli_un.sun_path));
  3629. size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path);
  3630. ASSERT_EQ(0, connect(cli_fd, (struct sockaddr *)&cli_un, size));
  3631. /* FIONREAD and other IOCTLs should not be forbidden. */
  3632. EXPECT_EQ(0, test_fionread_ioctl(cli_fd));
  3633. ASSERT_EQ(0, close(cli_fd));
  3634. }
  3635. /* clang-format off */
  3636. FIXTURE(ioctl) {};
  3637. FIXTURE_SETUP(ioctl) {};
  3638. FIXTURE_TEARDOWN(ioctl) {};
  3639. /* clang-format on */
  3640. FIXTURE_VARIANT(ioctl)
  3641. {
  3642. const __u64 handled;
  3643. const __u64 allowed;
  3644. const mode_t open_mode;
  3645. /*
  3646. * FIONREAD is used as a characteristic device-specific IOCTL command.
  3647. * It is implemented in fs/ioctl.c for regular files,
  3648. * but we do not blanket-permit it for devices.
  3649. */
  3650. const int expected_fionread_result;
  3651. };
  3652. /* clang-format off */
  3653. FIXTURE_VARIANT_ADD(ioctl, handled_i_allowed_none) {
  3654. /* clang-format on */
  3655. .handled = LANDLOCK_ACCESS_FS_IOCTL_DEV,
  3656. .allowed = 0,
  3657. .open_mode = O_RDWR,
  3658. .expected_fionread_result = EACCES,
  3659. };
  3660. /* clang-format off */
  3661. FIXTURE_VARIANT_ADD(ioctl, handled_i_allowed_i) {
  3662. /* clang-format on */
  3663. .handled = LANDLOCK_ACCESS_FS_IOCTL_DEV,
  3664. .allowed = LANDLOCK_ACCESS_FS_IOCTL_DEV,
  3665. .open_mode = O_RDWR,
  3666. .expected_fionread_result = 0,
  3667. };
  3668. /* clang-format off */
  3669. FIXTURE_VARIANT_ADD(ioctl, unhandled) {
  3670. /* clang-format on */
  3671. .handled = LANDLOCK_ACCESS_FS_EXECUTE,
  3672. .allowed = LANDLOCK_ACCESS_FS_EXECUTE,
  3673. .open_mode = O_RDWR,
  3674. .expected_fionread_result = 0,
  3675. };
  3676. TEST_F_FORK(ioctl, handle_dir_access_file)
  3677. {
  3678. const int flag = 0;
  3679. const struct rule rules[] = {
  3680. {
  3681. .path = "/dev",
  3682. .access = variant->allowed,
  3683. },
  3684. {},
  3685. };
  3686. int file_fd, ruleset_fd;
  3687. /* Enables Landlock. */
  3688. ruleset_fd = create_ruleset(_metadata, variant->handled, rules);
  3689. ASSERT_LE(0, ruleset_fd);
  3690. enforce_ruleset(_metadata, ruleset_fd);
  3691. ASSERT_EQ(0, close(ruleset_fd));
  3692. file_fd = open("/dev/zero", variant->open_mode);
  3693. ASSERT_LE(0, file_fd);
  3694. /* Checks that IOCTL commands return the expected errors. */
  3695. EXPECT_EQ(variant->expected_fionread_result,
  3696. test_fionread_ioctl(file_fd));
  3697. /* Checks that unrestrictable commands are unrestricted. */
  3698. EXPECT_EQ(0, ioctl(file_fd, FIOCLEX));
  3699. EXPECT_EQ(0, ioctl(file_fd, FIONCLEX));
  3700. EXPECT_EQ(0, ioctl(file_fd, FIONBIO, &flag));
  3701. EXPECT_EQ(0, ioctl(file_fd, FIOASYNC, &flag));
  3702. EXPECT_EQ(0, ioctl(file_fd, FIGETBSZ, &flag));
  3703. ASSERT_EQ(0, close(file_fd));
  3704. }
  3705. TEST_F_FORK(ioctl, handle_dir_access_dir)
  3706. {
  3707. const int flag = 0;
  3708. const struct rule rules[] = {
  3709. {
  3710. .path = "/dev",
  3711. .access = variant->allowed,
  3712. },
  3713. {},
  3714. };
  3715. int dir_fd, ruleset_fd;
  3716. /* Enables Landlock. */
  3717. ruleset_fd = create_ruleset(_metadata, variant->handled, rules);
  3718. ASSERT_LE(0, ruleset_fd);
  3719. enforce_ruleset(_metadata, ruleset_fd);
  3720. ASSERT_EQ(0, close(ruleset_fd));
  3721. /*
  3722. * Ignore variant->open_mode for this test, as we intend to open a
  3723. * directory. If the directory can not be opened, the variant is
  3724. * infeasible to test with an opened directory.
  3725. */
  3726. dir_fd = open("/dev", O_RDONLY);
  3727. if (dir_fd < 0)
  3728. return;
  3729. /*
  3730. * Checks that IOCTL commands return the expected errors.
  3731. * We do not use the expected values from the fixture here.
  3732. *
  3733. * When using IOCTL on a directory, no Landlock restrictions apply.
  3734. */
  3735. EXPECT_EQ(0, test_fionread_ioctl(dir_fd));
  3736. /* Checks that unrestrictable commands are unrestricted. */
  3737. EXPECT_EQ(0, ioctl(dir_fd, FIOCLEX));
  3738. EXPECT_EQ(0, ioctl(dir_fd, FIONCLEX));
  3739. EXPECT_EQ(0, ioctl(dir_fd, FIONBIO, &flag));
  3740. EXPECT_EQ(0, ioctl(dir_fd, FIOASYNC, &flag));
  3741. EXPECT_EQ(0, ioctl(dir_fd, FIGETBSZ, &flag));
  3742. ASSERT_EQ(0, close(dir_fd));
  3743. }
  3744. TEST_F_FORK(ioctl, handle_file_access_file)
  3745. {
  3746. const int flag = 0;
  3747. const struct rule rules[] = {
  3748. {
  3749. .path = "/dev/zero",
  3750. .access = variant->allowed,
  3751. },
  3752. {},
  3753. };
  3754. int file_fd, ruleset_fd;
  3755. /* Enables Landlock. */
  3756. ruleset_fd = create_ruleset(_metadata, variant->handled, rules);
  3757. ASSERT_LE(0, ruleset_fd);
  3758. enforce_ruleset(_metadata, ruleset_fd);
  3759. ASSERT_EQ(0, close(ruleset_fd));
  3760. file_fd = open("/dev/zero", variant->open_mode);
  3761. ASSERT_LE(0, file_fd)
  3762. {
  3763. TH_LOG("Failed to open /dev/zero: %s", strerror(errno));
  3764. }
  3765. /* Checks that IOCTL commands return the expected errors. */
  3766. EXPECT_EQ(variant->expected_fionread_result,
  3767. test_fionread_ioctl(file_fd));
  3768. /* Checks that unrestrictable commands are unrestricted. */
  3769. EXPECT_EQ(0, ioctl(file_fd, FIOCLEX));
  3770. EXPECT_EQ(0, ioctl(file_fd, FIONCLEX));
  3771. EXPECT_EQ(0, ioctl(file_fd, FIONBIO, &flag));
  3772. EXPECT_EQ(0, ioctl(file_fd, FIOASYNC, &flag));
  3773. EXPECT_EQ(0, ioctl(file_fd, FIGETBSZ, &flag));
  3774. ASSERT_EQ(0, close(file_fd));
  3775. }
  3776. /* clang-format off */
  3777. FIXTURE(layout1_bind) {};
  3778. /* clang-format on */
  3779. FIXTURE_SETUP(layout1_bind)
  3780. {
  3781. prepare_layout(_metadata);
  3782. create_layout1(_metadata);
  3783. set_cap(_metadata, CAP_SYS_ADMIN);
  3784. ASSERT_EQ(0, mount(dir_s1d2, dir_s2d2, NULL, MS_BIND, NULL));
  3785. clear_cap(_metadata, CAP_SYS_ADMIN);
  3786. }
  3787. FIXTURE_TEARDOWN_PARENT(layout1_bind)
  3788. {
  3789. /* umount(dir_s2d2)) is handled by namespace lifetime. */
  3790. remove_layout1(_metadata);
  3791. cleanup_layout(_metadata);
  3792. }
  3793. static const char bind_dir_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3";
  3794. static const char bind_file1_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3/f1";
  3795. /*
  3796. * layout1_bind hierarchy:
  3797. *
  3798. * tmp
  3799. * ├── s1d1
  3800. * │   ├── f1
  3801. * │   ├── f2
  3802. * │   └── s1d2
  3803. * │   ├── f1
  3804. * │   ├── f2
  3805. * │   └── s1d3
  3806. * │   ├── f1
  3807. * │   └── f2
  3808. * ├── s2d1
  3809. * │   ├── f1
  3810. * │   └── s2d2
  3811. * │   ├── f1
  3812. * │   ├── f2
  3813. * │   └── s1d3
  3814. * │   ├── f1
  3815. * │   └── f2
  3816. * └── s3d1
  3817. * └── s3d2
  3818. * └── s3d3
  3819. */
  3820. TEST_F_FORK(layout1_bind, no_restriction)
  3821. {
  3822. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
  3823. ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
  3824. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
  3825. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  3826. ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
  3827. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  3828. ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY));
  3829. ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY));
  3830. ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY));
  3831. ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY));
  3832. ASSERT_EQ(ENOENT, test_open(dir_s2d3, O_RDONLY));
  3833. ASSERT_EQ(ENOENT, test_open(file1_s2d3, O_RDONLY));
  3834. ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY));
  3835. ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY));
  3836. ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY));
  3837. }
  3838. TEST_F_FORK(layout1_bind, same_content_same_file)
  3839. {
  3840. /*
  3841. * Sets access right on parent directories of both source and
  3842. * destination mount points.
  3843. */
  3844. const struct rule layer1_parent[] = {
  3845. {
  3846. .path = dir_s1d1,
  3847. .access = ACCESS_RO,
  3848. },
  3849. {
  3850. .path = dir_s2d1,
  3851. .access = ACCESS_RW,
  3852. },
  3853. {},
  3854. };
  3855. /*
  3856. * Sets access rights on the same bind-mounted directories. The result
  3857. * should be ACCESS_RW for both directories, but not both hierarchies
  3858. * because of the first layer.
  3859. */
  3860. const struct rule layer2_mount_point[] = {
  3861. {
  3862. .path = dir_s1d2,
  3863. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  3864. },
  3865. {
  3866. .path = dir_s2d2,
  3867. .access = ACCESS_RW,
  3868. },
  3869. {},
  3870. };
  3871. /* Only allow read-access to the s1d3 hierarchies. */
  3872. const struct rule layer3_source[] = {
  3873. {
  3874. .path = dir_s1d3,
  3875. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  3876. },
  3877. {},
  3878. };
  3879. /* Removes all access rights. */
  3880. const struct rule layer4_destination[] = {
  3881. {
  3882. .path = bind_file1_s1d3,
  3883. .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
  3884. },
  3885. {},
  3886. };
  3887. int ruleset_fd;
  3888. /* Sets rules for the parent directories. */
  3889. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_parent);
  3890. ASSERT_LE(0, ruleset_fd);
  3891. enforce_ruleset(_metadata, ruleset_fd);
  3892. ASSERT_EQ(0, close(ruleset_fd));
  3893. /* Checks source hierarchy. */
  3894. ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
  3895. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  3896. ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  3897. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  3898. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  3899. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  3900. /* Checks destination hierarchy. */
  3901. ASSERT_EQ(0, test_open(file1_s2d1, O_RDWR));
  3902. ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY));
  3903. ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR));
  3904. ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY));
  3905. /* Sets rules for the mount points. */
  3906. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_mount_point);
  3907. ASSERT_LE(0, ruleset_fd);
  3908. enforce_ruleset(_metadata, ruleset_fd);
  3909. ASSERT_EQ(0, close(ruleset_fd));
  3910. /* Checks source hierarchy. */
  3911. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
  3912. ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
  3913. ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
  3914. ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
  3915. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  3916. ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  3917. /* Checks destination hierarchy. */
  3918. ASSERT_EQ(EACCES, test_open(file1_s2d1, O_RDONLY));
  3919. ASSERT_EQ(EACCES, test_open(file1_s2d1, O_WRONLY));
  3920. ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY));
  3921. ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR));
  3922. ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY));
  3923. ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY));
  3924. /* Sets a (shared) rule only on the source. */
  3925. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3_source);
  3926. ASSERT_LE(0, ruleset_fd);
  3927. enforce_ruleset(_metadata, ruleset_fd);
  3928. ASSERT_EQ(0, close(ruleset_fd));
  3929. /* Checks source hierarchy. */
  3930. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY));
  3931. ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
  3932. ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
  3933. ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
  3934. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  3935. ASSERT_EQ(EACCES, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
  3936. /* Checks destination hierarchy. */
  3937. ASSERT_EQ(EACCES, test_open(file1_s2d2, O_RDONLY));
  3938. ASSERT_EQ(EACCES, test_open(file1_s2d2, O_WRONLY));
  3939. ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY));
  3940. ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY));
  3941. ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY));
  3942. ASSERT_EQ(EACCES, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY));
  3943. /* Sets a (shared) rule only on the destination. */
  3944. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer4_destination);
  3945. ASSERT_LE(0, ruleset_fd);
  3946. enforce_ruleset(_metadata, ruleset_fd);
  3947. ASSERT_EQ(0, close(ruleset_fd));
  3948. /* Checks source hierarchy. */
  3949. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY));
  3950. ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
  3951. /* Checks destination hierarchy. */
  3952. ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_RDONLY));
  3953. ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY));
  3954. }
  3955. TEST_F_FORK(layout1_bind, reparent_cross_mount)
  3956. {
  3957. const struct rule layer1[] = {
  3958. {
  3959. /* dir_s2d1 is beneath the dir_s2d2 mount point. */
  3960. .path = dir_s2d1,
  3961. .access = LANDLOCK_ACCESS_FS_REFER,
  3962. },
  3963. {
  3964. .path = bind_dir_s1d3,
  3965. .access = LANDLOCK_ACCESS_FS_EXECUTE,
  3966. },
  3967. {},
  3968. };
  3969. int ruleset_fd = create_ruleset(
  3970. _metadata,
  3971. LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_EXECUTE, layer1);
  3972. ASSERT_LE(0, ruleset_fd);
  3973. enforce_ruleset(_metadata, ruleset_fd);
  3974. ASSERT_EQ(0, close(ruleset_fd));
  3975. /* Checks basic denied move. */
  3976. ASSERT_EQ(-1, rename(file1_s1d1, file1_s1d2));
  3977. ASSERT_EQ(EXDEV, errno);
  3978. /* Checks real cross-mount move (Landlock is not involved). */
  3979. ASSERT_EQ(-1, rename(file1_s2d1, file1_s2d2));
  3980. ASSERT_EQ(EXDEV, errno);
  3981. /* Checks move that will give more accesses. */
  3982. ASSERT_EQ(-1, rename(file1_s2d2, bind_file1_s1d3));
  3983. ASSERT_EQ(EXDEV, errno);
  3984. /* Checks legitimate downgrade move. */
  3985. ASSERT_EQ(0, rename(bind_file1_s1d3, file1_s2d2));
  3986. }
  3987. #define LOWER_BASE TMP_DIR "/lower"
  3988. #define LOWER_DATA LOWER_BASE "/data"
  3989. static const char lower_fl1[] = LOWER_DATA "/fl1";
  3990. static const char lower_dl1[] = LOWER_DATA "/dl1";
  3991. static const char lower_dl1_fl2[] = LOWER_DATA "/dl1/fl2";
  3992. static const char lower_fo1[] = LOWER_DATA "/fo1";
  3993. static const char lower_do1[] = LOWER_DATA "/do1";
  3994. static const char lower_do1_fo2[] = LOWER_DATA "/do1/fo2";
  3995. static const char lower_do1_fl3[] = LOWER_DATA "/do1/fl3";
  3996. static const char (*lower_base_files[])[] = {
  3997. &lower_fl1,
  3998. &lower_fo1,
  3999. NULL,
  4000. };
  4001. static const char (*lower_base_directories[])[] = {
  4002. &lower_dl1,
  4003. &lower_do1,
  4004. NULL,
  4005. };
  4006. static const char (*lower_sub_files[])[] = {
  4007. &lower_dl1_fl2,
  4008. &lower_do1_fo2,
  4009. &lower_do1_fl3,
  4010. NULL,
  4011. };
  4012. #define UPPER_BASE TMP_DIR "/upper"
  4013. #define UPPER_DATA UPPER_BASE "/data"
  4014. #define UPPER_WORK UPPER_BASE "/work"
  4015. static const char upper_fu1[] = UPPER_DATA "/fu1";
  4016. static const char upper_du1[] = UPPER_DATA "/du1";
  4017. static const char upper_du1_fu2[] = UPPER_DATA "/du1/fu2";
  4018. static const char upper_fo1[] = UPPER_DATA "/fo1";
  4019. static const char upper_do1[] = UPPER_DATA "/do1";
  4020. static const char upper_do1_fo2[] = UPPER_DATA "/do1/fo2";
  4021. static const char upper_do1_fu3[] = UPPER_DATA "/do1/fu3";
  4022. static const char (*upper_base_files[])[] = {
  4023. &upper_fu1,
  4024. &upper_fo1,
  4025. NULL,
  4026. };
  4027. static const char (*upper_base_directories[])[] = {
  4028. &upper_du1,
  4029. &upper_do1,
  4030. NULL,
  4031. };
  4032. static const char (*upper_sub_files[])[] = {
  4033. &upper_du1_fu2,
  4034. &upper_do1_fo2,
  4035. &upper_do1_fu3,
  4036. NULL,
  4037. };
  4038. #define MERGE_BASE TMP_DIR "/merge"
  4039. #define MERGE_DATA MERGE_BASE "/data"
  4040. static const char merge_fl1[] = MERGE_DATA "/fl1";
  4041. static const char merge_dl1[] = MERGE_DATA "/dl1";
  4042. static const char merge_dl1_fl2[] = MERGE_DATA "/dl1/fl2";
  4043. static const char merge_fu1[] = MERGE_DATA "/fu1";
  4044. static const char merge_du1[] = MERGE_DATA "/du1";
  4045. static const char merge_du1_fu2[] = MERGE_DATA "/du1/fu2";
  4046. static const char merge_fo1[] = MERGE_DATA "/fo1";
  4047. static const char merge_do1[] = MERGE_DATA "/do1";
  4048. static const char merge_do1_fo2[] = MERGE_DATA "/do1/fo2";
  4049. static const char merge_do1_fl3[] = MERGE_DATA "/do1/fl3";
  4050. static const char merge_do1_fu3[] = MERGE_DATA "/do1/fu3";
  4051. static const char (*merge_base_files[])[] = {
  4052. &merge_fl1,
  4053. &merge_fu1,
  4054. &merge_fo1,
  4055. NULL,
  4056. };
  4057. static const char (*merge_base_directories[])[] = {
  4058. &merge_dl1,
  4059. &merge_du1,
  4060. &merge_do1,
  4061. NULL,
  4062. };
  4063. static const char (*merge_sub_files[])[] = {
  4064. &merge_dl1_fl2, &merge_du1_fu2, &merge_do1_fo2,
  4065. &merge_do1_fl3, &merge_do1_fu3, NULL,
  4066. };
  4067. /*
  4068. * layout2_overlay hierarchy:
  4069. *
  4070. * tmp
  4071. * ├── lower
  4072. * │   └── data
  4073. * │   ├── dl1
  4074. * │   │   └── fl2
  4075. * │   ├── do1
  4076. * │   │   ├── fl3
  4077. * │   │   └── fo2
  4078. * │   ├── fl1
  4079. * │   └── fo1
  4080. * ├── merge
  4081. * │   └── data
  4082. * │   ├── dl1
  4083. * │   │   └── fl2
  4084. * │   ├── do1
  4085. * │   │   ├── fl3
  4086. * │   │   ├── fo2
  4087. * │   │   └── fu3
  4088. * │   ├── du1
  4089. * │   │   └── fu2
  4090. * │   ├── fl1
  4091. * │   ├── fo1
  4092. * │   └── fu1
  4093. * └── upper
  4094. * ├── data
  4095. * │   ├── do1
  4096. * │   │   ├── fo2
  4097. * │   │   └── fu3
  4098. * │   ├── du1
  4099. * │   │   └── fu2
  4100. * │   ├── fo1
  4101. * │   └── fu1
  4102. * └── work
  4103. * └── work
  4104. */
  4105. FIXTURE(layout2_overlay)
  4106. {
  4107. bool skip_test;
  4108. };
  4109. FIXTURE_SETUP(layout2_overlay)
  4110. {
  4111. if (!supports_filesystem("overlay")) {
  4112. self->skip_test = true;
  4113. SKIP(return, "overlayfs is not supported (setup)");
  4114. }
  4115. prepare_layout(_metadata);
  4116. create_directory(_metadata, LOWER_BASE);
  4117. set_cap(_metadata, CAP_SYS_ADMIN);
  4118. /* Creates tmpfs mount points to get deterministic overlayfs. */
  4119. ASSERT_EQ(0, mount_opt(&mnt_tmp, LOWER_BASE));
  4120. clear_cap(_metadata, CAP_SYS_ADMIN);
  4121. create_file(_metadata, lower_fl1);
  4122. create_file(_metadata, lower_dl1_fl2);
  4123. create_file(_metadata, lower_fo1);
  4124. create_file(_metadata, lower_do1_fo2);
  4125. create_file(_metadata, lower_do1_fl3);
  4126. create_directory(_metadata, UPPER_BASE);
  4127. set_cap(_metadata, CAP_SYS_ADMIN);
  4128. ASSERT_EQ(0, mount_opt(&mnt_tmp, UPPER_BASE));
  4129. clear_cap(_metadata, CAP_SYS_ADMIN);
  4130. create_file(_metadata, upper_fu1);
  4131. create_file(_metadata, upper_du1_fu2);
  4132. create_file(_metadata, upper_fo1);
  4133. create_file(_metadata, upper_do1_fo2);
  4134. create_file(_metadata, upper_do1_fu3);
  4135. ASSERT_EQ(0, mkdir(UPPER_WORK, 0700));
  4136. create_directory(_metadata, MERGE_DATA);
  4137. set_cap(_metadata, CAP_SYS_ADMIN);
  4138. set_cap(_metadata, CAP_DAC_OVERRIDE);
  4139. ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
  4140. "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
  4141. ",workdir=" UPPER_WORK));
  4142. clear_cap(_metadata, CAP_DAC_OVERRIDE);
  4143. clear_cap(_metadata, CAP_SYS_ADMIN);
  4144. }
  4145. FIXTURE_TEARDOWN_PARENT(layout2_overlay)
  4146. {
  4147. if (self->skip_test)
  4148. SKIP(return, "overlayfs is not supported (teardown)");
  4149. EXPECT_EQ(0, remove_path(lower_do1_fl3));
  4150. EXPECT_EQ(0, remove_path(lower_dl1_fl2));
  4151. EXPECT_EQ(0, remove_path(lower_fl1));
  4152. EXPECT_EQ(0, remove_path(lower_do1_fo2));
  4153. EXPECT_EQ(0, remove_path(lower_fo1));
  4154. /* umount(LOWER_BASE)) is handled by namespace lifetime. */
  4155. EXPECT_EQ(0, remove_path(LOWER_BASE));
  4156. EXPECT_EQ(0, remove_path(upper_do1_fu3));
  4157. EXPECT_EQ(0, remove_path(upper_du1_fu2));
  4158. EXPECT_EQ(0, remove_path(upper_fu1));
  4159. EXPECT_EQ(0, remove_path(upper_do1_fo2));
  4160. EXPECT_EQ(0, remove_path(upper_fo1));
  4161. EXPECT_EQ(0, remove_path(UPPER_WORK "/work"));
  4162. /* umount(UPPER_BASE)) is handled by namespace lifetime. */
  4163. EXPECT_EQ(0, remove_path(UPPER_BASE));
  4164. /* umount(MERGE_DATA)) is handled by namespace lifetime. */
  4165. EXPECT_EQ(0, remove_path(MERGE_DATA));
  4166. cleanup_layout(_metadata);
  4167. }
  4168. TEST_F_FORK(layout2_overlay, no_restriction)
  4169. {
  4170. if (self->skip_test)
  4171. SKIP(return, "overlayfs is not supported (test)");
  4172. ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY));
  4173. ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY));
  4174. ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY));
  4175. ASSERT_EQ(0, test_open(lower_fo1, O_RDONLY));
  4176. ASSERT_EQ(0, test_open(lower_do1, O_RDONLY));
  4177. ASSERT_EQ(0, test_open(lower_do1_fo2, O_RDONLY));
  4178. ASSERT_EQ(0, test_open(lower_do1_fl3, O_RDONLY));
  4179. ASSERT_EQ(0, test_open(upper_fu1, O_RDONLY));
  4180. ASSERT_EQ(0, test_open(upper_du1, O_RDONLY));
  4181. ASSERT_EQ(0, test_open(upper_du1_fu2, O_RDONLY));
  4182. ASSERT_EQ(0, test_open(upper_fo1, O_RDONLY));
  4183. ASSERT_EQ(0, test_open(upper_do1, O_RDONLY));
  4184. ASSERT_EQ(0, test_open(upper_do1_fo2, O_RDONLY));
  4185. ASSERT_EQ(0, test_open(upper_do1_fu3, O_RDONLY));
  4186. ASSERT_EQ(0, test_open(merge_fl1, O_RDONLY));
  4187. ASSERT_EQ(0, test_open(merge_dl1, O_RDONLY));
  4188. ASSERT_EQ(0, test_open(merge_dl1_fl2, O_RDONLY));
  4189. ASSERT_EQ(0, test_open(merge_fu1, O_RDONLY));
  4190. ASSERT_EQ(0, test_open(merge_du1, O_RDONLY));
  4191. ASSERT_EQ(0, test_open(merge_du1_fu2, O_RDONLY));
  4192. ASSERT_EQ(0, test_open(merge_fo1, O_RDONLY));
  4193. ASSERT_EQ(0, test_open(merge_do1, O_RDONLY));
  4194. ASSERT_EQ(0, test_open(merge_do1_fo2, O_RDONLY));
  4195. ASSERT_EQ(0, test_open(merge_do1_fl3, O_RDONLY));
  4196. ASSERT_EQ(0, test_open(merge_do1_fu3, O_RDONLY));
  4197. }
  4198. #define for_each_path(path_list, path_entry, i) \
  4199. for (i = 0, path_entry = *path_list[i]; path_list[i]; \
  4200. path_entry = *path_list[++i])
  4201. TEST_F_FORK(layout2_overlay, same_content_different_file)
  4202. {
  4203. /* Sets access right on parent directories of both layers. */
  4204. const struct rule layer1_base[] = {
  4205. {
  4206. .path = LOWER_BASE,
  4207. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4208. },
  4209. {
  4210. .path = UPPER_BASE,
  4211. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4212. },
  4213. {
  4214. .path = MERGE_BASE,
  4215. .access = ACCESS_RW,
  4216. },
  4217. {},
  4218. };
  4219. const struct rule layer2_data[] = {
  4220. {
  4221. .path = LOWER_DATA,
  4222. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4223. },
  4224. {
  4225. .path = UPPER_DATA,
  4226. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4227. },
  4228. {
  4229. .path = MERGE_DATA,
  4230. .access = ACCESS_RW,
  4231. },
  4232. {},
  4233. };
  4234. /* Sets access right on directories inside both layers. */
  4235. const struct rule layer3_subdirs[] = {
  4236. {
  4237. .path = lower_dl1,
  4238. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4239. },
  4240. {
  4241. .path = lower_do1,
  4242. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4243. },
  4244. {
  4245. .path = upper_du1,
  4246. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4247. },
  4248. {
  4249. .path = upper_do1,
  4250. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4251. },
  4252. {
  4253. .path = merge_dl1,
  4254. .access = ACCESS_RW,
  4255. },
  4256. {
  4257. .path = merge_du1,
  4258. .access = ACCESS_RW,
  4259. },
  4260. {
  4261. .path = merge_do1,
  4262. .access = ACCESS_RW,
  4263. },
  4264. {},
  4265. };
  4266. /* Tighten access rights to the files. */
  4267. const struct rule layer4_files[] = {
  4268. {
  4269. .path = lower_dl1_fl2,
  4270. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4271. },
  4272. {
  4273. .path = lower_do1_fo2,
  4274. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4275. },
  4276. {
  4277. .path = lower_do1_fl3,
  4278. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4279. },
  4280. {
  4281. .path = upper_du1_fu2,
  4282. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4283. },
  4284. {
  4285. .path = upper_do1_fo2,
  4286. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4287. },
  4288. {
  4289. .path = upper_do1_fu3,
  4290. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4291. },
  4292. {
  4293. .path = merge_dl1_fl2,
  4294. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  4295. LANDLOCK_ACCESS_FS_WRITE_FILE,
  4296. },
  4297. {
  4298. .path = merge_du1_fu2,
  4299. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  4300. LANDLOCK_ACCESS_FS_WRITE_FILE,
  4301. },
  4302. {
  4303. .path = merge_do1_fo2,
  4304. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  4305. LANDLOCK_ACCESS_FS_WRITE_FILE,
  4306. },
  4307. {
  4308. .path = merge_do1_fl3,
  4309. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  4310. LANDLOCK_ACCESS_FS_WRITE_FILE,
  4311. },
  4312. {
  4313. .path = merge_do1_fu3,
  4314. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  4315. LANDLOCK_ACCESS_FS_WRITE_FILE,
  4316. },
  4317. {},
  4318. };
  4319. const struct rule layer5_merge_only[] = {
  4320. {
  4321. .path = MERGE_DATA,
  4322. .access = LANDLOCK_ACCESS_FS_READ_FILE |
  4323. LANDLOCK_ACCESS_FS_WRITE_FILE,
  4324. },
  4325. {},
  4326. };
  4327. int ruleset_fd;
  4328. size_t i;
  4329. const char *path_entry;
  4330. if (self->skip_test)
  4331. SKIP(return, "overlayfs is not supported (test)");
  4332. /* Sets rules on base directories (i.e. outside overlay scope). */
  4333. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
  4334. ASSERT_LE(0, ruleset_fd);
  4335. enforce_ruleset(_metadata, ruleset_fd);
  4336. ASSERT_EQ(0, close(ruleset_fd));
  4337. /* Checks lower layer. */
  4338. for_each_path(lower_base_files, path_entry, i) {
  4339. ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
  4340. ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
  4341. }
  4342. for_each_path(lower_base_directories, path_entry, i) {
  4343. ASSERT_EQ(EACCES,
  4344. test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4345. }
  4346. for_each_path(lower_sub_files, path_entry, i) {
  4347. ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
  4348. ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
  4349. }
  4350. /* Checks upper layer. */
  4351. for_each_path(upper_base_files, path_entry, i) {
  4352. ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
  4353. ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
  4354. }
  4355. for_each_path(upper_base_directories, path_entry, i) {
  4356. ASSERT_EQ(EACCES,
  4357. test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4358. }
  4359. for_each_path(upper_sub_files, path_entry, i) {
  4360. ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
  4361. ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
  4362. }
  4363. /*
  4364. * Checks that access rights are independent from the lower and upper
  4365. * layers: write access to upper files viewed through the merge point
  4366. * is still allowed, and write access to lower file viewed (and copied)
  4367. * through the merge point is still allowed.
  4368. */
  4369. for_each_path(merge_base_files, path_entry, i) {
  4370. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4371. }
  4372. for_each_path(merge_base_directories, path_entry, i) {
  4373. ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4374. }
  4375. for_each_path(merge_sub_files, path_entry, i) {
  4376. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4377. }
  4378. /* Sets rules on data directories (i.e. inside overlay scope). */
  4379. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_data);
  4380. ASSERT_LE(0, ruleset_fd);
  4381. enforce_ruleset(_metadata, ruleset_fd);
  4382. ASSERT_EQ(0, close(ruleset_fd));
  4383. /* Checks merge. */
  4384. for_each_path(merge_base_files, path_entry, i) {
  4385. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4386. }
  4387. for_each_path(merge_base_directories, path_entry, i) {
  4388. ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4389. }
  4390. for_each_path(merge_sub_files, path_entry, i) {
  4391. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4392. }
  4393. /* Same checks with tighter rules. */
  4394. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3_subdirs);
  4395. ASSERT_LE(0, ruleset_fd);
  4396. enforce_ruleset(_metadata, ruleset_fd);
  4397. ASSERT_EQ(0, close(ruleset_fd));
  4398. /* Checks changes for lower layer. */
  4399. for_each_path(lower_base_files, path_entry, i) {
  4400. ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
  4401. }
  4402. /* Checks changes for upper layer. */
  4403. for_each_path(upper_base_files, path_entry, i) {
  4404. ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
  4405. }
  4406. /* Checks all merge accesses. */
  4407. for_each_path(merge_base_files, path_entry, i) {
  4408. ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR));
  4409. }
  4410. for_each_path(merge_base_directories, path_entry, i) {
  4411. ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4412. }
  4413. for_each_path(merge_sub_files, path_entry, i) {
  4414. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4415. }
  4416. /* Sets rules directly on overlayed files. */
  4417. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer4_files);
  4418. ASSERT_LE(0, ruleset_fd);
  4419. enforce_ruleset(_metadata, ruleset_fd);
  4420. ASSERT_EQ(0, close(ruleset_fd));
  4421. /* Checks unchanged accesses on lower layer. */
  4422. for_each_path(lower_sub_files, path_entry, i) {
  4423. ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
  4424. ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
  4425. }
  4426. /* Checks unchanged accesses on upper layer. */
  4427. for_each_path(upper_sub_files, path_entry, i) {
  4428. ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
  4429. ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
  4430. }
  4431. /* Checks all merge accesses. */
  4432. for_each_path(merge_base_files, path_entry, i) {
  4433. ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR));
  4434. }
  4435. for_each_path(merge_base_directories, path_entry, i) {
  4436. ASSERT_EQ(EACCES,
  4437. test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4438. }
  4439. for_each_path(merge_sub_files, path_entry, i) {
  4440. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4441. }
  4442. /* Only allowes access to the merge hierarchy. */
  4443. ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer5_merge_only);
  4444. ASSERT_LE(0, ruleset_fd);
  4445. enforce_ruleset(_metadata, ruleset_fd);
  4446. ASSERT_EQ(0, close(ruleset_fd));
  4447. /* Checks new accesses on lower layer. */
  4448. for_each_path(lower_sub_files, path_entry, i) {
  4449. ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
  4450. }
  4451. /* Checks new accesses on upper layer. */
  4452. for_each_path(upper_sub_files, path_entry, i) {
  4453. ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
  4454. }
  4455. /* Checks all merge accesses. */
  4456. for_each_path(merge_base_files, path_entry, i) {
  4457. ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR));
  4458. }
  4459. for_each_path(merge_base_directories, path_entry, i) {
  4460. ASSERT_EQ(EACCES,
  4461. test_open(path_entry, O_RDONLY | O_DIRECTORY));
  4462. }
  4463. for_each_path(merge_sub_files, path_entry, i) {
  4464. ASSERT_EQ(0, test_open(path_entry, O_RDWR));
  4465. }
  4466. }
  4467. FIXTURE(layout3_fs)
  4468. {
  4469. bool has_created_dir;
  4470. bool has_created_file;
  4471. bool skip_test;
  4472. };
  4473. FIXTURE_VARIANT(layout3_fs)
  4474. {
  4475. const struct mnt_opt mnt;
  4476. const char *const file_path;
  4477. unsigned int cwd_fs_magic;
  4478. };
  4479. /* clang-format off */
  4480. FIXTURE_VARIANT_ADD(layout3_fs, tmpfs) {
  4481. /* clang-format on */
  4482. .mnt = {
  4483. .type = "tmpfs",
  4484. .data = MNT_TMP_DATA,
  4485. },
  4486. .file_path = file1_s1d1,
  4487. };
  4488. FIXTURE_VARIANT_ADD(layout3_fs, ramfs) {
  4489. .mnt = {
  4490. .type = "ramfs",
  4491. .data = "mode=700",
  4492. },
  4493. .file_path = TMP_DIR "/dir/file",
  4494. };
  4495. FIXTURE_VARIANT_ADD(layout3_fs, cgroup2) {
  4496. .mnt = {
  4497. .type = "cgroup2",
  4498. },
  4499. .file_path = TMP_DIR "/test/cgroup.procs",
  4500. };
  4501. FIXTURE_VARIANT_ADD(layout3_fs, proc) {
  4502. .mnt = {
  4503. .type = "proc",
  4504. },
  4505. .file_path = TMP_DIR "/self/status",
  4506. };
  4507. FIXTURE_VARIANT_ADD(layout3_fs, sysfs) {
  4508. .mnt = {
  4509. .type = "sysfs",
  4510. },
  4511. .file_path = TMP_DIR "/kernel/notes",
  4512. };
  4513. FIXTURE_VARIANT_ADD(layout3_fs, hostfs) {
  4514. .mnt = {
  4515. .source = TMP_DIR,
  4516. .flags = MS_BIND,
  4517. },
  4518. .file_path = TMP_DIR "/dir/file",
  4519. .cwd_fs_magic = HOSTFS_SUPER_MAGIC,
  4520. };
  4521. static char *dirname_alloc(const char *path)
  4522. {
  4523. char *dup;
  4524. if (!path)
  4525. return NULL;
  4526. dup = strdup(path);
  4527. if (!dup)
  4528. return NULL;
  4529. return dirname(dup);
  4530. }
  4531. FIXTURE_SETUP(layout3_fs)
  4532. {
  4533. struct stat statbuf;
  4534. char *dir_path = dirname_alloc(variant->file_path);
  4535. if (!supports_filesystem(variant->mnt.type) ||
  4536. !cwd_matches_fs(variant->cwd_fs_magic)) {
  4537. self->skip_test = true;
  4538. SKIP(return, "this filesystem is not supported (setup)");
  4539. }
  4540. prepare_layout_opt(_metadata, &variant->mnt);
  4541. /* Creates directory when required. */
  4542. if (stat(dir_path, &statbuf)) {
  4543. set_cap(_metadata, CAP_DAC_OVERRIDE);
  4544. EXPECT_EQ(0, mkdir(dir_path, 0700))
  4545. {
  4546. TH_LOG("Failed to create directory \"%s\": %s",
  4547. dir_path, strerror(errno));
  4548. }
  4549. self->has_created_dir = true;
  4550. clear_cap(_metadata, CAP_DAC_OVERRIDE);
  4551. }
  4552. /* Creates file when required. */
  4553. if (stat(variant->file_path, &statbuf)) {
  4554. int fd;
  4555. set_cap(_metadata, CAP_DAC_OVERRIDE);
  4556. fd = creat(variant->file_path, 0600);
  4557. EXPECT_LE(0, fd)
  4558. {
  4559. TH_LOG("Failed to create file \"%s\": %s",
  4560. variant->file_path, strerror(errno));
  4561. }
  4562. EXPECT_EQ(0, close(fd));
  4563. self->has_created_file = true;
  4564. clear_cap(_metadata, CAP_DAC_OVERRIDE);
  4565. }
  4566. free(dir_path);
  4567. }
  4568. FIXTURE_TEARDOWN_PARENT(layout3_fs)
  4569. {
  4570. if (self->skip_test)
  4571. SKIP(return, "this filesystem is not supported (teardown)");
  4572. if (self->has_created_file) {
  4573. set_cap(_metadata, CAP_DAC_OVERRIDE);
  4574. /*
  4575. * Don't check for error because the file might already
  4576. * have been removed (cf. release_inode test).
  4577. */
  4578. unlink(variant->file_path);
  4579. clear_cap(_metadata, CAP_DAC_OVERRIDE);
  4580. }
  4581. if (self->has_created_dir) {
  4582. char *dir_path = dirname_alloc(variant->file_path);
  4583. set_cap(_metadata, CAP_DAC_OVERRIDE);
  4584. /*
  4585. * Don't check for error because the directory might already
  4586. * have been removed (cf. release_inode test).
  4587. */
  4588. rmdir(dir_path);
  4589. clear_cap(_metadata, CAP_DAC_OVERRIDE);
  4590. free(dir_path);
  4591. }
  4592. cleanup_layout(_metadata);
  4593. }
  4594. static void layer3_fs_tag_inode(struct __test_metadata *const _metadata,
  4595. FIXTURE_DATA(layout3_fs) * self,
  4596. const FIXTURE_VARIANT(layout3_fs) * variant,
  4597. const char *const rule_path)
  4598. {
  4599. const struct rule layer1_allow_read_file[] = {
  4600. {
  4601. .path = rule_path,
  4602. .access = LANDLOCK_ACCESS_FS_READ_FILE,
  4603. },
  4604. {},
  4605. };
  4606. const struct landlock_ruleset_attr layer2_deny_everything_attr = {
  4607. .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
  4608. };
  4609. const char *const dev_null_path = "/dev/null";
  4610. int ruleset_fd;
  4611. if (self->skip_test)
  4612. SKIP(return, "this filesystem is not supported (test)");
  4613. /* Checks without Landlock. */
  4614. EXPECT_EQ(0, test_open(dev_null_path, O_RDONLY | O_CLOEXEC));
  4615. EXPECT_EQ(0, test_open(variant->file_path, O_RDONLY | O_CLOEXEC));
  4616. ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
  4617. layer1_allow_read_file);
  4618. EXPECT_LE(0, ruleset_fd);
  4619. enforce_ruleset(_metadata, ruleset_fd);
  4620. EXPECT_EQ(0, close(ruleset_fd));
  4621. EXPECT_EQ(EACCES, test_open(dev_null_path, O_RDONLY | O_CLOEXEC));
  4622. EXPECT_EQ(0, test_open(variant->file_path, O_RDONLY | O_CLOEXEC));
  4623. /* Forbids directory reading. */
  4624. ruleset_fd =
  4625. landlock_create_ruleset(&layer2_deny_everything_attr,
  4626. sizeof(layer2_deny_everything_attr), 0);
  4627. EXPECT_LE(0, ruleset_fd);
  4628. enforce_ruleset(_metadata, ruleset_fd);
  4629. EXPECT_EQ(0, close(ruleset_fd));
  4630. /* Checks with Landlock and forbidden access. */
  4631. EXPECT_EQ(EACCES, test_open(dev_null_path, O_RDONLY | O_CLOEXEC));
  4632. EXPECT_EQ(EACCES, test_open(variant->file_path, O_RDONLY | O_CLOEXEC));
  4633. }
  4634. /* Matrix of tests to check file hierarchy evaluation. */
  4635. TEST_F_FORK(layout3_fs, tag_inode_dir_parent)
  4636. {
  4637. /* The current directory must not be the root for this test. */
  4638. layer3_fs_tag_inode(_metadata, self, variant, ".");
  4639. }
  4640. TEST_F_FORK(layout3_fs, tag_inode_dir_mnt)
  4641. {
  4642. layer3_fs_tag_inode(_metadata, self, variant, TMP_DIR);
  4643. }
  4644. TEST_F_FORK(layout3_fs, tag_inode_dir_child)
  4645. {
  4646. char *dir_path = dirname_alloc(variant->file_path);
  4647. layer3_fs_tag_inode(_metadata, self, variant, dir_path);
  4648. free(dir_path);
  4649. }
  4650. TEST_F_FORK(layout3_fs, tag_inode_file)
  4651. {
  4652. layer3_fs_tag_inode(_metadata, self, variant, variant->file_path);
  4653. }
  4654. /* Light version of layout1.release_inodes */
  4655. TEST_F_FORK(layout3_fs, release_inodes)
  4656. {
  4657. const struct rule layer1[] = {
  4658. {
  4659. .path = TMP_DIR,
  4660. .access = LANDLOCK_ACCESS_FS_READ_DIR,
  4661. },
  4662. {},
  4663. };
  4664. int ruleset_fd;
  4665. if (self->skip_test)
  4666. SKIP(return, "this filesystem is not supported (test)");
  4667. /* Clean up for the teardown to not fail. */
  4668. if (self->has_created_file)
  4669. EXPECT_EQ(0, remove_path(variant->file_path));
  4670. if (self->has_created_dir) {
  4671. char *dir_path = dirname_alloc(variant->file_path);
  4672. /* Don't check for error because of cgroup specificities. */
  4673. remove_path(dir_path);
  4674. free(dir_path);
  4675. }
  4676. ruleset_fd =
  4677. create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, layer1);
  4678. ASSERT_LE(0, ruleset_fd);
  4679. /* Unmount the filesystem while it is being used by a ruleset. */
  4680. set_cap(_metadata, CAP_SYS_ADMIN);
  4681. ASSERT_EQ(0, umount(TMP_DIR));
  4682. clear_cap(_metadata, CAP_SYS_ADMIN);
  4683. /* Replaces with a new mount point to simplify FIXTURE_TEARDOWN. */
  4684. set_cap(_metadata, CAP_SYS_ADMIN);
  4685. ASSERT_EQ(0, mount_opt(&mnt_tmp, TMP_DIR));
  4686. clear_cap(_metadata, CAP_SYS_ADMIN);
  4687. enforce_ruleset(_metadata, ruleset_fd);
  4688. ASSERT_EQ(0, close(ruleset_fd));
  4689. /* Checks that access to the new mount point is denied. */
  4690. ASSERT_EQ(EACCES, test_open(TMP_DIR, O_RDONLY));
  4691. }
  4692. TEST_HARNESS_MAIN