eBPF-based security platform
build: refuse sections the target cannot attach
Daniel Borkmann0007e4b7b093
diff --git a/testing/t0010-unsupported-network-sections.sh b/testing/t0010-unsupported-network-sections.shindex 6ddb9005..4be43973 100644--- a/testing/t0010-unsupported-network-sections.sh+++ b/testing/t0010-unsupported-network-sections.sh@@ -71,6 +71,20 @@ int generic(struct __sk_buff *skb)}EOF+cat > "$WORK/xdp.bpf.c" <<'EOF'+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause+#include "vmlinux.h"+#include <bpf/bpf_helpers.h>++char _license[] SEC("license") = "Dual BSD/GPL";++SEC("xdp")+int xdp_prog(struct xdp_md *ctx)+{+ return XDP_PASS;+}+EOF+test_expect_success 'sk_skb sections are rejected for now' '! ( cd .. && "$B" "$WORK/sk-skb.bpf.c" \-o "$WORK/sk-skb.upatch" \@@ -99,4 +113,14 @@ test_expect_success 'generic cgroup skb sections require ingress or egress' '"$WORK/cgroup-skb-generic.out"'+test_expect_success 'a section the target cannot attach is rejected' '+ ! ( cd .. && "$B" "$WORK/xdp.bpf.c" \+ -o "$WORK/xdp.upatch" \+ --vmlinux vendor/linux/vmlinux.h --name xdp \+ --key "$WORK/k.pem" --cert "$WORK/c.der" ) \+ >"$WORK/xdp.out" 2>&1 &&+ grep -q "unsupported section .xdp." "$WORK/xdp.out" &&+ ! test -e "$WORK/xdp.upatch"+'+test_donediff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 147968cc..ebb15d55 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -28,6 +28,7 @@#include "io.h"#include "loader.h"#include "log.h"+#include "macros.h"#include "malloc.h"#include "package.h"#include "sign.h"@@ -179,6 +180,31 @@ static bool unsupported_fmod_ret_section(const char *sec)strcmp(sec, "fmod_ret.s/") == 0;}+/*+ * The sections the target attaches by their BTF target alone, through+ * BPF_RAW_TRACEPOINT_OPEN or a BPF_LSM_MAC link; every other type fails+ * there.+ */+static bool default_attach_section(const char *sec)+{+ static const char * const prefixes[] = {+ "fentry/", "fentry.s/", "fexit/", "fexit.s/",+ "lsm/", "lsm.s/", "tp_btf/",+ };+ size_t i, n;++ if (!sec)+ return false;+ if (sec[0] == '?')+ sec++;+ for (i = 0; i < ARRAY_SIZE(prefixes); i++) {+ n = strlen(prefixes[i]);+ if (!strncmp(sec, prefixes[i], n) && sec[n])+ return true;+ }+ return false;+}+static uint32_t attach_kind_from_section(const char *sec,const char **target){@@ -834,6 +860,13 @@ static int build_prog_metadata(struct bpf_object *obj, uint32_t nr_progs,goto err_free;}kind = attach_kind_from_section(sec, &target);+ if (kind == SHIRUDO_UPATCH_ATTACH_RAW_TRACEPOINT &&+ !default_attach_section(sec)) {+ pr_err("unsupported section '%s'; supported are fentry, fexit, fmod_ret, lsm, tp_btf, kprobe, kretprobe, uprobe.multi, cgroup_skb and netfilter",+ sec);+ err = -EOPNOTSUPP;+ goto err_free;+ }md->progs[i].role =htole32(SHIRUDO_UPATCH_PROG_ROLE_ATTACH);md->progs[i].kind = htole32(kind);