Skip to content

eBPF-based security platform

upatch: refuse a command whose undo pass cannot finish

Daniel Borkmann21e0a043eda3
diff --git a/AGENTS.md b/AGENTS.md
index 21dd9943..d9e2d9b3 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -184,9 +184,10 @@ one usually still compiles and passes tests.
register event and a re-run of `init` adds none. A load stopped
between its pins and its extend leaves pins without a record: `status`
reports them (`UpatchesMeasured`) and the next upatch command undoes
- them under the register lock, which a load or an unload holds from
- before it touches the pins to its append, and a purge from its archive
- read until its pins and archive are gone. `init` restores only an
+ them, or refuses when it cannot, under the register lock, which a
+ load or an unload holds from before it touches the pins to its
+ append, and a purge from its archive read until its pins and archive
+ are gone. `init` restores only an
archive whose load the eventlog records without a later unload; a
labeled archive root brings back is reported and removed, never
restored. The register follows
diff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rst
index 05ba766a..c6e1d29d 100644
--- a/Documentation/internals/measurement-register.rst
+++ b/Documentation/internals/measurement-register.rst
@@ -272,7 +272,9 @@ that records more than happened, never less. A load stopped between its
pins and its extension leaves pins, and after its archive copy an
archive, that the register does not cover: ``status`` reports them and
the next load or unload undoes them under the same lock before it acts,
-so the register stays behind what runs for one command at most. An
+so the register stays behind what runs for one command at most. A load
+or unload that cannot read this boot's records, or cannot undo what they
+do not cover, refuses rather than act beside pins it cannot account for. An
append that fails after
a successful extension is reported as a desync for the operator to
reconcile.
diff --git a/Documentation/reference/cli/upatch-load.rst b/Documentation/reference/cli/upatch-load.rst
index a8659e8c..1fd3f095 100644
--- a/Documentation/reference/cli/upatch-load.rst
+++ b/Documentation/reference/cli/upatch-load.rst
@@ -50,8 +50,10 @@ Description
it holds to the end, and undoes the pins and archives this boot's
records do not cover: those of a load stopped before its extend, of
an unload stopped after it, and an archive brought back after its
- unload, which ``status`` reports under ``UpatchesMeasured``. A upatch
- of the same name that is loaded is refused (``already_loaded``).
+ unload, which ``status`` reports under ``UpatchesMeasured``. It
+ refuses (``tpm_error``) when it cannot read this boot's records or
+ cannot undo one of them. A upatch of the same name that is loaded is
+ refused (``already_loaded``).
#. It parses the artifact: a LOAD artifact, a safe and signed name, a
payload digest that matches the payload, valid attach metadata
(``parser_rejected``). For a uprobe it checks each target binary
diff --git a/Documentation/reference/cli/upatch-unload.rst b/Documentation/reference/cli/upatch-unload.rst
index 4c6ebc97..783951eb 100644
--- a/Documentation/reference/cli/upatch-unload.rst
+++ b/Documentation/reference/cli/upatch-unload.rst
@@ -41,7 +41,8 @@ both in signed sections (see :doc:`build`). ``unload``:
#. Refuses when the eventlog would exceed ``eventlog.max_bytes``
(``eventlog_full``).
#. Takes the register lock, which it holds to the end, and undoes the
- pins and archives this boot's records do not cover, as ``load`` does.
+ pins and archives this boot's records do not cover, or refuses when
+ it cannot, as ``load`` does.
#. Loads the directive's loader with ``BPF_PROG_LOAD`` against the
``.bpf`` keyring, so that the kernel verifies its signature before
anything is removed.
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index b89ecf71..ef539d40 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -199,8 +199,10 @@ Categories
* - ``tpm_error``
- ``tpm_extend``
- errno of the call
- - The register lock could not be taken, in which case nothing was
- loaded or detached, or the TPM or the extend failed. A load is
+ - The register lock could not be taken, or this boot's records could
+ not be read or what they do not cover could not be undone, in which
+ case nothing was loaded or detached, or the TPM or the extend
+ failed. A load is
undone when the register did not move. When the extend succeeded and the
eventlog append failed, the message reads ``eventlog append failed
after the extend``.
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 51951cf3..77d319e3 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -926,8 +926,9 @@ pins the register does not
cover and the upatch attached: ``shirudo status`` reports them
(``UpatchesMeasured``, verdict ``tampered``) and the next upatch
command, ``bpm``'s included, undoes them, pins and archives, before it
-acts. The same scan finds the pins an unload stopped after its extend
-leaves, and an archive without a record of its load; ``init`` restores
+acts, and refuses when it cannot read the records or undo them. The
+same scan finds the pins an unload stopped after its extend leaves, and
+an archive without a record of its load; ``init`` restores
only an archive whose load the eventlog records without a later unload
(``t6144``). A measuring
command holds a lock beside the eventlog from its extend to its append,
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 42148b0d..aad6391d 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2706,28 +2706,39 @@ static void unload_unmeasured(const char *name)
name);
}
+/* The rest are still undone when one fails; *ctx keeps the first error. */
static int undo_unmeasured_pin(const char *name, void *ctx)
{
- int ret = undo_load(name);
+ int *failed = ctx, ret = undo_load(name);
- if (ret)
+ if (ret) {
pr_err("upatch: %s is pinned without a record of this boot and could not be undone: %s; operator must reconcile",
name, strerror(-ret));
- else
+ if (!*failed)
+ *failed = ret;
+ } else {
pr_audit("upatch: %s was pinned without a record of this boot; the load was undone",
name);
+ }
return 0;
}
static int undo_unmeasured_archive(const char *name, const char *path,
void *ctx)
{
+ int *failed = ctx, ret = 0;
+
if (unlink(path) != 0 && errno != ENOENT)
+ ret = -errno;
+ if (ret) {
pr_err("upatch: %s is archived without a record of its load and could not be removed: %s; operator must reconcile",
- name, strerror(errno));
- else
+ name, strerror(-ret));
+ if (!*failed)
+ *failed = ret;
+ } else {
pr_audit("upatch: %s was archived without a record of its load; the archive was removed",
name);
+ }
return 0;
}
@@ -2738,28 +2749,37 @@ static int undo_unmeasured_archive(const char *name, const char *path,
* unload is one the records do not cover either; status reports both
* meanwhile. Every upatch command undoes them before it acts, under the
* register lock a load holds from before its pins to its append, so that
- * the scan never takes a load in flight for one that was stopped.
+ * the scan never takes a load in flight for one that was stopped. A
+ * command whose pass cannot read the records or take out what they do
+ * not cover refuses: a load beside such pins could share their directory
+ * under one name, and its own record would not tell the two apart.
*/
-static void undo_unmeasured(void)
+static int undo_unmeasured(struct op_report *rep, const char *op)
{
struct measured_set *set;
- int ret;
+ int failed = 0, ret;
ret = measured_set_boot(&set);
if (ret) {
- if (ret != -ENOENT)
- pr_warn("upatch: cannot read this boot's records: %s",
- strerror(-ret));
- return;
+ pr_err("upatch %s: cannot read this boot's records: %s; refused, nothing changed",
+ op, strerror(-ret));
+ rep_fail(rep, "tpm_extend", "tpm_error", ret,
+ "cannot read this boot's records");
+ return ret;
}
- ret = measured_unmeasured_pins(set, undo_unmeasured_pin, NULL);
+ ret = measured_unmeasured_pins(set, undo_unmeasured_pin, &failed);
if (ret >= 0)
ret = measured_unmeasured_archives(set, undo_unmeasured_archive,
- NULL);
+ &failed);
measured_set_free(set);
- if (ret < 0)
- pr_warn("upatch: cannot check the pins and archives against this boot's records: %s",
- strerror(-ret));
+ if (ret >= 0 && !failed)
+ return 0;
+ ret = ret < 0 ? ret : failed;
+ pr_err("upatch %s: cannot undo what this boot's records do not cover: %s; refused",
+ op, strerror(-ret));
+ rep_fail(rep, "tpm_extend", "tpm_error", ret,
+ "cannot undo what this boot's records do not cover");
+ return ret;
}
static int cmd_upatch_load(int argc, const char **argv)
@@ -2839,7 +2859,8 @@ static int cmd_upatch_load(int argc, const char **argv)
"cannot take the register lock");
goto done;
}
- undo_unmeasured();
+ if (undo_unmeasured(&rep, "load"))
+ goto done;
/*
* Only a completed load records the payload and only an unload
* removes it. A load over a live upatch rebinds its serializer pins
@@ -3020,7 +3041,8 @@ static int cmd_upatch_unload(int argc, const char **argv)
"cannot take the register lock");
goto done;
}
- undo_unmeasured();
+ if (undo_unmeasured(&rep, "unload"))
+ goto done;
/* A bad signature fails here, before anything is removed. */
if (__upatch_apply(file, true, &rep) != 0) {
pr_err("upatch unload: signature verification failed; refusing");