Landlock LSM
Subsystems (9)
Mickaël Salaün
maintainerv14 cover Landlock LSM
Hi,
This new version of Landlock is a major revamp of the previous series [1], hence the RFC tag. The three main changes are the replacement of eBPF with a dedicated safe management of access rules, the replacement of the use of seccomp(2) with a dedicated syscall, and the management of filesystem access-control (back from the v10).
As discussed in [2], eBPF may be too powerful and dangerous to be put in the hand of unprivileged and potentially malicious processes, especially because of side-channel attacks against access-controls or other parts of the kernel.
Thanks to this new implementation (1540 SLOC), designed from the ground to be used by unprivileged processes, this series enables a process to sandbox itself without requiring CAP_SYS_ADMIN, but only the no_new_privs constraint (like seccomp). Not relying on eBPF also enables to improve performances, especially for stacked security policies thanks to mergeable rulesets.
The compiled documentation is available here:
https://landlock.io/linux-doc/landlock-v14/security/landlock/index.html
This series can be applied on top of v5.6-rc3. This can be tested with CONFIG_SECURITY_LANDLOCK and CONFIG_SAMPLE_LANDLOCK. This patch series can be found in a Git repository here:
https://github.com/landlock-lsm/linux/commits/landlock-v14
I would really appreciate constructive comments on the design and the code.
# 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 [3], 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 empower 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.
# Current limitations
## Path walk
Landlock need to use dentries to identify a file hierarchy, which is needed for composable and unprivileged access-controls. This means that path resolution/walking (handled with inode_permission()) is not supported, yet. This could be filled with a future extension first of the LSM framework. The Landlock userspace ABI can handle such change with new option (e.g. to the struct landlock_ruleset).
## UnionFS
An UnionFS super-block use a set of upper and lower directories. An access request to a file in one of these hierarchy trigger a call to ovl_path_real() which generate another access request according to the matching hierarchy. Because such super-block is not aware of its current mount point, OverlayFS can't create a dedicated mnt_parent for each of the upper and lower directories mount clones. It is then not currently possible to track the source of such indirect access-request, and then not possible to identify a unified OverlayFS hierarchy.
## Syscall
Because it is only tested on x86_64, the syscall is only wired up for this architecture. The whole x86 family (and probably all the others) will be supported in the next patch series.
## Memory limits
There is currently no limit on the memory usage. Any idea to leverage an existing mechanism (e.g. rlimit)?
# Changes since v13
* Revamp of the LSM: remove the need for eBPF and seccomp(2).
* Implement a full filesystem access-control.
* Take care of the backward compatibility issues, especially for this security features.
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]/
[3] https://lore.kernel.org/lkml/[email protected]/
Regards,
Mickaël Salaün (10):
landlock: Add object and rule management
landlock: Add ruleset and domain management
landlock: Set up the security framework and manage credentials
landlock: Add ptrace restrictions
fs,landlock: Support filesystem access-control
landlock: Add syscall implementation
arch: Wire up landlock() syscall
selftests/landlock: Add initial tests samples/landlock: Add a sandbox manager example
landlock: Add user and kernel documentation
Documentation/security/index.rst | 1 + Documentation/security/landlock/index.rst | 18 + Documentation/security/landlock/kernel.rst | 44 ++ Documentation/security/landlock/user.rst | 233 +++++++ MAINTAINERS | 12 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/super.c | 2 + include/linux/landlock.h | 22 + include/linux/syscalls.h | 3 + include/uapi/asm-generic/unistd.h | 4 +- include/uapi/linux/landlock.h | 315 +++++++++ samples/Kconfig | 7 + samples/Makefile | 1 + samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 15 + samples/landlock/sandboxer.c | 226 +++++++ security/Kconfig | 11 +- security/Makefile | 2 + security/landlock/Kconfig | 16 + security/landlock/Makefile | 4 + security/landlock/cred.c | 47 ++ security/landlock/cred.h | 55 ++ security/landlock/fs.c | 591 +++++++++++++++++ security/landlock/fs.h | 42 ++ security/landlock/object.c | 341 ++++++++++ security/landlock/object.h | 134 ++++ security/landlock/ptrace.c | 118 ++++ security/landlock/ptrace.h | 14 + security/landlock/ruleset.c | 463 +++++++++++++ security/landlock/ruleset.h | 106 +++ security/landlock/setup.c | 38 ++ security/landlock/setup.h | 20 + security/landlock/syscall.c | 470 +++++++++++++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/landlock/.gitignore | 3 + tools/testing/selftests/landlock/Makefile | 13 + tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/test.h | 40 ++ tools/testing/selftests/landlock/test_base.c | 80 +++ tools/testing/selftests/landlock/test_fs.c | 624 ++++++++++++++++++ .../testing/selftests/landlock/test_ptrace.c | 293 ++++++++ 41 files changed, 4429 insertions(+), 6 deletions(-) create mode 100644 Documentation/security/landlock/index.rst create mode 100644 Documentation/security/landlock/kernel.rst create mode 100644 Documentation/security/landlock/user.rst create mode 100644 include/linux/landlock.h 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/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/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/syscall.c create mode 100644 tools/testing/selftests/landlock/.gitignore create mode 100644 tools/testing/selftests/landlock/Makefile create mode 100644 tools/testing/selftests/landlock/config create mode 100644 tools/testing/selftests/landlock/test.h create mode 100644 tools/testing/selftests/landlock/test_base.c create mode 100644 tools/testing/selftests/landlock/test_fs.c create mode 100644 tools/testing/selftests/landlock/test_ptrace.c
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 1/10 landlock: Add object and rule management
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 can't 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 this 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 object. 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 v13:
* New dedicated implementation, removing the need for eBPF.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 10 ++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 15 ++ security/landlock/Makefile | 3 + security/landlock/object.c | 339 +++++++++++++++++++++++++++++++++++++ security/landlock/object.h | 134 +++++++++++++++ 7 files changed, 504 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
@@ -9360,6 +9360,16 @@ F: net/core/skmsg.c F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c +LANDLOCK SECURITY MODULE +M: Mickaël Salaün <[email protected]> +L: [email protected] +W: https://landlock.io +T: git https://github.com/landlock-lsm/linux.git +S: Supported +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
@@ -12,6 +12,7 @@ subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o
@@ -29,6 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file lists
security/landlock/Kconfig new file +15 -0
@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n + help + This selects Landlock, a safe sandboxing mechanism. It enables to + restrict processes on the fly (i.e. enforce an access control policy), + which can complement seccomp-bpf. The security policy is a set of access + rights tied to an object, which could be a file, a socket or a process. + + See Documentation/security/landlock/ for further information. + + If you are unsure how to answer this question, answer N.
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 +339 -0
339 lines not on this page · raw message
security/landlock/object.h new file +134 -0
134 lines not on this page · raw message
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 2/10 landlock: Add ruleset and domain management
In reply to Mickaël SalaünA 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 adding the syscall) which enables a process to build it by adding new rules.
A domain is a ruleset tied to a set of processes. This group of rules defined the security policy enforced on these processes and their future children. A domain can transition to a new domain which is the merge of itself with a ruleset provided by the current process. This merge is the intersection of all the constraints, which means that a process can only gain more constraints over time.
---
Changes since v13:
* New implementation, inspired by the previous inode eBPF map, but agnostic to the underlying kernel object.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 1 + include/uapi/linux/landlock.h | 102 ++++++++ security/landlock/Makefile | 2 +- security/landlock/ruleset.c | 460 ++++++++++++++++++++++++++++++++++ security/landlock/ruleset.h | 106 ++++++++ 5 files changed, 670 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/landlock.h create mode 100644 security/landlock/ruleset.c create mode 100644 security/landlock/ruleset.h
MAINTAINERS +1 -0
@@ -9366,6 +9366,7 @@ L: [email protected] W: https://landlock.io T: git https://github.com/landlock-lsm/linux.git S: Supported +F: include/uapi/linux/landlock.h F: security/landlock/ K: landlock K: LANDLOCK
include/uapi/linux/landlock.h new file +102 -0
102 lines not on this page · raw message
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/ruleset.c new file +460 -0
460 lines not on this page · raw message
security/landlock/ruleset.h new file +106 -0
106 lines not on this page · raw message
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 3/10 landlock: Set up the security framework and manage credentials
In reply to Mickaël SalaünA process 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 v13:
* totally get ride of the seccomp dependency
* only keep credential management and LSM setup.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- security/Kconfig | 10 +++---- security/landlock/Makefile | 3 ++- security/landlock/cred.c | 47 ++++++++++++++++++++++++++++++++ security/landlock/cred.h | 55 ++++++++++++++++++++++++++++++++++++++ security/landlock/setup.c | 30 +++++++++++++++++++++ security/landlock/setup.h | 18 +++++++++++++ 6 files changed, 157 insertions(+), 6 deletions(-) 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" if DEFAULT_SECURITY_SMACK - default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR - default "lockdown,yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO - default "lockdown,yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC - default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" + default "landlock,lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK + default "landlock,lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR + default "landlock,lockdown,yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO + default "landlock,lockdown,yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC + default "landlock,lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" 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/cred.c new file +47 -0
47 lines not on this page · raw message
security/landlock/cred.h new file +55 -0
55 lines not on this page · raw message
security/landlock/setup.c new file +30 -0
@@ -0,0 +1,30 @@ +// 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 "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 ) +{ +pr_info (LANDLOCK_NAME ": Registering hooks\n" ); +landlock_add_hooks_cred (); +return 0 ; +} + +DEFINE_LSM (LANDLOCK_NAME ) = { + .name =LANDLOCK_NAME , + .init = landlock_init, + .blobs = &landlock_blob_sizes, +};
security/landlock/setup.h new file +18 -0
@@ -0,0 +1,18 @@ +/* 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> + +#define LANDLOCK_NAME "landlock" + +extern struct lsm_blob_sizes landlock_blob_sizes; + +#endif /* _SECURITY_LANDLOCK_SETUP_H */
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 4/10 landlock: Add ptrace restrictions
In reply to Mickaël SalaünUsing 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' rules (i.e. the tracee must be in a sub-domain of the tracer).
---
Changes since v13:
* Make the ptrace restriction mandatory, like in the v10.
* Remove the eBPF dependency.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- security/landlock/Makefile | 2 +- security/landlock/ptrace.c | 118 +++++++++++++++++++++++++++++++++++++ security/landlock/ptrace.h | 14 +++++ security/landlock/setup.c | 2 + 4 files changed, 135 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 +118 -0
118 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_hooks_ptrace (void ); + +#endif /* _SECURITY_LANDLOCK_PTRACE_H */
security/landlock/setup.c +2 -0
@@ -10,6 +10,7 @@#include <linux/lsm_hooks.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 @@ static int __init landlock_init(void) {pr_info (LANDLOCK_NAME ": Registering hooks\n" );landlock_add_hooks_cred (); +landlock_add_hooks_ptrace ();return 0 ; }
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 5/10 fs,landlock: Support filesystem access-control
In reply to Mickaël SalaünThanks to the Landlock objects and ruleset, it is possible to identify inodes according to a process' 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(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 and ANDed to create an 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.
---
Changes since v11:
* Add back, revamp and make a fully working filesystem access-control based on paths and inodes.
* Remove the eBPF dependency.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 1 + fs/super.c | 2 + include/linux/landlock.h | 22 ++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 +- security/landlock/fs.c | 591 +++++++++++++++++++++++++++++++++++++ security/landlock/fs.h | 42 +++ security/landlock/object.c | 2 + security/landlock/setup.c | 6 + security/landlock/setup.h | 2 + 10 files changed, 670 insertions(+), 1 deletion(-) create mode 100644 include/linux/landlock.h create mode 100644 security/landlock/fs.c create mode 100644 security/landlock/fs.h
MAINTAINERS +1 -0
@@ -9366,6 +9366,7 @@ L: [email protected] W: https://landlock.io T: git https://github.com/landlock-lsm/linux.git S: Supported +F: include/linux/landlock.h F: include/uapi/linux/landlock.h F: security/landlock/ K: landlock
fs/super.c +2 -0
@@ -34,6 +34,7 @@#include <linux/cleancache.h> #include <linux/fscrypt.h> #include <linux/fsnotify.h> +#include <linux/landlock.h> #include <linux/lockdep.h> #include <linux/user_namespace.h> #include <linux/fs_context.h>
@@ -454,6 +455,7 @@ void generic_shutdown_super(struct super_block *sb)evict_inodes (sb);/* only nonzero refcount inodes can have marks */ fsnotify_sb_delete (sb); +landlock_release_inodes (sb);if (sb->s_dio_done_wq) {destroy_workqueue (sb->s_dio_done_wq);
include/linux/landlock.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Landlock LSM - public kernel headers +* +* Copyright © 2016-2019 Mickaël Salaün <[email protected]> +* Copyright © 2018-2019 ANSSI +*/ + +#ifndef _LINUX_LANDLOCK_H +#define _LINUX_LANDLOCK_H + +#include <linux/fs.h> + +#ifdef CONFIG_SECURITY_LANDLOCK +extern void landlock_release_inodes (struct super_block *sb); +#else +static inline void landlock_release_inodes (struct super_block *sb) +{ +} +#endif + +#endif /* _LINUX_LANDLOCK_H */
security/landlock/Kconfig +1 -0
@@ -3,6 +3,7 @@ config SECURITY_LANDLOCK bool "Landlock support" depends on SECURITY + select SECURITY_PATH default n help This selects Landlock, a safe sandboxing mechanism. It enables 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 +591 -0
591 lines not on this page · raw message
security/landlock/fs.h new file +42 -0
42 lines not on this page · raw message
security/landlock/object.c +2 -0
@@ -29,6 +29,7 @@#include <linux/spinlock.h> #include <linux/workqueue.h> +#include "fs.h" #include "object.h" struct landlock_object *landlock_create_object(
@@ -138,6 +139,7 @@ static bool release_object(struct landlock_object *object)switch (object->type) {case LANDLOCK_OBJECT_INODE : +landlock_release_inode (underlying_object, object);break ;default :WARN_ON_ONCE (1 );
security/landlock/setup.c +6 -0
@@ -10,11 +10,15 @@#include <linux/lsm_hooks.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 ), };static int __init landlock_init (void )
@@ -22,6 +26,8 @@ static int __init landlock_init(void)pr_info (LANDLOCK_NAME ": Registering hooks\n" );landlock_add_hooks_cred ();landlock_add_hooks_ptrace (); +landlock_add_hooks_fs (); + landlock_initialized = true;return 0 ; }
security/landlock/setup.h +2 -0
@@ -13,6 +13,8 @@#define LANDLOCK_NAME "landlock" +extern bool landlock_initialized; +extern struct lsm_blob_sizes landlock_blob_sizes;#endif /* _SECURITY_LANDLOCK_SETUP_H */
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 6/10 landlock: Add syscall implementation
In reply to Mickaël SalaünThis syscall, inspired from seccomp(2) and bpf(2), is designed to be used by unprivileged processes to sandbox themselves. It has the same usage restrictions as seccomp(2): no_new_privs check.
There is currently four commands:
* get_features: Gets the supported features (required for backward compatibility and best-effort security).
* create_ruleset: Creates a ruleset and returns its file descriptor.
* add_rule: Adds a rule (e.g. file hierarchy access) to a ruleset, identified by the dedicated file descriptor.
* enforce_ruleset: Enforces a ruleset on the current thread (similar to seccomp).
See the user and code documentation for more details.
---
Changes since v13:
* New implementation, replacing the dependency on seccomp(2) and bpf(2).
--- include/linux/syscalls.h | 3 + include/uapi/linux/landlock.h | 213 +++++++++++++++ security/landlock/Makefile | 2 +- security/landlock/ruleset.c | 3 + security/landlock/syscall.c | 470 ++++++++++++++++++++++++++++++++++ 5 files changed, 690 insertions(+), 1 deletion(-) create mode 100644 security/landlock/syscall.c
include/linux/syscalls.h +3 -0
@@ -1003,6 +1003,9 @@ 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 (unsigned int command,unsigned int options, +size_t attr1_size,void __user *attr1_ptr, +size_t attr2_size,void __user *attr2_ptr);/* * Architecture-specific system calls
include/uapi/linux/landlock.h +213 -0
219 lines not on this page · raw message
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 syscall.o object.o ruleset.o \ cred.o ptrace.o fs.o
security/landlock/ruleset.c +3 -0
@@ -17,6 +17,7 @@#include <linux/slab.h> #include <linux/spinlock.h> #include <linux/workqueue.h> +#include <uapi/linux/landlock.h> #include "object.h" #include "ruleset.h"
@@ -40,6 +41,8 @@ struct landlock_ruleset *landlock_create_ruleset(u64 fs_access_mask)struct landlock_ruleset *ruleset;/* Safely handles 32-bits conversion. */ +BUILD_BUG_ON (!__same_type (fs_access_mask, ((struct +landlock_attr_ruleset *)NULL )->handled_access_fs));BUILD_BUG_ON (!__same_type (fs_access_mask, _LANDLOCK_ACCESS_FS_LAST));/* Checks content. */
security/landlock/syscall.c new file +470 -0
470 lines not on this page · raw message
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 7/10 arch: Wire up landlock() syscall
In reply to Mickaël SalaünWire up the landlock() call for x86_64 (for now).
---
Changes since v13:
* New implementation.
--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/uapi/asm-generic/unistd.h | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -359,6 +359,7 @@ 435 common clone3 __x64_sys_clone3/ptregs 437 common openat2 __x64_sys_openat2 438 common pidfd_getfd __x64_sys_pidfd_getfd +439 common landlock __x64_sys_landlock # # x32-specific system call numbers start at 512 to avoid cache impact
include/uapi/asm-generic/unistd.h +3 -1
@@ -855,9 +855,11 @@ __SYSCALL(__NR_clone3, sys_clone3) __SYSCALL(__NR_openat2, sys_openat2)#define __NR_pidfd_getfd 438 __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) +#define __NR_landlock 439 +__SYSCALL(__NR_landlock, sys_landlock)#undef __NR_syscalls -#define __NR_syscalls 439 +#define __NR_syscalls 440/* * 32 bit systems traditionally used different
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 8/10 selftests/landlock: Add initial tests
In reply to Mickaël SalaünTest landlock syscall, ptrace hooks semantic and filesystem access-control.
This is an initial batch, more tests will follow.
---
Changes since v13:
* Add back the filesystem tests (from v10) and extend them.
* Add tests for the new syscall.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/landlock/.gitignore | 3 + tools/testing/selftests/landlock/Makefile | 13 + tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/test.h | 40 ++ tools/testing/selftests/landlock/test_base.c | 80 +++ tools/testing/selftests/landlock/test_fs.c | 624 ++++++++++++++++++ .../testing/selftests/landlock/test_ptrace.c | 293 ++++++++ 8 files changed, 1058 insertions(+) create mode 100644 tools/testing/selftests/landlock/.gitignore create mode 100644 tools/testing/selftests/landlock/Makefile create mode 100644 tools/testing/selftests/landlock/config create mode 100644 tools/testing/selftests/landlock/test.h create mode 100644 tools/testing/selftests/landlock/test_base.c create mode 100644 tools/testing/selftests/landlock/test_fs.c create mode 100644 tools/testing/selftests/landlock/test_ptrace.c
tools/testing/selftests/Makefile +1 -0
@@ -24,6 +24,7 @@ TARGETS += ir TARGETS += kcmp TARGETS += kexec TARGETS += kvm +TARGETS += landlock TARGETS += lib TARGETS += livepatch TARGETS += lkdtm
tools/testing/selftests/landlock/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +/test_base +/test_fs +/test_ptrace
tools/testing/selftests/landlock/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0 + +test_src := $(wildcard test_*.c) + +TEST_GEN_PROGS := $(test_src:.c=) + +usr_include := ../../../../usr/include + +CFLAGS += -Wall -O2 -I$(usr_include) + +include ../lib.mk + +$(TEST_GEN_PROGS): ../kselftest_harness.h $(usr_include)/linux/landlock.h
tools/testing/selftests/landlock/config new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_HEADERS_INSTALL=y +CONFIG_SECURITY_LANDLOCK=y +CONFIG_SECURITY_PATH=y +CONFIG_SECURITY=y
tools/testing/selftests/landlock/test.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Landlock test helpers +* +* Copyright © 2017-2020 Mickaël Salaün <[email protected]> +* Copyright © 2019-2020 ANSSI +*/ + +#include <errno.h> +#include <sys/syscall.h> + +#include "../kselftest_harness.h" + +#ifndef landlock +static inline int landlock (unsigned int command,unsigned int options, +size_t attr_size,void *attr_ptr) +{ + errno =0 ; +return syscall (__NR_landlock, command, options, attr_size, attr_ptr,0 , +NULL ); +} +#endif + +FIXTURE (ruleset_rw) { +struct landlock_attr_ruleset attr_ruleset; +int ruleset_fd; +}; + +FIXTURE_SETUP (ruleset_rw) { + self->attr_ruleset.handled_access_fs =LANDLOCK_ACCESS_FS_READ | +LANDLOCK_ACCESS_FS_WRITE ; + self->ruleset_fd =landlock (LANDLOCK_CMD_CREATE_RULESET , +LANDLOCK_OPT_CREATE_RULESET , +sizeof (self->attr_ruleset), &self->attr_ruleset); +ASSERT_LE (0 , self->ruleset_fd); +} + +FIXTURE_TEARDOWN (ruleset_rw) { +ASSERT_EQ (0 ,close (self->ruleset_fd)); +}
tools/testing/selftests/landlock/test_base.c new file +80 -0
80 lines not on this page · raw message
tools/testing/selftests/landlock/test_fs.c new file +624 -0
624 lines not on this page · raw message
tools/testing/selftests/landlock/test_ptrace.c new file +293 -0
293 lines not on this page · raw message
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 9/10 samples/landlock: Add a sandbox manager example
In reply to Mickaël SalaünAdd a basic sandbox tool to launch a command which can only access a whitelist of file hierarchies in a read-only or read-write way.
---
Changes since v11:
* Add back the filesystem sandbox manager and update it to work with the new Landlock syscall.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- samples/Kconfig | 7 ++ samples/Makefile | 1 + samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 15 +++ samples/landlock/sandboxer.c | 226 +++++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+) create mode 100644 samples/landlock/.gitignore create mode 100644 samples/landlock/Makefile create mode 100644 samples/landlock/sandboxer.c
samples/Kconfig +7 -0
@@ -120,6 +120,13 @@ config SAMPLE_HIDRAW bool "hidraw sample" depends on HEADERS_INSTALL +config SAMPLE_LANDLOCK + bool "Build Landlock sample code" + depends on HEADERS_INSTALL + help + Build a simple Landlock sandbox manager able to launch a process + restricted by a user-defined filesystem access-control security policy. + config SAMPLE_PIDFD bool "pidfd sample" depends on 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 +15 -0
@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: BSD-3-Clause + +hostprogs-y := sandboxer + +always := $(hostprogs-y) + +KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include + +.PHONY: all clean + +all: + $(MAKE) -C ../.. samples/landlock/ + +clean: + $(MAKE) -C ../.. M=samples/landlock/ clean
samples/landlock/sandboxer.c new file +226 -0
226 lines not on this page · raw message
Signature · 1 line
2.25.0
Mickaël Salaün
maintainerRFC PATCH v14 10/10 landlock: Add user and kernel documentation
In reply to Mickaël SalaünThis documentation can be built with the Sphinx framework.
Another location might be more appropriate, though.
---
Changes since v13:
* Rewrote the documentation according to the major revamp.
Previous version:
https://lore.kernel.org/lkml/[email protected]/
--- Documentation/security/index.rst | 1 + Documentation/security/landlock/index.rst | 18 ++ Documentation/security/landlock/kernel.rst | 44 ++++ Documentation/security/landlock/user.rst | 233 +++++++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 Documentation/security/landlock/index.rst create mode 100644 Documentation/security/landlock/kernel.rst create mode 100644 Documentation/security/landlock/user.rst
Documentation/security/index.rst +1 -0
@@ -15,3 +15,4 @@ Security Documentation self-protection siphash tpm/index + landlock/index
Documentation/security/landlock/index.rst new file +18 -0
@@ -0,0 +1,18 @@ +========================================= +Landlock LSM: unprivileged access control +========================================= + +:Author: Mickaël Salaün + +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 empower any +process, including unprivileged ones, to securely restrict themselves. + +.. toctree:: + + user + kernel
Documentation/security/landlock/kernel.rst new file +44 -0
44 lines not on this page · raw message
Documentation/security/landlock/user.rst new file +233 -0
233 lines not on this page · raw message
Signature · 1 line
2.25.0
Quoted 5 lines · Mickaël Salaün
## Syscall
Because it is only tested on
x86_64, the syscall is only wired up for this architecture. The whole x86 family (and probably all the others) will be supported in the next patch series.
General question for u. What is it meant "whole x86 family will be supported". 32-bit x86 will be supported?
Thanks,
Jay
Re: [RFC PATCH v14 01/10] landlock: Add object and rule management
In reply to Mickaël SalaünQuoted 20 lines · Mickaël Salaün
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 can't 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 this 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 object. 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.
[...]
security/landlock/Kconfig line 6
Quoted 12 lines
security/landlock/Kconfig new file +6
-0@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n
(I think "default n" is implicit?)
Quoted 9 lines
security/landlock/Kconfig new file +9
-0+ help + This selects Landlock, a safe sandboxing mechanism. It enables to + restrict processes on the fly (i.e. enforce an access control policy), + which can complement seccomp-bpf. The security policy is a set of access + rights tied to an object, which could be a file, a socket or a process. + + See Documentation/security/landlock/ for further information. + + If you are unsure how to answer this question, answer N.
[...]
Quoted 45 lines
security/landlock/object.c new file +39
-0@@ -0,0 +1,339 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Object and rule management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +* +* Principles and constraints of the object and rule management: +* - Do not leak memory. +* - Try as much as possible to free a memory allocation as soon as it is +* unused. +* - Do not use global lock. +* - Do not charge processes other than the one requesting a Landlock +* operation. +*/ + +#include <linux/bug.h> +#include <linux/compiler.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/fs.h> +#include <linux/kernel.h> +#include <linux/list.h> +#include <linux/rbtree.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/workqueue.h> + +#include "object.h" + +struct landlock_object *landlock_create_object ( +const enum landlock_object_type type,void *underlying_object) +{ +struct landlock_object *object; + +if (WARN_ON_ONCE (!underlying_object))
security/landlock/object.c line 49
Quoted 10 lines
security/landlock/object.c new file +10
-0+return NULL ; + object =kzalloc (sizeof (*object),GFP_KERNEL ); +if (!object) +return NULL ; +refcount_set (&object->usage,1 ); +refcount_set (&object->cleaners,1 ); +spin_lock_init (&object->lock); +INIT_LIST_HEAD (&object->rules); + object->type = type; +WRITE_ONCE (object->underlying_object, underlying_object);
object is not globally visible at this point, so WRITE_ONCE() is unnecessary.
Quoted 25 lines
security/landlock/object.c new file +25
-0+return object; +} + +struct landlock_object *landlock_get_object (struct landlock_object *object) + __acquires(object->usage) +{ +__acquire (object->usage); +/* +* If @object->usage equal 0, then it will be ignored by writers, and +* underlying_object->object may be replaced, but this is not an issue +* for release_object(). +*/ +if (object &&refcount_inc_not_zero (&object->usage)) { +/* +* It should not be possible to get a reference to an object if +* its underlying object is being terminated (e.g. with +* landlock_release_object()), because an object is only +* modifiable through such underlying object. This is not the +* case with landlock_get_object_cleaner(). +*/ +WARN_ON_ONCE (!READ_ONCE (object->underlying_object)); +return object; + } +return NULL ; +}
security/landlock/object.c line 84
Quoted 10 lines
security/landlock/object.c new file +10
-0+ +static struct landlock_object *get_object_cleaner ( +struct landlock_object *object) + __acquires(object->cleaners) +{ +__acquire (object->cleaners); +if (object &&refcount_inc_not_zero (&object->cleaners)) +return object; +return NULL ; +}
I don't get this whole "cleaners" thing. Can you give a quick description of why this is necessary, and what benefits it has over a standard refcounting+RCU scheme? I don't immediately see anything that requires this.
Quoted 213 lines
security/landlock/object.c new file +213
-0+/* +* There is two cases when an object should be free and the reference to the +* underlying object should be put: +* - when the last rule tied to this object is removed, which is handled by +* landlock_put_rule() and then release_object(); +* - when the object is being terminated (e.g. no more reference to an inode), +* which is handled by landlock_put_object(). +*/ +static void put_object_free (struct landlock_object *object) + __releases(object->cleaners) +{ +__release (object->cleaners); +if (!refcount_dec_and_test (&object->cleaners)) +return ; +WARN_ON_ONCE (refcount_read (&object->usage)); +/* +* Ensures a safe use of @object in the RCU block from +* landlock_put_rule(). +*/ +kfree_rcu (object, rcu_free); +} + +/* +* Destroys a newly created and useless object. +*/ +void landlock_drop_object (struct landlock_object *object) +{ +if (WARN_ON_ONCE (!refcount_dec_and_test (&object->usage))) +return ; +__acquire (object->cleaners); +put_object_free (object); +} + +/* +* Puts the underlying object (e.g. inode) if it is the first request to +* release @object, without calling landlock_put_object(). +* +* Return true if this call effectively marks @object as released, false +* otherwise. +*/ +static bool release_object (struct landlock_object *object) + __releases(&object->lock) +{ +void *underlying_object; + +lockdep_assert_held (&object->lock); + + underlying_object =xchg (&object->underlying_object,NULL ); +spin_unlock (&object->lock); +might_sleep (); +if (!underlying_object) +return false; + +switch (object->type) { +case LANDLOCK_OBJECT_INODE : +break ; +default : +WARN_ON_ONCE (1 ); + } +return true; +} + +static void put_object_cleaner (struct landlock_object *object) + __releases(object->cleaners) +{ +/* Let's try an early lockless check. */ +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object)) { +/* +* Puts @object if there is no rule tied to it and the +* remaining user is the underlying object. This check is +* atomic because @object->rules and @object->underlying_object +* are protected by @object->lock. +*/ +spin_lock (&object->lock); +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object) && +refcount_dec_if_one (&object->usage)) { +/* +* Releases @object, in place of +* landlock_release_object(). +* +* @object is already empty, implying that all its +* previous rules are already disabled. +* +* Unbalance the @object->cleaners counter to reflect +* the underlying object release. +*/ +if (!WARN_ON_ONCE (!release_object (object))) { +__acquire (object->cleaners); +put_object_free (object); + } + }else { +spin_unlock (&object->lock); + } + } +put_object_free (object); +} + +/* +* Putting an object is easy when the object is being terminated, but it is +* much more tricky when the reason is that there is no more rule tied to this +* object. Indeed, new rules could be added at the same time. +*/ +void landlock_put_object (struct landlock_object *object) + __releases(object->usage) +{ +struct landlock_object *object_cleaner; + +__release (object->usage); +might_sleep (); +if (!object) +return ; +/* +* Guards against concurrent termination to be able to terminate +* @object if it is empty and not referenced by another rule-appender +* other than the underlying object. +*/ + object_cleaner =get_object_cleaner (object); +if (WARN_ON_ONCE (!object_cleaner)) { +__release (object->cleaners); +return ; + } +/* +* Decrements @object->usage and if it reach zero, also decrement +* @object->cleaners. If both reach zero, then release and free +* @object. +*/ +if (refcount_dec_and_test (&object->usage)) { +struct landlock_rule *rule_walker, *rule_walker2; + +spin_lock (&object->lock); +/* +* Disables all the rules tied to @object when it is forbidden +* to add new rule but still allowed to remove them with +* landlock_put_rule(). This is crucial to be able to safely +* free a rule according to landlock_rule_is_disabled(). +*/ +list_for_each_entry_safe (rule_walker, rule_walker2, + &object->rules, list) +list_del_rcu (&rule_walker->list); + +/* +* Releases @object if it is not already released (e.g. with +* landlock_release_object()). +*/ +release_object (object); +/* +* Unbalances the @object->cleaners counter to reflect the +* underlying object release. +*/ +__acquire (object->cleaners); +put_object_free (object); + } +put_object_cleaner (object_cleaner); +} + +void landlock_put_rule (struct landlock_object *object, +struct landlock_rule *rule) +{ +if (!rule) +return ; +WARN_ON_ONCE (!object); +/* +* Guards against a concurrent @object self-destruction with +* landlock_put_object() or put_object_cleaner(). +*/ +rcu_read_lock (); +if (landlock_rule_is_disabled (rule)) { +rcu_read_unlock (); +if (refcount_dec_and_test (&rule->usage)) +kfree_rcu (rule, rcu_free); +return ; + } +if (refcount_dec_and_test (&rule->usage)) { +struct landlock_object *safe_object; + +/* +* Now, @rule may still be enabled, or in the process of being +* untied to @object by put_object_cleaner(). However, we know +* that @object will not be freed until rcu_read_unlock() and +* until @object->cleaners reach zero. Furthermore, we may not +* be the only one willing to free a @rule linked with @object. +* If we succeed to hold @object with get_object_cleaner(), we +* know that until put_object_cleaner(), we can safely use +* @object to remove @rule. +*/ + safe_object =get_object_cleaner (object); +rcu_read_unlock (); +if (!safe_object) { +__release (safe_object->cleaners); +/* +* We can safely free @rule because it is already +* removed from @object's list. +*/ +WARN_ON_ONCE (!landlock_rule_is_disabled (rule)); +kfree_rcu (rule, rcu_free); + }else { +spin_lock (&safe_object->lock); +if (!landlock_rule_is_disabled (rule)) +list_del (&rule->list); +spin_unlock (&safe_object->lock); +kfree_rcu (rule, rcu_free); +put_object_cleaner (safe_object); + } + }else { +rcu_read_unlock (); + } +/* +* put_object_cleaner() might sleep, but it is only reachable if +* !landlock_rule_is_disabled(). Therefore, clean_ref() can not sleep. +*/ +might_sleep ();
security/landlock/object.c line 308
Quoted 10 lines
security/landlock/object.c new file +10
-0+} + +void landlock_release_object (struct landlock_object __rcu *rcu_object) +{ +struct landlock_object *object; + +if (!rcu_object) +return ; +rcu_read_lock (); + object =get_object_cleaner (rcu_dereference (rcu_object));
This is not how RCU works. You need the rcu annotation on the access to the data structure member (or global variable) that's actually being accessed. A "struct foo __rcu *foo" argument is essentially always wrong.
Quoted 9 lines
security/landlock/object.c new file +9
-0+struct landlock_rule { +struct landlock_access access; +/* +* @list: Linked list with other rules tied to the same object, which +* enable to manage their lifetimes. This is also used to identify if +* a rule is still valid, thanks to landlock_rule_is_disabled(), which +* is important in the matching process because the original object +* address might have been recycled. +*/
security/landlock/object.c line 326
Quoted 10 lines
security/landlock/object.c new file +10
-0+struct list_head list; +union { +/* +* @usage: Number of rulesets pointing to this rule. This +* field is never used by RCU readers. +*/ +refcount_t usage; +struct rcu_head rcu_free; + }; +};
An object that is subject to RCU but whose refcount must not be accessed from RCU context? That seems a weird.
security/landlock/object.c line 334
Quoted 8 lines
security/landlock/object.c new file +8
-0+enum landlock_object_type { +LANDLOCK_OBJECT_INODE =1 , +}; + +struct landlock_object { +/* +* @usage: Main usage counter, used to tie an object to it's underlying +* object (i.e. create a lifetime) and potentially add new rules.
I can't really follow this by reading this patch on its own. As one suggestion to make things at least a bit better, how about documenting here that usage always reaches zero before cleaners does?
security/landlock/object.c
Quoted 6 lines
security/landlock/object.c new file +6
-0+*/ +refcount_t usage; +/* +* @cleaners: Usage counter used to free a rule from @rules (thanks to +* put_rule()). Enables to get a reference to this object until it +* really become freed. Cf. put_object().
Maybe add: @usage being non-zero counts as one reference to @cleaners. Once @cleaners has become zero, the object is freed after an RCU grace period.
Quoted 7 lines
security/landlock/object.c new file +7
-0+*/ +refcount_t cleaners; +union { +/* +* The use of this struct is controlled by @usage and +* @cleaners, which makes it safe to union it with @rcu_free. +*/
[...]
Quoted 3 lines
security/landlock/object.c new file +3
-0+struct rcu_head rcu_free; + }; +};
[...]
security/landlock/object.c
Quoted 11 lines
security/landlock/object.c new file +11
-0+static inline bool landlock_rule_is_disabled ( +struct landlock_rule *rule) +{ +/* +* Disabling (i.e. unlinking) a landlock_rule is a one-way operation. +* It is not possible to re-enable such a rule, then there is no need +* for smp_load_acquire(). +* +* LIST_POISON2 is set by list_del() and list_del_rcu(). +*/ +return !rule ||READ_ONCE (rule->list.prev) ==LIST_POISON2 ;
You're not allowed to do this, the comment above list_del() states:
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
If you want to be able to test whether the element is on a list afterwards, use stuff like list_del_init().
Quoted 1 line
+}
Mickaël Salaün
maintainerRe: [RFC PATCH v14 01/10] landlock: Add object and rule management
Quoted 22 lines · Jann Horn
Quoted 20 lines · Mickaël Salaün
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 can't 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 this 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 object. 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.
[...]
Quoted 14 lines
security/landlock/Kconfig line 6
Quoted 12 lines
security/landlock/Kconfig new file +6
-0@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n(I think "default n" is implicit?)
It seems that most (all?) Kconfig are written like this.
Quoted 60 lines
Quoted 10 lines
security/landlock/Kconfig new file +9
-0+ help + This selects Landlock, a safe sandboxing mechanism. It enables to + restrict processes on the fly (i.e. enforce an access control policy), + which can complement seccomp-bpf. The security policy is a set of access + rights tied to an object, which could be a file, a socket or a process. + + See Documentation/security/landlock/ for further information. + + If you are unsure how to answer this question, answer N.[...]
Quoted 49 lines
security/landlock/object.c new file +43
-0@@ -0,0 +1,339 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Object and rule management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +* +* Principles and constraints of the object and rule management: +* - Do not leak memory. +* - Try as much as possible to free a memory allocation as soon as it is +* unused. +* - Do not use global lock. +* - Do not charge processes other than the one requesting a Landlock +* operation. +*/ + +#include <linux/bug.h> +#include <linux/compiler.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/fs.h> +#include <linux/kernel.h> +#include <linux/list.h> +#include <linux/rbtree.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/workqueue.h> + +#include "object.h" + +struct landlock_object *landlock_create_object ( +const enum landlock_object_type type,void *underlying_object) +{ +struct landlock_object *object; + +if (WARN_ON_ONCE (!underlying_object)) +return NULL ; + object =kzalloc (sizeof (*object),GFP_KERNEL ); +if (!object) +return NULL ;
Quoted 8 lines
security/landlock/object.c line 49
Quoted 6 lines
security/landlock/object.c new file +6
-0+refcount_set (&object->usage,1 ); +refcount_set (&object->cleaners,1 ); +spin_lock_init (&object->lock); +INIT_LIST_HEAD (&object->rules); + object->type = type; +WRITE_ONCE (object->underlying_object, underlying_object);
objectis not globally visible at this point, soWRITE_ONCE()is unnecessary.
Right. It was written like this to have a uniform use of this pointer, but I'll remove it.
Quoted 33 lines
Quoted 33 lines
security/landlock/object.c new file +32
-0+return object; +} + +struct landlock_object *landlock_get_object (struct landlock_object *object) + __acquires(object->usage) +{ +__acquire (object->usage); +/* +* If @object->usage equal 0, then it will be ignored by writers, and +* underlying_object->object may be replaced, but this is not an issue +* for release_object(). +*/ +if (object &&refcount_inc_not_zero (&object->usage)) { +/* +* It should not be possible to get a reference to an object if +* its underlying object is being terminated (e.g. with +* landlock_release_object()), because an object is only +* modifiable through such underlying object. This is not the +* case with landlock_get_object_cleaner(). +*/ +WARN_ON_ONCE (!READ_ONCE (object->underlying_object)); +return object; + } +return NULL ; +} + +static struct landlock_object *get_object_cleaner ( +struct landlock_object *object) + __acquires(object->cleaners) +{ +__acquire (object->cleaners); +if (object &&refcount_inc_not_zero (&object->cleaners))
Quoted 8 lines
security/landlock/object.c line 84
Quoted 3 lines
security/landlock/object.c new file +3
-0+return object; +return NULL ; +}I don't get this whole "cleaners" thing. Can you give a quick description of why this is necessary, and what benefits it has over a standard refcounting+RCU scheme? I don't immediately see anything that requires this.
This indeed needs more documentation here. Here is a comment I'll add to get_object_cleaner():
This enables to safely get a reference to an object to potentially free it if it is not already being freed by a concurrent thread. Indeed, the object's address may still be read and dereferenced while a concurrent thread is attempting to clean the object. Cf. &struct landlock_object->usage and &struct landlock_object->cleaners.
See below the explanation about "usage" and "cleaners".
Quoted 221 lines
Quoted 221 lines
security/landlock/object.c new file +220
-0+/* +* There is two cases when an object should be free and the reference to the +* underlying object should be put: +* - when the last rule tied to this object is removed, which is handled by +* landlock_put_rule() and then release_object(); +* - when the object is being terminated (e.g. no more reference to an inode), +* which is handled by landlock_put_object(). +*/ +static void put_object_free (struct landlock_object *object) + __releases(object->cleaners) +{ +__release (object->cleaners); +if (!refcount_dec_and_test (&object->cleaners)) +return ; +WARN_ON_ONCE (refcount_read (&object->usage)); +/* +* Ensures a safe use of @object in the RCU block from +* landlock_put_rule(). +*/ +kfree_rcu (object, rcu_free); +} + +/* +* Destroys a newly created and useless object. +*/ +void landlock_drop_object (struct landlock_object *object) +{ +if (WARN_ON_ONCE (!refcount_dec_and_test (&object->usage))) +return ; +__acquire (object->cleaners); +put_object_free (object); +} + +/* +* Puts the underlying object (e.g. inode) if it is the first request to +* release @object, without calling landlock_put_object(). +* +* Return true if this call effectively marks @object as released, false +* otherwise. +*/ +static bool release_object (struct landlock_object *object) + __releases(&object->lock) +{ +void *underlying_object; + +lockdep_assert_held (&object->lock); + + underlying_object =xchg (&object->underlying_object,NULL ); +spin_unlock (&object->lock); +might_sleep (); +if (!underlying_object) +return false; + +switch (object->type) { +case LANDLOCK_OBJECT_INODE : +break ; +default : +WARN_ON_ONCE (1 ); + } +return true; +} + +static void put_object_cleaner (struct landlock_object *object) + __releases(object->cleaners) +{ +/* Let's try an early lockless check. */ +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object)) { +/* +* Puts @object if there is no rule tied to it and the +* remaining user is the underlying object. This check is +* atomic because @object->rules and @object->underlying_object +* are protected by @object->lock. +*/ +spin_lock (&object->lock); +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object) && +refcount_dec_if_one (&object->usage)) { +/* +* Releases @object, in place of +* landlock_release_object(). +* +* @object is already empty, implying that all its +* previous rules are already disabled. +* +* Unbalance the @object->cleaners counter to reflect +* the underlying object release. +*/ +if (!WARN_ON_ONCE (!release_object (object))) { +__acquire (object->cleaners); +put_object_free (object); + } + }else { +spin_unlock (&object->lock); + } + } +put_object_free (object); +} + +/* +* Putting an object is easy when the object is being terminated, but it is +* much more tricky when the reason is that there is no more rule tied to this +* object. Indeed, new rules could be added at the same time. +*/ +void landlock_put_object (struct landlock_object *object) + __releases(object->usage) +{ +struct landlock_object *object_cleaner; + +__release (object->usage); +might_sleep (); +if (!object) +return ; +/* +* Guards against concurrent termination to be able to terminate +* @object if it is empty and not referenced by another rule-appender +* other than the underlying object. +*/ + object_cleaner =get_object_cleaner (object); +if (WARN_ON_ONCE (!object_cleaner)) { +__release (object->cleaners); +return ; + } +/* +* Decrements @object->usage and if it reach zero, also decrement +* @object->cleaners. If both reach zero, then release and free +* @object. +*/ +if (refcount_dec_and_test (&object->usage)) { +struct landlock_rule *rule_walker, *rule_walker2; + +spin_lock (&object->lock); +/* +* Disables all the rules tied to @object when it is forbidden +* to add new rule but still allowed to remove them with +* landlock_put_rule(). This is crucial to be able to safely +* free a rule according to landlock_rule_is_disabled(). +*/ +list_for_each_entry_safe (rule_walker, rule_walker2, + &object->rules, list) +list_del_rcu (&rule_walker->list); + +/* +* Releases @object if it is not already released (e.g. with +* landlock_release_object()). +*/ +release_object (object); +/* +* Unbalances the @object->cleaners counter to reflect the +* underlying object release. +*/ +__acquire (object->cleaners); +put_object_free (object); + } +put_object_cleaner (object_cleaner); +} + +void landlock_put_rule (struct landlock_object *object, +struct landlock_rule *rule) +{ +if (!rule) +return ; +WARN_ON_ONCE (!object); +/* +* Guards against a concurrent @object self-destruction with +* landlock_put_object() or put_object_cleaner(). +*/ +rcu_read_lock (); +if (landlock_rule_is_disabled (rule)) { +rcu_read_unlock (); +if (refcount_dec_and_test (&rule->usage)) +kfree_rcu (rule, rcu_free); +return ; + } +if (refcount_dec_and_test (&rule->usage)) { +struct landlock_object *safe_object; + +/* +* Now, @rule may still be enabled, or in the process of being +* untied to @object by put_object_cleaner(). However, we know +* that @object will not be freed until rcu_read_unlock() and +* until @object->cleaners reach zero. Furthermore, we may not +* be the only one willing to free a @rule linked with @object. +* If we succeed to hold @object with get_object_cleaner(), we +* know that until put_object_cleaner(), we can safely use +* @object to remove @rule. +*/ + safe_object =get_object_cleaner (object); +rcu_read_unlock (); +if (!safe_object) { +__release (safe_object->cleaners); +/* +* We can safely free @rule because it is already +* removed from @object's list. +*/ +WARN_ON_ONCE (!landlock_rule_is_disabled (rule)); +kfree_rcu (rule, rcu_free); + }else { +spin_lock (&safe_object->lock); +if (!landlock_rule_is_disabled (rule)) +list_del (&rule->list); +spin_unlock (&safe_object->lock); +kfree_rcu (rule, rcu_free); +put_object_cleaner (safe_object); + } + }else { +rcu_read_unlock (); + } +/* +* put_object_cleaner() might sleep, but it is only reachable if +* !landlock_rule_is_disabled(). Therefore, clean_ref() can not sleep. +*/ +might_sleep (); +} + +void landlock_release_object (struct landlock_object __rcu *rcu_object) +{ +struct landlock_object *object; + +if (!rcu_object)
Quoted 8 lines
security/landlock/object.c line 307
Quoted 3 lines
security/landlock/object.c new file +3
-0+return ; +rcu_read_lock (); + object =get_object_cleaner (rcu_dereference (rcu_object));This is not how RCU works. You need the rcu annotation on the access to the data structure member (or global variable) that's actually being accessed. A "struct foo
__rcu*foo" argument is essentially always wrong.
Absolutely! I fixed this with the following patch:
security/landlock/fs.c +3 -1
@@ -98,7 +98,9 @@ void landlock_release_inodes(struct super_block *sb)if (iput_inode)iput (iput_inode); -landlock_release_object (inode_landlock (inode)->object); +rcu_read_lock (); +landlock_release_object (rcu_dereference ( +inode_landlock (inode)->object)); iput_inode = inode;spin_lock (&sb->s_inode_list_lock);
security/landlock/object.c +6 -4
@@ -300,14 +300,16 @@ void landlock_put_rule(struct landlock_object *object,might_sleep (); } -void landlock_release_object (struct landlock_object __rcu *rcu_object) +void landlock_release_object (struct landlock_object *rcu_object) +__releases (RCU ) {struct landlock_object *object; -if (!rcu_object) +if (!rcu_object) { +rcu_read_unlock ();return ; -rcu_read_lock (); - object =get_object_cleaner (rcu_dereference (rcu_object)); + } + object =get_object_cleaner (rcu_object);rcu_read_unlock ();if (unlikely (!object)) {__release (object->cleaners);
security/landlock/object.h +3 -2
@@ -12,9 +12,9 @@#include <linux/compiler_types.h> #include <linux/list.h> #include <linux/poison.h> -#include <linux/rcupdate.h> #include <linux/refcount.h> #include <linux/spinlock.h> +#include <linux/types.h> struct landlock_access {/*
@@ -105,7 +105,8 @@ struct landlock_object {void landlock_put_rule (struct landlock_object *object,struct landlock_rule *rule); -void landlock_release_object (struct landlock_object __rcu *rcu_object); +void landlock_release_object(struct landlock_object *object) + __releases(RCU);struct landlock_object *landlock_create_object (const enum landlock_object_type type,void *underlying_object);
Quoted 15 lines
Quoted 15 lines
security/landlock/object.c new file +14
-0+struct landlock_rule { +struct landlock_access access; +/* +* @list: Linked list with other rules tied to the same object, which +* enable to manage their lifetimes. This is also used to identify if +* a rule is still valid, thanks to landlock_rule_is_disabled(), which +* is important in the matching process because the original object +* address might have been recycled. +*/ +struct list_head list; +union { +/* +* @usage: Number of rulesets pointing to this rule. This +* field is never used by RCU readers.
Quoted 8 lines
security/landlock/object.c line 326
Quoted 5 lines
security/landlock/object.c new file +5
-0+*/ +refcount_t usage; +struct rcu_head rcu_free; + }; +};An object that is subject to RCU but whose refcount must not be accessed from RCU context? That seems a weird.
The fields "access" and "list" are read (in a RCU-read block) by ruleset.c:landlock_find_access() (cf. patch 2). The use of the "usage" counter is in landlock_insert_ruleset_rule() and landlock_put_rule(), but in these cases the rule is always owned/held by the caller. I should say something like "This field must only be used when already holding the rule."
Quoted 13 lines
security/landlock/object.c line 334
Quoted 9 lines
security/landlock/object.c new file +8
-0+enum landlock_object_type { +LANDLOCK_OBJECT_INODE =1 , +}; + +struct landlock_object { +/* +* @usage: Main usage counter, used to tie an object to it's underlying +* object (i.e. create a lifetime) and potentially add new rules. I can't really follow this by reading this patch on its own. As one suggestion to make things at least a bit better, how about documenting here that
usagealways reaches zero beforecleanersdoes?
What about this?
This counter is used to tie an object to its underlying object (e.g. an inode) and to modify it (e.g. add or remove a rule). If this counter reaches zero, the object must not be modified, but it may still be used from within an RCU-read block. When adding a new rule to an object with a usage counter of zero, the underlying object must be locked and its object pointer can then be replaced with a new empty object (while ignoring the disabled object which is being handled by another thread). This counter always reaches zero before @cleaners does.
Quoted 11 lines
security/landlock/object.c
Quoted 7 lines
security/landlock/object.c new file +6
-0+*/ +refcount_t usage; +/* +* @cleaners: Usage counter used to free a rule from @rules (thanks to +* put_rule()). Enables to get a reference to this object until it +* really become freed. Cf. put_object(). Maybe add: @usage being non-zero counts as one reference to @cleaners. Once @cleaners has become zero, the object is freed after an RCU grace period.
What about this?
This counter can only reach zero if the @usage counter already reached zero. Indeed, @usage being non-zero counts as one reference to @cleaners. Once @cleaners has become zero, the object is freed after an RCU grace period. This enables concurrent threads to safely get an object reference to terminate it if there is no more concurrent cleaners for this object. This mechanism is required to enable concurrent threads to safely dereference an object from potentially different pointers (e.g. the underlying object, or a rule tied to this object), to potentially terminate and free it (i.e. if there is no more rules tied to it, or if the underlying object is being terminated).
Quoted 27 lines
Quoted 8 lines
security/landlock/object.c new file +7
-0+*/ +refcount_t cleaners; +union { +/* +* The use of this struct is controlled by @usage and +* @cleaners, which makes it safe to union it with @rcu_free. +*/ [...]
Quoted 3 lines
security/landlock/object.c new file +3
-0+struct rcu_head rcu_free; + }; +};[...]
security/landlock/object.c
Quoted 11 lines
security/landlock/object.c new file +11
-0+static inline bool landlock_rule_is_disabled ( +struct landlock_rule *rule) +{ +/* +* Disabling (i.e. unlinking) a landlock_rule is a one-way operation. +* It is not possible to re-enable such a rule, then there is no need +* for smp_load_acquire(). +* +* LIST_POISON2 is set by list_del() and list_del_rcu(). +*/ +return !rule ||READ_ONCE (rule->list.prev) ==LIST_POISON2 ;You're not allowed to do this, the comment above
list_del()states:
Quoted 2 lines
* Note:
list_empty()on entry does not return true after this, the entry is
* in an undefined state.
list_del() checks READ_ONCE(head->next) == head, but landlock_rule_is_disabled() checks READ_ONCE(rule->list.prev) == LIST_POISON2.
The comment about LIST_POISON2 is right but may be misleading. There is no use of list_empty() with a landlock_rule->list, only landlock_object->rules. The only list_del() is in landlock_put_rule() when there is a guarantee that there is no other reference to it, hence no possible use of landlock_rule_is_disabled() with this rule. I could replace it with a call to list_del_rcu() to make it more consistent.
Quoted 3 lines
If you want to be able to test whether the element is on a list afterwards, use stuff like
list_del_init().
There is no need to re-initialize the list but using list_del_init() and list_empty() could work too. However, there is no list_del_init_rcu() helper. Moreover, resetting the list's pointer with LIST_POISON2 might help to detect bugs.
Thanks for this review!
Mickaël Salaün
maintainerQuoted 11 lines · J Freyensee
Quoted 5 lines · Mickaël Salaün
## Syscall
Because it is only tested on
x86_64, the syscall is only wired up for this architecture. The whole x86 family (and probably all the others) will be supported in the next patch series.General question for u. What is it meant "whole x86 family will be supported". 32-bit x86 will be supported?
Yes, I was referring to x86_32, x86_64 and x32, but all architectures should be supported.
Re: [RFC PATCH v14 01/10] landlock: Add object and rule management
In reply to Mickaël SalaünQuoted 22 lines · Mickaël Salaün
Quoted 21 lines · Jann Horn
Quoted 20 lines · Mickaël Salaün
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 can't 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 this 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 object. 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.
[...]
Quoted 8 lines
Quoted 6 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n(I think "default n" is implicit?)
It seems that most (all?)
Kconfigare written like this.
See e.g. <https://lore.kernel.org/lkml/[email protected]/>.
[...]
Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
(quoted diff) +32
-0+ return object; +} + +struct landlock_object *landlock_get_object(struct landlock_object *object) + __acquires(object->usage) +{ + __acquire(object->usage); + /* + * If @object->usage equal 0, then it will be ignored by writers, and + * underlying_object->object may be replaced, but this is not an issue + * for release_object(). + */ + if (object && refcount_inc_not_zero(&object->usage)) { + /* + * It should not be possible to get a reference to an object if + * its underlying object is being terminated (e.g. with + * landlock_release_object()), because an object is only + * modifiable through such underlying object. This is not the + * case with landlock_get_object_cleaner(). + */ + WARN_ON_ONCE(!READ_ONCE(object->underlying_object)); + return object; + } + return NULL; +} + +static struct landlock_object *get_object_cleaner( + struct landlock_object *object) + __acquires(object->cleaners) +{ + __acquire(object->cleaners); + if (object && refcount_inc_not_zero(&object->cleaners))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return object; + return NULL; +}I don't get this whole "cleaners" thing. Can you give a quick description of why this is necessary, and what benefits it has over a standard refcounting+RCU scheme? I don't immediately see anything that requires this.
Quoted 5 lines
This indeed needs more documentation here. Here is a comment I'll add to
get_object_cleaner():This enables to safely get a reference to an object to potentially free it if it is not already being freed by a concurrent thread.
"get a reference to an object to potentially free it" just sounds all wrong to me. You free an object when you're *dropping* a reference to it. Your refcounting scheme doesn't fit my mental models of how normal refcounting works at all...
[...]
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/* + * Putting an object is easy when the object is being terminated, but it is + * much more tricky when the reason is that there is no more rule tied to this + * object. Indeed, new rules could be added at the same time. + */ +void landlock_put_object(struct landlock_object *object) + __releases(object->usage) +{ + struct landlock_object *object_cleaner; + + __release(object->usage); + might_sleep(); + if (!object) + return; + /* + * Guards against concurrent termination to be able to terminate + * @object if it is empty and not referenced by another rule-appender + * other than the underlying object. + */ + object_cleaner = get_object_cleaner(object);
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
(quoted diff) +10
-0+ /* + * Decrements @object->usage and if it reach zero, also decrement + * @object->cleaners. If both reach zero, then release and free + * @object. + */ + if (refcount_dec_and_test(&object->usage)) { + struct landlock_rule *rule_walker, *rule_walker2; + + spin_lock(&object->lock); + /*
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ * Disables all the rules tied to @object when it is forbidden + * to add new rule but still allowed to remove them with + * landlock_put_rule(). This is crucial to be able to safely + * free a rule according to landlock_rule_is_disabled(). + */ + list_for_each_entry_safe(rule_walker, rule_walker2, + &object->rules, list) + list_del_rcu(&rule_walker->list);
So... rules don't take references on the landlock_objects they use? Instead, the landlock_object knows which rules use it, and when the landlock_object goes away, it nukes all the rules associated with itself?
That seems terrible to me - AFAICS it means that if some random process decides to install a landlock rule that uses inode X, and then that process dies together with all its landlock rules, the inode still stays pinned in kernel memory as long as the superblock is mounted. In other words, it's a resource leak. (And if I'm not missing something in patch 5, that applies even if the inode has been unlinked?)
Can you please refactor your refcounting as follows?
- A rule takes a reference on each landlock_object it uses.
- A landlock_object takes a reference on the underlying object (just like now).
- The underlying object *DOES NOT* take a reference on thelandlock_object (unlike now); the reference from the underlying object to the landlock_object has weak pointer semantics.
- When a landlock_object's refcount drops to zero (iow no rules use
it anymore), it is freed.
That might also help get rid of the awkward ->cleaners thing?
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +14
-0+ /* + * Releases @object if it is not already released (e.g. with + * landlock_release_object()). + */ + release_object(object); + /* + * Unbalances the @object->cleaners counter to reflect the + * underlying object release. + */ + __acquire(object->cleaners); + put_object_free(object); + } + put_object_cleaner(object_cleaner); +}
[...]
Quoted 28 lines
Quoted 14 lines
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool landlock_rule_is_disabled( + struct landlock_rule *rule) +{ + /* + * Disabling (i.e. unlinking) a landlock_rule is a one-way operation. + * It is not possible to re-enable such a rule, then there is no need + * for smp_load_acquire(). + * + * LIST_POISON2 is set by list_del() and list_del_rcu(). + */ + return !rule || READ_ONCE(rule->list.prev) == LIST_POISON2;You're not allowed to do this, the comment above
list_del()states:Quoted 2 lines
* Note:
list_empty()on entry does not return true after this, the entry is
* in an undefined state.
list_del()checksREAD_ONCE(head->next)== head, butlandlock_rule_is_disabled()checksREAD_ONCE(rule->list.prev)==LIST_POISON2.
The comment aboutLIST_POISON2is right but may be misleading. There is no use oflist_empty()with alandlock_rule->list, onlylandlock_object->rules. The onlylist_del()is inlandlock_put_rule()when there is a guarantee that there is no other reference to it, hence no possible use oflandlock_rule_is_disabled()with this rule. I could replace it with a call tolist_del_rcu()to make it more consistent.Quoted 1 line
Quoted 7 lines
Quoted 2 lines
If you want to be able to test whether the element is on a list afterwards, use stuff like
list_del_init().There is no need to re-initialize the list but using
list_del_init()andlist_empty()could work too. However, there is nolist_del_init_rcu()helper. Moreover, resetting the list's pointer withLIST_POISON2might help to detect bugs.
Either way, you are currently using the list_head API in a way that goes against what the header documents. If you want to rely on list_del() bringing the object into a specific state, then you can't leave the comment above list_del() as-is that says that it puts the object in an undefined state; and this kind of check should probably be done in a helper in list.h instead of open-coding the check for LIST_POISON2.
Re: [RFC PATCH v14 05/10] fs,landlock: Support filesystem access-control
In reply to Mickaël Salaünsecurity/landlock/fs.c line 517
Quoted 13 lines · Mickaël Salaün
security/landlock/fs.c new file +13
-0+static inline u32 get_mem_access (unsigned long prot,bool private) +{ +u32 access =LANDLOCK_ACCESS_FS_MAP ; + +/* Private mapping do not write to files. */ +if (!private && (prot &PROT_WRITE )) + access |=LANDLOCK_ACCESS_FS_WRITE ; +if (prot &PROT_READ ) + access |=LANDLOCK_ACCESS_FS_READ ; +if (prot &PROT_EXEC ) + access |=LANDLOCK_ACCESS_FS_EXECUTE ; +return access; +}
When I do the following, is landlock going to detect that the mmap() is a read access, or is it incorrectly going to think that it's neither read nor write?
$ cat write-only.c
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
int main(void) {
int fd = open("/etc/passwd", O_RDONLY);
char *ptr = mmap(NULL, 0x1000, PROT_WRITE, MAP_PRIVATE, fd, 0);
printf("'%.*s'\n", 4, ptr);
}
$ gcc -o write-only write-only.c -Wall
$ ./write-only
'root'
$Re: [RFC PATCH v14 01/10] landlock: Add object and rule management
In reply to Mickaël SalaünOn Mon, 24 Feb 2020 17:02:06 +0100 Mickaël Salaün
Quoted 259 lines
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 can't 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 this 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 object. 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.
Signed-off-by Mickaël Salaün <[email protected]>Cc Andy Lutomirski <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>---
Changes since v13:
* New dedicated implementation, removing the need for eBPF.Previous version:
https://lore.kernel.org/lkml/[email protected]/--- MAINTAINERS | 10 ++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 15 ++ security/landlock/Makefile | 3 + security/landlock/object.c | 339 +++++++++++++++++++++++++++++++++++++ security/landlock/object.h | 134 +++++++++++++++ 7 files changed, 504 insertions(+) create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/object.c create mode 100644 security/landlock/object.hMAINTAINERS +10
-0@@ -9360,6 +9360,16 @@ F: net/core/skmsg.c F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c +LANDLOCK SECURITY MODULE +M: Mickaël Salaün <[email protected]> +L: [email protected] +W: https://landlock.io +T: git https://github.com/landlock-lsm/linux.git +S: Supported +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@@ -12,6 +12,7 @@ subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o@@ -29,6 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file listssecurity/landlock/Kconfig new file +15
-0@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n + help + This selects Landlock, a safe sandboxing mechanism. It enables to + restrict processes on the fly (i.e. enforce an access control policy), + which can complement seccomp-bpf. The security policy is a set of access + rights tied to an object, which could be a file, a socket or a process. + + See Documentation/security/landlock/ for further information. + + If you are unsure how to answer this question, answer N.security/landlock/Makefile new file +3
-0@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := object.osecurity/landlock/object.c new file +123
-0@@ -0,0 +1,339 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Object and rule management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +* +* Principles and constraints of the object and rule management: +* - Do not leak memory. +* - Try as much as possible to free a memory allocation as soon as it is +* unused. +* - Do not use global lock. +* - Do not charge processes other than the one requesting a Landlock +* operation. +*/ + +#include <linux/bug.h> +#include <linux/compiler.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/fs.h> +#include <linux/kernel.h> +#include <linux/list.h> +#include <linux/rbtree.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/workqueue.h> + +#include "object.h" + +struct landlock_object *landlock_create_object ( +const enum landlock_object_type type,void *underlying_object) +{ +struct landlock_object *object; + +if (WARN_ON_ONCE (!underlying_object)) +return NULL ; + object =kzalloc (sizeof (*object),GFP_KERNEL ); +if (!object) +return NULL ; +refcount_set (&object->usage,1 ); +refcount_set (&object->cleaners,1 ); +spin_lock_init (&object->lock); +INIT_LIST_HEAD (&object->rules); + object->type = type; +WRITE_ONCE (object->underlying_object, underlying_object); +return object; +} + +struct landlock_object *landlock_get_object (struct landlock_object *object) + __acquires(object->usage) +{ +__acquire (object->usage); +/* +* If @object->usage equal 0, then it will be ignored by writers, and +* underlying_object->object may be replaced, but this is not an issue +* for release_object(). +*/ +if (object &&refcount_inc_not_zero (&object->usage)) { +/* +* It should not be possible to get a reference to an object if +* its underlying object is being terminated (e.g. with +* landlock_release_object()), because an object is only +* modifiable through such underlying object. This is not the +* case with landlock_get_object_cleaner(). +*/ +WARN_ON_ONCE (!READ_ONCE (object->underlying_object)); +return object; + } +return NULL ; +} + +static struct landlock_object *get_object_cleaner ( +struct landlock_object *object) + __acquires(object->cleaners) +{ +__acquire (object->cleaners); +if (object &&refcount_inc_not_zero (&object->cleaners)) +return object; +return NULL ; +} + +/* +* There is two cases when an object should be free and the reference to the +* underlying object should be put: +* - when the last rule tied to this object is removed, which is handled by +* landlock_put_rule() and then release_object(); +* - when the object is being terminated (e.g. no more reference to an inode), +* which is handled by landlock_put_object(). +*/ +static void put_object_free (struct landlock_object *object) + __releases(object->cleaners) +{ +__release (object->cleaners); +if (!refcount_dec_and_test (&object->cleaners)) +return ; +WARN_ON_ONCE (refcount_read (&object->usage)); +/* +* Ensures a safe use of @object in the RCU block from +* landlock_put_rule(). +*/ +kfree_rcu (object, rcu_free); +} + +/* +* Destroys a newly created and useless object. +*/ +void landlock_drop_object (struct landlock_object *object) +{ +if (WARN_ON_ONCE (!refcount_dec_and_test (&object->usage))) +return ; +__acquire (object->cleaners); +put_object_free (object); +} + +/* +* Puts the underlying object (e.g. inode) if it is the first request to +* release @object, without calling landlock_put_object(). +* +* Return true if this call effectively marks @object as released, false
security/landlock/object.c line 133
Quoted 10 lines
security/landlock/object.c new file +10
-0+* otherwise. +*/ +static bool release_object (struct landlock_object *object) + __releases(&object->lock) +{ +void *underlying_object; + +lockdep_assert_held (&object->lock); + + underlying_object =xchg (&object->underlying_object,NULL );
A one-line comment looks needed for xchg.
security/landlock/object.c line 135
Quoted 2 lines
security/landlock/object.c new file +2
-0+spin_unlock (&object->lock); +might_sleep ();
Have trouble working out what might_sleep is put for.
Quoted 29 lines
security/landlock/object.c new file +29
-0+if (!underlying_object) +return false; + +switch (object->type) { +case LANDLOCK_OBJECT_INODE : +break ; +default : +WARN_ON_ONCE (1 ); + } +return true; +} + +static void put_object_cleaner (struct landlock_object *object) + __releases(object->cleaners) +{ +/* Let's try an early lockless check. */ +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object)) { +/* +* Puts @object if there is no rule tied to it and the +* remaining user is the underlying object. This check is +* atomic because @object->rules and @object->underlying_object +* are protected by @object->lock. +*/ +spin_lock (&object->lock); +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object) && +refcount_dec_if_one (&object->usage)) { +/*
security/landlock/object.c line 174
Quoted 10 lines
security/landlock/object.c new file +10
-0+* Releases @object, in place of +* landlock_release_object(). +* +* @object is already empty, implying that all its +* previous rules are already disabled. +* +* Unbalance the @object->cleaners counter to reflect +* the underlying object release. +*/ +if (!WARN_ON_ONCE (!release_object (object))) {
Two ! hurt more than help.
security/landlock/object.c line 176
Quoted 2 lines
security/landlock/object.c new file +2
-0+__acquire (object->cleaners); +put_object_free (object);
Why put object more than once?
Quoted 8 lines
security/landlock/object.c new file +8
-0+ } + }else { +spin_unlock (&object->lock); + } + } +put_object_free (object); +} +
Mickaël Salaün
maintainerRe: [RFC PATCH v14 01/10] landlock: Add object and rule management
In reply to Jann HornQuoted 29 lines · Jann Horn
Quoted 22 lines · Mickaël Salaün
Quoted 21 lines · Jann Horn
Quoted 20 lines · Mickaël Salaün
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 can't 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 this 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 object. 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.
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
(quoted diff) +4
-0+config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
(I think "default n" is implicit?)
It seems that most (all?)
Kconfigare written like this.See e.g. <https://lore.kernel.org/lkml/[email protected]/>.
Ok, done.
Quoted 46 lines
[...]
Quoted 44 lines
Quoted 32 lines
Quoted 32 lines
(quoted diff) +32
-0+ return object; +} + +struct landlock_object *landlock_get_object(struct landlock_object *object) + __acquires(object->usage) +{ + __acquire(object->usage); + /* + * If @object->usage equal 0, then it will be ignored by writers, and + * underlying_object->object may be replaced, but this is not an issue + * for release_object(). + */ + if (object && refcount_inc_not_zero(&object->usage)) { + /* + * It should not be possible to get a reference to an object if + * its underlying object is being terminated (e.g. with + * landlock_release_object()), because an object is only + * modifiable through such underlying object. This is not the + * case with landlock_get_object_cleaner(). + */ + WARN_ON_ONCE(!READ_ONCE(object->underlying_object)); + return object; + } + return NULL; +} + +static struct landlock_object *get_object_cleaner( + struct landlock_object *object) + __acquires(object->cleaners) +{ + __acquire(object->cleaners); + if (object && refcount_inc_not_zero(&object->cleaners))Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ return object; + return NULL; +}I don't get this whole "cleaners" thing. Can you give a quick description of why this is necessary, and what benefits it has over a standard refcounting+RCU scheme? I don't immediately see anything that requires this.
This indeed needs more documentation here. Here is a comment I'll add to
get_object_cleaner():
Quoted 7 lines
Quoted 2 lines
This enables to safely get a reference to an object to potentially free it if it is not already being freed by a concurrent thread.
"get a reference to an object to potentially free it" just sounds all wrong to me. You free an object when you're *dropping* a reference to it. Your refcounting scheme doesn't fit my mental models of how normal refcounting works at all...
Unfortunately, as I explain below, it is a bit tricky.
Quoted 42 lines
[...]
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/* + * Putting an object is easy when the object is being terminated, but it is + * much more tricky when the reason is that there is no more rule tied to this + * object. Indeed, new rules could be added at the same time. + */ +void landlock_put_object(struct landlock_object *object) + __releases(object->usage) +{ + struct landlock_object *object_cleaner; + + __release(object->usage); + might_sleep(); + if (!object) + return; + /* + * Guards against concurrent termination to be able to terminate + * @object if it is empty and not referenced by another rule-appender + * other than the underlying object. + */ + object_cleaner = get_object_cleaner(object);[...]
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
(quoted diff) +10
-0+ /* + * Decrements @object->usage and if it reach zero, also decrement + * @object->cleaners. If both reach zero, then release and free + * @object. + */ + if (refcount_dec_and_test(&object->usage)) { + struct landlock_rule *rule_walker, *rule_walker2; + + spin_lock(&object->lock); + /*Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ * Disables all the rules tied to @object when it is forbidden + * to add new rule but still allowed to remove them with + * landlock_put_rule(). This is crucial to be able to safely + * free a rule according to landlock_rule_is_disabled(). + */ + list_for_each_entry_safe(rule_walker, rule_walker2, + &object->rules, list) + list_del_rcu(&rule_walker->list);
Quoted 4 lines
So... rules don't take references on the
landlock_objectsthey use? Instead, thelandlock_objectknows which rules use it, and when thelandlock_objectgoes away, it nukes all the rules associated with itself?
Right.
Quoted 6 lines
That seems terrible to me - AFAICS it means that if some random process decides to install a landlock rule that uses inode X, and then that process dies together with all its landlock rules, the inode still stays pinned in kernel memory as long as the superblock is mounted. In other words, it's a resource leak.
That is not correct. When there is no more process enforced by a domain/ruleset, this domain is terminated, which means that every rules linked to this domain are put away. When the usage counter of a rule reaches zero, then the rule is terminated with landlock_put_rule() which unlink the rule from its object and clean this object. The cleaning involves to free the object if there is no rule tied to this object, thanks to put_object_cleaner().
When the underlying object is terminated, landlock_release_object() also decrement the usage counter. However, if there is a concurrent thread adding a new rule, the usage counter still stay greater than zero while the new rule is being added, but the counter then drops to zero at the end of this addition, which can then unbalance the "cleaners" counter, which will finally leads to the object freeing. This design enables to add rules without locking (if the object already exists). While this property is interesting for a performance point of view, the main reason is to avoid unnecessary lock between processes (especially from different domains).
Quoted 3 lines
(And if I'm not missing
something in patch 5, that applies even if the inode has been unlinked?)
That is true for now, but only because I didn't find yet the right spot to call landlock_release_inode(). Indeed, unlinking a file may not terminate an inode because it can still be open by a process, and freeing an object when the underlying object is unlinked could be a way to bypass a check on that object/inode.
Do you know where is the best spot to identify the last userspace reference (through the filesystem or a file descriptor) to an inode? Fnotify doesn't seem to check for that.
Quoted 8 lines
Can you please refactor your refcounting as follows?
- A rule takes a reference on each
landlock_objectit uses.
- Alandlock_objecttakes a reference on the underlying object (just like now).
- The underlying object *DOES NOT* take a reference on thelandlock_object(unlike now); the reference from the underlying object to thelandlock_objecthas weak pointer semantics.
We need to increment the reference counter of the underlying objects (i.e. inodes) not to lose the link with their Landlock object and then the related access-control. For instance, if a struct inode (e.g. a directory) is first tied to a Landlock object/access-control, then because the inode is not open nor used by any process and the kernel decides to free it, when a process tries to access a file beneath this directory, there will not have any Landlock object tied to it and the requested access might then be forbidden (whereas the initial policy allowed it).
Quoted 2 lines
- When a landlock_object's refcount drops to zero (iow no rules use
it anymore), it is freed.
Before the current design, I used a similar pattern, but this is not necessary because of the management of the underlying object lifetime. The list_empty() check is enough, and because we need to handle concurrent termination, the object's usage counter for the rules seems unnecessary.
Quoted 54 lines
That might also help get rid of the awkward ->cleaners thing?
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +14
-0+ /* + * Releases @object if it is not already released (e.g. with + * landlock_release_object()). + */ + release_object(object); + /* + * Unbalances the @object->cleaners counter to reflect the + * underlying object release. + */ + __acquire(object->cleaners); + put_object_free(object); + } + put_object_cleaner(object_cleaner); +}[...]
Quoted 28 lines
Quoted 14 lines
Review
Quoted 11 lines
(quoted diff) +11
-0+static inline bool landlock_rule_is_disabled( + struct landlock_rule *rule) +{ + /* + * Disabling (i.e. unlinking) a landlock_rule is a one-way operation. + * It is not possible to re-enable such a rule, then there is no need + * for smp_load_acquire(). + * + * LIST_POISON2 is set by list_del() and list_del_rcu(). + */ + return !rule || READ_ONCE(rule->list.prev) == LIST_POISON2;You're not allowed to do this, the comment above
list_del()states:Quoted 2 lines
* Note:
list_empty()on entry does not return true after this, the entry is
* in an undefined state.
list_del()checksREAD_ONCE(head->next)== head, butlandlock_rule_is_disabled()checksREAD_ONCE(rule->list.prev)==LIST_POISON2.
The comment aboutLIST_POISON2is right but may be misleading. There is no use oflist_empty()with alandlock_rule->list, onlylandlock_object->rules. The onlylist_del()is inlandlock_put_rule()when there is a guarantee that there is no other reference to it, hence no possible use oflandlock_rule_is_disabled()with this rule. I could replace it with a call tolist_del_rcu()to make it more consistent.Quoted 1 line
Quoted 7 lines
Quoted 2 lines
If you want to be able to test whether the element is on a list afterwards, use stuff like
list_del_init().There is no need to re-initialize the list but using
list_del_init()andlist_empty()could work too. However, there is nolist_del_init_rcu()helper. Moreover, resetting the list's pointer withLIST_POISON2might help to detect bugs.
Quoted 7 lines
Either way, you are currently using the
list_headAPI in a way that goes against what the header documents. If you want to rely onlist_del()bringing the object into a specific state, then you can't leave the comment abovelist_del()as-is that says that it puts the object in an undefined state; and this kind of check should probably be done in a helper inlist.hinstead of open-coding the check forLIST_POISON2.
In the case of Landlock, it is illegal to use or recycle a rule which was untied from its (initial) object. There is no use of list_empty(&landlock_rule->list), only landlock_rule_is_disabled(landlock_rule). The LIST_POISON2 might help to identify such misuse.
Mickaël Salaün
maintainerRe: [RFC PATCH v14 05/10] fs,landlock: Support filesystem access-control
In reply to Jann HornQuoted 19 lines · Jann Horn
security/landlock/fs.c line 517
Quoted 13 lines · Mickaël Salaün
security/landlock/fs.c new file +13
-0+static inline u32 get_mem_access (unsigned long prot,bool private) +{ +u32 access =LANDLOCK_ACCESS_FS_MAP ; + +/* Private mapping do not write to files. */ +if (!private && (prot &PROT_WRITE )) + access |=LANDLOCK_ACCESS_FS_WRITE ; +if (prot &PROT_READ ) + access |=LANDLOCK_ACCESS_FS_READ ; +if (prot &PROT_EXEC ) + access |=LANDLOCK_ACCESS_FS_EXECUTE ; +return access; +}When I do the following, is landlock going to detect that the
mmap()is a read access, or is it incorrectly going to think that it's neither read nor write?
Quoted 14 lines
$ cat write-only.c
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
int main(void) {
int fd = open("/etc/passwd", O_RDONLY);
char *ptr = mmap(NULL, 0x1000, PROT_WRITE, MAP_PRIVATE, fd, 0);
printf("'%.*s'\n", 4, ptr);
}
$ gcc -o write-only write-only.c -Wall
$ ./write-only
'root'
$Thanks to the "if (!private && (prot & PROT_WRITE))", Landlock allows this private mmap (as intended) even if there is no write access to this file, but not with a shared mmap (and a file opened with O_RDWR). I just added a test for this to be sure.
However, I'm not sure this hook is useful for now. Indeed, the process still need to have a file descriptor open with the right accesses.
Re: [RFC PATCH v14 05/10] fs,landlock: Support filesystem access-control
Quoted 15 lines · Mickaël Salaün
Quoted 14 lines · Jann Horn
Quoted 13 lines · Mickaël Salaün
security/landlock/fs.c new file +13
-0+static inline u32 get_mem_access (unsigned long prot,bool private) +{ +u32 access =LANDLOCK_ACCESS_FS_MAP ; + +/* Private mapping do not write to files. */ +if (!private && (prot &PROT_WRITE )) + access |=LANDLOCK_ACCESS_FS_WRITE ; +if (prot &PROT_READ ) + access |=LANDLOCK_ACCESS_FS_READ ; +if (prot &PROT_EXEC ) + access |=LANDLOCK_ACCESS_FS_EXECUTE ; +return access; +}
[...]
Quoted 2 lines
However, I'm not sure this hook is useful for now. Indeed, the process still need to have a file descriptor open with the right accesses.
Yeah, agreed.
Mickaël Salaün
maintainerRe: [RFC PATCH v14 01/10] landlock: Add object and rule management
In reply to Hillf DantonQuoted 265 lines · Hillf Danton
On Mon, 24 Feb 2020 17:02:06 +0100 Mickaël Salaün
Quoted 263 lines
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 can't 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 this 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 object. 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.
Signed-off-by Mickaël Salaün <[email protected]>Cc Andy Lutomirski <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>---
Changes since v13:
* New dedicated implementation, removing the need for eBPF.Previous version:
https://lore.kernel.org/lkml/[email protected]/--- MAINTAINERS | 10 ++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 15 ++ security/landlock/Makefile | 3 + security/landlock/object.c | 339 +++++++++++++++++++++++++++++++++++++ security/landlock/object.h | 134 +++++++++++++++ 7 files changed, 504 insertions(+) create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/object.c create mode 100644 security/landlock/object.hMAINTAINERS +10
-0@@ -9360,6 +9360,16 @@ F: net/core/skmsg.c F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c +LANDLOCK SECURITY MODULE +M: Mickaël Salaün <[email protected]> +L: [email protected] +W: https://landlock.io +T: git https://github.com/landlock-lsm/linux.git +S: Supported +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@@ -12,6 +12,7 @@ subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o@@ -29,6 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file listssecurity/landlock/Kconfig new file +15
-0@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config SECURITY_LANDLOCK + bool "Landlock support" + depends on SECURITY + default n + help + This selects Landlock, a safe sandboxing mechanism. It enables to + restrict processes on the fly (i.e. enforce an access control policy), + which can complement seccomp-bpf. The security policy is a set of access + rights tied to an object, which could be a file, a socket or a process. + + See Documentation/security/landlock/ for further information. + + If you are unsure how to answer this question, answer N.security/landlock/Makefile new file +3
-0@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := object.osecurity/landlock/object.c new file +127
-0@@ -0,0 +1,339 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Object and rule management +* +* Copyright © 2016-2020 Mickaël Salaün <[email protected]> +* Copyright © 2018-2020 ANSSI +* +* Principles and constraints of the object and rule management: +* - Do not leak memory. +* - Try as much as possible to free a memory allocation as soon as it is +* unused. +* - Do not use global lock. +* - Do not charge processes other than the one requesting a Landlock +* operation. +*/ + +#include <linux/bug.h> +#include <linux/compiler.h> +#include <linux/compiler_types.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/fs.h> +#include <linux/kernel.h> +#include <linux/list.h> +#include <linux/rbtree.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/workqueue.h> + +#include "object.h" + +struct landlock_object *landlock_create_object ( +const enum landlock_object_type type,void *underlying_object) +{ +struct landlock_object *object; + +if (WARN_ON_ONCE (!underlying_object)) +return NULL ; + object =kzalloc (sizeof (*object),GFP_KERNEL ); +if (!object) +return NULL ; +refcount_set (&object->usage,1 ); +refcount_set (&object->cleaners,1 ); +spin_lock_init (&object->lock); +INIT_LIST_HEAD (&object->rules); + object->type = type; +WRITE_ONCE (object->underlying_object, underlying_object); +return object; +} + +struct landlock_object *landlock_get_object (struct landlock_object *object) + __acquires(object->usage) +{ +__acquire (object->usage); +/* +* If @object->usage equal 0, then it will be ignored by writers, and +* underlying_object->object may be replaced, but this is not an issue +* for release_object(). +*/ +if (object &&refcount_inc_not_zero (&object->usage)) { +/* +* It should not be possible to get a reference to an object if +* its underlying object is being terminated (e.g. with +* landlock_release_object()), because an object is only +* modifiable through such underlying object. This is not the +* case with landlock_get_object_cleaner(). +*/ +WARN_ON_ONCE (!READ_ONCE (object->underlying_object)); +return object; + } +return NULL ; +} + +static struct landlock_object *get_object_cleaner ( +struct landlock_object *object) + __acquires(object->cleaners) +{ +__acquire (object->cleaners); +if (object &&refcount_inc_not_zero (&object->cleaners)) +return object; +return NULL ; +} + +/* +* There is two cases when an object should be free and the reference to the +* underlying object should be put: +* - when the last rule tied to this object is removed, which is handled by +* landlock_put_rule() and then release_object(); +* - when the object is being terminated (e.g. no more reference to an inode), +* which is handled by landlock_put_object(). +*/ +static void put_object_free (struct landlock_object *object) + __releases(object->cleaners) +{ +__release (object->cleaners); +if (!refcount_dec_and_test (&object->cleaners)) +return ; +WARN_ON_ONCE (refcount_read (&object->usage)); +/* +* Ensures a safe use of @object in the RCU block from +* landlock_put_rule(). +*/ +kfree_rcu (object, rcu_free); +} + +/* +* Destroys a newly created and useless object. +*/ +void landlock_drop_object (struct landlock_object *object) +{ +if (WARN_ON_ONCE (!refcount_dec_and_test (&object->usage))) +return ; +__acquire (object->cleaners); +put_object_free (object); +} + +/* +* Puts the underlying object (e.g. inode) if it is the first request to +* release @object, without calling landlock_put_object(). +* +* Return true if this call effectively marks @object as released, false +* otherwise. +*/ +static bool release_object(struct landlock_object *object) + __releases(&object->lock)
Quoted 8 lines
security/landlock/object.c line 133
Quoted 6 lines
security/landlock/object.c new file +6
-0+{ +void *underlying_object; + +lockdep_assert_held (&object->lock); + + underlying_object =xchg (&object->underlying_object,NULL );A one-line comment looks needed for xchg.
Ok. This is to have a guarantee that the underlying_object (e.g. the inode pointer) is only used once. I'll add a comment.
Quoted 5 lines
security/landlock/object.c line 135
Quoted 3 lines
security/landlock/object.c new file +2
-0+spin_unlock (&object->lock); +might_sleep ();Have trouble working out what
might_sleepis put for.
Patch 5 adds a call to landlock_release_inode(underlying_object, object) (LANDLOCK_OBJECT_INODE case), which can sleep e.g., with a call to iput().
Quoted 34 lines
Quoted 34 lines
security/landlock/object.c new file +33
-0+if (!underlying_object) +return false; + +switch (object->type) { +case LANDLOCK_OBJECT_INODE : +break ; +default : +WARN_ON_ONCE (1 ); + } +return true; +} + +static void put_object_cleaner (struct landlock_object *object) + __releases(object->cleaners) +{ +/* Let's try an early lockless check. */ +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object)) { +/* +* Puts @object if there is no rule tied to it and the +* remaining user is the underlying object. This check is +* atomic because @object->rules and @object->underlying_object +* are protected by @object->lock. +*/ +spin_lock (&object->lock); +if (list_empty (&object->rules) && +READ_ONCE (object->underlying_object) && +refcount_dec_if_one (&object->usage)) { +/* +* Releases @object, in place of +* landlock_release_object(). +* +* @object is already empty, implying that all its
Quoted 8 lines
security/landlock/object.c line 174
Quoted 6 lines
security/landlock/object.c new file +6
-0+* previous rules are already disabled. +* +* Unbalance the @object->cleaners counter to reflect +* the underlying object release. +*/ +if (!WARN_ON_ONCE (!release_object (object))) {Two ! hurt more than help.
Well, it may not look nice but don't you think it is better than a WARN_ON_ONCE(1) in the if block?
Quoted 4 lines
security/landlock/object.c line 176
Quoted 2 lines
security/landlock/object.c new file +2
-0+__acquire (object->cleaners); +put_object_free (object);Why put object more than once?
I just replied to Jann about this subject. This is to "unbalance" the counter to potentially free it (if there is no more user). I explain it here:
https://lore.kernel.org/lkml/[email protected]/
Quoted 10 lines
Quoted 9 lines
security/landlock/object.c new file +8
-0+ } + }else { +spin_unlock (&object->lock); + } + } +put_object_free (object); +} +
kbuild test robot
build botRe: [RFC PATCH v14 07/10] arch: Wire up landlock() syscall
In reply to Mickaël Salaün[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…
Hi "Mickaël,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kselftest/next]
[also build test ERROR on linus/master v5.6-rc3 next-20200228] [cannot apply to security/next-testing linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Micka-l-Sala-n/Landlock-LSM/20200225-030126 base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next config: openrisc-simple_smp_defconfig (attached as .config) compiler: or1k-linux-gcc (GCC) 9.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=9.2.0 make.cross ARCH=openrisc
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
>> or1k-linux-ld: arch/openrisc/kernel/sys_call_table.o:(.data+0x6dc): undefined reference to `sys_landlock'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kbuild test robot
build botRe: [RFC PATCH v14 07/10] arch: Wire up landlock() syscall
In reply to Mickaël Salaün[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…
Hi "Mickaël,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kselftest/next]
[also build test ERROR on linus/master v5.6-rc3 next-20200228] [cannot apply to security/next-testing linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Micka-l-Sala-n/Landlock-LSM/20200225-030126 base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next config: nios2-3c120_defconfig (attached as .config) compiler: nios2-linux-gcc (GCC) 7.5.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=nios2
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
>> arch/nios2/kernel/syscall_table.o:(.data+0x6dc): undefined reference to `sys_landlock'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Randy Dunlap
reviewerRe: [RFC PATCH v14 10/10] landlock: Add user and kernel documentation
In reply to Mickaël SalaünHi,
Here are a few corrections for you to consider.
Quoted 35 lines · Mickaël Salaün
This documentation can be built with the Sphinx framework.
Another location might be more appropriate, though.
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Vincent Dagonneau <[email protected]>Cc Andy Lutomirski <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>---
Changes since v13:
* Rewrote the documentation according to the major revamp.Previous version:
https://lore.kernel.org/lkml/[email protected]/--- Documentation/security/index.rst | 1 + Documentation/security/landlock/index.rst | 18 ++ Documentation/security/landlock/kernel.rst | 44 ++++ Documentation/security/landlock/user.rst | 233 +++++++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 Documentation/security/landlock/index.rst create mode 100644 Documentation/security/landlock/kernel.rst create mode 100644 Documentation/security/landlock/user.rstDocumentation/security/landlock/index.rst new file +2
-0@@ -0,0 +1,18 @@ +========================================= +Landlock LSM: unprivileged access control
Documentation/security/landlock/index.rst line 12
Quoted 10 lines
Documentation/security/landlock/index.rst new file +10
-0+========================================= + +:Author: Mickaël Salaün + +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 empower any
empowers
Quoted 20 lines
Documentation/security/landlock/index.rst new file +6
-0+process, including unprivileged ones, to securely restrict themselves. + +.. toctree:: + + user + kernelDocumentation/security/landlock/kernel.rst new file +8
-0@@ -0,0 +1,44 @@ +============================== +Landlock: kernel documentation +============================== + +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
Documentation/security/landlock/kernel.rst line 18
Quoted 10 lines
Documentation/security/landlock/kernel.rst new file +10
-0+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 ensure that only more
ensures
Documentation/security/landlock/kernel.rst line 28
Quoted 10 lines
Documentation/security/landlock/kernel.rst new file +10
-0+constraints can be added. + + +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 kind of side-channel attacks (e.g. leak of security
kinds
Quoted 22 lines
Documentation/security/landlock/kernel.rst new file +16
-0+ 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. enforce a ruleset) shall + only impact the processes requesting them. + + +Landlock rulesets and domains +============================= + +A domain is a read-only ruleset tied to a set of subjects (i.e. tasks). A +domain can transition to a new one which is the intersection of the constraints +from the current and a new ruleset. The definition of a subject is implicit +for a task sandboxing itself, which makes the reasoning much easier and helps +avoid pitfalls.Documentation/security/landlock/user.rst new file +0
-0
Documentation/security/landlock/user.rst line 10
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0@@ -0,0 +1,233 @@ +================================= +Landlock: userspace documentation +================================= + +Landlock rules +============== + +A Landlock rule enables to describe 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 are aggregated in a ruleset, which can then
is
Documentation/security/landlock/user.rst line 11
Quoted 1 line
Documentation/security/landlock/user.rst new file +1
-0+restricts the thread enforcing it, and its future children.
restrict
Quoted 11 lines
Documentation/security/landlock/user.rst new file +11
-0+ + +Defining and enforcing a security policy +---------------------------------------- + +Before defining a security policy, an application should first probe for the +features supported by the running kernel, which is important to be compatible +with older kernels. This can be done thanks to the `landlock` syscall (cf. +:ref:`syscall`). + +.. code-block:: c
Documentation/security/landlock/user.rst line 32
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ + struct landlock_attr_features attr_features; + + if (landlock(LANDLOCK_CMD_GET_FEATURES, LANDLOCK_OPT_GET_FEATURES, + sizeof(attr_features), &attr_features)) { + perror("Failed to probe the Landlock supported features"); + return 1; + } + +Then, we need to create the ruleset that will contains our rules. For this
contain
Documentation/security/landlock/user.rst line 33
Quoted 1 line
Documentation/security/landlock/user.rst new file +1
-0+example, the ruleset will contains rules which only allow read actions, but
contain
Quoted 32 lines
Documentation/security/landlock/user.rst new file +32
-0+write actions will be denied. The ruleset then needs to handle both of these +kind of actions. To have a backward compatibility, these actions should be +ANDed with the supported ones. + +.. code-block:: c + + int ruleset_fd; + struct landlock_attr_ruleset ruleset = { + .handled_access_fs = + LANDLOCK_ACCESS_FS_READ | + LANDLOCK_ACCESS_FS_READDIR | + LANDLOCK_ACCESS_FS_EXECUTE | + LANDLOCK_ACCESS_FS_WRITE | + LANDLOCK_ACCESS_FS_TRUNCATE | + LANDLOCK_ACCESS_FS_CHMOD | + LANDLOCK_ACCESS_FS_CHOWN | + LANDLOCK_ACCESS_FS_CHGRP | + LANDLOCK_ACCESS_FS_LINK_TO | + LANDLOCK_ACCESS_FS_RENAME_FROM | + LANDLOCK_ACCESS_FS_RENAME_TO | + LANDLOCK_ACCESS_FS_RMDIR | + LANDLOCK_ACCESS_FS_UNLINK | + 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.handled_access_fs &= attr_features.access_fs;
Documentation/security/landlock/user.rst line 75
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ ruleset_fd = landlock(LANDLOCK_CMD_CREATE_RULESET, + LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); + 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 enable to read the +file hierarchy ``/usr``. Without other rule, write actions would then be
Without other rules,
or
Without another rule,
Quoted 51 lines
Documentation/security/landlock/user.rst new file +51
-0+denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the +``O_PATH`` flag and fill the &struct landlock_attr_path_beneath with this file +descriptor. + +.. code-block:: c + + int err; + struct landlock_attr_path_beneath path_beneath = { + .ruleset_fd = ruleset_fd, + .allowed_access = + LANDLOCK_ACCESS_FS_READ | + LANDLOCK_ACCESS_FS_READDIR | + LANDLOCK_ACCESS_FS_EXECUTE, + }; + + path_beneath.allowed_access &= attr_features.access_fs; + 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(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, + sizeof(path_beneath), &path_beneath); + 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 accesses featured in ``attr_features.access_fs`` to everything else +on 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 + + struct landlock_attr_enforce attr_enforce = { + .ruleset_fd = ruleset_fd, + };
Documentation/security/landlock/user.rst line 136
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ + if (landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, + sizeof(attr_enforce), &attr_enforce)) { + perror("Failed to enforce ruleset"); + close(ruleset_fd); + return 1; + } + close(ruleset_fd); + +If this last system call succeeds, the current thread is now restricted and
If this last landlock system call succeeds,
[because close() is the last system call]
Documentation/security/landlock/user.rst line 138
Quoted 2 lines
Documentation/security/landlock/user.rst new file +2
-0+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,
preferably: policy;
Documentation/security/landlock/user.rst line 142
Quoted 4 lines
Documentation/security/landlock/user.rst new file +4
-0+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. + +A full working code can be found in `samples/landlock/sandboxer.c`_.
Full working code
Documentation/security/landlock/user.rst line 151
Quoted 9 lines
Documentation/security/landlock/user.rst new file +9
-0+ + +Inheritance +----------- + +Every new thread resulting from a :manpage:`clone(2)` inherits Landlock program +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' thread may apply
process's
Documentation/security/landlock/user.rst line 154
Quoted 3 lines
Documentation/security/landlock/user.rst new file +3
-0+Landlock rules to itself, but they will not be automatically applied to other +sibling threads (unlike POSIX thread credential changes, cf. +:manpage:`nptl(7)`).
[snip]
thanks for the documentation.
Signature · 1 line
~Randy
Mickaël Salaün
maintainerRe: [RFC PATCH v14 10/10] landlock: Add user and kernel documentation
Quoted 303 lines · Randy Dunlap
Hi,
Here are a few corrections for you to consider.Quoted 35 lines · Mickaël Salaün
This documentation can be built with the Sphinx framework.
Another location might be more appropriate, though.
Signed-off-by Mickaël Salaün <[email protected]>Reviewed-by Vincent Dagonneau <[email protected]>Cc Andy Lutomirski <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>---
Changes since v13:
* Rewrote the documentation according to the major revamp.Previous version:
https://lore.kernel.org/lkml/[email protected]/--- Documentation/security/index.rst | 1 + Documentation/security/landlock/index.rst | 18 ++ Documentation/security/landlock/kernel.rst | 44 ++++ Documentation/security/landlock/user.rst | 233 +++++++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 Documentation/security/landlock/index.rst create mode 100644 Documentation/security/landlock/kernel.rst create mode 100644 Documentation/security/landlock/user.rstDocumentation/security/landlock/index.rst new file +2
-0@@ -0,0 +1,18 @@ +========================================= +Landlock LSM: unprivileged access controlDocumentation/security/landlock/index.rst line 12
Quoted 10 lines
Documentation/security/landlock/index.rst new file +10
-0+========================================= + +:Author: Mickaël Salaün + +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 empower anyempowers
Quoted 20 lines
Documentation/security/landlock/index.rst new file +6
-0+process, including unprivileged ones, to securely restrict themselves. + +.. toctree:: + + user + kernelDocumentation/security/landlock/kernel.rst new file +8
-0@@ -0,0 +1,44 @@ +============================== +Landlock: kernel documentation +============================== + +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 theDocumentation/security/landlock/kernel.rst line 18
Quoted 10 lines
Documentation/security/landlock/kernel.rst new file +10
-0+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 ensure that only moreensures
Documentation/security/landlock/kernel.rst line 28
Quoted 10 lines
Documentation/security/landlock/kernel.rst new file +10
-0+constraints can be added. + + +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 kind of side-channel attacks (e.g. leak of securitykinds
Quoted 22 lines
Documentation/security/landlock/kernel.rst new file +16
-0+ 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. enforce a ruleset) shall + only impact the processes requesting them. + + +Landlock rulesets and domains +============================= + +A domain is a read-only ruleset tied to a set of subjects (i.e. tasks). A +domain can transition to a new one which is the intersection of the constraints +from the current and a new ruleset. The definition of a subject is implicit +for a task sandboxing itself, which makes the reasoning much easier and helps +avoid pitfalls.Documentation/security/landlock/user.rst new file +0
-0Documentation/security/landlock/user.rst line 10
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0@@ -0,0 +1,233 @@ +================================= +Landlock: userspace documentation +================================= + +Landlock rules +============== + +A Landlock rule enables to describe 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 are aggregated in a ruleset, which can thenis
Documentation/security/landlock/user.rst line 11
Quoted 1 line
Documentation/security/landlock/user.rst new file +1
-0+restricts the thread enforcing it, and its future children.restrict
Quoted 11 lines
Documentation/security/landlock/user.rst new file +11
-0+ + +Defining and enforcing a security policy +---------------------------------------- + +Before defining a security policy, an application should first probe for the +features supported by the running kernel, which is important to be compatible +with older kernels. This can be done thanks to the `landlock` syscall (cf. +:ref:`syscall`). + +.. code-block:: cDocumentation/security/landlock/user.rst line 32
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ + struct landlock_attr_features attr_features; + + if (landlock(LANDLOCK_CMD_GET_FEATURES, LANDLOCK_OPT_GET_FEATURES, + sizeof(attr_features), &attr_features)) { + perror("Failed to probe the Landlock supported features"); + return 1; + } + +Then, we need to create the ruleset that will contains our rules. For thiscontain
Documentation/security/landlock/user.rst line 33
Quoted 1 line
Documentation/security/landlock/user.rst new file +1
-0+example, the ruleset will contains rules which only allow read actions, butcontain
Quoted 32 lines
Documentation/security/landlock/user.rst new file +32
-0+write actions will be denied. The ruleset then needs to handle both of these +kind of actions. To have a backward compatibility, these actions should be +ANDed with the supported ones. + +.. code-block:: c + + int ruleset_fd; + struct landlock_attr_ruleset ruleset = { + .handled_access_fs = + LANDLOCK_ACCESS_FS_READ | + LANDLOCK_ACCESS_FS_READDIR | + LANDLOCK_ACCESS_FS_EXECUTE | + LANDLOCK_ACCESS_FS_WRITE | + LANDLOCK_ACCESS_FS_TRUNCATE | + LANDLOCK_ACCESS_FS_CHMOD | + LANDLOCK_ACCESS_FS_CHOWN | + LANDLOCK_ACCESS_FS_CHGRP | + LANDLOCK_ACCESS_FS_LINK_TO | + LANDLOCK_ACCESS_FS_RENAME_FROM | + LANDLOCK_ACCESS_FS_RENAME_TO | + LANDLOCK_ACCESS_FS_RMDIR | + LANDLOCK_ACCESS_FS_UNLINK | + 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.handled_access_fs &= attr_features.access_fs;Documentation/security/landlock/user.rst line 75
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ ruleset_fd = landlock(LANDLOCK_CMD_CREATE_RULESET, + LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); + 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 enable to read the +file hierarchy ``/usr``. Without other rule, write actions would then beWithout other rules,
or
Without another rule,Quoted 51 lines
Documentation/security/landlock/user.rst new file +51
-0+denied by the ruleset. To add ``/usr`` to the ruleset, we open it with the +``O_PATH`` flag and fill the &struct landlock_attr_path_beneath with this file +descriptor. + +.. code-block:: c + + int err; + struct landlock_attr_path_beneath path_beneath = { + .ruleset_fd = ruleset_fd, + .allowed_access = + LANDLOCK_ACCESS_FS_READ | + LANDLOCK_ACCESS_FS_READDIR | + LANDLOCK_ACCESS_FS_EXECUTE, + }; + + path_beneath.allowed_access &= attr_features.access_fs; + 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(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, + sizeof(path_beneath), &path_beneath); + 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 accesses featured in ``attr_features.access_fs`` to everything else +on 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 + + struct landlock_attr_enforce attr_enforce = { + .ruleset_fd = ruleset_fd, + };Documentation/security/landlock/user.rst line 136
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ + if (landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, + sizeof(attr_enforce), &attr_enforce)) { + perror("Failed to enforce ruleset"); + close(ruleset_fd); + return 1; + } + close(ruleset_fd); + +If this last system call succeeds, the current thread is now restricted andIf this last landlock system call succeeds,
[because
close()is the last system call]Documentation/security/landlock/user.rst line 138
Quoted 2 lines
Documentation/security/landlock/user.rst new file +2
-0+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,preferably: policy;Documentation/security/landlock/user.rst line 142
Quoted 4 lines
Documentation/security/landlock/user.rst new file +4
-0+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. + +A full working code can be found in `samples/landlock/sandboxer.c`_.Full working code
Documentation/security/landlock/user.rst line 151
Quoted 9 lines
Documentation/security/landlock/user.rst new file +9
-0+ + +Inheritance +----------- + +Every new thread resulting from a :manpage:`clone(2)` inherits Landlock program +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' thread may applyprocess's
Quoted 8 lines
Documentation/security/landlock/user.rst line 154
Quoted 3 lines
Documentation/security/landlock/user.rst new file +3
-0+Landlock rules to itself, but they will not be automatically applied to other +sibling threads (unlike POSIX thread credential changes, cf. +:manpage:`nptl(7)`).[snip]
thanks for the documentation.
Done. Thanks for this attentive review!
Quoted 18 lines · Mickaël Salaün
This new version of Landlock is a major revamp of the previous series [1], hence the RFC tag. The three main changes are the replacement of eBPF with a dedicated safe management of access rules, the replacement of the use of seccomp(2) with a dedicated syscall, and the management of filesystem access-control (back from the v10).
As discussed in [2], eBPF may be too powerful and dangerous to be put in the hand of unprivileged and potentially malicious processes, especially because of side-channel attacks against access-controls or other parts of the kernel.
Thanks to this new implementation (1540 SLOC), designed from the ground to be used by unprivileged processes, this series enables a process to sandbox itself without requiring
CAP_SYS_ADMIN, but only theno_new_privsconstraint (like seccomp). Not relying on eBPF also enables to improve performances, especially for stacked security policies thanks to mergeable rulesets.
Quoted 8 lines
The compiled documentation is available here:
https://landlock.io/linux-doc/landlock-v14/security/landlock/index.htmlThis series can be applied on top of v5.6-rc3. This can be tested with
CONFIG_SECURITY_LANDLOCKandCONFIG_SAMPLE_LANDLOCK. This patch series can be found in a Git repository here:
https://github.com/landlock-lsm/linux/commits/landlock-v14
I would really appreciate constructive comments on the design and the code.
I've looked through the patchset, and I think that it would be possible to simplify it quite a bit. I have tried to do that (and compiled-tested it, but not actually tried running it); here's what I came up with:
https://github.com/thejh/linux/commits/landlock-mod
The three modified patches (patches 1, 2 and 5) are marked with "[MODIFIED]" in their title. Please take a look - what do you think? Feel free to integrate my changes into your patches if you think they make sense.
Apart from simplifying the code, I also found the following issues, which I have fixed in the modified patches:
put_hierarchy() has to drop a reference on its parent. (However, this must not recurse, so we have to do it with a loop.)
put_ruleset() is not in an RCU read-side critical section, so as soon as it calls kfree_rcu(), "freeme" might disappear; but "orig" is in "freeme", so when the loop tries to find the next element with rb_next(orig), that can be a UAF. rbtree_postorder_for_each_entry_safe() exists for dealing with such issues.
AFAIK the calls to rb_erase() in clean_ruleset() is not safe if someone is concurrently accessing the rbtree as an RCU reader, because concurrent rotations can prevent a lookup from succeeding. The simplest fix is probably to just make any rbtree that has been installed on a process immutable, and give up on the cleaning - arguably the memory wastage that can cause is pretty limited. (By the way, as a future optimization, we might want to turn the rbtree into a hashtable when installing it?)
The iput() in landlock_release_inode() looks unsafe - you need to guarantee that even if the deletion of a ruleset races with generic_shutdown_super(), every iput() for that superblock finishes before landlock_release_inodes() returns, even if the iput() is happening in the context of ruleset deletion. This is why fsnotify_unmount_inodes() has that wait_var_event() at the end.
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct landlock_attr_ruleset ruleset = { .handled_access_fs = ACCESS_FS_ROUGHLY_WRITE};
int ruleset_fd = landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); struct landlock_attr_path_beneath path_beneath = {
.ruleset_fd = ruleset_fd,
.allowed_access = ACCESS_FS_ROUGHLY_WRITE,
.parent_fd = open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath); prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
struct landlock_attr_enforce attr_enforce = { .ruleset_fd = ruleset_fd }; landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);
At this point, the process is not supposed to be able to write to anything outside /tmp/foobar, right? But what happens if the process does the following next?
struct landlock_attr_ruleset ruleset = { .handled_access_fs = ACCESS_FS_ROUGHLY_WRITE};
int ruleset_fd = landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); struct landlock_attr_path_beneath path_beneath = {
.ruleset_fd = ruleset_fd,
.allowed_access = ACCESS_FS_ROUGHLY_WRITE,
.parent_fd = open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath); prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
struct landlock_attr_enforce attr_enforce = { .ruleset_fd = ruleset_fd }; landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that LANDLOCK_CMD_ENFORCE_RULESET should only let you drop privileges, not increase them, right?
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let check_access_path() collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.
But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
- LANDLOCK_CMD_CREATE_RULESET gives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
- LANDLOCK_CMD_ADD_RULE fails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
- LANDLOCK_CMD_ENFORCE_RULESET fails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
Mickaël Salaün
maintainerQuoted 33 lines · Jann Horn
Quoted 18 lines · Mickaël Salaün
This new version of Landlock is a major revamp of the previous series [1], hence the RFC tag. The three main changes are the replacement of eBPF with a dedicated safe management of access rules, the replacement of the use of seccomp(2) with a dedicated syscall, and the management of filesystem access-control (back from the v10).
As discussed in [2], eBPF may be too powerful and dangerous to be put in the hand of unprivileged and potentially malicious processes, especially because of side-channel attacks against access-controls or other parts of the kernel.
Thanks to this new implementation (1540 SLOC), designed from the ground to be used by unprivileged processes, this series enables a process to sandbox itself without requiring
CAP_SYS_ADMIN, but only theno_new_privsconstraint (like seccomp). Not relying on eBPF also enables to improve performances, especially for stacked security policies thanks to mergeable rulesets.Quoted 8 lines
The compiled documentation is available here:
https://landlock.io/linux-doc/landlock-v14/security/landlock/index.htmlThis series can be applied on top of v5.6-rc3. This can be tested with
CONFIG_SECURITY_LANDLOCKandCONFIG_SAMPLE_LANDLOCK. This patch series can be found in a Git repository here:
https://github.com/landlock-lsm/linux/commits/landlock-v14
I would really appreciate constructive comments on the design and the code.I've looked through the patchset, and I think that it would be possible to simplify it quite a bit. I have tried to do that (and compiled-tested it, but not actually tried running it); here's what I came up with:
Quoted 6 lines
https://github.com/thejh/linux/commits/landlock-mod
The three modified patches (patches 1, 2 and 5) are marked with "[MODIFIED]" in their title. Please take a look - what do you think? Feel free to integrate my changes into your patches if you think they make sense.
Regarding the landlock_release_inodes(), the final wait_var_event() is indeed needed (as does fsnotify), but why do you use a READ_ONCE() for landlock_initialized?
I was reluctant to use function pointers but landlock_object_operations makes a cleaner and more generic interface to manage objects.
Your get_inode_object() is much simpler and easier to understand than the get_object() and get_cleaner().
The other main change is about the object cross-reference: you entirely removed it, which means that an object will only be free when there are no rules using it. This does not free an object when its underlying object is being terminated. We now only have to worry about the termination of the parent of an underlying object (e.g. the super-block of an inode).
However, I think you forgot to increment object->usage in create_ruleset_elem(). There is also an unused checked_mask variable in merge_ruleset().
All this removes optimizations that made the code more difficult to understand. The performance difference is negligible, and I think that the memory footprint is fine.
These optimizations (and others) could be discussed later. I'm integrating most of your changes in the next patch series.
Thank you very much for this review and the code.
Quoted 7 lines
Apart from simplifying the code, I also found the following issues, which I have fixed in the modified patches:
put_hierarchy()has to drop a reference on its parent. (However, this must not recurse, so we have to do it with a loop.)
Right, fixed.
Quoted 7 lines
put_ruleset()is not in an RCU read-side critical section, so as soon as it callskfree_rcu(), "freeme" might disappear; but "orig" is in "freeme", so when the loop tries to find the next element withrb_next(orig), that can be a UAF.rbtree_postorder_for_each_entry_safe()exists for dealing with such issues.
Good catch.
Quoted 7 lines
AFAIK the calls to
rb_erase()inclean_ruleset()is not safe if someone is concurrently accessing the rbtree as an RCU reader, because concurrent rotations can prevent a lookup from succeeding. The simplest fix is probably to just make any rbtree that has been installed on a process immutable, and give up on the cleaning - arguably the memory wastage that can cause is pretty limited.
Yes, let's go for immutable domains.
Quoted 3 lines
(By the
way, as a future optimization, we might want to turn the rbtree into a hashtable when installing it?)
Definitely. This was a previous (private) implementation I did for domains, but to simplify the code I reused the same type as a ruleset. A future evolution of Landlock could add back this optimization.
Quoted 7 lines
The
iput()inlandlock_release_inode()looks unsafe - you need to guarantee that even if the deletion of a ruleset races withgeneric_shutdown_super(), everyiput()for that superblock finishes beforelandlock_release_inodes()returns, even if theiput()is happening in the context of ruleset deletion. This is whyfsnotify_unmount_inodes()has thatwait_var_event()at the end.
Right, much better with that.
Quoted 43 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);
Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to LANDLOCK_ACCESS_FS_OPEN) thanks to a call to security_file_open(), but this is definitely not perfect.
Quoted 21 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.
Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
I would prefer to add the same checks you described at first (with check_access_path), but only when creating a new ruleset with merge_ruleset() (which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Because check_access_path() goes through one path, another (bind mounted) path could be illegitimately allowed.
That makes the problem a bit more complicated. A solution may be to keep track of the hierarchy of each rule (e.g. with a layer/depth number), and only allow an access request if at least a rule of each layer allow this access. In this case we also need to correctly handle the case when rules from different layers are tied to the same object.
I would like Landlock to have "pure" syscalls, in the sense that a process A (e.g. a daemon) could prepare a ruleset and sends its FD to a process B which would then be able to use it to sandbox itself. I think it makes the reasoning clearer not to have a given ruleset (FD) tied to a domain (i.e. parent ruleset) at first.
Landlock should (as much as possible) return an error if a syscall argument is invalid, not according to the current access control (which is not the case currently because of the security_file_open() check). This means that these additional merge_ruleset() checks should only affect the new domain/ruleset, but it should not be visible to userspace.
In a future evolution, it may be useful to add a lock/seal command to deny any additional rule enforcement. However that may be counter-productive because that enable application developers (e.g. for a shell) to deny the use of Landlock features to its child processes. But it would be possible anyway with seccomp-bpf…
Quoted 36 lines · Mickaël Salaün
Quoted 35 lines · Jann Horn
Quoted 18 lines · Mickaël Salaün
This new version of Landlock is a major revamp of the previous series [1], hence the RFC tag. The three main changes are the replacement of eBPF with a dedicated safe management of access rules, the replacement of the use of seccomp(2) with a dedicated syscall, and the management of filesystem access-control (back from the v10).
As discussed in [2], eBPF may be too powerful and dangerous to be put in the hand of unprivileged and potentially malicious processes, especially because of side-channel attacks against access-controls or other parts of the kernel.
Thanks to this new implementation (1540 SLOC), designed from the ground to be used by unprivileged processes, this series enables a process to sandbox itself without requiring
CAP_SYS_ADMIN, but only theno_new_privsconstraint (like seccomp). Not relying on eBPF also enables to improve performances, especially for stacked security policies thanks to mergeable rulesets.Quoted 8 lines
The compiled documentation is available here:
https://landlock.io/linux-doc/landlock-v14/security/landlock/index.htmlThis series can be applied on top of v5.6-rc3. This can be tested with
CONFIG_SECURITY_LANDLOCKandCONFIG_SAMPLE_LANDLOCK. This patch series can be found in a Git repository here:
https://github.com/landlock-lsm/linux/commits/landlock-v14
I would really appreciate constructive comments on the design and the code.I've looked through the patchset, and I think that it would be possible to simplify it quite a bit. I have tried to do that (and compiled-tested it, but not actually tried running it); here's what I came up with:
Quoted 8 lines
Quoted 4 lines
The three modified patches (patches 1, 2 and 5) are marked with "[MODIFIED]" in their title. Please take a look - what do you think? Feel free to integrate my changes into your patches if you think they make sense.
Regarding the
landlock_release_inodes(), the finalwait_var_event()is indeed needed (as does fsnotify), but why do you use aREAD_ONCE()forlandlock_initialized?
Ah, good point - that READ_ONCE() should be unnecessary.
Quoted 9 lines
The other main change is about the object cross-reference: you entirely removed it, which means that an object will only be free when there are no rules using it. This does not free an object when its underlying object is being terminated. We now only have to worry about the termination of the parent of an underlying object (e.g. the super-block of an inode).
However, I think you forgot to increment
object->usageincreate_ruleset_elem().
Whoops, you're right.
Quoted 1 line
There is also an unused
checked_maskvariable inmerge_ruleset().
Oh, yeah, oops.
Quoted 5 lines
All this removes optimizations that made the code more difficult to understand. The performance difference is negligible, and I think that the memory footprint is fine.
These optimizations (and others) could be discussed later. I'm integrating most of your changes in the next patch series.
:)
Quoted 46 lines
Quoted 41 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?
Quoted 4 lines
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to
LANDLOCK_ACCESS_FS_OPEN) thanks to a call tosecurity_file_open(), but this is definitely not perfect.
Ah, I missed that.
Quoted 29 lines
Quoted 20 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
Quoted 8 lines
I would prefer to add the same checks you described at first (with
check_access_path), but only when creating a new ruleset withmerge_ruleset()(which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Becausecheck_access_path()goes through one path, another (bind mounted) path could be illegitimately allowed.
Hmm... I'm not sure what you mean. At the moment, landlock doesn't allow any sandboxed process to change the mount hierarchy, right? Can you give an example where this would go wrong?
Quoted 5 lines
That makes the problem a bit more complicated. A solution may be to keep track of the hierarchy of each rule (e.g. with a layer/depth number), and only allow an access request if at least a rule of each layer allow this access. In this case we also need to correctly handle the case when rules from different layers are tied to the same object.
Al Viro
maintainerRe: [RFC PATCH v14 06/10] landlock: Add syscall implementation
In reply to Mickaël Salaünsecurity/landlock/syscall.c line 272
Quoted 8 lines · Mickaël Salaün
security/landlock/syscall.c new file +7
-0+static int get_path_from_fd(u64 fd, struct path *path) + /* + * Only allows O_PATH FD: enable to restrict ambiant (FS) accesses + * without requiring to open and risk leaking or misuing a FD. Accept + * removed, but still open directory (S_DEAD). + */ + if (!(f.file->f_mode & FMODE_PATH) || !f.file->f_path.mnt ||
^^^^^^^^^^^^^^^^^^^
Could you explain what that one had been be about? The underlined subexpression is always false; was that supposed to check some condition and if so, which one?
Mickaël Salaün
maintainerQuoted 3 lines · Jann Horn
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Mon, Feb 24, 2020 at 5:03 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 91 lines
Quoted 46 lines
Quoted 41 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?Quoted 4 lines
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to
LANDLOCK_ACCESS_FS_OPEN) thanks to a call tosecurity_file_open(), but this is definitely not perfect.Ah, I missed that.
Quoted 29 lines
Quoted 20 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
Quoted 8 lines
I would prefer to add the same checks you described at first (with
check_access_path), but only when creating a new ruleset withmerge_ruleset()(which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Becausecheck_access_path()goes through one path, another (bind mounted) path could be illegitimately allowed.
Quoted 3 lines
Hmm... I'm not sure what you mean. At the moment, landlock doesn't allow any sandboxed process to change the mount hierarchy, right? Can you give an example where this would go wrong?
Indeed, a Landlocked process must no be able to change its mount namespace layout. However, bind mounts may already exist. Let's say a process sandbox itself to only access /a in a read-write way. Then, this process (or one of its children) add a new restriction on /a/b to only be able to read this hierarchy. The check at insertion time would allow this because this access right is a subset of the access right allowed with the parent directory. However, If /a/b is bind mounted somewhere else, let's say in /private/b, then the second enforcement just gave new access rights to this hierarchy too. This is why it seems risky to rely on a check about the legitimacy of a new access right when adding it to a ruleset or when enforcing it.
Quoted 7 lines
Quoted 6 lines
That makes the problem a bit more complicated. A solution may be to keep track of the hierarchy of each rule (e.g. with a layer/depth number), and only allow an access request if at least a rule of each layer allow this access. In this case we also need to correctly handle the case when rules from different layers are tied to the same object.
Mickaël Salaün
maintainerRe: [RFC PATCH v14 06/10] landlock: Add syscall implementation
In reply to Al ViroQuoted 11 lines · Al Viro
security/landlock/syscall.c line 272
Quoted 8 lines · Mickaël Salaün
security/landlock/syscall.c new file +7
-0+static int get_path_from_fd(u64 fd, struct path *path) + /* + * Only allows O_PATH FD: enable to restrict ambiant (FS) accesses + * without requiring to open and risk leaking or misuing a FD. Accept + * removed, but still open directory (S_DEAD). + */ + if (!(f.file->f_mode & FMODE_PATH) || !f.file->f_path.mnt ||^^^^^^^^^^^^^^^^^^^
Quoted 4 lines
Could you explain what that one had been be about? The underlined subexpression is always false; was that supposed to check some condition and if so, which one?
This was just to be sure that the next assignment "path->mnt = f.file->f_path.mnt;" always creates a valid path. If this is always true, I will remove it.
Quoted 98 lines · Mickaël Salaün
Quoted 3 lines · Jann Horn
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Mon, Feb 24, 2020 at 5:03 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 91 lines
Quoted 46 lines
Quoted 41 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?Quoted 4 lines
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to
LANDLOCK_ACCESS_FS_OPEN) thanks to a call tosecurity_file_open(), but this is definitely not perfect.Ah, I missed that.
Quoted 37 lines
Quoted 20 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
I would prefer to add the same checks you described at first (with
check_access_path), but only when creating a new ruleset withmerge_ruleset()(which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Becausecheck_access_path()goes through one path, another (bind mounted) path could be illegitimately allowed.
Quoted 8 lines
Quoted 3 lines
Hmm... I'm not sure what you mean. At the moment, landlock doesn't allow any sandboxed process to change the mount hierarchy, right? Can you give an example where this would go wrong?
Indeed, a Landlocked process must no be able to change its mount namespace layout. However, bind mounts may already exist. Let's say a process sandbox itself to only access /a in a read-write way.
So, first policy:
/a RW
Quoted 2 lines
Then, this process (or one of its children) add a new restriction on /a/b to only be able to read this hierarchy.
You mean with the second policy looking like this?
/a RW
/a/b R
Then the resulting policy would be:
/a RW policy_bitmask=0x00000003 (bits 0 and 1 set) /a/b R policy_bitmask=0x00000002 (bit 1 set) required_bits=0x00000003 (bits 0 and 1 set)
Quoted 5 lines
The check at insertion
time would allow this because this access right is a subset of the access right allowed with the parent directory. However, If /a/b is bind mounted somewhere else, let's say in /private/b, then the second enforcement just gave new access rights to this hierarchy too.
But with the solution I proposed, landlock's path walk would see something like this when accessing a file at /private/b/foo:
/private/b/foo <no rules>
policies seen until now: 0x00000000
/private/b <access: R, policy_bitmask=0x00000002>
policies seen until now: 0x00000002
/private <no rules>
policies seen until now: 0x00000002
/ <no rules>
policies seen until now: 0x00000002
It wouldn't encounter any rule from the first policy, so the OR of the seen policy bitmasks would be 0x00000002, which is not the required value 0x00000003, and so the access would be denied.
Mickaël Salaün
maintainerQuoted 108 lines · Jann Horn
Quoted 98 lines · Mickaël Salaün
Quoted 3 lines · Jann Horn
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Mon, Feb 24, 2020 at 5:03 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 91 lines
Quoted 46 lines
Quoted 41 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?Quoted 4 lines
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to
LANDLOCK_ACCESS_FS_OPEN) thanks to a call tosecurity_file_open(), but this is definitely not perfect.Ah, I missed that.
Quoted 37 lines
Quoted 20 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
I would prefer to add the same checks you described at first (with
check_access_path), but only when creating a new ruleset withmerge_ruleset()(which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Becausecheck_access_path()goes through one path, another (bind mounted) path could be illegitimately allowed.Quoted 8 lines
Quoted 3 lines
Hmm... I'm not sure what you mean. At the moment, landlock doesn't allow any sandboxed process to change the mount hierarchy, right? Can you give an example where this would go wrong?
Indeed, a Landlocked process must no be able to change its mount namespace layout. However, bind mounts may already exist. Let's say a process sandbox itself to only access /a in a read-write way.
Quoted 8 lines
So, first policy:
/a RW
Quoted 2 lines
Then, this process (or one of its children) add a new restriction on /a/b to only be able to read this hierarchy.
You mean with the second policy looking like this?
Right.
Quoted 27 lines
/a RW
/a/b RThen the resulting policy would be:
/a RW policy_bitmask=0x00000003 (bits 0 and 1 set) /a/b R policy_bitmask=0x00000002 (bit 1 set) required_bits=0x00000003 (bits 0 and 1 set)
Quoted 5 lines
The check at insertion
time would allow this because this access right is a subset of the access right allowed with the parent directory. However, If /a/b is bind mounted somewhere else, let's say in /private/b, then the second enforcement just gave new access rights to this hierarchy too.But with the solution I proposed, landlock's path walk would see something like this when accessing a file at /private/b/foo:
/private/b/foo <no rules>
policies seen until now:0x00000000
/private/b <access: R, policy_bitmask=0x00000002>
policies seen until now:0x00000002
/private <no rules>
policies seen until now:0x00000002
/ <no rules>
policies seen until now:0x00000002
Quoted 3 lines
It wouldn't encounter any rule from the first policy, so the OR of the seen policy bitmasks would be
0x00000002, which is not the required value0x00000003, and so the access would be denied.
As I understand your proposition, we need to build the required_bits when adding a rule or enforcing/merging a ruleset with a domain. The issue is that a rule only refers to a struct inode, not a struct path. For your proposition to work, we would need to walk through the file path when adding a rule to a ruleset, which means that we need to depend of the current view of the process (i.e. its mount namespace), and its Landlock domain. If the required_bits field is set when the ruleset is merged with the domain, it is not possible anymore to walk through the corresponding initial file path, which makes the enforcement step too late to check for such consistency. The important point is that a ruleset/domain doesn't have a notion of file hierarchy, a ruleset is only a set of tagged inodes.
I'm not sure I got your proposition right, though. When and how would you generate the required_bits?
Here is my updated proposition: add a layer level and a depth to each rule (once enforced/merged with a domain), and a top layer level for a domain. When enforcing a ruleset (i.e. merging a ruleset into the current domain), the layer level of a new rule would be the incremented top layer level. If there is no rule (from this domain) tied to the same inode, then the depth of the new rule is 1. However, if there is already a rule tied to the same inode and if this rule's layer level is the previous top layer level, then the depth and the layer level are both incremented and the rule is updated with the new access rights (boolean AND).
The policy looks like this:
domain top_layer=2
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=2 depth=1
The path walk access check walks through all inodes and start with a layer counter equal to the top layer of the current domain. For each encountered inode tied to a rule, the access rights are checked and a new check ensures that the layer of the matching rule is the same as the counter (this may be a merged ruleset containing rules pertaining to the same hierarchy, which is fine) or equal to the decremented counter (i.e. the path walk just reached the underlying layer). If the path walk encounter a rule with a layer strictly less than the counter minus one, there is a whole in the layers which means that the ruleset hierarchy/subset does not match, and the access must be denied.
When accessing a file at /private/b/foo for a read access:
/private/b/foo <no rules>
allowed_access=unknown layer_counter=2
/private/b <access: R, policy_bitmask=0x00000002, layer=2, depth=1>
allowed_access=allowed layer_counter=2
/private <no rules>
allowed_access=allowed layer_counter=2
/ <no rules>
allowed_access=allowed layer_counter=2
Because the layer_counter didn't reach 1, the access request is then denied.
This proposition enables not to rely on a parent ruleset at first, only when enforcing/merging a ruleset with a domain. This also solves the issue with multiple inherited/nested rules on the same inode (in which case the depth just grows). Moreover, this enables to safely stop the path walk as soon as we reach the layer 1.
Here is a more complex example. A process sandbox itself with a first rule:
domain top_layer=1
/a RW policy_bitmask=0x00000003 layer=1 depth=1
Then the sandbox process enforces to itself this second (useless) ruleset:
/a/b RW policy_bitmask=0x00000003
The resulting domain is then:
domain top_layer=2
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b RW policy_bitmask=0x00000003 layer=2 depth=1
Then the sandbox process enforces to itself this third ruleset (which effectively reduces its access):
/a/b R policy_bitmask=0x00000002
The resulting domain is then:
domain top_layer=3
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=3 depth=2
At this time, only /a/b is accessible in a read way. The access rights on /a are ignored (but still inherited).
Then the sandbox process enforces to itself this fourth ruleset:
/c R policy_bitmask=0x00000002
The resulting domain is then:
domain top_layer=4
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=3 depth=2
/c R policy_bitmask=0x00000002 layer=4 depth=1
Now, every read or write access requests will be denied.
Then the sandbox process enforces to itself this fifth ruleset:
/a R policy_bitmask=0x00000002
Because /a is not in a contiguous underneath layer, the resulting domain is unchanged (except the top_layer which may be incremented anyway). Of course, we must check that the top_layer is not overflowing, in which case an error must be returned to inform userspace that the ruleset can't be enforced.
Quoted 150 lines · Mickaël Salaün
Quoted 108 lines · Jann Horn
Quoted 98 lines · Mickaël Salaün
Quoted 3 lines · Jann Horn
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Mon, Feb 24, 2020 at 5:03 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 91 lines
Quoted 46 lines
Quoted 41 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?Quoted 4 lines
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to
LANDLOCK_ACCESS_FS_OPEN) thanks to a call tosecurity_file_open(), but this is definitely not perfect.Ah, I missed that.
Quoted 37 lines
Quoted 20 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
I would prefer to add the same checks you described at first (with
check_access_path), but only when creating a new ruleset withmerge_ruleset()(which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Becausecheck_access_path()goes through one path, another (bind mounted) path could be illegitimately allowed.Quoted 8 lines
Quoted 3 lines
Hmm... I'm not sure what you mean. At the moment, landlock doesn't allow any sandboxed process to change the mount hierarchy, right? Can you give an example where this would go wrong?
Indeed, a Landlocked process must no be able to change its mount namespace layout. However, bind mounts may already exist. Let's say a process sandbox itself to only access /a in a read-write way.
Quoted 8 lines
So, first policy:
/a RW
Quoted 2 lines
Then, this process (or one of its children) add a new restriction on /a/b to only be able to read this hierarchy.
You mean with the second policy looking like this?
Right.
Quoted 27 lines
/a RW
/a/b RThen the resulting policy would be:
/a RW policy_bitmask=0x00000003 (bits 0 and 1 set) /a/b R policy_bitmask=0x00000002 (bit 1 set) required_bits=0x00000003 (bits 0 and 1 set)
Quoted 5 lines
The check at insertion
time would allow this because this access right is a subset of the access right allowed with the parent directory. However, If /a/b is bind mounted somewhere else, let's say in /private/b, then the second enforcement just gave new access rights to this hierarchy too.But with the solution I proposed, landlock's path walk would see something like this when accessing a file at /private/b/foo:
/private/b/foo <no rules>
policies seen until now:0x00000000
/private/b <access: R, policy_bitmask=0x00000002>
policies seen until now:0x00000002
/private <no rules>
policies seen until now:0x00000002
/ <no rules>
policies seen until now:0x00000002Quoted 3 lines
It wouldn't encounter any rule from the first policy, so the OR of the seen policy bitmasks would be
0x00000002, which is not the required value0x00000003, and so the access would be denied.
Quoted 7 lines
As I understand your proposition, we need to build the
required_bitswhen adding a rule or enforcing/merging a ruleset with a domain. The issue is that a rule only refers to a struct inode, not a struct path. For your proposition to work, we would need to walk through the file path when adding a rule to a ruleset, which means that we need to depend of the current view of the process (i.e. its mount namespace), and its Landlock domain.
I don't see why that is necessary. Why would we have to walk the file path when adding a rule?
Quoted 9 lines
If the
required_bitsfield is set when the ruleset is merged with the domain, it is not possible anymore to walk through the corresponding initial file path, which makes the enforcement step too late to check for such consistency. The important point is that a ruleset/domain doesn't have a notion of file hierarchy, a ruleset is only a set of tagged inodes.I'm not sure I got your proposition right, though. When and how would you generate the
required_bits?
Using your terminology:
A domain is a collection of N layers, which are assigned indices 0..N-1. For each possible access type, a domain has a bitmask containing N bits that stores which layers control that access type. (Basically a per-layer version of fs_access_mask.)
To validate an access, you start by ORing together the bitmasks for the requested access types; that gives you the required_bits mask, which lists all layers that want to control the access.
Then you set seen_policy_bits=0, then do the check_access_path_continue() loop while keeping track of which layers you've seen with "seen_policy_bits |= access->contributing_policies", or something like that.
And in the end, you check that seen_policy_bits is a superset of required_bits - something like (~seen_policy_bits) & required_bits == 0.
AFAICS to create a new domain from a bunch of layers, you wouldn't have to do any path walking.
Quoted 38 lines
Here is my updated proposition: add a layer level and a depth to each rule (once enforced/merged with a domain), and a top layer level for a domain. When enforcing a ruleset (i.e. merging a ruleset into the current domain), the layer level of a new rule would be the incremented top layer level.
If there is no rule (from this domain) tied to the same inode, then the depth of the new rule is 1. However, if there is already a rule tied to the same inode and if this rule's layer level is the previous top layer level, then the depth and the layer level are both incremented and the rule is updated with the new access rights (boolean AND).The policy looks like this:
domain top_layer=2
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=2 depth=1The path walk access check walks through all inodes and start with a layer counter equal to the top layer of the current domain. For each encountered inode tied to a rule, the access rights are checked and a new check ensures that the layer of the matching rule is the same as the counter (this may be a merged ruleset containing rules pertaining to the same hierarchy, which is fine) or equal to the decremented counter (i.e. the path walk just reached the underlying layer). If the path walk encounter a rule with a layer strictly less than the counter minus one, there is a whole in the layers which means that the ruleset hierarchy/subset does not match, and the access must be denied.
When accessing a file at /private/b/foo for a read access:
/private/b/foo <no rules>
allowed_access=unknown layer_counter=2
/private/b <access: R, policy_bitmask=0x00000002, layer=2, depth=1>
allowed_access=allowed layer_counter=2
/private <no rules>
allowed_access=allowed layer_counter=2
/ <no rules>
allowed_access=allowed layer_counter=2
Quoted 7 lines
Because the
layer_counterdidn't reach 1, the access request is then denied.This proposition enables not to rely on a parent ruleset at first, only when enforcing/merging a ruleset with a domain. This also solves the issue with multiple inherited/nested rules on the same inode (in which case the depth just grows). Moreover, this enables to safely stop the path walk as soon as we reach the layer 1.
(FWIW, you could do the same optimization with the seen_policy_bits approach.)
I guess the difference between your proposal and mine is that in my proposal, the following would work, in effect permitting W access to /foo/bar/baz (and nothing else)?
first ruleset:
/foo W
second ruleset:
/foo/bar/baz W
third ruleset:
/foo/bar W
whereas in your proposal, IIUC it wouldn't be valid for a new ruleset to whitelist a superset of what was whitelisted in a previous ruleset?
Mickaël Salaün
maintainerQuoted 169 lines · Jann Horn
Quoted 150 lines · Mickaël Salaün
Quoted 108 lines · Jann Horn
Quoted 98 lines · Mickaël Salaün
Quoted 3 lines · Jann Horn
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Mon, Feb 24, 2020 at 5:03 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 91 lines
Quoted 46 lines
Quoted 41 lines
Aside from those things, there is also a major correctness issue where I'm not sure how to solve it properly:
Let's say a process installs a filter on itself like this:
struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/tmp/foobar", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);At this point, the process is not supposed to be able to write to anything outside
/tmp/foobar, right? But what happens if the process does the following next?struct
landlock_attr_rulesetruleset = { .handled_access_fs =ACCESS_FS_ROUGHLY_WRITE};
intruleset_fd=landlock(LANDLOCK_CMD_CREATE_RULESET, LANDLOCK_OPT_CREATE_RULESET, sizeof(ruleset), &ruleset); structlandlock_attr_path_beneathpath_beneath= {
.ruleset_fd =ruleset_fd,
.allowed_access =ACCESS_FS_ROUGHLY_WRITE,
.parent_fd =open("/", O_PATH),
};landlock(LANDLOCK_CMD_ADD_RULE, LANDLOCK_OPT_ADD_RULE_PATH_BENEATH, sizeof(path_beneath), &path_beneath);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
structlandlock_attr_enforceattr_enforce= { .ruleset_fd =ruleset_fd};landlock(LANDLOCK_CMD_ENFORCE_RULESET, LANDLOCK_OPT_ENFORCE_RULESET, sizeof(attr_enforce), &attr_enforce);Quoted 4 lines
As far as I can tell from looking at the source, after this, you will have write access to the entire filesystem again. I think the idea is that
LANDLOCK_CMD_ENFORCE_RULESETshould only let you drop privileges, not increase them, right?Quoted 4 lines
There is an additionnal check in syscall.c:get_path_from_fd(): it is forbidden to add a rule with a path which is not accessible (according to
LANDLOCK_ACCESS_FS_OPEN) thanks to a call tosecurity_file_open(), but this is definitely not perfect.Ah, I missed that.
Quoted 37 lines
Quoted 20 lines
I think the easy way to fix this would be to add a bitmask to each rule that says from which ruleset it originally comes, and then let
check_access_path()collect these bitmasks from each rule with OR, and check at the end whether the resulting bitmask is full - if not, at least one of the rulesets did not permit the access, and it should be denied.But maybe it would make more sense to change how the API works instead, and get rid of the concept of "merging" two rulesets together? Instead, we could make the API work like this:
-
LANDLOCK_CMD_CREATE_RULESETgives you a file descriptor whose
->private_data contains a pointer to the old ruleset of the process, as well as a pointer to a new empty ruleset.
-LANDLOCK_CMD_ADD_RULEfails if the specified rule would not be
permitted by the old ruleset, then adds the rule to the new ruleset
-LANDLOCK_CMD_ENFORCE_RULESETfails if the old ruleset pointer in
->private_data doesn't match the current ruleset of the process, then replaces the old ruleset with the new ruleset.Quoted 8 lines
With this, the new ruleset is guaranteed to be a subset of the old ruleset because each of the new ruleset's rules is permitted by the old ruleset. (Unless the directory hierarchy rotates, but in that case the inaccuracy isn't much worse than what would've been possible through RCU path walk anyway AFAIK.)
What do you think?
I would prefer to add the same checks you described at first (with
check_access_path), but only when creating a new ruleset withmerge_ruleset()(which should probably be renamed). This enables not to rely on a parent ruleset/domain until the enforcement, which is the case anyway.
Unfortunately this doesn't work for some cases with bind mounts. Becausecheck_access_path()goes through one path, another (bind mounted) path could be illegitimately allowed.Quoted 8 lines
Quoted 3 lines
Hmm... I'm not sure what you mean. At the moment, landlock doesn't allow any sandboxed process to change the mount hierarchy, right? Can you give an example where this would go wrong?
Indeed, a Landlocked process must no be able to change its mount namespace layout. However, bind mounts may already exist. Let's say a process sandbox itself to only access /a in a read-write way.
Quoted 8 lines
So, first policy:
/a RW
Quoted 2 lines
Then, this process (or one of its children) add a new restriction on /a/b to only be able to read this hierarchy.
You mean with the second policy looking like this?
Right.
Quoted 27 lines
/a RW
/a/b RThen the resulting policy would be:
/a RW policy_bitmask=0x00000003 (bits 0 and 1 set) /a/b R policy_bitmask=0x00000002 (bit 1 set) required_bits=0x00000003 (bits 0 and 1 set)
Quoted 5 lines
The check at insertion
time would allow this because this access right is a subset of the access right allowed with the parent directory. However, If /a/b is bind mounted somewhere else, let's say in /private/b, then the second enforcement just gave new access rights to this hierarchy too.But with the solution I proposed, landlock's path walk would see something like this when accessing a file at /private/b/foo:
/private/b/foo <no rules>
policies seen until now:0x00000000
/private/b <access: R, policy_bitmask=0x00000002>
policies seen until now:0x00000002
/private <no rules>
policies seen until now:0x00000002
/ <no rules>
policies seen until now:0x00000002Quoted 3 lines
It wouldn't encounter any rule from the first policy, so the OR of the seen policy bitmasks would be
0x00000002, which is not the required value0x00000003, and so the access would be denied.Quoted 7 lines
As I understand your proposition, we need to build the
required_bitswhen adding a rule or enforcing/merging a ruleset with a domain. The issue is that a rule only refers to a struct inode, not a struct path. For your proposition to work, we would need to walk through the file path when adding a rule to a ruleset, which means that we need to depend of the current view of the process (i.e. its mount namespace), and its Landlock domain.I don't see why that is necessary. Why would we have to walk the file path when adding a rule?
Quoted 7 lines
If the
required_bitsfield is set when the ruleset is merged with the domain, it is not possible anymore to walk through the corresponding initial file path, which makes the enforcement step too late to check for such consistency. The important point is that a ruleset/domain doesn't have a notion of file hierarchy, a ruleset is only a set of tagged inodes.
Quoted 8 lines
Quoted 2 lines
I'm not sure I got your proposition right, though. When and how would you generate the
required_bits?Using your terminology:
A domain is a collection of N layers, which are assigned indices 0..N-1. For each possible access type, a domain has a bitmask containing N bits that stores which layers control that access type. (Basically a per-layer version offs_access_mask.)
OK, so there is a bit for each domain, which means that you get a limit of, let's say 64 layers? Knowing that each layer can be created by a standalone application, potentially nested in a bunch of layers, this seems artificially limiting.
Quoted 11 lines
To validate an access, you start by ORing together the bitmasks for the requested access types; that gives you the
required_bitsmask, which lists all layers that want to control the access. Then you set seen_policy_bits=0, then do thecheck_access_path_continue()loop while keeping track of which layers you've seen with "seen_policy_bits|=access->contributing_policies", or something like that.
And in the end, you check thatseen_policy_bitsis a superset ofrequired_bits- something like(~seen_policy_bits) & required_bits == 0.
Quoted 2 lines
AFAICS to create a new domain from a bunch of layers, you wouldn't have to do any path walking.
Right, I misunderstood your previous email.
Quoted 60 lines
Quoted 39 lines
Here is my updated proposition: add a layer level and a depth to each rule (once enforced/merged with a domain), and a top layer level for a domain. When enforcing a ruleset (i.e. merging a ruleset into the current domain), the layer level of a new rule would be the incremented top layer level.
If there is no rule (from this domain) tied to the same inode, then the depth of the new rule is 1. However, if there is already a rule tied to the same inode and if this rule's layer level is the previous top layer level, then the depth and the layer level are both incremented and the rule is updated with the new access rights (boolean AND).The policy looks like this:
domain top_layer=2
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=2 depth=1The path walk access check walks through all inodes and start with a layer counter equal to the top layer of the current domain. For each encountered inode tied to a rule, the access rights are checked and a new check ensures that the layer of the matching rule is the same as the counter (this may be a merged ruleset containing rules pertaining to the same hierarchy, which is fine) or equal to the decremented counter (i.e. the path walk just reached the underlying layer). If the path walk encounter a rule with a layer strictly less than the counter minus one, there is a whole in the layers which means that the ruleset hierarchy/subset does not match, and the access must be denied.
When accessing a file at /private/b/foo for a read access:
/private/b/foo <no rules>
allowed_access=unknown layer_counter=2
/private/b <access: R, policy_bitmask=0x00000002, layer=2, depth=1>
allowed_access=allowed layer_counter=2
/private <no rules>
allowed_access=allowed layer_counter=2
/ <no rules>
allowed_access=allowed layer_counter=2Quoted 7 lines
Because the
layer_counterdidn't reach 1, the access request is then denied.This proposition enables not to rely on a parent ruleset at first, only when enforcing/merging a ruleset with a domain. This also solves the issue with multiple inherited/nested rules on the same inode (in which case the depth just grows). Moreover, this enables to safely stop the path walk as soon as we reach the layer 1.
(FWIW, you could do the same optimization with the
seen_policy_bitsapproach.)I guess the difference between your proposal and mine is that in my proposal, the following would work, in effect permitting W access to /foo/bar/baz (and nothing else)?
first ruleset:
/foo W
second ruleset:
/foo/bar/baz W
third ruleset:
/foo/bar W
Quoted 3 lines
whereas in your proposal, IIUC it wouldn't be valid for a new ruleset to whitelist a superset of what was whitelisted in a previous ruleset?
This behavior seems dangerous because a process which sandbox itself to only access /foo/bar W can bypass the restrictions from one of its parent domains (i.e. only access /foo/bar/baz W). Indeed, each layer is (most of the time) a different and standalone security policy.
To sum up, the bitmask approach doesn't have the notion of layers ordering. It is then not possible to check that a rule comes from a domain which is the direct ancestor of a child's domain. I want each policy/layer to be really nested in the sense that a process sandboxing itself can only add more restriction to itself with regard to its parent domain (and the whole hierarchy). This is a similar approach to seccomp-bpf (with chained filters), except there is almost no overhead to nest several policies/layers together because they are flattened. Using the layer level and depth approach enables to implement this.
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Wed, Mar 18, 2020 at 1:06 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 27 lines
Quoted 18 lines
Quoted 7 lines
As I understand your proposition, we need to build the
required_bitswhen adding a rule or enforcing/merging a ruleset with a domain. The issue is that a rule only refers to a struct inode, not a struct path. For your proposition to work, we would need to walk through the file path when adding a rule to a ruleset, which means that we need to depend of the current view of the process (i.e. its mount namespace), and its Landlock domain.I don't see why that is necessary. Why would we have to walk the file path when adding a rule?
Quoted 7 lines
If the
required_bitsfield is set when the ruleset is merged with the domain, it is not possible anymore to walk through the corresponding initial file path, which makes the enforcement step too late to check for such consistency. The important point is that a ruleset/domain doesn't have a notion of file hierarchy, a ruleset is only a set of tagged inodes.Quoted 8 lines
Quoted 2 lines
I'm not sure I got your proposition right, though. When and how would you generate the
required_bits?Using your terminology:
A domain is a collection of N layers, which are assigned indices 0..N-1. For each possible access type, a domain has a bitmask containing N bits that stores which layers control that access type. (Basically a per-layer version offs_access_mask.)
Quoted 4 lines
OK, so there is a bit for each domain, which means that you get a limit of, let's say 64 layers? Knowing that each layer can be created by a standalone application, potentially nested in a bunch of layers, this seems artificially limiting.
Yes, that is a downside of my approach.
Quoted 76 lines
Quoted 11 lines
To validate an access, you start by ORing together the bitmasks for the requested access types; that gives you the
required_bitsmask, which lists all layers that want to control the access. Then you set seen_policy_bits=0, then do thecheck_access_path_continue()loop while keeping track of which layers you've seen with "seen_policy_bits|=access->contributing_policies", or something like that.
And in the end, you check thatseen_policy_bitsis a superset ofrequired_bits- something like(~seen_policy_bits) & required_bits == 0.Quoted 2 lines
AFAICS to create a new domain from a bunch of layers, you wouldn't have to do any path walking.
Right, I misunderstood your previous email.
Quoted 60 lines
Quoted 39 lines
Here is my updated proposition: add a layer level and a depth to each rule (once enforced/merged with a domain), and a top layer level for a domain. When enforcing a ruleset (i.e. merging a ruleset into the current domain), the layer level of a new rule would be the incremented top layer level.
If there is no rule (from this domain) tied to the same inode, then the depth of the new rule is 1. However, if there is already a rule tied to the same inode and if this rule's layer level is the previous top layer level, then the depth and the layer level are both incremented and the rule is updated with the new access rights (boolean AND).The policy looks like this:
domain top_layer=2
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=2 depth=1The path walk access check walks through all inodes and start with a layer counter equal to the top layer of the current domain. For each encountered inode tied to a rule, the access rights are checked and a new check ensures that the layer of the matching rule is the same as the counter (this may be a merged ruleset containing rules pertaining to the same hierarchy, which is fine) or equal to the decremented counter (i.e. the path walk just reached the underlying layer). If the path walk encounter a rule with a layer strictly less than the counter minus one, there is a whole in the layers which means that the ruleset hierarchy/subset does not match, and the access must be denied.
When accessing a file at /private/b/foo for a read access:
/private/b/foo <no rules>
allowed_access=unknown layer_counter=2
/private/b <access: R, policy_bitmask=0x00000002, layer=2, depth=1>
allowed_access=allowed layer_counter=2
/private <no rules>
allowed_access=allowed layer_counter=2
/ <no rules>
allowed_access=allowed layer_counter=2Quoted 7 lines
Because the
layer_counterdidn't reach 1, the access request is then denied.This proposition enables not to rely on a parent ruleset at first, only when enforcing/merging a ruleset with a domain. This also solves the issue with multiple inherited/nested rules on the same inode (in which case the depth just grows). Moreover, this enables to safely stop the path walk as soon as we reach the layer 1.
(FWIW, you could do the same optimization with the
seen_policy_bitsapproach.)I guess the difference between your proposal and mine is that in my proposal, the following would work, in effect permitting W access to /foo/bar/baz (and nothing else)?
first ruleset:
/foo W
second ruleset:
/foo/bar/baz W
third ruleset:
/foo/bar W
Quoted 8 lines
Quoted 3 lines
whereas in your proposal, IIUC it wouldn't be valid for a new ruleset to whitelist a superset of what was whitelisted in a previous ruleset?
This behavior seems dangerous because a process which sandbox itself to only access /foo/bar W can bypass the restrictions from one of its parent domains (i.e. only access /foo/bar/baz W). Indeed, each layer is (most of the time) a different and standalone security policy.
It isn't actually bypassing the restriction: You still can't actually access files like /foo/bar/blah, because a path walk from there doesn't encounter any rules from the second ruleset.
Quoted 9 lines
To sum up, the bitmask approach doesn't have the notion of layers ordering. It is then not possible to check that a rule comes from a domain which is the direct ancestor of a child's domain. I want each policy/layer to be really nested in the sense that a process sandboxing itself can only add more restriction to itself with regard to its parent domain (and the whole hierarchy). This is a similar approach to seccomp-bpf (with chained filters), except there is almost no overhead to nest several policies/layers together because they are flattened. Using the layer level and depth approach enables to implement this.
Mickaël Salaün
maintainerQuoted 118 lines · Jann Horn
Quoted 2 lines · Mickaël Salaün
Quoted 1 line · Jann Horn
On Wed, Mar 18, 2020 at 1:06 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 27 lines
Quoted 18 lines
Quoted 7 lines
As I understand your proposition, we need to build the
required_bitswhen adding a rule or enforcing/merging a ruleset with a domain. The issue is that a rule only refers to a struct inode, not a struct path. For your proposition to work, we would need to walk through the file path when adding a rule to a ruleset, which means that we need to depend of the current view of the process (i.e. its mount namespace), and its Landlock domain.I don't see why that is necessary. Why would we have to walk the file path when adding a rule?
Quoted 7 lines
If the
required_bitsfield is set when the ruleset is merged with the domain, it is not possible anymore to walk through the corresponding initial file path, which makes the enforcement step too late to check for such consistency. The important point is that a ruleset/domain doesn't have a notion of file hierarchy, a ruleset is only a set of tagged inodes.Quoted 8 lines
Quoted 2 lines
I'm not sure I got your proposition right, though. When and how would you generate the
required_bits?Using your terminology:
A domain is a collection of N layers, which are assigned indices 0..N-1. For each possible access type, a domain has a bitmask containing N bits that stores which layers control that access type. (Basically a per-layer version offs_access_mask.)Quoted 4 lines
OK, so there is a bit for each domain, which means that you get a limit of, let's say 64 layers? Knowing that each layer can be created by a standalone application, potentially nested in a bunch of layers, this seems artificially limiting.
Yes, that is a downside of my approach.
Quoted 80 lines
Quoted 11 lines
To validate an access, you start by ORing together the bitmasks for the requested access types; that gives you the
required_bitsmask, which lists all layers that want to control the access. Then you set seen_policy_bits=0, then do thecheck_access_path_continue()loop while keeping track of which layers you've seen with "seen_policy_bits|=access->contributing_policies", or something like that.
And in the end, you check thatseen_policy_bitsis a superset ofrequired_bits- something like(~seen_policy_bits) & required_bits == 0.Quoted 2 lines
AFAICS to create a new domain from a bunch of layers, you wouldn't have to do any path walking.
Right, I misunderstood your previous email.
Quoted 63 lines
Quoted 39 lines
Here is my updated proposition: add a layer level and a depth to each rule (once enforced/merged with a domain), and a top layer level for a domain. When enforcing a ruleset (i.e. merging a ruleset into the current domain), the layer level of a new rule would be the incremented top layer level.
If there is no rule (from this domain) tied to the same inode, then the depth of the new rule is 1. However, if there is already a rule tied to the same inode and if this rule's layer level is the previous top layer level, then the depth and the layer level are both incremented and the rule is updated with the new access rights (boolean AND).The policy looks like this:
domain top_layer=2
/a RW policy_bitmask=0x00000003 layer=1 depth=1
/a/b R policy_bitmask=0x00000002 layer=2 depth=1The path walk access check walks through all inodes and start with a layer counter equal to the top layer of the current domain. For each encountered inode tied to a rule, the access rights are checked and a new check ensures that the layer of the matching rule is the same as the counter (this may be a merged ruleset containing rules pertaining to the same hierarchy, which is fine) or equal to the decremented counter (i.e. the path walk just reached the underlying layer). If the path walk encounter a rule with a layer strictly less than the counter minus one, there is a whole in the layers which means that the ruleset hierarchy/subset does not match, and the access must be denied.
When accessing a file at /private/b/foo for a read access:
/private/b/foo <no rules>
allowed_access=unknown layer_counter=2
/private/b <access: R, policy_bitmask=0x00000002, layer=2, depth=1>
allowed_access=allowed layer_counter=2
/private <no rules>
allowed_access=allowed layer_counter=2
/ <no rules>
allowed_access=allowed layer_counter=2Quoted 7 lines
Because the
layer_counterdidn't reach 1, the access request is then denied.This proposition enables not to rely on a parent ruleset at first, only when enforcing/merging a ruleset with a domain. This also solves the issue with multiple inherited/nested rules on the same inode (in which case the depth just grows). Moreover, this enables to safely stop the path walk as soon as we reach the layer 1.
(FWIW, you could do the same optimization with the
seen_policy_bitsapproach.)I guess the difference between your proposal and mine is that in my proposal, the following would work, in effect permitting W access to /foo/bar/baz (and nothing else)?
first ruleset:
/foo W
second ruleset:
/foo/bar/baz W
third ruleset:
/foo/bar Wwhereas in your proposal, IIUC it wouldn't be valid for a new ruleset to whitelist a superset of what was whitelisted in a previous ruleset?
Quoted 8 lines
Quoted 4 lines
This behavior seems dangerous because a process which sandbox itself to only access /foo/bar W can bypass the restrictions from one of its parent domains (i.e. only access /foo/bar/baz W). Indeed, each layer is (most of the time) a different and standalone security policy.
It isn't actually bypassing the restriction: You still can't actually access files like /foo/bar/blah, because a path walk from there doesn't encounter any rules from the second ruleset.
Right, this use case is legitimate, e.g. first giving access to ~/Downloads and then another layer giving access to ~/ (because it doesn't know about the current restriction).
I think that neither my initial approach nor yours fit well, but I found a new one inspired from both approaches. The first solution I gave, and since implemented in the v15 [1], can manage 2^31-1 layers but it only works when refining a security policy *knowing the parent one* (i.e. refining an access tied to an inode, not a full file hierarchy). Instead of having a layer level and a layer depth, my new implementation (for v16) use a layer bitfield for each rule (and ruleset). We still AND access rights when merging rulesets, but instead of storing the last layer lever and depth, we set the corresponding bit in the layer bitfield of the rule. This way we don't consume more memory than the v15 implementation (for 64 layers top, which would be 64 bits * number of access types with your approach, i.e. between 1KB and 2KB) and Landlock can properly manage supersets of access rights in nested hierarchies, whatever their stacking order. However, I don't see another solution to better handle more than 64 layers than a VLA, but that could come later.
[1] https://lore.kernel.org/lkml/[email protected]/
Quoted 11 lines
Quoted 10 lines
To sum up, the bitmask approach doesn't have the notion of layers ordering. It is then not possible to check that a rule comes from a domain which is the direct ancestor of a child's domain. I want each policy/layer to be really nested in the sense that a process sandboxing itself can only add more restriction to itself with regard to its parent domain (and the whole hierarchy). This is a similar approach to seccomp-bpf (with chained filters), except there is almost no overhead to nest several policies/layers together because they are flattened. Using the layer level and depth approach enables to implement this.