Skip to content

eBPF-based security platform

upatch: add reconcile

John Fastabend9acc7a266c03
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 7d12632b..a1da6510 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -203,6 +203,14 @@ signed config image.
and alternate-attachment lockdown; a repeated init adopts the
archived XDP without another event. Each case cleans up its state
so a failed assertion does not contaminate later cases.
+ * - ``t3112-xdp-reconcile``
+ - ``upatch reconcile`` attaches a device added after load, adds no
+ pin or event on a second pass, leaves a device another program
+ occupies, removes a gone device's pin without reusing a live
+ index, does nothing after unload, and leaves the link on a veth
+ end moved to another namespace. A changed archive is covered by
+ the ``upatch_verify`` unit cases. An unload racing a pass is not
+ reported as tampering.
``tpm`` (4)
~~~~~~~~~~~
diff --git a/Documentation/reference/cli/index.rst b/Documentation/reference/cli/index.rst
index 317d09cd..5c33ee6a 100644
--- a/Documentation/reference/cli/index.rst
+++ b/Documentation/reference/cli/index.rst
@@ -33,6 +33,7 @@ for common tasks.
upatch-info
upatch-get
upatch-map
+ upatch-reconcile
bpm
attest
attest-verify
diff --git a/Documentation/reference/cli/upatch-reconcile.rst b/Documentation/reference/cli/upatch-reconcile.rst
new file mode 100644
index 00000000..5bca7c83
--- /dev/null
+++ b/Documentation/reference/cli/upatch-reconcile.rst
@@ -0,0 +1,98 @@
+..
+ SPDX-License-Identifier: BSD-2-Clause
+ Copyright (c) 2026 Authors of shirudo
+
+:page-type: Reference
+:audience: Operators
+
+shirudo upatch reconcile
+========================
+
+Attach each loaded XDP upatch to the devices its signed classes select
+and that it is not attached to yet.
+
+Runs on: target, as root with the runtime binary, after ``init`` has
+finished on the root file system. Requires: the sealed ``.bpf`` keyring.
+Changes measured state: no.
+
+Usage
+-----
+
+.. code-block:: none
+
+ shirudo upatch reconcile [--report=<path>]
+
+Description
+-----------
+
+``reconcile`` runs one pass and exits. For every upatch with an XDP
+program pinned under ``/run/shirudo/bpffs/upatches/``, it hashes the
+sealed archive, compares the result with the pinned payload, and reads
+the signed device classes from that archive. It then asks each selected
+device what it runs:
+
+.. list-table::
+ :header-rows: 1
+ :widths: 30 70
+
+ * - The device runs
+ - ``reconcile``
+ * - This program
+ - Leaves it.
+ * - Nothing
+ - Attaches a link and pins it as the next ``link_<n>``.
+ * - Anything else
+ - Leaves it. A program already on a device is never replaced.
+
+It also removes the pin of a link whose device is gone. A pass that has
+nothing to do does not take the register lock.
+
+``reconcile`` adds no event to the register: the measured state is the
+upatch, not the devices it reaches.
+
+Every upatch is checked before any is attached. An archive that differs
+from what the records say was loaded stops the pass with ``tampered``,
+and no upatch gets a new link until that is resolved. Pins of a load
+still in progress, or of one that stopped before its record, are skipped,
+as is an upatch unloaded or replaced during the pass; ``status`` reports
+a load that stopped under ``UpatchesMeasured``.
+
+Options
+-------
+
+.. list-table::
+ :header-rows: 1
+ :widths: 18 12 12 58
+
+ * - Option
+ - Argument
+ - Default
+ - Description
+ * - ``--report``
+ - path
+ - none
+ - Write the operation report as JSON, ``-`` for standard output.
+
+Exit status
+-----------
+
+.. list-table::
+ :header-rows: 1
+ :widths: 12 88
+
+ * - Status
+ - Meaning
+ * - 0
+ - The pass finished. A device that is occupied, has no native XDP, or
+ changed or went away during the pass is left for the next pass and
+ does not change the status.
+ * - 1
+ - The pass stopped: see the category in the report.
+ * - 2
+ - A usage error.
+
+See also
+--------
+
+- :doc:`/reference/failure-categories`
+- :doc:`/upatches/write/attach-kinds`
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index 241a0f98..32550fea 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -8,7 +8,7 @@
Failure categories and exit status
==================================
-``shirudo upatch load`` and ``shirudo upatch unload`` classify every
+``shirudo upatch load``, ``unload`` and ``reconcile`` classify every
refusal by a phase and a category. Both appear in the operation report
(``--report``) and in the ``upatch unload failed:`` journal line. Other
commands report failures as messages and exit status only.
@@ -106,9 +106,17 @@ Categories
- The ``.bpf`` keyring is not the frozen anchor (no errno), the
``lsm_link`` pin cannot be opened, the register differs from
``--expect-chain`` or cannot be read (``EPERM``), or the register
- is not the one ``init`` bound (``EACCES``, the load is undone).
+ is not the one ``init`` bound (``EACCES``, the load is undone). From
+ ``upatch reconcile``: the archive of a loaded XDP upatch differs
+ from what the records say was loaded, or its program pin is missing.
- Do not retry. Keep the eventlog and the journal and handle the
target as an incident: :doc:`/operate/troubleshoot/incident-tampered`.
+ * - ``not_initialized``
+ - ``pre_flight``
+ - none
+ - ``upatch reconcile`` ran outside the initial network namespace, or
+ before ``init`` finished on the root file system.
+ - Wait for ``shirudo.service``; the timer runs the pass again.
* - ``eventlog_full``
- ``pre_flight``
- ``ENOSPC``
@@ -308,6 +316,9 @@ every verdict, ``tampered`` included. Parse ``.status`` of
* - ``upatch unload``
- 0, ``already_unloaded`` included; 1 on refusal or failure; 2 for a
usage error.
+ * - ``upatch reconcile``
+ - 0 when the pass finishes, whatever each device's outcome; 1 when it
+ stops; 2 for a usage error.
* - ``upatch list``, ``upatch info``
- 0; 1 on failure; 2 for a usage error.
* - ``upatch get``
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 053d5f33..c183b3e5 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -1549,12 +1549,16 @@ R21 upatches can be neutralized by changing their targets
(A4), by disabling kprobes through debugfs. These are readings of the
kernel source and are not tested.
- XDP discovery is a load-time snapshot: creating interfaces later does
- not attach the upatch to them. Removing a device can detach its XDP
- link while its pin remains, and changing topology can bypass the
- selected ingress path. TPM evidence covers the signed class mask and
- loaded artifact, not the live device inventory. ``t3111`` exercises
- load-time selection and signed unload; hotplug is not reconciled.
+ XDP coverage is reconciled, not continuous. A device added after load
+ runs without the upatch until ``upatch reconcile`` next runs, and is
+ unfiltered until then. A device stays uncovered while its XDP slot is
+ occupied or it has no native XDP. A veth end moved to another namespace
+ keeps its link, so a pair can hold two attachments, one of them running
+ in that namespace. Reconcile adds no register event: TPM evidence covers
+ the signed class mask and loaded artifact, not the device inventory or
+ which devices are covered. ``t3111`` and ``t3112`` exercise selection,
+ reconcile and unload; driver behaviour across a namespace move is
+ tested only for veth.
R22 Labeled inodes are interchangeable
The label carries no identity. Any labeled inode passes shirudo's
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rst
index a1ab1b3d..e978b354 100644
--- a/Documentation/upatches/write/attach-kinds.rst
+++ b/Documentation/upatches/write/attach-kinds.rst
@@ -306,9 +306,11 @@ XDP programs are never replaced. Device classification and peer identity
are rechecked around attachment, and the returned link's ifindex is
checked before pinning.
-Discovery runs once per load or restore. New devices require reload;
-there is no background reconciliation. Removing a device can detach its
-XDP link while its pin remains. The signed artifact and TPM measurement
+Load and restore each discover devices once.
+:doc:`/reference/cli/upatch-reconcile` repeats that discovery later: it
+attaches a loaded upatch to any selected device it does not run on yet
+and removes the pin of a link whose device has gone. Removing a device
+detaches its XDP link. The signed artifact and TPM measurement
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)``.
diff --git a/Makefile.shirudo b/Makefile.shirudo
index f859769b..30b340df 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -280,6 +280,7 @@ UTIL_SRCS := \
util/upatch_maps_otel.c \
util/elf_target.c \
util/upatch_pins.c \
+ util/upatch_verify.c \
util/upatch_resolve.c \
util/upatch_table.c
diff --git a/builtin-upatch.c b/builtin-upatch.c
index d305a9da..1d46a612 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -64,6 +64,7 @@
#include "util/upatch_maps_otel.h"
#include "util/upatch_pins.h"
#include "util/upatch_resolve.h"
+#include "util/upatch_verify.h"
#include "util/upatch_table.h"
#include "util/version.h"
#include "util/xdp.h"
@@ -4180,6 +4181,469 @@ static int cmd_upatch_get(int argc, const char **argv)
return ret;
}
+static const struct help upatch_reconcile_help = {
+ .usage = "shirudo upatch reconcile [--report=<path>]",
+ .summary = "Attach each loaded XDP upatch to the devices its signed "
+ "classes select\nand that it is not attached to yet, and "
+ "remove the pins of devices that\nare gone. One pass; udev "
+ "and a timer run it.",
+};
+
+struct xdp_work {
+ char name[UPATCH_NAME_MAX + 1];
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ struct upatch u;
+ struct prog_spec *specs;
+};
+
+static void xdp_work_free(struct xdp_work *work, size_t count)
+{
+ size_t i;
+
+ for (i = 0; i < count; i++) {
+ prog_specs_free(work[i].specs, work[i].u.hdr.nr_progs);
+ upatch_close(&work[i].u);
+ }
+ xfree(work, size_mul(count, sizeof(*work)));
+}
+
+/* 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;
+
+ *held = false;
+ 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);
+ }
+ measured_set_free(set);
+ return 0;
+}
+
+static int reconcile_read(struct xdp_work *w)
+{
+ const void *attach;
+ __u32 len;
+ int ret;
+
+ if (get_pin_payload(w->name, w->payload))
+ return -ENODATA;
+ ret = upatch_archive_verify(w->name, w->payload, &w->u);
+ if (ret)
+ return ret;
+ attach = upatch_signed_attach(&w->u, &len);
+ ret = attach ? prog_specs_parse(attach, len, w->u.hdr.nr_maps,
+ w->u.hdr.nr_progs, &w->specs) : -ESTALE;
+ if (ret) {
+ upatch_close(&w->u);
+ return ret == -ENOMEM ? ret : -ESTALE;
+ }
+ return 0;
+}
+
+static int reconcile_fail_verify(const char *name, int err,
+ struct op_report *rep)
+{
+ if (err == -ESTALE || err == -ENODATA) {
+ pr_err("upatch reconcile: the archive of %s is not what was "
+ "loaded (tampered)", name);
+ rep_fail(rep, "pre_flight", "tampered", 0,
+ "archive differs from the loaded payload");
+ } else {
+ pr_err("upatch reconcile: cannot check %s: %s", name,
+ strerror(-err));
+ rep_fail(rep, "pre_flight", "unknown", err, "archive check failed");
+ }
+ return 1;
+}
+
+/*
+ * A load in progress has pins before it has an archive, and one that died
+ * leaves pins without a record, which status reports as such. Only the
+ * register lock tells those from an upatch the records hold whose archive
+ * has changed, which is the one this refuses.
+ */
+static int reconcile_open(struct xdp_work *w, bool *skip,
+ struct op_report *rep)
+{
+ bool held;
+ int lock, ret;
+
+ *skip = false;
+ if (!reconcile_read(w))
+ return 0;
+ lock = eventlog_lock();
+ if (lock < 0) {
+ rep_fail(rep, "pre_flight", "tpm_error", lock,
+ "cannot take the register lock");
+ return 1;
+ }
+ ret = pin_is_measured(w->name, w->payload, &held);
+ if (!ret && !held) {
+ *skip = true;
+ eventlog_unlock(lock);
+ return 0;
+ }
+ if (!ret)
+ ret = reconcile_read(w);
+ eventlog_unlock(lock);
+ return ret ? reconcile_fail_verify(w->name, ret, rep) : 0;
+}
+
+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;
+
+ *out = NULL;
+ *count = 0;
+ d = opendir(BPFFS_UPATCHES_DIR);
+ if (!d) {
+ if (errno == ENOENT)
+ return 0;
+ rep_fail(rep, "pre_flight", "unknown", errno,
+ "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++;
+ }
+ closedir(d);
+ if (ret) {
+ xdp_work_free(work, nr);
+ return ret;
+ }
+ *out = work;
+ *count = nr;
+ return 0;
+}
+
+static int 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))
+ return -ENAMETOOLONG;
+ fd = bpf_obj_get(pin);
+ if (fd < 0)
+ return -errno;
+ ret = bpf_prog_get_info_by_fd(fd, &info, &len);
+ if (!ret && info.type != BPF_PROG_TYPE_XDP)
+ ret = -ESTALE;
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+ *id = info.id;
+ return fd;
+}
+
+struct link_scan {
+ __u32 next;
+ bool dead;
+ bool collect;
+};
+
+/* 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)
+{
+ 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) {
+ scan->dead = true;
+ if (scan->collect) {
+ if (unlink(pin))
+ return -errno;
+ pr_info("XDP: removed %s, its device is gone", pin);
+ return 0;
+ }
+ }
+ if (index == UINT32_MAX)
+ return -EOVERFLOW;
+ if (index >= scan->next)
+ scan->next = index + 1;
+ return 0;
+}
+
+static int reconcile_attach(int prog_fd, const char *name,
+ const struct prog_spec *spec,
+ const struct xdp_device *dev, __u32 *next,
+ struct op_report *rep)
+{
+ char dir[PATH_MAX], pin[PATH_MAX + 32];
+ int ret;
+
+ if (*next == UINT32_MAX ||
+ upatch_pin_path(dir, sizeof(dir), name, NULL) ||
+ strsprintf(pin, sizeof(pin), "%s/link_%u", dir, *next) < 0) {
+ rep_fail(rep, "link_create", "unknown", ENAMETOOLONG,
+ "no pin name left for the link");
+ return 1;
+ }
+ ret = xdp_attach_one(prog_fd, spec, dev, pin);
+ if (!ret) {
+ (*next)++;
+ pr_audit("XDP: upatch=%s attached device=%s ifindex=%u class=%u",
+ name, dev->name, dev->ifindex, xdp_device_class(dev));
+ } else if (ret == -EACCES || ret == -EPERM) {
+ rep_fail(rep, "link_create", "lsm_denied", ret,
+ "the guards refused the XDP link");
+ return 1;
+ }
+ /*
+ * Anything else is about this device alone: it is occupied, has no
+ * native XDP, or changed or went away. The next pass looks again.
+ */
+ return 0;
+}
+
+static int reconcile_prog_failed(const char *name, __u32 index, int err,
+ struct op_report *rep)
+{
+ pr_err("upatch reconcile: program %u of %s: %s", index, name,
+ strerror(-err));
+ if (err == -EACCES || err == -EPERM)
+ rep_fail(rep, "pre_flight", "lsm_denied", err,
+ "the guards refused the XDP program pin");
+ else
+ rep_fail(rep, "pre_flight", "tampered", err,
+ "an XDP program pin is missing or not XDP");
+ return 1;
+}
+
+/*
+ * With next NULL only says whether a device needs attaching, or that the
+ * pins need a look under the register lock.
+ */
+static int reconcile_walk(const struct xdp_work *w,
+ const struct xdp_device *devices, size_t count,
+ __u32 *next, bool *needed, struct op_report *rep)
+{
+ __u32 i;
+
+ for (i = 0; i < w->u.hdr.nr_progs; i++) {
+ const struct prog_spec *spec = &w->specs[i];
+ __u32 prog_id = 0;
+ size_t j;
+ int fd;
+
+ if (spec->kind != UPATCH_ATTACH_XDP)
+ continue;
+ fd = xdp_prog_open(w->name, i, &prog_id);
+ if (fd < 0) {
+ if (!next) {
+ *needed = true;
+ return 0;
+ }
+ return reconcile_prog_failed(w->name, i, fd, rep);
+ }
+ for (j = 0; j < count; j++) {
+ bool selected = xdp_device_selected(devices, count, j,
+ spec->device_classes);
+ __u32 attached = 0;
+
+ if (selected &&
+ bpf_xdp_query_id((int)devices[j].ifindex,
+ XDP_FLAGS_DRV_MODE, &attached))
+ continue;
+ if (xdp_reconcile_action(selected, attached, prog_id) !=
+ XDP_RECONCILE_ATTACH)
+ continue;
+ *needed = true;
+ if (next && reconcile_attach(fd, w->name, spec,
+ &devices[j], next, rep)) {
+ close(fd);
+ return 1;
+ }
+ }
+ close(fd);
+ }
+ return 0;
+}
+
+static int reconcile_apply(const struct xdp_work *w,
+ const struct xdp_device *devices, size_t count,
+ struct op_report *rep)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ struct link_scan scan = { .collect = true };
+ struct upatch again;
+ bool held, needed = false;
+ int lock, ret;
+
+ lock = eventlog_lock();
+ if (lock < 0) {
+ rep_fail(rep, "pre_flight", "tpm_error", lock,
+ "cannot take the register lock");
+ return 1;
+ }
+ ret = pin_is_measured(w->name, payload, &held);
+ if (ret) {
+ rep_fail(rep, "pre_flight", "tpm_error", ret,
+ "cannot read this boot's records");
+ ret = 1;
+ goto out;
+ }
+ if (!held || memneq_explicit(payload, w->payload, sizeof(payload)))
+ goto out;
+ ret = upatch_archive_verify(w->name, payload, &again);
+ if (ret) {
+ ret = reconcile_fail_verify(w->name, ret, rep);
+ goto out;
+ }
+ upatch_close(&again);
+ ret = upatch_each_link(w->name, scan_link, &scan);
+ if (ret) {
+ rep_fail(rep, "link_create", ret == -EACCES || ret == -EPERM ?
+ "lsm_denied" : "unknown", ret, "cannot read the links");
+ ret = 1;
+ goto out;
+ }
+ ret = reconcile_walk(w, devices, count, &scan.next, &needed, rep);
+out:
+ eventlog_unlock(lock);
+ return ret;
+}
+
+static int reconcile_preflight(struct op_report *rep)
+{
+ key_serial_t keyring, serial;
+
+ if (netns_initial()) {
+ rep_fail(rep, "pre_flight", "not_initialized", 0,
+ "not in the initial network namespace");
+ return 1;
+ }
+ if (!path_exists(LSM_LINK_PIN) || path_exists(eventlog_pending_path())) {
+ rep_fail(rep, "pre_flight", "not_initialized", 0,
+ "init has not finished on the root file system");
+ return 1;
+ }
+ return trust_anchor_keyring(&keyring, &serial, rep) ? 1 : 0;
+}
+
+static int reconcile_upatch(const struct xdp_work *w,
+ const struct xdp_device *devices, size_t count,
+ struct op_report *rep)
+{
+ struct link_scan scan = {};
+ bool needed = false;
+
+ /*
+ * Without the register lock a missing pin may be an unload or an
+ * 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))
+ needed = true;
+ else if (reconcile_walk(w, devices, count, NULL, &needed, rep))
+ return 1;
+ if (!needed && !scan.dead)
+ return 0;
+ return reconcile_apply(w, devices, count, rep);
+}
+
+/*
+ * Every upatch is checked before any is attached: a changed archive stops
+ * the pass, so nothing keeps acting on a host in that state.
+ */
+static int cmd_upatch_reconcile(int argc, const char **argv)
+{
+ struct op_report rep = { .op = "upatch_reconcile", .phase = "done" };
+ size_t nr_work = 0, nr_devices = 0, n;
+ struct xdp_device *devices = NULL;
+ struct xdp_work *work = NULL;
+ const char *report = NULL;
+ int ret = 1;
+ size_t i;
+
+ for (i = 1; i < (size_t)argc; i++) {
+ const char *a = argv[i], *v = NULL;
+ enum opt_status r;
+
+ if (is_help(a)) {
+ print_help(&upatch_reconcile_help);
+ return 0;
+ }
+ if (!opt_arg("--report", a, argc, argv, &i, &v, &r) ||
+ r == OPT_MISSING) {
+ fprintf(stderr, "upatch reconcile: bad option '%s'\n", a);
+ return 2;
+ }
+ report = v;
+ }
+ if (reconcile_preflight(&rep) ||
+ reconcile_collect(&work, &nr_work, &rep))
+ goto done;
+ if (nr_work) {
+ int err = xdp_devices_read(&devices, &nr_devices);
+
+ if (err) {
+ rep_fail(&rep, "pre_flight", "unknown", err,
+ "cannot read the interfaces");
+ goto done;
+ }
+ }
+ for (n = 0; n < nr_work; n++)
+ if (reconcile_upatch(&work[n], devices, nr_devices, &rep))
+ goto done;
+ rep.ok = true;
+ ret = 0;
+done:
+ xfree(devices, size_mul(nr_devices, sizeof(*devices)));
+ xdp_work_free(work, nr_work);
+ if (report)
+ report_emit(report, &rep);
+ report_free(&rep);
+ return ret;
+}
+
static const struct help_item upatch_subcmds[] = {
{ "load", "load a signed .upatch into the target kernel (target-only)" },
{ "unload", "apply a signed UNLOAD directive (forward chain extension)" },
@@ -4187,6 +4651,7 @@ static const struct help_item upatch_subcmds[] = {
{ "info", "describe a .upatch file (any host; offline, no kernel state)" },
{ "get", "dump source of a loaded upatch from the target kernel (BTF line_info)" },
{ "map", "read pinned map contents (target-only; BTF type-aware)" },
+ { "reconcile", "attach loaded XDP upatches to devices that appeared later (target-only)" },
};
static const struct help_section upatch_help_sections[] = {
@@ -4233,5 +4698,7 @@ int cmd_upatch(int argc, const char **argv)
return cmd_upatch_get(argc - 1, argv + 1);
if (strcmp(canonical, "map") == 0)
return cmd_upatch_map(argc - 1, argv + 1);
+ if (strcmp(canonical, "reconcile") == 0)
+ return cmd_upatch_reconcile(argc - 1, argv + 1);
return 2; /* unreachable */
}
diff --git a/completion/shirudo.bash b/completion/shirudo.bash
index 12cbfc84..3feda1b6 100644
--- a/completion/shirudo.bash
+++ b/completion/shirudo.bash
@@ -61,6 +61,9 @@ _shirudo()
--expect-version-id --no-precheck \
--no-version-check --report" -- "$cur") )
;;
+ reconcile)
+ COMPREPLY=( $(compgen -W "--report" -- "$cur") )
+ ;;
info|list)
COMPREPLY=( $(compgen -W "--json" -- "$cur") )
;;
diff --git a/testing/functional/upatch/t3112-xdp-reconcile.sh b/testing/functional/upatch/t3112-xdp-reconcile.sh
new file mode 100644
index 00000000..8a4d1f50
--- /dev/null
+++ b/testing/functional/upatch/t3112-xdp-reconcile.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+H=./helper/test-xdp
+B=/run/shirudo/bpffs/upatches
+NAME=shirudo-test-xdp-virtual
+CONFLICT=shirudo-test-xdp-conflict
+[ -x "$H" ] || test_skip_all "XDP helper unavailable"
+command -v ip >/dev/null 2>&1 || test_skip_all "ip unavailable"
+for name in "$NAME" "$CONFLICT"; do
+ [ -s "$FIXTURES/$name.upatch" ] || test_skip_all "XDP fixture unavailable"
+done
+
+load() {
+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$1.upatch" --no-precheck
+}
+
+unload() {
+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$1.unload.upatch" --no-precheck
+}
+
+reconcile() {
+ "$SHIRUDO_BIN" upatch reconcile
+}
+
+add_pair() {
+ ip link add "$1" index "$2" type veth peer name "$3" index "$4" &&
+ ip link set "$1" up && ip link set "$3" up
+}
+
+cleanup() {
+ for name in "$NAME" "$CONFLICT"; do
+ unload "$name" >/dev/null 2>&1 || true
+ done
+ for dev in xdpa xdpb xdpc xdpd xdpe xdpf; do
+ ip link del "$dev" >/dev/null 2>&1 || true
+ done
+ ip netns del xdp-test-a >/dev/null 2>&1 || true
+}
+trap cleanup EXIT
+
+test_xdp_success() {
+ cleanup
+ test_expect_success "$@"
+}
+
+test_xdp_success 'a pass with no XDP upatch does nothing' '
+ reconcile
+'
+
+test_xdp_success 'a device added after load is attached, once' '
+ add_pair xdpa 30000 xdpb 30001 &&
+ load "$NAME" &&
+ add_pair xdpc 30010 xdpd 30011 &&
+ [ "$("$H" query xdpc)" = 0 ] &&
+ reconcile &&
+ [ "$("$H" query xdpc)" -gt 0 ] &&
+ [ "$("$H" query xdpd)" = 0 ] &&
+ [ -e "$B/$NAME/link_1" ] && [ ! -e "$B/$NAME/link_2" ] &&
+ before=$(wc -l </var/lib/shirudo/eventlog) &&
+ pins=$(ls "$B/$NAME" | wc -l) &&
+ reconcile &&
+ [ "$(wc -l </var/lib/shirudo/eventlog)" = "$before" ] &&
+ [ "$(ls "$B/$NAME" | wc -l)" = "$pins" ]
+'
+
+test_xdp_success 'a device another program occupies is left alone' '
+ add_pair xdpa 30000 xdpb 30001 &&
+ load "$NAME" &&
+ ip link del xdpa &&
+ add_pair xdpc 30010 xdpd 30011 &&
+ load "$CONFLICT" &&
+ id=$("$H" query xdpc) && [ "$id" -gt 0 ] &&
+ reconcile &&
+ [ "$("$H" query xdpc)" = "$id" ]
+'
+
+test_xdp_success 'a gone device loses its pin and no live index is reused' '
+ add_pair xdpa 30000 xdpb 30001 &&
+ add_pair xdpc 30010 xdpd 30011 &&
+ load "$NAME" &&
+ [ -e "$B/$NAME/link_0" ] && [ -e "$B/$NAME/link_1" ] &&
+ ip link del xdpa &&
+ add_pair xdpe 30020 xdpf 30021 &&
+ reconcile &&
+ [ ! -e "$B/$NAME/link_0" ] &&
+ [ -e "$B/$NAME/link_1" ] && [ -e "$B/$NAME/link_2" ] &&
+ [ "$("$H" query xdpc)" -gt 0 ] &&
+ [ "$("$H" query xdpe)" -gt 0 ]
+'
+
+test_xdp_success 'a pass after unload leaves nothing behind' '
+ add_pair xdpa 30000 xdpb 30001 &&
+ load "$NAME" &&
+ unload "$NAME" &&
+ reconcile &&
+ [ ! -e "$B/$NAME" ] &&
+ [ "$("$H" query xdpa)" = 0 ]
+'
+
+test_xdp_success 'a veth end moved away keeps its link and the host end attaches' '
+ add_pair xdpa 30000 xdpb 30001 &&
+ load "$NAME" &&
+ [ "$("$H" query xdpa)" -gt 0 ] && [ "$("$H" query xdpb)" = 0 ] &&
+ ip netns add xdp-test-a &&
+ ip link set xdpa netns xdp-test-a &&
+ reconcile &&
+ [ "$(ip netns exec xdp-test-a "$H" query xdpa)" -gt 0 ] &&
+ [ "$("$H" query xdpb)" -gt 0 ]
+'
+
+test_xdp_success 'an unload racing a pass is not tampering' '
+ add_pair xdpa 30000 xdpb 30001 &&
+ (
+ i=0
+ while [ "$i" -lt 30 ]; do
+ load "$NAME" >/dev/null 2>&1
+ unload "$NAME" >/dev/null 2>&1
+ i=$((i + 1))
+ done
+ ) &
+ churn=$!
+ bad=0
+ while kill -0 "$churn" 2>/dev/null; do
+ report=$("$SHIRUDO_BIN" upatch reconcile --report=- 2>/dev/null) || bad=1
+ case $report in *tampered*) bad=1 ;; esac
+ done
+ wait "$churn"
+ [ "$bad" = 0 ]
+'
+
+test_done
diff --git a/testing/unit/upatch_verify.c b/testing/unit/upatch_verify.c
new file mode 100644
index 00000000..51e685a1
--- /dev/null
+++ b/testing/unit/upatch_verify.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "unit.h"
+#include "upatch.h"
+#include "upatch_verify.h"
+
+#define FIXTURE "fuzz/corpus/upatch/xdp-virtual"
+
+/* make -C testing runs from testing/, and by hand from the top of the tree. */
+static __u8 *read_fixture(size_t *len)
+{
+ static __u8 buf[1 << 16];
+ FILE *f = fopen(FIXTURE, "r");
+
+ if (!f)
+ f = fopen("testing/" FIXTURE, "r");
+ ASSERT_TRUE(f);
+ *len = fread(buf, 1, sizeof(buf), f);
+ fclose(f);
+ return buf;
+}
+
+static int memfd_of(const __u8 *buf, size_t len)
+{
+ char path[] = "/tmp/shirudo-unit-verify-XXXXXX";
+ int fd = mkstemp(path);
+
+ ASSERT_TRUE(fd >= 0);
+ unlink(path);
+ ASSERT_EQ(write(fd, buf, len), (ssize_t)len);
+ return fd;
+}
+
+static void payload_of_fixture(const __u8 *buf, size_t len,
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN])
+{
+ int fd = memfd_of(buf, len);
+ struct upatch u;
+
+ ASSERT_EQ(upatch_open_fd(fd, &u), 0);
+ close(fd);
+ memcpy(payload, u.hdr.payload_sha256, UPATCH_PAYLOAD_HASH_LEN);
+ upatch_close(&u);
+}
+
+static void the_loaded_bytes_verify(void)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ size_t len = 0;
+ const __u8 *buf = read_fixture(&len);
+ struct upatch u;
+ int fd;
+
+ payload_of_fixture(buf, len, payload);
+ fd = memfd_of(buf, len);
+ ASSERT_EQ(upatch_verify_fd(fd, payload, &u), 0);
+ close(fd);
+ upatch_close(&u);
+}
+
+static void another_payload_is_stale(void)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ size_t len = 0;
+ const __u8 *buf = read_fixture(&len);
+ struct upatch u;
+ int fd;
+
+ payload_of_fixture(buf, len, payload);
+ payload[0] ^= 1;
+ fd = memfd_of(buf, len);
+ EXPECT_EQ(upatch_verify_fd(fd, payload, &u), -ESTALE);
+ close(fd);
+}
+
+static void changed_data_under_an_intact_header_is_stale(void)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN], copy[1 << 16];
+ size_t len = 0, off;
+ const __u8 *buf = read_fixture(&len);
+ struct upatch u;
+ int fd;
+
+ payload_of_fixture(buf, len, payload);
+ fd = memfd_of(buf, len);
+ ASSERT_EQ(upatch_open_fd(fd, &u), 0);
+ close(fd);
+ off = u.hdr.data_off;
+ ASSERT_TRUE(u.hdr.data_len > 0);
+ upatch_close(&u);
+ memcpy(copy, buf, len);
+ copy[off] ^= 1;
+ fd = memfd_of(copy, len);
+ EXPECT_EQ(upatch_verify_fd(fd, payload, &u), -ESTALE);
+ close(fd);
+}
+
+static void a_truncated_file_is_stale(void)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN] = {};
+ size_t len = 0;
+ const __u8 *buf = read_fixture(&len);
+ struct upatch u;
+ int fd;
+
+ fd = memfd_of(buf, 64);
+ EXPECT_EQ(upatch_verify_fd(fd, payload, &u), -ESTALE);
+ close(fd);
+}
+
+static void a_missing_archive_is_stale(void)
+{
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN] = {};
+ struct upatch u;
+
+ EXPECT_EQ(upatch_archive_verify("shirudo-unit-no-such-upatch",
+ payload, &u), -ESTALE);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(the_loaded_bytes_verify),
+ UNIT_CASE(another_payload_is_stale),
+ UNIT_CASE(changed_data_under_an_intact_header_is_stale),
+ UNIT_CASE(a_truncated_file_is_stale),
+ UNIT_CASE(a_missing_archive_is_stale),
+ {}
+};
+
+UNIT_SUITE(upatch_verify, cases);
diff --git a/testing/unit/xdp.c b/testing/unit/xdp.c
index 26078e81..e5442572 100644
--- a/testing/unit/xdp.c
+++ b/testing/unit/xdp.c
@@ -96,6 +96,16 @@ static void unresolved_devices_sit_out(void)
EXPECT_FALSE(xdp_device_selected(devs, 3, 2, UPATCH_XDP_ALL));
}
+static void reconcile_never_touches_another_program(void)
+{
+ EXPECT_EQ(xdp_reconcile_action(false, 0, 7), XDP_RECONCILE_NONE);
+ EXPECT_EQ(xdp_reconcile_action(false, 7, 7), XDP_RECONCILE_NONE);
+ EXPECT_EQ(xdp_reconcile_action(false, 9, 7), XDP_RECONCILE_NONE);
+ EXPECT_EQ(xdp_reconcile_action(true, 0, 7), XDP_RECONCILE_ATTACH);
+ EXPECT_EQ(xdp_reconcile_action(true, 7, 7), XDP_RECONCILE_COVERED);
+ EXPECT_EQ(xdp_reconcile_action(true, 9, 7), XDP_RECONCILE_OCCUPIED);
+}
+
static size_t add_attr(__u8 *buf, size_t len, __u16 type,
const void *value, size_t size)
{
@@ -166,6 +176,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(veth_peers_are_namespace_aware),
UNIT_CASE(veth_without_a_peer_is_not_selected),
UNIT_CASE(unresolved_devices_sit_out),
+ UNIT_CASE(reconcile_never_touches_another_program),
UNIT_CASE(link_messages_are_checked),
UNIT_CASE(kernel_discovery_revalidates_devices),
{}
diff --git a/util/report.h b/util/report.h
index db90f8e6..cdce91de 100644
--- a/util/report.h
+++ b/util/report.h
@@ -22,7 +22,7 @@
* Category strings: ok, tampered, keyring_missing, lsm_denied,
* signature_rejected, verifier_rejected, parser_rejected,
* payload_mismatch, already_loaded, already_unloaded, detach_failed,
- * tpm_error, unknown.
+ * tpm_error, not_initialized, unknown.
*/
struct op_report {
bool ok;
diff --git a/util/upatch_verify.c b/util/upatch_verify.c
new file mode 100644
index 00000000..e0bb8cf7
--- /dev/null
+++ b/util/upatch_verify.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Whether the archive of a loaded upatch is still what was loaded.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <shirudo/string.h>
+
+#include "chain.h"
+#include "paths.h"
+#include "seal.h"
+#include "upatch.h"
+#include "upatch_verify.h"
+
+static int stale_unless_resources(int err)
+{
+ return err == -ENOMEM || err == -EMFILE || err == -ENFILE ? err : -ESTALE;
+}
+
+int upatch_verify_fd(int fd, const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],
+ struct upatch *out)
+{
+ __u8 digest[UPATCH_PAYLOAD_HASH_LEN];
+ int ret;
+
+ ret = upatch_open_fd(fd, out);
+ if (ret)
+ return stale_unless_resources(ret);
+ ret = sha256_2(upatch_loader(out), out->hdr.loader_insns_len,
+ upatch_data(out), out->hdr.data_len, digest);
+ if (!ret && (memneq_explicit(digest, payload, sizeof(digest)) ||
+ memneq_explicit(out->hdr.payload_sha256, payload,
+ sizeof(digest))))
+ ret = -ESTALE;
+ if (ret)
+ upatch_close(out);
+ return ret;
+}
+
+int upatch_archive_verify(const char *name,
+ const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],
+ struct upatch *out)
+{
+ char header_name[UPATCH_NAME_MAX + 1];
+ char path[PATH_MAX];
+ int fd, ret;
+
+ if (strsprintf(path, sizeof(path), "%s/%s.upatch", UPATCHES_DIR,
+ name) < 0)
+ return -ENAMETOOLONG;
+ fd = seal_open_read(path);
+ if (fd < 0)
+ return stale_unless_resources(fd);
+ ret = upatch_verify_fd(fd, payload, out);
+ close(fd);
+ if (ret)
+ return ret;
+ upatch_header_name(out, header_name);
+ if (strcmp(header_name, name) || !upatch_name_signed(out)) {
+ upatch_close(out);
+ return -ESTALE;
+ }
+ return 0;
+}
diff --git a/util/upatch_verify.h b/util/upatch_verify.h
new file mode 100644
index 00000000..2e6a067c
--- /dev/null
+++ b/util/upatch_verify.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Whether the archive of a loaded upatch is still what was loaded.
+ */
+
+#ifndef SHIRUDO_UTIL_UPATCH_VERIFY_H
+#define SHIRUDO_UTIL_UPATCH_VERIFY_H
+
+#include <shirudo/types.h>
+#include <shirudo/upatch.h>
+
+struct upatch;
+
+/*
+ * 0 with the artifact open in *out, -ESTALE when it is not what payload
+ * names, or another negative errno when the check itself could not run.
+ * The bytes are hashed, not the header's claim about them.
+ */
+int upatch_verify_fd(int fd, const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],
+ struct upatch *out);
+int upatch_archive_verify(const char *name,
+ const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],
+ struct upatch *out);
+
+#endif /* SHIRUDO_UTIL_UPATCH_VERIFY_H */
diff --git a/util/xdp.c b/util/xdp.c
index 5f4b0b69..9cc7528c 100644
--- a/util/xdp.c
+++ b/util/xdp.c
@@ -346,6 +346,16 @@ bool xdp_device_selected(const struct xdp_device *devs, size_t count,
return peer && dev->ifindex < peer->ifindex;
}
+enum xdp_reconcile xdp_reconcile_action(bool selected, __u32 attached,
+ __u32 prog_id)
+{
+ if (!selected)
+ return XDP_RECONCILE_NONE;
+ if (!attached)
+ return XDP_RECONCILE_ATTACH;
+ return attached == prog_id ? XDP_RECONCILE_COVERED : XDP_RECONCILE_OCCUPIED;
+}
+
struct device_list {
struct xdp_device *devices;
size_t count;
diff --git a/util/xdp.h b/util/xdp.h
index 6ce94619..4ac7f66e 100644
--- a/util/xdp.h
+++ b/util/xdp.h
@@ -28,6 +28,17 @@ struct xdp_device {
bool unresolved;
};
+enum xdp_reconcile {
+ XDP_RECONCILE_NONE,
+ XDP_RECONCILE_COVERED,
+ XDP_RECONCILE_ATTACH,
+ XDP_RECONCILE_OCCUPIED,
+};
+
+/* attached is what the device runs now, 0 for nothing. */
+enum xdp_reconcile xdp_reconcile_action(bool selected, __u32 attached,
+ __u32 prog_id);
+
int xdp_device_parse(const void *data, size_t len, struct xdp_device *out);
__u32 xdp_device_class(const struct xdp_device *dev);
bool xdp_device_selected(const struct xdp_device *devs, size_t count,