Skip to content

eBPF-based security platform

attest: refuse a --pcrs list that ends in a comma

Daniel Borkmann0c8124353353
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 1a635852..8ee3b166 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -73,6 +73,12 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- ``lines_len()``
- Up to 8 bytes
- Ends at a newline or is 0, with no newline behind it
+ * - ``attest-pcrs``
+ - ``attest_parse_pcrs()``, ``attest_parse_pcr_value()``
+ - Up to 6 bytes
+ - A list is accepted exactly when it is decimal PCRs 0 to 23 apart
+ by single commas, into the mask of those PCRs; a value exactly
+ after a PCR to 23 and an equals sign
* - ``paths-bpffs-name``
- ``upatch_bpffs_name()``, ``upatch_name_from_bpffs()``
- Names up to 4 bytes, pin names up to 8
diff --git a/testing/proofs/cbmc/attest-pcrs.c b/testing/proofs/cbmc/attest-pcrs.c
new file mode 100644
index 00000000..591a1488
--- /dev/null
+++ b/testing/proofs/cbmc/attest-pcrs.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The --pcrs and <pcr>=<value> parsers of util/attest.c on every string
+ * of up to LEN_MAX bytes: a list is accepted exactly when it is decimal
+ * PCRs 0 to 23 apart by single commas, into the mask of those PCRs, and a
+ * value exactly after a PCR and an equals sign.
+ */
+
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/attest.c"
+
+#define LEN_MAX 6
+
+static char *any_string(size_t len)
+{
+ char *s = any_bytes(len + 1);
+
+ __CPROVER_assume(!s[len] && strnlen(s, len) == len);
+ return s;
+}
+
+static void pcr_list(void)
+{
+ size_t len, i, digits_seen = 0;
+ __u32 mask, want = 0;
+ bool ok = true;
+ __u64 v = 0;
+ char *s;
+ int ret;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ s = any_string(len);
+ ret = attest_parse_pcrs(s, &mask);
+ for (i = 0; i <= len; i++) {
+ char c = i < len ? s[i] : ',';
+
+ if (c == ',') {
+ if (!digits_seen || v > 23)
+ ok = false;
+ else
+ want |= 1U << v;
+ digits_seen = 0;
+ v = 0;
+ } else if (c >= '0' && c <= '9') {
+ digits_seen++;
+ if (v <= 23)
+ v = v * 10 + (__u64)(c - '0');
+ } else {
+ ok = false;
+ }
+ }
+ __CPROVER_assert(!ret == ok, "PCRs 0 to 23 apart by single commas");
+ __CPROVER_assert(ret || mask == want, "the mask of those PCRs");
+}
+
+static void pcr_value(void)
+{
+ const char *value;
+ size_t len, n = 0;
+ __u64 pcr, v = 0;
+ char *s;
+ int ret;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ s = any_string(len);
+ ret = attest_parse_pcr_value(s, &pcr, &value);
+ while (n < len && s[n] >= '0' && s[n] <= '9') {
+ if (v <= 23)
+ v = v * 10 + (__u64)(s[n] - '0');
+ n++;
+ }
+ __CPROVER_assert(!ret == (n && n < len && s[n] == '=' && v <= 23),
+ "a PCR to 23, then an equals sign");
+ __CPROVER_assert(ret || (pcr == v && value == s + n + 1),
+ "the PCR, and the value after the sign");
+}
+
+void harness(void)
+{
+ pcr_list();
+ pcr_value();
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 060e4a1b..5c220fdf 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -7,6 +7,7 @@ string-suffix --unwind 8
string-sanitize --unwind 8 LEN=0-6
hex --unwind 12
io-lines --unwind 10
+attest-pcrs --unwind 9
paths-bpffs-name --unwind 18
upatch-header --unwind 2 --unwindset memchr.0:257
string-number-model --unwind 6
diff --git a/testing/unit/attest.c b/testing/unit/attest.c
index fe4441c2..7c045f26 100644
--- a/testing/unit/attest.c
+++ b/testing/unit/attest.c
@@ -20,6 +20,8 @@ static void pcrs_are_a_list_up_to_23(void)
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,2,", &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);
diff --git a/util/attest.c b/util/attest.c
index 79acc0cb..5e843122 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -1570,7 +1570,7 @@ int attest_parse_pcrs(const char *list, __u32 *mask)
return -EINVAL;
*mask |= 1u << n;
p += len;
- if (*p == ',')
+ if (*p == ',' && p[1])
p++;
else if (*p)
return -EINVAL;