Landlock LSM
Subsystems (24)
Mickaël Salaün
maintainerv28 cover Landlock LSM
Hi,
This patch series fixes a corner-case with non-overlapping access rights coming from different layers. This is now handled in a generic way and verified with new tests. A stricter check is enforced for landlock_add_rule(2) to forbid useless rules. Finally, the previous landlock_enforce_ruleset_self(2) is renamed to landlock_restrict_self(2), which is more consistent.
The SLOC count is 1314 for security/landlock/ and 2484 for tools/testing/selftest/landlock/ . Test coverage for security/landlock/ is 94.7% of lines. The code not covered only deals with internal kernel errors (e.g. memory allocation) and race conditions. This series is being fuzzed by syzkaller, and patches are on their way:
https://github.com/google/syzkaller/pull/2380
The compiled documentation is available here:
https://landlock.io/linux-doc/landlock-v28/userspace-api/landlock.html
This series can be applied on top of v5.11-rc6 . This can be tested with CONFIG_SECURITY_LANDLOCK, CONFIG_SAMPLE_LANDLOCK and by prepending "landlock," to CONFIG_LSM. This patch series can be found in a Git repository here:
https://github.com/landlock-lsm/linux/commits/landlock-v28
This patch series seems ready for upstream and I would really appreciate final reviews.
# Landlock LSM
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].
Previous version:
https://lore.kernel.org/lkml/[email protected]/
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
Casey Schaufler (1):
LSM: Infrastructure management of the superblock
Mickaël Salaün (11):
landlock: Add object management
landlock: Add ruleset and domain management
landlock: Set up the security framework and manage credentials
landlock: Add ptrace restrictions
fs,security: Add sb_delete hook
landlock: Support filesystem access-control
landlock: Add syscall implementations
arch: Wire up Landlock syscalls
selftests/landlock: Add user space tests samples/landlock: Add a sandbox manager example
landlock: Add user and kernel documentation
Documentation/security/index.rst | 1 + Documentation/security/landlock.rst | 79 + Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/landlock.rst | 307 ++ MAINTAINERS | 15 + arch/Kconfig | 7 + arch/alpha/kernel/syscalls/syscall.tbl | 3 + arch/arm/tools/syscall.tbl | 3 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 6 + arch/ia64/kernel/syscalls/syscall.tbl | 3 + arch/m68k/kernel/syscalls/syscall.tbl | 3 + arch/microblaze/kernel/syscalls/syscall.tbl | 3 + arch/mips/kernel/syscalls/syscall_n32.tbl | 3 + arch/mips/kernel/syscalls/syscall_n64.tbl | 3 + arch/mips/kernel/syscalls/syscall_o32.tbl | 3 + arch/parisc/kernel/syscalls/syscall.tbl | 3 + arch/powerpc/kernel/syscalls/syscall.tbl | 3 + arch/s390/kernel/syscalls/syscall.tbl | 3 + arch/sh/kernel/syscalls/syscall.tbl | 3 + arch/sparc/kernel/syscalls/syscall.tbl | 3 + arch/um/Kconfig | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 3 + arch/x86/entry/syscalls/syscall_64.tbl | 3 + arch/xtensa/kernel/syscalls/syscall.tbl | 3 + fs/super.c | 1 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 3 + include/linux/security.h | 4 + include/linux/syscalls.h | 7 + include/uapi/asm-generic/unistd.h | 8 +- include/uapi/linux/landlock.h | 128 + kernel/sys_ni.c | 5 + samples/Kconfig | 7 + samples/Makefile | 1 + samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 13 + samples/landlock/sandboxer.c | 238 ++ security/Kconfig | 11 +- security/Makefile | 2 + security/landlock/Kconfig | 21 + security/landlock/Makefile | 4 + security/landlock/common.h | 20 + security/landlock/cred.c | 46 + security/landlock/cred.h | 58 + security/landlock/fs.c | 627 ++++ security/landlock/fs.h | 56 + security/landlock/limits.h | 21 + security/landlock/object.c | 67 + security/landlock/object.h | 91 + security/landlock/ptrace.c | 120 + security/landlock/ptrace.h | 14 + security/landlock/ruleset.c | 473 +++ security/landlock/ruleset.h | 165 + security/landlock/setup.c | 40 + security/landlock/setup.h | 18 + security/landlock/syscalls.c | 444 +++ security/security.c | 51 +- security/selinux/hooks.c | 58 +- security/selinux/include/objsec.h | 6 + security/selinux/ss/services.c | 3 +- security/smack/smack.h | 6 + security/smack/smack_lsm.c | 35 +- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/landlock/.gitignore | 2 + tools/testing/selftests/landlock/Makefile | 24 + tools/testing/selftests/landlock/base_test.c | 219 ++ tools/testing/selftests/landlock/common.h | 169 ++ tools/testing/selftests/landlock/config | 6 + tools/testing/selftests/landlock/fs_test.c | 2664 +++++++++++++++++ .../testing/selftests/landlock/ptrace_test.c | 314 ++ tools/testing/selftests/landlock/true.c | 5 + 72 files changed, 6668 insertions(+), 77 deletions(-) create mode 100644 Documentation/security/landlock.rst create mode 100644 Documentation/userspace-api/landlock.rst create mode 100644 include/uapi/linux/landlock.h create mode 100644 samples/landlock/.gitignore create mode 100644 samples/landlock/Makefile create mode 100644 samples/landlock/sandboxer.c create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/common.h create mode 100644 security/landlock/cred.c create mode 100644 security/landlock/cred.h create mode 100644 security/landlock/fs.c create mode 100644 security/landlock/fs.h create mode 100644 security/landlock/limits.h create mode 100644 security/landlock/object.c create mode 100644 security/landlock/object.h create mode 100644 security/landlock/ptrace.c create mode 100644 security/landlock/ptrace.h create mode 100644 security/landlock/ruleset.c create mode 100644 security/landlock/ruleset.h create mode 100644 security/landlock/setup.c create mode 100644 security/landlock/setup.h create mode 100644 security/landlock/syscalls.c create mode 100644 tools/testing/selftests/landlock/.gitignore create mode 100644 tools/testing/selftests/landlock/Makefile create mode 100644 tools/testing/selftests/landlock/base_test.c create mode 100644 tools/testing/selftests/landlock/common.h create mode 100644 tools/testing/selftests/landlock/config create mode 100644 tools/testing/selftests/landlock/fs_test.c create mode 100644 tools/testing/selftests/landlock/ptrace_test.c create mode 100644 tools/testing/selftests/landlock/true.c
base-commit: 1048ba83fb1c00cd24172e23e8263972f6b5d9ac
Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 1/12 landlock: Add object management
From: Mickaël Salaün <[email protected]>
A Landlock object enables to identify a kernel object (e.g. an inode). A Landlock rule is a set of access rights allowed on an object. Rules are grouped in rulesets that may be tied to a set of processes (i.e. subjects) to enforce a scoped access-control (i.e. a domain).
Because Landlock's goal is to empower any process (especially unprivileged ones) to sandbox themselves, we cannot rely on a system-wide object identification such as file extended attributes. Indeed, we need innocuous, composable and modular access-controls.
The main challenge with these constraints is to identify kernel objects while this identification is useful (i.e. when a security policy makes use of this object). But this identification data should be freed once no policy is using it. This ephemeral tagging should not and may not be written in the filesystem. We then need to manage the lifetime of a rule according to the lifetime of its objects. To avoid a global lock, this implementation make use of RCU and counters to safely reference objects.
A following commit uses this generic object management for inodes.
---
Changes since v27:
* Update Kconfig for landlock_restrict_self(2).
* Cosmetic fixes: use 80 columns in Kconfig and align Makefile declarations.
Changes since v26:
* Update Kconfig for landlock_enforce_ruleset_self(2).
* Fix spelling.
Changes since v24:
* Fix typo in comment (spotted by Jann Horn).
* Add Reviewed-by: Jann Horn <[email protected]>
Changes since v23:
* Update landlock_create_object() to return error codes instead of NULL. This help error handling in callers.
* When using make oldconfig with a previous configuration already including the CONFIG_LSM variable, no question is asked to update its content. Update the Kconfig help to warn about LSM stacking configuration.
* Constify variable (spotted by Vincent Dagonneau).
Changes since v22:
* Fix spelling (spotted by Jann Horn).
Changes since v21:
* Update Kconfig help.
* Clean up comments.
Changes since v18:
* Account objects to kmemcg.
Changes since v14:
* Simplify the object, rule and ruleset management at the expense of a less aggressive memory freeing (contributed by Jann Horn, with additional modifications):
- Remove object->list aggregating the rules tied to an object.
- Remove landlock_get_object(), landlock_drop_object(), {get,put}_object_cleaner() and landlock_rule_is_disabled().
- Rewrite landlock_put_object() to use a more simple mechanism (no tricky RCU).
- Replace enum landlock_object_type and landlock_release_object() with
landlock_object_underops->release()
- Adjust unions and Sparse annotations.
Cf. https://lore.kernel.org/lkml/CAG48ez21bEn0wL1bbmTiiu8j9jP5iEWtHOwz4tURUJ+ki0ydYw@mail.gmail.com/
* Merge struct landlock_rule into landlock_ruleset_elem to simplify the
rule management.
* Constify variables.
* Improve kernel documentation.
* Cosmetic variable renames.
* Remove the "default" in the Kconfig (suggested by Jann Horn).
* Only use refcount_inc() through getter helpers.
* Update Kconfig description.
Changes since v13:
* New dedicated implementation, removing the need for eBPF.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 10 +++++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 21 +++++++++ security/landlock/Makefile | 3 ++ security/landlock/object.c | 67 ++++++++++++++++++++++++++++ security/landlock/object.h | 91 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 195 insertions(+) create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/object.c create mode 100644 security/landlock/object.h
MAINTAINERS +10 -0
@@ -9936,6 +9936,16 @@ F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c F: net/ipv4/udp_bpf.c +LANDLOCK SECURITY MODULE +M: Mickaël Salaün <[email protected]> +L: [email protected] +S: Supported +W: https://landlock.io +T: git https://github.com/landlock-lsm/linux.git +F: security/landlock/ +K: landlock +K: LANDLOCK + LANTIQ / INTEL Ethernet drivers M: Hauke Mehrtens <[email protected]> L: [email protected]
security/Kconfig +1 -0
@@ -238,6 +238,7 @@ source "security/loadpin/Kconfig" source "security/yama/Kconfig" source "security/safesetid/Kconfig" source "security/lockdown/Kconfig" +source "security/landlock/Kconfig" source "security/integrity/Kconfig"
security/Makefile +2 -0
@@ -13,6 +13,7 @@ subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown subdir-$(CONFIG_BPF_LSM) += bpf +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o
@@ -32,6 +33,7 @@ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUPS) += device_cgroup.o obj-$(CONFIG_BPF_LSM) += bpf/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integrity
security/landlock/Kconfig new file +21 -0
@@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + select SECURITY_PATH + help + Landlock is a safe sandboxing mechanism that enables processes to + restrict themselves (and their future children) by gradually + enforcing tailored access control policies. A security policy is a + set of access rights (e.g. open a file in read-only, make a + directory, etc.) tied to a file hierarchy. Such policy can be + configured and enforced by any processes for themselves thanks to + dedicated system calls: landlock_create_ruleset(), + landlock_add_rule(), and landlock_restrict_self(). + + See Documentation/userspace-api/landlock.rst for further information. + + If you are unsure how to answer this question, answer N. Otherwise, + you should also prepend "landlock," to the content of CONFIG_LSM to + enable Landlock at boot time.
security/landlock/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := object.o
security/landlock/object.c new file +67 -0
67 lines not on this page · raw message
security/landlock/object.h new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.30.0
Re: [PATCH v28 01/12] landlock: Add object management
Quoted 21 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
A Landlock object enables to identify a kernel object (e.g. an inode). A Landlock rule is a set of access rights allowed on an object. Rules are grouped in rulesets that may be tied to a set of processes (i.e. subjects) to enforce a scoped access-control (i.e. a domain).
Because Landlock's goal is to empower any process (especially unprivileged ones) to sandbox themselves, we cannot rely on a system-wide object identification such as file extended attributes. Indeed, we need innocuous, composable and modular access-controls.
The main challenge with these constraints is to identify kernel objects while this identification is useful (i.e. when a security policy makes use of this object). But this identification data should be freed once no policy is using it. This ephemeral tagging should not and may not be written in the filesystem. We then need to manage the lifetime of a rule according to the lifetime of its objects. To avoid a global lock, this implementation make use of RCU and counters to safely reference objects.
Quoted 5 lines
A following commit uses this generic object management for inodes.
Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
Just a few suggestions for the description below.
Quoted 136 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v27:
* UpdateKconfigforlandlock_restrict_self(2).
* Cosmetic fixes: use 80 columns inKconfigand alignMakefiledeclarations.Changes since v26:
* UpdateKconfigforlandlock_enforce_ruleset_self(2).
* Fix spelling.Changes since v24:
* Fix typo in comment (spotted by Jann Horn).
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v23:
* Updatelandlock_create_object()to return error codes instead of NULL. This help error handling in callers.
* When using make oldconfig with a previous configuration already including theCONFIG_LSMvariable, no question is asked to update its content. Update theKconfighelp to warn about LSM stacking configuration.
* Constify variable (spotted by Vincent Dagonneau).Changes since v22:
* Fix spelling (spotted by Jann Horn).Changes since v21:
* UpdateKconfighelp.
* Clean up comments.Changes since v18:
* Account objects to kmemcg.Changes since v14:
* Simplify the object, rule and ruleset management at the expense of a less aggressive memory freeing (contributed by Jann Horn, with additional modifications):
- Removeobject->listaggregating the rules tied to an object.
- Removelandlock_get_object(),landlock_drop_object(), {get,put}_object_cleaner() andlandlock_rule_is_disabled().
- Rewritelandlock_put_object()to use a more simple mechanism (no tricky RCU).
- Replace enumlandlock_object_typeandlandlock_release_object()withlandlock_object_underops->release()- Adjust unions and Sparse annotations.
Cf. https://lore.kernel.org/lkml/CAG48ez21bEn0wL1bbmTiiu8j9jP5iEWtHOwz4tURUJ+ki0ydYw@mail.gmail.com/
* Merge structlandlock_ruleintolandlock_ruleset_elemto simplify the
rule management.
* Constify variables.
* Improve kernel documentation.
* Cosmetic variable renames.
* Remove the "default" in theKconfig(suggested by Jann Horn).
* Only userefcount_inc()through getter helpers.
* UpdateKconfigdescription.Changes since v13:
* New dedicated implementation, removing the need for eBPF.Previous changes:
https://lore.kernel.org/lkml/[email protected]/--- MAINTAINERS | 10 +++++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 21 +++++++++ security/landlock/Makefile | 3 ++ security/landlock/object.c | 67 ++++++++++++++++++++++++++++ security/landlock/object.h | 91 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 195 insertions(+) create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/object.c create mode 100644 security/landlock/object.hMAINTAINERS +10
-0@@ -9936,6 +9936,16 @@ F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c F: net/ipv4/udp_bpf.c +LANDLOCK SECURITY MODULE +M: Mickaël Salaün <[email protected]> +L: [email protected] +S: Supported +W: https://landlock.io +T: git https://github.com/landlock-lsm/linux.git +F: security/landlock/ +K: landlock +K: LANDLOCK + LANTIQ / INTEL Ethernet drivers M: Hauke Mehrtens <[email protected]> L: [email protected]security/Kconfig +1
-0@@ -238,6 +238,7 @@ source "security/loadpin/Kconfig" source "security/yama/Kconfig" source "security/safesetid/Kconfig" source "security/lockdown/Kconfig" +source "security/landlock/Kconfig" source "security/integrity/Kconfig"security/Makefile +2
-0@@ -13,6 +13,7 @@ subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown subdir-$(CONFIG_BPF_LSM) += bpf +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o@@ -32,6 +33,7 @@ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUPS) += device_cgroup.o obj-$(CONFIG_BPF_LSM) += bpf/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integritysecurity/landlock/Kconfig new file +0
-0
security/landlock/Kconfig line 8
Quoted 8 lines
security/landlock/Kconfig new file +8
-0@@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + select SECURITY_PATH + help + Landlock is a safe sandboxing mechanism that enables processes to
"safe" probably doesn't need to be there :)
security/landlock/Kconfig line 10
Quoted 2 lines
security/landlock/Kconfig new file +2
-0+ restrict themselves (and their future children) by gradually + enforcing tailored access control policies. A security policy is a
You're redefining "security policy" which could be confusing. How about saying "a landlock security policy is a..."?
security/landlock/Kconfig line 13
Quoted 3 lines
security/landlock/Kconfig new file +3
-0+ set of access rights (e.g. open a file in read-only, make a + directory, etc.) tied to a file hierarchy. Such policy can be + configured and enforced by any processes for themselves thanks to
s/thanks to/using the/ ?
Quoted 189 lines
security/landlock/Kconfig new file +8
-0+ dedicated system calls: landlock_create_ruleset(), + landlock_add_rule(), and landlock_restrict_self(). + + See Documentation/userspace-api/landlock.rst for further information. + + If you are unsure how to answer this question, answer N. Otherwise, + you should also prepend "landlock," to the content of CONFIG_LSM to + enable Landlock at boot time.security/landlock/Makefile new file +3
-0@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := object.osecurity/landlock/object.c new file +67
-0@@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Object management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/bug.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/kernel.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> + +#include "object.h" + +struct landlock_object *landlock_create_object ( +const struct landlock_object_underops *const underops, +void *const underobj) +{ +struct landlock_object *new_object; + +if (WARN_ON_ONCE (!underops || !underobj)) +return ERR_PTR (-ENOENT ); + new_object =kzalloc (sizeof (*new_object),GFP_KERNEL_ACCOUNT ); +if (!new_object) +return ERR_PTR (-ENOMEM ); +refcount_set (&new_object->usage,1 ); +spin_lock_init (&new_object->lock); + new_object->underops = underops; + new_object->underobj = underobj; +return new_object; +} + +/* +* The caller must own the object (i.e. thanks to object->usage) to safely put +* it. +*/ +void landlock_put_object (struct landlock_object *const object) +{ +/* +* The call to @object->underops->release(object) might sleep, e.g. +* because of iput(). +*/ +might_sleep (); +if (!object) +return ; + +/* +* If the @object's refcount cannot drop to zero, we can just decrement +* the refcount without holding a lock. Otherwise, the decrement must +* happen under @object->lock for synchronization with things like +* get_inode_object(). +*/ +if (refcount_dec_and_lock (&object->usage, &object->lock)) { +__acquire (&object->lock); +/* +* With @object->lock initially held, remove the reference from +* @object->underobj to @object (if it still exists). +*/ + object->underops->release (object); +kfree_rcu (object, rcu_free); + } +}security/landlock/object.h new file +91
-0@@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Object management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_OBJECT_H +#define _SECURITY_LANDLOCK_OBJECT_H + +#include <linux/compiler_types.h> +#include <linux/refcount.h> +#include <linux/spinlock.h> + +struct landlock_object ; + +/** +* struct landlock_object_underops - Operations on an underlying object +*/ +struct landlock_object_underops { +/** +* @release: Releases the underlying object (e.g. iput() for an inode). +*/ + void (*release)(struct landlock_object *const object) + __releases(object->lock); +}; + +/** +* struct landlock_object - Security blob tied to a kernel object +* +* The goal of this structure is to enable to tie a set of ephemeral access +* rights (pertaining to different domains) to a kernel object (e.g an inode) +* in a safe way. This implies to handle concurrent use and modification. +* +* The lifetime of a &struct landlock_object depends of the rules referring to +* it. +*/ +struct landlock_object { +/** +* @usage: This counter is used to tie an object to the rules matching +* it or to keep it alive while adding a new rule. If this counter +* reaches zero, this struct must not be modified, but this counter can +* still be read from within an RCU read-side critical section. When +* adding a new rule to an object with a usage counter of zero, we must +* wait until the pointer to this object is set to NULL (or recycled). +*/ +refcount_t usage; +/** +* @lock: Guards against concurrent modifications. This lock must be +* held from the time @usage drops to zero until any weak references +* from @underobj to this object have been cleaned up. +* +* Lock ordering: inode->i_lock nests inside this. +*/ +spinlock_t lock; +/** +* @underobj: Used when cleaning up an object and to mark an object as +* tied to its underlying kernel structure. This pointer is protected +* by @lock. Cf. landlock_release_inodes() and release_inode(). +*/ +void *underobj; +union { +/** +* @rcu_free: Enables lockless use of @usage, @lock and +* @underobj from within an RCU read-side critical section. +* @rcu_free and @underops are only used by +* landlock_put_object(). +*/ +struct rcu_head rcu_free; +/** +* @underops: Enables landlock_put_object() to release the +* underlying object (e.g. inode). +*/ +const struct landlock_object_underops *underops; + }; +}; + +struct landlock_object *landlock_create_object ( +const struct landlock_object_underops *const underops, +void *const underobj); + +void landlock_put_object (struct landlock_object *const object); + +static inline void landlock_get_object (struct landlock_object *const object) +{ +if (object) +refcount_inc (&object->usage); +} + +#endif /* _SECURITY_LANDLOCK_OBJECT_H */ Signature · 1 line
2.30.0
Mickaël Salaün
maintainerRe: [PATCH v28 01/12] landlock: Add object management
Quoted 185 lines · Serge E. Hallyn
Quoted 21 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
A Landlock object enables to identify a kernel object (e.g. an inode). A Landlock rule is a set of access rights allowed on an object. Rules are grouped in rulesets that may be tied to a set of processes (i.e. subjects) to enforce a scoped access-control (i.e. a domain).
Because Landlock's goal is to empower any process (especially unprivileged ones) to sandbox themselves, we cannot rely on a system-wide object identification such as file extended attributes. Indeed, we need innocuous, composable and modular access-controls.
The main challenge with these constraints is to identify kernel objects while this identification is useful (i.e. when a security policy makes use of this object). But this identification data should be freed once no policy is using it. This ephemeral tagging should not and may not be written in the filesystem. We then need to manage the lifetime of a rule according to the lifetime of its objects. To avoid a global lock, this implementation make use of RCU and counters to safely reference objects.
Quoted 5 lines
A following commit uses this generic object management for inodes.
Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Acked-by Serge Hallyn <[email protected]>Just a few suggestions for the description below.
Quoted 136 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v27:
* UpdateKconfigforlandlock_restrict_self(2).
* Cosmetic fixes: use 80 columns inKconfigand alignMakefiledeclarations.Changes since v26:
* UpdateKconfigforlandlock_enforce_ruleset_self(2).
* Fix spelling.Changes since v24:
* Fix typo in comment (spotted by Jann Horn).
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v23:
* Updatelandlock_create_object()to return error codes instead of NULL. This help error handling in callers.
* When using make oldconfig with a previous configuration already including theCONFIG_LSMvariable, no question is asked to update its content. Update theKconfighelp to warn about LSM stacking configuration.
* Constify variable (spotted by Vincent Dagonneau).Changes since v22:
* Fix spelling (spotted by Jann Horn).Changes since v21:
* UpdateKconfighelp.
* Clean up comments.Changes since v18:
* Account objects to kmemcg.Changes since v14:
* Simplify the object, rule and ruleset management at the expense of a less aggressive memory freeing (contributed by Jann Horn, with additional modifications):
- Removeobject->listaggregating the rules tied to an object.
- Removelandlock_get_object(),landlock_drop_object(), {get,put}_object_cleaner() andlandlock_rule_is_disabled().
- Rewritelandlock_put_object()to use a more simple mechanism (no tricky RCU).
- Replace enumlandlock_object_typeandlandlock_release_object()withlandlock_object_underops->release()- Adjust unions and Sparse annotations.
Cf. https://lore.kernel.org/lkml/CAG48ez21bEn0wL1bbmTiiu8j9jP5iEWtHOwz4tURUJ+ki0ydYw@mail.gmail.com/
* Merge structlandlock_ruleintolandlock_ruleset_elemto simplify the
rule management.
* Constify variables.
* Improve kernel documentation.
* Cosmetic variable renames.
* Remove the "default" in theKconfig(suggested by Jann Horn).
* Only userefcount_inc()through getter helpers.
* UpdateKconfigdescription.Changes since v13:
* New dedicated implementation, removing the need for eBPF.Previous changes:
https://lore.kernel.org/lkml/[email protected]/--- MAINTAINERS | 10 +++++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 21 +++++++++ security/landlock/Makefile | 3 ++ security/landlock/object.c | 67 ++++++++++++++++++++++++++++ security/landlock/object.h | 91 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 195 insertions(+) create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/object.c create mode 100644 security/landlock/object.hMAINTAINERS +10
-0@@ -9936,6 +9936,16 @@ F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c F: net/ipv4/udp_bpf.c +LANDLOCK SECURITY MODULE +M: Mickaël Salaün <[email protected]> +L: [email protected] +S: Supported +W: https://landlock.io +T: git https://github.com/landlock-lsm/linux.git +F: security/landlock/ +K: landlock +K: LANDLOCK + LANTIQ / INTEL Ethernet drivers M: Hauke Mehrtens <[email protected]> L: [email protected]security/Kconfig +1
-0@@ -238,6 +238,7 @@ source "security/loadpin/Kconfig" source "security/yama/Kconfig" source "security/safesetid/Kconfig" source "security/lockdown/Kconfig" +source "security/landlock/Kconfig" source "security/integrity/Kconfig"security/Makefile +2
-0@@ -13,6 +13,7 @@ subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown subdir-$(CONFIG_BPF_LSM) += bpf +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o@@ -32,6 +33,7 @@ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUPS) += device_cgroup.o obj-$(CONFIG_BPF_LSM) += bpf/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integritysecurity/landlock/Kconfig new file +0
-0security/landlock/Kconfig line 8
Quoted 8 lines
security/landlock/Kconfig new file +8
-0@@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + select SECURITY_PATH + help + Landlock is a safe sandboxing mechanism that enables processes to"safe" probably doesn't need to be there :)
security/landlock/Kconfig line 10
Quoted 2 lines
security/landlock/Kconfig new file +2
-0+ restrict themselves (and their future children) by gradually + enforcing tailored access control policies. A security policy is aYou're redefining "security policy" which could be confusing. How about saying "a landlock security policy is a..."?
Quoted 5 lines
security/landlock/Kconfig line 13
Quoted 3 lines
security/landlock/Kconfig new file +3
-0+ set of access rights (e.g. open a file in read-only, make a + directory, etc.) tied to a file hierarchy. Such policy can be + configured and enforced by any processes for themselves thanks tos/thanks to/using the/ ?
OK for these three modifications. Thanks!
Quoted 190 lines
Quoted 190 lines
security/landlock/Kconfig new file +8
-0+ dedicated system calls: landlock_create_ruleset(), + landlock_add_rule(), and landlock_restrict_self(). + + See Documentation/userspace-api/landlock.rst for further information. + + If you are unsure how to answer this question, answer N. Otherwise, + you should also prepend "landlock," to the content of CONFIG_LSM to + enable Landlock at boot time.security/landlock/Makefile new file +3
-0@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := object.osecurity/landlock/object.c new file +67
-0@@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Object management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/bug.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/kernel.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> + +#include "object.h" + +struct landlock_object *landlock_create_object ( +const struct landlock_object_underops *const underops, +void *const underobj) +{ +struct landlock_object *new_object; + +if (WARN_ON_ONCE (!underops || !underobj)) +return ERR_PTR (-ENOENT ); + new_object =kzalloc (sizeof (*new_object),GFP_KERNEL_ACCOUNT ); +if (!new_object) +return ERR_PTR (-ENOMEM ); +refcount_set (&new_object->usage,1 ); +spin_lock_init (&new_object->lock); + new_object->underops = underops; + new_object->underobj = underobj; +return new_object; +} + +/* +* The caller must own the object (i.e. thanks to object->usage) to safely put +* it. +*/ +void landlock_put_object (struct landlock_object *const object) +{ +/* +* The call to @object->underops->release(object) might sleep, e.g. +* because of iput(). +*/ +might_sleep (); +if (!object) +return ; + +/* +* If the @object's refcount cannot drop to zero, we can just decrement +* the refcount without holding a lock. Otherwise, the decrement must +* happen under @object->lock for synchronization with things like +* get_inode_object(). +*/ +if (refcount_dec_and_lock (&object->usage, &object->lock)) { +__acquire (&object->lock); +/* +* With @object->lock initially held, remove the reference from +* @object->underobj to @object (if it still exists). +*/ + object->underops->release (object); +kfree_rcu (object, rcu_free); + } +}security/landlock/object.h new file +91
-0@@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Object management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_OBJECT_H +#define _SECURITY_LANDLOCK_OBJECT_H + +#include <linux/compiler_types.h> +#include <linux/refcount.h> +#include <linux/spinlock.h> + +struct landlock_object ; + +/** +* struct landlock_object_underops - Operations on an underlying object +*/ +struct landlock_object_underops { +/** +* @release: Releases the underlying object (e.g. iput() for an inode). +*/ + void (*release)(struct landlock_object *const object) + __releases(object->lock); +}; + +/** +* struct landlock_object - Security blob tied to a kernel object +* +* The goal of this structure is to enable to tie a set of ephemeral access +* rights (pertaining to different domains) to a kernel object (e.g an inode) +* in a safe way. This implies to handle concurrent use and modification. +* +* The lifetime of a &struct landlock_object depends of the rules referring to +* it. +*/ +struct landlock_object { +/** +* @usage: This counter is used to tie an object to the rules matching +* it or to keep it alive while adding a new rule. If this counter +* reaches zero, this struct must not be modified, but this counter can +* still be read from within an RCU read-side critical section. When +* adding a new rule to an object with a usage counter of zero, we must +* wait until the pointer to this object is set to NULL (or recycled). +*/ +refcount_t usage; +/** +* @lock: Guards against concurrent modifications. This lock must be +* held from the time @usage drops to zero until any weak references +* from @underobj to this object have been cleaned up. +* +* Lock ordering: inode->i_lock nests inside this. +*/ +spinlock_t lock; +/** +* @underobj: Used when cleaning up an object and to mark an object as +* tied to its underlying kernel structure. This pointer is protected +* by @lock. Cf. landlock_release_inodes() and release_inode(). +*/ +void *underobj; +union { +/** +* @rcu_free: Enables lockless use of @usage, @lock and +* @underobj from within an RCU read-side critical section. +* @rcu_free and @underops are only used by +* landlock_put_object(). +*/ +struct rcu_head rcu_free; +/** +* @underops: Enables landlock_put_object() to release the +* underlying object (e.g. inode). +*/ +const struct landlock_object_underops *underops; + }; +}; + +struct landlock_object *landlock_create_object ( +const struct landlock_object_underops *const underops, +void *const underobj); + +void landlock_put_object (struct landlock_object *const object); + +static inline void landlock_get_object (struct landlock_object *const object) +{ +if (object) +refcount_inc (&object->usage); +} + +#endif /* _SECURITY_LANDLOCK_OBJECT_H */ Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 2/12 landlock: Add ruleset and domain management
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
A Landlock ruleset is mainly a red-black tree with Landlock rules as nodes. This enables quick update and lookup to match a requested access, e.g. to a file. A ruleset is usable through a dedicated file descriptor (cf. following commit implementing syscalls) which enables a process to create and populate a ruleset with new rules.
A domain is a ruleset tied to a set of processes. This group of rules defines the security policy enforced on these processes and their future children. A domain can transition to a new domain which is the intersection of all its constraints and those of a ruleset provided by the current process. This modification only impact the current process. This means that a process can only gain more constraints (i.e. lose accesses) over time.
---
Changes since v27:
* Fix domains with layers of non-overlapping access rights.
* Add stricter limit checks (same semantic).
* Change the grow direction of a rule layer stack to make it the same as the new ruleset fs_access_masks stack (cosmetic change).
* Cosmetic fix for a comment block.
Changes since v26:
* Fix spelling.
Changes since v25:
* Add build-time checks for the num_layers and num_rules variables according to LANDLOCK_MAX_NUM_LAYERS and LANDLOCK_MAX_NUM_RULES, and move these limits to a dedicated file.
* Cosmetic variable renames.
Changes since v24:
* Update struct landlock_rule with a layer stack. This reverts "Always intersect access rights" from v24 and also adds the ability to tie access rights with their policy layer. As noted by Jann Horn, always intersecting access rights made some use cases uselessly more difficult to handle in user space. Thanks to this new stack, we still have a deterministic policy behavior whatever their level in the stack of policies, while using a "union" of accesses when building a ruleset. The implementation use a FAM to keep the access checks quick and memory efficient (4 bytes per layer per inode). Update insert_rule() accordingly.
Changes since v23:
* Always intersect access rights. Following the filesystem change logic, make ruleset updates more consistent by always intersecting access rights (boolean AND) instead of combining them (boolean OR) for the same layer. This defensive approach could also help avoid user space to inadvertently allow multiple access rights for the same object (e.g. write and execute access on a path hierarchy) instead of dealing with such inconsistency. This can happen when there is no deduplication of objects (e.g. paths and underlying inodes) whereas they get different access rights with landlock_add_rule(2).
* Add extra checks to make sure that:
- there is always an (allocated) object in each used rules;
- when updating a ruleset with a new rule (i.e. not merging two rulesets), the ruleset doesn't contain multiple layers.
* Hide merge parameter from the public landlock_insert_rule() API. This helps avoid misuse of this function.
* Replace a remaining hardcoded 1 with SINGLE_DEPTH_NESTING.
Changes since v22:
* Explicitely use RB_ROOT and SINGLE_DEPTH_NESTING (suggested by Jann Horn).
* Improve comments and fix spelling (suggested by Jann Horn).
Changes since v21:
* Add and clean up comments.
Changes since v18:
* Account rulesets to kmemcg.
* Remove struct holes.
* Cosmetic changes.
Changes since v17:
* Move include/uapi/linux/landlock.h and _LANDLOCK_ACCESS_FS_* to a following patch.
Changes since v16:
* Allow enforcement of empty ruleset, which enables deny-all policies.
Changes since v15:
* Replace layer_levels and layer_depth with a bitfield of layers, cf. filesystem commit.
* Rename the LANDLOCK_ACCESS_FS_{UNLINK,RMDIR} with LANDLOCK_ACCESS_FS_REMOVE_{FILE,DIR} because it makes sense to use them for the action of renaming a file or a directory, which may lead to the removal of the source file or directory. Removes the LANDLOCK_ACCESS_FS_{LINK_TO,RENAME_FROM,RENAME_TO} which are now replaced with LANDLOCK_ACCESS_FS_REMOVE_{FILE,DIR} and LANDLOCK_ACCESS_FS_MAKE_* .
* Update the documentation accordingly and highlight how the access rights are taken into account.
* Change nb_rules from atomic_t to u32 because it is not use anymore by show_fdinfo().
* Add safeguard for level variables types.
* Check max number of rules.
* Replace struct landlock_access (self and beneath bitfields) with one bitfield.
* Remove useless variable.
* Add comments.
Changes since v14:
* Simplify the object, rule and ruleset management at the expense of a
less aggressive memory freeing (contributed by Jann Horn, with
additional modifications):
- Make a domain immutable (remove the opportunistic cleaning).
- Remove RCU pointers.
- Merge struct landlock_ref and struct landlock_ruleset_elem into
landlock_rule: get ride of rule's RCU.
- Adjust union.
- Remove the landlock_insert_rule() check about a new object with the
same address as a previously disabled one, because it is not
possible to disable a rule anymore.
Cf. https://lore.kernel.org/lkml/CAG48ez21bEn0wL1bbmTiiu8j9jP5iEWtHOwz4tURUJ+ki0ydYw@mail.gmail.com/
* Fix nested domains by implementing a notion of layer level and depth:
- Update landlock_insert_rule() to manage such layers.
- Add an inherit_ruleset() helper to properly create a new domain.
- Rename landlock_find_access() to landlock_find_rule() and return a
full rule reference.
- Add a layer_level and a layer_depth fields to struct landlock_rule.
- Add a top_layer_level field to struct landlock_ruleset.
* Remove access rights that may be required for FD-only requests:
truncate, getattr, lock, chmod, chown, chgrp, ioctl. This will be
handle in a future evolution of Landlock, but right now the goal is to
lighten the code to ease review.
* Remove LANDLOCK_ACCESS_FS_OPEN and rename
LANDLOCK_ACCESS_FS_{READ,WRITE} with a FILE suffix.
* Rename LANDLOCK_ACCESS_FS_READDIR to match the *_FILE pattern.
* Remove LANDLOCK_ACCESS_FS_MAP which was useless.
* Fix memory leak in put_hierarchy() (reported by Jann Horn).
* Fix user-after-free and rename free_ruleset() (reported by Jann Horn).
* Replace the for loops with rbtree_postorder_for_each_entry_safe().
* Constify variables.
* Only use refcount_inc() through getter helpers.
* Change Landlock_insert_ruleset_access() to
Landlock_insert_ruleset_rule().
* Rename landlock_put_ruleset_enqueue() to landlock_put_ruleset_deferred().
* Improve kernel documentation and add a warning about the unhandled
access/syscall families.
* Move ABI check to syscall.c .
Changes since v13:
* New implementation, inspired by the previous inode eBPF map, but agnostic to the underlying kernel object.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- security/landlock/Makefile | 2 +- security/landlock/limits.h | 17 ++ security/landlock/ruleset.c | 469 ++++++++++++++++++++++++++++++++++++ security/landlock/ruleset.h | 165 +++++++++++++ 4 files changed, 652 insertions(+), 1 deletion(-) create mode 100644 security/landlock/limits.h create mode 100644 security/landlock/ruleset.c create mode 100644 security/landlock/ruleset.h
security/landlock/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := object.o +landlock-y := object.o ruleset.o
security/landlock/limits.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Limits for different components +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_LIMITS_H +#define _SECURITY_LANDLOCK_LIMITS_H + +#include <linux/limits.h> + +#define LANDLOCK_MAX_NUM_LAYERS 64 +#define LANDLOCK_MAX_NUM_RULES U32_MAX + +#endif /* _SECURITY_LANDLOCK_LIMITS_H */
security/landlock/ruleset.c new file +469 -0
469 lines not on this page · raw message
security/landlock/ruleset.h new file +165 -0
165 lines not on this page · raw message
Signature · 1 line
2.30.0
Re: [PATCH v28 02/12] landlock: Add ruleset and domain management
Quoted 16 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
A Landlock ruleset is mainly a red-black tree with Landlock rules as nodes. This enables quick update and lookup to match a requested access, e.g. to a file. A ruleset is usable through a dedicated file descriptor (cf. following commit implementing syscalls) which enables a process to create and populate a ruleset with new rules.
A domain is a ruleset tied to a set of processes. This group of rules defines the security policy enforced on these processes and their future children. A domain can transition to a new domain which is the intersection of all its constraints and those of a ruleset provided by the current process. This modification only impact the current process. This means that a process can only gain more constraints (i.e. lose accesses) over time.
Quoted 4 lines
Cc James Morris <[email protected]>Cc Jann Horn <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
Quoted 826 lines
Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v27:
* Fix domains with layers of non-overlapping access rights.
* Add stricter limit checks (same semantic).
* Change the grow direction of a rule layer stack to make it the same as the new rulesetfs_access_masksstack (cosmetic change).
* Cosmetic fix for a comment block.Changes since v26:
* Fix spelling.Changes since v25:
* Add build-time checks for thenum_layersandnum_rulesvariables according toLANDLOCK_MAX_NUM_LAYERSandLANDLOCK_MAX_NUM_RULES, and move these limits to a dedicated file.
* Cosmetic variable renames.Changes since v24:
* Update structlandlock_rulewith a layer stack. This reverts "Always intersect access rights" from v24 and also adds the ability to tie access rights with their policy layer. As noted by Jann Horn, always intersecting access rights made some use cases uselessly more difficult to handle in user space. Thanks to this new stack, we still have a deterministic policy behavior whatever their level in the stack of policies, while using a "union" of accesses when building a ruleset. The implementation use a FAM to keep the access checks quick and memory efficient (4 bytes per layer per inode). Updateinsert_rule()accordingly.Changes since v23:
* Always intersect access rights. Following the filesystem change logic, make ruleset updates more consistent by always intersecting access rights (boolean AND) instead of combining them (boolean OR) for the same layer. This defensive approach could also help avoid user space to inadvertently allow multiple access rights for the same object (e.g. write and execute access on a path hierarchy) instead of dealing with such inconsistency. This can happen when there is no deduplication of objects (e.g. paths and underlying inodes) whereas they get different access rights withlandlock_add_rule(2).
* Add extra checks to make sure that:
- there is always an (allocated) object in each used rules;
- when updating a ruleset with a new rule (i.e. not merging two rulesets), the ruleset doesn't contain multiple layers.
* Hide merge parameter from the publiclandlock_insert_rule()API. This helps avoid misuse of this function.
* Replace a remaining hardcoded 1 withSINGLE_DEPTH_NESTING.Changes since v22:
* Explicitely useRB_ROOTandSINGLE_DEPTH_NESTING(suggested by Jann Horn).
* Improve comments and fix spelling (suggested by Jann Horn).Changes since v21:
* Add and clean up comments.Changes since v18:
* Account rulesets to kmemcg.
* Remove struct holes.
* Cosmetic changes.Changes since v17:
* Moveinclude/uapi/linux/landlock.hand _LANDLOCK_ACCESS_FS_* to a following patch.Changes since v16:
* Allow enforcement of empty ruleset, which enables deny-all policies.Changes since v15:
* Replacelayer_levelsandlayer_depthwith a bitfield of layers, cf. filesystem commit.
* Rename the LANDLOCK_ACCESS_FS_{UNLINK,RMDIR} with LANDLOCK_ACCESS_FS_REMOVE_{FILE,DIR} because it makes sense to use them for the action of renaming a file or a directory, which may lead to the removal of the source file or directory. Removes the LANDLOCK_ACCESS_FS_{LINK_TO,RENAME_FROM,RENAME_TO} which are now replaced with LANDLOCK_ACCESS_FS_REMOVE_{FILE,DIR} and LANDLOCK_ACCESS_FS_MAKE_* .
* Update the documentation accordingly and highlight how the access rights are taken into account.
* Changenb_rulesfromatomic_tto u32 because it is not use anymore byshow_fdinfo().
* Add safeguard for level variables types.
* Check max number of rules.
* Replace structlandlock_access(self and beneath bitfields) with one bitfield.
* Remove useless variable.
* Add comments.Changes since v14:
* Simplify the object, rule and ruleset management at the expense of a
less aggressive memory freeing (contributed by Jann Horn, with
additional modifications):
- Make a domain immutable (remove the opportunistic cleaning).
- Remove RCU pointers.
- Merge structlandlock_refand structlandlock_ruleset_eleminto
landlock_rule: get ride of rule's RCU.
- Adjust union.
- Remove thelandlock_insert_rule()check about a new object with the
same address as a previously disabled one, because it is not
possible to disable a rule anymore.
Cf. https://lore.kernel.org/lkml/CAG48ez21bEn0wL1bbmTiiu8j9jP5iEWtHOwz4tURUJ+ki0ydYw@mail.gmail.com/
* Fix nested domains by implementing a notion of layer level and depth:
- Updatelandlock_insert_rule()to manage such layers.
- Add aninherit_ruleset()helper to properly create a new domain.
- Renamelandlock_find_access()tolandlock_find_rule()and return a
full rule reference.
- Add alayer_leveland alayer_depthfields to structlandlock_rule.
- Add atop_layer_levelfield to structlandlock_ruleset.
* Remove access rights that may be required for FD-only requests:
truncate, getattr, lock, chmod, chown, chgrp, ioctl. This will be
handle in a future evolution of Landlock, but right now the goal is to
lighten the code to ease review.
* RemoveLANDLOCK_ACCESS_FS_OPENand rename
LANDLOCK_ACCESS_FS_{READ,WRITE} with a FILE suffix.
* RenameLANDLOCK_ACCESS_FS_READDIRto match the*_FILEpattern.
* RemoveLANDLOCK_ACCESS_FS_MAPwhich was useless.
* Fix memory leak input_hierarchy()(reported by Jann Horn).
* Fix user-after-free and renamefree_ruleset()(reported by Jann Horn).
* Replace the for loops withrbtree_postorder_for_each_entry_safe().
* Constify variables.
* Only userefcount_inc()through getter helpers.
* ChangeLandlock_insert_ruleset_access()to
Landlock_insert_ruleset_rule().
* Renamelandlock_put_ruleset_enqueue()tolandlock_put_ruleset_deferred().
* Improve kernel documentation and add a warning about the unhandled
access/syscall families.
* Move ABI check tosyscall.c.Changes since v13:
* New implementation, inspired by the previous inode eBPF map, but agnostic to the underlying kernel object.Previous changes:
https://lore.kernel.org/lkml/[email protected]/--- security/landlock/Makefile | 2 +- security/landlock/limits.h | 17 ++ security/landlock/ruleset.c | 469 ++++++++++++++++++++++++++++++++++++ security/landlock/ruleset.h | 165 +++++++++++++ 4 files changed, 652 insertions(+), 1 deletion(-) create mode 100644 security/landlock/limits.h create mode 100644 security/landlock/ruleset.c create mode 100644 security/landlock/ruleset.hsecurity/landlock/Makefile +1
-1@@ -1,3 +1,3 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := object.o +landlock-y := object.o ruleset.osecurity/landlock/limits.h new file +17
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Limits for different components +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_LIMITS_H +#define _SECURITY_LANDLOCK_LIMITS_H + +#include <linux/limits.h> + +#define LANDLOCK_MAX_NUM_LAYERS 64 +#define LANDLOCK_MAX_NUM_RULES U32_MAX + +#endif /* _SECURITY_LANDLOCK_LIMITS_H */ security/landlock/ruleset.c new file +469
-0@@ -0,0 +1,469 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Ruleset management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/bits.h> +#include <linux/bug.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/lockdep.h> +#include <linux/overflow.h> +#include <linux/rbtree.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/workqueue.h> + +#include "limits.h" +#include "object.h" +#include "ruleset.h" + +static struct landlock_ruleset *create_ruleset (const u32 num_layers) +{ +struct landlock_ruleset *new_ruleset; + + new_ruleset =kzalloc (struct_size (new_ruleset, fs_access_masks, + num_layers),GFP_KERNEL_ACCOUNT ); +if (!new_ruleset) +return ERR_PTR (-ENOMEM ); +refcount_set (&new_ruleset->usage,1 ); +mutex_init (&new_ruleset->lock); + new_ruleset->root =RB_ROOT ; + new_ruleset->num_layers = num_layers; +/* +* hierarchy = NULL +* num_rules = 0 +* fs_access_masks[] = 0 +*/ +return new_ruleset; +} + +struct landlock_ruleset *landlock_create_ruleset (const u32 fs_access_mask) +{ +struct landlock_ruleset *new_ruleset; + +/* Informs about useless ruleset. */ +if (!fs_access_mask) +return ERR_PTR (-ENOMSG ); + new_ruleset =create_ruleset (1 ); +if (!IS_ERR (new_ruleset)) + new_ruleset->fs_access_masks[0 ] = fs_access_mask; +return new_ruleset; +} + +static void build_check_rule (void ) +{ +const struct landlock_rule rule = { + .num_layers = ~0 , + }; + +BUILD_BUG_ON (rule.num_layers <LANDLOCK_MAX_NUM_LAYERS ); +} + +static struct landlock_rule *create_rule ( +struct landlock_object *const object, +const struct landlock_layer (*const layers)[], +const u32 num_layers, +const struct landlock_layer *const new_layer) +{ +struct landlock_rule *new_rule; +u32 new_num_layers; + +build_check_rule (); +if (new_layer) { +/* Should already be checked by landlock_merge_ruleset(). */ +if (WARN_ON_ONCE (num_layers >=LANDLOCK_MAX_NUM_LAYERS )) +return ERR_PTR (-E2BIG ); + new_num_layers = num_layers +1 ; + }else { + new_num_layers = num_layers; + } + new_rule =kzalloc (struct_size (new_rule, layers, new_num_layers), +GFP_KERNEL_ACCOUNT ); +if (!new_rule) +return ERR_PTR (-ENOMEM ); +RB_CLEAR_NODE (&new_rule->node); +landlock_get_object (object); + new_rule->object = object; + new_rule->num_layers = new_num_layers; +/* Copies the original layer stack. */ +memcpy (new_rule->layers, layers, +flex_array_size (new_rule, layers, num_layers)); +if (new_layer) +/* Adds a copy of @new_layer on the layer stack. */ + new_rule->layers[new_rule->num_layers -1 ] = *new_layer; +return new_rule; +} + +static void put_rule (struct landlock_rule *const rule) +{ +might_sleep (); +if (!rule) +return ; +landlock_put_object (rule->object); +kfree (rule); +} + +static void build_check_ruleset (void ) +{ +const struct landlock_ruleset ruleset = { + .num_rules = ~0 , + .num_layers = ~0 , + }; + +BUILD_BUG_ON (ruleset.num_rules <LANDLOCK_MAX_NUM_RULES ); +BUILD_BUG_ON (ruleset.num_layers <LANDLOCK_MAX_NUM_LAYERS ); +} + +/** +* insert_rule - Create and insert a rule in a ruleset +* +* @ruleset: The ruleset to be updated. +* @object: The object to build the new rule with. The underlying kernel +* object must be held by the caller. +* @layers: One or multiple layers to be copied into the new rule. +* @num_layers: The number of @layers entries. +* +* When user space requests to add a new rule to a ruleset, @layers only +* contains one entry and this entry is not assigned to any level. In this +* case, the new rule will extend @ruleset, similarly to a boolean OR between +* access rights. +* +* When merging a ruleset in a domain, or copying a domain, @layers will be +* added to @ruleset as new constraints, similarly to a boolean AND between +* access rights. +*/ +static int insert_rule (struct landlock_ruleset *const ruleset, +struct landlock_object *const object, +const struct landlock_layer (*const layers)[], +size_t num_layers) +{ +struct rb_node **walker_node; +struct rb_node *parent_node =NULL ; +struct landlock_rule *new_rule; + +might_sleep (); +lockdep_assert_held (&ruleset->lock); +if (WARN_ON_ONCE (!object || !layers)) +return -ENOENT ; + walker_node = &(ruleset->root.rb_node); +while (*walker_node) { +struct landlock_rule *const this =rb_entry (*walker_node, + struct landlock_rule, node); + +if (this->object != object) { + parent_node = *walker_node; +if (this->object < object) + walker_node = &((*walker_node)->rb_right); +else + walker_node = &((*walker_node)->rb_left); +continue ; + } + +/* Only a single-level layer should match an existing rule. */ +if (WARN_ON_ONCE (num_layers !=1 )) +return -EINVAL ; + +/* If there is a matching rule, updates it. */ +if ((*layers)[0 ].level ==0 ) { +/* +* Extends access rights when the request comes from +* landlock_add_rule(2), i.e. @ruleset is not a domain. +*/ +if (WARN_ON_ONCE (this->num_layers !=1 )) +return -EINVAL ; +if (WARN_ON_ONCE (this->layers[0 ].level !=0 )) +return -EINVAL ; + this->layers[0 ].access |= (*layers)[0 ].access; +return 0 ; + } + +if (WARN_ON_ONCE (this->layers[0 ].level ==0 )) +return -EINVAL ; + +/* +* Intersects access rights when it is a merge between a +* ruleset and a domain. +*/ + new_rule =create_rule (object, &this->layers, this->num_layers, + &(*layers)[0 ]); +if (IS_ERR (new_rule)) +return PTR_ERR (new_rule); +rb_replace_node (&this->node, &new_rule->node, &ruleset->root); +put_rule (this); +return 0 ; + } + +/* There is no match for @object. */ +build_check_ruleset (); +if (ruleset->num_rules >=LANDLOCK_MAX_NUM_RULES ) +return -E2BIG ; + new_rule =create_rule (object, layers, num_layers,NULL ); +if (IS_ERR (new_rule)) +return PTR_ERR (new_rule); +rb_link_node (&new_rule->node, parent_node, walker_node); +rb_insert_color (&new_rule->node, &ruleset->root); + ruleset->num_rules++; +return 0 ; +} + +static void build_check_layer (void ) +{ +const struct landlock_layer layer = { + .level = ~0 , + }; + +BUILD_BUG_ON (layer.level <LANDLOCK_MAX_NUM_LAYERS ); +} + +/* @ruleset must be locked by the caller. */ +int landlock_insert_rule (struct landlock_ruleset *const ruleset, +struct landlock_object *const object,const u32 access) +{ +struct landlock_layer layers[] = {{ + .access = access, +/* When @level is zero, insert_rule() extends @ruleset. */ + .level =0 , + }}; + +build_check_layer (); +return insert_rule (ruleset, object, &layers,ARRAY_SIZE (layers)); +} + +static inline void get_hierarchy (struct landlock_hierarchy *const hierarchy) +{ +if (hierarchy) +refcount_inc (&hierarchy->usage); +} + +static void put_hierarchy (struct landlock_hierarchy *hierarchy) +{ +while (hierarchy &&refcount_dec_and_test (&hierarchy->usage)) { +const struct landlock_hierarchy *const freeme = hierarchy; + + hierarchy = hierarchy->parent; +kfree (freeme); + } +} + +static int merge_ruleset (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src) +{ +struct landlock_rule *walker_rule, *next_rule; +int err =0 ; + +might_sleep (); +/* Should already be checked by landlock_merge_ruleset() */ +if (WARN_ON_ONCE (!src)) +return 0 ; +/* Only merge into a domain. */ +if (WARN_ON_ONCE (!dst || !dst->hierarchy)) +return -EINVAL ; + +/* Locks @dst first because we are its only owner. */ +mutex_lock (&dst->lock); +mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); + +/* Stacks the new layer. */ +if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { + err = -EINVAL ; + goto out_unlock; + } + dst->fs_access_masks[dst->num_layers -1 ] = src->fs_access_masks[0 ]; + +/* Merges the @src tree. */ +rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, + &src->root, node) { +struct landlock_layer layers[] = {{ + .level = dst->num_layers, + }}; + +if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) { + err = -EINVAL ; + goto out_unlock; + } +if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) { + err = -EINVAL ; + goto out_unlock; + } + layers[0 ].access = walker_rule->layers[0 ].access; + err =insert_rule (dst, walker_rule->object, &layers, +ARRAY_SIZE (layers)); +if (err) + goto out_unlock; + } + +out_unlock: +mutex_unlock (&src->lock); +mutex_unlock (&dst->lock); +return err; +} + +static int inherit_ruleset (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child) +{ +struct landlock_rule *walker_rule, *next_rule; +int err =0 ; + +might_sleep (); +if (!parent) +return 0 ; + +/* Locks @child first because we are its only owner. */ +mutex_lock (&child->lock); +mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); + +/* Copies the @parent tree. */ +rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, + &parent->root, node) { + err =insert_rule (child, walker_rule->object, + &walker_rule->layers, walker_rule->num_layers); +if (err) + goto out_unlock; + } + +if (WARN_ON_ONCE (child->num_layers <= parent->num_layers)) { + err = -EINVAL ; + goto out_unlock; + } +/* Copies the parent layer stack and leaves a space for the new layer. */ +memcpy (child->fs_access_masks, parent->fs_access_masks, +flex_array_size (parent, fs_access_masks, parent->num_layers)); + +if (WARN_ON_ONCE (!parent->hierarchy)) { + err = -EINVAL ; + goto out_unlock; + } +get_hierarchy (parent->hierarchy); + child->hierarchy->parent = parent->hierarchy; + +out_unlock: +mutex_unlock (&parent->lock); +mutex_unlock (&child->lock); +return err; +} + +static void free_ruleset (struct landlock_ruleset *const ruleset) +{ +struct landlock_rule *freeme, *next; + +might_sleep (); +rbtree_postorder_for_each_entry_safe (freeme, next, &ruleset->root, + node) +put_rule (freeme); +put_hierarchy (ruleset->hierarchy); +kfree (ruleset); +} + +void landlock_put_ruleset (struct landlock_ruleset *const ruleset) +{ +might_sleep (); +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) +free_ruleset (ruleset); +} + +static void free_ruleset_work (struct work_struct *const work) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(work, struct landlock_ruleset, work_free); +free_ruleset (ruleset); +} + +void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset) +{ +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) { +INIT_WORK (&ruleset->work_free, free_ruleset_work); +schedule_work (&ruleset->work_free); + } +} + +/** +* landlock_merge_ruleset - Merge a ruleset with a domain +* +* @parent: Parent domain. +* @ruleset: New ruleset to be merged. +* +* Returns the intersection of @parent and @ruleset, or returns @parent if +* @ruleset is empty, or returns a duplicate of @ruleset if @parent is empty. +*/ +struct landlock_ruleset *landlock_merge_ruleset ( +struct landlock_ruleset *const parent, +struct landlock_ruleset *const ruleset) +{ +struct landlock_ruleset *new_dom; +u32 num_layers; +int err; + +might_sleep (); +if (WARN_ON_ONCE (!ruleset || parent == ruleset)) +return ERR_PTR (-EINVAL ); + +if (parent) { +if (parent->num_layers >=LANDLOCK_MAX_NUM_LAYERS ) +return ERR_PTR (-E2BIG ); + num_layers = parent->num_layers +1 ; + }else { + num_layers =1 ; + } + +/* Creates a new domain... */ + new_dom =create_ruleset (num_layers); +if (IS_ERR (new_dom)) +return new_dom; + new_dom->hierarchy =kzalloc (sizeof (*new_dom->hierarchy), +GFP_KERNEL_ACCOUNT ); +if (!new_dom->hierarchy) { + err = -ENOMEM ; + goto out_put_dom; + } +refcount_set (&new_dom->hierarchy->usage,1 ); + +/* ...as a child of @parent... */ + err =inherit_ruleset (parent, new_dom); +if (err) + goto out_put_dom; + +/* ...and including @ruleset. */ + err =merge_ruleset (new_dom, ruleset); +if (err) + goto out_put_dom; + +return new_dom; + +out_put_dom: +landlock_put_ruleset (new_dom); +return ERR_PTR (err); +} + +/* +* The returned access has the same lifetime as @ruleset. +*/ +const struct landlock_rule *landlock_find_rule ( +const struct landlock_ruleset *const ruleset, +const struct landlock_object *const object) +{ +const struct rb_node *node; + +if (!object) +return NULL ; + node = ruleset->root.rb_node; +while (node) { +struct landlock_rule *this =rb_entry (node, + struct landlock_rule, node); + +if (this->object == object) +return this; +if (this->object < object) + node = node->rb_right; +else + node = node->rb_left; + } +return NULL ; +}security/landlock/ruleset.h new file +165
-0@@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Ruleset management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_RULESET_H +#define _SECURITY_LANDLOCK_RULESET_H + +#include <linux/mutex.h> +#include <linux/rbtree.h> +#include <linux/refcount.h> +#include <linux/workqueue.h> + +#include "object.h" + +/** +* struct landlock_layer - Access rights for a given layer +*/ +struct landlock_layer { +/** +* @level: Position of this layer in the layer stack. +*/ +u16 level; +/** +* @access: Bitfield of allowed actions on the kernel object. They are +* relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ). +*/ +u16 access; +}; + +/** +* struct landlock_rule - Access rights tied to an object +*/ +struct landlock_rule { +/** +* @node: Node in the ruleset's red-black tree. +*/ +struct rb_node node; +/** +* @object: Pointer to identify a kernel object (e.g. an inode). This +* is used as a key for this ruleset element. This pointer is set once +* and never modified. It always points to an allocated object because +* each rule increments the refcount of its object. +*/ +struct landlock_object *object; +/** +* @num_layers: Number of entries in @layers. +*/ +u32 num_layers; +/** +* @layers: Stack of layers, from the latest to the newest, implemented +* as a flexible array member (FAM). +*/ +struct landlock_layer layers[]; +}; + +/** +* struct landlock_hierarchy - Node in a ruleset hierarchy +*/ +struct landlock_hierarchy { +/** +* @parent: Pointer to the parent node, or NULL if it is a root +* Landlock domain. +*/ +struct landlock_hierarchy *parent; +/** +* @usage: Number of potential children domains plus their parent +* domain. +*/ +refcount_t usage; +}; + +/** +* struct landlock_ruleset - Landlock ruleset +* +* This data structure must contain unique entries, be updatable, and quick to +* match an object. +*/ +struct landlock_ruleset { +/** +* @root: Root of a red-black tree containing &struct landlock_rule +* nodes. Once a ruleset is tied to a process (i.e. as a domain), this +* tree is immutable until @usage reaches zero. +*/ +struct rb_root root; +/** +* @hierarchy: Enables hierarchy identification even when a parent +* domain vanishes. This is needed for the ptrace protection. +*/ +struct landlock_hierarchy *hierarchy; +union { +/** +* @work_free: Enables to free a ruleset within a lockless +* section. This is only used by +* landlock_put_ruleset_deferred() when @usage reaches zero. +* The fields @lock, @usage, @num_rules, @num_layers and +* @fs_access_masks are then unused. +*/ +struct work_struct work_free; +struct { +/** +* @lock: Guards against concurrent modifications of +* @root, if @usage is greater than zero. +*/ +struct mutex lock; +/** +* @usage: Number of processes (i.e. domains) or file +* descriptors referencing this ruleset. +*/ +refcount_t usage; +/** +* @num_rules: Number of non-overlapping (i.e. not for +* the same object) rules in this ruleset. +*/ +u32 num_rules; +/** +* @num_layers: Number of layers that are used in this +* ruleset. This enables to check that all the layers +* allow an access request. A value of 0 identifies a +* non-merged ruleset (i.e. not a domain). +*/ +u32 num_layers; +/** +* @fs_access_masks: Contains the subset of filesystem +* actions that are restricted by a ruleset. A domain +* saves all layers of merged rulesets in a stack +* (FAM), starting from the first layer to the last +* one. These layers are used when merging rulesets, +* for user space backward compatibility (i.e. +* future-proof), and to properly handle merged +* rulesets without overlapping access rights. These +* layers are set once and never changed for the +* lifetime of the ruleset. +*/ +u16 fs_access_masks[]; + }; + }; +}; + +struct landlock_ruleset *landlock_create_ruleset (const u32 fs_access_mask); + +void landlock_put_ruleset (struct landlock_ruleset *const ruleset); +void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset); + +int landlock_insert_rule (struct landlock_ruleset *const ruleset, +struct landlock_object *const object,const u32 access); + +struct landlock_ruleset *landlock_merge_ruleset ( +struct landlock_ruleset *const parent, +struct landlock_ruleset *const ruleset); + +const struct landlock_rule *landlock_find_rule ( +const struct landlock_ruleset *const ruleset, +const struct landlock_object *const object); + +static inline void landlock_get_ruleset (struct landlock_ruleset *const ruleset) +{ +if (ruleset) +refcount_inc (&ruleset->usage); +} + +#endif /* _SECURITY_LANDLOCK_RULESET_H */ Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 3/12 landlock: Set up the security framework and manage credentials
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
Process's credentials point to a Landlock domain, which is underneath implemented with a ruleset. In the following commits, this domain is used to check and enforce the ptrace and filesystem security policies. A domain is inherited from a parent to its child the same way a thread inherits a seccomp policy.
---
Changes since v25:
* Rename function to landlock_add_cred_hooks().
Changes since v23:
* Add an early check for the current domain in hook_cred_free() to avoid superfluous call.
* Cosmetic cleanup to make the code more readable.
Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>
Changes since v21:
* Fix copyright dates.
Changes since v17:
* Constify returned domain pointers from landlock_get_current_domain() and landlock_get_task_domain() helpers.
Changes since v15:
* Optimize landlocked() for current thread.
* Display the greeting message when everything is initialized.
Changes since v14:
* Uses pr_fmt from common.h .
* Constify variables.
* Remove useless NULL initialization.
Changes since v13:
* totally get ride of the seccomp dependency
* only keep credential management and LSM setup.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- security/Kconfig | 10 +++---- security/landlock/Makefile | 3 +- security/landlock/common.h | 20 +++++++++++++ security/landlock/cred.c | 46 ++++++++++++++++++++++++++++++ security/landlock/cred.h | 58 ++++++++++++++++++++++++++++++++++++++ security/landlock/setup.c | 31 ++++++++++++++++++++ security/landlock/setup.h | 16 +++++++++++ 7 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 security/landlock/common.h create mode 100644 security/landlock/cred.c create mode 100644 security/landlock/cred.h create mode 100644 security/landlock/setup.c create mode 100644 security/landlock/setup.h
security/Kconfig +5 -5
@@ -278,11 +278,11 @@ endchoice config LSM string "Ordered list of enabled LSMs" - default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK - default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR - default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO - default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC - default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" + default "landlock,lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK + default "landlock,lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR + default "landlock,lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO + default "landlock,lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC + default "landlock,lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" help A comma-separated list of LSMs, in initialization order. Any LSMs left off this list will be ignored. This can be
security/landlock/Makefile +2 -1
@@ -1,3 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := object.o ruleset.o +landlock-y := setup.o object.o ruleset.o \ + cred.o
security/landlock/common.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Common constants and helpers +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_COMMON_H +#define _SECURITY_LANDLOCK_COMMON_H + +#define LANDLOCK_NAME "landlock" + +#ifdef pr_fmt +#undef pr_fmt +#endif + +#define pr_fmt (fmt) LANDLOCK_NAME ": " fmt + +#endif /* _SECURITY_LANDLOCK_COMMON_H */
security/landlock/cred.c new file +46 -0
46 lines not on this page · raw message
security/landlock/cred.h new file +58 -0
58 lines not on this page · raw message
security/landlock/setup.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Security framework setup +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/init.h> +#include <linux/lsm_hooks.h> + +#include "common.h" +#include "cred.h" +#include "setup.h" + +struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = { + .lbs_cred =sizeof (struct landlock_cred_security ), +}; + +static int __init landlock_init (void ) +{ +landlock_add_cred_hooks (); +pr_info ("Up and running.\n" ); +return 0 ; +} + +DEFINE_LSM (LANDLOCK_NAME ) = { + .name =LANDLOCK_NAME , + .init = landlock_init, + .blobs = &landlock_blob_sizes, +};
security/landlock/setup.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Security framework setup +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_SETUP_H +#define _SECURITY_LANDLOCK_SETUP_H + +#include <linux/lsm_hooks.h> + +extern struct lsm_blob_sizes landlock_blob_sizes; + +#endif /* _SECURITY_LANDLOCK_SETUP_H */
Signature · 1 line
2.30.0
Re: [PATCH v28 03/12] landlock: Set up the security framework and manage credentials
Quoted 11 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
Process's credentials point to a Landlock domain, which is underneath implemented with a ruleset. In the following commits, this domain is used to check and enforce the ptrace and filesystem security policies. A domain is inherited from a parent to its child the same way a thread inherits a seccomp policy.
Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
Quoted 286 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v25:
* Rename function tolandlock_add_cred_hooks().Changes since v23:
* Add an early check for the current domain inhook_cred_free()to avoid superfluous call.
* Cosmetic cleanup to make the code more readable.Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v21:
* Fix copyright dates.Changes since v17:
* Constify returned domain pointers fromlandlock_get_current_domain()andlandlock_get_task_domain()helpers.Changes since v15:
* Optimizelandlocked()for current thread.
* Display the greeting message when everything is initialized.Changes since v14:
* Usespr_fmtfromcommon.h.
* Constify variables.
* Remove useless NULL initialization.Changes since v13:
* totally get ride of the seccomp dependency
* only keep credential management and LSM setup.Previous changes:
https://lore.kernel.org/lkml/[email protected]/--- security/Kconfig | 10 +++---- security/landlock/Makefile | 3 +- security/landlock/common.h | 20 +++++++++++++ security/landlock/cred.c | 46 ++++++++++++++++++++++++++++++ security/landlock/cred.h | 58 ++++++++++++++++++++++++++++++++++++++ security/landlock/setup.c | 31 ++++++++++++++++++++ security/landlock/setup.h | 16 +++++++++++ 7 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 security/landlock/common.h create mode 100644 security/landlock/cred.c create mode 100644 security/landlock/cred.h create mode 100644 security/landlock/setup.c create mode 100644 security/landlock/setup.hsecurity/Kconfig +5
-5@@ -278,11 +278,11 @@ endchoice config LSM string "Ordered list of enabled LSMs" - default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK - default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR - default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO - default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC - default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" + default "landlock,lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK + default "landlock,lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR + default "landlock,lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO + default "landlock,lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC + default "landlock,lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf" help A comma-separated list of LSMs, in initialization order. Any LSMs left off this list will be ignored. This can besecurity/landlock/Makefile +2
-1@@ -1,3 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := object.o ruleset.o +landlock-y := setup.o object.o ruleset.o \ + cred.osecurity/landlock/common.h new file +20
-0@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Common constants and helpers +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_COMMON_H +#define _SECURITY_LANDLOCK_COMMON_H + +#define LANDLOCK_NAME "landlock" + +#ifdef pr_fmt +#undef pr_fmt +#endif + +#define pr_fmt (fmt) LANDLOCK_NAME ": " fmt + +#endif /* _SECURITY_LANDLOCK_COMMON_H */ security/landlock/cred.c new file +46
-0@@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Credential hooks +* +* Copyright © 2017-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/cred.h> +#include <linux/lsm_hooks.h> + +#include "common.h" +#include "cred.h" +#include "ruleset.h" +#include "setup.h" + +static int hook_cred_prepare (struct cred *const new, +const struct cred *const old,const gfp_t gfp) +{ +struct landlock_ruleset *const old_dom =landlock_cred (old)->domain; + +if (old_dom) { +landlock_get_ruleset (old_dom); +landlock_cred (new)->domain = old_dom; + } +return 0 ; +} + +static void hook_cred_free (struct cred *const cred) +{ +struct landlock_ruleset *const dom =landlock_cred (cred)->domain; + +if (dom) +landlock_put_ruleset_deferred (dom); +} + +static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (cred_prepare, hook_cred_prepare), +LSM_HOOK_INIT (cred_free, hook_cred_free), +}; + +__init void landlock_add_cred_hooks (void ) +{ +security_add_hooks (landlock_hooks,ARRAY_SIZE (landlock_hooks), +LANDLOCK_NAME ); +}security/landlock/cred.h new file +58
-0@@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Credential hooks +* +* Copyright © 2019-2020 Mickaël Salaün <[email protected]> +* Copyright © 2019-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_CRED_H +#define _SECURITY_LANDLOCK_CRED_H + +#include <linux/cred.h> +#include <linux/init.h> +#include <linux/rcupdate.h> + +#include "ruleset.h" +#include "setup.h" + +struct landlock_cred_security { +struct landlock_ruleset *domain; +}; + +static inline struct landlock_cred_security *landlock_cred ( +const struct cred *cred) +{ +return cred->security + landlock_blob_sizes.lbs_cred; +} + +static inline const struct landlock_ruleset *landlock_get_current_domain (void ) +{ +return landlock_cred (current_cred ())->domain; +} + +/* +* The call needs to come from an RCU read-side critical section. +*/ +static inline const struct landlock_ruleset *landlock_get_task_domain ( +const struct task_struct *const task) +{ +return landlock_cred (__task_cred (task))->domain; +} + +static inline bool landlocked (const struct task_struct *const task) +{ +bool has_dom; + +if (task == current) +return !!landlock_get_current_domain (); + +rcu_read_lock (); + has_dom = !!landlock_get_task_domain (task); +rcu_read_unlock (); +return has_dom; +} + +__init void landlock_add_cred_hooks (void ); + +#endif /* _SECURITY_LANDLOCK_CRED_H */ security/landlock/setup.c new file +31
-0@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Security framework setup +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/init.h> +#include <linux/lsm_hooks.h> + +#include "common.h" +#include "cred.h" +#include "setup.h" + +struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = { + .lbs_cred =sizeof (struct landlock_cred_security ), +}; + +static int __init landlock_init (void ) +{ +landlock_add_cred_hooks (); +pr_info ("Up and running.\n" ); +return 0 ; +} + +DEFINE_LSM (LANDLOCK_NAME ) = { + .name =LANDLOCK_NAME , + .init = landlock_init, + .blobs = &landlock_blob_sizes, +};security/landlock/setup.h new file +16
-0@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Security framework setup +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_SETUP_H +#define _SECURITY_LANDLOCK_SETUP_H + +#include <linux/lsm_hooks.h> + +extern struct lsm_blob_sizes landlock_blob_sizes; + +#endif /* _SECURITY_LANDLOCK_SETUP_H */ Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 4/12 landlock: Add ptrace restrictions
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
Using ptrace(2) and related debug features on a target process can lead to a privilege escalation. Indeed, ptrace(2) can be used by an attacker to impersonate another task and to remain undetected while performing malicious activities. Thanks to ptrace_may_access(), various part of the kernel can check if a tracer is more privileged than a tracee.
A landlocked process has fewer privileges than a non-landlocked process and must then be subject to additional restrictions when manipulating processes. To be allowed to use ptrace(2) and related syscalls on a target process, a landlocked process must have a subset of the target process's rules (i.e. the tracee must be in a sub-domain of the tracer).
---
Changes since v25:
* Rename function to landlock_add_ptrace_hooks().
Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>
Changes since v21:
* Fix copyright dates.
Changes since v14:
* Constify variables.
Changes since v13:
* Make the ptrace restriction mandatory, like in the v10.
* Remove the eBPF dependency.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- security/landlock/Makefile | 2 +- security/landlock/ptrace.c | 120 +++++++++++++++++++++++++++++++++++++ security/landlock/ptrace.h | 14 +++++ security/landlock/setup.c | 2 + 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 security/landlock/ptrace.c create mode 100644 security/landlock/ptrace.h
security/landlock/Makefile +1 -1
@@ -1,4 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o object.o ruleset.o \ - cred.o + cred.o ptrace.o
security/landlock/ptrace.c new file +120 -0
120 lines not on this page · raw message
security/landlock/ptrace.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Ptrace hooks +* +* Copyright © 2017-2019 Mickaël Salaün <[email protected]> +* Copyright © 2019 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_PTRACE_H +#define _SECURITY_LANDLOCK_PTRACE_H + +__init void landlock_add_ptrace_hooks (void ); + +#endif /* _SECURITY_LANDLOCK_PTRACE_H */
security/landlock/setup.c +2 -0
@@ -11,6 +11,7 @@#include "common.h" #include "cred.h" +#include "ptrace.h" #include "setup.h" struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = {
@@ -20,6 +21,7 @@ struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = {static int __init landlock_init (void ) {landlock_add_cred_hooks (); +landlock_add_ptrace_hooks ();pr_info ("Up and running.\n" );return 0 ; }
Signature · 1 line
2.30.0
Re: [PATCH v28 04/12] landlock: Add ptrace restrictions
Quoted 14 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
Using ptrace(2) and related debug features on a target process can lead to a privilege escalation. Indeed, ptrace(2) can be used by an attacker to impersonate another task and to remain undetected while performing malicious activities. Thanks to
ptrace_may_access(), various part of the kernel can check if a tracer is more privileged than a tracee.A landlocked process has fewer privileges than a non-landlocked process and must then be subject to additional restrictions when manipulating processes. To be allowed to use ptrace(2) and related syscalls on a target process, a landlocked process must have a subset of the target process's rules (i.e. the tracee must be in a sub-domain of the tracer).
Quoted 3 lines
Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
Thanks, I appreciate that things are well named and easy to reason about.
Quoted 209 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v25:
* Rename function tolandlock_add_ptrace_hooks().Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v21:
* Fix copyright dates.Changes since v14:
* Constify variables.Changes since v13:
* Make the ptrace restriction mandatory, like in the v10.
* Remove the eBPF dependency.Previous changes:
https://lore.kernel.org/lkml/[email protected]/--- security/landlock/Makefile | 2 +- security/landlock/ptrace.c | 120 +++++++++++++++++++++++++++++++++++++ security/landlock/ptrace.h | 14 +++++ security/landlock/setup.c | 2 + 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 security/landlock/ptrace.c create mode 100644 security/landlock/ptrace.hsecurity/landlock/Makefile +1
-1@@ -1,4 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o object.o ruleset.o \ - cred.o + cred.o ptrace.osecurity/landlock/ptrace.c new file +120
-0@@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Ptrace hooks +* +* Copyright © 2017-2020 Mickaël Salaün <[email protected]> +* Copyright © 2019-2020 ANSSI +*/ + +#include <asm/current.h> +#include <linux/cred.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/lsm_hooks.h> +#include <linux/rcupdate.h> +#include <linux/sched.h> + +#include "common.h" +#include "cred.h" +#include "ptrace.h" +#include "ruleset.h" +#include "setup.h" + +/** +* domain_scope_le - Checks domain ordering for scoped ptrace +* +* @parent: Parent domain. +* @child: Potential child of @parent. +* +* Checks if the @parent domain is less or equal to (i.e. an ancestor, which +* means a subset of) the @child domain. +*/ +static bool domain_scope_le (const struct landlock_ruleset *const parent, +const struct landlock_ruleset *const child) +{ +const struct landlock_hierarchy *walker; + +if (!parent) +return true; +if (!child) +return false; +for (walker = child->hierarchy; walker; walker = walker->parent) { +if (walker == parent->hierarchy) +/* @parent is in the scoped hierarchy of @child. */ +return true; + } +/* There is no relationship between @parent and @child. */ +return false; +} + +static bool task_is_scoped (const struct task_struct *const parent, +const struct task_struct *const child) +{ +bool is_scoped; +const struct landlock_ruleset *dom_parent, *dom_child; + +rcu_read_lock (); + dom_parent =landlock_get_task_domain (parent); + dom_child =landlock_get_task_domain (child); + is_scoped =domain_scope_le (dom_parent, dom_child); +rcu_read_unlock (); +return is_scoped; +} + +static int task_ptrace (const struct task_struct *const parent, +const struct task_struct *const child) +{ +/* Quick return for non-landlocked tasks. */ +if (!landlocked (parent)) +return 0 ; +if (task_is_scoped (parent, child)) +return 0 ; +return -EPERM ; +} + +/** +* hook_ptrace_access_check - Determines whether the current process may access +* another +* +* @child: Process to be accessed. +* @mode: Mode of attachment. +* +* If the current task has Landlock rules, then the child must have at least +* the same rules. Else denied. +* +* Determines whether a process may access another, returning 0 if permission +* granted, -errno if denied. +*/ +static int hook_ptrace_access_check (struct task_struct *const child, +const unsigned int mode) +{ +return task_ptrace (current, child); +} + +/** +* hook_ptrace_traceme - Determines whether another process may trace the +* current one +* +* @parent: Task proposed to be the tracer. +* +* If the parent has Landlock rules, then the current task must have the same +* or more rules. Else denied. +* +* Determines whether the nominated task is permitted to trace the current +* process, returning 0 if permission is granted, -errno if denied. +*/ +static int hook_ptrace_traceme (struct task_struct *const parent) +{ +return task_ptrace (parent, current); +} + +static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (ptrace_access_check, hook_ptrace_access_check), +LSM_HOOK_INIT (ptrace_traceme, hook_ptrace_traceme), +}; + +__init void landlock_add_ptrace_hooks (void ) +{ +security_add_hooks (landlock_hooks,ARRAY_SIZE (landlock_hooks), +LANDLOCK_NAME ); +}security/landlock/ptrace.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Ptrace hooks +* +* Copyright © 2017-2019 Mickaël Salaün <[email protected]> +* Copyright © 2019 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_PTRACE_H +#define _SECURITY_LANDLOCK_PTRACE_H + +__init void landlock_add_ptrace_hooks (void ); + +#endif /* _SECURITY_LANDLOCK_PTRACE_H */ security/landlock/setup.c +2
-0@@ -11,6 +11,7 @@#include "common.h" #include "cred.h" +#include "ptrace.h" #include "setup.h" struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = {@@ -20,6 +21,7 @@ struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = {static int __init landlock_init (void ) {landlock_add_cred_hooks (); +landlock_add_ptrace_hooks ();pr_info ("Up and running.\n" );return 0 ; }Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 5/12 LSM: Infrastructure management of the superblock
In reply to Mickaël SalaünFrom: Casey Schaufler <[email protected]>
Move management of the superblock->sb_security blob out of the individual security modules and into the security infrastructure. Instead of allocating the blobs from within the modules, the modules tell the infrastructure how much space is required, and the space is allocated there.
---
Changes since v26:
* Rebase on commit b159e86b5a2a ("selinux: drop super_block backpointer from superblock_security_struct"). No change in the patch itself, only a trivial conflict because of an updated nearby line in selinux_set_mnt_opts() variable declarations.
Changes since v20:
* Remove all Reviewed-by except Stephen Smalley:
https://lore.kernel.org/lkml/CAEjxPJ7ARJO57MBW66=xsBzMMRb=9uLgqocK5eskHCaiVMx7Vw@mail.gmail.com/
* Cosmetic fix in the commit message.
Changes since v17:
* Rebase the original LSM stacking patch from v5.3 to v5.7: I fixed some diff conflicts caused by code moves and function renames in selinux/include/objsec.h and selinux/hooks.c . I checked that it builds but I didn't test the changes for SELinux nor SMACK.
https://lore.kernel.org/r/[email protected]
--- include/linux/lsm_hooks.h | 1 + security/security.c | 46 ++++++++++++++++++++---- security/selinux/hooks.c | 58 ++++++++++++------------------- security/selinux/include/objsec.h | 6 ++++ security/selinux/ss/services.c | 3 +- security/smack/smack.h | 6 ++++ security/smack/smack_lsm.c | 35 +++++-------------- 7 files changed, 85 insertions(+), 70 deletions(-)
include/linux/lsm_hooks.h +1 -0
@@ -1563,6 +1563,7 @@ struct lsm_blob_sizes {int lbs_cred;int lbs_file;int lbs_inode; +int lbs_superblock;int lbs_ipc;int lbs_msg_msg;int lbs_task;
security/security.c +39 -7
75 lines not on this page · raw message
security/selinux/hooks.c +22 -36
188 lines not on this page · raw message
security/selinux/include/objsec.h +6 -0
@@ -188,4 +188,10 @@ static inline u32 current_sid(void) return tsec->sid; } +static inline struct superblock_security_struct *selinux_superblock ( +const struct super_block *superblock) +{ +return superblock->s_security + selinux_blob_sizes.lbs_superblock; +} +#endif /* _SELINUX_OBJSEC_H_ */
security/selinux/ss/services.c +2 -1
@@ -47,6 +47,7 @@#include <linux/sched.h> #include <linux/audit.h> #include <linux/vmalloc.h> +#include <linux/lsm_hooks.h> #include <net/netlabel.h> #include "flask.h"
@@ -2873,7 +2874,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb)struct sidtab *sidtab;int rc =0 ;struct ocontext *c; -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);const char *fstype = sb->s_type->name;if (!selinux_initialized (state)) {
security/smack/smack.h +6 -0
@@ -357,6 +357,12 @@ static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)return ipc->security + smack_blob_sizes.lbs_ipc; } +static inline struct superblock_smack *smack_superblock ( +const struct super_block *superblock) +{ +return superblock->s_security + smack_blob_sizes.lbs_superblock; +} +/* * Is the directory transmuting? */
security/smack/smack_lsm.c +9 -26
@@ -535,12 +535,7 @@ static int smack_syslog(int typefrom_file)*/ static int smack_sb_alloc_security (struct super_block *sb) { -struct superblock_smack *sbsp; - - sbsp =kzalloc (sizeof (struct superblock_smack ),GFP_KERNEL ); - -if (sbsp ==NULL ) -return -ENOMEM ; +struct superblock_smack *sbsp =smack_superblock (sb); sbsp->smk_root = &smack_known_floor; sbsp->smk_default = &smack_known_floor;
@@ -549,22 +544,10 @@ static int smack_sb_alloc_security(struct super_block *sb)/* * SMK_SB_INITIALIZED will be zero from kzalloc. */ - sb->s_security = sbsp;return 0 ; } -/** -* smack_sb_free_security - free a superblock blob -* @sb: the superblock getting the blob -* -*/ -static void smack_sb_free_security (struct super_block *sb) -{ -kfree (sb->s_security); - sb->s_security =NULL ; -} -struct smack_mnt_opts {const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute; };
@@ -772,7 +755,7 @@ static int smack_set_mnt_opts(struct super_block *sb, {struct dentry *root = sb->s_root;struct inode *inode =d_backing_inode (root); -struct superblock_smack *sp = sb->s_security; +struct superblock_smack *sp =smack_superblock (sb);struct inode_smack *isp;struct smack_known *skp;struct smack_mnt_opts *opts = mnt_opts;
@@ -871,7 +854,7 @@ static int smack_set_mnt_opts(struct super_block *sb,*/ static int smack_sb_statfs (struct dentry *dentry) { -struct superblock_smack *sbp = dentry->d_sb->s_security; +struct superblock_smack *sbp =smack_superblock (dentry->d_sb);int rc;struct smk_audit_info ad;
@@ -905,7 +888,7 @@ static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)if (isp->smk_task ==NULL || isp->smk_task == bsp->smk_task)return 0 ; - sbsp = inode->i_sb->s_security; + sbsp =smack_superblock (inode->i_sb);if ((sbsp->smk_flags &SMK_SB_UNTRUSTED ) && isp->smk_task != sbsp->smk_root)return 0 ;
@@ -1157,7 +1140,7 @@ static int smack_inode_rename(struct inode *old_inode,*/ static int smack_inode_permission (struct inode *inode,int mask) { -struct superblock_smack *sbsp = inode->i_sb->s_security; +struct superblock_smack *sbsp =smack_superblock (inode->i_sb);struct smk_audit_info ad;int no_block = mask &MAY_NOT_BLOCK ;int rc;
@@ -1398,7 +1381,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name)*/ if (strcmp (name,XATTR_NAME_SMACK ) ==0 ) {struct super_block *sbp = dentry->d_sb; -struct superblock_smack *sbsp = sbp->s_security; +struct superblock_smack *sbsp =smack_superblock (sbp); isp->smk_inode = sbsp->smk_default; }else if (strcmp (name,XATTR_NAME_SMACKEXEC ) ==0 )
@@ -1668,7 +1651,7 @@ static int smack_mmap_file(struct file *file, isp =smack_inode (file_inode (file));if (isp->smk_mmap ==NULL )return 0 ; - sbsp =file_inode (file)->i_sb->s_security; + sbsp =smack_superblock (file_inode (file)->i_sb);if (sbsp->smk_flags &SMK_SB_UNTRUSTED && isp->smk_mmap != sbsp->smk_root)return -EACCES ;
@@ -3283,7 +3266,7 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)return ; sbp = inode->i_sb; - sbsp = sbp->s_security; + sbsp =smack_superblock (sbp);/* * We're going to use the superblock default label * if there's no label on the file.
@@ -4696,6 +4679,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = { .lbs_inode = sizeof(struct inode_smack), .lbs_ipc = sizeof(struct smack_known *), .lbs_msg_msg = sizeof(struct smack_known *), + .lbs_superblock = sizeof(struct superblock_smack), }; static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
@@ -4707,7 +4691,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (fs_context_parse_param, smack_fs_context_parse_param),LSM_HOOK_INIT (sb_alloc_security, smack_sb_alloc_security), -LSM_HOOK_INIT (sb_free_security, smack_sb_free_security),LSM_HOOK_INIT (sb_free_mnt_opts, smack_free_mnt_opts),LSM_HOOK_INIT (sb_eat_lsm_opts, smack_sb_eat_lsm_opts), LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Signature · 1 line
2.30.0
Re: [PATCH v28 05/12] LSM: Infrastructure management of the superblock
Quoted 8 lines · Mickaël Salaün
From: Casey Schaufler <[email protected]>
Move management of the
superblock->sb_securityblob out of the individual security modules and into the security infrastructure. Instead of allocating the blobs from within the modules, the modules tell the infrastructure how much space is required, and the space is allocated there.
Quoted 5 lines
Cc Kees Cook <[email protected]>Cc John Johansen <[email protected]>Signed-off-by Casey Schaufler <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Stephen Smalley <[email protected]>
I wonder how many out of tree modules this will impact :) Actually if some new incoming module does an rcu callback to free the sb_security, then the security_sb_free will need an update, but that seems unlikely.
Quoted 511 lines
---
Changes since v26:
* Rebase on commitb159e86b5a2a("selinux: dropsuper_blockbackpointer fromsuperblock_security_struct"). No change in the patch itself, only a trivial conflict because of an updated nearby line inselinux_set_mnt_opts()variable declarations.Changes since v20:
* Remove all Reviewed-by except Stephen Smalley:
https://lore.kernel.org/lkml/CAEjxPJ7ARJO57MBW66=xsBzMMRb=9uLgqocK5eskHCaiVMx7Vw@mail.gmail.com/
* Cosmetic fix in the commit message.Changes since v17:
* Rebase the original LSM stacking patch from v5.3 to v5.7: I fixed some diff conflicts caused by code moves and function renames inselinux/include/objsec.handselinux/hooks.c. I checked that it builds but I didn't test the changes for SELinux nor SMACK.
https://lore.kernel.org/r/[email protected]--- include/linux/lsm_hooks.h | 1 + security/security.c | 46 ++++++++++++++++++++---- security/selinux/hooks.c | 58 ++++++++++++------------------- security/selinux/include/objsec.h | 6 ++++ security/selinux/ss/services.c | 3 +- security/smack/smack.h | 6 ++++ security/smack/smack_lsm.c | 35 +++++-------------- 7 files changed, 85 insertions(+), 70 deletions(-)include/linux/lsm_hooks.h +1
-0@@ -1563,6 +1563,7 @@ struct lsm_blob_sizes {int lbs_cred;int lbs_file;int lbs_inode; +int lbs_superblock;int lbs_ipc;int lbs_msg_msg;int lbs_task;security/security.c +39
-7@@ -203,6 +203,7 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)lsm_set_blob_size (&needed->lbs_inode, &blob_sizes.lbs_inode);lsm_set_blob_size (&needed->lbs_ipc, &blob_sizes.lbs_ipc);lsm_set_blob_size (&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); +lsm_set_blob_size (&needed->lbs_superblock, &blob_sizes.lbs_superblock);lsm_set_blob_size (&needed->lbs_task, &blob_sizes.lbs_task); }@@ -333,12 +334,13 @@ static void __init ordered_lsm_init(void)for (lsm = ordered_lsms; *lsm; lsm++)prepare_lsm (*lsm); -init_debug ("cred blob size = %d\n" , blob_sizes.lbs_cred); -init_debug ("file blob size = %d\n" , blob_sizes.lbs_file); -init_debug ("inode blob size = %d\n" , blob_sizes.lbs_inode); -init_debug ("ipc blob size = %d\n" , blob_sizes.lbs_ipc); -init_debug ("msg_msg blob size = %d\n" , blob_sizes.lbs_msg_msg); -init_debug ("task blob size = %d\n" , blob_sizes.lbs_task); +init_debug ("cred blob size = %d\n" , blob_sizes.lbs_cred); +init_debug ("file blob size = %d\n" , blob_sizes.lbs_file); +init_debug ("inode blob size = %d\n" , blob_sizes.lbs_inode); +init_debug ("ipc blob size = %d\n" , blob_sizes.lbs_ipc); +init_debug ("msg_msg blob size = %d\n" , blob_sizes.lbs_msg_msg); +init_debug ("superblock blob size = %d\n" , blob_sizes.lbs_superblock); +init_debug ("task blob size = %d\n" , blob_sizes.lbs_task);/* * Create any kmem_caches needed for blobs @@ -670,6 +672,27 @@ static void __init lsm_early_task(struct task_struct *task)panic ("%s: Early task alloc failed.\n" , __func__); } +/** +* lsm_superblock_alloc - allocate a composite superblock blob +* @sb: the superblock that needs a blob +* +* Allocate the superblock blob for all the modules +* +* Returns 0, or -ENOMEM if memory can't be allocated. +*/ +static int lsm_superblock_alloc (struct super_block *sb) +{ +if (blob_sizes.lbs_superblock ==0 ) { + sb->s_security =NULL ; +return 0 ; + } + + sb->s_security =kzalloc (blob_sizes.lbs_superblock,GFP_KERNEL ); +if (sb->s_security ==NULL ) +return -ENOMEM ; +return 0 ; +} +/* * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and * can be accessed with: @@ -867,12 +890,21 @@ int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *int security_sb_alloc (struct super_block *sb) { -return call_int_hook (sb_alloc_security,0 , sb); +int rc =lsm_superblock_alloc (sb); + +if (unlikely (rc)) +return rc; + rc =call_int_hook (sb_alloc_security,0 , sb); +if (unlikely (rc)) +security_sb_free (sb); +return rc; }void security_sb_free (struct super_block *sb) {call_void_hook (sb_free_security, sb); +kfree (sb->s_security); + sb->s_security =NULL ; }void security_free_mnt_opts (void **mnt_opts)security/selinux/hooks.c +22
-36@@ -322,7 +322,7 @@ static void inode_free_security(struct inode *inode)if (!isec)return ; - sbsec = inode->i_sb->s_security; + sbsec =selinux_superblock (inode->i_sb);/* * As not all inode security structures are in a list, we check for * empty list outside of the lock to make sure that we won't waste @@ -340,13 +340,6 @@ static void inode_free_security(struct inode *inode) } } -static void superblock_free_security (struct super_block *sb) -{ -struct superblock_security_struct *sbsec = sb->s_security; - sb->s_security =NULL ; -kfree (sbsec); -} -struct selinux_mnt_opts {const char *fscontext, *context, *rootcontext, *defcontext; };@@ -458,7 +451,7 @@ static int selinux_is_genfs_special_handling(struct super_block *sb)static int selinux_is_sblabel_mnt (struct super_block *sb) { -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);/* * IMPORTANT: Double-check logic in this function when adding a new @@ -486,7 +479,7 @@ static int selinux_is_sblabel_mnt(struct super_block *sb)static int sb_finish_set_opts (struct super_block *sb) { -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);struct dentry *root = sb->s_root;struct inode *root_inode =d_backing_inode (root);int rc =0 ;@@ -599,7 +592,7 @@ static int selinux_set_mnt_opts(struct super_block *sb, unsigned long *set_kern_flags) {const struct cred *cred =current_cred (); -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);struct dentry *root = sb->s_root;struct selinux_mnt_opts *opts = mnt_opts;struct inode_security_struct *root_isec;@@ -836,8 +829,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,static int selinux_cmp_sb_context (const struct super_block *oldsb,const struct super_block *newsb) { -struct superblock_security_struct *old = oldsb->s_security; -struct superblock_security_struct *new = newsb->s_security; +struct superblock_security_struct *old =selinux_superblock (oldsb); +struct superblock_security_struct *new =selinux_superblock (newsb);char oldflags = old->flags &SE_MNTMASK ;char newflags = new->flags &SE_MNTMASK ;@@ -869,8 +862,9 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, unsigned long *set_kern_flags) {int rc =0 ; -const struct superblock_security_struct *oldsbsec = oldsb->s_security; -struct superblock_security_struct *newsbsec = newsb->s_security; +const struct superblock_security_struct *oldsbsec = +selinux_superblock (oldsb); +struct superblock_security_struct *newsbsec =selinux_superblock (newsb);int set_fscontext = (oldsbsec->flags &FSCONTEXT_MNT );int set_context = (oldsbsec->flags &CONTEXT_MNT );@@ -1049,7 +1043,7 @@ static int show_sid(struct seq_file *m, u32 sid)static int selinux_sb_show_options (struct seq_file *m,struct super_block *sb) { -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);int rc;if (!(sbsec->flags &SE_SBINITIALIZED ))@@ -1399,7 +1393,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentif (isec->sclass ==SECCLASS_FILE ) isec->sclass =inode_mode_to_security_class (inode->i_mode); - sbsec = inode->i_sb->s_security; + sbsec =selinux_superblock (inode->i_sb);if (!(sbsec->flags &SE_SBINITIALIZED )) {/* Defer initialization until selinux_complete_init, after the initial policy is loaded and the security @@ -1750,7 +1744,8 @@ selinux_determine_inode_label(const struct task_security_struct *tsec, const struct qstr *name, u16 tclass, u32 *_new_isid) { - const struct superblock_security_struct *sbsec = dir->i_sb->s_security; + const struct superblock_security_struct *sbsec = +selinux_superblock (dir->i_sb);if ((sbsec->flags &SE_SBINITIALIZED ) && (sbsec->behavior ==SECURITY_FS_USE_MNTPOINT )) {@@ -1781,7 +1776,7 @@ static int may_create(struct inode *dir,int rc; dsec =inode_security (dir); - sbsec = dir->i_sb->s_security; + sbsec =selinux_superblock (dir->i_sb); sid = tsec->sid;@@ -1930,7 +1925,7 @@ static int superblock_has_perm(const struct cred *cred,struct superblock_security_struct *sbsec;u32 sid =cred_sid (cred); - sbsec = sb->s_security; + sbsec =selinux_superblock (sb);return avc_has_perm (&selinux_state, sid, sbsec->sid,SECCLASS_FILESYSTEM , perms, ad); }@@ -2559,11 +2554,7 @@ static void selinux_bprm_committed_creds(struct linux_binprm *bprm)static int selinux_sb_alloc_security (struct super_block *sb) { -struct superblock_security_struct *sbsec; - - sbsec =kzalloc (sizeof (struct superblock_security_struct ),GFP_KERNEL ); -if (!sbsec) -return -ENOMEM ; +struct superblock_security_struct *sbsec =selinux_superblock (sb);mutex_init (&sbsec->lock);INIT_LIST_HEAD (&sbsec->isec_head);@@ -2571,16 +2562,10 @@ static int selinux_sb_alloc_security(struct super_block *sb) sbsec->sid =SECINITSID_UNLABELED ; sbsec->def_sid =SECINITSID_FILE ; sbsec->mntpoint_sid =SECINITSID_UNLABELED ; - sb->s_security = sbsec;return 0 ; } -static void selinux_sb_free_security (struct super_block *sb) -{ -superblock_free_security (sb); -} -static inline int opt_len (const char *s) {bool open_quote = false;@@ -2659,7 +2644,7 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)static int selinux_sb_remount (struct super_block *sb,void *mnt_opts) {struct selinux_mnt_opts *opts = mnt_opts; -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);u32 sid;int rc;@@ -2897,7 +2882,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,int rc;char *context; - sbsec = dir->i_sb->s_security; + sbsec =selinux_superblock (dir->i_sb); newsid = tsec->create_sid;@@ -3142,7 +3127,7 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,if (!selinux_initialized (&selinux_state))return (inode_owner_or_capable (inode) ?0 : -EPERM ); - sbsec = inode->i_sb->s_security; + sbsec =selinux_superblock (inode->i_sb);if (!(sbsec->flags &SBLABEL_MNT ))return -EOPNOTSUPP ;@@ -3384,13 +3369,14 @@ static int selinux_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags) { struct inode_security_struct *isec = inode_security_novalidate(inode); -struct superblock_security_struct *sbsec = inode->i_sb->s_security; +struct superblock_security_struct *sbsec;u32 newsid;int rc;if (strcmp (name,XATTR_SELINUX_SUFFIX ))return -EOPNOTSUPP ; + sbsec =selinux_superblock (inode->i_sb);if (!(sbsec->flags &SBLABEL_MNT ))return -EOPNOTSUPP ;@@ -6882,6 +6868,7 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { .lbs_inode = sizeof(struct inode_security_struct), .lbs_ipc = sizeof(struct ipc_security_struct), .lbs_msg_msg = sizeof(struct msg_security_struct), + .lbs_superblock = sizeof(struct superblock_security_struct), };#ifdef CONFIG_PERF_EVENTS @@ -6982,7 +6969,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (bprm_committing_creds, selinux_bprm_committing_creds),LSM_HOOK_INIT (bprm_committed_creds, selinux_bprm_committed_creds), -LSM_HOOK_INIT (sb_free_security, selinux_sb_free_security),LSM_HOOK_INIT (sb_free_mnt_opts, selinux_free_mnt_opts),LSM_HOOK_INIT (sb_remount, selinux_sb_remount), LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),security/selinux/include/objsec.h +6
-0@@ -188,4 +188,10 @@ static inline u32 current_sid(void) return tsec->sid; } +static inline struct superblock_security_struct *selinux_superblock ( +const struct super_block *superblock) +{ +return superblock->s_security + selinux_blob_sizes.lbs_superblock; +} +#endif /* _SELINUX_OBJSEC_H_ */ security/selinux/ss/services.c +2
-1@@ -47,6 +47,7 @@#include <linux/sched.h> #include <linux/audit.h> #include <linux/vmalloc.h> +#include <linux/lsm_hooks.h> #include <net/netlabel.h> #include "flask.h" @@ -2873,7 +2874,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb)struct sidtab *sidtab;int rc =0 ;struct ocontext *c; -struct superblock_security_struct *sbsec = sb->s_security; +struct superblock_security_struct *sbsec =selinux_superblock (sb);const char *fstype = sb->s_type->name;if (!selinux_initialized (state)) {security/smack/smack.h +6
-0@@ -357,6 +357,12 @@ static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)return ipc->security + smack_blob_sizes.lbs_ipc; } +static inline struct superblock_smack *smack_superblock ( +const struct super_block *superblock) +{ +return superblock->s_security + smack_blob_sizes.lbs_superblock; +} +/* * Is the directory transmuting? */ security/smack/smack_lsm.c +9
-26@@ -535,12 +535,7 @@ static int smack_syslog(int typefrom_file)*/ static int smack_sb_alloc_security (struct super_block *sb) { -struct superblock_smack *sbsp; - - sbsp =kzalloc (sizeof (struct superblock_smack ),GFP_KERNEL ); - -if (sbsp ==NULL ) -return -ENOMEM ; +struct superblock_smack *sbsp =smack_superblock (sb); sbsp->smk_root = &smack_known_floor; sbsp->smk_default = &smack_known_floor;@@ -549,22 +544,10 @@ static int smack_sb_alloc_security(struct super_block *sb)/* * SMK_SB_INITIALIZED will be zero from kzalloc. */ - sb->s_security = sbsp;return 0 ; } -/** -* smack_sb_free_security - free a superblock blob -* @sb: the superblock getting the blob -* -*/ -static void smack_sb_free_security (struct super_block *sb) -{ -kfree (sb->s_security); - sb->s_security =NULL ; -} -struct smack_mnt_opts {const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute; };@@ -772,7 +755,7 @@ static int smack_set_mnt_opts(struct super_block *sb, {struct dentry *root = sb->s_root;struct inode *inode =d_backing_inode (root); -struct superblock_smack *sp = sb->s_security; +struct superblock_smack *sp =smack_superblock (sb);struct inode_smack *isp;struct smack_known *skp;struct smack_mnt_opts *opts = mnt_opts;@@ -871,7 +854,7 @@ static int smack_set_mnt_opts(struct super_block *sb,*/ static int smack_sb_statfs (struct dentry *dentry) { -struct superblock_smack *sbp = dentry->d_sb->s_security; +struct superblock_smack *sbp =smack_superblock (dentry->d_sb);int rc;struct smk_audit_info ad;@@ -905,7 +888,7 @@ static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)if (isp->smk_task ==NULL || isp->smk_task == bsp->smk_task)return 0 ; - sbsp = inode->i_sb->s_security; + sbsp =smack_superblock (inode->i_sb);if ((sbsp->smk_flags &SMK_SB_UNTRUSTED ) && isp->smk_task != sbsp->smk_root)return 0 ;@@ -1157,7 +1140,7 @@ static int smack_inode_rename(struct inode *old_inode,*/ static int smack_inode_permission (struct inode *inode,int mask) { -struct superblock_smack *sbsp = inode->i_sb->s_security; +struct superblock_smack *sbsp =smack_superblock (inode->i_sb);struct smk_audit_info ad;int no_block = mask &MAY_NOT_BLOCK ;int rc;@@ -1398,7 +1381,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name)*/ if (strcmp (name,XATTR_NAME_SMACK ) ==0 ) {struct super_block *sbp = dentry->d_sb; -struct superblock_smack *sbsp = sbp->s_security; +struct superblock_smack *sbsp =smack_superblock (sbp); isp->smk_inode = sbsp->smk_default; }else if (strcmp (name,XATTR_NAME_SMACKEXEC ) ==0 )@@ -1668,7 +1651,7 @@ static int smack_mmap_file(struct file *file, isp =smack_inode (file_inode (file));if (isp->smk_mmap ==NULL )return 0 ; - sbsp =file_inode (file)->i_sb->s_security; + sbsp =smack_superblock (file_inode (file)->i_sb);if (sbsp->smk_flags &SMK_SB_UNTRUSTED && isp->smk_mmap != sbsp->smk_root)return -EACCES ;@@ -3283,7 +3266,7 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)return ; sbp = inode->i_sb; - sbsp = sbp->s_security; + sbsp =smack_superblock (sbp);/* * We're going to use the superblock default label * if there's no label on the file. @@ -4696,6 +4679,7 @@ struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = { .lbs_inode = sizeof(struct inode_smack), .lbs_ipc = sizeof(struct smack_known *), .lbs_msg_msg = sizeof(struct smack_known *), + .lbs_superblock = sizeof(struct superblock_smack), }; static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {@@ -4707,7 +4691,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {LSM_HOOK_INIT (fs_context_parse_param, smack_fs_context_parse_param),LSM_HOOK_INIT (sb_alloc_security, smack_sb_alloc_security), -LSM_HOOK_INIT (sb_free_security, smack_sb_free_security),LSM_HOOK_INIT (sb_free_mnt_opts, smack_free_mnt_opts),LSM_HOOK_INIT (sb_eat_lsm_opts, smack_sb_eat_lsm_opts), LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),Signature · 1 line
2.30.0
Casey Schaufler
maintainerRe: [PATCH v28 05/12] LSM: Infrastructure management of the superblock
Quoted 9 lines · Serge E. Hallyn
Quoted 8 lines · Mickaël Salaün
From: Casey Schaufler <[email protected]>
Move management of the
superblock->sb_securityblob out of the individual security modules and into the security infrastructure. Instead of allocating the blobs from within the modules, the modules tell the infrastructure how much space is required, and the space is allocated there.
Quoted 8 lines
Quoted 5 lines
Cc Kees Cook <[email protected]>Cc John Johansen <[email protected]>Signed-off-by Casey Schaufler <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Stephen Smalley <[email protected]>Acked-by Serge Hallyn <[email protected]>I wonder how many out of tree modules this will impact :)
There are several blobs that have already been converted to infrastructure management. Not a peep from out-of-tree module developers/maintainers. I can only speculate that OOT modules are either less common than we may think, using alternative data management models (as does eBPF) or sticking with very old kernels. It's also possible that they're suffering in silence, which would be sad because every module that's worth having should be in the tree.
Quoted 4 lines
Actually
if some new incoming module does an rcu callback to free thesb_security, then thesecurity_sb_freewill need an update, but that seems unlikely.
We're already doing that for the inode blob, so it's really just a small matter of cut-n-paste and s/inode/sb/ to make that happen.
Casey Schaufler
maintainerRe: [PATCH v28 05/12] LSM: Infrastructure management of the superblock
In reply to Serge E. HallynQuoted 9 lines · Serge E. Hallyn
Quoted 8 lines · Mickaël Salaün
From: Casey Schaufler <[email protected]>
Move management of the
superblock->sb_securityblob out of the individual security modules and into the security infrastructure. Instead of allocating the blobs from within the modules, the modules tell the infrastructure how much space is required, and the space is allocated there.
Quoted 8 lines
Quoted 5 lines
Cc Kees Cook <[email protected]>Cc John Johansen <[email protected]>Signed-off-by Casey Schaufler <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Stephen Smalley <[email protected]>Acked-by Serge Hallyn <[email protected]>I wonder how many out of tree modules this will impact :)
There are several blobs that have already been converted to infrastructure management. Not a peep from out-of-tree module developers/maintainers. I can only speculate that OOT modules are either less common than we may think, using alternative data management models (as does eBPF) or sticking with very old kernels. It's also possible that they're suffering in silence, which would be sad because every module that's worth having should be in the tree.
Quoted 4 lines
Actually
if some new incoming module does an rcu callback to free thesb_security, then thesecurity_sb_freewill need an update, but that seems unlikely.
We're already doing that for the inode blob, so it's really just a small matter of cut-n-paste and s/inode/sb/ to make that happen.
Mickaël Salaün
maintainerPATCH v28 6/12 fs,security: Add sb_delete hook
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
The sb_delete security hook is called when shutting down a superblock, which may be useful to release kernel objects tied to the superblock's lifetime (e.g. inodes).
This new hook is needed by Landlock to release (ephemerally) tagged struct inodes. This comes from the unprivileged nature of Landlock described in the next commit.
---
Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>
Changes since v17:
* Initial patch to replace the direct call to landlock_release_inodes() (requested by James Morris).
https://lore.kernel.org/lkml/[email protected]/
--- fs/super.c | 1 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 2 ++ include/linux/security.h | 4 ++++ security/security.c | 5 +++++ 5 files changed, 13 insertions(+)
fs/super.c +1 -0
@@ -454,6 +454,7 @@ void generic_shutdown_super(struct super_block *sb)evict_inodes (sb);/* only nonzero refcount inodes can have marks */ fsnotify_sb_delete (sb); +security_sb_delete (sb);if (sb->s_dio_done_wq) {destroy_workqueue (sb->s_dio_done_wq);
include/linux/lsm_hook_defs.h +1 -0
@@ -59,6 +59,7 @@ LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc, LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc, struct fs_parameter *param) LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb) +LSM_HOOK(void, LSM_RET_VOID, sb_delete, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts)LSM_HOOK (int,0 , sb_eat_lsm_opts, char *orig, void **mnt_opts)
include/linux/lsm_hooks.h +2 -0
@@ -108,6 +108,8 @@* allocated. * @sb contains the super_block structure to be modified. * Return 0 if operation was successful. +* @sb_delete: +* Release objects tied to a superblock (e.g. inodes). * @sb_free_security: * Deallocate and clear the sb->s_security field. * @sb contains the super_block structure to be modified.
include/linux/security.h +4 -0
@@ -288,6 +288,7 @@ void security_bprm_committed_creds(struct linux_binprm *bprm);int security_fs_context_dup (struct fs_context *fc,struct fs_context *src_fc);int security_fs_context_parse_param (struct fs_context *fc,struct fs_parameter *param);int security_sb_alloc (struct super_block *sb); +void security_sb_delete (struct super_block *sb);void security_sb_free (struct super_block *sb);void security_free_mnt_opts (void **mnt_opts);int security_sb_eat_lsm_opts (char *options,void **mnt_opts);
@@ -620,6 +621,9 @@ static inline int security_sb_alloc(struct super_block *sb)return 0 ; } +static inline void security_sb_delete (struct super_block *sb) +{ } +static inline void security_sb_free (struct super_block *sb) { }
security/security.c +5 -0
@@ -900,6 +900,11 @@ int security_sb_alloc(struct super_block *sb)return rc; } +void security_sb_delete (struct super_block *sb) +{ +call_void_hook (sb_delete, sb); +} +void security_sb_free (struct super_block *sb) {call_void_hook (sb_free_security, sb);
Signature · 1 line
2.30.0
Re: [PATCH v28 06/12] fs,security: Add sb_delete hook
Quoted 6 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
The
sb_deletesecurity hook is called when shutting down a superblock, which may be useful to release kernel objects tied to the superblock's lifetime (e.g. inodes).
Quoted 8 lines
This new hook is needed by Landlock to release (ephemerally) tagged struct inodes. This comes from the unprivileged nature of Landlock described in the next commit.
Cc Al Viro <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
One note below, but
Quoted 48 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v17:
* Initial patch to replace the direct call tolandlock_release_inodes()(requested by James Morris).
https://lore.kernel.org/lkml/[email protected]/--- fs/super.c | 1 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 2 ++ include/linux/security.h | 4 ++++ security/security.c | 5 +++++ 5 files changed, 13 insertions(+)fs/super.c +1
-0@@ -454,6 +454,7 @@ void generic_shutdown_super(struct super_block *sb)evict_inodes (sb);/* only nonzero refcount inodes can have marks */ fsnotify_sb_delete (sb); +security_sb_delete (sb);if (sb->s_dio_done_wq) {destroy_workqueue (sb->s_dio_done_wq);include/linux/lsm_hook_defs.h +1
-0@@ -59,6 +59,7 @@ LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc, LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc, struct fs_parameter *param) LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb) +LSM_HOOK(void, LSM_RET_VOID, sb_delete, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts)LSM_HOOK (int,0 , sb_eat_lsm_opts, char *orig, void **mnt_opts)include/linux/lsm_hooks.h +0
-0
include/linux/lsm_hooks.h line 112
Quoted 5 lines
include/linux/lsm_hooks.h +2
-0@@ -108,6 +108,8 @@* allocated. * @sb contains the super_block structure to be modified. * Return 0 if operation was successful. +* @sb_delete: +* Release objects tied to a superblock (e.g. inodes).
It's customary here to add the line detailing the @sb argument.
Quoted 43 lines
include/linux/lsm_hooks.h +0
-0* @sb_free_security: * Deallocate and clear the sb->s_security field. * @sb contains the super_block structure to be modified. include/linux/security.h +4
-0@@ -288,6 +288,7 @@ void security_bprm_committed_creds(struct linux_binprm *bprm);int security_fs_context_dup (struct fs_context *fc,struct fs_context *src_fc);int security_fs_context_parse_param (struct fs_context *fc,struct fs_parameter *param);int security_sb_alloc (struct super_block *sb); +void security_sb_delete (struct super_block *sb);void security_sb_free (struct super_block *sb);void security_free_mnt_opts (void **mnt_opts);int security_sb_eat_lsm_opts (char *options,void **mnt_opts);@@ -620,6 +621,9 @@ static inline int security_sb_alloc(struct super_block *sb)return 0 ; } +static inline void security_sb_delete (struct super_block *sb) +{ } +static inline void security_sb_free (struct super_block *sb) { }security/security.c +5
-0@@ -900,6 +900,11 @@ int security_sb_alloc(struct super_block *sb)return rc; } +void security_sb_delete (struct super_block *sb) +{ +call_void_hook (sb_delete, sb); +} +void security_sb_free (struct super_block *sb) {call_void_hook (sb_free_security, sb);Signature · 1 line
2.30.0
Mickaël Salaün
maintainerRe: [PATCH v28 06/12] fs,security: Add sb_delete hook
Quoted 67 lines · Serge E. Hallyn
Quoted 6 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
The
sb_deletesecurity hook is called when shutting down a superblock, which may be useful to release kernel objects tied to the superblock's lifetime (e.g. inodes).Quoted 8 lines
This new hook is needed by Landlock to release (ephemerally) tagged struct inodes. This comes from the unprivileged nature of Landlock described in the next commit.
Cc Al Viro <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>One note below, but
Acked-by Serge Hallyn <[email protected]>Quoted 47 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v17:
* Initial patch to replace the direct call tolandlock_release_inodes()(requested by James Morris).
https://lore.kernel.org/lkml/[email protected]/--- fs/super.c | 1 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 2 ++ include/linux/security.h | 4 ++++ security/security.c | 5 +++++ 5 files changed, 13 insertions(+)fs/super.c +1
-0@@ -454,6 +454,7 @@ void generic_shutdown_super(struct super_block *sb)evict_inodes (sb);/* only nonzero refcount inodes can have marks */ fsnotify_sb_delete (sb); +security_sb_delete (sb);if (sb->s_dio_done_wq) {destroy_workqueue (sb->s_dio_done_wq);include/linux/lsm_hook_defs.h +1
-0@@ -59,6 +59,7 @@ LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc, LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc, struct fs_parameter *param) LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb) +LSM_HOOK(void, LSM_RET_VOID, sb_delete, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts)LSM_HOOK (int,0 , sb_eat_lsm_opts, char *orig, void **mnt_opts)include/linux/lsm_hooks.h +0
-0
Quoted 8 lines
include/linux/lsm_hooks.h line 112
Quoted 6 lines
include/linux/lsm_hooks.h +2
-0@@ -108,6 +108,8 @@* allocated. * @sb contains the super_block structure to be modified. * Return 0 if operation was successful. +* @sb_delete: +* Release objects tied to a superblock (e.g. inodes). It's customary here to add the line detailing the @sb argument.
What about "@sb contains the super_block structure being released."?
Quoted 44 lines
Quoted 44 lines
include/linux/lsm_hooks.h +0
-0* @sb_free_security: * Deallocate and clear the sb->s_security field. * @sb contains the super_block structure to be modified. include/linux/security.h +4
-0@@ -288,6 +288,7 @@ void security_bprm_committed_creds(struct linux_binprm *bprm);int security_fs_context_dup (struct fs_context *fc,struct fs_context *src_fc);int security_fs_context_parse_param (struct fs_context *fc,struct fs_parameter *param);int security_sb_alloc (struct super_block *sb); +void security_sb_delete (struct super_block *sb);void security_sb_free (struct super_block *sb);void security_free_mnt_opts (void **mnt_opts);int security_sb_eat_lsm_opts (char *options,void **mnt_opts);@@ -620,6 +621,9 @@ static inline int security_sb_alloc(struct super_block *sb)return 0 ; } +static inline void security_sb_delete (struct super_block *sb) +{ } +static inline void security_sb_free (struct super_block *sb) { }security/security.c +5
-0@@ -900,6 +900,11 @@ int security_sb_alloc(struct super_block *sb)return rc; } +void security_sb_delete (struct super_block *sb) +{ +call_void_hook (sb_delete, sb); +} +void security_sb_free (struct super_block *sb) {call_void_hook (sb_free_security, sb);Signature · 1 line
2.30.0
Serge E. Hallyn
maintainerRe: [PATCH v28 06/12] fs,security: Add sb_delete hook
Quoted 78 lines · Mickaël Salaün
Quoted 67 lines · Serge E. Hallyn
Quoted 6 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
The
sb_deletesecurity hook is called when shutting down a superblock, which may be useful to release kernel objects tied to the superblock's lifetime (e.g. inodes).Quoted 8 lines
This new hook is needed by Landlock to release (ephemerally) tagged struct inodes. This comes from the unprivileged nature of Landlock described in the next commit.
Cc Al Viro <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>One note below, but
Acked-by Serge Hallyn <[email protected]>Quoted 47 lines
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
Changes since v22:
* Add Reviewed-by: Jann Horn <[email protected]>Changes since v17:
* Initial patch to replace the direct call tolandlock_release_inodes()(requested by James Morris).
https://lore.kernel.org/lkml/[email protected]/--- fs/super.c | 1 + include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 2 ++ include/linux/security.h | 4 ++++ security/security.c | 5 +++++ 5 files changed, 13 insertions(+)fs/super.c +1
-0@@ -454,6 +454,7 @@ void generic_shutdown_super(struct super_block *sb)evict_inodes (sb);/* only nonzero refcount inodes can have marks */ fsnotify_sb_delete (sb); +security_sb_delete (sb);if (sb->s_dio_done_wq) {destroy_workqueue (sb->s_dio_done_wq);include/linux/lsm_hook_defs.h +1
-0@@ -59,6 +59,7 @@ LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc, LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc, struct fs_parameter *param) LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb) +LSM_HOOK(void, LSM_RET_VOID, sb_delete, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb) LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts)LSM_HOOK (int,0 , sb_eat_lsm_opts, char *orig, void **mnt_opts)include/linux/lsm_hooks.h +0
-0Quoted 8 lines
include/linux/lsm_hooks.h line 112
Quoted 6 lines
include/linux/lsm_hooks.h +2
-0@@ -108,6 +108,8 @@* allocated. * @sb contains the super_block structure to be modified. * Return 0 if operation was successful. +* @sb_delete: +* Release objects tied to a superblock (e.g. inodes). It's customary here to add the line detailing the @sb argument.
Quoted 1 line
What about "@sb contains the
super_blockstructure being released."?
That's good. Thanks.
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
include/linux/lsm_hooks.h +0
-0* @sb_free_security: * Deallocate and clear the sb->s_security field. * @sb contains the super_block structure to be modified. include/linux/security.h +4
-0@@ -288,6 +288,7 @@ void security_bprm_committed_creds(struct linux_binprm *bprm);int security_fs_context_dup (struct fs_context *fc,struct fs_context *src_fc);int security_fs_context_parse_param (struct fs_context *fc,struct fs_parameter *param);int security_sb_alloc (struct super_block *sb); +void security_sb_delete (struct super_block *sb);void security_sb_free (struct super_block *sb);void security_free_mnt_opts (void **mnt_opts);int security_sb_eat_lsm_opts (char *options,void **mnt_opts);@@ -620,6 +621,9 @@ static inline int security_sb_alloc(struct super_block *sb)return 0 ; } +static inline void security_sb_delete (struct super_block *sb) +{ } +static inline void security_sb_free (struct super_block *sb) { }security/security.c +5
-0@@ -900,6 +900,11 @@ int security_sb_alloc(struct super_block *sb)return rc; } +void security_sb_delete (struct super_block *sb) +{ +call_void_hook (sb_delete, sb); +} +void security_sb_free (struct super_block *sb) {call_void_hook (sb_free_security, sb);Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 7/12 landlock: Support filesystem access-control
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
Thanks to the Landlock objects and ruleset, it is possible to identify inodes according to a process's domain. To enable an unprivileged process to express a file hierarchy, it first needs to open a directory (or a file) and pass this file descriptor to the kernel through landlock_add_rule(2). When checking if a file access request is allowed, we walk from the requested dentry to the real root, following the different mount layers. The access to each "tagged" inodes are collected according to their rule layer level, and ANDed to create access to the requested file hierarchy. This makes possible to identify a lot of files without tagging every inodes nor modifying the filesystem, while still following the view and understanding the user has from the filesystem.
Add a new ARCH_EPHEMERAL_INODES for UML because it currently does not keep the same struct inodes for the same inodes whereas these inodes are in use.
This commit adds a minimal set of supported filesystem access-control which doesn't enable to restrict all file-related actions. This is the result of multiple discussions to minimize the code of Landlock to ease review. Thanks to the Landlock design, extending this access-control without breaking user space will not be a problem. Moreover, seccomp filters can be used to restrict the use of syscall families which may not be currently handled by Landlock.
---
Changes since v27:
* Fix domains with layers of non-overlapping access rights (cf. layout1.non_overlapping_accesses test) thanks to a stack of access rights per layer (replacing ORed access rights). This avoids too-restrictive domains.
* Cosmetic fixes and updates in comments and Kconfig.
Changes since v26:
* Check each rule of a path to enable a more permissive and pragmatic access control per layer. Suggested by Jann Horn:
https://lore.kernel.org/lkml/CAG48ez1O0VTwEiRd3KqexoF78WR+cmP5bGk5Kh5Cs7aPepiDVg@mail.gmail.com/
* Rename check_access_path_continue() to unmask_layers() and make it return the new layer mask.
* Avoid double domain check in hook_file_open().
* In the documentation, add utime(2) as another example of unhandled syscalls. Indeed, using touch to test write access may be tempting.
* Remove outdated comment about OverlayFS.
* Rename the landlock.h ifdef to align with most similar files.
* Fix spelling.
Changes since v25:
* Move build_check_layer() to ruleset.c, and add built-time checks for the fs_access_mask and access variables according to _LANDLOCK_ACCESS_FS_MASK.
* Move limits to a dedicated file and rename them:
_LANDLOCK_ACCESS_FS_LAST and _LANDLOCK_ACCESS_FS_MASK.
* Set build_check_layer() as non-inline to trigger a warning if it is not called.
* Use BITS_PER_TYPE() macro.
* Rename function to landlock_add_fs_hooks().
* Cosmetic variable renames.
Changes since v24:
* Use the new struct landlock_rule and landlock_layer to not mix accesses from different layers. Revert "Enforce deterministic interleaved path rules" from v24, and fix the layer check. This enables to follow a sane semantic: an access is granted if, for each policy layer, at least one rule encountered on the pathwalk grants the access, regardless of their position in the layer stack (suggested by Jann Horn). See layout1.interleaved_masked_accesses tests from tools/testing/selftests/landlock/fs_test.c for corner cases.
* Add build-time checks for layers.
* Use the new landlock_insert_rule() API.
Changes since v23:
* Enforce deterministic interleaved path rules. To have consistent layered rules, granting access to a path implies that all accesses tied to inodes, from the requested file to the real root, must be checked. Otherwise, stacked rules may result to overzealous restrictions. By excluding the ability to add exceptions in the same layer (e.g. /a allowed, /a/b denied, and /a/b/c allowed), we get deterministic interleaved path rules. This removes an optimization which could be replaced by a proper cache mechanism. This also further simplifies and explain check_access_path_continue().
* Fix memory allocation error handling in landlock_create_object() calls. This prevent to inadvertently hold an inode.
* In get_inode_object(), improve comments, make code more readable and move kfree() call out of the lock window.
* Use the simplified landlock_insert_rule() API.
Changes since v22:
* Simplify check_access_path_continue() (suggested by Jann Horn).
* Remove prefetch() call for now (suggested by Jann Horn).
* Fix spelling and remove superfluous comment (spotted by Jann Horn).
* Cosmetic variable renaming.
Changes since v21:
* Rename ARCH_EPHEMERAL_STATES to ARCH_EPHEMERAL_INODES (suggested by James Morris).
* Remove the LANDLOCK_ACCESS_FS_CHROOT right because chroot(2) (which requires CAP_SYS_CHROOT) doesn't enable to bypass Landlock (as tests demonstrate it), and because it is often used by sandboxes, it would be counterproductive to forbid it. This also reduces the code size.
* Clean up documentation.
Changes since v19:
* Fix spelling (spotted by Randy Dunlap).
Changes since v18:
* Remove useless include.
* Fix spelling.
Changes since v17:
* Replace landlock_release_inodes() with security_sb_delete() (requested by James Morris).
* Replace struct super_block->s_landlock_inode_refs with the LSM infrastructure management of the superblock (requested by James Morris).
* Fix mknod restriction with a zero mode (spotted by Vincent Dagonneau).
* Minimize executed code in path_mknod and file_open hooks when the current tasks is not sandboxed.
* Remove useless checks on the file pointer and inode in hook_file_open() .
* Constify domain pointers.
* Rename inode_landlock() to landlock_inode().
* Import include/uapi/linux/landlock.h and _LANDLOCK_ACCESS_FS_* from the ruleset and domain management patch.
* Explain the rational of this minimal set of access-control.
https://lore.kernel.org/lkml/[email protected]/
Changes since v16:
* Add ARCH_EPHEMERAL_STATES and enable it for UML.
Changes since v15:
* Replace layer_levels and layer_depth with a bitfield of layers: this enables to properly manage superset and subset of access rights, whatever their order in the stack of layers.
Cf. https://lore.kernel.org/lkml/[email protected]/
* Allow to open pipes and similar special files through /proc/self/fd/.
* Properly handle internal filesystems such as nsfs: always allow these kind of roots because disconnected path cannot be evaluated.
* Remove the LANDLOCK_ACCESS_FS_LINK_TO and LANDLOCK_ACCESS_FS_RENAME_{TO,FROM}, but use the LANDLOCK_ACCESS_FS_REMOVE_{FILE,DIR} and LANDLOCK_ACCESS_FS_MAKE_* instead. Indeed, it is not possible for now (and not really useful) to express the semantic of a source and a destination.
* Check access rights to remove a directory or a file with rename(2).
* Forbid reparenting when linking or renaming. This is needed to easily protect against possible privilege escalation by changing the place of a file or directory in relation to an enforced access policy (from the set of layers). This will be relaxed in the future.
* Update hooks to take into account replacement of the object's self and beneath access bitfields with one. Simplify the code.
* Check file related access rights.
* Check d_is_negative() instead of !d_backing_inode() in check_access_path_continue(), and continue the path walk while there is no mapped inode e.g., with rename(2).
* Check private inode in check_access_path().
* Optimize get_file_access() when dealing with a directory.
* Add missing atomic.h .
Changes since v14:
* Simplify the object, rule and ruleset management at the expense of a
less aggressive memory freeing (contributed by Jann Horn, with
additional modifications):
- Rewrite release_inode() to use inode->sb->s_landlock_inode_refs.
- Remove useless checks in landlock_release_inodes(), clean object
pointer according to the new struct landlock_object and wait for all
iput() to complete.
- Rewrite get_inode_object() according to the new struct
landlock_object. If there is a race-condition when cleaning up an
object, we retry until the concurrent thread finished the object
cleaning.
Cf. https://lore.kernel.org/lkml/CAG48ez21bEn0wL1bbmTiiu8j9jP5iEWtHOwz4tURUJ+ki0ydYw@mail.gmail.com/
* Fix nested domains by implementing a notion of layer level and depth:
- Check for matching level ranges when walking through a file path.
- Only allow access if every layer granted the access request.
* Handles files without mount points (e.g. pipes).
* Hardens path walk by checking inode pointer values.
* Prefetches d_parent when walking to the root directory.
* Remove useless inode_alloc_security hook() (suggested by Jann Horn):
already initialized by lsm_inode_alloc().
* Remove the inode_free_security hook.
* Remove access checks that may be required for FD-only requests:
truncate, getattr, lock, chmod, chown, chgrp, ioctl. This will be
handle in a future evolution of Landlock, but right now the goal is to
lighten the code to ease review.
* Constify variables.
* Move ABI checks into syscall.c .
* Cosmetic variable renames.
Changes since v11:
* Add back, revamp and make a fully working filesystem access-control based on paths and inodes.
* Remove the eBPF dependency.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 1 + arch/Kconfig | 7 + arch/um/Kconfig | 1 + include/uapi/linux/landlock.h | 75 ++++ security/landlock/Kconfig | 2 +- security/landlock/Makefile | 2 +- security/landlock/fs.c | 627 ++++++++++++++++++++++++++++++++++ security/landlock/fs.h | 56 +++ security/landlock/limits.h | 4 + security/landlock/ruleset.c | 4 + security/landlock/setup.c | 7 + security/landlock/setup.h | 2 + 12 files changed, 786 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/landlock.h create mode 100644 security/landlock/fs.c create mode 100644 security/landlock/fs.h
MAINTAINERS +1 -0
@@ -9942,6 +9942,7 @@ L: [email protected] S: Supported W: https://landlock.io T: git https://github.com/landlock-lsm/linux.git +F: include/uapi/linux/landlock.h F: security/landlock/ K: landlock K: LANDLOCK
arch/Kconfig +7 -0
@@ -946,6 +946,13 @@ config COMPAT_32BIT_TIME config ARCH_NO_PREEMPT bool +config ARCH_EPHEMERAL_INODES + def_bool n + help + An arch should select this symbol if it doesn't keep track of inode + instances on its own, but instead relies on something else (e.g. the + host kernel for an UML kernel). + config ARCH_SUPPORTS_RT bool
arch/um/Kconfig +1 -0
@@ -5,6 +5,7 @@ menu "UML-specific options" config UML bool default y + select ARCH_EPHEMERAL_INODES select ARCH_HAS_KCOV select ARCH_NO_PREEMPT select HAVE_ARCH_AUDITSYSCALL
include/uapi/linux/landlock.h new file +75 -0
75 lines not on this page · raw message
security/landlock/Kconfig +1 -1
@@ -2,7 +2,7 @@ config SECURITY_LANDLOCK bool "Landlock support" - depends on SECURITY + depends on SECURITY && !ARCH_EPHEMERAL_INODES select SECURITY_PATH help Landlock is a safe sandboxing mechanism that enables processes to
security/landlock/Makefile +1 -1
@@ -1,4 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o object.o ruleset.o \ - cred.o ptrace.o + cred.o ptrace.o fs.o
security/landlock/fs.c new file +627 -0
627 lines not on this page · raw message
security/landlock/fs.h new file +56 -0
56 lines not on this page · raw message
security/landlock/limits.h +4 -0
@@ -10,8 +10,12 @@#define _SECURITY_LANDLOCK_LIMITS_H#include <linux/limits.h> +#include <uapi/linux/landlock.h> #define LANDLOCK_MAX_NUM_LAYERS 64#define LANDLOCK_MAX_NUM_RULES U32_MAX +#define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_MAKE_SYM +#define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1) +#endif /* _SECURITY_LANDLOCK_LIMITS_H */
security/landlock/ruleset.c +4 -0
@@ -116,9 +116,11 @@ static void build_check_ruleset(void) .num_rules = ~0, .num_layers = ~0, }; + typeof(ruleset.fs_access_masks[0]) fs_access_mask = ~0;BUILD_BUG_ON (ruleset.num_rules <LANDLOCK_MAX_NUM_RULES );BUILD_BUG_ON (ruleset.num_layers <LANDLOCK_MAX_NUM_LAYERS ); +BUILD_BUG_ON (fs_access_mask <LANDLOCK_MASK_ACCESS_FS ); }/**
@@ -217,9 +219,11 @@ static void build_check_layer(void) {const struct landlock_layer layer = { .level = ~0 , + .access = ~0 , };BUILD_BUG_ON (layer.level <LANDLOCK_MAX_NUM_LAYERS ); +BUILD_BUG_ON (layer.access <LANDLOCK_MASK_ACCESS_FS ); }/* @ruleset must be locked by the caller. */
security/landlock/setup.c +7 -0
@@ -11,17 +11,24 @@#include "common.h" #include "cred.h" +#include "fs.h" #include "ptrace.h" #include "setup.h" +bool landlock_initialized __lsm_ro_after_init = false; + struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = { .lbs_cred =sizeof (struct landlock_cred_security ), + .lbs_inode =sizeof (struct landlock_inode_security ), + .lbs_superblock =sizeof (struct landlock_superblock_security ), };static int __init landlock_init (void ) {landlock_add_cred_hooks ();landlock_add_ptrace_hooks (); +landlock_add_fs_hooks (); + landlock_initialized = true;pr_info ("Up and running.\n" );return 0 ; }
security/landlock/setup.h +2 -0
@@ -11,6 +11,8 @@#include <linux/lsm_hooks.h> +extern bool landlock_initialized; +extern struct lsm_blob_sizes landlock_blob_sizes;#endif /* _SECURITY_LANDLOCK_SETUP_H */
Signature · 1 line
2.30.0
Serge E. Hallyn
maintainerRe: [PATCH v28 07/12] landlock: Support filesystem access-control
Quoted 4 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
Thanks to the Landlock objects and ruleset, it is possible to identify inodes according to a process's domain. To enable an unprivileged
This throws me off a bit. "identify inodes according to a process's domain". What exactly does it mean? "identify" how ?
Quoted 11 lines
process to express a file hierarchy, it first needs to open a directory (or a file) and pass this file descriptor to the kernel through
landlock_add_rule(2). When checking if a file access request is allowed, we walk from the requested dentry to the real root, following the different mount layers. The access to each "tagged" inodes are collected according to their rule layer level, and ANDed to create access to the requested file hierarchy. This makes possible to identify a lot of files without tagging every inodes nor modifying the filesystem, while still following the view and understanding the user has from the filesystem.
Quoted 3 lines
Add a new
ARCH_EPHEMERAL_INODESfor UML because it currently does not keep the same struct inodes for the same inodes whereas these inodes are in use.
-serge
Mickaël Salaün
maintainerRe: [PATCH v28 07/12] landlock: Support filesystem access-control
Quoted 8 lines · Serge E. Hallyn
Quoted 4 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
Thanks to the Landlock objects and ruleset, it is possible to identify inodes according to a process's domain. To enable an unprivileged
This throws me off a bit. "identify inodes according to a process's domain". What exactly does it mean? "identify" how ?
A domain is a set of rules (i.e. layers of rulesets) enforced on a set of threads. Inodes are tagged per domain (i.e. not system-wide) and actions are restricted thanks to these tags, which form rules. It means that the created access-controls are scoped to a set of threads.
Quoted 18 lines
Quoted 12 lines
process to express a file hierarchy, it first needs to open a directory (or a file) and pass this file descriptor to the kernel through
landlock_add_rule(2). When checking if a file access request is allowed, we walk from the requested dentry to the real root, following the different mount layers. The access to each "tagged" inodes are collected according to their rule layer level, and ANDed to create access to the requested file hierarchy. This makes possible to identify a lot of files without tagging every inodes nor modifying the filesystem, while still following the view and understanding the user has from the filesystem.Quoted 3 lines
Add a new
ARCH_EPHEMERAL_INODESfor UML because it currently does not keep the same struct inodes for the same inodes whereas these inodes are in use.-serge
Serge E. Hallyn
maintainerRe: [PATCH v28 07/12] landlock: Support filesystem access-control
Quoted 8 lines · Mickaël Salaün
Quoted 6 lines · Serge E. Hallyn
Quoted 4 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
Thanks to the Landlock objects and ruleset, it is possible to identify inodes according to a process's domain. To enable an unprivileged
Quoted 7 lines
Quoted 2 lines
This throws me off a bit. "identify inodes according to a process's domain". What exactly does it mean? "identify" how ?
A domain is a set of rules (i.e. layers of rulesets) enforced on a set of threads. Inodes are tagged per domain (i.e. not system-wide) and actions are restricted thanks to these tags, which form rules. It means that the created access-controls are scoped to a set of threads.
Thanks, that's helpful. To me it would be much clearer if you used the word 'tagged' :
Using the Landlock objects and ruleset, it is possible to tag inodes according to a process's domain.
Quoted 17 lines
Quoted 17 lines
Quoted 11 lines
process to express a file hierarchy, it first needs to open a directory (or a file) and pass this file descriptor to the kernel through
landlock_add_rule(2). When checking if a file access request is allowed, we walk from the requested dentry to the real root, following the different mount layers. The access to each "tagged" inodes are collected according to their rule layer level, and ANDed to create access to the requested file hierarchy. This makes possible to identify a lot of files without tagging every inodes nor modifying the filesystem, while still following the view and understanding the user has from the filesystem.Quoted 3 lines
Add a new
ARCH_EPHEMERAL_INODESfor UML because it currently does not keep the same struct inodes for the same inodes whereas these inodes are in use.-serge
Mickaël Salaün
maintainerPATCH v28 8/12 landlock: Add syscall implementations
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
These 3 system calls are designed to be used by unprivileged processes to sandbox themselves:
* landlock_create_ruleset(2): Creates a ruleset and returns its file descriptor.
* landlock_add_rule(2): Adds a rule (e.g. file hierarchy access) to a ruleset, identified by the dedicated file descriptor.
* landlock_restrict_self(2): Enforces a ruleset on the calling thread and its future children (similar to seccomp). This syscall has the same usage restrictions as seccomp(2): the caller must have the no_new_privs attribute set or have CAP_SYS_ADMIN in the current user namespace.
All these syscalls have a "flags" argument (not currently used) to enable extensibility.
Here are the motivations for these new syscalls:
* A sandboxed process may not have access to file systems, including /dev, /sys or /proc, but it should still be able to add more restrictions to itself.
* Neither prctl(2) nor seccomp(2) (which was used in a previous version) fit well with the current definition of a Landlock security policy.
All passed structs (attributes) are checked at build time to ensure that they don't contain holes and that they are aligned the same way for each architecture.
See the user and kernel documentation for more details (provided by a following commit):
* Documentation/userspace-api/landlock.rst
* Documentation/security/landlock.rst
---
Changes since v27:
* Forbid creation of rules with an empty allowed_access value because they are now ignored (since v26) in path walks.
* Rename landlock_enforce_ruleset_self(2) to landlock_restrict_self(2):
shorter and consistent with the two other syscalls (i.e. verb + direct object).
* Update ruleset access check according to the new access stack.
* Improve landlock_add_rule(2) documentation.
* Fix comment.
* Remove Reviewed-by Jann Horn because of the above changes.
Changes since v26:
* Rename landlock_enforce_ruleset_current(2) to landlock_enforce_ruleset_self(2). "current" makes sense for a kernel developer, but much less from a user space developer stand point. "self" is widely used to refer to the current task (e.g. /proc/self). "current" may refer to temporal properties, which could be added later to this syscall flags (cf. /proc/self/attr/{current,exec}).
* Simplify build_check_abi().
* Rename syscall.c to syscalls.c .
* Use less ambiguous comments.
* Fix spelling.
Changes since v25:
* Revert build_check_abi() as non-inline to trigger a warning if it is not called.
* Use the new limit names.
Changes since v24:
* Add Reviewed-by: Jann Horn <[email protected]>
* Set build_check_abi() as inline.
Changes since v23:
* Rewrite get_ruleset_from_fd() to please the 0-DAY CI Kernel Test Service that reported an uninitialized variable (false positive):
https://lore.kernel.org/linux-security-module/[email protected]/
Anyway, it is cleaner like this.
* Add a comment about E2BIG which can be returned by landlock_enforce_ruleset_current(2) when there is no more room for another stacked ruleset (i.e. domain).
Changes since v22:
* Replace security_capable() with ns_capable_noaudit() (suggested by Jann Horn) and explicitly return EPERM.
* Fix landlock_enforce_ruleset_current(2)'s out_put_creds (spotted by Jann Horn).
* Add __always_inline to copy_min_struct_from_user() to make its BUILD_BUG_ON() checks reliable (suggested by Jann Horn).
* Simplify path assignation in get_path_from_fd() (suggested by Jann Horn).
* Fix spelling (spotted by Jann Horn).
Changes since v21:
* Fix and improve comments.
Changes since v20:
* Remove two arguments to landlock_enforce_ruleset(2) (requested by Arnd Bergmann) and rename it to landlock_enforce_ruleset_current(2): remove the enum landlock_target_type and the target file descriptor (not used for now). A ruleset can only be enforced on the current thread.
* Remove the size argument in landlock_add_rule() (requested by Arnd Bergmann).
* Remove landlock_get_features(2) (suggested by Arnd Bergmann).
* Simplify and rename copy_struct_if_any_from_user() to copy_min_struct_from_user().
* Rename "options" to "flags" to allign with current syscalls.
* Rename some types and variables in a more consistent way.
* Fix missing type declarations in syscalls.h .
Changes since v19:
* Replace the landlock(2) syscall with 4 syscalls (one for each command): landlock_get_features(2), landlock_create_ruleset(2), landlock_add_rule(2) and landlock_enforce_ruleset(2) (suggested by Arnd Bergmann).
https://lore.kernel.org/lkml/[email protected]/
* Return EOPNOTSUPP (instead of ENOPKG) when Landlock is disabled.
* Add two new fields to landlock_attr_features to fit with the new
syscalls: last_rule_type and last_target_type. This enable to easily identify which types are supported.
* Pack landlock_attr_path_beneath struct because of the removed ruleset_fd.
* Update documentation and fix spelling.
Changes since v18:
* Remove useless include.
* Remove LLATTR_SIZE() which was only used to shorten lines. Cf. commit bdc48fa11e46 ("checkpatch/coding-style: deprecate 80-column warning").
Changes since v17:
* Synchronize syscall declaration.
* Fix comment.
Changes since v16:
* Add a size_attr_features field to struct landlock_attr_features for self-introspection, and move the access_fs field to be more consistent.
* Replace __aligned_u64 types of attribute fields with __u16, __s32, __u32 and __u64, and check at build time that these structures does not contain hole and that they are aligned the same way (8-bits) on all architectures. This shrinks the size of the userspace ABI, which may be appreciated especially for struct landlock_attr_features which could grow a lot in the future. For instance, struct landlock_attr_features shrinks from 72 bytes to 32 bytes. This change also enables to remove 64-bits to 32-bits conversion checks.
* Switch syscall attribute pointer and size arguments to follow similar syscall argument order (e.g. bpf, clone3, openat2).
* Set LANDLOCK_OPT_* types to 32-bits.
* Allow enforcement of empty ruleset, which enables deny-all policies.
* Fix documentation inconsistency.
Changes since v15:
* Do not add file descriptors referring to internal filesystems (e.g. nsfs) in a ruleset.
* Replace is_user_mountable() with in-place clean checks.
* Replace EBADR with EBADFD in get_ruleset_from_fd() and get_path_from_fd().
* Remove ruleset's show_fdinfo() for now.
Changes since v14:
* Remove the security_file_open() check in get_path_from_fd(): an opened FD should not be restricted here, and even less with this hook. As a result, it is now allowed to add a path to a ruleset even if the access to this path is not allowed (without O_PATH). This doesn't change the fact that enforcing a ruleset can't grant any right, only remove some rights. The new layer levels add more consistent restrictions.
* Check minimal landlock_attr_* size/content. This fix the case when no data was provided and e.g., FD 0 was interpreted as ruleset_fd. Now this leads to a returned -EINVAL.
* Fix credential double-free error case.
* Complete struct landlock_attr_size with size_attr_enforce.
* Fix undefined reference to syscall when Landlock is not selected.
* Remove f.file->f_path.mnt check (suggested by Al Viro).
* Add build-time checks.
* Move ABI checks from fs.c .
* Constify variables.
* Fix spelling.
* Add comments.
Changes since v13:
* New implementation, replacing the dependency on seccomp(2) and bpf(2).
--- include/linux/syscalls.h | 7 + include/uapi/linux/landlock.h | 53 ++++ kernel/sys_ni.c | 5 + security/landlock/Makefile | 2 +- security/landlock/syscalls.c | 444 ++++++++++++++++++++++++++++++++++ 5 files changed, 510 insertions(+), 1 deletion(-) create mode 100644 security/landlock/syscalls.c
include/linux/syscalls.h +7 -0
@@ -68,6 +68,8 @@ union bpf_attr;struct io_uring_params ;struct clone_args ;struct open_how ; +struct landlock_ruleset_attr ; +enum landlock_rule_type ;#include <linux/types.h> #include <linux/aio_abi.h>
@@ -1037,6 +1039,11 @@ asmlinkage long sys_pidfd_send_signal(int pidfd, int sig,siginfo_t __user *info, unsigned int flags);asmlinkage long sys_pidfd_getfd (int pidfd,int fd,unsigned int flags); +asmlinkage long sys_landlock_create_ruleset (const struct landlock_ruleset_attr __user *attr, +size_t size,__u32 flags); +asmlinkage long sys_landlock_add_rule (int ruleset_fd,enum landlock_rule_type rule_type, +const void __user *rule_attr,__u32 flags); +asmlinkage long sys_landlock_restrict_self (int ruleset_fd,__u32 flags);/* * Architecture-specific system calls
include/uapi/linux/landlock.h +53 -0
59 lines not on this page · raw message
kernel/sys_ni.c +5 -0
@@ -266,6 +266,11 @@ COND_SYSCALL(request_key);COND_SYSCALL (keyctl);COND_SYSCALL_COMPAT (keyctl); +/* security/landlock/syscalls.c */ +COND_SYSCALL (landlock_create_ruleset); +COND_SYSCALL (landlock_add_rule); +COND_SYSCALL (landlock_restrict_self); +/* arch/example/kernel/sys_example.c */ /* mm/fadvise.c */
security/landlock/Makefile +1 -1
@@ -1,4 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := setup.o object.o ruleset.o \ +landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.o
security/landlock/syscalls.c new file +444 -0
444 lines not on this page · raw message
Signature · 1 line
2.30.0
kernel test robot
build botRe: [PATCH v28 08/12] landlock: Add syscall implementations
I love your patch! Perhaps something to improve:
Hi "Mickaël,
I love your patch! Perhaps something to improve:
[auto build test WARNING on 1048ba83fb1c00cd24172e23e8263972f6b5d9ac]
url: https://github.com/0day-ci/linux/commits/Micka-l-Sala-n/Landlock-LSM/20210203-003928 base: 1048ba83fb1c00cd24172e23e8263972f6b5d9ac config: arm64-randconfig-r002-20210202 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 275c6af7d7f1ed63a03d05b4484413e447133269)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/5f9fa6dfd70510a079d0894025546ffeb785f52a
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Micka-l-Sala-n/Landlock-LSM/20210203-003928
git checkout 5f9fa6dfd70510a079d0894025546ffeb785f52a
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:138:1: note: expanded from here
__arm64_compat_sys_recvmsg
^
kernel/sys_ni.c:256:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:41:13: note: expanded from macro 'COND_SYSCALL_COMPAT'
asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:261:1: warning: no previous prototype for function '__arm64_sys_mremap' [-Wmissing-prototypes]
COND_SYSCALL(mremap);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:139:1: note: expanded from here
__arm64_sys_mremap
^
kernel/sys_ni.c:261:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:264:1: warning: no previous prototype for function '__arm64_sys_add_key' [-Wmissing-prototypes]
COND_SYSCALL(add_key);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:140:1: note: expanded from here
__arm64_sys_add_key
^
kernel/sys_ni.c:264:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:265:1: warning: no previous prototype for function '__arm64_sys_request_key' [-Wmissing-prototypes]
COND_SYSCALL(request_key);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:141:1: note: expanded from here
__arm64_sys_request_key
^
kernel/sys_ni.c:265:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:266:1: warning: no previous prototype for function '__arm64_sys_keyctl' [-Wmissing-prototypes]
COND_SYSCALL(keyctl);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:142:1: note: expanded from here
__arm64_sys_keyctl
^
kernel/sys_ni.c:266:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:267:1: warning: no previous prototype for function '__arm64_compat_sys_keyctl' [-Wmissing-prototypes]
COND_SYSCALL_COMPAT(keyctl);
^
arch/arm64/include/asm/syscall_wrapper.h:41:25: note: expanded from macro 'COND_SYSCALL_COMPAT'
asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:143:1: note: expanded from here
__arm64_compat_sys_keyctl
^
kernel/sys_ni.c:267:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:41:13: note: expanded from macro 'COND_SYSCALL_COMPAT'
asmlinkage long __weak __arm64_compat_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:270:1: warning: no previous prototype for function '__arm64_sys_landlock_create_ruleset' [-Wmissing-prototypes]
COND_SYSCALL(landlock_create_ruleset);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:144:1: note: expanded from here
__arm64_sys_landlock_create_ruleset
^
kernel/sys_ni.c:270:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:271:1: warning: no previous prototype for function '__arm64_sys_landlock_add_rule' [-Wmissing-prototypes]
COND_SYSCALL(landlock_add_rule);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:145:1: note: expanded from here
__arm64_sys_landlock_add_rule
^
kernel/sys_ni.c:271:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
>> kernel/sys_ni.c:272:1: warning: no previous prototype for function '__arm64_sys_landlock_restrict_self' [-Wmissing-prototypes]
COND_SYSCALL(landlock_restrict_self);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:146:1: note: expanded from here
__arm64_sys_landlock_restrict_self
^
kernel/sys_ni.c:272:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:277:1: warning: no previous prototype for function '__arm64_sys_fadvise64_64' [-Wmissing-prototypes]
COND_SYSCALL(fadvise64_64);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:147:1: note: expanded from here
__arm64_sys_fadvise64_64
^
kernel/sys_ni.c:277:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:280:1: warning: no previous prototype for function '__arm64_sys_swapon' [-Wmissing-prototypes]
COND_SYSCALL(swapon);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:148:1: note: expanded from here
__arm64_sys_swapon
^
kernel/sys_ni.c:280:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:281:1: warning: no previous prototype for function '__arm64_sys_swapoff' [-Wmissing-prototypes]
COND_SYSCALL(swapoff);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:149:1: note: expanded from here
__arm64_sys_swapoff
^
kernel/sys_ni.c:281:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:282:1: warning: no previous prototype for function '__arm64_sys_mprotect' [-Wmissing-prototypes]
COND_SYSCALL(mprotect);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:150:1: note: expanded from here
__arm64_sys_mprotect
^
kernel/sys_ni.c:282:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:283:1: warning: no previous prototype for function '__arm64_sys_msync' [-Wmissing-prototypes]
COND_SYSCALL(msync);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:151:1: note: expanded from here
__arm64_sys_msync
^
kernel/sys_ni.c:283:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:284:1: warning: no previous prototype for function '__arm64_sys_mlock' [-Wmissing-prototypes]
COND_SYSCALL(mlock);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
<scratch space>:152:1: note: expanded from here
__arm64_sys_mlock
^
kernel/sys_ni.c:284:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/arm64/include/asm/syscall_wrapper.h:76:13: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^
kernel/sys_ni.c:285:1: warning: no previous prototype for function '__arm64_sys_munlock' [-Wmissing-prototypes]
COND_SYSCALL(munlock);
^
arch/arm64/include/asm/syscall_wrapper.h:76:25: note: expanded from macro 'COND_SYSCALL'
asmlinkage long __weak __arm64_sys_##name(const struct pt_regs *regs) \
^ <scratch space>:153:1: note: expanded from here
__arm64_sys_munlock
^
kernel/sys_ni.c:285:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
vim +/__arm64_sys_landlock_restrict_self +272 kernel/sys_ni.c
268
269 /* security/landlock/syscalls.c */
270 COND_SYSCALL(landlock_create_ruleset);
271 COND_SYSCALL(landlock_add_rule);
> 272 COND_SYSCALL(landlock_restrict_self);
273
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH v28 08/12] landlock: Add syscall implementations
In reply to Mickaël SalaünQuoted 13 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
These 3 system calls are designed to be used by unprivileged processes to sandbox themselves:
*landlock_create_ruleset(2): Creates a ruleset and returns its file descriptor.
*landlock_add_rule(2): Adds a rule (e.g. file hierarchy access) to a ruleset, identified by the dedicated file descriptor.
*landlock_restrict_self(2): Enforces a ruleset on the calling thread and its future children (similar to seccomp). This syscall has the same usage restrictions as seccomp(2): the caller must have theno_new_privsattribute set or haveCAP_SYS_ADMINin the current user namespace.
Was looking through the set for this :) thanks.
Quoted 20 lines
All these syscalls have a "flags" argument (not currently used) to enable extensibility.
Here are the motivations for these new syscalls:
* A sandboxed process may not have access to file systems, including /dev, /sys or /proc, but it should still be able to add more restrictions to itself.
* Neither prctl(2) nor seccomp(2) (which was used in a previous version) fit well with the current definition of a Landlock security policy.All passed structs (attributes) are checked at build time to ensure that they don't contain holes and that they are aligned the same way for each architecture.
See the user and kernel documentation for more details (provided by a following commit):
*Documentation/userspace-api/landlock.rst
*Documentation/security/landlock.rst
Quoted 5 lines
Cc Arnd Bergmann <[email protected]>Cc James Morris <[email protected]>Cc Jann Horn <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
Quoted 720 lines
Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v27:
* Forbid creation of rules with an emptyallowed_accessvalue because they are now ignored (since v26) in path walks.
* Renamelandlock_enforce_ruleset_self(2)tolandlock_restrict_self(2):
shorter and consistent with the two other syscalls (i.e. verb + direct object).
* Update ruleset access check according to the new access stack.
* Improvelandlock_add_rule(2)documentation.
* Fix comment.
* Remove Reviewed-by Jann Horn because of the above changes.Changes since v26:
* Renamelandlock_enforce_ruleset_current(2)tolandlock_enforce_ruleset_self(2). "current" makes sense for a kernel developer, but much less from a user space developer stand point. "self" is widely used to refer to the current task (e.g./proc/self). "current" may refer to temporal properties, which could be added later to this syscall flags (cf./proc/self/attr/{current,exec}).
* Simplifybuild_check_abi().
* Renamesyscall.ctosyscalls.c.
* Use less ambiguous comments.
* Fix spelling.Changes since v25:
* Revertbuild_check_abi()as non-inline to trigger a warning if it is not called.
* Use the new limit names.Changes since v24:
* Add Reviewed-by: Jann Horn <[email protected]>
* Setbuild_check_abi()as inline.Changes since v23:
* Rewriteget_ruleset_from_fd()to please the 0-DAY CI Kernel Test Service that reported an uninitialized variable (false positive):
https://lore.kernel.org/linux-security-module/[email protected]/
Anyway, it is cleaner like this.
* Add a comment about E2BIG which can be returned bylandlock_enforce_ruleset_current(2)when there is no more room for another stacked ruleset (i.e. domain).Changes since v22:
* Replacesecurity_capable()withns_capable_noaudit()(suggested by Jann Horn) and explicitly return EPERM.
* Fix landlock_enforce_ruleset_current(2)'sout_put_creds(spotted by Jann Horn).
* Add__always_inlinetocopy_min_struct_from_user()to make itsBUILD_BUG_ON()checks reliable (suggested by Jann Horn).
* Simplify path assignation inget_path_from_fd()(suggested by Jann Horn).
* Fix spelling (spotted by Jann Horn).Changes since v21:
* Fix and improve comments.Changes since v20:
* Remove two arguments tolandlock_enforce_ruleset(2)(requested by Arnd Bergmann) and rename it tolandlock_enforce_ruleset_current(2): remove the enumlandlock_target_typeand the target file descriptor (not used for now). A ruleset can only be enforced on the current thread.
* Remove the size argument inlandlock_add_rule()(requested by Arnd Bergmann).
* Removelandlock_get_features(2)(suggested by Arnd Bergmann).
* Simplify and renamecopy_struct_if_any_from_user()tocopy_min_struct_from_user().
* Rename "options" to "flags" to allign with current syscalls.
* Rename some types and variables in a more consistent way.
* Fix missing type declarations insyscalls.h.Changes since v19:
* Replace the landlock(2) syscall with 4 syscalls (one for each command):landlock_get_features(2),landlock_create_ruleset(2),landlock_add_rule(2)andlandlock_enforce_ruleset(2)(suggested by Arnd Bergmann).
https://lore.kernel.org/lkml/[email protected]/
* Return EOPNOTSUPP (instead of ENOPKG) when Landlock is disabled.
* Add two new fields tolandlock_attr_featuresto fit with the new
syscalls:last_rule_typeandlast_target_type. This enable to easily identify which types are supported.
* Packlandlock_attr_path_beneathstruct because of the removedruleset_fd.
* Update documentation and fix spelling.Changes since v18:
* Remove useless include.
* RemoveLLATTR_SIZE()which was only used to shorten lines. Cf. commitbdc48fa11e46("checkpatch/coding-style: deprecate 80-column warning").Changes since v17:
* Synchronize syscall declaration.
* Fix comment.Changes since v16:
* Add asize_attr_featuresfield to structlandlock_attr_featuresfor self-introspection, and move theaccess_fsfield to be more consistent.
* Replace__aligned_u64types of attribute fields with__u16,__s32,__u32and__u64, and check at build time that these structures does not contain hole and that they are aligned the same way (8-bits) on all architectures. This shrinks the size of the userspace ABI, which may be appreciated especially for structlandlock_attr_featureswhich could grow a lot in the future. For instance, structlandlock_attr_featuresshrinks from 72 bytes to 32 bytes. This change also enables to remove 64-bits to 32-bits conversion checks.
* Switch syscall attribute pointer and size arguments to follow similar syscall argument order (e.g. bpf, clone3, openat2).
* Set LANDLOCK_OPT_* types to 32-bits.
* Allow enforcement of empty ruleset, which enables deny-all policies.
* Fix documentation inconsistency.Changes since v15:
* Do not add file descriptors referring to internal filesystems (e.g. nsfs) in a ruleset.
* Replaceis_user_mountable()with in-place clean checks.
* Replace EBADR with EBADFD inget_ruleset_from_fd()andget_path_from_fd().
* Remove ruleset'sshow_fdinfo()for now.Changes since v14:
* Remove thesecurity_file_open()check inget_path_from_fd(): an opened FD should not be restricted here, and even less with this hook. As a result, it is now allowed to add a path to a ruleset even if the access to this path is not allowed (withoutO_PATH). This doesn't change the fact that enforcing a ruleset can't grant any right, only remove some rights. The new layer levels add more consistent restrictions.
* Check minimal landlock_attr_* size/content. This fix the case when no data was provided and e.g., FD 0 was interpreted asruleset_fd. Now this leads to a returned-EINVAL.
* Fix credential double-free error case.
* Complete structlandlock_attr_sizewithsize_attr_enforce.
* Fix undefined reference to syscall when Landlock is not selected.
* Removef.file->f_path.mntcheck (suggested by Al Viro).
* Add build-time checks.
* Move ABI checks fromfs.c.
* Constify variables.
* Fix spelling.
* Add comments.Changes since v13:
* New implementation, replacing the dependency on seccomp(2) and bpf(2).--- include/linux/syscalls.h | 7 + include/uapi/linux/landlock.h | 53 ++++ kernel/sys_ni.c | 5 + security/landlock/Makefile | 2 +- security/landlock/syscalls.c | 444 ++++++++++++++++++++++++++++++++++ 5 files changed, 510 insertions(+), 1 deletion(-) create mode 100644 security/landlock/syscalls.cinclude/linux/syscalls.h +7
-0@@ -68,6 +68,8 @@ union bpf_attr;struct io_uring_params ;struct clone_args ;struct open_how ; +struct landlock_ruleset_attr ; +enum landlock_rule_type ;#include <linux/types.h> #include <linux/aio_abi.h> @@ -1037,6 +1039,11 @@ asmlinkage long sys_pidfd_send_signal(int pidfd, int sig,siginfo_t __user *info, unsigned int flags);asmlinkage long sys_pidfd_getfd (int pidfd,int fd,unsigned int flags); +asmlinkage long sys_landlock_create_ruleset (const struct landlock_ruleset_attr __user *attr, +size_t size,__u32 flags); +asmlinkage long sys_landlock_add_rule (int ruleset_fd,enum landlock_rule_type rule_type, +const void __user *rule_attr,__u32 flags); +asmlinkage long sys_landlock_restrict_self (int ruleset_fd,__u32 flags);/* * Architecture-specific system calls include/uapi/linux/landlock.h +53
-0@@ -9,6 +9,59 @@#ifndef _UAPI_LINUX_LANDLOCK_H#define _UAPI_LINUX_LANDLOCK_H +#include <linux/types.h> + +/** +* struct landlock_ruleset_attr - Ruleset definition +* +* Argument of sys_landlock_create_ruleset(). This structure can grow in +* future versions. +*/ +struct landlock_ruleset_attr { +/** +* @handled_access_fs: Bitmask of actions (cf. `Filesystem flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. This is needed for backward +* compatibility reasons. +*/ +__u64 handled_access_fs; +}; + +/** +* enum landlock_rule_type - Landlock rule type +* +* Argument of sys_landlock_add_rule(). +*/ +enum landlock_rule_type { +/** +* @LANDLOCK_RULE_PATH_BENEATH: Type of a &struct +* landlock_path_beneath_attr . +*/ +LANDLOCK_RULE_PATH_BENEATH =1 , +}; + +/** +* struct landlock_path_beneath_attr - Path hierarchy definition +* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_path_beneath_attr { +/** +* @allowed_access: Bitmask of allowed actions for this file hierarchy +* (cf. `Filesystem flags`_). +*/ +__u64 allowed_access; +/** +* @parent_fd: File descriptor, open with ``O_PATH``, which identifies +* the parent directory of a file hierarchy, or just a file. +*/ +__s32 parent_fd; +/* +* This struct is packed to avoid trailing reserved members. +* Cf. security/landlock/syscalls.c:build_check_abi() +*/ +} __attribute__((packed)); +/** * DOC: fs_access * kernel/sys_ni.c +5
-0@@ -266,6 +266,11 @@ COND_SYSCALL(request_key);COND_SYSCALL (keyctl);COND_SYSCALL_COMPAT (keyctl); +/* security/landlock/syscalls.c */ +COND_SYSCALL (landlock_create_ruleset); +COND_SYSCALL (landlock_add_rule); +COND_SYSCALL (landlock_restrict_self); +/* arch/example/kernel/sys_example.c */ /* mm/fadvise.c */ security/landlock/Makefile +1
-1@@ -1,4 +1,4 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := setup.o object.o ruleset.o \ +landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.osecurity/landlock/syscalls.c new file +444
-0@@ -0,0 +1,444 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - System call implementations and user space interfaces +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <asm/current.h> +#include <linux/anon_inodes.h> +#include <linux/build_bug.h> +#include <linux/capability.h> +#include <linux/compiler_types.h> +#include <linux/dcache.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/fs.h> +#include <linux/limits.h> +#include <linux/mount.h> +#include <linux/path.h> +#include <linux/sched.h> +#include <linux/security.h> +#include <linux/stddef.h> +#include <linux/syscalls.h> +#include <linux/types.h> +#include <linux/uaccess.h> +#include <uapi/linux/landlock.h> + +#include "cred.h" +#include "fs.h" +#include "limits.h" +#include "ruleset.h" +#include "setup.h" + +/** +* copy_min_struct_from_user - Safe future-proof argument copying +* +* Extend copy_struct_from_user() to check for consistent user buffer. +* +* @dst: Kernel space pointer or NULL. +* @ksize: Actual size of the data pointed to by @dst. +* @ksize_min: Minimal required size to be copied. +* @src: User space pointer or NULL. +* @usize: (Alleged) size of the data pointed to by @src. +*/ +static __always_inline int copy_min_struct_from_user(void *const dst, +const size_t ksize,const size_t ksize_min, +const void __user *const src,const size_t usize) +{ +/* Checks buffer inconsistencies. */ +BUILD_BUG_ON (!dst); +if (!src) +return -EFAULT ; + +/* Checks size ranges. */ +BUILD_BUG_ON (ksize <=0 ); +BUILD_BUG_ON (ksize < ksize_min); +if (usize < ksize_min) +return -EINVAL ; +if (usize >PAGE_SIZE ) +return -E2BIG ; + +/* Copies user buffer and fills with zeros. */ +return copy_struct_from_user (dst, ksize, src, usize); +} + +/* +* This function only contains arithmetic operations with constants, leading to +* BUILD_BUG_ON(). The related code is evaluated and checked at build time, +* but it is then ignored thanks to compiler optimizations. +*/ +static void build_check_abi (void ) +{ +struct landlock_ruleset_attr ruleset_attr; +struct landlock_path_beneath_attr path_beneath_attr; +size_t ruleset_size, path_beneath_size; + +/* +* For each user space ABI structures, first checks that there is no +* hole in them, then checks that all architectures have the same +* struct size. +*/ + ruleset_size =sizeof (ruleset_attr.handled_access_fs); +BUILD_BUG_ON (sizeof (ruleset_attr) != ruleset_size); +BUILD_BUG_ON (sizeof (ruleset_attr) !=8 ); + + path_beneath_size =sizeof (path_beneath_attr.allowed_access); + path_beneath_size +=sizeof (path_beneath_attr.parent_fd); +BUILD_BUG_ON (sizeof (path_beneath_attr) != path_beneath_size); +BUILD_BUG_ON (sizeof (path_beneath_attr) !=12 ); +} + +/* Ruleset handling */ + +static int fop_ruleset_release (struct inode *const inode, +struct file *const filp) +{ +struct landlock_ruleset *ruleset = filp->private_data; + +landlock_put_ruleset (ruleset); +return 0 ; +} + +static ssize_t fop_dummy_read (struct file *const filp,char __user *const buf, +const size_t size,loff_t *const ppos) +{ +/* Dummy handler to enable FMODE_CAN_READ. */ +return -EINVAL ; +} + +static ssize_t fop_dummy_write (struct file *const filp, +const char __user *const buf,const size_t size, +loff_t *const ppos) +{ +/* Dummy handler to enable FMODE_CAN_WRITE. */ +return -EINVAL ; +} + +/* +* A ruleset file descriptor enables to build a ruleset by adding (i.e. +* writing) rule after rule, without relying on the task's context. This +* reentrant design is also used in a read way to enforce the ruleset on the +* current task. +*/ +static const struct file_operations ruleset_fops = { + .release = fop_ruleset_release, + .read = fop_dummy_read, + .write = fop_dummy_write, +}; + +/** +* sys_landlock_create_ruleset - Create a new ruleset +* +* @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of +* the new ruleset. +* @size: Size of the pointed &struct landlock_ruleset_attr (needed for +* backward and forward compatibility). +* @flags: Must be 0. +* +* This system call enables to create a new Landlock ruleset, and returns the +* related file descriptor on success. +* +* Possible returned errors are: +* +* - EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; +* - EINVAL: @flags is not 0, or unknown access, or too small @size; +* - E2BIG or EFAULT: @attr or @size inconsistencies; +* - ENOMSG: empty &landlock_ruleset_attr.handled_access_fs. +*/ +SYSCALL_DEFINE3 (landlock_create_ruleset, + const struct landlock_ruleset_attr __user *const, attr, + const size_t, size, const __u32, flags) +{ +struct landlock_ruleset_attr ruleset_attr; +struct landlock_ruleset *ruleset; +int err, ruleset_fd; + +/* Build-time checks. */ +build_check_abi (); + +if (!landlock_initialized) +return -EOPNOTSUPP ; + +/* No flag for now. */ +if (flags) +return -EINVAL ; + +/* Copies raw user space buffer. */ + err =copy_min_struct_from_user (&ruleset_attr,sizeof (ruleset_attr), +offsetofend (typeof (ruleset_attr), handled_access_fs), + attr, size); +if (err) +return err; + +/* Checks content (and 32-bits cast). */ +if ((ruleset_attr.handled_access_fs |LANDLOCK_MASK_ACCESS_FS ) != +LANDLOCK_MASK_ACCESS_FS ) +return -EINVAL ; + +/* Checks arguments and transforms to kernel struct. */ + ruleset =landlock_create_ruleset (ruleset_attr.handled_access_fs); +if (IS_ERR (ruleset)) +return PTR_ERR (ruleset); + +/* Creates anonymous FD referring to the ruleset. */ + ruleset_fd =anon_inode_getfd ("landlock-ruleset" , &ruleset_fops, + ruleset,O_RDWR |O_CLOEXEC ); +if (ruleset_fd <0 ) +landlock_put_ruleset (ruleset); +return ruleset_fd; +} + +/* +* Returns an owned ruleset from a FD. It is thus needed to call +* landlock_put_ruleset() on the return value. +*/ +static struct landlock_ruleset *get_ruleset_from_fd (const int fd, +const fmode_t mode) +{ +struct fd ruleset_f; +struct landlock_ruleset *ruleset; + + ruleset_f =fdget (fd); +if (!ruleset_f.file) +return ERR_PTR (-EBADF ); + +/* Checks FD type and access right. */ +if (ruleset_f.file->f_op != &ruleset_fops) { + ruleset =ERR_PTR (-EBADFD ); + goto out_fdput; + } +if (!(ruleset_f.file->f_mode & mode)) { + ruleset =ERR_PTR (-EPERM ); + goto out_fdput; + } + ruleset = ruleset_f.file->private_data; +if (WARN_ON_ONCE (ruleset->num_layers !=1 )) { + ruleset =ERR_PTR (-EINVAL ); + goto out_fdput; + } +landlock_get_ruleset (ruleset); + +out_fdput: +fdput (ruleset_f); +return ruleset; +} + +/* Path handling */ + +/* +* @path: Must call put_path(@path) after the call if it succeeded. +*/ +static int get_path_from_fd (const s32 fd,struct path *const path) +{ +struct fd f; +int err =0 ; + +BUILD_BUG_ON (!__same_type (fd, + ((struct landlock_path_beneath_attr *)NULL )->parent_fd)); + +/* Handles O_PATH. */ + f =fdget_raw (fd); +if (!f.file) +return -EBADF ; +/* +* Only allows O_PATH file descriptor: enables to restrict ambient +* filesystem access without requiring to open and risk leaking or +* misusing a file descriptor. Forbid internal filesystems (e.g. +* nsfs), including pseudo filesystems that will never be mountable +* (e.g. sockfs, pipefs). +*/ +if (!(f.file->f_mode &FMODE_PATH ) || + (f.file->f_path.mnt->mnt_flags &MNT_INTERNAL ) || + (f.file->f_path.dentry->d_sb->s_flags &SB_NOUSER ) || +d_is_negative (f.file->f_path.dentry) || +IS_PRIVATE (d_backing_inode (f.file->f_path.dentry))) { + err = -EBADFD ; + goto out_fdput; + } + *path = f.file->f_path; +path_get (path); + +out_fdput: +fdput (f); +return err; +} + +/** +* sys_landlock_add_rule - Add a new rule to a ruleset +* +* @ruleset_fd: File descriptor tied to the ruleset that should be extended +* with the new rule. +* @rule_type: Identify the structure type pointed to by @rule_attr (only +* LANDLOCK_RULE_PATH_BENEATH for now). +* @rule_attr: Pointer to a rule (only of type &struct +* landlock_path_beneath_attr for now). +* @flags: Must be 0. +* +* This system call enables to define a new rule and add it to an existing +* ruleset. +* +* Possible returned errors are: +* +* - EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; +* - EINVAL: @flags is not 0, or inconsistent access in the rule (i.e. +* &landlock_path_beneath_attr.allowed_access is not a subset of the rule's +* accesses); +* - ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access); +* - EBADF: @ruleset_fd is not a file descriptor for the current thread, or a +* member of @rule_attr is not a file descriptor as expected; +* - EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of +* @rule_attr is not the expected file descriptor type (e.g. file open +* without O_PATH); +* - EPERM: @ruleset_fd has no write access to the underlying ruleset; +* - EFAULT: @rule_attr inconsistency. +*/ +SYSCALL_DEFINE4 (landlock_add_rule, + const int, ruleset_fd, const enum landlock_rule_type, rule_type, + const void __user *const, rule_attr, const __u32, flags) +{ +struct landlock_path_beneath_attr path_beneath_attr; +struct path path; +struct landlock_ruleset *ruleset; +int res, err; + +if (!landlock_initialized) +return -EOPNOTSUPP ; + +/* No flag for now. */ +if (flags) +return -EINVAL ; + +if (rule_type !=LANDLOCK_RULE_PATH_BENEATH ) +return -EINVAL ; + +/* Copies raw user space buffer, only one type for now. */ + res =copy_from_user (&path_beneath_attr, rule_attr, +sizeof (path_beneath_attr)); +if (res) +return -EFAULT ; + +/* Gets and checks the ruleset. */ + ruleset =get_ruleset_from_fd (ruleset_fd,FMODE_CAN_WRITE ); +if (IS_ERR (ruleset)) +return PTR_ERR (ruleset); + +/* +* Informs about useless rule: empty allowed_access (i.e. deny rules) +* are ignored in path walks. +*/ +if (!path_beneath_attr.allowed_access) { + err = -ENOMSG ; + goto out_put_ruleset; + } +/* +* Checks that allowed_access matches the @ruleset constraints +* (ruleset->fs_access_masks[0] is automatically upgraded to 64-bits). +*/ +if ((path_beneath_attr.allowed_access | ruleset->fs_access_masks[0 ]) != + ruleset->fs_access_masks[0 ]) { + err = -EINVAL ; + goto out_put_ruleset; + } + +/* Gets and checks the new rule. */ + err =get_path_from_fd (path_beneath_attr.parent_fd, &path); +if (err) + goto out_put_ruleset; + +/* Imports the new rule. */ + err =landlock_append_fs_rule (ruleset, &path, + path_beneath_attr.allowed_access); +path_put (&path); + +out_put_ruleset: +landlock_put_ruleset (ruleset); +return err; +} + +/* Enforcement */ + +/** +* sys_landlock_restrict_self - Enforce a ruleset on the calling thread +* +* @ruleset_fd: File descriptor tied to the ruleset to merge with the target. +* @flags: Must be 0. +* +* This system call enables to enforce a Landlock ruleset on the current +* thread. Enforcing a ruleset requires that the task has CAP_SYS_ADMIN in its +* namespace or is running with no_new_privs. This avoids scenarios where +* unprivileged tasks can affect the behavior of privileged children. +* +* Possible returned errors are: +* +* - EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time; +* - EINVAL: @flags is not 0. +* - EBADF: @ruleset_fd is not a file descriptor for the current thread; +* - EBADFD: @ruleset_fd is not a ruleset file descriptor; +* - EPERM: @ruleset_fd has no read access to the underlying ruleset, or the +* current thread is not running with no_new_privs, or it doesn't have +* CAP_SYS_ADMIN in its namespace. +* - E2BIG: The maximum number of stacked rulesets is reached for the current +* thread. +*/ +SYSCALL_DEFINE2 (landlock_restrict_self, + const int, ruleset_fd, const __u32, flags) +{ +struct landlock_ruleset *new_dom, *ruleset; +struct cred *new_cred; +struct landlock_cred_security *new_llcred; +int err; + +if (!landlock_initialized) +return -EOPNOTSUPP ; + +/* No flag for now. */ +if (flags) +return -EINVAL ; + +/* +* Similar checks as for seccomp(2), except that an -EPERM may be +* returned. +*/ +if (!task_no_new_privs (current) && + !ns_capable_noaudit (current_user_ns (),CAP_SYS_ADMIN )) +return -EPERM ; + +/* Gets and checks the ruleset. */ + ruleset =get_ruleset_from_fd (ruleset_fd,FMODE_CAN_READ ); +if (IS_ERR (ruleset)) +return PTR_ERR (ruleset); + +/* Prepares new credentials. */ + new_cred =prepare_creds (); +if (!new_cred) { + err = -ENOMEM ; + goto out_put_ruleset; + } + new_llcred =landlock_cred (new_cred); + +/* +* There is no possible race condition while copying and manipulating +* the current credentials because they are dedicated per thread. +*/ + new_dom =landlock_merge_ruleset (new_llcred->domain, ruleset); +if (IS_ERR (new_dom)) { + err =PTR_ERR (new_dom); + goto out_put_creds; + } + +/* Replaces the old (prepared) domain. */ +landlock_put_ruleset (new_llcred->domain); + new_llcred->domain = new_dom; + +landlock_put_ruleset (ruleset); +return commit_creds (new_cred); + +out_put_creds: +abort_creds (new_cred); + +out_put_ruleset: +landlock_put_ruleset (ruleset); +return err; +}Signature · 1 line
2.30.0
kernel test robot
build botRe: [PATCH v28 08/12] landlock: Add syscall implementations
In reply to Mickaël SalaünI love your patch! Perhaps something to improve:
Hi "Mickaël,
I love your patch! Perhaps something to improve:
[auto build test WARNING on 1048ba83fb1c00cd24172e23e8263972f6b5d9ac]
url: https://github.com/0day-ci/linux/commits/Micka-l-Sala-n/Landlock-LSM/20210203-003928 base: 1048ba83fb1c00cd24172e23e8263972f6b5d9ac config: x86_64-randconfig-r013-20210203 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 275c6af7d7f1ed63a03d05b4484413e447133269)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/5f9fa6dfd70510a079d0894025546ffeb785f52a
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Micka-l-Sala-n/Landlock-LSM/20210203-003928
git checkout 5f9fa6dfd70510a079d0894025546ffeb785f52a
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
kernel/sys_ni.c:266:1: warning: no previous prototype for function '__x64_sys_keyctl' [-Wmissing-prototypes] COND_SYSCALL(keyctl);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:222:1: note: expanded from here
__x64_sys_keyctl
^
kernel/sys_ni.c:266:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
kernel/sys_ni.c:267:1: warning: no previous prototype for function '__x32_compat_sys_keyctl' [-Wmissing-prototypes]
COND_SYSCALL_COMPAT(keyctl);
^
arch/x86/include/asm/syscall_wrapper.h:219:2: note: expanded from macro 'COND_SYSCALL_COMPAT'
__X32_COMPAT_COND_SYSCALL(name)
^
arch/x86/include/asm/syscall_wrapper.h:175:2: note: expanded from macro '__X32_COMPAT_COND_SYSCALL'
__COND_SYSCALL(x32, compat_sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:226:1: note: expanded from here
__x32_compat_sys_keyctl
^
kernel/sys_ni.c:267:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:219:2: note: expanded from macro 'COND_SYSCALL_COMPAT'
__X32_COMPAT_COND_SYSCALL(name)
^
arch/x86/include/asm/syscall_wrapper.h:175:2: note: expanded from macro '__X32_COMPAT_COND_SYSCALL'
__COND_SYSCALL(x32, compat_sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
kernel/sys_ni.c:270:1: warning: no previous prototype for function '__x64_sys_landlock_create_ruleset' [-Wmissing-prototypes]
COND_SYSCALL(landlock_create_ruleset);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:230:1: note: expanded from here
__x64_sys_landlock_create_ruleset
^
kernel/sys_ni.c:270:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
kernel/sys_ni.c:271:1: warning: no previous prototype for function '__x64_sys_landlock_add_rule' [-Wmissing-prototypes]
COND_SYSCALL(landlock_add_rule);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:234:1: note: expanded from here
__x64_sys_landlock_add_rule
^
kernel/sys_ni.c:271:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
>> kernel/sys_ni.c:272:1: warning: no previous prototype for function '__x64_sys_landlock_restrict_self' [-Wmissing-prototypes]
COND_SYSCALL(landlock_restrict_self);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:238:1: note: expanded from here
__x64_sys_landlock_restrict_self
^
kernel/sys_ni.c:272:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
kernel/sys_ni.c:277:1: warning: no previous prototype for function '__x64_sys_fadvise64_64' [-Wmissing-prototypes]
COND_SYSCALL(fadvise64_64);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:242:1: note: expanded from here
__x64_sys_fadvise64_64
^
kernel/sys_ni.c:277:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
kernel/sys_ni.c:280:1: warning: no previous prototype for function '__x64_sys_swapon' [-Wmissing-prototypes]
COND_SYSCALL(swapon);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:246:1: note: expanded from here
__x64_sys_swapon
^
kernel/sys_ni.c:280:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
kernel/sys_ni.c:281:1: warning: no previous prototype for function '__x64_sys_swapoff' [-Wmissing-prototypes]
COND_SYSCALL(swapoff);
^
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:14: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^
<scratch space>:250:1: note: expanded from here
__x64_sys_swapoff
^
kernel/sys_ni.c:281:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
arch/x86/include/asm/syscall_wrapper.h:256:2: note: expanded from macro 'COND_SYSCALL'
__X64_COND_SYSCALL(name) \
^
arch/x86/include/asm/syscall_wrapper.h:100:2: note: expanded from macro '__X64_COND_SYSCALL'
__COND_SYSCALL(x64, sys_##name)
^
arch/x86/include/asm/syscall_wrapper.h:83:9: note: expanded from macro '__COND_SYSCALL'
__weak long __##abi##_##name(const struct pt_regs *__unused) \
^ kernel/sys_ni.c:282:1: warning: no previous prototype for function '__x64_sys_mprotect' [-Wmissing-prototypes]
vim +/__x64_sys_landlock_restrict_self +272 kernel/sys_ni.c
268
269 /* security/landlock/syscalls.c */
270 COND_SYSCALL(landlock_create_ruleset);
271 COND_SYSCALL(landlock_add_rule);
> 272 COND_SYSCALL(landlock_restrict_self);
273
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Mickaël Salaün
maintainerPATCH v28 9/12 arch: Wire up Landlock syscalls
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
Wire up the following system calls for all architectures:
* landlock_create_ruleset(2)
* landlock_add_rule(2)
* landlock_restrict_self(2)
---
Changes since v27:
* Rename landlock_enforce_ruleset_self(2) to landlock_restrict_self(2).
* Cosmetic fix: align TBL enries.
Changes since v26:
* Rename landlock_enforce_ruleset_current(2) to landlock_enforce_ruleset_self(2).
Changes since v25:
* Rebase and leave space for the new epoll_pwait2(2) and memfd_secret(2) from -next.
Changes since v21:
* Rebase and leave space for watch_mount(2) from -next.
Changes since v20:
* Remove landlock_get_features(2).
* Decrease syscall numbers to stick to process_madvise(2) in -next.
* Rename landlock_enforce_ruleset(2) to landlock_enforce_ruleset_current(2).
Changes since v19:
* Increase syscall numbers by 4 to leave space for new ones (in linux-next): watch_mount(2), watch_sb(2), fsinfo(2) and process_madvise(2) (requested by Arnd Bergmann).
* Replace the previous multiplexor landlock(2) with 4 syscalls:
landlock_get_features(2), landlock_create_ruleset(2), landlock_add_rule(2) and landlock_enforce_ruleset(2).
Changes since v18:
* Increase the syscall number because of the new faccessat2(2).
Changes since v14:
* Add all architectures.
Changes since v13:
* New implementation.
--- arch/alpha/kernel/syscalls/syscall.tbl | 3 +++ arch/arm/tools/syscall.tbl | 3 +++ arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 6 ++++++ arch/ia64/kernel/syscalls/syscall.tbl | 3 +++ arch/m68k/kernel/syscalls/syscall.tbl | 3 +++ arch/microblaze/kernel/syscalls/syscall.tbl | 3 +++ arch/mips/kernel/syscalls/syscall_n32.tbl | 3 +++ arch/mips/kernel/syscalls/syscall_n64.tbl | 3 +++ arch/mips/kernel/syscalls/syscall_o32.tbl | 3 +++ arch/parisc/kernel/syscalls/syscall.tbl | 3 +++ arch/powerpc/kernel/syscalls/syscall.tbl | 3 +++ arch/s390/kernel/syscalls/syscall.tbl | 3 +++ arch/sh/kernel/syscalls/syscall.tbl | 3 +++ arch/sparc/kernel/syscalls/syscall.tbl | 3 +++ arch/x86/entry/syscalls/syscall_32.tbl | 3 +++ arch/x86/entry/syscalls/syscall_64.tbl | 3 +++ arch/xtensa/kernel/syscalls/syscall.tbl | 3 +++ include/uapi/asm-generic/unistd.h | 8 +++++++- 19 files changed, 62 insertions(+), 2 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +3 -0
@@ -481,3 +481,6 @@ 549 common faccessat2 sys_faccessat2 550 common process_madvise sys_process_madvise 551 common epoll_pwait2 sys_epoll_pwait2 +554 common landlock_create_ruleset sys_landlock_create_ruleset +555 common landlock_add_rule sys_landlock_add_rule +556 common landlock_restrict_self sys_landlock_restrict_self
arch/arm/tools/syscall.tbl +3 -0
@@ -455,3 +455,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/arm64/include/asm/unistd.h +1 -1
@@ -38,7 +38,7 @@#define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5)#define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800) -#define __NR_compat_syscalls 442 +#define __NR_compat_syscalls 447#endif #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +6 -0
@@ -891,6 +891,12 @@ __SYSCALL(__NR_faccessat2, sys_faccessat2) __SYSCALL(__NR_process_madvise, sys_process_madvise)#define __NR_epoll_pwait2 441 __SYSCALL(__NR_epoll_pwait2, compat_sys_epoll_pwait2) +#define __NR_landlock_create_ruleset 444 +__SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset) +#define __NR_landlock_add_rule 445 +__SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule) +#define __NR_landlock_restrict_self 446 +__SYSCALL (__NR_landlock_restrict_self, sys_landlock_restrict_self)/* * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +3 -0
@@ -362,3 +362,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/m68k/kernel/syscalls/syscall.tbl +3 -0
@@ -441,3 +441,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/microblaze/kernel/syscalls/syscall.tbl +3 -0
@@ -447,3 +447,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/mips/kernel/syscalls/syscall_n32.tbl +3 -0
@@ -380,3 +380,6 @@ 439 n32 faccessat2 sys_faccessat2 440 n32 process_madvise sys_process_madvise 441 n32 epoll_pwait2 compat_sys_epoll_pwait2 +444 n32 landlock_create_ruleset sys_landlock_create_ruleset +445 n32 landlock_add_rule sys_landlock_add_rule +446 n32 landlock_restrict_self sys_landlock_restrict_self
arch/mips/kernel/syscalls/syscall_n64.tbl +3 -0
@@ -356,3 +356,6 @@ 439 n64 faccessat2 sys_faccessat2 440 n64 process_madvise sys_process_madvise 441 n64 epoll_pwait2 sys_epoll_pwait2 +444 n64 landlock_create_ruleset sys_landlock_create_ruleset +445 n64 landlock_add_rule sys_landlock_add_rule +446 n64 landlock_restrict_self sys_landlock_restrict_self
arch/mips/kernel/syscalls/syscall_o32.tbl +3 -0
@@ -429,3 +429,6 @@ 439 o32 faccessat2 sys_faccessat2 440 o32 process_madvise sys_process_madvise 441 o32 epoll_pwait2 sys_epoll_pwait2 compat_sys_epoll_pwait2 +444 o32 landlock_create_ruleset sys_landlock_create_ruleset +445 o32 landlock_add_rule sys_landlock_add_rule +446 o32 landlock_restrict_self sys_landlock_restrict_self
arch/parisc/kernel/syscalls/syscall.tbl +3 -0
@@ -439,3 +439,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 compat_sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/powerpc/kernel/syscalls/syscall.tbl +3 -0
@@ -531,3 +531,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 compat_sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/s390/kernel/syscalls/syscall.tbl +3 -0
@@ -444,3 +444,6 @@ 439 common faccessat2 sys_faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 compat_sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self sys_landlock_restrict_self
arch/sh/kernel/syscalls/syscall.tbl +3 -0
@@ -444,3 +444,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/sparc/kernel/syscalls/syscall.tbl +3 -0
@@ -487,3 +487,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 compat_sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
arch/x86/entry/syscalls/syscall_32.tbl +3 -0
@@ -446,3 +446,6 @@ 439 i386 faccessat2 sys_faccessat2 440 i386 process_madvise sys_process_madvise 441 i386 epoll_pwait2 sys_epoll_pwait2 compat_sys_epoll_pwait2 +444 i386 landlock_create_ruleset sys_landlock_create_ruleset +445 i386 landlock_add_rule sys_landlock_add_rule +446 i386 landlock_restrict_self sys_landlock_restrict_self
arch/x86/entry/syscalls/syscall_64.tbl +3 -0
@@ -363,6 +363,9 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self # # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +3 -0
@@ -412,3 +412,6 @@ 439 common faccessat2 sys_faccessat2 440 common process_madvise sys_process_madvise 441 common epoll_pwait2 sys_epoll_pwait2 +444 common landlock_create_ruleset sys_landlock_create_ruleset +445 common landlock_add_rule sys_landlock_add_rule +446 common landlock_restrict_self sys_landlock_restrict_self
include/uapi/asm-generic/unistd.h +7 -1
@@ -861,9 +861,15 @@ __SYSCALL(__NR_faccessat2, sys_faccessat2) __SYSCALL(__NR_process_madvise, sys_process_madvise)#define __NR_epoll_pwait2 441 __SC_COMP(__NR_epoll_pwait2, sys_epoll_pwait2, compat_sys_epoll_pwait2) +#define __NR_landlock_create_ruleset 444 +__SYSCALL(__NR_landlock_create_ruleset, sys_landlock_create_ruleset) +#define __NR_landlock_add_rule 445 +__SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule) +#define __NR_landlock_restrict_self 446 +__SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)#undef __NR_syscalls -#define __NR_syscalls 442 +#define __NR_syscalls 447/* * 32 bit systems traditionally used different
Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 10/12 selftests/landlock: Add user space tests
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
Test all Landlock system calls, ptrace hooks semantic and filesystem access-control.
Test coverage for security/landlock/ is 94.7% of lines. The code not covered only deals with internal kernel errors (e.g. memory allocation) and race conditions.
---
Changes since v27:
* Add layout1.non_overlapping_accesses to check rules without overlapping access rights (fixed in this patchset).
* Extend layout1.interleaved_masked_accesses with a non-overlapping execute-only rule.
* Update tests for empty path_beneath.allowed_access, and replace useless (i.e. deny-only rules) code in layout1.interleaved_masked_accesses with equivalent meaningful rules.
* Fix the returned step when a test failed with TEST_F_FORK().
* Update MAINTAINERS.
* Cosmetic fix to please checkpatch.
* Fix typo in comment.
* Update landlock_restrict_self(2).
Changes since v26:
* Add layout1_bind tests to check inherited bind mount accesses.
* Add layout2_overlay tests to check non-inherited overlayfs accesses.
* Fix final cleanup which was reordered because of kselftest_harness changes.
* Update layout1.inherit_subset test according to the check_access_path_layer() change.
* Implement TEST_F_FORK() to be able to use FIXTURE_TEARDOWN() to clean up layouts even if the test (child) lost access rights or failed. Remove now useless layout*.cleanup .
* Update syscall names.
* Clean up FIXTURE_SETUP(layout1).
* Clean up file layout management:
- Replace specific create_dir_and_file() with generic create_directory() and create_file().
- Replace specific delete_dir_and_file() with generic remove_path().
- Rename and move cleanup_*() to remove_*() to improve readability.
- Use EXPECT_*() for all FIXTURE_TEARDOWN() code.
Changes since v25:
* Add a new test to check that Landlock ruleset file descriptors received through UNIX sockets are usable. Contributed by Vincent Dagonneau.
* Improve hierarchy.trace tests to not hang when testing on a kernel that don't support Landlock.
* Replace EXPECT_EQ(0, close(*)) with ASSERT_EQ(0, close(*)).
* Guard WEXITSTATUS() use with WIFEXITED() in ptrace tests.
* Use pipe2(2) with O_CLOEXEC.
* Remove useless errno set for syscall wrappers, and related useless checks.
* Rename test.
* Add Microsoft copyright for layout1.interleaved_masked_accesses .
Changes since v24:
* Revert the ruleset_overlap test from v24: check that access righs are ORed together when building a ruleset. Keep the extra checks added with v24.
* Revert inherit_subset test from v24: use the automatic ORing of access rights for the same file.
* Update interleaved_masked_accesses test (added with v24) to stop when all layers allowed at least one time an inode in the path walk.
* Extend interleaved_masked_accesses test with new tricky interleaved layers which would not work as intended with (allow or deny) bitmask layer implementations.
* Simplify and rename test_path*() to test_open*() to make easier the diagnostic in case of unattended errors.
* Replace most call to open(2) with a call to test_open(), which reduces the number of lines and make tests more readable.
* Fix erroneous check in inherit_superset.
Changes since v23:
* Add an interleaved_masked_accesses test to check corner cases for interleaved layered ruleset combinations.
* Update ruleset_overlap and inherit_subset tests to follow the new intersect access rights behavior.
* Extend the inherit_superset test to check that layers are handled as expected in the superset use case, which complete the inherit_subset checks.
* Fix comment (spotted by Vincent Dagonneau).
Changes since v22:
* Extend and add a new test to better check rules applied to the root
directory: rule_over_root_allow_then_deny, rule_over_root_deny.
* Change the signature of test_path*() to make the calls clearer.
Changes since v21:
* Remove layout1.chroot test and update layout1.unhandled_access to not rely on LANDLOCK_ACCESS_FS_CHROOT.
* Clean up comments.
Changes since v20:
* Update with new syscalls and type names.
* Use the full syscall interfaces: explicitely set the "flags" field to zero.
* Update the empty_path_beneath_attr test to check for EFAULT.
* Update and merge tests for the simplified copy_min_struct_from_user().
* Clean up makefile.
* Rename some types and variables in a more consistent way.
Changes since v19:
* Update with the new Landlock syscalls.
* Fix device creation.
* Check the new landlock_attr_features members: last_rule_type and last_target_type .
* Constify variables.
Changes since v18:
* Replace ruleset_rw.inval with layout1.inval to avoid inexistent test layout.
* Use the new FIXTURE_VARIANT for ptrace_test: makes the tests more readable and usable.
* Add ARRAY_SIZE() macro to please checkpatch.
Changes since v17:
* Add new test for mknod with a zero mode.
* Use memset(3) to initialize attr_features in base_test.
Changes since v16:
* Add new unpriv_enforce_without_no_new_privs test: check that ruleset enforcing is forbiden without no_new_privs and CAP_SYS_ADMIN.
* Drop capabilities when useful.
* Check the new size_attr_features field from struct landlock_attr_features.
* Update the empty_or_same_ruleset test to check complementary empty ruleset.
* Update base_test according to the new attribute structures and fix the inconsistent_attr test accordingly.
* Switch syscall attribute pointer and size arguments.
* Rename test files with a "_test" suffix.
Changes since v14:
* Add new tests:
- superset: check new layer bitmask.
- max_layers: check maximum number of layers.
- release_inodes: check that umount work well.
- empty_or_same_ruleset.
- inconsistent_attr: checks copy_to_user limits.
- in ruleset_rw.inval to check ruleset FD.
- proc_unlinked_file: check file access through /proc/self/fd .
- file_access_rights: check that a file can only get consistent access rights.
- unpriv: check that NO_NEW_PRIVS or CAP_SYS_ADMIN is required.
- check pipe access through /proc/self/fd .
- check move_mount(2).
- check ruleset file descriptor properties.
- proc_nsfs: extend to check that internal filesystems (e.g. nsfs) are allowed.
* Double-check read and write effective actions.
* Fix potential desynchronization between the kernel sources and installed headers by overriding the build step in the Makefile. This also enable to build with Clang.
* Add two files in the test directories (for link test and rename test).
* Remove test for ruleset's show_fdinfo().
* Replace EBADR with EBADFD.
* Update tests accordingly to the changes of rename and link rights.
* Fix (now) illegal access rights tied to files.
* Update rename and link tests.
* Remove superfluous '\n' in TH_LOG() calls.
* Make assert calls consistent and readable.
* Fix the execute test.
* Make tests future-proof.
* Cosmetic fixes.
Changes since v14:
* Add new tests:
- Compatibility: empty_attr_{ruleset,path_beneath,enforce} to check minimal attr size.
- Access types: link_to, rename_from, rename_to, rmdir, unlink, make_char, make_block, make_reg, make_sock, make_fifo, make_sym, make_dir, chroot, execute.
- Test privilege escalation prevention by enforcing a nested rule, on a parent directory, with less restrictions than one on a child directory.
- Test for empty and more than 32-bits allowed_access
* Merge the two test mount hierarchies.
* Complete relative path tests by combining chdir and chroot.
* Adjust tests:
- Remove the layout1/extend_ruleset_with_denied_path test.
- Extend layout1/whitelist test with checks on file.
- Add and use create_dir_and_file().
* Only use read/write checks but not stat(2) for tests.
* Rename test.h to common.h and improve it.
* Rename path name to make them more consistent, easy to understand and make them in a common directory.
* Make create_ruleset() more generic.
* Constify variables.
* Re-add static global variables.
* Remove useless openat(2).
* Fix and complete kernel config.
* Set umask and clean up file modes.
* Clean up open flags.
* Improve Makefile.
* Fix spelling.
* Improve comments and error messages.
Changes since v13:
* Add back the filesystem tests (from v10) and extend them.
* Add tests for the new syscall.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/landlock/.gitignore | 2 + tools/testing/selftests/landlock/Makefile | 24 + tools/testing/selftests/landlock/base_test.c | 219 ++ tools/testing/selftests/landlock/common.h | 169 ++ tools/testing/selftests/landlock/config | 6 + tools/testing/selftests/landlock/fs_test.c | 2664 +++++++++++++++++ .../testing/selftests/landlock/ptrace_test.c | 314 ++ tools/testing/selftests/landlock/true.c | 5 + 10 files changed, 3405 insertions(+) create mode 100644 tools/testing/selftests/landlock/.gitignore create mode 100644 tools/testing/selftests/landlock/Makefile create mode 100644 tools/testing/selftests/landlock/base_test.c create mode 100644 tools/testing/selftests/landlock/common.h create mode 100644 tools/testing/selftests/landlock/config create mode 100644 tools/testing/selftests/landlock/fs_test.c create mode 100644 tools/testing/selftests/landlock/ptrace_test.c create mode 100644 tools/testing/selftests/landlock/true.c
MAINTAINERS +1 -0
@@ -9944,6 +9944,7 @@ W: https://landlock.io T: git https://github.com/landlock-lsm/linux.git F: include/uapi/linux/landlock.h F: security/landlock/ +F: tools/testing/selftests/landlock/ K: landlock K: LANDLOCK
tools/testing/selftests/Makefile +1 -0
@@ -25,6 +25,7 @@ TARGETS += ir TARGETS += kcmp TARGETS += kexec TARGETS += kvm +TARGETS += landlock TARGETS += lib TARGETS += livepatch TARGETS += lkdtm
tools/testing/selftests/landlock/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +/*_test +/true
tools/testing/selftests/landlock/Makefile new file +24 -0
@@ -0,0 +1,24 @@ +# SPDX-License-Identifier: GPL-2.0 + +CFLAGS += -Wall -O2 + +src_test := $(wildcard *_test.c) + +TEST_GEN_PROGS := $(src_test:.c=) + +TEST_GEN_PROGS_EXTENDED := true + +KSFT_KHDR_INSTALL := 1 +OVERRIDE_TARGETS := 1 +include ../lib.mk + +khdr_dir = $(top_srcdir)/usr/include + +$(khdr_dir)/linux/landlock.h: khdr + @: + +$(OUTPUT)/true: true.c + $(LINK.c) $< $(LDLIBS) -o $@ -static + +$(OUTPUT)/%_test: %_test.c $(khdr_dir)/linux/landlock.h ../kselftest_harness.h common.h + $(LINK.c) $< $(LDLIBS) -o $@ -lcap -I$(khdr_dir)
tools/testing/selftests/landlock/base_test.c new file +219 -0
219 lines not on this page · raw message
tools/testing/selftests/landlock/common.h new file +169 -0
169 lines not on this page · raw message
tools/testing/selftests/landlock/config new file +6 -0
@@ -0,0 +1,6 @@ +CONFIG_OVERLAY_FS=y +CONFIG_SECURITY_LANDLOCK=y +CONFIG_SECURITY_PATH=y +CONFIG_SECURITY=y +CONFIG_SHMEM=y +CONFIG_TMPFS=y
tools/testing/selftests/landlock/fs_test.c new file +2664 -0
2,566 lines not on this page · raw message
tools/testing/selftests/landlock/ptrace_test.c new file +314 -0
314 lines not on this page · raw message
tools/testing/selftests/landlock/true.c new file +5 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.30.0
Part of the diff is not shown here. View the raw message.
Mickaël Salaün
maintainerPATCH v28 11/12 samples/landlock: Add a sandbox manager example
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
Add a basic sandbox tool to launch a command which can only access a list of file hierarchies in a read-only or read-write way.
---
Changes since v27:
* Add samples/landlock/ to MAINTAINERS.
* Update landlock_restrict_self(2).
* Tweak Kconfig title and description.
Changes since v25:
* Improve comments and fix help (suggested by Jann Horn).
* Add a safeguard for errno check (suggested by Jann Horn).
* Allows users to not use all possible restrictions (e.g. use LL_FS_RO without LL_FS_RW).
* Update syscall names.
* Improve Makefile:
- Replace hostprogs/always-y with userprogs-always-y, available since commit faabed295ccc ("kbuild: introduce hostprogs-always-y and userprogs-always-y").
- Depends on CC_CAN_LINK.
* Add Reviewed-by Jann Horn.
Changes since v25:
* Remove useless errno set in the syscall wrappers.
* Cosmetic variable renames.
Changes since v23:
* Re-add hints to help users understand the required kernel configuration. This was removed with the removal of landlock_get_features(2).
Changes since v21:
* Remove LANDLOCK_ACCESS_FS_CHROOT.
* Clean up help.
Changes since v20:
* Update with new syscalls and type names.
* Update errno check for EOPNOTSUPP.
* Use the full syscall interfaces: explicitely set the "flags" field to zero.
Changes since v19:
* Update with the new Landlock syscalls.
* Comply with commit 5f2fb52fac15 ("kbuild: rename hostprogs-y/always to hostprogs/always-y").
Changes since v16:
* Switch syscall attribute pointer and size arguments.
Changes since v15:
* Update access right names.
* Properly assign access right to files according to the new related syscall restriction.
* Replace "select" with "depends on" HEADERS_INSTALL (suggested by Randy Dunlap).
Changes since v14:
* Fix Kconfig dependency.
* Remove access rights that may be required for FD-only requests:
mmap, truncate, getattr, lock, chmod, chown, chgrp, ioctl.
* Fix useless hardcoded syscall number.
* Use execvpe().
* Follow symlinks.
* Extend help with common file paths.
* Constify variables.
* Clean up comments.
* Improve error message.
Changes since v11:
* Add back the filesystem sandbox manager and update it to work with the new Landlock syscall.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 1 + samples/Kconfig | 7 ++ samples/Makefile | 1 + samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 13 ++ samples/landlock/sandboxer.c | 238 +++++++++++++++++++++++++++++++++++ 6 files changed, 261 insertions(+) create mode 100644 samples/landlock/.gitignore create mode 100644 samples/landlock/Makefile create mode 100644 samples/landlock/sandboxer.c
MAINTAINERS +1 -0
@@ -9943,6 +9943,7 @@ S: Supported W: https://landlock.io T: git https://github.com/landlock-lsm/linux.git F: include/uapi/linux/landlock.h +F: samples/landlock/ F: security/landlock/ F: tools/testing/selftests/landlock/ K: landlock
samples/Kconfig +7 -0
@@ -124,6 +124,13 @@ config SAMPLE_HIDRAW bool "hidraw sample" depends on CC_CAN_LINK && HEADERS_INSTALL +config SAMPLE_LANDLOCK + bool "Build Landlock example" + depends on CC_CAN_LINK && HEADERS_INSTALL + help + Build a simple Landlock sandbox manager able to start a process + restricted by a user-defined filesystem access control policy. + config SAMPLE_PIDFD bool "pidfd sample" depends on CC_CAN_LINK && HEADERS_INSTALL
samples/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_SAMPLE_KDB) += kdb/ obj-$(CONFIG_SAMPLE_KFIFO) += kfifo/ obj-$(CONFIG_SAMPLE_KOBJECT) += kobject/ obj-$(CONFIG_SAMPLE_KPROBES) += kprobes/ +subdir-$(CONFIG_SAMPLE_LANDLOCK) += landlock obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch/ subdir-$(CONFIG_SAMPLE_PIDFD) += pidfd obj-$(CONFIG_SAMPLE_QMI_CLIENT) += qmi/
samples/landlock/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/sandboxer
samples/landlock/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause + +userprogs-always-y := sandboxer + +userccflags += -I usr/include + +.PHONY: all clean + +all: + $(MAKE) -C ../.. samples/landlock/ + +clean: + $(MAKE) -C ../.. M=samples/landlock/ clean
samples/landlock/sandboxer.c new file +238 -0
238 lines not on this page · raw message
Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v28 12/12 landlock: Add user and kernel documentation
In reply to Mickaël SalaünFrom: Mickaël Salaün <[email protected]>
This documentation can be built with the Sphinx framework.
---
Changes since v27:
* Update landlock_restrict_self(2).
* Update date and copyright.
Changes since v25:
* Explain the behavior of layered access rights.
* Explain how bind mounts and overayfs mounts are handled by Landlock:
merged overlayfs mount points have their own inodes, which makes these hierarchies independent from its upper and lower layers, unlike bind mounts which share the same inodes between the source hierarchy and the mount point hierarchy.
New overlayfs mount and bind mount tests check these behaviors.
* Synchronize with the new syscalls.c file and update syscall names.
* Fix spelling.
* Remove Reviewed-by Jann Horn because of the above changes.
Changes since v24:
* Add Reviewed-by Jann Horn.
* Add a paragraph to explain how the ruleset layers work.
* Bump date.
Changes since v23:
* Explain limitations for the maximum number of stacked ruleset, and the memory usage restrictions.
Changes since v22:
* Fix spelling and remove obsolete sentence (spotted by Jann Horn).
* Bump date.
Changes since v21:
* Move the user space documentation to userspace-api/landlock.rst and the kernel documentation to security/landlock.rst .
* Add license headers.
* Add last update dates.
* Update MAINTAINERS file.
* Add (back) links to git.kernel.org .
* Fix spelling.
Changes since v20:
* Update examples and documentation with the new syscalls.
Changes since v19:
* Update examples and documentation with the new syscalls.
Changes since v15:
* Add current limitations.
Changes since v14:
* Fix spelling (contributed by Randy Dunlap).
* Extend documentation about inheritance and explain layer levels.
* Remove the use of now-removed access rights.
* Use GitHub links.
* Improve kernel documentation.
* Add section for tests.
* Update example.
Changes since v13:
* Rewrote the documentation according to the major revamp.
Previous changes:
https://lore.kernel.org/lkml/[email protected]/
--- Documentation/security/index.rst | 1 + Documentation/security/landlock.rst | 79 ++++++ Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/landlock.rst | 307 +++++++++++++++++++++++ MAINTAINERS | 2 + 5 files changed, 390 insertions(+) create mode 100644 Documentation/security/landlock.rst create mode 100644 Documentation/userspace-api/landlock.rst
Documentation/security/index.rst +1 -0
@@ -16,3 +16,4 @@ Security Documentation siphash tpm/index digsig + landlock
Documentation/security/landlock.rst new file +79 -0
79 lines not on this page · raw message
Documentation/userspace-api/index.rst +1 -0
@@ -18,6 +18,7 @@ place where this information is gathered. no_new_privs seccomp_filter + landlock unshare spec_ctrl accelerators/ocxl
Documentation/userspace-api/landlock.rst new file +307 -0
307 lines not on this page · raw message
MAINTAINERS +2 -0
@@ -9942,6 +9942,8 @@ L: [email protected] S: Supported W: https://landlock.io T: git https://github.com/landlock-lsm/linux.git +F: Documentation/security/landlock.rst +F: Documentation/userspace-api/landlock.rst F: include/uapi/linux/landlock.h F: samples/landlock/ F: security/landlock/
Signature · 1 line
2.30.0