fs.c 58 KB

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