xref: /linux/security/landlock/fs.h (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Landlock - Filesystem management and hooks
4  *
5  * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
6  * Copyright © 2018-2020 ANSSI
7  * Copyright © 2024-2025 Microsoft Corporation
8  */
9 
10 #ifndef _SECURITY_LANDLOCK_FS_H
11 #define _SECURITY_LANDLOCK_FS_H
12 
13 #include <linux/build_bug.h>
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/rcupdate.h>
17 
18 #include "access.h"
19 #include "cred.h"
20 #include "ruleset.h"
21 #include "setup.h"
22 
23 /**
24  * struct landlock_inode_security - Inode security blob
25  *
26  * Enable to reference a &struct landlock_object tied to an inode (i.e.
27  * underlying object).
28  */
29 struct landlock_inode_security {
30 	/**
31 	 * @object: Weak pointer to an allocated object.  All assignments of a
32 	 * new object are protected by the underlying inode->i_lock.  However,
33 	 * atomically disassociating @object from the inode is only protected
34 	 * by @object->lock, from the time @object's usage refcount drops to
35 	 * zero to the time this pointer is nulled out (cf. release_inode() and
36 	 * hook_sb_delete()).  Indeed, such disassociation doesn't require
37 	 * inode->i_lock thanks to the careful rcu_access_pointer() check
38 	 * performed by get_inode_object().
39 	 */
40 	struct landlock_object __rcu *object;
41 };
42 
43 /**
44  * struct landlock_file_security - File security blob
45  *
46  * This information is populated when opening a file in hook_file_open, and
47  * tracks the relevant Landlock access rights that were available at the time
48  * of opening the file. Other LSM hooks use these rights in order to authorize
49  * operations on already opened files.
50  */
51 struct landlock_file_security {
52 	/**
53 	 * @allowed_access: Access rights that were available at the time of
54 	 * opening the file. This is not necessarily the full set of access
55 	 * rights available at that time, but it's the necessary subset as
56 	 * needed to authorize later operations on the open file.
57 	 */
58 	access_mask_t allowed_access;
59 
60 #ifdef CONFIG_AUDIT
61 	/**
62 	 * @deny_masks: Domain layer levels that deny an optional access (see
63 	 * _LANDLOCK_ACCESS_FS_OPTIONAL).
64 	 */
65 	deny_masks_t deny_masks;
66 	/**
67 	 * @fown_layer: Layer level of @fown_subject->domain with
68 	 * LANDLOCK_SCOPE_SIGNAL.
69 	 */
70 	u8 fown_layer;
71 #endif /* CONFIG_AUDIT */
72 
73 	/**
74 	 * @fown_subject: Landlock credential of the task that set the PID that
75 	 * may receive a signal e.g., SIGURG when writing MSG_OOB to the
76 	 * related socket.  This pointer is protected by the related
77 	 * file->f_owner->lock, as for fown_struct's members: pid, uid, and
78 	 * euid.
79 	 */
80 	struct landlock_cred_security fown_subject;
81 };
82 
83 #ifdef CONFIG_AUDIT
84 
85 /* Makes sure all layers can be identified. */
86 /* clang-format off */
87 static_assert((typeof_member(struct landlock_file_security, fown_layer))~0 >=
88 	      LANDLOCK_MAX_NUM_LAYERS);
89 /* clang-format off */
90 
91 #endif /* CONFIG_AUDIT */
92 
93 /**
94  * struct landlock_superblock_security - Superblock security blob
95  *
96  * Enable hook_sb_delete() to wait for concurrent calls to release_inode().
97  */
98 struct landlock_superblock_security {
99 	/**
100 	 * @inode_refs: Number of pending inodes (from this superblock) that
101 	 * are being released by release_inode().
102 	 * Cf. struct super_block->s_fsnotify_inode_refs .
103 	 */
104 	atomic_long_t inode_refs;
105 };
106 
107 static inline struct landlock_file_security *
108 landlock_file(const struct file *const file)
109 {
110 	return file->f_security + landlock_blob_sizes.lbs_file;
111 }
112 
113 static inline struct landlock_inode_security *
114 landlock_inode(const struct inode *const inode)
115 {
116 	return inode->i_security + landlock_blob_sizes.lbs_inode;
117 }
118 
119 static inline struct landlock_superblock_security *
120 landlock_superblock(const struct super_block *const superblock)
121 {
122 	return superblock->s_security + landlock_blob_sizes.lbs_superblock;
123 }
124 
125 __init void landlock_add_fs_hooks(void);
126 
127 int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
128 			    const struct path *const path,
129 			    access_mask_t access_hierarchy);
130 
131 #endif /* _SECURITY_LANDLOCK_FS_H */
132