Landlock LSM
Subsystems (24)
Mickaël Salaün
maintainerv27 cover Landlock LSM
Hi,
This patch series adjusts the semantic of file hierarchy access-control per layer to get a more pragmatic and compatible approach. I updated the documentation to explain how layers, bind mounts and overlayfs are handled by Landlock. A syscall is also renamed to make it less ambiguous for future evolution. Last but not least, the test file layout cleanups are more resilient, and a lot of tests are added to cover bind mounts and overlayfs, which are fully supported.
The SLOC count is 1292 for security/landlock/ and 2425 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-v27/userspace-api/landlock.html
This series can be applied on top of v5.11-rc4 . 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-v27
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 | 306 ++ MAINTAINERS | 13 + 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 | 239 ++ 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 | 621 ++++ 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 | 466 +++ security/landlock/ruleset.h | 161 + security/landlock/setup.c | 40 + security/landlock/setup.h | 18 + security/landlock/syscalls.c | 429 +++ 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 | 166 ++ tools/testing/selftests/landlock/config | 6 + tools/testing/selftests/landlock/fs_test.c | 2585 +++++++++++++++++ .../testing/selftests/landlock/ptrace_test.c | 314 ++ tools/testing/selftests/landlock/true.c | 5 + 72 files changed, 6552 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: 19c329f6808995b142b3966301f217c831e7cf31
Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v27 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 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_enforce_ruleset_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
Mickaël Salaün
maintainerPATCH v27 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 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 | 462 ++++++++++++++++++++++++++++++++++++ security/landlock/ruleset.h | 161 +++++++++++++ 4 files changed, 641 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 +462 -0
462 lines not on this page · raw message
security/landlock/ruleset.h new file +161 -0
161 lines not on this page · raw message
Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v27 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
Mickaël Salaün
maintainerPATCH v27 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
Mickaël Salaün
maintainerPATCH v27 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
Mickaël Salaün
maintainerPATCH v27 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
Mickaël Salaün
maintainerPATCH v27 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 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 | 621 ++++++++++++++++++++++++++++++++++ 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, 780 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 +621 -0
621 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
@@ -112,10 +112,12 @@ static void build_check_ruleset(void)const struct landlock_ruleset ruleset = { .num_rules = ~0 , .num_layers = ~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 (ruleset.fs_access_mask <LANDLOCK_MASK_ACCESS_FS ); }/**
@@ -214,9 +216,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 ); } int landlock_insert_rule(struct landlock_ruleset *const ruleset,
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
Mickaël Salaün
maintainerRe: [PATCH v27 07/12] landlock: Support filesystem access-control
I found a corner-case which is not well handled, e.g.
* layer1 only restricts X access and allows X access for /a -> X is allowed for /a
* layer2 only restricts Y access and allows Y access for /a/b -> X is not allowed for /a anymore because now the path walk for /a doesn't encounter a layer 2 rule
I'm working on a fix and I'll send a new patch series soon. Stay tune!
Quoted 1,141 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 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_INODESfor 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.
Cc Al Viro <[email protected]>Cc Anton Ivanov <[email protected]>Cc James Morris <[email protected]>Cc Jann Horn <[email protected]>Cc Jeff Dike <[email protected]>Cc Kees Cook <[email protected]>Cc Richard Weinberger <[email protected]>Cc Serge E. Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
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/
* Renamecheck_access_path_continue()tounmask_layers()and make it return the new layer mask.
* Avoid double domain check inhook_file_open().
* In the documentation, add utime(2) as another example of unhandled syscalls. Indeed, usingtouchto test write access may be tempting.
* Remove outdated comment about OverlayFS.
* Rename thelandlock.hifdef to align with most similar files.
* Fix spelling.Changes since v25:
* Movebuild_check_layer()toruleset.c, and add built-time checks for thefs_access_maskand access variables according to_LANDLOCK_ACCESS_FS_MASK.
* Move limits to a dedicated file and rename them:
_LANDLOCK_ACCESS_FS_LASTand_LANDLOCK_ACCESS_FS_MASK.
* Setbuild_check_layer()as non-inline to trigger a warning if it is not called.
* UseBITS_PER_TYPE()macro.
* Rename function tolandlock_add_fs_hooks().
* Cosmetic variable renames.Changes since v24:
* Use the new structlandlock_ruleandlandlock_layerto 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 fromtools/testing/selftests/landlock/fs_test.cfor corner cases.
* Add build-time checks for layers.
* Use the newlandlock_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 explaincheck_access_path_continue().
* Fix memory allocation error handling inlandlock_create_object()calls. This prevent to inadvertently hold an inode.
* Inget_inode_object(), improve comments, make code more readable and movekfree()call out of the lock window.
* Use the simplifiedlandlock_insert_rule()API.Changes since v22:
* Simplifycheck_access_path_continue()(suggested by Jann Horn).
* Removeprefetch()call for now (suggested by Jann Horn).
* Fix spelling and remove superfluous comment (spotted by Jann Horn).
* Cosmetic variable renaming.Changes since v21:
* RenameARCH_EPHEMERAL_STATEStoARCH_EPHEMERAL_INODES(suggested by James Morris).
* Remove theLANDLOCK_ACCESS_FS_CHROOTright because chroot(2) (which requiresCAP_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:
* Replacelandlock_release_inodes()withsecurity_sb_delete()(requested by James Morris).
* Replace structsuper_block->s_landlock_inode_refswith 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 inpath_mknodandfile_openhooks when the current tasks is not sandboxed.
* Remove useless checks on the file pointer and inode inhook_file_open().
* Constify domain pointers.
* Renameinode_landlock()tolandlock_inode().
* Importinclude/uapi/linux/landlock.hand _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:
* AddARCH_EPHEMERAL_STATESand enable it for UML.Changes since v15:
* Replacelayer_levelsandlayer_depthwith 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 theLANDLOCK_ACCESS_FS_LINK_TOand 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.
* Checkd_is_negative()instead of !d_backing_inode() incheck_access_path_continue(), and continue the path walk while there is no mapped inode e.g., with rename(2).
* Check private inode incheck_access_path().
* Optimizeget_file_access()when dealing with a directory.
* Add missingatomic.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):
- Rewriterelease_inode()to useinode->sb->s_landlock_inode_refs.
- Remove useless checks inlandlock_release_inodes(), clean object
pointer according to the new structlandlock_objectand wait for all
iput()to complete.
- Rewriteget_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.
* Prefetchesd_parentwhen walking to the root directory.
* Remove uselessinode_alloc_securityhook()(suggested by Jann Horn):
already initialized bylsm_inode_alloc().
* Remove theinode_free_securityhook.
* 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 intosyscall.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 | 621 ++++++++++++++++++++++++++++++++++ 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, 780 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/landlock.h create mode 100644 security/landlock/fs.c create mode 100644 security/landlock/fs.hMAINTAINERS +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: LANDLOCKarch/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 boolarch/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_AUDITSYSCALLinclude/uapi/linux/landlock.h new file +75
-0@@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* Landlock - User space API +* +* Copyright © 2017-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _UAPI_LINUX_LANDLOCK_H +#define _UAPI_LINUX_LANDLOCK_H + +/** +* DOC: fs_access +* +* A set of actions on kernel objects may be defined by an attribute (e.g. +* &struct landlock_path_beneath_attr) including a bitmask of access. +* +* Filesystem flags +* ~~~~~~~~~~~~~~~~ +* +* These flags enable to restrict a sandboxed process to a set of actions on +* files and directories. Files or directories opened before the sandboxing +* are not subject to these restrictions. +* +* A file can only receive these access rights: +* +* - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file. +* - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. +* - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access. +* +* A directory can receive access rights related to files or directories. The +* following access right is applied to the directory itself, and the +* directories beneath it: +* +* - %LANDLOCK_ACCESS_FS_READ_DIR: Open a directory or list its content. +* +* However, the following access rights only apply to the content of a +* directory, not the directory itself: +* +* - %LANDLOCK_ACCESS_FS_REMOVE_DIR: Remove an empty directory or rename one. +* - %LANDLOCK_ACCESS_FS_REMOVE_FILE: Unlink (or rename) a file. +* - %LANDLOCK_ACCESS_FS_MAKE_CHAR: Create (or rename or link) a character +* device. +* - %LANDLOCK_ACCESS_FS_MAKE_DIR: Create (or rename) a directory. +* - %LANDLOCK_ACCESS_FS_MAKE_REG: Create (or rename or link) a regular file. +* - %LANDLOCK_ACCESS_FS_MAKE_SOCK: Create (or rename or link) a UNIX domain +* socket. +* - %LANDLOCK_ACCESS_FS_MAKE_FIFO: Create (or rename or link) a named pipe. +* - %LANDLOCK_ACCESS_FS_MAKE_BLOCK: Create (or rename or link) a block device. +* - %LANDLOCK_ACCESS_FS_MAKE_SYM: Create (or rename or link) a symbolic link. +* +* .. warning:: +* +* It is currently not possible to restrict some file-related actions +* accessible through these syscall families: :manpage:`chdir(2)`, +* :manpage:`truncate(2)`, :manpage:`stat(2)`, :manpage:`flock(2)`, +* :manpage:`chmod(2)`, :manpage:`chown(2)`, :manpage:`setxattr(2)`, +* :manpage:`utime(2)`, :manpage:`ioctl(2)`, :manpage:`fcntl(2)`. +* Future Landlock evolutions will enable to restrict them. +*/ +#define LANDLOCK_ACCESS_FS_EXECUTE (1ULL << 0) +#define LANDLOCK_ACCESS_FS_WRITE_FILE (1ULL << 1) +#define LANDLOCK_ACCESS_FS_READ_FILE (1ULL << 2) +#define LANDLOCK_ACCESS_FS_READ_DIR (1ULL << 3) +#define LANDLOCK_ACCESS_FS_REMOVE_DIR (1ULL << 4) +#define LANDLOCK_ACCESS_FS_REMOVE_FILE (1ULL << 5) +#define LANDLOCK_ACCESS_FS_MAKE_CHAR (1ULL << 6) +#define LANDLOCK_ACCESS_FS_MAKE_DIR (1ULL << 7) +#define LANDLOCK_ACCESS_FS_MAKE_REG (1ULL << 8) +#define LANDLOCK_ACCESS_FS_MAKE_SOCK (1ULL << 9) +#define LANDLOCK_ACCESS_FS_MAKE_FIFO (1ULL << 10) +#define LANDLOCK_ACCESS_FS_MAKE_BLOCK (1ULL << 11) +#define LANDLOCK_ACCESS_FS_MAKE_SYM (1ULL << 12) + +#endif /* _UAPI_LINUX_LANDLOCK_H */ 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 tosecurity/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.osecurity/landlock/fs.c new file +621
-0@@ -0,0 +1,621 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Filesystem management and hooks +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#include <linux/atomic.h> +#include <linux/bitops.h> +#include <linux/bits.h> +#include <linux/compiler_types.h> +#include <linux/dcache.h> +#include <linux/err.h> +#include <linux/fs.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/limits.h> +#include <linux/list.h> +#include <linux/lsm_hooks.h> +#include <linux/mount.h> +#include <linux/namei.h> +#include <linux/path.h> +#include <linux/rcupdate.h> +#include <linux/spinlock.h> +#include <linux/stat.h> +#include <linux/types.h> +#include <linux/wait_bit.h> +#include <linux/workqueue.h> +#include <uapi/linux/landlock.h> + +#include "common.h" +#include "cred.h" +#include "fs.h" +#include "limits.h" +#include "object.h" +#include "ruleset.h" +#include "setup.h" + +/* Underlying object management */ + +static void release_inode (struct landlock_object *const object) + __releases(object->lock) +{ +struct inode *const inode = object->underobj; +struct super_block *sb; + +if (!inode) { +spin_unlock (&object->lock); +return ; + } + +spin_lock (&inode->i_lock); +/* +* Make sure that if the filesystem is concurrently unmounted, +* hook_sb_delete() will wait for us to finish iput(). +*/ + sb = inode->i_sb; +atomic_long_inc (&landlock_superblock (sb)->inode_refs); +rcu_assign_pointer (landlock_inode (inode)->object,NULL ); +spin_unlock (&inode->i_lock); +spin_unlock (&object->lock); +/* +* Now, new rules can safely be tied to @inode. +*/ + +iput (inode); +if (atomic_long_dec_and_test (&landlock_superblock (sb)->inode_refs)) +wake_up_var (&landlock_superblock (sb)->inode_refs); +} + +static const struct landlock_object_underops landlock_fs_underops = { + .release = release_inode +}; + +/* Ruleset management */ + +static struct landlock_object *get_inode_object (struct inode *const inode) +{ +struct landlock_object *object, *new_object; +struct landlock_inode_security *inode_sec =landlock_inode (inode); + +rcu_read_lock (); +retry: + object =rcu_dereference (inode_sec->object); +if (object) { +if (likely (refcount_inc_not_zero (&object->usage))) { +rcu_read_unlock (); +return object; + } +/* +* We are racing with release_inode(), the object is going +* away. Wait for release_inode(), then retry. +*/ +spin_lock (&object->lock); +spin_unlock (&object->lock); + goto retry; + } +rcu_read_unlock (); + +/* +* If there is no object tied to @inode, then create a new one (without +* holding any locks). +*/ + new_object =landlock_create_object (&landlock_fs_underops, inode); +if (IS_ERR (new_object)) +return new_object; + +spin_lock (&inode->i_lock); + object =rcu_dereference_protected (inode_sec->object, +lockdep_is_held (&inode->i_lock)); +if (unlikely (object)) { +/* Someone else just created the object, bail out and retry. */ +spin_unlock (&inode->i_lock); +kfree (new_object); + +rcu_read_lock (); + goto retry; + } + +rcu_assign_pointer (inode_sec->object, new_object); +/* +* @inode will be released by hook_sb_delete() on its superblock +* shutdown. +*/ +ihold (inode); +spin_unlock (&inode->i_lock); +return new_object; +} + +/* All access rights that can be tied to files. */ +#define ACCESS_FILE ( \ + LANDLOCK_ACCESS_FS_EXECUTE | \ + LANDLOCK_ACCESS_FS_WRITE_FILE | \ + LANDLOCK_ACCESS_FS_READ_FILE) + +/* +* @path: Should have been checked by get_path_from_fd(). +*/ +int landlock_append_fs_rule (struct landlock_ruleset *const ruleset, +const struct path *const path,u32 access_rights) +{ +int err; +struct landlock_object *object; + +/* Files only get access rights that make sense. */ +if (!d_is_dir (path->dentry) && (access_rights |ACCESS_FILE ) != +ACCESS_FILE ) +return -EINVAL ; + +/* Transforms relative access rights to absolute ones. */ + access_rights |=LANDLOCK_MASK_ACCESS_FS & ~ruleset->fs_access_mask; + object =get_inode_object (d_backing_inode (path->dentry)); +if (IS_ERR (object)) +return PTR_ERR (object); +mutex_lock (&ruleset->lock); + err =landlock_insert_rule (ruleset, object, access_rights); +mutex_unlock (&ruleset->lock); +/* +* No need to check for an error because landlock_insert_rule() +* increments the refcount for the new object if needed. +*/ +landlock_put_object (object); +return err; +} + +/* Access-control management */ + +static inline u64 unmask_layers ( +const struct landlock_ruleset *const domain, +const struct path *const path,const u32 access_request, +u64 layer_mask) +{ +const struct landlock_rule *rule; +const struct inode *inode; +size_t i; + +if (d_is_negative (path->dentry)) +/* Continues to walk while there is no mapped inode. */ +return layer_mask; + inode =d_backing_inode (path->dentry); +rcu_read_lock (); + rule =landlock_find_rule (domain, +rcu_dereference (landlock_inode (inode)->object)); +rcu_read_unlock (); +if (!rule) +return layer_mask; + +/* +* An access is granted if, for each policy layer, at least one rule +* encountered on the pathwalk grants the requested accesses, +* regardless of their position in the layer stack. We must then check +* the remaining layers for each inode, from the last added layer to +* the first one. +*/ +for (i =0 ; i < rule->num_layers; i++) { +const struct landlock_layer *const layer = &rule->layers[i]; +const u64 layer_level =BIT_ULL (layer->level -1 ); + +if ((layer->access & access_request) == access_request) { + layer_mask &= ~layer_level; + +if (layer_mask ==0 ) +return layer_mask; + } + } +return layer_mask; +} + +static int check_access_path (const struct landlock_ruleset *const domain, +const struct path *const path,u32 access_request) +{ +bool allowed = false; +struct path walker_path; +u64 layer_mask; + +/* Make sure all layers can be checked. */ +BUILD_BUG_ON (BITS_PER_TYPE (layer_mask) <LANDLOCK_MAX_NUM_LAYERS ); + +if (WARN_ON_ONCE (!domain || !path)) +return 0 ; +/* +* Allows access to pseudo filesystems that will never be mountable +* (e.g. sockfs, pipefs), but can still be reachable through +* /proc/self/fd . +*/ +if ((path->dentry->d_sb->s_flags &SB_NOUSER ) || + (d_is_positive (path->dentry) && +unlikely (IS_PRIVATE (d_backing_inode (path->dentry))))) +return 0 ; +if (WARN_ON_ONCE (domain->num_layers <1 )) +return -EACCES ; + + layer_mask =GENMASK_ULL (domain->num_layers -1 ,0 ); +/* +* An access request that is not handled by the domain should be +* allowed. +*/ + access_request &= domain->fs_access_mask; +if (access_request ==0 ) +return 0 ; + walker_path = *path; +path_get (&walker_path); +/* +* We need to walk through all the hierarchy to not miss any relevant +* restriction. +*/ +while (true) { +struct dentry *parent_dentry; + + layer_mask =unmask_layers (domain, &walker_path, + access_request, layer_mask); +if (layer_mask ==0 ) { +/* Stops when a rule from each layer grants access. */ + allowed = true; +break ; + } + +jump_up: +if (walker_path.dentry == walker_path.mnt->mnt_root) { +if (follow_up (&walker_path)) { +/* Ignores hidden mount points. */ + goto jump_up; + }else { +/* +* Stops at the real root. Denies access +* because not all layers have granted access. +*/ + allowed = false; +break ; + } + } +if (unlikely (IS_ROOT (walker_path.dentry))) { +/* +* Stops at disconnected root directories. Only allows +* access to internal filesystems (e.g. nsfs, which is +* reachable through /proc/self/ns). +*/ + allowed = !!(walker_path.mnt->mnt_flags &MNT_INTERNAL ); +break ; + } + parent_dentry =dget_parent (walker_path.dentry); +dput (walker_path.dentry); + walker_path.dentry = parent_dentry; + } +path_put (&walker_path); +return allowed ?0 : -EACCES ; +} + +static inline int current_check_access_path (const struct path *const path, +const u32 access_request) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom) +return 0 ; +return check_access_path (dom, path, access_request); +} + +/* Super-block hooks */ + +/* +* Release the inodes used in a security policy. +* +* Cf. fsnotify_unmount_inodes() +*/ +static void hook_sb_delete (struct super_block *const sb) +{ +struct inode *inode, *iput_inode =NULL ; + +if (!landlock_initialized) +return ; + +spin_lock (&sb->s_inode_list_lock); +list_for_each_entry (inode, &sb->s_inodes, i_sb_list) { +struct landlock_inode_security *inode_sec = +landlock_inode (inode); +struct landlock_object *object; +bool do_put = false; + +rcu_read_lock (); + object =rcu_dereference (inode_sec->object); +if (!object) { +rcu_read_unlock (); +continue ; + } + +spin_lock (&object->lock); +if (object->underobj) { + object->underobj =NULL ; + do_put = true; +spin_lock (&inode->i_lock); +rcu_assign_pointer (inode_sec->object,NULL ); +spin_unlock (&inode->i_lock); + } +spin_unlock (&object->lock); +rcu_read_unlock (); +if (!do_put) +/* +* A concurrent iput() in release_inode() is ongoing +* and we will just wait for it to finish. +*/ +continue ; + +/* +* At this point, we own the ihold() reference that was +* originally set up by get_inode_object(). Therefore we can +* drop the list lock and know that the inode won't disappear +* from under us until the next loop walk. +*/ +spin_unlock (&sb->s_inode_list_lock); +/* +* We can now actually put the previous inode, which is not +* needed anymore for the loop walk. +*/ +if (iput_inode) +iput (iput_inode); + iput_inode = inode; +spin_lock (&sb->s_inode_list_lock); + } +spin_unlock (&sb->s_inode_list_lock); +if (iput_inode) +iput (iput_inode); + +/* +* Wait for pending iput() in release_inode(). +*/ +wait_var_event (&landlock_superblock (sb)->inode_refs, !atomic_long_read ( + &landlock_superblock (sb)->inode_refs)); +} + +/* +* Because a Landlock security policy is defined according to the filesystem +* layout (i.e. the mount namespace), changing it may grant access to files not +* previously allowed. +* +* To make it simple, deny any filesystem layout modification by landlocked +* processes. Non-landlocked processes may still change the namespace of a +* landlocked process, but this kind of threat must be handled by a system-wide +* access-control security policy. +* +* This could be lifted in the future if Landlock can safely handle mount +* namespace updates requested by a landlocked process. Indeed, we could +* update the current domain (which is currently read-only) by taking into +* account the accesses of the source and the destination of a new mount point. +* However, it would also require to make all the child domains dynamically +* inherit these new constraints. Anyway, for backward compatibility reasons, +* a dedicated user space option would be required (e.g. as a ruleset command +* option). +*/ +static int hook_sb_mount (const char *const dev_name, +const struct path *const path,const char *const type, +const unsigned long flags,void *const data) +{ +if (!landlock_get_current_domain ()) +return 0 ; +return -EPERM ; +} + +static int hook_move_mount (const struct path *const from_path, +const struct path *const to_path) +{ +if (!landlock_get_current_domain ()) +return 0 ; +return -EPERM ; +} + +/* +* Removing a mount point may reveal a previously hidden file hierarchy, which +* may then grant access to files, which may have previously been forbidden. +*/ +static int hook_sb_umount (struct vfsmount *const mnt,const int flags) +{ +if (!landlock_get_current_domain ()) +return 0 ; +return -EPERM ; +} + +static int hook_sb_remount (struct super_block *const sb,void *const mnt_opts) +{ +if (!landlock_get_current_domain ()) +return 0 ; +return -EPERM ; +} + +/* +* pivot_root(2), like mount(2), changes the current mount namespace. It must +* then be forbidden for a landlocked process. +* +* However, chroot(2) may be allowed because it only changes the relative root +* directory of the current process. Moreover, it can be used to restrict the +* view of the filesystem. +*/ +static int hook_sb_pivotroot (const struct path *const old_path, +const struct path *const new_path) +{ +if (!landlock_get_current_domain ()) +return 0 ; +return -EPERM ; +} + +/* Path hooks */ + +static inline u32 get_mode_access (const umode_t mode) +{ +switch (mode &S_IFMT ) { +case S_IFLNK : +return LANDLOCK_ACCESS_FS_MAKE_SYM ; +case 0 : +/* A zero mode translates to S_IFREG. */ +case S_IFREG : +return LANDLOCK_ACCESS_FS_MAKE_REG ; +case S_IFDIR : +return LANDLOCK_ACCESS_FS_MAKE_DIR ; +case S_IFCHR : +return LANDLOCK_ACCESS_FS_MAKE_CHAR ; +case S_IFBLK : +return LANDLOCK_ACCESS_FS_MAKE_BLOCK ; +case S_IFIFO : +return LANDLOCK_ACCESS_FS_MAKE_FIFO ; +case S_IFSOCK : +return LANDLOCK_ACCESS_FS_MAKE_SOCK ; +default : +WARN_ON_ONCE (1 ); +return 0 ; + } +} + +/* +* Creating multiple links or renaming may lead to privilege escalations if not +* handled properly. Indeed, we must be sure that the source doesn't gain more +* privileges by being accessible from the destination. This is getting more +* complex when dealing with multiple layers. The whole picture can be seen as +* a multilayer partial ordering problem. A future version of Landlock will +* deal with that. +*/ +static int hook_path_link (struct dentry *const old_dentry, +const struct path *const new_dir, +struct dentry *const new_dentry) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom) +return 0 ; +/* The mount points are the same for old and new paths, cf. EXDEV. */ +if (old_dentry->d_parent != new_dir->dentry) +/* For now, forbid reparenting. */ +return -EACCES ; +if (unlikely (d_is_negative (old_dentry))) +return -EACCES ; +return check_access_path (dom, new_dir, +get_mode_access (d_backing_inode (old_dentry)->i_mode)); +} + +static inline u32 maybe_remove (const struct dentry *const dentry) +{ +if (d_is_negative (dentry)) +return 0 ; +return d_is_dir (dentry) ?LANDLOCK_ACCESS_FS_REMOVE_DIR : +LANDLOCK_ACCESS_FS_REMOVE_FILE ; +} + +static int hook_path_rename (const struct path *const old_dir, +struct dentry *const old_dentry, +const struct path *const new_dir, +struct dentry *const new_dentry) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom) +return 0 ; +/* The mount points are the same for old and new paths, cf. EXDEV. */ +if (old_dir->dentry != new_dir->dentry) +/* For now, forbid reparenting. */ +return -EACCES ; +if (WARN_ON_ONCE (d_is_negative (old_dentry))) +return -EACCES ; +/* RENAME_EXCHANGE is handled because directories are the same. */ +return check_access_path (dom, old_dir,maybe_remove (old_dentry) | +maybe_remove (new_dentry) | +get_mode_access (d_backing_inode (old_dentry)->i_mode)); +} + +static int hook_path_mkdir (const struct path *const dir, +struct dentry *const dentry,const umode_t mode) +{ +return current_check_access_path (dir,LANDLOCK_ACCESS_FS_MAKE_DIR ); +} + +static int hook_path_mknod (const struct path *const dir, +struct dentry *const dentry,const umode_t mode, +const unsigned int dev) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom) +return 0 ; +return check_access_path (dom, dir,get_mode_access (mode)); +} + +static int hook_path_symlink (const struct path *const dir, +struct dentry *const dentry,const char *const old_name) +{ +return current_check_access_path (dir,LANDLOCK_ACCESS_FS_MAKE_SYM ); +} + +static int hook_path_unlink (const struct path *const dir, +struct dentry *const dentry) +{ +return current_check_access_path (dir,LANDLOCK_ACCESS_FS_REMOVE_FILE ); +} + +static int hook_path_rmdir (const struct path *const dir, +struct dentry *const dentry) +{ +return current_check_access_path (dir,LANDLOCK_ACCESS_FS_REMOVE_DIR ); +} + +/* File hooks */ + +static inline u32 get_file_access (const struct file *const file) +{ +u32 access =0 ; + +if (file->f_mode &FMODE_READ ) { +/* A directory can only be opened in read mode. */ +if (S_ISDIR (file_inode (file)->i_mode)) +return LANDLOCK_ACCESS_FS_READ_DIR ; + access =LANDLOCK_ACCESS_FS_READ_FILE ; + } +if (file->f_mode &FMODE_WRITE ) + access |=LANDLOCK_ACCESS_FS_WRITE_FILE ; +/* __FMODE_EXEC is indeed part of f_flags, not f_mode. */ +if (file->f_flags & __FMODE_EXEC) + access |=LANDLOCK_ACCESS_FS_EXECUTE ; +return access; +} + +static int hook_file_open (struct file *const file) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom) +return 0 ; +/* +* Because a file may be opened with O_PATH, get_file_access() may +* return 0. This case will be handled with a future Landlock +* evolution. +*/ +return check_access_path (dom, &file->f_path,get_file_access (file)); +} + +static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { +LSM_HOOK_INIT (sb_delete, hook_sb_delete), +LSM_HOOK_INIT (sb_mount, hook_sb_mount), +LSM_HOOK_INIT (move_mount, hook_move_mount), +LSM_HOOK_INIT (sb_umount, hook_sb_umount), +LSM_HOOK_INIT (sb_remount, hook_sb_remount), +LSM_HOOK_INIT (sb_pivotroot, hook_sb_pivotroot), + +LSM_HOOK_INIT (path_link, hook_path_link), +LSM_HOOK_INIT (path_rename, hook_path_rename), +LSM_HOOK_INIT (path_mkdir, hook_path_mkdir), +LSM_HOOK_INIT (path_mknod, hook_path_mknod), +LSM_HOOK_INIT (path_symlink, hook_path_symlink), +LSM_HOOK_INIT (path_unlink, hook_path_unlink), +LSM_HOOK_INIT (path_rmdir, hook_path_rmdir), + +LSM_HOOK_INIT (file_open, hook_file_open), +}; + +__init void landlock_add_fs_hooks (void ) +{ +security_add_hooks (landlock_hooks,ARRAY_SIZE (landlock_hooks), +LANDLOCK_NAME ); +}security/landlock/fs.h new file +56
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Filesystem management and hooks +* +* Copyright © 2017-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +*/ + +#ifndef _SECURITY_LANDLOCK_FS_H +#define _SECURITY_LANDLOCK_FS_H + +#include <linux/fs.h> +#include <linux/init.h> +#include <linux/rcupdate.h> + +#include "ruleset.h" +#include "setup.h" + +struct landlock_inode_security { +/* +* @object: Weak pointer to an allocated object. All writes (i.e. +* creating a new object or removing one) are protected by the +* underlying inode->i_lock. Disassociating @object from the inode is +* additionally protected by @object->lock, from the time @object's +* usage refcount drops to zero to the time this pointer is nulled out. +* Cf. release_inode(). +*/ +struct landlock_object __rcu *object; +}; + +struct landlock_superblock_security { +/* +* @inode_refs: References to Landlock underlying objects. +* Cf. struct super_block->s_fsnotify_inode_refs . +*/ +atomic_long_t inode_refs; +}; + +static inline struct landlock_inode_security *landlock_inode ( +const struct inode *const inode) +{ +return inode->i_security + landlock_blob_sizes.lbs_inode; +} + +static inline struct landlock_superblock_security *landlock_superblock ( +const struct super_block *const superblock) +{ +return superblock->s_security + landlock_blob_sizes.lbs_superblock; +} + +__init void landlock_add_fs_hooks (void ); + +int landlock_append_fs_rule (struct landlock_ruleset *const ruleset, +const struct path *const path,u32 access_hierarchy); + +#endif /* _SECURITY_LANDLOCK_FS_H */ 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@@ -112,10 +112,12 @@ static void build_check_ruleset(void)const struct landlock_ruleset ruleset = { .num_rules = ~0 , .num_layers = ~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 (ruleset.fs_access_mask <LANDLOCK_MASK_ACCESS_FS ); }/** @@ -214,9 +216,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 ); } int landlock_insert_rule(struct landlock_ruleset *const ruleset,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 */
Mickaël Salaün
maintainerPATCH v27 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_enforce_ruleset_self(2): Enforces a ruleset on the current 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 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 | 429 ++++++++++++++++++++++++++++++++++ 5 files changed, 495 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_enforce_ruleset_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/syscall.c */ +COND_SYSCALL (landlock_create_ruleset); +COND_SYSCALL (landlock_add_rule); +COND_SYSCALL (landlock_enforce_ruleset_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 +429 -0
429 lines not on this page · raw message
Signature · 1 line
2.30.0
kernel test robot
build botRe: [PATCH v27 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 19c329f6808995b142b3966301f217c831e7cf31]
url: https://github.com/0day-ci/linux/commits/Micka-l-Sala-n/Landlock-LSM/20210122-050233 base: 19c329f6808995b142b3966301f217c831e7cf31 config: i386-randconfig-r025-20210121 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/686924e64fbb52511aeb64af1b811b93d9d0df8b git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Micka-l-Sala-n/Landlock-LSM/20210122-050233 git checkout 686924e64fbb52511aeb64af1b811b93d9d0df8b # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:249:1: note: in expansion of macro 'COND_SYSCALL'
249 | COND_SYSCALL(sendto);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_shutdown' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:250:1: note: in expansion of macro 'COND_SYSCALL'
250 | COND_SYSCALL(shutdown);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_recvfrom' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:251:1: note: in expansion of macro 'COND_SYSCALL'
251 | COND_SYSCALL(recvfrom);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_sendmsg' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:253:1: note: in expansion of macro 'COND_SYSCALL'
253 | COND_SYSCALL(sendmsg);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_recvmsg' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:255:1: note: in expansion of macro 'COND_SYSCALL'
255 | COND_SYSCALL(recvmsg);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_mremap' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:261:1: note: in expansion of macro 'COND_SYSCALL'
261 | COND_SYSCALL(mremap);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_add_key' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:264:1: note: in expansion of macro 'COND_SYSCALL'
264 | COND_SYSCALL(add_key);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_request_key' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:265:1: note: in expansion of macro 'COND_SYSCALL'
265 | COND_SYSCALL(request_key);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_keyctl' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:266:1: note: in expansion of macro 'COND_SYSCALL'
266 | COND_SYSCALL(keyctl);
| ^~~~~~~~~~~~
>> arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_landlock_create_ruleset' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:270:1: note: in expansion of macro 'COND_SYSCALL'
270 | COND_SYSCALL(landlock_create_ruleset);
| ^~~~~~~~~~~~
>> arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_landlock_add_rule' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:271:1: note: in expansion of macro 'COND_SYSCALL'
271 | COND_SYSCALL(landlock_add_rule);
| ^~~~~~~~~~~~
>> arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_landlock_enforce_ruleset_self' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:272:1: note: in expansion of macro 'COND_SYSCALL'
272 | COND_SYSCALL(landlock_enforce_ruleset_self);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_fadvise64_64' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:277:1: note: in expansion of macro 'COND_SYSCALL'
277 | COND_SYSCALL(fadvise64_64);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_swapon' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:280:1: note: in expansion of macro 'COND_SYSCALL'
280 | COND_SYSCALL(swapon);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_swapoff' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:281:1: note: in expansion of macro 'COND_SYSCALL'
281 | COND_SYSCALL(swapoff);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_mprotect' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:282:1: note: in expansion of macro 'COND_SYSCALL'
282 | COND_SYSCALL(mprotect);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_msync' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:283:1: note: in expansion of macro 'COND_SYSCALL'
283 | COND_SYSCALL(msync);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_mlock' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:284:1: note: in expansion of macro 'COND_SYSCALL'
284 | COND_SYSCALL(mlock);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_munlock' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)
| ^~~~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:257:2: note: in expansion of macro '__IA32_COND_SYSCALL'
257 | __IA32_COND_SYSCALL(name)
| ^~~~~~~~~~~~~~~~~~~
kernel/sys_ni.c:285:1: note: in expansion of macro 'COND_SYSCALL'
285 | COND_SYSCALL(munlock);
| ^~~~~~~~~~~~
arch/x86/include/asm/syscall_wrapper.h:83:14: warning: no previous prototype for '__ia32_sys_mlockall' [-Wmissing-prototypes]
83 | __weak long __##abi##_##name(const struct pt_regs *__unused) \
| ^~
arch/x86/include/asm/syscall_wrapper.h:120:2: note: in expansion of macro '__COND_SYSCALL'
120 | __COND_SYSCALL(ia32, sys_##name)vim +/__ia32_sys_landlock_create_ruleset +83 arch/x86/include/asm/syscall_wrapper.h
cc42c045af1ff4de Brian Gerst 2020-03-13 13
25c619e59b395a8c Brian Gerst 2020-03-13 14 /*
25c619e59b395a8c Brian Gerst 2020-03-13 15 * Instead of the generic __SYSCALL_DEFINEx() definition, the x86 version takes
25c619e59b395a8c Brian Gerst 2020-03-13 16 * struct pt_regs *regs as the only argument of the syscall stub(s) named as:
25c619e59b395a8c Brian Gerst 2020-03-13 17 * __x64_sys_*() - 64-bit native syscall
25c619e59b395a8c Brian Gerst 2020-03-13 18 * __ia32_sys_*() - 32-bit native syscall or common compat syscall
25c619e59b395a8c Brian Gerst 2020-03-13 19 * __ia32_compat_sys_*() - 32-bit compat syscall
25c619e59b395a8c Brian Gerst 2020-03-13 20 * __x32_compat_sys_*() - 64-bit X32 compat syscall
25c619e59b395a8c Brian Gerst 2020-03-13 21 *
25c619e59b395a8c Brian Gerst 2020-03-13 22 * The registers are decoded according to the ABI:
25c619e59b395a8c Brian Gerst 2020-03-13 23 * 64-bit: RDI, RSI, RDX, R10, R8, R9
25c619e59b395a8c Brian Gerst 2020-03-13 24 * 32-bit: EBX, ECX, EDX, ESI, EDI, EBP
25c619e59b395a8c Brian Gerst 2020-03-13 25 *
25c619e59b395a8c Brian Gerst 2020-03-13 26 * The stub then passes the decoded arguments to the __se_sys_*() wrapper to
25c619e59b395a8c Brian Gerst 2020-03-13 27 * perform sign-extension (omitted for zero-argument syscalls). Finally the
25c619e59b395a8c Brian Gerst 2020-03-13 28 * arguments are passed to the __do_sys_*() function which is the actual
25c619e59b395a8c Brian Gerst 2020-03-13 29 * syscall. These wrappers are marked as inline so the compiler can optimize
25c619e59b395a8c Brian Gerst 2020-03-13 30 * the functions where appropriate.
25c619e59b395a8c Brian Gerst 2020-03-13 31 *
25c619e59b395a8c Brian Gerst 2020-03-13 32 * Example assembly (slightly re-ordered for better readability):
25c619e59b395a8c Brian Gerst 2020-03-13 33 *
25c619e59b395a8c Brian Gerst 2020-03-13 34 * <__x64_sys_recv>: <-- syscall with 4 parameters
25c619e59b395a8c Brian Gerst 2020-03-13 35 * callq <__fentry__>
25c619e59b395a8c Brian Gerst 2020-03-13 36 *
25c619e59b395a8c Brian Gerst 2020-03-13 37 * mov 0x70(%rdi),%rdi <-- decode regs->di
25c619e59b395a8c Brian Gerst 2020-03-13 38 * mov 0x68(%rdi),%rsi <-- decode regs->si
25c619e59b395a8c Brian Gerst 2020-03-13 39 * mov 0x60(%rdi),%rdx <-- decode regs->dx
25c619e59b395a8c Brian Gerst 2020-03-13 40 * mov 0x38(%rdi),%rcx <-- decode regs->r10
25c619e59b395a8c Brian Gerst 2020-03-13 41 *
25c619e59b395a8c Brian Gerst 2020-03-13 42 * xor %r9d,%r9d <-- clear %r9
25c619e59b395a8c Brian Gerst 2020-03-13 43 * xor %r8d,%r8d <-- clear %r8
25c619e59b395a8c Brian Gerst 2020-03-13 44 *
25c619e59b395a8c Brian Gerst 2020-03-13 45 * callq __sys_recvfrom <-- do the actual work in __sys_recvfrom()
25c619e59b395a8c Brian Gerst 2020-03-13 46 * which takes 6 arguments
25c619e59b395a8c Brian Gerst 2020-03-13 47 *
25c619e59b395a8c Brian Gerst 2020-03-13 48 * cltq <-- extend return value to 64-bit
25c619e59b395a8c Brian Gerst 2020-03-13 49 * retq <-- return
25c619e59b395a8c Brian Gerst 2020-03-13 50 *
25c619e59b395a8c Brian Gerst 2020-03-13 51 * This approach avoids leaking random user-provided register content down
25c619e59b395a8c Brian Gerst 2020-03-13 52 * the call chain.
25c619e59b395a8c Brian Gerst 2020-03-13 53 */
25c619e59b395a8c Brian Gerst 2020-03-13 54
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 55 /* Mapping of registers to parameters for syscalls on x86-64 and x32 */
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 56 #define SC_X86_64_REGS_TO_ARGS(x, ...) \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 57 __MAP(x,__SC_ARGS \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 58 ,,regs->di,,regs->si,,regs->dx \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 59 ,,regs->r10,,regs->r8,,regs->r9) \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 60
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 61 /* Mapping of registers to parameters for syscalls on i386 */
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 62 #define SC_IA32_REGS_TO_ARGS(x, ...) \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 63 __MAP(x,__SC_ARGS \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 64 ,,(unsigned int)regs->bx,,(unsigned int)regs->cx \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 65 ,,(unsigned int)regs->dx,,(unsigned int)regs->si \
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 66 ,,(unsigned int)regs->di,,(unsigned int)regs->bp)
ebeb8c82ffaf9443 Dominik Brodowski 2018-04-05 67
d2b5de495ee9838b Brian Gerst 2020-03-13 68 #define __SYS_STUB0(abi, name) \
0f78ff17112d8b34 Brian Gerst 2020-03-13 69 long __##abi##_##name(const struct pt_regs *regs); \
d2b5de495ee9838b Brian Gerst 2020-03-13 70 ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO); \
0f78ff17112d8b34 Brian Gerst 2020-03-13 71 long __##abi##_##name(const struct pt_regs *regs) \
d2b5de495ee9838b Brian Gerst 2020-03-13 72 __alias(__do_##name);
d2b5de495ee9838b Brian Gerst 2020-03-13 73
4399e0cf494f739a Brian Gerst 2020-03-13 74 #define __SYS_STUBx(abi, name, ...) \
0f78ff17112d8b34 Brian Gerst 2020-03-13 75 long __##abi##_##name(const struct pt_regs *regs); \
4399e0cf494f739a Brian Gerst 2020-03-13 76 ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO); \
0f78ff17112d8b34 Brian Gerst 2020-03-13 77 long __##abi##_##name(const struct pt_regs *regs) \
4399e0cf494f739a Brian Gerst 2020-03-13 78 { \
4399e0cf494f739a Brian Gerst 2020-03-13 79 return __se_##name(__VA_ARGS__); \
4399e0cf494f739a Brian Gerst 2020-03-13 80 }
4399e0cf494f739a Brian Gerst 2020-03-13 81
6cc8d2b286d9e716 Brian Gerst 2020-03-13 82 #define __COND_SYSCALL(abi, name) \
0f78ff17112d8b34 Brian Gerst 2020-03-13 @83 __weak long __##abi##_##name(const struct pt_regs *__unused) \
6cc8d2b286d9e716 Brian Gerst 2020-03-13 84 { \
6cc8d2b286d9e716 Brian Gerst 2020-03-13 85 return sys_ni_syscall(); \
6cc8d2b286d9e716 Brian Gerst 2020-03-13 86 }
6cc8d2b286d9e716 Brian Gerst 2020-03-13 87 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH v27 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 19c329f6808995b142b3966301f217c831e7cf31]
url: https://github.com/0day-ci/linux/commits/Micka-l-Sala-n/Landlock-LSM/20210122-050233 base: 19c329f6808995b142b3966301f217c831e7cf31 config: arm64-randconfig-r014-20210121 (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project bd3a387ee76f58caa0d7901f3f84e9bb3d006f27)
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/686924e64fbb52511aeb64af1b811b93d9d0df8b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Micka-l-Sala-n/Landlock-LSM/20210122-050233
git checkout 686924e64fbb52511aeb64af1b811b93d9d0df8b
# 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>:136:1: note: expanded from here
__arm64_compat_sys_sendmsg
^
kernel/sys_ni.c:254: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:255:1: warning: no previous prototype for function '__arm64_sys_recvmsg' [-Wmissing-prototypes]
COND_SYSCALL(recvmsg);
^
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>:137:1: note: expanded from here
__arm64_sys_recvmsg
^
kernel/sys_ni.c:255: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:256:1: warning: no previous prototype for function '__arm64_compat_sys_recvmsg' [-Wmissing-prototypes]
COND_SYSCALL_COMPAT(recvmsg);
^
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>: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_enforce_ruleset_self' [-Wmissing-prototypes]
COND_SYSCALL(landlock_enforce_ruleset_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_enforce_ruleset_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_create_ruleset +270 kernel/sys_ni.c
268
269 /* security/landlock/syscall.c */
> 270 COND_SYSCALL(landlock_create_ruleset); > 271 COND_SYSCALL(landlock_add_rule); > 272 COND_SYSCALL(landlock_enforce_ruleset_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
maintainerRe: [PATCH v27 08/12] landlock: Add syscall implementations
In reply to Mickaël SalaünAfter further though, I think that the third syscall currently called "landlock_enforce_ruleset_self" may be better named "landlock_restrict_self". I want to keep the "self" part because, as explained in the changelog and previous discussions, we may want to be able to restrict other processes (e.g. with pidfd or cgroup) in the future with dedicated syscalls. "landlock_restrict_self" is shorter and follows the same semantic as the two other syscalls (i.e. verb + direct object). If needed, a future version of this syscall could extend "self" to the process (instead of the current thread) simply with a flag. What do you think of this renaming?
Quoted 731 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_enforce_ruleset_self(2): Enforces a ruleset on the current 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.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.rstCc Arnd Bergmann <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Jann Horn <[email protected]>---
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 | 429 ++++++++++++++++++++++++++++++++++ 5 files changed, 495 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_enforce_ruleset_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/syscall.c */ +COND_SYSCALL (landlock_create_ruleset); +COND_SYSCALL (landlock_add_rule); +COND_SYSCALL (landlock_enforce_ruleset_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 +429
-0@@ -0,0 +1,429 @@ +// 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; +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); +* - 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 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); + +/* +* Checks that allowed_access matches the @ruleset constraints +* (ruleset->fs_access_mask is automatically upgraded to 64-bits). +* Allows empty allowed_access i.e., deny @ruleset->fs_access_mask . +*/ +if ((path_beneath_attr.allowed_access | ruleset->fs_access_mask) != + ruleset->fs_access_mask) { + 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_enforce_ruleset_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_enforce_ruleset_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; +}
Mickaël Salaün
maintainerPATCH v27 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_enforce_ruleset_self(2)
---
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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self 446 +__SYSCALL (__NR_landlock_enforce_ruleset_self, sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self sys_landlock_enforce_ruleset_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_enforce_ruleset_self 446 +__SYSCALL(__NR_landlock_enforce_ruleset_self, sys_landlock_enforce_ruleset_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 v27 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.1% of lines. The code not covered only deals with internal kernel errors (e.g. memory allocation) and race conditions.
---
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 layou 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]/
--- 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 | 166 ++ tools/testing/selftests/landlock/config | 6 + tools/testing/selftests/landlock/fs_test.c | 2585 +++++++++++++++++ .../testing/selftests/landlock/ptrace_test.c | 314 ++ tools/testing/selftests/landlock/true.c | 5 + 9 files changed, 3322 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
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 +166 -0
166 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 +2585 -0
2,576 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 v27 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 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]/
--- samples/Kconfig | 7 + samples/Makefile | 1 + samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 13 ++ samples/landlock/sandboxer.c | 239 +++++++++++++++++++++++++++++++++++ 5 files changed, 261 insertions(+) create mode 100644 samples/landlock/.gitignore create mode 100644 samples/landlock/Makefile create mode 100644 samples/landlock/sandboxer.c
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 sample code" + depends on CC_CAN_LINK && HEADERS_INSTALL + help + Build a simple Landlock sandbox manager able to launch 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 +239 -0
239 lines not on this page · raw message
Signature · 1 line
2.30.0
Mickaël Salaün
maintainerPATCH v27 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 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 | 306 +++++++++++++++++++++++ MAINTAINERS | 2 + 5 files changed, 389 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 +306 -0
306 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: security/landlock/ K: landlock
Signature · 1 line
2.30.0
Re: [PATCH v27 12/12] landlock: Add user and kernel documentation
Hello Mickaël,
It would be great to have some manual pages for these system calls before release... Can you prepare something?
Thanks,
Michael
Quoted 514 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
This documentation can be built with the Sphinx framework.
Cc James Morris <[email protected]>Cc Jann Horn <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Vincent Dagonneau <[email protected]>---
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 newsyscalls.cfile 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 touserspace-api/landlock.rstand the kernel documentation tosecurity/landlock.rst.
* Add license headers.
* Add last update dates.
* UpdateMAINTAINERSfile.
* 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 | 306 +++++++++++++++++++++++ MAINTAINERS | 2 + 5 files changed, 389 insertions(+) create mode 100644 Documentation/security/landlock.rst create mode 100644 Documentation/userspace-api/landlock.rstDocumentation/security/index.rst +1
-0@@ -16,3 +16,4 @@ Security Documentation siphash tpm/index digsig + landlockDocumentation/security/landlock.rst new file +79
-0@@ -0,0 +1,79 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. Copyright © 2017-2020 Mickaël Salaün <[email protected]> +.. Copyright © 2019-2020 ANSSI + +================================== +Landlock LSM: kernel documentation +================================== + +:Author: Mickaël Salaün +:Date: January 2021 + +Landlock's goal is to create scoped access-control (i.e. sandboxing). To +harden a whole system, this feature should be available to any process, +including unprivileged ones. Because such process may be compromised or +backdoored (i.e. untrusted), Landlock's features must be safe to use from the +kernel and other processes point of view. Landlock's interface must therefore +expose a minimal attack surface. + +Landlock is designed to be usable by unprivileged processes while following the +system security policy enforced by other access control mechanisms (e.g. DAC, +LSM). Indeed, a Landlock rule shall not interfere with other access-controls +enforced on the system, only add more restrictions. + +Any user can enforce Landlock rulesets on their processes. They are merged and +evaluated according to the inherited ones in a way that ensures that only more +constraints can be added. + +User space documentation can be found here: :doc:`/userspace-api/landlock`. + +Guiding principles for safe access controls +=========================================== + +* A Landlock rule shall be focused on access control on kernel objects instead + of syscall filtering (i.e. syscall arguments), which is the purpose of + seccomp-bpf. +* To avoid multiple kinds of side-channel attacks (e.g. leak of security + policies, CPU-based attacks), Landlock rules shall not be able to + programmatically communicate with user space. +* Kernel access check shall not slow down access request from unsandboxed + processes. +* Computation related to Landlock operations (e.g. enforcing a ruleset) shall + only impact the processes requesting them. + +Tests +===== + +Userspace tests for backward compatibility, ptrace restrictions and filesystem +support can be found here: `tools/testing/selftests/landlock/`_. + +Kernel structures +================= + +Object +------ + +.. kernel-doc:: security/landlock/object.h + :identifiers: + +Ruleset and domain +------------------ + +A domain is a read-only ruleset tied to a set of subjects (i.e. tasks' +credentials). Each time a ruleset is enforced on a task, the current domain is +duplicated and the ruleset is imported as a new layer of rules in the new +domain. Indeed, once in a domain, each rule is tied to a layer level. To +grant access to an object, at least one rule of each layer must allow the +requested action on the object. A task can then only transit to a new domain +that is the intersection of the constraints from the current domain and those +of a ruleset provided by the task. + +The definition of a subject is implicit for a task sandboxing itself, which +makes the reasoning much easier and helps avoid pitfalls. + +.. kernel-doc:: security/landlock/ruleset.h + :identifiers: + +.. Links +.. _tools/testing/selftests/landlock/: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/landlock/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/ocxlDocumentation/userspace-api/landlock.rst new file +306
-0@@ -0,0 +1,306 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. Copyright © 2017-2020 Mickaël Salaün <[email protected]> +.. Copyright © 2019-2020 ANSSI + +===================================== +Landlock: unprivileged access control +===================================== + +:Author: Mickaël Salaün +:Date: January 2021 + +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, 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 rules +============== + +A Landlock rule describes an action on an object. An object is currently a +file hierarchy, and the related filesystem actions are defined in `Access +rights`_. A set of rules is aggregated in a ruleset, which can then restrict +the thread enforcing it, and its future children. + +Defining and enforcing a security policy +---------------------------------------- + +We first need to create the ruleset that will contain our rules. For this +example, the ruleset will contain rules that only allow read actions, but write +actions will be denied. The ruleset then needs to handle both of these kind of +actions. + +.. code-block:: c + + int ruleset_fd; + struct landlock_ruleset_attr ruleset_attr = { + .handled_access_fs = + LANDLOCK_ACCESS_FS_EXECUTE | + LANDLOCK_ACCESS_FS_WRITE_FILE | + LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_READ_DIR | + LANDLOCK_ACCESS_FS_REMOVE_DIR | + LANDLOCK_ACCESS_FS_REMOVE_FILE | + LANDLOCK_ACCESS_FS_MAKE_CHAR | + LANDLOCK_ACCESS_FS_MAKE_DIR | + LANDLOCK_ACCESS_FS_MAKE_REG | + LANDLOCK_ACCESS_FS_MAKE_SOCK | + LANDLOCK_ACCESS_FS_MAKE_FIFO | + LANDLOCK_ACCESS_FS_MAKE_BLOCK | + LANDLOCK_ACCESS_FS_MAKE_SYM, + }; + + ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); + if (ruleset_fd < 0) { + perror("Failed to create a ruleset"); + return 1; + } + +We can now add a new rule to this ruleset thanks to the returned file +descriptor referring to this ruleset. The rule will only allow reading the +file hierarchy ``/usr``. Without another rule, write actions would then be +denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the +``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with this file +descriptor. + +.. code-block:: c + + int err; + struct landlock_path_beneath_attr path_beneath = { + .allowed_access = + LANDLOCK_ACCESS_FS_EXECUTE | + LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_READ_DIR, + }; + + path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC); + if (path_beneath.parent_fd < 0) { + perror("Failed to open file"); + close(ruleset_fd); + return 1; + } + err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, + &path_beneath, 0); + close(path_beneath.parent_fd); + if (err) { + perror("Failed to update ruleset"); + close(ruleset_fd); + return 1; + } + +We now have a ruleset with one rule allowing read access to ``/usr`` while +denying all other handled accesses for the filesystem. The next step is to +restrict the current thread from gaining more privileges (e.g. thanks to a SUID +binary). + +.. code-block:: c + + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { + perror("Failed to restrict privileges"); + close(ruleset_fd); + return 1; + } + +The current thread is now ready to sandbox itself with the ruleset. + +.. code-block:: c + + if (landlock_enforce_ruleset_self(ruleset_fd, 0)) { + perror("Failed to enforce ruleset"); + close(ruleset_fd); + return 1; + } + close(ruleset_fd); + +If the `landlock_enforce_ruleset_self` system call succeeds, the current thread +is now restricted and this policy will be enforced on all its subsequently +created children as well. Once a thread is landlocked, there is no way to +remove its security policy; only adding more restrictions is allowed. These +threads are now in a new Landlock domain, merge of their parent one (if any) +with the new ruleset. + +Full working code can be found in `samples/landlock/sandboxer.c`_. + +Layers of file path access rights +--------------------------------- + +Each time a thread enforces a ruleset on itself, it updates its Landlock domain +with a new layer of policy. Indeed, this complementary policy is stacked with +the potentially other rulesets already restricting this thread. A sandboxed +thread can then safely add more constraints to itself with a new enforced +ruleset. + +One policy layer grants access to a file path if at least one of its rules +encountered on the path grants the access. A sandboxed thread can only access +a file path if all its enforced policy layers grant the access as well as all +the other system access controls (e.g. filesystem DAC, other LSM policies, +etc.). + +Bind mounts and OverlayFS +------------------------- + +Landlock enables to restrict access to file hierarchies, which means that these +access rights can be propagated with bind mounts (cf. +:doc:`/filesystems/sharedsubtree`) but not with :doc:`/filesystems/overlayfs`. + +A bind mount mirrors a source file hierarchy to a destination. The destination +hierarchy is then composed of the exact same files, on which Landlock rules can +be tied, either via the source or the destination path. These rules restrict +access when they are encountered on a path, which means that they can restrict +access to multiple file hierarchies at the same time, whether these hierarchies +are the result of bind mounts or not. + +An OverlayFS mount point consists of upper and lower layers. These layers are +combined in a merge directory, result of the mount point. This merge hierarchy +may include files from the upper and lower layers, but modifications performed +on the merge hierarchy only reflects on the upper layer. From a Landlock +policy point of view, each OverlayFS layers and merge hierarchies are +standalone and contains their own set of files and directories, which is +different from bind mounts. A policy restricting an OverlayFS layer will not +restrict the resulted merged hierarchy, and vice versa. + +Inheritance +----------- + +Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain +restrictions from its parent. This is similar to the seccomp inheritance (cf. +:doc:`/userspace-api/seccomp_filter`) or any other LSM dealing with task's +:manpage:`credentials(7)`. For instance, one process's thread may apply +Landlock rules to itself, but they will not be automatically applied to other +sibling threads (unlike POSIX thread credential changes, cf. +:manpage:`nptl(7)`). + +When a thread sandboxes itself, we have the guarantee that the related security +policy will stay enforced on all this thread's descendants. This allows +creating standalone and modular security policies per application, which will +automatically be composed between themselves according to their runtime parent +policies. + +Ptrace restrictions +------------------- + +A sandboxed process has less privileges than a non-sandboxed process and must +then be subject to additional restrictions when manipulating another process. +To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target +process, a sandboxed process should have a subset of the target process rules, +which means the tracee must be in a sub-domain of the tracer. + +Kernel interface +================ + +Access rights +------------- + +.. kernel-doc:: include/uapi/linux/landlock.h + :identifiers: fs_access + +Creating a new ruleset +---------------------- + +.. kernel-doc:: security/landlock/syscalls.c + :identifiers: sys_landlock_create_ruleset + +.. kernel-doc:: include/uapi/linux/landlock.h + :identifiers: landlock_ruleset_attr + +Extending a ruleset +------------------- + +.. kernel-doc:: security/landlock/syscalls.c + :identifiers: sys_landlock_add_rule + +.. kernel-doc:: include/uapi/linux/landlock.h + :identifiers: landlock_rule_type landlock_path_beneath_attr + +Enforcing a ruleset +------------------- + +.. kernel-doc:: security/landlock/syscalls.c + :identifiers: sys_landlock_enforce_ruleset_self + +Current limitations +=================== + +Ruleset layers +-------------- + +There is a limit of 64 layers of stacked rulesets. This can be an issue for a +task willing to enforce a new ruleset in complement to its 64 inherited +rulesets. Once this limit is reached, sys_landlock_enforce_ruleset_self() +returns E2BIG. It is then strongly suggested to carefully build rulesets once +in the life of a thread, especially for applications able to launch other +applications that may also want to sandbox themselves (e.g. shells, container +managers, etc.). + +Memory usage +------------ + +Kernel memory allocated to create rulesets is accounted and can be restricted +by the :doc:`/admin-guide/cgroup-v1/memory`. + +File renaming and linking +------------------------- + +Because Landlock targets unprivileged access controls, it is needed to properly +handle composition of rules. Such property also implies rules nesting. +Properly handling multiple layers of ruleset, each one of them able to restrict +access to files, also implies to inherit the ruleset restrictions from a parent +to its hierarchy. Because files are identified and restricted by their +hierarchy, moving or linking a file from one directory to another implies to +propagate the hierarchy constraints. To protect against privilege escalations +through renaming or linking, and for the sack of simplicity, Landlock currently +limits linking and renaming to the same directory. Future Landlock evolutions +will enable more flexibility for renaming and linking, with dedicated ruleset +flags. + +Filesystem layout modification +------------------------------ + +As for file renaming and linking, a sandboxed thread cannot modify its +filesystem layout, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`. +However, :manpage:`chroot(2)` calls are not denied. + +Special filesystems +------------------- + +Access to regular files and directories can be restricted by Landlock, +according to the handled accesses of a ruleset. However, files that do not +come from a user-visible filesystem (e.g. pipe, socket), but can still be +accessed through /proc/self/fd/, cannot currently be restricted. Likewise, +some special kernel filesystems such as nsfs, which can be accessed through +/proc/self/ns/, cannot currently be restricted. For now, these kind of special +paths are then always allowed. Future Landlock evolutions will enable to +restrict such paths with dedicated ruleset flags. + +Questions and answers +===================== + +What about user space sandbox managers? +--------------------------------------- + +Using user space process to enforce restrictions on kernel resources can lead +to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of +the OS code and state +<https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_). + +What about namespaces and containers? +------------------------------------- + +Namespaces can help create sandboxes but they are not designed for +access-control and then miss useful features for such use case (e.g. no +fine-grained restrictions). Moreover, their complexity can lead to security +issues, especially when untrusted processes can manipulate them (cf. +`Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_). + +Additional documentation +======================== + +* :doc:`/security/landlock` +* https://landlock.io + +.. Links +.. _samples/landlock/sandboxer.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.cMAINTAINERS +2
-1@@ -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: security/landlock/ K: landlock --2.30.0
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Mickaël Salaün
maintainerRe: [PATCH v27 12/12] landlock: Add user and kernel documentation
Quoted 1 line · Michael Kerrisk (man-pages)
Hello Mickaël,
Hi Michael,
Quoted 3 lines
It would be great to have some manual pages for these system calls before release... Can you prepare something?
Yes, I will start some pages based on the current documentation, but it will be some time before seeing Landlock in a (stable) release.
Could you please take a look at the UAPI (patch 8/12)?
Quoted 522 lines
Thanks,
Michael
Quoted 514 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
This documentation can be built with the Sphinx framework.
Cc James Morris <[email protected]>Cc Jann Horn <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Vincent Dagonneau <[email protected]>---
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 newsyscalls.cfile 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 touserspace-api/landlock.rstand the kernel documentation tosecurity/landlock.rst.
* Add license headers.
* Add last update dates.
* UpdateMAINTAINERSfile.
* 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 | 306 +++++++++++++++++++++++ MAINTAINERS | 2 + 5 files changed, 389 insertions(+) create mode 100644 Documentation/security/landlock.rst create mode 100644 Documentation/userspace-api/landlock.rstDocumentation/security/index.rst +1
-0@@ -16,3 +16,4 @@ Security Documentation siphash tpm/index digsig + landlockDocumentation/security/landlock.rst new file +79
-0@@ -0,0 +1,79 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. Copyright © 2017-2020 Mickaël Salaün <[email protected]> +.. Copyright © 2019-2020 ANSSI + +================================== +Landlock LSM: kernel documentation +================================== + +:Author: Mickaël Salaün +:Date: January 2021 + +Landlock's goal is to create scoped access-control (i.e. sandboxing). To +harden a whole system, this feature should be available to any process, +including unprivileged ones. Because such process may be compromised or +backdoored (i.e. untrusted), Landlock's features must be safe to use from the +kernel and other processes point of view. Landlock's interface must therefore +expose a minimal attack surface. + +Landlock is designed to be usable by unprivileged processes while following the +system security policy enforced by other access control mechanisms (e.g. DAC, +LSM). Indeed, a Landlock rule shall not interfere with other access-controls +enforced on the system, only add more restrictions. + +Any user can enforce Landlock rulesets on their processes. They are merged and +evaluated according to the inherited ones in a way that ensures that only more +constraints can be added. + +User space documentation can be found here: :doc:`/userspace-api/landlock`. + +Guiding principles for safe access controls +=========================================== + +* A Landlock rule shall be focused on access control on kernel objects instead + of syscall filtering (i.e. syscall arguments), which is the purpose of + seccomp-bpf. +* To avoid multiple kinds of side-channel attacks (e.g. leak of security + policies, CPU-based attacks), Landlock rules shall not be able to + programmatically communicate with user space. +* Kernel access check shall not slow down access request from unsandboxed + processes. +* Computation related to Landlock operations (e.g. enforcing a ruleset) shall + only impact the processes requesting them. + +Tests +===== + +Userspace tests for backward compatibility, ptrace restrictions and filesystem +support can be found here: `tools/testing/selftests/landlock/`_. + +Kernel structures +================= + +Object +------ + +.. kernel-doc:: security/landlock/object.h + :identifiers: + +Ruleset and domain +------------------ + +A domain is a read-only ruleset tied to a set of subjects (i.e. tasks' +credentials). Each time a ruleset is enforced on a task, the current domain is +duplicated and the ruleset is imported as a new layer of rules in the new +domain. Indeed, once in a domain, each rule is tied to a layer level. To +grant access to an object, at least one rule of each layer must allow the +requested action on the object. A task can then only transit to a new domain +that is the intersection of the constraints from the current domain and those +of a ruleset provided by the task. + +The definition of a subject is implicit for a task sandboxing itself, which +makes the reasoning much easier and helps avoid pitfalls. + +.. kernel-doc:: security/landlock/ruleset.h + :identifiers: + +.. Links +.. _tools/testing/selftests/landlock/: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/landlock/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/ocxlDocumentation/userspace-api/landlock.rst new file +306
-0@@ -0,0 +1,306 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. Copyright © 2017-2020 Mickaël Salaün <[email protected]> +.. Copyright © 2019-2020 ANSSI + +===================================== +Landlock: unprivileged access control +===================================== + +:Author: Mickaël Salaün +:Date: January 2021 + +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, 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 rules +============== + +A Landlock rule describes an action on an object. An object is currently a +file hierarchy, and the related filesystem actions are defined in `Access +rights`_. A set of rules is aggregated in a ruleset, which can then restrict +the thread enforcing it, and its future children. + +Defining and enforcing a security policy +---------------------------------------- + +We first need to create the ruleset that will contain our rules. For this +example, the ruleset will contain rules that only allow read actions, but write +actions will be denied. The ruleset then needs to handle both of these kind of +actions. + +.. code-block:: c + + int ruleset_fd; + struct landlock_ruleset_attr ruleset_attr = { + .handled_access_fs = + LANDLOCK_ACCESS_FS_EXECUTE | + LANDLOCK_ACCESS_FS_WRITE_FILE | + LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_READ_DIR | + LANDLOCK_ACCESS_FS_REMOVE_DIR | + LANDLOCK_ACCESS_FS_REMOVE_FILE | + LANDLOCK_ACCESS_FS_MAKE_CHAR | + LANDLOCK_ACCESS_FS_MAKE_DIR | + LANDLOCK_ACCESS_FS_MAKE_REG | + LANDLOCK_ACCESS_FS_MAKE_SOCK | + LANDLOCK_ACCESS_FS_MAKE_FIFO | + LANDLOCK_ACCESS_FS_MAKE_BLOCK | + LANDLOCK_ACCESS_FS_MAKE_SYM, + }; + + ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); + if (ruleset_fd < 0) { + perror("Failed to create a ruleset"); + return 1; + } + +We can now add a new rule to this ruleset thanks to the returned file +descriptor referring to this ruleset. The rule will only allow reading the +file hierarchy ``/usr``. Without another rule, write actions would then be +denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the +``O_PATH`` flag and fill the &struct landlock_path_beneath_attr with this file +descriptor. + +.. code-block:: c + + int err; + struct landlock_path_beneath_attr path_beneath = { + .allowed_access = + LANDLOCK_ACCESS_FS_EXECUTE | + LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_READ_DIR, + }; + + path_beneath.parent_fd = open("/usr", O_PATH | O_CLOEXEC); + if (path_beneath.parent_fd < 0) { + perror("Failed to open file"); + close(ruleset_fd); + return 1; + } + err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, + &path_beneath, 0); + close(path_beneath.parent_fd); + if (err) { + perror("Failed to update ruleset"); + close(ruleset_fd); + return 1; + } + +We now have a ruleset with one rule allowing read access to ``/usr`` while +denying all other handled accesses for the filesystem. The next step is to +restrict the current thread from gaining more privileges (e.g. thanks to a SUID +binary). + +.. code-block:: c + + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { + perror("Failed to restrict privileges"); + close(ruleset_fd); + return 1; + } + +The current thread is now ready to sandbox itself with the ruleset. + +.. code-block:: c + + if (landlock_enforce_ruleset_self(ruleset_fd, 0)) { + perror("Failed to enforce ruleset"); + close(ruleset_fd); + return 1; + } + close(ruleset_fd); + +If the `landlock_enforce_ruleset_self` system call succeeds, the current thread +is now restricted and this policy will be enforced on all its subsequently +created children as well. Once a thread is landlocked, there is no way to +remove its security policy; only adding more restrictions is allowed. These +threads are now in a new Landlock domain, merge of their parent one (if any) +with the new ruleset. + +Full working code can be found in `samples/landlock/sandboxer.c`_. + +Layers of file path access rights +--------------------------------- + +Each time a thread enforces a ruleset on itself, it updates its Landlock domain +with a new layer of policy. Indeed, this complementary policy is stacked with +the potentially other rulesets already restricting this thread. A sandboxed +thread can then safely add more constraints to itself with a new enforced +ruleset. + +One policy layer grants access to a file path if at least one of its rules +encountered on the path grants the access. A sandboxed thread can only access +a file path if all its enforced policy layers grant the access as well as all +the other system access controls (e.g. filesystem DAC, other LSM policies, +etc.). + +Bind mounts and OverlayFS +------------------------- + +Landlock enables to restrict access to file hierarchies, which means that these +access rights can be propagated with bind mounts (cf. +:doc:`/filesystems/sharedsubtree`) but not with :doc:`/filesystems/overlayfs`. + +A bind mount mirrors a source file hierarchy to a destination. The destination +hierarchy is then composed of the exact same files, on which Landlock rules can +be tied, either via the source or the destination path. These rules restrict +access when they are encountered on a path, which means that they can restrict +access to multiple file hierarchies at the same time, whether these hierarchies +are the result of bind mounts or not. + +An OverlayFS mount point consists of upper and lower layers. These layers are +combined in a merge directory, result of the mount point. This merge hierarchy +may include files from the upper and lower layers, but modifications performed +on the merge hierarchy only reflects on the upper layer. From a Landlock +policy point of view, each OverlayFS layers and merge hierarchies are +standalone and contains their own set of files and directories, which is +different from bind mounts. A policy restricting an OverlayFS layer will not +restrict the resulted merged hierarchy, and vice versa. + +Inheritance +----------- + +Every new thread resulting from a :manpage:`clone(2)` inherits Landlock domain +restrictions from its parent. This is similar to the seccomp inheritance (cf. +:doc:`/userspace-api/seccomp_filter`) or any other LSM dealing with task's +:manpage:`credentials(7)`. For instance, one process's thread may apply +Landlock rules to itself, but they will not be automatically applied to other +sibling threads (unlike POSIX thread credential changes, cf. +:manpage:`nptl(7)`). + +When a thread sandboxes itself, we have the guarantee that the related security +policy will stay enforced on all this thread's descendants. This allows +creating standalone and modular security policies per application, which will +automatically be composed between themselves according to their runtime parent +policies. + +Ptrace restrictions +------------------- + +A sandboxed process has less privileges than a non-sandboxed process and must +then be subject to additional restrictions when manipulating another process. +To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target +process, a sandboxed process should have a subset of the target process rules, +which means the tracee must be in a sub-domain of the tracer. + +Kernel interface +================ + +Access rights +------------- + +.. kernel-doc:: include/uapi/linux/landlock.h + :identifiers: fs_access + +Creating a new ruleset +---------------------- + +.. kernel-doc:: security/landlock/syscalls.c + :identifiers: sys_landlock_create_ruleset + +.. kernel-doc:: include/uapi/linux/landlock.h + :identifiers: landlock_ruleset_attr + +Extending a ruleset +------------------- + +.. kernel-doc:: security/landlock/syscalls.c + :identifiers: sys_landlock_add_rule + +.. kernel-doc:: include/uapi/linux/landlock.h + :identifiers: landlock_rule_type landlock_path_beneath_attr + +Enforcing a ruleset +------------------- + +.. kernel-doc:: security/landlock/syscalls.c + :identifiers: sys_landlock_enforce_ruleset_self + +Current limitations +=================== + +Ruleset layers +-------------- + +There is a limit of 64 layers of stacked rulesets. This can be an issue for a +task willing to enforce a new ruleset in complement to its 64 inherited +rulesets. Once this limit is reached, sys_landlock_enforce_ruleset_self() +returns E2BIG. It is then strongly suggested to carefully build rulesets once +in the life of a thread, especially for applications able to launch other +applications that may also want to sandbox themselves (e.g. shells, container +managers, etc.). + +Memory usage +------------ + +Kernel memory allocated to create rulesets is accounted and can be restricted +by the :doc:`/admin-guide/cgroup-v1/memory`. + +File renaming and linking +------------------------- + +Because Landlock targets unprivileged access controls, it is needed to properly +handle composition of rules. Such property also implies rules nesting. +Properly handling multiple layers of ruleset, each one of them able to restrict +access to files, also implies to inherit the ruleset restrictions from a parent +to its hierarchy. Because files are identified and restricted by their +hierarchy, moving or linking a file from one directory to another implies to +propagate the hierarchy constraints. To protect against privilege escalations +through renaming or linking, and for the sack of simplicity, Landlock currently +limits linking and renaming to the same directory. Future Landlock evolutions +will enable more flexibility for renaming and linking, with dedicated ruleset +flags. + +Filesystem layout modification +------------------------------ + +As for file renaming and linking, a sandboxed thread cannot modify its +filesystem layout, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`. +However, :manpage:`chroot(2)` calls are not denied. + +Special filesystems +------------------- + +Access to regular files and directories can be restricted by Landlock, +according to the handled accesses of a ruleset. However, files that do not +come from a user-visible filesystem (e.g. pipe, socket), but can still be +accessed through /proc/self/fd/, cannot currently be restricted. Likewise, +some special kernel filesystems such as nsfs, which can be accessed through +/proc/self/ns/, cannot currently be restricted. For now, these kind of special +paths are then always allowed. Future Landlock evolutions will enable to +restrict such paths with dedicated ruleset flags. + +Questions and answers +===================== + +What about user space sandbox managers? +--------------------------------------- + +Using user space process to enforce restrictions on kernel resources can lead +to race conditions or inconsistent evaluations (i.e. `Incorrect mirroring of +the OS code and state +<https://www.ndss-symposium.org/ndss2003/traps-and-pitfalls-practical-problems-system-call-interposition-based-security-tools/>`_). + +What about namespaces and containers? +------------------------------------- + +Namespaces can help create sandboxes but they are not designed for +access-control and then miss useful features for such use case (e.g. no +fine-grained restrictions). Moreover, their complexity can lead to security +issues, especially when untrusted processes can manipulate them (cf. +`Controlling access to user namespaces <https://lwn.net/Articles/673597/>`_). + +Additional documentation +======================== + +* :doc:`/security/landlock` +* https://landlock.io + +.. Links +.. _samples/landlock/sandboxer.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/samples/landlock/sandboxer.cMAINTAINERS +2
-1@@ -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: security/landlock/ K: landlock --2.30.0