Skip to contentSkip to conversation

MAC and Audit policy using eBPF (KRSI)

PATCH Superseded KP Singh 7 participants 32 messages Jan 15, 2020 – Jan 20, 2020

Subsystems (8)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

KP Singh

author
#0 lore raw

v2 cover MAC and Audit policy using eBPF (KRSI)

From: KP Singh <[email protected]>

# Changes since v1 (https://lore.kernel.org/bpf/[email protected]/):

* Eliminate the requirement to maintain LSM hooks separately in security/bpf/hooks.h Use BPF trampolines to dynamically allocate security hooks
* Drop the use of securityfs as bpftool provides the required introspection capabilities. Update the tests to use the bpf_skeleton and global variables
* Use O_CLOEXEC anonymous fds to represent BPF attachment in line with the other BPF programs with the possibility to use bpf program pinning in the future to provide "permanent attachment".
* Drop the logic based on prog names for handling re-attachment.
* Drop bpf_lsm_event_output from this series and send it as a separate patch.

# Motivation

Google does analysis of rich runtime security data to detect and thwart threats in real-time. Currently, this is done in custom kernel modules but we would like to replace this with something that's upstream and useful to others.

The current kernel infrastructure for providing telemetry (Audit, Perf etc.) is disjoint from access enforcement (i.e. LSMs). Augmenting the information provided by audit requires kernel changes to audit, its policy language and user-space components. Furthermore, building a MAC policy based on the newly added telemetry data requires changes to various LSMs and their respective policy languages.

This patchset proposes a new stackable and privileged LSM which allows the LSM hooks to be implemented using eBPF. This facilitates a unified and dynamic (not requiring re-compilation of the kernel) audit and MAC policy.

# Why an LSM?

Linux Security Modules target security behaviours rather than the kernel's API. For example, it's easy to miss out a newly added system call for executing processes (eg. execve, execveat etc.) but the LSM framework ensures that all process executions trigger the relevant hooks irrespective of how the process was executed.

Allowing users to implement LSM hooks at runtime also benefits the LSM eco-system by enabling a quick feedback loop from the security community about the kind of behaviours that the LSM Framework should be targeting.

# How does it work?

The LSM introduces a new eBPF (https://docs.cilium.io/en/v1.6/bpf/)
program type BPF_PROG_TYPE_LSM which can only be attached to LSM hooks. Attachment requires CAP_SYS_ADMIN for loading eBPF programs and CAP_MAC_ADMIN for modifying MAC policies.

The eBPF programs are attached to a separate security_hook_heads maintained by the BPF LSM for mutable hooks and executed after all the statically defined hooks (i.e. the ones declared by SELinux, AppArmor, Smack etc). This also ensures that statically defined LSM hooks retain the behaviour of "being read-only after init", i.e. __lsm_ro_after_init.

Upon attachment, a security hook is dynamically allocated with arch_bpf_prepare_trampoline which generates code to handle the conversion from the signature of the hook to the BPF context and allows the JIT'ed BPF program to be called as a C function with the same arguments as the LSM hooks. If any of the attached eBPF programs returns an error (like ENOPERM), the behaviour represented by the hook is denied.

Audit logs can be written using a format chosen by the eBPF program to the perf events buffer or to global eBPF variables or maps and can be further processed in user-space.

# BTF Based Design

The current design uses BTF
(https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html,
https://lwn.net/Articles/803258/) which allows verifiable read-only structure accesses by field names rather than fixed offsets. This allows accessing the hook parameters using a dynamically created context which provides a certain degree of ABI stability:

// Only declare the structure and fields intended to be used // in the program

struct vm_area_struct {
	unsigned long vm_start;
} __attribute__((preserve_access_index));

// Declare the eBPF program mprotect_audit which attaches to // to the file_mprotect LSM hook and accepts three arguments. SEC("lsm/file_mprotect")
int BPF_PROG(mprotect_audit, struct vm_area_struct *vma,
unsigned long reqprot, unsigned long prot)

{
	unsigned long vm_start = vma->vm_start;

	return 0;
}

By relocating field offsets, BTF makes a large portion of kernel data structures readily accessible across kernel versions without requiring a large corpus of BPF helper functions and requiring recompilation with every kernel version. The BTF type information is also used by the BPF verifier to validate memory accesses within the BPF program and also prevents arbitrary writes to the kernel memory.

The limitations of BTF compatibility are described in BPF Co-Re
(http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf, i.e. field renames, #defines and changes to the signature of LSM hooks).

This design imposes that the MAC policy (eBPF programs) be updated when the inspected kernel structures change outside of BTF compatibility guarantees. In practice, this is only required when a structure field used by a current policy is removed (or renamed) or when the used LSM hooks change. We expect the maintenance cost of these changes to be acceptable as compared to the previous design
(https://lore.kernel.org/bpf/[email protected]/).

# Why not tracepoints or kprobes?

In order to do MAC with tracepoints or kprobes, we would need to override the return value of the security hook. This is not possible with tracepoints or call-site kprobes.

Attaching to the return boundary (kretprobe) implies that BPF programs would always get called after all the other LSM hooks are called and clobber the pre-existing LSM semantics.

Enforcing MAC policy with an actual LSM helps leverage the verified semantics of the framework.

# Usage Examples

A simple example and some documentation is included in the patchset.

In order to better illustrate the capabilities of the framework some more advanced prototype (not-ready for review) code has also been published separately:

* Logging execution events (including environment variables and arguments)
https://github.com/sinkap/linux-krsi/blob/patch/v1/examples/samples/bpf/lsm_audit_env.c
* Detecting deletion of running executables:
https://github.com/sinkap/linux-krsi/blob/patch/v1/examples/samples/bpf/lsm_detect_exec_unlink.c
* Detection of writes to /proc/<pid>/mem:

https://github.com/sinkap/linux-krsi/blob/patch/v1/examples/samples/bpf/lsm_audit_env.c

We have updated Google's internal telemetry infrastructure and have started deploying this LSM on our Linux Workstations. This gives us more confidence in the real-world applications of such a system.

KP Singh (10):
bpf: btf: Make some of the API visible outside BTF
bpf: lsm: Add a skeleton and config options
bpf: lsm: Introduce types for eBPF based LSM
bpf: lsm: Add mutable hooks list for the BPF LSM
bpf: lsm: BTF API for LSM hooks
bpf: lsm: Implement attach, detach and execution
bpf: lsm: Make the allocated callback RO+X tools/libbpf: Add support for BPF_PROG_TYPE_LSM
bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
bpf: lsm: Add Documentation

 Documentation/security/bpf.rst                | 150 ++++++++
 Documentation/security/index.rst              |   1 +
 MAINTAINERS                                   |  11 +
 include/linux/bpf.h                           |   4 +
 include/linux/bpf_lsm.h                       |  98 +++++
 include/linux/bpf_types.h                     |   4 +
 include/linux/btf.h                           |   8 +
 include/uapi/linux/bpf.h                      |   6 +
 kernel/bpf/btf.c                              |  17 +
 kernel/bpf/syscall.c                          |  51 ++-
 kernel/bpf/verifier.c                         |  74 +++-
 security/Kconfig                              |  11 +-
 security/Makefile                             |   2 +
 security/bpf/Kconfig                          |  25 ++
 security/bpf/Makefile                         |   7 +
 security/bpf/hooks.c                          | 337 ++++++++++++++++++
 security/bpf/include/bpf_lsm.h                |  75 ++++
 security/bpf/lsm.c                            |  92 +++++
 security/bpf/ops.c                            |  30 ++
 security/security.c                           |  24 +-
 tools/include/uapi/linux/bpf.h                |   6 +
 tools/lib/bpf/bpf.c                           |   6 +-
 tools/lib/bpf/bpf.h                           |   1 +
 tools/lib/bpf/libbpf.c                        | 143 +++++++-
 tools/lib/bpf/libbpf.h                        |   4 +
 tools/lib/bpf/libbpf.map                      |   3 +
 tools/lib/bpf/libbpf_probes.c                 |   1 +
 tools/testing/selftests/bpf/lsm_helpers.h     |  19 +
 .../bpf/prog_tests/lsm_mprotect_audit.c       |  58 +++
 .../selftests/bpf/progs/lsm_mprotect_audit.c  |  48 +++
 30 files changed, 1271 insertions(+), 45 deletions(-)
 create mode 100644 Documentation/security/bpf.rst
 create mode 100644 include/linux/bpf_lsm.h
 create mode 100644 security/bpf/Kconfig
 create mode 100644 security/bpf/Makefile
 create mode 100644 security/bpf/hooks.c
 create mode 100644 security/bpf/include/bpf_lsm.h
 create mode 100644 security/bpf/lsm.c
 create mode 100644 security/bpf/ops.c
 create mode 100644 tools/testing/selftests/bpf/lsm_helpers.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v2 1/10 bpf: btf: Make some of the API visible outside BTF

From: KP Singh <[email protected]>

- Add an extern for btf_vmlinux in btf.h
- Add btf_type_by_name_kind, the LSM code does the combination of btf_find_by_name_kind and btf_type_by_id quite often.

---
 include/linux/btf.h |  8 ++++++++
 kernel/bpf/btf.c    | 17 +++++++++++++++++
 2 files changed, 25 insertions(+)
include/linux/btf.h +8 -0
index 881e9b76ef49..dc650d294bc4 100644
@@ -15,6 +15,7 @@ struct btf_type;
 union bpf_attr;
 
 extern const struct file_operations btf_fops;
+extern struct btf *btf_vmlinux;
 
 void btf_put(struct btf *btf);
 int btf_new_fd(const union bpf_attr *attr);
@@ -66,6 +67,8 @@ const struct btf_type *
 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
 		 u32 *type_size, const struct btf_type **elem_type,
 		 u32 *total_nelems);
+const struct btf_type *btf_type_by_name_kind(
+	struct btf *btf, const char *name, u8 kind);
 
 #define for_each_member(i, struct_type, member)			\
 	for (i = 0, member = btf_type_member(struct_type);	\
@@ -142,6 +145,11 @@ static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
 {
 	return NULL;
 }
+static inline const struct btf_type *btf_type_by_name_kind(
+	struct btf *btf, const char *name, u8 kind)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
 static inline const char *btf_name_by_offset(const struct btf *btf,
 					     u32 offset)
 {
kernel/bpf/btf.c +17 -0
index 832b5d7fd892..b8968cec8718 100644
@@ -436,6 +436,23 @@ const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
 	return NULL;
 }
 
+const struct btf_type *btf_type_by_name_kind(
+	struct btf *btf, const char *name, u8 kind)
+{
+	const struct btf_type *t;
+	s32 type_id;
+
+	type_id = btf_find_by_name_kind(btf, name, kind);
+	if (type_id < 0)
+		return ERR_PTR(-EINVAL);
+
+	t = btf_type_by_id(btf, type_id);
+	if (!t)
+		return ERR_PTR(-EINVAL);
+
+	return t;
+}
+
 /* Types that act only as a source, not sink or intermediate
  * type when resolving.
  */
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH bpf-next v2 01/10] bpf: btf: Make some of the API visible outside BTF

Thank you for the patch! Yet something to improve:

Hi KP,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20200116]
[cannot apply to bpf-next/master bpf/master linus/master security/next-testing v5.5-rc6 v5.5-rc5 v5.5-rc4 v5.5-rc6] [if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/KP-Singh/MAC-and-Audit-policy-using-eBPF-KRSI/20200117-070342
base:    2747d5fdab78f43210256cd52fb2718e0b3cce74
config: nds32-defconfig (attached as .config)
compiler: nds32le-linux-gcc (GCC) 9.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=9.2.0 make.cross ARCH=nds32

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from kernel/bpf/core.c:27:

>> include/linux/btf.h:148:38: error: static declaration of 'btf_type_by_name_kind' follows non-static declaration
     148 | static inline const struct btf_type *btf_type_by_name_kind(
         |                                      ^~~~~~~~~~~~~~~~~~~~~
   include/linux/btf.h:70:24: note: previous declaration of 'btf_type_by_name_kind' was here
      70 | const struct btf_type *btf_type_by_name_kind(
         |                        ^~~~~~~~~~~~~~~~~~~~~

vim +/btf_type_by_name_kind +148 include/linux/btf.h

136
137 #ifdef CONFIG_BPF_SYSCALL
138 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
139 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
140 struct btf *btf_parse_vmlinux(void);
141 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
142 #else
143 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
144 u32 type_id)
145 {
146 return NULL;
147 }

 > 148	static inline const struct btf_type *btf_type_by_name_kind(

149 struct btf *btf, const char *name, u8 kind)
150 {
151 return ERR_PTR(-EOPNOTSUPP);
152 }
153 static inline const char *btf_name_by_offset(const struct btf *btf,
154 u32 offset)
155 {
156 return NULL;
157 }
158 #endif
159

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

author
#3 lore raw

Re: [PATCH bpf-next v2 01/10] bpf: btf: Make some of the API visible outside BTF

Thanks! I have fixed this in the v3 of the series. btf_find_by_name_kind is independant of CONFIG_BPF_SYSCALL and btf_type_by_name_kind needs to be as well.

The mistake was adding a static inline definition of the function in the !CONFIG_BPF_SYSCALL section which is not needed in this case.

- KP

Quoted 63 lines · kbuild test robot

Hi KP,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20200116]
[cannot apply to bpf-next/master bpf/master linus/master security/next-testing v5.5-rc6 v5.5-rc5 v5.5-rc4 v5.5-rc6] [if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/KP-Singh/MAC-and-Audit-policy-using-eBPF-KRSI/20200117-070342
base:    2747d5fdab78f43210256cd52fb2718e0b3cce74
config: nds32-defconfig (attached as .config)
compiler: nds32le-linux-gcc (GCC) 9.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=9.2.0 make.cross ARCH=nds32

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from kernel/bpf/core.c:27:

Quoted 1 line
Quoted 1 line

include/linux/btf.h:148:38: error: static declaration of 'btf_type_by_name_kind' follows non-static declaration

     148 | static inline const struct btf_type *btf_type_by_name_kind(
         |                                      ^~~~~~~~~~~~~~~~~~~~~
   include/linux/btf.h:70:24: note: previous declaration of 'btf_type_by_name_kind' was here
      70 | const struct btf_type *btf_type_by_name_kind(
         |                        ^~~~~~~~~~~~~~~~~~~~~

vim +/btf_type_by_name_kind +148 include/linux/btf.h

   136	
   137	#ifdef CONFIG_BPF_SYSCALL
   138	const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
   139	const char *btf_name_by_offset(const struct btf *btf, u32 offset);
   140	struct btf *btf_parse_vmlinux(void);
   141	struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
   142	#else
   143	static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
   144							    u32 type_id)
   145	{
   146		return NULL;
   147	}
Quoted 1 line

148 static inline const struct btf_type *btf_type_by_name_kind(

149 struct btf *btf, const char *name, u8 kind)

   150	{
   151		return ERR_PTR(-EOPNOTSUPP);
   152	}
   153	static inline const char *btf_name_by_offset(const struct btf *btf,
   154						     u32 offset)
   155	{
   156		return NULL;
   157	}
   158	#endif
   159	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/[email protected] Intel Corporation

author
#4 lore raw

PATCH v2 2/10 bpf: lsm: Add a skeleton and config options

In reply to KP Singh

From: KP Singh <[email protected]>

The LSM can be enabled by CONFIG_SECURITY_BPF. Without CONFIG_SECURITY_BPF_ENFORCE, the LSM will run the attached eBPF programs but not enforce MAC policy based on the return value of the attached programs.

---
 MAINTAINERS           |  7 +++++++
 security/Kconfig      | 11 ++++++-----
 security/Makefile     |  2 ++
 security/bpf/Kconfig  | 22 ++++++++++++++++++++++
 security/bpf/Makefile |  5 +++++
 security/bpf/lsm.c    | 25 +++++++++++++++++++++++++
 6 files changed, 67 insertions(+), 5 deletions(-)
 create mode 100644 security/bpf/Kconfig
 create mode 100644 security/bpf/Makefile
 create mode 100644 security/bpf/lsm.c
MAINTAINERS +7 -0
index 66a2e5e07117..0941f478cfa5 100644
@@ -3203,6 +3203,13 @@ S:	Supported
 F:	arch/x86/net/
 X:	arch/x86/net/bpf_jit_comp32.c
 
+BPF SECURITY MODULE
+M:	KP Singh <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	security/bpf/
+
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
 L:	[email protected]
security/Kconfig +6 -5
index 2a1a2d396228..6f1aab195e7d 100644
@@ -236,6 +236,7 @@ source "security/tomoyo/Kconfig"
 source "security/apparmor/Kconfig"
 source "security/loadpin/Kconfig"
 source "security/yama/Kconfig"
+source "security/bpf/Kconfig"
 source "security/safesetid/Kconfig"
 source "security/lockdown/Kconfig"
 
@@ -277,11 +278,11 @@ endchoice
 
 config LSM
 	string "Ordered list of enabled LSMs"
-	default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK
-	default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR
-	default "lockdown,yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO
-	default "lockdown,yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC
-	default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor"
+	default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK
+	default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR
+	default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO
+	default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC
+	default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf"
 	help
 	  A comma-separated list of LSMs, in initialization order.
 	  Any LSMs left off this list will be ignored. This can be
security/Makefile +2 -0
index be1dd9d2cb2f..50e6821dd7b7 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_BPF)		+= bpf
 
 # 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_BPF)		+= bpf/
 obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
 
 # Object integrity file lists
security/bpf/Kconfig new file +22 -0
new file mode 100644
index 000000000000..a5f6c67ae526
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+config SECURITY_BPF
+	bool "BPF-based MAC and audit policy"
+	depends on SECURITY
+	depends on BPF_SYSCALL
+	help
+	  This enables instrumentation of the security hooks with
+	  eBPF programs.
+
+	  If you are unsure how to answer this question, answer N.
+
+config SECURITY_BPF_ENFORCE
+	bool "Deny operations based on the evaluation of the attached programs"
+	depends on SECURITY_BPF
+	help
+	  eBPF programs attached to hooks can be used for both auditing and
+	  enforcement. Enabling enforcement implies that the evaluation result
+	  from the attached eBPF programs will allow or deny the operation
+	  guarded by the security hook.
security/bpf/Makefile new file +5 -0
new file mode 100644
index 000000000000..26a0ab6f99b7
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+obj-$(CONFIG_SECURITY_BPF) := lsm.o
security/bpf/lsm.c new file +25 -0
new file mode 100644
index 000000000000..5c5c14f990ce
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/lsm_hooks.h>
+
+/* This is only for internal hooks, always statically shipped as part of the
+ * BPF LSM. Statically defined hooks are appeneded to the security_hook_heads
+ * which is common for LSMs and R/O after init.
+ */
+static struct security_hook_list lsm_hooks[] __lsm_ro_after_init = {};
+
+static int __init lsm_init(void)
+{
+	security_add_hooks(lsm_hooks, ARRAY_SIZE(lsm_hooks), "bpf");
+	pr_info("eBPF and LSM are friends now.\n");
+	return 0;
+}
+
+DEFINE_LSM(bpf) = {
+	.name = "bpf",
+	.init = lsm_init,
+};
Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH bpf-next v2 02/10] bpf: lsm: Add a skeleton and config options

Quoted 135 lines · KP Singh

From: KP Singh <[email protected]>

The LSM can be enabled by CONFIG_SECURITY_BPF. Without CONFIG_SECURITY_BPF_ENFORCE, the LSM will run the attached eBPF programs but not enforce MAC policy based on the return value of the attached programs.

---
 MAINTAINERS           |  7 +++++++
 security/Kconfig      | 11 ++++++-----
 security/Makefile     |  2 ++
 security/bpf/Kconfig  | 22 ++++++++++++++++++++++
 security/bpf/Makefile |  5 +++++
 security/bpf/lsm.c    | 25 +++++++++++++++++++++++++
 6 files changed, 67 insertions(+), 5 deletions(-)
 create mode 100644 security/bpf/Kconfig
 create mode 100644 security/bpf/Makefile
 create mode 100644 security/bpf/lsm.c
MAINTAINERS +7 -0
index 66a2e5e07117..0941f478cfa5 100644
@@ -3203,6 +3203,13 @@ S:	Supported
 F:	arch/x86/net/
 X:	arch/x86/net/bpf_jit_comp32.c
 
+BPF SECURITY MODULE
+M:	KP Singh <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	security/bpf/
+
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
 L:	[email protected]
security/Kconfig +6 -5
index 2a1a2d396228..6f1aab195e7d 100644
@@ -236,6 +236,7 @@ source "security/tomoyo/Kconfig"
 source "security/apparmor/Kconfig"
 source "security/loadpin/Kconfig"
 source "security/yama/Kconfig"
+source "security/bpf/Kconfig"
 source "security/safesetid/Kconfig"
 source "security/lockdown/Kconfig"
 
@@ -277,11 +278,11 @@ endchoice
 
 config LSM
 	string "Ordered list of enabled LSMs"
-	default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK
-	default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR
-	default "lockdown,yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO
-	default "lockdown,yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC
-	default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor"
+	default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK
+	default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR
+	default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO
+	default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC
+	default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf"
 	help
 	  A comma-separated list of LSMs, in initialization order.
 	  Any LSMs left off this list will be ignored. This can be
security/Makefile +2 -0
index be1dd9d2cb2f..50e6821dd7b7 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_BPF)		+= bpf
 
 # 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_BPF)		+= bpf/
 obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
 
 # Object integrity file lists
security/bpf/Kconfig new file +22 -0
new file mode 100644
index 000000000000..a5f6c67ae526
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+config SECURITY_BPF
+	bool "BPF-based MAC and audit policy"
+	depends on SECURITY
+	depends on BPF_SYSCALL
+	help
+	  This enables instrumentation of the security hooks with
+	  eBPF programs.
+
+	  If you are unsure how to answer this question, answer N.
+
+config SECURITY_BPF_ENFORCE
+	bool "Deny operations based on the evaluation of the attached programs"
+	depends on SECURITY_BPF
+	help
+	  eBPF programs attached to hooks can be used for both auditing and
+	  enforcement. Enabling enforcement implies that the evaluation result
+	  from the attached eBPF programs will allow or deny the operation
+	  guarded by the security hook.
security/bpf/Makefile new file +5 -0
new file mode 100644
index 000000000000..26a0ab6f99b7
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+obj-$(CONFIG_SECURITY_BPF) := lsm.o
security/bpf/lsm.c new file +3 -0
new file mode 100644
index 000000000000..5c5c14f990ce
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*

security/bpf/lsm.c line 13

Quoted 10 lines
security/bpf/lsm.c new file +10 -0
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/lsm_hooks.h>
+
+/* This is only for internal hooks, always statically shipped as part of the
+ * BPF LSM. Statically defined hooks are appeneded to the security_hook_heads
+ * which is common for LSMs and R/O after init.
+ */
+static struct security_hook_list lsm_hooks[] __lsm_ro_after_init = {};

s/lsm_hooks/bpf_hooks/

The lsm prefix is for the infrastructure. The way you have it is massively confusing.

security/bpf/lsm.c line 15

Quoted 2 lines
security/bpf/lsm.c new file +2 -0
+
+static int __init lsm_init(void)

s/lsm_init/bpf_init/

Same reason. When I'm looking at several security modules at once I need to be able to tell them apart.

security/bpf/lsm.c line 18

Quoted 3 lines
security/bpf/lsm.c new file +3 -0
+{
+	security_add_hooks(lsm_hooks, ARRAY_SIZE(lsm_hooks), "bpf");
+	pr_info("eBPF and LSM are friends now.\n");

Cute message, but not very informative if you haven't read the code. "LSM support for eBPF active\n" is more likely to be comprehensible.

Quoted 7 lines
security/bpf/lsm.c new file +7 -0
+	return 0;
+}
+
+DEFINE_LSM(bpf) = {
+	.name = "bpf",
+	.init = lsm_init,
+};
author
#6 lore raw

Re: [PATCH bpf-next v2 02/10] bpf: lsm: Add a skeleton and config options

Quoted 149 lines · Casey Schaufler
Quoted 135 lines · KP Singh

From: KP Singh <[email protected]>

The LSM can be enabled by CONFIG_SECURITY_BPF. Without CONFIG_SECURITY_BPF_ENFORCE, the LSM will run the attached eBPF programs but not enforce MAC policy based on the return value of the attached programs.

---
 MAINTAINERS           |  7 +++++++
 security/Kconfig      | 11 ++++++-----
 security/Makefile     |  2 ++
 security/bpf/Kconfig  | 22 ++++++++++++++++++++++
 security/bpf/Makefile |  5 +++++
 security/bpf/lsm.c    | 25 +++++++++++++++++++++++++
 6 files changed, 67 insertions(+), 5 deletions(-)
 create mode 100644 security/bpf/Kconfig
 create mode 100644 security/bpf/Makefile
 create mode 100644 security/bpf/lsm.c
MAINTAINERS +7 -0
index 66a2e5e07117..0941f478cfa5 100644
@@ -3203,6 +3203,13 @@ S:	Supported
 F:	arch/x86/net/
 X:	arch/x86/net/bpf_jit_comp32.c
 
+BPF SECURITY MODULE
+M:	KP Singh <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	security/bpf/
+
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
 L:	[email protected]
security/Kconfig +6 -5
index 2a1a2d396228..6f1aab195e7d 100644
@@ -236,6 +236,7 @@ source "security/tomoyo/Kconfig"
 source "security/apparmor/Kconfig"
 source "security/loadpin/Kconfig"
 source "security/yama/Kconfig"
+source "security/bpf/Kconfig"
 source "security/safesetid/Kconfig"
 source "security/lockdown/Kconfig"
 
@@ -277,11 +278,11 @@ endchoice
 
 config LSM
 	string "Ordered list of enabled LSMs"
-	default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor" if DEFAULT_SECURITY_SMACK
-	default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo" if DEFAULT_SECURITY_APPARMOR
-	default "lockdown,yama,loadpin,safesetid,integrity,tomoyo" if DEFAULT_SECURITY_TOMOYO
-	default "lockdown,yama,loadpin,safesetid,integrity" if DEFAULT_SECURITY_DAC
-	default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor"
+	default "lockdown,yama,loadpin,safesetid,integrity,smack,selinux,tomoyo,apparmor,bpf" if DEFAULT_SECURITY_SMACK
+	default "lockdown,yama,loadpin,safesetid,integrity,apparmor,selinux,smack,tomoyo,bpf" if DEFAULT_SECURITY_APPARMOR
+	default "lockdown,yama,loadpin,safesetid,integrity,tomoyo,bpf" if DEFAULT_SECURITY_TOMOYO
+	default "lockdown,yama,loadpin,safesetid,integrity,bpf" if DEFAULT_SECURITY_DAC
+	default "lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf"
 	help
 	  A comma-separated list of LSMs, in initialization order.
 	  Any LSMs left off this list will be ignored. This can be
security/Makefile +2 -0
index be1dd9d2cb2f..50e6821dd7b7 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_BPF)		+= bpf
 
 # 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_BPF)		+= bpf/
 obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
 
 # Object integrity file lists
security/bpf/Kconfig new file +22 -0
new file mode 100644
index 000000000000..a5f6c67ae526
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+config SECURITY_BPF
+	bool "BPF-based MAC and audit policy"
+	depends on SECURITY
+	depends on BPF_SYSCALL
+	help
+	  This enables instrumentation of the security hooks with
+	  eBPF programs.
+
+	  If you are unsure how to answer this question, answer N.
+
+config SECURITY_BPF_ENFORCE
+	bool "Deny operations based on the evaluation of the attached programs"
+	depends on SECURITY_BPF
+	help
+	  eBPF programs attached to hooks can be used for both auditing and
+	  enforcement. Enabling enforcement implies that the evaluation result
+	  from the attached eBPF programs will allow or deny the operation
+	  guarded by the security hook.
security/bpf/Makefile new file +5 -0
new file mode 100644
index 000000000000..26a0ab6f99b7
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+obj-$(CONFIG_SECURITY_BPF) := lsm.o
security/bpf/lsm.c new file +3 -0
new file mode 100644
index 000000000000..5c5c14f990ce
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*

security/bpf/lsm.c line 13

Quoted 10 lines
security/bpf/lsm.c new file +10 -0
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/lsm_hooks.h>
+
+/* This is only for internal hooks, always statically shipped as part of the
+ * BPF LSM. Statically defined hooks are appeneded to the security_hook_heads
+ * which is common for LSMs and R/O after init.
+ */
+static struct security_hook_list lsm_hooks[] __lsm_ro_after_init = {};

s/lsm_hooks/bpf_hooks/

Quoted 1 line

The lsm prefix is for the infrastructure. The way you have it is massively confusing.

Good point, I changed this to bpf_lsm_hooks as we prefix most types with bpf_lsm_

Quoted 8 lines

security/bpf/lsm.c line 15

Quoted 3 lines
security/bpf/lsm.c new file +2 -0
+
+static int __init lsm_init(void)

s/lsm_init/bpf_init/

Same reason. When I'm looking at several security modules at once I need to be able to tell them apart.

Changed to bpf_lsm_init.

Quoted 7 lines

security/bpf/lsm.c line 18

Quoted 4 lines
security/bpf/lsm.c new file +3 -0
+{
+	security_add_hooks(lsm_hooks, ARRAY_SIZE(lsm_hooks), "bpf");
+	pr_info("eBPF and LSM are friends now.\n");

Cute message, but not very informative if you haven't read the code. "LSM support for eBPF active\n" is more likely to be comprehensible.

Agreed, Updated :)

- KP

Quoted 9 lines
Quoted 8 lines
security/bpf/lsm.c new file +7 -0
+	return 0;
+}
+
+DEFINE_LSM(bpf) = {
+	.name = "bpf",
+	.init = lsm_init,
+};
author
#7 lore raw

