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