Skip to contentSkip to conversation

Landlock LSM

PATCH Superseded Mickaël Salaün 5 participants 26 messages Nov 4, 2019 – Nov 8, 2019

Subsystems (12)

v13 of 34 Superseded Latest is v34 →

lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

v13 cover Landlock LSM

Hi,

Following the previous series [1], this thirteenth series mainly clean up and add safeguards to the domain management. This series also improves the documentation, add comments, and fixes some latent bugs.

This is the first step of the roadmap discussed at LPC [2]. While the intended final goal is to allow unprivileged (or non-root) 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.

This series can be applied on top of bpf-next, commit e93d99180abd ("selftests/bpf: Restore $(OUTPUT)/test_stub.o rule"). This can be tested with CONFIG_BPF_SYSCALL, CONFIG_SECCOMP_FILTER and CONFIG_SECURITY_LANDLOCK. This patch series can be found in a Git repository here:
https://github.com/landlock-lsm/linux/commits/landlock-v13
I would really appreciate constructive comments on the design and the code.

# Landlock LSM

Landlock is a stackable LSM [3] intended to be used as a low-level framework to build custom access-control/audit systems or safe endpoint security agents. There is currently one Landlock hook dedicated to check ptrace(2). This hook accepts a dedicated eBPF program, called a Landlock program, which can currently compare its position in the hierarchy of similar programs tied to other processes. This enables to enforce programmatic scoped ptrace restrictions.

The final goal of this new Linux Security Module (LSM) called Landlock is to allow any process, including unprivileged ones, to create powerful security sandboxes comparable to XNU Sandbox, FreeBSD Capsicum or OpenBSD Pledge (which could be implemented with Landlock). This kind of sandbox is expected to help mitigate the security impact of bugs or unexpected/malicious behaviors in user-space applications.

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.

The documentation patch contains some kernel documentation, explanations on how to use Landlock and a FAQ. The compiled documentation and some talks can be found here: https://landlock.io

# 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 (i.e. seccomp-object), 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/userspace-api/seccomp_filter.rst

## 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 v12

* make landlock_prepend_prog() more consistant by putting its caller in charge of the lifetime of the Landlock domain being updated
* always copy a domain being updated, which garantee its immutability, and behave as a copy-on-write thanks to commit_creds()
* fix preemption
* improve documentation and comments

# Changes since v11

* rework domain management
* minor fixes

# Changes since v10

* remove all the file system related features: program types, inode map and expected_attach_triggers
* replace the static ptrace security policy with a new and simpler ptrace program (attached) type and a task_landlock_ptrace_ancestor() eBPF helper
* do not rely on seccomp internal structure but use stacked credentials insdead
* extend ptrace tests
* add more documentation
* split and rename files/patches
* miscellaneous fixes

Previous changes can be found in a previous cover-letter [4].

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/

Regards,

Mickaël Salaün (7):
bpf,landlock: Define an eBPF program type for Landlock hooks
landlock: Add the management of domains landlock,seccomp: Apply Landlock programs to process hierarchy
landlock: Add ptrace LSM hooks
bpf,landlock: Add task_landlock_ptrace_ancestor() helper bpf,landlock: Add tests for the Landlock ptrace program type
landlock: Add user and kernel documentation for Landlock

 Documentation/security/index.rst              |   1 +
 Documentation/security/landlock/index.rst     |  22 ++
 Documentation/security/landlock/kernel.rst    | 166 ++++++++++
 Documentation/security/landlock/user.rst      | 153 ++++++++++
 MAINTAINERS                                   |   9 +
 include/linux/bpf.h                           |   3 +
 include/linux/bpf_types.h                     |   3 +
 include/linux/landlock.h                      |  25 ++
 include/linux/lsm_hooks.h                     |   1 +
 include/uapi/linux/bpf.h                      |  23 +-
 include/uapi/linux/landlock.h                 |  39 +++
 include/uapi/linux/seccomp.h                  |   1 +
 kernel/bpf/syscall.c                          |   9 +
 kernel/bpf/verifier.c                         |  11 +
 kernel/seccomp.c                              |   4 +
 scripts/bpf_helpers_doc.py                    |   1 +
 security/Kconfig                              |   1 +
 security/Makefile                             |   2 +
 security/landlock/Kconfig                     |  19 ++
 security/landlock/Makefile                    |   6 +
 security/landlock/bpf_ptrace.c                |  98 ++++++
 security/landlock/bpf_ptrace.h                |  17 ++
 security/landlock/bpf_run.c                   |  65 ++++
 security/landlock/bpf_run.h                   |  25 ++
 security/landlock/bpf_verify.c                |  87 ++++++
 security/landlock/common.h                    |  84 +++++
 security/landlock/domain_manage.c             | 152 +++++++++
 security/landlock/domain_manage.h             |  22 ++
 security/landlock/domain_syscall.c            |  93 ++++++
 security/landlock/hooks_cred.c                |  47 +++
 security/landlock/hooks_cred.h                |  14 +
 security/landlock/hooks_ptrace.c              | 114 +++++++
 security/landlock/hooks_ptrace.h              |  19 ++
 security/landlock/init.c                      |  32 ++
 security/security.c                           |  15 +
 tools/include/uapi/linux/bpf.h                |  23 +-
 tools/include/uapi/linux/landlock.h           |  22 ++
 tools/lib/bpf/libbpf_probes.c                 |   3 +
 tools/testing/selftests/bpf/config            |   3 +
 tools/testing/selftests/bpf/test_verifier.c   |   1 +
 .../testing/selftests/bpf/verifier/landlock.c |  56 ++++
 tools/testing/selftests/landlock/.gitignore   |   5 +
 tools/testing/selftests/landlock/Makefile     |  27 ++
 tools/testing/selftests/landlock/config       |   5 +
 tools/testing/selftests/landlock/test.h       |  48 +++
 tools/testing/selftests/landlock/test_base.c  |  24 ++
 .../testing/selftests/landlock/test_ptrace.c  | 289 ++++++++++++++++++
 47 files changed, 1887 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/security/landlock/index.rst
 create mode 100644 Documentation/security/landlock/kernel.rst
 create mode 100644 Documentation/security/landlock/user.rst
 create mode 100644 include/linux/landlock.h
 create mode 100644 include/uapi/linux/landlock.h
 create mode 100644 security/landlock/Kconfig
 create mode 100644 security/landlock/Makefile
 create mode 100644 security/landlock/bpf_ptrace.c
 create mode 100644 security/landlock/bpf_ptrace.h
 create mode 100644 security/landlock/bpf_run.c
 create mode 100644 security/landlock/bpf_run.h
 create mode 100644 security/landlock/bpf_verify.c
 create mode 100644 security/landlock/common.h
 create mode 100644 security/landlock/domain_manage.c
 create mode 100644 security/landlock/domain_manage.h
 create mode 100644 security/landlock/domain_syscall.c
 create mode 100644 security/landlock/hooks_cred.c
 create mode 100644 security/landlock/hooks_cred.h
 create mode 100644 security/landlock/hooks_ptrace.c
 create mode 100644 security/landlock/hooks_ptrace.h
 create mode 100644 security/landlock/init.c
 create mode 100644 tools/include/uapi/linux/landlock.h
 create mode 100644 tools/testing/selftests/bpf/verifier/landlock.c
 create mode 100644 tools/testing/selftests/landlock/.gitignore
 create mode 100644 tools/testing/selftests/landlock/Makefile
 create mode 100644 tools/testing/selftests/landlock/config
 create mode 100644 tools/testing/selftests/landlock/test.h
 create mode 100644 tools/testing/selftests/landlock/test_base.c
 create mode 100644 tools/testing/selftests/landlock/test_ptrace.c
Signature · 1 line
2.23.0
author maintainer
#1 lore raw

PATCH v13 1/7 bpf,landlock: Define an eBPF program type for Landlock hooks

Add a new type of eBPF program used by Landlock hooks. The goal of this type of program is to accept or deny a requested access from userspace to a kernel object (e.g. ptrace a process). This will be more useful with the next commit adding a new eBPF helper.

The context of this program type contains two items of type PTR_TO_TASK, one for the tracer and one for the tracee. The underlying kernel structure is currently a task_struct pointer, but it could seamlessly evolve to a task wrapper with dedicated rights (i.e. capability-based security) to fit with different use cases (e.g. get and log the task's PID).

This new BPF program type will be registered with the Landlock LSM initialization.

Add an initial Landlock Kconfig and update the MAINTAINERS file.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v10:
* replace file system program types with a (simpler) ptrace program type
* add an eBPF task pointer type
* split files

Changes since v9:
* handle inode put and map put, which fix unmount (reported by Al Viro)
* replace subtype with expected_attach_type and expected_attach_triggers
* check eBPF program return code

Changes since v8:
* Remove the chaining concept from the eBPF program contexts (chain and cookie). We need to keep these subtypes this way to be able to make them evolve, though.
* remove bpf_landlock_put_extra() because there is no more a "previous" field to free (for now)

Changes since v7:
* cosmetic fixes
* rename LANDLOCK_SUBTYPE_* to LANDLOCK_*
* cleanup UAPI definitions and move them from bpf.h to landlock.h (suggested by Alexei Starovoitov)
* disable Landlock by default (suggested by Alexei Starovoitov)
* rename BPF_PROG_TYPE_LANDLOCK_{RULE,HOOK}
* update the Kconfig
* update the MAINTAINERS file
* replace the IOCTL, LOCK and FCNTL events with FS_PICK, FS_WALK and FS_GET hook types
* add the ability to chain programs with an eBPF program file descriptor (i.e. the "previous" field in a Landlock subtype) and keep a state with a "cookie" value available from the context
* add a "triggers" subtype bitfield to match specific actions (e.g. append, chdir, read...)

Changes since v6:
* add 3 more sub-events: IOCTL, LOCK, FCNTL
https://lkml.kernel.org/r/[email protected]
* rename LANDLOCK_VERSION to LANDLOCK_ABI to better reflect its purpose, and move it from landlock.h to common.h
* rename BPF_PROG_TYPE_LANDLOCK to BPF_PROG_TYPE_LANDLOCK_RULE: an eBPF program could be used for something else than a rule
* simplify struct landlock_context by removing the arch and syscall_nr fields
* remove all eBPF map functions call, remove ABILITY_WRITE
* refactor bpf_landlock_func_proto() (suggested by Kees Cook)
* constify pointers
* fix doc inclusion

Changes since v5:
* rename file hooks.c to init.c
* fix spelling

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

---
 MAINTAINERS                    |  8 ++++
 include/linux/bpf.h            |  1 +
 include/linux/bpf_types.h      |  3 ++
 include/uapi/linux/bpf.h       |  2 +
 include/uapi/linux/landlock.h  | 39 ++++++++++++++++
 kernel/bpf/syscall.c           |  9 ++++
 kernel/bpf/verifier.c          |  7 +++
 security/Kconfig               |  1 +
 security/Makefile              |  2 +
 security/landlock/Kconfig      | 19 ++++++++
 security/landlock/Makefile     |  4 ++
 security/landlock/bpf_ptrace.c | 30 ++++++++++++
 security/landlock/bpf_ptrace.h | 17 +++++++
 security/landlock/bpf_verify.c | 83 ++++++++++++++++++++++++++++++++++
 security/landlock/common.h     | 30 ++++++++++++
 15 files changed, 255 insertions(+)
 create mode 100644 include/uapi/linux/landlock.h
 create mode 100644 security/landlock/Kconfig
 create mode 100644 security/landlock/Makefile
 create mode 100644 security/landlock/bpf_ptrace.c
 create mode 100644 security/landlock/bpf_ptrace.h
 create mode 100644 security/landlock/bpf_verify.c
 create mode 100644 security/landlock/common.h
MAINTAINERS +8 -0
index 7fc074632eac..4cabb85ea52d 100644
@@ -9146,6 +9146,14 @@ F:	net/core/skmsg.c
 F:	net/core/sock_map.c
 F:	net/ipv4/tcp_bpf.c
 
+LANDLOCK SECURITY MODULE
+M:	Mickaël Salaün <[email protected]>
+S:	Supported
+F:	include/uapi/linux/landlock.h
+F:	security/landlock/
+K:	landlock
+K:	LANDLOCK
+
 LANTIQ / INTEL Ethernet drivers
 M:	Hauke Mehrtens <[email protected]>
 L:	[email protected]
include/linux/bpf.h +1 -0
index 171be30fe0ae..819a3e207438 100644
@@ -291,6 +291,7 @@ enum bpf_reg_type {
 	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */
 	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */
 	PTR_TO_BTF_ID,		 /* reg points to kernel struct */
+	PTR_TO_TASK,		 /* reg points to struct task_struct */
 };
 
 /* The information passed from prog-specific *_is_valid_access
include/linux/bpf_types.h +3 -0
index 36a9c2325176..bddabc961a3b 100644
@@ -38,6 +38,9 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_LIRC_MODE2, lirc_mode2)
 #ifdef CONFIG_INET
 BPF_PROG_TYPE(BPF_PROG_TYPE_SK_REUSEPORT, sk_reuseport)
 #endif
+#ifdef CONFIG_SECURITY_LANDLOCK
+BPF_PROG_TYPE(BPF_PROG_TYPE_LANDLOCK_HOOK, landlock)
+#endif
 
 BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
include/uapi/linux/bpf.h +2 -0
index 4af8b0819a32..6e4147790f96 100644
@@ -173,6 +173,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_CGROUP_SYSCTL,
 	BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
 	BPF_PROG_TYPE_CGROUP_SOCKOPT,
+	BPF_PROG_TYPE_LANDLOCK_HOOK,
 };
 
 enum bpf_attach_type {
@@ -199,6 +200,7 @@ enum bpf_attach_type {
 	BPF_CGROUP_UDP6_RECVMSG,
 	BPF_CGROUP_GETSOCKOPT,
 	BPF_CGROUP_SETSOCKOPT,
+	BPF_LANDLOCK_PTRACE,
 	__MAX_BPF_ATTACH_TYPE
 };
 
include/uapi/linux/landlock.h new file +39 -0
new file mode 100644
index 000000000000..3ffe3cbdbad6
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Landlock - UAPI headers
+ *
+ * Copyright © 2017-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#ifndef _UAPI__LINUX_LANDLOCK_H__
+#define _UAPI__LINUX_LANDLOCK_H__
+
+#include <linux/types.h>
+
+/**
+ * DOC: landlock_ret
+ *
+ * The return value of a landlock program is a bitmask that can allow or deny
+ * the action for which the program is run.
+ *
+ * In the future, this could be used to trigger an audit event as well.
+ *
+ * - %LANDLOCK_RET_ALLOW
+ * - %LANDLOCK_RET_DENY
+ */
+#define LANDLOCK_RET_ALLOW	0
+#define LANDLOCK_RET_DENY	1
+
+/**
+ * struct landlock_context_ptrace - context accessible to BPF_LANDLOCK_PTRACE
+ *
+ * @tracer: pointer to the task requesting to debug @tracee
+ * @tracee: pointer to the task being debugged
+ */
+struct landlock_context_ptrace {
+	__u64 tracer;
+	__u64 tracee;
+};
+
+#endif /* _UAPI__LINUX_LANDLOCK_H__ */
kernel/bpf/syscall.c +9 -0
index ff5225759553..5159e582a0d8 100644
@@ -1621,6 +1621,15 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
 		default:
 			return -EINVAL;
 		}
+#ifdef CONFIG_SECURITY_LANDLOCK
+	case BPF_PROG_TYPE_LANDLOCK_HOOK:
+		switch (expected_attach_type) {
+		case BPF_LANDLOCK_PTRACE:
+			return 0;
+		default:
+			return -EINVAL;
+		}
+#endif
 	default:
 		return 0;
 	}
kernel/bpf/verifier.c +7 -0
index c59778c0fc4d..ebf1991906b7 100644
@@ -421,6 +421,7 @@ static const char * const reg_type_str[] = {
 	[PTR_TO_TP_BUFFER]	= "tp_buffer",
 	[PTR_TO_XDP_SOCK]	= "xdp_sock",
 	[PTR_TO_BTF_ID]		= "ptr_",
+	[PTR_TO_TASK]		= "task",
 };
 
 static char slot_type_char[] = {
@@ -1878,6 +1879,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)
 	case PTR_TO_TCP_SOCK:
 	case PTR_TO_TCP_SOCK_OR_NULL:
 	case PTR_TO_XDP_SOCK:
