eBPF-based security platform
testing: cover kernel-obs exec paths
John Fastabend5c09b22d27f0
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 4e6edabe..e471af5a 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -507,7 +507,8 @@ TEST_HELPERS := testing/helper/test-json-parser \testing/helper/test-upatch-decode \testing/helper/test-upatch-serializer-pages \testing/helper/test-signed-load \- testing/helper/test-exec-churn+ testing/helper/test-exec-churn \+ testing/kernel-obs/exec-pathstesting/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o@printf ' CC $@\n'@@ -622,6 +623,10 @@ testing/helper/test-exec-churn: testing/helper/test-exec-churn.c util/io.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^+testing/kernel-obs/exec-paths: testing/kernel-obs/exec_paths.c+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) -pthread -o $@ $^+test-helpers: $(TEST_HELPERS)check test: shirudo $(TEST_HELPERS)diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex dbe726b5..49340294 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -294,7 +294,8 @@ trap cleanup EXITmake -C "$REPO_ROOT" DESTDIR="$TMP_DIR/root" install-target-install -d "$TMP_DIR/test-helpers/testing/helper"+install -d "$TMP_DIR/test-helpers/testing/helper" \+ "$TMP_DIR/test-helpers/testing/kernel-obs"while IFS= read -r helper; doinstall -m 0755 "$REPO_ROOT/$helper" "$TMP_DIR/test-helpers/$helper"done <<'EOF'@@ -311,6 +312,7 @@ testing/helper/test-upatch-decodetesting/helper/test-upatch-serializer-pagestesting/helper/test-signed-loadtesting/helper/test-exec-churn+testing/kernel-obs/exec-pathstesting/helper/otel-mock.pytesting/helper/test-oci-registry.pyEOFdiff --git a/testing/Makefile b/testing/Makefileindex fa9e39fa..0863eae2 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -101,7 +101,8 @@ TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \$(ROOT)/vendor/libbpf/include/linux/*.h \$(ROOT)/vendor/libbpf/include/uapi/linux/*.h \$(ROOT)/vendor/libbpf/include/asm/*.h \- $(ROOT)/testing/helper/*.c $(ROOT)/testing/helper/*.py $(ROOT)/completion/* $(ROOT)/systemd/*.in \+ $(ROOT)/testing/helper/*.c $(ROOT)/testing/helper/*.py \+ $(ROOT)/testing/kernel-obs/*.c $(ROOT)/completion/* $(ROOT)/systemd/*.in \$(ROOT)/systemd/*.timer $(ROOT)/testing/fixtures/*.c $(ROOT)/testing/fixtures/*.lua \$(ROOT)/testing/fixtures/decoders/* \$(ROOT)/upatches/kernel-obs/*.c $(ROOT)/upatches/kernel-obs/*.h $(ROOT)/upatches/kernel-obs/*.list \diff --git a/testing/kernel-obs/exec_paths.c b/testing/kernel-obs/exec_paths.cnew file mode 100644index 00000000..9683c36b--- /dev/null+++ b/testing/kernel-obs/exec_paths.c@@ -0,0 +1,219 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <fcntl.h>+#include <linux/sched.h>+#include <pthread.h>+#include <sched.h>+#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/mman.h>+#include <sys/syscall.h>+#include <sys/types.h>+#include <sys/wait.h>+#include <unistd.h>++extern char **environ;++#ifndef SYS_clone3+#define SYS_clone3 435+#endif++#define CLONE_STACK_SIZE (1024 * 1024)++struct clone_context {+ const char *path;+};++static void exec_payload(const char *path)+{+ char *const argv[] = { (char *)path, (char *)"--payload", NULL };++ execve(path, argv, environ);+ _exit(127);+}++static int wait_child(pid_t pid)+{+ int status;++ while (waitpid(pid, &status, 0) < 0) {+ if (errno != EINTR)+ return -1;+ }+ return WIFEXITED(status) && WEXITSTATUS(status) == 0 ? 0 : -1;+}++static int fork_exec(const char *path)+{+ pid_t pid = fork();++ if (pid == 0)+ exec_payload(path);+ if (pid < 0)+ return -1;+ return wait_child(pid);+}++static int vfork_exec(const char *path)+{+ pid_t pid = vfork();++ if (pid == 0)+ exec_payload(path);+ if (pid < 0)+ return -1;+ return wait_child(pid);+}++static int clone_child(void *data)+{+ struct clone_context *context = data;++ exec_payload(context->path);+ return 127;+}++static int clone_exec(const char *path)+{+ struct clone_context context = { .path = path };+ void *stack = mmap(NULL, CLONE_STACK_SIZE, PROT_READ | PROT_WRITE,+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);+ pid_t pid;+ int result;++ if (stack == MAP_FAILED)+ return -1;+ pid = clone(clone_child, (char *)stack + CLONE_STACK_SIZE, SIGCHLD,+ &context);+ if (pid < 0) {+ munmap(stack, CLONE_STACK_SIZE);+ return -1;+ }+ result = wait_child(pid);+ munmap(stack, CLONE_STACK_SIZE);+ return result;+}++static int clone3_exec(const char *path)+{+ struct clone_args args = {};+ pid_t pid;++ args.exit_signal = SIGCHLD;+ pid = syscall(SYS_clone3, &args, sizeof(args));+ if (pid == 0)+ exec_payload(path);+ if (pid < 0)+ return -1;+ return wait_child(pid);+}++static void exec_fd(const char *path, int use_fexecve)+{+ char *const args[] = { (char *)path, NULL };+ int fd = open(path, O_RDONLY);++ if (fd < 0)+ _exit(126);+ if (use_fexecve)+ fexecve(fd, args, environ);+ else+ syscall(SYS_execveat, fd, "", args, environ, AT_EMPTY_PATH);+ _exit(127);+}++static int fd_exec_path(const char *path, int use_fexecve)+{+ pid_t pid = fork();++ if (pid == 0)+ exec_fd(path, use_fexecve);+ if (pid < 0)+ return -1;+ return wait_child(pid);+}++static int make_script(const char *directory, char *path, size_t path_size)+{+ static const char script[] = "#!/bin/sh\nexit 0\n";+ ssize_t written;+ int length;+ int fd;++ length = snprintf(path, path_size, "%s/exec-script", directory);+ if (length < 0 || (size_t)length >= path_size)+ return -1;+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0700);+ if (fd < 0)+ return -1;+ written = write(fd, script, sizeof(script) - 1);+ if (close(fd) < 0 || written != sizeof(script) - 1)+ return -1;+ return 0;+}++static void *thread_exec(void *data)+{+ exec_payload(data);+ return NULL;+}++static int thread_exec_path(const char *path)+{+ pthread_t thread;+ int result;++ result = pthread_create(&thread, NULL, thread_exec, (void *)path);+ if (result)+ return -1;+ result = pthread_join(thread, NULL);+ return result ? -1 : 0;+}++int main(int argc, char **argv)+{+ char script[4096];+ const char *self = argv[0];++ if (argc == 2 && !strcmp(argv[1], "--payload"))+ return 0;+ if (argc == 3 && !strcmp(argv[1], "--replace"))+ exec_payload(argv[2]);+ if (argc == 3 && !strcmp(argv[1], "--execveat-empty"))+ return fd_exec_path(argv[2], 0) ? 1 : 0;+ if (argc == 3 && !strcmp(argv[1], "--fexecve"))+ return fd_exec_path(argv[2], 1) ? 1 : 0;+ if (argc != 2 || make_script(argv[1], script, sizeof(script))) {+ fprintf(stderr, "could not create shebang test executable\n");+ return 1;+ }+ if (fork_exec(self)) {+ fprintf(stderr, "fork() -> execve() case failed\n");+ return 1;+ }+ if (vfork_exec(self)) {+ fprintf(stderr, "vfork() -> execve() case failed\n");+ return 1;+ }+ if (clone_exec(self)) {+ fprintf(stderr, "clone() -> execve() case failed\n");+ return 1;+ }+ if (clone3_exec(self)) {+ fprintf(stderr, "clone3() -> execve() case failed\n");+ return 1;+ }+ if (fork_exec(script)) {+ fprintf(stderr, "fork() -> shebang execve() case failed\n");+ return 1;+ }+ if (thread_exec_path(self)) {+ fprintf(stderr, "non-leader thread execve() case failed\n");+ return 1;+ }+ return 0;+}diff --git a/testing/kernel-obs/kernel_obs_events.py b/testing/kernel-obs/kernel_obs_events.pyindex 76cca008..ac5dee78 100755--- a/testing/kernel-obs/kernel_obs_events.py+++ b/testing/kernel-obs/kernel_obs_events.py@@ -23,6 +23,15 @@ def exec_event():shutil.copyfile("/bin/true", executable)os.chmod(executable, 0o755)subprocess.run([executable], check=True)+ subprocess.run(["./kernel-obs/exec-paths", directory], check=True)+ for method in ("execveat-empty", "fexecve"):+ fd_executable = os.path.join(directory, f"true-{method}")+ shutil.copyfile("/bin/true", fd_executable)+ os.chmod(fd_executable, 0o755)+ subprocess.run(["./kernel-obs/exec-paths", f"--{method}",+ fd_executable], check=True)+ subprocess.run(["./kernel-obs/exec-paths", "--replace", executable],+ check=True)def tcp_event():diff --git a/testing/kernel-obs/t1210-kernel-obs.sh b/testing/kernel-obs/t1210-kernel-obs.shindex e724179d..c911b800 100755--- a/testing/kernel-obs/t1210-kernel-obs.sh+++ b/testing/kernel-obs/t1210-kernel-obs.sh@@ -91,6 +91,40 @@ exec_ids = {(r["executable"]["dev"], r["executable"]["ino"])assert any(r["name"] == "true" and r["path"].endswith("/true")for r in tables["exec"]), tables["exec"]assert any("caller" in r for r in tables["exec"]), tables["exec"]+helper_records = [r for r in tables["exec"]+ if r["path"].endswith("/kernel-obs/exec-paths")]+helper = next(r for r in helper_records+ if (r.get("caller", {}).get("dev"),+ r.get("caller", {}).get("ino")) !=+ (r["executable"]["dev"], r["executable"]["ino"]))+helper_id = (helper["executable"]["dev"], helper["executable"]["ino"])+self_exec = [r for r in tables["exec"]+ if (r["executable"]["dev"], r["executable"]["ino"]) == helper_id+ and (r.get("caller", {}).get("dev"),+ r.get("caller", {}).get("ino")) == helper_id]+assert sum(int(r["exec_count"]) for r in self_exec) >= 5, self_exec+replace_exec = [r for r in tables["exec"]+ if r["path"].endswith("/true") and+ (r.get("caller", {}).get("dev"),+ r.get("caller", {}).get("ino")) == helper_id]+assert replace_exec and any(r["pid"] == helper["pid"]+ for r in replace_exec), (helper, replace_exec)+for method in ("execveat-empty", "fexecve"):+ fd_exec = [r for r in tables["exec"]+ if r["path"].endswith(f"/true-{method}") and+ (r.get("caller", {}).get("dev"),+ r.get("caller", {}).get("ino")) == helper_id]+ assert fd_exec, (method, tables["exec"])+script = [r for r in tables["exec"]+ if r["path"].endswith("/exec-script") and+ (r.get("caller", {}).get("dev"),+ r.get("caller", {}).get("ino")) == helper_id]+assert script and not any(r["interpreter"] for r in script), script+script_id = (script[0]["executable"]["dev"], script[0]["executable"]["ino"])+assert any(r["interpreter"] and+ (r.get("caller", {}).get("dev"), r.get("caller", {}).get("ino")) ==+ script_id and r["pid"] == script[0]["pid"]+ for r in tables["exec"]), tables["exec"]for name in ("net", "dns"):assert any((r["executable"]["dev"], r["executable"]["ino"]) in exec_idsfor r in tables[name]), (name, tables[name])