eBPF-based security platform
upatch: report a pin directory read that fails partway
Javier5b6561f4650a
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 4f74ae89..7c2ffd88 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -1140,6 +1140,7 @@ build/unit/%: testing/unit/%.c testing/unit/unit.c testing/unit/unit.h \$(UNIT_OBJS) $(UNIT_LIB) $(LIBBPF_A) $(DYNAMIC_LIBS)build/unit/curl: LDFLAGS += -Wl,--wrap=curl_slist_free_all+build/unit/upatch_pins: LDFLAGS += -Wl,--wrap=opendir,--wrap=readdir64unit-tests: $(UNIT_TESTS)diff --git a/builtin-bpm.c b/builtin-bpm.cindex 99233ed9..09218e2d 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -2176,8 +2176,13 @@ static int tree_recover_pending(const struct tree_publication *pub,history_root);if (ret)return ret;- absent = !strcmp(op->op, "add") && !upatch_loaded_pins(op->id) &&- !java_agent_loaded(op->id);+ absent = false;+ if (!strcmp(op->op, "add")) {+ ret = upatch_loaded_pins(op->id);+ if (ret < 0)+ return ret;+ absent = !ret && !java_agent_loaded(op->id);+ }ret = tree_outcome_record(op->seq, digest,absent ? TREE_OUTCOME_MODULE_ABSENT :TREE_OUTCOME_APPLIED,diff --git a/builtin-init.c b/builtin-init.cindex e57423ce..d549bbd7 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -1086,6 +1086,7 @@ static int boot_restore_locked(bool recorded,char pinned_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];char path[PATH_MAX];struct upatch u;+ int loaded;if (ret)goto next;@@ -1122,7 +1123,14 @@ static int boot_restore_locked(bool recorded,cnt++;goto next;}- if (upatch_loaded_pins(set[cnt].name)) {+ loaded = upatch_loaded_pins(set[cnt].name);+ if (loaded < 0) {+ pr_err("boot-restore: cannot read the pins of %s: %s",+ set[cnt].name, strerror(-loaded));+ ret = loaded;+ goto next;+ }+ if (loaded) {upatch_payload_sha256(set[cnt].name, pinned_hex,sizeof(pinned_hex));if (strcmp(pinned_hex, set[cnt].payload_hex) != 0) {diff --git a/builtin-upatch.c b/builtin-upatch.cindex 0fbc377a..051e9bea 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -651,15 +651,18 @@ static int is_not_dot(const struct dirent *e)return strcmp(e->d_name, ".") && strcmp(e->d_name, "..");}-static bool upatch_is_loaded(const char *name)+/* 1 when loaded, 0 when not, -errno when the pins cannot be read. */+static int upatch_is_loaded(const char *name){char pin[PATH_MAX];struct stat st;+ int ret;- if (upatch_loaded_pins(name))- return true;+ ret = upatch_loaded_pins(name);+ if (ret)+ return ret;if (upatch_pin_path(pin, sizeof(pin), name, "test", NULL))- return false;+ return 0;return stat(pin, &st) == 0;}@@ -763,7 +766,7 @@ static int cmd_upatch_list(int argc, const char **argv)const char *type, *payload_type;struct list_row *row;struct upatch u;- bool loaded;+ int loaded;if (strsprintf(path, sizeof(path), "%s/%s",UPATCHES_DIR,@@ -787,19 +790,30 @@ static int cmd_upatch_list(int argc, const char **argv)loaded = u.hdr.type == UPATCH_TYPE_JAVA ?java_agent_loaded(name) : upatch_is_loaded(name);upatch_close(&u);+ if (loaded < 0) {+ pr_err("upatch list: cannot read the pins of %s: %s",+ name, strerror(-loaded));+ ret = 1;+ }if (json) {json_obj_open(&e);json_kv_string(&e, "name", name);json_kv_string(&e, "type", type);json_kv_string(&e, "payload_type", payload_type);- json_kv_bool(&e, "loaded", loaded);+ if (loaded < 0)+ json_kv_null(&e, "loaded");+ else+ json_kv_bool(&e, "loaded", loaded);json_kv_bool(&e, "archived", 1);json_kv_string(&e, "payload_sha256", payload_hex);json_obj_close(&e);} else {row = list_row_new(rows, &n_rows, name);- row->loaded = loaded ? "yes" : "no";+ if (loaded < 0)+ row->loaded = "?";+ else+ row->loaded = loaded ? "yes" : "no";row->type = type;row->kind = payload_type;memcpy(row->payload, payload_hex, 16);@@ -4983,7 +4997,13 @@ static int collect_one(const char *name, void *ctx)bool skip;int ret;- if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs)+ ret = upatch_pins_read(name, &pins);+ if (ret && ret != -ENOENT) {+ rep_fail(c->rep, "pre_flight", "unknown", -ret,+ "cannot read the pins of an upatch");+ return 1;+ }+ if (ret || !pins.nr_xdp_progs)return 0;grown = xreallocarray(c->work, c->nr, c->nr + 1, sizeof(*c->work));if (!grown) {diff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.cindex 50433a9d..7d7e36a4 100644--- a/testing/proofs/cbmc/bpm-tree-sync-model.c+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c@@ -589,9 +589,9 @@ int tree_outcome_append(__s64 seq, enum tree_outcome outcome,return 0;}-bool upatch_loaded_pins(const char *name)+int upatch_loaded_pins(const char *name){- return pins_loaded;+ return fail_here() ? -EIO : pins_loaded;}/* The model's upatches are BPF ones. */diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.cindex a5e6b9b2..b736ecc3 100644--- a/testing/unit/upatch_pins.c+++ b/testing/unit/upatch_pins.c@@ -1,15 +1,62 @@// SPDX-License-Identifier: BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */+#include <dirent.h>#include <errno.h>+#include <fcntl.h>#include <limits.h>#include <string.h>#include <unistd.h>+#include <shirudo/string.h>+#include "paths.h"#include "unit.h"#include "upatch_pins.h"+/*+ * readdir() fails only when the kernel or the libc makes it fail, which a+ * test cannot ask for, so these count the reads down and report EIO for the+ * one after the last, as a read that fails partway through does. opendir()+ * opens the directory of the case, because the pin path that the readers+ * resolve is not a directory the test may write. The readers call+ * readdir64, which readdir is with _FILE_OFFSET_BITS=64.+ */+static char test_dir[PATH_MAX];+static int reads_left = -1;++DIR *__real_opendir(const char *path);+DIR *__wrap_opendir(const char *path);+DIR *__wrap_opendir(const char *path)+{+ return __real_opendir(test_dir[0] ? test_dir : path);+}++struct dirent *__real_readdir64(DIR *d);+struct dirent *__wrap_readdir64(DIR *d);+struct dirent *__wrap_readdir64(DIR *d)+{+ if (reads_left == 0) {+ errno = EIO;+ return NULL;+ }+ if (reads_left > 0)+ reads_left--;+ return __real_readdir64(d);+}++static void read_entries_of(const char *dir, int reads)+{+ ASSERT_TRUE(strscpy(test_dir, dir, sizeof(test_dir)) >= 0);+ reads_left = reads;+}++static void read_entries_of_no_dir(void)+{+ test_dir[0] = '\0';+ reads_left = -1;+}+static void names_are_parsed_strictly(void){static const char * const ignored[] = {@@ -97,7 +144,7 @@ static void absent_upatch_has_no_pins(void)EXPECT_EQ(upatch_pins_read("shirudo-unit-no-such-upatch", &pins),-ENOENT);EXPECT_FALSE(pins.has_link);- EXPECT_FALSE(upatch_loaded_pins("shirudo-unit-no-such-upatch"));+ EXPECT_EQ(upatch_loaded_pins("shirudo-unit-no-such-upatch"), 0);}static void xdp_program_pins_have_one_name(void)@@ -164,6 +211,84 @@ static void no_pin_directory_is_no_upatch(void)EXPECT_EQ(seen, 0);}+static int count_link(__u32 index, const char *pin, void *ctx)+{+ (*(int *)ctx)++;+ return 0;+}++static void make_entry(const char *dir, const char *name)+{+ char path[PATH_MAX];+ int fd;++ ASSERT_EQ(path_join(path, sizeof(path), dir, name), 0);+ fd = open(path, O_CREAT | O_WRONLY | O_CLOEXEC, 0600);+ ASSERT_TRUE(fd >= 0);+ close(fd);+}++static void drop_entry(const char *dir, const char *name)+{+ char path[PATH_MAX];++ ASSERT_EQ(path_join(path, sizeof(path), dir, name), 0);+ ASSERT_EQ(unlink(path), 0);+}++static void pin_entries_are_walked(void)+{+ char dir[] = "/tmp/shirudo-unit-pins-XXXXXX";+ static const char * const entries[] = { "link_0", "link_2", "alpha" };+ struct upatch_pins pins = {};+ size_t i;+ int seen = 0;++ ASSERT_TRUE(mkdtemp(dir));+ for (i = 0; i < ARRAY_SIZE(entries); i++)+ make_entry(dir, entries[i]);++ read_entries_of(dir, -1);+ EXPECT_EQ(upatch_pins_read("shirudo-unit-pins", &pins), 0);+ EXPECT_UEQ(pins.nr_links, 2);+ EXPECT_UEQ(pins.first_link, 0);+ EXPECT_UEQ(pins.last_link, 2);+ EXPECT_EQ(upatch_each_link("shirudo-unit-pins", count_link, &seen), 0);+ EXPECT_UEQ(seen, 2);+ seen = 0;+ EXPECT_EQ(upatch_each_pinned(count_pinned, &seen), 0);+ EXPECT_UEQ(seen, ARRAY_SIZE(entries));+ read_entries_of_no_dir();++ for (i = 0; i < ARRAY_SIZE(entries); i++)+ drop_entry(dir, entries[i]);+ ASSERT_EQ(rmdir(dir), 0);+}++static void a_failed_readdir_is_an_error(void)+{+ char dir[] = "/tmp/shirudo-unit-pins-XXXXXX";+ struct upatch_pins pins = {};+ int seen = 0;++ ASSERT_TRUE(mkdtemp(dir));+ /* Read the "." and ".." of the empty directory, then fail. */+ read_entries_of(dir, 2);+ EXPECT_EQ(upatch_pins_read("shirudo-unit-pins", &pins), -EIO);+ EXPECT_UEQ(pins.nr_links, 0);+ reads_left = 2;+ EXPECT_EQ(upatch_loaded_pins("shirudo-unit-pins"), -EIO);+ reads_left = 2;+ EXPECT_EQ(upatch_each_link("shirudo-unit-pins", count_link, &seen),+ -EIO);+ EXPECT_UEQ(seen, 0);+ reads_left = 2;+ EXPECT_EQ(upatch_each_pinned(count_pinned, &seen), -EIO);+ EXPECT_UEQ(seen, 0);+ read_entries_of_no_dir();+ ASSERT_EQ(rmdir(dir), 0);+}+static const struct unit_case cases[] = {UNIT_CASE(names_are_parsed_strictly),UNIT_CASE(lowest_index_is_kept_in_any_order),@@ -175,6 +300,8 @@ static const struct unit_case cases[] = {UNIT_CASE(link_pins_read_back_as_links),UNIT_CASE(payload_of_absent_upatch_is_enoent),UNIT_CASE(no_pin_directory_is_no_upatch),+ UNIT_CASE(pin_entries_are_walked),+ UNIT_CASE(a_failed_readdir_is_an_error),{}};diff --git a/util/upatch_pins.c b/util/upatch_pins.cindex 10efcdb0..02a921b5 100644--- a/util/upatch_pins.c+++ b/util/upatch_pins.c@@ -82,6 +82,7 @@ int upatch_pins_read(const char *name, struct upatch_pins *out){char dir[PATH_MAX];struct dirent *e;+ int ret = 0;DIR *d;memset(out, 0, sizeof(*out));@@ -90,17 +91,28 @@ int upatch_pins_read(const char *name, struct upatch_pins *out)d = opendir(dir);if (!d)return -errno;- while ((e = readdir(d)))+ errno = 0;+ while ((e = readdir(d))) {upatch_pins_note(out, e->d_name);+ errno = 0;+ }+ if (errno)+ ret = -errno;closedir(d);- return 0;+ return ret;}-bool upatch_loaded_pins(const char *name)+int upatch_loaded_pins(const char *name){struct upatch_pins pins;+ int ret;- return !upatch_pins_read(name, &pins) && upatch_pins_loaded(&pins);+ ret = upatch_pins_read(name, &pins);+ if (ret == -ENOENT)+ return 0;+ if (ret)+ return ret;+ return upatch_pins_loaded(&pins);}int upatch_each_link(const char *name,@@ -117,6 +129,7 @@ int upatch_each_link(const char *name,d = opendir(dir);if (!d)return -errno;+ errno = 0;while (!ret && (e = readdir(d))) {__u32 index;@@ -126,7 +139,10 @@ int upatch_each_link(const char *name,ret = -ENAMETOOLONG;elseret = cb(index, pin, ctx);+ errno = 0;}+ if (!e && errno && !ret)+ ret = -errno;closedir(d);return ret;}@@ -192,6 +208,7 @@ int upatch_each_pinned(int (*cb)(const char *name, void *ctx), void *ctx)d = opendir(BPFFS_UPATCHES_DIR);if (!d)return errno == ENOENT ? 0 : -errno;+ errno = 0;while (!ret && (e = readdir(d))) {char name[UPATCH_NAME_MAX + 1];@@ -199,7 +216,10 @@ int upatch_each_pinned(int (*cb)(const char *name, void *ctx), void *ctx)upatch_name_from_bpffs(name, sizeof(name), e->d_name))continue;ret = cb(name, ctx);+ errno = 0;}+ if (!e && errno && !ret)+ ret = -errno;closedir(d);return ret;}diff --git a/util/upatch_pins.h b/util/upatch_pins.hindex 80def07b..e97fa37f 100644--- a/util/upatch_pins.h+++ b/util/upatch_pins.h@@ -34,13 +34,15 @@ int upatch_pins_read(const char *name, struct upatch_pins *out);/** By name alone, so a caller without the task mark adds no denied- * BPF_OBJ_GET to the tamper counters.+ * BPF_OBJ_GET to the tamper counters. 1 when loaded, 0 when not or+ * without a pin directory, -errno when the directory cannot be read.*/-bool upatch_loaded_pins(const char *name);+int upatch_loaded_pins(const char *name);/** Calls cb for each link_<n> pin, in directory order, and stops at the- * first nonzero return, which it returns.+ * first nonzero return, which it returns; -errno when a read of the+ * directory fails.*/int upatch_each_link(const char *name,int (*cb)(__u32 index, const char *pin, void *ctx),diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex 42be6c52..fb1990d1 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -347,7 +347,10 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,int link_fd, ret;out[0] = '\0';- if (!upatch_pins_read(name, &pins) && pins.nr_xdp_progs)+ ret = upatch_pins_read(name, &pins);+ if (ret)+ return ret;+ if (pins.nr_xdp_progs)return xdp_location(name, out, cap);link_fd = upatch_open_first_link(name);if (link_fd < 0)@@ -513,7 +516,9 @@ void upatch_loaded_at(const char *name, char *out, size_t cap)if (upatch_pin_path(dir, sizeof(dir), name, NULL))return;/* Later XDP links change the directory's ctime; a program pin stays. */- if (!upatch_pins_read(name, &pins) && pins.nr_xdp_progs &&+ if (upatch_pins_read(name, &pins))+ return;+ if (pins.nr_xdp_progs &&upatch_xdp_prog_pin(dir, sizeof(dir), name, pins.first_xdp_prog))return;if (stat(dir, &st) != 0)diff --git a/util/upatch_resolve.h b/util/upatch_resolve.hindex 618a3691..c21e355c 100644--- a/util/upatch_resolve.h+++ b/util/upatch_resolve.h@@ -87,6 +87,7 @@ int upatch_each_link_misses(struct btf *vbtf, const char *name,*/int upatch_missed_runs(const char *name, __u32 *nr_counted, __u64 *missed);+/* On failure, a pin directory that cannot be read included, out is empty. */void upatch_loaded_at(const char *name, char *out, size_t cap);/* Empty on any failure. */diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex 6ddbedc4..bd7e90fe 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -544,7 +544,12 @@ static int check_one(const char *name, void *ctx)if (!upatch_pin_measured(w->s, name, payload))return 0;- if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs)+ ret = upatch_pins_read(name, &pins);+ if (ret && ret != -ENOENT) {+ note_err(&w->c->check_err, ret);+ note_err(&w->c->coverage_err, ret);+ }+ if (ret || !pins.nr_xdp_progs)return note_links_unverified(w, name);ret = upatch_archive_verify(name, payload, &u);if (ret == -ESTALE) {