+	case PTR_TO_TASK:
 		return true;
 	default:
 		return false;
@@ -2600,6 +2602,9 @@ static int check_ptr_alignment(struct bpf_verifier_env *env,
 	case PTR_TO_XDP_SOCK:
 		pointer_desc = "xdp_sock ";
 		break;
+	case PTR_TO_TASK:
+		pointer_desc = "task ";
+		break;
 	default:
 		break;
 	}
@@ -4527,6 +4532,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 	case PTR_TO_TCP_SOCK:
 	case PTR_TO_TCP_SOCK_OR_NULL:
 	case PTR_TO_XDP_SOCK:
+	case PTR_TO_TASK:
 		verbose(env, "R%d pointer arithmetic on %s prohibited\n",
 			dst, reg_type_str[ptr_reg->type]);
 		return -EACCES;
@@ -6278,6 +6284,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 	case BPF_PROG_TYPE_CGROUP_DEVICE:
 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
+	case BPF_PROG_TYPE_LANDLOCK_HOOK:
 		break;
 	default:
 		return 0;
security/Kconfig +1 -0
index 2a1a2d396228..9d9981394fb0 100644
@@ -238,6 +238,7 @@ source "security/loadpin/Kconfig"
 source "security/yama/Kconfig"
 source "security/safesetid/Kconfig"
 source "security/lockdown/Kconfig"
+source "security/landlock/Kconfig"
 
 source "security/integrity/Kconfig"
 
security/Makefile +2 -0
index be1dd9d2cb2f..60b7f6f2fd30 100644
@@ -12,6 +12,7 @@ subdir-$(CONFIG_SECURITY_YAMA)		+= yama
 subdir-$(CONFIG_SECURITY_LOADPIN)	+= loadpin
 subdir-$(CONFIG_SECURITY_SAFESETID)    += safesetid
 subdir-$(CONFIG_SECURITY_LOCKDOWN_LSM)	+= lockdown
+subdir-$(CONFIG_SECURITY_LANDLOCK)		+= landlock
 
 # always enable default capabilities
 obj-y					+= commoncap.o
@@ -29,6 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA)		+= yama/
 obj-$(CONFIG_SECURITY_LOADPIN)		+= loadpin/
 obj-$(CONFIG_SECURITY_SAFESETID)       += safesetid/
 obj-$(CONFIG_SECURITY_LOCKDOWN_LSM)	+= lockdown/
+obj-$(CONFIG_SECURITY_LANDLOCK)	+= landlock/
 obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
 
 # Object integrity file lists
security/landlock/Kconfig new file +19 -0
new file mode 100644
index 000000000000..44921bd72380
@@ -0,0 +1,19 @@
+config SECURITY_LANDLOCK
+	bool "Landlock support"
+	depends on SECURITY
+	depends on BPF_SYSCALL
+	depends on SECCOMP_FILTER
+	default n
+	help
+	  This selects Landlock, a programmatic access control.  It enables to
+	  restrict processes on the fly (i.e. create a sandbox) or log some
+	  actions.  The security policy is a set of eBPF programs, dedicated to
+	  allow or deny a list of actions on specific kernel objects (e.g.
+	  process).
+
+	  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 N.
security/landlock/Makefile new file +4 -0
new file mode 100644
index 000000000000..682b798c6b76
@@ -0,0 +1,4 @@
+obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o
+
+landlock-y := \
+	bpf_verify.o bpf_ptrace.o
security/landlock/bpf_ptrace.c new file +30 -0
new file mode 100644
index 000000000000..2ec73078ad01
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Landlock LSM - eBPF ptrace
+ *
+ * Copyright © 2017-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2019 ANSSI
+ */
+
+#include <linux/bpf.h>
+#include <uapi/linux/landlock.h>
+
+#include "bpf_ptrace.h"
+
+bool landlock_is_valid_access_ptrace(int off, enum bpf_access_type type,
+		enum bpf_reg_type *reg_type, int *max_size)
+{
+	if (type != BPF_READ)
+		return false;
+
+	switch (off) {
+	case offsetof(struct landlock_context_ptrace, tracer):
+		/* fall through */
+	case offsetof(struct landlock_context_ptrace, tracee):
+		*reg_type = PTR_TO_TASK;
+		*max_size = sizeof(u64);
+		return true;
+	default:
+		return false;
+	}
+}
security/landlock/bpf_ptrace.h new file +17 -0
new file mode 100644
index 000000000000..85edce37b70a
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - eBPF ptrace headers
+ *
+ * Copyright © 2017-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2019 ANSSI
+ */
+
+#ifndef _SECURITY_LANDLOCK_BPF_PTRACE_H
+#define _SECURITY_LANDLOCK_BPF_PTRACE_H
+
+#include <linux/bpf.h>
+
+bool landlock_is_valid_access_ptrace(int off, enum bpf_access_type type,
+		enum bpf_reg_type *reg_type, int *max_size);
+
+#endif /* _SECURITY_LANDLOCK_BPF_PTRACE_H */
security/landlock/bpf_verify.c new file +83 -0

83 lines not on this page · raw message

security/landlock/common.h new file +30 -0
new file mode 100644
index 000000000000..0234c4bc4acd
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - private headers
+ *
+ * Copyright © 2016-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#ifndef _SECURITY_LANDLOCK_COMMON_H
+#define _SECURITY_LANDLOCK_COMMON_H
+
+#include <linux/bpf.h>
+#include <linux/filter.h>
+
+enum landlock_hook_type {
+	LANDLOCK_HOOK_PTRACE = 1,
+};
+
+static inline enum landlock_hook_type get_hook_type(const struct bpf_prog *prog)
+{
+	switch (prog->expected_attach_type) {
+	case BPF_LANDLOCK_PTRACE:
+		return LANDLOCK_HOOK_PTRACE;
+	default:
+		WARN_ON(1);
+		return BPF_LANDLOCK_PTRACE;
+	}
+}
+
+#endif /* _SECURITY_LANDLOCK_COMMON_H */
Signature · 1 line
2.23.0
author maintainer
#2 lore raw

PATCH v13 2/7 landlock: Add the management of domains

In reply to Mickaël Salaün

A Landlock domain is a set of eBPF programs. There is a list for each different program types that can be run on a specific Landlock hook (e.g. ptrace). A domain is tied to a set of subjects (i.e. tasks). A Landlock program should not try (nor be able) to infer which subject is currently enforced, but to have a unique security policy for all subjects tied to the same domain. This make the reasoning much easier and help avoid pitfalls.

The next commits tie a domain to a task's credentials thanks to seccomp(2), but we could use cgroups or a security file-system to enforce a sysadmin-defined policy .

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v12:
* move the landlock_put_domain() call from landlock_prepend_prog() to domain_syscall.c:landlock_seccomp_prepend_prog() (next commit): keep the same semantic but don't conditionally own the domain (safeguard domain owning)
* always copy a domain to-be-modified which guarantee domain
immutability: the callers of landlock_prepend_prog() are now fully in charge of concurrency handling, which is currently only handled by prepare_creds() and commit_creds() (next commit)
* simplify landlock_prepend_prog()
* use a consistent naming

Changes since v11:
* remove old code from previous refactoring (removing the program chaining concept) and simplify program prepending (reported by Serge E. Hallyn):
* simplify landlock_prepend_prog() and merge it with store_landlock_prog()
* add new_prog_list() and rework new_landlock_domain()
* remove the extra page allocation checks, only rely on the eBPF program checks
* replace the -EINVAL for the duplicate program check with the -EEXIST

Changes since v10:
* rename files and names to clearly define a domain
* create a standalone patch to ease review

---
 security/landlock/Makefile        |   3 +-
 security/landlock/common.h        |  38 ++++++++
 security/landlock/domain_manage.c | 152 ++++++++++++++++++++++++++++++
 security/landlock/domain_manage.h |  22 +++++
 4 files changed, 214 insertions(+), 1 deletion(-)
 create mode 100644 security/landlock/domain_manage.c
 create mode 100644 security/landlock/domain_manage.h
security/landlock/Makefile +2 -1
index 682b798c6b76..dd5f70185778 100644
@@ -1,4 +1,5 @@
 obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o
 
 landlock-y := \
-	bpf_verify.o bpf_ptrace.o
+	bpf_verify.o bpf_ptrace.o \
+	domain_manage.o
security/landlock/common.h +38 -0
index 0234c4bc4acd..fb2990eb5fb4 100644
@@ -11,11 +11,49 @@
 
 #include <linux/bpf.h>
 #include <linux/filter.h>
+#include <linux/refcount.h>
 
 enum landlock_hook_type {
 	LANDLOCK_HOOK_PTRACE = 1,
 };
 
