1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Landlock tests - Filesystem
- *
- * Copyright © 2017-2020 Mickaël Salaün <[email protected]>
- * Copyright © 2020 ANSSI
- * Copyright © 2020-2022 Microsoft Corporation
- */
- #define _GNU_SOURCE
- #include <fcntl.h>
- #include <linux/landlock.h>
- #include <sched.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/capability.h>
- #include <sys/mount.h>
- #include <sys/prctl.h>
- #include <sys/sendfile.h>
- #include <sys/stat.h>
- #include <sys/sysmacros.h>
- #include <unistd.h>
- #include "common.h"
- #ifndef renameat2
- int renameat2(int olddirfd, const char *oldpath, int newdirfd,
- const char *newpath, unsigned int flags)
- {
- return syscall(__NR_renameat2, olddirfd, oldpath, newdirfd, newpath,
- flags);
- }
- #endif
- #ifndef RENAME_EXCHANGE
- #define RENAME_EXCHANGE (1 << 1)
- #endif
- #define TMP_DIR "tmp"
- #define BINARY_PATH "./true"
- /* Paths (sibling number and depth) */
- static const char dir_s1d1[] = TMP_DIR "/s1d1";
- static const char file1_s1d1[] = TMP_DIR "/s1d1/f1";
- static const char file2_s1d1[] = TMP_DIR "/s1d1/f2";
- static const char dir_s1d2[] = TMP_DIR "/s1d1/s1d2";
- static const char file1_s1d2[] = TMP_DIR "/s1d1/s1d2/f1";
- static const char file2_s1d2[] = TMP_DIR "/s1d1/s1d2/f2";
- static const char dir_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3";
- static const char file1_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f1";
- static const char file2_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f2";
- static const char dir_s2d1[] = TMP_DIR "/s2d1";
- static const char file1_s2d1[] = TMP_DIR "/s2d1/f1";
- static const char dir_s2d2[] = TMP_DIR "/s2d1/s2d2";
- static const char file1_s2d2[] = TMP_DIR "/s2d1/s2d2/f1";
- static const char dir_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3";
- static const char file1_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f1";
- static const char file2_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f2";
- static const char dir_s3d1[] = TMP_DIR "/s3d1";
- /* dir_s3d2 is a mount point. */
- static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2";
- static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
- /*
- * layout1 hierarchy:
- *
- * tmp
- * ├── s1d1
- * │ ├── f1
- * │ ├── f2
- * │ └── s1d2
- * │ ├── f1
- * │ ├── f2
- * │ └── s1d3
- * │ ├── f1
- * │ └── f2
- * ├── s2d1
- * │ ├── f1
- * │ └── s2d2
- * │ ├── f1
- * │ └── s2d3
- * │ ├── f1
- * │ └── f2
- * └── s3d1
- * └── s3d2
- * └── s3d3
- */
- static bool fgrep(FILE *const inf, const char *const str)
- {
- char line[32];
- const int slen = strlen(str);
- while (!feof(inf)) {
- if (!fgets(line, sizeof(line), inf))
- break;
- if (strncmp(line, str, slen))
- continue;
- return true;
- }
- return false;
- }
- static bool supports_overlayfs(void)
- {
- bool res;
- FILE *const inf = fopen("/proc/filesystems", "r");
- /*
- * Consider that the filesystem is supported if we cannot get the
- * supported ones.
- */
- if (!inf)
- return true;
- res = fgrep(inf, "nodev\toverlay\n");
- fclose(inf);
- return res;
- }
- static void mkdir_parents(struct __test_metadata *const _metadata,
- const char *const path)
- {
- char *walker;
- const char *parent;
- int i, err;
- ASSERT_NE(path[0], '\0');
- walker = strdup(path);
- ASSERT_NE(NULL, walker);
- parent = walker;
- for (i = 1; walker[i]; i++) {
- if (walker[i] != '/')
- continue;
- walker[i] = '\0';
- err = mkdir(parent, 0700);
- ASSERT_FALSE(err && errno != EEXIST)
- {
- TH_LOG("Failed to create directory \"%s\": %s", parent,
- strerror(errno));
- }
- walker[i] = '/';
- }
- free(walker);
- }
- static void create_directory(struct __test_metadata *const _metadata,
- const char *const path)
- {
- mkdir_parents(_metadata, path);
- ASSERT_EQ(0, mkdir(path, 0700))
- {
- TH_LOG("Failed to create directory \"%s\": %s", path,
- strerror(errno));
- }
- }
- static void create_file(struct __test_metadata *const _metadata,
- const char *const path)
- {
- mkdir_parents(_metadata, path);
- ASSERT_EQ(0, mknod(path, S_IFREG | 0700, 0))
- {
- TH_LOG("Failed to create file \"%s\": %s", path,
- strerror(errno));
- }
- }
- static int remove_path(const char *const path)
- {
- char *walker;
- int i, ret, err = 0;
- walker = strdup(path);
- if (!walker) {
- err = ENOMEM;
- goto out;
- }
- if (unlink(path) && rmdir(path)) {
- if (errno != ENOENT && errno != ENOTDIR)
- err = errno;
- goto out;
- }
- for (i = strlen(walker); i > 0; i--) {
- if (walker[i] != '/')
- continue;
- walker[i] = '\0';
- ret = rmdir(walker);
- if (ret) {
- if (errno != ENOTEMPTY && errno != EBUSY)
- err = errno;
- goto out;
- }
- if (strcmp(walker, TMP_DIR) == 0)
- goto out;
- }
- out:
- free(walker);
- return err;
- }
- static void prepare_layout(struct __test_metadata *const _metadata)
- {
- disable_caps(_metadata);
- umask(0077);
- create_directory(_metadata, TMP_DIR);
- /*
- * Do not pollute the rest of the system: creates a private mount point
- * for tests relying on pivot_root(2) and move_mount(2).
- */
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, unshare(CLONE_NEWNS));
- ASSERT_EQ(0, mount("tmp", TMP_DIR, "tmpfs", 0, "size=4m,mode=700"));
- ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- }
- static void cleanup_layout(struct __test_metadata *const _metadata)
- {
- set_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, umount(TMP_DIR));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, remove_path(TMP_DIR));
- }
- static void create_layout1(struct __test_metadata *const _metadata)
- {
- create_file(_metadata, file1_s1d1);
- create_file(_metadata, file1_s1d2);
- create_file(_metadata, file1_s1d3);
- create_file(_metadata, file2_s1d1);
- create_file(_metadata, file2_s1d2);
- create_file(_metadata, file2_s1d3);
- create_file(_metadata, file1_s2d1);
- create_file(_metadata, file1_s2d2);
- create_file(_metadata, file1_s2d3);
- create_file(_metadata, file2_s2d3);
- create_directory(_metadata, dir_s3d2);
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, mount("tmp", dir_s3d2, "tmpfs", 0, "size=4m,mode=700"));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, mkdir(dir_s3d3, 0700));
- }
- static void remove_layout1(struct __test_metadata *const _metadata)
- {
- EXPECT_EQ(0, remove_path(file2_s1d3));
- EXPECT_EQ(0, remove_path(file2_s1d2));
- EXPECT_EQ(0, remove_path(file2_s1d1));
- EXPECT_EQ(0, remove_path(file1_s1d3));
- EXPECT_EQ(0, remove_path(file1_s1d2));
- EXPECT_EQ(0, remove_path(file1_s1d1));
- EXPECT_EQ(0, remove_path(file2_s2d3));
- EXPECT_EQ(0, remove_path(file1_s2d3));
- EXPECT_EQ(0, remove_path(file1_s2d2));
- EXPECT_EQ(0, remove_path(file1_s2d1));
- EXPECT_EQ(0, remove_path(dir_s3d3));
- set_cap(_metadata, CAP_SYS_ADMIN);
- umount(dir_s3d2);
- clear_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, remove_path(dir_s3d2));
- }
- /* clang-format off */
- FIXTURE(layout1) {};
- /* clang-format on */
- FIXTURE_SETUP(layout1)
- {
- prepare_layout(_metadata);
- create_layout1(_metadata);
- }
- FIXTURE_TEARDOWN(layout1)
- {
- remove_layout1(_metadata);
- cleanup_layout(_metadata);
- }
- /*
- * This helper enables to use the ASSERT_* macros and print the line number
- * pointing to the test caller.
- */
- static int test_open_rel(const int dirfd, const char *const path,
- const int flags)
- {
- int fd;
- /* Works with file and directories. */
- fd = openat(dirfd, path, flags | O_CLOEXEC);
- if (fd < 0)
- return errno;
- /*
- * Mixing error codes from close(2) and open(2) should not lead to any
- * (access type) confusion for this test.
- */
- if (close(fd) != 0)
- return errno;
- return 0;
- }
- static int test_open(const char *const path, const int flags)
- {
- return test_open_rel(AT_FDCWD, path, flags);
- }
- TEST_F_FORK(layout1, no_restriction)
- {
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file2_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file2_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s2d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s2d3, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY));
- }
- TEST_F_FORK(layout1, inval)
- {
- struct landlock_path_beneath_attr path_beneath = {
- .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- .parent_fd = -1,
- };
- struct landlock_ruleset_attr ruleset_attr = {
- .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- };
- int ruleset_fd;
- path_beneath.parent_fd =
- open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC);
- ASSERT_LE(0, path_beneath.parent_fd);
- ruleset_fd = open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- /* Returns EBADF because ruleset_fd is not a landlock-ruleset FD. */
- ASSERT_EQ(EBADF, errno);
- ASSERT_EQ(0, close(ruleset_fd));
- ruleset_fd = open(dir_s1d1, O_DIRECTORY | O_CLOEXEC);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- /* Returns EBADFD because ruleset_fd is not a valid ruleset. */
- ASSERT_EQ(EBADFD, errno);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Gets a real ruleset. */
- ruleset_fd =
- landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(0, close(path_beneath.parent_fd));
- /* Tests without O_PATH. */
- path_beneath.parent_fd = open(dir_s1d2, O_DIRECTORY | O_CLOEXEC);
- ASSERT_LE(0, path_beneath.parent_fd);
- ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(0, close(path_beneath.parent_fd));
- /* Tests with a ruleset FD. */
- path_beneath.parent_fd = ruleset_fd;
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(EBADFD, errno);
- /* Checks unhandled allowed_access. */
- path_beneath.parent_fd =
- open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC);
- ASSERT_LE(0, path_beneath.parent_fd);
- /* Test with legitimate values. */
- path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_EXECUTE;
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(EINVAL, errno);
- path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_EXECUTE;
- /* Tests with denied-by-default access right. */
- path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_REFER;
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(EINVAL, errno);
- path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_REFER;
- /* Test with unknown (64-bits) value. */
- path_beneath.allowed_access |= (1ULL << 60);
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(EINVAL, errno);
- path_beneath.allowed_access &= ~(1ULL << 60);
- /* Test with no access. */
- path_beneath.allowed_access = 0;
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(ENOMSG, errno);
- path_beneath.allowed_access &= ~(1ULL << 60);
- ASSERT_EQ(0, close(path_beneath.parent_fd));
- /* Enforces the ruleset. */
- ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
- ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
- ASSERT_EQ(0, close(ruleset_fd));
- }
- /* clang-format off */
- #define ACCESS_FILE ( \
- LANDLOCK_ACCESS_FS_EXECUTE | \
- LANDLOCK_ACCESS_FS_WRITE_FILE | \
- LANDLOCK_ACCESS_FS_READ_FILE)
- #define ACCESS_LAST LANDLOCK_ACCESS_FS_REFER
- #define ACCESS_ALL ( \
- ACCESS_FILE | \
- LANDLOCK_ACCESS_FS_READ_DIR | \
- LANDLOCK_ACCESS_FS_REMOVE_DIR | \
- LANDLOCK_ACCESS_FS_REMOVE_FILE | \
- LANDLOCK_ACCESS_FS_MAKE_CHAR | \
- LANDLOCK_ACCESS_FS_MAKE_DIR | \
- LANDLOCK_ACCESS_FS_MAKE_REG | \
- LANDLOCK_ACCESS_FS_MAKE_SOCK | \
- LANDLOCK_ACCESS_FS_MAKE_FIFO | \
- LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
- LANDLOCK_ACCESS_FS_MAKE_SYM | \
- ACCESS_LAST)
- /* clang-format on */
- TEST_F_FORK(layout1, file_and_dir_access_rights)
- {
- __u64 access;
- int err;
- struct landlock_path_beneath_attr path_beneath_file = {},
- path_beneath_dir = {};
- struct landlock_ruleset_attr ruleset_attr = {
- .handled_access_fs = ACCESS_ALL,
- };
- const int ruleset_fd =
- landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
- ASSERT_LE(0, ruleset_fd);
- /* Tests access rights for files. */
- path_beneath_file.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC);
- ASSERT_LE(0, path_beneath_file.parent_fd);
- /* Tests access rights for directories. */
- path_beneath_dir.parent_fd =
- open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC);
- ASSERT_LE(0, path_beneath_dir.parent_fd);
- for (access = 1; access <= ACCESS_LAST; access <<= 1) {
- path_beneath_dir.allowed_access = access;
- ASSERT_EQ(0, landlock_add_rule(ruleset_fd,
- LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath_dir, 0));
- path_beneath_file.allowed_access = access;
- err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath_file, 0);
- if (access & ACCESS_FILE) {
- ASSERT_EQ(0, err);
- } else {
- ASSERT_EQ(-1, err);
- ASSERT_EQ(EINVAL, errno);
- }
- }
- ASSERT_EQ(0, close(path_beneath_file.parent_fd));
- ASSERT_EQ(0, close(path_beneath_dir.parent_fd));
- ASSERT_EQ(0, close(ruleset_fd));
- }
- TEST_F_FORK(layout1, unknown_access_rights)
- {
- __u64 access_mask;
- for (access_mask = 1ULL << 63; access_mask != ACCESS_LAST;
- access_mask >>= 1) {
- struct landlock_ruleset_attr ruleset_attr = {
- .handled_access_fs = access_mask,
- };
- ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr,
- sizeof(ruleset_attr), 0));
- ASSERT_EQ(EINVAL, errno);
- }
- }
- static void add_path_beneath(struct __test_metadata *const _metadata,
- const int ruleset_fd, const __u64 allowed_access,
- const char *const path)
- {
- struct landlock_path_beneath_attr path_beneath = {
- .allowed_access = allowed_access,
- };
- path_beneath.parent_fd = open(path, O_PATH | O_CLOEXEC);
- ASSERT_LE(0, path_beneath.parent_fd)
- {
- TH_LOG("Failed to open directory \"%s\": %s", path,
- strerror(errno));
- }
- ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0))
- {
- TH_LOG("Failed to update the ruleset with \"%s\": %s", path,
- strerror(errno));
- }
- ASSERT_EQ(0, close(path_beneath.parent_fd));
- }
- struct rule {
- const char *path;
- __u64 access;
- };
- /* clang-format off */
- #define ACCESS_RO ( \
- LANDLOCK_ACCESS_FS_READ_FILE | \
- LANDLOCK_ACCESS_FS_READ_DIR)
- #define ACCESS_RW ( \
- ACCESS_RO | \
- LANDLOCK_ACCESS_FS_WRITE_FILE)
- /* clang-format on */
- static int create_ruleset(struct __test_metadata *const _metadata,
- const __u64 handled_access_fs,
- const struct rule rules[])
- {
- int ruleset_fd, i;
- struct landlock_ruleset_attr ruleset_attr = {
- .handled_access_fs = handled_access_fs,
- };
- ASSERT_NE(NULL, rules)
- {
- TH_LOG("No rule list");
- }
- ASSERT_NE(NULL, rules[0].path)
- {
- TH_LOG("Empty rule list");
- }
- ruleset_fd =
- landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
- ASSERT_LE(0, ruleset_fd)
- {
- TH_LOG("Failed to create a ruleset: %s", strerror(errno));
- }
- for (i = 0; rules[i].path; i++) {
- add_path_beneath(_metadata, ruleset_fd, rules[i].access,
- rules[i].path);
- }
- return ruleset_fd;
- }
- static void enforce_ruleset(struct __test_metadata *const _metadata,
- const int ruleset_fd)
- {
- ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
- ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0))
- {
- TH_LOG("Failed to enforce ruleset: %s", strerror(errno));
- }
- }
- TEST_F_FORK(layout1, proc_nsfs)
- {
- const struct rule rules[] = {
- {
- .path = "/dev/null",
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- struct landlock_path_beneath_attr path_beneath;
- const int ruleset_fd = create_ruleset(
- _metadata, rules[0].access | LANDLOCK_ACCESS_FS_READ_DIR,
- rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
- ASSERT_EQ(EACCES, test_open("/dev", O_RDONLY));
- ASSERT_EQ(0, test_open("/dev/null", O_RDONLY));
- ASSERT_EQ(EACCES, test_open("/dev/full", O_RDONLY));
- ASSERT_EQ(EACCES, test_open("/proc", O_RDONLY));
- ASSERT_EQ(EACCES, test_open("/proc/self", O_RDONLY));
- ASSERT_EQ(EACCES, test_open("/proc/self/ns", O_RDONLY));
- /*
- * Because nsfs is an internal filesystem, /proc/self/ns/mnt is a
- * disconnected path. Such path cannot be identified and must then be
- * allowed.
- */
- ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY));
- /*
- * Checks that it is not possible to add nsfs-like filesystem
- * references to a ruleset.
- */
- path_beneath.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- path_beneath.parent_fd = open("/proc/self/ns/mnt", O_PATH | O_CLOEXEC);
- ASSERT_LE(0, path_beneath.parent_fd);
- ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
- &path_beneath, 0));
- ASSERT_EQ(EBADFD, errno);
- ASSERT_EQ(0, close(path_beneath.parent_fd));
- }
- TEST_F_FORK(layout1, unpriv)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = ACCESS_RO,
- },
- {},
- };
- int ruleset_fd;
- drop_caps(_metadata);
- ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(-1, landlock_restrict_self(ruleset_fd, 0));
- ASSERT_EQ(EPERM, errno);
- /* enforce_ruleset() calls prctl(no_new_privs). */
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- }
- TEST_F_FORK(layout1, effective_access)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = ACCESS_RO,
- },
- {
- .path = file1_s2d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- char buf;
- int reg_fd;
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Tests on a directory (with or without O_PATH). */
- ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
- ASSERT_EQ(0, test_open("/", O_RDONLY | O_PATH));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_PATH));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY | O_PATH));
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- /* Tests on a file (with or without O_PATH). */
- ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_PATH));
- ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY));
- /* Checks effective read and write actions. */
- reg_fd = open(file1_s2d2, O_RDWR | O_CLOEXEC);
- ASSERT_LE(0, reg_fd);
- ASSERT_EQ(1, write(reg_fd, ".", 1));
- ASSERT_LE(0, lseek(reg_fd, 0, SEEK_SET));
- ASSERT_EQ(1, read(reg_fd, &buf, 1));
- ASSERT_EQ('.', buf);
- ASSERT_EQ(0, close(reg_fd));
- /* Just in case, double-checks effective actions. */
- reg_fd = open(file1_s2d2, O_RDONLY | O_CLOEXEC);
- ASSERT_LE(0, reg_fd);
- ASSERT_EQ(-1, write(reg_fd, &buf, 1));
- ASSERT_EQ(EBADF, errno);
- ASSERT_EQ(0, close(reg_fd));
- }
- TEST_F_FORK(layout1, unhandled_access)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = ACCESS_RO,
- },
- {},
- };
- /* Here, we only handle read accesses, not write accesses. */
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /*
- * Because the policy does not handle LANDLOCK_ACCESS_FS_WRITE_FILE,
- * opening for write-only should be allowed, but not read-write.
- */
- ASSERT_EQ(0, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
- ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR));
- }
- TEST_F_FORK(layout1, ruleset_overlap)
- {
- const struct rule rules[] = {
- /* These rules should be ORed among them. */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_READ_DIR,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks s1d1 hierarchy. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d2 hierarchy. */
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR));
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* Checks s1d3 hierarchy. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- }
- TEST_F_FORK(layout1, layer_rule_unions)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- const struct rule layer2[] = {
- /* Doesn't change anything from layer1. */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- const struct rule layer3[] = {
- /* Only allows write (but not read) to dir_s1d3. */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks s1d1 hierarchy with layer1. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d2 hierarchy with layer1. */
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d3 hierarchy with layer1. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
- /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Doesn't change anything from layer1. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks s1d1 hierarchy with layer2. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d2 hierarchy with layer2. */
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d3 hierarchy with layer2. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
- /* dir_s1d3 should allow READ_FILE and WRITE_FILE (O_RDWR). */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Only allows write (but not read) to dir_s1d3. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks s1d1 hierarchy with layer3. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d2 hierarchy with layer3. */
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Checks s1d3 hierarchy with layer3. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY));
- /* dir_s1d3 should now deny READ_FILE and WRITE_FILE (O_RDWR). */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- }
- TEST_F_FORK(layout1, non_overlapping_accesses)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {},
- };
- const struct rule layer2[] = {
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {},
- };
- int ruleset_fd;
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ruleset_fd =
- create_ruleset(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_REMOVE_FILE,
- layer2);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Unchanged accesses for file creation. */
- ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0));
- /* Checks file removing. */
- ASSERT_EQ(-1, unlink(file1_s1d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, unlink(file1_s1d3));
- }
- TEST_F_FORK(layout1, interleaved_masked_accesses)
- {
- /*
- * Checks overly restrictive rules:
- * layer 1: allows R s1d1/s1d2/s1d3/file1
- * layer 2: allows RW s1d1/s1d2/s1d3
- * allows W s1d1/s1d2
- * denies R s1d1/s1d2
- * layer 3: allows R s1d1
- * layer 4: allows R s1d1/s1d2
- * denies W s1d1/s1d2
- * layer 5: allows R s1d1/s1d2
- * layer 6: allows X ----
- * layer 7: allows W s1d1/s1d2
- * denies R s1d1/s1d2
- */
- const struct rule layer1_read[] = {
- /* Allows read access to file1_s1d3 with the first layer. */
- {
- .path = file1_s1d3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- /* First rule with write restrictions. */
- const struct rule layer2_read_write[] = {
- /* Start by granting read-write access via its parent directory... */
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- /* ...but also denies read access via its grandparent directory. */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- const struct rule layer3_read[] = {
- /* Allows read access via its great-grandparent directory. */
- {
- .path = dir_s1d1,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- const struct rule layer4_read_write[] = {
- /*
- * Try to confuse the deny access by denying write (but not
- * read) access via its grandparent directory.
- */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- const struct rule layer5_read[] = {
- /*
- * Try to override layer2's deny read access by explicitly
- * allowing read access via file1_s1d3's grandparent.
- */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- const struct rule layer6_execute[] = {
- /*
- * Restricts an unrelated file hierarchy with a new access
- * (non-overlapping) type.
- */
- {
- .path = dir_s2d1,
- .access = LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {},
- };
- const struct rule layer7_read_write[] = {
- /*
- * Finally, denies read access to file1_s1d3 via its
- * grandparent.
- */
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- int ruleset_fd;
- ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
- layer1_read);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks that read access is granted for file1_s1d3 with layer 1. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY));
- ruleset_fd = create_ruleset(_metadata,
- LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- layer2_read_write);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks that previous access rights are unchanged with layer 2. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY));
- ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
- layer3_read);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks that previous access rights are unchanged with layer 3. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY));
- /* This time, denies write access for the file hierarchy. */
- ruleset_fd = create_ruleset(_metadata,
- LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- layer4_read_write);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /*
- * Checks that the only change with layer 4 is that write access is
- * denied.
- */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
- ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE,
- layer5_read);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks that previous access rights are unchanged with layer 5. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_EXECUTE,
- layer6_execute);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks that previous access rights are unchanged with layer 6. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- ruleset_fd = create_ruleset(_metadata,
- LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- layer7_read_write);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks read access is now denied with layer 7. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY));
- }
- TEST_F_FORK(layout1, inherit_subset)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_READ_DIR,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* Write access is forbidden. */
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- /* Readdir access is allowed. */
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* Write access is forbidden. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- /* Readdir access is allowed. */
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- /*
- * Tests shared rule extension: the following rules should not grant
- * any new access, only remove some. Once enforced, these rules are
- * ANDed with the previous ones.
- */
- add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE,
- dir_s1d2);
- /*
- * According to ruleset_fd, dir_s1d2 should now have the
- * LANDLOCK_ACCESS_FS_READ_FILE and LANDLOCK_ACCESS_FS_WRITE_FILE
- * access rights (even if this directory is opened a second time).
- * However, when enforcing this updated ruleset, the ruleset tied to
- * the current process (i.e. its domain) will still only have the
- * dir_s1d2 with LANDLOCK_ACCESS_FS_READ_FILE and
- * LANDLOCK_ACCESS_FS_READ_DIR accesses, but
- * LANDLOCK_ACCESS_FS_WRITE_FILE must not be allowed because it would
- * be a privilege escalation.
- */
- enforce_ruleset(_metadata, ruleset_fd);
- /* Same tests and results as above. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* It is still forbidden to write in file1_s1d2. */
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- /* Readdir access is still allowed. */
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* It is still forbidden to write in file1_s1d3. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- /* Readdir access is still allowed. */
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- /*
- * Try to get more privileges by adding new access rights to the parent
- * directory: dir_s1d1.
- */
- add_path_beneath(_metadata, ruleset_fd, ACCESS_RW, dir_s1d1);
- enforce_ruleset(_metadata, ruleset_fd);
- /* Same tests and results as above. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* It is still forbidden to write in file1_s1d2. */
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- /* Readdir access is still allowed. */
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* It is still forbidden to write in file1_s1d3. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- /* Readdir access is still allowed. */
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- /*
- * Now, dir_s1d3 get a new rule tied to it, only allowing
- * LANDLOCK_ACCESS_FS_WRITE_FILE. The (kernel internal) difference is
- * that there was no rule tied to it before.
- */
- add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE,
- dir_s1d3);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /*
- * Same tests and results as above, except for open(dir_s1d3) which is
- * now denied because the new rule mask the rule previously inherited
- * from dir_s1d2.
- */
- /* Same tests and results as above. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- /* It is still forbidden to write in file1_s1d2. */
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- /* Readdir access is still allowed. */
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* It is still forbidden to write in file1_s1d3. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- /*
- * Readdir of dir_s1d3 is still allowed because of the OR policy inside
- * the same layer.
- */
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- }
- TEST_F_FORK(layout1, inherit_superset)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d3,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- /* Readdir access is denied for dir_s1d2. */
- ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* Readdir access is allowed for dir_s1d3. */
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- /* File access is allowed for file1_s1d3. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- /* Now dir_s1d2, parent of dir_s1d3, gets a new rule tied to it. */
- add_path_beneath(_metadata, ruleset_fd,
- LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_READ_DIR,
- dir_s1d2);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Readdir access is still denied for dir_s1d2. */
- ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* Readdir access is still allowed for dir_s1d3. */
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- /* File access is still allowed for file1_s1d3. */
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- }
- TEST_F_FORK(layout1, max_layers)
- {
- int i, err;
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- for (i = 0; i < 16; i++)
- enforce_ruleset(_metadata, ruleset_fd);
- for (i = 0; i < 2; i++) {
- err = landlock_restrict_self(ruleset_fd, 0);
- ASSERT_EQ(-1, err);
- ASSERT_EQ(E2BIG, errno);
- }
- ASSERT_EQ(0, close(ruleset_fd));
- }
- TEST_F_FORK(layout1, empty_or_same_ruleset)
- {
- struct landlock_ruleset_attr ruleset_attr = {};
- int ruleset_fd;
- /* Tests empty handled_access_fs. */
- ruleset_fd =
- landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
- ASSERT_LE(-1, ruleset_fd);
- ASSERT_EQ(ENOMSG, errno);
- /* Enforces policy which deny read access to all files. */
- ruleset_attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE;
- ruleset_fd =
- landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- /* Nests a policy which deny read access to all directories. */
- ruleset_attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR;
- ruleset_fd =
- landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
- /* Enforces a second time with the same ruleset. */
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- }
- TEST_F_FORK(layout1, rule_on_mountpoint)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d1,
- .access = ACCESS_RO,
- },
- {
- /* dir_s3d2 is a mount point. */
- .path = dir_s3d2,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s3d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY));
- }
- TEST_F_FORK(layout1, rule_over_mountpoint)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d1,
- .access = ACCESS_RO,
- },
- {
- /* dir_s3d2 is a mount point. */
- .path = dir_s3d1,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY));
- }
- /*
- * This test verifies that we can apply a landlock rule on the root directory
- * (which might require special handling).
- */
- TEST_F_FORK(layout1, rule_over_root_allow_then_deny)
- {
- struct rule rules[] = {
- {
- .path = "/",
- .access = ACCESS_RO,
- },
- {},
- };
- int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks allowed access. */
- ASSERT_EQ(0, test_open("/", O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- rules[0].access = LANDLOCK_ACCESS_FS_READ_FILE;
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks denied access (on a directory). */
- ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
- }
- TEST_F_FORK(layout1, rule_over_root_deny)
- {
- const struct rule rules[] = {
- {
- .path = "/",
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks denied access (on a directory). */
- ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY));
- }
- TEST_F_FORK(layout1, rule_inside_mount_ns)
- {
- const struct rule rules[] = {
- {
- .path = "s3d3",
- .access = ACCESS_RO,
- },
- {},
- };
- int ruleset_fd;
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3))
- {
- TH_LOG("Failed to pivot root: %s", strerror(errno));
- };
- ASSERT_EQ(0, chdir("/"));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, test_open("s3d3", O_RDONLY));
- ASSERT_EQ(EACCES, test_open("/", O_RDONLY));
- }
- TEST_F_FORK(layout1, mount_and_pivot)
- {
- const struct rule rules[] = {
- {
- .path = dir_s3d2,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_RDONLY, NULL));
- ASSERT_EQ(EPERM, errno);
- ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3));
- ASSERT_EQ(EPERM, errno);
- clear_cap(_metadata, CAP_SYS_ADMIN);
- }
- TEST_F_FORK(layout1, move_mount)
- {
- const struct rule rules[] = {
- {
- .path = dir_s3d2,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD,
- dir_s1d2, 0))
- {
- TH_LOG("Failed to move mount: %s", strerror(errno));
- }
- ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD,
- dir_s3d2, 0));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD,
- dir_s1d2, 0));
- ASSERT_EQ(EPERM, errno);
- clear_cap(_metadata, CAP_SYS_ADMIN);
- }
- TEST_F_FORK(layout1, release_inodes)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d1,
- .access = ACCESS_RO,
- },
- {
- .path = dir_s3d2,
- .access = ACCESS_RO,
- },
- {
- .path = dir_s3d3,
- .access = ACCESS_RO,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules);
- ASSERT_LE(0, ruleset_fd);
- /* Unmount a file hierarchy while it is being used by a ruleset. */
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, umount(dir_s3d2));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(dir_s3d2, O_RDONLY));
- /* This dir_s3d3 would not be allowed and does not exist anyway. */
- ASSERT_EQ(ENOENT, test_open(dir_s3d3, O_RDONLY));
- }
- enum relative_access {
- REL_OPEN,
- REL_CHDIR,
- REL_CHROOT_ONLY,
- REL_CHROOT_CHDIR,
- };
- static void test_relative_path(struct __test_metadata *const _metadata,
- const enum relative_access rel)
- {
- /*
- * Common layer to check that chroot doesn't ignore it (i.e. a chroot
- * is not a disconnected root directory).
- */
- const struct rule layer1_base[] = {
- {
- .path = TMP_DIR,
- .access = ACCESS_RO,
- },
- {},
- };
- const struct rule layer2_subs[] = {
- {
- .path = dir_s1d2,
- .access = ACCESS_RO,
- },
- {
- .path = dir_s2d2,
- .access = ACCESS_RO,
- },
- {},
- };
- int dirfd, ruleset_fd;
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_subs);
- ASSERT_LE(0, ruleset_fd);
- switch (rel) {
- case REL_OPEN:
- case REL_CHDIR:
- break;
- case REL_CHROOT_ONLY:
- ASSERT_EQ(0, chdir(dir_s2d2));
- break;
- case REL_CHROOT_CHDIR:
- ASSERT_EQ(0, chdir(dir_s1d2));
- break;
- default:
- ASSERT_TRUE(false);
- return;
- }
- set_cap(_metadata, CAP_SYS_CHROOT);
- enforce_ruleset(_metadata, ruleset_fd);
- switch (rel) {
- case REL_OPEN:
- dirfd = open(dir_s1d2, O_DIRECTORY);
- ASSERT_LE(0, dirfd);
- break;
- case REL_CHDIR:
- ASSERT_EQ(0, chdir(dir_s1d2));
- dirfd = AT_FDCWD;
- break;
- case REL_CHROOT_ONLY:
- /* Do chroot into dir_s1d2 (relative to dir_s2d2). */
- ASSERT_EQ(0, chroot("../../s1d1/s1d2"))
- {
- TH_LOG("Failed to chroot: %s", strerror(errno));
- }
- dirfd = AT_FDCWD;
- break;
- case REL_CHROOT_CHDIR:
- /* Do chroot into dir_s1d2. */
- ASSERT_EQ(0, chroot("."))
- {
- TH_LOG("Failed to chroot: %s", strerror(errno));
- }
- dirfd = AT_FDCWD;
- break;
- }
- ASSERT_EQ((rel == REL_CHROOT_CHDIR) ? 0 : EACCES,
- test_open_rel(dirfd, "..", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, ".", O_RDONLY));
- if (rel == REL_CHROOT_ONLY) {
- /* The current directory is dir_s2d2. */
- ASSERT_EQ(0, test_open_rel(dirfd, "./s2d3", O_RDONLY));
- } else {
- /* The current directory is dir_s1d2. */
- ASSERT_EQ(0, test_open_rel(dirfd, "./s1d3", O_RDONLY));
- }
- if (rel == REL_CHROOT_ONLY || rel == REL_CHROOT_CHDIR) {
- /* Checks the root dir_s1d2. */
- ASSERT_EQ(0, test_open_rel(dirfd, "/..", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "/", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "/f1", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "/s1d3", O_RDONLY));
- }
- if (rel != REL_CHROOT_CHDIR) {
- ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s1d1", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2/s1d3",
- O_RDONLY));
- ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s2d1", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2", O_RDONLY));
- ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2/s2d3",
- O_RDONLY));
- }
- if (rel == REL_OPEN)
- ASSERT_EQ(0, close(dirfd));
- ASSERT_EQ(0, close(ruleset_fd));
- }
- TEST_F_FORK(layout1, relative_open)
- {
- test_relative_path(_metadata, REL_OPEN);
- }
- TEST_F_FORK(layout1, relative_chdir)
- {
- test_relative_path(_metadata, REL_CHDIR);
- }
- TEST_F_FORK(layout1, relative_chroot_only)
- {
- test_relative_path(_metadata, REL_CHROOT_ONLY);
- }
- TEST_F_FORK(layout1, relative_chroot_chdir)
- {
- test_relative_path(_metadata, REL_CHROOT_CHDIR);
- }
- static void copy_binary(struct __test_metadata *const _metadata,
- const char *const dst_path)
- {
- int dst_fd, src_fd;
- struct stat statbuf;
- dst_fd = open(dst_path, O_WRONLY | O_TRUNC | O_CLOEXEC);
- ASSERT_LE(0, dst_fd)
- {
- TH_LOG("Failed to open \"%s\": %s", dst_path, strerror(errno));
- }
- src_fd = open(BINARY_PATH, O_RDONLY | O_CLOEXEC);
- ASSERT_LE(0, src_fd)
- {
- TH_LOG("Failed to open \"" BINARY_PATH "\": %s",
- strerror(errno));
- }
- ASSERT_EQ(0, fstat(src_fd, &statbuf));
- ASSERT_EQ(statbuf.st_size,
- sendfile(dst_fd, src_fd, 0, statbuf.st_size));
- ASSERT_EQ(0, close(src_fd));
- ASSERT_EQ(0, close(dst_fd));
- }
- static void test_execute(struct __test_metadata *const _metadata, const int err,
- const char *const path)
- {
- int status;
- char *const argv[] = { (char *)path, NULL };
- const pid_t child = fork();
- ASSERT_LE(0, child);
- if (child == 0) {
- ASSERT_EQ(err ? -1 : 0, execve(path, argv, NULL))
- {
- TH_LOG("Failed to execute \"%s\": %s", path,
- strerror(errno));
- };
- ASSERT_EQ(err, errno);
- _exit(_metadata->passed ? 2 : 1);
- return;
- }
- ASSERT_EQ(child, waitpid(child, &status, 0));
- ASSERT_EQ(1, WIFEXITED(status));
- ASSERT_EQ(err ? 2 : 0, WEXITSTATUS(status))
- {
- TH_LOG("Unexpected return code for \"%s\": %s", path,
- strerror(errno));
- };
- }
- TEST_F_FORK(layout1, execute)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- copy_binary(_metadata, file1_s1d1);
- copy_binary(_metadata, file1_s1d2);
- copy_binary(_metadata, file1_s1d3);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
- test_execute(_metadata, EACCES, file1_s1d1);
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- test_execute(_metadata, 0, file1_s1d2);
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- test_execute(_metadata, 0, file1_s1d3);
- }
- TEST_F_FORK(layout1, link)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {},
- };
- const struct rule layer2[] = {
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {},
- };
- int ruleset_fd = create_ruleset(_metadata, layer1[0].access, layer1);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- /* Denies linking because of reparenting. */
- ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, link(file2_s1d3, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(0, link(file2_s1d2, file1_s1d2));
- ASSERT_EQ(0, link(file2_s1d3, file1_s1d3));
- /* Prepares for next unlinks. */
- ASSERT_EQ(0, unlink(file2_s1d2));
- ASSERT_EQ(0, unlink(file2_s1d3));
- ruleset_fd = create_ruleset(_metadata, layer2[0].access, layer2);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks that linkind doesn't require the ability to delete a file. */
- ASSERT_EQ(0, link(file1_s1d2, file2_s1d2));
- ASSERT_EQ(0, link(file1_s1d3, file2_s1d3));
- }
- static int test_rename(const char *const oldpath, const char *const newpath)
- {
- if (rename(oldpath, newpath))
- return errno;
- return 0;
- }
- static int test_exchange(const char *const oldpath, const char *const newpath)
- {
- if (renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_EXCHANGE))
- return errno;
- return 0;
- }
- TEST_F_FORK(layout1, rename_file)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {
- .path = dir_s2d2,
- .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, unlink(file1_s1d2));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /*
- * Tries to replace a file, from a directory that allows file removal,
- * but to a different directory (which also allows file removal).
- */
- ASSERT_EQ(-1, rename(file1_s2d3, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Tries to replace a file, from a directory that denies file removal,
- * to a different directory (which allows file removal).
- */
- ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file1_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- /* Exchanges files and directories that partially allow removal. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s2d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks that file1_s2d1 cannot be removed (instead of ENOTDIR). */
- ASSERT_EQ(-1, rename(dir_s2d2, file1_s2d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, dir_s2d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks that file1_s1d1 cannot be removed (instead of EISDIR). */
- ASSERT_EQ(-1, rename(file1_s1d1, dir_s1d2));
- ASSERT_EQ(EACCES, errno);
- /* Renames files with different parents. */
- ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3));
- ASSERT_EQ(EACCES, errno);
- /* Exchanges and renames files with same parent. */
- ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(0, rename(file2_s2d3, file1_s2d3));
- /* Exchanges files and directories with same parent, twice. */
- ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- }
- TEST_F_FORK(layout1, rename_dir)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REMOVE_DIR,
- },
- {
- .path = dir_s2d1,
- .access = LANDLOCK_ACCESS_FS_REMOVE_DIR,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- /* Empties dir_s1d3 to allow renaming. */
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Exchanges and renames directory to a different parent. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(dir_s2d3, dir_s1d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Exchanges directory to the same parent, which doesn't allow
- * directory removal.
- */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d1, AT_FDCWD, dir_s2d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks that dir_s1d2 cannot be removed (instead of ENOTDIR). */
- ASSERT_EQ(-1, rename(dir_s1d2, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s1d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks that dir_s1d2 cannot be removed (instead of EISDIR). */
- ASSERT_EQ(-1, rename(file1_s1d1, dir_s1d2));
- ASSERT_EQ(EACCES, errno);
- /*
- * Exchanges and renames directory to the same parent, which allows
- * directory removal.
- */
- ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s1d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(0, unlink(dir_s1d3));
- ASSERT_EQ(0, mkdir(dir_s1d3, 0700));
- ASSERT_EQ(0, rename(file1_s1d2, dir_s1d3));
- ASSERT_EQ(0, rmdir(dir_s1d3));
- }
- TEST_F_FORK(layout1, reparent_refer)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = dir_s2d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {},
- };
- int ruleset_fd =
- create_ruleset(_metadata, LANDLOCK_ACCESS_FS_REFER, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d1));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(dir_s1d2, dir_s2d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d1));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d2));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Moving should only be allowed when the source and the destination
- * parent directory have REFER.
- */
- ASSERT_EQ(-1, rename(dir_s1d3, dir_s2d3));
- ASSERT_EQ(ENOTEMPTY, errno);
- ASSERT_EQ(0, unlink(file1_s2d3));
- ASSERT_EQ(0, unlink(file2_s2d3));
- ASSERT_EQ(0, rename(dir_s1d3, dir_s2d3));
- }
- /* Checks renames beneath dir_s1d1. */
- static void refer_denied_by_default(struct __test_metadata *const _metadata,
- const struct rule layer1[],
- const int layer1_err,
- const struct rule layer2[])
- {
- int ruleset_fd;
- ASSERT_EQ(0, unlink(file1_s1d2));
- ruleset_fd = create_ruleset(_metadata, layer1[0].access, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /*
- * If the first layer handles LANDLOCK_ACCESS_FS_REFER (according to
- * layer1_err), then it allows some different-parent renames and links.
- */
- ASSERT_EQ(layer1_err, test_rename(file1_s1d1, file1_s1d2));
- if (layer1_err == 0)
- ASSERT_EQ(layer1_err, test_rename(file1_s1d2, file1_s1d1));
- ASSERT_EQ(layer1_err, test_exchange(file2_s1d1, file2_s1d2));
- ASSERT_EQ(layer1_err, test_exchange(file2_s1d2, file2_s1d1));
- ruleset_fd = create_ruleset(_metadata, layer2[0].access, layer2);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /*
- * Now, either the first or the second layer does not handle
- * LANDLOCK_ACCESS_FS_REFER, which means that any different-parent
- * renames and links are denied, thus making the layer handling
- * LANDLOCK_ACCESS_FS_REFER null and void.
- */
- ASSERT_EQ(EXDEV, test_rename(file1_s1d1, file1_s1d2));
- ASSERT_EQ(EXDEV, test_exchange(file2_s1d1, file2_s1d2));
- ASSERT_EQ(EXDEV, test_exchange(file2_s1d2, file2_s1d1));
- }
- const struct rule layer_dir_s1d1_refer[] = {
- {
- .path = dir_s1d1,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {},
- };
- const struct rule layer_dir_s1d1_execute[] = {
- {
- /* Matches a parent directory. */
- .path = dir_s1d1,
- .access = LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {},
- };
- const struct rule layer_dir_s2d1_execute[] = {
- {
- /* Does not match a parent directory. */
- .path = dir_s2d1,
- .access = LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {},
- };
- /*
- * Tests precedence over renames: denied by default for different parent
- * directories, *with* a rule matching a parent directory, but not directly
- * denying access (with MAKE_REG nor REMOVE).
- */
- TEST_F_FORK(layout1, refer_denied_by_default1)
- {
- refer_denied_by_default(_metadata, layer_dir_s1d1_refer, 0,
- layer_dir_s1d1_execute);
- }
- /*
- * Same test but this time turning around the ABI version order: the first
- * layer does not handle LANDLOCK_ACCESS_FS_REFER.
- */
- TEST_F_FORK(layout1, refer_denied_by_default2)
- {
- refer_denied_by_default(_metadata, layer_dir_s1d1_execute, EXDEV,
- layer_dir_s1d1_refer);
- }
- /*
- * Tests precedence over renames: denied by default for different parent
- * directories, *without* a rule matching a parent directory, but not directly
- * denying access (with MAKE_REG nor REMOVE).
- */
- TEST_F_FORK(layout1, refer_denied_by_default3)
- {
- refer_denied_by_default(_metadata, layer_dir_s1d1_refer, 0,
- layer_dir_s2d1_execute);
- }
- /*
- * Same test but this time turning around the ABI version order: the first
- * layer does not handle LANDLOCK_ACCESS_FS_REFER.
- */
- TEST_F_FORK(layout1, refer_denied_by_default4)
- {
- refer_denied_by_default(_metadata, layer_dir_s2d1_execute, EXDEV,
- layer_dir_s1d1_refer);
- }
- TEST_F_FORK(layout1, reparent_link)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = dir_s2d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = dir_s2d3,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(
- _metadata,
- LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- /* Denies linking because of missing MAKE_REG. */
- ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- /* Denies linking because of missing source and destination REFER. */
- ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- /* Denies linking because of missing source REFER. */
- ASSERT_EQ(-1, link(file1_s2d1, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- /* Denies linking because of missing MAKE_REG. */
- ASSERT_EQ(-1, link(file1_s2d2, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- /* Denies linking because of missing destination REFER. */
- ASSERT_EQ(-1, link(file1_s2d2, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- /* Allows linking because of REFER and MAKE_REG. */
- ASSERT_EQ(0, link(file1_s2d2, file1_s1d3));
- ASSERT_EQ(0, unlink(file1_s2d2));
- /* Reverse linking denied because of missing MAKE_REG. */
- ASSERT_EQ(-1, link(file1_s1d3, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, unlink(file1_s2d3));
- /* Checks reverse linking. */
- ASSERT_EQ(0, link(file1_s1d3, file1_s2d3));
- ASSERT_EQ(0, unlink(file1_s1d3));
- /*
- * This is OK for a file link, but it should not be allowed for a
- * directory rename (because of the superset of access rights.
- */
- ASSERT_EQ(0, link(file1_s2d3, file1_s1d3));
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, link(file2_s1d3, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(0, link(file2_s1d2, file1_s1d2));
- ASSERT_EQ(0, link(file2_s1d3, file1_s1d3));
- }
- TEST_F_FORK(layout1, reparent_rename)
- {
- /* Same rules as for reparent_link. */
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = dir_s2d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = dir_s2d3,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(
- _metadata,
- LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- /* Denies renaming because of missing MAKE_REG. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file1_s1d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file2_s1d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- /* Even denies same file exchange. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file2_s1d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Denies renaming because of missing source and destination REFER. */
- ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Denies renaming because of missing MAKE_REG, source and destination
- * REFER.
- */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file2_s1d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file2_s1d1, AT_FDCWD, file1_s2d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Denies renaming because of missing source REFER. */
- ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- /* Denies renaming because of missing MAKE_REG. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file2_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Denies renaming because of missing MAKE_REG. */
- ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- /* Denies renaming because of missing destination REFER*/
- ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- /* Denies exchange because of one missing MAKE_REG. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, file2_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Allows renaming because of REFER and MAKE_REG. */
- ASSERT_EQ(0, rename(file1_s2d2, file1_s1d3));
- /* Reverse renaming denied because of missing MAKE_REG. */
- ASSERT_EQ(-1, rename(file1_s1d3, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, unlink(file1_s2d3));
- ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
- /* Tests reverse renaming. */
- ASSERT_EQ(0, rename(file1_s2d3, file1_s1d3));
- ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
- /*
- * This is OK for a file rename, but it should not be allowed for a
- * directory rename (because of the superset of access rights).
- */
- ASSERT_EQ(0, rename(file1_s2d3, file1_s1d3));
- ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
- /*
- * Tests superset restrictions applied to directories. Not only the
- * dir_s2d3's parent (dir_s2d2) should be taken into account but also
- * access rights tied to dir_s2d3. dir_s2d2 is missing one access right
- * compared to dir_s1d3/file1_s1d3 (MAKE_REG) but it is provided
- * directly by the moved dir_s2d3.
- */
- ASSERT_EQ(0, rename(dir_s2d3, file1_s1d3));
- ASSERT_EQ(0, rename(file1_s1d3, dir_s2d3));
- /*
- * The first rename is allowed but not the exchange because dir_s1d3's
- * parent (dir_s1d2) doesn't have REFER.
- */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(file1_s2d3, dir_s1d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(file2_s1d2, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(file2_s1d3, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- /* Renaming in the same directory is always allowed. */
- ASSERT_EQ(0, rename(file2_s1d2, file1_s1d2));
- ASSERT_EQ(0, rename(file2_s1d3, file1_s1d3));
- ASSERT_EQ(0, unlink(file1_s1d2));
- /* Denies because of missing source MAKE_REG and destination REFER. */
- ASSERT_EQ(-1, rename(dir_s2d3, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(0, unlink(file1_s1d3));
- /* Denies because of missing source MAKE_REG and REFER. */
- ASSERT_EQ(-1, rename(dir_s2d2, file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- }
- static void
- reparent_exdev_layers_enforce1(struct __test_metadata *const _metadata)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- /* Interesting for the layer2 tests. */
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {
- .path = dir_s2d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = dir_s2d3,
- .access = LANDLOCK_ACCESS_FS_MAKE_REG,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(
- _metadata,
- LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_REFER, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- }
- static void
- reparent_exdev_layers_enforce2(struct __test_metadata *const _metadata)
- {
- const struct rule layer2[] = {
- {
- .path = dir_s2d3,
- .access = LANDLOCK_ACCESS_FS_MAKE_DIR,
- },
- {},
- };
- /*
- * Same checks as before but with a second layer and a new MAKE_DIR
- * rule (and no explicit handling of REFER).
- */
- const int ruleset_fd =
- create_ruleset(_metadata, LANDLOCK_ACCESS_FS_MAKE_DIR, layer2);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- }
- TEST_F_FORK(layout1, reparent_exdev_layers_rename1)
- {
- ASSERT_EQ(0, unlink(file1_s2d2));
- ASSERT_EQ(0, unlink(file1_s2d3));
- reparent_exdev_layers_enforce1(_metadata);
- /*
- * Moving the dir_s1d3 directory below dir_s2d2 is allowed by Landlock
- * because it doesn't inherit new access rights.
- */
- ASSERT_EQ(0, rename(dir_s1d3, file1_s2d2));
- ASSERT_EQ(0, rename(file1_s2d2, dir_s1d3));
- /*
- * Moving the dir_s1d3 directory below dir_s2d3 is allowed, even if it
- * gets a new inherited access rights (MAKE_REG), because MAKE_REG is
- * already allowed for dir_s1d3.
- */
- ASSERT_EQ(0, rename(dir_s1d3, file1_s2d3));
- ASSERT_EQ(0, rename(file1_s2d3, dir_s1d3));
- /*
- * However, moving the file1_s1d3 file below dir_s2d3 is allowed
- * because it cannot inherit MAKE_REG right (which is dedicated to
- * directories).
- */
- ASSERT_EQ(0, rename(file1_s1d3, file1_s2d3));
- reparent_exdev_layers_enforce2(_metadata);
- /*
- * Moving the dir_s1d3 directory below dir_s2d2 is now denied because
- * MAKE_DIR is not tied to dir_s2d2.
- */
- ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- /*
- * Moving the dir_s1d3 directory below dir_s2d3 is forbidden because it
- * would grants MAKE_REG and MAKE_DIR rights to it.
- */
- ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Moving the file2_s1d3 file below dir_s2d3 is denied because the
- * second layer does not handle REFER, which is always denied by
- * default.
- */
- ASSERT_EQ(-1, rename(file2_s1d3, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- }
- TEST_F_FORK(layout1, reparent_exdev_layers_rename2)
- {
- reparent_exdev_layers_enforce1(_metadata);
- /* Checks EACCES predominance over EXDEV. */
- ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- /* Modify layout! */
- ASSERT_EQ(0, rename(file1_s1d2, file1_s2d3));
- /* Without REFER source. */
- ASSERT_EQ(-1, rename(dir_s1d1, file1_s2d2));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d2));
- ASSERT_EQ(EXDEV, errno);
- reparent_exdev_layers_enforce2(_metadata);
- /* Checks EACCES predominance over EXDEV. */
- ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- /* Checks with actual file2_s1d2. */
- ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(file1_s1d1, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Modifying the layout is now denied because the second layer does not
- * handle REFER, which is always denied by default.
- */
- ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- /* Without REFER source, EACCES wins over EXDEV. */
- ASSERT_EQ(-1, rename(dir_s1d1, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d2));
- ASSERT_EQ(EACCES, errno);
- }
- TEST_F_FORK(layout1, reparent_exdev_layers_exchange1)
- {
- const char *const dir_file1_s1d2 = file1_s1d2, *const dir_file2_s2d3 =
- file2_s2d3;
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, mkdir(file1_s1d2, 0700));
- ASSERT_EQ(0, unlink(file2_s2d3));
- ASSERT_EQ(0, mkdir(file2_s2d3, 0700));
- reparent_exdev_layers_enforce1(_metadata);
- /* Error predominance with file exchange: returns EXDEV and EACCES. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /*
- * Checks with directories which creation could be allowed, but denied
- * because of access rights that would be inherited.
- */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD,
- dir_file2_s2d3, RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD,
- dir_file1_s1d2, RENAME_EXCHANGE));
- ASSERT_EQ(EXDEV, errno);
- /* Checks with same access rights. */
- ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- /* Checks with different (child-only) access rights. */
- ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_file1_s1d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(0, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- /*
- * Checks that exchange between file and directory are consistent.
- *
- * Moving a file (file1_s2d2) to a directory which only grants more
- * directory-related access rights is allowed, and at the same time
- * moving a directory (dir_file2_s2d3) to another directory which
- * grants less access rights is allowed too.
- *
- * See layout1.reparent_exdev_layers_exchange3 for inverted arguments.
- */
- ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
- RENAME_EXCHANGE));
- /*
- * However, moving back the directory is denied because it would get
- * more access rights than the current state and because file creation
- * is forbidden (in dir_s2d2).
- */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- reparent_exdev_layers_enforce2(_metadata);
- /* Error predominance with file exchange: returns EXDEV and EACCES. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, file1_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d1,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks with directories which creation is now denied. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD,
- dir_file2_s2d3, RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD,
- dir_file1_s1d2, RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks with different (child-only) access rights. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- /* Denied because of MAKE_DIR. */
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Checks with different (child-only) access rights. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_file1_s1d2,
- RENAME_EXCHANGE));
- /* Denied because of MAKE_DIR. */
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file1_s1d2, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* See layout1.reparent_exdev_layers_exchange2 for complement. */
- }
- TEST_F_FORK(layout1, reparent_exdev_layers_exchange2)
- {
- const char *const dir_file2_s2d3 = file2_s2d3;
- ASSERT_EQ(0, unlink(file2_s2d3));
- ASSERT_EQ(0, mkdir(file2_s2d3, 0700));
- reparent_exdev_layers_enforce1(_metadata);
- reparent_exdev_layers_enforce2(_metadata);
- /* Checks that exchange between file and directory are consistent. */
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- }
- TEST_F_FORK(layout1, reparent_exdev_layers_exchange3)
- {
- const char *const dir_file2_s2d3 = file2_s2d3;
- ASSERT_EQ(0, unlink(file2_s2d3));
- ASSERT_EQ(0, mkdir(file2_s2d3, 0700));
- reparent_exdev_layers_enforce1(_metadata);
- /*
- * Checks that exchange between file and directory are consistent,
- * including with inverted arguments (see
- * layout1.reparent_exdev_layers_exchange1).
- */
- ASSERT_EQ(0, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_file2_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_file2_s2d3, AT_FDCWD, file1_s2d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- }
- TEST_F_FORK(layout1, reparent_remove)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d1,
- .access = LANDLOCK_ACCESS_FS_REFER |
- LANDLOCK_ACCESS_FS_REMOVE_DIR,
- },
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {
- .path = dir_s2d1,
- .access = LANDLOCK_ACCESS_FS_REFER |
- LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(
- _metadata,
- LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_REMOVE_DIR |
- LANDLOCK_ACCESS_FS_REMOVE_FILE,
- layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Access denied because of wrong/swapped remove file/dir. */
- ASSERT_EQ(-1, rename(file1_s1d1, dir_s2d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(dir_s2d2, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s2d2,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s2d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- /* Access allowed thanks to the matching rights. */
- ASSERT_EQ(-1, rename(file1_s2d1, dir_s1d2));
- ASSERT_EQ(EISDIR, errno);
- ASSERT_EQ(-1, rename(dir_s1d2, file1_s2d1));
- ASSERT_EQ(ENOTDIR, errno);
- ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d1));
- ASSERT_EQ(ENOTDIR, errno);
- ASSERT_EQ(0, unlink(file1_s2d1));
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- ASSERT_EQ(0, rename(dir_s1d3, file1_s2d1));
- /* Effectively removes a file and a directory by exchanging them. */
- ASSERT_EQ(0, mkdir(dir_s1d3, 0700));
- ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3,
- RENAME_EXCHANGE));
- ASSERT_EQ(EACCES, errno);
- }
- TEST_F_FORK(layout1, reparent_dom_superset)
- {
- const struct rule layer1[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = file1_s1d2,
- .access = LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_MAKE_SOCK |
- LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {
- .path = dir_s2d2,
- .access = LANDLOCK_ACCESS_FS_REFER |
- LANDLOCK_ACCESS_FS_EXECUTE |
- LANDLOCK_ACCESS_FS_MAKE_SOCK,
- },
- {
- .path = dir_s2d3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_MAKE_FIFO,
- },
- {},
- };
- int ruleset_fd = create_ruleset(_metadata,
- LANDLOCK_ACCESS_FS_REFER |
- LANDLOCK_ACCESS_FS_EXECUTE |
- LANDLOCK_ACCESS_FS_MAKE_SOCK |
- LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_MAKE_FIFO,
- layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d1));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Moving file1_s1d2 beneath dir_s2d3 would grant it the READ_FILE
- * access right.
- */
- ASSERT_EQ(-1, rename(file1_s1d2, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Moving file1_s1d2 should be allowed even if dir_s2d2 grants a
- * superset of access rights compared to dir_s1d2, because file1_s1d2
- * already has these access rights anyway.
- */
- ASSERT_EQ(0, rename(file1_s1d2, file1_s2d2));
- ASSERT_EQ(0, rename(file1_s2d2, file1_s1d2));
- ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d1));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Moving dir_s1d3 beneath dir_s2d3 would grant it the MAKE_FIFO access
- * right.
- */
- ASSERT_EQ(-1, rename(dir_s1d3, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- /*
- * Moving dir_s1d3 should be allowed even if dir_s2d2 grants a superset
- * of access rights compared to dir_s1d2, because dir_s1d3 already has
- * these access rights anyway.
- */
- ASSERT_EQ(0, rename(dir_s1d3, file1_s2d2));
- ASSERT_EQ(0, rename(file1_s2d2, dir_s1d3));
- /*
- * Moving file1_s2d3 beneath dir_s1d2 is allowed, but moving it back
- * will be denied because the new inherited access rights from dir_s1d2
- * will be less than the destination (original) dir_s2d3. This is a
- * sinkhole scenario where we cannot move back files or directories.
- */
- ASSERT_EQ(0, rename(file1_s2d3, file2_s1d2));
- ASSERT_EQ(-1, rename(file2_s1d2, file1_s2d3));
- ASSERT_EQ(EXDEV, errno);
- ASSERT_EQ(0, unlink(file2_s1d2));
- ASSERT_EQ(0, unlink(file2_s2d3));
- /*
- * Checks similar directory one-way move: dir_s2d3 loses EXECUTE and
- * MAKE_SOCK which were inherited from dir_s1d3.
- */
- ASSERT_EQ(0, rename(dir_s2d3, file2_s1d2));
- ASSERT_EQ(-1, rename(file2_s1d2, dir_s2d3));
- ASSERT_EQ(EXDEV, errno);
- }
- TEST_F_FORK(layout1, remove_dir)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REMOVE_DIR,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(0, rmdir(dir_s1d3));
- ASSERT_EQ(0, mkdir(dir_s1d3, 0700));
- ASSERT_EQ(0, unlinkat(AT_FDCWD, dir_s1d3, AT_REMOVEDIR));
- /* dir_s1d2 itself cannot be removed. */
- ASSERT_EQ(-1, rmdir(dir_s1d2));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d2, AT_REMOVEDIR));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rmdir(dir_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d1, AT_REMOVEDIR));
- ASSERT_EQ(EACCES, errno);
- }
- TEST_F_FORK(layout1, remove_file)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_REMOVE_FILE,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, unlink(file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, unlinkat(AT_FDCWD, file1_s1d1, 0));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlinkat(AT_FDCWD, file1_s1d3, 0));
- }
- static void test_make_file(struct __test_metadata *const _metadata,
- const __u64 access, const mode_t mode,
- const dev_t dev)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = access,
- },
- {},
- };
- const int ruleset_fd = create_ruleset(_metadata, access, rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file2_s1d1));
- ASSERT_EQ(0, mknod(file2_s1d1, mode | 0400, dev))
- {
- TH_LOG("Failed to make file \"%s\": %s", file2_s1d1,
- strerror(errno));
- };
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file2_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, mknod(file1_s1d1, mode | 0400, dev));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, mknod(file1_s1d2, mode | 0400, dev))
- {
- TH_LOG("Failed to make file \"%s\": %s", file1_s1d2,
- strerror(errno));
- };
- ASSERT_EQ(0, link(file1_s1d2, file2_s1d2));
- ASSERT_EQ(0, unlink(file2_s1d2));
- ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2));
- ASSERT_EQ(0, mknod(file1_s1d3, mode | 0400, dev));
- ASSERT_EQ(0, link(file1_s1d3, file2_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3));
- }
- TEST_F_FORK(layout1, make_char)
- {
- /* Creates a /dev/null device. */
- set_cap(_metadata, CAP_MKNOD);
- test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_CHAR, S_IFCHR,
- makedev(1, 3));
- }
- TEST_F_FORK(layout1, make_block)
- {
- /* Creates a /dev/loop0 device. */
- set_cap(_metadata, CAP_MKNOD);
- test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_BLOCK, S_IFBLK,
- makedev(7, 0));
- }
- TEST_F_FORK(layout1, make_reg_1)
- {
- test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, S_IFREG, 0);
- }
- TEST_F_FORK(layout1, make_reg_2)
- {
- test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, 0, 0);
- }
- TEST_F_FORK(layout1, make_sock)
- {
- test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_SOCK, S_IFSOCK, 0);
- }
- TEST_F_FORK(layout1, make_fifo)
- {
- test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_FIFO, S_IFIFO, 0);
- }
- TEST_F_FORK(layout1, make_sym)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_MAKE_SYM,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file2_s1d1));
- ASSERT_EQ(0, symlink("none", file2_s1d1));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file2_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(-1, symlink("none", file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, symlink("none", file1_s1d2));
- ASSERT_EQ(0, link(file1_s1d2, file2_s1d2));
- ASSERT_EQ(0, unlink(file2_s1d2));
- ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2));
- ASSERT_EQ(0, symlink("none", file1_s1d3));
- ASSERT_EQ(0, link(file1_s1d3, file2_s1d3));
- ASSERT_EQ(0, unlink(file2_s1d3));
- ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3));
- }
- TEST_F_FORK(layout1, make_dir)
- {
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_MAKE_DIR,
- },
- {},
- };
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- ASSERT_EQ(0, unlink(file1_s1d1));
- ASSERT_EQ(0, unlink(file1_s1d2));
- ASSERT_EQ(0, unlink(file1_s1d3));
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Uses file_* as directory names. */
- ASSERT_EQ(-1, mkdir(file1_s1d1, 0700));
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, mkdir(file1_s1d2, 0700));
- ASSERT_EQ(0, mkdir(file1_s1d3, 0700));
- }
- static int open_proc_fd(struct __test_metadata *const _metadata, const int fd,
- const int open_flags)
- {
- static const char path_template[] = "/proc/self/fd/%d";
- char procfd_path[sizeof(path_template) + 10];
- const int procfd_path_size =
- snprintf(procfd_path, sizeof(procfd_path), path_template, fd);
- ASSERT_LT(procfd_path_size, sizeof(procfd_path));
- return open(procfd_path, open_flags);
- }
- TEST_F_FORK(layout1, proc_unlinked_file)
- {
- const struct rule rules[] = {
- {
- .path = file1_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- int reg_fd, proc_fd;
- const int ruleset_fd = create_ruleset(
- _metadata,
- LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE,
- rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- reg_fd = open(file1_s1d2, O_RDONLY | O_CLOEXEC);
- ASSERT_LE(0, reg_fd);
- ASSERT_EQ(0, unlink(file1_s1d2));
- proc_fd = open_proc_fd(_metadata, reg_fd, O_RDONLY | O_CLOEXEC);
- ASSERT_LE(0, proc_fd);
- ASSERT_EQ(0, close(proc_fd));
- proc_fd = open_proc_fd(_metadata, reg_fd, O_RDWR | O_CLOEXEC);
- ASSERT_EQ(-1, proc_fd)
- {
- TH_LOG("Successfully opened /proc/self/fd/%d: %s", reg_fd,
- strerror(errno));
- }
- ASSERT_EQ(EACCES, errno);
- ASSERT_EQ(0, close(reg_fd));
- }
- TEST_F_FORK(layout1, proc_pipe)
- {
- int proc_fd;
- int pipe_fds[2];
- char buf = '\0';
- const struct rule rules[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- /* Limits read and write access to files tied to the filesystem. */
- const int ruleset_fd =
- create_ruleset(_metadata, rules[0].access, rules);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks enforcement for normal files. */
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR));
- /* Checks access to pipes through FD. */
- ASSERT_EQ(0, pipe2(pipe_fds, O_CLOEXEC));
- ASSERT_EQ(1, write(pipe_fds[1], ".", 1))
- {
- TH_LOG("Failed to write in pipe: %s", strerror(errno));
- }
- ASSERT_EQ(1, read(pipe_fds[0], &buf, 1));
- ASSERT_EQ('.', buf);
- /* Checks write access to pipe through /proc/self/fd . */
- proc_fd = open_proc_fd(_metadata, pipe_fds[1], O_WRONLY | O_CLOEXEC);
- ASSERT_LE(0, proc_fd);
- ASSERT_EQ(1, write(proc_fd, ".", 1))
- {
- TH_LOG("Failed to write through /proc/self/fd/%d: %s",
- pipe_fds[1], strerror(errno));
- }
- ASSERT_EQ(0, close(proc_fd));
- /* Checks read access to pipe through /proc/self/fd . */
- proc_fd = open_proc_fd(_metadata, pipe_fds[0], O_RDONLY | O_CLOEXEC);
- ASSERT_LE(0, proc_fd);
- buf = '\0';
- ASSERT_EQ(1, read(proc_fd, &buf, 1))
- {
- TH_LOG("Failed to read through /proc/self/fd/%d: %s",
- pipe_fds[1], strerror(errno));
- }
- ASSERT_EQ(0, close(proc_fd));
- ASSERT_EQ(0, close(pipe_fds[0]));
- ASSERT_EQ(0, close(pipe_fds[1]));
- }
- /* clang-format off */
- FIXTURE(layout1_bind) {};
- /* clang-format on */
- FIXTURE_SETUP(layout1_bind)
- {
- prepare_layout(_metadata);
- create_layout1(_metadata);
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, mount(dir_s1d2, dir_s2d2, NULL, MS_BIND, NULL));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- }
- FIXTURE_TEARDOWN(layout1_bind)
- {
- set_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, umount(dir_s2d2));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- remove_layout1(_metadata);
- cleanup_layout(_metadata);
- }
- static const char bind_dir_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3";
- static const char bind_file1_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3/f1";
- /*
- * layout1_bind hierarchy:
- *
- * tmp
- * ├── s1d1
- * │ ├── f1
- * │ ├── f2
- * │ └── s1d2
- * │ ├── f1
- * │ ├── f2
- * │ └── s1d3
- * │ ├── f1
- * │ └── f2
- * ├── s2d1
- * │ ├── f1
- * │ └── s2d2
- * │ ├── f1
- * │ ├── f2
- * │ └── s1d3
- * │ ├── f1
- * │ └── f2
- * └── s3d1
- * └── s3d2
- * └── s3d3
- */
- TEST_F_FORK(layout1_bind, no_restriction)
- {
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY));
- ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY));
- ASSERT_EQ(ENOENT, test_open(dir_s2d3, O_RDONLY));
- ASSERT_EQ(ENOENT, test_open(file1_s2d3, O_RDONLY));
- ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY));
- ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY));
- }
- TEST_F_FORK(layout1_bind, same_content_same_file)
- {
- /*
- * Sets access right on parent directories of both source and
- * destination mount points.
- */
- const struct rule layer1_parent[] = {
- {
- .path = dir_s1d1,
- .access = ACCESS_RO,
- },
- {
- .path = dir_s2d1,
- .access = ACCESS_RW,
- },
- {},
- };
- /*
- * Sets access rights on the same bind-mounted directories. The result
- * should be ACCESS_RW for both directories, but not both hierarchies
- * because of the first layer.
- */
- const struct rule layer2_mount_point[] = {
- {
- .path = dir_s1d2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = dir_s2d2,
- .access = ACCESS_RW,
- },
- {},
- };
- /* Only allow read-access to the s1d3 hierarchies. */
- const struct rule layer3_source[] = {
- {
- .path = dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {},
- };
- /* Removes all access rights. */
- const struct rule layer4_destination[] = {
- {
- .path = bind_file1_s1d3,
- .access = LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- int ruleset_fd;
- /* Sets rules for the parent directories. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_parent);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks source hierarchy. */
- ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* Checks destination hierarchy. */
- ASSERT_EQ(0, test_open(file1_s2d1, O_RDWR));
- ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR));
- ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY));
- /* Sets rules for the mount points. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_mount_point);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks source hierarchy. */
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- /* Checks destination hierarchy. */
- ASSERT_EQ(EACCES, test_open(file1_s2d1, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s2d1, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR));
- ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY));
- /* Sets a (shared) rule only on the source. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3_source);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks source hierarchy. */
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY));
- /* Checks destination hierarchy. */
- ASSERT_EQ(EACCES, test_open(file1_s2d2, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s2d2, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY));
- ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY));
- ASSERT_EQ(EACCES, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY));
- /* Sets a (shared) rule only on the destination. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer4_destination);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks source hierarchy. */
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY));
- /* Checks destination hierarchy. */
- ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY));
- }
- TEST_F_FORK(layout1_bind, reparent_cross_mount)
- {
- const struct rule layer1[] = {
- {
- /* dir_s2d1 is beneath the dir_s2d2 mount point. */
- .path = dir_s2d1,
- .access = LANDLOCK_ACCESS_FS_REFER,
- },
- {
- .path = bind_dir_s1d3,
- .access = LANDLOCK_ACCESS_FS_EXECUTE,
- },
- {},
- };
- int ruleset_fd = create_ruleset(
- _metadata,
- LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_EXECUTE, layer1);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks basic denied move. */
- ASSERT_EQ(-1, rename(file1_s1d1, file1_s1d2));
- ASSERT_EQ(EXDEV, errno);
- /* Checks real cross-mount move (Landlock is not involved). */
- ASSERT_EQ(-1, rename(file1_s2d1, file1_s2d2));
- ASSERT_EQ(EXDEV, errno);
- /* Checks move that will give more accesses. */
- ASSERT_EQ(-1, rename(file1_s2d2, bind_file1_s1d3));
- ASSERT_EQ(EXDEV, errno);
- /* Checks legitimate downgrade move. */
- ASSERT_EQ(0, rename(bind_file1_s1d3, file1_s2d2));
- }
- #define LOWER_BASE TMP_DIR "/lower"
- #define LOWER_DATA LOWER_BASE "/data"
- static const char lower_fl1[] = LOWER_DATA "/fl1";
- static const char lower_dl1[] = LOWER_DATA "/dl1";
- static const char lower_dl1_fl2[] = LOWER_DATA "/dl1/fl2";
- static const char lower_fo1[] = LOWER_DATA "/fo1";
- static const char lower_do1[] = LOWER_DATA "/do1";
- static const char lower_do1_fo2[] = LOWER_DATA "/do1/fo2";
- static const char lower_do1_fl3[] = LOWER_DATA "/do1/fl3";
- static const char (*lower_base_files[])[] = {
- &lower_fl1,
- &lower_fo1,
- NULL,
- };
- static const char (*lower_base_directories[])[] = {
- &lower_dl1,
- &lower_do1,
- NULL,
- };
- static const char (*lower_sub_files[])[] = {
- &lower_dl1_fl2,
- &lower_do1_fo2,
- &lower_do1_fl3,
- NULL,
- };
- #define UPPER_BASE TMP_DIR "/upper"
- #define UPPER_DATA UPPER_BASE "/data"
- #define UPPER_WORK UPPER_BASE "/work"
- static const char upper_fu1[] = UPPER_DATA "/fu1";
- static const char upper_du1[] = UPPER_DATA "/du1";
- static const char upper_du1_fu2[] = UPPER_DATA "/du1/fu2";
- static const char upper_fo1[] = UPPER_DATA "/fo1";
- static const char upper_do1[] = UPPER_DATA "/do1";
- static const char upper_do1_fo2[] = UPPER_DATA "/do1/fo2";
- static const char upper_do1_fu3[] = UPPER_DATA "/do1/fu3";
- static const char (*upper_base_files[])[] = {
- &upper_fu1,
- &upper_fo1,
- NULL,
- };
- static const char (*upper_base_directories[])[] = {
- &upper_du1,
- &upper_do1,
- NULL,
- };
- static const char (*upper_sub_files[])[] = {
- &upper_du1_fu2,
- &upper_do1_fo2,
- &upper_do1_fu3,
- NULL,
- };
- #define MERGE_BASE TMP_DIR "/merge"
- #define MERGE_DATA MERGE_BASE "/data"
- static const char merge_fl1[] = MERGE_DATA "/fl1";
- static const char merge_dl1[] = MERGE_DATA "/dl1";
- static const char merge_dl1_fl2[] = MERGE_DATA "/dl1/fl2";
- static const char merge_fu1[] = MERGE_DATA "/fu1";
- static const char merge_du1[] = MERGE_DATA "/du1";
- static const char merge_du1_fu2[] = MERGE_DATA "/du1/fu2";
- static const char merge_fo1[] = MERGE_DATA "/fo1";
- static const char merge_do1[] = MERGE_DATA "/do1";
- static const char merge_do1_fo2[] = MERGE_DATA "/do1/fo2";
- static const char merge_do1_fl3[] = MERGE_DATA "/do1/fl3";
- static const char merge_do1_fu3[] = MERGE_DATA "/do1/fu3";
- static const char (*merge_base_files[])[] = {
- &merge_fl1,
- &merge_fu1,
- &merge_fo1,
- NULL,
- };
- static const char (*merge_base_directories[])[] = {
- &merge_dl1,
- &merge_du1,
- &merge_do1,
- NULL,
- };
- static const char (*merge_sub_files[])[] = {
- &merge_dl1_fl2, &merge_du1_fu2, &merge_do1_fo2,
- &merge_do1_fl3, &merge_do1_fu3, NULL,
- };
- /*
- * layout2_overlay hierarchy:
- *
- * tmp
- * ├── lower
- * │ └── data
- * │ ├── dl1
- * │ │ └── fl2
- * │ ├── do1
- * │ │ ├── fl3
- * │ │ └── fo2
- * │ ├── fl1
- * │ └── fo1
- * ├── merge
- * │ └── data
- * │ ├── dl1
- * │ │ └── fl2
- * │ ├── do1
- * │ │ ├── fl3
- * │ │ ├── fo2
- * │ │ └── fu3
- * │ ├── du1
- * │ │ └── fu2
- * │ ├── fl1
- * │ ├── fo1
- * │ └── fu1
- * └── upper
- * ├── data
- * │ ├── do1
- * │ │ ├── fo2
- * │ │ └── fu3
- * │ ├── du1
- * │ │ └── fu2
- * │ ├── fo1
- * │ └── fu1
- * └── work
- * └── work
- */
- /* clang-format off */
- FIXTURE(layout2_overlay) {};
- /* clang-format on */
- FIXTURE_SETUP(layout2_overlay)
- {
- if (!supports_overlayfs())
- SKIP(return, "overlayfs is not supported");
- prepare_layout(_metadata);
- create_directory(_metadata, LOWER_BASE);
- set_cap(_metadata, CAP_SYS_ADMIN);
- /* Creates tmpfs mount points to get deterministic overlayfs. */
- ASSERT_EQ(0, mount("tmp", LOWER_BASE, "tmpfs", 0, "size=4m,mode=700"));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- create_file(_metadata, lower_fl1);
- create_file(_metadata, lower_dl1_fl2);
- create_file(_metadata, lower_fo1);
- create_file(_metadata, lower_do1_fo2);
- create_file(_metadata, lower_do1_fl3);
- create_directory(_metadata, UPPER_BASE);
- set_cap(_metadata, CAP_SYS_ADMIN);
- ASSERT_EQ(0, mount("tmp", UPPER_BASE, "tmpfs", 0, "size=4m,mode=700"));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- create_file(_metadata, upper_fu1);
- create_file(_metadata, upper_du1_fu2);
- create_file(_metadata, upper_fo1);
- create_file(_metadata, upper_do1_fo2);
- create_file(_metadata, upper_do1_fu3);
- ASSERT_EQ(0, mkdir(UPPER_WORK, 0700));
- create_directory(_metadata, MERGE_DATA);
- set_cap(_metadata, CAP_SYS_ADMIN);
- set_cap(_metadata, CAP_DAC_OVERRIDE);
- ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
- "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
- ",workdir=" UPPER_WORK));
- clear_cap(_metadata, CAP_DAC_OVERRIDE);
- clear_cap(_metadata, CAP_SYS_ADMIN);
- }
- FIXTURE_TEARDOWN(layout2_overlay)
- {
- if (!supports_overlayfs())
- SKIP(return, "overlayfs is not supported");
- EXPECT_EQ(0, remove_path(lower_do1_fl3));
- EXPECT_EQ(0, remove_path(lower_dl1_fl2));
- EXPECT_EQ(0, remove_path(lower_fl1));
- EXPECT_EQ(0, remove_path(lower_do1_fo2));
- EXPECT_EQ(0, remove_path(lower_fo1));
- set_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, umount(LOWER_BASE));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, remove_path(LOWER_BASE));
- EXPECT_EQ(0, remove_path(upper_do1_fu3));
- EXPECT_EQ(0, remove_path(upper_du1_fu2));
- EXPECT_EQ(0, remove_path(upper_fu1));
- EXPECT_EQ(0, remove_path(upper_do1_fo2));
- EXPECT_EQ(0, remove_path(upper_fo1));
- EXPECT_EQ(0, remove_path(UPPER_WORK "/work"));
- set_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, umount(UPPER_BASE));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, remove_path(UPPER_BASE));
- set_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, umount(MERGE_DATA));
- clear_cap(_metadata, CAP_SYS_ADMIN);
- EXPECT_EQ(0, remove_path(MERGE_DATA));
- cleanup_layout(_metadata);
- }
- TEST_F_FORK(layout2_overlay, no_restriction)
- {
- if (!supports_overlayfs())
- SKIP(return, "overlayfs is not supported");
- ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY));
- ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY));
- ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY));
- ASSERT_EQ(0, test_open(lower_fo1, O_RDONLY));
- ASSERT_EQ(0, test_open(lower_do1, O_RDONLY));
- ASSERT_EQ(0, test_open(lower_do1_fo2, O_RDONLY));
- ASSERT_EQ(0, test_open(lower_do1_fl3, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_fu1, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_du1, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_du1_fu2, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_fo1, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_do1, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_do1_fo2, O_RDONLY));
- ASSERT_EQ(0, test_open(upper_do1_fu3, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_fl1, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_dl1, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_dl1_fl2, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_fu1, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_du1, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_du1_fu2, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_fo1, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_do1, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_do1_fo2, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_do1_fl3, O_RDONLY));
- ASSERT_EQ(0, test_open(merge_do1_fu3, O_RDONLY));
- }
- #define for_each_path(path_list, path_entry, i) \
- for (i = 0, path_entry = *path_list[i]; path_list[i]; \
- path_entry = *path_list[++i])
- TEST_F_FORK(layout2_overlay, same_content_different_file)
- {
- /* Sets access right on parent directories of both layers. */
- const struct rule layer1_base[] = {
- {
- .path = LOWER_BASE,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = UPPER_BASE,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = MERGE_BASE,
- .access = ACCESS_RW,
- },
- {},
- };
- const struct rule layer2_data[] = {
- {
- .path = LOWER_DATA,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = UPPER_DATA,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = MERGE_DATA,
- .access = ACCESS_RW,
- },
- {},
- };
- /* Sets access right on directories inside both layers. */
- const struct rule layer3_subdirs[] = {
- {
- .path = lower_dl1,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = lower_do1,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = upper_du1,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = upper_do1,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = merge_dl1,
- .access = ACCESS_RW,
- },
- {
- .path = merge_du1,
- .access = ACCESS_RW,
- },
- {
- .path = merge_do1,
- .access = ACCESS_RW,
- },
- {},
- };
- /* Tighten access rights to the files. */
- const struct rule layer4_files[] = {
- {
- .path = lower_dl1_fl2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = lower_do1_fo2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = lower_do1_fl3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = upper_du1_fu2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = upper_do1_fo2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = upper_do1_fu3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE,
- },
- {
- .path = merge_dl1_fl2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {
- .path = merge_du1_fu2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {
- .path = merge_do1_fo2,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {
- .path = merge_do1_fl3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {
- .path = merge_do1_fu3,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- const struct rule layer5_merge_only[] = {
- {
- .path = MERGE_DATA,
- .access = LANDLOCK_ACCESS_FS_READ_FILE |
- LANDLOCK_ACCESS_FS_WRITE_FILE,
- },
- {},
- };
- int ruleset_fd;
- size_t i;
- const char *path_entry;
- if (!supports_overlayfs())
- SKIP(return, "overlayfs is not supported");
- /* Sets rules on base directories (i.e. outside overlay scope). */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks lower layer. */
- for_each_path(lower_base_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
- }
- for_each_path(lower_base_directories, path_entry, i) {
- ASSERT_EQ(EACCES,
- test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(lower_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
- }
- /* Checks upper layer. */
- for_each_path(upper_base_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
- }
- for_each_path(upper_base_directories, path_entry, i) {
- ASSERT_EQ(EACCES,
- test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(upper_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
- }
- /*
- * Checks that access rights are independent from the lower and upper
- * layers: write access to upper files viewed through the merge point
- * is still allowed, and write access to lower file viewed (and copied)
- * through the merge point is still allowed.
- */
- for_each_path(merge_base_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- for_each_path(merge_base_directories, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(merge_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- /* Sets rules on data directories (i.e. inside overlay scope). */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_data);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks merge. */
- for_each_path(merge_base_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- for_each_path(merge_base_directories, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(merge_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- /* Same checks with tighter rules. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3_subdirs);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks changes for lower layer. */
- for_each_path(lower_base_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
- }
- /* Checks changes for upper layer. */
- for_each_path(upper_base_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
- }
- /* Checks all merge accesses. */
- for_each_path(merge_base_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR));
- }
- for_each_path(merge_base_directories, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(merge_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- /* Sets rules directly on overlayed files. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer4_files);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks unchanged accesses on lower layer. */
- for_each_path(lower_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
- }
- /* Checks unchanged accesses on upper layer. */
- for_each_path(upper_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDONLY));
- ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY));
- }
- /* Checks all merge accesses. */
- for_each_path(merge_base_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR));
- }
- for_each_path(merge_base_directories, path_entry, i) {
- ASSERT_EQ(EACCES,
- test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(merge_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- /* Only allowes access to the merge hierarchy. */
- ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer5_merge_only);
- ASSERT_LE(0, ruleset_fd);
- enforce_ruleset(_metadata, ruleset_fd);
- ASSERT_EQ(0, close(ruleset_fd));
- /* Checks new accesses on lower layer. */
- for_each_path(lower_sub_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
- }
- /* Checks new accesses on upper layer. */
- for_each_path(upper_sub_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY));
- }
- /* Checks all merge accesses. */
- for_each_path(merge_base_files, path_entry, i) {
- ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR));
- }
- for_each_path(merge_base_directories, path_entry, i) {
- ASSERT_EQ(EACCES,
- test_open(path_entry, O_RDONLY | O_DIRECTORY));
- }
- for_each_path(merge_sub_files, path_entry, i) {
- ASSERT_EQ(0, test_open(path_entry, O_RDWR));
- }
- }
- TEST_HARNESS_MAIN
|