Landlock LSM: Unprivileged sandboxing
Subsystems (16)
Mickaël Salaün
maintainerv3 cover Landlock LSM: Unprivileged sandboxing
Hi,
This series, improvement of the previous RFC [1], is a proof of concept to fill some missing part of seccomp as the ability to check syscall argument pointers or creating more dynamic security policies. The goal of this new stackable Linux Security Module (LSM) called Landlock is to allow any process, including unprivileged ones, to create powerful security sandboxes comparable to the Seatbelt/XNU Sandbox or the OpenBSD Pledge. This kind of sandbox help to mitigate the security impact of bugs or unexpected/malicious behaviors in userland applications.
# Landlock LSM
This series is mainly focused on cgroups while keeping the possibility to enforce access rules through seccomp. eBPF programs are used to create a security rule. They are very limited (i.e. can only call a whitelist of functions) and can not do a denial of service (i.e. no loop). A new dedicated eBPF map allows to collect and compare Landlock handles with system resources (e.g. files or network connections).
The approach taken is to add the minimum amount of code while still allowing the userland to create quite complex access rules. A dedicated security policy language such as used by SELinux, AppArmor and other major LSMs is a lot of code and dedicated to a trusted process (i.e. root/administrator).
# eBPF
To get an expressive language while still being safe and small, Landlock is based on eBPF. Landlock should be usable by untrusted processes and must then expose a minimal attack surface. The eBPF bytecode is minimal while powerful, widely used and thought to be used by not so trusted application. Reusing this code allows to not reproduce the same mistakes and minimize new code while still taking a generic approach. There is only some new features like a new kind of arraymap and few dedicated eBPF functions.
An eBPF program have access to an eBPF context which contains the LSM hook arguments (as does seccomp-bpf with syscall arguments). They can be used directly or passed to helper functions according to their types. It is then possible to do complex access checks without race conditions nor inconsistent evaluation (i.e. incorrect mirroring of the OS code and state [2]).
There is one eBPF program subtype per LSM hook. This allow to statically check which context access is performed by an eBPF program. This is needed to deny kernel address leak and ensure the right use of LSM hook arguments with eBPF functions. Moreover, this safe pointer handling remove the need for runtime check or abstract data, which improve performances. Any user can add multiple Landlock eBPF programs per LSM hook. They are stacked and evaluated one after the other (cf. seccomp-bpf).
# LSM hooks
Contrary to syscalls, LSM hooks are security checkpoints and are not architecture dependant. They are designed to match a security need reflected by a security policy (e.g. access to a file). Exposing parts of some LSM hooks instead of using the syscall API for sandboxing should help to avoid bugs and hacks as encountered by the first RFC. Instead of redoing the work of the LSM hooks through syscalls, we should use and expose them as does policies of access control LSM.
Only a subset of the hooks are meaningful for an unprivileged sandbox mechanism (e.g. file system or network access control). Landlock use an abstraction of raw LSM hooks, which allow to deal with possible future API changes of the LSM hook API. Moreover, thanks to the ePBF program typing (per LSM hook) used by Landlock, it should not be hard to make such evolutions backward compatible.
# Use case scenario
First, a process need to create a new dedicated eBPF map containing handles. This handles are references to system resources (e.g. file or directory) and grouped in one or multiple maps to be efficiently managed and checked in batches. This kind of map can be passed to Landlock eBPF functions to compare, for example, with a file access request. The handles are only accessible from the eBPF programs created by the same thread.
The loaded Landlock eBPF programs can be triggered by a seccomp filter returning RET_LANDLOCK. In addition, a cookie (16-bit value) can be passed from a seccomp filter to eBPF programs. This allow flexible security policies between seccomp and Landlock.
Another way to enforce a Landlock security policy is to attach Landlock programs to a dedicated cgroup. All the processes in this cgroup will then be subject to this policy. For unprivileged processes, this can be done thanks to cgroup delegation.
A triggered Landlock eBPF program can allows or deny an access, according to its subtype (i.e. LSM hook), thanks to errno return values.
# Sandbox example with process hierarchy sandboxing (seccomp)
$ ls /home
user1
$ LANDLOCK_ALLOWED='/bin:/lib:/usr:/tmp:/proc/self/fd/0' \
./samples/landlock/sandbox /bin/sh -i
Launching a new sandboxed process.
$ ls /home
ls: cannot open directory '/home': Permission denied# Sandbox example with conditional access control depending on a cgroup
$ mkdir /sys/fs/cgroup/sandboxed
$ ls /home
user1
$ LANDLOCK_CGROUPS='/sys/fs/cgroup/sandboxed' \
LANDLOCK_ALLOWED='/bin:/lib:/usr:/tmp:/proc/self/fd/0' \
./samples/landlock/sandbox
Ready to sandbox with cgroups.
$ ls /home
user1
$ echo $$ > /sys/fs/cgroup/sandboxed/cgroup.procs
$ ls /home
ls: cannot open directory '/home': Permission denied# Current limitations and possible improvements
For now, eBPF programs can only return an errno code. It may be interesting to be able to do other actions like seccomp-filter does (e.g. kill process). Such features can easily be implemented but the main advantage of the current approach is to be able to only execute eBPF programs until one return an errno code instead of executing all programs like seccomp-filter does.
It is quite easy to add new eBPF functions to extend Landlock. The main concern should be about the ability to leak information from the current process to another one (e.g. through maps) to not reproduce the same security sensitive behavior as ptrace.
This design does not seems too intrusive but is flexible enough to allow a powerful sandbox mechanism accessible by any process on Linux. The use of seccomp and Landlock is more suitable with the help of a userland library (e.g. libseccomp) that could help to specify a high-level language to express a security policy instead of raw eBPF programs. Moreover, thanks to LLVM, it is possible to express an eBPF program with a subset of C.
# FAQ
## Why not use a policy language like used by SElinux or AppArmor?
This kind of LSMs are dedicated to administrators. They already manage the system and are not a threat to the system security. However, seccomp, and Landlock too, should be available to anyone, which potentially include untrusted users and processes. To reduce the attack surface, Landlock should expose the minimum amount of code, hence minimal complexity. Moreover, another threat is to make accessible to a malicious code a new way to gain more information. For example, Landlock features should not allow a program to get the file owner if the directory containing this file is not readable. This data could then be exfiltrated thanks to the access result. Thus, we should limit the expressiveness of the available checks. The current approach is to do the checks in such a way that only a comparison with an already accessed resource (e.g. file descriptor) is possible. This allow to have a reference to compare with, without exposing much information.
## Why a new LSM? Does SELinux, AppArmor, Smack or Tomoyo are not good enough?
The current access control LSMs are fine for their purpose which is to give the *root* the ability to enforce a security policy for the *system*. What is missing is a way to enforce a security policy for any applications by its developer and *unprivileged user* as seccomp can do for raw syscall filtering. Moreover, Landlock handles stacked hook programs from different users. It must then ensure there is no possible malicious interactions between this programs.
Difference with other (access control) LSMs:
* not only dedicated to administrators (i.e. no_new_priv);
* limited kernel attack surface (e.g. policy parsing);
* helpers to compare complex objects (path/FD), no access to internal kernel data (do not leak addresses);
* constraint policy rules/programs (no DoS: deterministic execution time);
* do not leak more information than the loader process can legitimately have access to (minimize metadata inference): must compare from an already allowed file (through a handle).
## Why does seccomp-filter is not enough?
A seccomp filter can access to raw syscall arguments which means that it is not possible to filter according to pointed data as a file path. As demonstrated the first version of this patch series, filtering at the syscall level is complicated (e.g. need to take care of race conditions). This is mainly because the access control checkpoints of the kernel are not at this high-level but more underneath, at LSM hooks level. The LSM hooks are designed to handle this kind of checks. This series use this approach to leverage the ability of unprivileged users to limit themselves.
Cf. "What it isn't?" in Documentation/prctl/seccomp_filter.txt
## As a developer, why do I need this feature?
Landlock's goal is to help userland to limit its attack surface. Security-conscious developers would like to protect users from a security bug in their applications and the third-party dependencies they are using. Such a bug can compromise all the user data and help an attacker to perform a privilege escalation. Using an *unprivileged sandbox* feature such as Landlock empower the developer with the ability to properly compartmentalize its software and limit the impact of being compromised.
## As a user, why do I need a this feature?
Any user can already use seccomp-filter to whitelist a set of syscalls to reduce the kernel attack surface for a set of processes. However an unprivileged user can't create a security policy as the root user can thanks to SELinux and other access control LSMs. Landlock allows any unprivileged user to protect their data from being accessed by any process they run but only an identified subset. User tools can be created to help create such a high-level access control policy. This policy may not be powerful enough to express the same policies as the current access control LSMs, because of the threat an unprivileged user can be to the system, but it should be enough for most use-cases (e.g. blacklist or whitelist a set of file hierarchies).
## Does Landlock can limit network access or other resources?
Limiting network access is obviously in the scope of Landlock but it is not yet implemented. The main goal now is to get feedback about the whole concept, the API and the file access control part. More access control types could be implemented in the future.
## Why using the seccomp(2) syscall?
Landlock use the same semantic as seccomp to apply access rule restrictions. It add a new layer of security for the current process which is inherited by its childs. It make sense to use an unique access-restricting syscall (that should be allowed by seccomp-filter rules) which can only drop privileges. Moreover, a Landlock eBPF program could come from outside a process (e.g. passed through a UNIX socket). It is then useful to differentiate the creation/load of Landlock eBPF programs via bpf(2), from rule enforcing via seccomp(2).
## Why using cgroups?
cgroups are designed to handle groups of processes. One use case is to manage containers. Sandboxing based on process hierarchy (seccomp) is design to handle immutable security policies, which is a good security property but does not match all use cases. A user can attach Landlock rules to a cgroup. Doing so, all the processes in that cgroup will be subject to the security policy. However, if the user is allowed to manage this cgroup, it could dynamically move this group of processes to a cgroup with another security policy (or none). Landlock rules can be applied either on a process hierarchy (e.g. application with built-in sandboxing) or a group of processes (e.g. container sandboxing). Both approaches can be combined for the same process.
# Changes since RFC v2
* revamp cgroup handling:
* use Daniel Mack's patches "Add eBPF hooks for cgroups" v5 [3]
* remove bpf_landlock_cmp_cgroup_beneath()
* make BPF_PROG_ATTACH usable with delegated cgroups
* add a new CGRP_NO_NEW_PRIVS flag for safe cgroups
* handle Landlock sandboxing for cgroups hierarchy
* allow unprivileged processes to attach Landlock eBPF program to cgroups
* add subtype to eBPF programs:
* replace Landlock hook identification by custom eBPF program types with a dedicated subtype field
* manage fine-grained privileged Landlock programs
* register Landlock programs for dedicated trigger origins (e.g. syscall, return from seccomp filter and/or interruption)
* performance and memory optimizations: use an array to access Landlock hooks directly but do not duplicated it for each thread (seccomp-based)
* allow running Landlock programs without seccomp filter
* fix seccomp-related issues
* remove extra errno bounding check for Landlock programs
* add some examples for optional eBPF functions or context access (network related) according to security checks to allow more features for privileged programs (e.g. Checmate)
# Changes since RFC v1
* focus on the LSM hooks, not the syscalls:
* much more simple implementation
* does not need audit cache tricks to avoid race conditions
* more simple to use and more generic because using the LSM hook abstraction directly
* more efficient because only checking in LSM hooks
* architecture agnostic
* switch from cBPF to eBPF:
* new eBPF program types dedicated to Landlock
* custom functions used by the eBPF program
* gain some new features (e.g. 10 registers, can load values of different size, LLVM translator) but only a few functions allowed and a dedicated map type
* new context: LSM hook ID, cookie and LSM hook arguments
* need to set the sysctl kernel.unprivileged_bpf_disable to 0 (default value) to be able to load hook filters as unprivileged users
* smaller and simpler:
* no more checker groups but dedicated arraymap of handles
* simpler userland structs thanks to eBPF functions
* distinctive name: Landlock
This series can be applied on top of the Daniel Mack's patches for BPF_PROG_ATTACH v5 [3] on Linux next-20160913 (commit 562d4a2d7fa26b11d995f418951f3396a5d0f550). This can be
tested with CONFIG_SECURITY_LANDLOCK, CONFIG_SECCOMP_FILTER and CONFIG_CGROUP_BPF. I would really appreciate constructive comments on the usability, architecture, code and userland API of Landlock LSM.
[1] https://lkml.kernel.org/r/[email protected]
[2] https://crypto.stanford.edu/cs155/papers/traps.pdf
[3] https://lkml.kernel.org/r/[email protected]
Regards,
Mickaël Salaün (22):
landlock: Add Kconfig
bpf: Move u64_to_ptr() to BPF headers and inline it bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
bpf: Set register type according to is_valid_access() bpf,landlock: Add eBPF program subtype and is_valid_subtype() verifier
landlock: Add LSM hooks
landlock: Handle file comparisons
seccomp: Fix documentation for struct seccomp_filter
seccomp: Move struct seccomp_filter in seccomp.h
seccomp: Split put_seccomp_filter() with put_seccomp() seccomp,landlock: Handle Landlock hooks per process hierarchy
bpf: Cosmetic change for bpf_prog_attach()
bpf/cgroup: Replace struct bpf_prog with union bpf_object bpf/cgroup: Make cgroup_bpf_update() return an error code bpf/cgroup: Move capability check
bpf/cgroup,landlock: Handle Landlock hooks per cgroup
cgroup: Add access check for cgroup_get_from_fd() cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
landlock: Add interrupted origin
landlock: Add update and debug access flags
bpf,landlock: Add optional skb pointer in the Landlock context samples/landlock: Add sandbox example
include/linux/bpf-cgroup.h | 19 +- include/linux/bpf.h | 45 +++- include/linux/cgroup-defs.h | 9 + include/linux/cgroup.h | 2 +- include/linux/filter.h | 1 + include/linux/landlock.h | 86 ++++++++ include/linux/lsm_hooks.h | 5 + include/linux/seccomp.h | 58 +++++- include/uapi/linux/bpf.h | 122 +++++++++++ include/uapi/linux/seccomp.h | 2 + kernel/bpf/arraymap.c | 226 ++++++++++++++++++++- kernel/bpf/cgroup.c | 78 ++++--- kernel/bpf/syscall.c | 78 ++++--- kernel/bpf/verifier.c | 47 ++++- kernel/cgroup.c | 66 +++++- kernel/fork.c | 25 ++- kernel/seccomp.c | 107 +++++++--- kernel/trace/bpf_trace.c | 12 +- net/core/filter.c | 21 +- samples/Makefile | 2 +- samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 16 ++ samples/landlock/sandbox.c | 307 ++++++++++++++++++++++++++++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 23 +++ security/landlock/Makefile | 3 + security/landlock/checker_fs.c | 179 ++++++++++++++++ security/landlock/checker_fs.h | 20 ++ security/landlock/common.h | 27 +++ security/landlock/lsm.c | 451 +++++++++++++++++++++++++++++++++++++++++ security/landlock/manager.c | 281 +++++++++++++++++++++++++ security/security.c | 1 + 33 files changed, 2194 insertions(+), 129 deletions(-) create mode 100644 include/linux/landlock.h create mode 100644 samples/landlock/.gitignore create mode 100644 samples/landlock/Makefile create mode 100644 samples/landlock/sandbox.c create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/checker_fs.c create mode 100644 security/landlock/checker_fs.h create mode 100644 security/landlock/common.h create mode 100644 security/landlock/lsm.c create mode 100644 security/landlock/manager.c
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 1/22 landlock: Add Kconfig
Initial Landlock Kconfig needed to split the Landlock eBPF and seccomp parts to ease the review.
Changes from v2:
* add seccomp filter or cgroups (with eBPF programs attached support) dependencies
--- security/Kconfig | 1 + security/landlock/Kconfig | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 security/landlock/Kconfig
security/Kconfig +1 -0
@@ -164,6 +164,7 @@ source security/tomoyo/Kconfig source security/apparmor/Kconfig source security/loadpin/Kconfig source security/yama/Kconfig +source security/landlock/Kconfig source security/integrity/Kconfig
security/landlock/Kconfig new file +23 -0
@@ -0,0 +1,23 @@ +config SECURITY_LANDLOCK + bool "Landlock sandbox support" + depends on SECURITY + depends on BPF_SYSCALL + depends on SECCOMP_FILTER || CGROUP_BPF + default y + help + Landlock is a stacked LSM which allows any user to load a security + policy to restrict their processes (i.e. create a sandbox). The + policy is a list of stacked eBPF programs for some LSM hooks. Each + program can do some access comparison to check if an access request + is legitimate. + + You need to enable seccomp filter and/or cgroups (with eBPF programs + attached support) to apply a security policy to either a process + hierarchy (e.g. application with built-in sandboxing) or a group of + processes (e.g. container sandboxing). It is recommended to enable + both seccomp filter and cgroups. + + Further information about eBPF can be found in + Documentation/networking/filter.txt + + If you are unsure how to answer this question, answer Y.
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 2/22 bpf: Move u64_to_ptr() to BPF headers and inline it
In reply to Mickaël SalaünThis helper will be useful for arraymap (next commit).
--- include/linux/bpf.h | 6 ++++++ kernel/bpf/syscall.c | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/bpf.h +6 -0
@@ -274,6 +274,12 @@ static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)/* verify correctness of eBPF program */ int bpf_check (struct bpf_prog **fp,union bpf_attr *attr); + +/* helper to convert user pointers passed inside __aligned_u64 fields */ +static inline void __user *u64_to_ptr (__u64 val) +{ +return (void __user *) (unsigned long ) val; +}#else static inline void bpf_register_prog_type (struct bpf_prog_type_list *tl) {
kernel/bpf/syscall.c +0 -6
@@ -252,12 +252,6 @@ struct bpf_map *bpf_map_get_with_uref(u32 ufd)return map; } -/* helper to convert user pointers passed inside __aligned_u64 fields */ -static void __user *u64_to_ptr (__u64 val) -{ -return (void __user *) (unsigned long ) val; -} -int __weak bpf_stackmap_copy (struct bpf_map *map,void *key,void *value) {return -ENOTSUPP ;
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 3/22 bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
In reply to Mickaël SalaünThis new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead of CONST_PTR_TO_MAP (e.g. CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.
This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum bpf_map_handle_type list low-level types (e.g. BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enum bpf_map_array_type (e.g. BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).
For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checks
Changes since v1:
* arraymap of handles replace custom checker groups
* simpler userland API
--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)
include/linux/bpf.h +14 -0
@@ -13,6 +13,10 @@#include <linux/percpu.h> #include <linux/err.h> +#ifdef CONFIG_SECURITY_LANDLOCK +#include <linux/fs.h> /* struct file */ +#endif /* CONFIG_SECURITY_LANDLOCK */ +struct perf_event ;struct bpf_map ;
@@ -38,6 +42,7 @@ struct bpf_map_ops {struct bpf_map {atomic_t refcnt;enum bpf_map_type map_type; +enum bpf_map_array_type map_array_type;u32 key_size;u32 value_size;u32 max_entries;
@@ -187,6 +192,9 @@ struct bpf_array {*/ enum bpf_prog_type owner_prog_type;bool owner_jited; +#ifdef CONFIG_SECURITY_LANDLOCK +u32 n_entries;/* number of entries in a handle array */ +#endif /* CONFIG_SECURITY_LANDLOCK */ union {char value[0 ]__aligned(8) ;void *ptrs[0 ]__aligned(8) ;
@@ -194,6 +202,12 @@ struct bpf_array { }; }; +#ifdef CONFIG_SECURITY_LANDLOCK +struct map_landlock_handle { +u32 type;/* enum bpf_map_handle_type */ +}; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#define MAX_TAIL_CALL_CNT 32struct bpf_event_entry {
include/uapi/linux/bpf.h +18 -0
@@ -87,6 +87,15 @@ enum bpf_map_type {BPF_MAP_TYPE_PERCPU_ARRAY ,BPF_MAP_TYPE_STACK_TRACE ,BPF_MAP_TYPE_CGROUP_ARRAY , + BPF_MAP_TYPE_LANDLOCK_ARRAY, +}; + +enum bpf_map_array_type { +BPF_MAP_ARRAY_TYPE_UNSPEC , +}; + +enum bpf_map_handle_type { +BPF_MAP_HANDLE_TYPE_UNSPEC , }; enum bpf_prog_type {
@@ -510,4 +519,13 @@ struct xdp_md { __u32 data_end; }; +/* Map handle entry */ +struct landlock_handle { +__u32 type;/* enum bpf_map_handle_type */ +union { +__u32 fd; +__aligned_u64 glob; + }; +} __attribute__((aligned (8 ))); +#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/arraymap.c +203 -0
212 lines not on this page · raw message
kernel/bpf/verifier.c +11 -1
@@ -1793,6 +1793,15 @@ static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)return (struct bpf_map *) (unsigned long ) imm64; } +static inline enum bpf_reg_type bpf_reg_type_from_map (struct bpf_map *map) +{ +switch (map->map_array_type) { +case BPF_MAP_ARRAY_TYPE_UNSPEC : +default : +return CONST_PTR_TO_MAP ; + } +} +/* verify BPF_LD_IMM64 instruction */ static int check_ld_imm (struct verifier_env *env,struct bpf_insn *insn) {
@@ -1819,8 +1828,9 @@ static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn)/* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ BUG_ON (insn->src_reg !=BPF_PSEUDO_MAP_FD ); - regs[insn->dst_reg].type =CONST_PTR_TO_MAP ; regs[insn->dst_reg].map_ptr =ld_imm64_to_map_ptr (insn); + regs[insn->dst_reg].type = +bpf_reg_type_from_map (regs[insn->dst_reg].map_ptr);return 0 ; }
Signature · 1 line
2.9.3
Re: [RFC v3 03/22] bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
Quoted 99 lines · Mickaël Salaün
This new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead ofCONST_PTR_TO_MAP(e.g.CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum
bpf_map_handle_typelist low-level types (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enumbpf_map_array_type(e.g.BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checksChanges since v1:
* arraymap of handles replace custom checker groups
* simpler userland APISigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)include/linux/bpf.h +14
-0@@ -13,6 +13,10 @@#include <linux/percpu.h> #include <linux/err.h> +#ifdef CONFIG_SECURITY_LANDLOCK +#include <linux/fs.h> /* struct file */ +#endif /* CONFIG_SECURITY_LANDLOCK */ +struct perf_event ;struct bpf_map ;@@ -38,6 +42,7 @@ struct bpf_map_ops {struct bpf_map {atomic_t refcnt;enum bpf_map_type map_type; +enum bpf_map_array_type map_array_type;u32 key_size;u32 value_size;u32 max_entries;@@ -187,6 +192,9 @@ struct bpf_array {*/ enum bpf_prog_type owner_prog_type;bool owner_jited; +#ifdef CONFIG_SECURITY_LANDLOCK +u32 n_entries;/* number of entries in a handle array */ +#endif /* CONFIG_SECURITY_LANDLOCK */ union {char value[0 ]__aligned(8) ;void *ptrs[0 ]__aligned(8) ;@@ -194,6 +202,12 @@ struct bpf_array { }; }; +#ifdef CONFIG_SECURITY_LANDLOCK +struct map_landlock_handle { +u32 type;/* enum bpf_map_handle_type */ +}; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#define MAX_TAIL_CALL_CNT 32struct bpf_event_entry {include/uapi/linux/bpf.h +0
-0@@ -87,6 +87,15 @@ enum bpf_map_type {BPF_MAP_TYPE_PERCPU_ARRAY , BPF_MAP_TYPE_STACK_TRACE,P_TYPE_CGROUP_ARRAY BPF_MAP_TYPE_CGROUP_ARRAY,
include/uapi/linux/bpf.h enum bpf_map_type { · line 99
Quoted 10 lines
include/uapi/linux/bpf.h +9
-0+ BPF_MAP_TYPE_LANDLOCK_ARRAY, +}; + +enum bpf_map_array_type { +BPF_MAP_ARRAY_TYPE_UNSPEC , +}; + +enum bpf_map_handle_type { +BPF_MAP_HANDLE_TYPE_UNSPEC , };
missing something. why it has to be special to have it's own fd array implementation?
Please take a look how BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_MAP_TYPE_CGROUP_ARRAY and BPF_MAP_TYPE_PROG_ARRAY are done. The all store objects into array map that user space passes via FD. I think the same model should apply here.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Mickaël Salaün
maintainerRe: [RFC v3 03/22] bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
Quoted 109 lines · Alexei Starovoitov
Quoted 108 lines · Mickaël Salaün
This new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead ofCONST_PTR_TO_MAP(e.g.CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum
bpf_map_handle_typelist low-level types (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enumbpf_map_array_type(e.g.BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checksChanges since v1:
* arraymap of handles replace custom checker groups
* simpler userland APISigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)include/linux/bpf.h +14
-0@@ -13,6 +13,10 @@#include <linux/percpu.h> #include <linux/err.h> +#ifdef CONFIG_SECURITY_LANDLOCK +#include <linux/fs.h> /* struct file */ +#endif /* CONFIG_SECURITY_LANDLOCK */ +struct perf_event ;struct bpf_map ;@@ -38,6 +42,7 @@ struct bpf_map_ops {struct bpf_map {atomic_t refcnt;enum bpf_map_type map_type; +enum bpf_map_array_type map_array_type;u32 key_size;u32 value_size;u32 max_entries;@@ -187,6 +192,9 @@ struct bpf_array {*/ enum bpf_prog_type owner_prog_type;bool owner_jited; +#ifdef CONFIG_SECURITY_LANDLOCK +u32 n_entries;/* number of entries in a handle array */ +#endif /* CONFIG_SECURITY_LANDLOCK */ union {char value[0 ]__aligned(8) ;void *ptrs[0 ]__aligned(8) ;@@ -194,6 +202,12 @@ struct bpf_array { }; }; +#ifdef CONFIG_SECURITY_LANDLOCK +struct map_landlock_handle { +u32 type;/* enum bpf_map_handle_type */ +}; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#define MAX_TAIL_CALL_CNT 32struct bpf_event_entry {include/uapi/linux/bpf.h +9
-0@@ -87,6 +87,15 @@ enum bpf_map_type {BPF_MAP_TYPE_PERCPU_ARRAY , BPF_MAP_TYPE_STACK_TRACE,P_TYPE_CGROUP_ARRAYBPF_MAP_TYPE_CGROUP_ARRAY , + BPF_MAP_TYPE_LANDLOCK_ARRAY, +}; + +enum bpf_map_array_type { +BPF_MAP_ARRAY_TYPE_UNSPEC , +}; + +enum bpf_map_handle_type { + BPF_MAP_HANDLE_TYPE_UNSPEC,
Quoted 8 lines
include/uapi/linux/bpf.h enum bpf_map_type { · line 99
Quoted 1 line
include/uapi/linux/bpf.h +0
-0};missing something. why it has to be special to have it's own fd array implementation?
Please take a look howBPF_MAP_TYPE_PERF_EVENT_ARRAY,BPF_MAP_TYPE_CGROUP_ARRAYandBPF_MAP_TYPE_PROG_ARRAYare done. The all store objects into array map that user space passes via FD. I think the same model should apply here.
The idea is to have multiple way for userland to describe a resource (e.g. an open file descriptor, a path or a glob pattern). The kernel representation could then be a "struct path *" or dedicated types (e.g. custom glob).
Another interesting point (that could replace check_map_func_compatibility()) is that BPF_MAP_TYPE_LANDLOCK_ARRAY translate to dedicated (abstract) types (instead of CONST_PTR_TO_MAP) thanks to bpf_reg_type_from_map(). This is useful to abstract userland (map) interface with kernel object(s) dealing with that type.
A third point is that BPF_MAP_TYPE_LANDLOCK_ARRAY is a kind of set. It is optimized to quickly walk through all the elements in a sequential way.
Re: [RFC v3 03/22] bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
Quoted 120 lines · Mickaël Salaün
Quoted 109 lines · Alexei Starovoitov
Quoted 108 lines · Mickaël Salaün
This new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead ofCONST_PTR_TO_MAP(e.g.CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum
bpf_map_handle_typelist low-level types (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enumbpf_map_array_type(e.g.BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checksChanges since v1:
* arraymap of handles replace custom checker groups
* simpler userland APISigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)include/linux/bpf.h +14
-0@@ -13,6 +13,10 @@#include <linux/percpu.h> #include <linux/err.h> +#ifdef CONFIG_SECURITY_LANDLOCK +#include <linux/fs.h> /* struct file */ +#endif /* CONFIG_SECURITY_LANDLOCK */ +struct perf_event ;struct bpf_map ;@@ -38,6 +42,7 @@ struct bpf_map_ops {struct bpf_map {atomic_t refcnt;enum bpf_map_type map_type; +enum bpf_map_array_type map_array_type;u32 key_size;u32 value_size;u32 max_entries;@@ -187,6 +192,9 @@ struct bpf_array {*/ enum bpf_prog_type owner_prog_type;bool owner_jited; +#ifdef CONFIG_SECURITY_LANDLOCK +u32 n_entries;/* number of entries in a handle array */ +#endif /* CONFIG_SECURITY_LANDLOCK */ union {char value[0 ]__aligned(8) ;void *ptrs[0 ]__aligned(8) ;@@ -194,6 +202,12 @@ struct bpf_array { }; }; +#ifdef CONFIG_SECURITY_LANDLOCK +struct map_landlock_handle { +u32 type;/* enum bpf_map_handle_type */ +}; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#define MAX_TAIL_CALL_CNT 32struct bpf_event_entry {include/uapi/linux/bpf.h +9
-0@@ -87,6 +87,15 @@ enum bpf_map_type {BPF_MAP_TYPE_PERCPU_ARRAY , BPF_MAP_TYPE_STACK_TRACE,P_TYPE_CGROUP_ARRAYBPF_MAP_TYPE_CGROUP_ARRAY , + BPF_MAP_TYPE_LANDLOCK_ARRAY, +}; + +enum bpf_map_array_type { +BPF_MAP_ARRAY_TYPE_UNSPEC , +}; + +enum bpf_map_handle_type { + BPF_MAP_HANDLE_TYPE_UNSPEC,Quoted 8 lines
include/uapi/linux/bpf.h enum bpf_map_type { · line 99
Quoted 1 line
include/uapi/linux/bpf.h +0
-0};missing something. why it has to be special to have it's own fd array implementation?
Please take a look howBPF_MAP_TYPE_PERF_EVENT_ARRAY,BPF_MAP_TYPE_CGROUP_ARRAYandBPF_MAP_TYPE_PROG_ARRAYare done. The all store objects into array map that user space passes via FD. I think the same model should apply here.
Quoted 4 lines
The idea is to have multiple way for userland to describe a resource (e.g. an open file descriptor, a path or a glob pattern). The kernel representation could then be a "struct path *" or dedicated types (e.g. custom glob).
hmm. I think user space api should only deal with FD. Everything else is user space job to encapsulate/hide.
Quoted 5 lines
Another interesting point (that could replace
check_map_func_compatibility()) is thatBPF_MAP_TYPE_LANDLOCK_ARRAYtranslate to dedicated (abstract) types (instead ofCONST_PTR_TO_MAP) thanks tobpf_reg_type_from_map(). This is useful to abstract userland (map) interface with kernel object(s) dealing with that type.
I probably missing something. If user space interface is FD, to the kernel they're different object types. Nothing else.
Quoted 2 lines
A third point is that
BPF_MAP_TYPE_LANDLOCK_ARRAYis a kind of set. It is optimized to quickly walk through all the elements in a sequential way.
why set is any faster to walk vs array?
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Mickaël Salaün
maintainerRe: [RFC v3 03/22] bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
Quoted 121 lines · Alexei Starovoitov
Quoted 120 lines · Mickaël Salaün
Quoted 117 lines · Alexei Starovoitov
Quoted 99 lines · Mickaël Salaün
This new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead ofCONST_PTR_TO_MAP(e.g.CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum
bpf_map_handle_typelist low-level types (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enumbpf_map_array_type(e.g.BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checksChanges since v1:
* arraymap of handles replace custom checker groups
* simpler userland APISigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)include/linux/bpf.h +14
-0@@ -13,6 +13,10 @@#include <linux/percpu.h> #include <linux/err.h> +#ifdef CONFIG_SECURITY_LANDLOCK +#include <linux/fs.h> /* struct file */ +#endif /* CONFIG_SECURITY_LANDLOCK */ +struct perf_event ;struct bpf_map ;@@ -38,6 +42,7 @@ struct bpf_map_ops {struct bpf_map {atomic_t refcnt;enum bpf_map_type map_type; +enum bpf_map_array_type map_array_type;u32 key_size;u32 value_size;u32 max_entries;@@ -187,6 +192,9 @@ struct bpf_array {*/ enum bpf_prog_type owner_prog_type;bool owner_jited; +#ifdef CONFIG_SECURITY_LANDLOCK +u32 n_entries;/* number of entries in a handle array */ +#endif /* CONFIG_SECURITY_LANDLOCK */ union {char value[0 ]__aligned(8) ;void *ptrs[0 ]__aligned(8) ;@@ -194,6 +202,12 @@ struct bpf_array { }; }; +#ifdef CONFIG_SECURITY_LANDLOCK +struct map_landlock_handle { +u32 type;/* enum bpf_map_handle_type */ +}; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#define MAX_TAIL_CALL_CNT 32struct bpf_event_entry {include/uapi/linux/bpf.h +0
-0@@ -87,6 +87,15 @@ enum bpf_map_type {BPF_MAP_TYPE_PERCPU_ARRAY , BPF_MAP_TYPE_STACK_TRACE,P_TYPE_CGROUP_ARRAY BPF_MAP_TYPE_CGROUP_ARRAY,include/uapi/linux/bpf.h enum bpf_map_type { · line 99
Quoted 10 lines
include/uapi/linux/bpf.h +9
-0+ BPF_MAP_TYPE_LANDLOCK_ARRAY, +}; + +enum bpf_map_array_type { +BPF_MAP_ARRAY_TYPE_UNSPEC , +}; + +enum bpf_map_handle_type { +BPF_MAP_HANDLE_TYPE_UNSPEC , };missing something. why it has to be special to have it's own fd array implementation?
Please take a look howBPF_MAP_TYPE_PERF_EVENT_ARRAY,BPF_MAP_TYPE_CGROUP_ARRAYandBPF_MAP_TYPE_PROG_ARRAYare done. The all store objects into array map that user space passes via FD. I think the same model should apply here.
Quoted 7 lines
Quoted 4 lines
The idea is to have multiple way for userland to describe a resource (e.g. an open file descriptor, a path or a glob pattern). The kernel representation could then be a "struct path *" or dedicated types (e.g. custom glob).
hmm. I think user space api should only deal with FD. Everything else is user space job to encapsulate/hide.
How would you create a FD referring to a glob, a user or port ranges for example ?
Quoted 9 lines
Quoted 6 lines
Another interesting point (that could replace
check_map_func_compatibility()) is thatBPF_MAP_TYPE_LANDLOCK_ARRAYtranslate to dedicated (abstract) types (instead ofCONST_PTR_TO_MAP) thanks tobpf_reg_type_from_map(). This is useful to abstract userland (map) interface with kernel object(s) dealing with that type.I probably missing something. If user space interface is FD, to the kernel they're different object types. Nothing else.
Yes but what if there is more than one way to express a resource (cf. previous comment). A FD can refer to an *existing file* but a glob pattern could match a bunch of files (existing or not). This was a concern for Kees Cook and James Morris [1].
[1]
https://lkml.kernel.org/r/CAGXu5jK1U12vMk11HD_x_gNz3Rk4ZgEfdThY7DHvm4e4sPRh4g@mail.gmail.com
Quoted 5 lines
Quoted 3 lines
A third point is that
BPF_MAP_TYPE_LANDLOCK_ARRAYis a kind of set. It is optimized to quickly walk through all the elements in a sequential way.why set is any faster to walk vs array?
It is an array with only sequential entries (i.e. no hole in the array).
Re: [RFC v3 03/22] bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
In reply to Mickaël SalaünQuoted 57 lines · Mickaël Salaün
This new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead ofCONST_PTR_TO_MAP(e.g.CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum
bpf_map_handle_typelist low-level types (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enumbpf_map_array_type(e.g.BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checksChanges since v1:
* arraymap of handles replace custom checker groups
* simpler userland APISigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <davem-fT/PcQaiUtIeIZ0/[email protected]>Cc Kees Cook <[email protected]>--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)[...]
kernel/bpf/arraymap.c +6
-0+/* +* Limit number of entries in an arraymap of handles to the maximum +* number of open files for the current process. The maximum number of +* handle entries (including all arraymaps) for a process is then +* (RLIMIT_NOFILE - 1) * RLIMIT_NOFILE. If the process' RLIMIT_NOFILE +* is 0, then any entry update is forbidden.
kernel/bpf/arraymap.c static int __init register_cgroup_array_map(void) · line 699
Quoted 10 lines
kernel/bpf/arraymap.c +10
-0+* +* An eBPF program can inherit all the arraymap FD. The worse case is +* to fill a bunch of arraymaps, create an eBPF program, close the +* arraymap FDs, and start again. The maximum number of arraymap +* entries can then be close to RLIMIT_NOFILE^3. +* +* FIXME: This should be improved... any idea? +*/ +if (unlikely (index >=rlimit (RLIMIT_NOFILE ))) +return -EMFILE ;
I'm not sure what's best for resource management here. Landlock will be holding open path structs, for example, but how are you expecting to track things like network policies? An allowed IP address, for example, doesn't have a handle outside of doing a full socket()/connect() setup.
I think an explicit design for resource management should be considered up front...
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Mickaël Salaün
maintainerRe: [RFC v3 03/22] bpf,landlock: Add a new arraymap type to deal with (Landlock) handles
Quoted 66 lines · Kees Cook
Quoted 65 lines · Mickaël Salaün
This new arraymap looks like a set and brings new properties:
* strong typing of entries: the eBPF functions get the array type of elements instead ofCONST_PTR_TO_MAP(e.g.CONST_PTR_TO_LANDLOCK_HANDLE_FS);
* force sequential filling (i.e. replace or append-only update), which allow quick browsing of all entries.This strong typing is useful to statically check if the content of a map can be passed to an eBPF function. For example, Landlock use it to store and manage kernel objects (e.g. struct file) instead of dealing with userland raw data. This improve efficiency and ensure that an eBPF program can only call functions with the right high-level arguments.
The enum
bpf_map_handle_typelist low-level types (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) which are identified when updating a map entry (handle). This handle types are used to infer a high-level arraymap type which are listed in enumbpf_map_array_type(e.g.BPF_MAP_ARRAY_TYPE_LANDLOCK_FS).For now, this new arraymap is only used by Landlock LSM (cf. next commits) but it could be useful for other needs.
Changes since v2:
* add a RLIMIT_NOFILE-based limit to the maximum number of arraymap handle entries (suggested by Andy Lutomirski)
* remove useless checksChanges since v1:
* arraymap of handles replace custom checker groups
* simpler userland APISigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>--- include/linux/bpf.h | 14 ++++ include/uapi/linux/bpf.h | 18 +++++ kernel/bpf/arraymap.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 12 ++- 4 files changed, 246 insertions(+), 1 deletion(-)[...]
kernel/bpf/arraymap.c +14
-0+/* +* Limit number of entries in an arraymap of handles to the maximum +* number of open files for the current process. The maximum number of +* handle entries (including all arraymaps) for a process is then +* (RLIMIT_NOFILE - 1) * RLIMIT_NOFILE. If the process' RLIMIT_NOFILE +* is 0, then any entry update is forbidden. +* +* An eBPF program can inherit all the arraymap FD. The worse case is +* to fill a bunch of arraymaps, create an eBPF program, close the +* arraymap FDs, and start again. The maximum number of arraymap +* entries can then be close to RLIMIT_NOFILE^3. +* +* FIXME: This should be improved... any idea? +*/
Quoted 8 lines
kernel/bpf/arraymap.c static int __init register_cgroup_array_map(void) · line 699
Quoted 2 lines
kernel/bpf/arraymap.c +2
-0+if (unlikely (index >=rlimit (RLIMIT_NOFILE ))) +return -EMFILE ;I'm not sure what's best for resource management here. Landlock will be holding open path structs, for example, but how are you expecting to track things like network policies? An allowed IP address, for example, doesn't have a handle outside of doing a full socket()/connect() setup.
Path and file references are hard to handle correctly but other things should be simpler. External resources (i.e. not relative to the running system as paths are) like network hosts or ports could simply be expressed as raw values (like used for iptables rules). Moreover, for network rules, relying on raw packet values (as BPF_PROG_TYPE_SOCKET_FILTER have access to) may be more than enough.
Quoted 3 lines
I think an explicit design for resource management should be considered up front...
I'm not really sure how to handle that part…
There is basically two ways to express a "kernel object": relative (with an internal pointer to a struct, e.g. struct file) or absolute (a raw value). Both of them use kernel memory. However, only the former may impact other parts of the kernel (e.g. can force to hold a kernel object like a struct dentry). The impact of this is not clear for me but it looks like other resource managements for a process: number of open files, number of network connections…
The more reasonable approach seems to charge the user for the (kernel) memory dedicated to the user's policy. How can I do it? Maybe to decrement the RLIMIT_NPROC and check the RLIMIT_AS (i.e. act like a virtual process)?
There is no such limits with other eBPF maps (even those dealing with FD), so this may be too much.
Mickaël
Mickaël Salaün
maintainerRFC v3 4/22 bpf: Set register type according to is_valid_access()
In reply to Mickaël SalaünThis fix a pointer leak when an unprivileged eBPF program read a pointer value from the context. Even if is_valid_access() returns a pointer type, the eBPF verifier replace it with UNKNOWN_VALUE. The register value containing an address is then allowed to leak. Moreover, this prevented unprivileged eBPF programs to use functions with (legitimate) pointer arguments.
This bug was not a problem until now because the only unprivileged eBPF program allowed is of type BPF_PROG_TYPE_SOCKET_FILTER and all the types from its context are UNKNOWN_VALUE.
--- kernel/bpf/verifier.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +2 -4
@@ -794,10 +794,8 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off, } err =check_ctx_access (env, off, size, t, ®_type);if (!err && t ==BPF_READ && value_regno >=0 ) { -mark_reg_unknown_value (state->regs, value_regno); -if (env->allow_ptr_leaks) -/* note that reg.[id|off|range] == 0 */ - state->regs[value_regno].type = reg_type; +/* note that reg.[id|off|range] == 0 */ + state->regs[value_regno].type = reg_type; } }else if (reg->type ==FRAME_PTR || reg->type ==PTR_TO_STACK ) {
Signature · 1 line
2.9.3
Re: [RFC v3 04/22] bpf: Set register type according to is_valid_access()
Quoted 7 lines · Mickaël Salaün
This fix a pointer leak when an unprivileged eBPF program read a pointer value from the context. Even if
is_valid_access()returns a pointer type, the eBPF verifier replace it withUNKNOWN_VALUE. The register value containing an address is then allowed to leak. Moreover, this prevented unprivileged eBPF programs to use functions with (legitimate) pointer arguments.
Quoted 8 lines
This bug was not a problem until now because the only unprivileged eBPF program allowed is of type
BPF_PROG_TYPE_SOCKET_FILTERand all the types from its context areUNKNOWN_VALUE.Signed-off-by Mickaël Salaün <[email protected]>Fixes 969bf05eb3ce ("bpf: direct packet access")Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>
Can you post this fix separately? It's valid and needed outside of the scope of this series.
Daniel Borkmann
maintainerRe: [RFC v3 04/22] bpf: Set register type according to is_valid_access()
Quoted 12 lines · Thomas Graf
Quoted 11 lines · Mickaël Salaün
This fix a pointer leak when an unprivileged eBPF program read a pointer value from the context. Even if
is_valid_access()returns a pointer type, the eBPF verifier replace it withUNKNOWN_VALUE. The register value containing an address is then allowed to leak. Moreover, this prevented unprivileged eBPF programs to use functions with (legitimate) pointer arguments.This bug was not a problem until now because the only unprivileged eBPF program allowed is of type
BPF_PROG_TYPE_SOCKET_FILTERand all the types from its context areUNKNOWN_VALUE.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Mickaël Salaün <[email protected]>Fixes 969bf05eb3ce ("bpf: direct packet access")Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Can you post this fix separately? It's valid and needed outside of the scope of this series.
Yes, that one was already merged:
Mickaël Salaün
maintainerRFC v3 5/22 bpf,landlock: Add eBPF program subtype and is_valid_subtype() verifier
In reply to Mickaël SalaünThe program subtype goal is to be able to have different static fine-grained verifications for a unique program type.
The struct bpf_verifier_ops gets a new optional function:is_valid_subtype(). This new verifier is called at the begening of the eBPF program verification to check if the (optional) program subtype is valid.
For now, only Landlock eBPF programs are using a program subtype but this could be used by other program types in the future.
Cf. the next commit to see how the subtype is used by Landlock LSM.
--- include/linux/bpf.h | 8 ++++++-- include/linux/filter.h | 1 + include/uapi/linux/bpf.h | 9 +++++++++ kernel/bpf/syscall.c | 5 +++-- kernel/bpf/verifier.c | 9 +++++++-- kernel/trace/bpf_trace.c | 12 ++++++++---- net/core/filter.c | 21 +++++++++++++-------- 7 files changed, 47 insertions(+), 18 deletions(-)
include/linux/bpf.h +6 -2
@@ -149,17 +149,21 @@ struct bpf_prog;struct bpf_verifier_ops {/* return eBPF function prototype for verification */ -const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id); +const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id, +union bpf_prog_subtype *prog_subtype);/* return true if 'size' wide access at offset 'off' within bpf_context * with 'type' (read or write) is allowed */ bool (*is_valid_access)(int off,int size,enum bpf_access_type type, -enum bpf_reg_type *reg_type); +enum bpf_reg_type *reg_type, +union bpf_prog_subtype *prog_subtype);u32 (*convert_ctx_access)(enum bpf_access_type type,int dst_reg,int src_reg,int ctx_off,struct bpf_insn *insn,struct bpf_prog *prog); + +bool (*is_valid_subtype)(union bpf_prog_subtype *prog_subtype); };struct bpf_prog_type_list {
include/linux/filter.h +1 -0
@@ -406,6 +406,7 @@ struct bpf_prog {kmemcheck_bitfield_end (meta);u32 len;/* Number of filter blocks */ enum bpf_prog_type type;/* Type of BPF program */ +union bpf_prog_subtype subtype;/* For fine-grained verifications */ struct bpf_prog_aux *aux;/* Auxiliary fields */ struct sock_fprog_kern *orig_prog;/* Original BPF program */ unsigned int (*bpf_func)(const struct sk_buff *skb,
include/uapi/linux/bpf.h +9 -0
@@ -127,6 +127,14 @@ enum bpf_attach_type {#define BPF_F_NO_PREALLOC (1U << 0) +union bpf_prog_subtype { +struct { +__u32 id;/* enum landlock_hook_id */ +__u16 origin;/* LANDLOCK_FLAG_ORIGIN_* */ +__aligned_u64 access;/* LANDLOCK_FLAG_ACCESS_* */ + } landlock_hook; +} __attribute__((aligned (8 ))); +union bpf_attr {struct {/* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type;/* one of enum bpf_map_type */
@@ -155,6 +163,7 @@ union bpf_attr {__u32 log_size;/* size of user buffer */ __aligned_u64 log_buf;/* user supplied buffer */ __u32 kern_version;/* checked when prog_type=kprobe */ +union bpf_prog_subtype prog_subtype;/* checked when prog_type=landlock */ };struct {/* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/syscall.c +3 -2
@@ -572,7 +572,7 @@ static void fixup_bpf_calls(struct bpf_prog *prog)continue ; } - fn = prog->aux->ops->get_func_proto (insn->imm); + fn = prog->aux->ops->get_func_proto (insn->imm, &prog->subtype);/* all functions that have prototype and verifier allowed * programs to call them, must be real in-kernel functions */
@@ -710,7 +710,7 @@ struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)EXPORT_SYMBOL_GPL (bpf_prog_get_type);/* last field in 'union bpf_attr' used by this command */ -#define BPF_PROG_LOAD_LAST_FIELD kern_version +#define BPF_PROG_LOAD_LAST_FIELD prog_subtypestatic int bpf_prog_load (union bpf_attr *attr) {
@@ -768,6 +768,7 @@ static int bpf_prog_load(union bpf_attr *attr) err =find_prog_type (type, prog);if (err <0 ) goto free_prog; + prog->subtype = attr->prog_subtype;/* run eBPF verifier */ err =bpf_check (&prog, attr);
kernel/bpf/verifier.c +7 -2
@@ -684,7 +684,8 @@ static int check_ctx_access(struct verifier_env *env, int off, int size, enum bpf_access_type t, enum bpf_reg_type *reg_type) {if (env->prog->aux->ops->is_valid_access && - env->prog->aux->ops->is_valid_access (off, size, t, reg_type)) { + env->prog->aux->ops->is_valid_access (off, size, t, reg_type, + &env->prog->subtype)) {/* remember the offset of last byte accessed in ctx */ if (env->prog->aux->max_ctx_offset < off + size) env->prog->aux->max_ctx_offset = off + size;
@@ -1173,7 +1174,7 @@ static int check_call(struct verifier_env *env, int func_id) }if (env->prog->aux->ops->get_func_proto) - fn = env->prog->aux->ops->get_func_proto (func_id); + fn = env->prog->aux->ops->get_func_proto (func_id, &env->prog->subtype);if (!fn) {verbose ("unknown func %d\n" , func_id);
@@ -2768,6 +2769,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)if ((*prog)->len <=0 || (*prog)->len >BPF_MAXINSNS )return -E2BIG ; +if ((*prog)->aux->ops->is_valid_subtype && + !(*prog)->aux->ops->is_valid_subtype (&(*prog)->subtype)) +return -EINVAL ; +/* 'struct verifier_env' can be global, but since it's not small, * allocate/free it every time bpf_check() is called */
kernel/trace/bpf_trace.c +8 -4
@@ -435,7 +435,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id) } } -static const struct bpf_func_proto *kprobe_prog_func_proto (enum bpf_func_id func_id) +static const struct bpf_func_proto *kprobe_prog_func_proto (enum bpf_func_id func_id, +union bpf_prog_subtype *prog_subtype) {switch (func_id) {case BPF_FUNC_perf_event_output:
@@ -449,7 +450,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func/* bpf+kprobe programs can access fields of 'struct pt_regs' */ static bool kprobe_prog_is_valid_access (int off,int size,enum bpf_access_type type, -enum bpf_reg_type *reg_type) +enum bpf_reg_type *reg_type, +union bpf_prog_subtype *prog_subtype) {if (off <0 || off >=sizeof (struct pt_regs ))return false;
@@ -517,7 +519,8 @@ static const struct bpf_func_proto bpf_get_stackid_proto_tp = { .arg3_type = ARG_ANYTHING, }; -static const struct bpf_func_proto *tp_prog_func_proto (enum bpf_func_id func_id) +static const struct bpf_func_proto *tp_prog_func_proto (enum bpf_func_id func_id, +union bpf_prog_subtype *prog_subtype) {switch (func_id) {case BPF_FUNC_perf_event_output:
@@ -530,7 +533,8 @@ static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id) }static bool tp_prog_is_valid_access (int off,int size,enum bpf_access_type type, -enum bpf_reg_type *reg_type) +enum bpf_reg_type *reg_type, +union bpf_prog_subtype *prog_subtype) {if (off <sizeof (void *) || off >=PERF_MAX_TRACE_SIZE )return false;
net/core/filter.c +13 -8
@@ -2411,7 +2411,8 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = { };static const struct bpf_func_proto * -sk_filter_func_proto (enum bpf_func_id func_id) +sk_filter_func_proto (enum bpf_func_id func_id, +union bpf_prog_subtype *prog_subtype) {switch (func_id) {case BPF_FUNC_map_lookup_elem:
@@ -2437,7 +2438,8 @@ sk_filter_func_proto(enum bpf_func_id func_id) }static const struct bpf_func_proto * -tc_cls_act_func_proto (enum bpf_func_id func_id) +tc_cls_act_func_proto (enum bpf_func_id func_id, +union bpf_prog_subtype *prog_subtype) {switch (func_id) {case BPF_FUNC_skb_store_bytes:
@@ -2485,18 +2487,18 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)case BPF_FUNC_skb_under_cgroup:return &bpf_skb_under_cgroup_proto;default : -return sk_filter_func_proto (func_id); +return sk_filter_func_proto (func_id, prog_subtype); } }static const struct bpf_func_proto * -xdp_func_proto (enum bpf_func_id func_id) +xdp_func_proto (enum bpf_func_id func_id,union bpf_prog_subtype *prog_subtype) {switch (func_id) {case BPF_FUNC_perf_event_output:return &bpf_xdp_event_output_proto;default : -return sk_filter_func_proto (func_id); +return sk_filter_func_proto (func_id, prog_subtype); } }
@@ -2515,7 +2517,8 @@ static bool __is_valid_access(int off, int size, enum bpf_access_type type)static bool sk_filter_is_valid_access (int off,int size,enum bpf_access_type type, -enum bpf_reg_type *reg_type) +enum bpf_reg_type *reg_type, +union bpf_prog_subtype *prog_subtype) {switch (off) {case offsetof(struct __sk_buff , tc_classid):
@@ -2539,7 +2542,8 @@ static bool sk_filter_is_valid_access(int off, int size,static bool tc_cls_act_is_valid_access (int off,int size,enum bpf_access_type type, -enum bpf_reg_type *reg_type) +enum bpf_reg_type *reg_type, +union bpf_prog_subtype *prog_subtype) {if (type ==BPF_WRITE ) {switch (off) {
@@ -2582,7 +2586,8 @@ static bool __is_valid_xdp_access(int off, int size,static bool xdp_is_valid_access (int off,int size,enum bpf_access_type type, -enum bpf_reg_type *reg_type) +enum bpf_reg_type *reg_type, +union bpf_prog_subtype *prog_subtype) {if (type ==BPF_WRITE )return false;
Signature · 1 line
2.9.3
Re: [RFC v3 05/22] bpf,landlock: Add eBPF program subtype and is_valid_subtype() verifier
include/uapi/linux/bpf.h union bpf_attr { · line 166
Quoted 5 lines · Mickaël Salaün
include/uapi/linux/bpf.h +1
-0@@ -155,6 +163,7 @@ union bpf_attr {__u32 log_size;/* size of user buffer */ __aligned_u64 log_buf;/* user supplied buffer */ __u32 kern_version;/* checked when prog_type=kprobe */ +union bpf_prog_subtype prog_subtype;/* checked when prog_type=landlock */
The comment seems bogus, this is not landlock specific.
Mickaël Salaün
maintainerRFC v3 6/22 landlock: Add LSM hooks
In reply to Mickaël SalaünAdd LSM hooks which can be used by userland through Landlock (eBPF) programs. This programs are limited to a whitelist of functions (cf. next commit). The eBPF program context is depicted by the struct landlock_data (cf. include/uapi/linux/bpf.h):
* hook: LSM hook ID
* origin: what triggered this Landlock program (syscall, dedicated seccomp return or interruption)
* cookie: the 16-bit value from the seccomp filter that triggered this Landlock program
* args[6]: array of some LSM hook arguments
The LSM hook arguments can contain raw values as integers or (unleakable) pointers. The only way to use the pointers are to pass them to an eBPF function according to their types (e.g. the bpf_landlock_cmp_fs_beneath_with_struct_file function can use a struct file pointer).
For each Landlock program, the subtype allows to specify for which LSM hook the program is dedicated thanks to the "id" field. The "origin" field must contains each triggers for which the Landlock program will be called (e.g. every syscall or/and seccomp filters returning RET_LANDLOCK). The "access" bitfield can be used to allow a program to access a specific feature from a Landlock hook (i.e. context value or function). The flag guarding this feature may only be enabled according to the capabilities of the process loading the program.
For now, there is three hooks for file system access control:
* file_open
* file_permission
* mmap_file
Changes since v2:
* use subtypes instead of dedicated eBPF program types for each hook (suggested by Alexei Starovoitov)
* replace convert_ctx_access() with subtype check
* use an array of Landlock program list instead of a single list
* handle running Landlock programs without needing a seccomp filter
* use, check and expose "origin" to Landlock programs
* mask the unused struct cred * (suggested by Andy Lutomirski)
Changes since v1:
* revamp access control from a syscall-based to a LSM hooks-based
* do not use audit cache
* no race conditions by design
* architecture agnostic
* switch from cBPF to eBPF (suggested by Daniel Borkmann)
* new BPF context
--- include/linux/bpf.h | 5 + include/linux/lsm_hooks.h | 5 + include/uapi/linux/bpf.h | 37 ++++++++ kernel/bpf/syscall.c | 10 +- kernel/bpf/verifier.c | 6 ++ security/Makefile | 2 + security/landlock/Makefile | 3 + security/landlock/lsm.c | 222 +++++++++++++++++++++++++++++++++++++++++++++ security/security.c | 1 + 9 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/lsm.c
include/linux/bpf.h +5 -0
@@ -85,6 +85,8 @@ enum bpf_arg_type {ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING ,/* any (initialized) argument is ok */ + + ARG_PTR_TO_STRUCT_FILE, /* pointer to struct file */ };/* type of values returned from helper functions */
@@ -143,6 +145,9 @@ enum bpf_reg_type {*/ PTR_TO_PACKET ,PTR_TO_PACKET_END ,/* skb->data + headlen */ + +/* Landlock */ + PTR_TO_STRUCT_FILE, };struct bpf_prog ;
include/linux/lsm_hooks.h +5 -0
@@ -1933,5 +1933,10 @@ void __init loadpin_add_hooks(void);#else static inline void loadpin_add_hooks (void ) { };#endif +#ifdef CONFIG_SECURITY_LANDLOCK +extern void __init landlock_add_hooks (void ); +#else +static inline void __init landlock_add_hooks (void ) { } +#endif /* CONFIG_SECURITY_LANDLOCK */ #endif /* ! __LINUX_LSM_HOOKS_H */
include/uapi/linux/bpf.h +37 -0
@@ -108,6 +108,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_XDP ,BPF_PROG_TYPE_PERF_EVENT ,BPF_PROG_TYPE_CGROUP_SOCKET , + BPF_PROG_TYPE_LANDLOCK, }; enum bpf_attach_type {
@@ -528,6 +529,23 @@ struct xdp_md {__u32 data_end; }; +/* LSM hooks */ +enum landlock_hook_id { +LANDLOCK_HOOK_UNSPEC , +LANDLOCK_HOOK_FILE_OPEN , +LANDLOCK_HOOK_FILE_PERMISSION , +LANDLOCK_HOOK_MMAP_FILE , +}; +#define _LANDLOCK_HOOK_LAST LANDLOCK_HOOK_MMAP_FILE + +/* Trigger type */ +#define LANDLOCK_FLAG_ORIGIN_SYSCALL (1 << 0) +#define LANDLOCK_FLAG_ORIGIN_SECCOMP (1 << 1) +#define _LANDLOCK_FLAG_ORIGIN_MASK ((1 << 2) - 1) + +/* context of function access flags */ +#define _LANDLOCK_FLAG_ACCESS_MASK ((1ULL << 0) - 1) +/* Map handle entry */ struct landlock_handle {__u32 type;/* enum bpf_map_handle_type */
@@ -537,4 +555,23 @@ struct landlock_handle { }; } __attribute__((aligned(8))); +/** +* struct landlock_data +* +* @hook: LSM hook ID (e.g. BPF_PROG_TYPE_LANDLOCK_FILE_OPEN) +* @origin: bit indicating for which reason the program is running +* @cookie: value set by a seccomp-filter return value RET_LANDLOCK. This come +* from a trusted seccomp-bpf program: the same process that loaded +* this Landlock hook program. +* @args: LSM hook arguments, see include/linux/lsm_hooks.h for there +* description and the LANDLOCK_HOOK* definitions from +* security/landlock/lsm.c for their types. +*/ +struct landlock_data { +__u32 hook;/* enum landlock_hook_id */ +__u16 origin;/* LANDLOCK_FLAG_ORIGIN_* */ +__u16 cookie;/* seccomp RET_LANDLOCK */ +__u64 args[6 ]; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/syscall.c +8 -2
@@ -739,8 +739,14 @@ static int bpf_prog_load(union bpf_attr *attr) attr->kern_version != LINUX_VERSION_CODE)return -EINVAL ; -if (type !=BPF_PROG_TYPE_SOCKET_FILTER && !capable (CAP_SYS_ADMIN )) -return -EPERM ; +switch (type) { +case BPF_PROG_TYPE_SOCKET_FILTER : +case BPF_PROG_TYPE_LANDLOCK : +break ; +default : +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + }/* plain bpf_prog allocation */ prog =bpf_prog_alloc (bpf_prog_size (attr->insn_cnt),GFP_USER );
kernel/bpf/verifier.c +6 -0
@@ -245,6 +245,7 @@ static const char * const reg_type_str[] = { [CONST_IMM] = "imm", [PTR_TO_PACKET] = "pkt", [PTR_TO_PACKET_END] = "pkt_end", + [PTR_TO_STRUCT_FILE] = "struct_file", };static void print_verifier_state (struct verifier_state *state)
@@ -555,6 +556,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)case PTR_TO_PACKET_END :case FRAME_PTR :case CONST_PTR_TO_MAP : +case PTR_TO_STRUCT_FILE :return true;default :return false;
@@ -972,6 +974,10 @@ static int check_func_arg(struct verifier_env *env, u32 regno, expected_type =PTR_TO_CTX ;if (type != expected_type) goto err_type; + }else if (arg_type ==ARG_PTR_TO_STRUCT_FILE ) { + expected_type =PTR_TO_STRUCT_FILE ; +if (type != expected_type) + goto err_type; }else if (arg_type ==ARG_PTR_TO_STACK || arg_type ==ARG_PTR_TO_RAW_STACK ) { expected_type =PTR_TO_STACK ;
security/Makefile +2 -0
@@ -9,6 +9,7 @@ subdir-$(CONFIG_SECURITY_TOMOYO) += tomoyo subdir-$(CONFIG_SECURITY_APPARMOR) += apparmor subdir-$(CONFIG_SECURITY_YAMA) += yama subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin +subdir-$(CONFIG_SECURITY_LANDLOCK) += landlock # always enable default capabilities obj-y += commoncap.o
@@ -24,6 +25,7 @@ obj-$(CONFIG_SECURITY_TOMOYO) += tomoyo/ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/ obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ +obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file lists
security/landlock/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := lsm.o
security/landlock/lsm.c new file +222 -0
222 lines not on this page · raw message
security/security.c +1 -0
@@ -61,6 +61,7 @@ int __init security_init(void)capability_add_hooks ();yama_add_hooks ();loadpin_add_hooks (); +landlock_add_hooks ();/* * Load all the remaining security modules.
Signature · 1 line
2.9.3
Re: [RFC v3 06/22] landlock: Add LSM hooks
include/linux/bpf.h enum bpf_arg_type { · line 89
Quoted 10 lines · Mickaël Salaün
include/linux/bpf.h +2
-0@@ -85,6 +85,8 @@ enum bpf_arg_type {ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING ,/* any (initialized) argument is ok */ + + ARG_PTR_TO_STRUCT_FILE, /* pointer to struct file */
This should go into patch 7 I guess?
include/linux/bpf.h enum bpf_arg_type {
Quoted 5 lines
include/linux/bpf.h +5
-0+void __init landlock_add_hooks (void ) +{ +pr_info ("landlock: Becoming ready for sandboxing\n" ); +security_add_hooks (landlock_hooks,ARRAY_SIZE (landlock_hooks)); +}
Can we add the hooks when we load the first BPF program for a hook? That would also allow to not make this conditional on a new config option which all all distros have to enable anyway.
I would really like to see this patch split into the LSM part which allows running BPF progs at LSM and your specific sandboxing use case which requires the new BPF helpers, new reg type, etc.
Mickaël Salaün
maintainerRe: [RFC v3 06/22] landlock: Add LSM hooks
Quoted 7 lines · Thomas Graf
Quoted 6 lines · Mickaël Salaün
include/linux/bpf.h +0
-0@@ -85,6 +85,8 @@ enum bpf_arg_type {
Quoted 6 lines
include/linux/bpf.h enum bpf_arg_type { · line 89
Quoted 4 lines
include/linux/bpf.h +2
-0ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING ,/* any (initialized) argument is ok */ + + ARG_PTR_TO_STRUCT_FILE, /* pointer to struct file */This should go into patch 7 I guess?
Right, the ARG_PTR_* are only used by BPF helpers.
Quoted 10 lines
include/linux/bpf.h enum bpf_arg_type {
Quoted 6 lines
include/linux/bpf.h +5
-0+void __init landlock_add_hooks (void ) +{ +pr_info ("landlock: Becoming ready for sandboxing\n" ); +security_add_hooks (landlock_hooks,ARRAY_SIZE (landlock_hooks)); +}Can we add the hooks when we load the first BPF program for a hook? That would also allow to not make this conditional on a new config option which all all distros have to enable anyway.
We could either add hook by hook or all hooks at once when loading a BPF program for which its subtype match the hook type, but I'm not sure it is worth it.
I'd like to enable this LSM by default but we should be able to disable it if needed, like most kernel features.
Quoted 5 lines
I would really like to see this patch split into the LSM part which allows running BPF progs at LSM and your specific sandboxing use case which requires the new BPF helpers, new reg type, etc.
I'll try to split it as much as possible.
Mickaël Salaün
maintainerRFC v3 7/22 landlock: Handle file comparisons
In reply to Mickaël SalaünAdd eBPF functions to compare file system access with a Landlock file system handle:
* bpf_landlock_cmp_fs_prop_with_struct_file(prop, map, map_op, file) This function allows to compare the dentry, inode, device or mount point of the currently accessed file, with a reference handle.
* bpf_landlock_cmp_fs_beneath_with_struct_file(opt, map, map_op, file) This function allows an eBPF program to check if the current accessed file is the same or in the hierarchy of a reference handle.
The goal of file system handle is to abstract kernel objects such as a struct file or a struct inode. Userland can create this kind of handle thanks to the BPF_MAP_UPDATE_ELEM command. The element is a struct landlock_handle containing the handle type (e.g. BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) and a file descriptor. This could also be any descriptions able to match a struct file or a struct inode (e.g. path or glob string).
Changes since v2:
* add MNT_INTERNAL check to only add file handle from user-visible FS (e.g. no anonymous inode)
* replace struct file* with struct path* in map_landlock_handle
* add BPF protos
* fix bpf_landlock_cmp_fs_prop_with_struct_file()
--- include/linux/bpf.h | 10 +++ include/uapi/linux/bpf.h | 49 +++++++++++ kernel/bpf/arraymap.c | 21 +++++ kernel/bpf/verifier.c | 8 ++ security/landlock/Makefile | 2 +- security/landlock/checker_fs.c | 179 +++++++++++++++++++++++++++++++++++++++++ security/landlock/checker_fs.h | 20 +++++ security/landlock/lsm.c | 6 ++ 8 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 security/landlock/checker_fs.c create mode 100644 security/landlock/checker_fs.h
include/linux/bpf.h +10 -0
@@ -87,6 +87,7 @@ enum bpf_arg_type {ARG_ANYTHING ,/* any (initialized) argument is ok */ ARG_PTR_TO_STRUCT_FILE ,/* pointer to struct file */ + ARG_CONST_PTR_TO_LANDLOCK_HANDLE_FS, /* pointer to Landlock FS handle */ };/* type of values returned from helper functions */
@@ -148,6 +149,7 @@ enum bpf_reg_type {/* Landlock */ PTR_TO_STRUCT_FILE , + CONST_PTR_TO_LANDLOCK_HANDLE_FS, };struct bpf_prog ;
@@ -214,6 +216,9 @@ struct bpf_array {#ifdef CONFIG_SECURITY_LANDLOCK struct map_landlock_handle {u32 type;/* enum bpf_map_handle_type */ +union { +struct path path; + }; };#endif /* CONFIG_SECURITY_LANDLOCK */
@@ -348,6 +353,11 @@ extern const struct bpf_func_proto bpf_skb_vlan_push_proto;extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;extern const struct bpf_func_proto bpf_get_stackid_proto; +#ifdef CONFIG_SECURITY_LANDLOCK +extern const struct bpf_func_proto bpf_landlock_cmp_fs_prop_with_struct_file_proto; +extern const struct bpf_func_proto bpf_landlock_cmp_fs_beneath_with_struct_file_proto; +#endif /* CONFIG_SECURITY_LANDLOCK */ +/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );u64 bpf_user_rnd_u32 (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5);
include/uapi/linux/bpf.h +49 -0
71 lines not on this page · raw message
kernel/bpf/arraymap.c +21 -0
@@ -21,6 +21,8 @@#ifdef CONFIG_SECURITY_LANDLOCK #include <asm/resource.h> /* RLIMIT_NOFILE */ +#include <linux/mount.h> /* struct vfsmount, MNT_INTERNAL */ +#include <linux/path.h> /* path_get(), path_put() */ #include <linux/sched.h> /* rlimit() */ #endif /* CONFIG_SECURITY_LANDLOCK */
@@ -603,6 +605,9 @@ static void landlock_put_handle(struct map_landlock_handle *handle)enum bpf_map_handle_type handle_type = handle->type;switch (handle_type) { +case BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD : +path_put (&handle->path); +break ;case BPF_MAP_HANDLE_TYPE_UNSPEC :default :WARN_ON (1 );
@@ -628,6 +633,8 @@ static enum bpf_map_array_type landlock_get_array_type( enum bpf_map_handle_type handle_type) {switch (handle_type) { +case BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD : +return BPF_MAP_ARRAY_TYPE_LANDLOCK_FS ;case BPF_MAP_HANDLE_TYPE_UNSPEC :default :return -EINVAL ;
@@ -650,8 +657,22 @@ static inline long landlock_store_handle(struct map_landlock_handle *dst, struct landlock_handle *handle) {enum bpf_map_handle_type handle_type = handle->type; +struct file *handle_file; + +/* access control already done for the FD */ switch (handle_type) { +case BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD : +FGET_OR_RET (handle_file, handle->fd); +/* check if the FD is tied to a user mount point */ +if (unlikely (handle_file->f_path.mnt->mnt_flags &MNT_INTERNAL )) { +fput (handle_file); +return -EINVAL ; + } +path_get (&handle_file->f_path); + dst->path = handle_file->f_path; +fput (handle_file); +break ;case BPF_MAP_HANDLE_TYPE_UNSPEC :default :WARN_ON (1 );
kernel/bpf/verifier.c +8 -0
@@ -246,6 +246,7 @@ static const char * const reg_type_str[] = { [PTR_TO_PACKET] = "pkt", [PTR_TO_PACKET_END] = "pkt_end", [PTR_TO_STRUCT_FILE] = "struct_file", + [CONST_PTR_TO_LANDLOCK_HANDLE_FS] = "landlock_handle_fs", };static void print_verifier_state (struct verifier_state *state)
@@ -557,6 +558,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)case FRAME_PTR :case CONST_PTR_TO_MAP :case PTR_TO_STRUCT_FILE : +case CONST_PTR_TO_LANDLOCK_HANDLE_FS :return true;default :return false;
@@ -978,6 +980,10 @@ static int check_func_arg(struct verifier_env *env, u32 regno, expected_type =PTR_TO_STRUCT_FILE ;if (type != expected_type) goto err_type; + }else if (arg_type ==ARG_CONST_PTR_TO_LANDLOCK_HANDLE_FS ) { + expected_type =CONST_PTR_TO_LANDLOCK_HANDLE_FS ; +if (type != expected_type) + goto err_type; }else if (arg_type ==ARG_PTR_TO_STACK || arg_type ==ARG_PTR_TO_RAW_STACK ) { expected_type =PTR_TO_STACK ;
@@ -1801,6 +1807,8 @@ static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)static inline enum bpf_reg_type bpf_reg_type_from_map (struct bpf_map *map) {switch (map->map_array_type) { +case BPF_MAP_ARRAY_TYPE_LANDLOCK_FS : +return CONST_PTR_TO_LANDLOCK_HANDLE_FS ;case BPF_MAP_ARRAY_TYPE_UNSPEC :default :return CONST_PTR_TO_MAP ;
security/landlock/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := lsm.o +landlock-y := lsm.o checker_fs.o
security/landlock/checker_fs.c new file +179 -0
179 lines not on this page · raw message
security/landlock/checker_fs.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* Landlock LSM - File System Checkers +* +* Copyright (C) 2016 Mickaël Salaün <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +*/ + +#ifndef _SECURITY_LANDLOCK_CHECKER_FS_H +#define _SECURITY_LANDLOCK_CHECKER_FS_H + +#include <linux/fs.h> +#include <linux/seccomp.h> + +extern const struct bpf_func_proto bpf_landlock_cmp_fs_prop_with_struct_file_proto; +extern const struct bpf_func_proto bpf_landlock_cmp_fs_beneath_with_struct_file_proto; + +#endif /* _SECURITY_LANDLOCK_CHECKER_FS_H */
security/landlock/lsm.c +6 -0
@@ -17,6 +17,8 @@#include <linux/lsm_hooks.h> #include <linux/types.h> /* uintptr_t */ +#include "checker_fs.h" +#define LANDLOCK_MAP0 (m, ...)#define LANDLOCK_MAP1 (m, d, t, a) m(d, t, a)#define LANDLOCK_MAP2 (m, d, t, a, ...) m(d, t, a), LANDLOCK_MAP1(m, __VA_ARGS__)
@@ -70,6 +72,10 @@ static const struct bpf_func_proto *bpf_landlock_func_proto( enum bpf_func_id func_id, union bpf_prog_subtype *prog_subtype) {switch (func_id) { +case BPF_FUNC_landlock_cmp_fs_prop_with_struct_file: +return &bpf_landlock_cmp_fs_prop_with_struct_file_proto; +case BPF_FUNC_landlock_cmp_fs_beneath_with_struct_file: +return &bpf_landlock_cmp_fs_beneath_with_struct_file_proto;default :return NULL ; }
Signature · 1 line
2.9.3
Re: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 8 lines · Mickaël Salaün
Add eBPF functions to compare file system access with a Landlock file system handle:
*bpf_landlock_cmp_fs_prop_with_struct_file(prop, map, map_op, file)This function allows to compare the dentry, inode, device or mount point of the currently accessed file, with a reference handle.
*bpf_landlock_cmp_fs_beneath_with_struct_file(opt, map, map_op, file)This function allows an eBPF program to check if the current accessed file is the same or in the hierarchy of a reference handle.
[...]
Quoted 14 lines
kernel/bpf/arraymap.c +3
-0@@ -603,6 +605,9 @@ static void landlock_put_handle(struct map_landlock_handle *handle)enum bpf_map_handle_type handle_type = handle->type;switch (handle_type) { +case BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD : +path_put (&handle->path); +break ;case BPF_MAP_HANDLE_TYPE_UNSPEC :default :WARN_ON (1 );
[...]
Quoted 5 lines
security/landlock/checker_fs.c new file +0
-0
[...]
security/landlock/checker_fs.c line 35
Quoted 11 lines
security/landlock/checker_fs.c new file +11
-0+static inline u64 bpf_landlock_cmp_fs_prop_with_struct_file (u64 r1_property, +u64 r2_map,u64 r3_map_op,u64 r4_file,u64 r5) +{ +u8 property = (u8 ) r1_property; +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r2_map; +enum bpf_map_array_op map_op = r3_map_op; +struct file *file = (struct file *) (unsigned long ) r4_file; + struct bpf_array *array = container_of(map, struct bpf_array, map); +struct path *p1, *p2; +struct map_landlock_handle *handle; +int i;
Please don't use int when iterating over an array, use size_t.
security/landlock/checker_fs.c
Quoted 1 line
security/landlock/checker_fs.c new file +1
-0+/* for now, only handle OP_OR */
Is "OP_OR" an appropriate name for something that ANDs the success of checks?
[...]
security/landlock/checker_fs.c
Quoted 1 line
security/landlock/checker_fs.c new file +1
-0+synchronize_rcu ();
Can you put a comment here that explains what's going on?
Quoted 7 lines
security/landlock/checker_fs.c new file +7
-0+for (i =0 ; i < array->n_entries; i++) { +bool result_dentry = !(property &LANDLOCK_FLAG_FS_DENTRY ); +bool result_inode = !(property &LANDLOCK_FLAG_FS_INODE ); +bool result_device = !(property &LANDLOCK_FLAG_FS_DEVICE ); +bool result_mount = !(property &LANDLOCK_FLAG_FS_MOUNT ); + + handle = (struct map_landlock_handle *)
security/landlock/checker_fs.c line 77
Quoted 10 lines
security/landlock/checker_fs.c new file +10
-0+ (array->value + array->elem_size * i); + +if (handle->type !=BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD ) { +WARN_ON (1 ); +return -EFAULT ; + } + p1 = &handle->path; + +if (!result_dentry && p1->dentry == p2->dentry) + result_dentry = true;
Why is this safe? As far as I can tell, this is not in an RCU read-side critical section (synchronize_rcu() was just called), and no lock has been taken. What prevents someone from removing the arraymap entry while we're looking at it? Am I missing something?
[...]
security/landlock/checker_fs.c line 121
Quoted 11 lines
security/landlock/checker_fs.c new file +11
-0+static inline u64 bpf_landlock_cmp_fs_beneath_with_struct_file (u64 r1_option, +u64 r2_map,u64 r3_map_op,u64 r4_file,u64 r5) +{ +u8 option = (u8 ) r1_option; +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r2_map; +enum bpf_map_array_op map_op = r3_map_op; +struct file *file = (struct file *) (unsigned long ) r4_file; + struct bpf_array *array = container_of(map, struct bpf_array, map); +struct path *p1, *p2; +struct map_landlock_handle *handle; +int i;
As above, please use size_t.
Mickaël Salaün
maintainerRe: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 31 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün
Add eBPF functions to compare file system access with a Landlock file system handle:
*bpf_landlock_cmp_fs_prop_with_struct_file(prop, map, map_op, file)This function allows to compare the dentry, inode, device or mount point of the currently accessed file, with a reference handle.
*bpf_landlock_cmp_fs_beneath_with_struct_file(opt, map, map_op, file)This function allows an eBPF program to check if the current accessed file is the same or in the hierarchy of a reference handle.[...]
Quoted 14 lines
kernel/bpf/arraymap.c +3
-0@@ -603,6 +605,9 @@ static void landlock_put_handle(struct map_landlock_handle *handle)enum bpf_map_handle_type handle_type = handle->type;switch (handle_type) { +case BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD : +path_put (&handle->path); +break ;case BPF_MAP_HANDLE_TYPE_UNSPEC :default :WARN_ON (1 );[...]
Quoted 5 lines
security/landlock/checker_fs.c new file +0
-0[...]
Quoted 13 lines
security/landlock/checker_fs.c line 35
Quoted 11 lines
security/landlock/checker_fs.c new file +11
-0+static inline u64 bpf_landlock_cmp_fs_prop_with_struct_file (u64 r1_property, +u64 r2_map,u64 r3_map_op,u64 r4_file,u64 r5) +{ +u8 property = (u8 ) r1_property; +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r2_map; +enum bpf_map_array_op map_op = r3_map_op; +struct file *file = (struct file *) (unsigned long ) r4_file; + struct bpf_array *array = container_of(map, struct bpf_array, map); +struct path *p1, *p2; +struct map_landlock_handle *handle; +int i;Please don't use int when iterating over an array, use
size_t.
OK, I will use size_t.
Quoted 8 lines
security/landlock/checker_fs.c
Quoted 3 lines
security/landlock/checker_fs.c new file +1
-0+/* for now, only handle OP_OR */ Is "
OP_OR" an appropriate name for something that ANDs the success of checks?
Quoted 4 lines
[...]
security/landlock/checker_fs.c
Quoted 1 line
security/landlock/checker_fs.c new file +1
-0+synchronize_rcu ();Can you put a comment here that explains what's going on?
Hum, this should not be here.
Quoted 16 lines
Quoted 16 lines
security/landlock/checker_fs.c new file +14
-0+for (i =0 ; i < array->n_entries; i++) { +bool result_dentry = !(property &LANDLOCK_FLAG_FS_DENTRY ); +bool result_inode = !(property &LANDLOCK_FLAG_FS_INODE ); +bool result_device = !(property &LANDLOCK_FLAG_FS_DEVICE ); +bool result_mount = !(property &LANDLOCK_FLAG_FS_MOUNT ); + + handle = (struct map_landlock_handle *) + (array->value + array->elem_size * i); + +if (handle->type !=BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD ) { +WARN_ON (1 ); +return -EFAULT ; + } + p1 = &handle->path;
Quoted 8 lines
security/landlock/checker_fs.c line 77
Quoted 3 lines
security/landlock/checker_fs.c new file +3
-0+ +if (!result_dentry && p1->dentry == p2->dentry) + result_dentry = true;Why is this safe? As far as I can tell, this is not in an RCU read-side critical section (
synchronize_rcu()was just called), and no lock has been taken. What prevents someone from removing the arraymap entry while we're looking at it? Am I missing something?
I will try to properly deal with RCU.
Re: [RFC v3 07/22] landlock: Handle file comparisons
In reply to Mickaël SalaünQuoted 24 lines · Mickaël Salaün
Add eBPF functions to compare file system access with a Landlock file system handle:
*bpf_landlock_cmp_fs_prop_with_struct_file(prop, map, map_op, file)This function allows to compare the dentry, inode, device or mount point of the currently accessed file, with a reference handle.
*bpf_landlock_cmp_fs_beneath_with_struct_file(opt, map, map_op, file)This function allows an eBPF program to check if the current accessed file is the same or in the hierarchy of a reference handle.The goal of file system handle is to abstract kernel objects such as a struct file or a struct inode. Userland can create this kind of handle thanks to the
BPF_MAP_UPDATE_ELEMcommand. The element is a structlandlock_handlecontaining the handle type (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) and a file descriptor. This could also be any descriptions able to match a struct file or a struct inode (e.g. path or glob string).Changes since v2:
* addMNT_INTERNALcheck to only add file handle from user-visible FS (e.g. no anonymous inode)
* replace struct file* with struct path* inmap_landlock_handle
* add BPF protos
* fixbpf_landlock_cmp_fs_prop_with_struct_file()
Quoted 9 lines
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <davem-fT/PcQaiUtIeIZ0/[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>
thanks for keeping the links to the previous discussion. Long term it should help, though I worry we already at the point where there are too many outstanding issues to resolve before we can proceed with reasonable code review.
security/landlock/checker_fs.c line 31
Quoted 12 lines
security/landlock/checker_fs.c new file +12
-0+/* +* bpf_landlock_cmp_fs_prop_with_struct_file +* +* Cf. include/uapi/linux/bpf.h +*/ +static inline u64 bpf_landlock_cmp_fs_prop_with_struct_file (u64 r1_property, +u64 r2_map,u64 r3_map_op,u64 r4_file,u64 r5) +{ +u8 property = (u8 ) r1_property; +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r2_map; +enum bpf_map_array_op map_op = r3_map_op; +struct file *file = (struct file *) (unsigned long ) r4_file;
please use just added BPF_CALL_ macros. They will help readability of the above.
Quoted 18 lines
security/landlock/checker_fs.c new file +18
-0+ struct bpf_array *array = container_of(map, struct bpf_array, map); +struct path *p1, *p2; +struct map_landlock_handle *handle; +int i; + +/* ARG_CONST_PTR_TO_LANDLOCK_HANDLE_FS is an arraymap */ +if (unlikely (!map)) { +WARN_ON (1 ); +return -EFAULT ; + } +if (unlikely (!file)) +return -ENOENT ; +if (unlikely ((property | _LANDLOCK_FLAG_FS_MASK) != _LANDLOCK_FLAG_FS_MASK)) +return -EINVAL ; + +/* for now, only handle OP_OR */ +switch (map_op) { +case BPF_MAP_ARRAY_OP_OR :
security/landlock/checker_fs.c line 59
Quoted 10 lines
security/landlock/checker_fs.c new file +10
-0+break ; +case BPF_MAP_ARRAY_OP_UNSPEC : +case BPF_MAP_ARRAY_OP_AND : +case BPF_MAP_ARRAY_OP_XOR : +default : +return -EINVAL ; + } + p2 = &file->f_path; + +synchronize_rcu ();
that is completely broken.
bpf programs are executing under rcu_lock. please enable CONFIG_PROVE_RCU and retest everything.
I would suggest for the next RFC to do minimal 7 patches up to this point with simple example that demonstrates the use case.
I would avoid all unpriv stuff and all of seccomp for the next RFC as well, otherwise I don't think we can realistically make forward progress, since there are too many issues raised in the subsequent patches.
The common part that is mergeable is prog's subtype extension to the verifier that can be used for better tracing and is the common piece of infra needed for both landlock and checmate LSMs (which must be one LSM anyway)
Mickaël Salaün
maintainerRe: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 79 lines · Alexei Starovoitov
Quoted 24 lines · Mickaël Salaün
Add eBPF functions to compare file system access with a Landlock file system handle:
*bpf_landlock_cmp_fs_prop_with_struct_file(prop, map, map_op, file)This function allows to compare the dentry, inode, device or mount point of the currently accessed file, with a reference handle.
*bpf_landlock_cmp_fs_beneath_with_struct_file(opt, map, map_op, file)This function allows an eBPF program to check if the current accessed file is the same or in the hierarchy of a reference handle.The goal of file system handle is to abstract kernel objects such as a struct file or a struct inode. Userland can create this kind of handle thanks to the
BPF_MAP_UPDATE_ELEMcommand. The element is a structlandlock_handlecontaining the handle type (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) and a file descriptor. This could also be any descriptions able to match a struct file or a struct inode (e.g. path or glob string).Changes since v2:
* addMNT_INTERNALcheck to only add file handle from user-visible FS (e.g. no anonymous inode)
* replace struct file* with struct path* inmap_landlock_handle
* add BPF protos
* fixbpf_landlock_cmp_fs_prop_with_struct_file()Quoted 9 lines
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>thanks for keeping the links to the previous discussion. Long term it should help, though I worry we already at the point where there are too many outstanding issues to resolve before we can proceed with reasonable code review.
security/landlock/checker_fs.c line 31
Quoted 12 lines
security/landlock/checker_fs.c new file +12
-0+/* +* bpf_landlock_cmp_fs_prop_with_struct_file +* +* Cf. include/uapi/linux/bpf.h +*/ +static inline u64 bpf_landlock_cmp_fs_prop_with_struct_file (u64 r1_property, +u64 r2_map,u64 r3_map_op,u64 r4_file,u64 r5) +{ +u8 property = (u8 ) r1_property; +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r2_map; +enum bpf_map_array_op map_op = r3_map_op; +struct file *file = (struct file *) (unsigned long ) r4_file;please use just added
BPF_CALL_macros. They will help readability of the above.Quoted 24 lines
security/landlock/checker_fs.c new file +24
-0+ struct bpf_array *array = container_of(map, struct bpf_array, map); +struct path *p1, *p2; +struct map_landlock_handle *handle; +int i; + +/* ARG_CONST_PTR_TO_LANDLOCK_HANDLE_FS is an arraymap */ +if (unlikely (!map)) { +WARN_ON (1 ); +return -EFAULT ; + } +if (unlikely (!file)) +return -ENOENT ; +if (unlikely ((property | _LANDLOCK_FLAG_FS_MASK) != _LANDLOCK_FLAG_FS_MASK)) +return -EINVAL ; + +/* for now, only handle OP_OR */ +switch (map_op) { +case BPF_MAP_ARRAY_OP_OR : +break ; +case BPF_MAP_ARRAY_OP_UNSPEC : +case BPF_MAP_ARRAY_OP_AND : +case BPF_MAP_ARRAY_OP_XOR : +default : +return -EINVAL ;
Quoted 8 lines
security/landlock/checker_fs.c line 59
Quoted 4 lines
security/landlock/checker_fs.c new file +4
-0+ } + p2 = &file->f_path; + +synchronize_rcu ();that is completely broken.
bpf programs are executing underrcu_lock. please enableCONFIG_PROVE_RCUand retest everything.
Thanks for the tip. I will fix this.
Quoted 6 lines
I would suggest for the next RFC to do minimal 7 patches up to this point with simple example that demonstrates the use case.
I would avoid all unpriv stuff and all of seccomp for the next RFC as well, otherwise I don't think we can realistically make forward progress, since there are too many issues raised in the subsequent patches.
I hope we will find a common agreement about seccomp vs cgroup… I think both approaches have their advantages, can be complementary and nicely combined.
Unprivileged sandboxing is the main goal of Landlock. This should not be a problem, even for privileged features, thanks to the new subtype/access.
Quoted 5 lines
The common part that is mergeable is prog's subtype extension to the verifier that can be used for better tracing and is the common piece of infra needed for both landlock and checmate LSMs (which must be one LSM anyway)
Agreed. With this RFC, the Checmate features (i.e. network helpers) should be able to sit on top of Landlock.
Re: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 10 lines · Mickaël Salaün
Quoted 6 lines
I would suggest for the next RFC to do minimal 7 patches up to this point with simple example that demonstrates the use case.
I would avoid all unpriv stuff and all of seccomp for the next RFC as well, otherwise I don't think we can realistically make forward progress, since there are too many issues raised in the subsequent patches.I hope we will find a common agreement about seccomp vs cgroup… I think both approaches have their advantages, can be complementary and nicely combined.
I don't mind having both task based lsm and cgroup based as long as infrastracture is not duplicated and scaling issues from earlier version are resolved.
I'm proposing to do cgroup only for the next RFC, since mine and Sargun's use case for this bpf+lsm+cgroup is _not_ security or sandboxing. No need for unpriv, no_new_priv to cgroups are other things that Andy is concerned about.
Quoted 2 lines
Unprivileged sandboxing is the main goal of Landlock. This should not be a problem, even for privileged features, thanks to the new subtype/access.
yes. the point that unpriv stuff can come later after agreement is reached. If we keep arguing about seccomp details this set won't go anywhere. Even in basic part (which is cgroup+bpf+lsm) are plenty of questions to be still agreed.
Quoted 2 lines
Agreed. With this RFC, the Checmate features (i.e. network helpers) should be able to sit on top of Landlock.
I think neither of them should be called fancy names for no technical reason. We will have only one bpf based lsm. That's it and it doesn't need an obscure name. Directory name can be security/bpf/..stuff.c
Mickaël Salaün
maintainerRe: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Mickaël Salaün
Quoted 6 lines
I would suggest for the next RFC to do minimal 7 patches up to this point with simple example that demonstrates the use case.
I would avoid all unpriv stuff and all of seccomp for the next RFC as well, otherwise I don't think we can realistically make forward progress, since there are too many issues raised in the subsequent patches.
Quoted 7 lines
Quoted 3 lines
I hope we will find a common agreement about seccomp vs cgroup… I think both approaches have their advantages, can be complementary and nicely combined.
I don't mind having both task based lsm and cgroup based as long as infrastracture is not duplicated and scaling issues from earlier version are resolved.
It should be much better with this RFC.
Quoted 2 lines
I'm proposing to do cgroup only for the next RFC, since mine and Sargun's use case for this bpf+lsm+cgroup is _not_ security or sandboxing.
Well, LSM purpose is to do security stuff. The main goal of Landlock is to bring security features to userland, including unprivileged processes, at least via the seccomp interface [1].
Quoted 2 lines
No need for unpriv,
no_new_privto cgroups are other things that Andy is concerned about.
I'm concern about security too! :)
Quoted 8 lines
Quoted 3 lines
Unprivileged sandboxing is the main goal of Landlock. This should not be a problem, even for privileged features, thanks to the new subtype/access.
yes. the point that unpriv stuff can come later after agreement is reached. If we keep arguing about seccomp details this set won't go anywhere. Even in basic part (which is cgroup+bpf+lsm) are plenty of questions to be still agreed.
Using the seccomp(2) (unpriv) *interface* is OK according to a more recent thread [1].
[1]
https://lkml.kernel.org/r/[email protected]
Quoted 7 lines
Quoted 3 lines
Agreed. With this RFC, the Checmate features (i.e. network helpers) should be able to sit on top of Landlock.
I think neither of them should be called fancy names for no technical reason. We will have only one bpf based lsm. That's it and it doesn't need an obscure name. Directory name can be
security/bpf/..stuff.c
I disagree on an LSM named "BPF". I first started with the "seccomp LSM" name (first RFC) but I later realized that it is confusing because seccomp is associated to its syscall and the underlying features. Same thing goes for BPF. It is also artificially hard to grep on a name too used in the kernel source tree.
Making an association between the generic eBPF mechanism and a security centric approach (i.e. LSM) seems a bit reductive (for BPF). Moreover, the seccomp interface [1] can still be used.
Landlock is a nice name to depict a sandbox as an enclave (i.e. a landlocked country/state). I want to keep this name, which is simple, express the goal of Landlock nicely and is comparable to other sandbox mechanisms as Seatbelt or Pledge.
Landlock should not be confused with the underlying eBPF implementation. Landlock could use more than only eBPF in the future and eBPF could be used in other LSM as well.
Mickaël
lsm naming dilemma. Re: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 7 lines · Mickaël Salaün
Quoted 6 lines
Quoted 2 lines
Agreed. With this RFC, the Checmate features (i.e. network helpers) should be able to sit on top of Landlock.
I think neither of them should be called fancy names for no technical reason. We will have only one bpf based lsm. That's it and it doesn't need an obscure name. Directory name can be
security/bpf/..stuff.c
Quoted 8 lines
I disagree on an LSM named "BPF". I first started with the "seccomp LSM" name (first RFC) but I later realized that it is confusing because seccomp is associated to its syscall and the underlying features. Same thing goes for BPF. It is also artificially hard to grep on a name too used in the kernel source tree.
Making an association between the generic eBPF mechanism and a security centric approach (i.e. LSM) seems a bit reductive (for BPF). Moreover, the seccomp interface [1] can still be used.
agree with above.
Quoted 7 lines
Landlock is a nice name to depict a sandbox as an enclave (i.e. a landlocked country/state). I want to keep this name, which is simple, express the goal of Landlock nicely and is comparable to other sandbox mechanisms as Seatbelt or Pledge.
Landlock should not be confused with the underlying eBPF implementation. Landlock could use more than only eBPF in the future and eBPF could be used in other LSM as well.
there will not be two bpf based LSMs.
Therefore unless you can convince Sargun to give up his 'checmate' name, nothing goes in.
The features you both need are 90% the same, so they must be done as part of single LSM whatever you both agree to call it.
Re: lsm naming dilemma. Re: [RFC v3 07/22] landlock: Handle file comparisons
I'm fine giving up the Checmate name. Landlock seems easy enough to Google. I haven't gotten a chance to look through the entire patchset yet, but it does seem like they are somewhat similar.
Quoted 33 lines · Alexei Starovoitov
Quoted 7 lines · Mickaël Salaün
Quoted 6 lines
Quoted 2 lines
Agreed. With this RFC, the Checmate features (i.e. network helpers) should be able to sit on top of Landlock.
I think neither of them should be called fancy names for no technical reason. We will have only one bpf based lsm. That's it and it doesn't need an obscure name. Directory name can be
security/bpf/..stuff.cQuoted 8 lines
I disagree on an LSM named "BPF". I first started with the "seccomp LSM" name (first RFC) but I later realized that it is confusing because seccomp is associated to its syscall and the underlying features. Same thing goes for BPF. It is also artificially hard to grep on a name too used in the kernel source tree.
Making an association between the generic eBPF mechanism and a security centric approach (i.e. LSM) seems a bit reductive (for BPF). Moreover, the seccomp interface [1] can still be used.agree with above.
Quoted 7 lines
Landlock is a nice name to depict a sandbox as an enclave (i.e. a landlocked country/state). I want to keep this name, which is simple, express the goal of Landlock nicely and is comparable to other sandbox mechanisms as Seatbelt or Pledge.
Landlock should not be confused with the underlying eBPF implementation. Landlock could use more than only eBPF in the future and eBPF could be used in other LSM as well.there will not be two bpf based LSMs.
Therefore unless you can convince Sargun to give up his 'checmate' name, nothing goes in.
The features you both need are 90% the same, so they must be done as part of single LSM whatever you both agree to call it.
Mickaël Salaün
maintainerRe: lsm naming dilemma. Re: [RFC v3 07/22] landlock: Handle file comparisons
Quoted 3 lines · Sargun Dhillon
I'm fine giving up the Checmate name. Landlock seems easy enough to Google. I haven't gotten a chance to look through the entire patchset yet, but it does seem like they are somewhat similar.
Excellent! I'm looking forward for your review.
Quoted 37 lines
Quoted 33 lines · Alexei Starovoitov
Quoted 7 lines · Mickaël Salaün
Quoted 6 lines
Quoted 2 lines
Agreed. With this RFC, the Checmate features (i.e. network helpers) should be able to sit on top of Landlock.
I think neither of them should be called fancy names for no technical reason. We will have only one bpf based lsm. That's it and it doesn't need an obscure name. Directory name can be
security/bpf/..stuff.cQuoted 8 lines
I disagree on an LSM named "BPF". I first started with the "seccomp LSM" name (first RFC) but I later realized that it is confusing because seccomp is associated to its syscall and the underlying features. Same thing goes for BPF. It is also artificially hard to grep on a name too used in the kernel source tree.
Making an association between the generic eBPF mechanism and a security centric approach (i.e. LSM) seems a bit reductive (for BPF). Moreover, the seccomp interface [1] can still be used.agree with above.
Quoted 7 lines
Landlock is a nice name to depict a sandbox as an enclave (i.e. a landlocked country/state). I want to keep this name, which is simple, express the goal of Landlock nicely and is comparable to other sandbox mechanisms as Seatbelt or Pledge.
Landlock should not be confused with the underlying eBPF implementation. Landlock could use more than only eBPF in the future and eBPF could be used in other LSM as well.there will not be two bpf based LSMs.
Therefore unless you can convince Sargun to give up his 'checmate' name, nothing goes in.
The features you both need are 90% the same, so they must be done as part of single LSM whatever you both agree to call it.
Re: [RFC v3 07/22] landlock: Handle file comparisons
In reply to Mickaël SalaünQuoted 83 lines · Mickaël Salaün
Add eBPF functions to compare file system access with a Landlock file system handle:
*bpf_landlock_cmp_fs_prop_with_struct_file(prop, map, map_op, file)This function allows to compare the dentry, inode, device or mount point of the currently accessed file, with a reference handle.
*bpf_landlock_cmp_fs_beneath_with_struct_file(opt, map, map_op, file)This function allows an eBPF program to check if the current accessed file is the same or in the hierarchy of a reference handle.The goal of file system handle is to abstract kernel objects such as a struct file or a struct inode. Userland can create this kind of handle thanks to the
BPF_MAP_UPDATE_ELEMcommand. The element is a structlandlock_handlecontaining the handle type (e.g.BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD) and a file descriptor. This could also be any descriptions able to match a struct file or a struct inode (e.g. path or glob string).Changes since v2:
* addMNT_INTERNALcheck to only add file handle from user-visible FS (e.g. no anonymous inode)
* replace struct file* with struct path* inmap_landlock_handle
* add BPF protos
* fixbpf_landlock_cmp_fs_prop_with_struct_file()Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David S. Miller <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>--- include/linux/bpf.h | 10 +++ include/uapi/linux/bpf.h | 49 +++++++++++ kernel/bpf/arraymap.c | 21 +++++ kernel/bpf/verifier.c | 8 ++ security/landlock/Makefile | 2 +- security/landlock/checker_fs.c | 179 +++++++++++++++++++++++++++++++++++++++++ security/landlock/checker_fs.h | 20 +++++ security/landlock/lsm.c | 6 ++ 8 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 security/landlock/checker_fs.c create mode 100644 security/landlock/checker_fs.h[...]
security/landlock/checker_fs.c new file +31
-0@@ -0,0 +1,179 @@ +/* +* Landlock LSM - File System Checkers +* +* Copyright (C) 2016 Mickaël Salaün <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +*/ + +#include <linux/bpf.h> /* enum bpf_map_array_op */ +#include <linux/errno.h> +#include <linux/fs.h> /* path_is_under() */ +#include <linux/path.h> /* struct path */ + +#include "checker_fs.h" + +#define EQUAL_NOT_NULL (a, b) (a && a == b) + +/* +* bpf_landlock_cmp_fs_prop_with_struct_file +* +* Cf. include/uapi/linux/bpf.h +*/ +static inline u64 bpf_landlock_cmp_fs_prop_with_struct_file (u64 r1_property, +u64 r2_map,u64 r3_map_op,u64 r4_file,u64 r5) +{ +u8 property = (u8 ) r1_property; +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r2_map; +enum bpf_map_array_op map_op = r3_map_op; +struct file *file = (struct file *) (unsigned long ) r4_file;
security/landlock/checker_fs.c line 41
Quoted 10 lines
security/landlock/checker_fs.c new file +10
-0+ struct bpf_array *array = container_of(map, struct bpf_array, map); +struct path *p1, *p2; +struct map_landlock_handle *handle; +int i; + +/* ARG_CONST_PTR_TO_LANDLOCK_HANDLE_FS is an arraymap */ +if (unlikely (!map)) { +WARN_ON (1 ); +return -EFAULT ; + }
Just some minor style/readability nits...
This is more readable as:
if (WARN_ON(!map))
return -EFAULT;
(WARN_ON already includes the unlikely() and passes through the test result.)
Quoted 22 lines
security/landlock/checker_fs.c new file +22
-0+if (unlikely (!file)) +return -ENOENT ; +if (unlikely ((property | _LANDLOCK_FLAG_FS_MASK) != _LANDLOCK_FLAG_FS_MASK)) +return -EINVAL ; + +/* for now, only handle OP_OR */ +switch (map_op) { +case BPF_MAP_ARRAY_OP_OR : +break ; +case BPF_MAP_ARRAY_OP_UNSPEC : +case BPF_MAP_ARRAY_OP_AND : +case BPF_MAP_ARRAY_OP_XOR : +default : +return -EINVAL ; + } + p2 = &file->f_path; + +synchronize_rcu (); + +for (i =0 ; i < array->n_entries; i++) { +bool result_dentry = !(property &LANDLOCK_FLAG_FS_DENTRY ); +bool result_inode = !(property &LANDLOCK_FLAG_FS_INODE );
security/landlock/checker_fs.c line 73
Quoted 10 lines
security/landlock/checker_fs.c new file +10
-0+bool result_device = !(property &LANDLOCK_FLAG_FS_DEVICE ); +bool result_mount = !(property &LANDLOCK_FLAG_FS_MOUNT ); + + handle = (struct map_landlock_handle *) + (array->value + array->elem_size * i); + +if (handle->type !=BPF_MAP_HANDLE_TYPE_LANDLOCK_FS_FD ) { +WARN_ON (1 ); +return -EFAULT ; + }
Same here... and in the other function (much of which seems to repeat -- can some of these checks be put into common functions?)
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Mickaël Salaün
maintainerRFC v3 8/22 seccomp: Fix documentation for struct seccomp_filter
In reply to Mickaël Salaün--- kernel/seccomp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/seccomp.c +1 -2
@@ -41,8 +41,7 @@* outside of a lifetime-guarded section. In general, this * is only needed for handling filters shared across tasks. * @prev: points to a previously installed, or inherited, filter -* @len: the number of instructions in the program -* @insnsi: the BPF program instructions to evaluate +* @prog: the BPF program to evaluate * * seccomp_filter objects are organized in a tree linked via the @prev * pointer. For any task, it appears to be a singly-linked list starting
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 9/22 seccomp: Move struct seccomp_filter in seccomp.h
In reply to Mickaël SalaünSet struct seccomp_filter public because of the next use of the new field thread_prev added for Landlock LSM.
--- include/linux/seccomp.h | 27 ++++++++++++++++++++++++++- kernel/seccomp.c | 26 -------------------------- 2 files changed, 26 insertions(+), 27 deletions(-)
include/linux/seccomp.h +26 -1
@@ -10,7 +10,32 @@#include <linux/thread_info.h> #include <asm/seccomp.h> -struct seccomp_filter ; +/** +* struct seccomp_filter - container for seccomp BPF programs +* +* @usage: reference count to manage the object lifetime. +* get/put helpers should be used when accessing an instance +* outside of a lifetime-guarded section. In general, this +* is only needed for handling filters shared across tasks. +* @prev: points to a previously installed, or inherited, filter +* @prog: the BPF program to evaluate +* +* seccomp_filter objects are organized in a tree linked via the @prev +* pointer. For any task, it appears to be a singly-linked list starting +* with current->seccomp.filter, the most recently attached or inherited filter. +* However, multiple filters may share a @prev node, by way of fork(), which +* results in a unidirectional tree existing in memory. This is similar to +* how namespaces work. +* +* seccomp_filter objects should never be modified after being attached +* to a task_struct (other than @usage). +*/ +struct seccomp_filter { +atomic_t usage; +struct seccomp_filter *prev; +struct bpf_prog *prog; +}; +/** * struct seccomp - the state of a seccomp'ed process *
kernel/seccomp.c +0 -26
@@ -33,32 +33,6 @@#include <linux/tracehook.h> #include <linux/uaccess.h> -/** -* struct seccomp_filter - container for seccomp BPF programs -* -* @usage: reference count to manage the object lifetime. -* get/put helpers should be used when accessing an instance -* outside of a lifetime-guarded section. In general, this -* is only needed for handling filters shared across tasks. -* @prev: points to a previously installed, or inherited, filter -* @prog: the BPF program to evaluate -* -* seccomp_filter objects are organized in a tree linked via the @prev -* pointer. For any task, it appears to be a singly-linked list starting -* with current->seccomp.filter, the most recently attached or inherited filter. -* However, multiple filters may share a @prev node, by way of fork(), which -* results in a unidirectional tree existing in memory. This is similar to -* how namespaces work. -* -* seccomp_filter objects should never be modified after being attached -* to a task_struct (other than @usage). -*/ -struct seccomp_filter { -atomic_t usage; -struct seccomp_filter *prev; -struct bpf_prog *prog; -}; -/* Limit any path through the tree to 256KB worth of instructions. */ #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 10/22 seccomp: Split put_seccomp_filter() with put_seccomp()
In reply to Mickaël SalaünThe semantic is unchanged. This will be useful for the Landlock integration with seccomp (next commit).
--- include/linux/seccomp.h | 5 +++-- kernel/fork.c | 2 +- kernel/seccomp.c | 18 +++++++++++++----- 3 files changed, 17 insertions(+), 8 deletions(-)
include/linux/seccomp.h +3 -2
@@ -102,13 +102,14 @@ static inline int seccomp_mode(struct seccomp *s)#endif /* CONFIG_SECCOMP */ #ifdef CONFIG_SECCOMP_FILTER -extern void put_seccomp_filter (struct task_struct *tsk); +extern void put_seccomp (struct task_struct *tsk);extern void get_seccomp_filter (struct task_struct *tsk);#else /* CONFIG_SECCOMP_FILTER */ -static inline void put_seccomp_filter (struct task_struct *tsk) +static inline void put_seccomp (struct task_struct *tsk) {return ; } +static inline void get_seccomp_filter (struct task_struct *tsk) {return ;
kernel/fork.c +1 -1
@@ -276,7 +276,7 @@ void free_task(struct task_struct *tsk)free_thread_stack (tsk);rt_mutex_debug_task_free (tsk);ftrace_graph_exit_task (tsk); -put_seccomp_filter (tsk); +put_seccomp (tsk);arch_release_task_struct (tsk);free_task_struct (tsk); }
kernel/seccomp.c +13 -5
@@ -36,6 +36,8 @@/* Limit any path through the tree to 256KB worth of instructions. */ #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) +static void put_seccomp_filter (struct seccomp_filter *filter); +/* * Endianness is explicitly ignored and left for BPF program authors to manage * as per the specific architecture.
@@ -286,7 +288,7 @@ static inline void seccomp_sync_threads(void)* current's path will hold a reference. (This also * allows a put before the assignment.) */ -put_seccomp_filter (thread); +put_seccomp_filter (thread->seccomp.filter);smp_store_release (&thread->seccomp.filter, caller->seccomp.filter);
@@ -448,10 +450,11 @@ static inline void seccomp_filter_free(struct seccomp_filter *filter) } } -/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ -void put_seccomp_filter (struct task_struct *tsk) +/* put_seccomp_filter - decrements the ref count of a filter */ +static void put_seccomp_filter (struct seccomp_filter *filter) { -struct seccomp_filter *orig = tsk->seccomp.filter; +struct seccomp_filter *orig = filter; +/* Clean up single-reference branches iteratively. */ while (orig &&atomic_dec_and_test (&orig->usage)) {struct seccomp_filter *freeme = orig;
@@ -460,6 +463,11 @@ void put_seccomp_filter(struct task_struct *tsk) } } +void put_seccomp (struct task_struct *tsk) +{ +put_seccomp_filter (tsk->seccomp.filter); +} +/** * seccomp_send_sigsys - signals the task to allow in-process syscall emulation * @syscall: syscall number to send to userland
@@ -871,7 +879,7 @@ long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,if (copy_to_user (data, fprog->filter,bpf_classic_proglen (fprog))) ret = -EFAULT ; -put_seccomp_filter (task); +put_seccomp_filter (task->seccomp.filter);return ret; out:
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 11/22 seccomp,landlock: Handle Landlock hooks per process hierarchy
In reply to Mickaël SalaünA Landlock program will be triggered according to its subtype/origin bitfield. The LANDLOCK_FLAG_ORIGIN_SECCOMP value will trigger the Landlock program when a seccomp filter will return RET_LANDLOCK. Moreover, it is possible to return a 16-bit cookie which will be readable by the Landlock programs in its context.
Only seccomp filters loaded from the same thread and before a Landlock program can trigger it through LANDLOCK_FLAG_ORIGIN_SECCOMP. Multiple Landlock programs can be triggered by one or more seccomp filters. This way, each RET_LANDLOCK (with specific cookie) will trigger all the allowed Landlock programs once.
Changes since v2:
* Landlock programs can now be run without seccomp filter but for any syscall (from the process) or interruption
* move Landlock related functions and structs into security/landlock/* (to manage cgroups as well)
* fix seccomp filter handling: run Landlock programs for each of their legitimate seccomp filter
* properly clean up all seccomp results
* cosmetic changes to ease the understanding
* fix some ifdef
--- include/linux/landlock.h | 77 ++++++++++++++ include/linux/seccomp.h | 26 +++++ include/uapi/linux/seccomp.h | 2 + kernel/fork.c | 23 +++- kernel/seccomp.c | 68 +++++++++++- security/landlock/Makefile | 2 +- security/landlock/common.h | 27 +++++ security/landlock/lsm.c | 96 ++++++++++++++++- security/landlock/manager.c | 242 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 552 insertions(+), 11 deletions(-) create mode 100644 include/linux/landlock.h create mode 100644 security/landlock/common.h create mode 100644 security/landlock/manager.c
include/linux/landlock.h new file +77 -0
@@ -0,0 +1,77 @@ +/* +* Landlock LSM - Public headers +* +* Copyright (C) 2016 Mickaël Salaün <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +*/ + +#ifndef _LINUX_LANDLOCK_H +#define _LINUX_LANDLOCK_H +#ifdef CONFIG_SECURITY_LANDLOCK + +#include <linux/bpf.h> /* _LANDLOCK_HOOK_LAST */ +#include <linux/types.h> /* atomic_t */ + +#ifdef CONFIG_SECCOMP_FILTER +#include <linux/seccomp.h> /* struct seccomp_filter */ +#endif /* CONFIG_SECCOMP_FILTER */ + + +#ifdef CONFIG_SECCOMP_FILTER +struct landlock_seccomp_ret { +struct landlock_seccomp_ret *prev; +struct seccomp_filter *filter; +u16 cookie; +bool triggered; +}; +#endif /* CONFIG_SECCOMP_FILTER */ + +struct landlock_rule { +atomic_t usage; +struct landlock_rule *prev; +/* +* List of filters (through filter->thread_prev) allowed to trigger +* this Landlock program. +*/ +struct bpf_prog *prog; +#ifdef CONFIG_SECCOMP_FILTER +struct seccomp_filter *thread_filter; +#endif /* CONFIG_SECCOMP_FILTER */ +}; + +/** +* struct landlock_hooks - Landlock hook programs enforced on a thread +* +* This is used for low performance impact when forking a process. Instead of +* copying the full array and incrementing the usage field of each entries, +* only create a pointer to struct landlock_hooks and increment the usage +* field. +* +* A new struct landlock_hooks must be created thanks to a call to +* new_landlock_hooks(). +* +* @usage: reference count to manage the object lifetime. When a thread need to +* add Landlock programs and if @usage is greater than 1, then the +* thread must duplicate struct landlock_hooks to not change the +* children' rules as well. +*/ +struct landlock_hooks { +atomic_t usage; +struct landlock_rule *rules[_LANDLOCK_HOOK_LAST]; +}; + + +struct landlock_hooks *new_landlock_hooks (void ); +void put_landlock_hooks (struct landlock_hooks *hooks); + +#ifdef CONFIG_SECCOMP_FILTER +void put_landlock_ret (struct landlock_seccomp_ret *landlock_ret); +int landlock_seccomp_set_hook (unsigned int flags, +const char __user *user_bpf_fd); +#endif /* CONFIG_SECCOMP_FILTER */ + +#endif /* CONFIG_SECURITY_LANDLOCK */ +#endif /* _LINUX_LANDLOCK_H */
include/linux/seccomp.h +26 -0
@@ -10,6 +10,10 @@#include <linux/thread_info.h> #include <asm/seccomp.h> +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +#include <linux/landlock.h> +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ +/** * struct seccomp_filter - container for seccomp BPF programs *
@@ -19,6 +23,7 @@* is only needed for handling filters shared across tasks. * @prev: points to a previously installed, or inherited, filter * @prog: the BPF program to evaluate +* @thread_prev: points to filters installed by the same thread * * seccomp_filter objects are organized in a tree linked via the @prev * pointer. For any task, it appears to be a singly-linked list starting
@@ -34,6 +39,9 @@ struct seccomp_filter {atomic_t usage;struct seccomp_filter *prev;struct bpf_prog *prog; +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct seccomp_filter *thread_prev; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ };/**
@@ -43,6 +51,11 @@ struct seccomp_filter {* system calls available to a process. * @filter: must always point to a valid seccomp-filter or NULL as it is * accessed without locking during system call entry. +* @thread_filter: list of filters allowed to trigger an associated Landlock +* hook via a RET_LANDLOCK; must walk through thread_prev. +* @landlock_ret: one unique private list per thread storing the RET_LANDLOCK +* values of all filters. +* @landlock_hooks: contains an array of Landlock programs. * * @filter must only be accessed from the context of current as there * is no read locking.
@@ -50,6 +63,12 @@ struct seccomp_filter {struct seccomp {int mode;struct seccomp_filter *filter; + +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct seccomp_filter *thread_filter; +struct landlock_seccomp_ret *landlock_ret; +struct landlock_hooks *landlock_hooks; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ };#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
@@ -103,13 +122,20 @@ static inline int seccomp_mode(struct seccomp *s)#ifdef CONFIG_SECCOMP_FILTER extern void put_seccomp (struct task_struct *tsk); +extern void put_seccomp_filter (struct seccomp_filter *filter);extern void get_seccomp_filter (struct task_struct *tsk); +#else /* CONFIG_SECCOMP_FILTER */ static inline void put_seccomp (struct task_struct *tsk) {return ; } +static void put_seccomp_filter (struct seccomp_filter *filter) +{ +return ; +} +static inline void get_seccomp_filter (struct task_struct *tsk) {return ;
include/uapi/linux/seccomp.h +2 -0
@@ -13,6 +13,7 @@/* Valid operations for seccomp syscall. */ #define SECCOMP_SET_MODE_STRICT 0#define SECCOMP_SET_MODE_FILTER 1 +#define SECCOMP_SET_LANDLOCK_HOOK 2/* Valid flags for SECCOMP_SET_MODE_FILTER */ #define SECCOMP_FILTER_FLAG_TSYNC 1
@@ -28,6 +29,7 @@#define SECCOMP_RET_KILL 0x00000000U/* kill the task immediately */ #define SECCOMP_RET_TRAP 0x00030000U/* disallow and force a SIGSYS */ #define SECCOMP_RET_ERRNO 0x00050000U/* returns an errno */ +#define SECCOMP_RET_LANDLOCK 0x00070000U/* trigger Landlock LSM */ #define SECCOMP_RET_TRACE 0x7ff00000U/* pass to a tracer or disallow */ #define SECCOMP_RET_ALLOW 0x7fff0000U/* allow */
kernel/fork.c +19 -4
@@ -429,7 +429,12 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)* the usage counts on the error path calling free_task. */ tsk->seccomp.filter =NULL ; -#endif +#ifdef CONFIG_SECURITY_LANDLOCK + tsk->seccomp.thread_filter =NULL ; + tsk->seccomp.landlock_ret =NULL ; + tsk->seccomp.landlock_hooks =NULL ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#endif /* CONFIG_SECCOMP */ setup_thread_stack (tsk, orig);clear_user_return_notifier (tsk);
@@ -1284,7 +1289,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)return 0 ; } -static void copy_seccomp (struct task_struct *p) +static int copy_seccomp (struct task_struct *p) {#ifdef CONFIG_SECCOMP /*
@@ -1297,7 +1302,14 @@ static void copy_seccomp(struct task_struct *p)/* Ref-count the new filter user, and assign it. */ get_seccomp_filter (current); - p->seccomp = current->seccomp; + p->seccomp.mode = current->seccomp.mode; + p->seccomp.filter = current->seccomp.filter; +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +/* No copy for thread_filter nor landlock_ret. */ + p->seccomp.landlock_hooks = current->seccomp.landlock_hooks; +if (p->seccomp.landlock_hooks) +atomic_inc (&p->seccomp.landlock_hooks->usage); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ /* * Explicitly enable no_new_privs here in case it got set
@@ -1315,6 +1327,7 @@ static void copy_seccomp(struct task_struct *p)if (p->seccomp.mode !=SECCOMP_MODE_DISABLED )set_tsk_thread_flag (p,TIF_SECCOMP );#endif +return 0 ; } SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
@@ -1674,7 +1687,9 @@ static __latent_entropy struct task_struct *copy_process(* Copy seccomp details explicitly here, in case they were changed * before holding sighand lock. */ -copy_seccomp (p); + retval =copy_seccomp (p); +if (retval) + goto bad_fork_cancel_cgroup;/* * Process group and session signals need to be delivered to just the
kernel/seccomp.c +64 -4
134 lines not on this page · raw message
security/landlock/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := lsm.o checker_fs.o +landlock-y := lsm.o manager.o checker_fs.o
security/landlock/common.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Landlock LSM - private headers +* +* Copyright (C) 2016 Mickaël Salaün <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2, as +* published by the Free Software Foundation. +*/ + +#ifndef _SECURITY_LANDLOCK_COMMON_H +#define _SECURITY_LANDLOCK_COMMON_H + +#include <linux/bpf.h> /* enum landlock_hook_id */ + +/** +* get_index - get an index for the rules of struct landlock_hooks +* +* @hook_id: a Landlock hook ID +*/ +static inline int get_index (enum landlock_hook_id hook_id) +{ +/* hook ID > 0 for loaded programs */ +return hook_id -1 ; +} + +#endif /* _SECURITY_LANDLOCK_COMMON_H */
security/landlock/lsm.c +94 -2
121 lines not on this page · raw message
security/landlock/manager.c new file +242 -0
242 lines not on this page · raw message
Signature · 1 line
2.9.3
Andy Lutomirski
reviewerRe: [RFC v3 11/22] seccomp,landlock: Handle Landlock hooks per process hierarchy
Quoted 5 lines · Mickaël Salaün
A Landlock program will be triggered according to its subtype/origin bitfield. The
LANDLOCK_FLAG_ORIGIN_SECCOMPvalue will trigger the Landlock program when a seccomp filter will returnRET_LANDLOCK. Moreover, it is possible to return a 16-bit cookie which will be readable by the Landlock programs in its context.
Are you envisioning that the filters will return RET_LANDLOCK most of the time or rarely? If it's most of the time, then maybe this could be simplified a bit by unconditionally calling the landlock filter and letting the landlock filter access a struct seccomp_data if needed.
Quoted 6 lines
Only seccomp filters loaded from the same thread and before a Landlock program can trigger it through
LANDLOCK_FLAG_ORIGIN_SECCOMP. Multiple Landlock programs can be triggered by one or more seccomp filters. This way, eachRET_LANDLOCK(with specific cookie) will trigger all the allowed Landlock programs once.
This interface seems somewhat awkward. Should we not have a way to atomicaly install a whole pile of landlock filters and associated seccomp filter all at once?
--Andy
Mickaël Salaün
maintainerRe: [RFC v3 11/22] seccomp,landlock: Handle Landlock hooks per process hierarchy
Quoted 11 lines · Andy Lutomirski
Quoted 5 lines · Mickaël Salaün
A Landlock program will be triggered according to its subtype/origin bitfield. The
LANDLOCK_FLAG_ORIGIN_SECCOMPvalue will trigger the Landlock program when a seccomp filter will returnRET_LANDLOCK. Moreover, it is possible to return a 16-bit cookie which will be readable by the Landlock programs in its context.Are you envisioning that the filters will return
RET_LANDLOCKmost of the time or rarely? If it's most of the time, then maybe this could be simplified a bit by unconditionally calling the landlock filter and letting the landlock filter access a structseccomp_dataif needed.
Exposing seccomp_data in a Landlock context may be a good idea. The main implication is that Landlock programs may then be architecture specific (if dealing with data) as seccomp filters are. Another point is that it remove any direct binding between seccomp filters and Landlock programs. I will try this (more simple) approach.
Quoted 11 lines
Quoted 7 lines
Only seccomp filters loaded from the same thread and before a Landlock program can trigger it through
LANDLOCK_FLAG_ORIGIN_SECCOMP. Multiple Landlock programs can be triggered by one or more seccomp filters. This way, eachRET_LANDLOCK(with specific cookie) will trigger all the allowed Landlock programs once.This interface seems somewhat awkward. Should we not have a way to atomicaly install a whole pile of landlock filters and associated seccomp filter all at once?
I can change the seccomp(2) use in this way: instead of loading a Landlock program, (atomically) load an array of Landlock programs.
However, exposing seccomp_data to Landlock programs looks like a better way to deal with it. This does not needs to manage an array of Landlock programs.
Mickaël
Re: [RFC v3 11/22] seccomp,landlock: Handle Landlock hooks per process hierarchy
Quoted 14 lines · Mickaël Salaün
Quoted 11 lines · Andy Lutomirski
Quoted 5 lines · Mickaël Salaün
A Landlock program will be triggered according to its subtype/origin bitfield. The
LANDLOCK_FLAG_ORIGIN_SECCOMPvalue will trigger the Landlock program when a seccomp filter will returnRET_LANDLOCK. Moreover, it is possible to return a 16-bit cookie which will be readable by the Landlock programs in its context.Are you envisioning that the filters will return
RET_LANDLOCKmost of the time or rarely? If it's most of the time, then maybe this could be simplified a bit by unconditionally calling the landlock filter and letting the landlock filter access a structseccomp_dataif needed.
Quoted 5 lines
Exposing
seccomp_datain a Landlock context may be a good idea. The main implication is that Landlock programs may then be architecture specific (if dealing with data) as seccomp filters are. Another point is that it remove any direct binding between seccomp filters and Landlock programs. I will try this (more simple) approach.
Yeah, I would prefer that the seccomp code isn't doing list management to identify the landlock hooks to trigger, etc. I think that's better done on the LSM side. And since multiple seccomp filters could trigger landlock, it may be best to just leave the low 16 bits unused entirely. Then all state management is handled by the landlock eBPF maps, not a value coming from seccomp that can get stomped on by new filters, etc.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Mickaël Salaün
maintainerRe: [RFC v3 11/22] seccomp,landlock: Handle Landlock hooks per process hierarchy
Quoted 21 lines · Kees Cook
Quoted 14 lines · Mickaël Salaün
Quoted 11 lines · Andy Lutomirski
Quoted 5 lines · Mickaël Salaün
A Landlock program will be triggered according to its subtype/origin bitfield. The
LANDLOCK_FLAG_ORIGIN_SECCOMPvalue will trigger the Landlock program when a seccomp filter will returnRET_LANDLOCK. Moreover, it is possible to return a 16-bit cookie which will be readable by the Landlock programs in its context.Are you envisioning that the filters will return
RET_LANDLOCKmost of the time or rarely? If it's most of the time, then maybe this could be simplified a bit by unconditionally calling the landlock filter and letting the landlock filter access a structseccomp_dataif needed.Quoted 5 lines
Exposing
seccomp_datain a Landlock context may be a good idea. The main implication is that Landlock programs may then be architecture specific (if dealing with data) as seccomp filters are. Another point is that it remove any direct binding between seccomp filters and Landlock programs. I will try this (more simple) approach.
Quoted 7 lines
Yeah, I would prefer that the seccomp code isn't doing list management to identify the landlock hooks to trigger, etc. I think that's better done on the LSM side. And since multiple seccomp filters could trigger landlock, it may be best to just leave the low 16 bits unused entirely. Then all state management is handled by the landlock eBPF maps, not a value coming from seccomp that can get stomped on by new filters, etc.
Right, this approach should be simpler, more efficient and independent from seccomp. This will be in the next RFC.
Mickaël Salaün
maintainerRFC v3 12/22 bpf: Cosmetic change for bpf_prog_attach()
In reply to Mickaël SalaünMove code outside a switch/case to ease code factoring (cf. next commit).
This apply on Daniel Mack's "Add eBPF hooks for cgroups":
https://lkml.kernel.org/r/[email protected]
--- kernel/bpf/syscall.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
kernel/bpf/syscall.c +12 -11
@@ -843,23 +843,24 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_CGROUP_INET_EGRESS : prog =bpf_prog_get_type (attr->attach_bpf_fd,BPF_PROG_TYPE_CGROUP_SOCKET ); -if (IS_ERR (prog)) -return PTR_ERR (prog); - - cgrp =cgroup_get_from_fd (attr->target_fd); -if (IS_ERR (cgrp)) { -bpf_prog_put (prog); -return PTR_ERR (cgrp); - } - -cgroup_bpf_update (cgrp, prog, attr->attach_type); -cgroup_put (cgrp);break ;default :return -EINVAL ; } +if (IS_ERR (prog)) +return PTR_ERR (prog); + + cgrp =cgroup_get_from_fd (attr->target_fd); +if (IS_ERR (cgrp)) { +bpf_prog_put (prog); +return PTR_ERR (cgrp); + } + +cgroup_bpf_update (cgrp, prog, attr->attach_type); +cgroup_put (cgrp); +return 0 ; }
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 13/22 bpf/cgroup: Replace struct bpf_prog with union bpf_object
In reply to Mickaël SalaünThis allows CONFIG_CGROUP_BPF to manage different type of pointers instead of only eBPF programs. This will be useful for the next commits to support Landlock with cgroups.
--- include/linux/bpf-cgroup.h | 8 ++++++-- kernel/bpf/cgroup.c | 44 +++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 23 deletions(-)
include/linux/bpf-cgroup.h +6 -2
@@ -14,14 +14,18 @@ struct sk_buff;extern struct static_key_false cgroup_bpf_enabled_key;#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) +union bpf_object { +struct bpf_prog *prog; +}; +struct cgroup_bpf {/* * Store two sets of bpf_prog pointers, one for programs that are * pinned directly to this cgroup, and one for those that are effective * when this cgroup is accessed. */ -struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE ]; -struct bpf_prog *effective[MAX_BPF_ATTACH_TYPE ]; +union bpf_object pinned[MAX_BPF_ATTACH_TYPE ]; +union bpf_object effective[MAX_BPF_ATTACH_TYPE ]; };void cgroup_bpf_put (struct cgroup *cgrp);
kernel/bpf/cgroup.c +23 -21
91 lines not on this page · raw message
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 14/22 bpf/cgroup: Make cgroup_bpf_update() return an error code
In reply to Mickaël SalaünThis will be useful to support Landlock for the next commits.
--- include/linux/bpf-cgroup.h | 4 ++-- kernel/bpf/cgroup.c | 3 ++- kernel/bpf/syscall.c | 10 ++++++---- kernel/cgroup.c | 6 ++++-- 4 files changed, 14 insertions(+), 9 deletions(-)
include/linux/bpf-cgroup.h +2 -2
@@ -31,13 +31,13 @@ struct cgroup_bpf {void cgroup_bpf_put (struct cgroup *cgrp);void cgroup_bpf_inherit (struct cgroup *cgrp,struct cgroup *parent); -void __cgroup_bpf_update (struct cgroup *cgrp, +int __cgroup_bpf_update (struct cgroup *cgrp,struct cgroup *parent,struct bpf_prog *prog,enum bpf_attach_type type);/* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */ -void cgroup_bpf_update (struct cgroup *cgrp, +int cgroup_bpf_update (struct cgroup *cgrp,struct bpf_prog *prog,enum bpf_attach_type type);
kernel/bpf/cgroup.c +2 -1
@@ -83,7 +83,7 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent)* * Must be called with cgroup_mutex held. */ -void __cgroup_bpf_update (struct cgroup *cgrp, +int __cgroup_bpf_update (struct cgroup *cgrp,struct cgroup *parent,struct bpf_prog *prog,enum bpf_attach_type type)
@@ -117,6 +117,7 @@ void __cgroup_bpf_update(struct cgroup *cgrp,bpf_prog_put (old_pinned.prog);static_branch_dec (&cgroup_bpf_enabled_key); } +return 0 ; }/**
kernel/bpf/syscall.c +6 -4
@@ -831,6 +831,7 @@ static int bpf_prog_attach(const union bpf_attr *attr) {struct bpf_prog *prog;struct cgroup *cgrp; +int result;if (!capable (CAP_NET_ADMIN ))return -EPERM ;
@@ -858,10 +859,10 @@ static int bpf_prog_attach(const union bpf_attr *attr)return PTR_ERR (cgrp); } -cgroup_bpf_update (cgrp, prog, attr->attach_type); + result =cgroup_bpf_update (cgrp, prog, attr->attach_type);cgroup_put (cgrp); -return 0 ; +return result; }#define BPF_PROG_DETACH_LAST_FIELD attach_type
@@ -869,6 +870,7 @@ static int bpf_prog_attach(const union bpf_attr *attr)static int bpf_prog_detach (const union bpf_attr *attr) {struct cgroup *cgrp; +int result =0 ;if (!capable (CAP_NET_ADMIN ))return -EPERM ;
@@ -883,7 +885,7 @@ static int bpf_prog_detach(const union bpf_attr *attr)if (IS_ERR (cgrp))return PTR_ERR (cgrp); -cgroup_bpf_update (cgrp,NULL , attr->attach_type); + result =cgroup_bpf_update (cgrp,NULL , attr->attach_type);cgroup_put (cgrp);break ;
@@ -891,7 +893,7 @@ static int bpf_prog_detach(const union bpf_attr *attr) return -EINVAL; } -return 0 ; +return result; }#endif /* CONFIG_CGROUP_BPF */
kernel/cgroup.c +4 -2
@@ -6450,15 +6450,17 @@ static __init int cgroup_namespaces_init(void)subsys_initcall (cgroup_namespaces_init);#ifdef CONFIG_CGROUP_BPF -void cgroup_bpf_update (struct cgroup *cgrp, +int cgroup_bpf_update (struct cgroup *cgrp,struct bpf_prog *prog,enum bpf_attach_type type) {struct cgroup *parent =cgroup_parent (cgrp); +int result;mutex_lock (&cgroup_mutex); -__cgroup_bpf_update (cgrp, parent, prog, type); + result =__cgroup_bpf_update (cgrp, parent, prog, type);mutex_unlock (&cgroup_mutex); +return result; }#endif /* CONFIG_CGROUP_BPF */
Signature · 1 line
2.9.3
Re: [RFC v3 14/22] bpf/cgroup: Make cgroup_bpf_update() return an error code
Quoted 8 lines · Mickaël Salaün
This will be useful to support Landlock for the next commits.
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Daniel Mack <[email protected]>Cc David S. Miller <[email protected]>Cc Tejun Heo <[email protected]>
I think this is good to do regardless. Sooner or later cgroup_bpf_update will start rejecting the prog attach. Like we discussed to have a flag that would dissallow processeses lower in the cgroup hierarchy to install their own bpf programs.
It will be minimal change to bpf_prog_attach() error handling, but will greatly help Mickael to build stuff on top.
DanielM can you refactor your patch to do that from the start ?
Thanks!
Quoted 126 lines
--- include/linux/bpf-cgroup.h | 4 ++-- kernel/bpf/cgroup.c | 3 ++- kernel/bpf/syscall.c | 10 ++++++---- kernel/cgroup.c | 6 ++++-- 4 files changed, 14 insertions(+), 9 deletions(-)include/linux/bpf-cgroup.h +2
-2@@ -31,13 +31,13 @@ struct cgroup_bpf {void cgroup_bpf_put (struct cgroup *cgrp);void cgroup_bpf_inherit (struct cgroup *cgrp,struct cgroup *parent); -void __cgroup_bpf_update (struct cgroup *cgrp, +int __cgroup_bpf_update (struct cgroup *cgrp,struct cgroup *parent,struct bpf_prog *prog,enum bpf_attach_type type);/* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */ -void cgroup_bpf_update (struct cgroup *cgrp, +int cgroup_bpf_update (struct cgroup *cgrp,struct bpf_prog *prog,enum bpf_attach_type type);kernel/bpf/cgroup.c +2
-1@@ -83,7 +83,7 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent)* * Must be called with cgroup_mutex held. */ -void __cgroup_bpf_update (struct cgroup *cgrp, +int __cgroup_bpf_update (struct cgroup *cgrp,struct cgroup *parent,struct bpf_prog *prog,enum bpf_attach_type type)@@ -117,6 +117,7 @@ void __cgroup_bpf_update(struct cgroup *cgrp,bpf_prog_put (old_pinned.prog);static_branch_dec (&cgroup_bpf_enabled_key); } +return 0 ; }/** kernel/bpf/syscall.c +6
-4@@ -831,6 +831,7 @@ static int bpf_prog_attach(const union bpf_attr *attr) {struct bpf_prog *prog;struct cgroup *cgrp; +int result;if (!capable (CAP_NET_ADMIN ))return -EPERM ;@@ -858,10 +859,10 @@ static int bpf_prog_attach(const union bpf_attr *attr)return PTR_ERR (cgrp); } -cgroup_bpf_update (cgrp, prog, attr->attach_type); + result =cgroup_bpf_update (cgrp, prog, attr->attach_type);cgroup_put (cgrp); -return 0 ; +return result; }#define BPF_PROG_DETACH_LAST_FIELD attach_type@@ -869,6 +870,7 @@ static int bpf_prog_attach(const union bpf_attr *attr)static int bpf_prog_detach (const union bpf_attr *attr) {struct cgroup *cgrp; +int result =0 ;if (!capable (CAP_NET_ADMIN ))return -EPERM ;@@ -883,7 +885,7 @@ static int bpf_prog_detach(const union bpf_attr *attr)if (IS_ERR (cgrp))return PTR_ERR (cgrp); -cgroup_bpf_update (cgrp,NULL , attr->attach_type); + result =cgroup_bpf_update (cgrp,NULL , attr->attach_type);cgroup_put (cgrp);break ;@@ -891,7 +893,7 @@ static int bpf_prog_detach(const union bpf_attr *attr) return -EINVAL; } -return 0 ; +return result; }#endif /* CONFIG_CGROUP_BPF */ kernel/cgroup.c +4
-2@@ -6450,15 +6450,17 @@ static __init int cgroup_namespaces_init(void)subsys_initcall (cgroup_namespaces_init);#ifdef CONFIG_CGROUP_BPF -void cgroup_bpf_update (struct cgroup *cgrp, +int cgroup_bpf_update (struct cgroup *cgrp,struct bpf_prog *prog,enum bpf_attach_type type) {struct cgroup *parent =cgroup_parent (cgrp); +int result;mutex_lock (&cgroup_mutex); -__cgroup_bpf_update (cgrp, parent, prog, type); + result =__cgroup_bpf_update (cgrp, parent, prog, type);mutex_unlock (&cgroup_mutex); +return result; }#endif /* CONFIG_CGROUP_BPF */ Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 15/22 bpf/cgroup: Move capability check
In reply to Mickaël SalaünThis will be useful to be able to add more BPF attach type with different capability checks.
--- kernel/bpf/syscall.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/bpf/syscall.c +6 -6
@@ -833,15 +833,15 @@ static int bpf_prog_attach(const union bpf_attr *attr)struct cgroup *cgrp;int result; -if (!capable (CAP_NET_ADMIN )) -return -EPERM ; -if (CHECK_ATTR (BPF_PROG_ATTACH ))return -EINVAL ;switch (attr->attach_type) {case BPF_CGROUP_INET_INGRESS :case BPF_CGROUP_INET_EGRESS : +if (!capable (CAP_NET_ADMIN )) +return -EPERM ; + prog =bpf_prog_get_type (attr->attach_bpf_fd,BPF_PROG_TYPE_CGROUP_SOCKET );break ;
@@ -872,15 +872,15 @@ static int bpf_prog_detach(const union bpf_attr *attr)struct cgroup *cgrp;int result =0 ; -if (!capable (CAP_NET_ADMIN )) -return -EPERM ; -if (CHECK_ATTR (BPF_PROG_DETACH ))return -EINVAL ;switch (attr->attach_type) {case BPF_CGROUP_INET_INGRESS :case BPF_CGROUP_INET_EGRESS : +if (!capable (CAP_NET_ADMIN )) +return -EPERM ; + cgrp =cgroup_get_from_fd (attr->target_fd);if (IS_ERR (cgrp))return PTR_ERR (cgrp);
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 16/22 bpf/cgroup,landlock: Handle Landlock hooks per cgroup
In reply to Mickaël SalaünThis allows to add new eBPF programs to Landlock hooks dedicated to a cgroup thanks to the BPF_PROG_ATTACH command. Like for socket eBPF programs, the Landlock hooks attached to a cgroup are propagated to the nested cgroups. However, when a new Landlock program is attached to one of this nested cgroup, this cgroup hierarchy fork the Landlock hooks. This design is simple and match the current CONFIG_BPF_CGROUP inheritance. The difference lie in the fact that Landlock programs can only be stacked but not removed. This match the append-only seccomp behavior. Userland is free to handle Landlock hooks attached to a cgroup in more complicated ways (e.g. continuous inheritance), but care should be taken to properly handle error cases (e.g. memory allocation errors).
Changes since v2:
* new design based on BPF_PROG_ATTACH (suggested by Alexei Starovoitov)
--- include/linux/bpf-cgroup.h | 7 +++++++ include/linux/cgroup-defs.h | 2 ++ include/linux/landlock.h | 9 +++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/cgroup.c | 33 ++++++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 11 +++++++++++ security/landlock/lsm.c | 40 +++++++++++++++++++++++++++++++++++++++- security/landlock/manager.c | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 131 insertions(+), 4 deletions(-)
include/linux/bpf-cgroup.h +7 -0
@@ -14,8 +14,15 @@ struct sk_buff;extern struct static_key_false cgroup_bpf_enabled_key;#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) +#ifdef CONFIG_SECURITY_LANDLOCK +struct landlock_hooks ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +union bpf_object {struct bpf_prog *prog; +#ifdef CONFIG_SECURITY_LANDLOCK +struct landlock_hooks *hooks; +#endif /* CONFIG_SECURITY_LANDLOCK */ };struct cgroup_bpf {
include/linux/cgroup-defs.h +2 -0
@@ -301,8 +301,10 @@ struct cgroup {/* used to schedule release agent */ struct work_struct release_agent_work; +#ifdef CONFIG_CGROUP_BPF /* used to store eBPF programs */ struct cgroup_bpf bpf; +#endif /* CONFIG_CGROUP_BPF */ /* ids of the ancestors at each level including self */ int ancestor_ids[];
include/linux/landlock.h +9 -0
@@ -19,6 +19,9 @@#include <linux/seccomp.h> /* struct seccomp_filter */ #endif /* CONFIG_SECCOMP_FILTER */ +#ifdef CONFIG_CGROUP_BPF +#include <linux/cgroup-defs.h> /* struct cgroup */ +#endif /* CONFIG_CGROUP_BPF */ #ifdef CONFIG_SECCOMP_FILTER struct landlock_seccomp_ret {
@@ -65,6 +68,7 @@ struct landlock_hooks {struct landlock_hooks *new_landlock_hooks (void ); +void get_landlock_hooks (struct landlock_hooks *hooks);void put_landlock_hooks (struct landlock_hooks *hooks);#ifdef CONFIG_SECCOMP_FILTER
@@ -73,5 +77,10 @@ int landlock_seccomp_set_hook(unsigned int flags, const char __user *user_bpf_fd);#endif /* CONFIG_SECCOMP_FILTER */ +#ifdef CONFIG_CGROUP_BPF +struct landlock_hooks *landlock_cgroup_set_hook (struct cgroup *cgrp, +struct bpf_prog *prog); +#endif /* CONFIG_CGROUP_BPF */ +#endif /* CONFIG_SECURITY_LANDLOCK */ #endif /* _LINUX_LANDLOCK_H */
include/uapi/linux/bpf.h +1 -0
@@ -124,6 +124,7 @@ enum bpf_prog_type {enum bpf_attach_type {BPF_CGROUP_INET_INGRESS ,BPF_CGROUP_INET_EGRESS , +BPF_CGROUP_LANDLOCK , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/cgroup.c +30 -3
@@ -15,6 +15,7 @@#include <linux/bpf.h> #include <linux/bpf-cgroup.h> #include <net/sock.h> +#include <linux/landlock.h> DEFINE_STATIC_KEY_FALSE (cgroup_bpf_enabled_key);EXPORT_SYMBOL (cgroup_bpf_enabled_key);
@@ -31,7 +32,15 @@ void cgroup_bpf_put(struct cgroup *cgrp)union bpf_object pinned = cgrp->bpf.pinned[type];if (pinned.prog) { -bpf_prog_put (pinned.prog); +switch (type) { +case BPF_CGROUP_LANDLOCK : +#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_hooks (pinned.hooks); +break ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +default : +bpf_prog_put (pinned.prog); + }static_branch_dec (&cgroup_bpf_enabled_key); } }
@@ -53,6 +62,10 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent) parent->bpf.effective[type].prog, lockdep_is_held(&cgroup_mutex));rcu_assign_pointer (cgrp->bpf.effective[type].prog, e.prog); +#ifdef CONFIG_SECURITY_LANDLOCK +if (type ==BPF_CGROUP_LANDLOCK ) +get_landlock_hooks (e.hooks); +#endif /* CONFIG_SECURITY_LANDLOCK */ } }
@@ -91,7 +104,18 @@ int __cgroup_bpf_update(struct cgroup *cgrp,union bpf_object obj, old_pinned, effective;struct cgroup_subsys_state *pos; - obj.prog = prog; +switch (type) { +case BPF_CGROUP_LANDLOCK : +#ifdef CONFIG_SECURITY_LANDLOCK +/* append hook */ + obj.hooks =landlock_cgroup_set_hook (cgrp, prog); +if (IS_ERR (obj.hooks)) +return PTR_ERR (obj.hooks); +break ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +default : + obj.prog = prog; + } old_pinned =xchg (cgrp->bpf.pinned + type, obj); effective.prog = (!obj.prog && parent) ?
@@ -114,7 +138,10 @@ int __cgroup_bpf_update(struct cgroup *cgrp,static_branch_inc (&cgroup_bpf_enabled_key);if (old_pinned.prog) { -bpf_prog_put (old_pinned.prog); +#ifdef CONFIG_SECURITY_LANDLOCK +if (type !=BPF_CGROUP_LANDLOCK ) +bpf_prog_put (old_pinned.prog); +#endif /* CONFIG_SECURITY_LANDLOCK */ static_branch_dec (&cgroup_bpf_enabled_key); }return 0 ;
kernel/bpf/syscall.c +11 -0
@@ -846,6 +846,16 @@ static int bpf_prog_attach(const union bpf_attr *attr) BPF_PROG_TYPE_CGROUP_SOCKET);break ; +case BPF_CGROUP_LANDLOCK : +#ifdef CONFIG_SECURITY_LANDLOCK +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + + prog =bpf_prog_get_type (attr->attach_bpf_fd, +BPF_PROG_TYPE_LANDLOCK ); +break ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +default :return -EINVAL ; }
@@ -889,6 +899,7 @@ static int bpf_prog_detach(const union bpf_attr *attr)cgroup_put (cgrp);break ; +case BPF_CGROUP_LANDLOCK :default :return -EINVAL ; }
security/landlock/lsm.c +39 -1
@@ -9,6 +9,7 @@*/ #include <asm/current.h> +#include <linux/bpf-cgroup.h> /* cgroup_bpf_enabled */ #include <linux/bpf.h> /* enum bpf_reg_type, struct landlock_data */ #include <linux/cred.h> #include <linux/err.h> /* MAX_ERRNO */
@@ -19,6 +20,10 @@#include <linux/seccomp.h> /* struct seccomp_* */ #include <linux/types.h> /* uintptr_t */ +#ifdef CONFIG_CGROUP_BPF +#include <linux/cgroup-defs.h> /* struct cgroup */ +#endif /* CONFIG_CGROUP_BPF */ +#include "checker_fs.h" #include "common.h"
@@ -99,6 +104,9 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6])#ifdef CONFIG_SECCOMP_FILTER struct landlock_seccomp_ret *lr;#endif /* CONFIG_SECCOMP_FILTER */ +#ifdef CONFIG_CGROUP_BPF +struct cgroup *cgrp; +#endif /* CONFIG_CGROUP_BPF */ struct landlock_rule *rule;u32 hook_idx =get_index (hook_id);
@@ -115,6 +123,11 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6])/* TODO: use lockless_dereference()? */ +/* +* Run the seccomp-based triggers before the cgroup-based triggers to +* prioritize fine-grained policies (i.e. per thread), and return early. +*/ +#ifdef CONFIG_SECCOMP_FILTER /* seccomp triggers and landlock_ret cleanup */ ctx.origin =LANDLOCK_FLAG_ORIGIN_SECCOMP ;
@@ -155,8 +168,21 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6]) ctx.origin =LANDLOCK_FLAG_ORIGIN_SYSCALL ; ret =landlock_run_prog_for_syscall (hook_idx, &ctx, current->seccomp.landlock_hooks); +if (ret) +return -ret;#endif /* CONFIG_SECCOMP_FILTER */ +#ifdef CONFIG_CGROUP_BPF +/* syscall trigger */ +if (cgroup_bpf_enabled) { + ctx.origin =LANDLOCK_FLAG_ORIGIN_SYSCALL ; +/* get the default cgroup associated with the current thread */ + cgrp =task_css_set (current)->dfl_cgrp; + ret =landlock_run_prog_for_syscall (hook_idx, &ctx, + cgrp->bpf.effective[BPF_CGROUP_LANDLOCK ].hooks); + } +#endif /* CONFIG_CGROUP_BPF */ +return -ret; }
@@ -242,9 +268,21 @@ static struct security_hook_list landlock_hooks[] = { LANDLOCK_HOOK_INIT(mmap_file), }; +#ifdef CONFIG_SECCOMP_FILTER +#ifdef CONFIG_CGROUP_BPF +#define LANDLOCK_MANAGERS "seccomp and cgroups" +#else /* CONFIG_CGROUP_BPF */ +#define LANDLOCK_MANAGERS "seccomp" +#endif /* CONFIG_CGROUP_BPF */ +#elif define (CONFIG_CGROUP_BPF ) +#define LANDLOCK_MANAGERS "cgroups" +#else +#error "Need CONFIG_SECCOMP_FILTER or CONFIG_CGROUP_BPF" +#endif /* CONFIG_SECCOMP_FILTER */ +void __init landlock_add_hooks (void ) { -pr_info ("landlock: Becoming ready to sandbox with seccomp\n" ); +pr_info ("landlock: Becoming ready to sandbox with " LANDLOCK_MANAGERS "\n" );security_add_hooks (landlock_hooks,ARRAY_SIZE (landlock_hooks)); }
security/landlock/manager.c +32 -0
@@ -24,6 +24,11 @@#include <linux/seccomp.h> /* struct seccomp_filter */ #endif /* CONFIG_SECCOMP_FILTER */ +#ifdef CONFIG_CGROUP_BPF +#include <linux/bpf-cgroup.h> /* struct cgroup_bpf */ +#include <linux/cgroup-defs.h> /* struct cgroup */ +#endif /* CONFIG_CGROUP_BPF */ +#include "common.h" static void put_landlock_rule (struct landlock_rule *rule)
@@ -84,6 +89,12 @@ struct landlock_hooks *new_landlock_hooks(void)return ret; } +inline void get_landlock_hooks (struct landlock_hooks *hooks) +{ +if (hooks) +atomic_inc (&hooks->usage); +} +/* Limit Landlock hooks to 256KB. */ #define LANDLOCK_HOOKS_MAX_PAGES (1 << 6)
@@ -240,3 +251,24 @@ int landlock_seccomp_set_hook(unsigned int flags, const char __user *user_bpf_fd return 0; }#endif /* CONFIG_SECCOMP_FILTER */ + +/** +* landlock_cgroup_set_hook - attach a Landlock program to a cgroup +* +* Must be called with cgroup_mutex held. +* +* @crgp: non-NULL cgroup pointer to attach to +* @prog: Landlock program pointer +*/ +#ifdef CONFIG_CGROUP_BPF +struct landlock_hooks *landlock_cgroup_set_hook (struct cgroup *cgrp, +struct bpf_prog *prog) +{ +if (!prog) +return ERR_PTR (-EINVAL ); + +/* copy the inherited hooks and append a new one */ +return landlock_set_hook (cgrp->bpf.effective[BPF_CGROUP_LANDLOCK ].hooks, + prog,NULL ); +} +#endif /* CONFIG_CGROUP_BPF */
Signature · 1 line
2.9.3
Re: [RFC v3 16/22] bpf/cgroup,landlock: Handle Landlock hooks per cgroup
Quoted 56 lines · Mickaël Salaün
This allows to add new eBPF programs to Landlock hooks dedicated to a cgroup thanks to the
BPF_PROG_ATTACHcommand. Like for socket eBPF programs, the Landlock hooks attached to a cgroup are propagated to the nested cgroups. However, when a new Landlock program is attached to one of this nested cgroup, this cgroup hierarchy fork the Landlock hooks. This design is simple and match the currentCONFIG_BPF_CGROUPinheritance. The difference lie in the fact that Landlock programs can only be stacked but not removed. This match the append-only seccomp behavior. Userland is free to handle Landlock hooks attached to a cgroup in more complicated ways (e.g. continuous inheritance), but care should be taken to properly handle error cases (e.g. memory allocation errors).Changes since v2:
* new design based onBPF_PROG_ATTACH(suggested by Alexei Starovoitov)Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Daniel Mack <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Tejun Heo <[email protected]>--- include/linux/bpf-cgroup.h | 7 +++++++ include/linux/cgroup-defs.h | 2 ++ include/linux/landlock.h | 9 +++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/cgroup.c | 33 ++++++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 11 +++++++++++ security/landlock/lsm.c | 40 +++++++++++++++++++++++++++++++++++++++- security/landlock/manager.c | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 131 insertions(+), 4 deletions(-)[...]
kernel/bpf/cgroup.c +3
-1@@ -15,6 +15,7 @@#include <linux/bpf.h> #include <linux/bpf-cgroup.h> #include <net/sock.h> +#include <linux/landlock.h> DEFINE_STATIC_KEY_FALSE (cgroup_bpf_enabled_key);EXPORT_SYMBOL (cgroup_bpf_enabled_key);@@ -31,7 +32,15 @@ void cgroup_bpf_put(struct cgroup *cgrp)union bpf_object pinned = cgrp->bpf.pinned[type];if (pinned.prog) { -bpf_prog_put (pinned.prog); +switch (type) { +case BPF_CGROUP_LANDLOCK :
kernel/bpf/cgroup.c void cgroup_bpf_put(struct cgroup *cgrp) · line 46
Quoted 10 lines
kernel/bpf/cgroup.c +7
-0+#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_hooks (pinned.hooks); +break ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +default : +bpf_prog_put (pinned.prog); + }static_branch_dec (&cgroup_bpf_enabled_key); } }
I get creeped out by type-controlled unions of pointers. :P I don't have a suggestion to improve this, but I don't like seeing a pointer type managed separately from the pointer itself as it tends to bypass a lot of both static and dynamic checking. A union is better than a cast of void *, but it still worries me. :)
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Mickaël Salaün
maintainerRe: [RFC v3 16/22] bpf/cgroup,landlock: Handle Landlock hooks per cgroup
Quoted 65 lines · Kees Cook
Quoted 64 lines · Mickaël Salaün
This allows to add new eBPF programs to Landlock hooks dedicated to a cgroup thanks to the
BPF_PROG_ATTACHcommand. Like for socket eBPF programs, the Landlock hooks attached to a cgroup are propagated to the nested cgroups. However, when a new Landlock program is attached to one of this nested cgroup, this cgroup hierarchy fork the Landlock hooks. This design is simple and match the currentCONFIG_BPF_CGROUPinheritance. The difference lie in the fact that Landlock programs can only be stacked but not removed. This match the append-only seccomp behavior. Userland is free to handle Landlock hooks attached to a cgroup in more complicated ways (e.g. continuous inheritance), but care should be taken to properly handle error cases (e.g. memory allocation errors).Changes since v2:
* new design based onBPF_PROG_ATTACH(suggested by Alexei Starovoitov)Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Daniel Mack <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Tejun Heo <[email protected]>--- include/linux/bpf-cgroup.h | 7 +++++++ include/linux/cgroup-defs.h | 2 ++ include/linux/landlock.h | 9 +++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/cgroup.c | 33 ++++++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 11 +++++++++++ security/landlock/lsm.c | 40 +++++++++++++++++++++++++++++++++++++++- security/landlock/manager.c | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 131 insertions(+), 4 deletions(-)[...]
kernel/bpf/cgroup.c +10
-1@@ -15,6 +15,7 @@#include <linux/bpf.h> #include <linux/bpf-cgroup.h> #include <net/sock.h> +#include <linux/landlock.h> DEFINE_STATIC_KEY_FALSE (cgroup_bpf_enabled_key);EXPORT_SYMBOL (cgroup_bpf_enabled_key);@@ -31,7 +32,15 @@ void cgroup_bpf_put(struct cgroup *cgrp)union bpf_object pinned = cgrp->bpf.pinned[type];if (pinned.prog) { -bpf_prog_put (pinned.prog); +switch (type) { +case BPF_CGROUP_LANDLOCK : +#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_hooks (pinned.hooks); +break ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +default : +bpf_prog_put (pinned.prog); + }static_branch_dec (&cgroup_bpf_enabled_key);
Quoted 8 lines
kernel/bpf/cgroup.c void cgroup_bpf_put(struct cgroup *cgrp) · line 46
Quoted 2 lines
kernel/bpf/cgroup.c +0
-0} }I get creeped out by type-controlled unions of pointers. :P I don't have a suggestion to improve this, but I don't like seeing a pointer type managed separately from the pointer itself as it tends to bypass a lot of both static and dynamic checking. A union is better than a cast of void *, but it still worries me. :)
This is not fully satisfactory for me neither but the other approach is to use two distinct struct fields instead of a union.
Do you prefer if there is a "type" field in the "pinned" struct to select the union?
Mickaël
Re: [RFC v3 16/22] bpf/cgroup,landlock: Handle Landlock hooks per cgroup
Quoted 77 lines · Mickaël Salaün
Quoted 65 lines · Kees Cook
Quoted 64 lines · Mickaël Salaün
This allows to add new eBPF programs to Landlock hooks dedicated to a cgroup thanks to the
BPF_PROG_ATTACHcommand. Like for socket eBPF programs, the Landlock hooks attached to a cgroup are propagated to the nested cgroups. However, when a new Landlock program is attached to one of this nested cgroup, this cgroup hierarchy fork the Landlock hooks. This design is simple and match the currentCONFIG_BPF_CGROUPinheritance. The difference lie in the fact that Landlock programs can only be stacked but not removed. This match the append-only seccomp behavior. Userland is free to handle Landlock hooks attached to a cgroup in more complicated ways (e.g. continuous inheritance), but care should be taken to properly handle error cases (e.g. memory allocation errors).Changes since v2:
* new design based onBPF_PROG_ATTACH(suggested by Alexei Starovoitov)Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Daniel Mack <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Tejun Heo <[email protected]>--- include/linux/bpf-cgroup.h | 7 +++++++ include/linux/cgroup-defs.h | 2 ++ include/linux/landlock.h | 9 +++++++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/cgroup.c | 33 ++++++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 11 +++++++++++ security/landlock/lsm.c | 40 +++++++++++++++++++++++++++++++++++++++- security/landlock/manager.c | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 131 insertions(+), 4 deletions(-)[...]
kernel/bpf/cgroup.c +10
-1@@ -15,6 +15,7 @@#include <linux/bpf.h> #include <linux/bpf-cgroup.h> #include <net/sock.h> +#include <linux/landlock.h> DEFINE_STATIC_KEY_FALSE (cgroup_bpf_enabled_key);EXPORT_SYMBOL (cgroup_bpf_enabled_key);@@ -31,7 +32,15 @@ void cgroup_bpf_put(struct cgroup *cgrp)union bpf_object pinned = cgrp->bpf.pinned[type];if (pinned.prog) { -bpf_prog_put (pinned.prog); +switch (type) { +case BPF_CGROUP_LANDLOCK : +#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_hooks (pinned.hooks); +break ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +default : +bpf_prog_put (pinned.prog); + }static_branch_dec (&cgroup_bpf_enabled_key);Quoted 8 lines
kernel/bpf/cgroup.c void cgroup_bpf_put(struct cgroup *cgrp) · line 46
Quoted 2 lines
kernel/bpf/cgroup.c +0
-0} }I get creeped out by type-controlled unions of pointers. :P I don't have a suggestion to improve this, but I don't like seeing a pointer type managed separately from the pointer itself as it tends to bypass a lot of both static and dynamic checking. A union is better than a cast of void *, but it still worries me. :)
Quoted 4 lines
This is not fully satisfactory for me neither but the other approach is to use two distinct struct fields instead of a union.
Do you prefer if there is a "type" field in the "pinned" struct to select the union?
Since memory usage isn't a huge deal for this, I'd actually prefer there just be no union at all. Have a type field, and a distinct pointer field for each type you're expecting to use. That way there can never be confusion between types and you could even validate that only a single field type has been populated, etc.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Mickaël Salaün
maintainerRFC v3 17/22 cgroup: Add access check for cgroup_get_from_fd()
In reply to Mickaël SalaünAdd security access check for cgroup backed FD. The "cgroup.procs" file of the corresponding cgroup must be readable to identify the cgroup, and writable to prove that the current process can manage this cgroup (e.g. through delegation). This is similar to the check done by cgroup_procs_write_permission().
--- include/linux/cgroup.h | 2 +- kernel/bpf/arraymap.c | 2 +- kernel/bpf/syscall.c | 6 +++--- kernel/cgroup.c | 16 +++++++++++++++- 4 files changed, 20 insertions(+), 6 deletions(-)
include/linux/cgroup.h +1 -1
@@ -87,7 +87,7 @@ struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, struct cgroup_subsys *ss);struct cgroup *cgroup_get_from_path (const char *path); -struct cgroup *cgroup_get_from_fd (int fd); +struct cgroup *cgroup_get_from_fd (int fd,int access_mask);int cgroup_attach_task_all (struct task_struct *from,struct task_struct *);int cgroup_transfer_tasks (struct cgroup *to,struct cgroup *from);
kernel/bpf/arraymap.c +1 -1
@@ -552,7 +552,7 @@ static void *cgroup_fd_array_get_ptr(struct bpf_map *map,struct file *map_file/* not used */ , int fd) { -return cgroup_get_from_fd (fd); + return cgroup_get_from_fd(fd, MAY_READ); }static void cgroup_fd_array_put_ptr (void *ptr)
kernel/bpf/syscall.c +3 -3
@@ -17,6 +17,7 @@#include <linux/license.h> #include <linux/filter.h> #include <linux/version.h> +#include <linux/fs.h> DEFINE_PER_CPU (int, bpf_prog_active);
@@ -863,7 +864,7 @@ static int bpf_prog_attach(const union bpf_attr *attr)if (IS_ERR (prog))return PTR_ERR (prog); - cgrp =cgroup_get_from_fd (attr->target_fd); + cgrp =cgroup_get_from_fd (attr->target_fd,MAY_WRITE );if (IS_ERR (cgrp)) {bpf_prog_put (prog);return PTR_ERR (cgrp);
@@ -891,10 +892,9 @@ static int bpf_prog_detach(const union bpf_attr *attr)if (!capable (CAP_NET_ADMIN ))return -EPERM ; - cgrp =cgroup_get_from_fd (attr->target_fd); + cgrp =cgroup_get_from_fd (attr->target_fd,MAY_WRITE );if (IS_ERR (cgrp))return PTR_ERR (cgrp); - result =cgroup_bpf_update (cgrp,NULL , attr->attach_type);cgroup_put (cgrp);break ;
kernel/cgroup.c +15 -1
@@ -6241,17 +6241,20 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);/** * cgroup_get_from_fd - get a cgroup pointer from a fd * @fd: fd obtained by open(cgroup2_dir) +* @access_mask: contains the permission mask * * Find the cgroup from a fd which should be obtained * by opening a cgroup directory. Returns a pointer to the * cgroup on success. ERR_PTR is returned if the cgroup * cannot be found. */ -struct cgroup *cgroup_get_from_fd (int fd) +struct cgroup *cgroup_get_from_fd (int fd,int access_mask) {struct cgroup_subsys_state *css;struct cgroup *cgrp;struct file *f; +struct inode *inode; +int ret; f =fget_raw (fd);if (!f)
@@ -6268,6 +6271,17 @@ struct cgroup *cgroup_get_from_fd(int fd)return ERR_PTR (-EBADF ); } + ret = -ENOMEM ; + inode =kernfs_get_inode (f->f_path.dentry->d_sb, cgrp->procs_file.kn); +if (inode) { + ret =inode_permission (inode, access_mask); +iput (inode); + } +if (ret) { +cgroup_put (cgrp); +return ERR_PTR (ret); + } +return cgrp; }EXPORT_SYMBOL_GPL (cgroup_get_from_fd);
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRe: [RFC v3 17/22] cgroup: Add access check for cgroup_get_from_fd()
Quoted 20 lines · Mickaël Salaün
Add security access check for cgroup backed FD. The "cgroup.procs" file of the corresponding cgroup must be readable to identify the cgroup, and writable to prove that the current process can manage this cgroup (e.g. through delegation). This is similar to the check done by
cgroup_procs_write_permission().Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Daniel Mack <[email protected]>Cc David S. Miller <[email protected]>Cc Kees Cook <[email protected]>Cc Tejun Heo <[email protected]>--- include/linux/cgroup.h | 2 +- kernel/bpf/arraymap.c | 2 +- kernel/bpf/syscall.c | 6 +++--- kernel/cgroup.c | 16 +++++++++++++++- 4 files changed, 20 insertions(+), 6 deletions(-)
...
Quoted 27 lines
kernel/cgroup.c +4
-1@@ -6241,17 +6241,20 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);/** * cgroup_get_from_fd - get a cgroup pointer from a fd * @fd: fd obtained by open(cgroup2_dir) +* @access_mask: contains the permission mask * * Find the cgroup from a fd which should be obtained * by opening a cgroup directory. Returns a pointer to the * cgroup on success. ERR_PTR is returned if the cgroup * cannot be found. */ -struct cgroup *cgroup_get_from_fd (int fd) +struct cgroup *cgroup_get_from_fd (int fd,int access_mask) {struct cgroup_subsys_state *css;struct cgroup *cgrp;struct file *f; +struct inode *inode; +int ret; f =fget_raw (fd);if (!f)
kernel/cgroup.c struct cgroup *cgroup_get_from_fd(int fd) · line 6275
Quoted 5 lines
kernel/cgroup.c +2
-0@@ -6268,6 +6271,17 @@ struct cgroup *cgroup_get_from_fd(int fd)return ERR_PTR (-EBADF ); } + ret = -ENOMEM ; + inode =kernfs_get_inode (f->f_path.dentry->d_sb, cgrp->procs_file.kn);
I forgot to properly move fput(f) after this line… This will be fixed.
Mickaël Salaün
maintainerRFC v3 18/22 cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
In reply to Mickaël SalaünAdd a new flag CGRP_NO_NEW_PRIVS for each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without the no_new_privs flags is attached to the cgroup.
If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without no_new_privs to this cgroup will be denied.
This allows to safely manage Landlock rules with cgroup delegation as with seccomp.
--- include/linux/cgroup-defs.h | 7 +++++++ kernel/bpf/syscall.c | 7 ++++--- kernel/cgroup.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- security/landlock/manager.c | 7 +++++++ 4 files changed, 60 insertions(+), 5 deletions(-)
include/linux/cgroup-defs.h +7 -0
@@ -59,6 +59,13 @@ enum {* specified at mount time and thus is implemented here. */ CGRP_CPUSET_CLONE_CHILDREN , +/* +* Keep track of the no_new_privs property of processes in the cgroup. +* This is useful to quickly check if all processes in the cgroup have +* their no_new_privs bit on. This flag is initially set to true but +* ANDed with every processes coming in the cgroup. +*/ + CGRP_NO_NEW_PRIVS, };/* cgroup_root->flags */
kernel/bpf/syscall.c +4 -3
@@ -849,9 +849,10 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_CGROUP_LANDLOCK :#ifdef CONFIG_SECURITY_LANDLOCK -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; - +/* +* security/capability check done in landlock_cgroup_set_hook() +* called by cgroup_bpf_update() +*/ prog =bpf_prog_get_type (attr->attach_bpf_fd,BPF_PROG_TYPE_LANDLOCK );break ;
kernel/cgroup.c +42 -2
89 lines not on this page · raw message
security/landlock/manager.c +7 -0
@@ -11,6 +11,7 @@#include <asm/atomic.h> /* atomic_*() */ #include <asm/page.h> /* PAGE_SIZE */ #include <asm/uaccess.h> /* copy_from_user() */ +#include <linux/bitops.h> /* BIT_ULL() */ #include <linux/bpf.h> /* bpf_prog_put() */ #include <linux/filter.h> /* struct bpf_prog */ #include <linux/kernel.h> /* round_up() */
@@ -267,6 +268,12 @@ struct landlock_hooks *landlock_cgroup_set_hook(struct cgroup *cgrp,if (!prog)return ERR_PTR (-EINVAL ); +/* check no_new_privs for tasks in the cgroup */ +if (!(cgrp->flags &BIT_ULL (CGRP_NO_NEW_PRIVS )) && +security_capable_noaudit (current_cred (), +current_user_ns (),CAP_SYS_ADMIN ) !=0 ) +return ERR_PTR (-EPERM ); +/* copy the inherited hooks and append a new one */ return landlock_set_hook (cgrp->bpf.effective[BPF_CGROUP_LANDLOCK ].hooks, prog,NULL );
Signature · 1 line
2.9.3
Andy Lutomirski
reviewerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 7 lines · Mickaël Salaün
Add a new flag
CGRP_NO_NEW_PRIVSfor each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without theno_new_privsflags is attached to the cgroup.If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without
no_new_privsto this cgroup will be denied.
Until and unless everyone can agree on a way to properly namespace, delegate, etc cgroups, I think that trying to add unprivileged semantics to cgroups is nuts. Given the big thread about cgroup v2, no-internal-tasks, etc, I just don't see how this approach can be viable.
Can we try to make landlock work completely independently of cgroups so that it doesn't get stuck and so that programs can use it without worrying about cgroup v1 vs v2, interactions with cgroup managers, cgroup managers that (supposedly?) will start migrating processes around piecemeal and almost certainly blowing up landlock in the process, etc?
I have no problem with looking at prototypes for how landlock + cgroups would work, but I can't imagine the result being mergeable.
Mickaël Salaün
maintainerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Mickaël Salaün
Add a new flag
CGRP_NO_NEW_PRIVSfor each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without theno_new_privsflags is attached to the cgroup.If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without
no_new_privsto this cgroup will be denied.
Quoted 5 lines
Until and unless everyone can agree on a way to properly namespace, delegate, etc cgroups, I think that trying to add unprivileged semantics to cgroups is nuts. Given the big thread about cgroup v2, no-internal-tasks, etc, I just don't see how this approach can be viable.
As far as I can tell, the no_new_privs flag of at task is not related to namespaces. The CGRP_NO_NEW_PRIVS flag is only a cache to quickly access the no_new_privs property of *tasks* in a cgroup. The semantic is unchanged.
Using cgroup is optional, any task could use the seccomp-based landlocking instead. However, for those that want/need to manage a security policy in a more dynamic way, using cgroups may make sense.
I though cgroup delegation was OK in the v2, isn't it the case? Do you have some links?
Quoted 7 lines
Can we try to make landlock work completely independently of cgroups so that it doesn't get stuck and so that programs can use it without worrying about cgroup v1 vs v2, interactions with cgroup managers, cgroup managers that (supposedly?) will start migrating processes around piecemeal and almost certainly blowing up landlock in the process, etc?
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
Quoted 4 lines
I have no problem with looking at prototypes for how landlock + cgroups would work, but I can't imagine the result being mergeable.
Andy Lutomirski
reviewerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 36 lines · Mickaël Salaün
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Mickaël Salaün
Add a new flag
CGRP_NO_NEW_PRIVSfor each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without theno_new_privsflags is attached to the cgroup.If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without
no_new_privsto this cgroup will be denied.Quoted 5 lines
Until and unless everyone can agree on a way to properly namespace, delegate, etc cgroups, I think that trying to add unprivileged semantics to cgroups is nuts. Given the big thread about cgroup v2, no-internal-tasks, etc, I just don't see how this approach can be viable.
As far as I can tell, the
no_new_privsflag of at task is not related to namespaces. TheCGRP_NO_NEW_PRIVSflag is only a cache to quickly access theno_new_privsproperty of *tasks* in a cgroup. The semantic is unchanged.Using cgroup is optional, any task could use the seccomp-based landlocking instead. However, for those that want/need to manage a security policy in a more dynamic way, using cgroups may make sense.
I though cgroup delegation was OK in the v2, isn't it the case? Do you have some links?
Quoted 7 lines
Can we try to make landlock work completely independently of cgroups so that it doesn't get stuck and so that programs can use it without worrying about cgroup v1 vs v2, interactions with cgroup managers, cgroup managers that (supposedly?) will start migrating processes around piecemeal and almost certainly blowing up landlock in the process, etc?
Quoted 3 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Re: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 51 lines · Andy Lutomirski
Quoted 36 lines · Mickaël Salaün
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Mickaël Salaün
Add a new flag
CGRP_NO_NEW_PRIVSfor each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without theno_new_privsflags is attached to the cgroup.If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without
no_new_privsto this cgroup will be denied.Quoted 5 lines
Until and unless everyone can agree on a way to properly namespace, delegate, etc cgroups, I think that trying to add unprivileged semantics to cgroups is nuts. Given the big thread about cgroup v2, no-internal-tasks, etc, I just don't see how this approach can be viable.
As far as I can tell, the
no_new_privsflag of at task is not related to namespaces. TheCGRP_NO_NEW_PRIVSflag is only a cache to quickly access theno_new_privsproperty of *tasks* in a cgroup. The semantic is unchanged.Using cgroup is optional, any task could use the seccomp-based landlocking instead. However, for those that want/need to manage a security policy in a more dynamic way, using cgroups may make sense.
I though cgroup delegation was OK in the v2, isn't it the case? Do you have some links?
Quoted 7 lines
Can we try to make landlock work completely independently of cgroups so that it doesn't get stuck and so that programs can use it without worrying about cgroup v1 vs v2, interactions with cgroup managers, cgroup managers that (supposedly?) will start migrating processes around piecemeal and almost certainly blowing up landlock in the process, etc?
Quoted 3 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Andy Lutomirski
reviewerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 52 lines · Alexei Starovoitov
Quoted 51 lines · Andy Lutomirski
Quoted 36 lines · Mickaël Salaün
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Mickaël Salaün
Add a new flag
CGRP_NO_NEW_PRIVSfor each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without theno_new_privsflags is attached to the cgroup.If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without
no_new_privsto this cgroup will be denied.Quoted 5 lines
Until and unless everyone can agree on a way to properly namespace, delegate, etc cgroups, I think that trying to add unprivileged semantics to cgroups is nuts. Given the big thread about cgroup v2, no-internal-tasks, etc, I just don't see how this approach can be viable.
As far as I can tell, the
no_new_privsflag of at task is not related to namespaces. TheCGRP_NO_NEW_PRIVSflag is only a cache to quickly access theno_new_privsproperty of *tasks* in a cgroup. The semantic is unchanged.Using cgroup is optional, any task could use the seccomp-based landlocking instead. However, for those that want/need to manage a security policy in a more dynamic way, using cgroups may make sense.
I though cgroup delegation was OK in the v2, isn't it the case? Do you have some links?
Quoted 7 lines
Can we try to make landlock work completely independently of cgroups so that it doesn't get stuck and so that programs can use it without worrying about cgroup v1 vs v2, interactions with cgroup managers, cgroup managers that (supposedly?) will start migrating processes around piecemeal and almost certainly blowing up landlock in the process, etc?
Quoted 3 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
--Andy
Re: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.
Andy Lutomirski
reviewerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves. Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
If I ever add a "seccomp monitor", which is something I want to do eventually, I think it should work for lsm+bpf as well, which is another argument for keeping it in seccomp.
--Andy
Re: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 46 lines · Andy Lutomirski
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.
Quoted 4 lines
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves.
you mean the attach part of seccomp syscall that deals with no_new_priv? sure, that's reusable.
Quoted 4 lines
Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
not sure what you mean by 'seccomp hierarchy'. The normal process hierarchy ?
imo the main deficiency of secccomp is inability to look into arguments. One can argue that it's a blessing, since composite args
are not yet copied into the kernel memory.
But in a lot of cases the seccomp arguments are FDs pointing to kernel objects and if programs could examine those objects the sandboxing scope would be more precise.
lsm+bpf solves that part and I'd still argue that it's orthogonal to seccomp's pass/reject flow.
I mean if seccomp says 'ok' the syscall should continue executing as normal and whatever LSM hooks were triggered by it may have their own lsm+bpf verdicts.
Furthermore in the process hierarchy different children should be able to set their own lsm+bpf filters that are not related to parallel seccomp+bpf hierarchy of programs. seccomp syscall can be an interface to attach programs
to lsm hooks, but nothing more than that.
Andy Lutomirski
reviewerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 55 lines · Alexei Starovoitov
Quoted 46 lines · Andy Lutomirski
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.Quoted 4 lines
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves.
you mean the attach part of seccomp syscall that deals with
no_new_priv? sure, that's reusable.
Quoted 7 lines
Quoted 4 lines
Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
not sure what you mean by 'seccomp hierarchy'. The normal process hierarchy ?
Kind of. I mean the filter layers that are inherited across fork(), the TSYNC mechanism, etc.
Quoted 11 lines
imo the main deficiency of secccomp is inability to look into arguments. One can argue that it's a blessing, since composite args
are not yet copied into the kernel memory.
But in a lot of cases the seccomp arguments are FDs pointing to kernel objects and if programs could examine those objects the sandboxing scope would be more precise.
lsm+bpf solves that part and I'd still argue that it's orthogonal to seccomp's pass/reject flow.
I mean if seccomp says 'ok' the syscall should continue executing as normal and whatever LSM hooks were triggered by it may have their own lsm+bpf verdicts.
I agree with all of this...
Quoted 5 lines
Furthermore in the process hierarchy different children should be able to set their own lsm+bpf filters that are not related to parallel seccomp+bpf hierarchy of programs. seccomp syscall can be an interface to attach programs to lsm hooks, but nothing more than that.
I'm not sure what you mean. I mean that, logically, I think we should be able to do:
seccomp(attach a syscall filter);fork();
child does seccomp(attach some lsm filters);
I think that they *should* be related to the seccomp+bpf hierarchy of programs in that they are entries in the same logical list of filter layers installed. Some of those layers can be syscall filters and some of the layers can be lsm filters. If we subsequently add a way to attach a removable seccomp filter or a way to attach a seccomp filter that logs failures to some fd watched by an outside monitor, I think that should work for lsm, too, with more or less the same interface.
If we need a way for a sandbox manager to opt different children into different subsets of fancy filters, then I think that syscall filters and lsm filters should use the same mechanism.
I think we might be on the same page here and just saying it different ways.
Re: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 104 lines · Andy Lutomirski
Quoted 55 lines · Alexei Starovoitov
Quoted 46 lines · Andy Lutomirski
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.Quoted 4 lines
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves.
you mean the attach part of seccomp syscall that deals with
no_new_priv? sure, that's reusable.Quoted 7 lines
Quoted 4 lines
Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
not sure what you mean by 'seccomp hierarchy'. The normal process hierarchy ?
Kind of. I mean the filter layers that are inherited across
fork(), the TSYNC mechanism, etc.Quoted 11 lines
imo the main deficiency of secccomp is inability to look into arguments. One can argue that it's a blessing, since composite args
are not yet copied into the kernel memory.
But in a lot of cases the seccomp arguments are FDs pointing to kernel objects and if programs could examine those objects the sandboxing scope would be more precise.
lsm+bpf solves that part and I'd still argue that it's orthogonal to seccomp's pass/reject flow.
I mean if seccomp says 'ok' the syscall should continue executing as normal and whatever LSM hooks were triggered by it may have their own lsm+bpf verdicts.I agree with all of this...
Quoted 5 lines
Furthermore in the process hierarchy different children should be able to set their own lsm+bpf filters that are not related to parallel seccomp+bpf hierarchy of programs. seccomp syscall can be an interface to attach programs to lsm hooks, but nothing more than that.
I'm not sure what you mean. I mean that, logically, I think we should be able to do:
seccomp(attach a syscall filter);
fork();
child does seccomp(attach some lsm filters);I think that they *should* be related to the seccomp+bpf hierarchy of programs in that they are entries in the same logical list of filter layers installed. Some of those layers can be syscall filters and some of the layers can be lsm filters. If we subsequently add a way to attach a removable seccomp filter or a way to attach a seccomp filter that logs failures to some fd watched by an outside monitor, I think that should work for lsm, too, with more or less the same interface.
Quoted 5 lines
If we need a way for a sandbox manager to opt different children into different subsets of fancy filters, then I think that syscall filters and lsm filters should use the same mechanism.
I think we might be on the same page here and just saying it different ways.
Sounds like it :)
All of the above makes sense to me.
The 'orthogonal' part is that the user should be able to use this seccomp-managed hierarchy without actually enabling TIF_SECCOMP for the task and syscalls should still go through fast path and all the way till lsm hooks as normal.
I don't want to pay _any_ performance penalty for this feature for lsm hooks (and all syscalls) that don't have bpf programs attached.
Mickaël Salaün
maintainerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 111 lines · Alexei Starovoitov
Quoted 104 lines · Andy Lutomirski
Quoted 55 lines · Alexei Starovoitov
Quoted 46 lines · Andy Lutomirski
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.Quoted 4 lines
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves.
you mean the attach part of seccomp syscall that deals with
no_new_priv? sure, that's reusable.Quoted 7 lines
Quoted 4 lines
Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
not sure what you mean by 'seccomp hierarchy'. The normal process hierarchy ?
Kind of. I mean the filter layers that are inherited across
fork(), the TSYNC mechanism, etc.Quoted 11 lines
imo the main deficiency of secccomp is inability to look into arguments. One can argue that it's a blessing, since composite args
are not yet copied into the kernel memory.
But in a lot of cases the seccomp arguments are FDs pointing to kernel objects and if programs could examine those objects the sandboxing scope would be more precise.
lsm+bpf solves that part and I'd still argue that it's orthogonal to seccomp's pass/reject flow.
I mean if seccomp says 'ok' the syscall should continue executing as normal and whatever LSM hooks were triggered by it may have their own lsm+bpf verdicts.I agree with all of this...
Quoted 5 lines
Furthermore in the process hierarchy different children should be able to set their own lsm+bpf filters that are not related to parallel seccomp+bpf hierarchy of programs. seccomp syscall can be an interface to attach programs to lsm hooks, but nothing more than that.
I'm not sure what you mean. I mean that, logically, I think we should be able to do:
seccomp(attach a syscall filter);
fork();
child does seccomp(attach some lsm filters);I think that they *should* be related to the seccomp+bpf hierarchy of programs in that they are entries in the same logical list of filter layers installed. Some of those layers can be syscall filters and some of the layers can be lsm filters. If we subsequently add a way to attach a removable seccomp filter or a way to attach a seccomp filter that logs failures to some fd watched by an outside monitor, I think that should work for lsm, too, with more or less the same interface.
Quoted 5 lines
If we need a way for a sandbox manager to opt different children into different subsets of fancy filters, then I think that syscall filters and lsm filters should use the same mechanism.
I think we might be on the same page here and just saying it different ways.
Quoted 8 lines
Sounds like it :)
All of the above makes sense to me.
The 'orthogonal' part is that the user should be able to use this seccomp-managed hierarchy without actually enablingTIF_SECCOMPfor the task and syscalls should still go through fast path and all the way till lsm hooks as normal.
I don't want to pay _any_ performance penalty for this feature for lsm hooks (and all syscalls) that don't have bpf programs attached.
Yes, it seems that we are all on the same page here, and that match this RFC implementation. So, using the seccomp(2) *interface* to attach Landlock programs to a process hierarchy is still on track. :)
Re: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 122 lines · Mickaël Salaün
Quoted 111 lines · Alexei Starovoitov
Quoted 104 lines · Andy Lutomirski
Quoted 55 lines · Alexei Starovoitov
Quoted 46 lines · Andy Lutomirski
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.Quoted 4 lines
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves.
you mean the attach part of seccomp syscall that deals with
no_new_priv? sure, that's reusable.Quoted 7 lines
Quoted 4 lines
Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
not sure what you mean by 'seccomp hierarchy'. The normal process hierarchy ?
Kind of. I mean the filter layers that are inherited across
fork(), the TSYNC mechanism, etc.Quoted 11 lines
imo the main deficiency of secccomp is inability to look into arguments. One can argue that it's a blessing, since composite args
are not yet copied into the kernel memory.
But in a lot of cases the seccomp arguments are FDs pointing to kernel objects and if programs could examine those objects the sandboxing scope would be more precise.
lsm+bpf solves that part and I'd still argue that it's orthogonal to seccomp's pass/reject flow.
I mean if seccomp says 'ok' the syscall should continue executing as normal and whatever LSM hooks were triggered by it may have their own lsm+bpf verdicts.I agree with all of this...
Quoted 5 lines
Furthermore in the process hierarchy different children should be able to set their own lsm+bpf filters that are not related to parallel seccomp+bpf hierarchy of programs. seccomp syscall can be an interface to attach programs to lsm hooks, but nothing more than that.
I'm not sure what you mean. I mean that, logically, I think we should be able to do:
seccomp(attach a syscall filter);
fork();
child does seccomp(attach some lsm filters);I think that they *should* be related to the seccomp+bpf hierarchy of programs in that they are entries in the same logical list of filter layers installed. Some of those layers can be syscall filters and some of the layers can be lsm filters. If we subsequently add a way to attach a removable seccomp filter or a way to attach a seccomp filter that logs failures to some fd watched by an outside monitor, I think that should work for lsm, too, with more or less the same interface.
Quoted 5 lines
If we need a way for a sandbox manager to opt different children into different subsets of fancy filters, then I think that syscall filters and lsm filters should use the same mechanism.
I think we might be on the same page here and just saying it different ways.
Quoted 8 lines
Sounds like it :)
All of the above makes sense to me.
The 'orthogonal' part is that the user should be able to use this seccomp-managed hierarchy without actually enablingTIF_SECCOMPfor the task and syscalls should still go through fast path and all the way till lsm hooks as normal.
I don't want to pay _any_ performance penalty for this feature for lsm hooks (and all syscalls) that don't have bpf programs attached.
Quoted 4 lines
Yes, it seems that we are all on the same page here, and that match this RFC implementation. So, using the seccomp(2) *interface* to attach Landlock programs to a process hierarchy is still on track. :)
So, I'm catching up on this after a little while away. I really like the simplicity of the approach Daniel took with his patches. I began to have difficulty reading your patchset once you got into using seccomp + unprivileged mode. I would love to see a separate patchset that only have the verifier, and lsm hook changes. Do you think you could decompose your patchset into an MVP?
Mickaël Salaün
maintainerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
Quoted 128 lines · Sargun Dhillon
Quoted 122 lines · Mickaël Salaün
Quoted 111 lines · Alexei Starovoitov
Quoted 104 lines · Andy Lutomirski
Quoted 55 lines · Alexei Starovoitov
Quoted 46 lines · Andy Lutomirski
Quoted 30 lines · Alexei Starovoitov
Quoted 24 lines · Andy Lutomirski
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 8 lines
Quoted 2 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
Ours and Sargun's use cases for cgroup+lsm+bpf is not for security and not for sandboxing. So the above doesn't matter in such contexts. lsm hooks + cgroups provide convenient scope and existing entry points. Please see checmate examples how it's used.
Quoted 4 lines
To be clear: I'm not arguing at all that there shouldn't be bpf+lsm+cgroup integration. I'm arguing that the unprivileged landlock interface shouldn't expose any cgroup integration, at least until the cgroup situation settles down a lot.
Quoted 13 lines
ahh. yes. we're perfectly in agreement here.
I'm suggesting that the next RFC shouldn't include unpriv and seccomp at all. Once bpf+lsm+cgroup is merged, we can argue about unpriv with cgroups and even unpriv as a whole, since it's not a given. Seccomp integration is also questionable. I'd rather not have seccomp as a gate keeper for this lsm. lsm and seccomp are orthogonal hook points. Syscalls and lsm hooks don't have one to one relationship, so mixing them up is only asking for trouble further down the road.
If we really need to carry some information from seccomp to lsm+bpf, it's easier to add eBPF support to seccomp and let bpf side deal with passing whatever information.Quoted 4 lines
As an argument for keeping seccomp (or an extended seccomp) as the interface for an unprivileged bpf+lsm: seccomp already checks off most of the boxes for safely letting unprivileged programs sandbox themselves.
you mean the attach part of seccomp syscall that deals with
no_new_priv? sure, that's reusable.Quoted 7 lines
Quoted 4 lines
Furthermore, to the extent that there are use cases for unprivileged bpf+lsm that *aren't* expressible within the seccomp hierarchy, I suspect that syscall filters have exactly the same problem and that we should fix seccomp to cover it.
not sure what you mean by 'seccomp hierarchy'. The normal process hierarchy ?
Kind of. I mean the filter layers that are inherited across
fork(), the TSYNC mechanism, etc.Quoted 11 lines
imo the main deficiency of secccomp is inability to look into arguments. One can argue that it's a blessing, since composite args
are not yet copied into the kernel memory.
But in a lot of cases the seccomp arguments are FDs pointing to kernel objects and if programs could examine those objects the sandboxing scope would be more precise.
lsm+bpf solves that part and I'd still argue that it's orthogonal to seccomp's pass/reject flow.
I mean if seccomp says 'ok' the syscall should continue executing as normal and whatever LSM hooks were triggered by it may have their own lsm+bpf verdicts.I agree with all of this...
Quoted 5 lines
Furthermore in the process hierarchy different children should be able to set their own lsm+bpf filters that are not related to parallel seccomp+bpf hierarchy of programs. seccomp syscall can be an interface to attach programs to lsm hooks, but nothing more than that.
I'm not sure what you mean. I mean that, logically, I think we should be able to do:
seccomp(attach a syscall filter);
fork();
child does seccomp(attach some lsm filters);I think that they *should* be related to the seccomp+bpf hierarchy of programs in that they are entries in the same logical list of filter layers installed. Some of those layers can be syscall filters and some of the layers can be lsm filters. If we subsequently add a way to attach a removable seccomp filter or a way to attach a seccomp filter that logs failures to some fd watched by an outside monitor, I think that should work for lsm, too, with more or less the same interface.
Quoted 5 lines
If we need a way for a sandbox manager to opt different children into different subsets of fancy filters, then I think that syscall filters and lsm filters should use the same mechanism.
I think we might be on the same page here and just saying it different ways.
Quoted 8 lines
Sounds like it :)
All of the above makes sense to me.
The 'orthogonal' part is that the user should be able to use this seccomp-managed hierarchy without actually enablingTIF_SECCOMPfor the task and syscalls should still go through fast path and all the way till lsm hooks as normal.
I don't want to pay _any_ performance penalty for this feature for lsm hooks (and all syscalls) that don't have bpf programs attached.Quoted 4 lines
Yes, it seems that we are all on the same page here, and that match this RFC implementation. So, using the seccomp(2) *interface* to attach Landlock programs to a process hierarchy is still on track. :)
Quoted 6 lines
So, I'm catching up on this after a little while away. I really like the simplicity of the approach Daniel took with his patches. I began to have difficulty reading your patchset once you got into using seccomp + unprivileged mode. I would love to see a separate patchset that only have the verifier, and lsm hook changes. Do you think you could decompose your patchset into an MVP?
OK, I'll try to split the common parts from the seccomp part, but there is already a dedicated patch for the LSM hooks [06/22].
Mickaël Salaün
maintainerRe: [RFC v3 18/22] cgroup,landlock: Add CGRP_NO_NEW_PRIVS to handle unprivileged hooks
In reply to Andy LutomirskiQuoted 51 lines · Andy Lutomirski
Quoted 36 lines · Mickaël Salaün
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Mickaël Salaün
Add a new flag
CGRP_NO_NEW_PRIVSfor each cgroup. This flag is initially set for all cgroup except the root. The flag is clear when a new process without theno_new_privsflags is attached to the cgroup.If a cgroup is landlocked, then any new attempt, from an unprivileged process, to attach a process without
no_new_privsto this cgroup will be denied.Quoted 5 lines
Until and unless everyone can agree on a way to properly namespace, delegate, etc cgroups, I think that trying to add unprivileged semantics to cgroups is nuts. Given the big thread about cgroup v2, no-internal-tasks, etc, I just don't see how this approach can be viable.
As far as I can tell, the
no_new_privsflag of at task is not related to namespaces. TheCGRP_NO_NEW_PRIVSflag is only a cache to quickly access theno_new_privsproperty of *tasks* in a cgroup. The semantic is unchanged.Using cgroup is optional, any task could use the seccomp-based landlocking instead. However, for those that want/need to manage a security policy in a more dynamic way, using cgroups may make sense.
I though cgroup delegation was OK in the v2, isn't it the case? Do you have some links?
Quoted 7 lines
Can we try to make landlock work completely independently of cgroups so that it doesn't get stuck and so that programs can use it without worrying about cgroup v1 vs v2, interactions with cgroup managers, cgroup managers that (supposedly?) will start migrating processes around piecemeal and almost certainly blowing up landlock in the process, etc?
Quoted 3 lines
This RFC handle both cgroup and seccomp approaches in a similar way. I don't see why building on top of cgroup v2 is a problem. Is there security issues with delegation?
What I mean is: cgroup v2 delegation has a functionality problem. Tejun says [1]:
We haven't had to face this decision because cgroup has never properly supported delegating to applications and the in-use setups where this happens are custom configurations where there is no boundary between system and applications and adhoc trial-and-error is good enough a way to find a working solution. That wiggle room goes away once we officially open this up to individual applications.
Quoted 5 lines
Unless and until that changes, I think that landlock should stay away from cgroups. Others could reasonably disagree with me.
I don't get the same echo here:
https://lkml.kernel.org/r/[email protected]
Quoted 4 lines · Tejun Heo
Please refer to "2-5. Delegation" of
Documentation/cgroup-v2.txt. Delegation on v1 is broken on both core and specific controller behaviors and thus discouraged. On v2, delegation should work just fine.
Tejun, could you please clarify if there is still a problem with cgroup v2 delegation?
This patch only implement a cache mechanism with the CGRP_NO_NEW_PRIVS flag. If cgroups can group processes correctly, I don't see any (security) issue here. It's the administrator choice to delegate a part of the cgroup management. It's then the delegatee responsibility to correctly put processes in cgroups. This is comparable to a process which is responsible to correctly call seccomp(2).
Mickaël
Mickaël Salaün
maintainerRFC v3 19/22 landlock: Add interrupted origin
In reply to Mickaël SalaünThis third origin of hook call should cover all possible trigger paths (e.g. page fault). Landlock eBPF programs can then take decisions accordingly.
--- include/uapi/linux/bpf.h | 3 ++- security/landlock/lsm.c | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-)
include/uapi/linux/bpf.h +2 -1
@@ -580,7 +580,8 @@ enum landlock_hook_id {/* Trigger type */ #define LANDLOCK_FLAG_ORIGIN_SYSCALL (1 << 0)#define LANDLOCK_FLAG_ORIGIN_SECCOMP (1 << 1) -#define _LANDLOCK_FLAG_ORIGIN_MASK ((1 << 2) - 1) +#define LANDLOCK_FLAG_ORIGIN_INTERRUPT (1 << 2) +#define _LANDLOCK_FLAG_ORIGIN_MASK ((1 << 3) - 1)/* context of function access flags */ #define _LANDLOCK_FLAG_ACCESS_MASK ((1ULL << 0) - 1)
security/landlock/lsm.c +15 -2
@@ -17,6 +17,7 @@#include <linux/kernel.h> /* FIELD_SIZEOF() */ #include <linux/landlock.h> #include <linux/lsm_hooks.h> +#include <linux/preempt.h> /* in_interrupt() */ #include <linux/seccomp.h> /* struct seccomp_* */ #include <linux/types.h> /* uintptr_t */
@@ -109,6 +110,7 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6])#endif /* CONFIG_CGROUP_BPF */ struct landlock_rule *rule;u32 hook_idx =get_index (hook_id); +u16 current_call;struct landlock_data ctx = { .hook = hook_id,
@@ -128,6 +130,16 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6])* prioritize fine-grained policies (i.e. per thread), and return early. */ +if (unlikely (in_interrupt ())) { + current_call =LANDLOCK_FLAG_ORIGIN_INTERRUPT ; +#ifdef CONFIG_SECCOMP_FILTER +/* bypass landlock_ret evaluation */ + goto seccomp_int; +#endif /* CONFIG_SECCOMP_FILTER */ + }else { + current_call =LANDLOCK_FLAG_ORIGIN_SYSCALL ; + } +#ifdef CONFIG_SECCOMP_FILTER /* seccomp triggers and landlock_ret cleanup */ ctx.origin =LANDLOCK_FLAG_ORIGIN_SECCOMP ;
@@ -164,8 +176,9 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6])return -ret; ctx.cookie =0 ; +seccomp_int:/* syscall trigger */ - ctx.origin =LANDLOCK_FLAG_ORIGIN_SYSCALL ; + ctx.origin = current_call; ret =landlock_run_prog_for_syscall (hook_idx, &ctx, current->seccomp.landlock_hooks);if (ret)
@@ -175,7 +188,7 @@ static int landlock_run_prog(enum landlock_hook_id hook_id, __u64 args[6])#ifdef CONFIG_CGROUP_BPF /* syscall trigger */ if (cgroup_bpf_enabled) { - ctx.origin =LANDLOCK_FLAG_ORIGIN_SYSCALL ; + ctx.origin = current_call;/* get the default cgroup associated with the current thread */ cgrp =task_css_set (current)->dfl_cgrp; ret = landlock_run_prog_for_syscall(hook_idx, &ctx,
Signature · 1 line
2.9.3
Andy Lutomirski
reviewerRe: [RFC v3 19/22] landlock: Add interrupted origin
Quoted 9 lines · Mickaël Salaün
This third origin of hook call should cover all possible trigger paths (e.g. page fault). Landlock eBPF programs can then take decisions accordingly.
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Kees Cook <[email protected]>
Quoted 5 lines
---
+ if (unlikely(in_interrupt())) {
IMO security hooks have no business being called from interrupts. Aren't they all synchronous things done by tasks? Interrupts are driver things.
Are you trying to check for page faults and such?
--Andy
Mickaël Salaün
maintainerRe: [RFC v3 19/22] landlock: Add interrupted origin
Quoted 14 lines · Andy Lutomirski
Quoted 13 lines · Mickaël Salaün
This third origin of hook call should cover all possible trigger paths (e.g. page fault). Landlock eBPF programs can then take decisions accordingly.
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Kees Cook <[email protected]>---
Quoted 7 lines
Quoted 1 line
+ if (unlikely(in_interrupt())) {
IMO security hooks have no business being called from interrupts. Aren't they all synchronous things done by tasks? Interrupts are driver things.
Are you trying to check for page faults and such?
Yes, that was the idea you did put in my mind. Not sure how to deal with this.
Andy Lutomirski
reviewerRe: [RFC v3 19/22] landlock: Add interrupted origin
Quoted 18 lines · Mickaël Salaün
Quoted 16 lines · Andy Lutomirski
Quoted 14 lines · Mickaël Salaün
This third origin of hook call should cover all possible trigger paths (e.g. page fault). Landlock eBPF programs can then take decisions accordingly.
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Kees Cook <[email protected]>---
+ if (unlikely(in_interrupt())) {
Quoted 9 lines
Quoted 5 lines
IMO security hooks have no business being called from interrupts. Aren't they all synchronous things done by tasks? Interrupts are driver things.
Are you trying to check for page faults and such?
Yes, that was the idea you did put in my mind. Not sure how to deal with this.
It's not so easy, unfortunately. The easiest reliable way might be to set a TS_ flag on all syscall entries when TIF_SECCOMP or similar is set.
--Andy
Re: [RFC v3 19/22] landlock: Add interrupted origin
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines · Mickaël Salaün
Quoted 21 lines · Andy Lutomirski
Quoted 9 lines · Mickaël Salaün
This third origin of hook call should cover all possible trigger paths (e.g. page fault). Landlock eBPF programs can then take decisions accordingly.
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Kees Cook <[email protected]>Quoted 5 lines
---
+ if (unlikely(in_interrupt())) {
IMO security hooks have no business being called from interrupts. Aren't they all synchronous things done by tasks? Interrupts are driver things.
Are you trying to check for page faults and such?
Quoted 7 lines
Quoted 3 lines
Yes, that was the idea you did put in my mind. Not sure how to deal with this.
It's not so easy, unfortunately. The easiest reliable way might be to set a
TS_flag on all syscall entries whenTIF_SECCOMPor similar is set.
For making this series smaller, let's leave the idea idea of interrupt hooks out -- the intention is for stricter syscall filtering, yes?
Once things are more well established and there's a use-case for this, it can be added back in.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Mickaël Salaün
maintainerRe: [RFC v3 19/22] landlock: Add interrupted origin
Quoted 34 lines · Kees Cook
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines · Mickaël Salaün
Quoted 21 lines · Andy Lutomirski
Quoted 9 lines · Mickaël Salaün
This third origin of hook call should cover all possible trigger paths (e.g. page fault). Landlock eBPF programs can then take decisions accordingly.
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Kees Cook <[email protected]>Quoted 5 lines
---
+ if (unlikely(in_interrupt())) {
IMO security hooks have no business being called from interrupts. Aren't they all synchronous things done by tasks? Interrupts are driver things.
Are you trying to check for page faults and such?
Quoted 7 lines
Quoted 3 lines
Yes, that was the idea you did put in my mind. Not sure how to deal with this.
It's not so easy, unfortunately. The easiest reliable way might be to set a
TS_flag on all syscall entries whenTIF_SECCOMPor similar is set.
Quoted 5 lines
For making this series smaller, let's leave the idea idea of interrupt hooks out -- the intention is for stricter syscall filtering, yes?
Once things are more well established and there's a use-case for this, it can be added back in.
Right, I'm no more convinced it's worth it.
Mickaël Salaün
maintainerRFC v3 20/22 landlock: Add update and debug access flags
In reply to Mickaël SalaünFor now, the update and debug accesses are only accessible to a process with CAP_SYS_ADMIN. This could change in the future.
The capability check is statically done when loading an eBPF program, according to the current process. If the process has enough rights and set the appropriate access flags, then the dedicated functions or data will be accessible.
With the update access, the following functions are available:
* bpf_map_lookup_elem
* bpf_map_update_elem
* bpf_map_delete_elem
* bpf_tail_call
With the debug access, the following functions are available:
* bpf_trace_printk
* bpf_get_prandom_u32
* bpf_get_current_pid_tgid
* bpf_get_current_uid_gid
* bpf_get_current_comm
--- include/uapi/linux/bpf.h | 4 +++- security/landlock/lsm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +3 -1
@@ -584,7 +584,9 @@ enum landlock_hook_id {#define _LANDLOCK_FLAG_ORIGIN_MASK ((1 << 3) - 1)/* context of function access flags */ -#define _LANDLOCK_FLAG_ACCESS_MASK ((1ULL << 0) - 1) +#define LANDLOCK_FLAG_ACCESS_UPDATE (1 << 0) +#define LANDLOCK_FLAG_ACCESS_DEBUG (1 << 1) +#define _LANDLOCK_FLAG_ACCESS_MASK ((1ULL << 2) - 1)/* Handle check flags */ #define LANDLOCK_FLAG_FS_DENTRY (1 << 0)
security/landlock/lsm.c +54 -0
71 lines not on this page · raw message
Signature · 1 line
2.9.3
Mickaël Salaün
maintainerRFC v3 21/22 bpf,landlock: Add optional skb pointer in the Landlock context
In reply to Mickaël SalaünThis is a proof of concept to expose optional values that could depend of the process access rights.
There is two dedicated flags: LANDLOCK_FLAG_ACCESS_SKB_READ and LANDLOCK_FLAG_ACCESS_SKB_WRITE. Each of them can be activated to access eBPF functions manipulating a skb in a read or write way.
--- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 7 ++++++- kernel/bpf/verifier.c | 6 ++++++ security/landlock/lsm.c | 26 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-)
include/linux/bpf.h +2 -0
@@ -88,6 +88,7 @@ enum bpf_arg_type {ARG_PTR_TO_STRUCT_FILE ,/* pointer to struct file */ ARG_CONST_PTR_TO_LANDLOCK_HANDLE_FS ,/* pointer to Landlock FS handle */ + ARG_PTR_TO_STRUCT_SKB, /* pointer to struct skb */ };/* type of values returned from helper functions */
@@ -150,6 +151,7 @@ enum bpf_reg_type {/* Landlock */ PTR_TO_STRUCT_FILE ,CONST_PTR_TO_LANDLOCK_HANDLE_FS , + PTR_TO_STRUCT_SKB, };struct bpf_prog ;
include/uapi/linux/bpf.h +6 -1
@@ -586,7 +586,9 @@ enum landlock_hook_id {/* context of function access flags */ #define LANDLOCK_FLAG_ACCESS_UPDATE (1 << 0)#define LANDLOCK_FLAG_ACCESS_DEBUG (1 << 1) -#define _LANDLOCK_FLAG_ACCESS_MASK ((1ULL << 2) - 1) +#define LANDLOCK_FLAG_ACCESS_SKB_READ (1 << 2) +#define LANDLOCK_FLAG_ACCESS_SKB_WRITE (1 << 3) +#define _LANDLOCK_FLAG_ACCESS_MASK ((1ULL << 4) - 1)/* Handle check flags */ #define LANDLOCK_FLAG_FS_DENTRY (1 << 0)
@@ -619,12 +621,15 @@ struct landlock_handle {* @args: LSM hook arguments, see include/linux/lsm_hooks.h for there * description and the LANDLOCK_HOOK* definitions from * security/landlock/lsm.c for their types. +* @opt_skb: optional skb pointer, accessible with the +* LANDLOCK_FLAG_ACCESS_SKB_* flags for network-related hooks. */ struct landlock_data {__u32 hook;/* enum landlock_hook_id */ __u16 origin;/* LANDLOCK_FLAG_ORIGIN_* */ __u16 cookie;/* seccomp RET_LANDLOCK */ __u64 args[6 ]; +__u64 opt_skb; };#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/verifier.c +6 -0
@@ -247,6 +247,7 @@ static const char * const reg_type_str[] = { [PTR_TO_PACKET_END] = "pkt_end", [PTR_TO_STRUCT_FILE] = "struct_file", [CONST_PTR_TO_LANDLOCK_HANDLE_FS] = "landlock_handle_fs", + [PTR_TO_STRUCT_SKB] = "struct_skb", };static void print_verifier_state (struct verifier_state *state)
@@ -559,6 +560,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)case CONST_PTR_TO_MAP :case PTR_TO_STRUCT_FILE :case CONST_PTR_TO_LANDLOCK_HANDLE_FS : +case PTR_TO_STRUCT_SKB :return true;default :return false;
@@ -984,6 +986,10 @@ static int check_func_arg(struct verifier_env *env, u32 regno, expected_type =CONST_PTR_TO_LANDLOCK_HANDLE_FS ;if (type != expected_type) goto err_type; + }else if (arg_type ==ARG_PTR_TO_STRUCT_SKB ) { + expected_type =PTR_TO_STRUCT_SKB ; +if (type != expected_type) + goto err_type; }else if (arg_type ==ARG_PTR_TO_STACK || arg_type ==ARG_PTR_TO_RAW_STACK ) { expected_type =PTR_TO_STACK ;
security/landlock/lsm.c +26 -0
@@ -281,6 +281,7 @@ static bool __is_valid_access(int off, int size, enum bpf_access_type type,break ; case offsetof(struct landlock_data, args[0]) ... offsetof(struct landlock_data, args[5]): +case offsetof(struct landlock_data , opt_skb): expected_size =sizeof (__u64);break ;default :
@@ -299,6 +300,13 @@ static bool __is_valid_access(int off, int size, enum bpf_access_type type,if (*reg_type ==NOT_INIT )return false;break ; +case offsetof(struct landlock_data , opt_skb): +if (!(prog_subtype->landlock_hook.access & + (LANDLOCK_FLAG_ACCESS_SKB_READ | +LANDLOCK_FLAG_ACCESS_SKB_WRITE ))) +return false; + *reg_type =PTR_TO_STRUCT_SKB ; +break ; }return true;
@@ -401,6 +409,24 @@ static inline bool bpf_landlock_is_valid_subtype(if (prog_subtype->landlock_hook.access &LANDLOCK_FLAG_ACCESS_DEBUG && !capable (CAP_SYS_ADMIN ))return false; +/* +* Capability checks must be enforced for every landlocked process. +* To support user namespaces/capabilities, we must then check the +* namespaces of a task before putting it in a landlocked cgroup. +* This could be implemented in the future. +*/ +if (prog_subtype->landlock_hook.access &LANDLOCK_FLAG_ACCESS_SKB_READ && + !capable (CAP_NET_ADMIN )) +return false; +/* +* It is interesting to differentiate read and write access to be able +* to securely delegate some work to unprivileged (and potentially +* compromised/untrusted) processes. This different type of access can +* be checked for function calls or context accesses. +*/ +if (prog_subtype->landlock_hook.access &LANDLOCK_FLAG_ACCESS_SKB_WRITE && + !capable (CAP_NET_ADMIN )) +return false;return true; }
Signature · 1 line
2.9.3
Re: [RFC v3 21/22] bpf,landlock: Add optional skb pointer in the Landlock context
Quoted 8 lines · Mickaël Salaün
This is a proof of concept to expose optional values that could depend of the process access rights.
There is two dedicated flags:
LANDLOCK_FLAG_ACCESS_SKB_READandLANDLOCK_FLAG_ACCESS_SKB_WRITE. Each of them can be activated to access eBPF functions manipulating a skb in a read or write way.Signed-off-by Mickaël Salaün <[email protected]>
...
Quoted 6 lines
/* Handle check flags */
#defineLANDLOCK_FLAG_FS_DENTRY(1 << 0)include/uapi/linux/bpf.h +0
-0@@ -619,12 +621,15 @@ struct landlock_handle {* @args: LSM hook arguments, see include/linux/lsm_hooks.h for there * description and the LANDLOCK_HOOK* definitions from * security/landlock/lsm.c for their types.
include/uapi/linux/bpf.h struct landlock_handle { · line 633
Quoted 10 lines
include/uapi/linux/bpf.h +3
-0+* @opt_skb: optional skb pointer, accessible with the +* LANDLOCK_FLAG_ACCESS_SKB_* flags for network-related hooks. */ struct landlock_data {__u32 hook;/* enum landlock_hook_id */ __u16 origin;/* LANDLOCK_FLAG_ORIGIN_* */ __u16 cookie;/* seccomp RET_LANDLOCK */ __u64 args[6 ]; +__u64 opt_skb; };
missing something here.
This patch doesn't make use of it. That's something for the future?
How that field will be populated?
Why make it different vs the rest or args[6] ?
Mickaël Salaün
maintainerRe: [RFC v3 21/22] bpf,landlock: Add optional skb pointer in the Landlock context
Quoted 24 lines · Alexei Starovoitov
Quoted 8 lines · Mickaël Salaün
This is a proof of concept to expose optional values that could depend of the process access rights.
There is two dedicated flags:
LANDLOCK_FLAG_ACCESS_SKB_READandLANDLOCK_FLAG_ACCESS_SKB_WRITE. Each of them can be activated to access eBPF functions manipulating a skb in a read or write way.Signed-off-by Mickaël Salaün <[email protected]>...
Quoted 14 lines
/* Handle check flags */
#defineLANDLOCK_FLAG_FS_DENTRY(1 << 0)include/uapi/linux/bpf.h +2
-0@@ -619,12 +621,15 @@ struct landlock_handle {* @args: LSM hook arguments, see include/linux/lsm_hooks.h for there * description and the LANDLOCK_HOOK* definitions from * security/landlock/lsm.c for their types. +* @opt_skb: optional skb pointer, accessible with the +* LANDLOCK_FLAG_ACCESS_SKB_* flags for network-related hooks. */ struct landlock_data {__u32 hook;/* enum landlock_hook_id */ __u16 origin;/* LANDLOCK_FLAG_ORIGIN_* */ __u16 cookie;/* seccomp RET_LANDLOCK */ __u64 args[6 ];
Quoted 10 lines
include/uapi/linux/bpf.h struct landlock_handle { · line 633
Quoted 2 lines
include/uapi/linux/bpf.h +1
-0+__u64 opt_skb; };missing something here.
This patch doesn't make use of it. That's something for the future?
How that field will be populated?
Why make it different vs the rest or args[6] ?
I don't use this code, it's only purpose is to show how to deal with fine-grained privileges of Landlock programs (to allow Sargun to add his custom helpers from Checmate). However, this optional field may be part of args[6].
Mickaël Salaün
maintainerRFC v3 22/22 samples/landlock: Add sandbox example
In reply to Mickaël SalaünAdd a basic sandbox tool to create a process isolated from some part of the system. This can depend of the current cgroup.
Example with the current process hierarchy (seccomp):
$ ls /home
user1
$ LANDLOCK_ALLOWED='/bin:/lib:/usr:/tmp:/proc/self/fd/0' \
./samples/landlock/sandbox /bin/sh -i
Launching a new sandboxed process.
$ ls /home
ls: cannot open directory '/home': Permission deniedExample with a cgroup:
$ mkdir /sys/fs/cgroup/sandboxed
$ ls /home
user1
$ LANDLOCK_CGROUPS='/sys/fs/cgroup/sandboxed' \
LANDLOCK_ALLOWED='/bin:/lib:/usr:/tmp:/proc/self/fd/0' \
./samples/landlock/sandbox
Ready to sandbox with cgroups.
$ ls /home
user1
$ echo $$ > /sys/fs/cgroup/sandboxed/cgroup.procs
$ ls /home
ls: cannot open directory '/home': Permission deniedChanges since v2:
* use BPF_PROG_ATTACH for cgroup handling
--- samples/Makefile | 2 +- samples/landlock/.gitignore | 1 + samples/landlock/Makefile | 16 +++ samples/landlock/sandbox.c | 307 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 samples/landlock/.gitignore create mode 100644 samples/landlock/Makefile create mode 100644 samples/landlock/sandbox.c
samples/Makefile +1 -1
@@ -2,4 +2,4 @@ obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ trace_events/ livepatch/ \ hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/ \ - configfs/ connector/ v4l/ trace_printk/ + configfs/ connector/ v4l/ trace_printk/ landlock/
samples/landlock/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/sandbox
samples/landlock/Makefile new file +16 -0
@@ -0,0 +1,16 @@ +# kbuild trick to avoid linker error. Can be omitted if a module is built. +obj- := dummy.o + +hostprogs-$(CONFIG_SECURITY_LANDLOCK) := sandbox +sandbox-objs := sandbox.o + +always := $(hostprogs-y) + +HOSTCFLAGS += -I$(objtree)/usr/include + +# Trick to allow make to be run from this directory +all: + $(MAKE) -C ../../ $$PWD/ + +clean: + $(MAKE) -C ../../ M=$$PWD clean
samples/landlock/sandbox.c new file +307 -0
307 lines not on this page · raw message
Signature · 1 line
2.9.3
Re: [RFC v3 22/22] samples/landlock: Add sandbox example
Quoted 32 lines · Mickaël Salaün
Add a basic sandbox tool to create a process isolated from some part of the system. This can depend of the current cgroup.
Example with the current process hierarchy (seccomp):
$ ls /home user1 $ LANDLOCK_ALLOWED='/bin:/lib:/usr:/tmp:/proc/self/fd/0' \ ./samples/landlock/sandbox /bin/sh -i Launching a new sandboxed process. $ ls /home ls: cannot open directory '/home': Permission deniedExample with a cgroup:
$ mkdir /sys/fs/cgroup/sandboxed $ ls /home user1 $ LANDLOCK_CGROUPS='/sys/fs/cgroup/sandboxed' \ LANDLOCK_ALLOWED='/bin:/lib:/usr:/tmp:/proc/self/fd/0' \ ./samples/landlock/sandbox Ready to sandbox with cgroups. $ ls /home user1 $ echo $$ > /sys/fs/cgroup/sandboxed/cgroup.procs $ ls /home ls: cannot open directory '/home': Permission deniedChanges since v2:
* useBPF_PROG_ATTACHfor cgroup handlingSigned-off-by Mickaël Salaün <[email protected]>
...
samples/landlock/sandbox.c line 157
Quoted 11 lines
samples/landlock/sandbox.c new file +11
-0+struct bpf_insn hook_path[] = { +/* specify an option, if any */ +BPF_MOV32_IMM (BPF_REG_1 ,0 ), +/* handles to compare with */ +BPF_LD_MAP_FD (BPF_REG_2 , map_fs), +BPF_MOV64_IMM (BPF_REG_3 ,BPF_MAP_ARRAY_OP_OR ), +/* hook argument (struct file) */ +BPF_LDX_MEM (BPF_DW ,BPF_REG_4 ,BPF_REG_6 , offsetof(struct + landlock_data, args[0])), +/* checker function */ +BPF_EMIT_CALL (BPF_FUNC_landlock_cmp_fs_beneath_with_struct_file),
the example is sweet!
Since only that helper is used, could you skip the other one from the patches (for now) ?
From: Mickaël Salaün
Quoted 1 line
Sent: 14 September 2016 08:24
...
Quoted 10 lines
## Why does seccomp-filter is not enough?
A seccomp filter can access to raw syscall arguments which means that it is not possible to filter according to pointed data as a file path. As demonstrated the first version of this patch series, filtering at the syscall level is complicated (e.g. need to take care of race conditions). This is mainly because the access control checkpoints of the kernel are not at this high-level but more underneath, at LSM hooks level. The LSM hooks are designed to handle this kind of checks. This series use this approach to leverage the ability of unprivileged users to limit themselves.
You cannot validate file path parameters during syscall entry. It can only be done after the user buffer has been read into kernel memory. (ie you must only access the buffer once.)
This has nothing to do with where the kernel does any access checks, and everything to do with the fact that another thread/process can modify the buffer after you have validated it.
David