+#define _LANDLOCK_HOOK_LAST LANDLOCK_HOOK_PTRACE
+
+struct landlock_prog_list {
+	struct landlock_prog_list *prev;
+	struct bpf_prog *prog;
+	refcount_t usage;
+};
+
+/**
+ * struct landlock_domain - Landlock programs enforced on a set of tasks
+ *
+ * When prepending a new program, if &struct landlock_domain is shared with
+ * other tasks, then duplicate it and prepend the program to this new &struct
+ * landlock_domain.
+ *
+ * @usage: reference count to manage the object lifetime. When a task needs to
+ *	   add Landlock programs and if @usage is greater than 1, then the
+ *	   task must duplicate &struct landlock_domain to not change the
+ *	   children's programs as well.
+ * @programs: array of non-NULL &struct landlock_prog_list pointers
+ */
+struct landlock_domain {
+	struct landlock_prog_list *programs[_LANDLOCK_HOOK_LAST];
+	refcount_t usage;
+};
+
+/**
+ * get_hook_index - get an index for the programs of struct landlock_prog_set
+ *
+ * @type: a Landlock hook type
+ */
+static inline size_t get_hook_index(enum landlock_hook_type type)
+{
+	/* type ID > 0 for loaded programs */
+	return type - 1;
+}
+
 static inline enum landlock_hook_type get_hook_type(const struct bpf_prog *prog)
 {
 	switch (prog->expected_attach_type) {
security/landlock/domain_manage.c new file +152 -0

152 lines not on this page · raw message

security/landlock/domain_manage.h new file +22 -0
new file mode 100644
index 000000000000..d32c65c941c0
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - domain management headers
+ *
+ * Copyright © 2016-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#ifndef _SECURITY_LANDLOCK_DOMAIN_MANAGE_H
+#define _SECURITY_LANDLOCK_DOMAIN_MANAGE_H
+
+#include <linux/filter.h>
+
+#include "common.h"
+
+void landlock_get_domain(struct landlock_domain *domain);
+void landlock_put_domain(struct landlock_domain *domain);
+
+struct landlock_domain *landlock_prepend_prog(struct landlock_domain *domain,
+		struct bpf_prog *prog);
+
+#endif /* _SECURITY_LANDLOCK_DOMAIN_MANAGE_H */
Signature · 1 line
2.23.0
author maintainer
#3 lore raw

PATCH v13 3/7 landlock,seccomp: Apply Landlock programs to process hierarchy

In reply to Mickaël Salaün

The seccomp(2) syscall can be used by a task to apply a Landlock program to itself. As a seccomp filter, a Landlock program is enforced for the current task and all its future children. A program is immutable and a task can only add new restricting programs to itself, forming a list of programs.

A Landlock program is tied to a Landlock hook. If the action on a kernel object is allowed by the other Linux security mechanisms (e.g. DAC, capabilities, other LSM), then a Landlock hook related to this kind of object is triggered. The list of programs for this hook is then evaluated. Each program return a binary value which can deny the action on a kernel object with a non-zero value. If every programs of the list return zero, then the action on the object is allowed.

The next commit adds the LSM hooks to enforce the memory protection programs on the appropriate process hierarchies.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v12:
* move the landlock_put_domain() call from domain_manage.c: same semantic (for this call) but less error-prone and self-explanatory (only put a domain when it is effectively replaced)
* only handle copy of domains (cf. domain management changes)
* use a consistent naming
* extend comment about unprivileged enforcement

Changes since v11:
* fix build of seccomp without Landlock (reported by kbuild test robot)

Changes since v10:
* rewrite the Landlock program attaching mechanisme to not rely on internal seccomp structures but only on the (LSM-stacked) task's credentials:
* make the use of seccomp (and task's credentials) optional if not relying on its syscall, which may be useful for domains defined by other means (e.g. cgroups or system-wide thanks to a dedicated securityfs)

Changes since v9:
* replace subtype with expected_attach_type and expected_attach_triggers

Changes since v8:
* Remove the chaining concept from the eBPF program contexts (chain and cookie). We need to keep these subtypes this way to be able to make them evolve, though.

Changes since v7:
* handle and verify program chains
* split and rename providers.c to enforce.c and enforce_seccomp.c
* rename LANDLOCK_SUBTYPE_* to LANDLOCK_*

Changes since v6:
* rename some functions with more accurate names to reflect that an eBPF program for Landlock could be used for something else than a rule
* reword rule "appending" to "prepending" and explain it
* remove the superfluous no_new_privs check, only check global CAP_SYS_ADMIN when prepending a Landlock rule (needed for containers)
* create and use {get,put}_seccomp_landlock() (suggested by Kees Cook)
* replace ifdef with static inlined function (suggested by Kees Cook)
* use get_user() (suggested by Kees Cook)
* replace atomic_t with refcount_t (requested by Kees Cook)
* move struct landlock_{rule,events} from landlock.h to common.h
* cleanup headers

Changes since v5:
* remove struct landlock_node and use a similar inheritance mechanisme as seccomp-bpf (requested by Andy Lutomirski)
* rename SECCOMP_ADD_LANDLOCK_RULE to SECCOMP_APPEND_LANDLOCK_RULE
* rename file manager.c to providers.c
* add comments
* typo and cosmetic fixes

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

---
 MAINTAINERS                        |  1 +
 include/linux/landlock.h           | 25 ++++++++
 include/linux/lsm_hooks.h          |  1 +
 include/uapi/linux/seccomp.h       |  1 +
 kernel/seccomp.c                   |  4 ++
 security/landlock/Makefile         |  5 +-
 security/landlock/common.h         | 16 +++++
 security/landlock/domain_syscall.c | 93 ++++++++++++++++++++++++++++++
 security/landlock/hooks_cred.c     | 47 +++++++++++++++
 security/landlock/hooks_cred.h     | 14 +++++
 security/landlock/init.c           | 30 ++++++++++
 security/security.c                | 15 +++++
 12 files changed, 250 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/landlock.h
 create mode 100644 security/landlock/domain_syscall.c
 create mode 100644 security/landlock/hooks_cred.c
 create mode 100644 security/landlock/hooks_cred.h
 create mode 100644 security/landlock/init.c
MAINTAINERS +1 -0
index 4cabb85ea52d..32bfd88159b0 100644
@@ -9149,6 +9149,7 @@ F:	net/ipv4/tcp_bpf.c
 LANDLOCK SECURITY MODULE
 M:	Mickaël Salaün <[email protected]>
 S:	Supported
+F:	include/linux/landlock.h
 F:	include/uapi/linux/landlock.h
 F:	security/landlock/
 K:	landlock
include/linux/landlock.h new file +25 -0
new file mode 100644
index 000000000000..ffbf2397c459
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Landlock LSM - public kernel headers
+ *
+ * Copyright © 2016-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#ifndef _LINUX_LANDLOCK_H
+#define _LINUX_LANDLOCK_H
+
+#include <linux/errno.h>
+
+#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_SECURITY_LANDLOCK)
+extern int landlock_seccomp_prepend_prog(unsigned int flags,
+		const int __user *user_bpf_fd);
+#else /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */
+static inline int landlock_seccomp_prepend_prog(unsigned int flags,
+		const int __user *user_bpf_fd)
+{
+		return -EINVAL;
+}
+#endif /* CONFIG_SECCOMP_FILTER && CONFIG_SECURITY_LANDLOCK */
+
+#endif /* _LINUX_LANDLOCK_H */
include/linux/lsm_hooks.h +1 -0
index a3763247547c..a8ba679b388a 100644
@@ -2106,6 +2106,7 @@ extern void security_add_hooks(struct security_hook_list *hooks, int count,
 enum lsm_order {
 	LSM_ORDER_FIRST = -1,	/* This is only for capabilities. */
 	LSM_ORDER_MUTABLE = 0,
+	LSM_ORDER_LAST = 1,	/* potentially-unprivileged LSM */
 };
 
 struct lsm_info {
include/uapi/linux/seccomp.h +1 -0
index 90734aa5aa36..bce6534e7feb 100644
@@ -16,6 +16,7 @@
 #define SECCOMP_SET_MODE_FILTER		1
 #define SECCOMP_GET_ACTION_AVAIL	2
 #define SECCOMP_GET_NOTIF_SIZES		3
+#define SECCOMP_PREPEND_LANDLOCK_PROG	4
 
 /* Valid flags for SECCOMP_SET_MODE_FILTER */
 #define SECCOMP_FILTER_FLAG_TSYNC		(1UL << 0)
kernel/seccomp.c +4 -0
index dba52a7db5e8..0688a1439587 100644
@@ -19,6 +19,7 @@
 #include <linux/compat.h>
 #include <linux/coredump.h>
 #include <linux/kmemleak.h>
+#include <linux/landlock.h>
 #include <linux/nospec.h>
 #include <linux/prctl.h>
 #include <linux/sched.h>
@@ -1397,6 +1398,9 @@ static long do_seccomp(unsigned int op, unsigned int flags,
 			return -EINVAL;
 
 		return seccomp_get_notif_sizes(uargs);
+	case SECCOMP_PREPEND_LANDLOCK_PROG:
+		return landlock_seccomp_prepend_prog(flags,
+				(const int __user *)uargs);
 	default:
 		return -EINVAL;
 	}
security/landlock/Makefile +3 -2
index dd5f70185778..0b291f2c027c 100644
@@ -1,5 +1,6 @@
 obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o
 
-landlock-y := \
+landlock-y := init.o \
 	bpf_verify.o bpf_ptrace.o \
-	domain_manage.o
+	domain_manage.o domain_syscall.o \
+	hooks_cred.o
security/landlock/common.h +16 -0
index fb2990eb5fb4..3ae8340a5b3d 100644
@@ -10,9 +10,15 @@
 #define _SECURITY_LANDLOCK_COMMON_H
 
 #include <linux/bpf.h>
+#include <linux/cred.h>
 #include <linux/filter.h>
+#include <linux/lsm_hooks.h>
 #include <linux/refcount.h>
 
+#define LANDLOCK_NAME "landlock"
+
+extern struct lsm_blob_sizes landlock_blob_sizes;
+
 enum landlock_hook_type {
 	LANDLOCK_HOOK_PTRACE = 1,
 };
@@ -43,6 +49,16 @@ struct landlock_domain {
 	refcount_t usage;
 };
 
+struct landlock_cred_security {
+	struct landlock_domain *domain;
+};
+
+static inline struct landlock_cred_security *landlock_cred(
+		const struct cred *cred)
+{
+	return cred->security + landlock_blob_sizes.lbs_cred;
+}
+
 /**
  * get_hook_index - get an index for the programs of struct landlock_prog_set
  *
security/landlock/domain_syscall.c new file +93 -0

93 lines not on this page · raw message

security/landlock/hooks_cred.c new file +47 -0

47 lines not on this page · raw message

security/landlock/hooks_cred.h new file +14 -0
new file mode 100644
index 000000000000..641d66f6bf9a
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - credential hooks headers
+ *
+ * Copyright © 2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2019 ANSSI
+ */
+
+#ifndef _SECURITY_LANDLOCK_HOOKS_CRED_H
+#define _SECURITY_LANDLOCK_HOOKS_CRED_H
+
+__init void landlock_add_hooks_cred(void);
+
+#endif /* _SECURITY_LANDLOCK_HOOKS_CRED_H */
security/landlock/init.c new file +30 -0
new file mode 100644
index 000000000000..8836ec4defd3
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Landlock LSM - initialization
+ *
+ * Copyright © 2016-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#include <linux/lsm_hooks.h>
+
+#include "common.h"
+#include "hooks_cred.h"
+
+static int __init landlock_init(void)
+{
+	pr_info(LANDLOCK_NAME ": Registering hooks\n");
+	landlock_add_hooks_cred();
+	return 0;
+}
+
+struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = {
+	.lbs_cred = sizeof(struct landlock_cred_security),
+};
+
+DEFINE_LSM(LANDLOCK_NAME) = {
+	.name = LANDLOCK_NAME,
+	.order = LSM_ORDER_LAST,
+	.blobs = &landlock_blob_sizes,
+	.init = landlock_init,
+};
security/security.c +15 -0
index 1bc000f834e2..03c7dce9e014 100644
@@ -264,6 +264,21 @@ static void __init ordered_lsm_parse(const char *order, const char *origin)
 		}
 	}
 
+	/*
+	 * In case of an unprivileged access-control, we don't want to give the
+	 * ability to any process to do some checks (e.g. through an eBPF
+	 * program) on kernel objects (e.g. files) if a privileged security
+	 * policy forbid their access.  We must then load
+	 * potentially-unprivileged security modules after all other LSMs.
+	 *
+	 * LSM_ORDER_LAST is always last and does not appear in the modifiable
+	 * ordered list of enabled LSMs.
+	 */
+	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
+		if (lsm->order == LSM_ORDER_LAST)
+			append_ordered_lsm(lsm, "last");
+	}
+
 	/* Disable all LSMs not in the ordered list. */
 	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
 		if (exists_ordered_lsm(lsm))
Signature · 1 line
2.23.0
author maintainer
#4 lore raw

PATCH v13 4/7 landlock: Add ptrace LSM hooks

In reply to Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v12:
* ensure preemption is disabled before running BPF programs, cf. commit 568f196756ad ("bpf: check that BPF programs run with preemption disabled")

Changes since v10:
* revamp and replace the static policy with a Landlock hook which may be used by the corresponding BPF_LANDLOCK_PTRACE program (attach) type and a dedicated process_cmp_landlock_ptrace() BPF helper
* check prog return value against LANDLOCK_RET_DENY (ret is a bitmask)

Changes since v6:
* factor out ptrace check
* constify pointers
* cleanup headers
* use the new security_add_hooks()

---
 security/landlock/Makefile       |   4 +-
 security/landlock/bpf_run.c      |  65 ++++++++++++++++++
 security/landlock/bpf_run.h      |  25 +++++++
 security/landlock/hooks_ptrace.c | 114 +++++++++++++++++++++++++++++++
 security/landlock/hooks_ptrace.h |  19 ++++++
 security/landlock/init.c         |   2 +
 6 files changed, 227 insertions(+), 2 deletions(-)
 create mode 100644 security/landlock/bpf_run.c
 create mode 100644 security/landlock/bpf_run.h
 create mode 100644 security/landlock/hooks_ptrace.c
 create mode 100644 security/landlock/hooks_ptrace.h
security/landlock/Makefile +2 -2
index 0b291f2c027c..93e4c2f31c8a 100644
@@ -1,6 +1,6 @@
 obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o
 
 landlock-y := init.o \
-	bpf_verify.o bpf_ptrace.o \
+	bpf_verify.o bpf_run.o bpf_ptrace.o \
 	domain_manage.o domain_syscall.o \
-	hooks_cred.o
+	hooks_cred.o hooks_ptrace.o
security/landlock/bpf_run.c new file +65 -0

65 lines not on this page · raw message

security/landlock/bpf_run.h new file +25 -0
new file mode 100644
index 000000000000..3461cbb8ec12
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - eBPF program evaluation headers
+ *
+ * Copyright © 2016-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#ifndef _SECURITY_LANDLOCK_BPF_RUN_H
+#define _SECURITY_LANDLOCK_BPF_RUN_H
+
+#include "common.h"
+#include "hooks_ptrace.h"
+
+struct landlock_hook_ctx {
+	enum landlock_hook_type type;
+	union {
+		struct landlock_hook_ctx_ptrace *ctx_ptrace;
+	};
+};
+
+bool landlock_access_denied(struct landlock_domain *domain,
+		struct landlock_hook_ctx *hook_ctx);
+
+#endif /* _SECURITY_LANDLOCK_BPF_RUN_H */
security/landlock/hooks_ptrace.c new file +114 -0

114 lines not on this page · raw message

security/landlock/hooks_ptrace.h new file +19 -0
new file mode 100644
index 000000000000..53fe651bdb3e
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - ptrace hooks headers
+ *
+ * Copyright © 2017-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2019 ANSSI
+ */
+
+#ifndef _SECURITY_LANDLOCK_HOOKS_PTRACE_H
+#define _SECURITY_LANDLOCK_HOOKS_PTRACE_H
+
+struct landlock_hook_ctx_ptrace;
+
+const struct landlock_context_ptrace *landlock_get_ctx_ptrace(
+		const struct landlock_hook_ctx_ptrace *hook_ctx);
+
+__init void landlock_add_hooks_ptrace(void);
+
+#endif /* _SECURITY_LANDLOCK_HOOKS_PTRACE_H */
security/landlock/init.c +2 -0
index 8836ec4defd3..541aad17418e 100644
@@ -10,11 +10,13 @@
 
 #include "common.h"
 #include "hooks_cred.h"
+#include "hooks_ptrace.h"
 
 static int __init landlock_init(void)
 {
 	pr_info(LANDLOCK_NAME ": Registering hooks\n");
 	landlock_add_hooks_cred();
+	landlock_add_hooks_ptrace();
 	return 0;
 }
 
Signature · 1 line
2.23.0
#5 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases. BPF is capable to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

#6 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 26 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 3 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases. BPF is capable to be a superset of all existing LSMs

I can't agree with this. Nope. There are many security models for which BPF introduces excessive complexity. You don't need or want the generality of a general purpose programming language to implement Smack or TOMOYO. Or a simple Bell & LaPadula for that matter. SELinux? I can't imagine anyone trying to do that in eBPF, although I'm willing to be surprised. Being able to enforce a policy isn't the only criteria for an LSM. It's got to perform well and integrate with the rest of the system. I see many issues with a BPF <-> vfs interface.

Quoted 2 lines

whereas landlock and KRSI propsals today are custom solutions to specific security concerns.

Yes. As they should be. No one has every solved the entire security problem, and no one ever will. The only hope we have to address security issues is to have the flexibility to add the mechanisms needed for the concerns of the day. Ideally, we should be able to drop mechanisms when we decide that they no longer add value.

Quoted 6 lines

BPF subsystem was extended
with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

I don't get your analogy at all. You have a variety of programs because you have a variety of protocols and administrative interfaces. Of course you don't have a single 'networking" program. Security has a variety of issues and policies. A single 'security' program makes no sense whatever.

#7 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 31 lines · Casey Schaufler
Quoted 26 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 3 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases. BPF is capable to be a superset of all existing LSMs

Quoted 7 lines

I can't agree with this. Nope. There are many security models for which BPF introduces excessive complexity. You don't need or want the generality of a general purpose programming language to implement Smack or TOMOYO. Or a simple Bell & LaPadula for that matter. SELinux? I can't imagine anyone trying to do that in eBPF, although I'm willing to be surprised. Being able to enforce a policy isn't the only criteria for an LSM.

what are the other criteria?

Quoted 2 lines

It's got
to perform well and integrate with the rest of the system.

what do you mean by that?

Quoted 1 line

I see many issues with a BPF <-> vfs interface.

There is no such interface today. What do you have in mind?

Quoted 3 lines

the mechanisms needed for the concerns of the day. Ideally, we should be able to drop mechanisms when we decide that they no longer add value.

Exactly. bpf-based lsm must not add to kernel abi.

#8 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 31 lines · Alexei Starovoitov
Quoted 30 lines · Casey Schaufler
Quoted 29 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases. BPF is capable to be a superset of all existing LSMs

Quoted 8 lines
Quoted 7 lines

I can't agree with this. Nope. There are many security models for which BPF introduces excessive complexity. You don't need or want the generality of a general purpose programming language to implement Smack or TOMOYO. Or a simple Bell & LaPadula for that matter. SELinux? I can't imagine anyone trying to do that in eBPF, although I'm willing to be surprised. Being able to enforce a policy isn't the only criteria for an LSM.

what are the other criteria?

They include, but are not limited to, performance impact and the ability to be analyzed. The interactions with other subsystems meeting the requirements thereof is always a concern.

Quoted 4 lines
Quoted 3 lines

It's got
to perform well and integrate with the rest of the system.

what do you mean by that?

It has to be fast, or the networking people are going to have fits. You can't require the addition of a pointer into the skb because it'll get rejected out of hand. You can't completely refactor the vfs locking to accommodate you needs.

Quoted 3 lines
Quoted 2 lines

I see many issues with a BPF <-> vfs interface.

There is no such interface today. What do you have in mind?

You can't implement SELinux or Smack using BPF without a way to manipulate inode data.

Quoted 5 lines
Quoted 4 lines

the mechanisms needed for the concerns of the day. Ideally, we should be able to drop mechanisms when we decide that they no longer add value.

Exactly. bpf-based lsm must not add to kernel abi.

Huh? I have no idea where that came from.

#9 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 39 lines · Casey Schaufler
Quoted 38 lines · Alexei Starovoitov
Quoted 37 lines · Casey Schaufler
Quoted 26 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 3 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases. BPF is capable to be a superset of all existing LSMs

I can't agree with this. Nope. There are many security models for which BPF introduces excessive complexity. You don't need or want the generality of a general purpose programming language to implement Smack or TOMOYO. Or a simple Bell & LaPadula for that matter. SELinux? I can't imagine anyone trying to do that in eBPF, although I'm willing to be surprised. Being able to enforce a policy isn't the only criteria for an LSM.

Quoted 4 lines
Quoted 1 line

what are the other criteria?

They include, but are not limited to, performance impact and the ability to be analyzed.

Right and BPF is the only thing that exists in the kernel where the verifier knows precisely the number of instructions the critical path through the program will take. Currently we don't quantify this cost for bpf helpers, but it's easy to add. Can you do this for smack? Can you tell upfront the longest execution time for all security rules?

Quoted 5 lines

It has to be fast, or the networking people are going to have fits. You can't require the addition of a pointer into the skb because it'll get rejected out of hand. You can't completely refactor the vfs locking to accommodate you needs.

I'm not sure why you got such impression. I'm not proposing to refactor vfs or add fields to skb. Once we have equivalent to smack policy implemented in bpf-based lsm let's do performance benchmarking and compare actual numbers instead of hypothesizing about them. Which policy do you think would be the most representative of smack use case?

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

I see many issues with a BPF <-> vfs interface.

There is no such interface today. What do you have in mind?

You can't implement SELinux or Smack using BPF without a way to manipulate inode data.

Are you talking about inode->i_security ? That's not manipulating inode data. It's attaching extra metadata to inode object without changing inode itself. BPF can do it already via hash maps. It's not as fast as direct pointer access, but for many use cases it's good enough. If it turns out to be a performance limiting factor we will accelerate it.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

the mechanisms needed for the concerns of the day. Ideally, we should be able to drop mechanisms when we decide that they no longer add value.

Exactly. bpf-based lsm must not add to kernel abi.

Huh? I have no idea where that came from.

It sounds to me that some folks in the community got wrong impression that anything that BPF accesses is magically turning that thing into stable kernel ABI. That is not true. BPF progs had access _all_ kernel data pointers and structures for years without turning the whole kernel into stable ABI. I want to make sure that this part is understood. This is also a requirement for bpf-based LSM. It must not make LSM hooks into stable ABI.

#10 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 40 lines · Alexei Starovoitov
Quoted 39 lines · Casey Schaufler
Quoted 38 lines · Alexei Starovoitov
Quoted 37 lines · Casey Schaufler
Quoted 26 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 3 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases. BPF is capable to be a superset of all existing LSMs

I can't agree with this. Nope. There are many security models for which BPF introduces excessive complexity. You don't need or want the generality of a general purpose programming language to implement Smack or TOMOYO. Or a simple Bell & LaPadula for that matter. SELinux? I can't imagine anyone trying to do that in eBPF, although I'm willing to be surprised. Being able to enforce a policy isn't the only criteria for an LSM.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

what are the other criteria?

They include, but are not limited to, performance impact and the ability to be analyzed.

Right and BPF is the only thing that exists in the kernel where the verifier knows precisely the number of instructions the critical path through the program will take. Currently we don't quantify this cost for bpf helpers, but it's easy to add. Can you do this for smack? Can you tell upfront the longest execution time for all security rules?

There's much more to analyze than number of instructions. There's also completion of policy enforcement. There are lots of tools for measuring performance within the kernel.

Quoted 7 lines
Quoted 5 lines

It has to be fast, or the networking people are going to have fits. You can't require the addition of a pointer into the skb because it'll get rejected out of hand. You can't completely refactor the vfs locking to accommodate you needs.

I'm not sure why you got such impression. I'm not proposing to refactor vfs or add fields to skb.

I'm not saying you did. Those are examples of things you would have trouble with.

Quoted 4 lines

Once we have equivalent to smack policy implemented in
bpf-based lsm let's do performance benchmarking and compare actual numbers instead of hypothesizing about them. Which policy do you think would be the most representative of smack use case?

The Tizen3 Three domain model will do just fine.
https://wiki.tizen.org/Security:SmackThreeDomainModel

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

I see many issues with a BPF <-> vfs interface.

There is no such interface today. What do you have in mind?

You can't implement SELinux or Smack using BPF without a way to manipulate inode data.

Are you talking about inode->i_security ? That's not manipulating inode data.

Poppycock.

Quoted 1 line

It's attaching extra metadata to inode object without changing inode itself.

Where I come from, we call that inode object data.

Quoted 1 line

BPF can do it already via hash maps. It's not as fast as direct pointer access,

Then you're not listening. Performance MATTERS!

Quoted 2 lines

but for many use cases it's good enough. If it turns out to be a performance limiting factor we will accelerate it.

How many times have I heard that bit of rubbish?
No. You can't start with a bad design and tweak it to acceptability later.

Quoted 12 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

the mechanisms needed for the concerns of the day. Ideally, we should be able to drop mechanisms when we decide that they no longer add value.

Exactly. bpf-based lsm must not add to kernel abi.

Huh? I have no idea where that came from.

It sounds to me that some folks in the community got wrong impression that anything that BPF accesses is magically turning that thing into stable kernel ABI. That is not true. BPF progs had access _all_ kernel data pointers and structures for years without turning the whole kernel into stable ABI. I want to make sure that this part is understood. This is also a requirement for bpf-based LSM. It must not make LSM hooks into stable ABI.

author maintainer
#11 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

In reply to Alexei Starovoitov
Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

Quoted 13 lines

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

How could you used these pointers without dedicated BPF helpers? This context items are typed as PTR_TO_TASK and can't be used without a dedicated helper able to deal with ARG_PTR_TO_TASK. Moreover, pointer arithmetic is explicitly forbidden (and I added tests for that). Did I miss something?

Quoted 3 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

#12 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 29 lines · Mickaël Salaün
Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

Quoted 13 lines

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 5 lines

How could you used these pointers without dedicated BPF helpers? This context items are typed as PTR_TO_TASK and can't be used without a dedicated helper able to deal with ARG_PTR_TO_TASK. Moreover, pointer arithmetic is explicitly forbidden (and I added tests for that). Did I miss something?

It's a pointer leak.

Quoted 21 lines
Quoted 4 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

author maintainer
#13 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 30 lines · Alexei Starovoitov
Quoted 29 lines · Mickaël Salaün
Quoted 26 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 7 lines
Quoted 5 lines

How could you used these pointers without dedicated BPF helpers? This context items are typed as PTR_TO_TASK and can't be used without a dedicated helper able to deal with ARG_PTR_TO_TASK. Moreover, pointer arithmetic is explicitly forbidden (and I added tests for that). Did I miss something?

It's a pointer leak.

The lifetimes of the pointers are scoped by the two LSM hooks that expose them. The LSM framework guarantee that they are safe to use in this context.

Quoted 27 lines
Quoted 22 lines
Quoted 5 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

Quoted 4 lines

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

With all due respect, Landlock got much more feedback than KRSI and I think this thirteenth Landlock patch series is more mature than the first KRSI RFC. I'm open to concrete suggestions and I'm willing to collaborate with the KRSI folks if they want to. However, I'm OK if they don't want to use Landlock as a common ground, and I don't think it should be a blocker for any of the projects.

Perfect is the enemy of good. ;)

#14 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

In reply to Alexei Starovoitov
Quoted 64 lines · Alexei Starovoitov
Quoted 29 lines · Mickaël Salaün
Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

Quoted 13 lines

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

Quoted 5 lines

How could you used these pointers without dedicated BPF helpers? This context items are typed as PTR_TO_TASK and can't be used without a dedicated helper able to deal with ARG_PTR_TO_TASK. Moreover, pointer arithmetic is explicitly forbidden (and I added tests for that). Did I miss something?

It's a pointer leak.

Quoted 21 lines
Quoted 4 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

Quoted 4 lines

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

We are willing to collaborate with the Landlock developers and come up with a common approach that would work for Landlock and KRSI. I want to mention that this collaboration and the current Landlock approach of using an eBPF based LSM for unprivileged sandboxing only makes sense if unprivileged usage of eBPF is going to be ever allowed.

Purely from a technical standpoint, both the current designs for Landlock and KRSI target separate use cases and it would not be possible to build "one on top of the other". We've tried to identify the lowest denominator ("eBPF+LSM") requirements for both Landlock (unprivileged sandboxing / Discretionary Access Control) and KRSI (flexibility and unification of privileged MAC and Audit) and prototyped an implementation based on the newly added / upcoming features in BPF.

We've been successfully able to prototype the use cases for KRSI (privileged MAC and Audit) using this "eBPF+LSM" and shared our approach at the Linux Security Summit [1]:

* Use the new in-kernel BTF (CO-RE eBPF programs) [2] and the ability of the BPF verifier to use the BTF information for access validation to provide a more generic way to attach to the various LSM hooks. This potentially saves a lot of redundant work:

- Creation of new program types.
- Multiple types of contexts (or a single context with Unions).
- Changes to the verifier and creation of new BPF argument types

     (eg. PTR_TO_TASK)

* These new BPF features also alleviate the original concerns that we raised when initially proposing KRSI and designing for precise BPF helpers. We have some patches coming up which incorporate these new changes and will be sharing something on the mailing list after some cleanup.

We can use the common "eBPF+LSM" for both privileged MAC and Audit and unprivileged sandboxing i.e. Discretionary Access Control. Here's what it could look like:

* Common infrastructure allows attachment to all hooks which works well for privileged MAC and Audit. This could be extended to provide another attachment type for unprivileged DAC, which can restrict the hooks that can be attached to, and also the information that is exposed to the eBPF programs which is something that Landlock could build.

* This attachment could use the proposed landlock domains and attach to the task_struct providing the discretionary access control semantics.

[1] https://static.sched.com/hosted_files/lsseu2019/a2/Kernel%20Runtime%20Security%20Instrumentation.pdf
[2] http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf

- KP Singh

Quoted 1 line
author maintainer
#15 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 29 lines
Quoted 24 lines
Quoted 19 lines
Quoted 2 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

Quoted 4 lines

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

Quoted 5 lines

We are willing to collaborate with the Landlock developers and come up with a common approach that would work for Landlock and KRSI. I want to mention that this collaboration and the current Landlock approach of using an eBPF based LSM for unprivileged sandboxing only makes sense if unprivileged usage of eBPF is going to be ever allowed.

The ability to *potentially* do unprivileged sandboxing is definitely not tied nor a blocker to the unprivileged usage of eBPF. As explained in the documentation [1] (cf. Guiding principles / Unprivileged use), Landlock is designed to be as safe as possible (from a security point of view). The impact is more complex and important than just using unprivileged eBPF, which may not be required. Unprivileged use of eBPF would be nice, but I think the current direction is to extend the Linux capabilities with one or multiple dedicated to eBPF [2] (e.g. CAP_BPF + something else), which may be even better (and a huge difference with CAP_SYS_ADMIN, a.k.a. privileged mode or root). Landlock is designed to deal with unprivileged (i.e. non-root) use cases, but of course, if the Landlock architecture may enable to do unprivileged stuff, it definitely can do privileged stuff too. However, having an architecture designed with safe unprivileged use in mind can't be achieve afterwards.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Quoted 9 lines

Purely from a technical standpoint, both the current designs for Landlock and KRSI target separate use cases and it would not be possible to build "one on top of the other". We've tried to identify the lowest denominator ("eBPF+LSM") requirements for both Landlock (unprivileged sandboxing / Discretionary Access Control) and KRSI (flexibility and unification of privileged MAC and Audit) and prototyped an implementation based on the newly added / upcoming features in BPF.

This is not as binary as that. Sandboxing can be seen as DAC but also as MAC, depending on the subject which apply the security policy and the subjects which are enforced by this policy. If the sandboxing is applied system-wide, it is what we usually call MAC. DAC, in the Linux world, enables any user to restrict access to their files to other users.

With Landlock it is not the same because a process can restrict itself but also enforce these restrictions on all its future children (which may be malicious, whatever their UID/GID). The threat and the definition of the attacker are not the same in both cases. With the Linux DAC the potentially malicious subjects are the other users, whereas with Landlock the potentially malicious subjects are (for now) the current process and all its children. Another way to explain it, and how Landlock is designed, is that a specific enforcement (i.e. a set of BPF programs) is tied to a domain, in which a set of subject are. From this perspective, this approach (subjects/tasks in a domain) is orthogonal to the DAC system (subjects/users). This design may apply to a system-wide MAC system by putting all the system tasks in one domain, and managing restrictions (by subject) with other means (e.g. task's UID, command-line strings, environment variables). In short, Landlock (in this patch series) is closer to a (potentially scoped) MAC system. But thanks to eBPF, Landlock is firstly a programmatic access-control, which means that the one who write the programs and tie them to a set of tasks, can implement their own access-control system (e.g. RBAC, time-based…), or something else (e.g. an audit system).

The audit part can simply be achieve with dedicated helpers and programs that always allow accesses.

Landlock evolved over multiple iterations and is now designed to be very flexible. The current way to enforce a security policy is to go through the seccomp syscall (which makes sense for multiple reasons explained and accepted before). But Landlock is designed to enable similar enforcements (or audit) with other ways to define a domain (e.g. cgroups [3], or system-wide securityfs as done in KRSI). Indeed, the only part tied to this scoped enforcement is in the domain_syscall.c file. A new file domain_fs.c could be added to implement a securityfs for a system-wide enforcement (and have other features as KRSI does).

[3] https://lore.kernel.org/lkml/[email protected]/

One possible important difference between Landlock and KRSI right now is the BPF program management. Both manage a list of programs per hook. However KRSI needs to be able to replace a program in these lists. This is not implemented in this Landlock patch series, first because it is not the main use-case and it is safer to have an append-only way to add restrictions (cf. seccomp-bpf model), and second because it is simpler to deal with immutable lists. However, it is worth considering extending the Landlock domain management with the ability to update the program lists. One challenge may be to identify which program should be replaced (which KRSI does with the program name). I think it would be wiser to implement this in a second step though, maybe not for the syscall interface (thanks to a new seccomp operation), but first with the securityfs one.

Quoted 10 lines

We've been successfully able to prototype the use cases for KRSI (privileged MAC and Audit) using this "eBPF+LSM" and shared our approach at the Linux Security Summit [1]:

* Use the new in-kernel BTF (CO-RE eBPF programs) [2] and the ability of the BPF verifier to use the BTF information for access validation to provide a more generic way to attach to the various LSM hooks. This potentially saves a lot of redundant work:

Quoted 4 lines

- Creation of new program types.
- Multiple types of contexts (or a single context with Unions).
- Changes to the verifier and creation of new BPF argument types

     (eg. PTR_TO_TASK)

As I understood from the LSS talk, KRSI's approach is to use the same hooks as LSM (cf. the securityfs). As said Alexei [4] "It must not make LSM hooks into stable ABI". Moveover, the LSM hooks may change according to internal kernel evolution, and their semantic may not make sense from a user space point of view. This is one reason for which Lanlock abstract those hooks into something that is simpler and designed to fit well with eBPF (program contexts and their attached types, as explained in the documentation).

[4]
https://lore.kernel.org/lkml/[email protected]/

How does KRSI plan to deal with one LSM hook being split in two hooks in a next version of the kernel (cf. [5])?

[5] https://lore.kernel.org/lkml/[email protected]/

Another reason to have multiple different attach types/contexts (cf. landlock_domain->programs[]) is to limit useless BPF program interpretation (in addition to the non-system-wide scoped of programs).
It also enables to handle and verify strict context use (which is also
explain in the Guiding principles). It would be a huge wast of time to run every BPF programs for all LSM hooks. KRSI does the same but instead of relying on the program type it rely on the list tied to the securityfs file.

BTF is great, but as far as I know, it's goal is to easily deal with the moving kernel ABI (e.g. task_struct layout, config/constant variables), and it is definitely useful to programs using bpf_probe_read() and similar accessors. However, I don't see how KRSI would avoid BPF types thanks to BTF.

There is only one program type for Landlock (i.e. BPF_PROG_TYPE_LANDLOCK_HOOK), and I don't see why adding new program *attach* types (e.g. BPF_LANDLOCK_PTRACE) may be an issue. The kernel will still need to be modified to implement new hooks and the new BPF helpers anyway, BTF will not change that, except maybe if the internal LSM API is exposed in a way or another to BPF (thanks to BTF), which does not seem acceptable. Am I missing something?

The current KRSI approach is to allow a common set of helpers to be called by all programs (because there is no way to differentiate them with their type).
How KRSI would deal with kernel objects other than the current task (e.g. a ptrace hook with a tracer and a tracee, a file open/read) with the struct krsi_ctx unions [6]?

[6] https://lore.kernel.org/lkml/[email protected]/

How does KRSI plan to deal with security blobs?

Quoted 11 lines

* These new BPF features also alleviate the original concerns that we raised when initially proposing KRSI and designing for precise BPF helpers. We have some patches coming up which incorporate these new changes and will be sharing something on the mailing list after some cleanup.

We can use the common "eBPF+LSM" for both privileged MAC and Audit and unprivileged sandboxing i.e. Discretionary Access Control. Here's what it could look like:

Quoted 6 lines

* Common infrastructure allows attachment to all hooks which works well for privileged MAC and Audit. This could be extended to provide another attachment type for unprivileged DAC, which can restrict the hooks that can be attached to, and also the information that is exposed to the eBPF programs which is something that Landlock could build.

I agree that the "privileged-only" hooks should be a superset of the "security-safe-and-potentially-unprivileged" hooks. :)
However, as said previously, I'm convinced it is a requirement to have abstract hooks (and associated program attach types) as defined by Landlock.

I'm not sure what you mean by "the information that is exposed to the eBPF program". Is it the current Landlock implementation of specific contexts and attach types?

Quoted 3 lines

* This attachment could use the proposed landlock domains and attach to the task_struct providing the discretionary access control semantics.

Not task_struct but creds, yes. This is a characteristic of sandboxing, which may not be useful for the KRSI use case. It makes sense for KRSI to attach program sets (or Landlock domains) to the whole system, then using the creds does not make sense here. This difference is small and a previous version of Landlock already validated this use case with cgroups [3] (which is postponed to simplify the patch series).

[3] https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

[1] https://static.sched.com/hosted_files/lsseu2019/a2/Kernel%20Runtime%20Security%20Instrumentation.pdf
[2] http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf

- KP Singh

I think it should be OK to first land something close to this Landlock patch series and then we could extend the domain management features and add the securityfs support that KRSI needs. The main concern seems to be about hook definitions.

Another approach would be to land Landlock and KRSI as distinct LSM while trying as much as possible to mutualize code/helpers.

#16 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 101 lines · Mickaël Salaün
Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 29 lines
Quoted 24 lines
Quoted 19 lines
Quoted 2 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

Quoted 4 lines

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

Quoted 5 lines

We are willing to collaborate with the Landlock developers and come up with a common approach that would work for Landlock and KRSI. I want to mention that this collaboration and the current Landlock approach of using an eBPF based LSM for unprivileged sandboxing only makes sense if unprivileged usage of eBPF is going to be ever allowed.

The ability to *potentially* do unprivileged sandboxing is definitely not tied nor a blocker to the unprivileged usage of eBPF. As explained in the documentation [1] (cf. Guiding principles / Unprivileged use), Landlock is designed to be as safe as possible (from a security point of view). The impact is more complex and important than just using unprivileged eBPF, which may not be required. Unprivileged use of eBPF would be nice, but I think the current direction is to extend the Linux capabilities with one or multiple dedicated to eBPF [2] (e.g. CAP_BPF + something else), which may be even better (and a huge difference with CAP_SYS_ADMIN, a.k.a. privileged mode or root). Landlock is designed to deal with unprivileged (i.e. non-root) use cases, but of course, if the Landlock architecture may enable to do unprivileged stuff, it definitely can do privileged stuff too. However, having an architecture designed with safe unprivileged use in mind can't be achieve afterwards.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Quoted 9 lines

Purely from a technical standpoint, both the current designs for Landlock and KRSI target separate use cases and it would not be possible to build "one on top of the other". We've tried to identify the lowest denominator ("eBPF+LSM") requirements for both Landlock (unprivileged sandboxing / Discretionary Access Control) and KRSI (flexibility and unification of privileged MAC and Audit) and prototyped an implementation based on the newly added / upcoming features in BPF.

This is not as binary as that. Sandboxing can be seen as DAC but also as MAC, depending on the subject which apply the security policy and the subjects which are enforced by this policy. If the sandboxing is applied system-wide, it is what we usually call MAC. DAC, in the Linux world, enables any user to restrict access to their files to other users.

With Landlock it is not the same because a process can restrict itself but also enforce these restrictions on all its future children (which may be malicious, whatever their UID/GID). The threat and the definition of the attacker are not the same in both cases. With the Linux DAC the potentially malicious subjects are the other users, whereas with Landlock the potentially malicious subjects are (for now) the current process and all its children. Another way to explain it, and how Landlock is designed, is that a specific enforcement (i.e. a set of BPF programs) is tied to a domain, in which a set of subject are. From this perspective, this approach (subjects/tasks in a domain) is orthogonal to the DAC system (subjects/users). This design may apply to a system-wide MAC system by putting all the system tasks in one domain, and managing restrictions (by subject) with other means (e.g. task's UID, command-line strings, environment variables). In short, Landlock (in this patch series) is closer to a (potentially scoped) MAC system. But thanks to eBPF, Landlock is firstly a programmatic access-control, which means that the one who write the programs and tie them to a set of tasks, can implement their own access-control system (e.g. RBAC, time-based…), or something else (e.g. an audit system).

The audit part can simply be achieve with dedicated helpers and programs that always allow accesses.

Quoted 10 lines

Landlock evolved over multiple iterations and is now designed to be very flexible. The current way to enforce a security policy is to go through the seccomp syscall (which makes sense for multiple reasons explained and accepted before). But Landlock is designed to enable similar enforcements (or audit) with other ways to define a domain (e.g. cgroups [3], or system-wide securityfs as done in KRSI). Indeed, the only part tied to this scoped enforcement is in the domain_syscall.c file. A new file domain_fs.c could be added to implement a securityfs for a system-wide enforcement (and have other features as KRSI does).

Given the current way landlock exposes LSM hooks, I don't think it's possible to build system-wide detections. But let’s try to come to a consensus on the semantics of the how the LSM hooks are exposed to BPF. At the moment I think we should:

* Bring the core interface exposed to eBPF closer to the LSM surface in a way that supports both use cases. One way Landlock can still provide a more abstract interface is by providing some BPF helper libraries that build on top of the core framework.

* Use a single BPF program type; this is necessary for a key requirement of KRSI, i.e. runtime instrumentation. The upcoming prototype should illustrate how this works for KRSI - note that it’s possible to vary the context types exposed by different hooks.

It would be nice to get the BPF maintainers’ opinion on these points.

Quoted 32 lines

[3] https://lore.kernel.org/lkml/[email protected]/

One possible important difference between Landlock and KRSI right now is the BPF program management. Both manage a list of programs per hook. However KRSI needs to be able to replace a program in these lists. This is not implemented in this Landlock patch series, first because it is not the main use-case and it is safer to have an append-only way to add restrictions (cf. seccomp-bpf model), and second because it is simpler to deal with immutable lists. However, it is worth considering extending the Landlock domain management with the ability to update the program lists. One challenge may be to identify which program should be replaced (which KRSI does with the program name). I think it would be wiser to implement this in a second step though, maybe not for the syscall interface (thanks to a new seccomp operation), but first with the securityfs one.

Quoted 10 lines

We've been successfully able to prototype the use cases for KRSI (privileged MAC and Audit) using this "eBPF+LSM" and shared our approach at the Linux Security Summit [1]:

* Use the new in-kernel BTF (CO-RE eBPF programs) [2] and the ability of the BPF verifier to use the BTF information for access validation to provide a more generic way to attach to the various LSM hooks. This potentially saves a lot of redundant work:

Quoted 4 lines

- Creation of new program types.
- Multiple types of contexts (or a single context with Unions).
- Changes to the verifier and creation of new BPF argument types

     (eg. PTR_TO_TASK)
Quoted 4 lines

As I understood from the LSS talk, KRSI's approach is to use the same hooks as LSM (cf. the securityfs). As said Alexei [4] "It must not make LSM hooks into stable ABI". Moveover, the LSM hooks may change according to internal kernel evolution, and their semantic may not make

I think you misunderstand Alexei here. I will let him elaborate.

Quoted 10 lines

sense from a user space point of view. This is one reason for which Lanlock abstract those hooks into something that is simpler and designed to fit well with eBPF (program contexts and their attached types, as explained in the documentation).

[4]
https://lore.kernel.org/lkml/[email protected]/

How does KRSI plan to deal with one LSM hook being split in two hooks in a next version of the kernel (cf. [5])?

How often has that happened in the past? And even if it does happen, it can still be handled as a part of the base framework we are trying to implement.

Quoted 13 lines

[5] https://lore.kernel.org/lkml/[email protected]/

Another reason to have multiple different attach types/contexts (cf. landlock_domain->programs[]) is to limit useless BPF program interpretation (in addition to the non-system-wide scoped of programs).
It also enables to handle and verify strict context use (which is also
explain in the Guiding principles). It would be a huge wast of time to run every BPF programs for all LSM hooks. KRSI does the same but instead of relying on the program type it rely on the list tied to the securityfs file.

Quoted 6 lines

BTF is great, but as far as I know, it's goal is to easily deal with the moving kernel ABI (e.g. task_struct layout, config/constant variables), and it is definitely useful to programs using bpf_probe_read() and similar accessors. However, I don't see how KRSI would avoid BPF types thanks to BTF.

This should become clearer once we post our updated patch-set. Do note that I am currently traveling and will be away for the next couple of weeks.

Quoted 9 lines

There is only one program type for Landlock (i.e. BPF_PROG_TYPE_LANDLOCK_HOOK), and I don't see why adding new program *attach* types (e.g. BPF_LANDLOCK_PTRACE) may be an issue. The kernel will still need to be modified to implement new hooks and the new BPF helpers anyway, BTF will not change that, except maybe if the internal LSM API is exposed in a way or another to BPF (thanks to BTF), which does not seem acceptable. Am I missing something?

Quoted 9 lines

The current KRSI approach is to allow a common set of helpers to be called by all programs (because there is no way to differentiate them with their type).
How KRSI would deal with kernel objects other than the current task (e.g. a ptrace hook with a tracer and a tracee, a file open/read) with the struct krsi_ctx unions [6]?

[6] https://lore.kernel.org/lkml/[email protected]/

The best part of BTF is that it can provide a common way to pass different contexts to the various attachments points and the verifier can use the BTF information to validate accesses which essentially allows us to change the helpers from:

is_running_executable(magical_krsi_ctx)

to

is_running_executable(inode)

which can work on any inode (ARG_PTR_TO_BTF_ID = btf_id(struct inode))

This makes the helpers much more useful and generic. All this is better explained in our upcoming patch-set.

Quoted 2 lines

How does KRSI plan to deal with security blobs?

The new prototype uses security blobs but does not expose them to user-space. These blobs are then used in various helpers like “is_running_executable” which uses blobs on the inode and the task_struct. This should become clearer when the next patchset is posted.

I don’t think it’s currently possible to allow the blobs to be set using eBPF programs with the main reason being that the blob will only be set after the program is loaded. The answer to “is_running_executable” becomes dependent on whether the file was executed before the blob setting eBPF program was loaded.

Blob management with eBPF is not possible unless we can load eBPF programs that can set blobs at boot-time.
In short, the next KRSI version will not give eBPF programs access to arbitrarily write security blobs.

Quoted 20 lines
Quoted 13 lines

* These new BPF features also alleviate the original concerns that we raised when initially proposing KRSI and designing for precise BPF helpers. We have some patches coming up which incorporate these new changes and will be sharing something on the mailing list after some cleanup.

We can use the common "eBPF+LSM" for both privileged MAC and Audit and unprivileged sandboxing i.e. Discretionary Access Control. Here's what it could look like:

Quoted 6 lines

* Common infrastructure allows attachment to all hooks which works well for privileged MAC and Audit. This could be extended to provide another attachment type for unprivileged DAC, which can restrict the hooks that can be attached to, and also the information that is exposed to the eBPF programs which is something that Landlock could build.

Quoted 4 lines

I agree that the "privileged-only" hooks should be a superset of the "security-safe-and-potentially-unprivileged" hooks. :)
However, as said previously, I'm convinced it is a requirement to have abstract hooks (and associated program attach types) as defined by Landlock.

I would like to hear the BPF maintainers’ perspective on this. I am not sure they agree with you here.

- KP Singh

Quoted 32 lines

I'm not sure what you mean by "the information that is exposed to the eBPF program". Is it the current Landlock implementation of specific contexts and attach types?

Quoted 3 lines

* This attachment could use the proposed landlock domains and attach to the task_struct providing the discretionary access control semantics.

Not task_struct but creds, yes. This is a characteristic of sandboxing, which may not be useful for the KRSI use case. It makes sense for KRSI to attach program sets (or Landlock domains) to the whole system, then using the creds does not make sense here. This difference is small and a previous version of Landlock already validated this use case with cgroups [3] (which is postponed to simplify the patch series).

[3] https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

[1] https://static.sched.com/hosted_files/lsseu2019/a2/Kernel%20Runtime%20Security%20Instrumentation.pdf
[2] http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf

- KP Singh

I think it should be OK to first land something close to this Landlock patch series and then we could extend the domain management features and add the securityfs support that KRSI needs. The main concern seems to be about hook definitions.

Another approach would be to land Landlock and KRSI as distinct LSM while trying as much as possible to mutualize code/helpers.

author maintainer
#17 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 113 lines · KP Singh
Quoted 101 lines · Mickaël Salaün
Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 29 lines
Quoted 24 lines
Quoted 19 lines
Quoted 2 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

Quoted 4 lines

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

Quoted 5 lines

We are willing to collaborate with the Landlock developers and come up with a common approach that would work for Landlock and KRSI. I want to mention that this collaboration and the current Landlock approach of using an eBPF based LSM for unprivileged sandboxing only makes sense if unprivileged usage of eBPF is going to be ever allowed.

The ability to *potentially* do unprivileged sandboxing is definitely not tied nor a blocker to the unprivileged usage of eBPF. As explained in the documentation [1] (cf. Guiding principles / Unprivileged use), Landlock is designed to be as safe as possible (from a security point of view). The impact is more complex and important than just using unprivileged eBPF, which may not be required. Unprivileged use of eBPF would be nice, but I think the current direction is to extend the Linux capabilities with one or multiple dedicated to eBPF [2] (e.g. CAP_BPF + something else), which may be even better (and a huge difference with CAP_SYS_ADMIN, a.k.a. privileged mode or root). Landlock is designed to deal with unprivileged (i.e. non-root) use cases, but of course, if the Landlock architecture may enable to do unprivileged stuff, it definitely can do privileged stuff too. However, having an architecture designed with safe unprivileged use in mind can't be achieve afterwards.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Quoted 9 lines

Purely from a technical standpoint, both the current designs for Landlock and KRSI target separate use cases and it would not be possible to build "one on top of the other". We've tried to identify the lowest denominator ("eBPF+LSM") requirements for both Landlock (unprivileged sandboxing / Discretionary Access Control) and KRSI (flexibility and unification of privileged MAC and Audit) and prototyped an implementation based on the newly added / upcoming features in BPF.

This is not as binary as that. Sandboxing can be seen as DAC but also as MAC, depending on the subject which apply the security policy and the subjects which are enforced by this policy. If the sandboxing is applied system-wide, it is what we usually call MAC. DAC, in the Linux world, enables any user to restrict access to their files to other users.

With Landlock it is not the same because a process can restrict itself but also enforce these restrictions on all its future children (which may be malicious, whatever their UID/GID). The threat and the definition of the attacker are not the same in both cases. With the Linux DAC the potentially malicious subjects are the other users, whereas with Landlock the potentially malicious subjects are (for now) the current process and all its children. Another way to explain it, and how Landlock is designed, is that a specific enforcement (i.e. a set of BPF programs) is tied to a domain, in which a set of subject are. From this perspective, this approach (subjects/tasks in a domain) is orthogonal to the DAC system (subjects/users). This design may apply to a system-wide MAC system by putting all the system tasks in one domain, and managing restrictions (by subject) with other means (e.g. task's UID, command-line strings, environment variables). In short, Landlock (in this patch series) is closer to a (potentially scoped) MAC system. But thanks to eBPF, Landlock is firstly a programmatic access-control, which means that the one who write the programs and tie them to a set of tasks, can implement their own access-control system (e.g. RBAC, time-based…), or something else (e.g. an audit system).

The audit part can simply be achieve with dedicated helpers and programs that always allow accesses.

Quoted 10 lines

Landlock evolved over multiple iterations and is now designed to be very flexible. The current way to enforce a security policy is to go through the seccomp syscall (which makes sense for multiple reasons explained and accepted before). But Landlock is designed to enable similar enforcements (or audit) with other ways to define a domain (e.g. cgroups [3], or system-wide securityfs as done in KRSI). Indeed, the only part tied to this scoped enforcement is in the domain_syscall.c file. A new file domain_fs.c could be added to implement a securityfs for a system-wide enforcement (and have other features as KRSI does).

Quoted 2 lines

Given the current way landlock exposes LSM hooks, I don't think it's possible to build system-wide detections.

Why ?

Quoted 9 lines

But let’s try to come to a
consensus on the semantics of the how the LSM hooks are exposed to BPF. At the moment I think we should:

* Bring the core interface exposed to eBPF closer to the LSM surface in a way that supports both use cases. One way Landlock can still provide a more abstract interface is by providing some BPF helper libraries that build on top of the core framework.

I still don't get why you think it is the only way or the better. I gave a lot of arguments and I explained why Landlock is designed the way it is, especially in the documentation (Guiding principles). Is there something similar for KRSI?

Quoted 5 lines

* Use a single BPF program type; this is necessary for a key requirement of KRSI, i.e. runtime instrumentation. The upcoming prototype should illustrate how this works for KRSI - note that it’s possible to vary the context types exposed by different hooks.

Why a single BPF program type? Do you mean *attach* types? Landlock only use one program type, but will use multiple attach types.

Why do you think it is necessary for KRSI or for runtime instrumentation?

If it is justified, it could be a dedicated program attach type (e.g. BPF_LANDLOCK_INTROSPECTION).

What is the advantage to have the possibility to vary the context types over dedicated *typed* contexts? I don't see any advantages, but at least one main drawback: to require runtime checks (when helpers use this generic context) instead of load time checks (thanks to static type checking of the context).

Quoted 50 lines

It would be nice to get the BPF maintainers’ opinion on these points.

Quoted 32 lines

[3] https://lore.kernel.org/lkml/[email protected]/

One possible important difference between Landlock and KRSI right now is the BPF program management. Both manage a list of programs per hook. However KRSI needs to be able to replace a program in these lists. This is not implemented in this Landlock patch series, first because it is not the main use-case and it is safer to have an append-only way to add restrictions (cf. seccomp-bpf model), and second because it is simpler to deal with immutable lists. However, it is worth considering extending the Landlock domain management with the ability to update the program lists. One challenge may be to identify which program should be replaced (which KRSI does with the program name). I think it would be wiser to implement this in a second step though, maybe not for the syscall interface (thanks to a new seccomp operation), but first with the securityfs one.

Quoted 10 lines

We've been successfully able to prototype the use cases for KRSI (privileged MAC and Audit) using this "eBPF+LSM" and shared our approach at the Linux Security Summit [1]:

* Use the new in-kernel BTF (CO-RE eBPF programs) [2] and the ability of the BPF verifier to use the BTF information for access validation to provide a more generic way to attach to the various LSM hooks. This potentially saves a lot of redundant work:

Quoted 4 lines

- Creation of new program types.
- Multiple types of contexts (or a single context with Unions).
- Changes to the verifier and creation of new BPF argument types

     (eg. PTR_TO_TASK)
Quoted 4 lines

As I understood from the LSS talk, KRSI's approach is to use the same hooks as LSM (cf. the securityfs). As said Alexei [4] "It must not make LSM hooks into stable ABI". Moveover, the LSM hooks may change according to internal kernel evolution, and their semantic may not make

I think you misunderstand Alexei here. I will let him elaborate.

Quoted 8 lines

sense from a user space point of view. This is one reason for which Lanlock abstract those hooks into something that is simpler and designed to fit well with eBPF (program contexts and their attached types, as explained in the documentation).

[4]
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 2 lines

How does KRSI plan to deal with one LSM hook being split in two hooks in a next version of the kernel (cf. [5])?

How often has that happened in the past? And even if it does happen, it can still be handled as a part of the base framework we are trying to implement.

I guess the security maintainers should have an opinion on this.

I don't clearly see the properties of this base framework. Could this be elaborated?

Quoted 51 lines
Quoted 12 lines

[5] https://lore.kernel.org/lkml/[email protected]/

Another reason to have multiple different attach types/contexts (cf. landlock_domain->programs[]) is to limit useless BPF program interpretation (in addition to the non-system-wide scoped of programs).
It also enables to handle and verify strict context use (which is also
explain in the Guiding principles). It would be a huge wast of time to run every BPF programs for all LSM hooks. KRSI does the same but instead of relying on the program type it rely on the list tied to the securityfs file.

Quoted 6 lines

BTF is great, but as far as I know, it's goal is to easily deal with the moving kernel ABI (e.g. task_struct layout, config/constant variables), and it is definitely useful to programs using bpf_probe_read() and similar accessors. However, I don't see how KRSI would avoid BPF types thanks to BTF.

This should become clearer once we post our updated patch-set. Do note that I am currently traveling and will be away for the next couple of weeks.

Quoted 9 lines

There is only one program type for Landlock (i.e. BPF_PROG_TYPE_LANDLOCK_HOOK), and I don't see why adding new program *attach* types (e.g. BPF_LANDLOCK_PTRACE) may be an issue. The kernel will still need to be modified to implement new hooks and the new BPF helpers anyway, BTF will not change that, except maybe if the internal LSM API is exposed in a way or another to BPF (thanks to BTF), which does not seem acceptable. Am I missing something?

Quoted 9 lines

The current KRSI approach is to allow a common set of helpers to be called by all programs (because there is no way to differentiate them with their type).
How KRSI would deal with kernel objects other than the current task (e.g. a ptrace hook with a tracer and a tracee, a file open/read) with the struct krsi_ctx unions [6]?

[6] https://lore.kernel.org/lkml/[email protected]/

The best part of BTF is that it can provide a common way to pass different contexts to the various attachments points and the verifier can use the BTF information to validate accesses which essentially allows us to change the helpers from:

is_running_executable(magical_krsi_ctx)

to

Quoted 7 lines

is_running_executable(inode)

which can work on any inode (ARG_PTR_TO_BTF_ID = btf_id(struct inode))

This makes the helpers much more useful and generic. All this is better explained in our upcoming patch-set.

I get the usefulness of BTF for future helper evolution and for moving kernel API, but again, I don't see advantages over static typing check of (well abstract/generic) kernel object handles in the context.

For instance, how BTF would replace the current BPF_LANDLOCK_PTRACE context and the associated helper?

Does this mean that the KRSI v1 design is outdated and superseded by the (future) v2 because of the more important use of BTF?

Quoted 14 lines
Quoted 1 line

How does KRSI plan to deal with security blobs?

The new prototype uses security blobs but does not expose them to user-space. These blobs are then used in various helpers like “is_running_executable” which uses blobs on the inode and the task_struct. This should become clearer when the next patchset is posted.

I don’t think it’s currently possible to allow the blobs to be set using eBPF programs with the main reason being that the blob will only be set after the program is loaded. The answer to “is_running_executable” becomes dependent on whether the file was executed before the blob setting eBPF program was loaded.

Quoted 4 lines

Blob management with eBPF is not possible unless we can load eBPF programs that can set blobs at boot-time.
In short, the next KRSI version will not give eBPF programs access to arbitrarily write security blobs.

A previous version of Landlock enabled programs to tag inodes (cf. FS_GET): https://lore.kernel.org/lkml/[email protected]/

Quoted 23 lines
Quoted 18 lines
Quoted 11 lines

* These new BPF features also alleviate the original concerns that we raised when initially proposing KRSI and designing for precise BPF helpers. We have some patches coming up which incorporate these new changes and will be sharing something on the mailing list after some cleanup.

We can use the common "eBPF+LSM" for both privileged MAC and Audit and unprivileged sandboxing i.e. Discretionary Access Control. Here's what it could look like:

Quoted 6 lines

* Common infrastructure allows attachment to all hooks which works well for privileged MAC and Audit. This could be extended to provide another attachment type for unprivileged DAC, which can restrict the hooks that can be attached to, and also the information that is exposed to the eBPF programs which is something that Landlock could build.

Quoted 4 lines

I agree that the "privileged-only" hooks should be a superset of the "security-safe-and-potentially-unprivileged" hooks. :)
However, as said previously, I'm convinced it is a requirement to have abstract hooks (and associated program attach types) as defined by Landlock.

Quoted 9 lines

I would like to hear the BPF maintainers’ perspective on this. I am not sure they agree with you here.

- KP Singh

Quoted 4 lines

I'm not sure what you mean by "the information that is exposed to the eBPF program". Is it the current Landlock implementation of specific contexts and attach types?

…or does BTF could magically solve this?

Quoted 29 lines
Quoted 28 lines
Quoted 4 lines

* This attachment could use the proposed landlock domains and attach to the task_struct providing the discretionary access control semantics.

Not task_struct but creds, yes. This is a characteristic of sandboxing, which may not be useful for the KRSI use case. It makes sense for KRSI to attach program sets (or Landlock domains) to the whole system, then using the creds does not make sense here. This difference is small and a previous version of Landlock already validated this use case with cgroups [3] (which is postponed to simplify the patch series).

[3] https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

[1] https://static.sched.com/hosted_files/lsseu2019/a2/Kernel%20Runtime%20Security%20Instrumentation.pdf
[2] http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf

- KP Singh

I think it should be OK to first land something close to this Landlock patch series and then we could extend the domain management features and add the securityfs support that KRSI needs. The main concern seems to be about hook definitions.

Another approach would be to land Landlock and KRSI as distinct LSM while trying as much as possible to mutualize code/helpers.

maintainer
#18 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 6 lines · Mickaël Salaün
Quoted 5 lines · KP Singh
Quoted 4 lines · Mickaël Salaün
Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 10 lines
Quoted 4 lines

* Use a single BPF program type; this is necessary for a key requirement of KRSI, i.e. runtime instrumentation. The upcoming prototype should illustrate how this works for KRSI - note that it’s possible to vary the context types exposed by different hooks.

Why a single BPF program type? Do you mean *attach* types? Landlock only use one program type, but will use multiple attach types.

Why do you think it is necessary for KRSI or for runtime instrumentation?

Quoted 8 lines

If it is justified, it could be a dedicated program attach type (e.g. BPF_LANDLOCK_INTROSPECTION).

What is the advantage to have the possibility to vary the context types over dedicated *typed* contexts? I don't see any advantages, but at least one main drawback: to require runtime checks (when helpers use this generic context) instead of load time checks (thanks to static type checking of the context).

Lets take security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) as one specific example here: the running kernel has its own internal btf_vmlinux and therefore a complete description of itself. From verifier side we can retrieve & introspect the security_sock_rcv_skb signatue and thus know that the given BPF attachment point has struct sock and struct sk_buff as input arguments which can then be accessed generically by the prog in order to allow sk_filter_trim_cap() to pass or to drop the skb. The same generic approach can be done for many of the other lsm hooks, so single program type would be enough there and context is derived automatically, no dedicated extra context per attach type would be needed and no runtime checks as you mentioned above since its still all asserted at verification time.

