[PATCH bpf-next v2 22/28] bpf/verifier: prevent non GPL programs to be loaded against HID
From: Benjamin Tissoires
Date: Fri Mar 04 2022 - 12:35:43 EST
This is just to hammer the obvious because I suspect you can not already
load a bpf HID program which is not GPL because all of the useful
functions are GPL only.
Anyway, this ensures that users are not tempted to bypass this requirement
and will allow us to ship tested BPF programs in the kernel without having
to aorry about the license.
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@xxxxxxxxxx>
---
new in v2:
- Note: I placed this statement in check_attach_btf_id() to be local to
other similar checks (regarding LSM), however, I have no idea if this
is the correct place. Please shout at me if it isn't.
---
include/linux/bpf-hid.h | 8 ++++++++
kernel/bpf/hid.c | 12 ++++++++++++
kernel/bpf/verifier.c | 7 +++++++
3 files changed, 27 insertions(+)
diff --git a/include/linux/bpf-hid.h b/include/linux/bpf-hid.h
index bd548f6a4a26..3da1d0ecb9be 100644
--- a/include/linux/bpf-hid.h
+++ b/include/linux/bpf-hid.h
@@ -2,6 +2,7 @@
#ifndef _BPF_HID_H
#define _BPF_HID_H
+#include <linux/bpf_verifier.h>
#include <linux/mutex.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/bpf_hid.h>
@@ -71,6 +72,8 @@ int bpf_hid_prog_query(const union bpf_attr *attr,
union bpf_attr __user *uattr);
int bpf_hid_link_create(const union bpf_attr *attr,
struct bpf_prog *prog);
+int bpf_hid_verify_prog(struct bpf_verifier_log *vlog,
+ const struct bpf_prog *prog);
#else
static inline int bpf_hid_prog_query(const union bpf_attr *attr,
union bpf_attr __user *uattr)
@@ -83,6 +86,11 @@ static inline int bpf_hid_link_create(const union bpf_attr *attr,
{
return -EOPNOTSUPP;
}
+static inline int bpf_hid_verify_prog(struct bpf_verifier_log *vlog,
+ const struct bpf_prog *prog)
+{
+ return -EOPNOTSUPP;
+}
#endif
static inline bool bpf_hid_link_empty(struct bpf_hid *bpf,
diff --git a/kernel/bpf/hid.c b/kernel/bpf/hid.c
index 653d10c0f4e6..b3dc1cd37a3e 100644
--- a/kernel/bpf/hid.c
+++ b/kernel/bpf/hid.c
@@ -37,6 +37,18 @@ void bpf_hid_set_hooks(struct bpf_hid_hooks *hooks)
}
EXPORT_SYMBOL_GPL(bpf_hid_set_hooks);
+int bpf_hid_verify_prog(struct bpf_verifier_log *vlog,
+ const struct bpf_prog *prog)
+{
+ if (!prog->gpl_compatible) {
+ bpf_log(vlog,
+ "HID programs must have a GPL compatible license\n");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
BPF_CALL_5(bpf_hid_get_data, void*, ctx, u64, offset, u32, n, void*, data, u64, size)
{
struct hid_bpf_ctx *bpf_ctx = ctx;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index a57db4b2803c..afec8fa1d674 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -21,6 +21,7 @@
#include <linux/perf_event.h>
#include <linux/ctype.h>
#include <linux/error-injection.h>
+#include <linux/bpf-hid.h>
#include <linux/bpf_lsm.h>
#include <linux/btf_ids.h>
@@ -14235,6 +14236,12 @@ 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_HID) {
+ ret = bpf_hid_verify_prog(&env->log, prog);
+ if (ret < 0)
+ return ret;
+ }
+
if (prog->type != BPF_PROG_TYPE_TRACING &&
prog->type != BPF_PROG_TYPE_LSM &&
prog->type != BPF_PROG_TYPE_EXT)
--
2.35.1