Skip to content

eBPF-based security platform

testing: check retries when config mapping removal is busy

KP Singh091d3c4e05c2
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 9b544d6f..153685bf 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -576,6 +576,7 @@ verity-test-helpers: $(VERITY_TEST_HELPERS) build/verity-tests/shirudo-image-fau
# Compiled test helpers (also usable as fuzz entry points).
TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-image-cli \
+ testing/helper/test-image-cleanup \
testing/helper/test-image-config \
testing/helper/test-config-image \
testing/helper/test-config-update \
@@ -794,6 +795,16 @@ testing/helper/test-image-cli: testing/helper/test-image-cli.c builtin-bpm.c \
-Wl,--wrap=bpm_tree_fetch_publication \
$(STATIC_LIBS) $(DYNAMIC_LIBS)
+testing/helper/test-image-cleanup: testing/helper/test-image-cleanup.c \
+ testing/helper/image-cleanup.h util/image_runtime.c $(BUILTIN_OBJS) \
+ $(filter-out $(call obj,util/image_runtime.o),$(UTIL_OBJS)) \
+ $(VERSION_OBJ) $(LIBBPF_A)
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(RPATH_LDFLAGS) -o $@ \
+ $(filter-out %.h util/image_runtime.c,$^) \
+ -Wl,--wrap=shirudo_dm_verity_remove,--wrap=usleep \
+ $(STATIC_LIBS) $(DYNAMIC_LIBS)
+
test-helpers: $(TEST_HELPERS)
# ----- fuzzing ----------------------------------------------------
diff --git a/testing/helper/image-cleanup.h b/testing/helper/image-cleanup.h
new file mode 100644
index 00000000..d6930fc7
--- /dev/null
+++ b/testing/helper/image-cleanup.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_TEST_IMAGE_CLEANUP_H
+#define SHIRUDO_TEST_IMAGE_CLEANUP_H
+
+#include <unistd.h>
+
+#include "util/dm_verity.h"
+
+int __wrap_shirudo_dm_verity_remove(struct shirudo_dm_verity *device,
+ bool deferred);
+int __wrap_usleep(useconds_t usec);
+
+#endif /* SHIRUDO_TEST_IMAGE_CLEANUP_H */
diff --git a/testing/helper/test-image-cleanup.c b/testing/helper/test-image-cleanup.c
new file mode 100644
index 00000000..3e6a4087
--- /dev/null
+++ b/testing/helper/test-image-cleanup.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+
+#include "../../util/image_runtime.c"
+#include "image-cleanup.h"
+
+static struct shirudo_dm_verity candidate;
+static __u32 busy_calls, remove_calls, sleep_calls;
+static int remove_result;
+static bool invalid_call;
+
+int __wrap_shirudo_dm_verity_remove(struct shirudo_dm_verity *device,
+ bool deferred)
+{
+ if (device != &candidate || deferred || remove_calls != sleep_calls)
+ invalid_call = true;
+ remove_calls++;
+ return remove_calls <= busy_calls ? -EBUSY : remove_result;
+}
+
+int __wrap_usleep(useconds_t usec)
+{
+ if (usec != 10000 || remove_calls != sleep_calls + 1)
+ invalid_call = true;
+ sleep_calls++;
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ __u32 expected_calls = 1;
+ int expected_result = 0;
+ int rc;
+
+ if (argc != 2)
+ return 2;
+ if (!strcmp(argv[1], "immediate")) {
+ busy_calls = 0;
+ } else if (!strcmp(argv[1], "transient")) {
+ busy_calls = 2;
+ expected_calls = 3;
+ } else if (!strcmp(argv[1], "persistent")) {
+ busy_calls = 100;
+ expected_calls = 100;
+ expected_result = -EBUSY;
+ } else if (!strcmp(argv[1], "io")) {
+ remove_result = -EIO;
+ expected_result = -EIO;
+ } else if (!strcmp(argv[1], "permission")) {
+ remove_result = -EPERM;
+ expected_result = -EPERM;
+ } else {
+ return 2;
+ }
+ rc = remove_candidate(&candidate);
+ printf("rc=%d removes=%u sleeps=%u invalid=%d\n", rc,
+ remove_calls, sleep_calls, invalid_call);
+ return rc != expected_result || remove_calls != expected_calls ||
+ sleep_calls != expected_calls - 1 || invalid_call;
+}
diff --git a/testing/t0052-image-cleanup.sh b/testing/t0052-image-cleanup.sh
new file mode 100755
index 00000000..ed04a13d
--- /dev/null
+++ b/testing/t0052-image-cleanup.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+cd "$(dirname "$0")"
+. ./test-lib.sh
+
+H=${SHIRUDO_TEST_IMAGE_CLEANUP_HELPER:-./helper/test-image-cleanup}
+if [ ! -x "$H" ]; then
+ printf "not ok 1 - test-image-cleanup helper is required\n1..1\n"
+ exit 1
+fi
+
+test_expect_success 'candidate removal succeeds without a retry or delay' '
+ "$H" immediate
+'
+
+test_expect_success 'candidate removal retries transient EBUSY synchronously' '
+ "$H" transient
+'
+
+test_expect_success 'persistent EBUSY stops after 100 attempts and 99 delays' '
+ "$H" persistent
+'
+
+test_expect_success 'candidate removal returns an I/O error immediately' '
+ "$H" io
+'
+
+test_expect_success 'candidate removal returns a permission error immediately' '
+ "$H" permission
+'
+
+test_done