Thanks,
Daniel

author maintainer
#19 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 27 lines · Daniel Borkmann
Quoted 6 lines · Mickaël Salaün
Quoted 5 lines · KP Singh
Quoted 4 lines · Mickaël Salaün
Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 10 lines
Quoted 4 lines

* Use a single BPF program type; this is necessary for a key requirement of KRSI, i.e. runtime instrumentation. The upcoming prototype should illustrate how this works for KRSI - note that it’s possible to vary the context types exposed by different hooks.

Why a single BPF program type? Do you mean *attach* types? Landlock only use one program type, but will use multiple attach types.

Why do you think it is necessary for KRSI or for runtime instrumentation?

Quoted 8 lines

If it is justified, it could be a dedicated program attach type (e.g. BPF_LANDLOCK_INTROSPECTION).

What is the advantage to have the possibility to vary the context types over dedicated *typed* contexts? I don't see any advantages, but at least one main drawback: to require runtime checks (when helpers use this generic context) instead of load time checks (thanks to static type checking of the context).

Quoted 4 lines

Lets take security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) as one specific example here: the running kernel has its own internal btf_vmlinux and therefore a complete description of itself. From verifier side we can retrieve & introspect the security_sock_rcv_skb signatue

OK, this is indeed the signature defined by the kernel API. What happen if this API change (e.g. if struct sock is replaced with a struct sock_meta)?

