Skip to content

eBPF-based security platform

testing: add tests for uretprobe.multi attachment

Jiri Olsa7ed3e8036228
diff --git a/testing/fixtures/uretprobe-multi.bpf.c b/testing/fixtures/uretprobe-multi.bpf.c
new file mode 100644
index 00000000..d7eb3cba
--- /dev/null
+++ b/testing/fixtures/uretprobe-multi.bpf.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} hits SEC(".maps");
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+SEC("uretprobe.multi//tmp/shirudo-test-uprobe-target:*Func")
+int probe_return(struct pt_regs *ctx)
+{
+ __u32 key = 0;
+ __u64 *value = bpf_map_lookup_elem(&hits, &key);
+
+ if (value)
+ __sync_fetch_and_add(value, 1);
+ return 0;
+}
diff --git a/testing/functional/upatch/t3007-uprobe-multi-metadata.sh b/testing/functional/upatch/t3007-uprobe-multi-metadata.sh
index 801f5d44..a773522b 100644
--- a/testing/functional/upatch/t3007-uprobe-multi-metadata.sh
+++ b/testing/functional/upatch/t3007-uprobe-multi-metadata.sh
@@ -2,8 +2,9 @@
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
-# t3006 -- uprobe.multi SEC metadata resolves a standard libbpf glob to
-# sorted ELF file offsets and records the target's GNU build ID.
+# t3007 -- uprobe.multi and uretprobe.multi SEC metadata resolves a
+# standard libbpf glob to sorted ELF file offsets and records the target's
+# GNU build ID; a uprobe section without a binary is refused.
cd "$(dirname "$0")/../.."
. ./test-lib.sh
@@ -44,6 +45,7 @@ esac
write_probe() {
pattern=$1
+ sec=${2:-uprobe.multi/$WORK/target:$pattern}
cat > "$WORK/probe.bpf.c" <<EOF
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
#include "vmlinux.h"
@@ -51,7 +53,7 @@ write_probe() {
char _license[] SEC("license") = "Dual BSD/GPL";
-SEC("uprobe.multi/$WORK/target:$pattern")
+SEC("$sec")
int probe(struct pt_regs *ctx)
{
return 0;
@@ -87,4 +89,27 @@ test_expect_success 'comma alternation is rejected with an actionable error' '
grep -q "alternation are unsupported" "$WORK/alternation.log"
'
+test_expect_success 'a uretprobe.multi glob records the return kind' '
+ write_probe "*Func" "uretprobe.multi/$WORK/target:*Func" &&
+ ( cd .. && "$B" "$WORK/probe.bpf.c" \
+ -o "$WORK/uretprobe.upatch" --vmlinux vendor/linux/vmlinux.h \
+ --name uretprobe-multi-test --key "$WORK/k.pem" \
+ --cert "$WORK/c.der" ) >/dev/null 2>&1 &&
+ "$CHECK" "$WORK/uretprobe.upatch" uretprobe-multi \
+ "$WORK/target" "*Func" 2
+'
+
+refused() {
+ write_probe "" "$1" &&
+ ! ( cd .. && "$B" "$WORK/probe.bpf.c" \
+ -o "$WORK/refused.upatch" --vmlinux vendor/linux/vmlinux.h \
+ --name uprobe-refused --key "$WORK/k.pem" \
+ --cert "$WORK/c.der" ) >"$WORK/refused.log" 2>&1 &&
+ grep -q "unsupported uprobe section" "$WORK/refused.log"
+}
+
+test_expect_success 'a uprobe without a binary is refused' '
+ refused "uprobe"
+'
+
test_done
diff --git a/testing/functional/upatch/t3107-uprobe-multi.sh b/testing/functional/upatch/t3107-uprobe-multi.sh
index bba69f7d..0003eaf6 100644
--- a/testing/functional/upatch/t3107-uprobe-multi.sh
+++ b/testing/functional/upatch/t3107-uprobe-multi.sh
@@ -2,7 +2,8 @@
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
-# t3107 -- live uprobe.multi coverage using host-built target fixtures.
+# t3107 -- live uprobe.multi and uretprobe.multi coverage using host-built
+# target fixtures.
cd "$(dirname "$0")/../.."
. ./integration-lib.sh
@@ -13,13 +14,26 @@ FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
NAME=shirudo-test-uprobe-multi
LOAD="$FIXTURES/$NAME.upatch"
UNLOAD="$FIXTURES/$NAME.unload.upatch"
+RET_NAME=shirudo-test-uretprobe-multi
+RET_LOAD="$FIXTURES/$RET_NAME.upatch"
+RET_UNLOAD="$FIXTURES/$RET_NAME.unload.upatch"
TARGET="$FIXTURES/shirudo-test-uprobe-target"
RUNTIME=/tmp/shirudo-test-uprobe-target
PIN="/run/shirudo/bpffs/upatches/$NAME/link"
+RET_PIN="/run/shirudo/bpffs/upatches/$RET_NAME/link"
WORK=$(mktemp -d)
-trap 'if [ -e "$PIN" ]; then "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true; fi; rm -rf "$WORK"' EXIT
+cleanup() {
+ if [ -e "$PIN" ]; then
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true
+ fi
+ if [ -e "$RET_PIN" ]; then
+ "$SHIRUDO_BIN" upatch unload "$RET_UNLOAD" --no-precheck >/dev/null 2>&1 || true
+ fi
+ rm -rf "$WORK"
+}
+trap cleanup EXIT
-for fixture in "$LOAD" "$UNLOAD" "$TARGET"; do
+for fixture in "$LOAD" "$UNLOAD" "$RET_LOAD" "$RET_UNLOAD" "$TARGET"; do
[ -s "$fixture" ] || test_skip_all "missing uprobe fixture: $fixture"
done
@@ -60,4 +74,22 @@ assert int(hits["entries"][0]["value"], 0) == 2
PY
'
+test_expect_success 'uretprobe.multi loads, runs on both returns and unloads' '
+ "$SHIRUDO_BIN" upatch load "$RET_LOAD" --no-precheck >/dev/null 2>&1 &&
+ [ -e "$RET_PIN" ] &&
+ "$RUNTIME" &&
+ "$SHIRUDO_BIN" upatch get "$RET_NAME" --json >"$WORK/ret.json" &&
+ python3 - "$WORK/ret.json" <<'PY' &&
+import json
+import sys
+doc = json.load(open(sys.argv[1], encoding="utf-8"))
+assert doc["upatch"]["location"] == \
+ "uretprobe.multi//tmp/shirudo-test-uprobe-target (2 funcs)", doc
+hits = next(m for m in doc["upatch"]["maps"] if m["name"] == "hits")
+assert int(hits["entries"][0]["value"], 0) == 2, doc
+PY
+ "$SHIRUDO_BIN" upatch unload "$RET_UNLOAD" --no-precheck >/dev/null 2>&1 &&
+ [ ! -e "$RET_PIN" ]
+'
+
test_done
diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.c
index 4f4bf95e..7f844b96 100644
--- a/testing/helper/test-upatch-prog-metadata.c
+++ b/testing/helper/test-upatch-prog-metadata.c
@@ -98,7 +98,7 @@ static int expect_serializer(const struct upatch *u)
return arena && error ? 0 : fail("serializer map mismatch");
}
-static int expect_uprobe_multi(const struct upatch *u,
+static int expect_uprobe_multi(const struct upatch *u, __u32 kind,
const char *path, const char *pattern,
__u32 expected_offsets)
{
@@ -123,7 +123,7 @@ static int expect_uprobe_multi(const struct upatch *u,
le32_to_cpu(md.nr_progs) != 1)
return fail("unexpected metadata header");
if (le32_to_cpu(prog.role) != UPATCH_PROG_ROLE_ATTACH ||
- le32_to_cpu(prog.kind) != UPATCH_ATTACH_UPROBE_MULTI ||
+ le32_to_cpu(prog.kind) != kind ||
le64_to_cpu(prog.offset) != 0 ||
!memchr(prog.target, '\0', sizeof(prog.target)) ||
strcmp(prog.target, pattern) != 0)
@@ -253,14 +253,16 @@ int main(int argc, char **argv)
ret = fail("XDP metadata mismatch");
prog_specs_free(specs, u.hdr.nr_progs);
}
- } else if (strcmp(mode, "uprobe-multi") == 0 && argc == 6) {
+ } else if ((strcmp(mode, "uprobe-multi") == 0 ||
+ strcmp(mode, "uretprobe-multi") == 0) && argc == 6) {
+ __u32 kind = strcmp(mode, "uretprobe-multi") == 0 ?
+ UPATCH_ATTACH_URETPROBE_MULTI : UPATCH_ATTACH_UPROBE_MULTI;
__u64 count;
if (parse_u64(argv[5], strlen(argv[5]), UINT32_MAX, &count))
ret = fail("invalid expected uprobe offset count");
else
- ret = expect_uprobe_multi(&u, argv[3], argv[4],
- (__u32)count);
+ ret = expect_uprobe_multi(&u, kind, argv[3], argv[4], (__u32)count);
} else if (strcmp(mode, "policy-labels") == 0 && argc == 3) {
ret = expect_policy_labels(&u);
} else {
diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.sh
index 1afe0245..7adfb66b 100755
--- a/testing/vm/build-bundle.sh
+++ b/testing/vm/build-bundle.sh
@@ -741,6 +741,7 @@ queue_fixture build_fixture otel-backend.bpf.c shirudo-test-otel-backend "$FIXTU
queue_fixture build_fixture serialized-metric.bpf.c shirudo-test-serialized-metric "$FIXTURE_WORK/trusted"
queue_fixture build_fixture arena-tree-serializer.bpf.c shirudo-test-arena-tree "$FIXTURE_WORK/trusted"
queue_fixture build_fixture uprobe-multi.bpf.c shirudo-test-uprobe-multi "$FIXTURE_WORK/trusted"
+queue_fixture build_fixture uretprobe-multi.bpf.c shirudo-test-uretprobe-multi "$FIXTURE_WORK/trusted"
queue_fixture build_fixture scope-netfilter.bpf.c shirudo-test-scope-netfilter "$FIXTURE_WORK/trusted"
queue_fixture build_fixture scope-cgroup.bpf.c shirudo-test-scope-cgroup "$FIXTURE_WORK/trusted"
queue_fixture build_fixture scope-kprobe.bpf.c shirudo-test-scope-kprobe "$FIXTURE_WORK/trusted"
@@ -775,6 +776,9 @@ tree_skip_payload="$($REPO_ROOT/shirudo upatch info \
uprobe_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/shirudo-test-uprobe-multi.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+uretprobe_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/shirudo-test-uretprobe-multi.upatch" --json |
+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
otel_backend_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/shirudo-test-otel-backend.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
@@ -841,6 +845,7 @@ shirudo-test-arena-tree $arena_tree_payload shirudo-test-arena-tree.unload.upatc
kernel-obs $observability_payload kernel-obs.unload.upatch
kernel-obs $observability_anomaly_payload kernel-obs-cred-anomaly.unload.upatch
shirudo-test-uprobe-multi $uprobe_payload shirudo-test-uprobe-multi.unload.upatch
+shirudo-test-uretprobe-multi $uretprobe_payload shirudo-test-uretprobe-multi.unload.upatch
EOF
wait_fixture_jobs