PATCH v2 3/10 bpf: lsm: Introduce types for eBPF based LSM

In reply to KP Singh

From: KP Singh <[email protected]>

A new eBPF program type BPF_PROG_TYPE_LSM with an expected attach type of BPF_LSM_MAC. Attachment to LSM hooks is not implemented in this patch.

On defining the types for the program, the macros expect that <prog_name>_prog_ops and <prog_name>_verifier_ops exist. This is implicitly required by the macro:

BPF_PROG_TYPE(BPF_PROG_TYPE_LSM, lsm, ...)

---
 include/linux/bpf_types.h      |  4 ++++
 include/uapi/linux/bpf.h       |  2 ++
 kernel/bpf/syscall.c           |  6 ++++++
 security/bpf/Makefile          |  2 +-
 security/bpf/ops.c             | 28 ++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  2 ++
 tools/lib/bpf/libbpf_probes.c  |  1 +
 7 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 security/bpf/ops.c
include/linux/bpf_types.h +4 -0
index 9f326e6ef885..2f5b054500d7 100644
@@ -68,6 +68,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SK_REUSEPORT, sk_reuseport,
 #if defined(CONFIG_BPF_JIT)
 BPF_PROG_TYPE(BPF_PROG_TYPE_STRUCT_OPS, bpf_struct_ops,
 	      void *, void *)
+#ifdef CONFIG_SECURITY_BPF
+BPF_PROG_TYPE(BPF_PROG_TYPE_LSM, lsm,
+	       void *, void *)
+#endif /* CONFIG_SECURITY_BPF */
 #endif
 
 BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
include/uapi/linux/bpf.h +2 -0
index 52966e758fe5..b6a725a8a21d 100644
@@ -176,6 +176,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_CGROUP_SOCKOPT,
 	BPF_PROG_TYPE_TRACING,
 	BPF_PROG_TYPE_STRUCT_OPS,
+	BPF_PROG_TYPE_LSM,
 };
 
 enum bpf_attach_type {
@@ -205,6 +206,7 @@ enum bpf_attach_type {
 	BPF_TRACE_RAW_TP,
 	BPF_TRACE_FENTRY,
 	BPF_TRACE_FEXIT,
+	BPF_LSM_MAC,
 	__MAX_BPF_ATTACH_TYPE
 };
 
kernel/bpf/syscall.c +6 -0
index f9db72a96ec0..2945739618c9 100644
@@ -2151,6 +2151,9 @@ static int bpf_prog_attach(const union bpf_attr *attr)
 	case BPF_LIRC_MODE2:
 		ptype = BPF_PROG_TYPE_LIRC_MODE2;
 		break;
+	case BPF_LSM_MAC:
+		ptype = BPF_PROG_TYPE_LSM;
+		break;
 	case BPF_FLOW_DISSECTOR:
 		ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
 		break;
@@ -2182,6 +2185,9 @@ static int bpf_prog_attach(const union bpf_attr *attr)
 	case BPF_PROG_TYPE_LIRC_MODE2:
 		ret = lirc_prog_attach(attr, prog);
 		break;
+	case BPF_PROG_TYPE_LSM:
+		ret = -EINVAL;
+		break;
 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
 		ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
 		break;
security/bpf/Makefile +1 -1
index 26a0ab6f99b7..c78a8a056e7e 100644
@@ -2,4 +2,4 @@
 #
 # Copyright 2019 Google LLC.
 
-obj-$(CONFIG_SECURITY_BPF) := lsm.o
+obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o
security/bpf/ops.c new file +28 -0
new file mode 100644
index 000000000000..81c2bd9c0495
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/filter.h>
+#include <linux/bpf.h>
+
+const struct bpf_prog_ops lsm_prog_ops = {
+};
+
+static const struct bpf_func_proto *get_bpf_func_proto(
+	enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+	switch (func_id) {
+	case BPF_FUNC_map_lookup_elem:
+		return &bpf_map_lookup_elem_proto;
+	case BPF_FUNC_get_current_pid_tgid:
+		return &bpf_get_current_pid_tgid_proto;
+	default:
+		return NULL;
+	}
+}
+
+const struct bpf_verifier_ops lsm_verifier_ops = {
+	.get_func_proto = get_bpf_func_proto,
+};
tools/include/uapi/linux/bpf.h +2 -0
index 52966e758fe5..b6a725a8a21d 100644
@@ -176,6 +176,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_CGROUP_SOCKOPT,
 	BPF_PROG_TYPE_TRACING,
 	BPF_PROG_TYPE_STRUCT_OPS,
+	BPF_PROG_TYPE_LSM,
 };
 
 enum bpf_attach_type {
@@ -205,6 +206,7 @@ enum bpf_attach_type {
 	BPF_TRACE_RAW_TP,
 	BPF_TRACE_FENTRY,
 	BPF_TRACE_FEXIT,
+	BPF_LSM_MAC,
 	__MAX_BPF_ATTACH_TYPE
 };
 
tools/lib/bpf/libbpf_probes.c +1 -0
index 8cc992bc532a..2314889369cc 100644
@@ -107,6 +107,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
 	case BPF_PROG_TYPE_TRACING:
 	case BPF_PROG_TYPE_STRUCT_OPS:
+	case BPF_PROG_TYPE_LSM:
 	default:
 		break;
 	}
Signature · 1 line
2.20.1
author
#8 lore raw

PATCH v2 4/10 bpf: lsm: Add mutable hooks list for the BPF LSM

In reply to KP Singh

From: KP Singh <[email protected]>

- The list of hooks registered by an LSM is currently immutable as they are declared with __lsm_ro_after_init and they are attached to a security_hook_heads struct.
- For the BPF LSM we need to de/register the hooks at runtime. Making the existing security_hook_heads mutable broadens an attack vector, so a separate security_hook_heads is added for only those that ~must~ be mutable.
- These mutable hooks are run only after all the static hooks have successfully executed.

This is based on the ideas discussed in:

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

---
 MAINTAINERS             |  1 +
 include/linux/bpf_lsm.h | 71 +++++++++++++++++++++++++++++++++++++++++
 security/bpf/Kconfig    |  1 +
 security/bpf/Makefile   |  2 +-
 security/bpf/hooks.c    | 20 ++++++++++++
 security/bpf/lsm.c      |  9 +++++-
 security/security.c     | 24 +++++++-------
 7 files changed, 115 insertions(+), 13 deletions(-)
 create mode 100644 include/linux/bpf_lsm.h
 create mode 100644 security/bpf/hooks.c
MAINTAINERS +1 -0
index 0941f478cfa5..02d7e05e9b75 100644
@@ -3209,6 +3209,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Maintained
 F:	security/bpf/
+F:	include/linux/bpf_lsm.h
 
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
include/linux/bpf_lsm.h new file +71 -0

71 lines not on this page · raw message

security/bpf/Kconfig +1 -0
index a5f6c67ae526..595e4ad597ae 100644
@@ -6,6 +6,7 @@ config SECURITY_BPF
 	bool "BPF-based MAC and audit policy"
 	depends on SECURITY
 	depends on BPF_SYSCALL
+	depends on SRCU
 	help
 	  This enables instrumentation of the security hooks with
 	  eBPF programs.
security/bpf/Makefile +1 -1
index c78a8a056e7e..c526927c337d 100644
@@ -2,4 +2,4 @@
 #
 # Copyright 2019 Google LLC.
 
-obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o
+obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o hooks.o
security/bpf/hooks.c new file +20 -0
new file mode 100644
index 000000000000..b123d9cb4cd4
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/bpf_lsm.h>
+#include <linux/srcu.h>
+
+DEFINE_STATIC_SRCU(security_hook_srcu);
+
+int bpf_lsm_srcu_read_lock(void)
+{
+	return srcu_read_lock(&security_hook_srcu);
+}
+
+void bpf_lsm_srcu_read_unlock(int idx)
+{
+	return srcu_read_unlock(&security_hook_srcu, idx);
+}
security/bpf/lsm.c +8 -1
index 5c5c14f990ce..d4ea6aa9ddb8 100644
@@ -4,14 +4,21 @@
  * Copyright 2019 Google LLC.
  */
 
+#include <linux/bpf_lsm.h>
 #include <linux/lsm_hooks.h>
 
 /* This is only for internal hooks, always statically shipped as part of the
- * BPF LSM. Statically defined hooks are appeneded to the security_hook_heads
+ * BPF LSM. Statically defined hooks are appended to the security_hook_heads
  * which is common for LSMs and R/O after init.
  */
 static struct security_hook_list lsm_hooks[] __lsm_ro_after_init = {};
 
+/* Security hooks registered dynamically by the BPF LSM and must be accessed
+ * by holding bpf_lsm_srcu_read_lock and bpf_lsm_srcu_read_unlock. The mutable
+ * hooks dynamically allocated by the BPF LSM are appeneded here.
+ */
+struct security_hook_heads bpf_lsm_hook_heads;
+
 static int __init lsm_init(void)
 {
 	security_add_hooks(lsm_hooks, ARRAY_SIZE(lsm_hooks), "bpf");
security/security.c +13 -11
index cd2d18d2d279..4a2eb4c089b2 100644
@@ -27,6 +27,7 @@
 #include <linux/backing-dev.h>
 #include <linux/string.h>
 #include <linux/msg.h>
+#include <linux/bpf_lsm.h>
 #include <net/flow.h>
 
 #define MAX_LSM_EVM_XATTR	2
@@ -652,20 +653,21 @@ static void __init lsm_early_task(struct task_struct *task)
 								\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
 			P->hook.FUNC(__VA_ARGS__);		\
+		CALL_BPF_LSM_VOID_HOOKS(FUNC, __VA_ARGS__);	\
 	} while (0)
 
-#define call_int_hook(FUNC, IRC, ...) ({			\
-	int RC = IRC;						\
-	do {							\
-		struct security_hook_list *P;			\
-								\
+#define call_int_hook(FUNC, IRC, ...) ({				\
+	int RC = IRC;							\
+	do {								\
+		struct security_hook_list *P;				\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
-			RC = P->hook.FUNC(__VA_ARGS__);		\
-			if (RC != 0)				\
-				break;				\
-		}						\
-	} while (0);						\
-	RC;							\
+			RC = P->hook.FUNC(__VA_ARGS__);			\
+			if (RC != 0)					\
+				break;					\
+		}							\
+		RC = CALL_BPF_LSM_INT_HOOKS(RC, FUNC, __VA_ARGS__);	\
+	} while (0);							\
+	RC;								\
 })
 
 /* Security operations */
Signature · 1 line
2.20.1
#9 lore raw

Re: [PATCH bpf-next v2 04/10] bpf: lsm: Add mutable hooks list for the BPF LSM

Quoted 18 lines · KP Singh

From: KP Singh <[email protected]>

- The list of hooks registered by an LSM is currently immutable as they are declared with __lsm_ro_after_init and they are attached to a security_hook_heads struct.
- For the BPF LSM we need to de/register the hooks at runtime. Making the existing security_hook_heads mutable broadens an attack vector, so a separate security_hook_heads is added for only those that ~must~ be mutable.
- These mutable hooks are run only after all the static hooks have successfully executed.

This is based on the ideas discussed in:

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

---

[...]

Quoted 22 lines
security/security.c +5 -6
index cd2d18d2d279..4a2eb4c089b2 100644
@@ -652,20 +653,21 @@ static void __init lsm_early_task(struct task_struct *task)
  								\
  		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
  			P->hook.FUNC(__VA_ARGS__);		\
+		CALL_BPF_LSM_VOID_HOOKS(FUNC, __VA_ARGS__);	\
  	} while (0)
 
-#define call_int_hook(FUNC, IRC, ...) ({			\
-	int RC = IRC;						\
-	do {							\
-		struct security_hook_list *P;			\
-								\
+#define call_int_hook(FUNC, IRC, ...) ({				\
+	int RC = IRC;							\
+	do {								\
+		struct security_hook_list *P;				\
  		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
-			RC = P->hook.FUNC(__VA_ARGS__);		\

security/security.c static void __init lsm_early_task(struct task_struct *task) · line 668

Quoted 10 lines
security/security.c +5 -5
-			if (RC != 0)				\
-				break;				\
-		}						\
-	} while (0);						\
-	RC;							\
+			RC = P->hook.FUNC(__VA_ARGS__);			\
+			if (RC != 0)					\
+				break;					\
+		}							\
+		RC = CALL_BPF_LSM_INT_HOOKS(RC, FUNC, __VA_ARGS__);	\

Let's not clobber the return code from the other LSMs with the bpf one.

Quoted 6 lines
security/security.c +2 -0
+	} while (0);							\
+	RC;								\
  })
 
  /* Security operations */
author
#10 lore raw

Re: [PATCH bpf-next v2 04/10] bpf: lsm: Add mutable hooks list for the BPF LSM

Quoted 45 lines · Stephen Smalley
Quoted 18 lines · KP Singh

From: KP Singh <[email protected]>

- The list of hooks registered by an LSM is currently immutable as they are declared with __lsm_ro_after_init and they are attached to a security_hook_heads struct.
- For the BPF LSM we need to de/register the hooks at runtime. Making the existing security_hook_heads mutable broadens an attack vector, so a separate security_hook_heads is added for only those that ~must~ be mutable.
- These mutable hooks are run only after all the static hooks have successfully executed.

This is based on the ideas discussed in:

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

---

[...]

Quoted 25 lines
security/security.c +5 -10
index cd2d18d2d279..4a2eb4c089b2 100644
@@ -652,20 +653,21 @@ static void __init lsm_early_task(struct task_struct *task)
  								\
  		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
  			P->hook.FUNC(__VA_ARGS__);		\
+		CALL_BPF_LSM_VOID_HOOKS(FUNC, __VA_ARGS__);	\
  	} while (0)
-#define call_int_hook(FUNC, IRC, ...) ({			\
-	int RC = IRC;						\
-	do {							\
-		struct security_hook_list *P;			\
-								\
+#define call_int_hook(FUNC, IRC, ...) ({				\
+	int RC = IRC;							\
+	do {								\
+		struct security_hook_list *P;				\
  		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
-			RC = P->hook.FUNC(__VA_ARGS__);		\
-			if (RC != 0)				\
-				break;				\
-		}						\
-	} while (0);						\
Quoted 8 lines

security/security.c static void __init lsm_early_task(struct task_struct *task) · line 668

Quoted 6 lines
security/security.c +5 -1
-	RC;							\
+			RC = P->hook.FUNC(__VA_ARGS__);			\
+			if (RC != 0)					\
+				break;					\
+		}							\
+		RC = CALL_BPF_LSM_INT_HOOKS(RC, FUNC, __VA_ARGS__);	\

Let's not clobber the return code from the other LSMs with the bpf one.

Good catch and thanks for pointing it out. Should be fixed in v3.

- KP

Quoted 7 lines
Quoted 6 lines
security/security.c +2 -0
+	} while (0);							\
+	RC;								\
  })
  /* Security operations */
 
#11 lore raw

Re: [PATCH bpf-next v2 04/10] bpf: lsm: Add mutable hooks list for the BPF LSM

In reply to KP Singh
Quoted 223 lines · KP Singh

From: KP Singh <[email protected]>

- The list of hooks registered by an LSM is currently immutable as they are declared with __lsm_ro_after_init and they are attached to a security_hook_heads struct.
- For the BPF LSM we need to de/register the hooks at runtime. Making the existing security_hook_heads mutable broadens an attack vector, so a separate security_hook_heads is added for only those that ~must~ be mutable.
- These mutable hooks are run only after all the static hooks have successfully executed.