Quoted 3 lines

and
thus know that the given BPF attachment point has struct sock and struct sk_buff as input arguments

How does the verifier know a given BPF attachment point for a program without relying on its type or attach type? How and where is registered this mapping?

To say it another way, if there is no way to differentiate two program targeting different hook, I don't understand how the verifier could check if a given program can legitimately call a helper which could read the tracer and tracee fields (legitimate for a ptrace hook), whereas this program may be attached to a sock_rcv_skb hook (and there is no way to know that).

Quoted 8 lines

which can then be accessed generically by the
prog in order to allow sk_filter_trim_cap() to pass or to drop the skb. The same generic approach can be done for many of the other lsm hooks, so single program type would be enough there and context is derived automatically,
no dedicated extra context per attach type would be needed and no runtime checks as you mentioned above since its still all asserted at verification time.

I mentioned runtime check because I though a helper should handle the case when it doesn't make sense for a program attached to a specific point/hook (e.g. ptrace) to use an input argument (e.g. sk) defined for another point/hook (e.g. sock_rcv_skb).

Quoted 4 lines

Thanks,
Daniel

Thanks for this explanation Daniel.

#20 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

In reply to Mickaël Salaün
Quoted 115 lines · Mickaël Salaün
Quoted 113 lines · KP Singh
Quoted 111 lines · Mickaël Salaün
Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 29 lines
Quoted 24 lines
Quoted 19 lines
Quoted 2 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

