Skip to content

eBPF-based security platform

attest: parse --pcrs and <pcr>=<value> in util/attest.c

Daniel Borkmann2a484113236f
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 932256ad..8a8a168e 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -491,14 +491,14 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover seventeen of the modules in
-:src:`util/`: ``bpm_oci``, ``bpm_package``, ``bpm_tree``,
+The unit suites in :src:`testing/unit/` cover eighteen of the modules in
+:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_tree``,
``bpm_tree_state``, ``chain``, ``eventlog``, ``format``, ``hex``, ``io``,
``json``, ``kernel_abi``, ``kernel_build_id``, ``kernel_release``,
-``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 the functional tests
-run.
+``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
+the functional tests run.
.. list-table::
:header-rows: 1
diff --git a/builtin-attest.c b/builtin-attest.c
index ad744381..8aefd4a6 100644
--- a/builtin-attest.c
+++ b/builtin-attest.c
@@ -136,38 +136,6 @@ static int parse_hex32(const char *what, const char *hex, __u8 out[32])
return 0;
}
-static int parse_pcrs(const char *list, __u32 *mask)
-{
- const char *p = list;
-
- *mask = 0;
- while (*p) {
- size_t len = digits(p, strlen(p));
- __u64 n;
-
- if (parse_u64(p, len, 23, &n))
- return -EINVAL;
- *mask |= 1u << n;
- p += len;
- if (*p == ',')
- p++;
- else if (*p)
- return -EINVAL;
- }
- return *mask ? 0 : -EINVAL;
-}
-
-/* "<pcr>=<value>": the PCR, below 24, and where its value starts. */
-static int parse_pcr_value(const char *arg, __u64 *pcr, const char **value)
-{
- size_t len = digits(arg, strlen(arg));
-
- if (arg[len] != '=' || parse_u64(arg, len, 23, pcr))
- return -EINVAL;
- *value = arg + len + 1;
- return 0;
-}
-
struct members {
struct measured_upatch *set;
size_t n;
@@ -384,7 +352,7 @@ static int cmd_attest_predict(int argc, const char **argv)
pr_err("attest predict: %s takes a value", a);
goto out;
}
- if (!parse_pcr_value(v, &n, &value))
+ if (!attest_parse_pcr_value(v, &n, &value))
slot = n == CHAIN_PCR ? 0 :
n == CHAIN_PHASE_PCR ? 1 : -1;
if (slot < 0 ||
@@ -741,7 +709,7 @@ static int cmd_attest_verify(int argc, const char **argv)
ret = 2;
goto out;
}
- if (parse_pcr_value(v, &n, &value) ||
+ if (attest_parse_pcr_value(v, &n, &value) ||
parse_hex32("--expect-pcr value", value, x.pcrs[n]))
goto out;
x.pcr_expect_mask |= 1u << n;
@@ -783,7 +751,7 @@ static int cmd_attest_verify(int argc, const char **argv)
ret = 2;
goto out;
}
- if (parse_pcr_value(v, &n, &value) ||
+ if (attest_parse_pcr_value(v, &n, &value) ||
parse_hex32("--expect-bind-pcr value", value,
x.bind_values[n]))
goto out;
@@ -1034,7 +1002,7 @@ static int cmd_attest_evidence(int argc, const char **argv)
pr_err("attest: %s takes a value", a);
return 2;
}
- if (parse_pcrs(v, &mask)) {
+ if (attest_parse_pcrs(v, &mask)) {
pr_err("attest: --pcrs takes a comma separated "
"list of 0..23");
return 2;
diff --git a/testing/unit/attest.c b/testing/unit/attest.c
new file mode 100644
index 00000000..fe4441c2
--- /dev/null
+++ b/testing/unit/attest.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+
+#include "attest.h"
+#include "unit.h"
+
+static void pcrs_are_a_list_up_to_23(void)
+{
+ __u32 mask;
+
+ ASSERT_EQ(attest_parse_pcrs("0", &mask), 0);
+ EXPECT_EQ(mask, 1U);
+ ASSERT_EQ(attest_parse_pcrs("0,9,23", &mask), 0);
+ EXPECT_EQ(mask, 1U | 1U << 9 | 1U << 23);
+ ASSERT_EQ(attest_parse_pcrs("9,9", &mask), 0);
+ EXPECT_EQ(mask, 1U << 9);
+ EXPECT_EQ(attest_parse_pcrs("", &mask), -EINVAL);
+ EXPECT_EQ(attest_parse_pcrs("24", &mask), -EINVAL);
+ EXPECT_EQ(attest_parse_pcrs("1,,2", &mask), -EINVAL);
+ EXPECT_EQ(attest_parse_pcrs(",1", &mask), -EINVAL);
+ EXPECT_EQ(attest_parse_pcrs("1 ", &mask), -EINVAL);
+ EXPECT_EQ(attest_parse_pcrs("+1", &mask), -EINVAL);
+ EXPECT_EQ(attest_parse_pcrs("0x1", &mask), -EINVAL);
+}
+
+static void pcr_value_is_a_pcr_and_an_equals(void)
+{
+ const char *value;
+ __u64 pcr;
+
+ ASSERT_EQ(attest_parse_pcr_value("15=ab", &pcr, &value), 0);
+ EXPECT_EQ(pcr, 15);
+ EXPECT_STREQ(value, "ab");
+ ASSERT_EQ(attest_parse_pcr_value("0=", &pcr, &value), 0);
+ EXPECT_STREQ(value, "");
+ EXPECT_EQ(attest_parse_pcr_value("24=ab", &pcr, &value), -EINVAL);
+ EXPECT_EQ(attest_parse_pcr_value("=ab", &pcr, &value), -EINVAL);
+ EXPECT_EQ(attest_parse_pcr_value("15", &pcr, &value), -EINVAL);
+ EXPECT_EQ(attest_parse_pcr_value("1 5=ab", &pcr, &value), -EINVAL);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(pcrs_are_a_list_up_to_23),
+ UNIT_CASE(pcr_value_is_a_pcr_and_an_equals),
+ {}
+};
+
+UNIT_SUITE(attest, cases);
diff --git a/util/attest.c b/util/attest.c
index f3fa2d5c..79acc0cb 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -1556,3 +1556,34 @@ out:
}
return ret;
}
+
+int attest_parse_pcrs(const char *list, __u32 *mask)
+{
+ const char *p = list;
+
+ *mask = 0;
+ while (*p) {
+ size_t len = digits(p, strlen(p));
+ __u64 n;
+
+ if (parse_u64(p, len, 23, &n))
+ return -EINVAL;
+ *mask |= 1u << n;
+ p += len;
+ if (*p == ',')
+ p++;
+ else if (*p)
+ return -EINVAL;
+ }
+ return *mask ? 0 : -EINVAL;
+}
+
+int attest_parse_pcr_value(const char *arg, __u64 *pcr, const char **value)
+{
+ size_t len = digits(arg, strlen(arg));
+
+ if (arg[len] != '=' || parse_u64(arg, len, 23, pcr))
+ return -EINVAL;
+ *value = arg + len + 1;
+ return 0;
+}
diff --git a/util/attest.h b/util/attest.h
index ba48c796..0549a5a0 100644
--- a/util/attest.h
+++ b/util/attest.h
@@ -108,4 +108,9 @@ int attest_verify(const char *evidence, size_t len,
const struct attest_expect *expect,
char *report, size_t report_cap);
+/* --pcrs: PCRs 0 to 23 apart by commas, into a mask. 0 or -EINVAL. */
+int attest_parse_pcrs(const char *list, __u32 *mask);
+/* "<pcr>=<value>": the PCR, below 24, and where its value starts. */
+int attest_parse_pcr_value(const char *arg, __u64 *pcr, const char **value);
+
#endif /* SHIRUDO_ATTEST_H */