This is based on the ideas discussed in:

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

---
 MAINTAINERS             |  1 +
 include/linux/bpf_lsm.h | 71 +++++++++++++++++++++++++++++++++++++++++
 security/bpf/Kconfig    |  1 +
 security/bpf/Makefile   |  2 +-
 security/bpf/hooks.c    | 20 ++++++++++++
 security/bpf/lsm.c      |  9 +++++-
 security/security.c     | 24 +++++++-------
 7 files changed, 115 insertions(+), 13 deletions(-)
 create mode 100644 include/linux/bpf_lsm.h
 create mode 100644 security/bpf/hooks.c
MAINTAINERS +1 -0
index 0941f478cfa5..02d7e05e9b75 100644
@@ -3209,6 +3209,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Maintained
 F:	security/bpf/
+F:	include/linux/bpf_lsm.h
 
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
include/linux/bpf_lsm.h new file +71 -0
new file mode 100644
index 000000000000..9883cf25241c
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#ifndef _LINUX_BPF_LSM_H
+#define _LINUX_BPF_LSM_H
+
+#include <linux/bpf.h>
+#include <linux/lsm_hooks.h>
+
+#ifdef CONFIG_SECURITY_BPF
+
+/* Mutable hooks defined at runtime and executed after all the statically
+ * define LSM hooks.
+ */
+extern struct security_hook_heads bpf_lsm_hook_heads;
+
+int bpf_lsm_srcu_read_lock(void);
+void bpf_lsm_srcu_read_unlock(int idx);
+
+#define CALL_BPF_LSM_VOID_HOOKS(FUNC, ...)			\
+	do {							\
+		struct security_hook_list *P;			\
+		int _idx;					\
+								\
+		if (hlist_empty(&bpf_lsm_hook_heads.FUNC))	\
+			break;					\
+								\
+		_idx = bpf_lsm_srcu_read_lock();		\
+		hlist_for_each_entry(P, &bpf_lsm_hook_heads.FUNC, list) \
+			P->hook.FUNC(__VA_ARGS__);		\
+		bpf_lsm_srcu_read_unlock(_idx);			\
+	} while (0)
+
+#define CALL_BPF_LSM_INT_HOOKS(RC, FUNC, ...) ({		\
+	do {							\
+		struct security_hook_list *P;			\
+		int _idx;					\
+								\
+		if (hlist_empty(&bpf_lsm_hook_heads.FUNC))	\
+			break;					\
+								\
+		_idx = bpf_lsm_srcu_read_lock();		\
+								\
+		hlist_for_each_entry(P,				\
+			&bpf_lsm_hook_heads.FUNC, list) {	\
+			RC = P->hook.FUNC(__VA_ARGS__);		\
+			if (RC && IS_ENABLED(CONFIG_SECURITY_BPF_ENFORCE)) \
+				break;				\
+		}						\
+		bpf_lsm_srcu_read_unlock(_idx);			\
+	} while (0);						\
+	IS_ENABLED(CONFIG_SECURITY_BPF_ENFORCE) ? RC : 0;	\
+})
+
+#else /* !CONFIG_SECURITY_BPF */
+
+#define CALL_BPF_LSM_INT_HOOKS(RC, FUNC, ...) (RC)
+#define CALL_BPF_LSM_VOID_HOOKS(...)
+
+static inline int bpf_lsm_srcu_read_lock(void)
+{
+	return 0;
+}
+static inline void bpf_lsm_srcu_read_unlock(int idx) {}
+
+#endif /* CONFIG_SECURITY_BPF */
+
+#endif /* _LINUX_BPF_LSM_H */
security/bpf/Kconfig +1 -0
index a5f6c67ae526..595e4ad597ae 100644
@@ -6,6 +6,7 @@ config SECURITY_BPF
 	bool "BPF-based MAC and audit policy"
 	depends on SECURITY
 	depends on BPF_SYSCALL
+	depends on SRCU
 	help
 	  This enables instrumentation of the security hooks with
 	  eBPF programs.
security/bpf/Makefile +1 -1
index c78a8a056e7e..c526927c337d 100644
@@ -2,4 +2,4 @@
 #
 # Copyright 2019 Google LLC.
 
-obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o
+obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o hooks.o
security/bpf/hooks.c new file +20 -0
new file mode 100644
index 000000000000..b123d9cb4cd4
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/bpf_lsm.h>
+#include <linux/srcu.h>
+
+DEFINE_STATIC_SRCU(security_hook_srcu);
+
+int bpf_lsm_srcu_read_lock(void)
+{
+	return srcu_read_lock(&security_hook_srcu);
+}
+
+void bpf_lsm_srcu_read_unlock(int idx)
+{
+	return srcu_read_unlock(&security_hook_srcu, idx);
+}
security/bpf/lsm.c +8 -1
index 5c5c14f990ce..d4ea6aa9ddb8 100644
@@ -4,14 +4,21 @@
  * Copyright 2019 Google LLC.
  */
 
+#include <linux/bpf_lsm.h>
 #include <linux/lsm_hooks.h>
 
 /* This is only for internal hooks, always statically shipped as part of the
- * BPF LSM. Statically defined hooks are appeneded to the security_hook_heads
+ * BPF LSM. Statically defined hooks are appended to the security_hook_heads
  * which is common for LSMs and R/O after init.
  */
 static struct security_hook_list lsm_hooks[] __lsm_ro_after_init = {};
 
+/* Security hooks registered dynamically by the BPF LSM and must be accessed
+ * by holding bpf_lsm_srcu_read_lock and bpf_lsm_srcu_read_unlock. The mutable
+ * hooks dynamically allocated by the BPF LSM are appeneded here.
+ */
+struct security_hook_heads bpf_lsm_hook_heads;
+
 static int __init lsm_init(void)
 {
 	security_add_hooks(lsm_hooks, ARRAY_SIZE(lsm_hooks), "bpf");
security/security.c +6 -6
index cd2d18d2d279..4a2eb4c089b2 100644
@@ -27,6 +27,7 @@
 #include <linux/backing-dev.h>
 #include <linux/string.h>
 #include <linux/msg.h>
+#include <linux/bpf_lsm.h>
 #include <net/flow.h>
 
 #define MAX_LSM_EVM_XATTR	2
@@ -652,20 +653,21 @@ static void __init lsm_early_task(struct task_struct *task)
 								\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
 			P->hook.FUNC(__VA_ARGS__);		\
+		CALL_BPF_LSM_VOID_HOOKS(FUNC, __VA_ARGS__);	\
 	} while (0)
 
-#define call_int_hook(FUNC, IRC, ...) ({			\
-	int RC = IRC;						\
-	do {							\
-		struct security_hook_list *P;			\
-								\
+#define call_int_hook(FUNC, IRC, ...) ({				\
+	int RC = IRC;							\
+	do {								\
+		struct security_hook_list *P;				\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
-			RC = P->hook.FUNC(__VA_ARGS__);		\

security/security.c static void __init lsm_early_task(struct task_struct *task) · line 668

Quoted 10 lines
security/security.c +5 -5
-			if (RC != 0)				\
-				break;				\
-		}						\
-	} while (0);						\
-	RC;							\
+			RC = P->hook.FUNC(__VA_ARGS__);			\
+			if (RC != 0)					\
+				break;					\
+		}							\
+		RC = CALL_BPF_LSM_INT_HOOKS(RC, FUNC, __VA_ARGS__);	\

Do not do this. Add LSM_ORDER_LAST for the lsm_order field of lsm_info and use that to identify your module as one to be put on the list last. Update the LSM registration code to do this. It will be much like the code that uses LSM_ORDER_FIRST to put the capabilities at the head of the lists.

What you have here to way to much like the way Yama was invoked before stacking.

Quoted 5 lines
security/security.c +2 -0
+	} while (0);							\
+	RC;								\
 })
 
 /* Security operations */
author
#12 lore raw

Re: [PATCH bpf-next v2 04/10] bpf: lsm: Add mutable hooks list for the BPF LSM

Quoted 240 lines · Casey Schaufler
Quoted 223 lines · KP Singh

From: KP Singh <[email protected]>

- The list of hooks registered by an LSM is currently immutable as they are declared with __lsm_ro_after_init and they are attached to a security_hook_heads struct.
- For the BPF LSM we need to de/register the hooks at runtime. Making the existing security_hook_heads mutable broadens an attack vector, so a separate security_hook_heads is added for only those that ~must~ be mutable.
- These mutable hooks are run only after all the static hooks have successfully executed.

This is based on the ideas discussed in:

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

---
 MAINTAINERS             |  1 +
 include/linux/bpf_lsm.h | 71 +++++++++++++++++++++++++++++++++++++++++
 security/bpf/Kconfig    |  1 +
 security/bpf/Makefile   |  2 +-
 security/bpf/hooks.c    | 20 ++++++++++++
 security/bpf/lsm.c      |  9 +++++-
 security/security.c     | 24 +++++++-------
 7 files changed, 115 insertions(+), 13 deletions(-)
 create mode 100644 include/linux/bpf_lsm.h
 create mode 100644 security/bpf/hooks.c
MAINTAINERS +1 -0
index 0941f478cfa5..02d7e05e9b75 100644
@@ -3209,6 +3209,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Maintained
 F:	security/bpf/
+F:	include/linux/bpf_lsm.h
 
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
include/linux/bpf_lsm.h new file +71 -0
new file mode 100644
index 000000000000..9883cf25241c
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#ifndef _LINUX_BPF_LSM_H
+#define _LINUX_BPF_LSM_H
+
+#include <linux/bpf.h>
+#include <linux/lsm_hooks.h>
+
+#ifdef CONFIG_SECURITY_BPF
+
+/* Mutable hooks defined at runtime and executed after all the statically
+ * define LSM hooks.
+ */
+extern struct security_hook_heads bpf_lsm_hook_heads;
+
+int bpf_lsm_srcu_read_lock(void);
+void bpf_lsm_srcu_read_unlock(int idx);
+
+#define CALL_BPF_LSM_VOID_HOOKS(FUNC, ...)			\
+	do {							\
+		struct security_hook_list *P;			\
+		int _idx;					\
+								\
+		if (hlist_empty(&bpf_lsm_hook_heads.FUNC))	\
+			break;					\
+								\
+		_idx = bpf_lsm_srcu_read_lock();		\
+		hlist_for_each_entry(P, &bpf_lsm_hook_heads.FUNC, list) \
+			P->hook.FUNC(__VA_ARGS__);		\
+		bpf_lsm_srcu_read_unlock(_idx);			\
+	} while (0)
+
+#define CALL_BPF_LSM_INT_HOOKS(RC, FUNC, ...) ({		\
+	do {							\
+		struct security_hook_list *P;			\
+		int _idx;					\
+								\
+		if (hlist_empty(&bpf_lsm_hook_heads.FUNC))	\
+			break;					\
+								\
+		_idx = bpf_lsm_srcu_read_lock();		\
+								\
+		hlist_for_each_entry(P,				\
+			&bpf_lsm_hook_heads.FUNC, list) {	\
+			RC = P->hook.FUNC(__VA_ARGS__);		\
+			if (RC && IS_ENABLED(CONFIG_SECURITY_BPF_ENFORCE)) \
+				break;				\
+		}						\
+		bpf_lsm_srcu_read_unlock(_idx);			\
+	} while (0);						\
+	IS_ENABLED(CONFIG_SECURITY_BPF_ENFORCE) ? RC : 0;	\
+})
+
+#else /* !CONFIG_SECURITY_BPF */
+
+#define CALL_BPF_LSM_INT_HOOKS(RC, FUNC, ...) (RC)
+#define CALL_BPF_LSM_VOID_HOOKS(...)
+
+static inline int bpf_lsm_srcu_read_lock(void)
+{
+	return 0;
+}
+static inline void bpf_lsm_srcu_read_unlock(int idx) {}
+
+#endif /* CONFIG_SECURITY_BPF */
+
+#endif /* _LINUX_BPF_LSM_H */
security/bpf/Kconfig +1 -0
index a5f6c67ae526..595e4ad597ae 100644
@@ -6,6 +6,7 @@ config SECURITY_BPF
 	bool "BPF-based MAC and audit policy"
 	depends on SECURITY
 	depends on BPF_SYSCALL
+	depends on SRCU
 	help
 	  This enables instrumentation of the security hooks with
 	  eBPF programs.
security/bpf/Makefile +1 -1
index c78a8a056e7e..c526927c337d 100644
@@ -2,4 +2,4 @@
 #
 # Copyright 2019 Google LLC.
 
-obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o
+obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o hooks.o
security/bpf/hooks.c new file +20 -0
new file mode 100644
index 000000000000..b123d9cb4cd4
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+
+#include <linux/bpf_lsm.h>
+#include <linux/srcu.h>
+
+DEFINE_STATIC_SRCU(security_hook_srcu);
+
+int bpf_lsm_srcu_read_lock(void)
+{
+	return srcu_read_lock(&security_hook_srcu);
+}
+
+void bpf_lsm_srcu_read_unlock(int idx)
+{
+	return srcu_read_unlock(&security_hook_srcu, idx);
+}
security/bpf/lsm.c +8 -1
index 5c5c14f990ce..d4ea6aa9ddb8 100644
@@ -4,14 +4,21 @@
  * Copyright 2019 Google LLC.
  */
 
+#include <linux/bpf_lsm.h>
 #include <linux/lsm_hooks.h>
 
 /* This is only for internal hooks, always statically shipped as part of the
- * BPF LSM. Statically defined hooks are appeneded to the security_hook_heads
+ * BPF LSM. Statically defined hooks are appended to the security_hook_heads
  * which is common for LSMs and R/O after init.
  */
 static struct security_hook_list lsm_hooks[] __lsm_ro_after_init = {};
 
+/* Security hooks registered dynamically by the BPF LSM and must be accessed
+ * by holding bpf_lsm_srcu_read_lock and bpf_lsm_srcu_read_unlock. The mutable
+ * hooks dynamically allocated by the BPF LSM are appeneded here.
+ */
+struct security_hook_heads bpf_lsm_hook_heads;
+
 static int __init lsm_init(void)
 {
 	security_add_hooks(lsm_hooks, ARRAY_SIZE(lsm_hooks), "bpf");
security/security.c +6 -6
index cd2d18d2d279..4a2eb4c089b2 100644
@@ -27,6 +27,7 @@
 #include <linux/backing-dev.h>
 #include <linux/string.h>
 #include <linux/msg.h>
+#include <linux/bpf_lsm.h>
 #include <net/flow.h>
 
 #define MAX_LSM_EVM_XATTR	2
@@ -652,20 +653,21 @@ static void __init lsm_early_task(struct task_struct *task)
 								\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
 			P->hook.FUNC(__VA_ARGS__);		\
+		CALL_BPF_LSM_VOID_HOOKS(FUNC, __VA_ARGS__);	\
 	} while (0)
 
-#define call_int_hook(FUNC, IRC, ...) ({			\
-	int RC = IRC;						\
-	do {							\
-		struct security_hook_list *P;			\
-								\
+#define call_int_hook(FUNC, IRC, ...) ({				\
+	int RC = IRC;							\
+	do {								\
+		struct security_hook_list *P;				\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
-			RC = P->hook.FUNC(__VA_ARGS__);		\

security/security.c static void __init lsm_early_task(struct task_struct *task) · line 668

Quoted 10 lines
security/security.c +5 -5
-			if (RC != 0)				\
-				break;				\
-		}						\
-	} while (0);						\
-	RC;							\
+			RC = P->hook.FUNC(__VA_ARGS__);			\
+			if (RC != 0)					\
+				break;					\
+		}							\
+		RC = CALL_BPF_LSM_INT_HOOKS(RC, FUNC, __VA_ARGS__);	\

Do not do this. Add LSM_ORDER_LAST for the lsm_order field of lsm_info and use that to identify your module as one to be put on the list last. Update the LSM registration code to do this. It will be much like the code that uses LSM_ORDER_FIRST to put the capabilities at the head of the lists.

Quoted 2 lines

What you have here to way to much like the way Yama was invoked before stacking.

Thanks for taking a look!

The BPF LSM has two kinds of hooks. The static hooks which are appended to the security_hook_heads in security.c and mutable hooks which are allocated at runtime and attached to a separate security_hook_heads (bpf_lsm_hook_heads) defined in security/bpf/lsm.c This macro runs these mutable hooks (dynamically allocated at runtime) under SRCU protection.

Having a separate security_hook_heads allows:

- The security_hook_heads in security.c to be __lsm_ro_after_init and thus limits the attack surface does not change the existing behaviour
- The SRCU critical section be limited to the execution of dynamically allocated hooks

I agree that the static hooks should indeed be LSM_ORDER_LAST which makes logical sense and represents correctly how the LSM will behave in reality. (i.e. its hooks are executed last irrespective of the position it is mentioned in the list of LSMs.)

I will update this for v3.

- KP

Quoted 7 lines
Quoted 6 lines
security/security.c +2 -0
+	} while (0);							\
+	RC;								\
 })
 
 /* Security operations */
author
#13 lore raw

PATCH v2 5/10 bpf: lsm: BTF API for LSM hooks

In reply to KP Singh

From: KP Singh <[email protected]>

The BTF API provides information required by the BPF verifier to attach eBPF programs to the LSM hooks by using the BTF information of two types:

- struct security_hook_heads: This type provides the offset which a new dynamically allocated security hook must be attached to.
- union security_list_options: This provides the information about the function prototype required by the hook.

When the program is loaded:

- The verifier receives the index of a member in struct security_hook_heads to which a program must be attached as prog->aux->lsm_hook_index. The index is one-based for better verification.
- bpf_lsm_type_by_index is used to determine the func_proto of the LSM hook and updates prog->aux->attach_func_proto
- bpf_lsm_head_by_index is used to determine the hlist_head to which the BPF program must be attached.

---
 include/linux/bpf_lsm.h |  12 +++++
 security/bpf/Kconfig    |   1 +
 security/bpf/hooks.c    | 104 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)
include/linux/bpf_lsm.h +12 -0
index 9883cf25241c..a9b4f7b41c65 100644
@@ -19,6 +19,8 @@ extern struct security_hook_heads bpf_lsm_hook_heads;
 
 int bpf_lsm_srcu_read_lock(void);
 void bpf_lsm_srcu_read_unlock(int idx);
+const struct btf_type *bpf_lsm_type_by_index(struct btf *btf, u32 offset);
+const struct btf_member *bpf_lsm_head_by_index(struct btf *btf, u32 id);
 
 #define CALL_BPF_LSM_VOID_HOOKS(FUNC, ...)			\
 	do {							\
@@ -65,6 +67,16 @@ static inline int bpf_lsm_srcu_read_lock(void)
 	return 0;
 }
 static inline void bpf_lsm_srcu_read_unlock(int idx) {}
+static inline const struct btf_type *bpf_lsm_type_by_index(
+	struct btf *btf, u32 index)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+static inline const struct btf_member *bpf_lsm_head_by_index(
+	struct btf *btf, u32 id)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
 
 #endif /* CONFIG_SECURITY_BPF */
 
security/bpf/Kconfig +1 -0
index 595e4ad597ae..9438d899b618 100644
@@ -7,6 +7,7 @@ config SECURITY_BPF
 	depends on SECURITY
 	depends on BPF_SYSCALL
 	depends on SRCU
+	depends on DEBUG_INFO_BTF
 	help
 	  This enables instrumentation of the security hooks with
 	  eBPF programs.
security/bpf/hooks.c +104 -0

113 lines not on this page · raw message

Signature · 1 line
2.20.1
#14 lore raw

Re: [PATCH bpf-next v2 05/10] bpf: lsm: BTF API for LSM hooks

Quoted 87 lines · KP Singh

From: KP Singh <[email protected]>

The BTF API provides information required by the BPF verifier to attach eBPF programs to the LSM hooks by using the BTF information of two types:

- struct security_hook_heads: This type provides the offset which a new dynamically allocated security hook must be attached to.
- union security_list_options: This provides the information about the function prototype required by the hook.

When the program is loaded:

- The verifier receives the index of a member in struct security_hook_heads to which a program must be attached as prog->aux->lsm_hook_index. The index is one-based for better verification.
- bpf_lsm_type_by_index is used to determine the func_proto of the LSM hook and updates prog->aux->attach_func_proto
- bpf_lsm_head_by_index is used to determine the hlist_head to which the BPF program must be attached.

---
 include/linux/bpf_lsm.h |  12 +++++
 security/bpf/Kconfig    |   1 +
 security/bpf/hooks.c    | 104 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)
include/linux/bpf_lsm.h +12 -0
index 9883cf25241c..a9b4f7b41c65 100644
@@ -19,6 +19,8 @@ extern struct security_hook_heads bpf_lsm_hook_heads;
 
 int bpf_lsm_srcu_read_lock(void);
 void bpf_lsm_srcu_read_unlock(int idx);
+const struct btf_type *bpf_lsm_type_by_index(struct btf *btf, u32 offset);
+const struct btf_member *bpf_lsm_head_by_index(struct btf *btf, u32 id);
 
 #define CALL_BPF_LSM_VOID_HOOKS(FUNC, ...)                     \
        do {                                                    \
@@ -65,6 +67,16 @@ static inline int bpf_lsm_srcu_read_lock(void)
        return 0;
 }
 static inline void bpf_lsm_srcu_read_unlock(int idx) {}
+static inline const struct btf_type *bpf_lsm_type_by_index(
+       struct btf *btf, u32 index)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+static inline const struct btf_member *bpf_lsm_head_by_index(
+       struct btf *btf, u32 id)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
 
 #endif /* CONFIG_SECURITY_BPF */
 
security/bpf/Kconfig +1 -0
index 595e4ad597ae..9438d899b618 100644
@@ -7,6 +7,7 @@ config SECURITY_BPF
        depends on SECURITY
        depends on BPF_SYSCALL
        depends on SRCU
+       depends on DEBUG_INFO_BTF
        help
          This enables instrumentation of the security hooks with
          eBPF programs.
security/bpf/hooks.c +2 -0
index b123d9cb4cd4..82725611693d 100644
@@ -5,6 +5,8 @@
  */
 
 #include <linux/bpf_lsm.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
 #include <linux/srcu.h>
 
 DEFINE_STATIC_SRCU(security_hook_srcu);
@@ -18,3 +20,105 @@ void bpf_lsm_srcu_read_unlock(int idx)
 {

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 30

Quoted 10 lines
security/bpf/hooks.c +8 -0
        return srcu_read_unlock(&security_hook_srcu, idx);
 }