Quoted 4 lines

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

Quoted 4 lines

I would like KRSI folks to speak up. So far I don't see any sharing happening between landlock and KRSI. You're claiming this set is a first step. They're claiming the same about their patches. I'd like to set a patchset that was jointly developed.

Quoted 5 lines

We are willing to collaborate with the Landlock developers and come up with a common approach that would work for Landlock and KRSI. I want to mention that this collaboration and the current Landlock approach of using an eBPF based LSM for unprivileged sandboxing only makes sense if unprivileged usage of eBPF is going to be ever allowed.

The ability to *potentially* do unprivileged sandboxing is definitely not tied nor a blocker to the unprivileged usage of eBPF. As explained in the documentation [1] (cf. Guiding principles / Unprivileged use), Landlock is designed to be as safe as possible (from a security point of view). The impact is more complex and important than just using unprivileged eBPF, which may not be required. Unprivileged use of eBPF would be nice, but I think the current direction is to extend the Linux capabilities with one or multiple dedicated to eBPF [2] (e.g. CAP_BPF + something else), which may be even better (and a huge difference with CAP_SYS_ADMIN, a.k.a. privileged mode or root). Landlock is designed to deal with unprivileged (i.e. non-root) use cases, but of course, if the Landlock architecture may enable to do unprivileged stuff, it definitely can do privileged stuff too. However, having an architecture designed with safe unprivileged use in mind can't be achieve afterwards.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Quoted 9 lines

Purely from a technical standpoint, both the current designs for Landlock and KRSI target separate use cases and it would not be possible to build "one on top of the other". We've tried to identify the lowest denominator ("eBPF+LSM") requirements for both Landlock (unprivileged sandboxing / Discretionary Access Control) and KRSI (flexibility and unification of privileged MAC and Audit) and prototyped an implementation based on the newly added / upcoming features in BPF.

This is not as binary as that. Sandboxing can be seen as DAC but also as MAC, depending on the subject which apply the security policy and the subjects which are enforced by this policy. If the sandboxing is applied system-wide, it is what we usually call MAC. DAC, in the Linux world, enables any user to restrict access to their files to other users.

With Landlock it is not the same because a process can restrict itself but also enforce these restrictions on all its future children (which may be malicious, whatever their UID/GID). The threat and the definition of the attacker are not the same in both cases. With the Linux DAC the potentially malicious subjects are the other users, whereas with Landlock the potentially malicious subjects are (for now) the current process and all its children. Another way to explain it, and how Landlock is designed, is that a specific enforcement (i.e. a set of BPF programs) is tied to a domain, in which a set of subject are. From this perspective, this approach (subjects/tasks in a domain) is orthogonal to the DAC system (subjects/users). This design may apply to a system-wide MAC system by putting all the system tasks in one domain, and managing restrictions (by subject) with other means (e.g. task's UID, command-line strings, environment variables). In short, Landlock (in this patch series) is closer to a (potentially scoped) MAC system. But thanks to eBPF, Landlock is firstly a programmatic access-control, which means that the one who write the programs and tie them to a set of tasks, can implement their own access-control system (e.g. RBAC, time-based…), or something else (e.g. an audit system).

The audit part can simply be achieve with dedicated helpers and programs that always allow accesses.

Landlock evolved over multiple iterations and is now designed to be very flexible. The current way to enforce a security policy is to go through the seccomp syscall (which makes sense for multiple reasons explained and accepted before). But Landlock is designed to enable similar enforcements (or audit) with other ways to define a domain (e.g. cgroups [3], or system-wide securityfs as done in KRSI). Indeed, the only part tied to this scoped enforcement is in the domain_syscall.c file. A new file domain_fs.c could be added to implement a securityfs for a system-wide enforcement (and have other features as KRSI does).

Quoted 4 lines
Quoted 2 lines

Given the current way landlock exposes LSM hooks, I don't think it's possible to build system-wide detections.

Why ?

This and some of the other questions (about the core-framework and guiding principles) are better discussed in the context of the new-patchset. It might take us some-time as I am on vacation for the next few weeks and some of the other members are traveling.

Quoted 165 lines
Quoted 11 lines

But let’s try to come to a
consensus on the semantics of the how the LSM hooks are exposed to BPF. At the moment I think we should:

* Bring the core interface exposed to eBPF closer to the LSM surface in a way that supports both use cases. One way Landlock can still provide a more abstract interface is by providing some BPF helper libraries that build on top of the core framework.

I still don't get why you think it is the only way or the better. I gave a lot of arguments and I explained why Landlock is designed the way it is, especially in the documentation (Guiding principles). Is there something similar for KRSI?

Quoted 5 lines

* Use a single BPF program type; this is necessary for a key requirement of KRSI, i.e. runtime instrumentation. The upcoming prototype should illustrate how this works for KRSI - note that it’s possible to vary the context types exposed by different hooks.

Why a single BPF program type? Do you mean *attach* types? Landlock only use one program type, but will use multiple attach types.

Why do you think it is necessary for KRSI or for runtime instrumentation?

If it is justified, it could be a dedicated program attach type (e.g. BPF_LANDLOCK_INTROSPECTION).

What is the advantage to have the possibility to vary the context types over dedicated *typed* contexts? I don't see any advantages, but at least one main drawback: to require runtime checks (when helpers use this generic context) instead of load time checks (thanks to static type checking of the context).

Quoted 50 lines

It would be nice to get the BPF maintainers’ opinion on these points.

Quoted 32 lines

[3] https://lore.kernel.org/lkml/[email protected]/

One possible important difference between Landlock and KRSI right now is the BPF program management. Both manage a list of programs per hook. However KRSI needs to be able to replace a program in these lists. This is not implemented in this Landlock patch series, first because it is not the main use-case and it is safer to have an append-only way to add restrictions (cf. seccomp-bpf model), and second because it is simpler to deal with immutable lists. However, it is worth considering extending the Landlock domain management with the ability to update the program lists. One challenge may be to identify which program should be replaced (which KRSI does with the program name). I think it would be wiser to implement this in a second step though, maybe not for the syscall interface (thanks to a new seccomp operation), but first with the securityfs one.

Quoted 10 lines

We've been successfully able to prototype the use cases for KRSI (privileged MAC and Audit) using this "eBPF+LSM" and shared our approach at the Linux Security Summit [1]:

* Use the new in-kernel BTF (CO-RE eBPF programs) [2] and the ability of the BPF verifier to use the BTF information for access validation to provide a more generic way to attach to the various LSM hooks. This potentially saves a lot of redundant work:

Quoted 4 lines

- Creation of new program types.
- Multiple types of contexts (or a single context with Unions).
- Changes to the verifier and creation of new BPF argument types

     (eg. PTR_TO_TASK)
Quoted 4 lines

As I understood from the LSS talk, KRSI's approach is to use the same hooks as LSM (cf. the securityfs). As said Alexei [4] "It must not make LSM hooks into stable ABI". Moveover, the LSM hooks may change according to internal kernel evolution, and their semantic may not make

I think you misunderstand Alexei here. I will let him elaborate.

Quoted 8 lines

sense from a user space point of view. This is one reason for which Lanlock abstract those hooks into something that is simpler and designed to fit well with eBPF (program contexts and their attached types, as explained in the documentation).

[4]
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 2 lines

How does KRSI plan to deal with one LSM hook being split in two hooks in a next version of the kernel (cf. [5])?

How often has that happened in the past? And even if it does happen, it can still be handled as a part of the base framework we are trying to implement.

I guess the security maintainers should have an opinion on this.

I don't clearly see the properties of this base framework. Could this be elaborated?

Quoted 51 lines
Quoted 12 lines

[5] https://lore.kernel.org/lkml/[email protected]/

Another reason to have multiple different attach types/contexts (cf. landlock_domain->programs[]) is to limit useless BPF program interpretation (in addition to the non-system-wide scoped of programs).
It also enables to handle and verify strict context use (which is also
explain in the Guiding principles). It would be a huge wast of time to run every BPF programs for all LSM hooks. KRSI does the same but instead of relying on the program type it rely on the list tied to the securityfs file.

Quoted 6 lines

BTF is great, but as far as I know, it's goal is to easily deal with the moving kernel ABI (e.g. task_struct layout, config/constant variables), and it is definitely useful to programs using bpf_probe_read() and similar accessors. However, I don't see how KRSI would avoid BPF types thanks to BTF.

This should become clearer once we post our updated patch-set. Do note that I am currently traveling and will be away for the next couple of weeks.

Quoted 9 lines

There is only one program type for Landlock (i.e. BPF_PROG_TYPE_LANDLOCK_HOOK), and I don't see why adding new program *attach* types (e.g. BPF_LANDLOCK_PTRACE) may be an issue. The kernel will still need to be modified to implement new hooks and the new BPF helpers anyway, BTF will not change that, except maybe if the internal LSM API is exposed in a way or another to BPF (thanks to BTF), which does not seem acceptable. Am I missing something?

Quoted 9 lines

The current KRSI approach is to allow a common set of helpers to be called by all programs (because there is no way to differentiate them with their type).
How KRSI would deal with kernel objects other than the current task (e.g. a ptrace hook with a tracer and a tracee, a file open/read) with the struct krsi_ctx unions [6]?

[6] https://lore.kernel.org/lkml/[email protected]/

The best part of BTF is that it can provide a common way to pass different contexts to the various attachments points and the verifier can use the BTF information to validate accesses which essentially allows us to change the helpers from:

is_running_executable(magical_krsi_ctx)

to

Quoted 7 lines

is_running_executable(inode)

which can work on any inode (ARG_PTR_TO_BTF_ID = btf_id(struct inode))

This makes the helpers much more useful and generic. All this is better explained in our upcoming patch-set.

I get the usefulness of BTF for future helper evolution and for moving kernel API, but again, I don't see advantages over static typing check of (well abstract/generic) kernel object handles in the context.

Quoted 5 lines

For instance, how BTF would replace the current BPF_LANDLOCK_PTRACE context and the associated helper?

Does this mean that the KRSI v1 design is outdated and superseded by the (future) v2 because of the more important use of BTF?

Yes, that's correct and based on the feedback we got on the RFC v1 at the Linux Plumbers 2019.

Also, the changes that allow BPF verifier to use the BTF information to validate accesses and dynamically populate the context are very recent and have influenced the design for the next iteration.

- KP Singh

Quoted 90 lines
Quoted 16 lines
Quoted 3 lines

How does KRSI plan to deal with security blobs?

The new prototype uses security blobs but does not expose them to user-space. These blobs are then used in various helpers like “is_running_executable” which uses blobs on the inode and the task_struct. This should become clearer when the next patchset is posted.

I don’t think it’s currently possible to allow the blobs to be set using eBPF programs with the main reason being that the blob will only be set after the program is loaded. The answer to “is_running_executable” becomes dependent on whether the file was executed before the blob setting eBPF program was loaded.

Quoted 4 lines

Blob management with eBPF is not possible unless we can load eBPF programs that can set blobs at boot-time.
In short, the next KRSI version will not give eBPF programs access to arbitrarily write security blobs.

A previous version of Landlock enabled programs to tag inodes (cf. FS_GET): https://lore.kernel.org/lkml/[email protected]/

Quoted 23 lines
Quoted 18 lines
Quoted 11 lines

* These new BPF features also alleviate the original concerns that we raised when initially proposing KRSI and designing for precise BPF helpers. We have some patches coming up which incorporate these new changes and will be sharing something on the mailing list after some cleanup.

We can use the common "eBPF+LSM" for both privileged MAC and Audit and unprivileged sandboxing i.e. Discretionary Access Control. Here's what it could look like:

Quoted 6 lines

* Common infrastructure allows attachment to all hooks which works well for privileged MAC and Audit. This could be extended to provide another attachment type for unprivileged DAC, which can restrict the hooks that can be attached to, and also the information that is exposed to the eBPF programs which is something that Landlock could build.

Quoted 4 lines

I agree that the "privileged-only" hooks should be a superset of the "security-safe-and-potentially-unprivileged" hooks. :)
However, as said previously, I'm convinced it is a requirement to have abstract hooks (and associated program attach types) as defined by Landlock.

Quoted 9 lines

I would like to hear the BPF maintainers’ perspective on this. I am not sure they agree with you here.

- KP Singh

Quoted 4 lines

I'm not sure what you mean by "the information that is exposed to the eBPF program". Is it the current Landlock implementation of specific contexts and attach types?

…or does BTF could magically solve this?

Quoted 29 lines
Quoted 28 lines
Quoted 4 lines

* This attachment could use the proposed landlock domains and attach to the task_struct providing the discretionary access control semantics.

Not task_struct but creds, yes. This is a characteristic of sandboxing, which may not be useful for the KRSI use case. It makes sense for KRSI to attach program sets (or Landlock domains) to the whole system, then using the creds does not make sense here. This difference is small and a previous version of Landlock already validated this use case with cgroups [3] (which is postponed to simplify the patch series).

[3] https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

[1] https://static.sched.com/hosted_files/lsseu2019/a2/Kernel%20Runtime%20Security%20Instrumentation.pdf
[2] http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf

- KP Singh

I think it should be OK to first land something close to this Landlock patch series and then we could extend the domain management features and add the securityfs support that KRSI needs. The main concern seems to be about hook definitions.

Another approach would be to land Landlock and KRSI as distinct LSM while trying as much as possible to mutualize code/helpers.

#21 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

In reply to Mickaël Salaün
Quoted 36 lines · Mickaël Salaün
Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Mickaël Salaün

Add a first Landlock hook that can be used to enforce a security policy or to audit some process activities. For a sandboxing use-case, it is needed to inform the kernel if a task can legitimately debug another. ptrace(2) can also be used by an attacker to impersonate another task and remain undetected while performing malicious activities.

Using ptrace(2) and related features on a target process can lead to a privilege escalation. A sandboxed task must then be able to tell the kernel if another task is more privileged, via ptrace_may_access().

...

Quoted 13 lines

security/landlock/hooks_ptrace.c line 42

Quoted 9 lines
security/landlock/hooks_ptrace.c new file +9 -0
+static int check_ptrace(struct landlock_domain *domain,
+		struct task_struct *tracer, struct task_struct *tracee)
+{
+	struct landlock_hook_ctx_ptrace ctx_ptrace = {
+		.prog_ctx = {
+			.tracer = (uintptr_t)tracer,
+			.tracee = (uintptr_t)tracee,
+		},
+	};

So you're passing two kernel pointers obfuscated as u64 into bpf program yet claiming that the end goal is to make landlock unprivileged?! The most basic security hole in the tool that is aiming to provide security.

How could you used these pointers without dedicated BPF helpers? This context items are typed as PTR_TO_TASK and can't be used without a dedicated helper able to deal with ARG_PTR_TO_TASK. Moreover, pointer arithmetic is explicitly forbidden (and I added tests for that). Did I miss something?

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

As I mentioned we are willing to collaborate but the current landlock patches does not meet the needs for KRSI:

* One program type per use-case (eg. LANDLOCK_PROG_PTRACE) as opposed to a single program type. This is something that KRSI proposed in it's initial design [1] and the new common "eBPF + LSM" based approach
[2] would maintain as well.

* Landlock chooses to have multiple LSM hooks per landlock hook which is more restrictive. It's not easy to write precise MAC and Audit policies for a privileged LSM based on this and this ends up bloating the context that needs to be maintained and requires avoidable boilerplate work in the kernel.

[1] https://lore.kernel.org/patchwork/project/lkml/list/?series=410101
[2] https://lore.kernel.org/bpf/[email protected]/T/#u

- KP Singh

Quoted 16 lines

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

BPF is capable
to be a superset of all existing LSMs whereas landlock and KRSI propsals today are custom solutions to specific security concerns. BPF subsystem was extended with custom things in the past. In networking we have lwt, skb, tc, xdp, sk program types with a lot of overlapping functionality. We couldn't figure out how to generalize them into single 'networking' program. Now we can and we should. Accepting two partially overlapping bpf-based LSMs would be repeating the same mistake again.

I'll let the LSM maintainers comment on whether BPF could be a superset of all LSM, but given the complexity of an access-control system, I have some doubts though. Anyway, we need to start somewhere and then iterate. This patch series is a first step.

author maintainer
#22 lore raw

Re: [PATCH bpf-next v13 4/7] landlock: Add ptrace LSM hooks

Quoted 2 lines · KP Singh
Quoted 1 line · Mickaël Salaün

On 05/11/2019 18:18, Alexei Starovoitov wrote:

[...]

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

I think the only way bpf-based LSM can land is both landlock and KRSI developers work together on a design that solves all use cases.

As I said in a previous cover letter [1], that would be great. I think that the current Landlock bases (almost everything from this series except the seccomp interface) should meet both needs, but I would like to have the point of view of the KRSI developers.

Quoted 7 lines

As I mentioned we are willing to collaborate but the current landlock patches does not meet the needs for KRSI:

* One program type per use-case (eg. LANDLOCK_PROG_PTRACE) as opposed to a single program type. This is something that KRSI proposed in it's initial design [1] and the new common "eBPF + LSM" based approach
[2] would maintain as well.

As ask in my previous email [1], I don't see how KRSI would efficiently deal with other LSM hooks with a unique program (attach) type.

[1]
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines

* Landlock chooses to have multiple LSM hooks per landlock hook which is more restrictive. It's not easy to write precise MAC and Audit policies for a privileged LSM based on this and this ends up bloating the context that needs to be maintained and requires avoidable boilerplate work in the kernel.

Why do you think it is more restrictive or it adds boilerplate work? How does KRSI will deal with more complex hooks than execve-like with multiple kernel objects?

Quoted 5 lines

[1] https://lore.kernel.org/patchwork/project/lkml/list/?series=410101
[2] https://lore.kernel.org/bpf/[email protected]/T/#u

- KP Singh

author maintainer
#23 lore raw

PATCH v13 5/7 bpf,landlock: Add task_landlock_ptrace_ancestor() helper

In reply to Mickaël Salaün

This new task_landlock_ptrace_ancestor() helper can be used to identify if the Landlock domain tied to the current tracer is in the same hierarchy as the domain of tracee. This may be a way for user space to programmatically defines that a set of tasks is less privileged than another set of tasks.

Indeed, ptrace(2) can be used to impersonate an unsandboxed process and lead to a privilege escalation. A common use-case when sandboxing a process is then to forbid it to debug a less-privileged process. A sandbox process (tracer) should only be allowed to trace another process (tracee) if the tracee has fewer privileges than the tracer. This policy can be implemented with this helper.

More complex helpers could be added in the future to enable other ways to check the relation between the tracer and the tracee (e.g. check other program types or other hierarchies) if there is a use case.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v10:
* new patch taking inspiration from the previous static ptrace policy

---
 include/linux/bpf.h            |  2 +
 include/uapi/linux/bpf.h       | 21 ++++++++++-
 kernel/bpf/verifier.c          |  4 ++
 security/landlock/bpf_ptrace.c | 68 ++++++++++++++++++++++++++++++++++
 security/landlock/bpf_verify.c |  4 ++
 5 files changed, 98 insertions(+), 1 deletion(-)
include/linux/bpf.h +2 -0
index 819a3e207438..67ec198a90cb 100644
@@ -214,6 +214,7 @@ enum bpf_arg_type {
 	ARG_PTR_TO_LONG,	/* pointer to long */
 	ARG_PTR_TO_SOCKET,	/* pointer to bpf_sock (fullsock) */
 	ARG_PTR_TO_BTF_ID,	/* pointer to in-kernel struct */
+	ARG_PTR_TO_TASK,	/* pointer to task_struct */
 };
 
 /* type of values returned from helper functions */
@@ -1088,6 +1089,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;
 extern const struct bpf_func_proto bpf_strtol_proto;
 extern const struct bpf_func_proto bpf_strtoul_proto;
 extern const struct bpf_func_proto bpf_tcp_sock_proto;
+extern const struct bpf_func_proto bpf_task_landlock_ptrace_ancestor_proto;
 
 /* Shared helpers among cBPF and eBPF. */
 void bpf_user_rnd_init_once(void);
include/uapi/linux/bpf.h +20 -1
index 6e4147790f96..c88436b97163 100644
@@ -2777,6 +2777,24 @@ union bpf_attr {
  * 		restricted to raw_tracepoint bpf programs.
  * 	Return
  * 		0 on success, or a negative error in case of failure.
+ *
+ * int bpf_task_landlock_ptrace_ancestor(struct task_struct *parent, struct task_struct *child)
+ *	Description
+ *		Check the relation of a potentially parent task with a child
+ *		one, according to their Landlock ptrace hook programs.
+ *	Return
+ *		**-EINVAL** if the child's ptrace programs are not comparable
+ *		to the parent ones, i.e. one of them is an empty set.
+ *
+ *		**-ENOENT** if the parent's ptrace programs are either in a
+ *		separate hierarchy of the child ones, or if the parent's ptrace
+ *		programs are a superset of the child ones.
+ *
+ *		0 if the parent's ptrace programs are the same as the child
+ *		ones.
+ *
+ *		1 if the parent's ptrace programs are indeed a subset of the
+ *		child ones.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -2890,7 +2908,8 @@ union bpf_attr {
 	FN(sk_storage_delete),		\
 	FN(send_signal),		\
 	FN(tcp_gen_syncookie),		\
-	FN(skb_output),
+	FN(skb_output),			\
+	FN(task_landlock_ptrace_ancestor),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
kernel/bpf/verifier.c +4 -0
index ebf1991906b7..af8f1a777a2d 100644
@@ -3492,6 +3492,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
 		    type != PTR_TO_MAP_VALUE &&
 		    type != expected_type)
 			goto err_type;
+	} else if (arg_type == ARG_PTR_TO_TASK) {
+		expected_type = PTR_TO_TASK;
+		if (type != expected_type)
+			goto err_type;
 	} else {
 		verbose(env, "unsupported arg_type %d\n", arg_type);
 		return -EFAULT;
security/landlock/bpf_ptrace.c +68 -0

80 lines not on this page · raw message

security/landlock/bpf_verify.c +4 -0
index 6ed921588178..a1d2db75d51d 100644
@@ -70,6 +70,10 @@ static const struct bpf_func_proto *bpf_landlock_func_proto(
 		return &bpf_map_update_elem_proto;
 	case BPF_FUNC_map_delete_elem:
 		return &bpf_map_delete_elem_proto;
+	case BPF_FUNC_task_landlock_ptrace_ancestor:
+		if (get_hook_type(prog) == LANDLOCK_HOOK_PTRACE)
+			return &bpf_task_landlock_ptrace_ancestor_proto;
+		return NULL;
 	default:
 		return NULL;
 	}
Signature · 1 line
2.23.0
author maintainer
#24 lore raw

PATCH v13 6/7 bpf,landlock: Add tests for the Landlock ptrace program type

In reply to Mickaël Salaün

Test eBPF program context access and ptrace hooks semantic.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v12:
* add documentation and diagrams (by Vincent Dagonneau)

Changes since v11:
* cosmetic fixes

Changes since v10:
* rework tests with new Landlock ptrace programs which restrict ptrace thanks to the task_landlock_ptrace_ancestor() helper
* simplify ptrace tests (make expect_ptrace implicit)
* add tests:
* check a child process tracing its parent
* check Landlock domain without ptrace enforcement (e.g. useful for audit/signaling purpose)
* check inherited-only domains
* check task pointer arithmetic
* fix flaky test for multi-core
* increase log size
* cosmetic renames
* update and improve the Makefile

Changes since v9:
* replace subtype with expected_attach_type and expected_attach_triggers
* rename inode_map_lookup() into inode_map_lookup_elem()
* check for inode map entry without value (which is now possible thanks to the pointer null check)
* use read-only inode map for Landlock programs

Changes since v8:
* update eBPF include path for macros
* use TEST_GEN_PROGS and use the generic "clean" target
* add more verbose errors
* update the bpf/verifier files
* remove chain tests (from landlock and bpf/verifier)
* replace the whitelist tests with blacklist tests (because of stateless Landlock programs): remove "dotdot" tests and other depth tests
* sync the landlock Makefile with its bpf sibling directory and use bpf_load_program_xattr()

Changes since v7:
* update tests and add new ones for filesystem hierarchy and Landlock chains.

Changes since v6:
* use the new kselftest_harness.h
* use const variables
* replace ASSERT_STEP with ASSERT_*
* rename BPF_PROG_TYPE_LANDLOCK to BPF_PROG_TYPE_LANDLOCK_RULE
* force sample library rebuild
* fix install target

Changes since v5:
* add subtype test
* add ptrace tests
* split and rename files
* cleanup and rebase

---
 scripts/bpf_helpers_doc.py                    |   1 +
 tools/include/uapi/linux/bpf.h                |  23 +-
 tools/include/uapi/linux/landlock.h           |  22 ++
 tools/lib/bpf/libbpf_probes.c                 |   3 +
 tools/testing/selftests/bpf/config            |   3 +
 tools/testing/selftests/bpf/test_verifier.c   |   1 +
 .../testing/selftests/bpf/verifier/landlock.c |  56 ++++
 tools/testing/selftests/landlock/.gitignore   |   5 +
 tools/testing/selftests/landlock/Makefile     |  27 ++
 tools/testing/selftests/landlock/config       |   5 +
 tools/testing/selftests/landlock/test.h       |  48 +++
 tools/testing/selftests/landlock/test_base.c  |  24 ++
 .../testing/selftests/landlock/test_ptrace.c  | 289 ++++++++++++++++++
 13 files changed, 506 insertions(+), 1 deletion(-)
 create mode 100644 tools/include/uapi/linux/landlock.h
 create mode 100644 tools/testing/selftests/bpf/verifier/landlock.c
 create mode 100644 tools/testing/selftests/landlock/.gitignore
 create mode 100644 tools/testing/selftests/landlock/Makefile
 create mode 100644 tools/testing/selftests/landlock/config
 create mode 100644 tools/testing/selftests/landlock/test.h
 create mode 100644 tools/testing/selftests/landlock/test_base.c
 create mode 100644 tools/testing/selftests/landlock/test_ptrace.c
scripts/bpf_helpers_doc.py +1 -0
index 7548569e8076..8e4c0fe75663 100755
@@ -466,6 +466,7 @@ class PrinterHelpers(Printer):
             'const struct sk_buff': 'const struct __sk_buff',
             'struct sk_msg_buff': 'struct sk_msg_md',
             'struct xdp_buff': 'struct xdp_md',
+            'struct task_struct': 'void',
     }
 
     def print_header(self):
tools/include/uapi/linux/bpf.h +22 -1
index 4af8b0819a32..c88436b97163 100644
@@ -173,6 +173,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_CGROUP_SYSCTL,
 	BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
 	BPF_PROG_TYPE_CGROUP_SOCKOPT,
+	BPF_PROG_TYPE_LANDLOCK_HOOK,
 };
 
 enum bpf_attach_type {
@@ -199,6 +200,7 @@ enum bpf_attach_type {
 	BPF_CGROUP_UDP6_RECVMSG,
 	BPF_CGROUP_GETSOCKOPT,
 	BPF_CGROUP_SETSOCKOPT,
+	BPF_LANDLOCK_PTRACE,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -2775,6 +2777,24 @@ union bpf_attr {
  * 		restricted to raw_tracepoint bpf programs.
  * 	Return
  * 		0 on success, or a negative error in case of failure.
+ *
+ * int bpf_task_landlock_ptrace_ancestor(struct task_struct *parent, struct task_struct *child)
+ *	Description
+ *		Check the relation of a potentially parent task with a child
+ *		one, according to their Landlock ptrace hook programs.
+ *	Return
+ *		**-EINVAL** if the child's ptrace programs are not comparable
+ *		to the parent ones, i.e. one of them is an empty set.
+ *
+ *		**-ENOENT** if the parent's ptrace programs are either in a
+ *		separate hierarchy of the child ones, or if the parent's ptrace
+ *		programs are a superset of the child ones.
+ *
+ *		0 if the parent's ptrace programs are the same as the child
+ *		ones.
+ *
+ *		1 if the parent's ptrace programs are indeed a subset of the
+ *		child ones.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -2888,7 +2908,8 @@ union bpf_attr {
 	FN(sk_storage_delete),		\
 	FN(send_signal),		\
 	FN(tcp_gen_syncookie),		\
-	FN(skb_output),
+	FN(skb_output),			\
+	FN(task_landlock_ptrace_ancestor),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
tools/include/uapi/linux/landlock.h new file +22 -0
new file mode 100644
index 000000000000..3db2d190c4e7
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Landlock - UAPI headers
+ *
+ * Copyright © 2017-2019 Mickaël Salaün <[email protected]>
+ * Copyright © 2018-2019 ANSSI
+ */
+
+#ifndef _UAPI__LINUX_LANDLOCK_H__
+#define _UAPI__LINUX_LANDLOCK_H__
+
+#include <linux/types.h>
+
+#define LANDLOCK_RET_ALLOW	0
+#define LANDLOCK_RET_DENY	1
+
+struct landlock_context_ptrace {
+	__u64 tracer;
+	__u64 tracee;
+};
+
+#endif /* _UAPI__LINUX_LANDLOCK_H__ */
tools/lib/bpf/libbpf_probes.c +3 -0
index 4b0b0364f5fc..1e0d6346a7c7 100644
@@ -78,6 +78,9 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
 	case BPF_PROG_TYPE_KPROBE:
 		xattr.kern_version = get_kernel_version();
 		break;
+	case BPF_PROG_TYPE_LANDLOCK_HOOK:
+		xattr.expected_attach_type = BPF_LANDLOCK_PTRACE;
+		break;
 	case BPF_PROG_TYPE_UNSPEC:
 	case BPF_PROG_TYPE_SOCKET_FILTER:
 	case BPF_PROG_TYPE_SCHED_CLS:
tools/testing/selftests/bpf/config +3 -0
index 5dc109f4c097..3161a88a6059 100644
@@ -35,3 +35,6 @@ CONFIG_MPLS_ROUTING=m
 CONFIG_MPLS_IPTUNNEL=m
 CONFIG_IPV6_SIT=m
 CONFIG_BPF_JIT=y
+CONFIG_SECCOMP_FILTER=y
+CONFIG_SECURITY=y
+CONFIG_SECURITY_LANDLOCK=y
tools/testing/selftests/bpf/test_verifier.c +1 -0
index d27fd929abb9..74f249dafc0b 100644
@@ -30,6 +30,7 @@
 #include <linux/bpf.h>
 #include <linux/if_ether.h>
 #include <linux/btf.h>
+#include <linux/landlock.h>
 
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
tools/testing/selftests/bpf/verifier/landlock.c new file +56 -0

56 lines not on this page · raw message

tools/testing/selftests/landlock/.gitignore new file +5 -0
new file mode 100644
index 000000000000..4c5c01d23fe0
@@ -0,0 +1,5 @@
+/feature
+/fixdep
+/*libbpf*
+/test_base
+/test_ptrace
tools/testing/selftests/landlock/Makefile new file +27 -0
new file mode 100644
index 000000000000..2da77c30e77f
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0
+
+LIBDIR := $(abspath ../../../lib)
+BPFDIR := $(LIBDIR)/bpf
+TOOLSDIR := $(abspath ../../../include)
+APIDIR := $(TOOLSDIR)/uapi
+
+CFLAGS += -g -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(TOOLSDIR)
+LDLIBS += -lelf
+
+test_src = $(wildcard test_*.c)
+
+TEST_GEN_PROGS := $(test_src:.c=)
+
+include ../lib.mk
+
+BPFOBJ := $(OUTPUT)/libbpf.a
+
+$(TEST_GEN_PROGS): $(BPFOBJ) ../kselftest_harness.h
+
+.PHONY: force
+
+# force a rebuild of BPFOBJ when its dependencies are updated
+force:
+
+$(BPFOBJ): force
+	$(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
tools/testing/selftests/landlock/config new file +5 -0
new file mode 100644
index 000000000000..fa5081b840ad
@@ -0,0 +1,5 @@
+CONFIG_BPF=y
+CONFIG_BPF_SYSCALL=y
+CONFIG_SECCOMP_FILTER=y
+CONFIG_SECURITY=y
+CONFIG_SECURITY_LANDLOCK=y
tools/testing/selftests/landlock/test.h new file +48 -0

48 lines not on this page · raw message

tools/testing/selftests/landlock/test_base.c new file +24 -0
new file mode 100644
index 000000000000..db46f39048cb
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Landlock tests - base
+ *
+ * Copyright © 2017-2019 Mickaël Salaün <[email protected]>
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+
+#include "test.h"
+
+TEST(seccomp_landlock)
+{
+	int ret;
+
+	ret = seccomp(SECCOMP_PREPEND_LANDLOCK_PROG, 0, NULL);
+	EXPECT_EQ(-1, ret);
+	EXPECT_EQ(EFAULT, errno) {
+		TH_LOG("Kernel does not support CONFIG_SECURITY_LANDLOCK");
+	}
+}
+
+TEST_HARNESS_MAIN
tools/testing/selftests/landlock/test_ptrace.c new file +289 -0

289 lines not on this page · raw message

Signature · 1 line
2.23.0
author maintainer
#25 lore raw

PATCH v13 7/7 landlock: Add user and kernel documentation for Landlock

In reply to Mickaël Salaün

This documentation can be built with the Sphinx framework.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc James Morris <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Kees Cook <[email protected]>
Cc Serge E. Hallyn <[email protected]>
Cc Will Drewry <[email protected]>

---

Changes since v12:
* enhance the "unprivileged use" explanation
* add more explanation about the domain/credentials inheritance
* update and add self-reference Sphinx links
* more clearly explain the capability-based security principles for program context

Changes since v11:
* cosmetic improvements

Changes since v10:
* replace the filesystem hooks with the ptrace one
* remove the triggers
* update example
* add documenation for Landlock domains and seccomp interaction
* reference more kernel documenation (e.g. LSM hooks)

Changes since v9:
* update with expected attach type and expected attach triggers

Changes since v8:
* remove documentation related to chaining and tagging according to this patch series

Changes since v7:
* update documentation according to the Landlock revamp

Changes since v6:
* add a check for ctx->event
* rename BPF_PROG_TYPE_LANDLOCK to BPF_PROG_TYPE_LANDLOCK_RULE
* rename Landlock version to ABI to better reflect its purpose and add a dedicated changelog section
* update tables
* relax no_new_privs recommendations
* remove ABILITY_WRITE related functions
* reword rule "appending" to "prepending" and explain it
* cosmetic fixes

Changes since v5:
* update the rule hierarchy inheritance explanation
* briefly explain ctx->arg2
* add ptrace restrictions
* explain EPERM
* update example (subtype)
* use ":manpage:"

---
 Documentation/security/index.rst           |   1 +
 Documentation/security/landlock/index.rst  |  22 +++
 Documentation/security/landlock/kernel.rst | 166 +++++++++++++++++++++
 Documentation/security/landlock/user.rst   | 153 +++++++++++++++++++
 4 files changed, 342 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
index fc503dd689a7..4d213e76ddf4 100644
@@ -15,3 +15,4 @@ Security Documentation
    self-protection
    siphash
    tpm/index
+   landlock/index
Documentation/security/landlock/index.rst new file +22 -0
new file mode 100644
index 000000000000..1eced757b05d
@@ -0,0 +1,22 @@
+=========================================
+Landlock LSM: programmatic access control
+=========================================
+
+:Author: Mickaël Salaün
+
+Landlock is a stackable Linux Security Module (LSM) that makes it possible to
+create security sandboxes, programmable access-controls or safe endpoint
+security agents.  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 processes and hence make a decision according to the
+kernel semantic.
+
+.. toctree::
+
+    user
+    kernel
Documentation/security/landlock/kernel.rst new file +166 -0

166 lines not on this page · raw message

Documentation/security/landlock/user.rst new file +153 -0

153 lines not on this page · raw message

Signature · 1 line
2.23.0