MAC and Audit policy using eBPF (KRSI)
Subsystems (12)
v9 cover MAC and Audit policy using eBPF (KRSI)
From: KP Singh <[email protected]>
# v8 -> v9
https://lore.kernel.org/bpf/[email protected]/
* Fixed a selftest crash when CONFIG_LSM doesn't have "bpf".
* Added James' Ack.
* Rebase.
# 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 | 142 ++++ 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 | 54 ++ kernel/bpf/btf.c | 16 +- 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, 985 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 v9 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
@@ -1515,6 +1515,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 v9 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 v9 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 v9 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"
@@ -6492,8 +6493,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 ;
@@ -9923,7 +9925,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) {
@@ -10054,8 +10058,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);
@@ -10070,7 +10082,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
PATCH v9 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 v9 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,6 +243,9 @@ 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; bpf_set_link_xdp_fd_opts; } LIBBPF_0.0.7;
Signature · 1 line
2.20.1
PATCH v9 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
Re: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
tools/testing/selftests/bpf/progs/lsm.c line 29
Quoted 11 lines · KP Singh
tools/testing/selftests/bpf/progs/lsm.c new file +11
-0+int BPF_PROG (test_int_hook ,struct vm_area_struct *vma, +unsigned long reqprot,unsigned long prot,int ret) +{ +if (ret !=0 ) +return ret; + +__u32 pid =bpf_get_current_pid_tgid () >>32 ; +int is_heap =0 ; + + is_heap = (vma->vm_start >= vma->vm_mm->start_brk && + vma->vm_end <= vma->vm_mm->brk);
This test fails for me.
I've added:
bpf_printk("start %llx %llx\n", vma->vm_start, vma->vm_mm->start_brk);
bpf_printk("end %llx %llx\n", vma->vm_end, vma->vm_mm->brk);
and see
cat /sys/kernel/debug/tracing/trace_pipe
true-2285 [001] ...2 858.717432: 0: start 7f66470a2000 607000
true-2285 [001] ...2 858.717440: 0: end 7f6647443000 607000
true-2285 [001] ...2 858.717658: 0: start 7f6647439000 607000
true-2285 [001] ...2 858.717659: 0: end 7f664743f000 607000
true-2285 [001] ...2 858.717691: 0: start 605000 607000
true-2285 [001] ...2 858.717692: 0: end 607000 607000
true-2285 [001] ...2 858.717700: 0: start 7f6647666000 607000
true-2285 [001] ...2 858.717701: 0: end 7f6647668000 607000
test_progs-2283 [000] ...2 858.718030: 0: start 523000 39b9000
test_progs-2283 [000] ...2 858.718033: 0: end 39e0000 39e0000523000 is not >= 39b9000.
523000 is higher than vm_mm->end_data, but lower than vm_mm->start_brk. No idea why this addr is passed into security_file_mprotect(). The address user space is passing to mprotect() is 0x39c0000 which is correct. Could you please help debug?
Re: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 14 lines · Alexei Starovoitov
tools/testing/selftests/bpf/progs/lsm.c line 29
Quoted 11 lines · KP Singh
tools/testing/selftests/bpf/progs/lsm.c new file +11
-0+int BPF_PROG (test_int_hook ,struct vm_area_struct *vma, +unsigned long reqprot,unsigned long prot,int ret) +{ +if (ret !=0 ) +return ret; + +__u32 pid =bpf_get_current_pid_tgid () >>32 ; +int is_heap =0 ; + + is_heap = (vma->vm_start >= vma->vm_mm->start_brk && + vma->vm_end <= vma->vm_mm->brk);This test fails for me.
Trying this from bpf/master:
b9258a2cece4 ("slcan: Don't transmit uninitialized stack data in padding")
also from bpf-next/master:
1a323ea5356e ("x86: get rid of 'errret' argument to __get_user_xyz() macross")
and I am unable to reproduce the failure (the output when using bpf/master):
./test_progs -t test_lsm
#70 test_lsm:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
cat /sys/kernel/debug/tracing/trace
# tracer: nop
#
# entries-in-buffer/entries-written: 10/10 #P:4
#
# _-----=> irqs-off
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / delay
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
true-322 [001] ...2 187.127231: 0: start 7fc7ffccc000 556a623be000
true-322 [001] ...2 187.127238: 0: end 7fc7ffe8c000 556a623be000
true-322 [001] ...2 187.128233: 0: start 7fc7ffe82000 556a623be000
true-322 [001] ...2 187.128237: 0: end 7fc7ffe88000 556a623be000
true-322 [001] ...2 187.128306: 0: start 556a604f7000 556a623be000
true-322 [001] ...2 187.128309: 0: end 556a604f9000 556a623be000
true-322 [001] ...2 187.128372: 0: start 7fc7ffebf000 556a623be000
true-322 [001] ...2 187.128375: 0: end 7fc7ffec1000 556a623be000
test_progs-321 [000] ...2 187.129952: 0: start 55dc6e8df000 55dc6e8df000
test_progs-321 [000] ...2 187.129955: 0: end 55dc6e906000 55dc6e906000The full run also works for me:
./test_progs
[...]
#70 test_lsm:OK
#71 test_overhead:OK
#72 tp_attach_query:OK
My config is:
https://gist.githubusercontent.com/sinkap/cb24b955e1b6e6c1dc736054a774fb41/raw/
and the kernel commandline is (on a QEMU VM):
cat /proc/cmdline
console=ttyS0,115200 root=/dev/sda rw nokaslr
Could you share your config and cmdline?
Also, I am wondering if this happens just in the BPF program or also in the kernel as the other variable I can think of is the compiled bpf program itself which might be reading a different value thinking it's vm->vma_start, possible something to do with BTF / CO RE due to a compiler bug:
Here's the version of clang I am using:
clang version 10.0.0 (https://github.com/llvm/llvm-project.git 2026d7b80a1a5534b5e263683c85aa95e7593b98)
- KP
Quoted 21 lines
I've added:
bpf_printk("start %llx %llx\n", vma->vm_start, vma->vm_mm->start_brk); bpf_printk("end %llx %llx\n", vma->vm_end, vma->vm_mm->brk); and see cat /sys/kernel/debug/tracing/trace_pipe true-2285 [001] ...2 858.717432: 0: start 7f66470a2000 607000 true-2285 [001] ...2 858.717440: 0: end 7f6647443000 607000 true-2285 [001] ...2 858.717658: 0: start 7f6647439000 607000 true-2285 [001] ...2 858.717659: 0: end 7f664743f000 607000 true-2285 [001] ...2 858.717691: 0: start 605000 607000 true-2285 [001] ...2 858.717692: 0: end 607000 607000 true-2285 [001] ...2 858.717700: 0: start 7f6647666000 607000 true-2285 [001] ...2 858.717701: 0: end 7f6647668000 607000 test_progs-2283 [000] ...2 858.718030: 0: start 523000 39b9000 test_progs-2283 [000] ...2 858.718033: 0: end 39e0000 39e0000523000 is not >= 39b9000.
523000 is higher thanvm_mm->end_data, but lower thanvm_mm->start_brk. No idea why this addr is passed intosecurity_file_mprotect(). The address user space is passing tomprotect()is0x39c0000which is correct. Could you please help debug?
Re: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 25 lines · KP Singh
Quoted 14 lines · Alexei Starovoitov
Review
Quoted 11 lines · KP Singh
(quoted diff) +11
-0+int BPF_PROG(test_int_hook, struct vm_area_struct *vma, + unsigned long reqprot, unsigned long prot, int ret) +{ + if (ret != 0) + return ret; + + __u32 pid = bpf_get_current_pid_tgid() >> 32; + int is_heap = 0; + + is_heap = (vma->vm_start >= vma->vm_mm->start_brk && + vma->vm_end <= vma->vm_mm->brk);This test fails for me.
Trying this from bpf/master:
b9258a2cece4("slcan: Don't transmit uninitialized stack data in padding")also from bpf-next/master:
1a323ea5356e("x86: get rid of 'errret' argument to__get_user_xyz()macross")and I am unable to reproduce the failure (the output when using bpf/master):
..
Quoted 6 lines
Also, I am wondering if this happens just in the BPF program or also in the kernel as the other variable I can think of is the compiled bpf program itself which might be reading a different value thinking it's
vm->vma_start, possible something to do with BTF / CO RE due to a compiler bug:
I don't think it's anything to do with clang/btf or core. I think that condition is simply incorrect.
I've added:
mm/mprotect.c +1 -0
@@ -577,6 +577,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len, goto out; } +printk ("start %llx %llx\n" , vma->vm_start, vma->vm_mm->start_brk); error =security_file_mprotect (vma, reqprot, prot);
and see exactly the same values as bpf side (at least it was nice to see that all CO-RE logic is working as expected :))
[ 24.787442] start 523000 39b9000
I think it has something to do with the way test_progs is linked.
But the problem is in condition itself.
I suspect you copy-pasted it from selinux_file_mprotect() ?
I think it's incorrect there as well.
Did we just discover a way to side step selinux protection?
Try objdump -h test_progs|grep bss
the number I see in vma->vm_start is the beginning of .bss rounded to page boundary. I wonder where your 55dc6e8df000 is coming from.
Jann Horn
reviewerRe: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 53 lines · Alexei Starovoitov
Quoted 25 lines · KP Singh
Quoted 14 lines · Alexei Starovoitov
Review
Quoted 11 lines · KP Singh
(quoted diff) +11
-0+int BPF_PROG(test_int_hook, struct vm_area_struct *vma, + unsigned long reqprot, unsigned long prot, int ret) +{ + if (ret != 0) + return ret; + + __u32 pid = bpf_get_current_pid_tgid() >> 32; + int is_heap = 0; + + is_heap = (vma->vm_start >= vma->vm_mm->start_brk && + vma->vm_end <= vma->vm_mm->brk);This test fails for me.
Trying this from bpf/master:
b9258a2cece4("slcan: Don't transmit uninitialized stack data in padding")also from bpf-next/master:
1a323ea5356e("x86: get rid of 'errret' argument to__get_user_xyz()macross")and I am unable to reproduce the failure (the output when using bpf/master):
..
Quoted 6 lines
Also, I am wondering if this happens just in the BPF program or also in the kernel as the other variable I can think of is the compiled bpf program itself which might be reading a different value thinking it's
vm->vma_start, possible something to do with BTF / CO RE due to a compiler bug:I don't think it's anything to do with clang/btf or core. I think that condition is simply incorrect.
I've added:mm/mprotect.c +1
-0@@ -577,6 +577,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len, goto out; } +printk ("start %llx %llx\n" , vma->vm_start, vma->vm_mm->start_brk); error =security_file_mprotect (vma, reqprot, prot);and see exactly the same values as bpf side (at least it was nice to see that all CO-RE logic is working as expected :))
[ 24.787442] start 523000 39b9000
Quoted 8 lines
I think it has something to do with the way
test_progsis linked.
But the problem is in condition itself.
I suspect you copy-pasted it fromselinux_file_mprotect()?
I think it's incorrect there as well.
Did we just discover a way to side step selinux protection?
Try objdump -h test_progs|grep bss
the number I see invma->vm_startis the beginning of .bss rounded to page boundary. I wonder where your55dc6e8df000is coming from.
I suspect that you're using different versions of libc, or something's different in the memory layout, or something like that. The brk region is used for memory allocations using brk(), but memory allocations using mmap() land outside it. At least some versions of libc try to allocate memory for malloc() with brk(), then fall back to mmap() if that fails because there's something else behind the current end of the brk region; but I think there might also be versions of libc that directly use mmap() and don't even try to use brk().
So yeah, security checks based on the brk region aren't exactly useful; but e.g. in SELinux, both cases have appropriate checks. The brk region gets SECCLASS_PROCESS:PROCESS__EXECHEAP, anonymous mmap allocations get SECCLASS_PROCESS:PROCESS__EXECMEM in file_map_prot_check() instead. (This makes *some* amount of sense - although not a lot - because for the brk region you know that it comes from something like malloc(), while an anonymous mmap() allocation might reasonably be used for JIT executable memory.)
In other words, you may want to pick something different as test case, since the behavior here depends on libc.
Re: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Thanks Jann and Alexei,
Quoted 6 lines · Jann Horn
Quoted 4 lines · Alexei Starovoitov
Quoted 3 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
Quoted 2 lines
(quoted diff) +2
-0+ unsigned long reqprot, unsigned long prot, int ret) +{
[...]
Quoted 15 lines
Quoted 6 lines
and see exactly the same values as bpf side (at least it was nice to see that all CO-RE logic is working as expected :))
[ 24.787442] start 523000 39b9000Quoted 8 lines
I think it has something to do with the way
test_progsis linked.
But the problem is in condition itself.
I suspect you copy-pasted it fromselinux_file_mprotect()?
I think it's incorrect there as well.
Did we just discover a way to side step selinux protection?
Try objdump -h test_progs|grep bss
the number I see invma->vm_startis the beginning of .bss rounded to page boundary. I wonder where your55dc6e8df000is coming from.
Quoted 8 lines
I suspect that you're using different versions of libc, or something's different in the memory layout, or something like that. The brk region is used for memory allocations using
brk(), but memory allocations usingmmap()land outside it. At least some versions of libc try to allocate memory formalloc()withbrk(), then fall back tommap()if that fails because there's something else behind the current end of the brk region; but I think there might also be versions of libc that directly usemmap()and don't even try to usebrk().
Yeah missed this that heap can also be allocated using mmap:
Quoting the manual:
"""
Normally, malloc() allocates memory from the heap, and adjusts the
size of the heap as required, using sbrk(2). When allocating blocks
of memory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap(2). MMAP_THRESHOLD is 128 kB by default, but is adjustable using mallopt(3). Prior to Linux 4.7 allocations performed using mmap(2) were unaffected by the RLIMIT_DATA resource limit; since Linux 4.7, this limit is also enforced for allocations performed using mmap(2).
"""
So it seems like we might have separate MMAP_THRESHOLD or resource limits.
I updated my test case to check for mmaps on the stack instead:
tools/testing/selftests/bpf/prog_tests/test_lsm.c +11 -9
@@ -15,8 +15,13 @@char *CMD_ARGS [] = {"true" ,NULL }; -int heap_mprotect (void ) +#define PAGE_SIZE 4096 +#define GET_PAGE_ADDR (ADDR ) \ + (char *)(((unsigned long) ADDR) & ~(PAGE_SIZE-1)) + +int stack_mprotect (void ) { +void *buf;long sz;int ret;
@@ -25,12 +30,9 @@ int heap_mprotect(void)if (sz <0 )return sz; - buf =memalign (sz,2 * sz); -if (buf ==NULL ) -return -ENOMEM ; - - ret =mprotect (buf, sz,PROT_READ |PROT_WRITE |PROT_EXEC ); -free (buf); + buf =alloca (PAGE_SIZE *3 ); + ret =mprotect (GET_PAGE_ADDR (buf +PAGE_SIZE ),PAGE_SIZE , +PROT_READ |PROT_WRITE |PROT_EXEC );return ret; }
@@ -73,8 +75,8 @@ void test_test_lsm(void) skel->bss->monitored_pid =getpid (); - err =heap_mprotect (); -if (CHECK (errno !=EPERM ,"heap_mprotect" ,"want errno=EPERM, got %d\n" , + err =stack_mprotect (); +if (CHECK (errno !=EPERM ,"stack_mprotect" ,"want err=EPERM, got %d\n" , errno)) goto close_prog;
tools/testing/selftests/bpf/progs/lsm.c +4 -4
@@ -23,12 +23,12 @@ int BPF_PROG(test_int_hook, struct vm_area_struct *vma,return ret;__u32 pid =bpf_get_current_pid_tgid () >>32 ; -int is_heap =0 ; +int is_stack =0 ; - is_heap = (vma->vm_start >= vma->vm_mm->start_brk && - vma->vm_end <= vma->vm_mm->brk); + is_stack = (vma->vm_start <= vma->vm_mm->start_stack && + vma->vm_end >= vma->vm_mm->start_stack); -if (is_heap && monitored_pid == pid) { +if (is_stack && monitored_pid == pid) { mprotect_count++; ret = -EPERM ; }
and the the logic seems to work for me. Do you think we could use this instead?
- KP
Quoted 12 lines
So yeah, security checks based on the brk region aren't exactly useful; but e.g. in SELinux, both cases have appropriate checks. The brk region gets SECCLASS_PROCESS:PROCESS__EXECHEAP, anonymous mmap allocations get SECCLASS_PROCESS:PROCESS__EXECMEM in
file_map_prot_check()instead. (This makes *some* amount of sense - although not a lot - because for the brk region you know that it comes from something likemalloc(), while an anonymousmmap()allocation might reasonably be used for JIT executable memory.)In other words, you may want to pick something different as test case, since the behavior here depends on libc.
Jann Horn
reviewerRe: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 1 line · KP Singh
On 02-Apr 07:15, Jann Horn wrote:
[...]
Quoted 10 lines
Quoted 8 lines
I suspect that you're using different versions of libc, or something's different in the memory layout, or something like that. The brk region is used for memory allocations using
brk(), but memory allocations usingmmap()land outside it. At least some versions of libc try to allocate memory formalloc()withbrk(), then fall back tommap()if that fails because there's something else behind the current end of the brk region; but I think there might also be versions of libc that directly usemmap()and don't even try to usebrk().Yeah missed this that heap can also be allocated using mmap:
[...]
Quoted 1 line
I updated my test case to check for mmaps on the stack instead:
[...]
Quoted 11 lines
tools/testing/selftests/bpf/progs/lsm.c +3
-1+ is_stack = (vma->vm_start <= vma->vm_mm->start_stack && + vma->vm_end >= vma->vm_mm->start_stack); -if (is_heap && monitored_pid == pid) { +if (is_stack && monitored_pid == pid) { mprotect_count++; ret = -EPERM ; }and the the logic seems to work for me. Do you think we could use this instead?
Yeah, I think that should work. (Just keep in mind that a successful mprotect() operation will split the VMA into three VMAs - but that shouldn't be a problem here, since you only do it once per process, and since you're denying the operation, it won't go through anyway.)
Re: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 26 lines · Jann Horn
Quoted 1 line · KP Singh
On 02-Apr 07:15, Jann Horn wrote:
[...]
Quoted 10 lines
Quoted 8 lines
I suspect that you're using different versions of libc, or something's different in the memory layout, or something like that. The brk region is used for memory allocations using
brk(), but memory allocations usingmmap()land outside it. At least some versions of libc try to allocate memory formalloc()withbrk(), then fall back tommap()if that fails because there's something else behind the current end of the brk region; but I think there might also be versions of libc that directly usemmap()and don't even try to usebrk().Yeah missed this that heap can also be allocated using mmap:
[...]
Quoted 1 line
I updated my test case to check for mmaps on the stack instead:
[...]
Quoted 9 lines
tools/testing/selftests/bpf/progs/lsm.c +3
-1+ is_stack = (vma->vm_start <= vma->vm_mm->start_stack && + vma->vm_end >= vma->vm_mm->start_stack); -if (is_heap && monitored_pid == pid) { +if (is_stack && monitored_pid == pid) { mprotect_count++; ret = -EPERM ; }
Quoted 7 lines
Quoted 2 lines
and the the logic seems to work for me. Do you think we could use this instead?
Yeah, I think that should work. (Just keep in mind that a successful
mprotect()operation will split the VMA into three VMAs - but that shouldn't be a problem here, since you only do it once per process, and since you're denying the operation, it won't go through anyway.)
Okay I will send a patch that fixes this test. Thanks everyone!
- KP
Re: [PATCH bpf-next v9 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
In reply to KP SinghQuoted 15 lines · KP Singh
tools/testing/selftests/bpf/prog_tests/test_lsm.c +6
-1-int heap_mprotect (void ) +#define PAGE_SIZE 4096 +#define GET_PAGE_ADDR (ADDR ) \ + (char *)(((unsigned long) ADDR) & ~(PAGE_SIZE-1)) + +int stack_mprotect (void ) { +void *buf;long sz;int ret;@@ -25,12 +30,9 @@ int heap_mprotect(void)if (sz <0 )return sz;
tools/testing/selftests/bpf/prog_tests/test_lsm.c int heap_mprotect(void) · line 35
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c +3
-6- buf =memalign (sz,2 * sz); -if (buf ==NULL ) -return -ENOMEM ; - - ret =mprotect (buf, sz,PROT_READ |PROT_WRITE |PROT_EXEC ); -free (buf); + buf =alloca (PAGE_SIZE *3 ); + ret =mprotect (GET_PAGE_ADDR (buf +PAGE_SIZE ),PAGE_SIZE , +PROT_READ |PROT_WRITE |PROT_EXEC );
Great. Something like this should work.
Please use sysconf(_SC_PAGE_SIZE); like prog_tests/mmap.c does. selftests/bpf are run not only on x86
PATCH v9 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
Quoted 9 lines · KP Singh
From: KP Singh <[email protected]>
# v8 -> v9https://lore.kernel.org/bpf/[email protected]/* Fixed a selftest crash when
CONFIG_LSMdoesn't have "bpf".
* Added James' Ack.
* Rebase.
Applied, thanks for all the hard work!
Hi,
Quoted 11 lines · KP Singh
# How does it work?
The patchset introduces a new eBPF (https://docs.cilium.io/en/v1.6/bpf/)
program typeBPF_PROG_TYPE_LSMwhich can only be attached to LSM hooks. Loading and attachment of BPF programs requiresCAP_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_RETURNtrampoline programs for hooks that return an int, andBPF_TRAMP_FEXITtrampoline programs for void LSM hooks.
I have two systems (a NUC and a qemu VM) that fail to boot if I enable the BPF LSM without enabling SELinux first. Anything I might be missing or are you able to trigger it too?
For instance, the following additional cmdline args: "lsm.debug=1 lsm="capability,apparmor,bpf" results in:
[ 1.251889] Call Trace: [ 1.252344] dump_stack+0x57/0x7a [ 1.252951] panic+0xe6/0x2a4 [ 1.253497] ? printk+0x43/0x45 [ 1.254075] mount_block_root+0x30c/0x31b [ 1.254798] mount_root+0x78/0x7b [ 1.255417] prepare_namespace+0x13a/0x16b [ 1.256168] kernel_init_freeable+0x210/0x222 [ 1.257021] ? rest_init+0xa5/0xa5 [ 1.257639] kernel_init+0x9/0xfb [ 1.258074] ret_from_fork+0x35/0x40 [ 1.258885] Kernel Offset: 0x11000000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff) [ 1.264046] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(253,3)
Taking out "bpf" or adding "selinux" before it boots OK. I've tried with both 5.7-rc2 and -rc3.
LSM logs:
[ 0.267219] LSM: Security Framework initializing [ 0.267844] LSM: first ordering: capability (enabled) [ 0.267870] LSM: cmdline ignored: capability [ 0.268869] LSM: cmdline ordering: apparmor (enabled) [ 0.269508] LSM: cmdline ordering: bpf (enabled) [ 0.269869] LSM: cmdline disabled: selinux [ 0.270377] LSM: cmdline disabled: integrity [ 0.270869] LSM: exclusive chosen: apparmor [ 0.271869] LSM: cred blob size = 8 [ 0.272354] LSM: file blob size = 24 [ 0.272869] LSM: inode blob size = 0 [ 0.273362] LSM: ipc blob size = 0 [ 0.273869] LSM: msg_msg blob size = 0 [ 0.274352] LSM: task blob size = 32 [ 0.274873] LSM: initializing capability [ 0.275381] LSM: initializing apparmor [ 0.275880] AppArmor: AppArmor initialized [ 0.276437] LSM: initializing bpf [ 0.276871] LSM support for eBPF active
-- Regards, Mikko
Thanks for reporting this! Can you share your Kconfig please?
Quoted 65 lines · Mikko Ylinen
Hi,
Quoted 11 lines · KP Singh
# How does it work?
The patchset introduces a new eBPF (https://docs.cilium.io/en/v1.6/bpf/)
program typeBPF_PROG_TYPE_LSMwhich can only be attached to LSM hooks. Loading and attachment of BPF programs requiresCAP_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_RETURNtrampoline programs for hooks that return an int, andBPF_TRAMP_FEXITtrampoline programs for void LSM hooks.I have two systems (a NUC and a qemu VM) that fail to boot if I enable the BPF LSM without enabling SELinux first. Anything I might be missing or are you able to trigger it too?
For instance, the following additional cmdline args: "lsm.debug=1 lsm="capability,apparmor,bpf" results in:
[ 1.251889] Call Trace: [ 1.252344] dump_stack+0x57/0x7a [ 1.252951] panic+0xe6/0x2a4 [ 1.253497] ? printk+0x43/0x45 [ 1.254075] mount_block_root+0x30c/0x31b [ 1.254798] mount_root+0x78/0x7b [ 1.255417] prepare_namespace+0x13a/0x16b [ 1.256168] kernel_init_freeable+0x210/0x222 [ 1.257021] ? rest_init+0xa5/0xa5 [ 1.257639] kernel_init+0x9/0xfb [ 1.258074] ret_from_fork+0x35/0x40 [ 1.258885] Kernel Offset: 0x11000000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff) [ 1.264046] ---[ end Kernel panic - not syncing: VFS: Unable to mountroot fs on unknown-block(253,3)
Taking out "bpf" or adding "selinux" before it boots OK. I've tried with both 5.7-rc2 and -rc3.
LSM logs:
[ 0.267219] LSM: Security Framework initializing [ 0.267844] LSM: first ordering: capability (enabled) [ 0.267870] LSM: cmdline ignored: capability [ 0.268869] LSM: cmdline ordering: apparmor (enabled) [ 0.269508] LSM: cmdline ordering: bpf (enabled) [ 0.269869] LSM: cmdline disabled: selinux [ 0.270377] LSM: cmdline disabled: integrity [ 0.270869] LSM: exclusive chosen: apparmor [ 0.271869] LSM: cred blob size = 8 [ 0.272354] LSM: file blob size = 24 [ 0.272869] LSM: inode blob size = 0 [ 0.273362] LSM: ipc blob size = 0 [ 0.273869] LSM: msg_msg blob size = 0 [ 0.274352] LSM: task blob size = 32 [ 0.274873] LSM: initializing capability [ 0.275381] LSM: initializing apparmor [ 0.275880] AppArmor: AppArmor initialized [ 0.276437] LSM: initializing bpf [ 0.276871] LSM support for eBPF active-- Regards, Mikko
Quoted 1 line · KP Singh
Thanks for reporting this! Can you share your
Kconfigplease?
This is what I originally started with
https://raw.githubusercontent.com/clearlinux-pkgs/linux-mainline/master/config
but I also tried your _LSM_ settings found in this
https://lore.kernel.org/bpf/[email protected]/
-- Regards, Mikko
So I was able to reproduce the issue and also fix it (will separately send a patch).
include/linux/lsm_hook_defs.h +1 -1
@@ -55,7 +55,7 @@ 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, +LSM_HOOK(int, -ENOPARAM, 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)
So what was happening was that:
bpf_lsm hook for fs_context_parse_param was returning 0 which led this bit of logic to believe the parameter was parsed by the LSM.
int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
{
[...]
ret = security_fs_context_parse_param(fc, param);
if (ret != -ENOPARAM)
/* Param belongs to the LSM or is disallowed by the
* LSM; so
* don't pass to the FS.
*/
return ret;
if (fc->ops->parse_param) {
ret = fc->ops->parse_param(fc, param);
if (ret != -ENOPARAM)
return ret;
}[...]
This resulted in the fs_context->dev_name being NULL and the following chain to throw an -EINVAL resulting in unsuccessful mount of the root file-system:
- do_mount_root -> do_mount -> do_new_mount -> vfs_get_tree -> -> fc->ops->get_tree -> legacy->get_tree -> fc->fs_type->mount -> ext4_mount -> mount_bdev -> blkdev_get_by_path -> lookup_bdev
- KP
Quoted 12 lines · Mikko Ylinen
Quoted 1 line · KP Singh
Thanks for reporting this! Can you share your
Kconfigplease?This is what I originally started with
https://raw.githubusercontent.com/clearlinux-pkgs/linux-mainline/master/configbut I also tried your _LSM_ settings found in this
https://lore.kernel.org/bpf/[email protected]/-- Regards, Mikko