MAC and Audit policy using eBPF (KRSI)
Subsystems (10)
v7 cover MAC and Audit policy using eBPF (KRSI)
From: KP Singh <[email protected]>
# 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 | 150 +++++ 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, 991 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 v7 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
Re: [PATCH bpf-next v7 1/8] bpf: Introduce BPF_PROG_TYPE_LSM
Quoted 6 lines · KP Singh
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.
Quoted 6 lines
Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>Acked-by Yonghong Song <[email protected]>Acked-by Andrii Nakryiko <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v7 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
Re: [PATCH bpf-next v7 2/8] security: Refactor declaration of LSM hooks
Quoted 15 lines · KP Singh
From: KP Singh <[email protected]>
The information about the different types of LSM hooks is scattered in two locations i.e. union
security_list_optionsand structsecurity_hook_heads. Rather than duplicating this information even further forBPF_PROG_TYPE_LSM, define all the hooks with theLSM_HOOKmacro inlsm_hook_defs.hwhich 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 insecurity.cas:
Quoted 7 lines
LSM_RET_DEFAULT(<hook_name>)Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Kees Cook <[email protected]>Reviewed-by Casey Schaufler <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v7 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
Re: [PATCH bpf-next v7 3/8] bpf: lsm: provide attachment points for BPF LSM programs
Quoted 11 lines · KP Singh
From: KP Singh <[email protected]>
When
CONFIG_BPF_LSMis enabled, nop functions, bpf_lsm_<hook_name>, are generated for each LSM hook. These functions are initialized as LSM hooks in a subsequent patch.Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Kees Cook <[email protected]>Acked-by Yonghong Song <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v7 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 | 28 ++++++++++++++++++++ kernel/bpf/btf.c | 9 ++++++- kernel/bpf/syscall.c | 57 ++++++++++++++++++++++++++++------------- kernel/bpf/trampoline.c | 17 +++++++++--- kernel/bpf/verifier.c | 19 +++++++++++--- 6 files changed, 114 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 +28 -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,32 @@ 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) +{ +/* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks +*/ +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; + +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 +8 -1
@@ -3710,7 +3710,14 @@ 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 ) { +/* BPF_LSM_MAC programs only have int and void functions they +* can be attached to. When they are attached to a void function +* they result in the creation of an FEXIT trampoline and when +* to a function that returns an int, a MODIFY_RETURN +* trampoline. +*/ +if (prog->expected_attach_type ==BPF_TRACE_FEXIT || + prog->expected_attach_type ==BPF_LSM_MAC ) {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 v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 20 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]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>---
Quoted 8 lines
include/linux/bpf_lsm.h | 11 ++++++++ kernel/bpf/bpf_lsm.c | 28 ++++++++++++++++++++ kernel/bpf/btf.c | 9 ++++++- kernel/bpf/syscall.c | 57 ++++++++++++++++++++++++++++------------- kernel/bpf/trampoline.c | 17 +++++++++--- kernel/bpf/verifier.c | 19 +++++++++++--- 6 files changed, 114 insertions(+), 27 deletions(-)
[...]
kernel/bpf/syscall.c static int bpf_raw_tracepoint_open(const union bpf_attr *attr) · line 2503
Quoted 9 lines
kernel/bpf/syscall.c +4
-0@@ -2479,6 +2496,10 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr) } buf[sizeof (buf) -1 ] =0 ; tp_name = buf; +break ; +default : + err = -EINVAL ; + goto out_put_prog; }
is indentation off here or it's my email client?
[...]
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 40 lines · Andrii Nakryiko
Quoted 20 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]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>---
Acked-by Andrii Nakryiko <[email protected]>Quoted 8 lines
include/linux/bpf_lsm.h | 11 ++++++++ kernel/bpf/bpf_lsm.c | 28 ++++++++++++++++++++ kernel/bpf/btf.c | 9 ++++++- kernel/bpf/syscall.c | 57 ++++++++++++++++++++++++++++------------- kernel/bpf/trampoline.c | 17 +++++++++--- kernel/bpf/verifier.c | 19 +++++++++++--- 6 files changed, 114 insertions(+), 27 deletions(-)[...]
Quoted 11 lines
kernel/bpf/syscall.c static int bpf_raw_tracepoint_open(const union bpf_attr *attr) · line 2503
Quoted 9 lines
kernel/bpf/syscall.c +4
-0@@ -2479,6 +2496,10 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr) } buf[sizeof (buf) -1 ] =0 ; tp_name = buf; +break ; +default : + err = -EINVAL ; + goto out_put_prog; }is indentation off here or it's my email client?
You're mail client is fine :) It's me.
- KP
Quoted 2 lines
[...]
James Morris
maintainerRe: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
In reply to KP Singhkernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +/* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks +*/ +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; +
Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a bpf_prog callback) ?
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 13 lines · James Morris
kernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +/* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks +*/ +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; +Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a
bpf_progcallback) ?
I guess the only residual concern I have is that CAP_MAC_ADMIN means something different to SELinux (ability to get/set file security contexts unknown to the currently loaded policy), so leaving the CAP_MAC_ADMIN check here (versus calling a new security hook here and checking CAP_MAC_ADMIN in the implementation of that hook for the modules that want that) conflates two very different things. Prior to this patch, there are no users of CAP_MAC_ADMIN outside of individual security modules; it is only checked in module-specific logic within apparmor, safesetid, selinux, and smack, so the meaning was module-specific.
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · James Morris
kernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +/* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks +*/ +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; +Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a
bpf_progcallback) ?
Quoted 9 lines
I guess the only residual concern I have is that
CAP_MAC_ADMINmeans something different to SELinux (ability to get/set file security contexts unknown to the currently loaded policy), so leaving theCAP_MAC_ADMINcheck here (versus calling a new security hook here and checkingCAP_MAC_ADMINin the implementation of that hook for the modules that want that) conflates two very different things. Prior to this patch, there are no users ofCAP_MAC_ADMINoutside of individual security modules; it is only checked in module-specific logic within apparmor, safesetid, selinux, and smack, so the meaning was module-specific.
As we had discussed, We do have a security hook as well:
https://lore.kernel.org/bpf/[email protected]/
The bpf_prog hook which can check for BPF_PROG_TYPE_LSM and implement module specific logic for LSM programs. I thougt that was okay?
Kees was in favor of keeping the CAP_MAC_ADMIN check here:
https://lore.kernel.org/bpf/202003241133.16C02BE5B@keescook
If you feel strongly and Kees agrees, we can remove the CAP_MAC_ADMIN check here, but given that we already have a security hook that meets the requirements, we probably don't need another one.
- KP
Quoted 4 lines
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 33 lines · KP Singh
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · James Morris
kernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +/* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks +*/ +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; +Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a
bpf_progcallback) ?Quoted 9 lines
I guess the only residual concern I have is that
CAP_MAC_ADMINmeans something different to SELinux (ability to get/set file security contexts unknown to the currently loaded policy), so leaving theCAP_MAC_ADMINcheck here (versus calling a new security hook here and checkingCAP_MAC_ADMINin the implementation of that hook for the modules that want that) conflates two very different things. Prior to this patch, there are no users ofCAP_MAC_ADMINoutside of individual security modules; it is only checked in module-specific logic within apparmor, safesetid, selinux, and smack, so the meaning was module-specific.As we had discussed, We do have a security hook as well:
https://lore.kernel.org/bpf/[email protected]/
The
bpf_proghook which can check forBPF_PROG_TYPE_LSMand implement module specific logic for LSM programs. I thougt that was okay?
Quoted 7 lines
Kees was in favor of keeping the
CAP_MAC_ADMINcheck here:https://lore.kernel.org/bpf/202003241133.16C02BE5B@keescook
If you feel strongly and Kees agrees, we can remove the
CAP_MAC_ADMINcheck here, but given that we already have a security hook that meets the requirements, we probably don't need another one.
I would favor removing the CAP_MAC_ADMIN check here, and implementing it in a bpf_prog hook for Smack and AppArmor if they want that. SELinux would implement its own check in its existing bpf_prog hook.
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 36 lines · Stephen Smalley
Quoted 35 lines · KP Singh
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · James Morris
kernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, +const struct bpf_prog *prog) +{ +/* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks +*/ +if (!capable (CAP_MAC_ADMIN )) +return -EPERM ; +Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a
bpf_progcallback) ?Quoted 9 lines
I guess the only residual concern I have is that
CAP_MAC_ADMINmeans something different to SELinux (ability to get/set file security contexts unknown to the currently loaded policy), so leaving theCAP_MAC_ADMINcheck here (versus calling a new security hook here and checkingCAP_MAC_ADMINin the implementation of that hook for the modules that want that) conflates two very different things. Prior to this patch, there are no users ofCAP_MAC_ADMINoutside of individual security modules; it is only checked in module-specific logic within apparmor, safesetid, selinux, and smack, so the meaning was module-specific.As we had discussed, We do have a security hook as well:
https://lore.kernel.org/bpf/[email protected]/
The
bpf_proghook which can check forBPF_PROG_TYPE_LSMand implement module specific logic for LSM programs. I thougt that was okay?Kees was in favor of keeping the
CAP_MAC_ADMINcheck here:
Quoted 7 lines
Quoted 5 lines
https://lore.kernel.org/bpf/202003241133.16C02BE5B@keescook
If you feel strongly and Kees agrees, we can remove the
CAP_MAC_ADMINcheck here, but given that we already have a security hook that meets the requirements, we probably don't need another one.I would favor removing the
CAP_MAC_ADMINcheck here, and implementing it in
Okay. For the scope of this series I will remove this check in the next revision. If people feel strongly that we need it centrally within the BPF infrastructure, we can do that as a separate patch and discuss it there.
Quoted 2 lines
a
bpf_proghook for Smack and AppArmor if they want that. SELinux would implement its own check in its existingbpf_proghook.
I think Smack and AppArmor can also use the same hook. Since we already have a hook, I don't think anyone is blocked from implementing policy logic for loading LSM BPF programs.
James/Kees does this sound okay?
- KP
Quoted 3 lines
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
In reply to Stephen SmalleyQuoted 36 lines · Stephen Smalley
Quoted 35 lines · KP Singh
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · James Morris
kernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, + const struct bpf_prog *prog) +{ + /* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks + */ + if (!capable(CAP_MAC_ADMIN)) + return -EPERM; +Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a
bpf_progcallback) ?Quoted 9 lines
I guess the only residual concern I have is that
CAP_MAC_ADMINmeans something different to SELinux (ability to get/set file security contexts unknown to the currently loaded policy), so leaving theCAP_MAC_ADMINcheck here (versus calling a new security hook here and checkingCAP_MAC_ADMINin the implementation of that hook for the modules that want that) conflates two very different things. Prior to this patch, there are no users ofCAP_MAC_ADMINoutside of individual security modules; it is only checked in module-specific logic within apparmor, safesetid, selinux, and smack, so the meaning was module-specific.As we had discussed, We do have a security hook as well:
https://lore.kernel.org/bpf/[email protected]/
The
bpf_proghook which can check forBPF_PROG_TYPE_LSMand implement module specific logic for LSM programs. I thougt that was okay?Kees was in favor of keeping the
CAP_MAC_ADMINcheck here:
Quoted 8 lines
Quoted 5 lines
https://lore.kernel.org/bpf/202003241133.16C02BE5B@keescook
If you feel strongly and Kees agrees, we can remove the
CAP_MAC_ADMINcheck here, but given that we already have a security hook that meets the requirements, we probably don't need another one.I would favor removing the
CAP_MAC_ADMINcheck here, and implementing it in abpf_proghook for Smack and AppArmor if they want that. SELinux would implement its own check in its existingbpf_proghook.
The whole notion of one security module calling into another for permission to do something still gives me the heebee jeebees, but if more nimble minds than mine think this is a good idea I won't nack it.
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 43 lines · Casey Schaufler
Quoted 42 lines · Stephen Smalley
Quoted 40 lines · KP Singh
Quoted 15 lines · Stephen Smalley
Quoted 13 lines · James Morris
kernel/bpf/bpf_lsm.c noinline RET bpf_lsm_##NAME(__VA_ARGS__) \ · line 36
Quoted 8 lines · KP Singh
kernel/bpf/bpf_lsm.c +8
-0+int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog, + const struct bpf_prog *prog) +{ + /* Only CAP_MAC_ADMIN users are allowed to make changes to LSM hooks + */ + if (!capable(CAP_MAC_ADMIN)) + return -EPERM; +Stephen, can you confirm that your concerns around this are resolved (IIRC, by SELinux implementing a
bpf_progcallback) ?Quoted 9 lines
I guess the only residual concern I have is that
CAP_MAC_ADMINmeans something different to SELinux (ability to get/set file security contexts unknown to the currently loaded policy), so leaving theCAP_MAC_ADMINcheck here (versus calling a new security hook here and checkingCAP_MAC_ADMINin the implementation of that hook for the modules that want that) conflates two very different things. Prior to this patch, there are no users ofCAP_MAC_ADMINoutside of individual security modules; it is only checked in module-specific logic within apparmor, safesetid, selinux, and smack, so the meaning was module-specific.As we had discussed, We do have a security hook as well:
https://lore.kernel.org/bpf/[email protected]/
The
bpf_proghook which can check forBPF_PROG_TYPE_LSMand implement module specific logic for LSM programs. I thougt that was okay?Kees was in favor of keeping the
CAP_MAC_ADMINcheck here:https://lore.kernel.org/bpf/202003241133.16C02BE5B@keescook
If you feel strongly and Kees agrees, we can remove the
CAP_MAC_ADMINcheck here, but given that we already have a security hook that meets the requirements, we probably don't need another one.
Quoted 5 lines
Quoted 2 lines
I would favor removing the
CAP_MAC_ADMINcheck here, and implementing it in abpf_proghook for Smack and AppArmor if they want that. SELinux would implement its own check in its existingbpf_proghook.The whole notion of one security module calling into another for permission to do something still gives me the heebee jeebees, but if more nimble minds than mine think this is a good idea I won't nack it.
Well, it's a hook into BPF prog creation, not the BPF LSM specifically, so that's why I think it's general enough control without it being directly weird. :)
As far as dropping CAP_MAC_ADMIN, yeah, that should be fine. Creating LSM BPF programs already requires CAP_SYS_ADMIN, so for SELinux-less systems, that's likely fine. If we need to change the BPF program creation access control in the future we can revisit it then.
Signature · 1 line
Kees Cook
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 5 lines · Kees Cook
Quoted 4 lines · Casey Schaufler
Quoted 3 lines · Stephen Smalley
Quoted 2 lines · KP Singh
Quoted 1 line · Stephen Smalley
Quoted 1 line
Quoted 1 line
+ return
-EPERM;
[...]
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
I would favor removing the
CAP_MAC_ADMINcheck here, and implementing it in abpf_proghook for Smack and AppArmor if they want that. SELinux would implement its own check in its existingbpf_proghook.The whole notion of one security module calling into another for permission to do something still gives me the heebee jeebees, but if more nimble minds than mine think this is a good idea I won't nack it.
Quoted 8 lines
Well, it's a hook into BPF prog creation, not the BPF LSM specifically, so that's why I think it's general enough control without it being directly weird. :)
As far as dropping
CAP_MAC_ADMIN, yeah, that should be fine. Creating LSM BPF programs already requiresCAP_SYS_ADMIN, so for SELinux-less systems, that's likely fine. If we need to change the BPF program creation access control in the future we can revisit it then.
Sounds good, I will send out v8 carrying James and Andri's Acks/Review tags, CAP_MAC_ADMIN check removed and some other minor fixes.
- KP
Quoted 3 lines
Signature · 2 lines
-- Kees Cook
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
In reply to KP SinghQuoted 4 lines · KP Singh
kernel/bpf/btf.c +1
-1if (arg == nr_args) { -if (prog->expected_attach_type ==BPF_TRACE_FEXIT ) { +/* BPF_LSM_MAC programs only have int and void functions they
kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 3723
Quoted 10 lines
kernel/bpf/btf.c +7
-0+* can be attached to. When they are attached to a void function +* they result in the creation of an FEXIT trampoline and when +* to a function that returns an int, a MODIFY_RETURN +* trampoline. +*/ +if (prog->expected_attach_type ==BPF_TRACE_FEXIT || + prog->expected_attach_type ==BPF_LSM_MAC ) {if (!t)return true; t =btf_type_by_id (btf, t->type);
Could you add a comment here that though BPF_MODIFY_RETURN-like check if (ret_type != 'int') return -EINVAL;
is _not_ done here. It is still safe, since LSM hooks have only void and int return types.
kernel/bpf/trampoline.c static int bpf_trampoline_update(struct bpf_trampoline *tr) · line 253
Quoted 8 lines
kernel/bpf/trampoline.c +8
-0+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 ;
I was thinking whether it would help performance significantly enough if we add a flavor of BPF_TRAMP_FEXIT that doesn't have BPF_TRAMP_F_CALL_ORIG.
That will save the cost of nop call, but I guess indirect call due to lsm infra is slow enough, so this extra few cycles won't be noticeable. So I'm fine with it as-is. When lsm hooks will get rid of indirect call we can optimize it further.
Re: [PATCH bpf-next v7 4/8] bpf: lsm: Implement attach, detach and execution
Quoted 12 lines · Alexei Starovoitov
Quoted 11 lines · KP Singh
kernel/bpf/btf.c +8
-1if (arg == nr_args) { -if (prog->expected_attach_type ==BPF_TRACE_FEXIT ) { +/* BPF_LSM_MAC programs only have int and void functions they +* can be attached to. When they are attached to a void function +* they result in the creation of an FEXIT trampoline and when +* to a function that returns an int, a MODIFY_RETURN +* trampoline. +*/ +if (prog->expected_attach_type ==BPF_TRACE_FEXIT || + prog->expected_attach_type ==BPF_LSM_MAC ) {
Quoted 8 lines
kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 3723
Quoted 3 lines
kernel/bpf/btf.c +0
-0if (!t)return true; t =btf_type_by_id (btf, t->type);Could you add a comment here that though BPF_MODIFY_RETURN-like check if (
ret_type!= 'int') return-EINVAL;
is _not_ done here. It is still safe, since LSM hooks have only void and int return types.
Good idea, I reworded the comment to make this explicit and moved the comment to inside the if condition.
Quoted 13 lines
kernel/bpf/trampoline.c static int bpf_trampoline_update(struct bpf_trampoline *tr) · line 253
Quoted 9 lines
kernel/bpf/trampoline.c +8
-0+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 ;I was thinking whether it would help performance significantly enough if we add a flavor of
BPF_TRAMP_FEXITthat doesn't haveBPF_TRAMP_F_CALL_ORIG.
Agreed.
Quoted 4 lines
That will save the cost of nop call, but I guess indirect call due to lsm infra is slow enough, so this extra few cycles won't be noticeable. So I'm fine with it as-is. When lsm hooks will get rid of indirect call we can optimize it further.
Also agreed, that's the next step. :)
- KP
PATCH v7 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
Re: [PATCH bpf-next v7 5/8] bpf: lsm: Initialize the BPF LSM hooks
Quoted 10 lines · KP Singh
From: KP Singh <[email protected]>
* The hooks are initialized using the definitions in
include/linux/lsm_hook_defs.h.
* The LSM can be enabled / disabled withCONFIG_BPF_LSM.Signed-off-by KP Singh <[email protected]>Acked-by Kees Cook <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>
Quoted 8 lines
--- 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
Signature · 2 lines
James Morris <[email protected]>
PATCH v7 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
Re: [PATCH bpf-next v7 6/8] tools/libbpf: Add support for BPF_PROG_TYPE_LSM
Quoted 6 lines · KP Singh
From: KP Singh <[email protected]>
Since
BPF_PROG_TYPE_LSMuses the same attaching mechanism asBPF_PROG_TYPE_TRACING, the common logic is refactored into a static functionbpf_program__attach_btf_id.
Quoted 8 lines
A new API call
bpf_program__attach_lsmis still added to avoid userspace conflicts if this ever changes in the future.Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Acked-by Yonghong Song <[email protected]>Acked-by Andrii Nakryiko <[email protected]>
Signature · 2 lines
James Morris <[email protected]>
PATCH v7 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 v7 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 10 lines · KP Singh
From: KP Singh <[email protected]>
* Load/attach a BPF program that hooks to
file_mprotect(int) andbprm_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.
Quoted 5 lines
Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>---
Please fix endlines below. With that:
Quoted 7 lines
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
[...]
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +8
-0+void test_test_lsm (void ) +{ +struct lsm *skel =NULL ; +int err, duration =0 ; + + skel =lsm__open_and_load (); +if (CHECK (!skel,"skel_load" ,"lsm skeleton failed\n" )) + goto close_prog;
tools/testing/selftests/bpf/prog_tests/test_lsm.c line 71
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +10
-0+ + err =lsm__attach (skel); +if (CHECK (err,"attach" ,"lsm attach failed: %d\n" , err)) + goto close_prog; + + err =exec_cmd (&skel->bss->monitored_pid); +if (CHECK (err <0 ,"exec_cmd" ,"err %d errno %d\n" , err, errno)) + goto close_prog; + + CHECK(skel->bss->bprm_count != 1, "bprm_count", "bprm_count = %d",
\n is missing
tools/testing/selftests/bpf/prog_tests/test_lsm.c line 82
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +11
-0+ skel->bss->bprm_count); + + skel->bss->monitored_pid =getpid (); + + err =heap_mprotect (); +if (CHECK (errno !=EPERM ,"heap_mprotect" ,"want errno=EPERM, got %d\n" , + errno)) + goto close_prog; + +CHECK (skel->bss->mprotect_count !=1 ,"mprotect_count" , +"mprotect_count = %d" , skel->bss->mprotect_count);
\n is missing
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +4
-0+ +close_prog: +lsm__destroy (skel); +}
[...]
Re: [PATCH bpf-next v7 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
Quoted 43 lines · Andrii Nakryiko
Quoted 10 lines · KP Singh
From: KP Singh <[email protected]>
* Load/attach a BPF program that hooks to
file_mprotect(int) andbprm_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.Quoted 5 lines
Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>---
Please fix endlines below. With that:
Acked-by Andrii Nakryiko <[email protected]>Quoted 7 lines
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[...]
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +12
-0+void test_test_lsm (void ) +{ +struct lsm *skel =NULL ; +int err, duration =0 ; + + skel =lsm__open_and_load (); +if (CHECK (!skel,"skel_load" ,"lsm skeleton failed\n" )) + goto close_prog; + + err =lsm__attach (skel); +if (CHECK (err,"attach" ,"lsm attach failed: %d\n" , err)) + goto close_prog;
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c line 71
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +6
-0+ + err =exec_cmd (&skel->bss->monitored_pid); +if (CHECK (err <0 ,"exec_cmd" ,"err %d errno %d\n" , err, errno)) + goto close_prog; + + CHECK(skel->bss->bprm_count != 1, "bprm_count", "bprm_count = %d",\n is missing
Done.
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c line 82
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +11
-0+ skel->bss->bprm_count); + + skel->bss->monitored_pid =getpid (); + + err =heap_mprotect (); +if (CHECK (errno !=EPERM ,"heap_mprotect" ,"want errno=EPERM, got %d\n" , + errno)) + goto close_prog; + +CHECK (skel->bss->mprotect_count !=1 ,"mprotect_count" , +"mprotect_count = %d" , skel->bss->mprotect_count);\n is missing
Done.
- KP
Quoted 7 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/test_lsm.c new file +4
-0+ +close_prog: +lsm__destroy (skel); +}[...]
Re: [PATCH bpf-next v7 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM
In reply to KP SinghQuoted 9 lines · KP Singh
From: KP Singh <[email protected]>
* Load/attach a BPF program that hooks to
file_mprotect(int) andbprm_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.
Quoted 4 lines
Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>
Cool stuff!
Signature · 2 lines
James Morris <[email protected]>
PATCH v7 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 | 150 ++++++++++++++++++++++++++++++++++ Documentation/bpf/index.rst | 1 + 2 files changed, 151 insertions(+) create mode 100644 Documentation/bpf/bpf_lsm.rst
Documentation/bpf/bpf_lsm.rst new file +150 -0
@@ -0,0 +1,150 @@ +.. 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. Since these program end up modifying the MAC policies of +the system, they require both ``CAP_MAC_ADMIN`` and also require +``CAP_SYS_ADMIN`` for the loading of BPF programs. + +Structure +--------- + +The example shows an eBPF program that can be attached to the ``file_mprotect`` +LSM hook: + +.. c:function:: int file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); + +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:: Only the size and the names of the fields must match the type in the + kernel and 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 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 + */ + } + +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 or more simply by using the the libbpf helper +``bpf_prog_load_xattr``: + + +.. code-block:: c + + struct bpf_prog_load_attr attr = { + .file = "./prog.o", + }; + struct bpf_object *prog_obj; + struct bpf_program *prog; + int prog_fd; + + bpf_prog_load_xattr(&attr, &prog_obj, &prog_fd); + +Attachment to LSM Hooks +----------------------- + +The LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)` +syscall's ``BPF_PROG_ATTACH`` operation or more simply by +using the libbpf helper ``bpf_program__attach_lsm``. In the code shown below +``prog`` is the eBPF program loaded using ``BPF_PROG_LOAD``: + +.. code-block:: c + + struct bpf_link *link; + + link = bpf_program__attach_lsm(prog); + +The program can be detached from the LSM hook by *destroying* the ``link`` +link returned by ``bpf_program__attach_lsm``: + +.. code-block:: c + + link->destroy(); + +Examples +-------- + +An example eBPF programs 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/progs/lsm_void_hook.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm_void_hook.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
Re: [PATCH bpf-next v7 8/8] bpf: lsm: Add Documentation
Quoted 11 lines · KP Singh
From: KP Singh <[email protected]>
Document how eBPF programs (
BPF_PROG_TYPE_LSM) can be loaded and attached (BPF_LSM_MAC) to the LSM hooks.Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>---
This needs another pass and re-reading, has a bunch of outdated info :)
Quoted 45 lines
Documentation/bpf/bpf_lsm.rst | 150 ++++++++++++++++++++++++++++++++++ Documentation/bpf/index.rst | 1 + 2 files changed, 151 insertions(+) create mode 100644 Documentation/bpf/bpf_lsm.rstDocumentation/bpf/bpf_lsm.rst new file +34
-0@@ -0,0 +1,150 @@ +.. 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. Since these program end up modifying the MAC policies of +the system, they require both ``CAP_MAC_ADMIN`` and also require +``CAP_SYS_ADMIN`` for the loading of BPF programs. + +Structure +--------- + +The example shows an eBPF program that can be attached to the ``file_mprotect`` +LSM hook: + +.. c:function:: int file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); + +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));
Documentation/bpf/bpf_lsm.rst line 44
Quoted 10 lines
Documentation/bpf/bpf_lsm.rst new file +10
-0+ + struct vm_area_struct { + unsigned long start_brk, brk, start_stack; + unsigned long vm_start, vm_end; + struct mm_struct *vm_mm; + } __attribute__((preserve_access_index)); + + +.. note:: Only the size and the names of the fields must match the type in the + kernel and the order of the fields is irrelevant.
type should match/be compatible as well?
Documentation/bpf/bpf_lsm.rst line 52
Quoted 8 lines
Documentation/bpf/bpf_lsm.rst new file +8
-0+ +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 dump file <path-to-btf-vmlinux> format c > vmlinux.h +
bpftool btf *dump* file
Quoted 25 lines
Documentation/bpf/bpf_lsm.rst new file +25
-0+.. 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)
Documentation/bpf/bpf_lsm.rst line 87
Quoted 10 lines
Documentation/bpf/bpf_lsm.rst new file +10
-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 + */
return missing?
Quoted 15 lines
Documentation/bpf/bpf_lsm.rst new file +15
-0+ } + +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 or more simply by using the the libbpf helper +``bpf_prog_load_xattr``: + +
Documentation/bpf/bpf_lsm.rst line 112
Quoted 10 lines
Documentation/bpf/bpf_lsm.rst new file +10
-0+.. code-block:: c + + struct bpf_prog_load_attr attr = { + .file = "./prog.o", + }; + struct bpf_object *prog_obj; + struct bpf_program *prog; + int prog_fd; + + bpf_prog_load_xattr(&attr, &prog_obj, &prog_fd);
Can you please update this to not use deprecated/legacy APIs. Please suggest bpf_object__open/bpf_object__load and/or BPF skeleton as an example.
Documentation/bpf/bpf_lsm.rst line 118
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ +Attachment to LSM Hooks +----------------------- + +The LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)` +syscall's ``BPF_PROG_ATTACH`` operation or more simply by
BPF_PROG_ATTACH is incorrect, it's RAW_TRACEPOINT_OPEN, isn't it?
Quoted 5 lines
Documentation/bpf/bpf_lsm.rst new file +5
-0+using the libbpf helper ``bpf_program__attach_lsm``. In the code shown below +``prog`` is the eBPF program loaded using ``BPF_PROG_LOAD``: + +.. code-block:: c +
Documentation/bpf/bpf_lsm.rst line 133
Quoted 10 lines
Documentation/bpf/bpf_lsm.rst new file +10
-0+ struct bpf_link *link; + + link = bpf_program__attach_lsm(prog); + +The program can be detached from the LSM hook by *destroying* the ``link`` +link returned by ``bpf_program__attach_lsm``: + +.. code-block:: c + + link->destroy();
that's not how it works in C ;)
bpf_link__destroy(link);
Quoted 32 lines
Documentation/bpf/bpf_lsm.rst new file +17
-0+ +Examples +-------- + +An example eBPF programs 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/progs/lsm_void_hook.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm_void_hook.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.cDocumentation/bpf/index.rst +1
-1@@ -45,6 +45,7 @@ Program types prog_cgroup_sockopt prog_cgroup_sysctl prog_flow_dissector + bpf_lsm Testing and debugging BPF --2.20.1
Re: [PATCH bpf-next v7 8/8] bpf: lsm: Add Documentation
Thanks for the reviews!
Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · KP Singh
From: KP Singh <[email protected]>
Document how eBPF programs (
BPF_PROG_TYPE_LSM) can be loaded and attached (BPF_LSM_MAC) to the LSM hooks.
Quoted 7 lines
Quoted 5 lines
Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>---
This needs another pass and re-reading, has a bunch of outdated info :)
Indeed :)
Quoted 50 lines
Quoted 50 lines
Documentation/bpf/bpf_lsm.rst | 150 ++++++++++++++++++++++++++++++++++ Documentation/bpf/index.rst | 1 + 2 files changed, 151 insertions(+) create mode 100644 Documentation/bpf/bpf_lsm.rstDocumentation/bpf/bpf_lsm.rst new file +38
-0@@ -0,0 +1,150 @@ +.. 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. Since these program end up modifying the MAC policies of +the system, they require both ``CAP_MAC_ADMIN`` and also require +``CAP_SYS_ADMIN`` for the loading of BPF programs. + +Structure +--------- + +The example shows an eBPF program that can be attached to the ``file_mprotect`` +LSM hook: + +.. c:function:: int file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); + +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;
Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 44
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ struct mm_struct *vm_mm; + } __attribute__((preserve_access_index)); + + +.. note:: Only the size and the names of the fields must match the type in the + kernel and the order of the fields is irrelevant.type should match/be compatible as well?
I changed it to simply be:
.. note:: The order of the fields is irrelevant.
Quoted 11 lines
Documentation/bpf/bpf_lsm.rst line 52
Quoted 9 lines
Documentation/bpf/bpf_lsm.rst new file +8
-0+ +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 dump file <path-to-btf-vmlinux> format c > vmlinux.h +bpftool btf *dump* file
Done.
Quoted 30 lines
Quoted 30 lines
Documentation/bpf/bpf_lsm.rst new file +29
-0+.. 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; +
Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 87
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ 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 + */return missing?
Fixed.
Quoted 22 lines
Quoted 22 lines
Documentation/bpf/bpf_lsm.rst new file +21
-0+ } + +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 or more simply by using the the libbpf helper +``bpf_prog_load_xattr``: + + +.. code-block:: c + + struct bpf_prog_load_attr attr = { + .file = "./prog.o", + }; + struct bpf_object *prog_obj;
Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 112
Quoted 4 lines
Documentation/bpf/bpf_lsm.rst new file +4
-0+ struct bpf_program *prog; + int prog_fd; + + bpf_prog_load_xattr(&attr, &prog_obj, &prog_fd);Can you please update this to not use deprecated/legacy APIs. Please suggest bpf_object__open/bpf_object__load and/or BPF skeleton as an example.
Simplified and modernized this section as:
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.
</end>
If this looks okay, I will send a v8 with this updated and other fixes.
- KP
Quoted 9 lines
Documentation/bpf/bpf_lsm.rst line 118
Quoted 7 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ +Attachment to LSM Hooks +----------------------- + +The LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)` +syscall's ``BPF_PROG_ATTACH`` operation or more simply by
BPF_PROG_ATTACHis incorrect, it'sRAW_TRACEPOINT_OPEN, isn't it?
Correct, updated. Thanks!
Quoted 10 lines
Quoted 10 lines
Documentation/bpf/bpf_lsm.rst new file +9
-0+using the libbpf helper ``bpf_program__attach_lsm``. In the code shown below +``prog`` is the eBPF program loaded using ``BPF_PROG_LOAD``: + +.. code-block:: c + + struct bpf_link *link; + + link = bpf_program__attach_lsm(prog); +
Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 133
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+The program can be detached from the LSM hook by *destroying* the ``link`` +link returned by ``bpf_program__attach_lsm``: + +.. code-block:: c + + link->destroy();that's not how it works in C ;)
Oops, I incorrectly picked it up from link->destroy(link); and wrote something stupid.
Quoted 2 lines
bpf_link__destroy(link);
Updated in the snippet posted above.
- KP
Quoted 33 lines
Quoted 33 lines
Documentation/bpf/bpf_lsm.rst new file +17
-0+ +Examples +-------- + +An example eBPF programs 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/progs/lsm_void_hook.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm_void_hook.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.cDocumentation/bpf/index.rst +1
-1@@ -45,6 +45,7 @@ Program types prog_cgroup_sockopt prog_cgroup_sysctl prog_flow_dissector + bpf_lsm Testing and debugging BPF --2.20.1
Re: [PATCH bpf-next v7 8/8] bpf: lsm: Add Documentation
Quoted 206 lines · KP Singh
Thanks for the reviews!
Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · KP Singh
From: KP Singh <[email protected]>
Document how eBPF programs (
BPF_PROG_TYPE_LSM) can be loaded and attached (BPF_LSM_MAC) to the LSM hooks.Quoted 7 lines
Quoted 5 lines
Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>---
This needs another pass and re-reading, has a bunch of outdated info :)
Indeed :)
Quoted 50 lines
Quoted 50 lines
Documentation/bpf/bpf_lsm.rst | 150 ++++++++++++++++++++++++++++++++++ Documentation/bpf/index.rst | 1 + 2 files changed, 151 insertions(+) create mode 100644 Documentation/bpf/bpf_lsm.rstDocumentation/bpf/bpf_lsm.rst new file +38
-0@@ -0,0 +1,150 @@ +.. 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. Since these program end up modifying the MAC policies of +the system, they require both ``CAP_MAC_ADMIN`` and also require +``CAP_SYS_ADMIN`` for the loading of BPF programs. + +Structure +--------- + +The example shows an eBPF program that can be attached to the ``file_mprotect`` +LSM hook: + +.. c:function:: int file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); + +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;Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 44
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ struct mm_struct *vm_mm; + } __attribute__((preserve_access_index)); + + +.. note:: Only the size and the names of the fields must match the type in the + kernel and the order of the fields is irrelevant.type should match/be compatible as well?
I changed it to simply be:
.. note:: The order of the fields is irrelevant.
Quoted 11 lines
Documentation/bpf/bpf_lsm.rst line 52
Quoted 9 lines
Documentation/bpf/bpf_lsm.rst new file +8
-0+ +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 dump file <path-to-btf-vmlinux> format c > vmlinux.h +bpftool btf *dump* file
Done.
Quoted 30 lines
Quoted 30 lines
Documentation/bpf/bpf_lsm.rst new file +29
-0+.. 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; +Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 87
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ 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 + */return missing?
Fixed.
Quoted 22 lines
Quoted 22 lines
Documentation/bpf/bpf_lsm.rst new file +21
-0+ } + +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 or more simply by using the the libbpf helper +``bpf_prog_load_xattr``: + + +.. code-block:: c + + struct bpf_prog_load_attr attr = { + .file = "./prog.o", + }; + struct bpf_object *prog_obj;Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 112
Quoted 4 lines
Documentation/bpf/bpf_lsm.rst new file +4
-0+ struct bpf_program *prog; + int prog_fd; + + bpf_prog_load_xattr(&attr, &prog_obj, &prog_fd);Can you please update this to not use deprecated/legacy APIs. Please suggest bpf_object__open/bpf_object__load and/or BPF skeleton as an example.
Simplified and modernized this section as:
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.hand 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``.
Quoted 8 lines
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.
</end>
If this looks okay, I will send a v8 with this updated and other fixes.
Sounds good.
Quoted 76 lines
- KP
Quoted 9 lines
Documentation/bpf/bpf_lsm.rst line 118
Quoted 7 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+ +Attachment to LSM Hooks +----------------------- + +The LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)` +syscall's ``BPF_PROG_ATTACH`` operation or more simply by
BPF_PROG_ATTACHis incorrect, it'sRAW_TRACEPOINT_OPEN, isn't it?Correct, updated. Thanks!
Quoted 10 lines
Quoted 10 lines
Documentation/bpf/bpf_lsm.rst new file +9
-0+using the libbpf helper ``bpf_program__attach_lsm``. In the code shown below +``prog`` is the eBPF program loaded using ``BPF_PROG_LOAD``: + +.. code-block:: c + + struct bpf_link *link; + + link = bpf_program__attach_lsm(prog); +Quoted 8 lines
Documentation/bpf/bpf_lsm.rst line 133
Quoted 6 lines
Documentation/bpf/bpf_lsm.rst new file +6
-0+The program can be detached from the LSM hook by *destroying* the ``link`` +link returned by ``bpf_program__attach_lsm``: + +.. code-block:: c + + link->destroy();that's not how it works in C ;)
Oops, I incorrectly picked it up from
link->destroy(link); and wrote something stupid.Quoted 2 lines
bpf_link__destroy(link);Updated in the snippet posted above.
- KP
Quoted 33 lines
Quoted 33 lines
Documentation/bpf/bpf_lsm.rst new file +17
-0+ +Examples +-------- + +An example eBPF programs 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/progs/lsm_void_hook.c: + https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm_void_hook.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.cDocumentation/bpf/index.rst +1
-1@@ -45,6 +45,7 @@ Program types prog_cgroup_sockopt prog_cgroup_sysctl prog_flow_dissector + bpf_lsm Testing and debugging BPF --2.20.1
Re: [PATCH bpf-next v7 8/8] bpf: lsm: Add Documentation
In reply to KP SinghQuoted 9 lines · KP Singh
From: KP Singh <[email protected]>
Document how eBPF programs (
BPF_PROG_TYPE_LSM) can be loaded and attached (BPF_LSM_MAC) to the LSM hooks.Signed-off-by KP Singh <[email protected]>Reviewed-by Brendan Jackman <[email protected]>Reviewed-by Florent Revest <[email protected]>Reviewed-by Thomas Garnier <[email protected]>
Signature · 2 lines
James Morris <[email protected]>