bpf: Add signature checking for BPF programs
Subsystems (6)
PATCH 1/1 bpf: Add signature checking for BPF programs
From: Xichen Lin <[email protected]>
Check the signature of a BPF program against the same set of keys for module signature checking.
Currently the format of a signed BPF program is similar to that of a signed kernel module, composed of BPF bytecode, signature, module_signature structure and a magic string, in order, aligned to struct sock_filter.
--- include/linux/bpf_sig.h | 26 ++++++++++++++++++ init/Kconfig | 10 +++++++ kernel/bpf/Makefile | 3 +++ kernel/bpf/bpf_sig.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 5 ++++ kernel/sysctl.c | 14 ++++++++++ 6 files changed, 128 insertions(+) create mode 100644 include/linux/bpf_sig.h create mode 100644 kernel/bpf/bpf_sig.c
include/linux/bpf_sig.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2020 Bytedance +*/ + +#ifndef _LINUX_BPF_SIG_H +#define _LINUX_BPF_SIG_H + +#include <linux/bpf.h> + +#define BPF_PROG_SIG_STRING "~BPF signature appended~\n" + +#ifdef CONFIG_BPF_SIGNATURE +extern int sysctl_bpf_signature_enable; +#endif /* CONFIG_BPF_SIGNATURE */ + +#ifdef CONFIG_BPF_SIGNATURE +int bpf_check_prog_sig (struct bpf_prog *prog); +#else +static inline int bpf_check_prog_sig (struct bpf_prog *prog) +{ +return 0 ; +} +#endif /* CONFIG_BPF_SIGNATURE */ +#endif /* _LINUX_BPF_SIG_H */
init/Kconfig +10 -0
@@ -2212,6 +2212,16 @@ config MODULE_SIG_HASH default "sha384" if MODULE_SIG_SHA384 default "sha512" if MODULE_SIG_SHA512 +config BPF_SIGNATURE + bool "BPF program signature verification" + depends on MODULE_SIG + help + Check BPF programs for valid signatures upon load: the signature + is appended to the end of the BPF program similar to module signing. + + BPF signature checking will use the same kernel facilities as + module signature checking as well as the keys and hash functions. + config MODULE_COMPRESS bool "Compress modules on installation" help
kernel/bpf/Makefile +3 -0
@@ -37,3 +37,6 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o obj-${CONFIG_BPF_LSM} += bpf_lsm.o endif obj-$(CONFIG_BPF_PRELOAD) += preload/ +ifeq ($(CONFIG_BPF_SIGNATURE),y) +obj-$(CONFIG_BPF_SIGNATURE) += bpf_sig.o +endif
kernel/bpf/bpf_sig.c new file +70 -0
70 lines not on this page · raw message
kernel/bpf/syscall.c +5 -0
@@ -32,6 +32,7 @@#include <linux/bpf-netns.h> #include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> +#include <linux/bpf_sig.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
@@ -2201,6 +2202,10 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)if (err <0 ) goto free_prog_sec; + err =bpf_check_prog_sig (prog); +if (err <0 ) + goto free_prog; +/* run eBPF verifier */ err =bpf_check (&prog, attr, uattr);if (err <0 )
kernel/sysctl.c +14 -0
@@ -103,6 +103,9 @@#ifdef CONFIG_LOCKUP_DETECTOR #include <linux/nmi.h> #endif +#ifdef CONFIG_BPF_SIGNATURE +#include <linux/bpf_sig.h> +#endif /* CONFIG_BPF_SIGNATURE */ #if defined(CONFIG_SYSCTL )
@@ -2621,6 +2624,17 @@ static struct ctl_table kern_table[] = { },#endif #ifdef CONFIG_BPF_SYSCALL +#ifdef CONFIG_BPF_SIGNATURE + { + .procname = "bpf_signature_enable", + .data = &sysctl_bpf_signature_enable, + .maxlen = sizeof(sysctl_bpf_signature_enable), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = SYSCTL_ONE, + }, +#endif /* CONFIG_BPF_SIGNATURE */ { .procname = "unprivileged_bpf_disabled", .data = &sysctl_unprivileged_bpf_disabled,
Signature · 1 line
2.11.0
Quoted 10 lines · Xichen Lin
From: Xichen Lin <[email protected]>
Check the signature of a BPF program against the same set of keys for module signature checking.
Currently the format of a signed BPF program is similar to that of a signed kernel module, composed of BPF bytecode, signature,
module_signaturestructure and a magic string, in order, aligned to structsock_filter.
Commit log talks about 'what' and gives no insight into 'why' the patch was sent.
Please take time to clearly explain the motivation for the changes. Also please see earlier discussions on the subject and Arnaldo's preso from plumbers.