Pull Landlock LSM from James Morris: "Add Landlock, a new LSM from Mickaël Salaün. Briefly, Landlock provides for unprivileged application sandboxing. From Mickaël's cover letter: "The goal of Landlock is to enable to restrict ambient rights (e.g. global filesystem access) for a set of processes. Because Landlock is a stackable LSM [1], it makes possible to create safe security sandboxes as new security layers in addition to the existing system-wide access-controls. This kind of sandbox is expected to help mitigate the security impact of bugs or unexpected/malicious behaviors in user-space applications. Landlock empowers any process, including unprivileged ones, to securely restrict themselves. Landlock is inspired by seccomp-bpf but instead of filtering syscalls and their raw arguments, a Landlock rule can restrict the use of kernel objects like file hierarchies, according to the kernel semantic. Landlock also takes inspiration from other OS sandbox mechanisms: XNU Sandbox, FreeBSD Capsicum or OpenBSD Pledge/Unveil. In this current form, Landlock misses some access-control features. This enables to minimize this patch series and ease review. This series still addresses multiple use cases, especially with the combined use of seccomp-bpf: applications with built-in sandboxing, init systems, security sandbox tools and security-oriented APIs [2]" The cover letter and v34 posting is here: https://lore.kernel.org/linux-security-module/20210422154123.13086-1-mic@digikod.net/ See also: https://landlock.io/ This code has had extensive design discussion and review over several years" Link: https://lore.kernel.org/lkml/50db058a-7dde-441b-a7f9-f6837fe8b69f@schaufler-ca.com/ [1] Link: https://lore.kernel.org/lkml/f646e1c7-33cf-333f-070c-0a40ad0468cd@digikod.net/ [2] * tag 'landlock_v34' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: landlock: Enable user space to infer supported features landlock: Add user and kernel documentation samples/landlock: Add a sandbox manager example selftests/landlock: Add user space tests landlock: Add syscall implementations arch: Wire up Landlock syscalls fs,security: Add sb_delete hook landlock: Support filesystem access-control LSM: Infrastructure management of the superblock landlock: Add ptrace restrictions landlock: Set up the security framework and manage credentials landlock: Add ruleset and domain management landlock: Add object management
Showing the changes to security (25 of 72 files). Show all
Unified · Split
| @@ -238,6 +238,7 @@ source "security/loadpin/Kconfig" · 237 unmodified lines | |||
| 238 | source "security/yama/Kconfig" | 238 | source "security/yama/Kconfig" |
| 239 | source "security/safesetid/Kconfig" | 239 | source "security/safesetid/Kconfig" |
| 240 | source "security/lockdown/Kconfig" | 240 | source "security/lockdown/Kconfig" |
| 241 | source "security/landlock/Kconfig" | ||
| 241 | 242 | ||
| 242 | source "security/integrity/Kconfig" | 243 | source "security/integrity/Kconfig" |
| 243 | 244 | ||
| @@ -277,11 +278,11 @@ endchoice · 33 unmodified lines | |||
| 277 | 278 | ||
| 278 | config LSM | 279 | config LSM |
| 279 | string "Ordered list of enabled LSMs" | 280 | string "Ordered list of enabled LSMs" |
| 280 | default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK | 281 | default "landlock,lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK |
| 281 | default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR | 282 | default "landlock,lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR |
| 282 | default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO | 283 | default "landlock,lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO |
| 283 | default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC | 284 | default "landlock,lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC |
| 284 | default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" | 285 | default "landlock,lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" |
| 285 | help | 286 | help |
| 286 | A comma-separated list of LSMs, in initialization order. | 287 | A comma-separated list of LSMs, in initialization order. |
| 287 | Any LSMs left off this list will be ignored. This can be | 288 | Any LSMs left off this list will be ignored. This can be |
| @@ -13,6 +13,7 @@ subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin · 12 unmodified lines | |||
| 13 | subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid | 13 | subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid |
| 14 | subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown | 14 | subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown |
| 15 | subdir-$(CONFIG_BPF_LSM) += bpf | 15 | subdir-$(CONFIG_BPF_LSM) += bpf |
| 16 | subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock | ||
| 16 | 17 | ||
| 17 | # always enable default capabilities | 18 | # always enable default capabilities |
| 18 | obj-y += commoncap.o | 19 | obj-y += commoncap.o |
| @@ -32,6 +33,7 @@ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ · 13 unmodified lines | |||
| 32 | obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ | 33 | obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ |
| 33 | obj-$(CONFIG_CGROUPS) += device_cgroup.o | 34 | obj-$(CONFIG_CGROUPS) += device_cgroup.o |
| 34 | obj-$(CONFIG_BPF_LSM) += bpf/ | 35 | obj-$(CONFIG_BPF_LSM) += bpf/ |
| 36 | obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ | ||
| 35 | 37 | ||
| 36 | # Object integrity file lists | 38 | # Object integrity file lists |
| 37 | subdir-$(CONFIG_INTEGRITY) += integrity | 39 | subdir-$(CONFIG_INTEGRITY) += integrity |
| @@ -0,0 +1,21 @@ | |||
| 1 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | |||
| 3 | config SECURITY_LANDLOCK | ||
| 4 | bool "Landlock support" | ||
| 5 | depends on SECURITY && !ARCH_EPHEMERAL_INODES | ||
| 6 | select SECURITY_PATH | ||
| 7 | help | ||
| 8 | Landlock is a sandboxing mechanism that enables processes to restrict | ||
| 9 | themselves (and their future children) by gradually enforcing | ||
| 10 | tailored access control policies. A Landlock security policy is a | ||
| 11 | set of access rights (e.g. open a file in read-only, make a | ||
| 12 | directory, etc.) tied to a file hierarchy. Such policy can be | ||
| 13 | configured and enforced by any processes for themselves using the | ||
| 14 | dedicated system calls: landlock_create_ruleset(), | ||
| 15 | landlock_add_rule(), and landlock_restrict_self(). | ||
| 16 | |||
| 17 | See Documentation/userspace-api/landlock.rst for further information. | ||
| 18 | |||
| 19 | If you are unsure how to answer this question, answer N. Otherwise, | ||
| 20 | you should also prepend "landlock," to the content of CONFIG_LSM to | ||
| 21 | enable Landlock at boot time. | ||
| @@ -0,0 +1,4 @@ | |||
| 1 | obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o | ||
| 2 | |||
| 3 | landlock-y := setup.o syscalls.o object.o ruleset.o \ | ||
| 4 | cred.o ptrace.o fs.o | ||
| @@ -0,0 +1,20 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Common constants and helpers | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_COMMON_H | ||
| 10 | #define _SECURITY_LANDLOCK_COMMON_H | ||
| 11 | |||
| 12 | #define LANDLOCK_NAME "landlock" | ||
| 13 | |||
| 14 | #ifdef pr_fmt | ||
| 15 | #undef pr_fmt | ||
| 16 | #endif | ||
| 17 | |||
| 18 | #define pr_fmt(fmt) LANDLOCK_NAME ": " fmt | ||
| 19 | |||
| 20 | #endif /* _SECURITY_LANDLOCK_COMMON_H */ | ||
| @@ -0,0 +1,46 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Credential hooks | ||
| 4 | * | ||
| 5 | * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <linux/cred.h> | ||
| 10 | #include <linux/lsm_hooks.h> | ||
| 11 | |||
| 12 | #include "common.h" | ||
| 13 | #include "cred.h" | ||
| 14 | #include "ruleset.h" | ||
| 15 | #include "setup.h" | ||
| 16 | |||
| 17 | static int hook_cred_prepare(struct cred *const new, | ||
| 18 | const struct cred *const old, const gfp_t gfp) | ||
| 19 | { | ||
| 20 | struct landlock_ruleset *const old_dom = landlock_cred(old)->domain; | ||
| 21 | |||
| 22 | if (old_dom) { | ||
| 23 | landlock_get_ruleset(old_dom); | ||
| 24 | landlock_cred(new)->domain = old_dom; | ||
| 25 | } | ||
| 26 | return 0; | ||
| 27 | } | ||
| 28 | |||
| 29 | static void hook_cred_free(struct cred *const cred) | ||
| 30 | { | ||
| 31 | struct landlock_ruleset *const dom = landlock_cred(cred)->domain; | ||
| 32 | |||
| 33 | if (dom) | ||
| 34 | landlock_put_ruleset_deferred(dom); | ||
| 35 | } | ||
| 36 | |||
| 37 | static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { | ||
| 38 | LSM_HOOK_INIT(cred_prepare, hook_cred_prepare), | ||
| 39 | LSM_HOOK_INIT(cred_free, hook_cred_free), | ||
| 40 | }; | ||
| 41 | |||
| 42 | __init void landlock_add_cred_hooks(void) | ||
| 43 | { | ||
| 44 | security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks), | ||
| 45 | LANDLOCK_NAME); | ||
| 46 | } | ||
| @@ -0,0 +1,58 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Credential hooks | ||
| 4 | * | ||
| 5 | * Copyright © 2019-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2019-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_CRED_H | ||
| 10 | #define _SECURITY_LANDLOCK_CRED_H | ||
| 11 | |||
| 12 | #include <linux/cred.h> | ||
| 13 | #include <linux/init.h> | ||
| 14 | #include <linux/rcupdate.h> | ||
| 15 | |||
| 16 | #include "ruleset.h" | ||
| 17 | #include "setup.h" | ||
| 18 | |||
| 19 | struct landlock_cred_security { | ||
| 20 | struct landlock_ruleset *domain; | ||
| 21 | }; | ||
| 22 | |||
| 23 | static inline struct landlock_cred_security *landlock_cred( | ||
| 24 | const struct cred *cred) | ||
| 25 | { | ||
| 26 | return cred->security + landlock_blob_sizes.lbs_cred; | ||
| 27 | } | ||
| 28 | |||
| 29 | static inline const struct landlock_ruleset *landlock_get_current_domain(void) | ||
| 30 | { | ||
| 31 | return landlock_cred(current_cred())->domain; | ||
| 32 | } | ||
| 33 | |||
| 34 | /* | ||
| 35 | * The call needs to come from an RCU read-side critical section. | ||
| 36 | */ | ||
| 37 | static inline const struct landlock_ruleset *landlock_get_task_domain( | ||
| 38 | const struct task_struct *const task) | ||
| 39 | { | ||
| 40 | return landlock_cred(__task_cred(task))->domain; | ||
| 41 | } | ||
| 42 | |||
| 43 | static inline bool landlocked(const struct task_struct *const task) | ||
| 44 | { | ||
| 45 | bool has_dom; | ||
| 46 | |||
| 47 | if (task == current) | ||
| 48 | return !!landlock_get_current_domain(); | ||
| 49 | |||
| 50 | rcu_read_lock(); | ||
| 51 | has_dom = !!landlock_get_task_domain(task); | ||
| 52 | rcu_read_unlock(); | ||
| 53 | return has_dom; | ||
| 54 | } | ||
| 55 | |||
| 56 | __init void landlock_add_cred_hooks(void); | ||
| 57 | |||
| 58 | #endif /* _SECURITY_LANDLOCK_CRED_H */ | ||
| @@ -0,0 +1,692 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Filesystem management and hooks | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <linux/atomic.h> | ||
| 10 | #include <linux/bitops.h> | ||
| 11 | #include <linux/bits.h> | ||
| 12 | #include <linux/compiler_types.h> | ||
| 13 | #include <linux/dcache.h> | ||
| 14 | #include <linux/err.h> | ||
| 15 | #include <linux/fs.h> | ||
| 16 | #include <linux/init.h> | ||
| 17 | #include <linux/kernel.h> | ||
| 18 | #include <linux/limits.h> | ||
| 19 | #include <linux/list.h> | ||
| 20 | #include <linux/lsm_hooks.h> | ||
| 21 | #include <linux/mount.h> | ||
| 22 | #include <linux/namei.h> | ||
| 23 | #include <linux/path.h> | ||
| 24 | #include <linux/rcupdate.h> | ||
| 25 | #include <linux/spinlock.h> | ||
| 26 | #include <linux/stat.h> | ||
| 27 | #include <linux/types.h> | ||
| 28 | #include <linux/wait_bit.h> | ||
| 29 | #include <linux/workqueue.h> | ||
| 30 | #include <uapi/linux/landlock.h> | ||
| 31 | |||
| 32 | #include "common.h" | ||
| 33 | #include "cred.h" | ||
| 34 | #include "fs.h" | ||
| 35 | #include "limits.h" | ||
| 36 | #include "object.h" | ||
| 37 | #include "ruleset.h" | ||
| 38 | #include "setup.h" | ||
| 39 | |||
| 40 | /* Underlying object management */ | ||
| 41 | |||
| 42 | static void release_inode(struct landlock_object *const object) | ||
| 43 | __releases(object->lock) | ||
| 44 | { | ||
| 45 | struct inode *const inode = object->underobj; | ||
| 46 | struct super_block *sb; | ||
| 47 | |||
| 48 | if (!inode) { | ||
| 49 | spin_unlock(&object->lock); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | /* | ||
| 54 | * Protects against concurrent use by hook_sb_delete() of the reference | ||
| 55 | * to the underlying inode. | ||
| 56 | */ | ||
| 57 | object->underobj = NULL; | ||
| 58 | /* | ||
| 59 | * Makes sure that if the filesystem is concurrently unmounted, | ||
| 60 | * hook_sb_delete() will wait for us to finish iput(). | ||
| 61 | */ | ||
| 62 | sb = inode->i_sb; | ||
| 63 | atomic_long_inc(&landlock_superblock(sb)->inode_refs); | ||
| 64 | spin_unlock(&object->lock); | ||
| 65 | /* | ||
| 66 | * Because object->underobj was not NULL, hook_sb_delete() and | ||
| 67 | * get_inode_object() guarantee that it is safe to reset | ||
| 68 | * landlock_inode(inode)->object while it is not NULL. It is therefore | ||
| 69 | * not necessary to lock inode->i_lock. | ||
| 70 | */ | ||
| 71 | rcu_assign_pointer(landlock_inode(inode)->object, NULL); | ||
| 72 | /* | ||
| 73 | * Now, new rules can safely be tied to @inode with get_inode_object(). | ||
| 74 | */ | ||
| 75 | |||
| 76 | iput(inode); | ||
| 77 | if (atomic_long_dec_and_test(&landlock_superblock(sb)->inode_refs)) | ||
| 78 | wake_up_var(&landlock_superblock(sb)->inode_refs); | ||
| 79 | } | ||
| 80 | |||
| 81 | static const struct landlock_object_underops landlock_fs_underops = { | ||
| 82 | .release = release_inode | ||
| 83 | }; | ||
| 84 | |||
| 85 | /* Ruleset management */ | ||
| 86 | |||
| 87 | static struct landlock_object *get_inode_object(struct inode *const inode) | ||
| 88 | { | ||
| 89 | struct landlock_object *object, *new_object; | ||
| 90 | struct landlock_inode_security *inode_sec = landlock_inode(inode); | ||
| 91 | |||
| 92 | rcu_read_lock(); | ||
| 93 | retry: | ||
| 94 | object = rcu_dereference(inode_sec->object); | ||
| 95 | if (object) { | ||
| 96 | if (likely(refcount_inc_not_zero(&object->usage))) { | ||
| 97 | rcu_read_unlock(); | ||
| 98 | return object; | ||
| 99 | } | ||
| 100 | /* | ||
| 101 | * We are racing with release_inode(), the object is going | ||
| 102 | * away. Wait for release_inode(), then retry. | ||
| 103 | */ | ||
| 104 | spin_lock(&object->lock); | ||
| 105 | spin_unlock(&object->lock); | ||
| 106 | goto retry; | ||
| 107 | } | ||
| 108 | rcu_read_unlock(); | ||
| 109 | |||
| 110 | /* | ||
| 111 | * If there is no object tied to @inode, then create a new one (without | ||
| 112 | * holding any locks). | ||
| 113 | */ | ||
| 114 | new_object = landlock_create_object(&landlock_fs_underops, inode); | ||
| 115 | if (IS_ERR(new_object)) | ||
| 116 | return new_object; | ||
| 117 | |||
| 118 | /* | ||
| 119 | * Protects against concurrent calls to get_inode_object() or | ||
| 120 | * hook_sb_delete(). | ||
| 121 | */ | ||
| 122 | spin_lock(&inode->i_lock); | ||
| 123 | if (unlikely(rcu_access_pointer(inode_sec->object))) { | ||
| 124 | /* Someone else just created the object, bail out and retry. */ | ||
| 125 | spin_unlock(&inode->i_lock); | ||
| 126 | kfree(new_object); | ||
| 127 | |||
| 128 | rcu_read_lock(); | ||
| 129 | goto retry; | ||
| 130 | } | ||
| 131 | |||
| 132 | /* | ||
| 133 | * @inode will be released by hook_sb_delete() on its superblock | ||
| 134 | * shutdown, or by release_inode() when no more ruleset references the | ||
| 135 | * related object. | ||
| 136 | */ | ||
| 137 | ihold(inode); | ||
| 138 | rcu_assign_pointer(inode_sec->object, new_object); | ||
| 139 | spin_unlock(&inode->i_lock); | ||
| 140 | return new_object; | ||
| 141 | } | ||
| 142 | |||
| 143 | /* All access rights that can be tied to files. */ | ||
| 144 | #define ACCESS_FILE ( \ | ||
| 145 | LANDLOCK_ACCESS_FS_EXECUTE | \ | ||
| 146 | LANDLOCK_ACCESS_FS_WRITE_FILE | \ | ||
| 147 | LANDLOCK_ACCESS_FS_READ_FILE) | ||
| 148 | |||
| 149 | /* | ||
| 150 | * @path: Should have been checked by get_path_from_fd(). | ||
| 151 | */ | ||
| 152 | int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, | ||
| 153 | const struct path *const path, u32 access_rights) | ||
| 154 | { | ||
| 155 | int err; | ||
| 156 | struct landlock_object *object; | ||
| 157 | |||
| 158 | /* Files only get access rights that make sense. */ | ||
| 159 | if (!d_is_dir(path->dentry) && (access_rights | ACCESS_FILE) != | ||
| 160 | ACCESS_FILE) | ||
| 161 | return -EINVAL; | ||
| 162 | if (WARN_ON_ONCE(ruleset->num_layers != 1)) | ||
| 163 | return -EINVAL; | ||
| 164 | |||
| 165 | /* Transforms relative access rights to absolute ones. */ | ||
| 166 | access_rights |= LANDLOCK_MASK_ACCESS_FS & ~ruleset->fs_access_masks[0]; | ||
| 167 | object = get_inode_object(d_backing_inode(path->dentry)); | ||
| 168 | if (IS_ERR(object)) | ||
| 169 | return PTR_ERR(object); | ||
| 170 | mutex_lock(&ruleset->lock); | ||
| 171 | err = landlock_insert_rule(ruleset, object, access_rights); | ||
| 172 | mutex_unlock(&ruleset->lock); | ||
| 173 | /* | ||
| 174 | * No need to check for an error because landlock_insert_rule() | ||
| 175 | * increments the refcount for the new object if needed. | ||
| 176 | */ | ||
| 177 | landlock_put_object(object); | ||
| 178 | return err; | ||
| 179 | } | ||
| 180 | |||
| 181 | /* Access-control management */ | ||
| 182 | |||
| 183 | static inline u64 unmask_layers( | ||
| 184 | const struct landlock_ruleset *const domain, | ||
| 185 | const struct path *const path, const u32 access_request, | ||
| 186 | u64 layer_mask) | ||
| 187 | { | ||
| 188 | const struct landlock_rule *rule; | ||
| 189 | const struct inode *inode; | ||
| 190 | size_t i; | ||
| 191 | |||
| 192 | if (d_is_negative(path->dentry)) | ||
| 193 | /* Ignore nonexistent leafs. */ | ||
| 194 | return layer_mask; | ||
| 195 | inode = d_backing_inode(path->dentry); | ||
| 196 | rcu_read_lock(); | ||
| 197 | rule = landlock_find_rule(domain, | ||
| 198 | rcu_dereference(landlock_inode(inode)->object)); | ||
| 199 | rcu_read_unlock(); | ||
| 200 | if (!rule) | ||
| 201 | return layer_mask; | ||
| 202 | |||
| 203 | /* | ||
| 204 | * An access is granted if, for each policy layer, at least one rule | ||
| 205 | * encountered on the pathwalk grants the requested accesses, | ||
| 206 | * regardless of their position in the layer stack. We must then check | ||
| 207 | * the remaining layers for each inode, from the first added layer to | ||
| 208 | * the last one. | ||
| 209 | */ | ||
| 210 | for (i = 0; i < rule->num_layers; i++) { | ||
| 211 | const struct landlock_layer *const layer = &rule->layers[i]; | ||
| 212 | const u64 layer_level = BIT_ULL(layer->level - 1); | ||
| 213 | |||
| 214 | /* Checks that the layer grants access to the full request. */ | ||
| 215 | if ((layer->access & access_request) == access_request) { | ||
| 216 | layer_mask &= ~layer_level; | ||
| 217 | |||
| 218 | if (layer_mask == 0) | ||
| 219 | return layer_mask; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | return layer_mask; | ||
| 223 | } | ||
| 224 | |||
| 225 | static int check_access_path(const struct landlock_ruleset *const domain, | ||
| 226 | const struct path *const path, u32 access_request) | ||
| 227 | { | ||
| 228 | bool allowed = false; | ||
| 229 | struct path walker_path; | ||
| 230 | u64 layer_mask; | ||
| 231 | size_t i; | ||
| 232 | |||
| 233 | /* Make sure all layers can be checked. */ | ||
| 234 | BUILD_BUG_ON(BITS_PER_TYPE(layer_mask) < LANDLOCK_MAX_NUM_LAYERS); | ||
| 235 | |||
| 236 | if (!access_request) | ||
| 237 | return 0; | ||
| 238 | if (WARN_ON_ONCE(!domain || !path)) | ||
| 239 | return 0; | ||
| 240 | /* | ||
| 241 | * Allows access to pseudo filesystems that will never be mountable | ||
| 242 | * (e.g. sockfs, pipefs), but can still be reachable through | ||
| 243 | * /proc/<pid>/fd/<file-descriptor> . | ||
| 244 | */ | ||
| 245 | if ((path->dentry->d_sb->s_flags & SB_NOUSER) || | ||
| 246 | (d_is_positive(path->dentry) && | ||
| 247 | unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))) | ||
| 248 | return 0; | ||
| 249 | if (WARN_ON_ONCE(domain->num_layers < 1)) | ||
| 250 | return -EACCES; | ||
| 251 | |||
| 252 | /* Saves all layers handling a subset of requested accesses. */ | ||
| 253 | layer_mask = 0; | ||
| 254 | for (i = 0; i < domain->num_layers; i++) { | ||
| 255 | if (domain->fs_access_masks[i] & access_request) | ||
| 256 | layer_mask |= BIT_ULL(i); | ||
| 257 | } | ||
| 258 | /* An access request not handled by the domain is allowed. */ | ||
| 259 | if (layer_mask == 0) | ||
| 260 | return 0; | ||
| 261 | |||
| 262 | walker_path = *path; | ||
| 263 | path_get(&walker_path); | ||
| 264 | /* | ||
| 265 | * We need to walk through all the hierarchy to not miss any relevant | ||
| 266 | * restriction. | ||
| 267 | */ | ||
| 268 | while (true) { | ||
| 269 | struct dentry *parent_dentry; | ||
| 270 | |||
| 271 | layer_mask = unmask_layers(domain, &walker_path, | ||
| 272 | access_request, layer_mask); | ||
| 273 | if (layer_mask == 0) { | ||
| 274 | /* Stops when a rule from each layer grants access. */ | ||
| 275 | allowed = true; | ||
| 276 | break; | ||
| 277 | } | ||
| 278 | |||
| 279 | jump_up: | ||
| 280 | if (walker_path.dentry == walker_path.mnt->mnt_root) { | ||
| 281 | if (follow_up(&walker_path)) { | ||
| 282 | /* Ignores hidden mount points. */ | ||
| 283 | goto jump_up; | ||
| 284 | } else { | ||
| 285 | /* | ||
| 286 | * Stops at the real root. Denies access | ||
| 287 | * because not all layers have granted access. | ||
| 288 | */ | ||
| 289 | allowed = false; | ||
| 290 | break; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | if (unlikely(IS_ROOT(walker_path.dentry))) { | ||
| 294 | /* | ||
| 295 | * Stops at disconnected root directories. Only allows | ||
| 296 | * access to internal filesystems (e.g. nsfs, which is | ||
| 297 | * reachable through /proc/<pid>/ns/<namespace>). | ||
| 298 | */ | ||
| 299 | allowed = !!(walker_path.mnt->mnt_flags & MNT_INTERNAL); | ||
| 300 | break; | ||
| 301 | } | ||
| 302 | parent_dentry = dget_parent(walker_path.dentry); | ||
| 303 | dput(walker_path.dentry); | ||
| 304 | walker_path.dentry = parent_dentry; | ||
| 305 | } | ||
| 306 | path_put(&walker_path); | ||
| 307 | return allowed ? 0 : -EACCES; | ||
| 308 | } | ||
| 309 | |||
| 310 | static inline int current_check_access_path(const struct path *const path, | ||
| 311 | const u32 access_request) | ||
| 312 | { | ||
| 313 | const struct landlock_ruleset *const dom = | ||
| 314 | landlock_get_current_domain(); | ||
| 315 | |||
| 316 | if (!dom) | ||
| 317 | return 0; | ||
| 318 | return check_access_path(dom, path, access_request); | ||
| 319 | } | ||
| 320 | |||
| 321 | /* Inode hooks */ | ||
| 322 | |||
| 323 | static void hook_inode_free_security(struct inode *const inode) | ||
| 324 | { | ||
| 325 | /* | ||
| 326 | * All inodes must already have been untied from their object by | ||
| 327 | * release_inode() or hook_sb_delete(). | ||
| 328 | */ | ||
| 329 | WARN_ON_ONCE(landlock_inode(inode)->object); | ||
| 330 | } | ||
| 331 | |||
| 332 | /* Super-block hooks */ | ||
| 333 | |||
| 334 | /* | ||
| 335 | * Release the inodes used in a security policy. | ||
| 336 | * | ||
| 337 | * Cf. fsnotify_unmount_inodes() and invalidate_inodes() | ||
| 338 | */ | ||
| 339 | static void hook_sb_delete(struct super_block *const sb) | ||
| 340 | { | ||
| 341 | struct inode *inode, *prev_inode = NULL; | ||
| 342 | |||
| 343 | if (!landlock_initialized) | ||
| 344 | return; | ||
| 345 | |||
| 346 | spin_lock(&sb->s_inode_list_lock); | ||
| 347 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { | ||
| 348 | struct landlock_object *object; | ||
| 349 | |||
| 350 | /* Only handles referenced inodes. */ | ||
| 351 | if (!atomic_read(&inode->i_count)) | ||
| 352 | continue; | ||
| 353 | |||
| 354 | /* | ||
| 355 | * Protects against concurrent modification of inode (e.g. | ||
| 356 | * from get_inode_object()). | ||
| 357 | */ | ||
| 358 | spin_lock(&inode->i_lock); | ||
| 359 | /* | ||
| 360 | * Checks I_FREEING and I_WILL_FREE to protect against a race | ||
| 361 | * condition when release_inode() just called iput(), which | ||
| 362 | * could lead to a NULL dereference of inode->security or a | ||
| 363 | * second call to iput() for the same Landlock object. Also | ||
| 364 | * checks I_NEW because such inode cannot be tied to an object. | ||
| 365 | */ | ||
| 366 | if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) { | ||
| 367 | spin_unlock(&inode->i_lock); | ||
| 368 | continue; | ||
| 369 | } | ||
| 370 | |||
| 371 | rcu_read_lock(); | ||
| 372 | object = rcu_dereference(landlock_inode(inode)->object); | ||
| 373 | if (!object) { | ||
| 374 | rcu_read_unlock(); | ||
| 375 | spin_unlock(&inode->i_lock); | ||
| 376 | continue; | ||
| 377 | } | ||
| 378 | /* Keeps a reference to this inode until the next loop walk. */ | ||
| 379 | __iget(inode); | ||
| 380 | spin_unlock(&inode->i_lock); | ||
| 381 | |||
| 382 | /* | ||
| 383 | * If there is no concurrent release_inode() ongoing, then we | ||
| 384 | * are in charge of calling iput() on this inode, otherwise we | ||
| 385 | * will just wait for it to finish. | ||
| 386 | */ | ||
| 387 | spin_lock(&object->lock); | ||
| 388 | if (object->underobj == inode) { | ||
| 389 | object->underobj = NULL; | ||
| 390 | spin_unlock(&object->lock); | ||
| 391 | rcu_read_unlock(); | ||
| 392 | |||
| 393 | /* | ||
| 394 | * Because object->underobj was not NULL, | ||
| 395 | * release_inode() and get_inode_object() guarantee | ||
| 396 | * that it is safe to reset | ||
| 397 | * landlock_inode(inode)->object while it is not NULL. | ||
| 398 | * It is therefore not necessary to lock inode->i_lock. | ||
| 399 | */ | ||
| 400 | rcu_assign_pointer(landlock_inode(inode)->object, NULL); | ||
| 401 | /* | ||
| 402 | * At this point, we own the ihold() reference that was | ||
| 403 | * originally set up by get_inode_object() and the | ||
| 404 | * __iget() reference that we just set in this loop | ||
| 405 | * walk. Therefore the following call to iput() will | ||
| 406 | * not sleep nor drop the inode because there is now at | ||
| 407 | * least two references to it. | ||
| 408 | */ | ||
| 409 | iput(inode); | ||
| 410 | } else { | ||
| 411 | spin_unlock(&object->lock); | ||
| 412 | rcu_read_unlock(); | ||
| 413 | } | ||
| 414 | |||
| 415 | if (prev_inode) { | ||
| 416 | /* | ||
| 417 | * At this point, we still own the __iget() reference | ||
| 418 | * that we just set in this loop walk. Therefore we | ||
| 419 | * can drop the list lock and know that the inode won't | ||
| 420 | * disappear from under us until the next loop walk. | ||
| 421 | */ | ||
| 422 | spin_unlock(&sb->s_inode_list_lock); | ||
| 423 | /* | ||
| 424 | * We can now actually put the inode reference from the | ||
| 425 | * previous loop walk, which is not needed anymore. | ||
| 426 | */ | ||
| 427 | iput(prev_inode); | ||
| 428 | cond_resched(); | ||
| 429 | spin_lock(&sb->s_inode_list_lock); | ||
| 430 | } | ||
| 431 | prev_inode = inode; | ||
| 432 | } | ||
| 433 | spin_unlock(&sb->s_inode_list_lock); | ||
| 434 | |||
| 435 | /* Puts the inode reference from the last loop walk, if any. */ | ||
| 436 | if (prev_inode) | ||
| 437 | iput(prev_inode); | ||
| 438 | /* Waits for pending iput() in release_inode(). */ | ||
| 439 | wait_var_event(&landlock_superblock(sb)->inode_refs, !atomic_long_read( | ||
| 440 | &landlock_superblock(sb)->inode_refs)); | ||
| 441 | } | ||
| 442 | |||
| 443 | /* | ||
| 444 | * Because a Landlock security policy is defined according to the filesystem | ||
| 445 | * topology (i.e. the mount namespace), changing it may grant access to files | ||
| 446 | * not previously allowed. | ||
| 447 | * | ||
| 448 | * To make it simple, deny any filesystem topology modification by landlocked | ||
| 449 | * processes. Non-landlocked processes may still change the namespace of a | ||
| 450 | * landlocked process, but this kind of threat must be handled by a system-wide | ||
| 451 | * access-control security policy. | ||
| 452 | * | ||
| 453 | * This could be lifted in the future if Landlock can safely handle mount | ||
| 454 | * namespace updates requested by a landlocked process. Indeed, we could | ||
| 455 | * update the current domain (which is currently read-only) by taking into | ||
| 456 | * account the accesses of the source and the destination of a new mount point. | ||
| 457 | * However, it would also require to make all the child domains dynamically | ||
| 458 | * inherit these new constraints. Anyway, for backward compatibility reasons, | ||
| 459 | * a dedicated user space option would be required (e.g. as a ruleset flag). | ||
| 460 | */ | ||
| 461 | static int hook_sb_mount(const char *const dev_name, | ||
| 462 | const struct path *const path, const char *const type, | ||
| 463 | const unsigned long flags, void *const data) | ||
| 464 | { | ||
| 465 | if (!landlock_get_current_domain()) | ||
| 466 | return 0; | ||
| 467 | return -EPERM; | ||
| 468 | } | ||
| 469 | |||
| 470 | static int hook_move_mount(const struct path *const from_path, | ||
| 471 | const struct path *const to_path) | ||
| 472 | { | ||
| 473 | if (!landlock_get_current_domain()) | ||
| 474 | return 0; | ||
| 475 | return -EPERM; | ||
| 476 | } | ||
| 477 | |||
| 478 | /* | ||
| 479 | * Removing a mount point may reveal a previously hidden file hierarchy, which | ||
| 480 | * may then grant access to files, which may have previously been forbidden. | ||
| 481 | */ | ||
| 482 | static int hook_sb_umount(struct vfsmount *const mnt, const int flags) | ||
| 483 | { | ||
| 484 | if (!landlock_get_current_domain()) | ||
| 485 | return 0; | ||
| 486 | return -EPERM; | ||
| 487 | } | ||
| 488 | |||
| 489 | static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts) | ||
| 490 | { | ||
| 491 | if (!landlock_get_current_domain()) | ||
| 492 | return 0; | ||
| 493 | return -EPERM; | ||
| 494 | } | ||
| 495 | |||
| 496 | /* | ||
| 497 | * pivot_root(2), like mount(2), changes the current mount namespace. It must | ||
| 498 | * then be forbidden for a landlocked process. | ||
| 499 | * | ||
| 500 | * However, chroot(2) may be allowed because it only changes the relative root | ||
| 501 | * directory of the current process. Moreover, it can be used to restrict the | ||
| 502 | * view of the filesystem. | ||
| 503 | */ | ||
| 504 | static int hook_sb_pivotroot(const struct path *const old_path, | ||
| 505 | const struct path *const new_path) | ||
| 506 | { | ||
| 507 | if (!landlock_get_current_domain()) | ||
| 508 | return 0; | ||
| 509 | return -EPERM; | ||
| 510 | } | ||
| 511 | |||
| 512 | /* Path hooks */ | ||
| 513 | |||
| 514 | static inline u32 get_mode_access(const umode_t mode) | ||
| 515 | { | ||
| 516 | switch (mode & S_IFMT) { | ||
| 517 | case S_IFLNK: | ||
| 518 | return LANDLOCK_ACCESS_FS_MAKE_SYM; | ||
| 519 | case 0: | ||
| 520 | /* A zero mode translates to S_IFREG. */ | ||
| 521 | case S_IFREG: | ||
| 522 | return LANDLOCK_ACCESS_FS_MAKE_REG; | ||
| 523 | case S_IFDIR: | ||
| 524 | return LANDLOCK_ACCESS_FS_MAKE_DIR; | ||
| 525 | case S_IFCHR: | ||
| 526 | return LANDLOCK_ACCESS_FS_MAKE_CHAR; | ||
| 527 | case S_IFBLK: | ||
| 528 | return LANDLOCK_ACCESS_FS_MAKE_BLOCK; | ||
| 529 | case S_IFIFO: | ||
| 530 | return LANDLOCK_ACCESS_FS_MAKE_FIFO; | ||
| 531 | case S_IFSOCK: | ||
| 532 | return LANDLOCK_ACCESS_FS_MAKE_SOCK; | ||
| 533 | default: | ||
| 534 | WARN_ON_ONCE(1); | ||
| 535 | return 0; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | /* | ||
| 540 | * Creating multiple links or renaming may lead to privilege escalations if not | ||
| 541 | * handled properly. Indeed, we must be sure that the source doesn't gain more | ||
| 542 | * privileges by being accessible from the destination. This is getting more | ||
| 543 | * complex when dealing with multiple layers. The whole picture can be seen as | ||
| 544 | * a multilayer partial ordering problem. A future version of Landlock will | ||
| 545 | * deal with that. | ||
| 546 | */ | ||
| 547 | static int hook_path_link(struct dentry *const old_dentry, | ||
| 548 | const struct path *const new_dir, | ||
| 549 | struct dentry *const new_dentry) | ||
| 550 | { | ||
| 551 | const struct landlock_ruleset *const dom = | ||
| 552 | landlock_get_current_domain(); | ||
| 553 | |||
| 554 | if (!dom) | ||
| 555 | return 0; | ||
| 556 | /* The mount points are the same for old and new paths, cf. EXDEV. */ | ||
| 557 | if (old_dentry->d_parent != new_dir->dentry) | ||
| 558 | /* Gracefully forbids reparenting. */ | ||
| 559 | return -EXDEV; | ||
| 560 | if (unlikely(d_is_negative(old_dentry))) | ||
| 561 | return -ENOENT; | ||
| 562 | return check_access_path(dom, new_dir, | ||
| 563 | get_mode_access(d_backing_inode(old_dentry)->i_mode)); | ||
| 564 | } | ||
| 565 | |||
| 566 | static inline u32 maybe_remove(const struct dentry *const dentry) | ||
| 567 | { | ||
| 568 | if (d_is_negative(dentry)) | ||
| 569 | return 0; | ||
| 570 | return d_is_dir(dentry) ? LANDLOCK_ACCESS_FS_REMOVE_DIR : | ||
| 571 | LANDLOCK_ACCESS_FS_REMOVE_FILE; | ||
| 572 | } | ||
| 573 | |||
| 574 | static int hook_path_rename(const struct path *const old_dir, | ||
| 575 | struct dentry *const old_dentry, | ||
| 576 | const struct path *const new_dir, | ||
| 577 | struct dentry *const new_dentry) | ||
| 578 | { | ||
| 579 | const struct landlock_ruleset *const dom = | ||
| 580 | landlock_get_current_domain(); | ||
| 581 | |||
| 582 | if (!dom) | ||
| 583 | return 0; | ||
| 584 | /* The mount points are the same for old and new paths, cf. EXDEV. */ | ||
| 585 | if (old_dir->dentry != new_dir->dentry) | ||
| 586 | /* Gracefully forbids reparenting. */ | ||
| 587 | return -EXDEV; | ||
| 588 | if (unlikely(d_is_negative(old_dentry))) | ||
| 589 | return -ENOENT; | ||
| 590 | /* RENAME_EXCHANGE is handled because directories are the same. */ | ||
| 591 | return check_access_path(dom, old_dir, maybe_remove(old_dentry) | | ||
| 592 | maybe_remove(new_dentry) | | ||
| 593 | get_mode_access(d_backing_inode(old_dentry)->i_mode)); | ||
| 594 | } | ||
| 595 | |||
| 596 | static int hook_path_mkdir(const struct path *const dir, | ||
| 597 | struct dentry *const dentry, const umode_t mode) | ||
| 598 | { | ||
| 599 | return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_DIR); | ||
| 600 | } | ||
| 601 | |||
| 602 | static int hook_path_mknod(const struct path *const dir, | ||
| 603 | struct dentry *const dentry, const umode_t mode, | ||
| 604 | const unsigned int dev) | ||
| 605 | { | ||
| 606 | const struct landlock_ruleset *const dom = | ||
| 607 | landlock_get_current_domain(); | ||
| 608 | |||
| 609 | if (!dom) | ||
| 610 | return 0; | ||
| 611 | return check_access_path(dom, dir, get_mode_access(mode)); | ||
| 612 | } | ||
| 613 | |||
| 614 | static int hook_path_symlink(const struct path *const dir, | ||
| 615 | struct dentry *const dentry, const char *const old_name) | ||
| 616 | { | ||
| 617 | return current_check_access_path(dir, LANDLOCK_ACCESS_FS_MAKE_SYM); | ||
| 618 | } | ||
| 619 | |||
| 620 | static int hook_path_unlink(const struct path *const dir, | ||
| 621 | struct dentry *const dentry) | ||
| 622 | { | ||
| 623 | return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_FILE); | ||
| 624 | } | ||
| 625 | |||
| 626 | static int hook_path_rmdir(const struct path *const dir, | ||
| 627 | struct dentry *const dentry) | ||
| 628 | { | ||
| 629 | return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR); | ||
| 630 | } | ||
| 631 | |||
| 632 | /* File hooks */ | ||
| 633 | |||
| 634 | static inline u32 get_file_access(const struct file *const file) | ||
| 635 | { | ||
| 636 | u32 access = 0; | ||
| 637 | |||
| 638 | if (file->f_mode & FMODE_READ) { | ||
| 639 | /* A directory can only be opened in read mode. */ | ||
| 640 | if (S_ISDIR(file_inode(file)->i_mode)) | ||
| 641 | return LANDLOCK_ACCESS_FS_READ_DIR; | ||
| 642 | access = LANDLOCK_ACCESS_FS_READ_FILE; | ||
| 643 | } | ||
| 644 | if (file->f_mode & FMODE_WRITE) | ||
| 645 | access |= LANDLOCK_ACCESS_FS_WRITE_FILE; | ||
| 646 | /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */ | ||
| 647 | if (file->f_flags & __FMODE_EXEC) | ||
| 648 | access |= LANDLOCK_ACCESS_FS_EXECUTE; | ||
| 649 | return access; | ||
| 650 | } | ||
| 651 | |||
| 652 | static int hook_file_open(struct file *const file) | ||
| 653 | { | ||
| 654 | const struct landlock_ruleset *const dom = | ||
| 655 | landlock_get_current_domain(); | ||
| 656 | |||
| 657 | if (!dom) | ||
| 658 | return 0; | ||
| 659 | /* | ||
| 660 | * Because a file may be opened with O_PATH, get_file_access() may | ||
| 661 | * return 0. This case will be handled with a future Landlock | ||
| 662 | * evolution. | ||
| 663 | */ | ||
| 664 | return check_access_path(dom, &file->f_path, get_file_access(file)); | ||
| 665 | } | ||
| 666 | |||
| 667 | static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { | ||
| 668 | LSM_HOOK_INIT(inode_free_security, hook_inode_free_security), | ||
| 669 | |||
| 670 | LSM_HOOK_INIT(sb_delete, hook_sb_delete), | ||
| 671 | LSM_HOOK_INIT(sb_mount, hook_sb_mount), | ||
| 672 | LSM_HOOK_INIT(move_mount, hook_move_mount), | ||
| 673 | LSM_HOOK_INIT(sb_umount, hook_sb_umount), | ||
| 674 | LSM_HOOK_INIT(sb_remount, hook_sb_remount), | ||
| 675 | LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot), | ||
| 676 | |||
| 677 | LSM_HOOK_INIT(path_link, hook_path_link), | ||
| 678 | LSM_HOOK_INIT(path_rename, hook_path_rename), | ||
| 679 | LSM_HOOK_INIT(path_mkdir, hook_path_mkdir), | ||
| 680 | LSM_HOOK_INIT(path_mknod, hook_path_mknod), | ||
| 681 | LSM_HOOK_INIT(path_symlink, hook_path_symlink), | ||
| 682 | LSM_HOOK_INIT(path_unlink, hook_path_unlink), | ||
| 683 | LSM_HOOK_INIT(path_rmdir, hook_path_rmdir), | ||
| 684 | |||
| 685 | LSM_HOOK_INIT(file_open, hook_file_open), | ||
| 686 | }; | ||
| 687 | |||
| 688 | __init void landlock_add_fs_hooks(void) | ||
| 689 | { | ||
| 690 | security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks), | ||
| 691 | LANDLOCK_NAME); | ||
| 692 | } | ||
| @@ -0,0 +1,70 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Filesystem management and hooks | ||
| 4 | * | ||
| 5 | * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_FS_H | ||
| 10 | #define _SECURITY_LANDLOCK_FS_H | ||
| 11 | |||
| 12 | #include <linux/fs.h> | ||
| 13 | #include <linux/init.h> | ||
| 14 | #include <linux/rcupdate.h> | ||
| 15 | |||
| 16 | #include "ruleset.h" | ||
| 17 | #include "setup.h" | ||
| 18 | |||
| 19 | /** | ||
| 20 | * struct landlock_inode_security - Inode security blob | ||
| 21 | * | ||
| 22 | * Enable to reference a &struct landlock_object tied to an inode (i.e. | ||
| 23 | * underlying object). | ||
| 24 | */ | ||
| 25 | struct landlock_inode_security { | ||
| 26 | /** | ||
| 27 | * @object: Weak pointer to an allocated object. All assignments of a | ||
| 28 | * new object are protected by the underlying inode->i_lock. However, | ||
| 29 | * atomically disassociating @object from the inode is only protected | ||
| 30 | * by @object->lock, from the time @object's usage refcount drops to | ||
| 31 | * zero to the time this pointer is nulled out (cf. release_inode() and | ||
| 32 | * hook_sb_delete()). Indeed, such disassociation doesn't require | ||
| 33 | * inode->i_lock thanks to the careful rcu_access_pointer() check | ||
| 34 | * performed by get_inode_object(). | ||
| 35 | */ | ||
| 36 | struct landlock_object __rcu *object; | ||
| 37 | }; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * struct landlock_superblock_security - Superblock security blob | ||
| 41 | * | ||
| 42 | * Enable hook_sb_delete() to wait for concurrent calls to release_inode(). | ||
| 43 | */ | ||
| 44 | struct landlock_superblock_security { | ||
| 45 | /** | ||
| 46 | * @inode_refs: Number of pending inodes (from this superblock) that | ||
| 47 | * are being released by release_inode(). | ||
| 48 | * Cf. struct super_block->s_fsnotify_inode_refs . | ||
| 49 | */ | ||
| 50 | atomic_long_t inode_refs; | ||
| 51 | }; | ||
| 52 | |||
| 53 | static inline struct landlock_inode_security *landlock_inode( | ||
| 54 | const struct inode *const inode) | ||
| 55 | { | ||
| 56 | return inode->i_security + landlock_blob_sizes.lbs_inode; | ||
| 57 | } | ||
| 58 | |||
| 59 | static inline struct landlock_superblock_security *landlock_superblock( | ||
| 60 | const struct super_block *const superblock) | ||
| 61 | { | ||
| 62 | return superblock->s_security + landlock_blob_sizes.lbs_superblock; | ||
| 63 | } | ||
| 64 | |||
| 65 | __init void landlock_add_fs_hooks(void); | ||
| 66 | |||
| 67 | int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, | ||
| 68 | const struct path *const path, u32 access_hierarchy); | ||
| 69 | |||
| 70 | #endif /* _SECURITY_LANDLOCK_FS_H */ | ||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Limits for different components | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_LIMITS_H | ||
| 10 | #define _SECURITY_LANDLOCK_LIMITS_H | ||
| 11 | |||
| 12 | #include <linux/limits.h> | ||
| 13 | #include <uapi/linux/landlock.h> | ||
| 14 | |||
| 15 | #define LANDLOCK_MAX_NUM_LAYERS 64 | ||
| 16 | #define LANDLOCK_MAX_NUM_RULES U32_MAX | ||
| 17 | |||
| 18 | #define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_MAKE_SYM | ||
| 19 | #define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1) | ||
| 20 | |||
| 21 | #endif /* _SECURITY_LANDLOCK_LIMITS_H */ | ||
| @@ -0,0 +1,67 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Object management | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <linux/bug.h> | ||
| 10 | #include <linux/compiler_types.h> | ||
| 11 | #include <linux/err.h> | ||
| 12 | #include <linux/kernel.h> | ||
| 13 | #include <linux/rcupdate.h> | ||
| 14 | #include <linux/refcount.h> | ||
| 15 | #include <linux/slab.h> | ||
| 16 | #include <linux/spinlock.h> | ||
| 17 | |||
| 18 | #include "object.h" | ||
| 19 | |||
| 20 | struct landlock_object *landlock_create_object( | ||
| 21 | const struct landlock_object_underops *const underops, | ||
| 22 | void *const underobj) | ||
| 23 | { | ||
| 24 | struct landlock_object *new_object; | ||
| 25 | |||
| 26 | if (WARN_ON_ONCE(!underops || !underobj)) | ||
| 27 | return ERR_PTR(-ENOENT); | ||
| 28 | new_object = kzalloc(sizeof(*new_object), GFP_KERNEL_ACCOUNT); | ||
| 29 | if (!new_object) | ||
| 30 | return ERR_PTR(-ENOMEM); | ||
| 31 | refcount_set(&new_object->usage, 1); | ||
| 32 | spin_lock_init(&new_object->lock); | ||
| 33 | new_object->underops = underops; | ||
| 34 | new_object->underobj = underobj; | ||
| 35 | return new_object; | ||
| 36 | } | ||
| 37 | |||
| 38 | /* | ||
| 39 | * The caller must own the object (i.e. thanks to object->usage) to safely put | ||
| 40 | * it. | ||
| 41 | */ | ||
| 42 | void landlock_put_object(struct landlock_object *const object) | ||
| 43 | { | ||
| 44 | /* | ||
| 45 | * The call to @object->underops->release(object) might sleep, e.g. | ||
| 46 | * because of iput(). | ||
| 47 | */ | ||
| 48 | might_sleep(); | ||
| 49 | if (!object) | ||
| 50 | return; | ||
| 51 | |||
| 52 | /* | ||
| 53 | * If the @object's refcount cannot drop to zero, we can just decrement | ||
| 54 | * the refcount without holding a lock. Otherwise, the decrement must | ||
| 55 | * happen under @object->lock for synchronization with things like | ||
| 56 | * get_inode_object(). | ||
| 57 | */ | ||
| 58 | if (refcount_dec_and_lock(&object->usage, &object->lock)) { | ||
| 59 | __acquire(&object->lock); | ||
| 60 | /* | ||
| 61 | * With @object->lock initially held, remove the reference from | ||
| 62 | * @object->underobj to @object (if it still exists). | ||
| 63 | */ | ||
| 64 | object->underops->release(object); | ||
| 65 | kfree_rcu(object, rcu_free); | ||
| 66 | } | ||
| 67 | } | ||
| @@ -0,0 +1,91 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Object management | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_OBJECT_H | ||
| 10 | #define _SECURITY_LANDLOCK_OBJECT_H | ||
| 11 | |||
| 12 | #include <linux/compiler_types.h> | ||
| 13 | #include <linux/refcount.h> | ||
| 14 | #include <linux/spinlock.h> | ||
| 15 | |||
| 16 | struct landlock_object; | ||
| 17 | |||
| 18 | /** | ||
| 19 | * struct landlock_object_underops - Operations on an underlying object | ||
| 20 | */ | ||
| 21 | struct landlock_object_underops { | ||
| 22 | /** | ||
| 23 | * @release: Releases the underlying object (e.g. iput() for an inode). | ||
| 24 | */ | ||
| 25 | void (*release)(struct landlock_object *const object) | ||
| 26 | __releases(object->lock); | ||
| 27 | }; | ||
| 28 | |||
| 29 | /** | ||
| 30 | * struct landlock_object - Security blob tied to a kernel object | ||
| 31 | * | ||
| 32 | * The goal of this structure is to enable to tie a set of ephemeral access | ||
| 33 | * rights (pertaining to different domains) to a kernel object (e.g an inode) | ||
| 34 | * in a safe way. This implies to handle concurrent use and modification. | ||
| 35 | * | ||
| 36 | * The lifetime of a &struct landlock_object depends on the rules referring to | ||
| 37 | * it. | ||
| 38 | */ | ||
| 39 | struct landlock_object { | ||
| 40 | /** | ||
| 41 | * @usage: This counter is used to tie an object to the rules matching | ||
| 42 | * it or to keep it alive while adding a new rule. If this counter | ||
| 43 | * reaches zero, this struct must not be modified, but this counter can | ||
| 44 | * still be read from within an RCU read-side critical section. When | ||
| 45 | * adding a new rule to an object with a usage counter of zero, we must | ||
| 46 | * wait until the pointer to this object is set to NULL (or recycled). | ||
| 47 | */ | ||
| 48 | refcount_t usage; | ||
| 49 | /** | ||
| 50 | * @lock: Protects against concurrent modifications. This lock must be | ||
| 51 | * held from the time @usage drops to zero until any weak references | ||
| 52 | * from @underobj to this object have been cleaned up. | ||
| 53 | * | ||
| 54 | * Lock ordering: inode->i_lock nests inside this. | ||
| 55 | */ | ||
| 56 | spinlock_t lock; | ||
| 57 | /** | ||
| 58 | * @underobj: Used when cleaning up an object and to mark an object as | ||
| 59 | * tied to its underlying kernel structure. This pointer is protected | ||
| 60 | * by @lock. Cf. landlock_release_inodes() and release_inode(). | ||
| 61 | */ | ||
| 62 | void *underobj; | ||
| 63 | union { | ||
| 64 | /** | ||
| 65 | * @rcu_free: Enables lockless use of @usage, @lock and | ||
| 66 | * @underobj from within an RCU read-side critical section. | ||
| 67 | * @rcu_free and @underops are only used by | ||
| 68 | * landlock_put_object(). | ||
| 69 | */ | ||
| 70 | struct rcu_head rcu_free; | ||
| 71 | /** | ||
| 72 | * @underops: Enables landlock_put_object() to release the | ||
| 73 | * underlying object (e.g. inode). | ||
| 74 | */ | ||
| 75 | const struct landlock_object_underops *underops; | ||
| 76 | }; | ||
| 77 | }; | ||
| 78 | |||
| 79 | struct landlock_object *landlock_create_object( | ||
| 80 | const struct landlock_object_underops *const underops, | ||
| 81 | void *const underobj); | ||
| 82 | |||
| 83 | void landlock_put_object(struct landlock_object *const object); | ||
| 84 | |||
| 85 | static inline void landlock_get_object(struct landlock_object *const object) | ||
| 86 | { | ||
| 87 | if (object) | ||
| 88 | refcount_inc(&object->usage); | ||
| 89 | } | ||
| 90 | |||
| 91 | #endif /* _SECURITY_LANDLOCK_OBJECT_H */ | ||
| @@ -0,0 +1,120 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Ptrace hooks | ||
| 4 | * | ||
| 5 | * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2019-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <asm/current.h> | ||
| 10 | #include <linux/cred.h> | ||
| 11 | #include <linux/errno.h> | ||
| 12 | #include <linux/kernel.h> | ||
| 13 | #include <linux/lsm_hooks.h> | ||
| 14 | #include <linux/rcupdate.h> | ||
| 15 | #include <linux/sched.h> | ||
| 16 | |||
| 17 | #include "common.h" | ||
| 18 | #include "cred.h" | ||
| 19 | #include "ptrace.h" | ||
| 20 | #include "ruleset.h" | ||
| 21 | #include "setup.h" | ||
| 22 | |||
| 23 | /** | ||
| 24 | * domain_scope_le - Checks domain ordering for scoped ptrace | ||
| 25 | * | ||
| 26 | * @parent: Parent domain. | ||
| 27 | * @child: Potential child of @parent. | ||
| 28 | * | ||
| 29 | * Checks if the @parent domain is less or equal to (i.e. an ancestor, which | ||
| 30 | * means a subset of) the @child domain. | ||
| 31 | */ | ||
| 32 | static bool domain_scope_le(const struct landlock_ruleset *const parent, | ||
| 33 | const struct landlock_ruleset *const child) | ||
| 34 | { | ||
| 35 | const struct landlock_hierarchy *walker; | ||
| 36 | |||
| 37 | if (!parent) | ||
| 38 | return true; | ||
| 39 | if (!child) | ||
| 40 | return false; | ||
| 41 | for (walker = child->hierarchy; walker; walker = walker->parent) { | ||
| 42 | if (walker == parent->hierarchy) | ||
| 43 | /* @parent is in the scoped hierarchy of @child. */ | ||
| 44 | return true; | ||
| 45 | } | ||
| 46 | /* There is no relationship between @parent and @child. */ | ||
| 47 | return false; | ||
| 48 | } | ||
| 49 | |||
| 50 | static bool task_is_scoped(const struct task_struct *const parent, | ||
| 51 | const struct task_struct *const child) | ||
| 52 | { | ||
| 53 | bool is_scoped; | ||
| 54 | const struct landlock_ruleset *dom_parent, *dom_child; | ||
| 55 | |||
| 56 | rcu_read_lock(); | ||
| 57 | dom_parent = landlock_get_task_domain(parent); | ||
| 58 | dom_child = landlock_get_task_domain(child); | ||
| 59 | is_scoped = domain_scope_le(dom_parent, dom_child); | ||
| 60 | rcu_read_unlock(); | ||
| 61 | return is_scoped; | ||
| 62 | } | ||
| 63 | |||
| 64 | static int task_ptrace(const struct task_struct *const parent, | ||
| 65 | const struct task_struct *const child) | ||
| 66 | { | ||
| 67 | /* Quick return for non-landlocked tasks. */ | ||
| 68 | if (!landlocked(parent)) | ||
| 69 | return 0; | ||
| 70 | if (task_is_scoped(parent, child)) | ||
| 71 | return 0; | ||
| 72 | return -EPERM; | ||
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * hook_ptrace_access_check - Determines whether the current process may access | ||
| 77 | * another | ||
| 78 | * | ||
| 79 | * @child: Process to be accessed. | ||
| 80 | * @mode: Mode of attachment. | ||
| 81 | * | ||
| 82 | * If the current task has Landlock rules, then the child must have at least | ||
| 83 | * the same rules. Else denied. | ||
| 84 | * | ||
| 85 | * Determines whether a process may access another, returning 0 if permission | ||
| 86 | * granted, -errno if denied. | ||
| 87 | */ | ||
| 88 | static int hook_ptrace_access_check(struct task_struct *const child, | ||
| 89 | const unsigned int mode) | ||
| 90 | { | ||
| 91 | return task_ptrace(current, child); | ||
| 92 | } | ||
| 93 | |||
| 94 | /** | ||
| 95 | * hook_ptrace_traceme - Determines whether another process may trace the | ||
| 96 | * current one | ||
| 97 | * | ||
| 98 | * @parent: Task proposed to be the tracer. | ||
| 99 | * | ||
| 100 | * If the parent has Landlock rules, then the current task must have the same | ||
| 101 | * or more rules. Else denied. | ||
| 102 | * | ||
| 103 | * Determines whether the nominated task is permitted to trace the current | ||
| 104 | * process, returning 0 if permission is granted, -errno if denied. | ||
| 105 | */ | ||
| 106 | static int hook_ptrace_traceme(struct task_struct *const parent) | ||
| 107 | { | ||
| 108 | return task_ptrace(parent, current); | ||
| 109 | } | ||
| 110 | |||
| 111 | static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { | ||
| 112 | LSM_HOOK_INIT(ptrace_access_check, hook_ptrace_access_check), | ||
| 113 | LSM_HOOK_INIT(ptrace_traceme, hook_ptrace_traceme), | ||
| 114 | }; | ||
| 115 | |||
| 116 | __init void landlock_add_ptrace_hooks(void) | ||
| 117 | { | ||
| 118 | security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks), | ||
| 119 | LANDLOCK_NAME); | ||
| 120 | } | ||
| @@ -0,0 +1,14 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Ptrace hooks | ||
| 4 | * | ||
| 5 | * Copyright © 2017-2019 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2019 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_PTRACE_H | ||
| 10 | #define _SECURITY_LANDLOCK_PTRACE_H | ||
| 11 | |||
| 12 | __init void landlock_add_ptrace_hooks(void); | ||
| 13 | |||
| 14 | #endif /* _SECURITY_LANDLOCK_PTRACE_H */ | ||
| @@ -0,0 +1,473 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Ruleset management | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <linux/bits.h> | ||
| 10 | #include <linux/bug.h> | ||
| 11 | #include <linux/compiler_types.h> | ||
| 12 | #include <linux/err.h> | ||
| 13 | #include <linux/errno.h> | ||
| 14 | #include <linux/kernel.h> | ||
| 15 | #include <linux/lockdep.h> | ||
| 16 | #include <linux/overflow.h> | ||
| 17 | #include <linux/rbtree.h> | ||
| 18 | #include <linux/refcount.h> | ||
| 19 | #include <linux/slab.h> | ||
| 20 | #include <linux/spinlock.h> | ||
| 21 | #include <linux/workqueue.h> | ||
| 22 | |||
| 23 | #include "limits.h" | ||
| 24 | #include "object.h" | ||
| 25 | #include "ruleset.h" | ||
| 26 | |||
| 27 | static struct landlock_ruleset *create_ruleset(const u32 num_layers) | ||
| 28 | { | ||
| 29 | struct landlock_ruleset *new_ruleset; | ||
| 30 | |||
| 31 | new_ruleset = kzalloc(struct_size(new_ruleset, fs_access_masks, | ||
| 32 | num_layers), GFP_KERNEL_ACCOUNT); | ||
| 33 | if (!new_ruleset) | ||
| 34 | return ERR_PTR(-ENOMEM); | ||
| 35 | refcount_set(&new_ruleset->usage, 1); | ||
| 36 | mutex_init(&new_ruleset->lock); | ||
| 37 | new_ruleset->root = RB_ROOT; | ||
| 38 | new_ruleset->num_layers = num_layers; | ||
| 39 | /* | ||
| 40 | * hierarchy = NULL | ||
| 41 | * num_rules = 0 | ||
| 42 | * fs_access_masks[] = 0 | ||
| 43 | */ | ||
| 44 | return new_ruleset; | ||
| 45 | } | ||
| 46 | |||
| 47 | struct landlock_ruleset *landlock_create_ruleset(const u32 fs_access_mask) | ||
| 48 | { | ||
| 49 | struct landlock_ruleset *new_ruleset; | ||
| 50 | |||
| 51 | /* Informs about useless ruleset. */ | ||
| 52 | if (!fs_access_mask) | ||
| 53 | return ERR_PTR(-ENOMSG); | ||
| 54 | new_ruleset = create_ruleset(1); | ||
| 55 | if (!IS_ERR(new_ruleset)) | ||
| 56 | new_ruleset->fs_access_masks[0] = fs_access_mask; | ||
| 57 | return new_ruleset; | ||
| 58 | } | ||
| 59 | |||
| 60 | static void build_check_rule(void) | ||
| 61 | { | ||
| 62 | const struct landlock_rule rule = { | ||
| 63 | .num_layers = ~0, | ||
| 64 | }; | ||
| 65 | |||
| 66 | BUILD_BUG_ON(rule.num_layers < LANDLOCK_MAX_NUM_LAYERS); | ||
| 67 | } | ||
| 68 | |||
| 69 | static struct landlock_rule *create_rule( | ||
| 70 | struct landlock_object *const object, | ||
| 71 | const struct landlock_layer (*const layers)[], | ||
| 72 | const u32 num_layers, | ||
| 73 | const struct landlock_layer *const new_layer) | ||
| 74 | { | ||
| 75 | struct landlock_rule *new_rule; | ||
| 76 | u32 new_num_layers; | ||
| 77 | |||
| 78 | build_check_rule(); | ||
| 79 | if (new_layer) { | ||
| 80 | /* Should already be checked by landlock_merge_ruleset(). */ | ||
| 81 | if (WARN_ON_ONCE(num_layers >= LANDLOCK_MAX_NUM_LAYERS)) | ||
| 82 | return ERR_PTR(-E2BIG); | ||
| 83 | new_num_layers = num_layers + 1; | ||
| 84 | } else { | ||
| 85 | new_num_layers = num_layers; | ||
| 86 | } | ||
| 87 | new_rule = kzalloc(struct_size(new_rule, layers, new_num_layers), | ||
| 88 | GFP_KERNEL_ACCOUNT); | ||
| 89 | if (!new_rule) | ||
| 90 | return ERR_PTR(-ENOMEM); | ||
| 91 | RB_CLEAR_NODE(&new_rule->node); | ||
| 92 | landlock_get_object(object); | ||
| 93 | new_rule->object = object; | ||
| 94 | new_rule->num_layers = new_num_layers; | ||
| 95 | /* Copies the original layer stack. */ | ||
| 96 | memcpy(new_rule->layers, layers, | ||
| 97 | flex_array_size(new_rule, layers, num_layers)); | ||
| 98 | if (new_layer) | ||
| 99 | /* Adds a copy of @new_layer on the layer stack. */ | ||
| 100 | new_rule->layers[new_rule->num_layers - 1] = *new_layer; | ||
| 101 | return new_rule; | ||
| 102 | } | ||
| 103 | |||
| 104 | static void free_rule(struct landlock_rule *const rule) | ||
| 105 | { | ||
| 106 | might_sleep(); | ||
| 107 | if (!rule) | ||
| 108 | return; | ||
| 109 | landlock_put_object(rule->object); | ||
| 110 | kfree(rule); | ||
| 111 | } | ||
| 112 | |||
| 113 | static void build_check_ruleset(void) | ||
| 114 | { | ||
| 115 | const struct landlock_ruleset ruleset = { | ||
| 116 | .num_rules = ~0, | ||
| 117 | .num_layers = ~0, | ||
| 118 | }; | ||
| 119 | typeof(ruleset.fs_access_masks[0]) fs_access_mask = ~0; | ||
| 120 | |||
| 121 | BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES); | ||
| 122 | BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS); | ||
| 123 | BUILD_BUG_ON(fs_access_mask < LANDLOCK_MASK_ACCESS_FS); | ||
| 124 | } | ||
| 125 | |||
| 126 | /** | ||
| 127 | * insert_rule - Create and insert a rule in a ruleset | ||
| 128 | * | ||
| 129 | * @ruleset: The ruleset to be updated. | ||
| 130 | * @object: The object to build the new rule with. The underlying kernel | ||
| 131 | * object must be held by the caller. | ||
| 132 | * @layers: One or multiple layers to be copied into the new rule. | ||
| 133 | * @num_layers: The number of @layers entries. | ||
| 134 | * | ||
| 135 | * When user space requests to add a new rule to a ruleset, @layers only | ||
| 136 | * contains one entry and this entry is not assigned to any level. In this | ||
| 137 | * case, the new rule will extend @ruleset, similarly to a boolean OR between | ||
| 138 | * access rights. | ||
| 139 | * | ||
| 140 | * When merging a ruleset in a domain, or copying a domain, @layers will be | ||
| 141 | * added to @ruleset as new constraints, similarly to a boolean AND between | ||
| 142 | * access rights. | ||
| 143 | */ | ||
| 144 | static int insert_rule(struct landlock_ruleset *const ruleset, | ||
| 145 | struct landlock_object *const object, | ||
| 146 | const struct landlock_layer (*const layers)[], | ||
| 147 | size_t num_layers) | ||
| 148 | { | ||
| 149 | struct rb_node **walker_node; | ||
| 150 | struct rb_node *parent_node = NULL; | ||
| 151 | struct landlock_rule *new_rule; | ||
| 152 | |||
| 153 | might_sleep(); | ||
| 154 | lockdep_assert_held(&ruleset->lock); | ||
| 155 | if (WARN_ON_ONCE(!object || !layers)) | ||
| 156 | return -ENOENT; | ||
| 157 | walker_node = &(ruleset->root.rb_node); | ||
| 158 | while (*walker_node) { | ||
| 159 | struct landlock_rule *const this = rb_entry(*walker_node, | ||
| 160 | struct landlock_rule, node); | ||
| 161 | |||
| 162 | if (this->object != object) { | ||
| 163 | parent_node = *walker_node; | ||
| 164 | if (this->object < object) | ||
| 165 | walker_node = &((*walker_node)->rb_right); | ||
| 166 | else | ||
| 167 | walker_node = &((*walker_node)->rb_left); | ||
| 168 | continue; | ||
| 169 | } | ||
| 170 | |||
| 171 | /* Only a single-level layer should match an existing rule. */ | ||
| 172 | if (WARN_ON_ONCE(num_layers != 1)) | ||
| 173 | return -EINVAL; | ||
| 174 | |||
| 175 | /* If there is a matching rule, updates it. */ | ||
| 176 | if ((*layers)[0].level == 0) { | ||
| 177 | /* | ||
| 178 | * Extends access rights when the request comes from | ||
| 179 | * landlock_add_rule(2), i.e. @ruleset is not a domain. | ||
| 180 | */ | ||
| 181 | if (WARN_ON_ONCE(this->num_layers != 1)) | ||
| 182 | return -EINVAL; | ||
| 183 | if (WARN_ON_ONCE(this->layers[0].level != 0)) | ||
| 184 | return -EINVAL; | ||
| 185 | this->layers[0].access |= (*layers)[0].access; | ||
| 186 | return 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | if (WARN_ON_ONCE(this->layers[0].level == 0)) | ||
| 190 | return -EINVAL; | ||
| 191 | |||
| 192 | /* | ||
| 193 | * Intersects access rights when it is a merge between a | ||
| 194 | * ruleset and a domain. | ||
| 195 | */ | ||
| 196 | new_rule = create_rule(object, &this->layers, this->num_layers, | ||
| 197 | &(*layers)[0]); | ||
| 198 | if (IS_ERR(new_rule)) | ||
| 199 | return PTR_ERR(new_rule); | ||
| 200 | rb_replace_node(&this->node, &new_rule->node, &ruleset->root); | ||
| 201 | free_rule(this); | ||
| 202 | return 0; | ||
| 203 | } | ||
| 204 | |||
| 205 | /* There is no match for @object. */ | ||
| 206 | build_check_ruleset(); | ||
| 207 | if (ruleset->num_rules >= LANDLOCK_MAX_NUM_RULES) | ||
| 208 | return -E2BIG; | ||
| 209 | new_rule = create_rule(object, layers, num_layers, NULL); | ||
| 210 | if (IS_ERR(new_rule)) | ||
| 211 | return PTR_ERR(new_rule); | ||
| 212 | rb_link_node(&new_rule->node, parent_node, walker_node); | ||
| 213 | rb_insert_color(&new_rule->node, &ruleset->root); | ||
| 214 | ruleset->num_rules++; | ||
| 215 | return 0; | ||
| 216 | } | ||
| 217 | |||
| 218 | static void build_check_layer(void) | ||
| 219 | { | ||
| 220 | const struct landlock_layer layer = { | ||
| 221 | .level = ~0, | ||
| 222 | .access = ~0, | ||
| 223 | }; | ||
| 224 | |||
| 225 | BUILD_BUG_ON(layer.level < LANDLOCK_MAX_NUM_LAYERS); | ||
| 226 | BUILD_BUG_ON(layer.access < LANDLOCK_MASK_ACCESS_FS); | ||
| 227 | } | ||
| 228 | |||
| 229 | /* @ruleset must be locked by the caller. */ | ||
| 230 | int landlock_insert_rule(struct landlock_ruleset *const ruleset, | ||
| 231 | struct landlock_object *const object, const u32 access) | ||
| 232 | { | ||
| 233 | struct landlock_layer layers[] = {{ | ||
| 234 | .access = access, | ||
| 235 | /* When @level is zero, insert_rule() extends @ruleset. */ | ||
| 236 | .level = 0, | ||
| 237 | }}; | ||
| 238 | |||
| 239 | build_check_layer(); | ||
| 240 | return insert_rule(ruleset, object, &layers, ARRAY_SIZE(layers)); | ||
| 241 | } | ||
| 242 | |||
| 243 | static inline void get_hierarchy(struct landlock_hierarchy *const hierarchy) | ||
| 244 | { | ||
| 245 | if (hierarchy) | ||
| 246 | refcount_inc(&hierarchy->usage); | ||
| 247 | } | ||
| 248 | |||
| 249 | static void put_hierarchy(struct landlock_hierarchy *hierarchy) | ||
| 250 | { | ||
| 251 | while (hierarchy && refcount_dec_and_test(&hierarchy->usage)) { | ||
| 252 | const struct landlock_hierarchy *const freeme = hierarchy; | ||
| 253 | |||
| 254 | hierarchy = hierarchy->parent; | ||
| 255 | kfree(freeme); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | static int merge_ruleset(struct landlock_ruleset *const dst, | ||
| 260 | struct landlock_ruleset *const src) | ||
| 261 | { | ||
| 262 | struct landlock_rule *walker_rule, *next_rule; | ||
| 263 | int err = 0; | ||
| 264 | |||
| 265 | might_sleep(); | ||
| 266 | /* Should already be checked by landlock_merge_ruleset() */ | ||
| 267 | if (WARN_ON_ONCE(!src)) | ||
| 268 | return 0; | ||
| 269 | /* Only merge into a domain. */ | ||
| 270 | if (WARN_ON_ONCE(!dst || !dst->hierarchy)) | ||
| 271 | return -EINVAL; | ||
| 272 | |||
| 273 | /* Locks @dst first because we are its only owner. */ | ||
| 274 | mutex_lock(&dst->lock); | ||
| 275 | mutex_lock_nested(&src->lock, SINGLE_DEPTH_NESTING); | ||
| 276 | |||
| 277 | /* Stacks the new layer. */ | ||
| 278 | if (WARN_ON_ONCE(src->num_layers != 1 || dst->num_layers < 1)) { | ||
| 279 | err = -EINVAL; | ||
| 280 | goto out_unlock; | ||
| 281 | } | ||
| 282 | dst->fs_access_masks[dst->num_layers - 1] = src->fs_access_masks[0]; | ||
| 283 | |||
| 284 | /* Merges the @src tree. */ | ||
| 285 | rbtree_postorder_for_each_entry_safe(walker_rule, next_rule, | ||
| 286 | &src->root, node) { | ||
| 287 | struct landlock_layer layers[] = {{ | ||
| 288 | .level = dst->num_layers, | ||
| 289 | }}; | ||
| 290 | |||
| 291 | if (WARN_ON_ONCE(walker_rule->num_layers != 1)) { | ||
| 292 | err = -EINVAL; | ||
| 293 | goto out_unlock; | ||
| 294 | } | ||
| 295 | if (WARN_ON_ONCE(walker_rule->layers[0].level != 0)) { | ||
| 296 | err = -EINVAL; | ||
| 297 | goto out_unlock; | ||
| 298 | } | ||
| 299 | layers[0].access = walker_rule->layers[0].access; | ||
| 300 | err = insert_rule(dst, walker_rule->object, &layers, | ||
| 301 | ARRAY_SIZE(layers)); | ||
| 302 | if (err) | ||
| 303 | goto out_unlock; | ||
| 304 | } | ||
| 305 | |||
| 306 | out_unlock: | ||
| 307 | mutex_unlock(&src->lock); | ||
| 308 | mutex_unlock(&dst->lock); | ||
| 309 | return err; | ||
| 310 | } | ||
| 311 | |||
| 312 | static int inherit_ruleset(struct landlock_ruleset *const parent, | ||
| 313 | struct landlock_ruleset *const child) | ||
| 314 | { | ||
| 315 | struct landlock_rule *walker_rule, *next_rule; | ||
| 316 | int err = 0; | ||
| 317 | |||
| 318 | might_sleep(); | ||
| 319 | if (!parent) | ||
| 320 | return 0; | ||
| 321 | |||
| 322 | /* Locks @child first because we are its only owner. */ | ||
| 323 | mutex_lock(&child->lock); | ||
| 324 | mutex_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING); | ||
| 325 | |||
| 326 | /* Copies the @parent tree. */ | ||
| 327 | rbtree_postorder_for_each_entry_safe(walker_rule, next_rule, | ||
| 328 | &parent->root, node) { | ||
| 329 | err = insert_rule(child, walker_rule->object, | ||
| 330 | &walker_rule->layers, walker_rule->num_layers); | ||
| 331 | if (err) | ||
| 332 | goto out_unlock; | ||
| 333 | } | ||
| 334 | |||
| 335 | if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) { | ||
| 336 | err = -EINVAL; | ||
| 337 | goto out_unlock; | ||
| 338 | } | ||
| 339 | /* Copies the parent layer stack and leaves a space for the new layer. */ | ||
| 340 | memcpy(child->fs_access_masks, parent->fs_access_masks, | ||
| 341 | flex_array_size(parent, fs_access_masks, parent->num_layers)); | ||
| 342 | |||
| 343 | if (WARN_ON_ONCE(!parent->hierarchy)) { | ||
| 344 | err = -EINVAL; | ||
| 345 | goto out_unlock; | ||
| 346 | } | ||
| 347 | get_hierarchy(parent->hierarchy); | ||
| 348 | child->hierarchy->parent = parent->hierarchy; | ||
| 349 | |||
| 350 | out_unlock: | ||
| 351 | mutex_unlock(&parent->lock); | ||
| 352 | mutex_unlock(&child->lock); | ||
| 353 | return err; | ||
| 354 | } | ||
| 355 | |||
| 356 | static void free_ruleset(struct landlock_ruleset *const ruleset) | ||
| 357 | { | ||
| 358 | struct landlock_rule *freeme, *next; | ||
| 359 | |||
| 360 | might_sleep(); | ||
| 361 | rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root, | ||
| 362 | node) | ||
| 363 | free_rule(freeme); | ||
| 364 | put_hierarchy(ruleset->hierarchy); | ||
| 365 | kfree(ruleset); | ||
| 366 | } | ||
| 367 | |||
| 368 | void landlock_put_ruleset(struct landlock_ruleset *const ruleset) | ||
| 369 | { | ||
| 370 | might_sleep(); | ||
| 371 | if (ruleset && refcount_dec_and_test(&ruleset->usage)) | ||
| 372 | free_ruleset(ruleset); | ||
| 373 | } | ||
| 374 | |||
| 375 | static void free_ruleset_work(struct work_struct *const work) | ||
| 376 | { | ||
| 377 | struct landlock_ruleset *ruleset; | ||
| 378 | |||
| 379 | ruleset = container_of(work, struct landlock_ruleset, work_free); | ||
| 380 | free_ruleset(ruleset); | ||
| 381 | } | ||
| 382 | |||
| 383 | void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) | ||
| 384 | { | ||
| 385 | if (ruleset && refcount_dec_and_test(&ruleset->usage)) { | ||
| 386 | INIT_WORK(&ruleset->work_free, free_ruleset_work); | ||
| 387 | schedule_work(&ruleset->work_free); | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | /** | ||
| 392 | * landlock_merge_ruleset - Merge a ruleset with a domain | ||
| 393 | * | ||
| 394 | * @parent: Parent domain. | ||
| 395 | * @ruleset: New ruleset to be merged. | ||
| 396 | * | ||
| 397 | * Returns the intersection of @parent and @ruleset, or returns @parent if | ||
| 398 | * @ruleset is empty, or returns a duplicate of @ruleset if @parent is empty. | ||
| 399 | */ | ||
| 400 | struct landlock_ruleset *landlock_merge_ruleset( | ||
| 401 | struct landlock_ruleset *const parent, | ||
| 402 | struct landlock_ruleset *const ruleset) | ||
| 403 | { | ||
| 404 | struct landlock_ruleset *new_dom; | ||
| 405 | u32 num_layers; | ||
| 406 | int err; | ||
| 407 | |||
| 408 | might_sleep(); | ||
| 409 | if (WARN_ON_ONCE(!ruleset || parent == ruleset)) | ||
| 410 | return ERR_PTR(-EINVAL); | ||
| 411 | |||
| 412 | if (parent) { | ||
| 413 | if (parent->num_layers >= LANDLOCK_MAX_NUM_LAYERS) | ||
| 414 | return ERR_PTR(-E2BIG); | ||
| 415 | num_layers = parent->num_layers + 1; | ||
| 416 | } else { | ||
| 417 | num_layers = 1; | ||
| 418 | } | ||
| 419 | |||
| 420 | /* Creates a new domain... */ | ||
| 421 | new_dom = create_ruleset(num_layers); | ||
| 422 | if (IS_ERR(new_dom)) | ||
| 423 | return new_dom; | ||
| 424 | new_dom->hierarchy = kzalloc(sizeof(*new_dom->hierarchy), | ||
| 425 | GFP_KERNEL_ACCOUNT); | ||
| 426 | if (!new_dom->hierarchy) { | ||
| 427 | err = -ENOMEM; | ||
| 428 | goto out_put_dom; | ||
| 429 | } | ||
| 430 | refcount_set(&new_dom->hierarchy->usage, 1); | ||
| 431 | |||
| 432 | /* ...as a child of @parent... */ | ||
| 433 | err = inherit_ruleset(parent, new_dom); | ||
| 434 | if (err) | ||
| 435 | goto out_put_dom; | ||
| 436 | |||
| 437 | /* ...and including @ruleset. */ | ||
| 438 | err = merge_ruleset(new_dom, ruleset); | ||
| 439 | if (err) | ||
| 440 | goto out_put_dom; | ||
| 441 | |||
| 442 | return new_dom; | ||
| 443 | |||
| 444 | out_put_dom: | ||
| 445 | landlock_put_ruleset(new_dom); | ||
| 446 | return ERR_PTR(err); | ||
| 447 | } | ||
| 448 | |||
| 449 | /* | ||
| 450 | * The returned access has the same lifetime as @ruleset. | ||
| 451 | */ | ||
| 452 | const struct landlock_rule *landlock_find_rule( | ||
| 453 | const struct landlock_ruleset *const ruleset, | ||
| 454 | const struct landlock_object *const object) | ||
| 455 | { | ||
| 456 | const struct rb_node *node; | ||
| 457 | |||
| 458 | if (!object) | ||
| 459 | return NULL; | ||
| 460 | node = ruleset->root.rb_node; | ||
| 461 | while (node) { | ||
| 462 | struct landlock_rule *this = rb_entry(node, | ||
| 463 | struct landlock_rule, node); | ||
| 464 | |||
| 465 | if (this->object == object) | ||
| 466 | return this; | ||
| 467 | if (this->object < object) | ||
| 468 | node = node->rb_right; | ||
| 469 | else | ||
| 470 | node = node->rb_left; | ||
| 471 | } | ||
| 472 | return NULL; | ||
| 473 | } | ||
| @@ -0,0 +1,165 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Ruleset management | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_RULESET_H | ||
| 10 | #define _SECURITY_LANDLOCK_RULESET_H | ||
| 11 | |||
| 12 | #include <linux/mutex.h> | ||
| 13 | #include <linux/rbtree.h> | ||
| 14 | #include <linux/refcount.h> | ||
| 15 | #include <linux/workqueue.h> | ||
| 16 | |||
| 17 | #include "object.h" | ||
| 18 | |||
| 19 | /** | ||
| 20 | * struct landlock_layer - Access rights for a given layer | ||
| 21 | */ | ||
| 22 | struct landlock_layer { | ||
| 23 | /** | ||
| 24 | * @level: Position of this layer in the layer stack. | ||
| 25 | */ | ||
| 26 | u16 level; | ||
| 27 | /** | ||
| 28 | * @access: Bitfield of allowed actions on the kernel object. They are | ||
| 29 | * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ). | ||
| 30 | */ | ||
| 31 | u16 access; | ||
| 32 | }; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * struct landlock_rule - Access rights tied to an object | ||
| 36 | */ | ||
| 37 | struct landlock_rule { | ||
| 38 | /** | ||
| 39 | * @node: Node in the ruleset's red-black tree. | ||
| 40 | */ | ||
| 41 | struct rb_node node; | ||
| 42 | /** | ||
| 43 | * @object: Pointer to identify a kernel object (e.g. an inode). This | ||
| 44 | * is used as a key for this ruleset element. This pointer is set once | ||
| 45 | * and never modified. It always points to an allocated object because | ||
| 46 | * each rule increments the refcount of its object. | ||
| 47 | */ | ||
| 48 | struct landlock_object *object; | ||
| 49 | /** | ||
| 50 | * @num_layers: Number of entries in @layers. | ||
| 51 | */ | ||
| 52 | u32 num_layers; | ||
| 53 | /** | ||
| 54 | * @layers: Stack of layers, from the latest to the newest, implemented | ||
| 55 | * as a flexible array member (FAM). | ||
| 56 | */ | ||
| 57 | struct landlock_layer layers[]; | ||
| 58 | }; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * struct landlock_hierarchy - Node in a ruleset hierarchy | ||
| 62 | */ | ||
| 63 | struct landlock_hierarchy { | ||
| 64 | /** | ||
| 65 | * @parent: Pointer to the parent node, or NULL if it is a root | ||
| 66 | * Landlock domain. | ||
| 67 | */ | ||
| 68 | struct landlock_hierarchy *parent; | ||
| 69 | /** | ||
| 70 | * @usage: Number of potential children domains plus their parent | ||
| 71 | * domain. | ||
| 72 | */ | ||
| 73 | refcount_t usage; | ||
| 74 | }; | ||
| 75 | |||
| 76 | /** | ||
| 77 | * struct landlock_ruleset - Landlock ruleset | ||
| 78 | * | ||
| 79 | * This data structure must contain unique entries, be updatable, and quick to | ||
| 80 | * match an object. | ||
| 81 | */ | ||
| 82 | struct landlock_ruleset { | ||
| 83 | /** | ||
| 84 | * @root: Root of a red-black tree containing &struct landlock_rule | ||
| 85 | * nodes. Once a ruleset is tied to a process (i.e. as a domain), this | ||
| 86 | * tree is immutable until @usage reaches zero. | ||
| 87 | */ | ||
| 88 | struct rb_root root; | ||
| 89 | /** | ||
| 90 | * @hierarchy: Enables hierarchy identification even when a parent | ||
| 91 | * domain vanishes. This is needed for the ptrace protection. | ||
| 92 | */ | ||
| 93 | struct landlock_hierarchy *hierarchy; | ||
| 94 | union { | ||
| 95 | /** | ||
| 96 | * @work_free: Enables to free a ruleset within a lockless | ||
| 97 | * section. This is only used by | ||
| 98 | * landlock_put_ruleset_deferred() when @usage reaches zero. | ||
| 99 | * The fields @lock, @usage, @num_rules, @num_layers and | ||
| 100 | * @fs_access_masks are then unused. | ||
| 101 | */ | ||
| 102 | struct work_struct work_free; | ||
| 103 | struct { | ||
| 104 | /** | ||
| 105 | * @lock: Protects against concurrent modifications of | ||
| 106 | * @root, if @usage is greater than zero. | ||
| 107 | */ | ||
| 108 | struct mutex lock; | ||
| 109 | /** | ||
| 110 | * @usage: Number of processes (i.e. domains) or file | ||
| 111 | * descriptors referencing this ruleset. | ||
| 112 | */ | ||
| 113 | refcount_t usage; | ||
| 114 | /** | ||
| 115 | * @num_rules: Number of non-overlapping (i.e. not for | ||
| 116 | * the same object) rules in this ruleset. | ||
| 117 | */ | ||
| 118 | u32 num_rules; | ||
| 119 | /** | ||
| 120 | * @num_layers: Number of layers that are used in this | ||
| 121 | * ruleset. This enables to check that all the layers | ||
| 122 | * allow an access request. A value of 0 identifies a | ||
| 123 | * non-merged ruleset (i.e. not a domain). | ||
| 124 | */ | ||
| 125 | u32 num_layers; | ||
| 126 | /** | ||
| 127 | * @fs_access_masks: Contains the subset of filesystem | ||
| 128 | * actions that are restricted by a ruleset. A domain | ||
| 129 | * saves all layers of merged rulesets in a stack | ||
| 130 | * (FAM), starting from the first layer to the last | ||
| 131 | * one. These layers are used when merging rulesets, | ||
| 132 | * for user space backward compatibility (i.e. | ||
| 133 | * future-proof), and to properly handle merged | ||
| 134 | * rulesets without overlapping access rights. These | ||
| 135 | * layers are set once and never changed for the | ||
| 136 | * lifetime of the ruleset. | ||
| 137 | */ | ||
| 138 | u16 fs_access_masks[]; | ||
| 139 | }; | ||
| 140 | }; | ||
| 141 | }; | ||
| 142 | |||
| 143 | struct landlock_ruleset *landlock_create_ruleset(const u32 fs_access_mask); | ||
| 144 | |||
| 145 | void landlock_put_ruleset(struct landlock_ruleset *const ruleset); | ||
| 146 | void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset); | ||
| 147 | |||
| 148 | int landlock_insert_rule(struct landlock_ruleset *const ruleset, | ||
| 149 | struct landlock_object *const object, const u32 access); | ||
| 150 | |||
| 151 | struct landlock_ruleset *landlock_merge_ruleset( | ||
| 152 | struct landlock_ruleset *const parent, | ||
| 153 | struct landlock_ruleset *const ruleset); | ||
| 154 | |||
| 155 | const struct landlock_rule *landlock_find_rule( | ||
| 156 | const struct landlock_ruleset *const ruleset, | ||
| 157 | const struct landlock_object *const object); | ||
| 158 | |||
| 159 | static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset) | ||
| 160 | { | ||
| 161 | if (ruleset) | ||
| 162 | refcount_inc(&ruleset->usage); | ||
| 163 | } | ||
| 164 | |||
| 165 | #endif /* _SECURITY_LANDLOCK_RULESET_H */ | ||
| @@ -0,0 +1,40 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Security framework setup | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <linux/init.h> | ||
| 10 | #include <linux/lsm_hooks.h> | ||
| 11 | |||
| 12 | #include "common.h" | ||
| 13 | #include "cred.h" | ||
| 14 | #include "fs.h" | ||
| 15 | #include "ptrace.h" | ||
| 16 | #include "setup.h" | ||
| 17 | |||
| 18 | bool landlock_initialized __lsm_ro_after_init = false; | ||
| 19 | |||
| 20 | struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = { | ||
| 21 | .lbs_cred = sizeof(struct landlock_cred_security), | ||
| 22 | .lbs_inode = sizeof(struct landlock_inode_security), | ||
| 23 | .lbs_superblock = sizeof(struct landlock_superblock_security), | ||
| 24 | }; | ||
| 25 | |||
| 26 | static int __init landlock_init(void) | ||
| 27 | { | ||
| 28 | landlock_add_cred_hooks(); | ||
| 29 | landlock_add_ptrace_hooks(); | ||
| 30 | landlock_add_fs_hooks(); | ||
| 31 | landlock_initialized = true; | ||
| 32 | pr_info("Up and running.\n"); | ||
| 33 | return 0; | ||
| 34 | } | ||
| 35 | |||
| 36 | DEFINE_LSM(LANDLOCK_NAME) = { | ||
| 37 | .name = LANDLOCK_NAME, | ||
| 38 | .init = landlock_init, | ||
| 39 | .blobs = &landlock_blob_sizes, | ||
| 40 | }; | ||
| @@ -0,0 +1,18 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | ||
| 2 | /* | ||
| 3 | * Landlock LSM - Security framework setup | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifndef _SECURITY_LANDLOCK_SETUP_H | ||
| 10 | #define _SECURITY_LANDLOCK_SETUP_H | ||
| 11 | |||
| 12 | #include <linux/lsm_hooks.h> | ||
| 13 | |||
| 14 | extern bool landlock_initialized; | ||
| 15 | |||
| 16 | extern struct lsm_blob_sizes landlock_blob_sizes; | ||
| 17 | |||
| 18 | #endif /* _SECURITY_LANDLOCK_SETUP_H */ | ||
| @@ -0,0 +1,451 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* | ||
| 3 | * Landlock LSM - System call implementations and user space interfaces | ||
| 4 | * | ||
| 5 | * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net> | ||
| 6 | * Copyright © 2018-2020 ANSSI | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <asm/current.h> | ||
| 10 | #include <linux/anon_inodes.h> | ||
| 11 | #include <linux/build_bug.h> | ||
| 12 | #include <linux/capability.h> | ||
| 13 | #include <linux/compiler_types.h> | ||
| 14 | #include <linux/dcache.h> | ||
| 15 | #include <linux/err.h> | ||
| 16 | #include <linux/errno.h> | ||
| 17 | #include <linux/fs.h> | ||
| 18 | #include <linux/limits.h> | ||
| 19 | #include <linux/mount.h> | ||
| 20 | #include <linux/path.h> | ||
| 21 | #include <linux/sched.h> | ||
| 22 | #include <linux/security.h> | ||
| 23 | #include <linux/stddef.h> | ||
| 24 | #include <linux/syscalls.h> | ||
| 25 | #include <linux/types.h> | ||
| 26 | #include <linux/uaccess.h> | ||
| 27 | #include <uapi/linux/landlock.h> | ||
| 28 | |||
| 29 | #include "cred.h" | ||
| 30 | #include "fs.h" | ||
| 31 | #include "limits.h" | ||
| 32 | #include "ruleset.h" | ||
| 33 | #include "setup.h" | ||
| 34 | |||
| 35 | /** | ||
| 36 | * copy_min_struct_from_user - Safe future-proof argument copying | ||
| 37 | * | ||
| 38 | * Extend copy_struct_from_user() to check for consistent user buffer. | ||
| 39 | * | ||
| 40 | * @dst: Kernel space pointer or NULL. | ||
| 41 | * @ksize: Actual size of the data pointed to by @dst. | ||
| 42 | * @ksize_min: Minimal required size to be copied. | ||
| 43 | * @src: User space pointer or NULL. | ||
| 44 | * @usize: (Alleged) size of the data pointed to by @src. | ||
| 45 | */ | ||
| 46 | static __always_inline int copy_min_struct_from_user(void *const dst, | ||
| 47 | const size_t ksize, const size_t ksize_min, | ||
| 48 | const void __user *const src, const size_t usize) | ||
| 49 | { | ||
| 50 | /* Checks buffer inconsistencies. */ | ||
| 51 | BUILD_BUG_ON(!dst); | ||
| 52 | if (!src) | ||
| 53 | return -EFAULT; | ||
| 54 | |||
| 55 | /* Checks size ranges. */ | ||
| 56 | BUILD_BUG_ON(ksize <= 0); | ||
| 57 | BUILD_BUG_ON(ksize < ksize_min); | ||
| 58 | if (usize < ksize_min) | ||
| 59 | return -EINVAL; | ||
| 60 | if (usize > PAGE_SIZE) | ||
| 61 | return -E2BIG; | ||
| 62 | |||
| 63 | /* Copies user buffer and fills with zeros. */ | ||
| 64 | return copy_struct_from_user(dst, ksize, src, usize); | ||
| 65 | } | ||
| 66 | |||
| 67 | /* | ||
| 68 | * This function only contains arithmetic operations with constants, leading to | ||
| 69 | * BUILD_BUG_ON(). The related code is evaluated and checked at build time, | ||
| 70 | * but it is then ignored thanks to compiler optimizations. | ||
| 71 | */ | ||
| 72 | static void build_check_abi(void) | ||
| 73 | { | ||
| 74 | struct landlock_ruleset_attr ruleset_attr; | ||
| 75 | struct landlock_path_beneath_attr path_beneath_attr; | ||
| 76 | size_t ruleset_size, path_beneath_size; | ||
| 77 | |||
| 78 | /* | ||
| 79 | * For each user space ABI structures, first checks that there is no | ||
| 80 | * hole in them, then checks that all architectures have the same | ||
| 81 | * struct size. | ||
| 82 | */ | ||
| 83 | ruleset_size = sizeof(ruleset_attr.handled_access_fs); | ||
| 84 | BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size); | ||
| 85 | BUILD_BUG_ON(sizeof(ruleset_attr) != 8); | ||
| 86 | |||
| 87 | path_beneath_size = sizeof(path_beneath_attr.allowed_access); | ||
| 88 | path_beneath_size += sizeof(path_beneath_attr.parent_fd); | ||
| 89 | BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size); | ||
| 90 | BUILD_BUG_ON(sizeof(path_beneath_attr) != 12); | ||
| 91 | } | ||
| 92 | |||
| 93 | /* Ruleset handling */ | ||
| 94 | |||
| 95 | static int fop_ruleset_release(struct inode *const inode, | ||
| 96 | struct file *const filp) | ||
| 97 | { | ||
| 98 | struct landlock_ruleset *ruleset = filp->private_data; | ||
| 99 | |||
| 100 | landlock_put_ruleset(ruleset); | ||
| 101 | return 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf, | ||
| 105 | const size_t size, loff_t *const ppos) | ||
| 106 | { | ||
| 107 | /* Dummy handler to enable FMODE_CAN_READ. */ | ||
| 108 | return -EINVAL; | ||
| 109 | } | ||
| 110 | |||
| 111 | static ssize_t fop_dummy_write(struct file *const filp, | ||
| 112 | const char __user *const buf, const size_t size, | ||
| 113 | loff_t *const ppos) | ||
| 114 | { | ||
| 115 | /* Dummy handler to enable FMODE_CAN_WRITE. */ | ||
| 116 | return -EINVAL; | ||
| 117 | } | ||
| 118 | |||
| 119 | /* | ||
| 120 | * A ruleset file descriptor enables to build a ruleset by adding (i.e. | ||
| 121 | * writing) rule after rule, without relying on the task's context. This | ||
| 122 | * reentrant design is also used in a read way to enforce the ruleset on the | ||
| 123 | * current task. | ||
| 124 | */ | ||
| 125 | static const struct file_operations ruleset_fops = { | ||
| 126 | .release = fop_ruleset_release, | ||
| 127 | .read = fop_dummy_read, | ||
| 128 | .write = fop_dummy_write, | ||
| 129 | }; | ||
| 130 | |||
| 131 | #define LANDLOCK_ABI_VERSION 1 | ||
| 132 | |||
| 133 | /** | ||
| 134 | * sys_landlock_create_ruleset - Create a new ruleset | ||
| 135 | * | ||
| 136 | * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of | ||
| 137 | * the new ruleset. | ||
| 138 | * @size: Size of the pointed &struct landlock_ruleset_attr (needed for | ||
| 139 | * backward and forward compatibility). | ||
| 140 | * @flags: Supported value: %LANDLOCK_CREATE_RULESET_VERSION. | ||
| 141 | * | ||
| 142 | * This system call enables to create a new Landlock ruleset, and returns the | ||
| 143 | * related file descriptor on success. | ||
| 144 | * | ||
| 145 | * If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is | ||
| 146 | * 0, then the returned value is the highest supported Landlock ABI version | ||
| 147 | * (starting at 1). | ||
| 148 | * | ||
| 149 | * Possible returned errors are: | ||
| 150 | * | ||
| 151 | * - EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; | ||
| 152 | * - EINVAL: unknown @flags, or unknown access, or too small @size; | ||
| 153 | * - E2BIG or EFAULT: @attr or @size inconsistencies; | ||
| 154 | * - ENOMSG: empty &landlock_ruleset_attr.handled_access_fs. | ||
| 155 | */ | ||
| 156 | SYSCALL_DEFINE3(landlock_create_ruleset, | ||
| 157 | const struct landlock_ruleset_attr __user *const, attr, | ||
| 158 | const size_t, size, const __u32, flags) | ||
| 159 | { | ||
| 160 | struct landlock_ruleset_attr ruleset_attr; | ||
| 161 | struct landlock_ruleset *ruleset; | ||
| 162 | int err, ruleset_fd; | ||
| 163 | |||
| 164 | /* Build-time checks. */ | ||
| 165 | build_check_abi(); | ||
| 166 | |||
| 167 | if (!landlock_initialized) | ||
| 168 | return -EOPNOTSUPP; | ||
| 169 | |||
| 170 | if (flags) { | ||
| 171 | if ((flags == LANDLOCK_CREATE_RULESET_VERSION) | ||
| 172 | && !attr && !size) | ||
| 173 | return LANDLOCK_ABI_VERSION; | ||
| 174 | return -EINVAL; | ||
| 175 | } | ||
| 176 | |||
| 177 | /* Copies raw user space buffer. */ | ||
| 178 | err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr), | ||
| 179 | offsetofend(typeof(ruleset_attr), handled_access_fs), | ||
| 180 | attr, size); | ||
| 181 | if (err) | ||
| 182 | return err; | ||
| 183 | |||
| 184 | /* Checks content (and 32-bits cast). */ | ||
| 185 | if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) != | ||
| 186 | LANDLOCK_MASK_ACCESS_FS) | ||
| 187 | return -EINVAL; | ||
| 188 | |||
| 189 | /* Checks arguments and transforms to kernel struct. */ | ||
| 190 | ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs); | ||
| 191 | if (IS_ERR(ruleset)) | ||
| 192 | return PTR_ERR(ruleset); | ||
| 193 | |||
| 194 | /* Creates anonymous FD referring to the ruleset. */ | ||
| 195 | ruleset_fd = anon_inode_getfd("landlock-ruleset", &ruleset_fops, | ||
| 196 | ruleset, O_RDWR | O_CLOEXEC); | ||
| 197 | if (ruleset_fd < 0) | ||
| 198 | landlock_put_ruleset(ruleset); | ||
| 199 | return ruleset_fd; | ||
| 200 | } | ||
| 201 | |||
| 202 | /* | ||
| 203 | * Returns an owned ruleset from a FD. It is thus needed to call | ||
| 204 | * landlock_put_ruleset() on the return value. | ||
| 205 | */ | ||
| 206 | static struct landlock_ruleset *get_ruleset_from_fd(const int fd, | ||
| 207 | const fmode_t mode) | ||
| 208 | { | ||
| 209 | struct fd ruleset_f; | ||
| 210 | struct landlock_ruleset *ruleset; | ||
| 211 | |||
| 212 | ruleset_f = fdget(fd); | ||
| 213 | if (!ruleset_f.file) | ||
| 214 | return ERR_PTR(-EBADF); | ||
| 215 | |||
| 216 | /* Checks FD type and access right. */ | ||
| 217 | if (ruleset_f.file->f_op != &ruleset_fops) { | ||
| 218 | ruleset = ERR_PTR(-EBADFD); | ||
| 219 | goto out_fdput; | ||
| 220 | } | ||
| 221 | if (!(ruleset_f.file->f_mode & mode)) { | ||
| 222 | ruleset = ERR_PTR(-EPERM); | ||
| 223 | goto out_fdput; | ||
| 224 | } | ||
| 225 | ruleset = ruleset_f.file->private_data; | ||
| 226 | if (WARN_ON_ONCE(ruleset->num_layers != 1)) { | ||
| 227 | ruleset = ERR_PTR(-EINVAL); | ||
| 228 | goto out_fdput; | ||
| 229 | } | ||
| 230 | landlock_get_ruleset(ruleset); | ||
| 231 | |||
| 232 | out_fdput: | ||
| 233 | fdput(ruleset_f); | ||
| 234 | return ruleset; | ||
| 235 | } | ||
| 236 | |||
| 237 | /* Path handling */ | ||
| 238 | |||
| 239 | /* | ||
| 240 | * @path: Must call put_path(@path) after the call if it succeeded. | ||
| 241 | */ | ||
| 242 | static int get_path_from_fd(const s32 fd, struct path *const path) | ||
| 243 | { | ||
| 244 | struct fd f; | ||
| 245 | int err = 0; | ||
| 246 | |||
| 247 | BUILD_BUG_ON(!__same_type(fd, | ||
| 248 | ((struct landlock_path_beneath_attr *)NULL)->parent_fd)); | ||
| 249 | |||
| 250 | /* Handles O_PATH. */ | ||
| 251 | f = fdget_raw(fd); | ||
| 252 | if (!f.file) | ||
| 253 | return -EBADF; | ||
| 254 | /* | ||
| 255 | * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including | ||
| 256 | * pseudo filesystems that will never be mountable (e.g. sockfs, | ||
| 257 | * pipefs). | ||
| 258 | */ | ||
| 259 | if ((f.file->f_op == &ruleset_fops) || | ||
| 260 | (f.file->f_path.mnt->mnt_flags & MNT_INTERNAL) || | ||
| 261 | (f.file->f_path.dentry->d_sb->s_flags & SB_NOUSER) || | ||
| 262 | d_is_negative(f.file->f_path.dentry) || | ||
| 263 | IS_PRIVATE(d_backing_inode(f.file->f_path.dentry))) { | ||
| 264 | err = -EBADFD; | ||
| 265 | goto out_fdput; | ||
| 266 | } | ||
| 267 | *path = f.file->f_path; | ||
| 268 | path_get(path); | ||
| 269 | |||
| 270 | out_fdput: | ||
| 271 | fdput(f); | ||
| 272 | return err; | ||
| 273 | } | ||
| 274 | |||
| 275 | /** | ||
| 276 | * sys_landlock_add_rule - Add a new rule to a ruleset | ||
| 277 | * | ||
| 278 | * @ruleset_fd: File descriptor tied to the ruleset that should be extended | ||
| 279 | * with the new rule. | ||
| 280 | * @rule_type: Identify the structure type pointed to by @rule_attr (only | ||
| 281 | * LANDLOCK_RULE_PATH_BENEATH for now). | ||
| 282 | * @rule_attr: Pointer to a rule (only of type &struct | ||
| 283 | * landlock_path_beneath_attr for now). | ||
| 284 | * @flags: Must be 0. | ||
| 285 | * | ||
| 286 | * This system call enables to define a new rule and add it to an existing | ||
| 287 | * ruleset. | ||
| 288 | * | ||
| 289 | * Possible returned errors are: | ||
| 290 | * | ||
| 291 | * - EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; | ||
| 292 | * - EINVAL: @flags is not 0, or inconsistent access in the rule (i.e. | ||
| 293 | * &landlock_path_beneath_attr.allowed_access is not a subset of the rule's | ||
| 294 | * accesses); | ||
| 295 | * - ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access); | ||
| 296 | * - EBADF: @ruleset_fd is not a file descriptor for the current thread, or a | ||
| 297 | * member of @rule_attr is not a file descriptor as expected; | ||
| 298 | * - EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of | ||
| 299 | * @rule_attr is not the expected file descriptor type (e.g. file open | ||
| 300 | * without O_PATH); | ||
| 301 | * - EPERM: @ruleset_fd has no write access to the underlying ruleset; | ||
| 302 | * - EFAULT: @rule_attr inconsistency. | ||
| 303 | */ | ||
| 304 | SYSCALL_DEFINE4(landlock_add_rule, | ||
| 305 | const int, ruleset_fd, const enum landlock_rule_type, rule_type, | ||
| 306 | const void __user *const, rule_attr, const __u32, flags) | ||
| 307 | { | ||
| 308 | struct landlock_path_beneath_attr path_beneath_attr; | ||
| 309 | struct path path; | ||
| 310 | struct landlock_ruleset *ruleset; | ||
| 311 | int res, err; | ||
| 312 | |||
| 313 | if (!landlock_initialized) | ||
| 314 | return -EOPNOTSUPP; | ||
| 315 | |||
| 316 | /* No flag for now. */ | ||
| 317 | if (flags) | ||
| 318 | return -EINVAL; | ||
| 319 | |||
| 320 | if (rule_type != LANDLOCK_RULE_PATH_BENEATH) | ||
| 321 | return -EINVAL; | ||
| 322 | |||
| 323 | /* Copies raw user space buffer, only one type for now. */ | ||
| 324 | res = copy_from_user(&path_beneath_attr, rule_attr, | ||
| 325 | sizeof(path_beneath_attr)); | ||
| 326 | if (res) | ||
| 327 | return -EFAULT; | ||
| 328 | |||
| 329 | /* Gets and checks the ruleset. */ | ||
| 330 | ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE); | ||
| 331 | if (IS_ERR(ruleset)) | ||
| 332 | return PTR_ERR(ruleset); | ||
| 333 | |||
| 334 | /* | ||
| 335 | * Informs about useless rule: empty allowed_access (i.e. deny rules) | ||
| 336 | * are ignored in path walks. | ||
| 337 | */ | ||
| 338 | if (!path_beneath_attr.allowed_access) { | ||
| 339 | err = -ENOMSG; | ||
| 340 | goto out_put_ruleset; | ||
| 341 | } | ||
| 342 | /* | ||
| 343 | * Checks that allowed_access matches the @ruleset constraints | ||
| 344 | * (ruleset->fs_access_masks[0] is automatically upgraded to 64-bits). | ||
| 345 | */ | ||
| 346 | if ((path_beneath_attr.allowed_access | ruleset->fs_access_masks[0]) != | ||
| 347 | ruleset->fs_access_masks[0]) { | ||
| 348 | err = -EINVAL; | ||
| 349 | goto out_put_ruleset; | ||
| 350 | } | ||
| 351 | |||
| 352 | /* Gets and checks the new rule. */ | ||
| 353 | err = get_path_from_fd(path_beneath_attr.parent_fd, &path); | ||
| 354 | if (err) | ||
| 355 | goto out_put_ruleset; | ||
| 356 | |||
| 357 | /* Imports the new rule. */ | ||
| 358 | err = landlock_append_fs_rule(ruleset, &path, | ||
| 359 | path_beneath_attr.allowed_access); | ||
| 360 | path_put(&path); | ||
| 361 | |||
| 362 | out_put_ruleset: | ||
| 363 | landlock_put_ruleset(ruleset); | ||
| 364 | return err; | ||
| 365 | } | ||
| 366 | |||
| 367 | /* Enforcement */ | ||
| 368 | |||
| 369 | /** | ||
| 370 | * sys_landlock_restrict_self - Enforce a ruleset on the calling thread | ||
| 371 | * | ||
| 372 | * @ruleset_fd: File descriptor tied to the ruleset to merge with the target. | ||
| 373 | * @flags: Must be 0. | ||
| 374 | * | ||
| 375 | * This system call enables to enforce a Landlock ruleset on the current | ||
| 376 | * thread. Enforcing a ruleset requires that the task has CAP_SYS_ADMIN in its | ||
| 377 | * namespace or is running with no_new_privs. This avoids scenarios where | ||
| 378 | * unprivileged tasks can affect the behavior of privileged children. | ||
| 379 | * | ||
| 380 | * Possible returned errors are: | ||
| 381 | * | ||
| 382 | * - EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; | ||
| 383 | * - EINVAL: @flags is not 0. | ||
| 384 | * - EBADF: @ruleset_fd is not a file descriptor for the current thread; | ||
| 385 | * - EBADFD: @ruleset_fd is not a ruleset file descriptor; | ||
| 386 | * - EPERM: @ruleset_fd has no read access to the underlying ruleset, or the | ||
| 387 | * current thread is not running with no_new_privs, or it doesn't have | ||
| 388 | * CAP_SYS_ADMIN in its namespace. | ||
| 389 | * - E2BIG: The maximum number of stacked rulesets is reached for the current | ||
| 390 | * thread. | ||
| 391 | */ | ||
| 392 | SYSCALL_DEFINE2(landlock_restrict_self, | ||
| 393 | const int, ruleset_fd, const __u32, flags) | ||
| 394 | { | ||
| 395 | struct landlock_ruleset *new_dom, *ruleset; | ||
| 396 | struct cred *new_cred; | ||
| 397 | struct landlock_cred_security *new_llcred; | ||
| 398 | int err; | ||
| 399 | |||
| 400 | if (!landlock_initialized) | ||
| 401 | return -EOPNOTSUPP; | ||
| 402 | |||
| 403 | /* No flag for now. */ | ||
| 404 | if (flags) | ||
| 405 | return -EINVAL; | ||
| 406 | |||
| 407 | /* | ||
| 408 | * Similar checks as for seccomp(2), except that an -EPERM may be | ||
| 409 | * returned. | ||
| 410 | */ | ||
| 411 | if (!task_no_new_privs(current) && | ||
| 412 | !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN)) | ||
| 413 | return -EPERM; | ||
| 414 | |||
| 415 | /* Gets and checks the ruleset. */ | ||
| 416 | ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ); | ||
| 417 | if (IS_ERR(ruleset)) | ||
| 418 | return PTR_ERR(ruleset); | ||
| 419 | |||
| 420 | /* Prepares new credentials. */ | ||
| 421 | new_cred = prepare_creds(); | ||
| 422 | if (!new_cred) { | ||
| 423 | err = -ENOMEM; | ||
| 424 | goto out_put_ruleset; | ||
| 425 | } | ||
| 426 | new_llcred = landlock_cred(new_cred); | ||
| 427 | |||
| 428 | /* | ||
| 429 | * There is no possible race condition while copying and manipulating | ||
| 430 | * the current credentials because they are dedicated per thread. | ||
| 431 | */ | ||
| 432 | new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset); | ||
| 433 | if (IS_ERR(new_dom)) { | ||
| 434 | err = PTR_ERR(new_dom); | ||
| 435 | goto out_put_creds; | ||
| 436 | } | ||
| 437 | |||
| 438 | /* Replaces the old (prepared) domain. */ | ||
| 439 | landlock_put_ruleset(new_llcred->domain); | ||
| 440 | new_llcred->domain = new_dom; | ||
| 441 | |||
| 442 | landlock_put_ruleset(ruleset); | ||
| 443 | return commit_creds(new_cred); | ||
| 444 | |||
| 445 | out_put_creds: | ||
| 446 | abort_creds(new_cred); | ||
| 447 | |||
| 448 | out_put_ruleset: | ||
| 449 | landlock_put_ruleset(ruleset); | ||
| 450 | return err; | ||
| 451 | } | ||
| @@ -203,6 +203,7 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed) · 202 unmodified lines | |||
| 203 | lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode); | 203 | lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode); |
| 204 | lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc); | 204 | lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc); |
| 205 | lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); | 205 | lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); |
| 206 | lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock); | ||
| 206 | lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task); | 207 | lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task); |
| 207 | } | 208 | } |
| 208 | 209 | ||
| @@ -333,12 +334,13 @@ static void __init ordered_lsm_init(void) · 124 unmodified lines | |||
| 333 | for (lsm = ordered_lsms; *lsm; lsm++) | 334 | for (lsm = ordered_lsms; *lsm; lsm++) |
| 334 | prepare_lsm(*lsm); | 335 | prepare_lsm(*lsm); |
| 335 | 336 | ||
| 336 | init_debug("cred blob size = %d\n", blob_sizes.lbs_cred); | 337 | init_debug("cred blob size = %d\n", blob_sizes.lbs_cred); |
| 337 | init_debug("file blob size = %d\n", blob_sizes.lbs_file); | 338 | init_debug("file blob size = %d\n", blob_sizes.lbs_file); |
| 338 | init_debug("inode blob size = %d\n", blob_sizes.lbs_inode); | 339 | init_debug("inode blob size = %d\n", blob_sizes.lbs_inode); |
| 339 | init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc); | 340 | init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc); |
| 340 | init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg); | 341 | init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg); |
| 341 | init_debug("task blob size = %d\n", blob_sizes.lbs_task); | 342 | init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock); |
| 343 | init_debug("task blob size = %d\n", blob_sizes.lbs_task); | ||
| 342 | 344 | ||
| 343 | /* | 345 | /* |
| 344 | * Create any kmem_caches needed for blobs | 346 | * Create any kmem_caches needed for blobs |
| @@ -670,6 +672,27 @@ static void __init lsm_early_task(struct task_struct *task) · 325 unmodified lines | |||
| 670 | panic("%s: Early task alloc failed.\n", __func__); | 672 | panic("%s: Early task alloc failed.\n", __func__); |
| 671 | } | 673 | } |
| 672 | 674 | ||
| 675 | /** | ||
| 676 | * lsm_superblock_alloc - allocate a composite superblock blob | ||
| 677 | * @sb: the superblock that needs a blob | ||
| 678 | * | ||
| 679 | * Allocate the superblock blob for all the modules | ||
| 680 | * | ||
| 681 | * Returns 0, or -ENOMEM if memory can't be allocated. | ||
| 682 | */ | ||
| 683 | static int lsm_superblock_alloc(struct super_block *sb) | ||
| 684 | { | ||
| 685 | if (blob_sizes.lbs_superblock == 0) { | ||
| 686 | sb->s_security = NULL; | ||
| 687 | return 0; | ||
| 688 | } | ||
| 689 | |||
| 690 | sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL); | ||
| 691 | if (sb->s_security == NULL) | ||
| 692 | return -ENOMEM; | ||
| 693 | return 0; | ||
| 694 | } | ||
| 695 | |||
| 673 | /* | 696 | /* |
| 674 | * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and | 697 | * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and |
| 675 | * can be accessed with: | 698 | * can be accessed with: |
| @@ -867,12 +890,26 @@ int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter * · 191 unmodified lines | |||
| 867 | 890 | ||
| 868 | int security_sb_alloc(struct super_block *sb) | 891 | int security_sb_alloc(struct super_block *sb) |
| 869 | { | 892 | { |
| 870 | return call_int_hook(sb_alloc_security, 0, sb); | 893 | int rc = lsm_superblock_alloc(sb); |
| 894 | |||
| 895 | if (unlikely(rc)) | ||
| 896 | return rc; | ||
| 897 | rc = call_int_hook(sb_alloc_security, 0, sb); | ||
| 898 | if (unlikely(rc)) | ||
| 899 | security_sb_free(sb); | ||
| 900 | return rc; | ||
| 901 | } | ||
| 902 | |||
| 903 | void security_sb_delete(struct super_block *sb) | ||
| 904 | { | ||
| 905 | call_void_hook(sb_delete, sb); | ||
| 871 | } | 906 | } |
| 872 | 907 | ||
| 873 | void security_sb_free(struct super_block *sb) | 908 | void security_sb_free(struct super_block *sb) |
| 874 | { | 909 | { |
| 875 | call_void_hook(sb_free_security, sb); | 910 | call_void_hook(sb_free_security, sb); |
| 911 | kfree(sb->s_security); | ||
| 912 | sb->s_security = NULL; | ||
| 876 | } | 913 | } |
| 877 | 914 | ||
| 878 | void security_free_mnt_opts(void **mnt_opts) | 915 | void security_free_mnt_opts(void **mnt_opts) |
| @@ -358,7 +358,7 @@ static void inode_free_security(struct inode *inode) · 357 unmodified lines | |||
| 358 | 358 | ||
| 359 | if (!isec) | 359 | if (!isec) |
| 360 | return; | 360 | return; |
| 361 | sbsec = inode->i_sb->s_security; | 361 | sbsec = selinux_superblock(inode->i_sb); |
| 362 | /* | 362 | /* |
| 363 | * As not all inode security structures are in a list, we check for | 363 | * As not all inode security structures are in a list, we check for |
| 364 | * empty list outside of the lock to make sure that we won't waste | 364 | * empty list outside of the lock to make sure that we won't waste |
| @@ -376,13 +376,6 @@ static void inode_free_security(struct inode *inode) · 11 unmodified lines | |||
| 376 | } | 376 | } |
| 377 | } | 377 | } |
| 378 | 378 | ||
| 379 | static void superblock_free_security(struct super_block *sb) | ||
| 380 | { | ||
| 381 | struct superblock_security_struct *sbsec = sb->s_security; | ||
| 382 | sb->s_security = NULL; | ||
| 383 | kfree(sbsec); | ||
| 384 | } | ||
| 385 | |||
| 386 | struct selinux_mnt_opts { | 379 | struct selinux_mnt_opts { |
| 387 | const char *fscontext, *context, *rootcontext, *defcontext; | 380 | const char *fscontext, *context, *rootcontext, *defcontext; |
| 388 | }; | 381 | }; |
| @@ -494,7 +487,7 @@ static int selinux_is_genfs_special_handling(struct super_block *sb) · 105 unmodified lines | |||
| 494 | 487 | ||
| 495 | static int selinux_is_sblabel_mnt(struct super_block *sb) | 488 | static int selinux_is_sblabel_mnt(struct super_block *sb) |
| 496 | { | 489 | { |
| 497 | struct superblock_security_struct *sbsec = sb->s_security; | 490 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 498 | 491 | ||
| 499 | /* | 492 | /* |
| 500 | * IMPORTANT: Double-check logic in this function when adding a new | 493 | * IMPORTANT: Double-check logic in this function when adding a new |
| @@ -571,7 +564,7 @@ fallback: · 70 unmodified lines | |||
| 571 | 564 | ||
| 572 | static int sb_finish_set_opts(struct super_block *sb) | 565 | static int sb_finish_set_opts(struct super_block *sb) |
| 573 | { | 566 | { |
| 574 | struct superblock_security_struct *sbsec = sb->s_security; | 567 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 575 | struct dentry *root = sb->s_root; | 568 | struct dentry *root = sb->s_root; |
| 576 | struct inode *root_inode = d_backing_inode(root); | 569 | struct inode *root_inode = d_backing_inode(root); |
| 577 | int rc = 0; | 570 | int rc = 0; |
| @@ -662,7 +655,7 @@ static int selinux_set_mnt_opts(struct super_block *sb, · 84 unmodified lines | |||
| 662 | unsigned long *set_kern_flags) | 655 | unsigned long *set_kern_flags) |
| 663 | { | 656 | { |
| 664 | const struct cred *cred = current_cred(); | 657 | const struct cred *cred = current_cred(); |
| 665 | struct superblock_security_struct *sbsec = sb->s_security; | 658 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 666 | struct dentry *root = sb->s_root; | 659 | struct dentry *root = sb->s_root; |
| 667 | struct selinux_mnt_opts *opts = mnt_opts; | 660 | struct selinux_mnt_opts *opts = mnt_opts; |
| 668 | struct inode_security_struct *root_isec; | 661 | struct inode_security_struct *root_isec; |
| @@ -900,8 +893,8 @@ out_double_mount: · 231 unmodified lines | |||
| 900 | static int selinux_cmp_sb_context(const struct super_block *oldsb, | 893 | static int selinux_cmp_sb_context(const struct super_block *oldsb, |
| 901 | const struct super_block *newsb) | 894 | const struct super_block *newsb) |
| 902 | { | 895 | { |
| 903 | struct superblock_security_struct *old = oldsb->s_security; | 896 | struct superblock_security_struct *old = selinux_superblock(oldsb); |
| 904 | struct superblock_security_struct *new = newsb->s_security; | 897 | struct superblock_security_struct *new = selinux_superblock(newsb); |
| 905 | char oldflags = old->flags & SE_MNTMASK; | 898 | char oldflags = old->flags & SE_MNTMASK; |
| 906 | char newflags = new->flags & SE_MNTMASK; | 899 | char newflags = new->flags & SE_MNTMASK; |
| 907 | 900 | ||
| @@ -933,8 +926,9 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, · 25 unmodified lines | |||
| 933 | unsigned long *set_kern_flags) | 926 | unsigned long *set_kern_flags) |
| 934 | { | 927 | { |
| 935 | int rc = 0; | 928 | int rc = 0; |
| 936 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; | 929 | const struct superblock_security_struct *oldsbsec = |
| 937 | struct superblock_security_struct *newsbsec = newsb->s_security; | 930 | selinux_superblock(oldsb); |
| 931 | struct superblock_security_struct *newsbsec = selinux_superblock(newsb); | ||
| 938 | 932 | ||
| 939 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); | 933 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 940 | int set_context = (oldsbsec->flags & CONTEXT_MNT); | 934 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| @@ -1113,7 +1107,7 @@ static int show_sid(struct seq_file *m, u32 sid) · 172 unmodified lines | |||
| 1113 | 1107 | ||
| 1114 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) | 1108 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1115 | { | 1109 | { |
| 1116 | struct superblock_security_struct *sbsec = sb->s_security; | 1110 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 1117 | int rc; | 1111 | int rc; |
| 1118 | 1112 | ||
| 1119 | if (!(sbsec->flags & SE_SBINITIALIZED)) | 1113 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| @@ -1464,7 +1458,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent · 344 unmodified lines | |||
| 1464 | if (isec->sclass == SECCLASS_FILE) | 1458 | if (isec->sclass == SECCLASS_FILE) |
| 1465 | isec->sclass = inode_mode_to_security_class(inode->i_mode); | 1459 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 1466 | 1460 | ||
| 1467 | sbsec = inode->i_sb->s_security; | 1461 | sbsec = selinux_superblock(inode->i_sb); |
| 1468 | if (!(sbsec->flags & SE_SBINITIALIZED)) { | 1462 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
| 1469 | /* Defer initialization until selinux_complete_init, | 1463 | /* Defer initialization until selinux_complete_init, |
| 1470 | after the initial policy is loaded and the security | 1464 | after the initial policy is loaded and the security |
| @@ -1815,7 +1809,8 @@ selinux_determine_inode_label(const struct task_security_struct *tsec, · 344 unmodified lines | |||
| 1815 | const struct qstr *name, u16 tclass, | 1809 | const struct qstr *name, u16 tclass, |
| 1816 | u32 *_new_isid) | 1810 | u32 *_new_isid) |
| 1817 | { | 1811 | { |
| 1818 | const struct superblock_security_struct *sbsec = dir->i_sb->s_security; | 1812 | const struct superblock_security_struct *sbsec = |
| 1813 | selinux_superblock(dir->i_sb); | ||
| 1819 | 1814 | ||
| 1820 | if ((sbsec->flags & SE_SBINITIALIZED) && | 1815 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 1821 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { | 1816 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { |
| @@ -1846,7 +1841,7 @@ static int may_create(struct inode *dir, · 24 unmodified lines | |||
| 1846 | int rc; | 1841 | int rc; |
| 1847 | 1842 | ||
| 1848 | dsec = inode_security(dir); | 1843 | dsec = inode_security(dir); |
| 1849 | sbsec = dir->i_sb->s_security; | 1844 | sbsec = selinux_superblock(dir->i_sb); |
| 1850 | 1845 | ||
| 1851 | sid = tsec->sid; | 1846 | sid = tsec->sid; |
| 1852 | 1847 | ||
| @@ -1995,7 +1990,7 @@ static int superblock_has_perm(const struct cred *cred, · 142 unmodified lines | |||
| 1995 | struct superblock_security_struct *sbsec; | 1990 | struct superblock_security_struct *sbsec; |
| 1996 | u32 sid = cred_sid(cred); | 1991 | u32 sid = cred_sid(cred); |
| 1997 | 1992 | ||
| 1998 | sbsec = sb->s_security; | 1993 | sbsec = selinux_superblock(sb); |
| 1999 | return avc_has_perm(&selinux_state, | 1994 | return avc_has_perm(&selinux_state, |
| 2000 | sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); | 1995 | sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
| 2001 | } | 1996 | } |
| @@ -2617,11 +2612,7 @@ static void selinux_bprm_committed_creds(struct linux_binprm *bprm) · 615 unmodified lines | |||
| 2617 | 2612 | ||
| 2618 | static int selinux_sb_alloc_security(struct super_block *sb) | 2613 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2619 | { | 2614 | { |
| 2620 | struct superblock_security_struct *sbsec; | 2615 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 2621 | |||
| 2622 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); | ||
| 2623 | if (!sbsec) | ||
| 2624 | return -ENOMEM; | ||
| 2625 | 2616 | ||
| 2626 | mutex_init(&sbsec->lock); | 2617 | mutex_init(&sbsec->lock); |
| 2627 | INIT_LIST_HEAD(&sbsec->isec_head); | 2618 | INIT_LIST_HEAD(&sbsec->isec_head); |
| @@ -2629,16 +2620,10 @@ static int selinux_sb_alloc_security(struct super_block *sb) · 1 unmodified line | |||
| 2629 | sbsec->sid = SECINITSID_UNLABELED; | 2620 | sbsec->sid = SECINITSID_UNLABELED; |
| 2630 | sbsec->def_sid = SECINITSID_FILE; | 2621 | sbsec->def_sid = SECINITSID_FILE; |
| 2631 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; | 2622 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
| 2632 | sb->s_security = sbsec; | ||
| 2633 | 2623 | ||
| 2634 | return 0; | 2624 | return 0; |
| 2635 | } | 2625 | } |
| 2636 | 2626 | ||
| 2637 | static void selinux_sb_free_security(struct super_block *sb) | ||
| 2638 | { | ||
| 2639 | superblock_free_security(sb); | ||
| 2640 | } | ||
| 2641 | |||
| 2642 | static inline int opt_len(const char *s) | 2627 | static inline int opt_len(const char *s) |
| 2643 | { | 2628 | { |
| 2644 | bool open_quote = false; | 2629 | bool open_quote = false; |
| @@ -2772,7 +2757,7 @@ static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts) · 127 unmodified lines | |||
| 2772 | static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) | 2757 | static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) |
| 2773 | { | 2758 | { |
| 2774 | struct selinux_mnt_opts *opts = mnt_opts; | 2759 | struct selinux_mnt_opts *opts = mnt_opts; |
| 2775 | struct superblock_security_struct *sbsec = sb->s_security; | 2760 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 2776 | u32 sid; | 2761 | u32 sid; |
| 2777 | int rc; | 2762 | int rc; |
| 2778 | 2763 | ||
| @@ -3010,7 +2995,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 231 unmodified lines | |||
| 3010 | int rc; | 2995 | int rc; |
| 3011 | char *context; | 2996 | char *context; |
| 3012 | 2997 | ||
| 3013 | sbsec = dir->i_sb->s_security; | 2998 | sbsec = selinux_superblock(dir->i_sb); |
| 3014 | 2999 | ||
| 3015 | newsid = tsec->create_sid; | 3000 | newsid = tsec->create_sid; |
| 3016 | 3001 | ||
| @@ -3312,7 +3297,7 @@ static int selinux_inode_setxattr(struct user_namespace *mnt_userns, · 295 unmodified lines | |||
| 3312 | if (!selinux_initialized(&selinux_state)) | 3297 | if (!selinux_initialized(&selinux_state)) |
| 3313 | return (inode_owner_or_capable(mnt_userns, inode) ? 0 : -EPERM); | 3298 | return (inode_owner_or_capable(mnt_userns, inode) ? 0 : -EPERM); |
| 3314 | 3299 | ||
| 3315 | sbsec = inode->i_sb->s_security; | 3300 | sbsec = selinux_superblock(inode->i_sb); |
| 3316 | if (!(sbsec->flags & SBLABEL_MNT)) | 3301 | if (!(sbsec->flags & SBLABEL_MNT)) |
| 3317 | return -EOPNOTSUPP; | 3302 | return -EOPNOTSUPP; |
| 3318 | 3303 | ||
| @@ -3557,13 +3542,14 @@ static int selinux_inode_setsecurity(struct inode *inode, const char *name, · 238 unmodified lines | |||
| 3557 | const void *value, size_t size, int flags) | 3542 | const void *value, size_t size, int flags) |
| 3558 | { | 3543 | { |
| 3559 | struct inode_security_struct *isec = inode_security_novalidate(inode); | 3544 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
| 3560 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; | 3545 | struct superblock_security_struct *sbsec; |
| 3561 | u32 newsid; | 3546 | u32 newsid; |
| 3562 | int rc; | 3547 | int rc; |
| 3563 | 3548 | ||
| 3564 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) | 3549 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3565 | return -EOPNOTSUPP; | 3550 | return -EOPNOTSUPP; |
| 3566 | 3551 | ||
| 3552 | sbsec = selinux_superblock(inode->i_sb); | ||
| 3567 | if (!(sbsec->flags & SBLABEL_MNT)) | 3553 | if (!(sbsec->flags & SBLABEL_MNT)) |
| 3568 | return -EOPNOTSUPP; | 3554 | return -EOPNOTSUPP; |
| 3569 | 3555 | ||
| @@ -7065,6 +7051,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { · 3495 unmodified lines | |||
| 7065 | .lbs_inode = sizeof(struct inode_security_struct), | 7051 | .lbs_inode = sizeof(struct inode_security_struct), |
| 7066 | .lbs_ipc = sizeof(struct ipc_security_struct), | 7052 | .lbs_ipc = sizeof(struct ipc_security_struct), |
| 7067 | .lbs_msg_msg = sizeof(struct msg_security_struct), | 7053 | .lbs_msg_msg = sizeof(struct msg_security_struct), |
| 7054 | .lbs_superblock = sizeof(struct superblock_security_struct), | ||
| 7068 | }; | 7055 | }; |
| 7069 | 7056 | ||
| 7070 | #ifdef CONFIG_PERF_EVENTS | 7057 | #ifdef CONFIG_PERF_EVENTS |
| @@ -7165,7 +7152,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 94 unmodified lines | |||
| 7165 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), | 7152 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), |
| 7166 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), | 7153 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), |
| 7167 | 7154 | ||
| 7168 | LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), | ||
| 7169 | LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), | 7155 | LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), |
| 7170 | LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat), | 7156 | LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat), |
| 7171 | LSM_HOOK_INIT(sb_remount, selinux_sb_remount), | 7157 | LSM_HOOK_INIT(sb_remount, selinux_sb_remount), |
| @@ -188,4 +188,10 @@ static inline u32 current_sid(void) · 187 unmodified lines | |||
| 188 | return tsec->sid; | 188 | return tsec->sid; |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | static inline struct superblock_security_struct *selinux_superblock( | ||
| 192 | const struct super_block *superblock) | ||
| 193 | { | ||
| 194 | return superblock->s_security + selinux_blob_sizes.lbs_superblock; | ||
| 195 | } | ||
| 196 | |||
| 191 | #endif /* _SELINUX_OBJSEC_H_ */ | 197 | #endif /* _SELINUX_OBJSEC_H_ */ |
| @@ -47,6 +47,7 @@ · 46 unmodified lines | |||
| 47 | #include <linux/sched.h> | 47 | #include <linux/sched.h> |
| 48 | #include <linux/audit.h> | 48 | #include <linux/audit.h> |
| 49 | #include <linux/vmalloc.h> | 49 | #include <linux/vmalloc.h> |
| 50 | #include <linux/lsm_hooks.h> | ||
| 50 | #include <net/netlabel.h> | 51 | #include <net/netlabel.h> |
| 51 | 52 | ||
| 52 | #include "flask.h" | 53 | #include "flask.h" |
| @@ -2955,7 +2956,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb) · 2902 unmodified lines | |||
| 2955 | struct sidtab *sidtab; | 2956 | struct sidtab *sidtab; |
| 2956 | int rc; | 2957 | int rc; |
| 2957 | struct ocontext *c; | 2958 | struct ocontext *c; |
| 2958 | struct superblock_security_struct *sbsec = sb->s_security; | 2959 | struct superblock_security_struct *sbsec = selinux_superblock(sb); |
| 2959 | const char *fstype = sb->s_type->name; | 2960 | const char *fstype = sb->s_type->name; |
| 2960 | 2961 | ||
| 2961 | if (!selinux_initialized(state)) { | 2962 | if (!selinux_initialized(state)) { |
| @@ -357,6 +357,12 @@ static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc) · 356 unmodified lines | |||
| 357 | return ipc->security + smack_blob_sizes.lbs_ipc; | 357 | return ipc->security + smack_blob_sizes.lbs_ipc; |
| 358 | } | 358 | } |
| 359 | 359 | ||
| 360 | static inline struct superblock_smack *smack_superblock( | ||
| 361 | const struct super_block *superblock) | ||
| 362 | { | ||
| 363 | return superblock->s_security + smack_blob_sizes.lbs_superblock; | ||
| 364 | } | ||
| 365 | |||
| 360 | /* | 366 | /* |
| 361 | * Is the directory transmuting? | 367 | * Is the directory transmuting? |
| 362 | */ | 368 | */ |
| @@ -535,12 +535,7 @@ static int smack_syslog(int typefrom_file) · 534 unmodified lines | |||
| 535 | */ | 535 | */ |
| 536 | static int smack_sb_alloc_security(struct super_block *sb) | 536 | static int smack_sb_alloc_security(struct super_block *sb) |
| 537 | { | 537 | { |
| 538 | struct superblock_smack *sbsp; | 538 | struct superblock_smack *sbsp = smack_superblock(sb); |
| 539 | |||
| 540 | sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL); | ||
| 541 | |||
| 542 | if (sbsp == NULL) | ||
| 543 | return -ENOMEM; | ||
| 544 | 539 | ||
| 545 | sbsp->smk_root = &smack_known_floor; | 540 | sbsp->smk_root = &smack_known_floor; |
| 546 | sbsp->smk_default = &smack_known_floor; | 541 | sbsp->smk_default = &smack_known_floor; |
| @@ -549,22 +544,10 @@ static int smack_sb_alloc_security(struct super_block *sb) · 2 unmodified lines | |||
| 549 | /* | 544 | /* |
| 550 | * SMK_SB_INITIALIZED will be zero from kzalloc. | 545 | * SMK_SB_INITIALIZED will be zero from kzalloc. |
| 551 | */ | 546 | */ |
| 552 | sb->s_security = sbsp; | ||
| 553 | 547 | ||
| 554 | return 0; | 548 | return 0; |
| 555 | } | 549 | } |
| 556 | 550 | ||
| 557 | /** | ||
| 558 | * smack_sb_free_security - free a superblock blob | ||
| 559 | * @sb: the superblock getting the blob | ||
| 560 | * | ||
| 561 | */ | ||
| 562 | static void smack_sb_free_security(struct super_block *sb) | ||
| 563 | { | ||
| 564 | kfree(sb->s_security); | ||
| 565 | sb->s_security = NULL; | ||
| 566 | } | ||
| 567 | |||
| 568 | struct smack_mnt_opts { | 551 | struct smack_mnt_opts { |
| 569 | const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute; | 552 | const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute; |
| 570 | }; | 553 | }; |
| @@ -772,7 +755,7 @@ static int smack_set_mnt_opts(struct super_block *sb, · 201 unmodified lines | |||
| 772 | { | 755 | { |
| 773 | struct dentry *root = sb->s_root; | 756 | struct dentry *root = sb->s_root; |
| 774 | struct inode *inode = d_backing_inode(root); | 757 | struct inode *inode = d_backing_inode(root); |
| 775 | struct superblock_smack *sp = sb->s_security; | 758 | struct superblock_smack *sp = smack_superblock(sb); |
| 776 | struct inode_smack *isp; | 759 | struct inode_smack *isp; |
| 777 | struct smack_known *skp; | 760 | struct smack_known *skp; |
| 778 | struct smack_mnt_opts *opts = mnt_opts; | 761 | struct smack_mnt_opts *opts = mnt_opts; |
| @@ -871,7 +854,7 @@ static int smack_set_mnt_opts(struct super_block *sb, · 92 unmodified lines | |||
| 871 | */ | 854 | */ |
| 872 | static int smack_sb_statfs(struct dentry *dentry) | 855 | static int smack_sb_statfs(struct dentry *dentry) |
| 873 | { | 856 | { |
| 874 | struct superblock_smack *sbp = dentry->d_sb->s_security; | 857 | struct superblock_smack *sbp = smack_superblock(dentry->d_sb); |
| 875 | int rc; | 858 | int rc; |
| 876 | struct smk_audit_info ad; | 859 | struct smk_audit_info ad; |
| 877 | 860 | ||
| @@ -905,7 +888,7 @@ static int smack_bprm_creds_for_exec(struct linux_binprm *bprm) · 27 unmodified lines | |||
| 905 | if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) | 888 | if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) |
| 906 | return 0; | 889 | return 0; |
| 907 | 890 | ||
| 908 | sbsp = inode->i_sb->s_security; | 891 | sbsp = smack_superblock(inode->i_sb); |
| 909 | if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) && | 892 | if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) && |
| 910 | isp->smk_task != sbsp->smk_root) | 893 | isp->smk_task != sbsp->smk_root) |
| 911 | return 0; | 894 | return 0; |
| @@ -1157,7 +1140,7 @@ static int smack_inode_rename(struct inode *old_inode, · 245 unmodified lines | |||
| 1157 | */ | 1140 | */ |
| 1158 | static int smack_inode_permission(struct inode *inode, int mask) | 1141 | static int smack_inode_permission(struct inode *inode, int mask) |
| 1159 | { | 1142 | { |
| 1160 | struct superblock_smack *sbsp = inode->i_sb->s_security; | 1143 | struct superblock_smack *sbsp = smack_superblock(inode->i_sb); |
| 1161 | struct smk_audit_info ad; | 1144 | struct smk_audit_info ad; |
| 1162 | int no_block = mask & MAY_NOT_BLOCK; | 1145 | int no_block = mask & MAY_NOT_BLOCK; |
| 1163 | int rc; | 1146 | int rc; |
| @@ -1400,7 +1383,7 @@ static int smack_inode_removexattr(struct user_namespace *mnt_userns, · 236 unmodified lines | |||
| 1400 | */ | 1383 | */ |
| 1401 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { | 1384 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { |
| 1402 | struct super_block *sbp = dentry->d_sb; | 1385 | struct super_block *sbp = dentry->d_sb; |
| 1403 | struct superblock_smack *sbsp = sbp->s_security; | 1386 | struct superblock_smack *sbsp = smack_superblock(sbp); |
| 1404 | 1387 | ||
| 1405 | isp->smk_inode = sbsp->smk_default; | 1388 | isp->smk_inode = sbsp->smk_default; |
| 1406 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) | 1389 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) |
| @@ -1670,7 +1653,7 @@ static int smack_mmap_file(struct file *file, · 263 unmodified lines | |||
| 1670 | isp = smack_inode(file_inode(file)); | 1653 | isp = smack_inode(file_inode(file)); |
| 1671 | if (isp->smk_mmap == NULL) | 1654 | if (isp->smk_mmap == NULL) |
| 1672 | return 0; | 1655 | return 0; |
| 1673 | sbsp = file_inode(file)->i_sb->s_security; | 1656 | sbsp = smack_superblock(file_inode(file)->i_sb); |
| 1674 | if (sbsp->smk_flags & SMK_SB_UNTRUSTED && | 1657 | if (sbsp->smk_flags & SMK_SB_UNTRUSTED && |
| 1675 | isp->smk_mmap != sbsp->smk_root) | 1658 | isp->smk_mmap != sbsp->smk_root) |
| 1676 | return -EACCES; | 1659 | return -EACCES; |
| @@ -3299,7 +3282,7 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) · 1622 unmodified lines | |||
| 3299 | return; | 3282 | return; |
| 3300 | 3283 | ||
| 3301 | sbp = inode->i_sb; | 3284 | sbp = inode->i_sb; |
| 3302 | sbsp = sbp->s_security; | 3285 | sbsp = smack_superblock(sbp); |
| 3303 | /* | 3286 | /* |
| 3304 | * We're going to use the superblock default label | 3287 | * We're going to use the superblock default label |
| 3305 | * if there's no label on the file. | 3288 | * if there's no label on the file. |
| @@ -4714,6 +4697,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = { · 1408 unmodified lines | |||
| 4714 | .lbs_inode = sizeof(struct inode_smack), | 4697 | .lbs_inode = sizeof(struct inode_smack), |
| 4715 | .lbs_ipc = sizeof(struct smack_known *), | 4698 | .lbs_ipc = sizeof(struct smack_known *), |
| 4716 | .lbs_msg_msg = sizeof(struct smack_known *), | 4699 | .lbs_msg_msg = sizeof(struct smack_known *), |
| 4700 | .lbs_superblock = sizeof(struct superblock_smack), | ||
| 4717 | }; | 4701 | }; |
| 4718 | 4702 | ||
| 4719 | static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { | 4703 | static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { |
| @@ -4725,7 +4709,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { · 5 unmodified lines | |||
| 4725 | LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param), | 4709 | LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param), |
| 4726 | 4710 | ||
| 4727 | LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), | 4711 | LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), |
| 4728 | LSM_HOOK_INIT(sb_free_security, smack_sb_free_security), | ||
| 4729 | LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts), | 4712 | LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts), |
| 4730 | LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts), | 4713 | LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts), |
| 4731 | LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), | 4714 | LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), |