fs.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Landlock LSM - Filesystem management and hooks
  4. *
  5. * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
  6. * Copyright © 2018-2020 ANSSI
  7. * Copyright © 2021-2022 Microsoft Corporation
  8. * Copyright © 2022 Günther Noack <gnoack3000@gmail.com>
  9. * Copyright © 2023-2024 Google LLC
  10. */
  11. #include <asm/ioctls.h>
  12. #include <kunit/test.h>
  13. #include <linux/atomic.h>
  14. #include <linux/bitops.h>
  15. #include <linux/bits.h>
  16. #include <linux/compiler_types.h>
  17. #include <linux/dcache.h>
  18. #include <linux/err.h>
  19. #include <linux/falloc.h>
  20. #include <linux/fs.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/limits.h>
  24. #include <linux/list.h>
  25. #include <linux/lsm_hooks.h>
  26. #include <linux/mount.h>
  27. #include <linux/namei.h>
  28. #include <linux/path.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/stat.h>
  32. #include <linux/types.h>
  33. #include <linux/wait_bit.h>
  34. #include <linux/workqueue.h>
  35. #include <uapi/linux/fiemap.h>
  36. #include <uapi/linux/landlock.h>
  37. #include "common.h"
  38. #include "cred.h"
  39. #include "fs.h"
  40. #include "limits.h"
  41. #include "object.h"
  42. #include "ruleset.h"
  43. #include "setup.h"
  44. /* Underlying object management */
  45. static void release_inode(struct landlock_object *const object)
  46. __releases(object->lock)
  47. {
  48. struct inode *const inode = object->underobj;
  49. struct super_block *sb;
  50. if (!inode) {
  51. spin_unlock(&object->lock);
  52. return;
  53. }
  54. /*
  55. * Protects against concurrent use by hook_sb_delete() of the reference
  56. * to the underlying inode.
  57. */
  58. object->underobj = NULL;
  59. /*
  60. * Makes sure that if the filesystem is concurrently unmounted,
  61. * hook_sb_delete() will wait for us to finish iput().
  62. */
  63. sb = inode->i_sb;
  64. atomic_long_inc(&landlock_superblock(sb)->inode_refs);
  65. spin_unlock(&object->lock);
  66. /*
  67. * Because object->underobj was not NULL, hook_sb_delete() and
  68. * get_inode_object() guarantee that it is safe to reset
  69. * landlock_inode(inode)->object while it is not NULL. It is therefore
  70. * not necessary to lock inode->i_lock.
  71. */
  72. rcu_assign_pointer(landlock_inode(inode)->object, NULL);
  73. /*
  74. * Now, new rules can safely be tied to @inode with get_inode_object().
  75. */
  76. iput(inode);
  77. if (atomic_long_dec_and_test(&landlock_superblock(sb)->inode_refs))
  78. wake_up_var(&landlock_superblock(sb)->inode_refs);
  79. }
  80. static const struct landlock_object_underops landlock_fs_underops = {
  81. .release = release_inode
  82. };
  83. /* IOCTL helpers */
  84. /**
  85. * is_masked_device_ioctl - Determine whether an IOCTL command is always
  86. * permitted with Landlock for device files. These commands can not be
  87. * restricted on device files by enforcing a Landlock policy.
  88. *
  89. * @cmd: The IOCTL command that is supposed to be run.
  90. *
  91. * By default, any IOCTL on a device file requires the
  92. * LANDLOCK_ACCESS_FS_IOCTL_DEV right. However, we blanket-permit some
  93. * commands, if:
  94. *
  95. * 1. The command is implemented in fs/ioctl.c's do_vfs_ioctl(),
  96. * not in f_ops->unlocked_ioctl() or f_ops->compat_ioctl().
  97. *
  98. * 2. The command is harmless when invoked on devices.
  99. *
  100. * We also permit commands that do not make sense for devices, but where the
  101. * do_vfs_ioctl() implementation returns a more conventional error code.
  102. *
  103. * Any new IOCTL commands that are implemented in fs/ioctl.c's do_vfs_ioctl()
  104. * should be considered for inclusion here.
  105. *
  106. * Returns: true if the IOCTL @cmd can not be restricted with Landlock for
  107. * device files.
  108. */
  109. static __attribute_const__ bool is_masked_device_ioctl(const unsigned int cmd)
  110. {
  111. switch (cmd) {
  112. /*
  113. * FIOCLEX, FIONCLEX, FIONBIO and FIOASYNC manipulate the FD's
  114. * close-on-exec and the file's buffered-IO and async flags. These
  115. * operations are also available through fcntl(2), and are
  116. * unconditionally permitted in Landlock.
  117. */
  118. case FIOCLEX:
  119. case FIONCLEX:
  120. case FIONBIO:
  121. case FIOASYNC:
  122. /*
  123. * FIOQSIZE queries the size of a regular file, directory, or link.
  124. *
  125. * We still permit it, because it always returns -ENOTTY for
  126. * other file types.
  127. */
  128. case FIOQSIZE:
  129. /*
  130. * FIFREEZE and FITHAW freeze and thaw the file system which the
  131. * given file belongs to. Requires CAP_SYS_ADMIN.
  132. *
  133. * These commands operate on the file system's superblock rather
  134. * than on the file itself. The same operations can also be
  135. * done through any other file or directory on the same file
  136. * system, so it is safe to permit these.
  137. */
  138. case FIFREEZE:
  139. case FITHAW:
  140. /*
  141. * FS_IOC_FIEMAP queries information about the allocation of
  142. * blocks within a file.
  143. *
  144. * This IOCTL command only makes sense for regular files and is
  145. * not implemented by devices. It is harmless to permit.
  146. */
  147. case FS_IOC_FIEMAP:
  148. /*
  149. * FIGETBSZ queries the file system's block size for a file or
  150. * directory.
  151. *
  152. * This command operates on the file system's superblock rather
  153. * than on the file itself. The same operation can also be done
  154. * through any other file or directory on the same file system,
  155. * so it is safe to permit it.
  156. */
  157. case FIGETBSZ:
  158. /*
  159. * FICLONE, FICLONERANGE and FIDEDUPERANGE make files share
  160. * their underlying storage ("reflink") between source and
  161. * destination FDs, on file systems which support that.
  162. *
  163. * These IOCTL commands only apply to regular files
  164. * and are harmless to permit for device files.
  165. */
  166. case FICLONE:
  167. case FICLONERANGE:
  168. case FIDEDUPERANGE:
  169. /*
  170. * FS_IOC_GETFSUUID and FS_IOC_GETFSSYSFSPATH both operate on
  171. * the file system superblock, not on the specific file, so
  172. * these operations are available through any other file on the
  173. * same file system as well.
  174. */
  175. case FS_IOC_GETFSUUID:
  176. case FS_IOC_GETFSSYSFSPATH:
  177. return true;
  178. /*
  179. * FIONREAD, FS_IOC_GETFLAGS, FS_IOC_SETFLAGS, FS_IOC_FSGETXATTR and
  180. * FS_IOC_FSSETXATTR are forwarded to device implementations.
  181. */
  182. /*
  183. * file_ioctl() commands (FIBMAP, FS_IOC_RESVSP, FS_IOC_RESVSP64,
  184. * FS_IOC_UNRESVSP, FS_IOC_UNRESVSP64 and FS_IOC_ZERO_RANGE) are
  185. * forwarded to device implementations, so not permitted.
  186. */
  187. /* Other commands are guarded by the access right. */
  188. default:
  189. return false;
  190. }
  191. }
  192. /*
  193. * is_masked_device_ioctl_compat - same as the helper above, but checking the
  194. * "compat" IOCTL commands.
  195. *
  196. * The IOCTL commands with special handling in compat-mode should behave the
  197. * same as their non-compat counterparts.
  198. */
  199. static __attribute_const__ bool
  200. is_masked_device_ioctl_compat(const unsigned int cmd)
  201. {
  202. switch (cmd) {
  203. /* FICLONE is permitted, same as in the non-compat variant. */
  204. case FICLONE:
  205. return true;
  206. #if defined(CONFIG_X86_64)
  207. /*
  208. * FS_IOC_RESVSP_32, FS_IOC_RESVSP64_32, FS_IOC_UNRESVSP_32,
  209. * FS_IOC_UNRESVSP64_32, FS_IOC_ZERO_RANGE_32: not blanket-permitted,
  210. * for consistency with their non-compat variants.
  211. */
  212. case FS_IOC_RESVSP_32:
  213. case FS_IOC_RESVSP64_32:
  214. case FS_IOC_UNRESVSP_32:
  215. case FS_IOC_UNRESVSP64_32:
  216. case FS_IOC_ZERO_RANGE_32:
  217. #endif
  218. /*
  219. * FS_IOC32_GETFLAGS, FS_IOC32_SETFLAGS are forwarded to their device
  220. * implementations.
  221. */
  222. case FS_IOC32_GETFLAGS:
  223. case FS_IOC32_SETFLAGS:
  224. return false;
  225. default:
  226. return is_masked_device_ioctl(cmd);
  227. }
  228. }
  229. /* Ruleset management */
  230. static struct landlock_object *get_inode_object(struct inode *const inode)
  231. {
  232. struct landlock_object *object, *new_object;
  233. struct landlock_inode_security *inode_sec = landlock_inode(inode);
  234. rcu_read_lock();
  235. retry:
  236. object = rcu_dereference(inode_sec->object);
  237. if (object) {
  238. if (likely(refcount_inc_not_zero(&object->usage))) {
  239. rcu_read_unlock();
  240. return object;
  241. }
  242. /*
  243. * We are racing with release_inode(), the object is going
  244. * away. Wait for release_inode(), then retry.
  245. */
  246. spin_lock(&object->lock);
  247. spin_unlock(&object->lock);
  248. goto retry;
  249. }
  250. rcu_read_unlock();
  251. /*
  252. * If there is no object tied to @inode, then create a new one (without
  253. * holding any locks).
  254. */
  255. new_object = landlock_create_object(&landlock_fs_underops, inode);
  256. if (IS_ERR(new_object))
  257. return new_object;
  258. /*
  259. * Protects against concurrent calls to get_inode_object() or
  260. * hook_sb_delete().
  261. */
  262. spin_lock(&inode->i_lock);
  263. if (unlikely(rcu_access_pointer(inode_sec->object))) {
  264. /* Someone else just created the object, bail out and retry. */
  265. spin_unlock(&inode->i_lock);
  266. kfree(new_object);
  267. rcu_read_lock();
  268. goto retry;
  269. }
  270. /*
  271. * @inode will be released by hook_sb_delete() on its superblock
  272. * shutdown, or by release_inode() when no more ruleset references the
  273. * related object.
  274. */
  275. ihold(inode);
  276. rcu_assign_pointer(inode_sec->object, new_object);
  277. spin_unlock(&inode->i_lock);
  278. return new_object;
  279. }
  280. /* All access rights that can be tied to files. */
  281. /* clang-format off */
  282. #define ACCESS_FILE ( \
  283. LANDLOCK_ACCESS_FS_EXECUTE | \
  284. LANDLOCK_ACCESS_FS_WRITE_FILE | \
  285. LANDLOCK_ACCESS_FS_READ_FILE | \
  286. LANDLOCK_ACCESS_FS_TRUNCATE | \
  287. LANDLOCK_ACCESS_FS_IOCTL_DEV)
  288. /* clang-format on */
  289. /*
  290. * @path: Should have been checked by get_path_from_fd().
  291. */
  292. int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
  293. const struct path *const path,
  294. access_mask_t access_rights)
  295. {
  296. int err;
  297. struct landlock_id id = {
  298. .type = LANDLOCK_KEY_INODE,
  299. };
  300. /* Files only get access rights that make sense. */
  301. if (!d_is_dir(path->dentry) &&
  302. (access_rights | ACCESS_FILE) != ACCESS_FILE)
  303. return -EINVAL;
  304. if (WARN_ON_ONCE(ruleset->num_layers != 1))
  305. return -EINVAL;
  306. /* Transforms relative access rights to absolute ones. */
  307. access_rights |= LANDLOCK_MASK_ACCESS_FS &
  308. ~landlock_get_fs_access_mask(ruleset, 0);
  309. id.key.object = get_inode_object(d_backing_inode(path->dentry));
  310. if (IS_ERR(id.key.object))
  311. return PTR_ERR(id.key.object);
  312. mutex_lock(&ruleset->lock);
  313. err = landlock_insert_rule(ruleset, id, access_rights);
  314. mutex_unlock(&ruleset->lock);
  315. /*
  316. * No need to check for an error because landlock_insert_rule()
  317. * increments the refcount for the new object if needed.
  318. */
  319. landlock_put_object(id.key.object);
  320. return err;
  321. }
  322. /* Access-control management */
  323. /*
  324. * The lifetime of the returned rule is tied to @domain.
  325. *
  326. * Returns NULL if no rule is found or if @dentry is negative.
  327. */
  328. static const struct landlock_rule *
  329. find_rule(const struct landlock_ruleset *const domain,
  330. const struct dentry *const dentry)
  331. {
  332. const struct landlock_rule *rule;
  333. const struct inode *inode;
  334. struct landlock_id id = {
  335. .type = LANDLOCK_KEY_INODE,
  336. };
  337. /* Ignores nonexistent leafs. */
  338. if (d_is_negative(dentry))
  339. return NULL;
  340. inode = d_backing_inode(dentry);
  341. rcu_read_lock();
  342. id.key.object = rcu_dereference(landlock_inode(inode)->object);
  343. rule = landlock_find_rule(domain, id);
  344. rcu_read_unlock();
  345. return rule;
  346. }
  347. /*
  348. * Allows access to pseudo filesystems that will never be mountable (e.g.
  349. * sockfs, pipefs), but can still be reachable through
  350. * /proc/<pid>/fd/<file-descriptor>
  351. */
  352. static bool is_nouser_or_private(const struct dentry *dentry)
  353. {
  354. return (dentry->d_sb->s_flags & SB_NOUSER) ||
  355. (d_is_positive(dentry) &&
  356. unlikely(IS_PRIVATE(d_backing_inode(dentry))));
  357. }
  358. static access_mask_t
  359. get_handled_fs_accesses(const struct landlock_ruleset *const domain)
  360. {
  361. /* Handles all initially denied by default access rights. */
  362. return landlock_union_access_masks(domain).fs |
  363. LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
  364. }
  365. static const struct access_masks any_fs = {
  366. .fs = ~0,
  367. };
  368. static const struct landlock_ruleset *get_current_fs_domain(void)
  369. {
  370. return landlock_get_applicable_domain(landlock_get_current_domain(),
  371. any_fs);
  372. }
  373. /*
  374. * Check that a destination file hierarchy has more restrictions than a source
  375. * file hierarchy. This is only used for link and rename actions.
  376. *
  377. * @layer_masks_child2: Optional child masks.
  378. */
  379. static bool no_more_access(
  380. const layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS],
  381. const layer_mask_t (*const layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS],
  382. const bool child1_is_directory,
  383. const layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS],
  384. const layer_mask_t (*const layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS],
  385. const bool child2_is_directory)
  386. {
  387. unsigned long access_bit;
  388. for (access_bit = 0; access_bit < ARRAY_SIZE(*layer_masks_parent2);
  389. access_bit++) {
  390. /* Ignores accesses that only make sense for directories. */
  391. const bool is_file_access =
  392. !!(BIT_ULL(access_bit) & ACCESS_FILE);
  393. if (child1_is_directory || is_file_access) {
  394. /*
  395. * Checks if the destination restrictions are a
  396. * superset of the source ones (i.e. inherited access
  397. * rights without child exceptions):
  398. * restrictions(parent2) >= restrictions(child1)
  399. */
  400. if ((((*layer_masks_parent1)[access_bit] &
  401. (*layer_masks_child1)[access_bit]) |
  402. (*layer_masks_parent2)[access_bit]) !=
  403. (*layer_masks_parent2)[access_bit])
  404. return false;
  405. }
  406. if (!layer_masks_child2)
  407. continue;
  408. if (child2_is_directory || is_file_access) {
  409. /*
  410. * Checks inverted restrictions for RENAME_EXCHANGE:
  411. * restrictions(parent1) >= restrictions(child2)
  412. */
  413. if ((((*layer_masks_parent2)[access_bit] &
  414. (*layer_masks_child2)[access_bit]) |
  415. (*layer_masks_parent1)[access_bit]) !=
  416. (*layer_masks_parent1)[access_bit])
  417. return false;
  418. }
  419. }
  420. return true;
  421. }
  422. #define NMA_TRUE(...) KUNIT_EXPECT_TRUE(test, no_more_access(__VA_ARGS__))
  423. #define NMA_FALSE(...) KUNIT_EXPECT_FALSE(test, no_more_access(__VA_ARGS__))
  424. #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
  425. static void test_no_more_access(struct kunit *const test)
  426. {
  427. const layer_mask_t rx0[LANDLOCK_NUM_ACCESS_FS] = {
  428. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(0),
  429. [BIT_INDEX(LANDLOCK_ACCESS_FS_READ_FILE)] = BIT_ULL(0),
  430. };
  431. const layer_mask_t mx0[LANDLOCK_NUM_ACCESS_FS] = {
  432. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(0),
  433. [BIT_INDEX(LANDLOCK_ACCESS_FS_MAKE_REG)] = BIT_ULL(0),
  434. };
  435. const layer_mask_t x0[LANDLOCK_NUM_ACCESS_FS] = {
  436. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(0),
  437. };
  438. const layer_mask_t x1[LANDLOCK_NUM_ACCESS_FS] = {
  439. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(1),
  440. };
  441. const layer_mask_t x01[LANDLOCK_NUM_ACCESS_FS] = {
  442. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(0) |
  443. BIT_ULL(1),
  444. };
  445. const layer_mask_t allows_all[LANDLOCK_NUM_ACCESS_FS] = {};
  446. /* Checks without restriction. */
  447. NMA_TRUE(&x0, &allows_all, false, &allows_all, NULL, false);
  448. NMA_TRUE(&allows_all, &x0, false, &allows_all, NULL, false);
  449. NMA_FALSE(&x0, &x0, false, &allows_all, NULL, false);
  450. /*
  451. * Checks that we can only refer a file if no more access could be
  452. * inherited.
  453. */
  454. NMA_TRUE(&x0, &x0, false, &rx0, NULL, false);
  455. NMA_TRUE(&rx0, &rx0, false, &rx0, NULL, false);
  456. NMA_FALSE(&rx0, &rx0, false, &x0, NULL, false);
  457. NMA_FALSE(&rx0, &rx0, false, &x1, NULL, false);
  458. /* Checks allowed referring with different nested domains. */
  459. NMA_TRUE(&x0, &x1, false, &x0, NULL, false);
  460. NMA_TRUE(&x1, &x0, false, &x0, NULL, false);
  461. NMA_TRUE(&x0, &x01, false, &x0, NULL, false);
  462. NMA_TRUE(&x0, &x01, false, &rx0, NULL, false);
  463. NMA_TRUE(&x01, &x0, false, &x0, NULL, false);
  464. NMA_TRUE(&x01, &x0, false, &rx0, NULL, false);
  465. NMA_FALSE(&x01, &x01, false, &x0, NULL, false);
  466. /* Checks that file access rights are also enforced for a directory. */
  467. NMA_FALSE(&rx0, &rx0, true, &x0, NULL, false);
  468. /* Checks that directory access rights don't impact file referring... */
  469. NMA_TRUE(&mx0, &mx0, false, &x0, NULL, false);
  470. /* ...but only directory referring. */
  471. NMA_FALSE(&mx0, &mx0, true, &x0, NULL, false);
  472. /* Checks directory exchange. */
  473. NMA_TRUE(&mx0, &mx0, true, &mx0, &mx0, true);
  474. NMA_TRUE(&mx0, &mx0, true, &mx0, &x0, true);
  475. NMA_FALSE(&mx0, &mx0, true, &x0, &mx0, true);
  476. NMA_FALSE(&mx0, &mx0, true, &x0, &x0, true);
  477. NMA_FALSE(&mx0, &mx0, true, &x1, &x1, true);
  478. /* Checks file exchange with directory access rights... */
  479. NMA_TRUE(&mx0, &mx0, false, &mx0, &mx0, false);
  480. NMA_TRUE(&mx0, &mx0, false, &mx0, &x0, false);
  481. NMA_TRUE(&mx0, &mx0, false, &x0, &mx0, false);
  482. NMA_TRUE(&mx0, &mx0, false, &x0, &x0, false);
  483. /* ...and with file access rights. */
  484. NMA_TRUE(&rx0, &rx0, false, &rx0, &rx0, false);
  485. NMA_TRUE(&rx0, &rx0, false, &rx0, &x0, false);
  486. NMA_FALSE(&rx0, &rx0, false, &x0, &rx0, false);
  487. NMA_FALSE(&rx0, &rx0, false, &x0, &x0, false);
  488. NMA_FALSE(&rx0, &rx0, false, &x1, &x1, false);
  489. /*
  490. * Allowing the following requests should not be a security risk
  491. * because domain 0 denies execute access, and domain 1 is always
  492. * nested with domain 0. However, adding an exception for this case
  493. * would mean to check all nested domains to make sure none can get
  494. * more privileges (e.g. processes only sandboxed by domain 0).
  495. * Moreover, this behavior (i.e. composition of N domains) could then
  496. * be inconsistent compared to domain 1's ruleset alone (e.g. it might
  497. * be denied to link/rename with domain 1's ruleset, whereas it would
  498. * be allowed if nested on top of domain 0). Another drawback would be
  499. * to create a cover channel that could enable sandboxed processes to
  500. * infer most of the filesystem restrictions from their domain. To
  501. * make it simple, efficient, safe, and more consistent, this case is
  502. * always denied.
  503. */
  504. NMA_FALSE(&x1, &x1, false, &x0, NULL, false);
  505. NMA_FALSE(&x1, &x1, false, &rx0, NULL, false);
  506. NMA_FALSE(&x1, &x1, true, &x0, NULL, false);
  507. NMA_FALSE(&x1, &x1, true, &rx0, NULL, false);
  508. /* Checks the same case of exclusive domains with a file... */
  509. NMA_TRUE(&x1, &x1, false, &x01, NULL, false);
  510. NMA_FALSE(&x1, &x1, false, &x01, &x0, false);
  511. NMA_FALSE(&x1, &x1, false, &x01, &x01, false);
  512. NMA_FALSE(&x1, &x1, false, &x0, &x0, false);
  513. /* ...and with a directory. */
  514. NMA_FALSE(&x1, &x1, false, &x0, &x0, true);
  515. NMA_FALSE(&x1, &x1, true, &x0, &x0, false);
  516. NMA_FALSE(&x1, &x1, true, &x0, &x0, true);
  517. }
  518. #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */
  519. #undef NMA_TRUE
  520. #undef NMA_FALSE
  521. /*
  522. * Removes @layer_masks accesses that are not requested.
  523. *
  524. * Returns true if the request is allowed, false otherwise.
  525. */
  526. static bool
  527. scope_to_request(const access_mask_t access_request,
  528. layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS])
  529. {
  530. const unsigned long access_req = access_request;
  531. unsigned long access_bit;
  532. if (WARN_ON_ONCE(!layer_masks))
  533. return true;
  534. for_each_clear_bit(access_bit, &access_req, ARRAY_SIZE(*layer_masks))
  535. (*layer_masks)[access_bit] = 0;
  536. return !memchr_inv(layer_masks, 0, sizeof(*layer_masks));
  537. }
  538. #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
  539. static void test_scope_to_request_with_exec_none(struct kunit *const test)
  540. {
  541. /* Allows everything. */
  542. layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
  543. /* Checks and scopes with execute. */
  544. KUNIT_EXPECT_TRUE(test, scope_to_request(LANDLOCK_ACCESS_FS_EXECUTE,
  545. &layer_masks));
  546. KUNIT_EXPECT_EQ(test, 0,
  547. layer_masks[BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)]);
  548. KUNIT_EXPECT_EQ(test, 0,
  549. layer_masks[BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)]);
  550. }
  551. static void test_scope_to_request_with_exec_some(struct kunit *const test)
  552. {
  553. /* Denies execute and write. */
  554. layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {
  555. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(0),
  556. [BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)] = BIT_ULL(1),
  557. };
  558. /* Checks and scopes with execute. */
  559. KUNIT_EXPECT_FALSE(test, scope_to_request(LANDLOCK_ACCESS_FS_EXECUTE,
  560. &layer_masks));
  561. KUNIT_EXPECT_EQ(test, BIT_ULL(0),
  562. layer_masks[BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)]);
  563. KUNIT_EXPECT_EQ(test, 0,
  564. layer_masks[BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)]);
  565. }
  566. static void test_scope_to_request_without_access(struct kunit *const test)
  567. {
  568. /* Denies execute and write. */
  569. layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {
  570. [BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)] = BIT_ULL(0),
  571. [BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)] = BIT_ULL(1),
  572. };
  573. /* Checks and scopes without access request. */
  574. KUNIT_EXPECT_TRUE(test, scope_to_request(0, &layer_masks));
  575. KUNIT_EXPECT_EQ(test, 0,
  576. layer_masks[BIT_INDEX(LANDLOCK_ACCESS_FS_EXECUTE)]);
  577. KUNIT_EXPECT_EQ(test, 0,
  578. layer_masks[BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)]);
  579. }
  580. #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */
  581. /*
  582. * Returns true if there is at least one access right different than
  583. * LANDLOCK_ACCESS_FS_REFER.
  584. */
  585. static bool
  586. is_eacces(const layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS],
  587. const access_mask_t access_request)
  588. {
  589. unsigned long access_bit;
  590. /* LANDLOCK_ACCESS_FS_REFER alone must return -EXDEV. */
  591. const unsigned long access_check = access_request &
  592. ~LANDLOCK_ACCESS_FS_REFER;
  593. if (!layer_masks)
  594. return false;
  595. for_each_set_bit(access_bit, &access_check, ARRAY_SIZE(*layer_masks)) {
  596. if ((*layer_masks)[access_bit])
  597. return true;
  598. }
  599. return false;
  600. }
  601. #define IE_TRUE(...) KUNIT_EXPECT_TRUE(test, is_eacces(__VA_ARGS__))
  602. #define IE_FALSE(...) KUNIT_EXPECT_FALSE(test, is_eacces(__VA_ARGS__))
  603. #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
  604. static void test_is_eacces_with_none(struct kunit *const test)
  605. {
  606. const layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
  607. IE_FALSE(&layer_masks, 0);
  608. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_REFER);
  609. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_EXECUTE);
  610. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_WRITE_FILE);
  611. }
  612. static void test_is_eacces_with_refer(struct kunit *const test)
  613. {
  614. const layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {
  615. [BIT_INDEX(LANDLOCK_ACCESS_FS_REFER)] = BIT_ULL(0),
  616. };
  617. IE_FALSE(&layer_masks, 0);
  618. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_REFER);
  619. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_EXECUTE);
  620. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_WRITE_FILE);
  621. }
  622. static void test_is_eacces_with_write(struct kunit *const test)
  623. {
  624. const layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {
  625. [BIT_INDEX(LANDLOCK_ACCESS_FS_WRITE_FILE)] = BIT_ULL(0),
  626. };
  627. IE_FALSE(&layer_masks, 0);
  628. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_REFER);
  629. IE_FALSE(&layer_masks, LANDLOCK_ACCESS_FS_EXECUTE);
  630. IE_TRUE(&layer_masks, LANDLOCK_ACCESS_FS_WRITE_FILE);
  631. }
  632. #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */
  633. #undef IE_TRUE
  634. #undef IE_FALSE
  635. /**
  636. * is_access_to_paths_allowed - Check accesses for requests with a common path
  637. *
  638. * @domain: Domain to check against.
  639. * @path: File hierarchy to walk through.
  640. * @access_request_parent1: Accesses to check, once @layer_masks_parent1 is
  641. * equal to @layer_masks_parent2 (if any). This is tied to the unique
  642. * requested path for most actions, or the source in case of a refer action
  643. * (i.e. rename or link), or the source and destination in case of
  644. * RENAME_EXCHANGE.
  645. * @layer_masks_parent1: Pointer to a matrix of layer masks per access
  646. * masks, identifying the layers that forbid a specific access. Bits from
  647. * this matrix can be unset according to the @path walk. An empty matrix
  648. * means that @domain allows all possible Landlock accesses (i.e. not only
  649. * those identified by @access_request_parent1). This matrix can
  650. * initially refer to domain layer masks and, when the accesses for the
  651. * destination and source are the same, to requested layer masks.
  652. * @dentry_child1: Dentry to the initial child of the parent1 path. This
  653. * pointer must be NULL for non-refer actions (i.e. not link nor rename).
  654. * @access_request_parent2: Similar to @access_request_parent1 but for a
  655. * request involving a source and a destination. This refers to the
  656. * destination, except in case of RENAME_EXCHANGE where it also refers to
  657. * the source. Must be set to 0 when using a simple path request.
  658. * @layer_masks_parent2: Similar to @layer_masks_parent1 but for a refer
  659. * action. This must be NULL otherwise.
  660. * @dentry_child2: Dentry to the initial child of the parent2 path. This
  661. * pointer is only set for RENAME_EXCHANGE actions and must be NULL
  662. * otherwise.
  663. *
  664. * This helper first checks that the destination has a superset of restrictions
  665. * compared to the source (if any) for a common path. Because of
  666. * RENAME_EXCHANGE actions, source and destinations may be swapped. It then
  667. * checks that the collected accesses and the remaining ones are enough to
  668. * allow the request.
  669. *
  670. * Returns:
  671. * - true if the access request is granted;
  672. * - false otherwise.
  673. */
  674. static bool is_access_to_paths_allowed(
  675. const struct landlock_ruleset *const domain,
  676. const struct path *const path,
  677. const access_mask_t access_request_parent1,
  678. layer_mask_t (*const layer_masks_parent1)[LANDLOCK_NUM_ACCESS_FS],
  679. const struct dentry *const dentry_child1,
  680. const access_mask_t access_request_parent2,
  681. layer_mask_t (*const layer_masks_parent2)[LANDLOCK_NUM_ACCESS_FS],
  682. const struct dentry *const dentry_child2)
  683. {
  684. bool allowed_parent1 = false, allowed_parent2 = false, is_dom_check,
  685. child1_is_directory = true, child2_is_directory = true;
  686. struct path walker_path;
  687. access_mask_t access_masked_parent1, access_masked_parent2;
  688. layer_mask_t _layer_masks_child1[LANDLOCK_NUM_ACCESS_FS],
  689. _layer_masks_child2[LANDLOCK_NUM_ACCESS_FS];
  690. layer_mask_t(*layer_masks_child1)[LANDLOCK_NUM_ACCESS_FS] = NULL,
  691. (*layer_masks_child2)[LANDLOCK_NUM_ACCESS_FS] = NULL;
  692. if (!access_request_parent1 && !access_request_parent2)
  693. return true;
  694. if (WARN_ON_ONCE(!domain || !path))
  695. return true;
  696. if (is_nouser_or_private(path->dentry))
  697. return true;
  698. if (WARN_ON_ONCE(domain->num_layers < 1 || !layer_masks_parent1))
  699. return false;
  700. if (unlikely(layer_masks_parent2)) {
  701. if (WARN_ON_ONCE(!dentry_child1))
  702. return false;
  703. /*
  704. * For a double request, first check for potential privilege
  705. * escalation by looking at domain handled accesses (which are
  706. * a superset of the meaningful requested accesses).
  707. */
  708. access_masked_parent1 = access_masked_parent2 =
  709. get_handled_fs_accesses(domain);
  710. is_dom_check = true;
  711. } else {
  712. if (WARN_ON_ONCE(dentry_child1 || dentry_child2))
  713. return false;
  714. /* For a simple request, only check for requested accesses. */
  715. access_masked_parent1 = access_request_parent1;
  716. access_masked_parent2 = access_request_parent2;
  717. is_dom_check = false;
  718. }
  719. if (unlikely(dentry_child1)) {
  720. landlock_unmask_layers(
  721. find_rule(domain, dentry_child1),
  722. landlock_init_layer_masks(
  723. domain, LANDLOCK_MASK_ACCESS_FS,
  724. &_layer_masks_child1, LANDLOCK_KEY_INODE),
  725. &_layer_masks_child1, ARRAY_SIZE(_layer_masks_child1));
  726. layer_masks_child1 = &_layer_masks_child1;
  727. child1_is_directory = d_is_dir(dentry_child1);
  728. }
  729. if (unlikely(dentry_child2)) {
  730. landlock_unmask_layers(
  731. find_rule(domain, dentry_child2),
  732. landlock_init_layer_masks(
  733. domain, LANDLOCK_MASK_ACCESS_FS,
  734. &_layer_masks_child2, LANDLOCK_KEY_INODE),
  735. &_layer_masks_child2, ARRAY_SIZE(_layer_masks_child2));
  736. layer_masks_child2 = &_layer_masks_child2;
  737. child2_is_directory = d_is_dir(dentry_child2);
  738. }
  739. walker_path = *path;
  740. path_get(&walker_path);
  741. /*
  742. * We need to walk through all the hierarchy to not miss any relevant
  743. * restriction.
  744. */
  745. while (true) {
  746. struct dentry *parent_dentry;
  747. const struct landlock_rule *rule;
  748. /*
  749. * If at least all accesses allowed on the destination are
  750. * already allowed on the source, respectively if there is at
  751. * least as much as restrictions on the destination than on the
  752. * source, then we can safely refer files from the source to
  753. * the destination without risking a privilege escalation.
  754. * This also applies in the case of RENAME_EXCHANGE, which
  755. * implies checks on both direction. This is crucial for
  756. * standalone multilayered security policies. Furthermore,
  757. * this helps avoid policy writers to shoot themselves in the
  758. * foot.
  759. */
  760. if (unlikely(is_dom_check &&
  761. no_more_access(
  762. layer_masks_parent1, layer_masks_child1,
  763. child1_is_directory, layer_masks_parent2,
  764. layer_masks_child2,
  765. child2_is_directory))) {
  766. allowed_parent1 = scope_to_request(
  767. access_request_parent1, layer_masks_parent1);
  768. allowed_parent2 = scope_to_request(
  769. access_request_parent2, layer_masks_parent2);
  770. /* Stops when all accesses are granted. */
  771. if (allowed_parent1 && allowed_parent2)
  772. break;
  773. /*
  774. * Now, downgrades the remaining checks from domain
  775. * handled accesses to requested accesses.
  776. */
  777. is_dom_check = false;
  778. access_masked_parent1 = access_request_parent1;
  779. access_masked_parent2 = access_request_parent2;
  780. }
  781. rule = find_rule(domain, walker_path.dentry);
  782. allowed_parent1 = landlock_unmask_layers(
  783. rule, access_masked_parent1, layer_masks_parent1,
  784. ARRAY_SIZE(*layer_masks_parent1));
  785. allowed_parent2 = landlock_unmask_layers(
  786. rule, access_masked_parent2, layer_masks_parent2,
  787. ARRAY_SIZE(*layer_masks_parent2));
  788. /* Stops when a rule from each layer grants access. */
  789. if (allowed_parent1 && allowed_parent2)
  790. break;
  791. jump_up:
  792. if (walker_path.dentry == walker_path.mnt->mnt_root) {
  793. if (follow_up(&walker_path)) {
  794. /* Ignores hidden mount points. */
  795. goto jump_up;
  796. } else {
  797. /*
  798. * Stops at the real root. Denies access
  799. * because not all layers have granted access.
  800. */
  801. break;
  802. }
  803. }
  804. if (unlikely(IS_ROOT(walker_path.dentry))) {
  805. /*
  806. * Stops at disconnected root directories. Only allows
  807. * access to internal filesystems (e.g. nsfs, which is
  808. * reachable through /proc/<pid>/ns/<namespace>).
  809. */
  810. allowed_parent1 = allowed_parent2 =
  811. !!(walker_path.mnt->mnt_flags & MNT_INTERNAL);
  812. break;
  813. }
  814. parent_dentry = dget_parent(walker_path.dentry);
  815. dput(walker_path.dentry);
  816. walker_path.dentry = parent_dentry;
  817. }
  818. path_put(&walker_path);
  819. return allowed_parent1 && allowed_parent2;
  820. }
  821. static int check_access_path(const struct landlock_ruleset *const domain,
  822. const struct path *const path,
  823. access_mask_t access_request)
  824. {
  825. layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
  826. access_request = landlock_init_layer_masks(
  827. domain, access_request, &layer_masks, LANDLOCK_KEY_INODE);
  828. if (is_access_to_paths_allowed(domain, path, access_request,
  829. &layer_masks, NULL, 0, NULL, NULL))
  830. return 0;
  831. return -EACCES;
  832. }
  833. static int current_check_access_path(const struct path *const path,
  834. const access_mask_t access_request)
  835. {
  836. const struct landlock_ruleset *const dom = get_current_fs_domain();
  837. if (!dom)
  838. return 0;
  839. return check_access_path(dom, path, access_request);
  840. }
  841. static access_mask_t get_mode_access(const umode_t mode)
  842. {
  843. switch (mode & S_IFMT) {
  844. case S_IFLNK:
  845. return LANDLOCK_ACCESS_FS_MAKE_SYM;
  846. case S_IFDIR:
  847. return LANDLOCK_ACCESS_FS_MAKE_DIR;
  848. case S_IFCHR:
  849. return LANDLOCK_ACCESS_FS_MAKE_CHAR;
  850. case S_IFBLK:
  851. return LANDLOCK_ACCESS_FS_MAKE_BLOCK;
  852. case S_IFIFO:
  853. return LANDLOCK_ACCESS_FS_MAKE_FIFO;
  854. case S_IFSOCK:
  855. return LANDLOCK_ACCESS_FS_MAKE_SOCK;
  856. case S_IFREG:
  857. case 0:
  858. /* A zero mode translates to S_IFREG. */
  859. default:
  860. /* Treats weird files as regular files. */
  861. return LANDLOCK_ACCESS_FS_MAKE_REG;
  862. }
  863. }
  864. static access_mask_t maybe_remove(const struct dentry *const dentry)
  865. {
  866. if (d_is_negative(dentry))
  867. return 0;
  868. return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR :
  869. LANDLOCK_ACCESS_FS_REMOVE_FILE;
  870. }
  871. /**
  872. * collect_domain_accesses - Walk through a file path and collect accesses
  873. *
  874. * @domain: Domain to check against.
  875. * @mnt_root: Last directory to check.
  876. * @dir: Directory to start the walk from.
  877. * @layer_masks_dom: Where to store the collected accesses.
  878. *
  879. * This helper is useful to begin a path walk from the @dir directory to a
  880. * @mnt_root directory used as a mount point. This mount point is the common
  881. * ancestor between the source and the destination of a renamed and linked
  882. * file. While walking from @dir to @mnt_root, we record all the domain's
  883. * allowed accesses in @layer_masks_dom.
  884. *
  885. * This is similar to is_access_to_paths_allowed() but much simpler because it
  886. * only handles walking on the same mount point and only checks one set of
  887. * accesses.
  888. *
  889. * Returns:
  890. * - true if all the domain access rights are allowed for @dir;
  891. * - false if the walk reached @mnt_root.
  892. */
  893. static bool collect_domain_accesses(
  894. const struct landlock_ruleset *const domain,
  895. const struct dentry *const mnt_root, struct dentry *dir,
  896. layer_mask_t (*const layer_masks_dom)[LANDLOCK_NUM_ACCESS_FS])
  897. {
  898. unsigned long access_dom;
  899. bool ret = false;
  900. if (WARN_ON_ONCE(!domain || !mnt_root || !dir || !layer_masks_dom))
  901. return true;
  902. if (is_nouser_or_private(dir))
  903. return true;
  904. access_dom = landlock_init_layer_masks(domain, LANDLOCK_MASK_ACCESS_FS,
  905. layer_masks_dom,
  906. LANDLOCK_KEY_INODE);
  907. dget(dir);
  908. while (true) {
  909. struct dentry *parent_dentry;
  910. /* Gets all layers allowing all domain accesses. */
  911. if (landlock_unmask_layers(find_rule(domain, dir), access_dom,
  912. layer_masks_dom,
  913. ARRAY_SIZE(*layer_masks_dom))) {
  914. /*
  915. * Stops when all handled accesses are allowed by at
  916. * least one rule in each layer.
  917. */
  918. ret = true;
  919. break;
  920. }
  921. /* We should not reach a root other than @mnt_root. */
  922. if (dir == mnt_root || WARN_ON_ONCE(IS_ROOT(dir)))
  923. break;
  924. parent_dentry = dget_parent(dir);
  925. dput(dir);
  926. dir = parent_dentry;
  927. }
  928. dput(dir);
  929. return ret;
  930. }
  931. /**
  932. * current_check_refer_path - Check if a rename or link action is allowed
  933. *
  934. * @old_dentry: File or directory requested to be moved or linked.
  935. * @new_dir: Destination parent directory.
  936. * @new_dentry: Destination file or directory.
  937. * @removable: Sets to true if it is a rename operation.
  938. * @exchange: Sets to true if it is a rename operation with RENAME_EXCHANGE.
  939. *
  940. * Because of its unprivileged constraints, Landlock relies on file hierarchies
  941. * (and not only inodes) to tie access rights to files. Being able to link or
  942. * rename a file hierarchy brings some challenges. Indeed, moving or linking a
  943. * file (i.e. creating a new reference to an inode) can have an impact on the
  944. * actions allowed for a set of files if it would change its parent directory
  945. * (i.e. reparenting).
  946. *
  947. * To avoid trivial access right bypasses, Landlock first checks if the file or
  948. * directory requested to be moved would gain new access rights inherited from
  949. * its new hierarchy. Before returning any error, Landlock then checks that
  950. * the parent source hierarchy and the destination hierarchy would allow the
  951. * link or rename action. If it is not the case, an error with EACCES is
  952. * returned to inform user space that there is no way to remove or create the
  953. * requested source file type. If it should be allowed but the new inherited
  954. * access rights would be greater than the source access rights, then the
  955. * kernel returns an error with EXDEV. Prioritizing EACCES over EXDEV enables
  956. * user space to abort the whole operation if there is no way to do it, or to
  957. * manually copy the source to the destination if this remains allowed, e.g.
  958. * because file creation is allowed on the destination directory but not direct
  959. * linking.
  960. *
  961. * To achieve this goal, the kernel needs to compare two file hierarchies: the
  962. * one identifying the source file or directory (including itself), and the
  963. * destination one. This can be seen as a multilayer partial ordering problem.
  964. * The kernel walks through these paths and collects in a matrix the access
  965. * rights that are denied per layer. These matrices are then compared to see
  966. * if the destination one has more (or the same) restrictions as the source
  967. * one. If this is the case, the requested action will not return EXDEV, which
  968. * doesn't mean the action is allowed. The parent hierarchy of the source
  969. * (i.e. parent directory), and the destination hierarchy must also be checked
  970. * to verify that they explicitly allow such action (i.e. referencing,
  971. * creation and potentially removal rights). The kernel implementation is then
  972. * required to rely on potentially four matrices of access rights: one for the
  973. * source file or directory (i.e. the child), a potentially other one for the
  974. * other source/destination (in case of RENAME_EXCHANGE), one for the source
  975. * parent hierarchy and a last one for the destination hierarchy. These
  976. * ephemeral matrices take some space on the stack, which limits the number of
  977. * layers to a deemed reasonable number: 16.
  978. *
  979. * Returns:
  980. * - 0 if access is allowed;
  981. * - -EXDEV if @old_dentry would inherit new access rights from @new_dir;
  982. * - -EACCES if file removal or creation is denied.
  983. */
  984. static int current_check_refer_path(struct dentry *const old_dentry,
  985. const struct path *const new_dir,
  986. struct dentry *const new_dentry,
  987. const bool removable, const bool exchange)
  988. {
  989. const struct landlock_ruleset *const dom = get_current_fs_domain();
  990. bool allow_parent1, allow_parent2;
  991. access_mask_t access_request_parent1, access_request_parent2;
  992. struct path mnt_dir;
  993. struct dentry *old_parent;
  994. layer_mask_t layer_masks_parent1[LANDLOCK_NUM_ACCESS_FS] = {},
  995. layer_masks_parent2[LANDLOCK_NUM_ACCESS_FS] = {};
  996. if (!dom)
  997. return 0;
  998. if (WARN_ON_ONCE(dom->num_layers < 1))
  999. return -EACCES;
  1000. if (unlikely(d_is_negative(old_dentry)))
  1001. return -ENOENT;
  1002. if (exchange) {
  1003. if (unlikely(d_is_negative(new_dentry)))
  1004. return -ENOENT;
  1005. access_request_parent1 =
  1006. get_mode_access(d_backing_inode(new_dentry)->i_mode);
  1007. } else {
  1008. access_request_parent1 = 0;
  1009. }
  1010. access_request_parent2 =
  1011. get_mode_access(d_backing_inode(old_dentry)->i_mode);
  1012. if (removable) {
  1013. access_request_parent1 |= maybe_remove(old_dentry);
  1014. access_request_parent2 |= maybe_remove(new_dentry);
  1015. }
  1016. /* The mount points are the same for old and new paths, cf. EXDEV. */
  1017. if (old_dentry->d_parent == new_dir->dentry) {
  1018. /*
  1019. * The LANDLOCK_ACCESS_FS_REFER access right is not required
  1020. * for same-directory referer (i.e. no reparenting).
  1021. */
  1022. access_request_parent1 = landlock_init_layer_masks(
  1023. dom, access_request_parent1 | access_request_parent2,
  1024. &layer_masks_parent1, LANDLOCK_KEY_INODE);
  1025. if (is_access_to_paths_allowed(
  1026. dom, new_dir, access_request_parent1,
  1027. &layer_masks_parent1, NULL, 0, NULL, NULL))
  1028. return 0;
  1029. return -EACCES;
  1030. }
  1031. access_request_parent1 |= LANDLOCK_ACCESS_FS_REFER;
  1032. access_request_parent2 |= LANDLOCK_ACCESS_FS_REFER;
  1033. /* Saves the common mount point. */
  1034. mnt_dir.mnt = new_dir->mnt;
  1035. mnt_dir.dentry = new_dir->mnt->mnt_root;
  1036. /*
  1037. * old_dentry may be the root of the common mount point and
  1038. * !IS_ROOT(old_dentry) at the same time (e.g. with open_tree() and
  1039. * OPEN_TREE_CLONE). We do not need to call dget(old_parent) because
  1040. * we keep a reference to old_dentry.
  1041. */
  1042. old_parent = (old_dentry == mnt_dir.dentry) ? old_dentry :
  1043. old_dentry->d_parent;
  1044. /* new_dir->dentry is equal to new_dentry->d_parent */
  1045. allow_parent1 = collect_domain_accesses(dom, mnt_dir.dentry, old_parent,
  1046. &layer_masks_parent1);
  1047. allow_parent2 = collect_domain_accesses(
  1048. dom, mnt_dir.dentry, new_dir->dentry, &layer_masks_parent2);
  1049. if (allow_parent1 && allow_parent2)
  1050. return 0;
  1051. /*
  1052. * To be able to compare source and destination domain access rights,
  1053. * take into account the @old_dentry access rights aggregated with its
  1054. * parent access rights. This will be useful to compare with the
  1055. * destination parent access rights.
  1056. */
  1057. if (is_access_to_paths_allowed(
  1058. dom, &mnt_dir, access_request_parent1, &layer_masks_parent1,
  1059. old_dentry, access_request_parent2, &layer_masks_parent2,
  1060. exchange ? new_dentry : NULL))
  1061. return 0;
  1062. /*
  1063. * This prioritizes EACCES over EXDEV for all actions, including
  1064. * renames with RENAME_EXCHANGE.
  1065. */
  1066. if (likely(is_eacces(&layer_masks_parent1, access_request_parent1) ||
  1067. is_eacces(&layer_masks_parent2, access_request_parent2)))
  1068. return -EACCES;
  1069. /*
  1070. * Gracefully forbids reparenting if the destination directory
  1071. * hierarchy is not a superset of restrictions of the source directory
  1072. * hierarchy, or if LANDLOCK_ACCESS_FS_REFER is not allowed by the
  1073. * source or the destination.
  1074. */
  1075. return -EXDEV;
  1076. }
  1077. /* Inode hooks */
  1078. static void hook_inode_free_security_rcu(void *inode_security)
  1079. {
  1080. struct landlock_inode_security *inode_sec;
  1081. /*
  1082. * All inodes must already have been untied from their object by
  1083. * release_inode() or hook_sb_delete().
  1084. */
  1085. inode_sec = inode_security + landlock_blob_sizes.lbs_inode;
  1086. WARN_ON_ONCE(inode_sec->object);
  1087. }
  1088. /* Super-block hooks */
  1089. /*
  1090. * Release the inodes used in a security policy.
  1091. *
  1092. * Cf. fsnotify_unmount_inodes() and invalidate_inodes()
  1093. */
  1094. static void hook_sb_delete(struct super_block *const sb)
  1095. {
  1096. struct inode *inode, *prev_inode = NULL;
  1097. if (!landlock_initialized)
  1098. return;
  1099. spin_lock(&sb->s_inode_list_lock);
  1100. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  1101. struct landlock_object *object;
  1102. /* Only handles referenced inodes. */
  1103. if (!atomic_read(&inode->i_count))
  1104. continue;
  1105. /*
  1106. * Protects against concurrent modification of inode (e.g.
  1107. * from get_inode_object()).
  1108. */
  1109. spin_lock(&inode->i_lock);
  1110. /*
  1111. * Checks I_FREEING and I_WILL_FREE to protect against a race
  1112. * condition when release_inode() just called iput(), which
  1113. * could lead to a NULL dereference of inode->security or a
  1114. * second call to iput() for the same Landlock object. Also
  1115. * checks I_NEW because such inode cannot be tied to an object.
  1116. */
  1117. if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
  1118. spin_unlock(&inode->i_lock);
  1119. continue;
  1120. }
  1121. rcu_read_lock();
  1122. object = rcu_dereference(landlock_inode(inode)->object);
  1123. if (!object) {
  1124. rcu_read_unlock();
  1125. spin_unlock(&inode->i_lock);
  1126. continue;
  1127. }
  1128. /* Keeps a reference to this inode until the next loop walk. */
  1129. __iget(inode);
  1130. spin_unlock(&inode->i_lock);
  1131. /*
  1132. * If there is no concurrent release_inode() ongoing, then we
  1133. * are in charge of calling iput() on this inode, otherwise we
  1134. * will just wait for it to finish.
  1135. */
  1136. spin_lock(&object->lock);
  1137. if (object->underobj == inode) {
  1138. object->underobj = NULL;
  1139. spin_unlock(&object->lock);
  1140. rcu_read_unlock();
  1141. /*
  1142. * Because object->underobj was not NULL,
  1143. * release_inode() and get_inode_object() guarantee
  1144. * that it is safe to reset
  1145. * landlock_inode(inode)->object while it is not NULL.
  1146. * It is therefore not necessary to lock inode->i_lock.
  1147. */
  1148. rcu_assign_pointer(landlock_inode(inode)->object, NULL);
  1149. /*
  1150. * At this point, we own the ihold() reference that was
  1151. * originally set up by get_inode_object() and the
  1152. * __iget() reference that we just set in this loop
  1153. * walk. Therefore the following call to iput() will
  1154. * not sleep nor drop the inode because there is now at
  1155. * least two references to it.
  1156. */
  1157. iput(inode);
  1158. } else {
  1159. spin_unlock(&object->lock);
  1160. rcu_read_unlock();
  1161. }
  1162. if (prev_inode) {
  1163. /*
  1164. * At this point, we still own the __iget() reference
  1165. * that we just set in this loop walk. Therefore we
  1166. * can drop the list lock and know that the inode won't
  1167. * disappear from under us until the next loop walk.
  1168. */
  1169. spin_unlock(&sb->s_inode_list_lock);
  1170. /*
  1171. * We can now actually put the inode reference from the
  1172. * previous loop walk, which is not needed anymore.
  1173. */
  1174. iput(prev_inode);
  1175. cond_resched();
  1176. spin_lock(&sb->s_inode_list_lock);
  1177. }
  1178. prev_inode = inode;
  1179. }
  1180. spin_unlock(&sb->s_inode_list_lock);
  1181. /* Puts the inode reference from the last loop walk, if any. */
  1182. if (prev_inode)
  1183. iput(prev_inode);
  1184. /* Waits for pending iput() in release_inode(). */
  1185. wait_var_event(&landlock_superblock(sb)->inode_refs,
  1186. !atomic_long_read(&landlock_superblock(sb)->inode_refs));
  1187. }
  1188. /*
  1189. * Because a Landlock security policy is defined according to the filesystem
  1190. * topology (i.e. the mount namespace), changing it may grant access to files
  1191. * not previously allowed.
  1192. *
  1193. * To make it simple, deny any filesystem topology modification by landlocked
  1194. * processes. Non-landlocked processes may still change the namespace of a
  1195. * landlocked process, but this kind of threat must be handled by a system-wide
  1196. * access-control security policy.
  1197. *
  1198. * This could be lifted in the future if Landlock can safely handle mount
  1199. * namespace updates requested by a landlocked process. Indeed, we could
  1200. * update the current domain (which is currently read-only) by taking into
  1201. * account the accesses of the source and the destination of a new mount point.
  1202. * However, it would also require to make all the child domains dynamically
  1203. * inherit these new constraints. Anyway, for backward compatibility reasons,
  1204. * a dedicated user space option would be required (e.g. as a ruleset flag).
  1205. */
  1206. static int hook_sb_mount(const char *const dev_name,
  1207. const struct path *const path, const char *const type,
  1208. const unsigned long flags, void *const data)
  1209. {
  1210. if (!get_current_fs_domain())
  1211. return 0;
  1212. return -EPERM;
  1213. }
  1214. static int hook_move_mount(const struct path *const from_path,
  1215. const struct path *const to_path)
  1216. {
  1217. if (!get_current_fs_domain())
  1218. return 0;
  1219. return -EPERM;
  1220. }
  1221. /*
  1222. * Removing a mount point may reveal a previously hidden file hierarchy, which
  1223. * may then grant access to files, which may have previously been forbidden.
  1224. */
  1225. static int hook_sb_umount(struct vfsmount *const mnt, const int flags)
  1226. {
  1227. if (!get_current_fs_domain())
  1228. return 0;
  1229. return -EPERM;
  1230. }
  1231. static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts)
  1232. {
  1233. if (!get_current_fs_domain())
  1234. return 0;
  1235. return -EPERM;
  1236. }
  1237. /*
  1238. * pivot_root(2), like mount(2), changes the current mount namespace. It must
  1239. * then be forbidden for a landlocked process.
  1240. *
  1241. * However, chroot(2) may be allowed because it only changes the relative root
  1242. * directory of the current process. Moreover, it can be used to restrict the
  1243. * view of the filesystem.
  1244. */
  1245. static int hook_sb_pivotroot(const struct path *const old_path,
  1246. const struct path *const new_path)
  1247. {
  1248. if (!get_current_fs_domain())
  1249. return 0;
  1250. return -EPERM;
  1251. }
  1252. /* Path hooks */
  1253. static int hook_path_link(struct dentry *const old_dentry,
  1254. const struct path *const new_dir,
  1255. struct dentry *const new_dentry)
  1256. {
  1257. return current_check_refer_path(old_dentry, new_dir, new_dentry, false,
  1258. false);
  1259. }
  1260. static int hook_path_rename(const struct path *const old_dir,
  1261. struct dentry *const old_dentry,
  1262. const struct path *const new_dir,
  1263. struct dentry *const new_dentry,
  1264. const unsigned int flags)
  1265. {
  1266. /* old_dir refers to old_dentry->d_parent and new_dir->mnt */
  1267. return current_check_refer_path(old_dentry, new_dir, new_dentry, true,
  1268. !!(flags & RENAME_EXCHANGE));
  1269. }
  1270. static int hook_path_mkdir(const struct path *const dir,
  1271. struct dentry *const dentry, const umode_t mode)
  1272. {
  1273. return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR);
  1274. }
  1275. static int hook_path_mknod(const struct path *const dir,
  1276. struct dentry *const dentry, const umode_t mode,
  1277. const unsigned int dev)
  1278. {
  1279. const struct landlock_ruleset *const dom = get_current_fs_domain();
  1280. if (!dom)
  1281. return 0;
  1282. return check_access_path(dom, dir, get_mode_access(mode));
  1283. }
  1284. static int hook_path_symlink(const struct path *const dir,
  1285. struct dentry *const dentry,
  1286. const char *const old_name)
  1287. {
  1288. return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM);
  1289. }
  1290. static int hook_path_unlink(const struct path *const dir,
  1291. struct dentry *const dentry)
  1292. {
  1293. return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE);
  1294. }
  1295. static int hook_path_rmdir(const struct path *const dir,
  1296. struct dentry *const dentry)
  1297. {
  1298. return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR);
  1299. }
  1300. static int hook_path_truncate(const struct path *const path)
  1301. {
  1302. return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE);
  1303. }
  1304. /* File hooks */
  1305. /**
  1306. * get_required_file_open_access - Get access needed to open a file
  1307. *
  1308. * @file: File being opened.
  1309. *
  1310. * Returns the access rights that are required for opening the given file,
  1311. * depending on the file type and open mode.
  1312. */
  1313. static access_mask_t
  1314. get_required_file_open_access(const struct file *const file)
  1315. {
  1316. access_mask_t access = 0;
  1317. if (file->f_mode & FMODE_READ) {
  1318. /* A directory can only be opened in read mode. */
  1319. if (S_ISDIR(file_inode(file)->i_mode))
  1320. return LANDLOCK_ACCESS_FS_READ_DIR;
  1321. access = LANDLOCK_ACCESS_FS_READ_FILE;
  1322. }
  1323. if (file->f_mode & FMODE_WRITE)
  1324. access |= LANDLOCK_ACCESS_FS_WRITE_FILE;
  1325. /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */
  1326. if (file->f_flags & __FMODE_EXEC)
  1327. access |= LANDLOCK_ACCESS_FS_EXECUTE;
  1328. return access;
  1329. }
  1330. static int hook_file_alloc_security(struct file *const file)
  1331. {
  1332. /*
  1333. * Grants all access rights, even if most of them are not checked later
  1334. * on. It is more consistent.
  1335. *
  1336. * Notably, file descriptors for regular files can also be acquired
  1337. * without going through the file_open hook, for example when using
  1338. * memfd_create(2).
  1339. */
  1340. landlock_file(file)->allowed_access = LANDLOCK_MASK_ACCESS_FS;
  1341. return 0;
  1342. }
  1343. static bool is_device(const struct file *const file)
  1344. {
  1345. const struct inode *inode = file_inode(file);
  1346. return S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode);
  1347. }
  1348. static int hook_file_open(struct file *const file)
  1349. {
  1350. layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
  1351. access_mask_t open_access_request, full_access_request, allowed_access,
  1352. optional_access;
  1353. const struct landlock_ruleset *const dom =
  1354. landlock_get_applicable_domain(
  1355. landlock_cred(file->f_cred)->domain, any_fs);
  1356. if (!dom)
  1357. return 0;
  1358. /*
  1359. * Because a file may be opened with O_PATH, get_required_file_open_access()
  1360. * may return 0. This case will be handled with a future Landlock
  1361. * evolution.
  1362. */
  1363. open_access_request = get_required_file_open_access(file);
  1364. /*
  1365. * We look up more access than what we immediately need for open(), so
  1366. * that we can later authorize operations on opened files.
  1367. */
  1368. optional_access = LANDLOCK_ACCESS_FS_TRUNCATE;
  1369. if (is_device(file))
  1370. optional_access |= LANDLOCK_ACCESS_FS_IOCTL_DEV;
  1371. full_access_request = open_access_request | optional_access;
  1372. if (is_access_to_paths_allowed(
  1373. dom, &file->f_path,
  1374. landlock_init_layer_masks(dom, full_access_request,
  1375. &layer_masks, LANDLOCK_KEY_INODE),
  1376. &layer_masks, NULL, 0, NULL, NULL)) {
  1377. allowed_access = full_access_request;
  1378. } else {
  1379. unsigned long access_bit;
  1380. const unsigned long access_req = full_access_request;
  1381. /*
  1382. * Calculate the actual allowed access rights from layer_masks.
  1383. * Add each access right to allowed_access which has not been
  1384. * vetoed by any layer.
  1385. */
  1386. allowed_access = 0;
  1387. for_each_set_bit(access_bit, &access_req,
  1388. ARRAY_SIZE(layer_masks)) {
  1389. if (!layer_masks[access_bit])
  1390. allowed_access |= BIT_ULL(access_bit);
  1391. }
  1392. }
  1393. /*
  1394. * For operations on already opened files (i.e. ftruncate()), it is the
  1395. * access rights at the time of open() which decide whether the
  1396. * operation is permitted. Therefore, we record the relevant subset of
  1397. * file access rights in the opened struct file.
  1398. */
  1399. landlock_file(file)->allowed_access = allowed_access;
  1400. if ((open_access_request & allowed_access) == open_access_request)
  1401. return 0;
  1402. return -EACCES;
  1403. }
  1404. static int hook_file_truncate(struct file *const file)
  1405. {
  1406. /*
  1407. * Allows truncation if the truncate right was available at the time of
  1408. * opening the file, to get a consistent access check as for read, write
  1409. * and execute operations.
  1410. *
  1411. * Note: For checks done based on the file's Landlock allowed access, we
  1412. * enforce them independently of whether the current thread is in a
  1413. * Landlock domain, so that open files passed between independent
  1414. * processes retain their behaviour.
  1415. */
  1416. if (landlock_file(file)->allowed_access & LANDLOCK_ACCESS_FS_TRUNCATE)
  1417. return 0;
  1418. return -EACCES;
  1419. }
  1420. static int hook_file_ioctl(struct file *file, unsigned int cmd,
  1421. unsigned long arg)
  1422. {
  1423. access_mask_t allowed_access = landlock_file(file)->allowed_access;
  1424. /*
  1425. * It is the access rights at the time of opening the file which
  1426. * determine whether IOCTL can be used on the opened file later.
  1427. *
  1428. * The access right is attached to the opened file in hook_file_open().
  1429. */
  1430. if (allowed_access & LANDLOCK_ACCESS_FS_IOCTL_DEV)
  1431. return 0;
  1432. if (!is_device(file))
  1433. return 0;
  1434. if (is_masked_device_ioctl(cmd))
  1435. return 0;
  1436. return -EACCES;
  1437. }
  1438. static int hook_file_ioctl_compat(struct file *file, unsigned int cmd,
  1439. unsigned long arg)
  1440. {
  1441. access_mask_t allowed_access = landlock_file(file)->allowed_access;
  1442. /*
  1443. * It is the access rights at the time of opening the file which
  1444. * determine whether IOCTL can be used on the opened file later.
  1445. *
  1446. * The access right is attached to the opened file in hook_file_open().
  1447. */
  1448. if (allowed_access & LANDLOCK_ACCESS_FS_IOCTL_DEV)
  1449. return 0;
  1450. if (!is_device(file))
  1451. return 0;
  1452. if (is_masked_device_ioctl_compat(cmd))
  1453. return 0;
  1454. return -EACCES;
  1455. }
  1456. static void hook_file_set_fowner(struct file *file)
  1457. {
  1458. struct landlock_ruleset *new_dom, *prev_dom;
  1459. /*
  1460. * Lock already held by __f_setown(), see commit 26f204380a3c ("fs: Fix
  1461. * file_set_fowner LSM hook inconsistencies").
  1462. */
  1463. lockdep_assert_held(&file_f_owner(file)->lock);
  1464. new_dom = landlock_get_current_domain();
  1465. landlock_get_ruleset(new_dom);
  1466. prev_dom = landlock_file(file)->fown_domain;
  1467. landlock_file(file)->fown_domain = new_dom;
  1468. /* Called in an RCU read-side critical section. */
  1469. landlock_put_ruleset_deferred(prev_dom);
  1470. }
  1471. static void hook_file_free_security(struct file *file)
  1472. {
  1473. landlock_put_ruleset_deferred(landlock_file(file)->fown_domain);
  1474. }
  1475. static struct security_hook_list landlock_hooks[] __ro_after_init = {
  1476. LSM_HOOK_INIT(inode_free_security_rcu, hook_inode_free_security_rcu),
  1477. LSM_HOOK_INIT(sb_delete, hook_sb_delete),
  1478. LSM_HOOK_INIT(sb_mount, hook_sb_mount),
  1479. LSM_HOOK_INIT(move_mount, hook_move_mount),
  1480. LSM_HOOK_INIT(sb_umount, hook_sb_umount),
  1481. LSM_HOOK_INIT(sb_remount, hook_sb_remount),
  1482. LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot),
  1483. LSM_HOOK_INIT(path_link, hook_path_link),
  1484. LSM_HOOK_INIT(path_rename, hook_path_rename),
  1485. LSM_HOOK_INIT(path_mkdir, hook_path_mkdir),
  1486. LSM_HOOK_INIT(path_mknod, hook_path_mknod),
  1487. LSM_HOOK_INIT(path_symlink, hook_path_symlink),
  1488. LSM_HOOK_INIT(path_unlink, hook_path_unlink),
  1489. LSM_HOOK_INIT(path_rmdir, hook_path_rmdir),
  1490. LSM_HOOK_INIT(path_truncate, hook_path_truncate),
  1491. LSM_HOOK_INIT(file_alloc_security, hook_file_alloc_security),
  1492. LSM_HOOK_INIT(file_open, hook_file_open),
  1493. LSM_HOOK_INIT(file_truncate, hook_file_truncate),
  1494. LSM_HOOK_INIT(file_ioctl, hook_file_ioctl),
  1495. LSM_HOOK_INIT(file_ioctl_compat, hook_file_ioctl_compat),
  1496. LSM_HOOK_INIT(file_set_fowner, hook_file_set_fowner),
  1497. LSM_HOOK_INIT(file_free_security, hook_file_free_security),
  1498. };
  1499. __init void landlock_add_fs_hooks(void)
  1500. {
  1501. security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
  1502. &landlock_lsmid);
  1503. }
  1504. #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
  1505. /* clang-format off */
  1506. static struct kunit_case test_cases[] = {
  1507. KUNIT_CASE(test_no_more_access),
  1508. KUNIT_CASE(test_scope_to_request_with_exec_none),
  1509. KUNIT_CASE(test_scope_to_request_with_exec_some),
  1510. KUNIT_CASE(test_scope_to_request_without_access),
  1511. KUNIT_CASE(test_is_eacces_with_none),
  1512. KUNIT_CASE(test_is_eacces_with_refer),
  1513. KUNIT_CASE(test_is_eacces_with_write),
  1514. {}
  1515. };
  1516. /* clang-format on */
  1517. static struct kunit_suite test_suite = {
  1518. .name = "landlock_fs",
  1519. .test_cases = test_cases,
  1520. };
  1521. kunit_test_suite(test_suite);
  1522. #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */