[PATCH bpf-next 2/3] bpf: add option to require BPF signature

Matteo Croce
2021-12-03T19:19:20Z

From: Matteo Croce <mcroce@microsoft.com>

Add a compile time option which makes the BPF signature mandatory,
i.e. all programs without signature or with an invalid one are rejected.

CO-RE programs load a program of type BPF_PROG_TYPE_SYSCALL, which then
uses the bpf() syscall to load the final program. This one won't have any
signature, so never enforce signature for programs coming from the kernel.

This happens when loading a program with a missing signature:

# ip link set lo xdp object xdp.o
[ 8677.652546] Rejecting BPF '' with no signature

Signed-off-by: Matteo Croce <mcroce@microsoft.com>
---
kernel/bpf/Kconfig | 6 ++++++
kernel/bpf/syscall.c | 4 ++++
2 files changed, 10 insertions(+)

diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig
index 735979bb8672..fe6e84abe84c 100644
--- a/kernel/bpf/Kconfig
+++ b/kernel/bpf/Kconfig
@@ -87,6 +87,12 @@ config BPF_SIG
Check BPF programs for valid signatures upon load: the signature
is passed via the bpf() syscall together with the instructions.

+config BPF_SIG_FORCE
+ bool "Require BPF to be validly signed"
+ depends on BPF_SIG
+ help
+ Reject unsigned BPF or signed BPF for which we don't have a key.
+
source "kernel/bpf/preload/Kconfig"

config BPF_LSM
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 5aaa74a72b46..9e36614719fd 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2340,6 +2340,10 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
prog->aux->name, ERR_PTR(err));
goto free_prog_sec;
}
+ } else if (IS_ENABLED(CONFIG_BPF_SIG_FORCE) && !uattr.is_kernel) {
+ pr_warn("Rejecting BPF '%s' with no signature\n", prog->aux->name);
+ err = -EKEYREJECTED;
+ goto free_prog_sec;
}
#endif

--
2.33.1