Landlock LSM: Toward unprivileged sandboxing
Subsystems (20)
Mickaël Salaün
maintainerv5 cover Landlock LSM: Toward unprivileged sandboxing
Hi,
This series follows 4 previous RFCs [1]. This is the first step of the roadmap discussed at LPC [2] (with the inheritance feature included). The big change is an abstraction over LSM hooks. Instead of exposing a similar interface to userland, Landlock wraps LSM hooks into Landlock events. A Landlock rule is a dedicated eBPF program enforced for the calling process through the seccomp(2) syscall. While the intended final goal is to allow unprivileged users to use Landlock, this series allows only a process with global CAP_SYS_ADMIN to load and enforce a rule. This may help to get feedback and avoid unexpected behaviors. The documentation patch contains some kernel documentation and explanations on how to use Landlock. The compiled documentation can be found here:
https://landlock-lsm.github.io/linux-doc/landlock-v5/security/landlock/index.html
This series can be applied on top of net-next: e623a9e9dec2 ("net: socket: fix recvmmsg not returning error from sock_error"). You may need a commit from the perf tree: 7a5980f9c006 ("tools lib bpf: Add missing header to the library"). This can be tested with CONFIG_SECCOMP_FILTER and CONFIG_SECURITY_LANDLOCK. I would really appreciate constructive comments on the usability, architecture, code, userland API or use cases.
# Landlock LSM
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 XNU Sandbox or OpenBSD Pledge. This kind of sandbox is expected to help mitigate the security impact of bugs or unexpected/malicious behaviors in user-space applications.
The approach taken is to add the minimum amount of code while still allowing the user-space application to create quite complex access rules. A dedicated security policy language such as the one used by SELinux, AppArmor and other major LSMs involves a lot of code and is usually permitted to only a trusted user (i.e. root). On the contrary, eBPF programs already exist and are designed to be safely loaded by unprivileged user-space.
This design does not seem 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 user-space 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 the LLVM front-end, it is quite easy to write an eBPF program with a subset of the C language.
# Landlock events and rule enforcement
Unlike syscalls, LSM hooks are security checkpoints and are not architecture dependent. They are designed to match a security need associated with a security policy (e.g. access to a file). The approach taken for Landlock is to abstract these hooks with Landlock events such as a generic filesystem event (LANDLOCK_SUBTYPE_EVENT_FS). Further explanations can be found in the documentation.
This series uses seccomp(2) only as an entry point to apply a rule to the calling process and its future children. It is planed to restore the ability to use cgroup as an alternative way to enforce a Landlock rule.
There is as yet no way to allow a process to access only a subset of the filesystem where the subset is specified via a path or a file descriptor. This feature is intentionally left out so as to minimize the amount of code of this patch series but will come in a following series. However, it is possible to check the file type, as done in the following example.
# Sandbox example with a read-only filesystem
This example is provided in the samples/bpf directory. It creates a read-only environment for all kind of file access except for character devices such as a TTY.
# :> X
# echo $? 0 # ./samples/bpf/landlock1 /bin/sh -i Launching a new sandboxed process. # :> Y cannot create Y: Operation not permitted
# Warning on read-only filesystems
Other than owing a mount namespace and remounting every accessible mounts points as read-only, which may not be possible for an unprivileged security sandbox, there is no way of preventing a process to change the access time of a file, including anonymous inodes. This provides a trivial way to leak information from a sandboxed environment. A new LSM hook has been proposed to allow an LSM to enforce a real read-only filesystem view, but it did not get strong support so far [5].
# Frequently asked questions
## Why is seccomp-bpf not enough?
A seccomp filter can access only raw syscall arguments (i.e. the register values) which means that it is not possible to filter according to the value pointed to by an argument, such as a file pathname. As an embryonic Landlock version demonstrated, 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 the LSM-hook level. The LSM hooks are designed to handle this kind of checks. Landlock abstracts this approach to leverage the ability of unprivileged users to limit themselves.
Cf. section "What it isn't?" in Documentation/prctl/seccomp_filter.txt
## Why use 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 children. It makes sense to use an unique access-restricting syscall (that should be allowed by seccomp filters) which can only drop privileges. Moreover, a Landlock rule 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 enforcement via seccomp(2).
## Why a new LSM? Are SELinux, AppArmor, Smack and Tomoyo 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 application by its developer and *unprivileged user* as seccomp can do for raw syscall filtering.
Differences from other (access control) LSMs:
* not only dedicated to administrators (i.e. no_new_priv);
* limited kernel attack surface (e.g. policy parsing);
* constrained policy rules (no DoS: deterministic execution time);
* do not leak more information than the loader process can legitimately have access to (minimize metadata inference).
# Changes since v4
* revamp Landlock to not expose an LSM hook interface but wrap and abstract them with Landlock events (currently one for all filesystem related
operations: LANDLOCK_SUBTYPE_EVENT_FS)
* wrap all filesystem kernel objects through the same FS handle (struct landlock_handle_fs): struct file, struct inode, struct path and struct dentry
* a rule don't return an errno code but only a boolean to allow or deny an access request
* handle all filesystem related LSM hooks
* add some tests and a sample:
* BPF context tests
* Landlock sandboxing tests and sample
* write Landlock rules in C and compiled them with LLVM
* change field names of eBPF program subtype
* remove arraymap of handles for now (will be replaced with a revamped map)
* remove cgroup handling for now
* add user and kernel documentation
* rebase on net-next (which includes some needed commits already upstreamed)
# Changes since v3
* use abstract LSM hook arguments with custom types (e.g. *_LANDLOCK_ARG_FS for struct file, struct inode and struct path)
* add more LSM hooks to support full filesystem access control
* improve the sandbox example
* fix races and RCU issues:
* eBPF program execution and eBPF helpers
* revamp the arraymap of handles to cleanly deal with update/delete
* eBPF program subtype for Landlock:
* remove the "origin" field
* add an "option" field
* rebase onto Daniel Mack's patches v7 [3]
* remove merged commit 1955351da41c ("bpf: Set register type according to is_valid_access()")
* fix spelling mistakes
* cleanup some type and variable names
* split patches
* for now, remove cgroup delegation handling for unprivileged user
* remove extra access check for cgroup_get_from_fd()
* remove unused example code dealing with skb
* remove seccomp-bpf link:
* no more seccomp cookie
* for now, it is no more possible to check the current syscall properties
# Changes since v2
* revamp cgroup handling:
* use Daniel Mack's patches "Add eBPF hooks for cgroups" v5
* 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 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
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]
Regards,
Mickaël Salaün (10):
bpf: Add eBPF program subtype and is_valid_subtype() verifier bpf,landlock: Define an eBPF program type for Landlock
bpf: Define handle_fs and add a new helper bpf_handle_fs_get_mode()
landlock: Add LSM hooks related to filesystem
seccomp: Split put_seccomp_filter() with put_seccomp() seccomp,landlock: Handle Landlock events per process hierarchy
bpf: Add a Landlock sandbox example
seccomp: Enhance test_harness with an assert step mechanism bpf,landlock: Add tests for Landlock
landlock: Add user and kernel documentation for Landlock
Documentation/security/index.rst | 1 + Documentation/security/landlock/index.rst | 19 + Documentation/security/landlock/kernel.rst | 132 +++ Documentation/security/landlock/user.rst | 298 +++++++ include/linux/bpf.h | 40 +- include/linux/filter.h | 1 + include/linux/landlock.h | 80 ++ include/linux/lsm_hooks.h | 5 + include/linux/seccomp.h | 12 +- include/uapi/linux/bpf.h | 125 ++- include/uapi/linux/seccomp.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/helpers_fs.c | 52 ++ kernel/bpf/syscall.c | 5 +- kernel/bpf/verifier.c | 16 +- kernel/fork.c | 16 +- kernel/seccomp.c | 26 +- kernel/trace/bpf_trace.c | 15 +- net/core/filter.c | 48 +- samples/bpf/.gitignore | 32 + samples/bpf/Makefile | 4 + samples/bpf/bpf_helpers.h | 2 + samples/bpf/bpf_load.c | 29 +- samples/bpf/fds_example.c | 2 +- samples/bpf/landlock1_kern.c | 46 + samples/bpf/landlock1_user.c | 102 +++ samples/bpf/sock_example.c | 2 +- samples/bpf/test_cgrp2_attach.c | 2 +- samples/bpf/test_cgrp2_attach2.c | 2 +- samples/bpf/test_cgrp2_sock.c | 2 +- security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 18 + security/landlock/Makefile | 5 + security/landlock/common.h | 25 + security/landlock/hooks.c | 962 +++++++++++++++++++++ security/landlock/manager.c | 321 +++++++ security/security.c | 7 +- tools/include/uapi/linux/bpf.h | 125 ++- tools/lib/bpf/bpf.c | 5 +- tools/lib/bpf/bpf.h | 2 +- tools/lib/bpf/libbpf.c | 4 +- tools/perf/tests/bpf.c | 2 +- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/bpf/test_tag.c | 2 +- tools/testing/selftests/bpf/test_verifier.c | 57 +- tools/testing/selftests/landlock/.gitignore | 2 + tools/testing/selftests/landlock/Makefile | 47 + tools/testing/selftests/landlock/rules/Makefile | 52 ++ tools/testing/selftests/landlock/rules/README.rst | 1 + .../testing/selftests/landlock/rules/bpf_helpers.h | 1 + tools/testing/selftests/landlock/rules/fs1.c | 31 + tools/testing/selftests/landlock/rules/fs2.c | 31 + tools/testing/selftests/landlock/test_fs.c | 347 ++++++++ tools/testing/selftests/seccomp/test_harness.h | 8 +- 55 files changed, 3116 insertions(+), 62 deletions(-) create mode 100644 Documentation/security/landlock/index.rst create mode 100644 Documentation/security/landlock/kernel.rst create mode 100644 Documentation/security/landlock/user.rst create mode 100644 include/linux/landlock.h create mode 100644 kernel/bpf/helpers_fs.c create mode 100644 samples/bpf/.gitignore create mode 100644 samples/bpf/landlock1_kern.c create mode 100644 samples/bpf/landlock1_user.c create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/common.h create mode 100644 security/landlock/hooks.c create mode 100644 security/landlock/manager.c create mode 100644 tools/testing/selftests/landlock/.gitignore create mode 100644 tools/testing/selftests/landlock/Makefile create mode 100644 tools/testing/selftests/landlock/rules/Makefile create mode 120000 tools/testing/selftests/landlock/rules/README.rst create mode 120000 tools/testing/selftests/landlock/rules/bpf_helpers.h create mode 100644 tools/testing/selftests/landlock/rules/fs1.c create mode 100644 tools/testing/selftests/landlock/rules/fs2.c create mode 100644 tools/testing/selftests/landlock/test_fs.c
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerPATCH v5 1/10 bpf: Add eBPF program subtype and is_valid_subtype() verifier
The goal of the program subtype 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 beginning 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 (see next commit) but this could be used by other program types in the future.
Changes since v4:
* replace the "status" field with "version" (more generic)
* replace the "access" field with "ability" (less confusing)
Changes since v3:
* remove the "origin" field
* add an "option" field
* cleanup comments
--- include/linux/bpf.h | 7 +++-- include/linux/filter.h | 1 + include/uapi/linux/bpf.h | 10 ++++++ kernel/bpf/syscall.c | 5 +-- kernel/bpf/verifier.c | 10 ++++-- kernel/trace/bpf_trace.c | 15 ++++++--- net/core/filter.c | 48 ++++++++++++++++++----------- samples/bpf/bpf_load.c | 3 +- samples/bpf/fds_example.c | 2 +- samples/bpf/sock_example.c | 2 +- samples/bpf/test_cgrp2_attach.c | 2 +- samples/bpf/test_cgrp2_attach2.c | 2 +- samples/bpf/test_cgrp2_sock.c | 2 +- tools/include/uapi/linux/bpf.h | 10 ++++++ tools/lib/bpf/bpf.c | 5 ++- tools/lib/bpf/bpf.h | 2 +- tools/lib/bpf/libbpf.c | 4 +-- tools/perf/tests/bpf.c | 2 +- tools/testing/selftests/bpf/test_tag.c | 2 +- tools/testing/selftests/bpf/test_verifier.c | 3 +- 20 files changed, 95 insertions(+), 42 deletions(-)
include/linux/bpf.h +5 -2
@@ -154,19 +154,22 @@ 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);int (*gen_prologue)(struct bpf_insn *insn,bool direct_write,const struct bpf_prog *prog);u32 (*convert_ctx_access)(enum bpf_access_type type,const struct bpf_insn *src,struct bpf_insn *dst,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
@@ -417,6 +417,7 @@ struct bpf_prog {enum bpf_prog_type type;/* Type of BPF program */ u32 len;/* Number of filter blocks */ u8 tag[BPF_TAG_SIZE ]; +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 void *ctx,
include/uapi/linux/bpf.h +10 -0
@@ -145,6 +145,15 @@ enum bpf_attach_type {*/ #define BPF_F_NO_COMMON_LRU (1U << 1) +union bpf_prog_subtype { +struct { +__u32 version;/* cf. documentation */ +__u32 event;/* enum landlock_subtype_event */ +__aligned_u64 ability;/* LANDLOCK_SUBTYPE_ABILITY_* */ +__aligned_u64 option;/* LANDLOCK_SUBTYPE_OPTION_* */ + } landlock_rule; +} __attribute__((aligned (8 ))); +union bpf_attr {struct {/* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type;/* one of enum bpf_map_type */
@@ -173,6 +182,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; };struct {/* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/syscall.c +3 -2
@@ -628,7 +628,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 */
@@ -827,7 +827,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) {
@@ -885,6 +885,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 +8 -2
@@ -740,7 +740,8 @@ static int check_ctx_access(struct bpf_verifier_env *env, int off, int size,return 0 ;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;
@@ -1290,7 +1291,8 @@ static int check_call(struct bpf_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 %s#%d\n" ,func_id_name (func_id), func_id);
@@ -3261,6 +3263,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)struct bpf_verifier_env *env;int ret = -EINVAL ; +if ((*prog)->aux->ops->is_valid_subtype && + !(*prog)->aux->ops->is_valid_subtype (&(*prog)->subtype)) +return -EINVAL ; +/* 'struct bpf_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 +10 -5
@@ -469,7 +469,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:
@@ -483,7 +484,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;
@@ -558,7 +560,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:
@@ -571,7 +574,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;
@@ -595,7 +599,8 @@ static struct bpf_prog_type_list tracepoint_tl __ro_after_init = { };static bool pe_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 bpf_perf_event_data ))return false;
net/core/filter.c +30 -18
144 lines not on this page · raw message
samples/bpf/bpf_load.c +2 -1
@@ -29,6 +29,7 @@static char license[128 ];static int kern_version; +static union bpf_prog_subtype subtype = {};static bool processed_sec[128 ];char bpf_log_buf[BPF_LOG_BUF_SIZE ];int map_fd[MAX_MAPS ];
@@ -98,7 +99,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) } fd =bpf_load_program (prog_type, prog, insns_cnt, license, kern_version, - bpf_log_buf,BPF_LOG_BUF_SIZE ); + bpf_log_buf,BPF_LOG_BUF_SIZE , &subtype);if (fd <0 ) {printf ("bpf_load_program() err=%d\n%s" , errno, bpf_log_buf);return -1 ;
samples/bpf/fds_example.c +1 -1
@@ -62,7 +62,7 @@ static int bpf_prog_create(const char *object) }else {return bpf_load_program (BPF_PROG_TYPE_SOCKET_FILTER , insns, insns_cnt,"GPL" ,0 , - bpf_log_buf,BPF_LOG_BUF_SIZE ); + bpf_log_buf,BPF_LOG_BUF_SIZE ,NULL ); } }
samples/bpf/sock_example.c +1 -1
@@ -60,7 +60,7 @@ static int test_sock(void)size_t insns_cnt =sizeof (prog) /sizeof (struct bpf_insn ); prog_fd =bpf_load_program (BPF_PROG_TYPE_SOCKET_FILTER , prog, insns_cnt, -"GPL" ,0 , bpf_log_buf,BPF_LOG_BUF_SIZE ); +"GPL" ,0 , bpf_log_buf,BPF_LOG_BUF_SIZE ,NULL );if (prog_fd <0 ) {printf ("failed to load prog '%s'\n" ,strerror (errno)); goto cleanup;
samples/bpf/test_cgrp2_attach.c +1 -1
@@ -72,7 +72,7 @@ static int prog_load(int map_fd, int verdict)return bpf_load_program (BPF_PROG_TYPE_CGROUP_SKB , prog, insns_cnt,"GPL" ,0 , - bpf_log_buf,BPF_LOG_BUF_SIZE ); + bpf_log_buf,BPF_LOG_BUF_SIZE ,NULL ); }static int usage (const char *argv0)
samples/bpf/test_cgrp2_attach2.c +1 -1
@@ -45,7 +45,7 @@ static int prog_load(int verdict) ret =bpf_load_program (BPF_PROG_TYPE_CGROUP_SKB , prog, insns_cnt,"GPL" ,0 , - bpf_log_buf,BPF_LOG_BUF_SIZE ); + bpf_log_buf,BPF_LOG_BUF_SIZE ,NULL );if (ret <0 ) {log_err ("Loading program" );
samples/bpf/test_cgrp2_sock.c +1 -1
@@ -38,7 +38,7 @@ static int prog_load(int idx)size_t insns_cnt =sizeof (prog) /sizeof (struct bpf_insn );return bpf_load_program (BPF_PROG_TYPE_CGROUP_SOCK , prog, insns_cnt, -"GPL" ,0 , bpf_log_buf,BPF_LOG_BUF_SIZE ); +"GPL" ,0 , bpf_log_buf,BPF_LOG_BUF_SIZE ,NULL ); }static int usage (const char *argv0)
tools/include/uapi/linux/bpf.h +10 -0
@@ -145,6 +145,15 @@ enum bpf_attach_type {*/ #define BPF_F_NO_COMMON_LRU (1U << 1) +union bpf_prog_subtype { +struct { +__u32 version;/* cf. documentation */ +__u32 event;/* enum landlock_subtype_event */ +__aligned_u64 ability;/* LANDLOCK_SUBTYPE_ABILITY_* */ +__aligned_u64 option;/* LANDLOCK_SUBTYPE_OPTION_* */ + } landlock_rule; +} __attribute__((aligned (8 ))); +union bpf_attr {struct {/* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type;/* one of enum bpf_map_type */
@@ -173,6 +182,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; };struct {/* anonymous struct used by BPF_OBJ_* commands */
tools/lib/bpf/bpf.c +4 -1
@@ -71,10 +71,12 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size,int bpf_load_program (enum bpf_prog_type type,const struct bpf_insn *insns,size_t insns_cnt,const char *license, -__u32 kern_version,char *log_buf,size_t log_buf_sz) +__u32 kern_version,char *log_buf,size_t log_buf_sz, +union bpf_prog_subtype *subtype) {int fd;union bpf_attr attr; +union bpf_prog_subtype st_none = {};bzero (&attr,sizeof (attr)); attr.prog_type = type;
@@ -85,6 +87,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, attr.log_size =0 ; attr.log_level =0 ; attr.kern_version = kern_version; + attr.prog_subtype = subtype ? *subtype : st_none; fd =sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr));if (fd >=0 || !log_buf || !log_buf_sz)
tools/lib/bpf/bpf.h +1 -1
@@ -32,7 +32,7 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,int bpf_load_program (enum bpf_prog_type type,const struct bpf_insn *insns,size_t insns_cnt,const char *license,__u32 kern_version,char *log_buf, -size_t log_buf_sz); +size_t log_buf_sz,union bpf_prog_subtype *subtype);int bpf_map_update_elem (int fd,const void *key,const void *value,__u64 flags);
tools/lib/bpf/libbpf.c +2 -2
@@ -975,7 +975,7 @@ load_program(enum bpf_prog_type type, struct bpf_insn *insns,pr_warning ("Alloc log buffer for bpf loader error, continue without log\n" ); ret =bpf_load_program (type, insns, insns_cnt, license, - kern_version, log_buf,BPF_LOG_BUF_SIZE ); + kern_version, log_buf,BPF_LOG_BUF_SIZE ,NULL );if (ret >=0 ) { *pfd = ret;
@@ -1002,7 +1002,7 @@ load_program(enum bpf_prog_type type, struct bpf_insn *insns, fd =bpf_load_program (BPF_PROG_TYPE_KPROBE , insns, insns_cnt, license, kern_version, -NULL ,0 ); +NULL ,0 ,NULL );if (fd >=0 ) {close (fd); ret = -LIBBPF_ERRNO__PROGTYPE ;
tools/perf/tests/bpf.c +1 -1
@@ -266,7 +266,7 @@ static int check_env(void) err =bpf_load_program (BPF_PROG_TYPE_KPROBE , insns,sizeof (insns) /sizeof (insns[0 ]), - license, kver_int,NULL ,0 ); + license, kver_int,NULL ,0 ,NULL );if (err <0 ) {pr_err ("Missing basic BPF support, skip this test: %s\n" ,strerror (errno));
tools/testing/selftests/bpf/test_tag.c +1 -1
@@ -57,7 +57,7 @@ static int bpf_try_load_prog(int insns, int fd_map,bpf_filler (insns, fd_map); fd_prog =bpf_load_program (BPF_PROG_TYPE_SCHED_CLS , prog, insns,"" ,0 , -NULL ,0 ); +NULL ,0 ,NULL );assert (fd_prog >0 );if (fd_map >0 )bpf_filler (insns,0 );
tools/testing/selftests/bpf/test_verifier.c +2 -1
@@ -51,6 +51,7 @@ struct bpf_test { REJECT } result, result_unpriv;enum bpf_prog_type prog_type; +union bpf_prog_subtype prog_subtype; };/* Note we want this to be 64 bit aligned so that the end of our array is
@@ -4539,7 +4540,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv, fd_prog =bpf_load_program (prog_type ? :BPF_PROG_TYPE_SOCKET_FILTER , prog, prog_len,"GPL" ,0 , bpf_vlog, -sizeof (bpf_vlog)); +sizeof (bpf_vlog), &test->prog_subtype); expected_ret = unpriv && test->result_unpriv !=UNDEF ? test->result_unpriv : test->result;
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerPATCH v5 2/10 bpf,landlock: Define an eBPF program type for Landlock
In reply to Mickaël SalaünAdd a new type of eBPF program used by Landlock rules.
This new BPF program type will be registered with the Landlock LSM initialization.
Add an initial Landlock Kconfig.
Changes since v4:
* merge a minimal (not enabled) LSM code and Kconfig in this commit
Changes since v3:
* split commit
* revamp the landlock_context:
* add arch, syscall_nr and syscall_cmd (ioctl, fcntl…) to be able to cross-check action with the event type
* replace args array with dedicated fields to ease the addition of new fields
--- include/linux/landlock.h | 80 ++++++++++++++++++++++++++ include/uapi/linux/bpf.h | 105 ++++++++++++++++++++++++++++++++++ security/Kconfig | 1 + security/Makefile | 2 + security/landlock/Kconfig | 18 ++++++ security/landlock/Makefile | 3 + security/landlock/common.h | 25 +++++++++ security/landlock/hooks.c | 124 +++++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 105 ++++++++++++++++++++++++++++++++++ 9 files changed, 463 insertions(+) create mode 100644 include/linux/landlock.h create mode 100644 security/landlock/Kconfig create mode 100644 security/landlock/Makefile create mode 100644 security/landlock/common.h create mode 100644 security/landlock/hooks.c
include/linux/landlock.h new file +80 -0
@@ -0,0 +1,80 @@ +/* +* Landlock LSM - Public headers +* +* Copyright © 2017 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_SUBTYPE_EVENT_LAST */ +#include <linux/types.h> /* atomic_t */ + +/* +* This is not intended for the UAPI headers. Each userland software should use +* a static minimal version for the required features as explained in the +* documentation. +*/ +#define LANDLOCK_VERSION 1 + +struct landlock_rule { +atomic_t usage; +struct landlock_rule *prev; +struct bpf_prog *prog; +}; + +/** +* struct landlock_node - node in the rule hierarchy +* +* This is created when a task inserts its first rule in the Landlock rule +* hierarchy. The set of Landlock rules referenced by this node is then +* enforced for all the tasks that inherit this node. However, if a task is +* cloned before inserting any rule, it doesn't get a dedicated node and its +* children will not inherit any rules from this task. +* +* @usage: reference count to manage the node lifetime +* @rule: list of Landlock rules managed by this node +* @prev: reference the parent node +* @owner: reference the address of the node in the &struct landlock_events. +* This is needed to know if we need to append a rule to the current +* node or create a new node. +*/ +struct landlock_node { +atomic_t usage; +struct landlock_rule *rule; +struct landlock_node *prev; +struct landlock_node **owner; +}; + +/** +* struct landlock_events - Landlock event rules 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 of each entries, only +* create a pointer to &struct landlock_events and increments its usage. +* +* @usage: reference count to manage the object lifetime. When a thread need to +* add Landlock rules and if @usage is greater than 1, then the thread +* must duplicate &struct landlock_events to not change the children's +* rules as well. +* @nodes: array of non-NULL &struct landlock_node pointers +*/ +struct landlock_events { +atomic_t usage; +struct landlock_node *nodes[_LANDLOCK_SUBTYPE_EVENT_LAST]; +}; + +void put_landlock_events (struct landlock_events *events); + +#ifdef CONFIG_SECCOMP_FILTER +int landlock_seccomp_append_prog (unsigned int flags, +const char __user *user_bpf_fd); +#endif /* CONFIG_SECCOMP_FILTER */ + +#endif /* CONFIG_SECURITY_LANDLOCK */ +#endif /* _LINUX_LANDLOCK_H */
include/uapi/linux/bpf.h +105 -0
115 lines not on this page · raw message
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/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/Kconfig new file +18 -0
@@ -0,0 +1,18 @@ +config SECURITY_LANDLOCK + bool "Landlock sandbox support" + depends on SECURITY + depends on BPF_SYSCALL + depends on SECCOMP_FILTER + default y + help + Landlock is a stackable LSM which allows to load a security policy to + restrict processes (i.e. create a sandbox). The policy is a list of + stacked eBPF programs, called rules, dedicated to restrict access to + a type of kernel object (e.g. file). + + You need to enable seccomp filter to apply a security policy to a + process hierarchy (e.g. application with built-in sandboxing). + + See Documentation/security/landlock/ for further information. + + If you are unsure how to answer this question, answer Y.
security/landlock/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o + +landlock-y := hooks.o
security/landlock/common.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Landlock LSM - private headers +* +* Copyright © 2017 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 + +/** +* get_index - get an index for the rules of struct landlock_events +* +* @event: a Landlock event type +*/ +static inline int get_index (enum landlock_subtype_event event) +{ +/* event ID > 0 for loaded programs */ +return event -1 ; +} + +#endif /* _SECURITY_LANDLOCK_COMMON_H */
security/landlock/hooks.c new file +124 -0
124 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +105 -0
115 lines not on this page · raw message
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerPATCH v5 3/10 bpf: Define handle_fs and add a new helper bpf_handle_fs_get_mode()
In reply to Mickaël SalaünAdd an eBPF function bpf_handle_fs_get_mode(handle_fs) to get the mode of a an abstract object wrapping either a file, a dentry, a path, or an inode.
Changes since v4:
* use a file abstraction (handle) to wrap inode, dentry, path and file structs
* remove bpf_landlock_cmp_fs_beneath()
* rename the BPF helper and move it to kernel/bpf/
* tighten helpers accessible by a Landlock rule
Changes since v3:
* remove bpf_landlock_cmp_fs_prop() (suggested by Alexie Starovoitov)
* add hooks dealing with struct inode and struct path pointers:
inode_permission and inode_getattr
* add abstraction over eBPF helper arguments thanks to wrapping structs
* add bpf_landlock_get_fs_mode() helper to check file type and mode
* merge WARN_ON() (suggested by Kees Cook)
* fix and update bpf_helpers.h
* use BPF_CALL_* for eBPF helpers (suggested by Alexie Starovoitov)
* make handle arraymap safe (RCU) and remove buggy synchronize_rcu()
* factor out the arraymay walk
* use size_t to index array (suggested by Jann Horn)
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 | 33 +++++++++++++++++++++++++++ include/uapi/linux/bpf.h | 10 +++++++- kernel/bpf/Makefile | 2 +- kernel/bpf/helpers_fs.c | 52 ++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++ samples/bpf/bpf_helpers.h | 2 ++ security/landlock/hooks.c | 8 ++++++- tools/include/uapi/linux/bpf.h | 10 +++++++- 8 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 kernel/bpf/helpers_fs.c
include/linux/bpf.h +33 -0
@@ -15,6 +15,11 @@#include <linux/err.h> #include <linux/rbtree_latch.h> +/* FS helpers */ +#include <linux/dcache.h> /* struct dentry */ +#include <linux/fs.h> /* struct file, struct inode */ +#include <linux/path.h> /* struct path */ +struct perf_event ;struct bpf_map ;
@@ -82,6 +87,8 @@ enum bpf_arg_type {ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING ,/* any (initialized) argument is ok */ + + ARG_CONST_PTR_TO_HANDLE_FS, /* pointer to an abstract FS struct */ };/* type of values returned from helper functions */
@@ -148,6 +155,9 @@ enum bpf_reg_type {* map element. */ PTR_TO_MAP_VALUE_ADJ , + +/* FS helpers */ + CONST_PTR_TO_HANDLE_FS, };struct bpf_prog ;
@@ -220,6 +230,26 @@ struct bpf_event_entry {struct rcu_head rcu; }; +/* FS helpers */ +enum bpf_handle_fs_type { +BPF_HANDLE_FS_TYPE_NONE , +BPF_HANDLE_FS_TYPE_FILE , +BPF_HANDLE_FS_TYPE_INODE , +BPF_HANDLE_FS_TYPE_PATH , +BPF_HANDLE_FS_TYPE_DENTRY , +}; + +struct bpf_handle_fs { +enum bpf_handle_fs_type type; +union { +struct file *file; +struct inode *inode; +const struct path *path; +struct dentry *dentry; + }; +}; + +u64 bpf_tail_call (u64 ctx,u64 r2,u64 index,u64 r4,u64 r5);u64 bpf_get_stackid (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5);
@@ -358,6 +388,9 @@ 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; +/* FS helpers */ +extern const struct bpf_func_proto bpf_handle_fs_get_mode_proto; +/* 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 +9 -1
@@ -467,6 +467,13 @@ union bpf_attr {* Return: * > 0 length of the string including the trailing NUL on success * < 0 error +* +* s64 bpf_handle_fs_get_mode(handle_fs) +* Get the mode of a struct bpf_handle_fs +* fs: struct bpf_handle_fs address +* Return: +* >= 0 file mode +* < 0 error */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -514,7 +521,8 @@ union bpf_attr {FN (get_numa_node_id), \FN (skb_change_head), \FN (xdp_adjust_head), \ -FN (probe_read_str), +FN (probe_read_str), \ +FN (handle_fs_get_mode),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/Makefile +1 -1
@@ -1,6 +1,6 @@ obj-y := core.o -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o helpers_fs.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o ifeq ($(CONFIG_PERF_EVENTS),y) obj-$(CONFIG_BPF_SYSCALL) += stackmap.o
kernel/bpf/helpers_fs.c new file +52 -0
52 lines not on this page · raw message
kernel/bpf/verifier.c +6 -0
@@ -189,6 +189,7 @@ static const char * const reg_type_str[] = { [CONST_IMM] = "imm", [PTR_TO_PACKET] = "pkt", [PTR_TO_PACKET_END] = "pkt_end", + [CONST_PTR_TO_HANDLE_FS] = "handle_fs", };#define __BPF_FUNC_STR_FN (x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
@@ -546,6 +547,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 CONST_PTR_TO_HANDLE_FS :return true;default :return false;
@@ -1052,6 +1054,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno, expected_type =PTR_TO_CTX ;if (type != expected_type) goto err_type; + }else if (arg_type ==ARG_CONST_PTR_TO_HANDLE_FS ) { + expected_type =CONST_PTR_TO_HANDLE_FS ; +if (type != expected_type) + goto err_type; }else if (arg_type ==ARG_PTR_TO_MEM || arg_type ==ARG_PTR_TO_UNINIT_MEM ) { expected_type =PTR_TO_STACK ;
samples/bpf/bpf_helpers.h +2 -0
@@ -59,6 +59,8 @@ static unsigned long long (*bpf_get_prandom_u32)(void) = (void *) BPF_FUNC_get_prandom_u32;static int (*bpf_xdp_adjust_head)(void *ctx,int offset) = (void *) BPF_FUNC_xdp_adjust_head; +static long long (*bpf_handle_fs_get_mode)(void *handle_fs) = + (void *) BPF_FUNC_handle_fs_get_mode;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
security/landlock/hooks.c +7 -1
@@ -11,7 +11,7 @@#include <asm/current.h> #include <asm/processor.h> /* task_pt_regs() */ #include <asm/syscall.h> /* syscall_get_nr(), syscall_get_arch() */ -#include <linux/bpf.h> /* enum bpf_access_type, enum bpf_*, enum landlock_subtype_event, struct landlock_context, struct bpf_handle_fs */ +#include <linux/bpf.h> /* enum bpf_access_type, struct landlock_context */ #include <linux/err.h> /* EPERM */ #include <linux/filter.h> /* struct bpf_prog, BPF_PROG_RUN() */ #include <linux/kernel.h> /* ARRAY_SIZE */
@@ -79,6 +79,12 @@ static inline const struct bpf_func_proto *bpf_landlock_func_proto(case BPF_FUNC_map_lookup_elem:return &bpf_map_lookup_elem_proto; +/* event_fs */ +case BPF_FUNC_handle_fs_get_mode: +if (event_fs) +return &bpf_handle_fs_get_mode_proto; +return NULL ; +/* ability_write */ case BPF_FUNC_map_delete_elem:if (ability_write)
tools/include/uapi/linux/bpf.h +9 -1
@@ -467,6 +467,13 @@ union bpf_attr {* Return: * > 0 length of the string including the trailing NUL on success * < 0 error +* +* s64 bpf_handle_fs_get_mode(handle_fs) +* Get the mode of a struct bpf_handle_fs +* fs: struct bpf_handle_fs address +* Return: +* >= 0 file mode +* < 0 error */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -514,7 +521,8 @@ union bpf_attr {FN (get_numa_node_id), \FN (skb_change_head), \FN (xdp_adjust_head), \ -FN (probe_read_str), +FN (probe_read_str), \ +FN (handle_fs_get_mode),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Signature · 1 line
2.11.0
James Morris
maintainerRe: [PATCH v5 03/10] bpf: Define handle_fs and add a new helper bpf_handle_fs_get_mode()
Quoted 7 lines · Mickaël Salaün
Add an eBPF function
bpf_handle_fs_get_mode(handle_fs)to get the mode of a an abstract object wrapping either a file, a dentry, a path, or an inode.Changes since v4:
* use a file abstraction (handle) to wrap inode, dentry, path and file structs
Good to see these abstractions. As discussed at LPC, we need to ensure that we don't couple the Landlock API too closely with the LSM API, as the former is an ABI exposed to userland -- we don't want to lose the ability to change LSM internally due to breaking Landlock policies.
include/linux/bpf.h enum bpf_arg_type { · line 92
Quoted 7 lines
include/linux/bpf.h +2
-0@@ -82,6 +87,8 @@ enum bpf_arg_type {ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING ,/* any (initialized) argument is ok */ + + ARG_CONST_PTR_TO_HANDLE_FS, /* pointer to an abstract FS struct */ };
Extraneous whitespace?
Signature · 2 lines
James Morris <[email protected]>
Mickaël Salaün
maintainerRe: [PATCH v5 03/10] bpf: Define handle_fs and add a new helper bpf_handle_fs_get_mode()
Quoted 6 lines · James Morris
Quoted 4 lines · Mickaël Salaün
Add an eBPF function
bpf_handle_fs_get_mode(handle_fs)to get the mode of a an abstract object wrapping either a file, a dentry, a path, or an inode.
Quoted 8 lines
Quoted 3 lines
Changes since v4:
* use a file abstraction (handle) to wrap inode, dentry, path and file structsGood to see these abstractions. As discussed at LPC, we need to ensure that we don't couple the Landlock API too closely with the LSM API, as the former is an ABI exposed to userland -- we don't want to lose the ability to change LSM internally due to breaking Landlock policies.
Right, it is the case now, especially with the Landlock events.
Quoted 10 lines
include/linux/bpf.h enum bpf_arg_type { · line 92
Quoted 8 lines
include/linux/bpf.h +2
-0@@ -82,6 +87,8 @@ enum bpf_arg_type {ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING ,/* any (initialized) argument is ok */ + + ARG_CONST_PTR_TO_HANDLE_FS, /* pointer to an abstract FS struct */ };Extraneous whitespace?
It is on purpose, following the same rules as used for this enum.
Mickaël
Mickaël Salaün
maintainerPATCH v5 4/10 landlock: Add LSM hooks related to filesystem
In reply to Mickaël SalaünHandle 33 filesystem-related LSM hooks for the Landlock filesystem
event: LANDLOCK_SUBTYPE_EVENT_FS.
A Landlock event wrap LSM hooks for similar kernel object types (e.g. struct file, struct path...). Multiple LSM hooks can trigger the same Landlock event.
Landlock handle nine coarse-grained actions: read, write, execute, new, get, remove, ioctl, lock and fcntl. Each of them abstract LSM hook access control in a way that can be extended in the future.
The Landlock LSM hook registration is done after other LSM to only run actions from user-space, via eBPF programs, if the access was granted by major (privileged) LSMs.
Changes since v4:
* add LSM hook abstraction called Landlock event
* use the compiler type checking to verify hooks use by an event
* handle all filesystem related LSM hooks (e.g. file_permission,
mmap_file, sb_mount...)
* register BPF programs for Landlock just after LSM hooks registration
* move hooks registration after other LSMs
* add failsafes to check if a hook is not used by the kernel
* allow partial raw value access form the context (needed for programs generated by LLVM)
Changes since v3:
* split commit
* add hooks dealing with struct inode and struct path pointers:
inode_permission and inode_getattr
* add abstraction over eBPF helper arguments thanks to wrapping structs
--- include/linux/lsm_hooks.h | 5 + security/landlock/Makefile | 2 + security/landlock/hooks.c | 794 ++++++++++++++++++++++++++++++++++++++++++++- security/security.c | 7 +- 4 files changed, 806 insertions(+), 2 deletions(-)
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 */
security/landlock/Makefile +2 -0
@@ -1,3 +1,5 @@ +ccflags-$(CONFIG_SECURITY_LANDLOCK) += -Werror=unused-function + obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := hooks.o
security/landlock/hooks.c +793 -1
810 lines not on this page · raw message
security/security.c +6 -1
@@ -63,10 +63,15 @@ int __init security_init(void)loadpin_add_hooks ();/* -* Load all the remaining security modules. +* Load all remaining privileged security modules. */ do_security_initcalls (); +/* +* Load potentially-unprivileged security modules at the end. +*/ +landlock_add_hooks (); +return 0 ; }
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerPATCH v5 5/10 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 | 4 ++-- kernel/fork.c | 2 +- kernel/seccomp.c | 18 +++++++++++++----- 3 files changed, 16 insertions(+), 8 deletions(-)
include/linux/seccomp.h +2 -2
@@ -77,10 +77,10 @@ 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 ; }
kernel/fork.c +1 -1
@@ -352,7 +352,7 @@ void free_task(struct task_struct *tsk)#endif rt_mutex_debug_task_free (tsk);ftrace_graph_exit_task (tsk); -put_seccomp_filter (tsk); +put_seccomp (tsk);arch_release_task_struct (tsk);if (tsk->flags &PF_KTHREAD )free_kthread_struct (tsk);
kernel/seccomp.c +13 -5
@@ -62,6 +62,8 @@ struct seccomp_filter {/* 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.
@@ -312,7 +314,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);
@@ -474,10 +476,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;
@@ -486,6 +489,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
@@ -897,7 +905,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.11.0
Mickaël Salaün
maintainerPATCH v5 6/10 seccomp,landlock: Handle Landlock events per process hierarchy
In reply to Mickaël SalaünThe seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule). However, an intermediate task, which did not create a rule, will not be able to update its children's rules.
Landlock rules can be tied to a Landlock event. When such an event is triggered, a tree of rules can be evaluated. Thisk kind of tree is created with a first node. This node reference a list of rules and an optional parent node. Each rule return a 32-bit value which can interrupt the evaluation with a non-zero value. If every rules returned zero, the evaluation continues with the rule list of the parent node, until the end of the tree.
Changes since v4:
* merge manager and seccomp patches
* return -EFAULT in seccomp(2) when user_bpf_fd is null to easely check if Landlock is supported
* only allow a process with the global CAP_SYS_ADMIN to use Landlock (will be lifted in the future)
* add an early check to exit as soon as possible if the current process does not have Landlock rules
Changes since v3:
* remove the hard link with seccomp (suggested by Andy Lutomirski and Kees Cook):
* remove the cookie which could imply multiple evaluation of Landlock rules
* remove the origin field in struct landlock_data
* remove documentation fix (merged upstream)
* rename the new seccomp command to SECCOMP_ADD_LANDLOCK_RULE
* internal renaming
* split commit
* new design to be able to inherit on the fly the parent rules
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/seccomp.h | 8 ++ include/uapi/linux/seccomp.h | 1 + kernel/fork.c | 14 +- kernel/seccomp.c | 8 ++ security/landlock/Makefile | 2 +- security/landlock/hooks.c | 42 +++++- security/landlock/manager.c | 321 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 392 insertions(+), 4 deletions(-) create mode 100644 security/landlock/manager.c
include/linux/seccomp.h +8 -0
@@ -10,6 +10,10 @@#include <linux/thread_info.h> #include <asm/seccomp.h> +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct landlock_events ; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ +struct seccomp_filter ;/** * struct seccomp - the state of a seccomp'ed process
@@ -18,6 +22,7 @@ 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. +* @landlock_events: contains an array of Landlock rules. * * @filter must only be accessed from the context of current as there * is no read locking.
@@ -25,6 +30,9 @@ struct seccomp_filter;struct seccomp {int mode;struct seccomp_filter *filter; +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct landlock_events *landlock_events; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ };#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
include/uapi/linux/seccomp.h +1 -0
@@ -13,6 +13,7 @@/* Valid operations for seccomp syscall. */ #define SECCOMP_SET_MODE_STRICT 0#define SECCOMP_SET_MODE_FILTER 1 +#define SECCOMP_ADD_LANDLOCK_RULE 2/* Valid flags for SECCOMP_SET_MODE_FILTER */ #define SECCOMP_FILTER_FLAG_TSYNC 1
kernel/fork.c +12 -2
@@ -37,6 +37,7 @@#include <linux/security.h> #include <linux/hugetlb.h> #include <linux/seccomp.h> +#include <linux/landlock.h> #include <linux/swap.h> #include <linux/syscalls.h> #include <linux/jiffies.h>
@@ -515,7 +516,10 @@ 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.landlock_events =NULL ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#endif /* CONFIG_SECCOMP */ setup_thread_stack (tsk, orig);clear_user_return_notifier (tsk);
@@ -1388,7 +1392,13 @@ 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 ) + p->seccomp.landlock_events = current->seccomp.landlock_events; +if (p->seccomp.landlock_events) +atomic_inc (&p->seccomp.landlock_events->usage); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ /* * Explicitly enable no_new_privs here in case it got set
kernel/seccomp.c +8 -0
@@ -32,6 +32,7 @@#include <linux/security.h> #include <linux/tracehook.h> #include <linux/uaccess.h> +#include <linux/landlock.h> /** * struct seccomp_filter - container for seccomp BPF programs
@@ -492,6 +493,9 @@ static void put_seccomp_filter(struct seccomp_filter *filter)void put_seccomp (struct task_struct *tsk) {put_seccomp_filter (tsk->seccomp.filter); +#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_events (tsk->seccomp.landlock_events); +#endif /* CONFIG_SECURITY_LANDLOCK */ }/**
@@ -796,6 +800,10 @@ static long do_seccomp(unsigned int op, unsigned int flags,return seccomp_set_mode_strict ();case SECCOMP_SET_MODE_FILTER :return seccomp_set_mode_filter (flags, uargs); +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +case SECCOMP_ADD_LANDLOCK_RULE : +return landlock_seccomp_append_prog (flags, uargs); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ default :return -EINVAL ; }
security/landlock/Makefile +1 -1
@@ -2,4 +2,4 @@ ccflags-$(CONFIG_SECURITY_LANDLOCK) += -Werror=unused-function obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := hooks.o +landlock-y := hooks.o manager.o
security/landlock/hooks.c +41 -1
55 lines not on this page · raw message
security/landlock/manager.c new file +321 -0
321 lines not on this page · raw message
Signature · 1 line
2.11.0
Andy Lutomirski
reviewerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 5 lines · Mickaël Salaün
The seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule)
Can you clarify exaclty what this type of update does? Is it something that should be supported by normal seccomp rules as well?
Quoted 2 lines
+/**
+ *landlock_run_prog- run Landlock program for a syscall
Unless this is actually specific to syscalls, s/for a syscall//, perhaps?
security/landlock/manager.c line 204
Quoted 4 lines
security/landlock/manager.c new file +4
-0+if (new_events->nodes[event_idx]->owner == + &new_events->nodes[event_idx]) { +/* We are the owner, we can then update the node. */ +add_landlock_rule (new_events, rule);
This is the part I don't get. Adding a rule if you're the owner (BTW, why is ownership visible to userspace at all?) for just yourself and future children is very different from adding it so it applies to preexisting children too.
Quoted 100 lines
security/landlock/manager.c new file +100
-0+ }else if (atomic_read (¤t_events->usage) ==1 ) { +WARN_ON (new_events->nodes[event_idx]->owner); +/* +* We can become the new owner if no other task use it. +* This avoid an unnecessary allocation. +*/ + new_events->nodes[event_idx]->owner = + &new_events->nodes[event_idx]; +add_landlock_rule (new_events, rule); + }else { +/* +* We are not the owner, we need to fork current_events +* and then add a new node. +*/ +struct landlock_node *node; +size_t i; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) { + new_events =ERR_PTR (-ENOMEM ); + goto put_rule; + } +atomic_set (&node->usage,1 ); +/* set the previous node after the new_events +* allocation */ + node->prev =NULL ; +/* do not increment the previous node usage */ + node->owner = &new_events->nodes[event_idx]; +/* rule->prev is already NULL */ +atomic_set (&rule->usage,1 ); + node->rule = rule; + + new_events =new_raw_landlock_events (); +if (IS_ERR (new_events)) { +/* put the rule as well */ +put_landlock_node (node); +return ERR_PTR (-ENOMEM ); + } +for (i =0 ; i <ARRAY_SIZE (new_events->nodes); i++) { + new_events->nodes[i] = +lockless_dereference ( + current_events->nodes[i]); +if (i == event_idx) + node->prev = new_events->nodes[i]; +if (!WARN_ON (!new_events->nodes[i])) +atomic_inc (&new_events->nodes[i]->usage); + } + new_events->nodes[event_idx] = node; + +/* +* @current_events will not be freed here because it's usage +* field is > 1. It is only prevented to be freed by another +* subject thanks to the caller of landlock_append_prog() which +* should be locked if needed. +*/ +put_landlock_events (current_events); + } + } +return new_events; + +put_prog: +bpf_prog_put (prog); +return new_events; + +put_rule: +put_landlock_rule (rule); +return new_events; +} + +/** +* landlock_seccomp_append_prog - attach a Landlock rule to the current process +* +* current->seccomp.landlock_events is lazily allocated. When a process fork, +* only a pointer is copied. When a new event is added by a process, if there +* is other references to this process' landlock_events, then a new allocation +* is made to contains an array pointing to Landlock rule lists. This design +* has low-performance impact and is memory efficient while keeping the +* property of append-only rules. +* +* @flags: not used for now, but could be used for TSYNC +* @user_bpf_fd: file descriptor pointing to a loaded Landlock rule +*/ +#ifdef CONFIG_SECCOMP_FILTER +int landlock_seccomp_append_prog (unsigned int flags,const char __user *user_bpf_fd) +{ +struct landlock_events *new_events; +struct bpf_prog *prog; +int bpf_fd; + +/* force no_new_privs to limit privilege escalation */ +if (!task_no_new_privs (current)) +return -EPERM ; +/* will be removed in the future to allow unprivileged tasks */ +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; +if (!user_bpf_fd) +return -EFAULT ; +if (flags) +return -EINVAL ; +if (copy_from_user (&bpf_fd, user_bpf_fd,sizeof (bpf_fd)))
security/landlock/manager.c line 314
Quoted 10 lines
security/landlock/manager.c new file +10
-0+return -EFAULT ; + prog =bpf_prog_get (bpf_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); + +/* +* We don't need to lock anything for the current process hierarchy, +* everything is guarded by the atomic counters. +*/ + new_events =landlock_append_prog (current->seccomp.landlock_events, prog);
Do you need to check that it's the right *kind* of bpf prog or is that handled elsewhere?
--Andy
Mickaël Salaün
maintainerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines · Mickaël Salaün
The seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule)
Can you clarify exaclty what this type of update does? Is it something that should be supported by normal seccomp rules as well?
There is two main structures involved here: struct landlock_node and struct landlock_rule, both defined in include/linux/landlock.h [02/10].
Let's take an example with seccomp filter and then Landlock:
* seccomp filter: Process P1 creates and applies a seccomp filter F1 to itself. Then it forks and creates a child P2, which inherits P1's filters, hence F1. Now, if P1 add a new seccomp filter F2 to itself, P2 *won't get it*. The P2's filter list will still only contains F1 but not F2. If P2 sets up and applies a new filter F3 to itself, its filter list will contains F1 and F3.
* Landlock: Process P1 creates and applies a Landlock rule R1 to itself. Underneath the kernel creates a new node N1 dedicated to P1, which contains all its rules. Then P1 forks and creates a child P2, which inherits P1's rules, hence R1. Underneath P2 inherited N1. Now, if P1 add a new Landlock rule R2 to itself, P2 *will get it* as well (because R2 is part of N1). If P2 creates and applies a new rule R3 to itself, its rules will contains R1, R2 and R3. Underneath the kernel created a new node N2 for P2, which only contains R3 but inherits/links to N1.
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Quoted 5 lines
Quoted 3 lines
+/**
+ *landlock_run_prog- run Landlock program for a syscallUnless this is actually specific to syscalls, s/for a syscall//, perhaps?
Right, not specific to syscall anymore.
Quoted 10 lines
security/landlock/manager.c line 204
Quoted 5 lines
security/landlock/manager.c new file +4
-0+if (new_events->nodes[event_idx]->owner == + &new_events->nodes[event_idx]) { +/* We are the owner, we can then update the node. */ +add_landlock_rule (new_events, rule);This is the part I don't get. Adding a rule if you're the owner (BTW, why is ownership visible to userspace at all?) for just yourself and future children is very different from adding it so it applies to preexisting children too.
Node ownership is not (directly) visible to userspace.
The current inheritance mechanism doesn't enable to only add a rule to the current process. The rule will be inherited by its children (starting from the children created after the first applied rule). An option flag NEW_RULE_HIERARCHY (or maybe another seccomp operation) could enable to create a new node for the current process, and then makes it not inherited by the previous children.
Quoted 107 lines
Quoted 107 lines
security/landlock/manager.c new file +105
-0+ }else if (atomic_read (¤t_events->usage) ==1 ) { +WARN_ON (new_events->nodes[event_idx]->owner); +/* +* We can become the new owner if no other task use it. +* This avoid an unnecessary allocation. +*/ + new_events->nodes[event_idx]->owner = + &new_events->nodes[event_idx]; +add_landlock_rule (new_events, rule); + }else { +/* +* We are not the owner, we need to fork current_events +* and then add a new node. +*/ +struct landlock_node *node; +size_t i; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) { + new_events =ERR_PTR (-ENOMEM ); + goto put_rule; + } +atomic_set (&node->usage,1 ); +/* set the previous node after the new_events +* allocation */ + node->prev =NULL ; +/* do not increment the previous node usage */ + node->owner = &new_events->nodes[event_idx]; +/* rule->prev is already NULL */ +atomic_set (&rule->usage,1 ); + node->rule = rule; + + new_events =new_raw_landlock_events (); +if (IS_ERR (new_events)) { +/* put the rule as well */ +put_landlock_node (node); +return ERR_PTR (-ENOMEM ); + } +for (i =0 ; i <ARRAY_SIZE (new_events->nodes); i++) { + new_events->nodes[i] = +lockless_dereference ( + current_events->nodes[i]); +if (i == event_idx) + node->prev = new_events->nodes[i]; +if (!WARN_ON (!new_events->nodes[i])) +atomic_inc (&new_events->nodes[i]->usage); + } + new_events->nodes[event_idx] = node; + +/* +* @current_events will not be freed here because it's usage +* field is > 1. It is only prevented to be freed by another +* subject thanks to the caller of landlock_append_prog() which +* should be locked if needed. +*/ +put_landlock_events (current_events); + } + } +return new_events; + +put_prog: +bpf_prog_put (prog); +return new_events; + +put_rule: +put_landlock_rule (rule); +return new_events; +} + +/** +* landlock_seccomp_append_prog - attach a Landlock rule to the current process +* +* current->seccomp.landlock_events is lazily allocated. When a process fork, +* only a pointer is copied. When a new event is added by a process, if there +* is other references to this process' landlock_events, then a new allocation +* is made to contains an array pointing to Landlock rule lists. This design +* has low-performance impact and is memory efficient while keeping the +* property of append-only rules. +* +* @flags: not used for now, but could be used for TSYNC +* @user_bpf_fd: file descriptor pointing to a loaded Landlock rule +*/ +#ifdef CONFIG_SECCOMP_FILTER +int landlock_seccomp_append_prog (unsigned int flags,const char __user *user_bpf_fd) +{ +struct landlock_events *new_events; +struct bpf_prog *prog; +int bpf_fd; + +/* force no_new_privs to limit privilege escalation */ +if (!task_no_new_privs (current)) +return -EPERM ; +/* will be removed in the future to allow unprivileged tasks */ +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; +if (!user_bpf_fd) +return -EFAULT ; +if (flags) +return -EINVAL ; +if (copy_from_user (&bpf_fd, user_bpf_fd,sizeof (bpf_fd))) +return -EFAULT ; + prog =bpf_prog_get (bpf_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); +
Quoted 8 lines
security/landlock/manager.c line 314
Quoted 5 lines
security/landlock/manager.c new file +5
-0+/* +* We don't need to lock anything for the current process hierarchy, +* everything is guarded by the atomic counters. +*/ + new_events =landlock_append_prog (current->seccomp.landlock_events, prog);Do you need to check that it's the right *kind* of bpf prog or is that handled elsewhere?
The program type is checked at the beginning of landlock_append_prog().
Mickaël
Andy Lutomirski
reviewerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 31 lines · Mickaël Salaün
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines · Mickaël Salaün
The seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule)
Can you clarify exaclty what this type of update does? Is it something that should be supported by normal seccomp rules as well?
There is two main structures involved here: struct
landlock_nodeand structlandlock_rule, both defined ininclude/linux/landlock.h[02/10].Let's take an example with seccomp filter and then Landlock:
* seccomp filter: Process P1 creates and applies a seccomp filter F1 to itself. Then it forks and creates a child P2, which inherits P1's filters, hence F1. Now, if P1 add a new seccomp filter F2 to itself, P2 *won't get it*. The P2's filter list will still only contains F1 but not F2. If P2 sets up and applies a new filter F3 to itself, its filter list will contains F1 and F3.
* Landlock: Process P1 creates and applies a Landlock rule R1 to itself. Underneath the kernel creates a new node N1 dedicated to P1, which contains all its rules. Then P1 forks and creates a child P2, which inherits P1's rules, hence R1. Underneath P2 inherited N1. Now, if P1 add a new Landlock rule R2 to itself, P2 *will get it* as well (because R2 is part of N1). If P2 creates and applies a new rule R3 to itself, its rules will contains R1, R2 and R3. Underneath the kernel created a new node N2 for P2, which only contains R3 but inherits/links to N1.
Quoted 6 lines
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Then let's do it right.
Currently each task has an array of seccomp filter layers. When a task forks, the child inherits the layers. All the layers are presently immutable. With Landlock, a layer can logically be a syscall fitler layer or a Landlock layer. This fits in to the existing model just fine.
If we want to have an interface to allow modification of an existing layer, let's make it so that, when a layer is added, you have to specify a flag to make the layer modifiable (by current, presumably, although I can imagine other policies down the road). Then have a separate API that modifies a layer.
IOW, I think your patch is bad for three reasons, all fixable:
1. The default is wrong. A layer should be immutable to avoid an easy attack in which you try to sandbox *yourself* and then you just modify the layer to weaken it.
2. The API that adds a layer should be different from the API that modifies a layer.
3. The whole modification mechanism should be a separate patch to be reviewed on its own merits.
Quoted 6 lines
The current inheritance mechanism doesn't enable to only add a rule to the current process. The rule will be inherited by its children (starting from the children created after the first applied rule). An option flag
NEW_RULE_HIERARCHY(or maybe another seccomp operation) could enable to create a new node for the current process, and then makes it not inherited by the previous children.
I like my proposal above much better. "Add a layer" and "change a layer" should be different operations.
--Andy
Mickaël Salaün
maintainerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 53 lines · Andy Lutomirski
Quoted 31 lines · Mickaël Salaün
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines · Mickaël Salaün
The seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule)
Can you clarify exaclty what this type of update does? Is it something that should be supported by normal seccomp rules as well?
There is two main structures involved here: struct
landlock_nodeand structlandlock_rule, both defined ininclude/linux/landlock.h[02/10].Let's take an example with seccomp filter and then Landlock:
* seccomp filter: Process P1 creates and applies a seccomp filter F1 to itself. Then it forks and creates a child P2, which inherits P1's filters, hence F1. Now, if P1 add a new seccomp filter F2 to itself, P2 *won't get it*. The P2's filter list will still only contains F1 but not F2. If P2 sets up and applies a new filter F3 to itself, its filter list will contains F1 and F3.
* Landlock: Process P1 creates and applies a Landlock rule R1 to itself. Underneath the kernel creates a new node N1 dedicated to P1, which contains all its rules. Then P1 forks and creates a child P2, which inherits P1's rules, hence R1. Underneath P2 inherited N1. Now, if P1 add a new Landlock rule R2 to itself, P2 *will get it* as well (because R2 is part of N1). If P2 creates and applies a new rule R3 to itself, its rules will contains R1, R2 and R3. Underneath the kernel created a new node N2 for P2, which only contains R3 but inherits/links to N1.Quoted 6 lines
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Then let's do it right.
Currently each task has an array of seccomp filter layers. When a task forks, the child inherits the layers. All the layers are presently immutable. With Landlock, a layer can logically be a syscall fitler layer or a Landlock layer. This fits in to the existing model just fine.
If we want to have an interface to allow modification of an existing layer, let's make it so that, when a layer is added, you have to specify a flag to make the layer modifiable (by current, presumably, although I can imagine other policies down the road). Then have a separate API that modifies a layer.
Quoted 5 lines
IOW, I think your patch is bad for three reasons, all fixable:
1. The default is wrong. A layer should be immutable to avoid an easy attack in which you try to sandbox *yourself* and then you just modify the layer to weaken it.
This is not possible, there is only an operation for now:SECCOMP_ADD_LANDLOCK_RULE. You can only add more rules to the list (as for seccomp filter). There is no way to weaken a sandbox. The question
is: how do we want to handle the rules *tree* (from the kernel point of view)?
Quoted 3 lines
2. The API that adds a layer should be different from the API that modifies a layer.
Right, but it doesn't apply now because we can only add rules.
Quoted 3 lines
3. The whole modification mechanism should be a separate patch to be reviewed on its own merits.
For a rule *replacement*, sure!
Quoted 10 lines
Quoted 7 lines
The current inheritance mechanism doesn't enable to only add a rule to the current process. The rule will be inherited by its children (starting from the children created after the first applied rule). An option flag
NEW_RULE_HIERARCHY(or maybe another seccomp operation) could enable to create a new node for the current process, and then makes it not inherited by the previous children.I like my proposal above much better. "Add a layer" and "change a layer" should be different operations.
I agree, but for now it's about how to handle immutable (but growing) inherited rules.
Andy Lutomirski
reviewerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 34 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 9 lines · Mickaël Salaün
Quoted 1 line · Andy Lutomirski
On Tue, Feb 21, 2017 at 5:26 PM, Mickaël Salaün <[email protected]> wrote:
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Then let's do it right.
Currently each task has an array of seccomp filter layers. When a task forks, the child inherits the layers. All the layers are presently immutable. With Landlock, a layer can logically be a syscall fitler layer or a Landlock layer. This fits in to the existing model just fine.
If we want to have an interface to allow modification of an existing layer, let's make it so that, when a layer is added, you have to specify a flag to make the layer modifiable (by current, presumably, although I can imagine other policies down the road). Then have a separate API that modifies a layer.
Quoted 5 lines
IOW, I think your patch is bad for three reasons, all fixable:
1. The default is wrong. A layer should be immutable to avoid an easy attack in which you try to sandbox *yourself* and then you just modify the layer to weaken it.
Quoted 6 lines
This is not possible, there is only an operation for now:
SECCOMP_ADD_LANDLOCK_RULE. You can only add more rules to the list (as for seccomp filter). There is no way to weaken a sandbox. The question
is: how do we want to handle the rules *tree* (from the kernel point of view)?
Fair enough. But I still think that immutability (like regular seccomp) should be the default. For security, simplicity is important. I guess there could be two ways to relax immutability:
allowing making the layer stricter and allowing any change at all.
As a default, though, programs should be able to expect that:
seccomp(SECCOMP_ADD_WHATEVER, ...);fork();
[parent gets compromised]
[in parent]seccomp(anything whatsoever);
will not affect the child, If the parent wants to relax that, that's fine, but I think it should be explicit.
Quoted 5 lines
Quoted 3 lines
2. The API that adds a layer should be different from the API that modifies a layer.
Right, but it doesn't apply now because we can only add rules.
That's not what the code appears to do, though. Sometimes it makes a new layer without modifying tasks that share the layer and sometimes it modifies the layer.
Both operations are probably okay, but they're not the same operation and they shouldn't pretend to be.
Quoted 6 lines
Quoted 4 lines
3. The whole modification mechanism should be a separate patch to be reviewed on its own merits.
For a rule *replacement*, sure!
And for modification of policy for non-current tasks. That's a big departure from normal seccomp and should be reviewed as such.
Mickaël Salaün
maintainerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 49 lines · Andy Lutomirski
Quoted 34 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 9 lines · Mickaël Salaün
Quoted 1 line · Andy Lutomirski
On Tue, Feb 21, 2017 at 5:26 PM, Mickaël Salaün <[email protected]> wrote:
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Then let's do it right.
Currently each task has an array of seccomp filter layers. When a task forks, the child inherits the layers. All the layers are presently immutable. With Landlock, a layer can logically be a syscall fitler layer or a Landlock layer. This fits in to the existing model just fine.
If we want to have an interface to allow modification of an existing layer, let's make it so that, when a layer is added, you have to specify a flag to make the layer modifiable (by current, presumably, although I can imagine other policies down the road). Then have a separate API that modifies a layer.
Quoted 5 lines
IOW, I think your patch is bad for three reasons, all fixable:
1. The default is wrong. A layer should be immutable to avoid an easy attack in which you try to sandbox *yourself* and then you just modify the layer to weaken it.
Quoted 6 lines
This is not possible, there is only an operation for now:
SECCOMP_ADD_LANDLOCK_RULE. You can only add more rules to the list (as for seccomp filter). There is no way to weaken a sandbox. The question
is: how do we want to handle the rules *tree* (from the kernel point of view)?Fair enough. But I still think that immutability (like regular seccomp) should be the default. For security, simplicity is important. I guess there could be two ways to relax immutability:
allowing making the layer stricter and allowing any change at all.As a default, though, programs should be able to expect that:
Quoted 8 lines
seccomp(SECCOMP_ADD_WHATEVER, ...);fork();[parent gets compromised]
[in parent]seccomp(anything whatsoever);will not affect the child, If the parent wants to relax that, that's fine, but I think it should be explicit.
Good point. However the term "immutability" doesn't fit right because the process is still allowed to add more rules to itself (as for seccomp). The difference lays in the way a rule may be "appended" (by the current process) or "inserted" (by a parent process).
I think three or four kind of operations (through the seccomp syscall) make sense:
* append a rule (for the current process and its future children)
* add a node (insert point), from which the inserted rules will be tied
* insert a rule in the node, which will be inherited by futures children
* (maybe a "lock" command to make a layer immutable for the current process and its children)
Doing so, a process is only allowed to insert a rule if a node was previously added. To forbid itself to insert new rules to one of its children, a process just need to not add a node before forking. Like this, there is no need for special rule flags nor default behavior, everything is explicit.
For this series, I will stick to the same behavior as seccomp filter:
only append rules to the current process (and its future children).
Quoted 11 lines
Quoted 4 lines
Quoted 2 lines
2. The API that adds a layer should be different from the API that modifies a layer.
Right, but it doesn't apply now because we can only add rules.
That's not what the code appears to do, though. Sometimes it makes a new layer without modifying tasks that share the layer and sometimes it modifies the layer.
Both operations are probably okay, but they're not the same operation and they shouldn't pretend to be.
It should be OK with my previous proposal. The other details could be discussed in a separate future patch series.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
3. The whole modification mechanism should be a separate patch to be reviewed on its own merits.
For a rule *replacement*, sure!
And for modification of policy for non-current tasks. That's a big departure from normal seccomp and should be reviewed as such.
Agreed
Re: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 60 lines · Mickaël Salaün
Quoted 49 lines · Andy Lutomirski
Quoted 34 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 9 lines · Mickaël Salaün
Quoted 1 line · Andy Lutomirski
On Tue, Feb 21, 2017 at 5:26 PM, Mickaël Salaün <[email protected]> wrote:
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Then let's do it right.
Currently each task has an array of seccomp filter layers. When a task forks, the child inherits the layers. All the layers are presently immutable. With Landlock, a layer can logically be a syscall fitler layer or a Landlock layer. This fits in to the existing model just fine.
If we want to have an interface to allow modification of an existing layer, let's make it so that, when a layer is added, you have to specify a flag to make the layer modifiable (by current, presumably, although I can imagine other policies down the road). Then have a separate API that modifies a layer.
Quoted 5 lines
IOW, I think your patch is bad for three reasons, all fixable:
1. The default is wrong. A layer should be immutable to avoid an easy attack in which you try to sandbox *yourself* and then you just modify the layer to weaken it.
Quoted 6 lines
This is not possible, there is only an operation for now:
SECCOMP_ADD_LANDLOCK_RULE. You can only add more rules to the list (as for seccomp filter). There is no way to weaken a sandbox. The question
is: how do we want to handle the rules *tree* (from the kernel point of view)?Fair enough. But I still think that immutability (like regular seccomp) should be the default. For security, simplicity is important. I guess there could be two ways to relax immutability:
allowing making the layer stricter and allowing any change at all.As a default, though, programs should be able to expect that:
Quoted 8 lines
seccomp(SECCOMP_ADD_WHATEVER, ...);fork();[parent gets compromised]
[in parent]seccomp(anything whatsoever);will not affect the child, If the parent wants to relax that, that's fine, but I think it should be explicit.
Quoted 8 lines
Good point. However the term "immutability" doesn't fit right because the process is still allowed to add more rules to itself (as for seccomp). The difference lays in the way a rule may be "appended" (by the current process) or "inserted" (by a parent process).
I think three or four kind of operations (through the seccomp syscall) make sense:
* append a rule (for the current process and its future children)
Sure, but this operation should *never* affect existing children, existing seccomp layers, existing nodes, etc. It should affect current and future children only. Or it could simply not exist for Landlock and instead you'd have to add a layer (see below) and then program that layer.
Quoted 2 lines
* add a node (insert point), from which the inserted rules will be tied
* insert a rule in the node, which will be inherited by futures children
I would advocate calling this a "seccomp layer" and making creation and manipulation of them generic.
Quoted 2 lines
* (maybe a "lock" command to make a layer immutable for the current process and its children)
Hmm, maybe.
Quoted 6 lines
Doing so, a process is only allowed to insert a rule if a node was previously added. To forbid itself to insert new rules to one of its children, a process just need to not add a node before forking. Like this, there is no need for special rule flags nor default behavior, everything is explicit.
This is still slightly too complicated. If you really want an operation that adds a layer (please don't call it a node in the ABI) and adds a rule to that layer in a single operation, it should be a separate operation. Please make everything explicit.
(I don't like exposing the word "node" to userspace because it means nothing. Having more than one layer of filter makes sense to me, which is why I like "layer". I'm sure that other good choices exist.)
Quoted 14 lines
For this series, I will stick to the same behavior as seccomp filter:
only append rules to the current process (and its future children).Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
2. The API that adds a layer should be different from the API that modifies a layer.
Right, but it doesn't apply now because we can only add rules.
That's not what the code appears to do, though. Sometimes it makes a new layer without modifying tasks that share the layer and sometimes it modifies the layer.
Quoted 6 lines
Quoted 2 lines
Both operations are probably okay, but they're not the same operation and they shouldn't pretend to be.
It should be OK with my previous proposal. The other details could be discussed in a separate future patch series.
NAK, or at least NAK pending better docs and justification. The operations of "add a layer and put a rule in it" and "add a rule to an existing layer" are logically different and should not be the same SECCOMP operation. "Do what I mean" is a nice paradigm for a language like Perl, but for security (and for kernel interfaces in general), "do what I say and error out if I said nonsense" is much safer.
Mickaël Salaün
maintainerRe: [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 94 lines · Andy Lutomirski
Quoted 60 lines · Mickaël Salaün
Quoted 49 lines · Andy Lutomirski
Quoted 34 lines · Mickaël Salaün
Quoted 25 lines · Andy Lutomirski
Quoted 9 lines · Mickaël Salaün
Quoted 1 line · Andy Lutomirski
On Tue, Feb 21, 2017 at 5:26 PM, Mickaël Salaün <[email protected]> wrote:
This design makes it possible for a process to add more constraints to its children on the fly. I think it is a good feature to have and a safer default inheritance mechanism, but it could be guarded by an option flag if we want both mechanism to be available. The same design could be used by seccomp filter too.
Then let's do it right.
Currently each task has an array of seccomp filter layers. When a task forks, the child inherits the layers. All the layers are presently immutable. With Landlock, a layer can logically be a syscall fitler layer or a Landlock layer. This fits in to the existing model just fine.
If we want to have an interface to allow modification of an existing layer, let's make it so that, when a layer is added, you have to specify a flag to make the layer modifiable (by current, presumably, although I can imagine other policies down the road). Then have a separate API that modifies a layer.
Quoted 5 lines
IOW, I think your patch is bad for three reasons, all fixable:
1. The default is wrong. A layer should be immutable to avoid an easy attack in which you try to sandbox *yourself* and then you just modify the layer to weaken it.
Quoted 6 lines
This is not possible, there is only an operation for now:
SECCOMP_ADD_LANDLOCK_RULE. You can only add more rules to the list (as for seccomp filter). There is no way to weaken a sandbox. The question
is: how do we want to handle the rules *tree* (from the kernel point of view)?Fair enough. But I still think that immutability (like regular seccomp) should be the default. For security, simplicity is important. I guess there could be two ways to relax immutability:
allowing making the layer stricter and allowing any change at all.As a default, though, programs should be able to expect that:
Quoted 8 lines
seccomp(SECCOMP_ADD_WHATEVER, ...);fork();[parent gets compromised]
[in parent]seccomp(anything whatsoever);will not affect the child, If the parent wants to relax that, that's fine, but I think it should be explicit.
Quoted 8 lines
Good point. However the term "immutability" doesn't fit right because the process is still allowed to add more rules to itself (as for seccomp). The difference lays in the way a rule may be "appended" (by the current process) or "inserted" (by a parent process).
I think three or four kind of operations (through the seccomp syscall) make sense:
* append a rule (for the current process and its future children)Sure, but this operation should *never* affect existing children, existing seccomp layers, existing nodes, etc. It should affect current and future children only. Or it could simply not exist for Landlock and instead you'd have to add a layer (see below) and then program that layer.
Quoted 2 lines
* add a node (insert point), from which the inserted rules will be tied
* insert a rule in the node, which will be inherited by futures childrenI would advocate calling this a "seccomp layer" and making creation and manipulation of them generic.
Quoted 2 lines
* (maybe a "lock" command to make a layer immutable for the current process and its children)
Hmm, maybe.
Quoted 6 lines
Doing so, a process is only allowed to insert a rule if a node was previously added. To forbid itself to insert new rules to one of its children, a process just need to not add a node before forking. Like this, there is no need for special rule flags nor default behavior, everything is explicit.
Quoted 8 lines
This is still slightly too complicated. If you really want an operation that adds a layer (please don't call it a node in the ABI) and adds a rule to that layer in a single operation, it should be a separate operation. Please make everything explicit.
(I don't like exposing the word "node" to userspace because it means nothing. Having more than one layer of filter makes sense to me, which is why I like "layer". I'm sure that other good choices exist.)
I keep that for a future discussion, I'm now convinced the simple inheritance (seccomp-like) doesn't block future extension.
Quoted 18 lines
Quoted 18 lines
For this series, I will stick to the same behavior as seccomp filter:
only append rules to the current process (and its future children).Quoted 11 lines
Quoted 4 lines
Quoted 2 lines
2. The API that adds a layer should be different from the API that modifies a layer.
Right, but it doesn't apply now because we can only add rules.
That's not what the code appears to do, though. Sometimes it makes a new layer without modifying tasks that share the layer and sometimes it modifies the layer.
Both operations are probably okay, but they're not the same operation and they shouldn't pretend to be.
Quoted 8 lines
Quoted 3 lines
It should be OK with my previous proposal. The other details could be discussed in a separate future patch series.
NAK, or at least NAK pending better docs and justification. The operations of "add a layer and put a rule in it" and "add a rule to an existing layer" are logically different and should not be the same SECCOMP operation.
We are agree.
Quoted 4 lines
"Do what I mean" is a nice paradigm for a language
like Perl, but for security (and for kernel interfaces in general), "do what I say and error out if I said nonsense" is much safer.
Totally agree.
Re: [kernel-hardening] [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
In reply to Mickaël SalaünQuoted 463 lines · Mickaël Salaün
The seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule). However, an intermediate task, which did not create a rule, will not be able to update its children's rules.
Landlock rules can be tied to a Landlock event. When such an event is triggered, a tree of rules can be evaluated. Thisk kind of tree is created with a first node. This node reference a list of rules and an optional parent node. Each rule return a 32-bit value which can interrupt the evaluation with a non-zero value. If every rules returned zero, the evaluation continues with the rule list of the parent node, until the end of the tree.
Changes since v4:
* merge manager and seccomp patches
* return-EFAULTin seccomp(2) whenuser_bpf_fdis null to easely check if Landlock is supported
* only allow a process with the globalCAP_SYS_ADMINto use Landlock (will be lifted in the future)
* add an early check to exit as soon as possible if the current process does not have Landlock rulesChanges since v3:
* remove the hard link with seccomp (suggested by Andy Lutomirski and Kees Cook):
* remove the cookie which could imply multiple evaluation of Landlock rules
* remove the origin field in structlandlock_data
* remove documentation fix (merged upstream)
* rename the new seccomp command toSECCOMP_ADD_LANDLOCK_RULE
* internal renaming
* split commit
* new design to be able to inherit on the fly the parent rulesChanges 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 intosecurity/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 ifdefSigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrew Morton <[email protected]>Cc Andy Lutomirski <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Cc Will Drewry <[email protected]>--- include/linux/seccomp.h | 8 ++ include/uapi/linux/seccomp.h | 1 + kernel/fork.c | 14 +- kernel/seccomp.c | 8 ++ security/landlock/Makefile | 2 +- security/landlock/hooks.c | 42 +++++- security/landlock/manager.c | 321 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 392 insertions(+), 4 deletions(-) create mode 100644 security/landlock/manager.cinclude/linux/seccomp.h +8
-0@@ -10,6 +10,10 @@#include <linux/thread_info.h> #include <asm/seccomp.h> +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct landlock_events ; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ +struct seccomp_filter ;/** * struct seccomp - the state of a seccomp'ed process @@ -18,6 +22,7 @@ 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. +* @landlock_events: contains an array of Landlock rules. * * @filter must only be accessed from the context of current as there * is no read locking. @@ -25,6 +30,9 @@ struct seccomp_filter;struct seccomp {int mode;struct seccomp_filter *filter; +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct landlock_events *landlock_events; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ };#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER include/uapi/linux/seccomp.h +1
-0@@ -13,6 +13,7 @@/* Valid operations for seccomp syscall. */ #define SECCOMP_SET_MODE_STRICT 0#define SECCOMP_SET_MODE_FILTER 1 +#define SECCOMP_ADD_LANDLOCK_RULE 2/* Valid flags for SECCOMP_SET_MODE_FILTER */ #define SECCOMP_FILTER_FLAG_TSYNC 1kernel/fork.c +12
-2@@ -37,6 +37,7 @@#include <linux/security.h> #include <linux/hugetlb.h> #include <linux/seccomp.h> +#include <linux/landlock.h> #include <linux/swap.h> #include <linux/syscalls.h> #include <linux/jiffies.h> @@ -515,7 +516,10 @@ 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.landlock_events =NULL ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#endif /* CONFIG_SECCOMP */ setup_thread_stack (tsk, orig);clear_user_return_notifier (tsk);@@ -1388,7 +1392,13 @@ 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 ) + p->seccomp.landlock_events = current->seccomp.landlock_events; +if (p->seccomp.landlock_events) +atomic_inc (&p->seccomp.landlock_events->usage); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ /* * Explicitly enable no_new_privs here in case it got set kernel/seccomp.c +8
-0@@ -32,6 +32,7 @@#include <linux/security.h> #include <linux/tracehook.h> #include <linux/uaccess.h> +#include <linux/landlock.h> /** * struct seccomp_filter - container for seccomp BPF programs @@ -492,6 +493,9 @@ static void put_seccomp_filter(struct seccomp_filter *filter)void put_seccomp (struct task_struct *tsk) {put_seccomp_filter (tsk->seccomp.filter); +#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_events (tsk->seccomp.landlock_events); +#endif /* CONFIG_SECURITY_LANDLOCK */ }/** @@ -796,6 +800,10 @@ static long do_seccomp(unsigned int op, unsigned int flags,return seccomp_set_mode_strict ();case SECCOMP_SET_MODE_FILTER :return seccomp_set_mode_filter (flags, uargs); +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +case SECCOMP_ADD_LANDLOCK_RULE : +return landlock_seccomp_append_prog (flags, uargs); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ default :return -EINVAL ; }security/landlock/Makefile +1
-1@@ -2,4 +2,4 @@ ccflags-$(CONFIG_SECURITY_LANDLOCK) += -Werror=unused-function obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := hooks.o +landlock-y := hooks.o manager.osecurity/landlock/hooks.c +41
-1@@ -290,7 +290,44 @@ static u64 mem_prot_to_access(unsigned long prot, bool private)static inline bool landlock_used (void ) { +#ifdef CONFIG_SECCOMP_FILTER +return !!(current->seccomp.landlock_events); +#else return false; +#endif /* CONFIG_SECCOMP_FILTER */ +} + +/** +* landlock_run_prog - run Landlock program for a syscall +* +* @event_idx: event index in the rules array +* @ctx: non-NULL eBPF context +* @events: Landlock events pointer +*/ +static int landlock_run_prog (u32 event_idx,const struct landlock_context *ctx, +struct landlock_events *events) +{ +struct landlock_node *node; + +if (!events) +return 0 ; + +for (node = events->nodes[event_idx]; node; node = node->prev) { +struct landlock_rule *rule; + +for (rule = node->rule; rule; rule = rule->prev) { +u32 ret; + +if (WARN_ON (!rule->prog)) +continue ; +rcu_read_lock (); + ret =BPF_PROG_RUN (rule->prog, (void *)ctx); +rcu_read_unlock (); +if (ret) +return -EPERM ; + } + } +return 0 ; } static int landlock_decide(enum landlock_subtype_event event,@@ -309,7 +346,10 @@ static int landlock_decide(enum landlock_subtype_event event, .arg2 = ctx_values[1], }; -/* insert manager call here */ +#ifdef CONFIG_SECCOMP_FILTER + ret =landlock_run_prog (event_idx, &ctx, + current->seccomp.landlock_events); +#endif /* CONFIG_SECCOMP_FILTER */ return ret; }security/landlock/manager.c new file +203
-0@@ -0,0 +1,321 @@ +/* +* Landlock LSM - seccomp manager +* +* Copyright © 2017 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 <asm/page.h> /* PAGE_SIZE */ +#include <linux/atomic.h> /* atomic_*(), smp_store_release() */ +#include <linux/bpf.h> /* bpf_prog_put() */ +#include <linux/filter.h> /* struct bpf_prog */ +#include <linux/kernel.h> /* round_up() */ +#include <linux/landlock.h> +#include <linux/sched.h> /* current_cred(), task_no_new_privs() */ +#include <linux/security.h> /* security_capable_noaudit() */ +#include <linux/slab.h> /* alloc(), kfree() */ +#include <linux/types.h> /* atomic_t */ +#include <linux/uaccess.h> /* copy_from_user() */ + +#include "common.h" + +static void put_landlock_rule (struct landlock_rule *rule) +{ +struct landlock_rule *orig = rule; + +/* clean up single-reference branches iteratively */ +while (orig &&atomic_dec_and_test (&orig->usage)) { +struct landlock_rule *freeme = orig; + +bpf_prog_put (orig->prog); + orig = orig->prev; +kfree (freeme); + } +} + +static void put_landlock_node (struct landlock_node *node) +{ +struct landlock_node *orig = node; + +/* clean up single-reference branches iteratively */ +while (orig &&atomic_dec_and_test (&orig->usage)) { +struct landlock_node *freeme = orig; + +put_landlock_rule (orig->rule); + orig = orig->prev; +kfree (freeme); + } +} + +void put_landlock_events (struct landlock_events *events) +{ +if (events &&atomic_dec_and_test (&events->usage)) { +size_t i; + +/* XXX: Do we need to use lockless_dereference() here? */ +for (i =0 ; i <ARRAY_SIZE (events->nodes); i++) { +if (!events->nodes[i]) +continue ; +/* Are we the owner of this node? */ +if (events->nodes[i]->owner == &events->nodes[i]) + events->nodes[i]->owner =NULL ; +put_landlock_node (events->nodes[i]); + } +kfree (events); + } +} + +static struct landlock_events *new_raw_landlock_events (void ) +{ +struct landlock_events *ret; + +/* array filled with NULL values */ + ret =kzalloc (sizeof (*ret),GFP_KERNEL ); +if (!ret) +return ERR_PTR (-ENOMEM ); +atomic_set (&ret->usage,1 ); +return ret; +} + +static struct landlock_events *new_filled_landlock_events (void ) +{ +size_t i; +struct landlock_events *ret; + + ret =new_raw_landlock_events (); +if (IS_ERR (ret)) +return ret; +/* +* We need to initially allocate every nodes to be able to update the +* rules they are pointing to, across every (future) children of the +* current task. +*/ +for (i =0 ; i <ARRAY_SIZE (ret->nodes); i++) { +struct landlock_node *node; + + node =kzalloc (sizeof (*node),GFP_KERNEL ); +if (!node) + goto put_events; +atomic_set (&node->usage,1 ); +/* we are the owner of this node */ + node->owner = &ret->nodes[i]; + ret->nodes[i] = node; + } +return ret; + +put_events: +put_landlock_events (ret); +return ERR_PTR (-ENOMEM ); +} + +static void add_landlock_rule (struct landlock_events *events, +struct landlock_rule *rule) +{ +/* subtype.landlock_rule.event > 0 for loaded programs */ +u32 event_idx =get_index (rule->prog->subtype.landlock_rule.event); + + rule->prev = events->nodes[event_idx]->rule; +WARN_ON (atomic_read (&rule->usage)); +atomic_set (&rule->usage,1 ); +/* do not increment the previous rule usage */ +smp_store_release (&events->nodes[event_idx]->rule, rule); +} + +/* Limit Landlock events to 256KB. */ +#define LANDLOCK_EVENTS_MAX_PAGES (1 << 6) + +/** +* landlock_append_prog - attach a Landlock rule to @current_events +* +* @current_events: landlock_events pointer, must be locked (if needed) to +* prevent a concurrent put/free. This pointer must not be +* freed after the call. +* @prog: non-NULL Landlock rule to append to @current_events. @prog will be +* owned by landlock_append_prog() and freed if an error happened. +* +* Return @current_events or a new pointer when OK. Return a pointer error +* otherwise. +*/ +static struct landlock_events *landlock_append_prog ( +struct landlock_events *current_events,struct bpf_prog *prog) +{ +struct landlock_events *new_events = current_events; +unsigned long pages; +struct landlock_rule *rule; +u32 event_idx; + +if (prog->type !=BPF_PROG_TYPE_LANDLOCK ) { + new_events =ERR_PTR (-EINVAL ); + goto put_prog; + } + +/* validate memory size allocation */ + pages = prog->pages; +if (current_events) { +size_t i; + +for (i =0 ; i <ARRAY_SIZE (current_events->nodes); i++) { +struct landlock_node *walker_n; + +for (walker_n = current_events->nodes[i]; + walker_n; + walker_n = walker_n->prev) { +struct landlock_rule *walker_r; + +for (walker_r = walker_n->rule; + walker_r; + walker_r = walker_r->prev) + pages += walker_r->prog->pages; + } + } +/* count a struct landlock_events if we need to allocate one */ +if (atomic_read (¤t_events->usage) !=1 ) + pages +=round_up (sizeof (*current_events),PAGE_SIZE ) / +PAGE_SIZE ; + } +if (pages >LANDLOCK_EVENTS_MAX_PAGES ) { + new_events =ERR_PTR (-E2BIG ); + goto put_prog; + } + + rule =kzalloc (sizeof (*rule),GFP_KERNEL ); +if (!rule) { + new_events =ERR_PTR (-ENOMEM ); + goto put_prog; + } + rule->prog = prog; + +/* subtype.landlock_rule.event > 0 for loaded programs */ + event_idx =get_index (rule->prog->subtype.landlock_rule.event); + +if (!current_events) { +/* add a new landlock_events, if needed */ + new_events =new_filled_landlock_events (); +if (IS_ERR (new_events)) + goto put_rule; +add_landlock_rule (new_events, rule); + }else { +if (new_events->nodes[event_idx]->owner == + &new_events->nodes[event_idx]) { +/* We are the owner, we can then update the node. */
security/landlock/manager.c line 213
Quoted 10 lines
security/landlock/manager.c new file +10
-0+add_landlock_rule (new_events, rule); + }else if (atomic_read (¤t_events->usage) ==1 ) { +WARN_ON (new_events->nodes[event_idx]->owner); +/* +* We can become the new owner if no other task use it. +* This avoid an unnecessary allocation. +*/ + new_events->nodes[event_idx]->owner = + &new_events->nodes[event_idx]; +add_landlock_rule (new_events, rule);
I still don't get it all, but maybe here to make it simple you have to always allocate, since the WARN_ON() suggests it should be scheduled to be removed... and avoid to care about whether you are using/freeing old rules of the dead task... ?
Quoted 111 lines
security/landlock/manager.c new file +108
-1+ }else { +/* +* We are not the owner, we need to fork current_events +* and then add a new node. +*/ +struct landlock_node *node; +size_t i; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) { + new_events =ERR_PTR (-ENOMEM ); + goto put_rule; + } +atomic_set (&node->usage,1 ); +/* set the previous node after the new_events +* allocation */ + node->prev =NULL ; +/* do not increment the previous node usage */ + node->owner = &new_events->nodes[event_idx]; +/* rule->prev is already NULL */ +atomic_set (&rule->usage,1 ); + node->rule = rule; + + new_events =new_raw_landlock_events (); +if (IS_ERR (new_events)) { +/* put the rule as well */ +put_landlock_node (node); +return ERR_PTR (-ENOMEM ); + } +for (i =0 ; i <ARRAY_SIZE (new_events->nodes); i++) { + new_events->nodes[i] = +lockless_dereference ( + current_events->nodes[i]); +if (i == event_idx) + node->prev = new_events->nodes[i]; +if (!WARN_ON (!new_events->nodes[i])) +atomic_inc (&new_events->nodes[i]->usage); + } + new_events->nodes[event_idx] = node; + +/* +* @current_events will not be freed here because it's usage +* field is > 1. It is only prevented to be freed by another +* subject thanks to the caller of landlock_append_prog() which +* should be locked if needed. +*/ +put_landlock_events (current_events); + } + } +return new_events; + +put_prog: +bpf_prog_put (prog); +return new_events; + +put_rule: +put_landlock_rule (rule); +return new_events; +} + +/** +* landlock_seccomp_append_prog - attach a Landlock rule to the current process +* +* current->seccomp.landlock_events is lazily allocated. When a process fork, +* only a pointer is copied. When a new event is added by a process, if there +* is other references to this process' landlock_events, then a new allocation +* is made to contains an array pointing to Landlock rule lists. This design +* has low-performance impact and is memory efficient while keeping the +* property of append-only rules. +* +* @flags: not used for now, but could be used for TSYNC +* @user_bpf_fd: file descriptor pointing to a loaded Landlock rule +*/ +#ifdef CONFIG_SECCOMP_FILTER +int landlock_seccomp_append_prog (unsigned int flags,const char __user *user_bpf_fd) +{ +struct landlock_events *new_events; +struct bpf_prog *prog; +int bpf_fd; + +/* force no_new_privs to limit privilege escalation */ +if (!task_no_new_privs (current)) +return -EPERM ; +/* will be removed in the future to allow unprivileged tasks */ +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; +if (!user_bpf_fd) +return -EFAULT ; +if (flags) +return -EINVAL ; +if (copy_from_user (&bpf_fd, user_bpf_fd,sizeof (bpf_fd))) +return -EFAULT ; + prog =bpf_prog_get (bpf_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); + +/* +* We don't need to lock anything for the current process hierarchy, +* everything is guarded by the atomic counters. +*/ + new_events =landlock_append_prog (current->seccomp.landlock_events, prog); +/* @prog is managed/freed by landlock_append_prog() */ +if (IS_ERR (new_events)) +return PTR_ERR (new_events); + current->seccomp.landlock_events = new_events; +return 0 ; +} +#endif /* CONFIG_SECCOMP_FILTER */ --2.11.0
Signature · 1 line
tixxdz
Mickaël Salaün
maintainerRe: [kernel-hardening] [PATCH v5 06/10] seccomp,landlock: Handle Landlock events per process hierarchy
Quoted 471 lines · Djalal Harouni
Quoted 470 lines · Mickaël Salaün
The seccomp(2) syscall can be use to apply a Landlock rule to the current process. As with a seccomp filter, the Landlock rule is enforced for all its future children. An inherited rule tree can be updated (append-only) by the owner of inherited Landlock nodes (e.g. a parent process that create a new rule). However, an intermediate task, which did not create a rule, will not be able to update its children's rules.
Landlock rules can be tied to a Landlock event. When such an event is triggered, a tree of rules can be evaluated. Thisk kind of tree is created with a first node. This node reference a list of rules and an optional parent node. Each rule return a 32-bit value which can interrupt the evaluation with a non-zero value. If every rules returned zero, the evaluation continues with the rule list of the parent node, until the end of the tree.
Changes since v4:
* merge manager and seccomp patches
* return-EFAULTin seccomp(2) whenuser_bpf_fdis null to easely check if Landlock is supported
* only allow a process with the globalCAP_SYS_ADMINto use Landlock (will be lifted in the future)
* add an early check to exit as soon as possible if the current process does not have Landlock rulesChanges since v3:
* remove the hard link with seccomp (suggested by Andy Lutomirski and Kees Cook):
* remove the cookie which could imply multiple evaluation of Landlock rules
* remove the origin field in structlandlock_data
* remove documentation fix (merged upstream)
* rename the new seccomp command toSECCOMP_ADD_LANDLOCK_RULE
* internal renaming
* split commit
* new design to be able to inherit on the fly the parent rulesChanges 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 intosecurity/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 ifdefSigned-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Andrew Morton <[email protected]>Cc Andy Lutomirski <[email protected]>Cc James Morris <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Cc Will Drewry <[email protected]>--- include/linux/seccomp.h | 8 ++ include/uapi/linux/seccomp.h | 1 + kernel/fork.c | 14 +- kernel/seccomp.c | 8 ++ security/landlock/Makefile | 2 +- security/landlock/hooks.c | 42 +++++- security/landlock/manager.c | 321 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 392 insertions(+), 4 deletions(-) create mode 100644 security/landlock/manager.cinclude/linux/seccomp.h +8
-0@@ -10,6 +10,10 @@#include <linux/thread_info.h> #include <asm/seccomp.h> +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct landlock_events ; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ +struct seccomp_filter ;/** * struct seccomp - the state of a seccomp'ed process @@ -18,6 +22,7 @@ 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. +* @landlock_events: contains an array of Landlock rules. * * @filter must only be accessed from the context of current as there * is no read locking. @@ -25,6 +30,9 @@ struct seccomp_filter;struct seccomp {int mode;struct seccomp_filter *filter; +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +struct landlock_events *landlock_events; +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ };#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER include/uapi/linux/seccomp.h +1
-0@@ -13,6 +13,7 @@/* Valid operations for seccomp syscall. */ #define SECCOMP_SET_MODE_STRICT 0#define SECCOMP_SET_MODE_FILTER 1 +#define SECCOMP_ADD_LANDLOCK_RULE 2/* Valid flags for SECCOMP_SET_MODE_FILTER */ #define SECCOMP_FILTER_FLAG_TSYNC 1kernel/fork.c +12
-2@@ -37,6 +37,7 @@#include <linux/security.h> #include <linux/hugetlb.h> #include <linux/seccomp.h> +#include <linux/landlock.h> #include <linux/swap.h> #include <linux/syscalls.h> #include <linux/jiffies.h> @@ -515,7 +516,10 @@ 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.landlock_events =NULL ; +#endif /* CONFIG_SECURITY_LANDLOCK */ +#endif /* CONFIG_SECCOMP */ setup_thread_stack (tsk, orig);clear_user_return_notifier (tsk);@@ -1388,7 +1392,13 @@ 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 ) + p->seccomp.landlock_events = current->seccomp.landlock_events; +if (p->seccomp.landlock_events) +atomic_inc (&p->seccomp.landlock_events->usage); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ /* * Explicitly enable no_new_privs here in case it got set kernel/seccomp.c +8
-0@@ -32,6 +32,7 @@#include <linux/security.h> #include <linux/tracehook.h> #include <linux/uaccess.h> +#include <linux/landlock.h> /** * struct seccomp_filter - container for seccomp BPF programs @@ -492,6 +493,9 @@ static void put_seccomp_filter(struct seccomp_filter *filter)void put_seccomp (struct task_struct *tsk) {put_seccomp_filter (tsk->seccomp.filter); +#ifdef CONFIG_SECURITY_LANDLOCK +put_landlock_events (tsk->seccomp.landlock_events); +#endif /* CONFIG_SECURITY_LANDLOCK */ }/** @@ -796,6 +800,10 @@ static long do_seccomp(unsigned int op, unsigned int flags,return seccomp_set_mode_strict ();case SECCOMP_SET_MODE_FILTER :return seccomp_set_mode_filter (flags, uargs); +#if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_SECURITY_LANDLOCK ) +case SECCOMP_ADD_LANDLOCK_RULE : +return landlock_seccomp_append_prog (flags, uargs); +#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */ default :return -EINVAL ; }security/landlock/Makefile +1
-1@@ -2,4 +2,4 @@ ccflags-$(CONFIG_SECURITY_LANDLOCK) += -Werror=unused-function obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o -landlock-y := hooks.o +landlock-y := hooks.o manager.osecurity/landlock/hooks.c +41
-1@@ -290,7 +290,44 @@ static u64 mem_prot_to_access(unsigned long prot, bool private)static inline bool landlock_used (void ) { +#ifdef CONFIG_SECCOMP_FILTER +return !!(current->seccomp.landlock_events); +#else return false; +#endif /* CONFIG_SECCOMP_FILTER */ +} + +/** +* landlock_run_prog - run Landlock program for a syscall +* +* @event_idx: event index in the rules array +* @ctx: non-NULL eBPF context +* @events: Landlock events pointer +*/ +static int landlock_run_prog (u32 event_idx,const struct landlock_context *ctx, +struct landlock_events *events) +{ +struct landlock_node *node; + +if (!events) +return 0 ; + +for (node = events->nodes[event_idx]; node; node = node->prev) { +struct landlock_rule *rule; + +for (rule = node->rule; rule; rule = rule->prev) { +u32 ret; + +if (WARN_ON (!rule->prog)) +continue ; +rcu_read_lock (); + ret =BPF_PROG_RUN (rule->prog, (void *)ctx); +rcu_read_unlock (); +if (ret) +return -EPERM ; + } + } +return 0 ; } static int landlock_decide(enum landlock_subtype_event event,@@ -309,7 +346,10 @@ static int landlock_decide(enum landlock_subtype_event event, .arg2 = ctx_values[1], }; -/* insert manager call here */ +#ifdef CONFIG_SECCOMP_FILTER + ret =landlock_run_prog (event_idx, &ctx, + current->seccomp.landlock_events); +#endif /* CONFIG_SECCOMP_FILTER */ return ret; }security/landlock/manager.c new file +210
-0@@ -0,0 +1,321 @@ +/* +* Landlock LSM - seccomp manager +* +* Copyright © 2017 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 <asm/page.h> /* PAGE_SIZE */ +#include <linux/atomic.h> /* atomic_*(), smp_store_release() */ +#include <linux/bpf.h> /* bpf_prog_put() */ +#include <linux/filter.h> /* struct bpf_prog */ +#include <linux/kernel.h> /* round_up() */ +#include <linux/landlock.h> +#include <linux/sched.h> /* current_cred(), task_no_new_privs() */ +#include <linux/security.h> /* security_capable_noaudit() */ +#include <linux/slab.h> /* alloc(), kfree() */ +#include <linux/types.h> /* atomic_t */ +#include <linux/uaccess.h> /* copy_from_user() */ + +#include "common.h" + +static void put_landlock_rule (struct landlock_rule *rule) +{ +struct landlock_rule *orig = rule; + +/* clean up single-reference branches iteratively */ +while (orig &&atomic_dec_and_test (&orig->usage)) { +struct landlock_rule *freeme = orig; + +bpf_prog_put (orig->prog); + orig = orig->prev; +kfree (freeme); + } +} + +static void put_landlock_node (struct landlock_node *node) +{ +struct landlock_node *orig = node; + +/* clean up single-reference branches iteratively */ +while (orig &&atomic_dec_and_test (&orig->usage)) { +struct landlock_node *freeme = orig; + +put_landlock_rule (orig->rule); + orig = orig->prev; +kfree (freeme); + } +} + +void put_landlock_events (struct landlock_events *events) +{ +if (events &&atomic_dec_and_test (&events->usage)) { +size_t i; + +/* XXX: Do we need to use lockless_dereference() here? */ +for (i =0 ; i <ARRAY_SIZE (events->nodes); i++) { +if (!events->nodes[i]) +continue ; +/* Are we the owner of this node? */ +if (events->nodes[i]->owner == &events->nodes[i]) + events->nodes[i]->owner =NULL ; +put_landlock_node (events->nodes[i]); + } +kfree (events); + } +} + +static struct landlock_events *new_raw_landlock_events (void ) +{ +struct landlock_events *ret; + +/* array filled with NULL values */ + ret =kzalloc (sizeof (*ret),GFP_KERNEL ); +if (!ret) +return ERR_PTR (-ENOMEM ); +atomic_set (&ret->usage,1 ); +return ret; +} + +static struct landlock_events *new_filled_landlock_events (void ) +{ +size_t i; +struct landlock_events *ret; + + ret =new_raw_landlock_events (); +if (IS_ERR (ret)) +return ret; +/* +* We need to initially allocate every nodes to be able to update the +* rules they are pointing to, across every (future) children of the +* current task. +*/ +for (i =0 ; i <ARRAY_SIZE (ret->nodes); i++) { +struct landlock_node *node; + + node =kzalloc (sizeof (*node),GFP_KERNEL ); +if (!node) + goto put_events; +atomic_set (&node->usage,1 ); +/* we are the owner of this node */ + node->owner = &ret->nodes[i]; + ret->nodes[i] = node; + } +return ret; + +put_events: +put_landlock_events (ret); +return ERR_PTR (-ENOMEM ); +} + +static void add_landlock_rule (struct landlock_events *events, +struct landlock_rule *rule) +{ +/* subtype.landlock_rule.event > 0 for loaded programs */ +u32 event_idx =get_index (rule->prog->subtype.landlock_rule.event); + + rule->prev = events->nodes[event_idx]->rule; +WARN_ON (atomic_read (&rule->usage)); +atomic_set (&rule->usage,1 ); +/* do not increment the previous rule usage */ +smp_store_release (&events->nodes[event_idx]->rule, rule); +} + +/* Limit Landlock events to 256KB. */ +#define LANDLOCK_EVENTS_MAX_PAGES (1 << 6) + +/** +* landlock_append_prog - attach a Landlock rule to @current_events +* +* @current_events: landlock_events pointer, must be locked (if needed) to +* prevent a concurrent put/free. This pointer must not be +* freed after the call. +* @prog: non-NULL Landlock rule to append to @current_events. @prog will be +* owned by landlock_append_prog() and freed if an error happened. +* +* Return @current_events or a new pointer when OK. Return a pointer error +* otherwise. +*/ +static struct landlock_events *landlock_append_prog ( +struct landlock_events *current_events,struct bpf_prog *prog) +{ +struct landlock_events *new_events = current_events; +unsigned long pages; +struct landlock_rule *rule; +u32 event_idx; + +if (prog->type !=BPF_PROG_TYPE_LANDLOCK ) { + new_events =ERR_PTR (-EINVAL ); + goto put_prog; + } + +/* validate memory size allocation */ + pages = prog->pages; +if (current_events) { +size_t i; + +for (i =0 ; i <ARRAY_SIZE (current_events->nodes); i++) { +struct landlock_node *walker_n; + +for (walker_n = current_events->nodes[i]; + walker_n; + walker_n = walker_n->prev) { +struct landlock_rule *walker_r; + +for (walker_r = walker_n->rule; + walker_r; + walker_r = walker_r->prev) + pages += walker_r->prog->pages; + } + } +/* count a struct landlock_events if we need to allocate one */ +if (atomic_read (¤t_events->usage) !=1 ) + pages +=round_up (sizeof (*current_events),PAGE_SIZE ) / +PAGE_SIZE ; + } +if (pages >LANDLOCK_EVENTS_MAX_PAGES ) { + new_events =ERR_PTR (-E2BIG ); + goto put_prog; + } + + rule =kzalloc (sizeof (*rule),GFP_KERNEL ); +if (!rule) { + new_events =ERR_PTR (-ENOMEM ); + goto put_prog; + } + rule->prog = prog; + +/* subtype.landlock_rule.event > 0 for loaded programs */ + event_idx =get_index (rule->prog->subtype.landlock_rule.event); + +if (!current_events) { +/* add a new landlock_events, if needed */ + new_events =new_filled_landlock_events (); +if (IS_ERR (new_events)) + goto put_rule; +add_landlock_rule (new_events, rule); + }else { +if (new_events->nodes[event_idx]->owner == + &new_events->nodes[event_idx]) { +/* We are the owner, we can then update the node. */ +add_landlock_rule (new_events, rule); + }else if (atomic_read (¤t_events->usage) ==1 ) { +WARN_ON (new_events->nodes[event_idx]->owner); +/* +* We can become the new owner if no other task use it. +* This avoid an unnecessary allocation. +*/
Quoted 8 lines
security/landlock/manager.c line 213
Quoted 3 lines
security/landlock/manager.c new file +3
-0+ new_events->nodes[event_idx]->owner = + &new_events->nodes[event_idx]; +add_landlock_rule (new_events, rule);I still don't get it all, but maybe here to make it simple you have to always allocate, since the
WARN_ON()suggests it should be scheduled to be removed... and avoid to care about whether you are using/freeing old rules of the dead task... ?
I'm not sure to get your point but I will make the inheritance behavior similar to seccomp filter at first, hence simpler.
Mickaël Salaün
maintainerPATCH v5 7/10 bpf: Add a Landlock 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 sandbox create a read-only environment. It is only allowed to write to a character device such as a TTY:
# :> X
# echo $? 0 # ./samples/bpf/landlock1 /bin/sh -i Launching a new sandboxed process. # :> Y cannot create Y: Operation not permitted
Changes since v4:
* write Landlock rule in C and compiled it with LLVM
* remove cgroup handling
* remove path handling: only handle a read-only environment
* remove errno return codes
Changes since v3:
* remove seccomp and origin field: completely free from seccomp programs
* handle more FS-related hooks
* handle inode hooks and directory traversal
* add faked but consistent view thanks to ENOENT
* add /lib64 in the example
* fix spelling
* rename some types and definitions (e.g. SECCOMP_ADD_LANDLOCK_RULE)
Changes since v2:
* use BPF_PROG_ATTACH for cgroup handling
--- samples/bpf/.gitignore | 32 ++++++++++++++ samples/bpf/Makefile | 4 ++ samples/bpf/bpf_load.c | 26 +++++++++-- samples/bpf/landlock1_kern.c | 46 +++++++++++++++++++ samples/bpf/landlock1_user.c | 102 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 samples/bpf/.gitignore create mode 100644 samples/bpf/landlock1_kern.c create mode 100644 samples/bpf/landlock1_user.c
samples/bpf/.gitignore new file +32 -0
@@ -0,0 +1,32 @@ +fds_example +lathist +lwt_len_hist +map_perf_test +offwaketime +sampleip +sockex1 +sockex2 +sockex3 +sock_example +spintest +tc_l2_redirect +test_cgrp2_array_pin +test_cgrp2_attach +test_cgrp2_attach2 +test_cgrp2_sock +test_cgrp2_sock2 +test_current_task_under_cgroup +test_lru_dist +test_overhead +test_probe_write_user +trace_event +trace_output +tracex1 +tracex2 +tracex3 +tracex4 +tracex5 +tracex6 +xdp1 +xdp2 +xdp_tx_iptunnel
samples/bpf/Makefile +4 -0
@@ -34,6 +34,7 @@ hostprogs-y += sampleip hostprogs-y += tc_l2_redirect hostprogs-y += lwt_len_hist hostprogs-y += xdp_tx_iptunnel +hostprogs-y += landlock1 # Libbpf dependencies LIBBPF := ../../tools/lib/bpf/bpf.o
@@ -72,6 +73,7 @@ sampleip-objs := bpf_load.o $(LIBBPF) sampleip_user.o tc_l2_redirect-objs := bpf_load.o $(LIBBPF) tc_l2_redirect_user.o lwt_len_hist-objs := bpf_load.o $(LIBBPF) lwt_len_hist_user.o xdp_tx_iptunnel-objs := bpf_load.o $(LIBBPF) xdp_tx_iptunnel_user.o +landlock1-objs := bpf_load.o $(LIBBPF) landlock1_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -105,6 +107,7 @@ always += trace_event_kern.o always += sampleip_kern.o always += lwt_len_hist_kern.o always += xdp_tx_iptunnel_kern.o +always += landlock1_kern.o HOSTCFLAGS += -I$(objtree)/usr/include HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -139,6 +142,7 @@ HOSTLOADLIBES_sampleip += -lelf HOSTLOADLIBES_tc_l2_redirect += -l elf HOSTLOADLIBES_lwt_len_hist += -l elf HOSTLOADLIBES_xdp_tx_iptunnel += -lelf +HOSTLOADLIBES_landlock1 += -lelf # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: # make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
samples/bpf/bpf_load.c +22 -4
@@ -68,6 +68,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)bool is_perf_event =strncmp (event,"perf_event" ,10 ) ==0 ;bool is_cgroup_skb =strncmp (event,"cgroup/skb" ,10 ) ==0 ;bool is_cgroup_sk =strncmp (event,"cgroup/sock" ,11 ) ==0 ; +bool is_landlock =strncmp (event,"landlock" ,8 ) ==0 ;size_t insns_cnt = size /sizeof (struct bpf_insn );enum bpf_prog_type prog_type;char buf[256 ];
@@ -93,6 +94,12 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) prog_type =BPF_PROG_TYPE_CGROUP_SKB ; }else if (is_cgroup_sk) { prog_type =BPF_PROG_TYPE_CGROUP_SOCK ; + }else if (is_landlock) { + prog_type =BPF_PROG_TYPE_LANDLOCK ; +if (!subtype.landlock_rule.event) { +printf ("No subtype\n" ); +return -1 ; + } }else {printf ("Unknown event '%s'\n" , event);return -1 ;
@@ -107,7 +114,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) prog_fd[prog_cnt++] = fd; -if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk) +if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk || is_landlock)return 0 ;if (is_socket) {
@@ -278,6 +285,8 @@ int load_bpf_file(char *path)Elf_Data *data, *data_prog, *symbols =NULL ;char *shname, *shname_prog; + subtype.landlock_rule.event =0 ; +if (elf_version (EV_CURRENT ) ==EV_NONE )return 1 ;
@@ -322,6 +331,14 @@ int load_bpf_file(char *path) processed_sec[i] = true;if (load_maps (data->d_buf, data->d_size))return 1 ; + }else if (strcmp (shname,"subtype" ) ==0 ) { + processed_sec[i] = true; +if (data->d_size !=sizeof (union bpf_prog_subtype )) { +printf ("invalid size of subtype section %zd\n" , + data->d_size); +return 1 ; + } +memcpy (&subtype, data->d_buf,sizeof (union bpf_prog_subtype )); }else if (shdr.sh_type ==SHT_SYMTAB ) { symbols = data; }
@@ -357,14 +374,14 @@ int load_bpf_file(char *path) memcmp(shname_prog, "xdp", 3) == 0 || memcmp(shname_prog, "perf_event", 10) == 0 || memcmp(shname_prog, "socket", 6) == 0 || - memcmp(shname_prog, "cgroup/", 7) == 0) + memcmp(shname_prog, "cgroup/", 7) == 0 || + memcmp(shname_prog, "landlock", 8) == 0)load_and_attach (shname_prog, insns, data_prog->d_size); } }/* load programs that don't use maps */ for (i =1 ; i < ehdr.e_shnum; i++) { -if (processed_sec[i])continue ;
@@ -377,7 +394,8 @@ int load_bpf_file(char *path) memcmp(shname, "xdp", 3) == 0 || memcmp(shname, "perf_event", 10) == 0 || memcmp(shname, "socket", 6) == 0 || - memcmp(shname, "cgroup/", 7) == 0) + memcmp(shname, "cgroup/", 7) == 0 || + memcmp(shname, "landlock", 8) == 0)load_and_attach (shname, data->d_buf, data->d_size); }
samples/bpf/landlock1_kern.c new file +46 -0
46 lines not on this page · raw message
samples/bpf/landlock1_user.c new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerRe: [PATCH v5 07/10] bpf: Add a Landlock sandbox example
Quoted 77 lines · Mickaël Salaün
Add a basic sandbox tool to create a process isolated from some part of the system. This sandbox create a read-only environment. It is only allowed to write to a character device such as a TTY:
# :> X
# echo $? 0 # ./samples/bpf/landlock1 /bin/sh -i Launching a new sandboxed process. # :> Y cannot create Y: Operation not permittedChanges since v4:
* write Landlock rule in C and compiled it with LLVM
* remove cgroup handling
* remove path handling: only handle a read-only environment
* remove errno return codesChanges since v3:
* remove seccomp and origin field: completely free from seccomp programs
* handle more FS-related hooks
* handle inode hooks and directory traversal
* add faked but consistent view thanks to ENOENT
* add /lib64 in the example
* fix spelling
* rename some types and definitions (e.g.SECCOMP_ADD_LANDLOCK_RULE)Changes since v2:
* useBPF_PROG_ATTACHfor cgroup handlingSigned-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]>--- samples/bpf/.gitignore | 32 ++++++++++++++ samples/bpf/Makefile | 4 ++ samples/bpf/bpf_load.c | 26 +++++++++-- samples/bpf/landlock1_kern.c | 46 +++++++++++++++++++ samples/bpf/landlock1_user.c | 102 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 samples/bpf/.gitignore create mode 100644 samples/bpf/landlock1_kern.c create mode 100644 samples/bpf/landlock1_user.csamples/bpf/.gitignore new file +22
-0@@ -0,0 +1,32 @@ +fds_example +lathist +lwt_len_hist +map_perf_test +offwaketime +sampleip +sockex1 +sockex2 +sockex3 +sock_example +spintest +tc_l2_redirect +test_cgrp2_array_pin +test_cgrp2_attach +test_cgrp2_attach2 +test_cgrp2_sock +test_cgrp2_sock2 +test_current_task_under_cgroup +test_lru_dist +test_overhead +test_probe_write_user +trace_event
samples/bpf/.gitignore line 32
Quoted 10 lines
samples/bpf/.gitignore new file +10
-0+trace_output +tracex1 +tracex2 +tracex3 +tracex4 +tracex5 +tracex6 +xdp1 +xdp2 +xdp_tx_iptunnel
Please ignore this hunk, it was part of another patch series…
Mickaël Salaün
maintainerPATCH v5 8/10 seccomp: Enhance test_harness with an assert step mechanism
In reply to Mickaël SalaünThis is useful to return an information about the error without being able to write to TH_LOG_STREAM.
Helpers from test_harness.h may be useful outside of the seccomp directory.
--- tools/testing/selftests/seccomp/test_harness.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/seccomp/test_harness.h +7 -1
@@ -397,7 +397,7 @@ struct __test_metadata {const char *name;void (*fn)(struct __test_metadata *);int termsig; -int passed; +__s8 passed;int trigger;/* extra handler after the evaluation */ struct __test_metadata *prev, *next; };
@@ -476,6 +476,12 @@ void __run_test(struct __test_metadata *t)"instead of by signal (code: %d)\n" , t->name, WEXITSTATUS(status)); + }else if (t->passed <0 ) { +fprintf (TH_LOG_STREAM , +"%s: Failed at step #%d\n" , + t->name, + t->passed *-1 ); + t->passed =0 ; } }else if (WIFSIGNALED (status)) { t->passed =0 ;
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerPATCH v5 9/10 bpf,landlock: Add tests for Landlock
In reply to Mickaël SalaünTest basic context access and filesystem event with multiple cases.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/bpf/test_verifier.c | 54 +++- tools/testing/selftests/landlock/.gitignore | 2 + tools/testing/selftests/landlock/Makefile | 47 +++ tools/testing/selftests/landlock/rules/Makefile | 52 +++ tools/testing/selftests/landlock/rules/README.rst | 1 + .../testing/selftests/landlock/rules/bpf_helpers.h | 1 + tools/testing/selftests/landlock/rules/fs1.c | 31 ++ tools/testing/selftests/landlock/rules/fs2.c | 31 ++ tools/testing/selftests/landlock/test_fs.c | 347 +++++++++++++++++++++ 10 files changed, 566 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/landlock/.gitignore create mode 100644 tools/testing/selftests/landlock/Makefile create mode 100644 tools/testing/selftests/landlock/rules/Makefile create mode 120000 tools/testing/selftests/landlock/rules/README.rst create mode 120000 tools/testing/selftests/landlock/rules/bpf_helpers.h create mode 100644 tools/testing/selftests/landlock/rules/fs1.c create mode 100644 tools/testing/selftests/landlock/rules/fs2.c create mode 100644 tools/testing/selftests/landlock/test_fs.c
tools/testing/selftests/Makefile +1 -0
@@ -10,6 +10,7 @@ TARGETS += futex TARGETS += gpio TARGETS += ipc TARGETS += kcmp +TARGETS += landlock TARGETS += lib TARGETS += membarrier TARGETS += memfd
tools/testing/selftests/bpf/test_verifier.c +53 -1
60 lines not on this page · raw message
tools/testing/selftests/landlock/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +/test_fs +/tmp_*
tools/testing/selftests/landlock/Makefile new file +47 -0
47 lines not on this page · raw message
tools/testing/selftests/landlock/rules/Makefile new file +52 -0
52 lines not on this page · raw message
tools/testing/selftests/landlock/rules/README.rst new file +1 -0
@@ -0,0 +1 @@ +../../../../../samples/bpf/README.rst
tools/testing/selftests/landlock/rules/bpf_helpers.h new file +1 -0
@@ -0,0 +1 @@ +../../../../../samples/bpf/bpf_helpers.h
tools/testing/selftests/landlock/rules/fs1.c new file +31 -0
@@ -0,0 +1,31 @@ +/* +* Copyright © 2017 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. +* +* Landlock test - Read-only filesystem +*/ + +#include <uapi/linux/bpf.h> +#include "bpf_helpers.h" + +SEC("landlock1") +static int landlock_fs_prog1 (struct landlock_context *ctx) +{ +if (!(ctx->arg2 &LANDLOCK_ACTION_FS_WRITE )) +return 0 ; +return 1 ; +} + +SEC("subtype") +static union bpf_prog_subtype _subtype = { + .landlock_rule = { + .version =1 , + .event =LANDLOCK_SUBTYPE_EVENT_FS , + } +}; + +SEC("license") +static char _license[] ="GPL" ;
tools/testing/selftests/landlock/rules/fs2.c new file +31 -0
@@ -0,0 +1,31 @@ +/* +* Copyright © 2017 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. +* +* Landlock test - No-open filesystem +*/ + +#include <uapi/linux/bpf.h> +#include "bpf_helpers.h" + +SEC("landlock1") +static int landlock_fs_prog1 (struct landlock_context *ctx) +{ +if (!(ctx->arg2 &LANDLOCK_ACTION_FS_GET )) +return 0 ; +return 1 ; +} + +SEC("subtype") +static union bpf_prog_subtype _subtype = { + .landlock_rule = { + .version =1 , + .event =LANDLOCK_SUBTYPE_EVENT_FS , + } +}; + +SEC("license") +static char _license[] ="GPL" ;
tools/testing/selftests/landlock/test_fs.c new file +347 -0
347 lines not on this page · raw message
Signature · 1 line
2.11.0
Mickaël Salaün
maintainerPATCH v5 10/10 landlock: Add user and kernel documentation for Landlock
In reply to Mickaël SalaünThis documentation can be built with the Sphinx framework.
--- Documentation/security/index.rst | 1 + Documentation/security/landlock/index.rst | 19 ++ Documentation/security/landlock/kernel.rst | 132 +++++++++++++ Documentation/security/landlock/user.rst | 298 +++++++++++++++++++++++++++++ 4 files changed, 450 insertions(+) create mode 100644 Documentation/security/landlock/index.rst create mode 100644 Documentation/security/landlock/kernel.rst create mode 100644 Documentation/security/landlock/user.rst
Documentation/security/index.rst +1 -0
@@ -5,3 +5,4 @@ Security documentation .. toctree:: tpm/index + landlock/index
Documentation/security/landlock/index.rst new file +19 -0
@@ -0,0 +1,19 @@ +============ +Landlock LSM +============ + +Landlock is a stackable Linux Security Module (LSM) that makes it possible to +create security sandboxes. This kind of sandbox is expected to help mitigate +the security impact of bugs or unexpected/malicious behaviors in user-space +applications. The current version allows only a process with the global +CAP_SYS_ADMIN capability to create such sandboxes but the ultimate goal of +Landlock is to empower any process, including unprivileged ones, to securely +restrict themselves. Landlock is inspired by seccomp-bpf but instead of +filtering syscalls and their raw arguments, a Landlock rule can inspect the use +of kernel objects like files and hence make a decision according to the kernel +semantic. + +.. toctree:: + + user + kernel
Documentation/security/landlock/kernel.rst new file +132 -0
132 lines not on this page · raw message
Documentation/security/landlock/user.rst new file +298 -0
298 lines not on this page · raw message
Signature · 1 line
2.11.0
Andy Lutomirski
reviewerRe: [PATCH v5 10/10] landlock: Add user and kernel documentation for Landlock
Quoted 24 lines · Mickaël Salaün
This documentation can be built with the Sphinx framework.
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 Jonathan Corbet <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Documentation/security/landlock/user.rst new file +11
-0+ +Writing a rule +-------------- + +To enforce a security policy, a thread first needs to create a Landlock rule. +The easiest way to write an eBPF program depicting a security rule is to write +it in the C language. As described in *samples/bpf/README.rst*, LLVM can +compile such programs. Files *samples/bpf/landlock1_kern.c* and those in +*tools/testing/selftests/landlock/rules/* can be used as examples. The +following example is a simple rule to forbid file creation, whatever syscall +may be used (e.g. open, mkdir, link...).
Documentation/security/landlock/user.rst line 33
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ +.. code-block:: c + + static int deny_file_creation(struct landlock_context *ctx) + { + if (ctx->arg2 & LANDLOCK_ACTION_FS_NEW) + return 1; + return 0; + } +
Would it make sense to define landlock_context (or at least a prefix thereof) in here? Also, can't "arg2" have a better name?
Can you specify what the return value means? Are 0 and 1 the only choices? Would "KILL" be useful? How about "COREDUMP"?
Quoted 30 lines
Documentation/security/landlock/user.rst new file +30
-0+File system action types +------------------------ + +Flags are used to express actions. This makes it possible to compose actions +and leaves room for future improvements to add more fine-grained action types. + +.. kernel-doc:: include/uapi/linux/bpf.h + :doc: landlock_action_fs + +.. flat-table:: FS action types availability + + * - flags + - since + + * - LANDLOCK_ACTION_FS_EXEC + - v1 + + * - LANDLOCK_ACTION_FS_WRITE + - v1 + + * - LANDLOCK_ACTION_FS_READ + - v1 + + * - LANDLOCK_ACTION_FS_NEW + - v1 + + * - LANDLOCK_ACTION_FS_GET + - v1 + + * - LANDLOCK_ACTION_FS_REMOVE
Documentation/security/landlock/user.rst line 219
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ - v1 + + * - LANDLOCK_ACTION_FS_IOCTL + - v1 + + * - LANDLOCK_ACTION_FS_LOCK + - v1 + + * - LANDLOCK_ACTION_FS_FCNTL + - v1
What happens if you run an old program on a new kernel? Can you get unexpected action types?
Quoted 19 lines
Documentation/security/landlock/user.rst new file +19
-0+ + +Ability types +------------- + +The ability of a Landlock rule describes the available features (i.e. context +fields and helpers). This is useful to abstract user-space privileges for +Landlock rules, which may not need all abilities (e.g. debug). Only the +minimal set of abilities should be used (e.g. disable debug once in +production). + + +.. kernel-doc:: include/uapi/linux/bpf.h + :doc: landlock_subtype_ability + +.. flat-table:: Ability types availability + + * - flags + - since
Documentation/security/landlock/user.rst line 248
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ - capability + + * - LANDLOCK_SUBTYPE_ABILITY_WRITE + - v1 + - CAP_SYS_ADMIN + + * - LANDLOCK_SUBTYPE_ABILITY_DEBUG + - v1 + - CAP_SYS_ADMIN +
What do "WRITE" and "DEBUG" mean in this context? I'm totally lost.
Hmm. Reading below, "WRITE" seems to mean "modify state". Would that be accurate?
Documentation/security/landlock/user.rst line 275
Quoted 13 lines
Documentation/security/landlock/user.rst new file +12
-0+ +Helper functions +---------------- + +See *include/uapi/linux/bpf.h* for functions documentation. + +.. flat-table:: Generic functions availability + + + * - bpf_get_current_comm + - v1 + - LANDLOCK_SUBTYPE_ABILITY_DEBUG
What would this be used for?
Documentation/security/landlock/user.rst line 288
Quoted 4 lines
Documentation/security/landlock/user.rst new file +4
-0+ * - bpf_get_trace_printk + - v1 + - LANDLOCK_SUBTYPE_ABILITY_DEBUG +
This is different from the other DEBUG stuff in that it has side effects. I wonder if it should have a different flag.
--Andy
Mickaël Salaün
maintainerRe: [PATCH v5 10/10] landlock: Add user and kernel documentation for Landlock
Quoted 30 lines · Andy Lutomirski
Quoted 29 lines · Mickaël Salaün
This documentation can be built with the Sphinx framework.
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 Jonathan Corbet <[email protected]>Cc Kees Cook <[email protected]>Cc Serge E. Hallyn <[email protected]>Documentation/security/landlock/user.rst new file +16
-0+ +Writing a rule +-------------- + +To enforce a security policy, a thread first needs to create a Landlock rule. +The easiest way to write an eBPF program depicting a security rule is to write +it in the C language. As described in *samples/bpf/README.rst*, LLVM can +compile such programs. Files *samples/bpf/landlock1_kern.c* and those in +*tools/testing/selftests/landlock/rules/* can be used as examples. The +following example is a simple rule to forbid file creation, whatever syscall +may be used (e.g. open, mkdir, link...). + +.. code-block:: c + + static int deny_file_creation(struct landlock_context *ctx) + {
Quoted 8 lines
Documentation/security/landlock/user.rst line 33
Quoted 5 lines
Documentation/security/landlock/user.rst new file +5
-0+ if (ctx->arg2 & LANDLOCK_ACTION_FS_NEW) + return 1; + return 0; + } +Would it make sense to define
landlock_context(or at least a prefix thereof) in here? Also, can't "arg2" have a better name?
arg2 is a generic name. Its meaning depends on the Landlock event, here it is an action bitfield (FS event).
Quoted 3 lines
Can you specify what the return value means? Are 0 and 1 the only choices? Would "KILL" be useful? How about "COREDUMP"?
This is explained thereafter and in the kernel Q&A section. I need to briefly introduce that here.
Quoted 36 lines
Quoted 36 lines
Documentation/security/landlock/user.rst new file +35
-0+File system action types +------------------------ + +Flags are used to express actions. This makes it possible to compose actions +and leaves room for future improvements to add more fine-grained action types. + +.. kernel-doc:: include/uapi/linux/bpf.h + :doc: landlock_action_fs + +.. flat-table:: FS action types availability + + * - flags + - since + + * - LANDLOCK_ACTION_FS_EXEC + - v1 + + * - LANDLOCK_ACTION_FS_WRITE + - v1 + + * - LANDLOCK_ACTION_FS_READ + - v1 + + * - LANDLOCK_ACTION_FS_NEW + - v1 + + * - LANDLOCK_ACTION_FS_GET + - v1 + + * - LANDLOCK_ACTION_FS_REMOVE + - v1 + + * - LANDLOCK_ACTION_FS_IOCTL + - v1 +
Quoted 8 lines
Documentation/security/landlock/user.rst line 219
Quoted 5 lines
Documentation/security/landlock/user.rst new file +5
-0+ * - LANDLOCK_ACTION_FS_LOCK + - v1 + + * - LANDLOCK_ACTION_FS_FCNTL + - v1What happens if you run an old program on a new kernel? Can you get unexpected action types?
The old flags will still make sense, the new ones should be ignored by the rule.
Quoted 33 lines
Quoted 20 lines
Documentation/security/landlock/user.rst new file +19
-0+ + +Ability types +------------- + +The ability of a Landlock rule describes the available features (i.e. context +fields and helpers). This is useful to abstract user-space privileges for +Landlock rules, which may not need all abilities (e.g. debug). Only the +minimal set of abilities should be used (e.g. disable debug once in +production). + + +.. kernel-doc:: include/uapi/linux/bpf.h + :doc: landlock_subtype_ability + +.. flat-table:: Ability types availability + + * - flags + - sinceDocumentation/security/landlock/user.rst line 248
Quoted 10 lines
Documentation/security/landlock/user.rst new file +10
-0+ - capability + + * - LANDLOCK_SUBTYPE_ABILITY_WRITE + - v1 + - CAP_SYS_ADMIN + + * - LANDLOCK_SUBTYPE_ABILITY_DEBUG + - v1 + - CAP_SYS_ADMIN +What do "WRITE" and "DEBUG" mean in this context? I'm totally lost.
Quoted 2 lines
Hmm. Reading below, "WRITE" seems to mean "modify state". Would that be accurate?
That is correct, but handling a state in a safe way imply more than only the ability to "write" outside bpfland (e.g. sequential execution).
Quoted 10 lines
Quoted 9 lines
Documentation/security/landlock/user.rst new file +8
-0+ +Helper functions +---------------- + +See *include/uapi/linux/bpf.h* for functions documentation. + +.. flat-table:: Generic functions availability +
Quoted 6 lines
Documentation/security/landlock/user.rst line 275
Quoted 4 lines
Documentation/security/landlock/user.rst new file +4
-0+ + * - bpf_get_current_comm + - v1 + - LANDLOCK_SUBTYPE_ABILITY_DEBUGWhat would this be used for?
To get more information about the process which trigger an action?
Quoted 8 lines
Documentation/security/landlock/user.rst line 288
Quoted 5 lines
Documentation/security/landlock/user.rst new file +4
-0+ * - bpf_get_trace_printk + - v1 + - LANDLOCK_SUBTYPE_ABILITY_DEBUG +This is different from the other DEBUG stuff in that it has side effects. I wonder if it should have a different flag.
I think the debug flag is a clear warning to not ship a rule using this ability. Maybe a sub-flag LANDLOCK_SUBTYPE_ABILITY_DEBUG_PRINT would fit here?
Mickaël