eBPF-based security platform
upatch: resolve an upatch from any of its pins
John Fastabend4b92f0857e04
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex 90cda4ad..a1ab1b3d 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -309,7 +309,9 @@ checked before pinning.Discovery runs once per load or restore. New devices require reload;there is no background reconciliation. Removing a device can detach itsXDP link while its pin remains. The signed artifact and TPM measurement-cover the class policy, not a fixed device inventory.+cover the class policy, not a fixed device inventory. The location+``status`` and ``upatch get`` report names the first device still+attached and how many more there are, as in ``xdp/eth0 (+1)``.Boot restore requires a matching native-XDP interface to be present when``init`` restores the archive; otherwise initialization fails.diff --git a/Makefile.shirudo b/Makefile.shirudoindex fd8f3f43..f859769b 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -279,6 +279,7 @@ UTIL_SRCS := \util/upatch_decode.c \util/upatch_maps_otel.c \util/elf_target.c \+ util/upatch_pins.c \util/upatch_resolve.c \util/upatch_table.c@@ -825,7 +826,8 @@ testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.outil/upatch_maps_otel.o util/curl.o util/crypto_openssl.o \util/tls_openssl.o util/serializer.o util/seal.o \util/config.o util/image_config.o \- util/upatch_decode.o util/upatch_resolve.o \+ util/upatch_decode.o util/upatch_pins.o \+ util/upatch_resolve.o \util/upatch.o util/json.o util/string.o util/log.o \util/paths.o \util/io.o util/malloc.o $(LIBBPF_A) util/hex.o util/timefmt.o@@ -843,7 +845,7 @@ testing/helper/test-upatch-prog-metadata: testing/helper/test-upatch-prog-metada$(Q)$(CC) $(CFLAGS) -o $@ $^testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializer-pages.c \- util/upatch_maps.o util/upatch_resolve.o \+ util/upatch_maps.o util/upatch_pins.o util/upatch_resolve.o \util/serializer.o util/seal.o \util/json.o util/string.o \util/config.o util/image_config.o \@@ -1064,8 +1066,8 @@ testing/fuzz/fuzz-elf: testing/fuzz/fuzz-elf.c $(FUZZ_BASE) \testing/fuzz/fuzz-elf: FUZZ_CFLAGS += $(LIBELF_CFLAGS)testing/fuzz/fuzz-elf: FUZZ_LIBS += $(LIBBPF_A) $(LIBELF_LIBS)testing/fuzz/fuzz-serializer: testing/fuzz/fuzz-serializer.c $(FUZZ_BASE) \- util/upatch_maps.c util/upatch_resolve.c \- util/serializer.c $(FUZZ_SEAL) util/io.c util/paths.c \+ util/upatch_maps.c util/upatch_pins.c \+ util/upatch_resolve.c util/serializer.c $(FUZZ_SEAL) util/io.c util/paths.c \util/timefmt.c $(LIBBPF_A)testing/fuzz/fuzz-serializer: FUZZ_CFLAGS += $(LIBELF_CFLAGS)testing/fuzz/fuzz-serializer: FUZZ_LIBS += $(LIBBPF_A) $(LIBELF_LIBS)diff --git a/builtin-init.c b/builtin-init.cindex 9250071a..2f2dafd2 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -57,6 +57,7 @@#include "util/seal.h"#include "util/tpm.h"#include "util/upatch.h"+#include "util/upatch_pins.h"#include "util/upatch_resolve.h"static void print_preflight(const struct preflight_result *r)@@ -975,7 +976,6 @@ static int boot_restore_locked(bool recorded,char pinned_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];char path[PATH_MAX];struct upatch u;- int fd;if (ret)goto next;@@ -1006,9 +1006,7 @@ static int boot_restore_locked(bool recorded,"entry); ignored", names[i]->d_name);goto next;}- fd = upatch_open_first_link(set[cnt].name);- if (fd >= 0) {- close(fd);+ if (upatch_loaded_pins(set[cnt].name)) {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 2019ffbd..19d8b6cc 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -62,6 +62,7 @@#include "util/upatch_decode.h"#include "util/upatch_maps.h"#include "util/upatch_maps_otel.h"+#include "util/upatch_pins.h"#include "util/upatch_resolve.h"#include "util/upatch_table.h"#include "util/version.h"@@ -493,15 +494,9 @@ static bool upatch_is_loaded(const char *name){char pin[PATH_MAX];struct stat st;- int fd;- fd = upatch_open_first_link(name);- if (fd < 0)- goto test_pin;- close(fd);- return true;--test_pin:+ if (upatch_loaded_pins(name))+ return true;if (upatch_pin_path(pin, sizeof(pin), name, "test", NULL))return false;return stat(pin, &st) == 0;@@ -3897,13 +3892,12 @@ static void upatch_get_ctx_free(struct upatch_get_ctx *c)/* Missing line_info is not a failure: the rest of the get still renders. */static int upatch_get_collect(const char *want, struct upatch_get_ctx *c){- struct bpf_link_info linfo = {};struct bpf_prog_info info = {};struct btf *vbtf = NULL;void *recs = NULL;size_t recs_sz = 0;__u32 len, nr, rec_size, btf_id;- int link_fd = -1, prog_fd = -1, ret = 0;+ int prog_fd = -1, ret = 0;c->upatch = want;c->ents = NULL;@@ -3915,19 +3909,9 @@ static int upatch_get_collect(const char *want, struct upatch_get_ctx *c)c->payload[0] = '\0';c->prog_id = 0;- link_fd = upatch_open_first_link(want);- if (link_fd < 0)- return link_fd;- len = sizeof(linfo);- if (bpf_link_get_info_by_fd(link_fd, &linfo, &len) != 0) {- ret = -errno;- close(link_fd);+ ret = upatch_first_prog_id(want, &c->prog_id);+ if (ret)return ret;- }- close(link_fd);- if (linfo.prog_id == 0)- return -EINVAL;- c->prog_id = linfo.prog_id;upatch_loaded_at(want, c->loaded_at, sizeof(c->loaded_at));upatch_payload_sha256(want, c->payload, sizeof(c->payload));@@ -3936,7 +3920,7 @@ static int upatch_get_collect(const char *want, struct upatch_get_ctx *c)sizeof(c->location));btf__free(vbtf);- prog_fd = bpf_prog_get_fd_by_id(linfo.prog_id);+ prog_fd = bpf_prog_get_fd_by_id(c->prog_id);if (prog_fd < 0)return -errno;len = sizeof(info);diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.cnew file mode 100644index 00000000..623356d6--- /dev/null+++ b/testing/unit/upatch_pins.c@@ -0,0 +1,79 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>++#include "unit.h"+#include "upatch_pins.h"++static void names_are_parsed_strictly(void)+{+ static const char * const ignored[] = {+ "link_", "link_07", "link_1x", "link_-1", "link_+1",+ "link_4294967296", "link_ 1", "links", "xdp_prog_",+ "xdp_prog_01", "xdp_prog_a", "test", "maps", "serializers",+ ".", "..",+ };+ struct upatch_pins pins = {};+ size_t i;++ for (i = 0; i < ARRAY_SIZE(ignored); i++)+ upatch_pins_note(&pins, ignored[i]);+ EXPECT_FALSE(pins.has_link);+ EXPECT_UEQ(pins.nr_links, 0);+ EXPECT_UEQ(pins.nr_xdp_progs, 0);+ EXPECT_FALSE(upatch_pins_loaded(&pins));+}++static void lowest_index_is_kept_in_any_order(void)+{+ struct upatch_pins pins = {};++ upatch_pins_note(&pins, "link_7");+ upatch_pins_note(&pins, "link_4294967295");+ upatch_pins_note(&pins, "link_2");+ upatch_pins_note(&pins, "xdp_prog_3");+ upatch_pins_note(&pins, "xdp_prog_1");+ EXPECT_UEQ(pins.nr_links, 3);+ EXPECT_UEQ(pins.first_link, 2);+ EXPECT_UEQ(pins.nr_xdp_progs, 2);+ EXPECT_UEQ(pins.first_xdp_prog, 1);+}++static void loaded_needs_the_first_link_or_a_program(void)+{+ struct upatch_pins pins = {};++ upatch_pins_note(&pins, "link_1");+ EXPECT_FALSE(upatch_pins_loaded(&pins));+ upatch_pins_note(&pins, "link_0");+ EXPECT_TRUE(upatch_pins_loaded(&pins));++ memset(&pins, 0, sizeof(pins));+ upatch_pins_note(&pins, "link");+ EXPECT_TRUE(upatch_pins_loaded(&pins));++ memset(&pins, 0, sizeof(pins));+ upatch_pins_note(&pins, "xdp_prog_4");+ EXPECT_TRUE(upatch_pins_loaded(&pins));+}++static void absent_upatch_has_no_pins(void)+{+ struct upatch_pins pins = { .has_link = true };++ 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"));+}++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(absent_upatch_has_no_pins),+ {}+};++UNIT_SUITE(upatch_pins, cases);diff --git a/util/upatch_pins.c b/util/upatch_pins.cnew file mode 100644index 00000000..4bb476b9--- /dev/null+++ b/util/upatch_pins.c@@ -0,0 +1,115 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What the pin directory of a loaded upatch holds.+ */++#include <dirent.h>+#include <errno.h>+#include <limits.h>+#include <stdint.h>+#include <string.h>++#include <shirudo/string.h>++#include "paths.h"+#include "upatch_pins.h"++static bool pin_index(const char *entry, const char *prefix, __u32 *out)+{+ size_t n = strlen(prefix);+ __u64 value;++ if (strncmp(entry, prefix, n))+ return false;+ entry += n;+ if (entry[0] == '0' && entry[1])+ return false;+ if (parse_u64(entry, strlen(entry), UINT32_MAX, &value))+ return false;+ *out = (__u32)value;+ return true;+}++void upatch_pins_note(struct upatch_pins *pins, const char *entry)+{+ __u32 index;++ if (!strcmp(entry, "link")) {+ pins->has_link = true;+ } else if (pin_index(entry, "link_", &index)) {+ if (!pins->nr_links || index < pins->first_link)+ pins->first_link = index;+ pins->nr_links++;+ } else if (pin_index(entry, XDP_PROG_PIN_PREFIX, &index)) {+ if (!pins->nr_xdp_progs || index < pins->first_xdp_prog)+ pins->first_xdp_prog = index;+ pins->nr_xdp_progs++;+ }+}++/*+ * Only link_0 stands for the numbered links: a non-XDP upatch that has+ * lost it must still read as not loaded, where restore calls that+ * tampering. XDP link pins come and go with devices, so its program pin+ * says it is loaded.+ */+bool upatch_pins_loaded(const struct upatch_pins *pins)+{+ return pins->has_link || (pins->nr_links && !pins->first_link) ||+ pins->nr_xdp_progs;+}++int upatch_pins_read(const char *name, struct upatch_pins *out)+{+ char dir[PATH_MAX];+ struct dirent *e;+ DIR *d;++ memset(out, 0, sizeof(*out));+ if (upatch_pin_path(dir, sizeof(dir), name, NULL))+ return -ENAMETOOLONG;+ d = opendir(dir);+ if (!d)+ return -errno;+ while ((e = readdir(d)))+ upatch_pins_note(out, e->d_name);+ closedir(d);+ return 0;+}++bool upatch_loaded_pins(const char *name)+{+ struct upatch_pins pins;++ return !upatch_pins_read(name, &pins) && upatch_pins_loaded(&pins);+}++int upatch_each_link(const char *name,+ int (*cb)(__u32 index, const char *pin, void *ctx),+ void *ctx)+{+ char dir[PATH_MAX], pin[PATH_MAX + NAME_MAX + 2];+ struct dirent *e;+ int ret = 0;+ DIR *d;++ if (upatch_pin_path(dir, sizeof(dir), name, NULL))+ return -ENAMETOOLONG;+ d = opendir(dir);+ if (!d)+ return -errno;+ while (!ret && (e = readdir(d))) {+ __u32 index;++ if (!pin_index(e->d_name, "link_", &index))+ continue;+ if (strsprintf(pin, sizeof(pin), "%s/%s", dir, e->d_name) < 0)+ ret = -ENAMETOOLONG;+ else+ ret = cb(index, pin, ctx);+ }+ closedir(d);+ return ret;+}diff --git a/util/upatch_pins.h b/util/upatch_pins.hnew file mode 100644index 00000000..a1aa705c--- /dev/null+++ b/util/upatch_pins.h@@ -0,0 +1,43 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What the pin directory of a loaded upatch holds.+ */++#ifndef SHIRUDO_UTIL_UPATCH_PINS_H+#define SHIRUDO_UTIL_UPATCH_PINS_H++#include <stdbool.h>++#include <shirudo/types.h>++struct upatch_pins {+ bool has_link;+ __u32 nr_links;+ __u32 first_link;+ __u32 nr_xdp_progs;+ __u32 first_xdp_prog;+};++void upatch_pins_note(struct upatch_pins *pins, const char *entry);+bool upatch_pins_loaded(const struct upatch_pins *pins);++/* 0, or a negative errno; -ENOENT when the upatch has no pin directory. */+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.+ */+bool 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.+ */+int upatch_each_link(const char *name,+ int (*cb)(__u32 index, const char *pin, void *ctx),+ void *ctx);++#endif /* SHIRUDO_UTIL_UPATCH_PINS_H */diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex b9e2ecbf..f7d7044f 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -27,6 +27,7 @@#include "malloc.h"#include "paths.h"#include "hex.h"+#include "upatch_pins.h"#include "upatch_resolve.h"#include "timefmt.h"@@ -85,6 +86,102 @@ static const char *loc_prefix(__u32 prog_type, __u32 attach_type)return NULL;}+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;+ int fd, ret;++ *out = 0;+ fd = upatch_open_first_link(name);+ if (fd >= 0) {+ ret = bpf_link_get_info_by_fd(fd, &linfo, &len);+ close(fd);+ if (ret)+ return ret;+ *out = linfo.prog_id;+ return linfo.prog_id ? 0 : -EINVAL;+ }+ if (fd != -ENOENT)+ return fd;+ ret = upatch_pins_read(name, &pins);+ if (ret)+ 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))+ return -ENAMETOOLONG;+ fd = bpf_obj_get(pin);+ if (fd < 0)+ return -errno;+ len = sizeof(info);+ ret = bpf_prog_get_info_by_fd(fd, &info, &len);+ close(fd);+ if (ret)+ return ret;+ *out = info.id;+ return info.id ? 0 : -EINVAL;+}++struct xdp_links {+ char device[IF_NAMESIZE + 16];+ __u32 lowest;+ __u32 live;+};++static int note_xdp_link(__u32 index, const char *pin, void *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);+ if (fd < 0)+ return -errno;+ ret = bpf_link_get_info_by_fd(fd, &info, &len);+ close(fd);+ if (ret)+ return ret;+ /* The kernel clears the ifindex when the device goes away. */+ 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)+ return -E2BIG;+ }+ l->live++;+ return 0;+}++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);+ if (ret)+ return ret;+ if (!links.live)+ n = strsprintf(out, cap, "xdp (waiting for a device)");+ else if (links.live == 1)+ n = strsprintf(out, cap, "xdp/%s", links.device);+ else+ n = strsprintf(out, cap, "xdp/%s (+%u)", links.device,+ links.live - 1);+ return n < 0 ? -E2BIG : 0;+}+int upatch_resolve_location(struct btf *vbtf, const char *name,char *out, size_t cap){@@ -93,10 +190,13 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,const char *attach_name = NULL;int link_fd, prog_fd, ret;__u32 attach_type = 0;+ struct upatch_pins pins;const char *prefix;__u32 len;out[0] = '\0';+ if (!upatch_pins_read(name, &pins) && pins.nr_xdp_progs)+ return xdp_location(name, out, cap);link_fd = upatch_open_first_link(name);if (link_fd < 0)return link_fd;@@ -140,14 +240,6 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,return ret;}close(link_fd);- if (linfo.type == BPF_LINK_TYPE_XDP) {- char device[IF_NAMESIZE];-- if (linfo.xdp.ifindex && if_indextoname(linfo.xdp.ifindex, device))- return strsprintf(out, cap, "xdp/%s", device) < 0 ? -E2BIG : 0;- return strsprintf(out, cap, "xdp/ifindex:%u", linfo.xdp.ifindex) < 0 ?- -E2BIG : 0;- }if (linfo.type == BPF_LINK_TYPE_TRACING)attach_type = linfo.tracing.attach_type;if (linfo.type == BPF_LINK_TYPE_NETFILTER &&@@ -196,12 +288,20 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,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';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 &&+ (strsprintf(entry, sizeof(entry), XDP_PROG_PIN_PREFIX "%u",+ pins.first_xdp_prog) < 0 ||+ upatch_pin_path(dir, sizeof(dir), name, entry, NULL)))+ return;if (stat(dir, &st) != 0)return;iso8601(st.st_ctim.tv_sec, out, cap);diff --git a/util/upatch_resolve.h b/util/upatch_resolve.hindex 9fb91222..98fc56c1 100644--- a/util/upatch_resolve.h+++ b/util/upatch_resolve.h@@ -10,6 +10,8 @@#include <stddef.h>+#include <shirudo/types.h>+struct btf;#define UPATCH_LOCATION_MAX 160@@ -21,6 +23,9 @@ struct btf;*/int upatch_open_first_link(const char *name);+/* From the first link, or from an XDP program pin when it has none. */+int upatch_first_prog_id(const char *name, __u32 *out);+/* vbtf may be NULL; the location then degrades to the bare prefix. */int upatch_resolve_location(struct btf *vbtf, const char *name,char *out, size_t cap);