Skip to content

eBPF-based security platform

attest: list an expected upatch once, by a name an upatch can have

Daniel Borkmannfddb4477c727
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 23723dfc..08f9b2c9 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -494,11 +494,11 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover nineteen of the modules in
+The unit suites in :src:`testing/unit/` cover twenty 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``, ``paths``, ``string``, ``upatch`` and
+``kernel_release``, ``measured``, ``paths``, ``string``, ``upatch`` and
``upatch_decode``, 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
diff --git a/Documentation/reference/cli/attest-predict.rst b/Documentation/reference/cli/attest-predict.rst
index 6c09fda8..f2469870 100644
--- a/Documentation/reference/cli/attest-predict.rst
+++ b/Documentation/reference/cli/attest-predict.rst
@@ -111,10 +111,13 @@ Options
16.
* - ``--upatch``
- ``<file>``
- - An artifact archived on the target at boot. Repeatable.
+ - An artifact archived on the target at boot. Repeatable, each
+ upatch name once across ``--upatch`` and ``--expect``.
* - ``--expect``
- ``<name>=<digest>``
- - An archived upatch by name and payload digest. Repeatable.
+ - An archived upatch by name and payload digest. Repeatable. The
+ name is one an upatch can have, ``[A-Za-z0-9._-]`` not starting
+ with a dot, and comes once.
* - ``--tree-root``
- ``<digest>``, 64 hex characters
- The upatch tree projection root at boot. Default: all zero.
diff --git a/Documentation/reference/cli/attest-verify.rst b/Documentation/reference/cli/attest-verify.rst
index b782d3ee..e2b26328 100644
--- a/Documentation/reference/cli/attest-verify.rst
+++ b/Documentation/reference/cli/attest-verify.rst
@@ -98,12 +98,14 @@ Options
- A certificate the target enrolls. Repeat for each, up to 16.
* - ``--upatch``
- ``<file>``
- - An artifact archived on the target at boot. Repeatable. Needs
+ - An artifact archived on the target at boot. Repeatable, each
+ upatch name once across ``--upatch`` and ``--expect``. Needs
``--cert``.
* - ``--expect``
- ``<name>=<digest>``
- - An archived upatch by name and payload digest. Repeatable. Needs
- ``--cert``.
+ - An archived upatch by name and payload digest. Repeatable. The
+ name is one an upatch can have, ``[A-Za-z0-9._-]`` not starting
+ with a dot, and comes once. Needs ``--cert``.
* - ``--tree-root``
- ``<digest>``, 64 hex characters
- The upatch tree projection root at boot. Needs ``--cert``.
diff --git a/builtin-attest.c b/builtin-attest.c
index 7a44be12..ecd57081 100644
--- a/builtin-attest.c
+++ b/builtin-attest.c
@@ -143,34 +143,45 @@ struct members {
static int member_add_file(struct members *m, const char *path)
{
+ struct measured_upatch e;
struct upatch u;
int ret;
- if (m->n >= ATTEST_MEMBERS_MAX)
- return -E2BIG;
ret = upatch_open(path, &u);
if (ret) {
pr_err("attest: open %s: %s", path, strerror(-ret));
return ret;
}
- measured_upatch_of(&m->set[m->n++], &u);
+ measured_upatch_of(&e, &u);
upatch_close(&u);
- return 0;
+ ret = measured_set_add(m->set, &m->n, ATTEST_MEMBERS_MAX, e.name,
+ e.payload_hex);
+ if (ret)
+ pr_err("attest: --upatch %s: %s", path,
+ ret == -EEXIST ? "a second upatch of that name" :
+ strerror(-ret));
+ return ret;
}
static int member_add_spec(struct members *m, const char *spec)
{
- struct measured_upatch *e;
- __u8 payload[32];
+ char name[UPATCH_NAME_MAX + 1];
+ char hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ int ret;
- if (m->n >= ATTEST_MEMBERS_MAX)
- return -E2BIG;
- e = &m->set[m->n];
- if (attest_parse_name_digest(spec, e->name, payload))
- return -EINVAL;
- hex_encode(payload, 32, e->payload_hex);
- m->n++;
- return 0;
+ ret = attest_parse_name_digest(spec, name, payload);
+ if (!ret) {
+ hex_encode(payload, sizeof(payload), hex);
+ ret = measured_set_add(m->set, &m->n, ATTEST_MEMBERS_MAX, name,
+ hex);
+ }
+ if (ret)
+ pr_err("attest: --expect '%s': %s", spec,
+ ret == -EEXIST ? "a second upatch of that name" :
+ ret == -EINVAL ? "not an upatch name and a digest" :
+ strerror(-ret));
+ return ret;
}
/* What the init event counts of a certificate: the SHA-256 of its DER. */
@@ -286,10 +297,8 @@ static int cmd_attest_predict(int argc, const char **argv)
pr_err("attest predict: %s takes a value", a);
goto out;
}
- if (member_add_spec(&m, v)) {
- pr_err("attest: bad --expect '%s'", v);
+ if (member_add_spec(&m, v))
goto out;
- }
} else if (opt_arg("--tree-root", a, argc, argv, &i, &v, &r)) {
if (r == OPT_MISSING) {
pr_err("attest predict: %s takes a value", a);
@@ -604,11 +613,8 @@ static int cmd_attest_verify(int argc, const char **argv)
ret = 2;
goto out;
}
- if (member_add_spec(&m, v)) {
- pr_err("attest verify: bad --expect '%s'",
- v);
+ if (member_add_spec(&m, v))
goto out;
- }
} else if (opt_arg("--tree-root", a, argc, argv, &i, &v, &r)) {
if (r == OPT_MISSING) {
pr_err("attest verify: %s takes a value", a);
diff --git a/testing/unit/measured.c b/testing/unit/measured.c
new file mode 100644
index 00000000..3fc40a0b
--- /dev/null
+++ b/testing/unit/measured.c
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+
+#include "chain.h"
+#include "measured.h"
+#include "unit.h"
+
+#define PAYLOAD "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"
+
+/* An init event lists each upatch once, by a name an upatch can have. */
+static void set_takes_each_upatch_once(void)
+{
+ struct measured_upatch set[2];
+ size_t n = 0;
+
+ EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD), 0);
+ EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD), -EEXIST);
+ EXPECT_EQ(measured_set_add(set, &n, 2, "a\n" PAYLOAD " b", PAYLOAD),
+ -EINVAL);
+ EXPECT_EQ(measured_set_add(set, &n, 2, "b", "00"), -EINVAL);
+ EXPECT_EQ(measured_set_add(set, &n, 2, "b", PAYLOAD), 0);
+ EXPECT_EQ(measured_set_add(set, &n, 2, "c", PAYLOAD), -E2BIG);
+ ASSERT_EQ(n, 2);
+ EXPECT_STREQ(set[0].name, "a");
+ EXPECT_STREQ(set[1].payload_hex, PAYLOAD);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(set_takes_each_upatch_once),
+ {}
+};
+
+UNIT_SUITE(measured, cases);
diff --git a/util/measured.c b/util/measured.c
index 938e7adb..3993df37 100644
--- a/util/measured.c
+++ b/util/measured.c
@@ -108,6 +108,28 @@ void measured_upatch_of(struct measured_upatch *e, const struct upatch *u)
e->payload_hex);
}
+int measured_set_add(struct measured_upatch *set, size_t *n, size_t max,
+ const char *name, const char *payload_hex)
+{
+ struct measured_upatch *e;
+ size_t i;
+
+ if (!upatch_member_valid(name, payload_hex))
+ return -EINVAL;
+ for (i = 0; i < *n; i++) {
+ if (!strcmp(set[i].name, name))
+ return -EEXIST;
+ }
+ if (*n >= max)
+ return -E2BIG;
+ e = &set[*n];
+ if (strscpy(e->name, name, sizeof(e->name)) < 0 ||
+ strscpy(e->payload_hex, payload_hex, sizeof(e->payload_hex)) < 0)
+ return -E2BIG;
+ (*n)++;
+ return 0;
+}
+
bool measured_holds(const struct measured_set *s, const char *name,
const char *payload_hex)
{
diff --git a/util/measured.h b/util/measured.h
index d223aad5..4499df78 100644
--- a/util/measured.h
+++ b/util/measured.h
@@ -5,6 +5,7 @@
#define SHIRUDO_MEASURED_H
#include <stdbool.h>
+#include <stddef.h>
/*
* The set of upatches the eventlog records as loaded, each with the
@@ -28,6 +29,15 @@ struct upatch;
/* The name and payload an init record lists for the upatch u. */
void measured_upatch_of(struct measured_upatch *e, const struct upatch *u);
+/*
+ * A member added to the *n of set, which holds at most max: 0, -EINVAL
+ * for one upatch_member_valid() refuses, -EEXIST for a name the set
+ * holds, for an init event lists each upatch once and sorts by name, or
+ * -E2BIG when the set is full.
+ */
+int measured_set_add(struct measured_upatch *set, size_t *n, size_t max,
+ const char *name, const char *payload_hex);
+
/*
* The upatches pinned in the private bpffs, or archived with the seal
* label, that the set does not hold with the payload their pin or