eBPF-based security platform
attest: parse a <name>=<digest> value in one place
Daniel Borkmann017d04738845
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 8ee3b166..1aaeddb9 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -79,6 +79,12 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- A list is accepted exactly when it is decimal PCRs 0 to 23 apartby single commas, into the mask of those PCRs; a value exactlyafter a PCR to 23 and an equals sign+ * - ``attest-name-digest``+ - ``attest_parse_name_digest()``+ - Lengths 64 to 70+ - Accepted exactly when a name of a byte or more runs to the first+ equals sign and 64 hex digits follow, into that name and their+ value* - ``paths-bpffs-name``- ``upatch_bpffs_name()``, ``upatch_name_from_bpffs()``- Names up to 4 bytes, pin names up to 8diff --git a/builtin-attest.c b/builtin-attest.cindex 8aefd4a6..62eb42b6 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -161,19 +161,15 @@ static int member_add_file(struct members *m, const char *path)static int member_add_spec(struct members *m, const char *spec){struct measured_upatch *e;- const char *eq = strchr(spec, '=');__u8 payload[32];- if (!eq || eq == spec ||- (size_t)(eq - spec) > UPATCH_NAME_MAX ||- parse_hex32("--expect payload digest", eq + 1, payload))- return -EINVAL;if (m->n >= ATTEST_MEMBERS_MAX)return -E2BIG;- e = &m->set[m->n++];- memcpy(e->name, spec, (size_t)(eq - spec));- e->name[eq - spec] = '\0';+ 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;}@@ -211,21 +207,14 @@ static int cert_digest(const char *path, __u8 out[32])static int apply_event(const char *spec, __u8 chain[32]){+ const char *colon = strchr(spec, ':');char name[UPATCH_NAME_MAX + 1];- const char *colon = strchr(spec, ':'), *eq;__u8 digest[32], event[32];- size_t kind_len, name_len;+ size_t kind_len;- eq = colon ? strchr(colon, '=') : NULL;- if (!colon || !eq || eq <= colon + 1 ||- parse_hex32("--event digest", eq + 1, digest))+ if (!colon || attest_parse_name_digest(colon + 1, name, digest))return -EINVAL;kind_len = (size_t)(colon - spec);- name_len = (size_t)(eq - colon - 1);- if (name_len > UPATCH_NAME_MAX)- return -EINVAL;- memcpy(name, colon + 1, name_len);- name[name_len] = '\0';if (kind_len == 4 && !strncmp(spec, "load", 4)) {if (chain_upatch_event("shirudo-upatch", name, digest, event))return -EIO;diff --git a/testing/proofs/cbmc/attest-name-digest.c b/testing/proofs/cbmc/attest-name-digest.cnew file mode 100644index 00000000..7174357f--- /dev/null+++ b/testing/proofs/cbmc/attest-name-digest.c@@ -0,0 +1,59 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * attest_parse_name_digest() of util/attest.c, which reads attest+ * predict's --expect and --event values, on every string of LEN bytes:+ * accepted exactly when a name of one byte or more runs up to the first+ * equals sign and 64 hex digits follow it, into that name and the value+ * of those digits.+ */++#include <stdbool.h>++#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/attest.c"++static int digit(char c)+{+ if (c >= '0' && c <= '9')+ return c - '0';+ if (c >= 'a' && c <= 'f')+ return c - 'a' + 10;+ if (c >= 'A' && c <= 'F')+ return c - 'A' + 10;+ return -1;+}++void harness(void)+{+ char name[UPATCH_NAME_MAX + 1];+ bool hex = true, ok;+ size_t eq = LEN, i;+ __u8 digest[32];+ char *s;+ int ret;++ s = any_bytes(LEN + 1);+ __CPROVER_assume(!s[LEN] && strnlen(s, LEN) == LEN);+ ret = attest_parse_name_digest(s, name, digest);+ for (i = LEN; i > 0; i--)+ if (s[i - 1] == '=')+ eq = i - 1;+ for (i = eq + 1; i < LEN; i++)+ if (digit(s[i]) < 0)+ hex = false;+ ok = eq < LEN && eq >= 1 && LEN - eq - 1 == 64 && hex;+ __CPROVER_assert(!ret == ok,+ "a name up to the first equals sign, then 64 digits");+ if (ret)+ return;+ __CPROVER_assert(strnlen(name, sizeof(name)) == eq &&+ !memcmp(name, s, eq), "the name");+ for (i = 0; i < 32; i++)+ __CPROVER_assert(digest[i] == digit(s[eq + 1 + 2 * i]) * 16 ++ digit(s[eq + 2 + 2 * i]),+ "the value of the digits");+}diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 5c220fdf..14a79fbd 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -8,6 +8,7 @@ string-sanitize --unwind 8 LEN=0-6hex --unwind 12io-lines --unwind 10attest-pcrs --unwind 9+attest-name-digest --unwind 72 LEN=64-70paths-bpffs-name --unwind 18upatch-header --unwind 2 --unwindset memchr.0:257string-number-model --unwind 6diff --git a/testing/unit/attest.c b/testing/unit/attest.cindex 7c045f26..d97a55c4 100644--- a/testing/unit/attest.c+++ b/testing/unit/attest.c@@ -2,6 +2,9 @@/* Copyright (c) 2026 Authors of shirudo */#include <errno.h>+#include <string.h>++#include <shirudo/string.h>#include "attest.h"#include "unit.h"@@ -43,9 +46,39 @@ static void pcr_value_is_a_pcr_and_an_equals(void)EXPECT_EQ(attest_parse_pcr_value("1 5=ab", &pcr, &value), -EINVAL);}+#define D64 "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"++static void name_digest_is_a_name_and_64_digits(void)+{+ char name[UPATCH_NAME_MAX + 1], longest[UPATCH_NAME_MAX + 3 + 64];+ __u8 digest[32];++ ASSERT_EQ(attest_parse_name_digest("fix=" D64, name, digest), 0);+ EXPECT_STREQ(name, "fix");+ EXPECT_EQ(digest[1], 0x11);+ ASSERT_EQ(attest_parse_name_digest("a:b=" D64, name, digest), 0);+ EXPECT_STREQ(name, "a:b");+ EXPECT_EQ(attest_parse_name_digest("=" D64, name, digest), -EINVAL);+ EXPECT_EQ(attest_parse_name_digest("fix" D64, name, digest), -EINVAL);+ EXPECT_EQ(attest_parse_name_digest("fix=" D64 "0", name, digest),+ -EINVAL);+ EXPECT_EQ(attest_parse_name_digest("fix==" D64, name, digest),+ -EINVAL);+ memset(longest, 'n', UPATCH_NAME_MAX);+ ASSERT_TRUE(strscpy(longest + UPATCH_NAME_MAX, "=" D64,+ sizeof(longest) - UPATCH_NAME_MAX) > 0);+ EXPECT_EQ(attest_parse_name_digest(longest, name, digest), 0);+ EXPECT_EQ(strlen(name), UPATCH_NAME_MAX);+ memset(longest, 'n', UPATCH_NAME_MAX + 1);+ ASSERT_TRUE(strscpy(longest + UPATCH_NAME_MAX + 1, "=" D64,+ sizeof(longest) - UPATCH_NAME_MAX - 1) > 0);+ EXPECT_EQ(attest_parse_name_digest(longest, name, digest), -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_CASE(name_digest_is_a_name_and_64_digits),{}};diff --git a/util/attest.c b/util/attest.cindex 5e843122..83616deb 100644--- a/util/attest.c+++ b/util/attest.c@@ -1587,3 +1587,19 @@ int attest_parse_pcr_value(const char *arg, __u64 *pcr, const char **value)*value = arg + len + 1;return 0;}++int attest_parse_name_digest(const char *spec, char name[UPATCH_NAME_MAX + 1],+ __u8 digest[32])+{+ const char *eq = strchr(spec, '=');+ size_t len;++ if (!eq)+ return -EINVAL;+ len = eq - spec;+ if (!len || len > UPATCH_NAME_MAX || hex_decode(eq + 1, digest, 32))+ return -EINVAL;+ memcpy(name, spec, len);+ name[len] = '\0';+ return 0;+}diff --git a/util/attest.h b/util/attest.hindex 0549a5a0..d8423963 100644--- a/util/attest.h+++ b/util/attest.h@@ -112,5 +112,12 @@ int attest_verify(const char *evidence, size_t len,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);+/*+ * "<name>=<digest>", as attest predict's --expect and the part of each+ * --event after its kind take it: a name of 1 to UPATCH_NAME_MAX bytes up+ * to the first equals sign, then 64 hex digits. 0 or -EINVAL.+ */+int attest_parse_name_digest(const char *spec, char name[UPATCH_NAME_MAX + 1],+ __u8 digest[32]);#endif /* SHIRUDO_ATTEST_H */