Skip to content

eBPF-based security platform

upatch: share the pin and link helpers the XDP code repeated

John Fastabend098cb5e09ac9
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index e284c985..e7838464 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -549,12 +549,14 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover twenty of the modules in
+The unit suites in :src:`testing/unit/` cover 25 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
-``bpm_tree``, ``bpm_tree_state``, ``chain``, ``eventlog``, ``format``,
-``hex``, ``io``, ``json``, ``kernel_abi``, ``kernel_build_id``,
-``kernel_release``, ``measured``, ``paths``, ``string``, ``upatch`` and
-``upatch_decode``, and ``overflow`` the arithmetic of
+``bpm_tree``, ``bpm_tree_state``, ``chain``, ``conditions``,
+``eventlog``, ``format``, ``hex``, ``io``, ``json``, ``kernel_abi``,
+``kernel_build_id``, ``kernel_release``, ``measured``, ``paths``,
+``string``, ``upatch``, ``upatch_decode``, ``upatch_pins``,
+``upatch_resolve``, ``upatch_verify`` and ``xdp``, and ``overflow`` the
+arithmetic of
:src:`include/shirudo/overflow.h`. The other modules
are reached through the test helpers, the fuzz targets and the binaries
the functional tests run.
@@ -938,7 +940,7 @@ Parsers and units
- The dm-verity superblock check in :src:`util/config_image.c` has no
fuzz target. ``t5001`` covers truncation and unsupported fields.
-- 54 of the 62 modules in :src:`util/` have no unit suite of their own.
+- 42 of the 67 modules in :src:`util/` have no unit suite of their own.
Related
-------
diff --git a/builtin-status.c b/builtin-status.c
index a4ce16e0..31031764 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -91,7 +91,7 @@ static int note_uncovered(const char *name, const char *dev, const char *why,
if (strsprintf(entry, sizeof(entry), "%s on %s (%s)", name, dev,
why) < 0)
- strscpy(entry, "?", sizeof(entry));
+ return note_unmeasured(name, ctx);
return note_unmeasured(entry, ctx);
}
diff --git a/builtin-upatch.c b/builtin-upatch.c
index eb35026f..34dfc58d 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1331,14 +1331,13 @@ static int prog_pin_path(char *pin, size_t sz, const char *dir,
* Links exist only per device, so with none attached the pin is what keeps
* the program loaded, and its index is what ties it to its signed classes.
*/
-static int pin_xdp_prog(int prog_fd, const char *dir, __u32 index)
+static int pin_xdp_prog(int prog_fd, const char *name, __u32 index)
{
- char pin[PATH_MAX + 32];
+ char pin[PATH_MAX];
struct stat st;
int ret;
- if (strsprintf(pin, sizeof(pin), "%s/" XDP_PROG_PIN_PREFIX "%u", dir,
- index) < 0)
+ if (upatch_xdp_prog_pin(pin, sizeof(pin), name, index))
return -ENAMETOOLONG;
if (stat(pin, &st) == 0 && unlink(pin))
return -errno;
@@ -1562,7 +1561,7 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,
}
attach_seen++;
if (spec->kind == UPATCH_ATTACH_XDP) {
- ret = pin_xdp_prog(inner_fd, dir, i);
+ ret = pin_xdp_prog(inner_fd, name, i);
if (ret)
goto out;
made[i] = true;
@@ -1650,8 +1649,7 @@ out:
attach_seen = 0;
for (i = 0; i < nr_progs; i++) {
if (made[i] && specs[i].kind == UPATCH_ATTACH_XDP &&
- strsprintf(pin, sizeof(pin),
- "%s/" XDP_PROG_PIN_PREFIX "%u", dir, i) >= 0)
+ !upatch_xdp_prog_pin(pin, sizeof(pin), name, i))
unlink(pin);
if (made[i] &&
!(has_xdp && specs[i].role == UPATCH_PROG_ROLE_ATTACH) &&
@@ -4227,7 +4225,6 @@ static void xdp_work_free(struct xdp_work *work, size_t count)
/* Whether this boot's records hold the upatch with the payload pinned now. */
static int pin_is_measured(const char *name, __u8 *payload, bool *held)
{
- char hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];
struct measured_set *set;
int ret;
@@ -4235,10 +4232,7 @@ static int pin_is_measured(const char *name, __u8 *payload, bool *held)
ret = measured_set_boot(&set);
if (ret)
return ret;
- if (!get_pin_payload(name, payload)) {
- hex_encode(payload, UPATCH_PAYLOAD_HASH_LEN, hex);
- *held = measured_holds(set, name, hex);
- }
+ *held = upatch_pin_measured(set, name, payload);
measured_set_free(set);
return 0;
}
@@ -4299,56 +4293,61 @@ static int reconcile_open(struct xdp_work *w, bool *skip,
return ret ? reconcile_fail_verify(w->name, ret, rep) : 0;
}
+struct xdp_collect {
+ struct xdp_work *work;
+ size_t nr;
+ struct op_report *rep;
+};
+
+static int collect_one(const char *name, void *ctx)
+{
+ struct xdp_collect *c = ctx;
+ struct upatch_pins pins;
+ struct xdp_work *grown, *w;
+ bool skip;
+ int ret;
+
+ if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs)
+ return 0;
+ grown = xreallocarray(c->work, c->nr, c->nr + 1, sizeof(*c->work));
+ if (!grown) {
+ rep_fail(c->rep, "pre_flight", "unknown", ENOMEM, "out of memory");
+ return 1;
+ }
+ c->work = grown;
+ w = &c->work[c->nr];
+ memset(w, 0, sizeof(*w));
+ if (strscpy(w->name, name, sizeof(w->name)) < 0) {
+ rep_fail(c->rep, "pre_flight", "unknown", ENAMETOOLONG,
+ "upatch name too long");
+ return 1;
+ }
+ ret = reconcile_open(w, &skip, c->rep);
+ if (!ret && !skip)
+ c->nr++;
+ return ret;
+}
+
static int reconcile_collect(struct xdp_work **out, size_t *count,
struct op_report *rep)
{
- struct xdp_work *work = NULL;
- size_t nr = 0;
- struct dirent *e;
- int ret = 0;
- DIR *d;
+ struct xdp_collect c = { .rep = rep };
+ int ret;
*out = NULL;
*count = 0;
- d = opendir(BPFFS_UPATCHES_DIR);
- if (!d) {
- if (errno == ENOENT)
- return 0;
- rep_fail(rep, "pre_flight", "unknown", errno,
+ ret = upatch_each_pinned(collect_one, &c);
+ if (ret < 0) {
+ rep_fail(rep, "pre_flight", "unknown", -ret,
"cannot read the pin directory");
- return 1;
- }
- while (!ret && (e = readdir(d))) {
- char name[UPATCH_NAME_MAX + 1];
- struct upatch_pins pins;
- struct xdp_work *grown;
- bool skip;
-
- if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") ||
- upatch_name_from_bpffs(name, sizeof(name), e->d_name) ||
- upatch_pins_read(name, &pins) || !pins.nr_xdp_progs)
- continue;
- grown = xreallocarray(work, nr, nr + 1, sizeof(*work));
- if (!grown) {
- rep_fail(rep, "pre_flight", "unknown", ENOMEM,
- "out of memory");
- ret = 1;
- break;
- }
- work = grown;
- memset(&work[nr], 0, sizeof(work[nr]));
- strscpy(work[nr].name, name, sizeof(work[nr].name));
- ret = reconcile_open(&work[nr], &skip, rep);
- if (!ret && !skip)
- nr++;
+ ret = 1;
}
- closedir(d);
if (ret) {
- xdp_work_free(work, nr);
+ xdp_work_free(c.work, c.nr);
return ret;
}
- *out = work;
- *count = nr;
+ *out = c.work;
+ *count = c.nr;
return 0;
}
@@ -4359,21 +4358,12 @@ struct link_scan {
};
/* The kernel clears a link's ifindex for good when its device goes away. */
-static int scan_link(__u32 index, const char *pin, void *ctx)
+static int scan_link(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx)
{
- struct bpf_link_info info = {};
struct link_scan *scan = ctx;
- __u32 len = sizeof(info);
- int fd, ret;
- fd = bpf_obj_get(pin);
- if (fd < 0)
- return -errno;
- ret = bpf_link_get_info_by_fd(fd, &info, &len);
- close(fd);
- if (ret)
- return ret;
- if (info.type == BPF_LINK_TYPE_XDP && !info.xdp.ifindex) {
+ if (info->type == BPF_LINK_TYPE_XDP && !info->xdp.ifindex) {
scan->dead = true;
if (scan->collect) {
if (unlink(pin))
@@ -4512,7 +4502,7 @@ static int reconcile_apply(const struct xdp_work *w,
goto out;
}
upatch_close(&again);
- ret = upatch_each_link(w->name, scan_link, &scan);
+ ret = upatch_each_link_info(w->name, scan_link, &scan);
if (ret) {
rep_fail(rep, "link_create", ret == -EACCES || ret == -EPERM ?
"lsm_denied" : "unknown", ret, "cannot read the links");
@@ -4554,7 +4544,7 @@ static int reconcile_upatch(const struct xdp_work *w,
* upgrade, so a failure here only sends the upatch on to
* reconcile_apply(), which can tell.
*/
- if (upatch_each_link(w->name, scan_link, &scan))
+ if (upatch_each_link_info(w->name, scan_link, &scan))
needed = true;
else if (reconcile_walk(w, devices, count, NULL, &needed, rep))
return 1;
diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.c
index 23ffdef0..2b2bde35 100644
--- a/testing/unit/upatch_pins.c
+++ b/testing/unit/upatch_pins.c
@@ -2,7 +2,11 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include <unistd.h>
+#include "paths.h"
#include "unit.h"
#include "upatch_pins.h"
@@ -96,12 +100,43 @@ static void absent_upatch_has_no_pins(void)
EXPECT_FALSE(upatch_loaded_pins("shirudo-unit-no-such-upatch"));
}
+static void xdp_program_pins_have_one_name(void)
+{
+ char pin[PATH_MAX], small[8];
+ size_t len;
+
+ ASSERT_EQ(upatch_xdp_prog_pin(pin, sizeof(pin), "fw", 3), 0);
+ len = strlen(pin);
+ ASSERT_TRUE(len > strlen("/xdp_prog_3"));
+ EXPECT_STREQ(pin + len - strlen("/xdp_prog_3"), "/xdp_prog_3");
+ EXPECT_EQ(upatch_xdp_prog_pin(small, sizeof(small), "fw", 3),
+ -ENAMETOOLONG);
+}
+
+static int count_pinned(const char *name, void *ctx)
+{
+ (*(int *)ctx)++;
+ return 0;
+}
+
+static void no_pin_directory_is_no_upatch(void)
+{
+ int seen = 0;
+
+ if (access(BPFFS_UPATCHES_DIR, F_OK) == 0)
+ unit_skip("this host has a shirudo pin directory");
+ EXPECT_EQ(upatch_each_pinned(count_pinned, &seen), 0);
+ EXPECT_EQ(seen, 0);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(names_are_parsed_strictly),
UNIT_CASE(lowest_index_is_kept_in_any_order),
UNIT_CASE(loaded_needs_the_first_link_or_a_program),
UNIT_CASE(complete_means_every_link),
UNIT_CASE(absent_upatch_has_no_pins),
+ UNIT_CASE(xdp_program_pins_have_one_name),
+ UNIT_CASE(no_pin_directory_is_no_upatch),
{}
};
diff --git a/testing/unit/upatch_resolve.c b/testing/unit/upatch_resolve.c
new file mode 100644
index 00000000..6b3fcc2f
--- /dev/null
+++ b/testing/unit/upatch_resolve.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+
+#include "unit.h"
+#include "upatch_resolve.h"
+
+static int never_called(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx)
+{
+ (*(int *)ctx)++;
+ return 0;
+}
+
+static void links_of_an_absent_upatch_are_not_read(void)
+{
+ int calls = 0;
+
+ EXPECT_EQ(upatch_each_link_info("shirudo-unit-no-such-upatch",
+ never_called, &calls), -ENOENT);
+ EXPECT_EQ(calls, 0);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(links_of_an_absent_upatch_are_not_read),
+ {}
+};
+
+UNIT_SUITE(upatch_resolve, cases);
diff --git a/util/upatch_pins.c b/util/upatch_pins.c
index 47ee4422..59e86785 100644
--- a/util/upatch_pins.c
+++ b/util/upatch_pins.c
@@ -12,6 +12,7 @@
#include <string.h>
#include <shirudo/string.h>
+#include <shirudo/upatch.h>
#include "paths.h"
#include "upatch_pins.h"
@@ -125,3 +126,34 @@ int upatch_each_link(const char *name,
closedir(d);
return ret;
}
+
+int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index)
+{
+ char entry[32];
+
+ if (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u", index) < 0 ||
+ upatch_pin_path(out, cap, name, entry, NULL))
+ return -ENAMETOOLONG;
+ return 0;
+}
+
+int upatch_each_pinned(int (*cb)(const char *name, void *ctx), void *ctx)
+{
+ struct dirent *e;
+ int ret = 0;
+ DIR *d;
+
+ d = opendir(BPFFS_UPATCHES_DIR);
+ if (!d)
+ return errno == ENOENT ? 0 : -errno;
+ while (!ret && (e = readdir(d))) {
+ char name[UPATCH_NAME_MAX + 1];
+
+ if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") ||
+ upatch_name_from_bpffs(name, sizeof(name), e->d_name))
+ continue;
+ ret = cb(name, ctx);
+ }
+ closedir(d);
+ return ret;
+}
diff --git a/util/upatch_pins.h b/util/upatch_pins.h
index e9fd34c5..b5d2a6cb 100644
--- a/util/upatch_pins.h
+++ b/util/upatch_pins.h
@@ -9,6 +9,7 @@
#define SHIRUDO_UTIL_UPATCH_PINS_H
#include <stdbool.h>
+#include <stddef.h>
#include <shirudo/types.h>
@@ -44,4 +45,14 @@ int upatch_each_link(const char *name,
int (*cb)(__u32 index, const char *pin, void *ctx),
void *ctx);
+/* The pin that keeps XDP program index of name loaded: 0 or -ENAMETOOLONG. */
+int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index);
+
+/*
+ * Calls cb with the name of each upatch that has a pin directory and stops
+ * at the first nonzero return, which it returns. No directory at all is 0,
+ * any other failure to read it -errno.
+ */
+int upatch_each_pinned(int (*cb)(const char *name, void *ctx), void *ctx);
+
#endif /* SHIRUDO_UTIL_UPATCH_PINS_H */
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index f7d7044f..3d691ee2 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -90,9 +90,9 @@ int upatch_first_prog_id(const char *name, __u32 *out)
{
struct bpf_link_info linfo = {};
struct bpf_prog_info info = {};
- char entry[32], pin[PATH_MAX];
__u32 len = sizeof(linfo);
struct upatch_pins pins;
+ char pin[PATH_MAX];
int fd, ret;
*out = 0;
@@ -112,9 +112,7 @@ int upatch_first_prog_id(const char *name, __u32 *out)
return ret;
if (!pins.nr_xdp_progs)
return -ENOENT;
- if (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u",
- pins.first_xdp_prog) < 0 ||
- upatch_pin_path(pin, sizeof(pin), name, entry, NULL))
+ if (upatch_xdp_prog_pin(pin, sizeof(pin), name, pins.first_xdp_prog))
return -ENAMETOOLONG;
fd = bpf_obj_get(pin);
if (fd < 0)
@@ -134,12 +132,17 @@ struct xdp_links {
__u32 live;
};
-static int note_xdp_link(__u32 index, const char *pin, void *ctx)
+struct link_info_walk {
+ int (*cb)(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx);
+ void *ctx;
+};
+
+static int read_link_info(__u32 index, const char *pin, void *ctx)
{
+ struct link_info_walk *w = ctx;
struct bpf_link_info info = {};
__u32 len = sizeof(info);
- struct xdp_links *l = ctx;
- char name[IF_NAMESIZE];
int fd, ret;
fd = bpf_obj_get(pin);
@@ -147,18 +150,37 @@ static int note_xdp_link(__u32 index, const char *pin, void *ctx)
return -errno;
ret = bpf_link_get_info_by_fd(fd, &info, &len);
close(fd);
- if (ret)
- return ret;
+ return ret ? ret : w->cb(index, pin, &info, w->ctx);
+}
+
+int upatch_each_link_info(const char *name,
+ int (*cb)(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx),
+ void *ctx)
+{
+ struct link_info_walk w = { .cb = cb, .ctx = ctx };
+
+ return upatch_each_link(name, read_link_info, &w);
+}
+
+static int note_xdp_link(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx)
+{
+ struct xdp_links *l = ctx;
+ char name[IF_NAMESIZE];
+
/* The kernel clears the ifindex when the device goes away. */
- if (info.type != BPF_LINK_TYPE_XDP || !info.xdp.ifindex)
+ if (info->type != BPF_LINK_TYPE_XDP || !info->xdp.ifindex)
return 0;
if (!l->live || index < l->lowest) {
l->lowest = index;
- if (if_indextoname(info.xdp.ifindex, name))
- strscpy(l->device, name, sizeof(l->device));
- else if (strsprintf(l->device, sizeof(l->device), "ifindex:%u",
- info.xdp.ifindex) < 0)
+ if (if_indextoname(info->xdp.ifindex, name)) {
+ if (strscpy(l->device, name, sizeof(l->device)) < 0)
+ return -E2BIG;
+ } else if (strsprintf(l->device, sizeof(l->device), "ifindex:%u",
+ info->xdp.ifindex) < 0) {
return -E2BIG;
+ }
}
l->live++;
return 0;
@@ -169,7 +191,7 @@ static int xdp_location(const char *name, char *out, size_t cap)
struct xdp_links links = {};
int ret, n;
- ret = upatch_each_link(name, note_xdp_link, &links);
+ ret = upatch_each_link_info(name, note_xdp_link, &links);
if (ret)
return ret;
if (!links.live)
@@ -290,7 +312,6 @@ void upatch_loaded_at(const char *name, char *out, size_t cap)
{
struct upatch_pins pins;
char dir[PATH_MAX];
- char entry[32];
struct stat st;
out[0] = '\0';
@@ -298,9 +319,7 @@ void upatch_loaded_at(const char *name, char *out, size_t cap)
return;
/* Later XDP links change the directory's ctime; a program pin stays. */
if (!upatch_pins_read(name, &pins) && pins.nr_xdp_progs &&
- (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u",
- pins.first_xdp_prog) < 0 ||
- upatch_pin_path(dir, sizeof(dir), name, entry, NULL)))
+ upatch_xdp_prog_pin(dir, sizeof(dir), name, pins.first_xdp_prog))
return;
if (stat(dir, &st) != 0)
return;
diff --git a/util/upatch_resolve.h b/util/upatch_resolve.h
index 98fc56c1..e346360f 100644
--- a/util/upatch_resolve.h
+++ b/util/upatch_resolve.h
@@ -12,6 +12,7 @@
#include <shirudo/types.h>
+struct bpf_link_info;
struct btf;
#define UPATCH_LOCATION_MAX 160
@@ -23,6 +24,15 @@ struct btf;
*/
int upatch_open_first_link(const char *name);
+/*
+ * As upatch_each_link(), with what the kernel says of each link; a link
+ * whose pin cannot be opened or read ends the walk with its -errno.
+ */
+int upatch_each_link_info(const char *name,
+ int (*cb)(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx),
+ void *ctx);
+
/* From the first link, or from an XDP program pin when it has none. */
int upatch_first_prog_id(const char *name, __u32 *out);
diff --git a/util/upatch_verify.c b/util/upatch_verify.c
index 53f07258..631c67a7 100644
--- a/util/upatch_verify.c
+++ b/util/upatch_verify.c
@@ -5,7 +5,6 @@
* Whether the archive of a loaded upatch is still what was loaded.
*/
-#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
@@ -31,6 +30,7 @@
#include "seal.h"
#include "upatch.h"
#include "upatch_pins.h"
+#include "upatch_resolve.h"
#include "upatch_verify.h"
#include "xdp.h"
@@ -85,21 +85,12 @@ int upatch_archive_verify(const char *name,
return 0;
}
-static int count_non_xdp_link(__u32 index, const char *pin, void *ctx)
+static int count_non_xdp_link(__u32 index, const char *pin,
+ const struct bpf_link_info *info, void *ctx)
{
- struct bpf_link_info info = {};
- __u32 len = sizeof(info);
__u32 *found = ctx;
- int fd, ret;
- fd = bpf_obj_get(pin);
- if (fd < 0)
- return -errno;
- ret = bpf_link_get_info_by_fd(fd, &info, &len);
- close(fd);
- if (ret)
- return ret;
- if (info.type != BPF_LINK_TYPE_XDP)
+ if (info->type != BPF_LINK_TYPE_XDP)
(*found)++;
return 0;
}
@@ -115,7 +106,6 @@ static int xdp_upatch_complete(const char *name, const struct prog_spec *specs,
{
__u32 i, nr_xdp = 0, found = 0;
char pin[PATH_MAX];
- char entry[32];
struct stat st;
int ret;
@@ -123,15 +113,14 @@ static int xdp_upatch_complete(const char *name, const struct prog_spec *specs,
if (specs[i].kind != UPATCH_ATTACH_XDP)
continue;
nr_xdp++;
- if (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u", i) < 0 ||
- upatch_pin_path(pin, sizeof(pin), name, entry, NULL))
+ if (upatch_xdp_prog_pin(pin, sizeof(pin), name, i))
return -ENAMETOOLONG;
if (stat(pin, &st))
return errno == ENOENT ? 0 : -errno;
}
if (nr_attach == nr_xdp)
return 1;
- ret = upatch_each_link(name, count_non_xdp_link, &found);
+ ret = upatch_each_link_info(name, count_non_xdp_link, &found);
if (ret)
return ret;
return found == nr_attach - nr_xdp;
@@ -202,11 +191,9 @@ int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id)
struct bpf_prog_info info = {};
__u32 len = sizeof(info);
char pin[PATH_MAX];
- char entry[32];
int fd, ret;
- if (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u", index) < 0 ||
- upatch_pin_path(pin, sizeof(pin), name, entry, NULL))
+ if (upatch_xdp_prog_pin(pin, sizeof(pin), name, index))
return -ENAMETOOLONG;
fd = bpf_obj_get(pin);
if (fd < 0)
@@ -265,8 +252,7 @@ int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,
return 0;
}
-/* Whether the set holds name with the payload its pin directory records. */
-static bool pin_measured(const struct measured_set *s, const char *name,
+bool upatch_pin_measured(const struct measured_set *s, const char *name,
__u8 payload[UPATCH_PAYLOAD_HASH_LEN])
{
char hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];
@@ -290,7 +276,7 @@ static int xdp_archive_changed(const struct measured_set *s, const char *name,
int ret;
if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs ||
- !pin_measured(s, name, payload))
+ !upatch_pin_measured(s, name, payload))
return 0;
ret = upatch_archive_verify(name, payload, &u);
if (!ret)
@@ -305,7 +291,7 @@ static int links_lost(const struct measured_set *s, const char *name,
char path[PATH_MAX];
int ret;
- if (!pin_measured(s, name, payload))
+ if (!upatch_pin_measured(s, name, payload))
return 0;
if (strsprintf(path, sizeof(path), "%s/%s.upatch", UPATCHES_DIR,
name) < 0)
@@ -316,6 +302,33 @@ static int links_lost(const struct measured_set *s, const char *name,
return !ret || ret == -ESTALE ? 1 : ret;
}
+struct pinned_walk {
+ const struct measured_set *s;
+ int (*check)(const struct measured_set *s,
+ const char *name, void *ctx);
+ int (*cb)(const char *name, void *ctx);
+ void *ctx;
+ int *check_err;
+ int found;
+ int stopped;
+};
+
+static int walk_one(const char *name, void *ctx)
+{
+ struct pinned_walk *w = ctx;
+ int result;
+
+ result = w->check(w->s, name, w->ctx);
+ if (result < 0 && !*w->check_err)
+ *w->check_err = result;
+ if (result <= 0)
+ return 0;
+ w->found++;
+ if (w->cb)
+ w->stopped = w->cb(name, w->ctx);
+ return w->stopped;
+}
+
/* check gets ctx as well, and cb may be NULL. */
static int walk_pinned(const struct measured_set *s,
int (*check)(const struct measured_set *s, const char *name,
@@ -323,35 +336,18 @@ static int walk_pinned(const struct measured_set *s,
int (*cb)(const char *name, void *ctx), void *ctx,
int *check_err)
{
- struct dirent *e;
- int found = 0;
- int ret = 0;
- DIR *d;
-
- d = opendir(BPFFS_UPATCHES_DIR);
- if (!d) {
- if (errno != ENOENT && !*check_err)
- *check_err = -errno;
- return 0;
- }
- while (!ret && (e = readdir(d))) {
- char name[UPATCH_NAME_MAX + 1];
- int result;
+ struct pinned_walk w = {
+ .s = s, .check = check, .cb = cb, .ctx = ctx,
+ .check_err = check_err,
+ };
+ int ret;
- if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") ||
- upatch_name_from_bpffs(name, sizeof(name), e->d_name))
- continue;
- result = check(s, name, ctx);
- if (result < 0 && !*check_err)
- *check_err = result;
- if (result <= 0)
- continue;
- found++;
- if (cb)
- ret = cb(name, ctx);
- }
- closedir(d);
- return ret ? ret : found;
+ ret = upatch_each_pinned(walk_one, &w);
+ if (w.stopped)
+ return w.stopped;
+ if (ret && !*check_err)
+ *check_err = ret;
+ return w.found;
}
int upatch_xdp_archives_changed(const struct measured_set *s,
@@ -406,7 +402,7 @@ static int xdp_uncovered(const struct measured_set *s, const char *name,
int ret;
if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs ||
- !pin_measured(s, name, payload))
+ !upatch_pin_measured(s, name, payload))
return 0;
ret = upatch_archive_specs(name, payload, &u, &specs);
if (ret)
diff --git a/util/upatch_verify.h b/util/upatch_verify.h
index ba1e5218..8a250944 100644
--- a/util/upatch_verify.h
+++ b/util/upatch_verify.h
@@ -8,6 +8,7 @@
#ifndef SHIRUDO_UTIL_UPATCH_VERIFY_H
#define SHIRUDO_UTIL_UPATCH_VERIFY_H
+#include <stdbool.h>
#include <stddef.h>
#include <shirudo/types.h>
@@ -62,6 +63,13 @@ int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,
struct measured_set;
+/*
+ * Whether the set holds name with the payload its pin directory records,
+ * which is left in payload when the directory records one.
+ */
+bool upatch_pin_measured(const struct measured_set *s, const char *name,
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN]);
+
/*
* The XDP upatches the set holds whose archive is not what was loaded. cb
* runs for each and a non-zero return ends the walk with that value. The