Skip to content

eBPF-based security platform

testing: run fentry, fexit and fmod_ret upatches on a target

Daniel Borkmanne235102c2f08
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index c45f3013..cb605f93 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -327,6 +327,34 @@ signed config image.
every op as ``activity`` and, for a marked socket whose op failed, a
``hit``. A spliced failing decrypt moves both counters; an ordinary
copying failing decrypt moves only ``activity``; the unload removes it
+ * - ``t3118-fentry-fexit``
+ - Live ``fentry`` and ``fexit`` links attach to ``vfs_fallocate()``
+ from one upatch and report their kinds. The ``fentry`` program
+ sees a call with its arguments and follows a pointer among them
+ to the file's inode number. The ``fexit`` program sees a call
+ that succeeds and a call the kernel refuses, each with the
+ arguments as passed and the value returned. The unload detaches
+ both
+ * - ``t3119-fmod-ret``
+ - Live ``fmod_ret`` links. On ``security_task_setnice()``, a target
+ on every kernel, the program fails the one call it is written
+ for, the caller gets its error, another call runs, and the unload
+ lets the refused call through again. On ``vfs_fallocate()``, on a
+ kernel whose BTF carries the tags of
+ ``CONFIG_BPF_FMODRET_SCAN``, the same holds for a listed error,
+ an ``fentry`` program runs before the skip, an ``fexit`` program
+ sees that error with the arguments of the skipped call, and a
+ program that returns an error outside the list is refused as
+ ``verifier_rejected``. On a kernel without tags, the load for
+ ``vfs_fallocate()`` is refused the same way
+ * - ``t3120-sleepable``
+ - Live ``fentry.s``, ``fmod_ret.s`` and ``fexit.s`` links attach to
+ the entry point of ``chdir(2)`` from one upatch and report their
+ kinds. Each program reads the path from user memory with
+ ``bpf_copy_from_user()``, which only a sleepable program may
+ call. The ``fmod_ret.s`` program fails the one call it is written
+ for, the caller gets its error, the other two programs see that
+ call, another call runs, and the unload detaches all three
``tpm`` (4)
~~~~~~~~~~~
@@ -1429,10 +1457,13 @@ several of them are tested, the counter is not.
Attach kinds
~~~~~~~~~~~~
-- ``fexit``, ``tp_btf`` and ``kretprobe`` have no test, offline or on a
- target.
-- ``fmod_ret`` and kprobe override are checked offline only
- (``t3008``).
+- ``tp_btf`` and ``kretprobe`` have no test, offline or on a target.
+- kprobe override is checked offline only (``t3008``).
+- ``t3119`` runs its cases for a tagged function only on a kernel whose
+ BTF carries the tags. On another kernel it checks the refusal instead.
+- ``t3120`` covers the sleepable forms on a system call entry point
+ only. The test kernel has no ``CONFIG_FUNCTION_ERROR_INJECTION``, so
+ no other function takes them there.
- kprobe loads run only on a kernel with a kprobe PMU. The test kernel
has none, so ``t6116`` leaves out its kprobe case.
diff --git a/testing/fixtures/fentry-fexit.bpf.c b/testing/fixtures/fentry-fexit.bpf.c
new file mode 100644
index 00000000..1f2c9c3a
--- /dev/null
+++ b/testing/fixtures/fentry-fexit.bpf.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+/* A length that only t3118 and t3119 pass to fallocate(2) in the VM. */
+#define TEST_LEN 1234567
+
+enum {
+ ENTRY_CALLS,
+ ENTRY_INODE,
+ EXIT_OK,
+ EXIT_FAILED,
+ EXIT_ERRNO,
+ EXIT_OFFSET,
+ NR_SEEN,
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, NR_SEEN);
+ __type(key, __u32);
+ __type(value, __u64);
+} seen SEC(".maps");
+
+static __always_inline void count(__u32 slot)
+{
+ __u64 *value = bpf_map_lookup_elem(&seen, &slot);
+
+ if (value)
+ __sync_fetch_and_add(value, 1);
+}
+
+static __always_inline void store(__u32 slot, __u64 last)
+{
+ __u64 *value = bpf_map_lookup_elem(&seen, &slot);
+
+ if (value)
+ *value = last;
+}
+
+SEC("fentry/vfs_fallocate")
+int BPF_PROG(see_entry, struct file *file, int mode, loff_t offset,
+ loff_t len)
+{
+ if (len != TEST_LEN)
+ return 0;
+ count(ENTRY_CALLS);
+ store(ENTRY_INODE, file->f_inode->i_ino);
+ return 0;
+}
+
+SEC("fexit/vfs_fallocate")
+int BPF_PROG(see_exit, struct file *file, int mode, loff_t offset,
+ loff_t len, int ret)
+{
+ if (len != TEST_LEN)
+ return 0;
+ if (!ret) {
+ count(EXIT_OK);
+ return 0;
+ }
+ count(EXIT_FAILED);
+ store(EXIT_ERRNO, -ret);
+ store(EXIT_OFFSET, offset);
+ return 0;
+}
diff --git a/testing/fixtures/fmod-ret-tagged.bpf.c b/testing/fixtures/fmod-ret-tagged.bpf.c
new file mode 100644
index 00000000..4dae1685
--- /dev/null
+++ b/testing/fixtures/fmod-ret-tagged.bpf.c
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+#define EFBIG 27
+
+/* As in fentry-fexit.bpf.c, which t3119 loads beside this. */
+#define TEST_LEN 1234567
+
+/*
+ * vfs_fallocate() is a target only by the tag of a kernel built with
+ * CONFIG_BPF_FMODRET_SCAN, and -EFBIG one of the errors the tag lists.
+ */
+SEC("fmod_ret/vfs_fallocate")
+int BPF_PROG(refuse_test_len, struct file *file, int mode, loff_t offset,
+ loff_t len, int ret)
+{
+ if (len == TEST_LEN)
+ return -EFBIG;
+ return 0;
+}
diff --git a/testing/fixtures/fmod-ret-unlisted.bpf.c b/testing/fixtures/fmod-ret-unlisted.bpf.c
new file mode 100644
index 00000000..4dfe7cc6
--- /dev/null
+++ b/testing/fixtures/fmod-ret-unlisted.bpf.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+#define ESRCH 3
+
+#define TEST_LEN 1234567
+
+/* vfs_fallocate() never returns -ESRCH, so no tag of it lists the error. */
+SEC("fmod_ret/vfs_fallocate")
+int BPF_PROG(return_unlisted, struct file *file, int mode, loff_t offset,
+ loff_t len, int ret)
+{
+ if (len == TEST_LEN)
+ return -ESRCH;
+ return 0;
+}
diff --git a/testing/fixtures/fmod-ret.bpf.c b/testing/fixtures/fmod-ret.bpf.c
new file mode 100644
index 00000000..eae268b4
--- /dev/null
+++ b/testing/fixtures/fmod-ret.bpf.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+#define EOPNOTSUPP 95
+
+/* A nice value that only t3119 asks for in the VM. */
+#define TEST_NICE 17
+
+/* security_*() is a target on every kernel, and its return value unchecked. */
+SEC("fmod_ret/security_task_setnice")
+int BPF_PROG(refuse_test_nice, struct task_struct *p, int nice, int ret)
+{
+ if (nice == TEST_NICE)
+ return -EOPNOTSUPP;
+ return 0;
+}
diff --git a/testing/fixtures/sleepable.bpf.c b/testing/fixtures/sleepable.bpf.c
new file mode 100644
index 00000000..2d588e05
--- /dev/null
+++ b/testing/fixtures/sleepable.bpf.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+/*
+ * A kernel without CONFIG_FUNCTION_ERROR_INJECTION takes sleepable
+ * programs of these kinds on the entry points of system calls alone,
+ * which carry the architecture in their name.
+ */
+#if defined(__TARGET_ARCH_x86)
+#define SYS_CHDIR "__x64_sys_chdir"
+#elif defined(__TARGET_ARCH_arm64)
+#define SYS_CHDIR "__arm64_sys_chdir"
+#else
+#error "no system call entry point known for this architecture"
+#endif
+
+#define EXDEV 18
+
+/* A path that only t3120 passes to chdir(2) in the VM. */
+static const char test_path[] = "/shirudo-test-sleepable";
+
+enum {
+ SEEN_ENTRY,
+ SEEN_EXIT,
+ NR_SEEN,
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, NR_SEEN);
+ __type(key, __u32);
+ __type(value, __u64);
+} seen SEC(".maps");
+
+static __always_inline void count(__u32 slot)
+{
+ __u64 *value = bpf_map_lookup_elem(&seen, &slot);
+
+ if (value)
+ __sync_fetch_and_add(value, 1);
+}
+
+/* bpf_copy_from_user() may sleep: the verifier refuses it anywhere else. */
+static __always_inline bool is_test_path(const struct pt_regs *regs)
+{
+ const char *filename = (const char *)PT_REGS_PARM1_CORE_SYSCALL(regs);
+ char path[sizeof(test_path)];
+ __u32 i;
+
+ if (bpf_copy_from_user(path, sizeof(path), filename))
+ return false;
+ for (i = 0; i < sizeof(path); i++) {
+ if (path[i] != test_path[i])
+ return false;
+ }
+ return true;
+}
+
+SEC("fentry.s/" SYS_CHDIR)
+int BPF_PROG(enter_chdir, const struct pt_regs *regs)
+{
+ if (is_test_path(regs))
+ count(SEEN_ENTRY);
+ return 0;
+}
+
+SEC("fmod_ret.s/" SYS_CHDIR)
+long BPF_PROG(refuse_chdir, const struct pt_regs *regs, long ret)
+{
+ if (is_test_path(regs))
+ return -EXDEV;
+ return 0;
+}
+
+SEC("fexit.s/" SYS_CHDIR)
+int BPF_PROG(exit_chdir, const struct pt_regs *regs, long ret)
+{
+ if (ret == -EXDEV && is_test_path(regs))
+ count(SEEN_EXIT);
+ return 0;
+}
diff --git a/testing/functional/upatch/t3118-fentry-fexit.sh b/testing/functional/upatch/t3118-fentry-fexit.sh
new file mode 100755
index 00000000..01266815
--- /dev/null
+++ b/testing/functional/upatch/t3118-fentry-fexit.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t3118 -- live fentry and fexit programs on one function: both links
+# attach and get names them, the fentry program sees a call with its
+# arguments and follows a pointer among them, the fexit program sees a
+# call that succeeded and a call the kernel refused, each with the
+# arguments as passed and the value returned, and an unload detaches
+# both.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+NAME=shirudo-test-fentry-fexit
+LOAD="$FIXTURES/$NAME.upatch"
+UNLOAD="$FIXTURES/$NAME.unload.upatch"
+DIR="/run/shirudo/bpffs/upatches/$NAME"
+WORK=$(mktemp -d)
+trap 'if [ -e "$DIR/link_0" ]; then "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true; fi; rm -rf "$WORK"' EXIT
+
+for fixture in "$LOAD" "$UNLOAD"; do
+ [ -s "$fixture" ] || test_skip_all "missing fentry and fexit fixture: $fixture"
+done
+
+test_expect_success 'both links are pinned' '
+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&
+ [ -e "$DIR/link_0" ] && [ -e "$DIR/link_1" ]
+'
+
+test_expect_success 'get reports both kinds and the function' '
+ "$SHIRUDO_BIN" upatch get "$NAME" --json >"$WORK/get.json" &&
+ python3 - "$WORK/get.json" <<'PY'
+import json
+import sys
+upatch = json.load(open(sys.argv[1], encoding="utf-8"))["upatch"]
+assert sorted(p["location"] for p in upatch["programs"]) == \
+ ["fentry/vfs_fallocate", "fexit/vfs_fallocate"], upatch
+PY
+'
+
+# The fixture watches the length 1234567 alone. A range whose end does not
+# fit a file offset fails in vfs_fallocate() itself, on every file system,
+# with EFBIG.
+test_expect_success 'a call that succeeds and a call that fails run through' '
+ python3 - "$WORK/file" >"$WORK/inode" <<'PY'
+import errno
+import os
+import sys
+fd = os.open(sys.argv[1], os.O_RDWR | os.O_CREAT, 0o600)
+os.posix_fallocate(fd, 0, 1234567)
+os.posix_fallocate(fd, 0, 4096)
+try:
+ os.posix_fallocate(fd, 2 ** 63 - 1234567, 1234567)
+except OSError as e:
+ assert e.errno == errno.EFBIG, e
+else:
+ raise AssertionError("a range past the largest offset was allocated")
+print(os.fstat(fd).st_ino)
+PY
+'
+
+test_expect_success 'each program saw both calls, with the arguments and the result' '
+ "$SHIRUDO_BIN" upatch get "$NAME" --json >"$WORK/get.json" &&
+ python3 - "$WORK/get.json" "$(cat "$WORK/inode")" <<'PY'
+import errno
+import json
+import sys
+doc = json.load(open(sys.argv[1], encoding="utf-8"))
+maps = doc["upatch"]["maps"]
+seen = next(map_data for map_data in maps if map_data["name"] == "seen")
+values = {int(entry["key"]): int(str(entry["value"]), 0)
+ for entry in seen["entries"]}
+entry_calls, entry_inode, exit_ok, exit_failed, exit_errno, exit_offset = \
+ (values[slot] for slot in range(6))
+assert entry_calls == 2 and entry_inode == int(sys.argv[2]), values
+assert exit_ok == 1 and exit_failed == 1, values
+assert exit_errno == errno.EFBIG, values
+assert exit_offset == 2 ** 63 - 1234567, values
+PY
+'
+
+test_expect_success 'unload removes the links' '
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&
+ [ ! -e "$DIR/link_0" ] && [ ! -e "$DIR/link_1" ]
+'
+
+test_done
diff --git a/testing/functional/upatch/t3119-fmod-ret.sh b/testing/functional/upatch/t3119-fmod-ret.sh
new file mode 100755
index 00000000..1e81fd54
--- /dev/null
+++ b/testing/functional/upatch/t3119-fmod-ret.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t3119 -- live fmod_ret programs. On security_task_setnice(), a target
+# on every kernel, the program fails the one call it is written for, and
+# the caller gets its error. On vfs_fallocate(), a target only by the tag
+# of a kernel built with CONFIG_BPF_FMODRET_SCAN, the same holds, an
+# fentry program runs before the skip, an fexit program sees the error
+# with the arguments of the skipped call, and a program that returns an
+# error outside the tag's list is refused. A kernel without tags refuses
+# the function.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+NAME=shirudo-test-fmod-ret
+TAGGED=shirudo-test-fmod-ret-tagged
+UNLISTED=shirudo-test-fmod-ret-unlisted
+OBSERVERS=shirudo-test-fentry-fexit
+PINS=/run/shirudo/bpffs/upatches
+WORK=$(mktemp -d)
+
+load() {
+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$1.upatch" --no-precheck \
+ >/dev/null 2>&1
+}
+
+unload() {
+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$1.unload.upatch" \
+ --no-precheck >/dev/null 2>&1
+}
+
+# Load a fixture the kernel has to refuse, and check how the load failed.
+refused() {
+ status=0
+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$1.upatch" --no-precheck \
+ --report="$WORK/report.json" >/dev/null 2>&1 || status=$?
+ [ "$status" = 1 ] &&
+ [ "$(report_field category <"$WORK/report.json")" = verifier_rejected ] &&
+ [ "$(report_field phase <"$WORK/report.json")" = payload_load ] &&
+ report_field verifier_log <"$WORK/report.json" | grep -q "$2" &&
+ [ ! -e "$PINS/$1" ]
+}
+
+# Each exits with the errno of the call, or 0.
+set_nice() {
+ python3 -c "import os, sys
+try:
+ os.setpriority(os.PRIO_PROCESS, 0, int(sys.argv[1]))
+except OSError as e:
+ sys.exit(e.errno)" "$1"
+}
+
+allocate() {
+ python3 -c "import os, sys
+fd = os.open(sys.argv[1], os.O_RDWR | os.O_CREAT, 0o600)
+try:
+ os.posix_fallocate(fd, 0, int(sys.argv[2]))
+except OSError as e:
+ sys.exit(e.errno)" "$WORK/file" "$1"
+}
+
+restore() {
+ for fixture in "$NAME" "$TAGGED" "$OBSERVERS"; do
+ [ ! -d "$PINS/$fixture" ] || unload "$fixture" || true
+ done
+ rm -rf "$WORK"
+}
+trap restore EXIT
+
+for fixture in "$NAME" "$TAGGED" "$OBSERVERS"; do
+ [ -s "$FIXTURES/$fixture.upatch" ] &&
+ [ -s "$FIXTURES/$fixture.unload.upatch" ] ||
+ test_skip_all "missing fmod_ret fixture: $fixture"
+done
+[ -s "$FIXTURES/$UNLISTED.upatch" ] ||
+ test_skip_all "missing fmod_ret fixture: $UNLISTED"
+
+EOPNOTSUPP=95
+EFBIG=27
+
+test_expect_success 'the nice value is set before the load' '
+ set_nice 17
+'
+
+test_expect_success 'the link is pinned and get reports the kind' '
+ load "$NAME" &&
+ [ -e "$PINS/$NAME/link" ] &&
+ "$SHIRUDO_BIN" upatch get "$NAME" --json >"$WORK/get.json" &&
+ python3 - "$WORK/get.json" <<'PY'
+import json
+import sys
+doc = json.load(open(sys.argv[1], encoding="utf-8"))
+location = doc["upatch"]["location"]
+assert location == "fmod_ret/security_task_setnice", doc
+PY
+'
+
+test_expect_success 'the call the program refuses fails with its error' '
+ status=0 &&
+ { set_nice 17 || status=$?; } &&
+ [ "$status" = "$EOPNOTSUPP" ]
+'
+
+test_expect_success 'a call it lets through still runs' '
+ set_nice 16
+'
+
+test_expect_success 'the unload lets the refused call through again' '
+ unload "$NAME" &&
+ [ ! -e "$PINS/$NAME/link" ] &&
+ set_nice 17
+'
+
+if grep -a -q "bpf_fmodret_retval:" /sys/kernel/btf/vmlinux; then
+ test_expect_success 'a tagged function takes a program that returns a listed error' '
+ load "$OBSERVERS" &&
+ load "$TAGGED" &&
+ [ -e "$PINS/$TAGGED/link" ]
+ '
+
+ test_expect_success 'the call it refuses fails with that error, another one runs' '
+ status=0 &&
+ { allocate 1234567 || status=$?; } &&
+ [ "$status" = "$EFBIG" ] &&
+ allocate 4096
+ '
+
+ test_expect_success 'fentry runs before the skip, fexit sees its error and arguments' '
+ "$SHIRUDO_BIN" upatch get "$OBSERVERS" --json >"$WORK/get.json" &&
+ python3 - "$WORK/get.json" <<'PY'
+import errno
+import json
+import sys
+doc = json.load(open(sys.argv[1], encoding="utf-8"))
+maps = doc["upatch"]["maps"]
+seen = next(map_data for map_data in maps if map_data["name"] == "seen")
+values = {int(entry["key"]): int(str(entry["value"]), 0)
+ for entry in seen["entries"]}
+entry_calls, entry_inode, exit_ok, exit_failed, exit_errno, exit_offset = \
+ (values[slot] for slot in range(6))
+assert entry_calls == 1, values
+assert exit_ok == 0 and exit_failed == 1, values
+assert exit_errno == errno.EFBIG and exit_offset == 0, values
+PY
+ '
+
+ test_expect_success 'the unload lets the refused call through again' '
+ unload "$TAGGED" &&
+ allocate 1234567 &&
+ unload "$OBSERVERS"
+ '
+
+ test_expect_success 'a program that returns an error outside the list is refused' '
+ refused "$UNLISTED" "should be 0 or one of"
+ '
+else
+ test_expect_success 'a kernel without tags refuses the function' '
+ refused "$TAGGED" "vfs_fallocate() is not modifiable"
+ '
+fi
+
+test_done
diff --git a/testing/functional/upatch/t3120-sleepable.sh b/testing/functional/upatch/t3120-sleepable.sh
new file mode 100755
index 00000000..c46b54c0
--- /dev/null
+++ b/testing/functional/upatch/t3120-sleepable.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t3120 -- live fentry.s, fmod_ret.s and fexit.s programs on the entry
+# point of chdir(2): the three links attach and get names them, each
+# program reads the path from user memory with a helper only a sleepable
+# program may call, the fmod_ret.s program fails the one call it is
+# written for and the caller gets its error, the other two see that
+# call, and an unload detaches all three.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+NAME=shirudo-test-sleepable
+LOAD="$FIXTURES/$NAME.upatch"
+UNLOAD="$FIXTURES/$NAME.unload.upatch"
+DIR="/run/shirudo/bpffs/upatches/$NAME"
+WORK=$(mktemp -d)
+trap 'if [ -e "$DIR/link_0" ]; then "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true; fi; rm -rf "$WORK"' EXIT
+
+for fixture in "$LOAD" "$UNLOAD"; do
+ [ -s "$fixture" ] || test_skip_all "missing sleepable fixture: $fixture"
+done
+
+# Exits with the errno of chdir(2) to the path, or 0.
+change_dir() {
+ python3 -c "import os, sys
+try:
+ os.chdir(sys.argv[1])
+except OSError as e:
+ sys.exit(e.errno)" "$1"
+}
+
+TEST_PATH=/shirudo-test-sleepable
+ENOENT=2
+EXDEV=18
+
+test_expect_success 'the path does not exist before the load' '
+ status=0 &&
+ { change_dir "$TEST_PATH" || status=$?; } &&
+ [ "$status" = "$ENOENT" ]
+'
+
+test_expect_success 'the three links are pinned' '
+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&
+ [ -e "$DIR/link_0" ] && [ -e "$DIR/link_1" ] && [ -e "$DIR/link_2" ]
+'
+
+test_expect_success 'get reports the three kinds on the entry point' '
+ "$SHIRUDO_BIN" upatch get "$NAME" --json >"$WORK/get.json" &&
+ python3 - "$WORK/get.json" <<'PY'
+import json
+import sys
+upatch = json.load(open(sys.argv[1], encoding="utf-8"))["upatch"]
+locations = sorted(p["location"] for p in upatch["programs"])
+kinds = [location.split("/")[0] for location in locations]
+assert kinds == ["fentry", "fexit", "fmod_ret"], upatch
+assert all(location.endswith("_sys_chdir") for location in locations), upatch
+PY
+'
+
+test_expect_success 'the call the program refuses fails with its error' '
+ status=0 &&
+ { change_dir "$TEST_PATH" || status=$?; } &&
+ [ "$status" = "$EXDEV" ]
+'
+
+test_expect_success 'a call it lets through still runs' '
+ change_dir /
+'
+
+test_expect_success 'the entry and the exit program saw the refused call' '
+ "$SHIRUDO_BIN" upatch get "$NAME" --json >"$WORK/get.json" &&
+ python3 - "$WORK/get.json" <<'PY'
+import json
+import sys
+doc = json.load(open(sys.argv[1], encoding="utf-8"))
+maps = doc["upatch"]["maps"]
+seen = next(map_data for map_data in maps if map_data["name"] == "seen")
+values = {int(entry["key"]): int(str(entry["value"]), 0)
+ for entry in seen["entries"]}
+assert values == {0: 1, 1: 1}, values
+PY
+'
+
+test_expect_success 'unload removes the links and the refusal' '
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&
+ [ ! -e "$DIR/link_0" ] && [ ! -e "$DIR/link_2" ] &&
+ status=0 &&
+ { change_dir "$TEST_PATH" || status=$?; } &&
+ [ "$status" = "$ENOENT" ]
+'
+
+test_done
diff --git a/testing/vm/build-bundle-payload.sh b/testing/vm/build-bundle-payload.sh
index a43e414b..770e5f38 100755
--- a/testing/vm/build-bundle-payload.sh
+++ b/testing/vm/build-bundle-payload.sh
@@ -172,6 +172,7 @@ build_fixture()
local source="$1"
local name="$2"
local keys_dir="$3"
+ local cflags="${4:-}"
(
cd "$REPO_ROOT"
@@ -179,7 +180,7 @@ build_fixture()
"$REPO_ROOT/testing/fixtures/$source" \
-o "$TMP_DIR/fixtures/$name.upatch" \
--name "$name" --vmlinux "$REPO_ROOT/vendor/linux/vmlinux.h" \
- --extra-cflags "-I$REPO_ROOT/vendor/libbpf" \
+ --extra-cflags "-I$REPO_ROOT/vendor/libbpf${cflags:+ $cflags}" \
--key "$keys_dir/signing_key.pem" \
--cert "$keys_dir/cert.der"
)
@@ -871,6 +872,17 @@ queue_fixture build_fixture uprobe.bpf.c shirudo-test-uprobe "$FIXTURE_WORK/trus
queue_fixture build_fixture uretprobe.bpf.c shirudo-test-uretprobe "$FIXTURE_WORK/trusted"
queue_fixture build_fixture kprobe-multi.bpf.c shirudo-test-kprobe-multi "$FIXTURE_WORK/trusted"
queue_fixture build_fixture kprobe-multi-missing.bpf.c shirudo-test-kprobe-multi-missing "$FIXTURE_WORK/trusted"
+queue_fixture build_fixture fentry-fexit.bpf.c shirudo-test-fentry-fexit "$FIXTURE_WORK/trusted"
+queue_fixture build_fixture fmod-ret.bpf.c shirudo-test-fmod-ret "$FIXTURE_WORK/trusted"
+queue_fixture build_fixture fmod-ret-tagged.bpf.c shirudo-test-fmod-ret-tagged "$FIXTURE_WORK/trusted"
+queue_fixture build_fixture fmod-ret-unlisted.bpf.c shirudo-test-fmod-ret-unlisted "$FIXTURE_WORK/trusted"
+# The entry point of a system call carries the architecture in its name.
+case "$TEST_VM_ARCH" in
+aarch64) fixture_arch=arm64 ;;
+*) fixture_arch=x86 ;;
+esac
+queue_fixture build_fixture sleepable.bpf.c shirudo-test-sleepable "$FIXTURE_WORK/trusted" \
+ "-D__TARGET_ARCH_$fixture_arch"
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"
@@ -924,6 +936,18 @@ uretprobe_single_payload="$($REPO_ROOT/shirudo upatch info \
kprobe_multi_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/shirudo-test-kprobe-multi.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+fentry_fexit_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/shirudo-test-fentry-fexit.upatch" --json |
+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+sleepable_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/shirudo-test-sleepable.upatch" --json |
+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+fmod_ret_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/shirudo-test-fmod-ret.upatch" --json |
+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+fmod_ret_tagged_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/shirudo-test-fmod-ret-tagged.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"])')"
@@ -1007,6 +1031,10 @@ shirudo-test-uprobe-multi-sleepable $uprobe_sleepable_payload shirudo-test-uprob
shirudo-test-uprobe $uprobe_single_payload shirudo-test-uprobe.unload.upatch
shirudo-test-uretprobe $uretprobe_single_payload shirudo-test-uretprobe.unload.upatch
shirudo-test-kprobe-multi $kprobe_multi_payload shirudo-test-kprobe-multi.unload.upatch
+shirudo-test-fentry-fexit $fentry_fexit_payload shirudo-test-fentry-fexit.unload.upatch
+shirudo-test-sleepable $sleepable_payload shirudo-test-sleepable.unload.upatch
+shirudo-test-fmod-ret $fmod_ret_payload shirudo-test-fmod-ret.unload.upatch
+shirudo-test-fmod-ret-tagged $fmod_ret_tagged_payload shirudo-test-fmod-ret-tagged.unload.upatch
EOF
wait_fixture_jobs