MAC and Audit policy using eBPF (KRSI)
Subsystems (10)
v8 cover MAC and Audit policy using eBPF (KRSI)
From: KP Singh <[email protected]>
# v7 -> v8
https://lore.kernel.org/bpf/[email protected]/
* Removed CAP_MAC_ADMIN check from bpf_lsm_verify_prog. LSMs can add it in their own bpf_prog hook. This can be revisited as a separate patch.
* Added Andrii and James' Ack/Review tags.
* Fixed an indentation issue and missing newlines in selftest error a cases.
* Updated a comment as suggested by Alexei.
* Updated the documentation to use the newer libbpf API and some other fixes.
* Rebase
# v6 -> v7
https://lore.kernel.org/bpf/[email protected]/
* Removed __weak from the LSM attachment nops per Kees' suggestion. Will send a separate patch (if needed) to update the noinline definition in include/linux/compiler_attributes.h.
* waitpid to wait specifically for the forked child in selftests.
* Comment format fixes in security/... as suggested by Casey.
* Added Acks from Kees and Andrii and Casey's Reviewed-by: tags to the respective patches.
* Rebase
# v5 -> v6
https://lore.kernel.org/bpf/[email protected]/
* Updated LSM_HOOK macro to define a default value and cleaned up the BPF LSM hook declarations.
* Added Yonghong's Acks and Kees' Reviewed-by tags.
* Simplification of the selftest code.
* Rebase and fixes suggested by Andrii and Yonghong and some other minor fixes noticed in internal review.
# v4 -> v5
https://lore.kernel.org/bpf/[email protected]/
* Removed static keys and special casing of BPF calls from the LSM framework.
* Initialized the BPF callbacks (nops) as proper LSM hooks.
* Updated to using the newly introduced BPF_TRAMP_MODIFY_RETURN trampolines in https://lkml.org/lkml/2020/3/4/877
* Addressed Andrii's feedback and rebased.
# v3 -> v4
* Moved away from allocating a separate security_hook_heads and adding a new special case for arch_prepare_bpf_trampoline to using BPF fexit trampolines called from the right place in the LSM hook and toggled by static keys based on the discussion in:
https://lore.kernel.org/bpf/CAG48ez25mW+_oCxgCtbiGMX07g_ph79UOJa07h=o_6B6+Q-u5g@mail.gmail.com/
* Since the code does not deal with security_hook_heads anymore, it goes from "being a BPF LSM" to "BPF program attachment to LSM hooks".
* Added a new test case which ensures that the BPF programs' return value is reflected by the LSM hook.
# v2 -> v3 does not change the overall design and has some minor fixes:
* LSM_ORDER_LAST is introduced to represent the behaviour of the BPF LSM
* Fixed the inadvertent clobbering of the LSM Hook error codes
* Added GPL license requirement to the commit log
* The lsm_hook_idx is now the more conventional 0-based index
* Some changes were split into a separate patch ("Load btf_vmlinux only once per object")
https://lore.kernel.org/bpf/[email protected]/
* Addressed Andrii's feedback on the BTF implementation
* Documentation update for using generated vmlinux.h to simplify programs
* Rebase
# 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 allows BPF programs to be attached to LSM hooks 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 patchset 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. Loading and attachment of BPF programs requires CAP_SYS_ADMIN.
The new LSM registers nop functions (bpf_lsm_<hook_name>) as LSM hook callbacks. Their purpose is to provide a definite point where BPF programs can be attached as BPF_TRAMP_MODIFY_RETURN trampoline programs for hooks that return an int, and BPF_TRAMP_FEXIT trampoline programs for void LSM hooks.
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, int ret)
{
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 design presented in the RFC.
(https://lore.kernel.org/bpf/[email protected]/).
# 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 (8):
bpf: Introduce BPF_PROG_TYPE_LSM
security: Refactor declaration of LSM hooks
bpf: lsm: provide attachment points for BPF LSM programs
bpf: lsm: Implement attach, detach and execution
bpf: lsm: Initialize the BPF LSM hooks tools/libbpf: Add support for BPF_PROG_TYPE_LSM
bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
bpf: lsm: Add Documentation
Documentation/bpf/bpf_lsm.rst | 146 ++++ Documentation/bpf/index.rst | 1 + MAINTAINERS | 1 + include/linux/bpf.h | 3 + include/linux/bpf_lsm.h | 33 + include/linux/bpf_types.h | 4 + include/linux/lsm_hook_defs.h | 381 +++++++++++ include/linux/lsm_hooks.h | 628 +----------------- include/uapi/linux/bpf.h | 2 + init/Kconfig | 12 + kernel/bpf/Makefile | 1 + kernel/bpf/bpf_lsm.c | 59 ++ kernel/bpf/btf.c | 9 +- kernel/bpf/syscall.c | 57 +- kernel/bpf/trampoline.c | 17 +- kernel/bpf/verifier.c | 19 +- kernel/trace/bpf_trace.c | 12 +- security/Kconfig | 10 +- security/Makefile | 2 + security/bpf/Makefile | 5 + security/bpf/hooks.c | 26 + security/security.c | 41 +- tools/include/uapi/linux/bpf.h | 2 + tools/lib/bpf/bpf.c | 3 +- tools/lib/bpf/libbpf.c | 39 +- tools/lib/bpf/libbpf.h | 4 + tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/libbpf_probes.c | 1 + tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/test_lsm.c | 86 +++ tools/testing/selftests/bpf/progs/lsm.c | 48 ++ 31 files changed, 987 insertions(+), 670 deletions(-) create mode 100644 Documentation/bpf/bpf_lsm.rst create mode 100644 include/linux/bpf_lsm.h create mode 100644 include/linux/lsm_hook_defs.h create mode 100644 kernel/bpf/bpf_lsm.c create mode 100644 security/bpf/Makefile create mode 100644 security/bpf/hooks.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_lsm.c create mode 100644 tools/testing/selftests/bpf/progs/lsm.c
Signature · 1 line
2.20.1
PATCH v8 1/8 bpf: Introduce BPF_PROG_TYPE_LSM
From: KP Singh <[email protected]>
Introduce types and configs for bpf programs that can be attached to LSM hooks. The programs can be enabled by the config option CONFIG_BPF_LSM.
--- MAINTAINERS | 1 + include/linux/bpf.h | 3 +++ include/linux/bpf_types.h | 4 ++++ include/uapi/linux/bpf.h | 2 ++ init/Kconfig | 12 ++++++++++++ kernel/bpf/Makefile | 1 + kernel/bpf/bpf_lsm.c | 17 +++++++++++++++++ kernel/trace/bpf_trace.c | 12 ++++++------ tools/include/uapi/linux/bpf.h | 2 ++ tools/lib/bpf/libbpf_probes.c | 1 + 10 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 kernel/bpf/bpf_lsm.c
MAINTAINERS +1 -0
@@ -3147,6 +3147,7 @@ R: Martin KaFai Lau <[email protected]> R: Song Liu <[email protected]> R: Yonghong Song <[email protected]> R: Andrii Nakryiko <[email protected]> +R: KP Singh <[email protected]> L: [email protected] L: [email protected] T: git git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git
include/linux/bpf.h +3 -0
@@ -1513,6 +1513,9 @@ extern const struct bpf_func_proto bpf_tcp_sock_proto;extern const struct bpf_func_proto bpf_jiffies64_proto;extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto; +const struct bpf_func_proto *bpf_tracing_func_proto ( +enum bpf_func_id func_id,const struct bpf_prog *prog); +/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );u64 bpf_user_rnd_u32 (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5);
include/linux/bpf_types.h +4 -0
@@ -70,6 +70,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_STRUCT_OPS, bpf_struct_ops, void *, void *)BPF_PROG_TYPE (BPF_PROG_TYPE_EXT ,bpf_extension ,void *,void *) +#ifdef CONFIG_BPF_LSM +BPF_PROG_TYPE (BPF_PROG_TYPE_LSM , lsm, + void *, void *) +#endif /* CONFIG_BPF_LSM */ #endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
include/uapi/linux/bpf.h +2 -0
@@ -181,6 +181,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_TRACING ,BPF_PROG_TYPE_STRUCT_OPS ,BPF_PROG_TYPE_EXT , + BPF_PROG_TYPE_LSM, }; enum bpf_attach_type {
@@ -211,6 +212,7 @@ enum bpf_attach_type {BPF_TRACE_FENTRY ,BPF_TRACE_FEXIT ,BPF_MODIFY_RETURN , +BPF_LSM_MAC , __MAX_BPF_ATTACH_TYPE };
init/Kconfig +12 -0
@@ -1616,6 +1616,18 @@ config KALLSYMS_BASE_RELATIVE # end of the "standard kernel features (expert users)" menu # syscall, maps, verifier + +config BPF_LSM + bool "LSM Instrumentation with BPF" + depends on BPF_SYSCALL + depends on SECURITY + depends on BPF_JIT + help + Enables instrumentation of the security hooks with eBPF programs for + implementing dynamic MAC and Audit Policies. + + If you are unsure how to answer this question, answer N. + config BPF_SYSCALL bool "Enable bpf() system call" select BPF
kernel/bpf/Makefile +1 -0
@@ -29,4 +29,5 @@ obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o endif ifeq ($(CONFIG_BPF_JIT),y) obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o +obj-${CONFIG_BPF_LSM} += bpf_lsm.o endif
kernel/bpf/bpf_lsm.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2020 Google LLC. +*/ + +#include <linux/filter.h> +#include <linux/bpf.h> +#include <linux/btf.h> + +const struct bpf_prog_ops lsm_prog_ops = { +}; + +const struct bpf_verifier_ops lsm_verifier_ops = { + .get_func_proto = bpf_tracing_func_proto, + .is_valid_access = btf_ctx_access, +};
kernel/trace/bpf_trace.c +6 -6
@@ -779,8 +779,8 @@ static const struct bpf_func_proto bpf_send_signal_thread_proto = { .arg1_type = ARG_ANYTHING, }; -static const struct bpf_func_proto * -tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) +const struct bpf_func_proto * +bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {switch (func_id) {case BPF_FUNC_map_lookup_elem:
@@ -865,7 +865,7 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_override_return_proto;#endif default : -return tracing_func_proto (func_id, prog); +return bpf_tracing_func_proto (func_id, prog); } }
@@ -975,7 +975,7 @@ tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_stack:return &bpf_get_stack_proto_tp;default : -return tracing_func_proto (func_id, prog); +return bpf_tracing_func_proto (func_id, prog); } }
@@ -1082,7 +1082,7 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_read_branch_records:return &bpf_read_branch_records_proto;default : -return tracing_func_proto (func_id, prog); +return bpf_tracing_func_proto (func_id, prog); } }
@@ -1210,7 +1210,7 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_stack:return &bpf_get_stack_proto_raw_tp;default : -return tracing_func_proto (func_id, prog); +return bpf_tracing_func_proto (func_id, prog); } }
tools/include/uapi/linux/bpf.h +2 -0
@@ -181,6 +181,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_TRACING ,BPF_PROG_TYPE_STRUCT_OPS ,BPF_PROG_TYPE_EXT , + BPF_PROG_TYPE_LSM, }; enum bpf_attach_type {
@@ -211,6 +212,7 @@ enum bpf_attach_type {BPF_TRACE_FENTRY ,BPF_TRACE_FEXIT ,BPF_MODIFY_RETURN , +BPF_LSM_MAC , __MAX_BPF_ATTACH_TYPE };
tools/lib/bpf/libbpf_probes.c +1 -0
@@ -108,6 +108,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,case BPF_PROG_TYPE_TRACING :case BPF_PROG_TYPE_STRUCT_OPS :case BPF_PROG_TYPE_EXT : +case BPF_PROG_TYPE_LSM :default :break ; }
Signature · 1 line
2.20.1
PATCH v8 2/8 security: Refactor declaration of LSM hooks
In reply to KP SinghFrom: KP Singh <[email protected]>
The information about the different types of LSM hooks is scattered in two locations i.e. union security_list_options and struct security_hook_heads. Rather than duplicating this information even further for BPF_PROG_TYPE_LSM, define all the hooks with the LSM_HOOK macro in lsm_hook_defs.h which is then used to generate all the data structures required by the LSM framework.
The LSM hooks are defined as:
LSM_HOOK(<return_type>, <default_value>, <hook_name>, args...)
with <default_value> acccessible in security.c as:
LSM_RET_DEFAULT(<hook_name>)
--- include/linux/lsm_hook_defs.h | 381 +++++++++++++++++++++ include/linux/lsm_hooks.h | 628 +--------------------------------- security/security.c | 41 ++- 3 files changed, 423 insertions(+), 627 deletions(-) create mode 100644 include/linux/lsm_hook_defs.h
include/linux/lsm_hook_defs.h new file +381 -0
@@ -0,0 +1,381 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Linux Security Module Hook declarations. +* +* Copyright (C) 2001 WireX Communications, Inc <[email protected]> +* Copyright (C) 2001 Greg Kroah-Hartman <[email protected]> +* Copyright (C) 2001 Networks Associates Technology, Inc <[email protected]> +* Copyright (C) 2001 James Morris <[email protected]> +* Copyright (C) 2001 Silicon Graphics, Inc. (Trust Technology Group) +* Copyright (C) 2015 Intel Corporation. +* Copyright (C) 2015 Casey Schaufler <[email protected]> +* Copyright (C) 2016 Mellanox Techonologies +* Copyright (C) 2020 Google LLC. +*/ + +/* +* The macro LSM_HOOK is used to define the data structures required by the +* the LSM framework using the pattern: +* +* LSM_HOOK(<return_type>, <default_value>, <hook_name>, args...) +* +* struct security_hook_heads { +* #define LSM_HOOK(RET, DEFAULT, NAME, ...) struct hlist_head NAME; +* #include <linux/lsm_hook_defs.h> +* #undef LSM_HOOK +* }; +*/ +LSM_HOOK(int, 0, binder_set_context_mgr, struct task_struct *mgr) +LSM_HOOK(int, 0, binder_transaction, struct task_struct *from, + struct task_struct *to) +LSM_HOOK(int, 0, binder_transfer_binder, struct task_struct *from, + struct task_struct *to) +LSM_HOOK(int, 0, binder_transfer_file, struct task_struct *from, + struct task_struct *to, struct file *file) +LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child, + unsigned int mode) +LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent) +LSM_HOOK(int, 0, capget, struct task_struct *target, kernel_cap_t *effective, + kernel_cap_t *inheritable, kernel_cap_t *permitted) +LSM_HOOK(int, 0, capset, struct cred *new, const struct cred *old, + const kernel_cap_t *effective, const kernel_cap_t *inheritable, + const kernel_cap_t *permitted) +LSM_HOOK(int, 0, capable, const struct cred *cred, struct user_namespace *ns, + int cap, unsigned int opts) +LSM_HOOK(int, 0, quotactl, int cmds, int type, int id, struct super_block *sb) +LSM_HOOK(int, 0, quota_on, struct dentry *dentry) +LSM_HOOK(int, 0, syslog, int type) +LSM_HOOK(int, 0, settime, const struct timespec64 *ts, + const struct timezone *tz) +LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages) +LSM_HOOK(int, 0, bprm_set_creds, struct linux_binprm *bprm) +LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm) +LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm) +LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm) +LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc, + struct fs_context *src_sc) +LSM_HOOK(int, 0, fs_context_parse_param, struct fs_context *fc, + struct fs_parameter *param) +LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb) +LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb) +LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts) +LSM_HOOK(int, 0, sb_eat_lsm_opts, char *orig, void **mnt_opts) +LSM_HOOK(int, 0, sb_remount, struct super_block *sb, void *mnt_opts) +LSM_HOOK(int, 0, sb_kern_mount, struct super_block *sb) +LSM_HOOK(int, 0, sb_show_options, struct seq_file *m, struct super_block *sb) +LSM_HOOK(int, 0, sb_statfs, struct dentry *dentry) +LSM_HOOK(int, 0, sb_mount, const char *dev_name, const struct path *path, + const char *type, unsigned long flags, void *data) +LSM_HOOK(int, 0, sb_umount, struct vfsmount *mnt, int flags) +LSM_HOOK(int, 0, sb_pivotroot, const struct path *old_path, + const struct path *new_path) +LSM_HOOK(int, 0, sb_set_mnt_opts, struct super_block *sb, void *mnt_opts, + unsigned long kern_flags, unsigned long *set_kern_flags) +LSM_HOOK(int, 0, sb_clone_mnt_opts, const struct super_block *oldsb, + struct super_block *newsb, unsigned long kern_flags, + unsigned long *set_kern_flags) +LSM_HOOK(int, 0, sb_add_mnt_opt, const char *option, const char *val, + int len, void **mnt_opts) +LSM_HOOK(int, 0, move_mount, const struct path *from_path, + const struct path *to_path) +LSM_HOOK(int, 0, dentry_init_security, struct dentry *dentry, + int mode, const struct qstr *name, void **ctx, u32 *ctxlen) +LSM_HOOK(int, 0, dentry_create_files_as, struct dentry *dentry, int mode, + struct qstr *name, const struct cred *old, struct cred *new) + +#ifdef CONFIG_SECURITY_PATH +LSM_HOOK(int, 0, path_unlink, const struct path *dir, struct dentry *dentry) +LSM_HOOK(int, 0, path_mkdir, const struct path *dir, struct dentry *dentry, + umode_t mode) +LSM_HOOK(int, 0, path_rmdir, const struct path *dir, struct dentry *dentry) +LSM_HOOK(int, 0, path_mknod, const struct path *dir, struct dentry *dentry, + umode_t mode, unsigned int dev) +LSM_HOOK(int, 0, path_truncate, const struct path *path) +LSM_HOOK(int, 0, path_symlink, const struct path *dir, struct dentry *dentry, + const char *old_name) +LSM_HOOK(int, 0, path_link, struct dentry *old_dentry, + const struct path *new_dir, struct dentry *new_dentry) +LSM_HOOK(int, 0, path_rename, const struct path *old_dir, + struct dentry *old_dentry, const struct path *new_dir, + struct dentry *new_dentry) +LSM_HOOK(int, 0, path_chmod, const struct path *path, umode_t mode) +LSM_HOOK(int, 0, path_chown, const struct path *path, kuid_t uid, kgid_t gid) +LSM_HOOK(int, 0, path_chroot, const struct path *path) +#endif /* CONFIG_SECURITY_PATH */ + +/* Needed for inode based security check */ +LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask, + unsigned int obj_type) +LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode) +LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode) +LSM_HOOK(int, 0, inode_init_security, struct inode *inode, + struct inode *dir, const struct qstr *qstr, const char **name, + void **value, size_t *len) +LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry, + umode_t mode) +LSM_HOOK(int, 0, inode_link, struct dentry *old_dentry, struct inode *dir, + struct dentry *new_dentry) +LSM_HOOK(int, 0, inode_unlink, struct inode *dir, struct dentry *dentry) +LSM_HOOK(int, 0, inode_symlink, struct inode *dir, struct dentry *dentry, + const char *old_name) +LSM_HOOK(int, 0, inode_mkdir, struct inode *dir, struct dentry *dentry, + umode_t mode) +LSM_HOOK(int, 0, inode_rmdir, struct inode *dir, struct dentry *dentry) +LSM_HOOK(int, 0, inode_mknod, struct inode *dir, struct dentry *dentry, + umode_t mode, dev_t dev) +LSM_HOOK(int, 0, inode_rename, struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry) +LSM_HOOK(int, 0, inode_readlink, struct dentry *dentry) +LSM_HOOK(int, 0, inode_follow_link, struct dentry *dentry, struct inode *inode, + bool rcu) +LSM_HOOK(int, 0, inode_permission, struct inode *inode, int mask) +LSM_HOOK(int, 0, inode_setattr, struct dentry *dentry, struct iattr *attr) +LSM_HOOK(int, 0, inode_getattr, const struct path *path) +LSM_HOOK(int, 0, inode_setxattr, struct dentry *dentry, const char *name, + const void *value, size_t size, int flags) +LSM_HOOK(void, LSM_RET_VOID, inode_post_setxattr, struct dentry *dentry, + const char *name, const void *value, size_t size, int flags) +LSM_HOOK(int, 0, inode_getxattr, struct dentry *dentry, const char *name) +LSM_HOOK(int, 0, inode_listxattr, struct dentry *dentry) +LSM_HOOK(int, 0, inode_removexattr, struct dentry *dentry, const char *name) +LSM_HOOK(int, 0, inode_need_killpriv, struct dentry *dentry) +LSM_HOOK(int, 0, inode_killpriv, struct dentry *dentry) +LSM_HOOK(int, -EOPNOTSUPP, inode_getsecurity, struct inode *inode, + const char *name, void **buffer, bool alloc) +LSM_HOOK(int, -EOPNOTSUPP, inode_setsecurity, struct inode *inode, + const char *name, const void *value, size_t size, int flags) +LSM_HOOK(int, 0, inode_listsecurity, struct inode *inode, char *buffer, + size_t buffer_size) +LSM_HOOK(void, LSM_RET_VOID, inode_getsecid, struct inode *inode, u32 *secid) +LSM_HOOK(int, 0, inode_copy_up, struct dentry *src, struct cred **new) +LSM_HOOK(int, 0, inode_copy_up_xattr, const char *name) +LSM_HOOK(int, 0, kernfs_init_security, struct kernfs_node *kn_dir, + struct kernfs_node *kn) +LSM_HOOK(int, 0, file_permission, struct file *file, int mask) +LSM_HOOK(int, 0, file_alloc_security, struct file *file) +LSM_HOOK(void, LSM_RET_VOID, file_free_security, struct file *file) +LSM_HOOK(int, 0, file_ioctl, struct file *file, unsigned int cmd, + unsigned long arg) +LSM_HOOK(int, 0, mmap_addr, unsigned long addr) +LSM_HOOK(int, 0, mmap_file, struct file *file, unsigned long reqprot, + unsigned long prot, unsigned long flags) +LSM_HOOK(int, 0, file_mprotect, struct vm_area_struct *vma, + unsigned long reqprot, unsigned long prot) +LSM_HOOK(int, 0, file_lock, struct file *file, unsigned int cmd) +LSM_HOOK(int, 0, file_fcntl, struct file *file, unsigned int cmd, + unsigned long arg) +LSM_HOOK(void, LSM_RET_VOID, file_set_fowner, struct file *file) +LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk, + struct fown_struct *fown, int sig) +LSM_HOOK(int, 0, file_receive, struct file *file) +LSM_HOOK(int, 0, file_open, struct file *file) +LSM_HOOK(int, 0, task_alloc, struct task_struct *task, + unsigned long clone_flags) +LSM_HOOK(void, LSM_RET_VOID, task_free, struct task_struct *task) +LSM_HOOK(int, 0, cred_alloc_blank, struct cred *cred, gfp_t gfp) +LSM_HOOK(void, LSM_RET_VOID, cred_free, struct cred *cred) +LSM_HOOK(int, 0, cred_prepare, struct cred *new, const struct cred *old, + gfp_t gfp) +LSM_HOOK(void, LSM_RET_VOID, cred_transfer, struct cred *new, + const struct cred *old) +LSM_HOOK(void, LSM_RET_VOID, cred_getsecid, const struct cred *c, u32 *secid) +LSM_HOOK(int, 0, kernel_act_as, struct cred *new, u32 secid) +LSM_HOOK(int, 0, kernel_create_files_as, struct cred *new, struct inode *inode) +LSM_HOOK(int, 0, kernel_module_request, char *kmod_name) +LSM_HOOK(int, 0, kernel_load_data, enum kernel_load_data_id id) +LSM_HOOK(int, 0, kernel_read_file, struct file *file, + enum kernel_read_file_id id) +LSM_HOOK(int, 0, kernel_post_read_file, struct file *file, char *buf, + loff_t size, enum kernel_read_file_id id) +LSM_HOOK(int, 0, task_fix_setuid, struct cred *new, const struct cred *old, + int flags) +LSM_HOOK(int, 0, task_setpgid, struct task_struct *p, pid_t pgid) +LSM_HOOK(int, 0, task_getpgid, struct task_struct *p) +LSM_HOOK(int, 0, task_getsid, struct task_struct *p) +LSM_HOOK(void, LSM_RET_VOID, task_getsecid, struct task_struct *p, u32 *secid) +LSM_HOOK(int, 0, task_setnice, struct task_struct *p, int nice) +LSM_HOOK(int, 0, task_setioprio, struct task_struct *p, int ioprio) +LSM_HOOK(int, 0, task_getioprio, struct task_struct *p) +LSM_HOOK(int, 0, task_prlimit, const struct cred *cred, + const struct cred *tcred, unsigned int flags) +LSM_HOOK(int, 0, task_setrlimit, struct task_struct *p, unsigned int resource, + struct rlimit *new_rlim) +LSM_HOOK(int, 0, task_setscheduler, struct task_struct *p) +LSM_HOOK(int, 0, task_getscheduler, struct task_struct *p) +LSM_HOOK(int, 0, task_movememory, struct task_struct *p) +LSM_HOOK(int, 0, task_kill, struct task_struct *p, struct kernel_siginfo *info, + int sig, const struct cred *cred) +LSM_HOOK(int, -ENOSYS, task_prctl, int option, unsigned long arg2, + unsigned long arg3, unsigned long arg4, unsigned long arg5) +LSM_HOOK(void, LSM_RET_VOID, task_to_inode, struct task_struct *p, + struct inode *inode) +LSM_HOOK(int, 0, ipc_permission, struct kern_ipc_perm *ipcp, short flag) +LSM_HOOK(void, LSM_RET_VOID, ipc_getsecid, struct kern_ipc_perm *ipcp, + u32 *secid) +LSM_HOOK(int, 0, msg_msg_alloc_security, struct msg_msg *msg) +LSM_HOOK(void, LSM_RET_VOID, msg_msg_free_security, struct msg_msg *msg) +LSM_HOOK(int, 0, msg_queue_alloc_security, struct kern_ipc_perm *perm) +LSM_HOOK (void,LSM_RET_VOID , msg_queue_free_security, + struct kern_ipc_perm *perm) +LSM_HOOK(int, 0, msg_queue_associate, struct kern_ipc_perm *perm, int msqflg) +LSM_HOOK(int, 0, msg_queue_msgctl, struct kern_ipc_perm *perm, int cmd) +LSM_HOOK(int, 0, msg_queue_msgsnd, struct kern_ipc_perm *perm, + struct msg_msg *msg, int msqflg) +LSM_HOOK(int, 0, msg_queue_msgrcv, struct kern_ipc_perm *perm, + struct msg_msg *msg, struct task_struct *target, long type, int mode) +LSM_HOOK(int, 0, shm_alloc_security, struct kern_ipc_perm *perm) +LSM_HOOK(void, LSM_RET_VOID, shm_free_security, struct kern_ipc_perm *perm) +LSM_HOOK(int, 0, shm_associate, struct kern_ipc_perm *perm, int shmflg) +LSM_HOOK(int, 0, shm_shmctl, struct kern_ipc_perm *perm, int cmd) +LSM_HOOK(int, 0, shm_shmat, struct kern_ipc_perm *perm, char __user *shmaddr, + int shmflg) +LSM_HOOK(int, 0, sem_alloc_security, struct kern_ipc_perm *perm) +LSM_HOOK(void, LSM_RET_VOID, sem_free_security, struct kern_ipc_perm *perm) +LSM_HOOK(int, 0, sem_associate, struct kern_ipc_perm *perm, int semflg) +LSM_HOOK(int, 0, sem_semctl, struct kern_ipc_perm *perm, int cmd) +LSM_HOOK(int, 0, sem_semop, struct kern_ipc_perm *perm, struct sembuf *sops, + unsigned nsops, int alter) +LSM_HOOK(int, 0, netlink_send, struct sock *sk, struct sk_buff *skb) +LSM_HOOK(void, LSM_RET_VOID, d_instantiate, struct dentry *dentry, + struct inode *inode) +LSM_HOOK(int, -EINVAL, getprocattr, struct task_struct *p, char *name, + char **value) +LSM_HOOK(int, -EINVAL, setprocattr, const char *name, void *value, size_t size) +LSM_HOOK(int, 0, ismaclabel, const char *name) +LSM_HOOK(int, 0, secid_to_secctx, u32 secid, char **secdata, + u32 *seclen) +LSM_HOOK(int, 0, secctx_to_secid, const char *secdata, u32 seclen, u32 *secid) +LSM_HOOK(void, LSM_RET_VOID, release_secctx, char *secdata, u32 seclen) +LSM_HOOK(void, LSM_RET_VOID, inode_invalidate_secctx, struct inode *inode) +LSM_HOOK(int, 0, inode_notifysecctx, struct inode *inode, void *ctx, u32 ctxlen) +LSM_HOOK(int, 0, inode_setsecctx, struct dentry *dentry, void *ctx, u32 ctxlen) +LSM_HOOK(int, 0, inode_getsecctx, struct inode *inode, void **ctx, + u32 *ctxlen) + +#ifdef CONFIG_SECURITY_NETWORK +LSM_HOOK(int, 0, unix_stream_connect, struct sock *sock, struct sock *other, + struct sock *newsk) +LSM_HOOK(int, 0, unix_may_send, struct socket *sock, struct socket *other) +LSM_HOOK(int, 0, socket_create, int family, int type, int protocol, int kern) +LSM_HOOK(int, 0, socket_post_create, struct socket *sock, int family, int type, + int protocol, int kern) +LSM_HOOK(int, 0, socket_socketpair, struct socket *socka, struct socket *sockb) +LSM_HOOK(int, 0, socket_bind, struct socket *sock, struct sockaddr *address, + int addrlen) +LSM_HOOK(int, 0, socket_connect, struct socket *sock, struct sockaddr *address, + int addrlen) +LSM_HOOK(int, 0, socket_listen, struct socket *sock, int backlog) +LSM_HOOK(int, 0, socket_accept, struct socket *sock, struct socket *newsock) +LSM_HOOK(int, 0, socket_sendmsg, struct socket *sock, struct msghdr *msg, + int size) +LSM_HOOK(int, 0, socket_recvmsg, struct socket *sock, struct msghdr *msg, + int size, int flags) +LSM_HOOK(int, 0, socket_getsockname, struct socket *sock) +LSM_HOOK(int, 0, socket_getpeername, struct socket *sock) +LSM_HOOK(int, 0, socket_getsockopt, struct socket *sock, int level, int optname) +LSM_HOOK(int, 0, socket_setsockopt, struct socket *sock, int level, int optname) +LSM_HOOK(int, 0, socket_shutdown, struct socket *sock, int how) +LSM_HOOK(int, 0, socket_sock_rcv_skb, struct sock *sk, struct sk_buff *skb) +LSM_HOOK(int, 0, socket_getpeersec_stream, struct socket *sock, + char __user *optval, int __user *optlen, unsigned len) +LSM_HOOK(int, 0, socket_getpeersec_dgram, struct socket *sock, + struct sk_buff *skb, u32 *secid) +LSM_HOOK(int, 0, sk_alloc_security, struct sock *sk, int family, gfp_t priority) +LSM_HOOK(void, LSM_RET_VOID, sk_free_security, struct sock *sk) +LSM_HOOK(void, LSM_RET_VOID, sk_clone_security, const struct sock *sk, + struct sock *newsk) +LSM_HOOK(void, LSM_RET_VOID, sk_getsecid, struct sock *sk, u32 *secid) +LSM_HOOK(void, LSM_RET_VOID, sock_graft, struct sock *sk, struct socket *parent) +LSM_HOOK(int, 0, inet_conn_request, struct sock *sk, struct sk_buff *skb, + struct request_sock *req) +LSM_HOOK(void, LSM_RET_VOID, inet_csk_clone, struct sock *newsk, + const struct request_sock *req) +LSM_HOOK(void, LSM_RET_VOID, inet_conn_established, struct sock *sk, + struct sk_buff *skb) +LSM_HOOK(int, 0, secmark_relabel_packet, u32 secid) +LSM_HOOK(void, LSM_RET_VOID, secmark_refcount_inc, void) +LSM_HOOK(void, LSM_RET_VOID, secmark_refcount_dec, void) +LSM_HOOK(void, LSM_RET_VOID, req_classify_flow, const struct request_sock *req, + struct flowi *fl) +LSM_HOOK(int, 0, tun_dev_alloc_security, void **security) +LSM_HOOK(void, LSM_RET_VOID, tun_dev_free_security, void *security) +LSM_HOOK(int, 0, tun_dev_create, void) +LSM_HOOK(int, 0, tun_dev_attach_queue, void *security) +LSM_HOOK(int, 0, tun_dev_attach, struct sock *sk, void *security) +LSM_HOOK(int, 0, tun_dev_open, void *security) +LSM_HOOK(int, 0, sctp_assoc_request, struct sctp_endpoint *ep, + struct sk_buff *skb) +LSM_HOOK(int, 0, sctp_bind_connect, struct sock *sk, int optname, + struct sockaddr *address, int addrlen) +LSM_HOOK(void, LSM_RET_VOID, sctp_sk_clone, struct sctp_endpoint *ep, + struct sock *sk, struct sock *newsk) +#endif /* CONFIG_SECURITY_NETWORK */ + +#ifdef CONFIG_SECURITY_INFINIBAND +LSM_HOOK(int, 0, ib_pkey_access, void *sec, u64 subnet_prefix, u16 pkey) +LSM_HOOK(int, 0, ib_endport_manage_subnet, void *sec, const char *dev_name, + u8 port_num) +LSM_HOOK(int, 0, ib_alloc_security, void **sec) +LSM_HOOK(void, LSM_RET_VOID, ib_free_security, void *sec) +#endif /* CONFIG_SECURITY_INFINIBAND */ + +#ifdef CONFIG_SECURITY_NETWORK_XFRM +LSM_HOOK(int, 0, xfrm_policy_alloc_security, struct xfrm_sec_ctx **ctxp, + struct xfrm_user_sec_ctx *sec_ctx, gfp_t gfp) +LSM_HOOK(int, 0, xfrm_policy_clone_security, struct xfrm_sec_ctx *old_ctx, + struct xfrm_sec_ctx **new_ctx) +LSM_HOOK (void,LSM_RET_VOID , xfrm_policy_free_security, + struct xfrm_sec_ctx *ctx) +LSM_HOOK(int, 0, xfrm_policy_delete_security, struct xfrm_sec_ctx *ctx) +LSM_HOOK(int, 0, xfrm_state_alloc, struct xfrm_state *x, + struct xfrm_user_sec_ctx *sec_ctx) +LSM_HOOK(int, 0, xfrm_state_alloc_acquire, struct xfrm_state *x, + struct xfrm_sec_ctx *polsec, u32 secid) +LSM_HOOK(void, LSM_RET_VOID, xfrm_state_free_security, struct xfrm_state *x) +LSM_HOOK(int, 0, xfrm_state_delete_security, struct xfrm_state *x) +LSM_HOOK(int, 0, xfrm_policy_lookup, struct xfrm_sec_ctx *ctx, u32 fl_secid, + u8 dir) +LSM_HOOK(int, 1, xfrm_state_pol_flow_match, struct xfrm_state *x, + struct xfrm_policy *xp, const struct flowi *fl) +LSM_HOOK(int, 0, xfrm_decode_session, struct sk_buff *skb, u32 *secid, + int ckall) +#endif /* CONFIG_SECURITY_NETWORK_XFRM */ + +/* key management security hooks */ +#ifdef CONFIG_KEYS +LSM_HOOK(int, 0, key_alloc, struct key *key, const struct cred *cred, + unsigned long flags) +LSM_HOOK(void, LSM_RET_VOID, key_free, struct key *key) +LSM_HOOK(int, 0, key_permission, key_ref_t key_ref, const struct cred *cred, + unsigned perm) +LSM_HOOK(int, 0, key_getsecurity, struct key *key, char **_buffer) +#endif /* CONFIG_KEYS */ + +#ifdef CONFIG_AUDIT +LSM_HOOK(int, 0, audit_rule_init, u32 field, u32 op, char *rulestr, + void **lsmrule) +LSM_HOOK(int, 0, audit_rule_known, struct audit_krule *krule) +LSM_HOOK(int, 0, audit_rule_match, u32 secid, u32 field, u32 op, void *lsmrule) +LSM_HOOK(void, LSM_RET_VOID, audit_rule_free, void *lsmrule) +#endif /* CONFIG_AUDIT */ + +#ifdef CONFIG_BPF_SYSCALL +LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size) +LSM_HOOK(int, 0, bpf_map, struct bpf_map *map, fmode_t fmode) +LSM_HOOK(int, 0, bpf_prog, struct bpf_prog *prog) +LSM_HOOK(int, 0, bpf_map_alloc_security, struct bpf_map *map) +LSM_HOOK(void, LSM_RET_VOID, bpf_map_free_security, struct bpf_map *map) +LSM_HOOK(int, 0, bpf_prog_alloc_security, struct bpf_prog_aux *aux) +LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free_security, struct bpf_prog_aux *aux) +#endif /* CONFIG_BPF_SYSCALL */ + +LSM_HOOK(int, 0, locked_down, enum lockdown_reason what) + +#ifdef CONFIG_PERF_EVENTS +LSM_HOOK(int, 0, perf_event_open, struct perf_event_attr *attr, int type) +LSM_HOOK(int, 0, perf_event_alloc, struct perf_event *event) +LSM_HOOK(void, LSM_RET_VOID, perf_event_free, struct perf_event *event) +LSM_HOOK(int, 0, perf_event_read, struct perf_event *event) +LSM_HOOK(int, 0, perf_event_write, struct perf_event *event) +#endif /* CONFIG_PERF_EVENTS */
include/linux/lsm_hooks.h +12 -616
643 lines not on this page · raw message
security/security.c +30 -11
102 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v8 3/8 bpf: lsm: provide attachment points for BPF LSM programs
In reply to KP SinghFrom: KP Singh <[email protected]>
When CONFIG_BPF_LSM is enabled, nop functions, bpf_lsm_<hook_name>, are generated for each LSM hook. These functions are initialized as LSM hooks in a subsequent patch.
--- include/linux/bpf_lsm.h | 22 ++++++++++++++++++++++ kernel/bpf/bpf_lsm.c | 14 ++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 include/linux/bpf_lsm.h
include/linux/bpf_lsm.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2020 Google LLC. +*/ + +#ifndef _LINUX_BPF_LSM_H +#define _LINUX_BPF_LSM_H + +#include <linux/bpf.h> +#include <linux/lsm_hooks.h> + +#ifdef CONFIG_BPF_LSM + +#define LSM_HOOK (RET ,DEFAULT ,NAME , ...) \ + RET bpf_lsm_##NAME(__VA_ARGS__); +#include <linux/lsm_hook_defs.h> +#undef LSM_HOOK + +#endif /* CONFIG_BPF_LSM */ + +#endif /* _LINUX_BPF_LSM_H */
kernel/bpf/bpf_lsm.c +14 -0
@@ -7,6 +7,20 @@#include <linux/filter.h> #include <linux/bpf.h> #include <linux/btf.h> +#include <linux/lsm_hooks.h> +#include <linux/bpf_lsm.h> + +/* For every LSM hook that allows attachment of BPF programs, declare a nop +* function where a BPF program can be attached. +*/ +#define LSM_HOOK (RET ,DEFAULT ,NAME , ...) \ +noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ +{ \ + return DEFAULT; \ +} + +#include <linux/lsm_hook_defs.h> +#undef LSM_HOOKconst struct bpf_prog_ops lsm_prog_ops = { };
Signature · 1 line
2.20.1
PATCH v8 4/8 bpf: lsm: Implement attach, detach and execution
In reply to KP SinghFrom: KP Singh <[email protected]>
JITed BPF programs are dynamically attached to the LSM hooks using BPF trampolines. The trampoline prologue generates code to handle conversion of the signature of the hook to the appropriate BPF context.
The allocated trampoline programs are attached to the nop functions initialized as LSM hooks.
BPF_PROG_TYPE_LSM programs must have a GPL compatible license and and need CAP_SYS_ADMIN (required for loading eBPF programs).
Upon attachment:
* A BPF fexit trampoline is used for LSM hooks with a void return type.
* A BPF fmod_ret trampoline is used for LSM hooks which return an int. The attached programs can override the return value of the bpf LSM hook to indicate a MAC Policy decision.
--- include/linux/bpf_lsm.h | 11 ++++++++ kernel/bpf/bpf_lsm.c | 23 +++++++++++++++++ kernel/bpf/btf.c | 16 +++++++++++- kernel/bpf/syscall.c | 57 ++++++++++++++++++++++++++++------------- kernel/bpf/trampoline.c | 17 +++++++++--- kernel/bpf/verifier.c | 19 +++++++++++--- 6 files changed, 116 insertions(+), 27 deletions(-)
include/linux/bpf_lsm.h +11 -0
@@ -17,6 +17,17 @@#include <linux/lsm_hook_defs.h> #undef LSM_HOOK +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog); + +#else /* !CONFIG_BPF_LSM */ + +static inline int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
kernel/bpf/bpf_lsm.c +23 -0
@@ -9,6 +9,8 @@#include <linux/btf.h> #include <linux/lsm_hooks.h> #include <linux/bpf_lsm.h> +#include <linux/kallsyms.h> +#include <linux/bpf_verifier.h> /* For every LSM hook that allows attachment of BPF programs, declare a nop * function where a BPF program can be attached.
@@ -22,6 +24,27 @@ noinline RET bpf_lsm_##NAME(__VA_ARGS__) \#include <linux/lsm_hook_defs.h> #undef LSM_HOOK +#define BPF_LSM_SYM_PREFX "bpf_lsm_" + +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +if (!prog->gpl_compatible) { +bpf_log (vlog, +"LSM programs must have a GPL compatible license\n" ); +return -EINVAL ; + } + +if (strncmp (BPF_LSM_SYM_PREFX , prog->aux->attach_func_name, +sizeof (BPF_LSM_SYM_PREFX ) -1 )) { +bpf_log (vlog,"attach_btf_id %u points to wrong type name %s\n" , + prog->aux->attach_btf_id, prog->aux->attach_func_name); +return -EINVAL ; + } + +return 0 ; +} +const struct bpf_prog_ops lsm_prog_ops = { };
kernel/bpf/btf.c +15 -1
@@ -3710,7 +3710,21 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, }if (arg == nr_args) { -if (prog->expected_attach_type ==BPF_TRACE_FEXIT ) { +if (prog->expected_attach_type ==BPF_TRACE_FEXIT || + prog->expected_attach_type ==BPF_LSM_MAC ) { +/* When LSM programs are attached to void LSM hooks +* they use FEXIT trampolines and when attached to +* int LSM hooks, they use MODIFY_RETURN trampolines. +* +* While the LSM programs are BPF_MODIFY_RETURN-like +* the check: +* +* if (ret_type != 'int') +* return -EINVAL; +* +* is _not_ done here. This is still safe as LSM hooks +* have only void and int return types. +*/ if (!t)return true; t =btf_type_by_id (btf, t->type);
kernel/bpf/syscall.c +39 -18
94 lines not on this page · raw message
kernel/bpf/trampoline.c +13 -4
@@ -6,6 +6,7 @@#include <linux/ftrace.h> #include <linux/rbtree_latch.h> #include <linux/perf_event.h> +#include <linux/btf.h> /* dummy _ops. The verifier will operate on target program's ops. */ const struct bpf_verifier_ops bpf_extension_verifier_ops = {
@@ -233,15 +234,23 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr)return err; } -static enum bpf_tramp_prog_type bpf_attach_type_to_tramp (enum bpf_attach_type t) +static enum bpf_tramp_prog_type bpf_attach_type_to_tramp (struct bpf_prog *prog) { -switch (t) { +switch (prog->expected_attach_type) {case BPF_TRACE_FENTRY :return BPF_TRAMP_FENTRY ;case BPF_MODIFY_RETURN :return BPF_TRAMP_MODIFY_RETURN ;case BPF_TRACE_FEXIT :return BPF_TRAMP_FEXIT ; +case BPF_LSM_MAC : +if (!prog->aux->attach_func_proto->type) +/* The function returns void, we cannot modify its +* return value. +*/ +return BPF_TRAMP_FEXIT ; +else +return BPF_TRAMP_MODIFY_RETURN ;default :return BPF_TRAMP_REPLACE ; }
@@ -255,7 +264,7 @@ int bpf_trampoline_link_prog(struct bpf_prog *prog)int cnt; tr = prog->aux->trampoline; - kind =bpf_attach_type_to_tramp (prog->expected_attach_type); + kind =bpf_attach_type_to_tramp (prog);mutex_lock (&tr->mutex);if (tr->extension_prog) {/* cannot attach fentry/fexit if extension prog is attached.
@@ -305,7 +314,7 @@ int bpf_trampoline_unlink_prog(struct bpf_prog *prog)int err; tr = prog->aux->trampoline; - kind =bpf_attach_type_to_tramp (prog->expected_attach_type); + kind =bpf_attach_type_to_tramp (prog);mutex_lock (&tr->mutex);if (kind ==BPF_TRAMP_REPLACE ) {WARN_ON_ONCE (!tr->extension_prog);
kernel/bpf/verifier.c +15 -4
@@ -20,6 +20,7 @@#include <linux/perf_event.h> #include <linux/ctype.h> #include <linux/error-injection.h> +#include <linux/bpf_lsm.h> #include "disasm.h"
@@ -6488,8 +6489,9 @@ static int check_return_code(struct bpf_verifier_env *env)struct tnum range =tnum_range (0 ,1 );int err; -/* The struct_ops func-ptr's return type could be "void" */ -if (env->prog->type ==BPF_PROG_TYPE_STRUCT_OPS && +/* LSM and struct_ops func-ptr's return type could be "void" */ +if ((env->prog->type ==BPF_PROG_TYPE_STRUCT_OPS || + env->prog->type ==BPF_PROG_TYPE_LSM ) && !prog->aux->attach_func_proto->type)return 0 ;
@@ -9919,7 +9921,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS )return check_struct_ops_btf_id (env); -if (prog->type !=BPF_PROG_TYPE_TRACING && !prog_extension) +if (prog->type !=BPF_PROG_TYPE_TRACING && + prog->type !=BPF_PROG_TYPE_LSM && + !prog_extension)return 0 ;if (!btf_id) {
@@ -10050,8 +10054,16 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -EINVAL ;/* fallthrough */ case BPF_MODIFY_RETURN : +case BPF_LSM_MAC :case BPF_TRACE_FENTRY :case BPF_TRACE_FEXIT : + prog->aux->attach_func_name = tname; +if (prog->type ==BPF_PROG_TYPE_LSM ) { + ret =bpf_lsm_verify_prog (&env->log, prog); +if (ret <0 ) +return ret; + } +if (!btf_type_is_func (t)) {verbose (env,"attach_btf_id %u is not a function\n" , btf_id);
@@ -10066,7 +10078,6 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) tr =bpf_trampoline_lookup (key);if (!tr)return -ENOMEM ; - prog->aux->attach_func_name = tname;/* t is either vmlinux type or another program's type */ prog->aux->attach_func_proto = t;mutex_lock (&tr->mutex);
Signature · 1 line
2.20.1
Re: [PATCH bpf-next v8 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 19 lines · KP Singh
From: KP Singh <[email protected]>
JITed BPF programs are dynamically attached to the LSM hooks using BPF trampolines. The trampoline prologue generates code to handle conversion of the signature of the hook to the appropriate BPF context.
The allocated trampoline programs are attached to the nop functions initialized as LSM hooks.
BPF_PROG_TYPE_LSMprograms must have a GPL compatible license and and needCAP_SYS_ADMIN(required for loading eBPF programs).Upon attachment:
* A BPF fexit trampoline is used for LSM hooks with a void return type.
* A BPFfmod_rettrampoline is used for LSM hooks which return an int. The attached programs can override the return value of the bpf LSM hook to indicate a MAC Policy decision.
Quoted 4 lines
Signed-off-by KP Singh <[email protected]>Acked-by Andrii Nakryiko <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v8 5/8 bpf: lsm: Initialize the BPF LSM hooks
In reply to KP SinghFrom: KP Singh <[email protected]>
* The hooks are initialized using the definitions in include/linux/lsm_hook_defs.h.
* The LSM can be enabled / disabled with CONFIG_BPF_LSM.
--- security/Kconfig | 10 +++++----- security/Makefile | 2 ++ security/bpf/Makefile | 5 +++++ security/bpf/hooks.c | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 security/bpf/Makefile create mode 100644 security/bpf/hooks.c
security/Kconfig +5 -5
@@ -277,11 +277,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
@@ -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_BPF_LSM) += bpf # always enable default capabilities obj-y += commoncap.o
@@ -30,6 +31,7 @@ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o +obj-$(CONFIG_BPF_LSM) += bpf/ # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integrity
security/bpf/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020 Google LLC. + +obj-$(CONFIG_BPF_LSM) := hooks.o
security/bpf/hooks.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2020 Google LLC. +*/ +#include <linux/lsm_hooks.h> +#include <linux/bpf_lsm.h> + +static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = { + #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ + LSM_HOOK_INIT(NAME, bpf_lsm_##NAME), + #include <linux/lsm_hook_defs.h> + #undef LSM_HOOK +}; + +static int __init bpf_lsm_init (void ) +{ +security_add_hooks (bpf_lsm_hooks,ARRAY_SIZE (bpf_lsm_hooks),"bpf" ); +pr_info ("LSM support for eBPF active\n" ); +return 0 ; +} + +DEFINE_LSM (bpf) = { + .name ="bpf" , + .init = bpf_lsm_init, +};
Signature · 1 line
2.20.1
PATCH v8 6/8 tools/libbpf: Add support for BPF_PROG_TYPE_LSM
In reply to KP SinghFrom: KP Singh <[email protected]>
Since BPF_PROG_TYPE_LSM uses the same attaching mechanism as BPF_PROG_TYPE_TRACING, the common logic is refactored into a static function bpf_program__attach_btf_id.
A new API call bpf_program__attach_lsm is still added to avoid userspace conflicts if this ever changes in the future.
--- tools/lib/bpf/bpf.c | 3 ++- tools/lib/bpf/libbpf.c | 39 +++++++++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 4 ++++ tools/lib/bpf/libbpf.map | 3 +++ 4 files changed, 44 insertions(+), 5 deletions(-)
tools/lib/bpf/bpf.c +2 -1
@@ -235,7 +235,8 @@ 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_STRUCT_OPS || + attr.prog_type ==BPF_PROG_TYPE_LSM ) { attr.attach_btf_id = load_attr->attach_btf_id; }else if (attr.prog_type ==BPF_PROG_TYPE_TRACING || attr.prog_type ==BPF_PROG_TYPE_EXT ) {
tools/lib/bpf/libbpf.c +35 -4
@@ -2358,7 +2358,8 @@ static int bpf_object__finalize_btf(struct bpf_object *obj)static inline bool libbpf_prog_needs_vmlinux_btf (struct bpf_program *prog) { -if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS ) +if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_LSM )return true;/* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
@@ -4866,7 +4867,8 @@ 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_STRUCT_OPS || + prog->type ==BPF_PROG_TYPE_LSM ) { load_attr.attach_btf_id = prog->attach_btf_id; }else if (prog->type ==BPF_PROG_TYPE_TRACING || prog->type ==BPF_PROG_TYPE_EXT ) {
@@ -4957,6 +4959,7 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)int err =0 , fd, i, btf_id;if ((prog->type ==BPF_PROG_TYPE_TRACING || + prog->type ==BPF_PROG_TYPE_LSM || prog->type ==BPF_PROG_TYPE_EXT ) && !prog->attach_btf_id) { btf_id =libbpf_find_attach_btf_id (prog);if (btf_id <=0 )
@@ -6196,6 +6199,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 );
@@ -6262,6 +6266,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;
@@ -6312,6 +6318,10 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("freplace/" ,EXT , .is_attach_btf = true, .attach_fn = attach_trace), +SEC_DEF ("lsm/" ,LSM , + .is_attach_btf = true, + .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),
@@ -6574,6 +6584,7 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, }#define BTF_TRACE_PREFIX "btf_trace_" +#define BTF_LSM_PREFIX "bpf_lsm_"#define BTF_MAX_NAME_SIZE 128 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
@@ -6601,6 +6612,9 @@ static inline int __find_vmlinux_btf_id(struct btf *btf, const char *name,if (attach_type ==BPF_TRACE_RAW_TP ) err =find_btf_by_prefix_kind (btf,BTF_TRACE_PREFIX , name,BTF_KIND_TYPEDEF ); +else if (attach_type ==BPF_LSM_MAC ) + err =find_btf_by_prefix_kind (btf,BTF_LSM_PREFIX , name, +BTF_KIND_FUNC );else err =btf__find_by_name_kind (btf, name,BTF_KIND_FUNC );
@@ -7454,7 +7468,8 @@ static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,return bpf_program__attach_raw_tracepoint (prog, tp_name); } -struct bpf_link *bpf_program__attach_trace (struct bpf_program *prog) +/* Common logic for all BPF program types that attach to a btf_id */ +static struct bpf_link *bpf_program__attach_btf_id (struct bpf_program *prog) {char errmsg[STRERR_BUFSIZE ];struct bpf_link *link;
@@ -7476,7 +7491,7 @@ struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)if (pfd <0 ) { pfd = -errno;free (link); -pr_warn ("program '%s': failed to attach to trace: %s\n" , +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);
@@ -7485,12 +7500,28 @@ struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)return (struct bpf_link *)link; } +struct bpf_link *bpf_program__attach_trace (struct bpf_program *prog) +{ +return bpf_program__attach_btf_id (prog); +} + +struct bpf_link *bpf_program__attach_lsm (struct bpf_program *prog) +{ +return bpf_program__attach_btf_id (prog); +} +static struct bpf_link *attach_trace (const struct bpf_sec_def *sec,struct bpf_program *prog) {return bpf_program__attach_trace (prog); } +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 +4 -0
@@ -248,6 +248,8 @@ bpf_program__attach_raw_tracepoint(struct bpf_program *prog, LIBBPF_API struct bpf_link *bpf_program__attach_trace (struct bpf_program *prog); +LIBBPF_API struct bpf_link * +bpf_program__attach_lsm (struct bpf_program *prog);struct bpf_map ; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map);struct bpf_insn ;
@@ -321,6 +323,7 @@ LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog); LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog); LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog); LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog); +LIBBPF_API int bpf_program__set_lsm(struct bpf_program *prog); LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
@@ -347,6 +350,7 @@ LIBBPF_API bool bpf_program__is_socket_filter(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_tracepoint(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_raw_tracepoint(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_kprobe(const struct bpf_program *prog); +LIBBPF_API bool bpf_program__is_lsm(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_sched_cls(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_sched_act(const struct bpf_program *prog); LIBBPF_API bool bpf_program__is_xdp(const struct bpf_program *prog);
tools/lib/bpf/libbpf.map +3 -0
@@ -243,5 +243,8 @@ LIBBPF_0.0.8 { bpf_link__pin; bpf_link__pin_path; bpf_link__unpin; + bpf_program__attach_lsm; + bpf_program__is_lsm; bpf_program__set_attach_target; + bpf_program__set_lsm; } LIBBPF_0.0.7;
Signature · 1 line
2.20.1
PATCH v8 7/8 bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
In reply to KP SinghFrom: KP Singh <[email protected]>
* Load/attach a BPF program that hooks to file_mprotect (int) and bprm_committed_creds (void).
* Perform an action that triggers the hook.
* Verify if the audit event was received using the shared global variables for the process executed.
* Verify if the mprotect returns a -EPERM.
--- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/test_lsm.c | 86 +++++++++++++++++++ tools/testing/selftests/bpf/progs/lsm.c | 48 +++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_lsm.c create mode 100644 tools/testing/selftests/bpf/progs/lsm.c
tools/testing/selftests/bpf/config +2 -0
@@ -35,3 +35,5 @@ CONFIG_MPLS_ROUTING=m CONFIG_MPLS_IPTUNNEL=m CONFIG_IPV6_SIT=m CONFIG_BPF_JIT=y +CONFIG_BPF_LSM=y +CONFIG_SECURITY=y
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +86 -0
86 lines not on this page · raw message
tools/testing/selftests/bpf/progs/lsm.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v8 8/8 bpf: lsm: Add Documentation
In reply to KP SinghFrom: 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/bpf/bpf_lsm.rst | 142 ++++++++++++++++++++++++++++++++++ Documentation/bpf/index.rst | 1 + 2 files changed, 143 insertions(+) create mode 100644 Documentation/bpf/bpf_lsm.rst
Documentation/bpf/bpf_lsm.rst new file +142 -0
@@ -0,0 +1,142 @@ +.. SPDX-License-Identifier: GPL-2.0+ +.. Copyright (C) 2020 Google LLC. + +================ +LSM BPF Programs +================ + +These BPF programs allow runtime instrumentation of the LSM hooks by privileged +users to implement system-wide MAC (Mandatory Access Control) and Audit +policies using eBPF. + +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); + +Other LSM hooks which can be instrumented can be found in +``include/linux/lsm_hooks.h``. + +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:: The order of the fields is irrelevant. + +This can be further simplified (if one has access to the BTF information at +build time) by generating the ``vmlinux.h`` with: + +.. code-block:: console + + # bpftool btf dump file <path-to-btf-vmlinux> format c > vmlinux.h + +.. note:: ``path-to-btf-vmlinux`` can be ``/sys/kernel/btf/vmlinux`` if the + build environment matches the environment the BPF programs are + deployed in. + +The ``vmlinux.h`` can then simply be included in the BPF programs without +requiring the definition of the types. + +The eBPF programs can be declared using the``BPF_PROG`` +macros defined in `tools/lib/bpf/bpf_tracing.h`_. In this +example: + + * ``"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 ret) + { + /* ret is the return value from the previous BPF program + * or 0 if it's the first hook. + */ + if (ret != 0) + return ret; + + 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 + */ + if (is_heap) + return -EPERM; + } + +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 +------- + +eBPF programs can be loaded with the :manpage:`bpf(2)` syscall's +``BPF_PROG_LOAD`` operation: + +.. code-block:: c + + struct bpf_object *obj; + + obj = bpf_object__open("./my_prog.o"); + bpf_object__load(obj); + +This can be simplified by using a skeleton header generated by ``bpftool``: + +.. code-block:: console + + # bpftool gen skeleton my_prog.o > my_prog.skel.h + +and the program can be loaded by including ``my_prog.skel.h`` and using +the generated helper, ``my_prog__open_and_load``. + +Attachment to LSM Hooks +----------------------- + +The LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)` +syscall's ``BPF_RAW_TRACEPOINT_OPEN`` operation or more simply by +using the libbpf helper ``bpf_program__attach_lsm``. + +The program can be detached from the LSM hook by *destroying* the ``link`` +link returned by ``bpf_program__attach_lsm`` using ``bpf_link__destroy``. + +One can also use the helpers generated in ``my_prog.skel.h`` i.e. +``my_prog__attach`` for attachment and ``my_prog__destroy`` for cleaning up. + +Examples +-------- + +An example eBPF program can be found in +`tools/testing/selftests/bpf/progs/lsm.c`_ and the corresponding +userspace code in `tools/testing/selftests/bpf/prog_tests/test_lsm.c`_ + +.. Links +.. _tools/lib/bpf/bpf_tracing.h: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/lib/bpf/bpf_tracing.h +.. _tools/testing/selftests/bpf/progs/lsm.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm.c +.. _tools/testing/selftests/bpf/prog_tests/test_lsm.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/test_lsm.c
Documentation/bpf/index.rst +1 -0
@@ -45,6 +45,7 @@ Program types prog_cgroup_sockopt prog_cgroup_sysctl prog_flow_dissector + bpf_lsm Testing and debugging BPF
Signature · 1 line
2.20.1
Daniel Borkmann
maintainerHey KP,
Quoted 20 lines · KP Singh
From: KP Singh <[email protected]>
# v7 -> v8https://lore.kernel.org/bpf/[email protected]/* Removed
CAP_MAC_ADMINcheck frombpf_lsm_verify_prog. LSMs can add it in their ownbpf_proghook. This can be revisited as a separate patch.
* Added Andrii and James' Ack/Review tags.
* Fixed an indentation issue and missing newlines in selftest error a cases.
* Updated a comment as suggested by Alexei.
* Updated the documentation to use the newer libbpf API and some other fixes.
* Rebase# v6 -> v7https://lore.kernel.org/bpf/[email protected]/
[...]
Quoted 9 lines
KP Singh (8):
bpf: IntroduceBPF_PROG_TYPE_LSM
security: Refactor declaration of LSM hooks
bpf: lsm: provide attachment points for BPF LSM programs
bpf: lsm: Implement attach, detach and execution
bpf: lsm: Initialize the BPF LSM hookstools/libbpf: Add support forBPF_PROG_TYPE_LSM
bpf: lsm: Add selftests forBPF_PROG_TYPE_LSM
bpf: lsm: Add Documentation
I was about to apply, but then I'm getting the following selftest issue on the added LSM one, ptal:
# ./test_progs [...] #65/1 test_global_func1.o:OK #65/2 test_global_func2.o:OK #65/3 test_global_func3.o:OK #65/4 test_global_func4.o:OK #65/5 test_global_func5.o:OK #65/6 test_global_func6.o:OK #65/7 test_global_func7.o:OK #65 test_global_funcs:OK test_test_lsm:PASS:skel_load 0 nsec test_test_lsm:PASS:attach 0 nsec test_test_lsm:PASS:exec_cmd 0 nsec test_test_lsm:FAIL:bprm_count bprm_count = 0 test_test_lsm:FAIL:heap_mprotect want errno=EPERM, got 22 #66 test_lsm:FAIL test_test_overhead:PASS:obj_open_file 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec Caught signal #11! Stack trace: ./test_progs(crash_handler+0x31)[0x56100f25eb51] /lib/x86_64-linux-gnu/libpthread.so.0(+0x12890)[0x7f9d8d225890] /lib/x86_64-linux-gnu/libc.so.6(+0x18ef2d)[0x7f9d8cfb0f2d] /lib/x86_64-linux-gnu/libc.so.6(__libc_calloc+0x372)[0x7f9d8cebc3a2] /usr/local/lib/libelf.so.1(+0x33ce)[0x7f9d8d85a3ce] /usr/local/lib/libelf.so.1(+0x3fb2)[0x7f9d8d85afb2] ./test_progs(btf__parse_elf+0x15d)[0x56100f27a141] ./test_progs(libbpf_find_kernel_btf+0x169)[0x56100f27ee83] ./test_progs(+0x43906)[0x56100f266906] ./test_progs(bpf_object__load_xattr+0xe5)[0x56100f26e93c] ./test_progs(bpf_object__load+0x47)[0x56100f26eafd] ./test_progs(test_test_overhead+0x252)[0x56100f24a922] ./test_progs(main+0x212)[0x56100f22f772] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f9d8ce43b97] ./test_progs(_start+0x2a)[0x56100f22f8fa] Segmentation fault (core dumped) #
(Before the series, it runs through fine on my side.)
Thanks,
Daniel
Quoted 37 lines · Daniel Borkmann
Hey KP,
Quoted 20 lines · KP Singh
From: KP Singh <[email protected]>
# v7 -> v8https://lore.kernel.org/bpf/[email protected]/* Removed
CAP_MAC_ADMINcheck frombpf_lsm_verify_prog. LSMs can add it in their ownbpf_proghook. This can be revisited as a separate patch.
* Added Andrii and James' Ack/Review tags.
* Fixed an indentation issue and missing newlines in selftest error a cases.
* Updated a comment as suggested by Alexei.
* Updated the documentation to use the newer libbpf API and some other fixes.
* Rebase# v6 -> v7https://lore.kernel.org/bpf/[email protected]/[...]
Quoted 9 lines
KP Singh (8):
bpf: IntroduceBPF_PROG_TYPE_LSM
security: Refactor declaration of LSM hooks
bpf: lsm: provide attachment points for BPF LSM programs
bpf: lsm: Implement attach, detach and execution
bpf: lsm: Initialize the BPF LSM hookstools/libbpf: Add support forBPF_PROG_TYPE_LSM
bpf: lsm: Add selftests forBPF_PROG_TYPE_LSM
bpf: lsm: Add DocumentationI was about to apply, but then I'm getting the following selftest issue on the added LSM one, ptal:
Quoted 15 lines
# ./test_progs [...] #65/1 test_global_func1.o:OK #65/2 test_global_func2.o:OK #65/3 test_global_func3.o:OK #65/4 test_global_func4.o:OK #65/5 test_global_func5.o:OK #65/6 test_global_func6.o:OK #65/7 test_global_func7.o:OK #65 test_global_funcs:OK test_test_lsm:PASS:skel_load 0 nsec test_test_lsm:PASS:attach 0 nsec test_test_lsm:PASS:exec_cmd 0 nsec test_test_lsm:FAIL:bprm_count bprm_count = 0 test_test_lsm:FAIL:heap_mprotect want errno=EPERM, got 22
The test seems to pass for me [classic, "works on my machine" ;)]
./test_progs -t test_lsm
#66 test_lsm:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
and also in the complete run of test_progs.
Since the attachment succeeds and the hook does not get called, it seems like "bpf" LSM is not being initialized and the hook, although present, does not get called.
This indicates that "bpf" is not in CONFIG_LSM. It should, however, be there by default as we added it to default value of CONFIG_LSM and also for other DEFAULT_SECURITY_* options.
Let me know if that's the case and it fixes it.
- KP
Quoted 31 lines
#66 test_lsm:FAIL
test_test_overhead:PASS:obj_open_file 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec test_test_overhead:PASS:find_probe 0 nsec
Caught signal #11!
Stack trace:
./test_progs(crash_handler+0x31)[0x56100f25eb51] /lib/x86_64-linux-gnu/libpthread.so.0(+0x12890)[0x7f9d8d225890] /lib/x86_64-linux-gnu/libc.so.6(+0x18ef2d)[0x7f9d8cfb0f2d] /lib/x86_64-linux-gnu/libc.so.6(__libc_calloc+0x372)[0x7f9d8cebc3a2] /usr/local/lib/libelf.so.1(+0x33ce)[0x7f9d8d85a3ce] /usr/local/lib/libelf.so.1(+0x3fb2)[0x7f9d8d85afb2] ./test_progs(btf__parse_elf+0x15d)[0x56100f27a141] ./test_progs(libbpf_find_kernel_btf+0x169)[0x56100f27ee83] ./test_progs(+0x43906)[0x56100f266906] ./test_progs(bpf_object__load_xattr+0xe5)[0x56100f26e93c] ./test_progs(bpf_object__load+0x47)[0x56100f26eafd] ./test_progs(test_test_overhead+0x252)[0x56100f24a922] ./test_progs(main+0x212)[0x56100f22f772] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f9d8ce43b97] ./test_progs(_start+0x2a)[0x56100f22f8fa]
Segmentation fault (core dumped)
#(Before the series, it runs through fine on my side.)
Thanks,
Daniel
Quoted 9 lines · KP Singh
Since the attachment succeeds and the hook does not get called, it seems like "bpf" LSM is not being initialized and the hook, although present, does not get called.
This indicates that "bpf" is not in
CONFIG_LSM. It should, however, be there by default as we added it to default value ofCONFIG_LSMand also for other DEFAULT_SECURITY_* options.Let me know if that's the case and it fixes it.
Is the selftest expected to at least fail cleanly (i.e. not segfault) when the BPF LSF is not built into the kernel?
Signature · 1 line
Kees Cook
Quoted 6 lines · Kees Cook
Quoted 4 lines · KP Singh
Since the attachment succeeds and the hook does not get called, it seems like "bpf" LSM is not being initialized and the hook, although present, does not get called.
Quoted 7 lines
Quoted 5 lines
This indicates that "bpf" is not in
CONFIG_LSM. It should, however, be there by default as we added it to default value ofCONFIG_LSMand also for other DEFAULT_SECURITY_* options.Let me know if that's the case and it fixes it.
Is the selftest expected to at least fail cleanly (i.e. not segfault)
I am not sure where the crash comes from, it does not look like it's test_lsm, it seems to happen in test_overhead. Both seem to run fine for me.
- KP
Quoted 4 lines
when the BPF LSF is not built into the kernel?
--
Kees Cook
Quoted 11 lines · KP Singh
Quoted 10 lines · Kees Cook
Quoted 8 lines · KP Singh
Since the attachment succeeds and the hook does not get called, it seems like "bpf" LSM is not being initialized and the hook, although present, does not get called.
This indicates that "bpf" is not in
CONFIG_LSM. It should, however, be there by default as we added it to default value ofCONFIG_LSMand also for other DEFAULT_SECURITY_* options.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Let me know if that's the case and it fixes it.
Is the selftest expected to at least fail cleanly (i.e. not segfault)
I am not sure where the crash comes from, it does not look like it's
test_lsm, it seems to happen intest_overhead. Both seem to run fine for me.
So I was able to reproduce the crash:
* Remove "bpf" from CONFIG_LSM
./test_progs -n 66,67
test_test_lsm:PASS:skel_load 0 nsec test_test_lsm:PASS:attach 0 nsec test_test_lsm:PASS:exec_cmd 0 nsec test_test_lsm:FAIL:bprm_count bprm_count = 0 test_test_lsm:FAIL:heap_mprotect want errno=EPERM, got 0 #66 test_lsm:FAIL
Caught signal #11!
Stack trace:
./test_progs(crash_handler+0x1f)[0x55b7f9867acf] /lib/x86_64-linux-gnu/libpthread.so.0(+0x13520)[0x7fcf1467e520] /lib/x86_64-linux-gnu/libc.so.6(+0x15f73d)[0x7fcf1460a73d] /lib/x86_64-linux-gnu/libc.so.6(__libc_calloc+0x2ca)[0x7fcf1453286a] /usr/lib/x86_64-linux-gnu/libelf.so.1(+0x37
[snip]
* The crash went away when I removed the heap_mprotect call, now the BPF hook attached did not allow this operation, so it had no side-effects. Which lead me to believe the crash could be a side-effect of this operation. So I did:
tools/testing/selftests/bpf/prog_tests/test_lsm.c +1 -1
@@ -29,7 +29,7 @@ int heap_mprotect(void)if (buf ==NULL )return -ENOMEM ; - ret =mprotect (buf, sz,PROT_READ |PROT_EXEC ); + ret =mprotect (buf, sz,PROT_READ |PROT_WRITE |PROT_EXEC );free (buf);return ret; }
and the crash went away. Which made me realize that the free operation does not like memory without PROT_WRITE, So I did this:
tools/testing/selftests/bpf/prog_tests/test_lsm.c +1 -1
@@ -30,7 +30,7 @@ int heap_mprotect(void)return -ENOMEM ; ret =mprotect (buf, sz,PROT_READ |PROT_EXEC ); -free (buf); +// free(buf); return ret; }
and the crash went away as well. So it indeed was a combination of:
* CONFIG_LSM not enabling the hook
* mprotect marking the memory as non-writeable
* free being called on the memory.
I will send a v9 which has the PROT_WRITE on the mprotect. Thanks for noticing this!
- KP
Quoted 7 lines
- KP
Quoted 4 lines
when the BPF LSF is not built into the kernel?
--
Kees Cook
Daniel Borkmann
maintainerQuoted 75 lines · KP Singh
Quoted 11 lines · KP Singh
Quoted 10 lines · Kees Cook
Quoted 8 lines · KP Singh
Since the attachment succeeds and the hook does not get called, it seems like "bpf" LSM is not being initialized and the hook, although present, does not get called.
This indicates that "bpf" is not in
CONFIG_LSM. It should, however, be there by default as we added it to default value ofCONFIG_LSMand also for other DEFAULT_SECURITY_* options.Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Let me know if that's the case and it fixes it.
Is the selftest expected to at least fail cleanly (i.e. not segfault)
I am not sure where the crash comes from, it does not look like it's
test_lsm, it seems to happen intest_overhead. Both seem to run fine for me.So I was able to reproduce the crash:
* Remove "bpf" from
CONFIG_LSM./test_progs -n 66,67
test_test_lsm:PASS:skel_load 0 nsec test_test_lsm:PASS:attach 0 nsec test_test_lsm:PASS:exec_cmd 0 nsec test_test_lsm:FAIL:bprm_countbprm_count= 0 test_test_lsm:FAIL:heap_mprotect want errno=EPERM, got 0 #66 test_lsm:FAIL
Caught signal #11!
Stack trace:
./test_progs(crash_handler+0x1f)[0x55b7f9867acf] /lib/x86_64-linux-gnu/libpthread.so.0(+0x13520)[0x7fcf1467e520] /lib/x86_64-linux-gnu/libc.so.6(+0x15f73d)[0x7fcf1460a73d] /lib/x86_64-linux-gnu/libc.so.6(__libc_calloc+0x2ca)[0x7fcf1453286a] /usr/lib/x86_64-linux-gnu/libelf.so.1(+0x37[snip]
* The crash went away when I removed the
heap_mprotectcall, now the BPF hook attached did not allow this operation, so it had no side-effects. Which lead me to believe the crash could be a side-effect of this operation. So I did:tools/testing/selftests/bpf/prog_tests/test_lsm.c +1
-1@@ -29,7 +29,7 @@ int heap_mprotect(void)if (buf ==NULL )return -ENOMEM ; - ret =mprotect (buf, sz,PROT_READ |PROT_EXEC ); + ret =mprotect (buf, sz,PROT_READ |PROT_WRITE |PROT_EXEC );free (buf);return ret; }and the crash went away. Which made me realize that the free operation does not like memory without
PROT_WRITE, So I did this:tools/testing/selftests/bpf/prog_tests/test_lsm.c +1
-1@@ -30,7 +30,7 @@ int heap_mprotect(void)return -ENOMEM ; ret =mprotect (buf, sz,PROT_READ |PROT_EXEC ); -free (buf); +// free(buf); return ret; }and the crash went away as well. So it indeed was a combination of:
Quoted 6 lines
*
CONFIG_LSMnot enabling the hook
* mprotect marking the memory as non-writeable
* free being called on the memory.I will send a v9 which has the
PROT_WRITEon the mprotect. Thanks for noticing this!
And also explains the stack trace for __libc_calloc() where it's trying to zero the area later on.
Thanks for the quick debugging,
Daniel