eBPF-based security platform
upatch: leave no pins in the result of a failed pin directory read
John Fastabend1da0ef68dc57
diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.cindex b736ecc3..d8312c61 100644--- a/testing/unit/upatch_pins.c+++ b/testing/unit/upatch_pins.c@@ -289,6 +289,30 @@ static void a_failed_readdir_is_an_error(void)ASSERT_EQ(rmdir(dir), 0);}+static void a_failed_read_leaves_no_pins(void)+{+ char dir[] = "/tmp/shirudo-unit-pins-XXXXXX";+ static const char * const entries[] = { "link", "link_0", "link_1" };+ struct upatch_pins pins = {};+ size_t i;++ ASSERT_TRUE(mkdtemp(dir));+ for (i = 0; i < ARRAY_SIZE(entries); i++)+ make_entry(dir, entries[i]);++ /* Four reads succeed, "." and ".." among them: two pins at least. */+ read_entries_of(dir, 4);+ EXPECT_EQ(upatch_pins_read("shirudo-unit-pins", &pins), -EIO);+ EXPECT_FALSE(pins.has_link);+ EXPECT_UEQ(pins.nr_links, 0);+ EXPECT_FALSE(upatch_pins_loaded(&pins));+ read_entries_of_no_dir();++ for (i = 0; i < ARRAY_SIZE(entries); i++)+ drop_entry(dir, entries[i]);+ 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),@@ -302,6 +326,7 @@ static const struct unit_case cases[] = {UNIT_CASE(no_pin_directory_is_no_upatch),UNIT_CASE(pin_entries_are_walked),UNIT_CASE(a_failed_readdir_is_an_error),+ UNIT_CASE(a_failed_read_leaves_no_pins),{}};diff --git a/util/upatch_pins.c b/util/upatch_pins.cindex 02a921b5..fd3f3a24 100644--- a/util/upatch_pins.c+++ b/util/upatch_pins.c@@ -80,6 +80,7 @@ bool upatch_pins_complete(const struct upatch_pins *pins, __u32 nr_attach)int upatch_pins_read(const char *name, struct upatch_pins *out){+ struct upatch_pins pins = {};char dir[PATH_MAX];struct dirent *e;int ret = 0;@@ -93,11 +94,13 @@ int upatch_pins_read(const char *name, struct upatch_pins *out)return -errno;errno = 0;while ((e = readdir(d))) {- upatch_pins_note(out, e->d_name);+ upatch_pins_note(&pins, e->d_name);errno = 0;}if (errno)ret = -errno;+ else+ *out = pins;closedir(d);return ret;}diff --git a/util/upatch_pins.h b/util/upatch_pins.hindex e97fa37f..cd9f5d64 100644--- a/util/upatch_pins.h+++ b/util/upatch_pins.h@@ -29,7 +29,10 @@ bool upatch_pins_loaded(const struct upatch_pins *pins);/* Whether the pins are the links of an upatch of nr_attach non-XDP programs. */bool upatch_pins_complete(const struct upatch_pins *pins, __u32 nr_attach);-/* 0, or a negative errno; -ENOENT when the upatch has no pin directory. */+/*+ * 0, or a negative errno; -ENOENT when the upatch has no pin directory.+ * On an error out holds no pins, whatever was read before it.+ */int upatch_pins_read(const char *name, struct upatch_pins *out);/*