eBPF-based security platform
testing: add tests for standard uprobe attachment
Jiri Olsa4fbf083e37dd
diff --git a/testing/fixtures/uprobe.bpf.c b/testing/fixtures/uprobe.bpf.cnew file mode 100644index 00000000..8f8b0ad2--- /dev/null+++ b/testing/fixtures/uprobe.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("uprobe//tmp/shirudo-test-uprobe-target:fooFunc")+int probe_entry(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/fixtures/uretprobe.bpf.c b/testing/fixtures/uretprobe.bpf.cnew file mode 100644index 00000000..b8601c2d--- /dev/null+++ b/testing/fixtures/uretprobe.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//tmp/shirudo-test-uprobe-target:fooFunc")+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.shindex e3b4ad67..5a814801 100644--- a/testing/functional/upatch/t3007-uprobe-multi-metadata.sh+++ b/testing/functional/upatch/t3007-uprobe-multi-metadata.sh@@ -2,10 +2,10 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# t3007 -- uprobe.multi and uretprobe.multi SEC metadata, sleepable forms-# included, 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.+# t3007 -- uprobe, uretprobe, uprobe.multi and uretprobe.multi SEC+# metadata, sleepable forms included, resolves a function, a function and+# offset, a raw file offset or a standard libbpf glob to ELF file offsets+# and records the target's GNU build ID.cd "$(dirname "$0")/../..". ./test-lib.sh@@ -113,6 +113,75 @@ test_expect_success 'a uprobe without a binary is refused' 'refused "uprobe"'+# build_probe <name> <section>: build the probe into $WORK/<name>.upatch.+build_probe() {+ write_probe "" "$2" &&+ ( cd .. && "$B" "$WORK/probe.bpf.c" \+ -o "$WORK/$1.upatch" --vmlinux vendor/linux/vmlinux.h \+ --name "$1" --key "$WORK/k.pem" \+ --cert "$WORK/c.der" ) >"$WORK/$1.log" 2>&1+}++# check_probe <name> <mode> <target> <entry offset> <file offset>+check_probe() {+ "$CHECK" "$WORK/$1.upatch" "$2" "$WORK/target" "$3" "$4" >"$WORK/$1.out" &&+ [ "$(head -n 1 "$WORK/$1.out")" = "$5" ]+}++test_expect_success 'a uprobe on a function records its one file offset' '+ build_probe uprobe-func "uprobe/$WORK/target:fooFunc" &&+ "$CHECK" "$WORK/uprobe-func.upatch" uprobe "$WORK/target" fooFunc 0 \+ >"$WORK/uprobe-func.out" &&+ base=$(head -n 1 "$WORK/uprobe-func.out") &&+ [ "$base" -gt 0 ] &&+ echo "$base" >"$WORK/base"+'++base=$(cat "$WORK/base" 2>/dev/null || echo 0)++test_expect_success 'a uprobe at function+offset adds the offset' '+ build_probe uprobe-off "uprobe/$WORK/target:fooFunc+4" &&+ check_probe uprobe-off uprobe fooFunc 4 $((base + 4))+'++test_expect_success 'a uprobe at a raw file offset records it as given' '+ raw=$(printf "0x%x" "$base") &&+ build_probe uprobe-raw "uprobe/$WORK/target:$raw" &&+ check_probe uprobe-raw uprobe "$raw" 0 "$base"+'++test_expect_success 'a uretprobe on a function records the return kind' '+ build_probe uretprobe-func "uretprobe/$WORK/target:fooFunc" &&+ check_probe uretprobe-func uretprobe fooFunc 0 "$base"+'++test_expect_success 'the sleepable uprobe.s and uretprobe.s take the same kinds' '+ build_probe uprobe-s "uprobe.s/$WORK/target:fooFunc" &&+ check_probe uprobe-s uprobe fooFunc 0 "$base" &&+ build_probe uretprobe-s "uretprobe.s/$WORK/target:fooFunc" &&+ check_probe uretprobe-s uretprobe fooFunc 0 "$base"+'++test_expect_success 'a uretprobe offset is refused' '+ ! build_probe uretprobe-off "uretprobe/$WORK/target:fooFunc+4" &&+ grep -q "uretprobe offsets are unsupported" "$WORK/uretprobe-off.log"+'++test_expect_success 'a uprobe on a missing function is refused' '+ ! build_probe uprobe-missing "uprobe/$WORK/target:noSuchFunc" &&+ grep -q "not found in" "$WORK/uprobe-missing.log"+'++test_expect_success 'a uprobe offset past the end of the file is refused' '+ ! build_probe uprobe-past "uprobe/$WORK/target:0x10000000" &&+ grep -q "lies outside" "$WORK/uprobe-past.log"+'++test_expect_success 'a uprobe on a relative path is refused' '+ ! build_probe uprobe-relative "uprobe/target:fooFunc" &&+ grep -q "invalid uprobe section" "$WORK/uprobe-relative.log"+'+test_expect_success 'a sleepable uprobe.multi.s glob records the entry kind' 'write_probe "*Func" "uprobe.multi.s/$WORK/target:*Func" &&( cd .. && "$B" "$WORK/probe.bpf.c" \diff --git a/testing/functional/upatch/t3107-uprobe-multi.sh b/testing/functional/upatch/t3107-uprobe-multi.shindex b7013ff8..da4c7d0d 100644--- a/testing/functional/upatch/t3107-uprobe-multi.sh+++ b/testing/functional/upatch/t3107-uprobe-multi.sh@@ -2,8 +2,8 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# t3107 -- live uprobe.multi, uretprobe.multi and sleepable uprobe.multi.s-# coverage using host-built target fixtures.+# t3107 -- live uprobe.multi, uretprobe.multi, sleepable uprobe.multi.s,+# uprobe and uretprobe coverage using host-built target fixtures.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -20,6 +20,7 @@ RET_UNLOAD="$FIXTURES/$RET_NAME.unload.upatch"SLEEP_NAME=shirudo-test-uprobe-multi-sleepableSLEEP_LOAD="$FIXTURES/$SLEEP_NAME.upatch"SLEEP_UNLOAD="$FIXTURES/$SLEEP_NAME.unload.upatch"+SINGLE_NAMES="shirudo-test-uprobe shirudo-test-uretprobe"TARGET="$FIXTURES/shirudo-test-uprobe-target"RUNTIME=/tmp/shirudo-test-uprobe-targetPIN="/run/shirudo/bpffs/upatches/$NAME/link"@@ -36,12 +37,21 @@ cleanup() {if [ -e "$SLEEP_PIN" ]; then"$SHIRUDO_BIN" upatch unload "$SLEEP_UNLOAD" --no-precheck >/dev/null 2>&1 || truefi+ for single in $SINGLE_NAMES; do+ if [ -e "/run/shirudo/bpffs/upatches/$single/link" ]; then+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$single.unload.upatch" \+ --no-precheck >/dev/null 2>&1 || true+ fi+ donerm -rf "$WORK"}trap cleanup EXITfor fixture in "$LOAD" "$UNLOAD" "$RET_LOAD" "$RET_UNLOAD" "$SLEEP_LOAD" \- "$SLEEP_UNLOAD" "$TARGET"; do+ "$SLEEP_UNLOAD" "$TARGET" "$FIXTURES/shirudo-test-uprobe.upatch" \+ "$FIXTURES/shirudo-test-uprobe.unload.upatch" \+ "$FIXTURES/shirudo-test-uretprobe.upatch" \+ "$FIXTURES/shirudo-test-uretprobe.unload.upatch"; do[ -s "$fixture" ] || test_skip_all "missing uprobe fixture: $fixture"done@@ -118,4 +128,34 @@ PY[ ! -e "$SLEEP_PIN" ]'+# check_single <name> <prefix>: load, run fooFunc once, check, unload.+check_single() {+ pin="/run/shirudo/bpffs/upatches/$1/link"+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$1.upatch" --no-precheck >/dev/null 2>&1 &&+ [ -e "$pin" ] &&+ "$RUNTIME" &&+ "$SHIRUDO_BIN" upatch get "$1" --json >"$WORK/$1.json" &&+ python3 - "$WORK/$1.json" "$2" <<'PY' &&+import json+import re+import sys+doc = json.load(open(sys.argv[1], encoding="utf-8"))+want = re.escape(sys.argv[2]) + r"//proc/self/fd/[0-9]+\+0x[0-9a-f]+"+location = doc["upatch"]["location"]+assert re.fullmatch(want, location), {"pattern": want, "location": location}+hits = next(m for m in doc["upatch"]["maps"] if m["name"] == "hits")+assert int(hits["entries"][0]["value"], 0) == 1, doc+PY+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$1.unload.upatch" --no-precheck >/dev/null 2>&1 &&+ [ ! -e "$pin" ]+}++test_expect_success 'a uprobe on fooFunc runs once per call and unloads' '+ check_single shirudo-test-uprobe uprobe+'++test_expect_success 'a uretprobe on fooFunc runs once per return and unloads' '+ check_single shirudo-test-uretprobe uretprobe+'+test_donediff --git a/testing/fuzz/corpus/prog-metadata/uprobe-uretprobe b/testing/fuzz/corpus/prog-metadata/uprobe-uretprobenew file mode 100644index 00000000..656350e1Binary files /dev/null and b/testing/fuzz/corpus/prog-metadata/uprobe-uretprobe differdiff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.cindex 7f844b96..01d6fddf 100644--- a/testing/helper/test-upatch-prog-metadata.c+++ b/testing/helper/test-upatch-prog-metadata.c@@ -98,9 +98,14 @@ 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, __u32 kind,- const char *path, const char *pattern,- __u32 expected_offsets)+/*+ * The one program attaches as kind to target in path at expected_offsets+ * file offsets, the first stored in *first; entry_offset is the offset+ * the entry records inside the function.+ */+static int expect_uprobe(const struct upatch *u, __u32 kind,+ const char *path, const char *pattern,+ __u64 entry_offset, __u32 expected_offsets, __u64 *first){struct upatch_uprobe_targets targets;struct upatch_uprobe_target record;@@ -124,7 +129,7 @@ static int expect_uprobe_multi(const struct upatch *u, __u32 kind,return fail("unexpected metadata header");if (le32_to_cpu(prog.role) != UPATCH_PROG_ROLE_ATTACH ||le32_to_cpu(prog.kind) != kind ||- le64_to_cpu(prog.offset) != 0 ||+ le64_to_cpu(prog.offset) != entry_offset ||!memchr(prog.target, '\0', sizeof(prog.target)) ||strcmp(prog.target, pattern) != 0)return fail("uprobe program entry mismatch");@@ -156,6 +161,8 @@ static int expect_uprobe_multi(const struct upatch *u, __u32 kind,cursor += sizeof(__le64);if (i && offset <= previous)return fail("uprobe offsets are not sorted and unique");+ if (!i)+ *first = offset;previous = offset;}return cursor == end ? 0 : fail("trailing uprobe metadata");@@ -257,12 +264,25 @@ int main(int argc, char **argv)strcmp(mode, "uretprobe-multi") == 0) && argc == 6) {__u32 kind = strcmp(mode, "uretprobe-multi") == 0 ?UPATCH_ATTACH_URETPROBE_MULTI : UPATCH_ATTACH_UPROBE_MULTI;- __u64 count;+ __u64 count, first;if (parse_u64(argv[5], strlen(argv[5]), UINT32_MAX, &count))ret = fail("invalid expected uprobe offset count");else- ret = expect_uprobe_multi(&u, kind, argv[3], argv[4], (__u32)count);+ ret = expect_uprobe(&u, kind, argv[3], argv[4], 0, (__u32)count, &first);+ } else if ((strcmp(mode, "uprobe") == 0 || strcmp(mode, "uretprobe") == 0) &&+ argc == 6) {+ __u32 kind = strcmp(mode, "uretprobe") == 0 ?+ UPATCH_ATTACH_URETPROBE : UPATCH_ATTACH_UPROBE;+ __u64 entry_offset, first;++ /* Prints the file offset, which the caller cannot know beforehand. */+ if (parse_u64(argv[5], strlen(argv[5]), UINT64_MAX, &entry_offset))+ ret = fail("invalid expected uprobe entry offset");+ else+ ret = expect_uprobe(&u, kind, argv[3], argv[4], entry_offset, 1, &first);+ if (!ret)+ printf("%llu\n", first);} 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.shindex b79cc0d5..5181b63e 100755--- a/testing/vm/build-bundle.sh+++ b/testing/vm/build-bundle.sh@@ -743,6 +743,8 @@ queue_fixture build_fixture arena-tree-serializer.bpf.c shirudo-test-arena-treequeue_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 uprobe-multi-sleepable.bpf.c shirudo-test-uprobe-multi-sleepable "$FIXTURE_WORK/trusted"+queue_fixture build_fixture uprobe.bpf.c shirudo-test-uprobe "$FIXTURE_WORK/trusted"+queue_fixture build_fixture uretprobe.bpf.c shirudo-test-uretprobe "$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"@@ -783,6 +785,12 @@ uretprobe_payload="$($REPO_ROOT/shirudo upatch info \uprobe_sleepable_payload="$($REPO_ROOT/shirudo upatch info \"$TMP_DIR/fixtures/shirudo-test-uprobe-multi-sleepable.upatch" --json |python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+uprobe_single_payload="$($REPO_ROOT/shirudo upatch info \+ "$TMP_DIR/fixtures/shirudo-test-uprobe.upatch" --json |+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+uretprobe_single_payload="$($REPO_ROOT/shirudo upatch info \+ "$TMP_DIR/fixtures/shirudo-test-uretprobe.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"])')"@@ -851,6 +859,8 @@ kernel-obs $observability_anomaly_payload kernel-obs-cred-anomaly.unload.upatchshirudo-test-uprobe-multi $uprobe_payload shirudo-test-uprobe-multi.unload.upatchshirudo-test-uretprobe-multi $uretprobe_payload shirudo-test-uretprobe-multi.unload.upatchshirudo-test-uprobe-multi-sleepable $uprobe_sleepable_payload shirudo-test-uprobe-multi-sleepable.unload.upatch+shirudo-test-uprobe $uprobe_single_payload shirudo-test-uprobe.unload.upatch+shirudo-test-uretprobe $uretprobe_single_payload shirudo-test-uretprobe.unload.upatchEOFwait_fixture_jobs