+
+static inline int validate_hlist_head(struct btf *btf, u32 type_id)
+{
+       s32 hlist_id;
+
+       hlist_id = btf_find_by_name_kind(btf, "hlist_head", BTF_KIND_STRUCT);
+       if (hlist_id < 0 || hlist_id != type_id)
+               return -EINVAL;

This feels backwards and expensive. You already have type_id you want to check. Do a quick look up, check type and other attributes, if you want. There is no need to do linear search for struct named "hlist_head".

But in reality, you should trust kernel BTF, you already know that you found correct "security_hook_heads" struct, so its member has to be hlist_head, no?

Quoted 17 lines
security/bpf/hooks.c +17 -0
+
+       return 0;
+}
+
+/* Find the BTF representation of the security_hook_heads member for a member
+ * with a given index in struct security_hook_heads.
+ */
+const struct btf_member *bpf_lsm_head_by_index(struct btf *btf, u32 index)
+{
+       const struct btf_member *member;
+       const struct btf_type *t;
+       u32 off, i;
+       int ret;
+
+       t = btf_type_by_name_kind(btf, "security_hook_heads", BTF_KIND_STRUCT);
+       if (WARN_ON_ONCE(IS_ERR(t)))
+               return ERR_CAST(t);

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 57

Quoted 10 lines
security/bpf/hooks.c +10 -0
+
+       for_each_member(i, t, member) {
+               /* We've found the id requested and need to check the
+                * the following:
+                *
+                * - Is it at a valid alignment for struct hlist_head?
+                *
+                * - Is it a valid hlist_head struct?
+                */
+               if (index == i) {

Also not efficient. Check index to be < vlen(t), then member = btf_type_member(t) + index;

Quoted 4 lines
security/bpf/hooks.c +4 -0
+                       off = btf_member_bit_offset(t, member);
+                       if (off % 8)
+                               /* valid c code cannot generate such btf */
+                               return ERR_PTR(-EINVAL);

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 71

Quoted 10 lines
security/bpf/hooks.c +10 -0
+                       off /= 8;
+
+                       if (off % __alignof__(struct hlist_head))
+                               return ERR_PTR(-EINVAL);
+
+                       ret = validate_hlist_head(btf, member->type);
+                       if (ret < 0)
+                               return ERR_PTR(ret);
+
+                       return member;

This feels a bit over-cautious to double-check this. If security_hook_heads definition is controlled by kernel sources, then we could just trust vmlinux BTF?

Quoted 14 lines
security/bpf/hooks.c +14 -0
+               }
+       }
+
+       return ERR_PTR(-ENOENT);
+}
+
+/* Given an index of a member in security_hook_heads return the
+ * corresponding type for the LSM hook. The members of the union
+ * security_list_options have the same name as the security_hook_heads which
+ * is ensured by the LSM_HOOK_INIT macro defined in include/linux/lsm_hooks.h
+ */
+const struct btf_type *bpf_lsm_type_by_index(struct btf *btf, u32 index)
+{
+       const struct btf_member *member, *hook_head = NULL;

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 95

Quoted 10 lines
security/bpf/hooks.c +10 -0
+       const struct btf_type *t, *hook_type = NULL;
+       u32 i;
+
+       hook_head = bpf_lsm_head_by_index(btf, index);
+       if (IS_ERR(hook_head))
+               return ERR_PTR(PTR_ERR(hook_head));
+
+       t = btf_type_by_name_kind(btf, "security_list_options", BTF_KIND_UNION);
+       if (WARN_ON_ONCE(IS_ERR(t)))
+               return ERR_CAST(t);

btf_type_by_name_kind() is a linear search (at least right now), so it might be a good idea to cache found type_id's of security_list_options and security_hook_heads?

Quoted 16 lines
security/bpf/hooks.c +16 -0
+
+       for_each_member(i, t, member) {
+               if (hook_head->name_off == member->name_off) {
+                       /* There should be only one member with the same name
+                        * as the LSM hook. This should never really happen
+                        * and either indicates malformed BTF or someone trying
+                        * trick the LSM.
+                        */
+                       if (WARN_ON(hook_type))
+                               return ERR_PTR(-EINVAL);
+
+                       hook_type = btf_type_by_id(btf, member->type);
+                       if (unlikely(!hook_type))
+                               return ERR_PTR(-EINVAL);
+
+                       if (!btf_type_is_ptr(hook_type))

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 121

Quoted 10 lines
security/bpf/hooks.c +10 -0
+                               return ERR_PTR(-EINVAL);
+               }
+       }
+
+       if (!hook_type)
+               return ERR_PTR(-ENOENT);
+
+       t = btf_type_by_id(btf, hook_type->type);
+       if (unlikely(!t))
+               return ERR_PTR(-EINVAL);

why not do this inside the loop when you find correct member and not continue processing all the fields?

Quoted 6 lines
security/bpf/hooks.c +3 -1
+
+       return t;
+}
--

2.20.1

author
#15 lore raw

Re: [PATCH bpf-next v2 05/10] bpf: lsm: BTF API for LSM hooks

Thanks for reviewing!

Quoted 104 lines · Andrii Nakryiko
Quoted 87 lines · KP Singh

From: KP Singh <[email protected]>

The BTF API provides information required by the BPF verifier to attach eBPF programs to the LSM hooks by using the BTF information of two types:

- struct security_hook_heads: This type provides the offset which a new dynamically allocated security hook must be attached to.
- union security_list_options: This provides the information about the function prototype required by the hook.

When the program is loaded:

- The verifier receives the index of a member in struct security_hook_heads to which a program must be attached as prog->aux->lsm_hook_index. The index is one-based for better verification.
- bpf_lsm_type_by_index is used to determine the func_proto of the LSM hook and updates prog->aux->attach_func_proto
- bpf_lsm_head_by_index is used to determine the hlist_head to which the BPF program must be attached.

---
 include/linux/bpf_lsm.h |  12 +++++
 security/bpf/Kconfig    |   1 +
 security/bpf/hooks.c    | 104 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)
include/linux/bpf_lsm.h +12 -0
index 9883cf25241c..a9b4f7b41c65 100644
@@ -19,6 +19,8 @@ extern struct security_hook_heads bpf_lsm_hook_heads;
 
 int bpf_lsm_srcu_read_lock(void);
 void bpf_lsm_srcu_read_unlock(int idx);
+const struct btf_type *bpf_lsm_type_by_index(struct btf *btf, u32 offset);
+const struct btf_member *bpf_lsm_head_by_index(struct btf *btf, u32 id);
 
 #define CALL_BPF_LSM_VOID_HOOKS(FUNC, ...)                     \
        do {                                                    \
@@ -65,6 +67,16 @@ static inline int bpf_lsm_srcu_read_lock(void)
        return 0;
 }
 static inline void bpf_lsm_srcu_read_unlock(int idx) {}
+static inline const struct btf_type *bpf_lsm_type_by_index(
+       struct btf *btf, u32 index)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+static inline const struct btf_member *bpf_lsm_head_by_index(
+       struct btf *btf, u32 id)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
 
 #endif /* CONFIG_SECURITY_BPF */
 
security/bpf/Kconfig +1 -0
index 595e4ad597ae..9438d899b618 100644
@@ -7,6 +7,7 @@ config SECURITY_BPF
        depends on SECURITY
        depends on BPF_SYSCALL
        depends on SRCU
+       depends on DEBUG_INFO_BTF
        help
          This enables instrumentation of the security hooks with
          eBPF programs.
security/bpf/hooks.c +2 -0
index b123d9cb4cd4..82725611693d 100644
@@ -5,6 +5,8 @@
  */
 
 #include <linux/bpf_lsm.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
 #include <linux/srcu.h>
 
 DEFINE_STATIC_SRCU(security_hook_srcu);
@@ -18,3 +20,105 @@ void bpf_lsm_srcu_read_unlock(int idx)
 {

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 30

Quoted 10 lines
security/bpf/hooks.c +8 -0
        return srcu_read_unlock(&security_hook_srcu, idx);
 }
+
+static inline int validate_hlist_head(struct btf *btf, u32 type_id)
+{
+       s32 hlist_id;
+
+       hlist_id = btf_find_by_name_kind(btf, "hlist_head", BTF_KIND_STRUCT);
+       if (hlist_id < 0 || hlist_id != type_id)
+               return -EINVAL;

This feels backwards and expensive. You already have type_id you want to check. Do a quick look up, check type and other attributes, if you want. There is no need to do linear search for struct named "hlist_head".

Quoted 3 lines

But in reality, you should trust kernel BTF, you already know that you found correct "security_hook_heads" struct, so its member has to be hlist_head, no?

We had a discussion internally and also came the same conclusion (it's okay to trust the BTF) and will remove sone of the "over-cautious" checks in the next revision.

This one, however, in particular is to protect against the case when a new member which is not a hlist_head is added to security_hook_heads and the user-space tries to attach at that index.

I admit that the likelyhood of that happening is very little but I think it's worth checking. I do, like your idea and will update the code to use the type_id and do a quick check rather than a linear search to look for the type_id.

This is what remains of all the pedantic checks pertaining to hlist_head:

	t = btf_type_by_id(btf, member->type);
	if (unlikely(!t))
		return -EINVAL;

	if (BTF_INFO_KIND(t->info) != BTF_KIND_STRUCT)
		return -EINVAL;

	if (t->size != sizeof(struct hlist_head))
		return -EINVAL;
Quoted 30 lines
Quoted 19 lines
security/bpf/hooks.c +18 -0
+
+       return 0;
+}
+
+/* Find the BTF representation of the security_hook_heads member for a member
+ * with a given index in struct security_hook_heads.
+ */
+const struct btf_member *bpf_lsm_head_by_index(struct btf *btf, u32 index)
+{
+       const struct btf_member *member;
+       const struct btf_type *t;
+       u32 off, i;
+       int ret;
+
+       t = btf_type_by_name_kind(btf, "security_hook_heads", BTF_KIND_STRUCT);
+       if (WARN_ON_ONCE(IS_ERR(t)))
+               return ERR_CAST(t);
+

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 57

Quoted 10 lines
security/bpf/hooks.c +9 -0
+       for_each_member(i, t, member) {
+               /* We've found the id requested and need to check the
 
+                * the following:
+                *
+                * - Is it at a valid alignment for struct hlist_head?
+                *
+                * - Is it a valid hlist_head struct?
+                */
+               if (index == i) {
Quoted 2 lines

Also not efficient. Check index to be < vlen(t), then member = btf_type_member(t) + index;

Neat! Updated.

Quoted 12 lines
Quoted 12 lines
security/bpf/hooks.c +10 -0
+                       off = btf_member_bit_offset(t, member);
+                       if (off % 8)
+                               /* valid c code cannot generate such btf */
+                               return ERR_PTR(-EINVAL);
+                       off /= 8;
+
+                       if (off % __alignof__(struct hlist_head))
+                               return ERR_PTR(-EINVAL);
+
+                       ret = validate_hlist_head(btf, member->type);
Quoted 8 lines

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 71

Quoted 4 lines
security/bpf/hooks.c +4 -0
+                       if (ret < 0)
+                               return ERR_PTR(ret);
+
+                       return member;

This feels a bit over-cautious to double-check this. If security_hook_heads definition is controlled by kernel sources, then we could just trust vmlinux BTF?

Yep, makes sense. Removed some of these checks.

Quoted 21 lines
Quoted 21 lines
security/bpf/hooks.c +20 -0
+               }
+       }
+
+       return ERR_PTR(-ENOENT);
+}
+
+/* Given an index of a member in security_hook_heads return the
+ * corresponding type for the LSM hook. The members of the union
+ * security_list_options have the same name as the security_hook_heads which
+ * is ensured by the LSM_HOOK_INIT macro defined in include/linux/lsm_hooks.h
+ */
+const struct btf_type *bpf_lsm_type_by_index(struct btf *btf, u32 index)
+{
+       const struct btf_member *member, *hook_head = NULL;
+       const struct btf_type *t, *hook_type = NULL;
+       u32 i;
+
+       hook_head = bpf_lsm_head_by_index(btf, index);
+       if (IS_ERR(hook_head))
+               return ERR_PTR(PTR_ERR(hook_head));
Quoted 8 lines

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 95

Quoted 4 lines
security/bpf/hooks.c +4 -0
+
+       t = btf_type_by_name_kind(btf, "security_list_options", BTF_KIND_UNION);
+       if (WARN_ON_ONCE(IS_ERR(t)))
+               return ERR_CAST(t);

btf_type_by_name_kind() is a linear search (at least right now), so it might be a good idea to cache found type_id's of security_list_options and security_hook_heads?

I am already caching these types in the next patch (struct bpf_lsm_info) of the series which implements attachment. I moved it to this patch so that it's clearer.

Quoted 22 lines
Quoted 22 lines
security/bpf/hooks.c +21 -0
+
+       for_each_member(i, t, member) {
+               if (hook_head->name_off == member->name_off) {
+                       /* There should be only one member with the same name
+                        * as the LSM hook. This should never really happen
+                        * and either indicates malformed BTF or someone trying
+                        * trick the LSM.
+                        */
+                       if (WARN_ON(hook_type))
+                               return ERR_PTR(-EINVAL);
+
+                       hook_type = btf_type_by_id(btf, member->type);
+                       if (unlikely(!hook_type))
+                               return ERR_PTR(-EINVAL);
+
+                       if (!btf_type_is_ptr(hook_type))
+                               return ERR_PTR(-EINVAL);
+               }
+       }
+
+       if (!hook_type)
Quoted 8 lines

security/bpf/hooks.c void bpf_lsm_srcu_read_unlock(int idx) · line 121

Quoted 5 lines
security/bpf/hooks.c +5 -0
+               return ERR_PTR(-ENOENT);
+
+       t = btf_type_by_id(btf, hook_type->type);
+       if (unlikely(!t))
+               return ERR_PTR(-EINVAL);

why not do this inside the loop when you find correct member and not continue processing all the fields?

Updated.

- KP

Quoted 7 lines
Quoted 7 lines
security/bpf/hooks.c +3 -1
+
+       return t;
+}
--

2.20.1

author
#16 lore raw

PATCH v2 6/10 bpf: lsm: Implement attach, detach and execution

In reply to KP Singh

From: KP Singh <[email protected]>

JITed BPF programs are used by the BPF LSM as dynamically allocated security hooks. arch_bpf_prepare_trampoline handles the arch_bpf_prepare_trampoline generates code to handle conversion of the signature of the hook to the BPF context and allows the BPF program to be called directly as a C function.

The following permissions are required to attach a program to a hook:

- CAP_SYS_ADMIN to load the program
- CAP_MAC_ADMIN to attach it (i.e. to update the security policy)

When the program is loaded (BPF_PROG_LOAD), the verifier receives the index (one-based) of the member in the security_hook_heads struct in the prog->aux->lsm_hook_index and uses the BTF API provided by the LSM to:

- Populate the name of the hook in prog->aux->attach_func_name and the prototype in prog->aux->attach_func_proto.
- Verify if the offset is valid for a type struct hlist_head.
- The program is verified for accesses based on the attach_func_proto similar to raw_tp BPF programs.

When an attachment (BPF_PROG_ATTACH) is requested:

- The information required to set-up of a callback is populated in the struct bpf_lsm_list.
- A new callback and a bpf_lsm_hook is allocated and the address of the hook is set to the be the address of the allocated callback.
- The attachment returns an anonymous O_CLOEXEC fd which detaches the program on close.

---
 include/linux/bpf.h            |   4 +
 include/linux/bpf_lsm.h        |  15 +++
 include/uapi/linux/bpf.h       |   4 +
 kernel/bpf/syscall.c           |  47 ++++++-
 kernel/bpf/verifier.c          |  74 +++++++++--
 security/bpf/Kconfig           |   1 +
 security/bpf/Makefile          |   2 +
 security/bpf/hooks.c           | 230 +++++++++++++++++++++++++++++++--
 security/bpf/include/bpf_lsm.h |  75 +++++++++++
 security/bpf/lsm.c             |  60 +++++++++
 security/bpf/ops.c             |   2 +
 tools/include/uapi/linux/bpf.h |   4 +
 12 files changed, 494 insertions(+), 24 deletions(-)
 create mode 100644 security/bpf/include/bpf_lsm.h
include/linux/bpf.h +4 -0
index aed2bc39d72b..5ed4780c2091 100644
@@ -599,6 +599,10 @@ struct bpf_prog_aux {
 	u32 func_cnt; /* used by non-func prog as the number of func progs */
 	u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
 	u32 attach_btf_id; /* in-kernel BTF type id to attach to */
+	/* Index (one-based) of the hlist_head in security_hook_heads to which
+	 * the program must be attached.
+	 */
+	u32 lsm_hook_index;
 	struct bpf_prog *linked_prog;
 	bool verifier_zext; /* Zero extensions has been inserted by verifier. */
 	bool offload_requested;
include/linux/bpf_lsm.h +15 -0
index a9b4f7b41c65..8c86672437f0 100644
@@ -19,8 +19,11 @@ extern struct security_hook_heads bpf_lsm_hook_heads;
 
 int bpf_lsm_srcu_read_lock(void);
 void bpf_lsm_srcu_read_unlock(int idx);
+int bpf_lsm_verify_prog(const struct bpf_prog *prog);
 const struct btf_type *bpf_lsm_type_by_index(struct btf *btf, u32 offset);
 const struct btf_member *bpf_lsm_head_by_index(struct btf *btf, u32 id);
+int bpf_lsm_attach(struct bpf_prog *prog);
+int bpf_lsm_detach(struct bpf_prog *prog);
 
 #define CALL_BPF_LSM_VOID_HOOKS(FUNC, ...)			\
 	do {							\
@@ -67,6 +70,10 @@ static inline int bpf_lsm_srcu_read_lock(void)
 	return 0;
 }
 static inline void bpf_lsm_srcu_read_unlock(int idx) {}
+static inline int bpf_lsm_verify_prog(const struct bpf_prog *prog)
+{
+	return -EOPNOTSUPP;
+}
 static inline const struct btf_type *bpf_lsm_type_by_index(
 	struct btf *btf, u32 index)
 {
@@ -77,6 +84,14 @@ static inline const struct btf_member *bpf_lsm_head_by_index(
 {
 	return ERR_PTR(-EOPNOTSUPP);
 }
+static inline int bpf_lsm_attach(struct bpf_prog *prog)
+{
+	return -EOPNOTSUPP;
+}
+static inline int bpf_lsm_detach(struct bpf_prog *prog)
+{
+	return -EOPNOTSUPP;
+}
 
 #endif /* CONFIG_SECURITY_BPF */
 
include/uapi/linux/bpf.h +4 -0
index b6a725a8a21d..fa5513cb3332 100644
@@ -448,6 +448,10 @@ union bpf_attr {
 		__u32		line_info_cnt;	/* number of bpf_line_info records */
 		__u32		attach_btf_id;	/* in-kernel BTF type id to attach to */
 		__u32		attach_prog_fd; /* 0 to attach to vmlinux */
+		/* Index (one-based) of the hlist_head in security_hook_heads to
+		 * which the program must be attached.
+		 */
+		__u32		lsm_hook_index;
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/syscall.c +45 -2

77 lines not on this page · raw message

kernel/bpf/verifier.c +65 -9

104 lines not on this page · raw message

security/bpf/Kconfig +1 -0
index 9438d899b618..a915c549f4b8 100644
@@ -8,6 +8,7 @@ config SECURITY_BPF
 	depends on BPF_SYSCALL
 	depends on SRCU
 	depends on DEBUG_INFO_BTF
+	depends on BPF_JIT && HAVE_EBPF_JIT
 	help
 	  This enables instrumentation of the security hooks with
 	  eBPF programs.
security/bpf/Makefile +2 -0
index c526927c337d..748b9b7d4bc7 100644
@@ -3,3 +3,5 @@
 # Copyright 2019 Google LLC.
 
 obj-$(CONFIG_SECURITY_BPF) := lsm.o ops.o hooks.o
+
+ccflags-y := -I$(srctree)/security/bpf -I$(srctree)/security/bpf/include
security/bpf/hooks.c +217 -13

269 lines not on this page · raw message

security/bpf/include/bpf_lsm.h new file +75 -0

75 lines not on this page · raw message

security/bpf/lsm.c +60 -0

72 lines not on this page · raw message

security/bpf/ops.c +2 -0
index 81c2bd9c0495..9a26633ac6f3 100644
@@ -6,6 +6,7 @@
 
 #include <linux/filter.h>
 #include <linux/bpf.h>
+#include <linux/btf.h>
 
 const struct bpf_prog_ops lsm_prog_ops = {
 };
@@ -25,4 +26,5 @@ static const struct bpf_func_proto *get_bpf_func_proto(
 
 const struct bpf_verifier_ops lsm_verifier_ops = {
 	.get_func_proto = get_bpf_func_proto,
+	.is_valid_access = btf_ctx_access,
 };
tools/include/uapi/linux/bpf.h +4 -0
index b6a725a8a21d..fa5513cb3332 100644
@@ -448,6 +448,10 @@ union bpf_attr {
 		__u32		line_info_cnt;	/* number of bpf_line_info records */
 		__u32		attach_btf_id;	/* in-kernel BTF type id to attach to */
 		__u32		attach_prog_fd; /* 0 to attach to vmlinux */
+		/* Index (one-based) of the hlist_head in security_hook_heads to
+		 * which the program must be attached.
+		 */
+		__u32		lsm_hook_index;
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_* commands */
Signature · 1 line
2.20.1
Reviewed-by on v2 6/10
#17 lore raw

Re: [PATCH bpf-next v2 06/10] bpf: lsm: Implement attach, detach and execution

Quoted 8 lines · KP Singh

From: KP Singh <[email protected]>

JITed BPF programs are used by the BPF LSM as dynamically allocated security hooks. arch_bpf_prepare_trampoline handles the arch_bpf_prepare_trampoline generates code to handle conversion of the signature of the hook to the BPF context and allows the BPF program to be called directly as a C function.

Quoted 4 lines

The following permissions are required to attach a program to a hook:

- CAP_SYS_ADMIN to load the program
- CAP_MAC_ADMIN to attach it (i.e. to update the security policy)

You forgot to list "GPL-compatible license" here :)

Anyway, looks good to me:

author
#18 lore raw

Re: [PATCH bpf-next v2 06/10] bpf: lsm: Implement attach, detach and execution

Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · KP Singh

From: KP Singh <[email protected]>

JITed BPF programs are used by the BPF LSM as dynamically allocated security hooks. arch_bpf_prepare_trampoline handles the arch_bpf_prepare_trampoline generates code to handle conversion of the signature of the hook to the BPF context and allows the BPF program to be called directly as a C function.

Quoted 6 lines
Quoted 4 lines

The following permissions are required to attach a program to a hook:

- CAP_SYS_ADMIN to load the program
- CAP_MAC_ADMIN to attach it (i.e. to update the security policy)

You forgot to list "GPL-compatible license" here :)

Added it to the commit log for v3.

Quoted 2 lines

Anyway, looks good to me:

Thanks! :)

Quoted 2 lines
author
#19 lore raw

PATCH v2 7/10 bpf: lsm: Make the allocated callback RO+X

In reply to KP Singh

From: KP Singh <[email protected]>

This patch is not needed after arch_bpf_prepare_trampoline moves to using text_poke.

The two IPI TLB flushes can be further optimized if a new API to handle W^X in the kernel emerges as an outcome of:

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

---
 security/bpf/hooks.c | 9 +++++++++
 1 file changed, 9 insertions(+)
security/bpf/hooks.c +9 -0
index 4e71da0e8e9e..30f68341f5ef 100644
@@ -222,6 +222,15 @@ static struct bpf_lsm_hook *bpf_lsm_hook_alloc(
 		goto error;
 	}
 
+	/* First make the page read-only, and only then make it executable to
+	 * prevent it from being W+X in between.
+	 */
+	set_memory_ro((unsigned long)image, 1);
+	/* More checks can be done here to ensure that nothing was changed
+	 * between arch_prepare_bpf_trampoline and set_memory_ro.
+	 */
+	set_memory_x((unsigned long)image, 1);
+
 	hook = kzalloc(sizeof(struct bpf_lsm_hook), GFP_KERNEL);
 	if (!hook) {
 		ret = -ENOMEM;
Signature · 1 line
2.20.1
author
#20 lore raw

PATCH v2 8/10 tools/libbpf: Add support for BPF_PROG_TYPE_LSM

In reply to KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)
tools/lib/bpf/bpf.c +5 -1
index 500afe478e94..b138d98ff862 100644
@@ -235,7 +235,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 	memset(&attr, 0, sizeof(attr));
 	attr.prog_type = load_attr->prog_type;
 	attr.expected_attach_type = load_attr->expected_attach_type;
-	if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+	if (attr.prog_type == BPF_PROG_TYPE_LSM) {
+		attr.lsm_hook_index = load_attr->lsm_hook_index;
+	} else if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
 		attr.attach_btf_id = load_attr->attach_btf_id;
 	} else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
 		attr.attach_btf_id = load_attr->attach_btf_id;
@@ -244,6 +247,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 		attr.prog_ifindex = load_attr->prog_ifindex;
 		attr.kern_version = load_attr->kern_version;
 	}
+
 	attr.insn_cnt = (__u32)load_attr->insns_cnt;
 	attr.insns = ptr_to_u64(load_attr->insns);
 	attr.license = ptr_to_u64(load_attr->license);
tools/lib/bpf/bpf.h +1 -0
index 56341d117e5b..54458a102939 100644
@@ -86,6 +86,7 @@ struct bpf_load_program_attr {
 		__u32 prog_ifindex;
 		__u32 attach_btf_id;
 	};
+	__u32 lsm_hook_index;
 	__u32 prog_btf_fd;
 	__u32 func_info_rec_size;
 	const void *func_info;
tools/lib/bpf/libbpf.c +125 -18

228 lines not on this page · raw message

tools/lib/bpf/libbpf.h +4 -0
index 01639f9a1062..a97e709a29e6 100644
@@ -241,6 +241,8 @@ LIBBPF_API struct bpf_link *
 bpf_program__attach_trace(struct bpf_program *prog);
 struct bpf_map;
 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_lsm(struct bpf_program *prog);
 struct bpf_insn;
 
 /*
@@ -318,6 +320,7 @@ LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
+LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
 
 LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
@@ -339,6 +342,7 @@ LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
+LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
 
 /*
  * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +3 -0
index a19f04e6e3d9..3da0452ce679 100644
@@ -227,4 +227,7 @@ LIBBPF_0.0.7 {
 		bpf_program__is_struct_ops;
 		bpf_program__set_struct_ops;
 		btf__align_of;
+		bpf_program__is_lsm;
+		bpf_program__set_lsm;
+		bpf_program__attach_lsm;
 } LIBBPF_0.0.6;
Signature · 1 line
2.20.1
#21 lore raw

Re: [PATCH bpf-next v2 08/10] tools/libbpf: Add support for BPF_PROG_TYPE_LSM

Quoted 45 lines · KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)
tools/lib/bpf/bpf.c +5 -1
index 500afe478e94..b138d98ff862 100644
@@ -235,7 +235,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
        memset(&attr, 0, sizeof(attr));
        attr.prog_type = load_attr->prog_type;
        attr.expected_attach_type = load_attr->expected_attach_type;
-       if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (attr.prog_type == BPF_PROG_TYPE_LSM) {
+               attr.lsm_hook_index = load_attr->lsm_hook_index;
+       } else if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
                attr.attach_btf_id = load_attr->attach_btf_id;
        } else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
                attr.attach_btf_id = load_attr->attach_btf_id;
@@ -244,6 +247,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
                attr.prog_ifindex = load_attr->prog_ifindex;
                attr.kern_version = load_attr->kern_version;
        }
+
        attr.insn_cnt = (__u32)load_attr->insns_cnt;
        attr.insns = ptr_to_u64(load_attr->insns);
        attr.license = ptr_to_u64(load_attr->license);
tools/lib/bpf/bpf.h +0 -0
index 56341d117e5b..54458a102939 100644

tools/lib/bpf/bpf.h struct bpf_load_program_attr { · line 89

Quoted 4 lines
tools/lib/bpf/bpf.h +1 -0
@@ -86,6 +86,7 @@ struct bpf_load_program_attr {
                __u32 prog_ifindex;
                __u32 attach_btf_id;
        };
+       __u32 lsm_hook_index;

this is changing memory layout of struct bpf_load_program_attr, which is part of public API, so breaking backward compatibility. But I think you intended to put it inside union along the attach_btf_id?

also, we use idx for index pretty consistently (apart from ifindex), so maybe lsm_hook_idx?

Quoted 84 lines
tools/lib/bpf/bpf.h +0 -0
        __u32 prog_btf_fd;
        __u32 func_info_rec_size;
        const void *func_info;
tools/lib/bpf/libbpf.c +20 -2
index 0c229f00a67e..60737559a9a6 100644
@@ -229,6 +229,7 @@ struct bpf_program {
        enum bpf_attach_type expected_attach_type;
        __u32 attach_btf_id;
        __u32 attach_prog_fd;
+       __u32 lsm_hook_index
        void *func_info;
        __u32 func_info_rec_size;
        __u32 func_info_cnt;
@@ -4886,7 +4887,10 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
        load_attr.insns = insns;
        load_attr.insns_cnt = insns_cnt;
        load_attr.license = license;
-       if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               load_attr.lsm_hook_index = prog->lsm_hook_index;
+       } else if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
                load_attr.attach_btf_id = prog->attach_btf_id;
        } else if (prog->type == BPF_PROG_TYPE_TRACING) {
                load_attr.attach_prog_fd = prog->attach_prog_fd;
@@ -4895,6 +4899,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
                load_attr.kern_version = kern_version;
                load_attr.prog_ifindex = prog->prog_ifindex;
        }
+
        /* if .BTF.ext was loaded, kernel supports associated BTF for prog */
        if (prog->obj->btf_ext)
                btf_fd = bpf_object__btf_fd(prog->obj);
@@ -4967,9 +4972,11 @@ static int libbpf_find_attach_btf_id(const char *name,
                                     enum bpf_attach_type attach_type,
                                     __u32 attach_prog_fd);
 
+static __s32 btf__find_lsm_hook_index(const char *name);
+
 int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
 {
-       int err = 0, fd, i, btf_id;
+       int err = 0, fd, i, btf_id, index;
 
        if (prog->type == BPF_PROG_TYPE_TRACING) {
                btf_id = libbpf_find_attach_btf_id(prog->section_name,
@@ -4980,6 +4987,13 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
                prog->attach_btf_id = btf_id;
        }
 
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               index = btf__find_lsm_hook_index(prog->section_name);
+               if (index < 0)
+                       return index;
+               prog->lsm_hook_index = index;
+       }
+
        if (prog->instances.nr < 0 || !prog->instances.fds) {
                if (prog->preprocessor) {
                        pr_warn("Internal error: can't load program '%s'\n",
@@ -6207,6 +6221,7 @@ bool bpf_program__is_##NAME(const struct bpf_program *prog)       \
 }                                                              \
 
 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
+BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
@@ -6272,6 +6287,8 @@ static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
                                      struct bpf_program *prog);
 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
                                     struct bpf_program *prog);
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog);
 
 struct bpf_sec_def {
        const char *sec;
@@ -6315,12 +6332,17 @@ static const struct bpf_sec_def section_defs[] = {
                .expected_attach_type = BPF_TRACE_FEXIT,
                .is_attach_btf = true,
                .attach_fn = attach_trace),
+       SEC_DEF("lsm/", LSM,

tools/lib/bpf/libbpf.c static const struct bpf_sec_def section_defs[] = { · line 6345

Quoted 10 lines
tools/lib/bpf/libbpf.c +4 -0
+               .expected_attach_type = BPF_LSM_MAC,
+               .attach_fn = attach_lsm),
        BPF_PROG_SEC("xdp",                     BPF_PROG_TYPE_XDP),
        BPF_PROG_SEC("perf_event",              BPF_PROG_TYPE_PERF_EVENT),
        BPF_PROG_SEC("lwt_in",                  BPF_PROG_TYPE_LWT_IN),
        BPF_PROG_SEC("lwt_out",                 BPF_PROG_TYPE_LWT_OUT),
        BPF_PROG_SEC("lwt_xmit",                BPF_PROG_TYPE_LWT_XMIT),
        BPF_PROG_SEC("lwt_seg6local",           BPF_PROG_TYPE_LWT_SEG6LOCAL),
+       BPF_PROG_BTF("lsm/",                    BPF_PROG_TYPE_LSM,
+                                               BPF_LSM_MAC),

This is just a duplicate of SEC_DEF above, remove?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6604

Quoted 12 lines
tools/lib/bpf/libbpf.c +4 -1
        BPF_APROG_SEC("cgroup_skb/ingress",     BPF_PROG_TYPE_CGROUP_SKB,
                                                BPF_CGROUP_INET_INGRESS),
        BPF_APROG_SEC("cgroup_skb/egress",      BPF_PROG_TYPE_CGROUP_SKB,
@@ -6576,32 +6598,80 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
        return -EINVAL;
 }
 
-#define BTF_PREFIX "btf_trace_"
+#define BTF_TRACE_PREFIX "btf_trace_"
+
+static inline int btf__find_by_prefix_kind(struct btf *btf, const char *name,
+                                          const char *prefix, __u32 kind)

this is internal helper, not really BTF API, let's call it find_btf_by_prefix_kind? Also const char *prefix more logically should go before name argument?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6608

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+{
+       char btf_type_name[128];
+
+       snprintf(btf_type_name, sizeof(btf_type_name), "%s%s", prefix, name);

check overflow?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6612

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+       return btf__find_by_name_kind(btf, btf_type_name, kind);
+}
+
+static __s32 btf__find_lsm_hook_index(const char *name)

this name is violating libbpf naming guidelines. Just find_lsm_hook_idx for now?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6614

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+{
+       struct btf *btf = bpf_find_kernel_btf();

ok, it's probably time to do this right. Let's ensure we load kernel BTF just once, keep it inside bpf_object while we need it and then release it after successful load. We are at the point where all the new types of program is loading/releasing kernel BTF for every section and it starts to feel very wasteful.

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6620

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       const struct bpf_sec_def *sec_def;
+       const struct btf_type *hl_type;
+       struct btf_member *m;
+       __u16 vlen;
+       __s32 hl_id;
+       int j;

j without having i used anywhere?...

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6631

Quoted 11 lines
tools/lib/bpf/libbpf.c +11 -0
+
+       sec_def = find_sec_def(name);
+       if (!sec_def)
+               return -ESRCH;
+
+       name += sec_def->len;
+
+       hl_id = btf__find_by_name_kind(btf, "security_hook_heads",
+                                      BTF_KIND_STRUCT);
+       if (hl_id < 0) {
+               pr_debug("security_hook_heads cannot be found in BTF\n");

"in vmlinux BTF" ?

and it should be pr_warn(), we don't really expect this, right?

and it should be (hl_id <= 0) with current btf__find_by_name_kind(), and then return hl_id ? : -ESRCH, which further proves we need to change btf__find_by_name_kind() as I suggested below.

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6638

Quoted 7 lines
tools/lib/bpf/libbpf.c +7 -0
+               return hl_id;
+       }
+
+       hl_type = btf__type_by_id(btf, hl_id);
+       if (!hl_type) {
+               pr_warn("Can't find type for security_hook_heads: %u\n", hl_id);
+               return -EINVAL;

-ESRCH?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6644

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       }
+
+       m = btf_members(hl_type);
+       vlen = btf_vlen(hl_type);
+
+       for (j = 0; j < vlen; j++) {

can add succinct , m++ here instead

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6646

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
+                       return j + 1;

I looked briefly through kernel-side patch introducing lsm_hook_index, but it didn't seem to explain why this index needs to be (unnaturally) 1-based. So asking here first as I'm looking through libbpf changes?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6650

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+               m++;
+       }
+
+       pr_warn("Cannot find offset for %s in security_hook_heads\n", name);

it's not offset, rather member index?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6651

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+       return -ENOENT;

not entirely clear about distinction between ENOENT and ESRCH? So far we typically used ESRCH, does ENOENT have more specific semantics?

Quoted 26 lines
tools/lib/bpf/libbpf.c +2 -13
+}
+
 int libbpf_find_vmlinux_btf_id(const char *name,
                               enum bpf_attach_type attach_type)
 {
        struct btf *btf = bpf_find_kernel_btf();
-       char raw_tp_btf[128] = BTF_PREFIX;
-       char *dst = raw_tp_btf + sizeof(BTF_PREFIX) - 1;
-       const char *btf_name;
        int err = -EINVAL;
-       __u32 kind;
 
        if (IS_ERR(btf)) {
                pr_warn("vmlinux BTF is not found\n");
                return -EINVAL;
        }
 
-       if (attach_type == BPF_TRACE_RAW_TP) {
-               /* prepend "btf_trace_" prefix per kernel convention */
-               strncat(dst, name, sizeof(raw_tp_btf) - sizeof(BTF_PREFIX));
-               btf_name = raw_tp_btf;
-               kind = BTF_KIND_TYPEDEF;
-       } else {
-               btf_name = name;
-               kind = BTF_KIND_FUNC;
-       }

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6673

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -1
-       err = btf__find_by_name_kind(btf, btf_name, kind);
+       if (attach_type == BPF_TRACE_RAW_TP)
+               err = btf__find_by_prefix_kind(btf, name, BTF_TRACE_PREFIX,
+                                              BTF_KIND_TYPEDEF);
+       else
+               err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+
+       /* err = 0 means void / UNKNOWN which is treated as an error */
+       if (err == 0)
+               err = -EINVAL;

I think it's actually less error-prone to make btf__find_by_name_kind and btf__find_by_prefix_kind to return -ESRCH when type is not found, instead of a valid type_id 0. I just checked, and struct_ops code already is mishandling it, only checking for <0. Could you make this change and just do a natural <0 check everywhere?

Quoted 27 lines
tools/lib/bpf/libbpf.c +12 -1
+
        btf__free(btf);
        return err;
 }
@@ -6630,7 +6700,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
        }
        err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
        btf__free(btf);
-       if (err <= 0) {
+       if (err < 0) {
                pr_warn("%s is not found in prog's BTF\n", name);
                goto out;
        }
@@ -7395,6 +7465,43 @@ static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
        return bpf_program__attach_trace(prog);
 }
 
+struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
+{
+       char errmsg[STRERR_BUFSIZE];
+       struct bpf_link_fd *link;
+       int prog_fd, pfd;
+
+       prog_fd = bpf_program__fd(prog);
+       if (prog_fd < 0) {
+               pr_warn("program '%s': can't attach before loaded\n",
+                       bpf_program__title(prog, false));

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7487

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+               return ERR_PTR(-EINVAL);
+       }
+
+       link = calloc(1, sizeof(*link));
+       if (!link)
+               return ERR_PTR(-ENOMEM);
+       link->link.detach = &bpf_link__detach_fd;
+
+       pfd = bpf_prog_attach(prog_fd, 0, BPF_LSM_MAC,
+                             BPF_F_ALLOW_OVERRIDE);

do we want to always specify ALLOW_OVERRIDE? Or should it be an option?

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7493

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       if (pfd < 0) {
+               pfd = -errno;
+               pr_warn("program '%s': failed to attach: %s\n",
+                       bpf_program__title(prog, false),
+                       libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
+               return ERR_PTR(pfd);

leaking link here

Quoted 19 lines
tools/lib/bpf/libbpf.c +11 -0
+       }
+       link->fd = pfd;
+       return (struct bpf_link *)link;
+}
+
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog)
+{
+       return bpf_program__attach_lsm(prog);
+}
+
 struct bpf_link *bpf_program__attach(struct bpf_program *prog)
 {
        const struct bpf_sec_def *sec_def;
tools/lib/bpf/libbpf.h +0 -0
index 01639f9a1062..a97e709a29e6 100644

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 245

Quoted 5 lines
tools/lib/bpf/libbpf.h +2 -0
@@ -241,6 +241,8 @@ LIBBPF_API struct bpf_link *
 bpf_program__attach_trace(struct bpf_program *prog);
 struct bpf_map;
 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_lsm(struct bpf_program *prog);

nit: put it after attach_trace, so that program attaches and map attaches are grouped together, not intermixed

Quoted 24 lines
tools/lib/bpf/libbpf.h +2 -0
 struct bpf_insn;
 
 /*
@@ -318,6 +320,7 @@ LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
+LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
 
 LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
@@ -339,6 +342,7 @@ LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
+LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
 
 /*
  * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +0 -0
index a19f04e6e3d9..3da0452ce679 100644

tools/lib/bpf/libbpf.map LIBBPF_0.0.7 { · line 232

Quoted 6 lines
tools/lib/bpf/libbpf.map +3 -0
@@ -227,4 +227,7 @@ LIBBPF_0.0.7 {
                bpf_program__is_struct_ops;
                bpf_program__set_struct_ops;
                btf__align_of;
+               bpf_program__is_lsm;
+               bpf_program__set_lsm;
+               bpf_program__attach_lsm;

preserve alphabetical order, please

Quoted 5 lines
tools/lib/bpf/libbpf.map +0 -1
 } LIBBPF_0.0.6;
 
--

2.20.1

#22 lore raw

Re: [PATCH bpf-next v2 08/10] tools/libbpf: Add support for BPF_PROG_TYPE_LSM

Quoted 299 lines · Andrii Nakryiko
Quoted 45 lines · KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)
tools/lib/bpf/bpf.c +5 -1
index 500afe478e94..b138d98ff862 100644
@@ -235,7 +235,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
        memset(&attr, 0, sizeof(attr));
        attr.prog_type = load_attr->prog_type;
        attr.expected_attach_type = load_attr->expected_attach_type;
-       if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (attr.prog_type == BPF_PROG_TYPE_LSM) {
+               attr.lsm_hook_index = load_attr->lsm_hook_index;
+       } else if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
                attr.attach_btf_id = load_attr->attach_btf_id;
        } else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
                attr.attach_btf_id = load_attr->attach_btf_id;
@@ -244,6 +247,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
                attr.prog_ifindex = load_attr->prog_ifindex;
                attr.kern_version = load_attr->kern_version;
        }
+
        attr.insn_cnt = (__u32)load_attr->insns_cnt;
        attr.insns = ptr_to_u64(load_attr->insns);
        attr.license = ptr_to_u64(load_attr->license);
tools/lib/bpf/bpf.h +0 -0
index 56341d117e5b..54458a102939 100644

tools/lib/bpf/bpf.h struct bpf_load_program_attr { · line 89

Quoted 4 lines
tools/lib/bpf/bpf.h +1 -0
@@ -86,6 +86,7 @@ struct bpf_load_program_attr {
                __u32 prog_ifindex;
                __u32 attach_btf_id;
        };
+       __u32 lsm_hook_index;

this is changing memory layout of struct bpf_load_program_attr, which is part of public API, so breaking backward compatibility. But I think you intended to put it inside union along the attach_btf_id?

also, we use idx for index pretty consistently (apart from ifindex), so maybe lsm_hook_idx?

Quoted 84 lines
tools/lib/bpf/bpf.h +0 -0
        __u32 prog_btf_fd;
        __u32 func_info_rec_size;
        const void *func_info;
tools/lib/bpf/libbpf.c +20 -2
index 0c229f00a67e..60737559a9a6 100644
@@ -229,6 +229,7 @@ struct bpf_program {
        enum bpf_attach_type expected_attach_type;
        __u32 attach_btf_id;
        __u32 attach_prog_fd;
+       __u32 lsm_hook_index
        void *func_info;
        __u32 func_info_rec_size;
        __u32 func_info_cnt;
@@ -4886,7 +4887,10 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
        load_attr.insns = insns;
        load_attr.insns_cnt = insns_cnt;
        load_attr.license = license;
-       if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               load_attr.lsm_hook_index = prog->lsm_hook_index;
+       } else if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
                load_attr.attach_btf_id = prog->attach_btf_id;
        } else if (prog->type == BPF_PROG_TYPE_TRACING) {
                load_attr.attach_prog_fd = prog->attach_prog_fd;
@@ -4895,6 +4899,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
                load_attr.kern_version = kern_version;
                load_attr.prog_ifindex = prog->prog_ifindex;
        }
+
        /* if .BTF.ext was loaded, kernel supports associated BTF for prog */
        if (prog->obj->btf_ext)
                btf_fd = bpf_object__btf_fd(prog->obj);
@@ -4967,9 +4972,11 @@ static int libbpf_find_attach_btf_id(const char *name,
                                     enum bpf_attach_type attach_type,
                                     __u32 attach_prog_fd);
 
+static __s32 btf__find_lsm_hook_index(const char *name);
+
 int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
 {
-       int err = 0, fd, i, btf_id;
+       int err = 0, fd, i, btf_id, index;
 
        if (prog->type == BPF_PROG_TYPE_TRACING) {
                btf_id = libbpf_find_attach_btf_id(prog->section_name,
@@ -4980,6 +4987,13 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
                prog->attach_btf_id = btf_id;
        }
 
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               index = btf__find_lsm_hook_index(prog->section_name);
+               if (index < 0)
+                       return index;
+               prog->lsm_hook_index = index;
+       }
+
        if (prog->instances.nr < 0 || !prog->instances.fds) {
                if (prog->preprocessor) {
                        pr_warn("Internal error: can't load program '%s'\n",
@@ -6207,6 +6221,7 @@ bool bpf_program__is_##NAME(const struct bpf_program *prog)       \
 }                                                              \
 
 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
+BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
@@ -6272,6 +6287,8 @@ static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
                                      struct bpf_program *prog);
 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
                                     struct bpf_program *prog);
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog);
 
 struct bpf_sec_def {
        const char *sec;
@@ -6315,12 +6332,17 @@ static const struct bpf_sec_def section_defs[] = {
                .expected_attach_type = BPF_TRACE_FEXIT,
                .is_attach_btf = true,
                .attach_fn = attach_trace),
+       SEC_DEF("lsm/", LSM,

tools/lib/bpf/libbpf.c static const struct bpf_sec_def section_defs[] = { · line 6345

Quoted 10 lines
tools/lib/bpf/libbpf.c +4 -0
+               .expected_attach_type = BPF_LSM_MAC,
+               .attach_fn = attach_lsm),
        BPF_PROG_SEC("xdp",                     BPF_PROG_TYPE_XDP),
        BPF_PROG_SEC("perf_event",              BPF_PROG_TYPE_PERF_EVENT),
        BPF_PROG_SEC("lwt_in",                  BPF_PROG_TYPE_LWT_IN),
        BPF_PROG_SEC("lwt_out",                 BPF_PROG_TYPE_LWT_OUT),
        BPF_PROG_SEC("lwt_xmit",                BPF_PROG_TYPE_LWT_XMIT),
        BPF_PROG_SEC("lwt_seg6local",           BPF_PROG_TYPE_LWT_SEG6LOCAL),
+       BPF_PROG_BTF("lsm/",                    BPF_PROG_TYPE_LSM,
+                                               BPF_LSM_MAC),

This is just a duplicate of SEC_DEF above, remove?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6604

Quoted 12 lines
tools/lib/bpf/libbpf.c +4 -1
        BPF_APROG_SEC("cgroup_skb/ingress",     BPF_PROG_TYPE_CGROUP_SKB,
                                                BPF_CGROUP_INET_INGRESS),
        BPF_APROG_SEC("cgroup_skb/egress",      BPF_PROG_TYPE_CGROUP_SKB,
@@ -6576,32 +6598,80 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
        return -EINVAL;
 }
 
-#define BTF_PREFIX "btf_trace_"
+#define BTF_TRACE_PREFIX "btf_trace_"
+
+static inline int btf__find_by_prefix_kind(struct btf *btf, const char *name,
+                                          const char *prefix, __u32 kind)

this is internal helper, not really BTF API, let's call it find_btf_by_prefix_kind? Also const char *prefix more logically should go before name argument?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6608

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+{
+       char btf_type_name[128];
+
+       snprintf(btf_type_name, sizeof(btf_type_name), "%s%s", prefix, name);

check overflow?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6612

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+       return btf__find_by_name_kind(btf, btf_type_name, kind);
+}
+
+static __s32 btf__find_lsm_hook_index(const char *name)

this name is violating libbpf naming guidelines. Just find_lsm_hook_idx for now?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6614

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+{
+       struct btf *btf = bpf_find_kernel_btf();

ok, it's probably time to do this right. Let's ensure we load kernel BTF just once, keep it inside bpf_object while we need it and then release it after successful load. We are at the point where all the new types of program is loading/releasing kernel BTF for every section and it starts to feel very wasteful.

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6620

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       const struct bpf_sec_def *sec_def;
+       const struct btf_type *hl_type;
+       struct btf_member *m;
+       __u16 vlen;
+       __s32 hl_id;
+       int j;

j without having i used anywhere?...

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6631

Quoted 11 lines
tools/lib/bpf/libbpf.c +11 -0
+
+       sec_def = find_sec_def(name);
+       if (!sec_def)
+               return -ESRCH;
+
+       name += sec_def->len;
+
+       hl_id = btf__find_by_name_kind(btf, "security_hook_heads",
+                                      BTF_KIND_STRUCT);
+       if (hl_id < 0) {
+               pr_debug("security_hook_heads cannot be found in BTF\n");

"in vmlinux BTF" ?

and it should be pr_warn(), we don't really expect this, right?

and it should be (hl_id <= 0) with current btf__find_by_name_kind(), and then return hl_id ? : -ESRCH, which further proves we need to change btf__find_by_name_kind() as I suggested below.

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6638

Quoted 7 lines
tools/lib/bpf/libbpf.c +7 -0
+               return hl_id;
+       }
+
+       hl_type = btf__type_by_id(btf, hl_id);
+       if (!hl_type) {
+               pr_warn("Can't find type for security_hook_heads: %u\n", hl_id);
+               return -EINVAL;

-ESRCH?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6644

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       }
+
+       m = btf_members(hl_type);
+       vlen = btf_vlen(hl_type);
+
+       for (j = 0; j < vlen; j++) {

can add succinct , m++ here instead

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6646

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
+                       return j + 1;

I looked briefly through kernel-side patch introducing lsm_hook_index, but it didn't seem to explain why this index needs to be (unnaturally) 1-based. So asking here first as I'm looking through libbpf changes?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6650

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+               m++;
+       }
+
+       pr_warn("Cannot find offset for %s in security_hook_heads\n", name);

it's not offset, rather member index?

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6651

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+       return -ENOENT;

not entirely clear about distinction between ENOENT and ESRCH? So far we typically used ESRCH, does ENOENT have more specific semantics?

Quoted 34 lines
tools/lib/bpf/libbpf.c +9 -14
+}
+
 int libbpf_find_vmlinux_btf_id(const char *name,
                               enum bpf_attach_type attach_type)
 {
        struct btf *btf = bpf_find_kernel_btf();
-       char raw_tp_btf[128] = BTF_PREFIX;
-       char *dst = raw_tp_btf + sizeof(BTF_PREFIX) - 1;
-       const char *btf_name;
        int err = -EINVAL;
-       __u32 kind;
 
        if (IS_ERR(btf)) {
                pr_warn("vmlinux BTF is not found\n");
                return -EINVAL;
        }
 
-       if (attach_type == BPF_TRACE_RAW_TP) {
-               /* prepend "btf_trace_" prefix per kernel convention */
-               strncat(dst, name, sizeof(raw_tp_btf) - sizeof(BTF_PREFIX));
-               btf_name = raw_tp_btf;
-               kind = BTF_KIND_TYPEDEF;
-       } else {
-               btf_name = name;
-               kind = BTF_KIND_FUNC;
-       }
-       err = btf__find_by_name_kind(btf, btf_name, kind);
+       if (attach_type == BPF_TRACE_RAW_TP)
+               err = btf__find_by_prefix_kind(btf, name, BTF_TRACE_PREFIX,
+                                              BTF_KIND_TYPEDEF);
+       else
+               err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+
+       /* err = 0 means void / UNKNOWN which is treated as an error */
Quoted 8 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6673

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+       if (err == 0)
+               err = -EINVAL;

I think it's actually less error-prone to make btf__find_by_name_kind and btf__find_by_prefix_kind to return -ESRCH when type is not found, instead of a valid type_id 0. I just checked, and struct_ops code already is mishandling it, only checking for <0. Could you make this change and just do a natural <0 check everywhere?

ok, re-read btf__find_by_name_kind() source code. It will return -ENOENT when type is not handled (unless type name is "void", that part is not clear and should be fixed separately), so this (err == 0) check shouldn't be here. Disregard my comments about changing return results of btf__find_by_name_kind(), please.

Quoted 12 lines

tools/lib/bpf/libbpf.c static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) · line 6703

Quoted 12 lines
tools/lib/bpf/libbpf.c +2 -1
+
        btf__free(btf);
        return err;
 }
@@ -6630,7 +6700,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
        }
        err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
        btf__free(btf);
-       if (err <= 0) {
+       if (err < 0) {

This change is good, thanks!

Quoted 105 lines
Quoted 17 lines
tools/lib/bpf/libbpf.c +10 -0
                pr_warn("%s is not found in prog's BTF\n", name);
                goto out;
        }
@@ -7395,6 +7465,43 @@ static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
        return bpf_program__attach_trace(prog);
 }
 
+struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
+{
+       char errmsg[STRERR_BUFSIZE];
+       struct bpf_link_fd *link;
+       int prog_fd, pfd;
+
+       prog_fd = bpf_program__fd(prog);
+       if (prog_fd < 0) {
+               pr_warn("program '%s': can't attach before loaded\n",
+                       bpf_program__title(prog, false));

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7487

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+               return ERR_PTR(-EINVAL);
+       }
+
+       link = calloc(1, sizeof(*link));
+       if (!link)
+               return ERR_PTR(-ENOMEM);
+       link->link.detach = &bpf_link__detach_fd;
+
+       pfd = bpf_prog_attach(prog_fd, 0, BPF_LSM_MAC,
+                             BPF_F_ALLOW_OVERRIDE);

do we want to always specify ALLOW_OVERRIDE? Or should it be an option?

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7493

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       if (pfd < 0) {
+               pfd = -errno;
+               pr_warn("program '%s': failed to attach: %s\n",
+                       bpf_program__title(prog, false),
+                       libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
+               return ERR_PTR(pfd);

leaking link here

Quoted 19 lines
tools/lib/bpf/libbpf.c +11 -0
+       }
+       link->fd = pfd;
+       return (struct bpf_link *)link;
+}
+
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog)
+{
+       return bpf_program__attach_lsm(prog);
+}
+
 struct bpf_link *bpf_program__attach(struct bpf_program *prog)
 {
        const struct bpf_sec_def *sec_def;
tools/lib/bpf/libbpf.h +0 -0
index 01639f9a1062..a97e709a29e6 100644

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 245

Quoted 5 lines
tools/lib/bpf/libbpf.h +2 -0
@@ -241,6 +241,8 @@ LIBBPF_API struct bpf_link *
 bpf_program__attach_trace(struct bpf_program *prog);
 struct bpf_map;
 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_lsm(struct bpf_program *prog);

nit: put it after attach_trace, so that program attaches and map attaches are grouped together, not intermixed

Quoted 24 lines
tools/lib/bpf/libbpf.h +2 -0
 struct bpf_insn;
 
 /*
@@ -318,6 +320,7 @@ LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
+LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
 
 LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
@@ -339,6 +342,7 @@ LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
+LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
 
 /*
  * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +0 -0
index a19f04e6e3d9..3da0452ce679 100644

tools/lib/bpf/libbpf.map LIBBPF_0.0.7 { · line 232

Quoted 6 lines
tools/lib/bpf/libbpf.map +3 -0
@@ -227,4 +227,7 @@ LIBBPF_0.0.7 {
                bpf_program__is_struct_ops;
                bpf_program__set_struct_ops;
                btf__align_of;
+               bpf_program__is_lsm;
+               bpf_program__set_lsm;
+               bpf_program__attach_lsm;

preserve alphabetical order, please

Quoted 5 lines
tools/lib/bpf/libbpf.map +0 -1
 } LIBBPF_0.0.6;
 
--

2.20.1

author
#23 lore raw

Re: [PATCH bpf-next v2 08/10] tools/libbpf: Add support for BPF_PROG_TYPE_LSM

In reply to Andrii Nakryiko

Thanks for the review Andrii!

I will incorporate the fixes in the next revision.

Quoted 47 lines · Andrii Nakryiko
Quoted 46 lines · KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)
tools/lib/bpf/bpf.c +5 -1
index 500afe478e94..b138d98ff862 100644
@@ -235,7 +235,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
        memset(&attr, 0, sizeof(attr));
        attr.prog_type = load_attr->prog_type;
        attr.expected_attach_type = load_attr->expected_attach_type;
-       if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (attr.prog_type == BPF_PROG_TYPE_LSM) {
+               attr.lsm_hook_index = load_attr->lsm_hook_index;
+       } else if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
                attr.attach_btf_id = load_attr->attach_btf_id;
        } else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
                attr.attach_btf_id = load_attr->attach_btf_id;
@@ -244,6 +247,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
                attr.prog_ifindex = load_attr->prog_ifindex;
                attr.kern_version = load_attr->kern_version;
        }
+
        attr.insn_cnt = (__u32)load_attr->insns_cnt;
        attr.insns = ptr_to_u64(load_attr->insns);
        attr.license = ptr_to_u64(load_attr->license);
tools/lib/bpf/bpf.h +0 -0
index 56341d117e5b..54458a102939 100644
@@ -86,6 +86,7 @@ struct bpf_load_program_attr {
                __u32 prog_ifindex;
Quoted 9 lines

tools/lib/bpf/bpf.h struct bpf_load_program_attr { · line 89

Quoted 3 lines
tools/lib/bpf/bpf.h +1 -0
                __u32 attach_btf_id;
        };
+       __u32 lsm_hook_index;

this is changing memory layout of struct bpf_load_program_attr, which is part of public API, so breaking backward compatibility. But I think you intended to put it inside union along the attach_btf_id?

Correct, I moved it to the union.

Quoted 2 lines

also, we use idx for index pretty consistently (apart from ifindex), so maybe lsm_hook_idx?

Renamed all usages of index -> idx.

Quoted 89 lines
Quoted 89 lines
tools/lib/bpf/bpf.h +0 -0
        __u32 prog_btf_fd;
        __u32 func_info_rec_size;
        const void *func_info;
tools/lib/bpf/libbpf.c +22 -2
index 0c229f00a67e..60737559a9a6 100644
@@ -229,6 +229,7 @@ struct bpf_program {
        enum bpf_attach_type expected_attach_type;
        __u32 attach_btf_id;
        __u32 attach_prog_fd;
+       __u32 lsm_hook_index
        void *func_info;
        __u32 func_info_rec_size;
        __u32 func_info_cnt;
@@ -4886,7 +4887,10 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
        load_attr.insns = insns;
        load_attr.insns_cnt = insns_cnt;
        load_attr.license = license;
-       if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               load_attr.lsm_hook_index = prog->lsm_hook_index;
+       } else if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
                load_attr.attach_btf_id = prog->attach_btf_id;
        } else if (prog->type == BPF_PROG_TYPE_TRACING) {
                load_attr.attach_prog_fd = prog->attach_prog_fd;
@@ -4895,6 +4899,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
                load_attr.kern_version = kern_version;
                load_attr.prog_ifindex = prog->prog_ifindex;
        }
+
        /* if .BTF.ext was loaded, kernel supports associated BTF for prog */
        if (prog->obj->btf_ext)
                btf_fd = bpf_object__btf_fd(prog->obj);
@@ -4967,9 +4972,11 @@ static int libbpf_find_attach_btf_id(const char *name,
                                     enum bpf_attach_type attach_type,
                                     __u32 attach_prog_fd);
 
+static __s32 btf__find_lsm_hook_index(const char *name);
+
 int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
 {
-       int err = 0, fd, i, btf_id;
+       int err = 0, fd, i, btf_id, index;
 
        if (prog->type == BPF_PROG_TYPE_TRACING) {
                btf_id = libbpf_find_attach_btf_id(prog->section_name,
@@ -4980,6 +4987,13 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
                prog->attach_btf_id = btf_id;
        }
 
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               index = btf__find_lsm_hook_index(prog->section_name);
+               if (index < 0)
+                       return index;
+               prog->lsm_hook_index = index;
+       }
+
        if (prog->instances.nr < 0 || !prog->instances.fds) {
                if (prog->preprocessor) {
                        pr_warn("Internal error: can't load program '%s'\n",
@@ -6207,6 +6221,7 @@ bool bpf_program__is_##NAME(const struct bpf_program *prog)       \
 }                                                              \
 
 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
+BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
@@ -6272,6 +6287,8 @@ static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
                                      struct bpf_program *prog);
 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
                                     struct bpf_program *prog);
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog);
 
 struct bpf_sec_def {
        const char *sec;
@@ -6315,12 +6332,17 @@ static const struct bpf_sec_def section_defs[] = {
                .expected_attach_type = BPF_TRACE_FEXIT,
                .is_attach_btf = true,
                .attach_fn = attach_trace),
+       SEC_DEF("lsm/", LSM,
+               .expected_attach_type = BPF_LSM_MAC,
+               .attach_fn = attach_lsm),
        BPF_PROG_SEC("xdp",                     BPF_PROG_TYPE_XDP),
        BPF_PROG_SEC("perf_event",              BPF_PROG_TYPE_PERF_EVENT),
Quoted 8 lines

tools/lib/bpf/libbpf.c static const struct bpf_sec_def section_defs[] = { · line 6345

Quoted 6 lines
tools/lib/bpf/libbpf.c +2 -0
        BPF_PROG_SEC("lwt_in",                  BPF_PROG_TYPE_LWT_IN),
        BPF_PROG_SEC("lwt_out",                 BPF_PROG_TYPE_LWT_OUT),
        BPF_PROG_SEC("lwt_xmit",                BPF_PROG_TYPE_LWT_XMIT),
        BPF_PROG_SEC("lwt_seg6local",           BPF_PROG_TYPE_LWT_SEG6LOCAL),
+       BPF_PROG_BTF("lsm/",                    BPF_PROG_TYPE_LSM,
+                                               BPF_LSM_MAC),

This is just a duplicate of SEC_DEF above, remove?

Oops. Removed.

Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
        BPF_APROG_SEC("cgroup_skb/ingress",     BPF_PROG_TYPE_CGROUP_SKB,
                                                BPF_CGROUP_INET_INGRESS),
        BPF_APROG_SEC("cgroup_skb/egress",      BPF_PROG_TYPE_CGROUP_SKB,
@@ -6576,32 +6598,80 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
        return -EINVAL;
 }
 
Quoted 9 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6604

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -1
-#define BTF_PREFIX "btf_trace_"
+#define BTF_TRACE_PREFIX "btf_trace_"
+
+static inline int btf__find_by_prefix_kind(struct btf *btf, const char *name,
+                                          const char *prefix, __u32 kind)

this is internal helper, not really BTF API, let's call it find_btf_by_prefix_kind? Also const char *prefix more logically should go before name argument?

Done.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6608

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -0
+{
+       char btf_type_name[128];
+
+       snprintf(btf_type_name, sizeof(btf_type_name), "%s%s", prefix, name);

check overflow?

Done.

Quoted 8 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6612

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -0
+       return btf__find_by_name_kind(btf, btf_type_name, kind);
+}
+
+static __s32 btf__find_lsm_hook_index(const char *name)

this name is violating libbpf naming guidelines. Just find_lsm_hook_idx for now?

Cool. I think I finally hang of the naming convention now :). I mixed up btf.c and libbpf.c here.

Quoted 9 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6614

Quoted 3 lines
tools/lib/bpf/libbpf.c +2 -0
+{
+       struct btf *btf = bpf_find_kernel_btf();

ok, it's probably time to do this right. Let's ensure we load kernel BTF just once, keep it inside bpf_object while we need it and then release it after successful load. We are at the point where all the new types of program is loading/releasing kernel BTF for every section and it starts to feel very wasteful.

Sure, will give it a shot in v3.

Quoted 9 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6620

Quoted 7 lines
tools/lib/bpf/libbpf.c +6 -0
+       const struct bpf_sec_def *sec_def;
+       const struct btf_type *hl_type;
+       struct btf_member *m;
+       __u16 vlen;
+       __s32 hl_id;
+       int j;

j without having i used anywhere?...

Fixed.

Quoted 15 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6631

Quoted 12 lines
tools/lib/bpf/libbpf.c +11 -0
+
+       sec_def = find_sec_def(name);
+       if (!sec_def)
+               return -ESRCH;
+
+       name += sec_def->len;
+
+       hl_id = btf__find_by_name_kind(btf, "security_hook_heads",
+                                      BTF_KIND_STRUCT);
+       if (hl_id < 0) {
+               pr_debug("security_hook_heads cannot be found in BTF\n");

"in vmlinux BTF" ?

Quoted 1 line

and it should be pr_warn(), we don't really expect this, right?

Fixed both.

Quoted 13 lines

and it should be (hl_id <= 0) with current btf__find_by_name_kind(), and then return hl_id ? : -ESRCH, which further proves we need to change btf__find_by_name_kind() as I suggested below.

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6638

Quoted 7 lines
tools/lib/bpf/libbpf.c +7 -0
+               return hl_id;
+       }
+
+       hl_type = btf__type_by_id(btf, hl_id);
+       if (!hl_type) {
+               pr_warn("Can't find type for security_hook_heads: %u\n", hl_id);
+               return -EINVAL;
Quoted 1 line

-ESRCH?

Done.

Quoted 9 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6644

Quoted 7 lines
tools/lib/bpf/libbpf.c +6 -0
+       }
+
+       m = btf_members(hl_type);
+       vlen = btf_vlen(hl_type);
+
+       for (j = 0; j < vlen; j++) {

can add succinct , m++ here instead

Done.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6646

Quoted 3 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
+                       return j + 1;

I looked briefly through kernel-side patch introducing lsm_hook_index, but it didn't seem to explain why this index needs to be (unnaturally) 1-based. So asking here first as I'm looking through libbpf changes?

The lsm_hook_idx is one-based as it makes it easy to validate the input. If we make it zero-based it's hard to check if the user intended to attach to the LSM hook at index 0 or did not set it.

We are then up to the verifier to reject the loaded program which may or may not match the signature of the hook at lsm_hook_idx = 0.

I will clarify this in the commit log as well.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6650

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -0
+               m++;
+       }
+
+       pr_warn("Cannot find offset for %s in security_hook_heads\n", name);

it's not offset, rather member index?

Correct, fixed.

Quoted 5 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6651

Quoted 2 lines
tools/lib/bpf/libbpf.c +1 -0
+       return -ENOENT;

not entirely clear about distinction between ENOENT and ESRCH? So far we typically used ESRCH, does ENOENT have more specific semantics?

Updated it to ESRCH, to be consistent with libbpf's convention.

Quoted 76 lines
Quoted 27 lines
tools/lib/bpf/libbpf.c +2 -13
+}
+
 int libbpf_find_vmlinux_btf_id(const char *name,
                               enum bpf_attach_type attach_type)
 {
        struct btf *btf = bpf_find_kernel_btf();
-       char raw_tp_btf[128] = BTF_PREFIX;
-       char *dst = raw_tp_btf + sizeof(BTF_PREFIX) - 1;
-       const char *btf_name;
        int err = -EINVAL;
-       __u32 kind;
 
        if (IS_ERR(btf)) {
                pr_warn("vmlinux BTF is not found\n");
                return -EINVAL;
        }
 
-       if (attach_type == BPF_TRACE_RAW_TP) {
-               /* prepend "btf_trace_" prefix per kernel convention */
-               strncat(dst, name, sizeof(raw_tp_btf) - sizeof(BTF_PREFIX));
-               btf_name = raw_tp_btf;
-               kind = BTF_KIND_TYPEDEF;
-       } else {
-               btf_name = name;
-               kind = BTF_KIND_FUNC;
-       }

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6673

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -1
-       err = btf__find_by_name_kind(btf, btf_name, kind);
+       if (attach_type == BPF_TRACE_RAW_TP)
+               err = btf__find_by_prefix_kind(btf, name, BTF_TRACE_PREFIX,
+                                              BTF_KIND_TYPEDEF);
+       else
+               err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+
+       /* err = 0 means void / UNKNOWN which is treated as an error */
+       if (err == 0)
+               err = -EINVAL;

I think it's actually less error-prone to make btf__find_by_name_kind and btf__find_by_prefix_kind to return -ESRCH when type is not found, instead of a valid type_id 0. I just checked, and struct_ops code already is mishandling it, only checking for <0. Could you make this change and just do a natural <0 check everywhere?

Quoted 31 lines
tools/lib/bpf/libbpf.c +16 -1
+
        btf__free(btf);
        return err;
 }
@@ -6630,7 +6700,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
        }
        err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
        btf__free(btf);
-       if (err <= 0) {
+       if (err < 0) {
                pr_warn("%s is not found in prog's BTF\n", name);
                goto out;
        }
@@ -7395,6 +7465,43 @@ static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
        return bpf_program__attach_trace(prog);
 }
 
+struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
+{
+       char errmsg[STRERR_BUFSIZE];
+       struct bpf_link_fd *link;
+       int prog_fd, pfd;
+
+       prog_fd = bpf_program__fd(prog);
+       if (prog_fd < 0) {
+               pr_warn("program '%s': can't attach before loaded\n",
+                       bpf_program__title(prog, false));
+               return ERR_PTR(-EINVAL);
+       }
+
+       link = calloc(1, sizeof(*link));
Quoted 8 lines

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7487

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       if (!link)
+               return ERR_PTR(-ENOMEM);
+       link->link.detach = &bpf_link__detach_fd;
+
+       pfd = bpf_prog_attach(prog_fd, 0, BPF_LSM_MAC,
+                             BPF_F_ALLOW_OVERRIDE);

do we want to always specify ALLOW_OVERRIDE? Or should it be an option?

I think this is a relic from the duplicate attachment code. With the anonymous-fd + link which can be destroyed. The way the OVERRIDE should work is:

- Destroy the link (detach)
- And attach.

We don't use the BPF_F_ALLOW_OVERRIDE in the attach logic in the LSM. So I will get rid of that.

Quoted 9 lines

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7493

Quoted 7 lines
tools/lib/bpf/libbpf.c +6 -0
+       if (pfd < 0) {
+               pfd = -errno;
+               pr_warn("program '%s': failed to attach: %s\n",
+                       bpf_program__title(prog, false),
+                       libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
+               return ERR_PTR(pfd);

leaking link here

Fixed.

Quoted 20 lines
Quoted 20 lines
tools/lib/bpf/libbpf.c +11 -0
+       }
+       link->fd = pfd;
+       return (struct bpf_link *)link;
+}
+
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog)
+{
+       return bpf_program__attach_lsm(prog);
+}
+
 struct bpf_link *bpf_program__attach(struct bpf_program *prog)
 {
        const struct bpf_sec_def *sec_def;
tools/lib/bpf/libbpf.h +0 -0
index 01639f9a1062..a97e709a29e6 100644
@@ -241,6 +241,8 @@ LIBBPF_API struct bpf_link *
Quoted 8 lines

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 245

Quoted 5 lines
tools/lib/bpf/libbpf.h +2 -0
 bpf_program__attach_trace(struct bpf_program *prog);
 struct bpf_map;
 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_lsm(struct bpf_program *prog);

nit: put it after attach_trace, so that program attaches and map attaches are grouped together, not intermixed

Done.

Quoted 18 lines
Quoted 18 lines
tools/lib/bpf/libbpf.h +2 -0
 struct bpf_insn;
 
 /*
@@ -318,6 +320,7 @@ LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
+LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
 
 LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
@@ -339,6 +342,7 @@ LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
+LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
 
Quoted 15 lines

tools/lib/bpf/libbpf.map LIBBPF_0.0.7 { · line 232

Quoted 13 lines
tools/lib/bpf/libbpf.h +0 -0
 /*
  * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +3 -0
index a19f04e6e3d9..3da0452ce679 100644
@@ -227,4 +227,7 @@ LIBBPF_0.0.7 {
                bpf_program__is_struct_ops;
                bpf_program__set_struct_ops;
                btf__align_of;
+               bpf_program__is_lsm;
+               bpf_program__set_lsm;
+               bpf_program__attach_lsm;

preserve alphabetical order, please

Sure.

Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.map +0 -1
 } LIBBPF_0.0.6;
 
--

2.20.1

author
#24 lore raw

Re: [PATCH bpf-next v2 08/10] tools/libbpf: Add support for BPF_PROG_TYPE_LSM

Quoted 215 lines · KP Singh

Thanks for the review Andrii!

I will incorporate the fixes in the next revision.

Quoted 47 lines · Andrii Nakryiko
Quoted 46 lines · KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)
tools/lib/bpf/bpf.c +5 -1
index 500afe478e94..b138d98ff862 100644
@@ -235,7 +235,10 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
        memset(&attr, 0, sizeof(attr));
        attr.prog_type = load_attr->prog_type;
        attr.expected_attach_type = load_attr->expected_attach_type;
-       if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (attr.prog_type == BPF_PROG_TYPE_LSM) {
+               attr.lsm_hook_index = load_attr->lsm_hook_index;
+       } else if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
                attr.attach_btf_id = load_attr->attach_btf_id;
        } else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
                attr.attach_btf_id = load_attr->attach_btf_id;
@@ -244,6 +247,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
                attr.prog_ifindex = load_attr->prog_ifindex;
                attr.kern_version = load_attr->kern_version;
        }
+
        attr.insn_cnt = (__u32)load_attr->insns_cnt;
        attr.insns = ptr_to_u64(load_attr->insns);
        attr.license = ptr_to_u64(load_attr->license);
tools/lib/bpf/bpf.h +0 -0
index 56341d117e5b..54458a102939 100644
@@ -86,6 +86,7 @@ struct bpf_load_program_attr {
                __u32 prog_ifindex;
Quoted 9 lines

tools/lib/bpf/bpf.h struct bpf_load_program_attr { · line 89

Quoted 3 lines
tools/lib/bpf/bpf.h +1 -0
                __u32 attach_btf_id;
        };
+       __u32 lsm_hook_index;

this is changing memory layout of struct bpf_load_program_attr, which is part of public API, so breaking backward compatibility. But I think you intended to put it inside union along the attach_btf_id?

Correct, I moved it to the union.

Quoted 2 lines

also, we use idx for index pretty consistently (apart from ifindex), so maybe lsm_hook_idx?

Renamed all usages of index -> idx.

Quoted 89 lines
Quoted 89 lines
tools/lib/bpf/bpf.h +0 -0
        __u32 prog_btf_fd;
        __u32 func_info_rec_size;
        const void *func_info;
tools/lib/bpf/libbpf.c +22 -2
index 0c229f00a67e..60737559a9a6 100644
@@ -229,6 +229,7 @@ struct bpf_program {
        enum bpf_attach_type expected_attach_type;
        __u32 attach_btf_id;
        __u32 attach_prog_fd;
+       __u32 lsm_hook_index
        void *func_info;
        __u32 func_info_rec_size;
        __u32 func_info_cnt;
@@ -4886,7 +4887,10 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
        load_attr.insns = insns;
        load_attr.insns_cnt = insns_cnt;
        load_attr.license = license;
-       if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
+
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               load_attr.lsm_hook_index = prog->lsm_hook_index;
+       } else if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
                load_attr.attach_btf_id = prog->attach_btf_id;
        } else if (prog->type == BPF_PROG_TYPE_TRACING) {
                load_attr.attach_prog_fd = prog->attach_prog_fd;
@@ -4895,6 +4899,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
                load_attr.kern_version = kern_version;
                load_attr.prog_ifindex = prog->prog_ifindex;
        }
+
        /* if .BTF.ext was loaded, kernel supports associated BTF for prog */
        if (prog->obj->btf_ext)
                btf_fd = bpf_object__btf_fd(prog->obj);
@@ -4967,9 +4972,11 @@ static int libbpf_find_attach_btf_id(const char *name,
                                     enum bpf_attach_type attach_type,
                                     __u32 attach_prog_fd);
 
+static __s32 btf__find_lsm_hook_index(const char *name);
+
 int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
 {
-       int err = 0, fd, i, btf_id;
+       int err = 0, fd, i, btf_id, index;
 
        if (prog->type == BPF_PROG_TYPE_TRACING) {
                btf_id = libbpf_find_attach_btf_id(prog->section_name,
@@ -4980,6 +4987,13 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
                prog->attach_btf_id = btf_id;
        }
 
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               index = btf__find_lsm_hook_index(prog->section_name);
+               if (index < 0)
+                       return index;
+               prog->lsm_hook_index = index;
+       }
+
        if (prog->instances.nr < 0 || !prog->instances.fds) {
                if (prog->preprocessor) {
                        pr_warn("Internal error: can't load program '%s'\n",
@@ -6207,6 +6221,7 @@ bool bpf_program__is_##NAME(const struct bpf_program *prog)       \
 }                                                              \
 
 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
+BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
@@ -6272,6 +6287,8 @@ static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
                                      struct bpf_program *prog);
 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
                                     struct bpf_program *prog);
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog);
 
 struct bpf_sec_def {
        const char *sec;
@@ -6315,12 +6332,17 @@ static const struct bpf_sec_def section_defs[] = {
                .expected_attach_type = BPF_TRACE_FEXIT,
                .is_attach_btf = true,
                .attach_fn = attach_trace),
+       SEC_DEF("lsm/", LSM,
+               .expected_attach_type = BPF_LSM_MAC,
+               .attach_fn = attach_lsm),
        BPF_PROG_SEC("xdp",                     BPF_PROG_TYPE_XDP),
        BPF_PROG_SEC("perf_event",              BPF_PROG_TYPE_PERF_EVENT),
Quoted 8 lines

tools/lib/bpf/libbpf.c static const struct bpf_sec_def section_defs[] = { · line 6345

Quoted 6 lines
tools/lib/bpf/libbpf.c +2 -0
        BPF_PROG_SEC("lwt_in",                  BPF_PROG_TYPE_LWT_IN),
        BPF_PROG_SEC("lwt_out",                 BPF_PROG_TYPE_LWT_OUT),
        BPF_PROG_SEC("lwt_xmit",                BPF_PROG_TYPE_LWT_XMIT),
        BPF_PROG_SEC("lwt_seg6local",           BPF_PROG_TYPE_LWT_SEG6LOCAL),
+       BPF_PROG_BTF("lsm/",                    BPF_PROG_TYPE_LSM,
+                                               BPF_LSM_MAC),

This is just a duplicate of SEC_DEF above, remove?

Oops. Removed.

Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
        BPF_APROG_SEC("cgroup_skb/ingress",     BPF_PROG_TYPE_CGROUP_SKB,
                                                BPF_CGROUP_INET_INGRESS),
        BPF_APROG_SEC("cgroup_skb/egress",      BPF_PROG_TYPE_CGROUP_SKB,
@@ -6576,32 +6598,80 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
        return -EINVAL;
 }
 
Quoted 9 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6604

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -1
-#define BTF_PREFIX "btf_trace_"
+#define BTF_TRACE_PREFIX "btf_trace_"
+
+static inline int btf__find_by_prefix_kind(struct btf *btf, const char *name,
+                                          const char *prefix, __u32 kind)

this is internal helper, not really BTF API, let's call it find_btf_by_prefix_kind? Also const char *prefix more logically should go before name argument?

Done.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6608

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -0
+{
+       char btf_type_name[128];
+
+       snprintf(btf_type_name, sizeof(btf_type_name), "%s%s", prefix, name);

check overflow?

Done.

Quoted 8 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6612

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -0
+       return btf__find_by_name_kind(btf, btf_type_name, kind);
+}
+
+static __s32 btf__find_lsm_hook_index(const char *name)

this name is violating libbpf naming guidelines. Just find_lsm_hook_idx for now?

Cool. I think I finally hang of the naming convention now :). I mixed up btf.c and libbpf.c here.

Quoted 4 lines
Quoted 3 lines
tools/lib/bpf/libbpf.c +2 -0
+{
+       struct btf *btf = bpf_find_kernel_btf();
Quoted 7 lines
Quoted 5 lines

ok, it's probably time to do this right. Let's ensure we load kernel BTF just once, keep it inside bpf_object while we need it and then release it after successful load. We are at the point where all the new types of program is loading/releasing kernel BTF for every section and it starts to feel very wasteful.

Sure, will give it a shot in v3.

Since this will be useful for other programs as well, I will send it as a separate patch sooner than the v3 of the LSM patch-set.

- KP

Quoted 276 lines
Quoted 10 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6620

Quoted 8 lines
tools/lib/bpf/libbpf.c +6 -0
+       const struct bpf_sec_def *sec_def;
+       const struct btf_type *hl_type;
+       struct btf_member *m;
+       __u16 vlen;
+       __s32 hl_id;
+       int j;

j without having i used anywhere?...

Fixed.

Quoted 15 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6631

Quoted 12 lines
tools/lib/bpf/libbpf.c +11 -0
+
+       sec_def = find_sec_def(name);
+       if (!sec_def)
+               return -ESRCH;
+
+       name += sec_def->len;
+
+       hl_id = btf__find_by_name_kind(btf, "security_hook_heads",
+                                      BTF_KIND_STRUCT);
+       if (hl_id < 0) {
+               pr_debug("security_hook_heads cannot be found in BTF\n");

"in vmlinux BTF" ?

Quoted 1 line

and it should be pr_warn(), we don't really expect this, right?

Fixed both.

Quoted 13 lines

and it should be (hl_id <= 0) with current btf__find_by_name_kind(), and then return hl_id ? : -ESRCH, which further proves we need to change btf__find_by_name_kind() as I suggested below.

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6638

Quoted 7 lines
tools/lib/bpf/libbpf.c +7 -0
+               return hl_id;
+       }
+
+       hl_type = btf__type_by_id(btf, hl_id);
+       if (!hl_type) {
+               pr_warn("Can't find type for security_hook_heads: %u\n", hl_id);
+               return -EINVAL;
Quoted 1 line

-ESRCH?

Done.

Quoted 9 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6644

Quoted 7 lines
tools/lib/bpf/libbpf.c +6 -0
+       }
+
+       m = btf_members(hl_type);
+       vlen = btf_vlen(hl_type);
+
+       for (j = 0; j < vlen; j++) {

can add succinct , m++ here instead

Done.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6646

Quoted 3 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
+                       return j + 1;

I looked briefly through kernel-side patch introducing lsm_hook_index, but it didn't seem to explain why this index needs to be (unnaturally) 1-based. So asking here first as I'm looking through libbpf changes?

The lsm_hook_idx is one-based as it makes it easy to validate the input. If we make it zero-based it's hard to check if the user intended to attach to the LSM hook at index 0 or did not set it.

We are then up to the verifier to reject the loaded program which may or may not match the signature of the hook at lsm_hook_idx = 0.

I will clarify this in the commit log as well.

Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6650

Quoted 5 lines
tools/lib/bpf/libbpf.c +4 -0
+               m++;
+       }
+
+       pr_warn("Cannot find offset for %s in security_hook_heads\n", name);

it's not offset, rather member index?

Correct, fixed.

Quoted 5 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6651

Quoted 2 lines
tools/lib/bpf/libbpf.c +1 -0
+       return -ENOENT;

not entirely clear about distinction between ENOENT and ESRCH? So far we typically used ESRCH, does ENOENT have more specific semantics?

Updated it to ESRCH, to be consistent with libbpf's convention.

Quoted 76 lines
Quoted 27 lines
tools/lib/bpf/libbpf.c +2 -13
+}
+
 int libbpf_find_vmlinux_btf_id(const char *name,
                               enum bpf_attach_type attach_type)
 {
        struct btf *btf = bpf_find_kernel_btf();
-       char raw_tp_btf[128] = BTF_PREFIX;
-       char *dst = raw_tp_btf + sizeof(BTF_PREFIX) - 1;
-       const char *btf_name;
        int err = -EINVAL;
-       __u32 kind;
 
        if (IS_ERR(btf)) {
                pr_warn("vmlinux BTF is not found\n");
                return -EINVAL;
        }
 
-       if (attach_type == BPF_TRACE_RAW_TP) {
-               /* prepend "btf_trace_" prefix per kernel convention */
-               strncat(dst, name, sizeof(raw_tp_btf) - sizeof(BTF_PREFIX));
-               btf_name = raw_tp_btf;
-               kind = BTF_KIND_TYPEDEF;
-       } else {
-               btf_name = name;
-               kind = BTF_KIND_FUNC;
-       }

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6673

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -1
-       err = btf__find_by_name_kind(btf, btf_name, kind);
+       if (attach_type == BPF_TRACE_RAW_TP)
+               err = btf__find_by_prefix_kind(btf, name, BTF_TRACE_PREFIX,
+                                              BTF_KIND_TYPEDEF);
+       else
+               err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+
+       /* err = 0 means void / UNKNOWN which is treated as an error */
+       if (err == 0)
+               err = -EINVAL;

I think it's actually less error-prone to make btf__find_by_name_kind and btf__find_by_prefix_kind to return -ESRCH when type is not found, instead of a valid type_id 0. I just checked, and struct_ops code already is mishandling it, only checking for <0. Could you make this change and just do a natural <0 check everywhere?

Quoted 31 lines
tools/lib/bpf/libbpf.c +16 -1
+
        btf__free(btf);
        return err;
 }
@@ -6630,7 +6700,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
        }
        err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
        btf__free(btf);
-       if (err <= 0) {
+       if (err < 0) {
                pr_warn("%s is not found in prog's BTF\n", name);
                goto out;
        }
@@ -7395,6 +7465,43 @@ static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
        return bpf_program__attach_trace(prog);
 }
 
+struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
+{
+       char errmsg[STRERR_BUFSIZE];
+       struct bpf_link_fd *link;
+       int prog_fd, pfd;
+
+       prog_fd = bpf_program__fd(prog);
+       if (prog_fd < 0) {
+               pr_warn("program '%s': can't attach before loaded\n",
+                       bpf_program__title(prog, false));
+               return ERR_PTR(-EINVAL);
+       }
+
+       link = calloc(1, sizeof(*link));
Quoted 8 lines

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7487

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+       if (!link)
+               return ERR_PTR(-ENOMEM);
+       link->link.detach = &bpf_link__detach_fd;
+
+       pfd = bpf_prog_attach(prog_fd, 0, BPF_LSM_MAC,
+                             BPF_F_ALLOW_OVERRIDE);

do we want to always specify ALLOW_OVERRIDE? Or should it be an option?

I think this is a relic from the duplicate attachment code. With the anonymous-fd + link which can be destroyed. The way the OVERRIDE should work is:

(quoted diff) +0 -2
-  Destroy the link (detach)
-  And attach.

We don't use the BPF_F_ALLOW_OVERRIDE in the attach logic in the LSM. So I will get rid of that.

Quoted 9 lines

tools/lib/bpf/libbpf.c static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, · line 7493

Quoted 7 lines
tools/lib/bpf/libbpf.c +6 -0
+       if (pfd < 0) {
+               pfd = -errno;
+               pr_warn("program '%s': failed to attach: %s\n",
+                       bpf_program__title(prog, false),
+                       libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
+               return ERR_PTR(pfd);

leaking link here

Fixed.

Quoted 20 lines
Quoted 20 lines
tools/lib/bpf/libbpf.c +11 -0
+       }
+       link->fd = pfd;
+       return (struct bpf_link *)link;
+}
+
+static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
+                                  struct bpf_program *prog)
+{
+       return bpf_program__attach_lsm(prog);
+}
+
 struct bpf_link *bpf_program__attach(struct bpf_program *prog)
 {
        const struct bpf_sec_def *sec_def;
tools/lib/bpf/libbpf.h +0 -0
index 01639f9a1062..a97e709a29e6 100644
@@ -241,6 +241,8 @@ LIBBPF_API struct bpf_link *
Quoted 8 lines

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 245

Quoted 5 lines
tools/lib/bpf/libbpf.h +2 -0
 bpf_program__attach_trace(struct bpf_program *prog);
 struct bpf_map;
 LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_lsm(struct bpf_program *prog);

nit: put it after attach_trace, so that program attaches and map attaches are grouped together, not intermixed

Done.

Quoted 18 lines
Quoted 18 lines
tools/lib/bpf/libbpf.h +2 -0
 struct bpf_insn;
 
 /*
@@ -318,6 +320,7 @@ LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_tracing(struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_struct_ops(struct bpf_program *prog);
+LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog);
 
 LIBBPF_API enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
@@ -339,6 +342,7 @@ LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_perf_event(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_tracing(const struct bpf_program *prog);
 LIBBPF_API bool bpf_program__is_struct_ops(const struct bpf_program *prog);
+LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog);
 
Quoted 15 lines

tools/lib/bpf/libbpf.map LIBBPF_0.0.7 { · line 232

Quoted 13 lines
tools/lib/bpf/libbpf.h +0 -0
 /*
  * No need for __attribute__((packed)), all members of 'bpf_map_def'
tools/lib/bpf/libbpf.map +3 -0
index a19f04e6e3d9..3da0452ce679 100644
@@ -227,4 +227,7 @@ LIBBPF_0.0.7 {
                bpf_program__is_struct_ops;
                bpf_program__set_struct_ops;
                btf__align_of;
+               bpf_program__is_lsm;
+               bpf_program__set_lsm;
+               bpf_program__attach_lsm;

preserve alphabetical order, please

Sure.

Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.map +0 -1
 } LIBBPF_0.0.6;
 
--

2.20.1

#25 lore raw

Re: [PATCH bpf-next v2 08/10] tools/libbpf: Add support for BPF_PROG_TYPE_LSM

In reply to KP Singh
Quoted 23 lines · KP Singh

Thanks for the review Andrii!

I will incorporate the fixes in the next revision.

Quoted 17 lines · Andrii Nakryiko
Quoted 16 lines · KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)

[...]

Quoted 11 lines
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+{
+       struct btf *btf = bpf_find_kernel_btf();

ok, it's probably time to do this right. Let's ensure we load kernel BTF just once, keep it inside bpf_object while we need it and then release it after successful load. We are at the point where all the new types of program is loading/releasing kernel BTF for every section and it starts to feel very wasteful.

Sure, will give it a shot in v3.

thanks!

[...]

Quoted 11 lines
Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6646

Quoted 3 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
+                       return j + 1;

I looked briefly through kernel-side patch introducing lsm_hook_index, but it didn't seem to explain why this index needs to be (unnaturally) 1-based. So asking here first as I'm looking through libbpf changes?

The lsm_hook_idx is one-based as it makes it easy to validate the input. If we make it zero-based it's hard to check if the user intended to attach to the LSM hook at index 0 or did not set it.

Think about providing FDs. 0 is a valid, though rarely intended/correct value. Yet we don't make all FD arguments artificially 1-based, right? This extra +1/-1 translation just makes for more confusing interface, IMO. If user "accidentally" guessed type signature of very first hook, well, so be it... If not, BPF verifier will politely refuse. Seems like enough protection.

Quoted 6 lines

We are then up to the verifier to reject the loaded program which may or may not match the signature of the hook at lsm_hook_idx = 0.

I will clarify this in the commit log as well.

[...]

author
#26 lore raw

Re: [PATCH bpf-next v2 08/10] tools/libbpf: Add support for BPF_PROG_TYPE_LSM

Quoted 55 lines · Andrii Nakryiko
Quoted 23 lines · KP Singh

Thanks for the review Andrii!

I will incorporate the fixes in the next revision.

Quoted 17 lines · Andrii Nakryiko
Quoted 16 lines · KP Singh

From: KP Singh <[email protected]>

* Add functionality in libbpf to attach eBPF program to LSM hooks
* Lookup the index of the LSM hook in security_hook_heads and pass it in attr->lsm_hook_index

---
 tools/lib/bpf/bpf.c      |   6 +-
 tools/lib/bpf/bpf.h      |   1 +
 tools/lib/bpf/libbpf.c   | 143 ++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h   |   4 ++
 tools/lib/bpf/libbpf.map |   3 +
 5 files changed, 138 insertions(+), 19 deletions(-)

[...]

Quoted 11 lines
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+{
+       struct btf *btf = bpf_find_kernel_btf();

ok, it's probably time to do this right. Let's ensure we load kernel BTF just once, keep it inside bpf_object while we need it and then release it after successful load. We are at the point where all the new types of program is loading/releasing kernel BTF for every section and it starts to feel very wasteful.

Sure, will give it a shot in v3.

thanks!

[...]

Quoted 11 lines
Quoted 7 lines

tools/lib/bpf/libbpf.c static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, · line 6646

Quoted 3 lines
tools/lib/bpf/libbpf.c +2 -0
+               if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
+                       return j + 1;

I looked briefly through kernel-side patch introducing lsm_hook_index, but it didn't seem to explain why this index needs to be (unnaturally) 1-based. So asking here first as I'm looking through libbpf changes?

The lsm_hook_idx is one-based as it makes it easy to validate the input. If we make it zero-based it's hard to check if the user intended to attach to the LSM hook at index 0 or did not set it.

Quoted 6 lines

Think about providing FDs. 0 is a valid, though rarely intended/correct value. Yet we don't make all FD arguments artificially 1-based, right? This extra +1/-1 translation just makes for more confusing interface, IMO. If user "accidentally" guessed type signature of very first hook, well, so be it... If not, BPF verifier will politely refuse. Seems like enough protection.

Thanks! I see your point and will update to using the more-conventional 0-based indexing for the next revision.

- KP

Quoted 9 lines
Quoted 7 lines

We are then up to the verifier to reject the loaded program which may or may not match the signature of the hook at lsm_hook_idx = 0.

I will clarify this in the commit log as well.

[...]

author
#27 lore raw

PATCH v2 9/10 bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM

In reply to KP Singh

From: KP Singh <[email protected]>

* Load a BPF program that audits mprotect calls
* Attach the program to the "file_mprotect" LSM hook
* Initialize the perf events buffer and poll for audit events
* Do an mprotect on some memory allocated on the heap
* Verify if the audit event was received

---
 MAINTAINERS                                   |  2 +
 tools/testing/selftests/bpf/lsm_helpers.h     | 19 ++++++
 .../bpf/prog_tests/lsm_mprotect_audit.c       | 58 +++++++++++++++++++
 .../selftests/bpf/progs/lsm_mprotect_audit.c  | 48 +++++++++++++++
 4 files changed, 127 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/lsm_helpers.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c
MAINTAINERS +2 -0
index 02d7e05e9b75..5d553c2e7452 100644
@@ -3210,6 +3210,8 @@ L:	[email protected]
 S:	Maintained
 F:	security/bpf/
 F:	include/linux/bpf_lsm.h
+F:	tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c
+F:	tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c
 
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
tools/testing/selftests/bpf/lsm_helpers.h new file +19 -0
new file mode 100644
index 000000000000..8bad08f77654
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright 2019 Google LLC.
+ */
+#ifndef _LSM_HELPERS_H
+#define _LSM_HELPERS_H
+
+struct lsm_mprotect_audit_result {
+	/* This ensures that the LSM Hook only monitors the PID requested
+	 * by the loader
+	 */
+	__u32 monitored_pid;
+	/* The number of mprotect calls for the monitored PID.
+	 */
+	__u32 mprotect_count;
+};
+
+#endif /* _LSM_HELPERS_H */
tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c new file +58 -0

58 lines not on this page · raw message

tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#28 lore raw

PATCH v2 10/10 bpf: lsm: Add Documentation

In reply to KP Singh

From: KP Singh <[email protected]>

Document how eBPF programs (BPF_PROG_TYPE_LSM) can be loaded and attached (BPF_LSM_MAC) to the LSM hooks.

---
 Documentation/security/bpf.rst   | 150 +++++++++++++++++++++++++++++++
 Documentation/security/index.rst |   1 +
 MAINTAINERS                      |   1 +
 3 files changed, 152 insertions(+)
 create mode 100644 Documentation/security/bpf.rst
Documentation/security/bpf.rst new file +150 -0
new file mode 100644
index 000000000000..4d115c07c370
@@ -0,0 +1,150 @@
+.. SPDX-License-Identifier: GPL-2.0+
+.. Copyright 2019 Google LLC.
+
+==========================
+eBPF Linux Security Module
+==========================
+
+This LSM allows runtime instrumentation of the LSM hooks by privileged users to
+implement system-wide MAC (Mandatory Access Control) and Audit policies using
+eBPF. The LSM is privileged and stackable and requires both ``CAP_SYS_ADMIN``
+and ``CAP_MAC_ADMIN`` for the loading of BPF programs and modification of MAC
+policies respectively.
+
+eBPF Programs
+==============
+
+`eBPF (extended BPF) <https://cilium.readthedocs.io/en/latest/bpf>`_ is a
+virtual machine-like construct in the Linux Kernel allowing the execution of
+verifiable, just-in-time compiled byte code at various points in the Kernel.
+
+The eBPF LSM adds a new type, ``BPF_PROG_TYPE_LSM``, of eBPF programs which
+have the following characteristics:
+
+	* Multiple eBPF programs can be attached to the same LSM hook
+	* The programs are always run after the static hooks (i.e. the ones
+	  registered by SELinux, AppArmor, Smack etc.)
+	* LSM hooks can return an ``-EPERM`` to indicate the decision of the
+	  MAC policy being enforced or simply be used for auditing
+	* If ``CONFIG_SECURITY_BPF_ENFORCE`` is enabled and a non-zero error
+	  code is returned from the BPF program, no further BPF programs for the hook are executed
+	* Allowing the eBPF programs to be attached to all the LSM hooks by
+	  making :doc:`/bpf/btf` type information available for all LSM hooks
+	  and allowing the BPF verifier to perform runtime relocations and
+	  validation on the programs
+
+Structure
+---------
+
+The example shows an eBPF program that can be attached to the ``file_mprotect``
+LSM hook:
+
+.. c:function:: int file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot);
+
+eBPF programs that use :doc:`/bpf/btf` do not need to include kernel headers
+for accessing information from the attached eBPF program's context. They can
+simply declare the structures in the eBPF program and only specify the fields
+that need to be accessed.
+
+.. code-block:: c
+
+	struct mm_struct {
+		unsigned long start_brk, brk, start_stack;
+	} __attribute__((preserve_access_index));
+
+	struct vm_area_struct {
+		unsigned long start_brk, brk, start_stack;
+		unsigned long vm_start, vm_end;
+		struct mm_struct *vm_mm;
+	} __attribute__((preserve_access_index));
+
+
+.. note:: Only the size and the names of the fields must match the type in the
+	  kernel and the order of the fields is irrelevant.
+
+The eBPF programs can be declared using macros similar to the ``BPF_TRACE_<N>``
+macros defined in `tools/testing/selftests/bpf/bpf_trace_helpers.h`_. In this
+example:
+
+	* The LSM hook takes 3 args so we use ``BPF_TRACE_3``
+	* ``"lsm/file_mprotect"`` indicates the LSM hook that the program must
+	  be attached to
+	* ``mprotect_audit`` is the name of the eBPF program
+
+.. code-block:: c
+
+        SEC("lsm/file_mprotect")
+        int BPF_PROG(mprotect_audit, struct vm_area_struct *vma,
+                     unsigned long reqprot, unsigned long prot)
+	{
+		int is_heap;
+
+		is_heap = (vma->vm_start >= vma->vm_mm->start_brk &&
+			   vma->vm_end <= vma->vm_mm->brk);
+
+		/*
+		 * Return an -EPERM or write information to the perf events buffer
+		 * for auditing
+		 */
+	}
+
+The ``__attribute__((preserve_access_index))`` is a clang feature that allows
+the BPF verifier to update the offsets for the access at runtime using the
+:doc:`/bpf/btf` information. Since the BPF verifier is aware of the types, it
+also validates all the accesses made to the various types in the eBPF program.
+
+Loading
+-------
+
+eBPP programs can be loaded with the :manpage:`bpf(2)` syscall's
+``BPF_PROG_LOAD`` operation or more simply by using the the libbpf helper
+``bpf_prog_load_xattr``:
+
+
+.. code-block:: c
+
+	struct bpf_prog_load_attr attr = {
+		.file = "./prog.o",
+	};
+	struct bpf_object *prog_obj;
+	struct bpf_program *prog;
+	int prog_fd;
+
+	bpf_prog_load_xattr(&attr, &prog_obj, &prog_fd);
+
+Attachment to LSM Hooks
+-----------------------
+
+The LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)`
+syscall's ``BPF_PROG_ATTACH`` operation or more simply by
+using the libbpf helper ``bpf_program__attach_lsm``. In the code shown below
+``prog`` is the eBPF program loaded using ``BPF_PROG_LOAD``:
+
+.. code-block:: c
+
+	struct bpf_link *link;
+
+	link = bpf_program__attach_lsm(prog);
+
+The program can be detached from the LSM hook by *destroying* the ``link``
+link returned by ``bpf_program__attach_lsm``:
+
+.. code-block:: c
+
+	link->destroy();
+
+Examples
+--------
+
+An example eBPF program can be found in
+`tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c`_ and the corresponding
+userspace code in
+`tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c`_
+
+.. Links
+.. _tools/testing/selftests/bpf/bpf_trace_helpers.h:
+   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/selftests/bpf/bpf_trace_helpers.h
+.. _tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c:
+   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c
+.. _tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c:
+   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c
Documentation/security/index.rst +1 -0
index fc503dd689a7..844463df4547 100644
@@ -5,6 +5,7 @@ Security Documentation
 .. toctree::
    :maxdepth: 1
 
+   bpf
    credentials
    IMA-templates
    keys/index
MAINTAINERS +1 -0
index 5d553c2e7452..dd4c4ee151b0 100644
@@ -3212,6 +3212,7 @@ F:	security/bpf/
 F:	include/linux/bpf_lsm.h
 F:	tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c
 F:	tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c
+F:	Documentation/security/bpf.rst
 
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
Signature · 1 line
2.20.1
#29 lore raw
In reply to KP Singh
Quoted 84 lines · KP Singh

From: KP Singh <[email protected]>

# Changes since v1 (https://lore.kernel.org/bpf/[email protected]/):

* Eliminate the requirement to maintain LSM hooks separately in security/bpf/hooks.h Use BPF trampolines to dynamically allocate security hooks
* Drop the use of securityfs as bpftool provides the required introspection capabilities. Update the tests to use the bpf_skeleton and global variables
* Use O_CLOEXEC anonymous fds to represent BPF attachment in line with the other BPF programs with the possibility to use bpf program pinning in the future to provide "permanent attachment".
* Drop the logic based on prog names for handling re-attachment.
* Drop bpf_lsm_event_output from this series and send it as a separate patch.

# Motivation

Google does analysis of rich runtime security data to detect and thwart threats in real-time. Currently, this is done in custom kernel modules but we would like to replace this with something that's upstream and useful to others.

The current kernel infrastructure for providing telemetry (Audit, Perf etc.) is disjoint from access enforcement (i.e. LSMs). Augmenting the information provided by audit requires kernel changes to audit, its policy language and user-space components. Furthermore, building a MAC policy based on the newly added telemetry data requires changes to various LSMs and their respective policy languages.

This patchset proposes a new stackable and privileged LSM which allows the LSM hooks to be implemented using eBPF. This facilitates a unified and dynamic (not requiring re-compilation of the kernel) audit and MAC policy.

# Why an LSM?

Linux Security Modules target security behaviours rather than the kernel's API. For example, it's easy to miss out a newly added system call for executing processes (eg. execve, execveat etc.) but the LSM framework ensures that all process executions trigger the relevant hooks irrespective of how the process was executed.

Allowing users to implement LSM hooks at runtime also benefits the LSM eco-system by enabling a quick feedback loop from the security community about the kind of behaviours that the LSM Framework should be targeting.

# How does it work?

The LSM introduces a new eBPF (https://docs.cilium.io/en/v1.6/bpf/)
program type BPF_PROG_TYPE_LSM which can only be attached to LSM hooks. Attachment requires CAP_SYS_ADMIN for loading eBPF programs and CAP_MAC_ADMIN for modifying MAC policies.

The eBPF programs are attached to a separate security_hook_heads maintained by the BPF LSM for mutable hooks and executed after all the statically defined hooks (i.e. the ones declared by SELinux, AppArmor, Smack etc). This also ensures that statically defined LSM hooks retain the behaviour of "being read-only after init", i.e. __lsm_ro_after_init.

Upon attachment, a security hook is dynamically allocated with arch_bpf_prepare_trampoline which generates code to handle the conversion from the signature of the hook to the BPF context and allows the JIT'ed BPF program to be called as a C function with the same arguments as the LSM hooks. If any of the attached eBPF programs returns an error (like ENOPERM), the behaviour represented by the hook is denied.

Audit logs can be written using a format chosen by the eBPF program to the perf events buffer or to global eBPF variables or maps and can be further processed in user-space.

# BTF Based Design

The current design uses BTF
(https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html,
https://lwn.net/Articles/803258/) which allows verifiable read-only structure accesses by field names rather than fixed offsets. This allows accessing the hook parameters using a dynamically created context which provides a certain degree of ABI stability:

Quoted 6 lines

// Only declare the structure and fields intended to be used // in the program

struct vm_area_struct {
        unsigned long vm_start;
} __attribute__((preserve_access_index));

It would be nice to also mention that you don't even have to "re-define" these structs if you use vmlinux.h generated with bpftool btf dump file <path-to-vm-linux-or-/sys/kernel/btf/vmlinux> format c. Its output will contain all types of the kernel, including internal ones not exposed through any public headers. And it will also automatically have __attribute__((preserve_access_index)) applied to all structs/unions. It can be pre-generated and checked in somewhere along the application or generated on the fly, if environment and use case allows.

Quoted 30 lines

// Declare the eBPF program mprotect_audit which attaches to // to the file_mprotect LSM hook and accepts three arguments. SEC("lsm/file_mprotect")
int BPF_PROG(mprotect_audit, struct vm_area_struct *vma,
unsigned long reqprot, unsigned long prot)

{
        unsigned long vm_start = vma->vm_start;

        return 0;
}

By relocating field offsets, BTF makes a large portion of kernel data structures readily accessible across kernel versions without requiring a large corpus of BPF helper functions and requiring recompilation with every kernel version. The BTF type information is also used by the BPF verifier to validate memory accesses within the BPF program and also prevents arbitrary writes to the kernel memory.

The limitations of BTF compatibility are described in BPF Co-Re
(http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf, i.e. field renames, #defines and changes to the signature of LSM hooks).

This design imposes that the MAC policy (eBPF programs) be updated when the inspected kernel structures change outside of BTF compatibility guarantees. In practice, this is only required when a structure field used by a current policy is removed (or renamed) or when the used LSM hooks change. We expect the maintenance cost of these changes to be acceptable as compared to the previous design
(https://lore.kernel.org/bpf/[email protected]/).

[...]

author
#30 lore raw
Quoted 92 lines · Andrii Nakryiko
Quoted 84 lines · KP Singh

From: KP Singh <[email protected]>

# Changes since v1 (https://lore.kernel.org/bpf/[email protected]/):

* Eliminate the requirement to maintain LSM hooks separately in security/bpf/hooks.h Use BPF trampolines to dynamically allocate security hooks
* Drop the use of securityfs as bpftool provides the required introspection capabilities. Update the tests to use the bpf_skeleton and global variables
* Use O_CLOEXEC anonymous fds to represent BPF attachment in line with the other BPF programs with the possibility to use bpf program pinning in the future to provide "permanent attachment".
* Drop the logic based on prog names for handling re-attachment.
* Drop bpf_lsm_event_output from this series and send it as a separate patch.

# Motivation

Google does analysis of rich runtime security data to detect and thwart threats in real-time. Currently, this is done in custom kernel modules but we would like to replace this with something that's upstream and useful to others.

The current kernel infrastructure for providing telemetry (Audit, Perf etc.) is disjoint from access enforcement (i.e. LSMs). Augmenting the information provided by audit requires kernel changes to audit, its policy language and user-space components. Furthermore, building a MAC policy based on the newly added telemetry data requires changes to various LSMs and their respective policy languages.

This patchset proposes a new stackable and privileged LSM which allows the LSM hooks to be implemented using eBPF. This facilitates a unified and dynamic (not requiring re-compilation of the kernel) audit and MAC policy.

# Why an LSM?

Linux Security Modules target security behaviours rather than the kernel's API. For example, it's easy to miss out a newly added system call for executing processes (eg. execve, execveat etc.) but the LSM framework ensures that all process executions trigger the relevant hooks irrespective of how the process was executed.

Allowing users to implement LSM hooks at runtime also benefits the LSM eco-system by enabling a quick feedback loop from the security community about the kind of behaviours that the LSM Framework should be targeting.

# How does it work?

The LSM introduces a new eBPF (https://docs.cilium.io/en/v1.6/bpf/)
program type BPF_PROG_TYPE_LSM which can only be attached to LSM hooks. Attachment requires CAP_SYS_ADMIN for loading eBPF programs and CAP_MAC_ADMIN for modifying MAC policies.

The eBPF programs are attached to a separate security_hook_heads maintained by the BPF LSM for mutable hooks and executed after all the statically defined hooks (i.e. the ones declared by SELinux, AppArmor, Smack etc). This also ensures that statically defined LSM hooks retain the behaviour of "being read-only after init", i.e. __lsm_ro_after_init.

Upon attachment, a security hook is dynamically allocated with arch_bpf_prepare_trampoline which generates code to handle the conversion from the signature of the hook to the BPF context and allows the JIT'ed BPF program to be called as a C function with the same arguments as the LSM hooks. If any of the attached eBPF programs returns an error (like ENOPERM), the behaviour represented by the hook is denied.

Audit logs can be written using a format chosen by the eBPF program to the perf events buffer or to global eBPF variables or maps and can be further processed in user-space.

# BTF Based Design

The current design uses BTF
(https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html,
https://lwn.net/Articles/803258/) which allows verifiable read-only structure accesses by field names rather than fixed offsets. This allows accessing the hook parameters using a dynamically created context which provides a certain degree of ABI stability:

Quoted 6 lines

// Only declare the structure and fields intended to be used // in the program

struct vm_area_struct {
        unsigned long vm_start;
} __attribute__((preserve_access_index));
Quoted 9 lines

It would be nice to also mention that you don't even have to "re-define" these structs if you use vmlinux.h generated with bpftool btf dump file <path-to-vm-linux-or-/sys/kernel/btf/vmlinux> format c. Its output will contain all types of the kernel, including internal ones not exposed through any public headers. And it will also automatically have __attribute__((preserve_access_index)) applied to all structs/unions. It can be pre-generated and checked in somewhere along the application or generated on the fly, if environment and use case allows.

Cool, I will update the documentation to mention this. Thanks!

- KP

Quoted 33 lines
Quoted 31 lines

// Declare the eBPF program mprotect_audit which attaches to // to the file_mprotect LSM hook and accepts three arguments. SEC("lsm/file_mprotect")
int BPF_PROG(mprotect_audit, struct vm_area_struct *vma,
unsigned long reqprot, unsigned long prot)

{
        unsigned long vm_start = vma->vm_start;

        return 0;
}

By relocating field offsets, BTF makes a large portion of kernel data structures readily accessible across kernel versions without requiring a large corpus of BPF helper functions and requiring recompilation with every kernel version. The BTF type information is also used by the BPF verifier to validate memory accesses within the BPF program and also prevents arbitrary writes to the kernel memory.

The limitations of BTF compatibility are described in BPF Co-Re
(http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf, i.e. field renames, #defines and changes to the signature of LSM hooks).

This design imposes that the MAC policy (eBPF programs) be updated when the inspected kernel structures change outside of BTF compatibility guarantees. In practice, this is only required when a structure field used by a current policy is removed (or renamed) or when the used LSM hooks change. We expect the maintenance cost of these changes to be acceptable as compared to the previous design
(https://lore.kernel.org/bpf/[email protected]/).

[...]

Reviewed-by on the v2 series
#31 lore raw
In reply to KP Singh
Quoted 1 line · KP Singh

From: KP Singh <[email protected]>

[...]

Quoted 11 lines

KP Singh (10):
bpf: btf: Make some of the API visible outside BTF
bpf: lsm: Add a skeleton and config options
bpf: lsm: Introduce types for eBPF based LSM
bpf: lsm: Add mutable hooks list for the BPF LSM
bpf: lsm: BTF API for LSM hooks
bpf: lsm: Implement attach, detach and execution
bpf: lsm: Make the allocated callback RO+X tools/libbpf: Add support for BPF_PROG_TYPE_LSM
bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
bpf: lsm: Add Documentation

Just to note internal review cycles at Google; For the whole set: