eBPF-based security platform
hex: decode hex of up to a given length in one place
Daniel Borkmannb7822cf645cb
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex 1ffa211f..7ba9202c 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -106,7 +106,7 @@ extra check left to the caller, and move one found in a builtin into| Memory | `xmalloc()`, `xcalloc()`, `xreallocarray()`, `xstrndup()`, `xasprintf()`, `xfree(p, size)` (`util/malloc.h`) || Secrets | `memzero_explicit()`, `memneq_explicit()` || Files | `read_file()`, `read_fd()`, `write_complete()`, `append_line()`, `lines_len()`, `open_beneath()` (`util/io.h`) |-| Bytes on the wire | `get_le32()`, `put_be32()`, `le32_to_cpu()` and kin (`<shirudo/byteorder.h>`), `hex_encode()`, `hex_decode()` |+| Bytes on the wire | `get_le32()`, `put_be32()`, `le32_to_cpu()` and kin (`<shirudo/byteorder.h>`), `hex_encode()`, `hex_decode()`, `hex_decode_max()`, `sha256_digest_decode()` (`util/hex.h`) || JSON | `util/json.h`: `json_parse()` with a schema, `json_walk()`, `struct json_emit` || Messages | `pr_err()`, `pr_warn()`, `pr_info()`, `pr_audit()` (`util/log.h`) |diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 195469a6..eef3e766 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -58,10 +58,12 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Terminates what it writes; the whole string and its length, orwhat fits and ``-E2BIG``* - ``hex``- - ``hex_encode()``, ``hex_decode()``+ - ``hex_encode()``, ``hex_decode()``, ``hex_decode_max()``- Up to 4 bytes, strings up to 9 characters- Lowercase hex that decodes back to the input; a decode takes- exactly ``2 * n`` hex digits of either case and yields their value+ exactly ``2 * n`` hex digits of either case and yields their+ value, and ``hex_decode_max()`` any even count up to ``2 * cap``,+ reading no further than the digit after them* - ``io-lines``- ``lines_len()``- Up to 8 bytesdiff --git a/builtin-attest.c b/builtin-attest.cindex a4bb28e1..ff21f383 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -126,19 +126,9 @@ static const struct help attest_help = {.n_sections = ARRAY_SIZE(attest_help_sections),};-static int parse_hex(const char *hex, __u8 *out, size_t cap, size_t *len)-{- size_t n = strlen(hex);-- if (n == 0 || (n & 1) || n / 2 > cap)- return -EINVAL;- *len = n / 2;- return hex_decode(hex, out, n / 2);-}-static int parse_hex32(const char *what, const char *hex, __u8 out[32]){- if (strlen(hex) != 64 || hex_decode(hex, out, 32)) {+ if (hex_decode(hex, out, 32)) {pr_err("attest: %s must be 64 hex characters", what);return -EINVAL;}@@ -648,8 +638,8 @@ static int cmd_attest_verify(int argc, const char **argv)ret = 2;goto out;}- if (parse_hex(v, nonce, sizeof(nonce),- &nonce_len)) {+ if (hex_decode_max(v, nonce, sizeof(nonce),+ &nonce_len) || !nonce_len) {pr_err("attest verify: --nonce must be hex of ""up to %d bytes",ATTEST_NONCE_MAX);@@ -657,15 +647,12 @@ static int cmd_attest_verify(int argc, const char **argv)}have_nonce = true;} else if (opt_arg("--ak-name", a, argc, argv, &i, &v, &r)) {- size_t n;-if (r == OPT_MISSING) {pr_err("attest verify: %s takes a value", a);ret = 2;goto out;}- if (parse_hex(v, x.ak_name, sizeof(x.ak_name),- &n) || n != TPM_NAME_LEN) {+ if (hex_decode(v, x.ak_name, TPM_NAME_LEN)) {pr_err("attest verify: --ak-name must be %d ""hex characters",2 * TPM_NAME_LEN);@@ -779,15 +766,12 @@ static int cmd_attest_verify(int argc, const char **argv)goto out;x.have_credential = true;} else if (opt_arg("--ek-name", a, argc, argv, &i, &v, &r)) {- size_t n;-if (r == OPT_MISSING) {pr_err("attest verify: %s takes a value", a);ret = 2;goto out;}- if (parse_hex(v, x.ek_name, sizeof(x.ek_name),- &n) || n != TPM_NAME_LEN) {+ if (hex_decode(v, x.ek_name, TPM_NAME_LEN)) {pr_err("attest verify: --ek-name must be %d ""hex characters",2 * TPM_NAME_LEN);@@ -891,7 +875,7 @@ static int cmd_attest_challenge(int argc, const char **argv)const char *evidence_path = NULL, *out_path = NULL;char *evidence = NULL, *doc = NULL, *report;char hex[2 * TPM_CREDENTIAL_LEN + 1];- size_t evidence_len = 0, len = 0, cap = 0, i, n;+ size_t evidence_len = 0, len = 0, cap = 0, i;int err, ret = 2;report = xmalloc(ATTEST_REPORT_MAX);@@ -925,8 +909,7 @@ static int cmd_attest_challenge(int argc, const char **argv)ret = 2;goto out;}- if (parse_hex(v, ak_name, sizeof(ak_name), &n) ||- n != TPM_NAME_LEN) {+ if (hex_decode(v, ak_name, TPM_NAME_LEN)) {pr_err("attest challenge: --ak-name must be %d ""hex characters", 2 * TPM_NAME_LEN);goto out;@@ -938,8 +921,7 @@ static int cmd_attest_challenge(int argc, const char **argv)ret = 2;goto out;}- if (parse_hex(v, ek_name, sizeof(ek_name), &n) ||- n != TPM_NAME_LEN) {+ if (hex_decode(v, ek_name, TPM_NAME_LEN)) {pr_err("attest challenge: --ek-name must be %d ""hex characters", 2 * TPM_NAME_LEN);goto out;@@ -1045,8 +1027,8 @@ static int cmd_attest_evidence(int argc, const char **argv)pr_err("attest: %s takes a value", a);return 2;}- if (parse_hex(v, nonce, sizeof(nonce),- &nonce_len)) {+ if (hex_decode_max(v, nonce, sizeof(nonce),+ &nonce_len) || !nonce_len) {pr_err("attest: --nonce must be hex of up to ""%d bytes", ATTEST_NONCE_MAX);return 2;diff --git a/testing/proofs/cbmc/hex.c b/testing/proofs/cbmc/hex.cindex 244b1b43..bbe2226d 100644--- a/testing/proofs/cbmc/hex.c+++ b/testing/proofs/cbmc/hex.c@@ -3,8 +3,10 @@* Copyright (c) 2026 Authors of shirudo** util/hex.c for every input of up to LEN_MAX bytes: an encoding is- * lowercase hex that decodes back to its input, and a decode takes- * exactly 2 * n hex digits of either case and yields their value.+ * lowercase hex that decodes back to its input, a decode takes exactly+ * 2 * n hex digits of either case and yields their value, and one of at+ * most cap bytes any even count of them up to 2 * cap, reading no+ * further than the digit after those.*/#include <errno.h>@@ -71,8 +73,50 @@ static void decoded(void)"the value of each pair");}+static void decoded_max(void)+{+ size_t len, cap, n, i;+ bool hex = true;+ char *s;+ __u8 *out;+ int ret;++ __CPROVER_assume(len <= 2 * LEN_MAX + 1 && cap <= LEN_MAX);+ s = any_bytes(len + 1);+ __CPROVER_assume(!s[len] && strnlen(s, len) == len);+ out = any_bytes(cap);+ ret = hex_decode_max(s, out, cap, &n);+ for (i = 0; i < len; i++)+ if (digit(s[i]) < 0)+ hex = false;+ __CPROVER_assert(!ret || ret == -EINVAL, "0 or -EINVAL");+ __CPROVER_assert(!ret == (len % 2 == 0 && len / 2 <= cap && hex),+ "an even count of hex digits, at most 2 * cap");+ __CPROVER_assert(ret || n == len / 2, "half as many bytes");+ for (i = 0; !ret && i < n; i++)+ __CPROVER_assert(out[i] == digit(s[2 * i]) * 16 ++ digit(s[2 * i + 1]),+ "the value of each pair");+}++static void decoded_max_unterminated(void)+{+ size_t cap, n;+ __u8 *out;+ char *s;++ __CPROVER_assume(cap <= LEN_MAX);+ s = any_bytes(2 * cap + 1);+ out = any_bytes(cap);+ __CPROVER_assume(!memchr(s, '\0', 2 * cap + 1));+ __CPROVER_assert(hex_decode_max(s, out, cap, &n) == -EINVAL,+ "too long, read no further than 2 * cap + 1");+}+void harness(void){encoded();decoded();+ decoded_max();+ decoded_max_unterminated();}diff --git a/testing/unit/hex.c b/testing/unit/hex.cindex 8858eff1..e92f5f7e 100644--- a/testing/unit/hex.c+++ b/testing/unit/hex.c@@ -42,6 +42,26 @@ static void decode_refuses_non_hex(void)EXPECT_EQ(hex_decode("zz", out, 1), -EINVAL);}+static void decode_max_takes_an_even_count_up_to_cap(void)+{+ __u8 out[3] = {};+ size_t len = 9;++ EXPECT_EQ(hex_decode_max("", out, sizeof(out), &len), 0);+ EXPECT_EQ(len, 0);+ EXPECT_EQ(hex_decode_max("aB", out, sizeof(out), &len), 0);+ EXPECT_EQ(len, 1);+ EXPECT_EQ(out[0], 0xab);+ EXPECT_EQ(hex_decode_max("00ff10", out, sizeof(out), &len), 0);+ EXPECT_EQ(len, 3);+ EXPECT_EQ(out[2], 0x10);+ len = 9;+ EXPECT_EQ(hex_decode_max("abc", out, sizeof(out), &len), -EINVAL);+ EXPECT_EQ(hex_decode_max("00ff1022", out, sizeof(out), &len), -EINVAL);+ EXPECT_EQ(hex_decode_max("0g", out, sizeof(out), &len), -EINVAL);+ EXPECT_EQ(len, 9);+}+#define HEX64 "00112233445566778899aabbccddeeffFFEEDDCCBBAA99887766554433221100"#define NOTHEX64 "g0112233445566778899aabbccddeeffFFEEDDCCBBAA99887766554433221100"@@ -68,6 +88,7 @@ static const struct unit_case cases[] = {UNIT_CASE(decode_takes_either_case),UNIT_CASE(decode_wants_the_exact_length),UNIT_CASE(decode_refuses_non_hex),+ UNIT_CASE(decode_max_takes_an_even_count_up_to_cap),UNIT_CASE(sha256_digest_is_prefix_and_64_digits),{}};diff --git a/util/attest.c b/util/attest.cindex 864041c5..f3fa2d5c 100644--- a/util/attest.c+++ b/util/attest.c@@ -689,16 +689,6 @@ static void note(struct reporter *r, const char *fmt, ...)}}-static int hex_bytes(const char *hex, __u8 *out, size_t cap, size_t *len)-{- size_t n = strlen(hex);-- if (n & 1 || n / 2 > cap)- return -EINVAL;- *len = n / 2;- return hex_decode(hex, out, n / 2);-}-static int check_signed(struct reporter *rep, const char *what,const char *attest_hex, const char *sig_hex,const struct tpm_ecc_public *ak,@@ -712,8 +702,8 @@ static int check_signed(struct reporter *rep, const char *what,size_t sig_len;int ret;- if (hex_bytes(attest_hex, attest, attest_cap, attest_len) ||- hex_bytes(sig_hex, sig, sizeof(sig), &sig_len))+ if (hex_decode_max(attest_hex, attest, attest_cap, attest_len) ||+ hex_decode_max(sig_hex, sig, sizeof(sig), &sig_len))return -EINVAL;if (tpm_attest_parse(attest, *attest_len, parsed)) {say(rep, false, "%s: attestation structure unparseable", what);@@ -755,8 +745,8 @@ int attest_verify(const char *evidence, size_t len,__u8 expected_name[TPM_NAME_LEN], pcr_expected[32];__u8 anchor_digest[32] = {}, cred[TPM_CREDENTIAL_LEN];struct parse *p;- size_t ak_public_len, ak_name_len, nonce_len, attest_len;- size_t reg_name_len, reg_value_len, cred_len = 0, i, runtime = 0;+ size_t ak_public_len, nonce_len, attest_len;+ size_t cred_len = 0, i, runtime = 0;__u64 reg_index;__u32 bit;bool have_ek = false, have_cred = false, replayed = true;@@ -925,7 +915,7 @@ int attest_verify(const char *evidence, size_t len,goto out;/* Freshness: the document must answer this request. */- if (hex_bytes(p->nonce_hex, nonce, sizeof(nonce), &nonce_len)) {+ if (hex_decode_max(p->nonce_hex, nonce, sizeof(nonce), &nonce_len)) {ret = -EINVAL;goto out;}@@ -934,10 +924,9 @@ int attest_verify(const char *evidence, size_t len,!memneq_explicit(nonce, expect->nonce, nonce_len)),"nonce matches the request");- if (hex_bytes(p->ak_public_hex, ak_public, sizeof(ak_public),- &ak_public_len) ||- hex_bytes(p->ak_name_hex, ak_name, sizeof(ak_name), &ak_name_len) ||- ak_name_len != TPM_NAME_LEN) {+ if (hex_decode_max(p->ak_public_hex, ak_public, sizeof(ak_public),+ &ak_public_len) ||+ hex_decode(p->ak_name_hex, ak_name, TPM_NAME_LEN)) {ret = -EINVAL;goto out;}@@ -969,13 +958,12 @@ int attest_verify(const char *evidence, size_t len,__u8 ek_public[TPM_PUBLIC_MAX];__u8 ek_name[TPM_NAME_LEN], computed[TPM_NAME_LEN];struct tpm_public ek_pub;- size_t ek_public_len, ek_name_len;+ size_t ek_public_len;bool parsed;- if (hex_bytes(p->ek_public_hex, ek_public, sizeof(ek_public),- &ek_public_len) ||- hex_bytes(p->ek_name_hex, ek_name, sizeof(ek_name),- &ek_name_len) || ek_name_len != TPM_NAME_LEN) {+ if (hex_decode_max(p->ek_public_hex, ek_public,+ sizeof(ek_public), &ek_public_len) ||+ hex_decode(p->ek_name_hex, ek_name, TPM_NAME_LEN)) {ret = -EINVAL;goto out;}@@ -995,15 +983,14 @@ int attest_verify(const char *evidence, size_t len,}if (expect->have_ek_name) {__u8 ek_name[TPM_NAME_LEN];- size_t n;say(&rep, have_ek &&- !hex_bytes(p->ek_name_hex, ek_name, sizeof(ek_name), &n) &&- n == TPM_NAME_LEN &&+ !hex_decode(p->ek_name_hex, ek_name, TPM_NAME_LEN) &&!memneq_explicit(ek_name, expect->ek_name, TPM_NAME_LEN),"endorsement key: the pinned key");}- if (have_cred && hex_bytes(p->cred_hex, cred, sizeof(cred), &cred_len)) {+ if (have_cred &&+ hex_decode_max(p->cred_hex, cred, sizeof(cred), &cred_len)) {ret = -EINVAL;goto out;}@@ -1064,11 +1051,8 @@ int attest_verify(const char *evidence, size_t len,if (ret == -EINVAL)goto out;crc = ret;- if (hex_bytes(p->reg_name_hex, reg_name, sizeof(reg_name),- ®_name_len) ||- reg_name_len != TPM_NAME_LEN ||- hex_bytes(p->reg_value_hex, reg_value, sizeof(reg_value),- ®_value_len) || reg_value_len != 32 ||+ if (hex_decode(p->reg_name_hex, reg_name, TPM_NAME_LEN) ||+ hex_decode(p->reg_value_hex, reg_value, 32) ||strncmp(p->reg_index, "0x", 2) ||parse_x64(p->reg_index + 2, strlen(p->reg_index + 2), 0xffffffffULL,®_index) ||@@ -1307,7 +1291,7 @@ int attest_challenge_read(const char *path,{ "secret", JSON_T_STRING, secret_hex,sizeof(secret_hex), 0, NULL, 0 },};- size_t len, ak_len;+ size_t len;char *doc;int ret;@@ -1329,12 +1313,12 @@ int attest_challenge_read(const char *path,out->ek_rsa = true;else if (strcmp(type, "ecc"))return -EINVAL;- if (hex_bytes(ak_hex, out->ak_name, sizeof(out->ak_name), &ak_len) ||- ak_len != TPM_NAME_LEN ||- hex_bytes(blob_hex, out->blob, sizeof(out->blob), &out->blob_len) ||+ if (hex_decode(ak_hex, out->ak_name, TPM_NAME_LEN) ||+ hex_decode_max(blob_hex, out->blob, sizeof(out->blob),+ &out->blob_len) ||out->blob_len != TPM_ID_OBJECT_LEN ||- hex_bytes(secret_hex, out->secret, sizeof(out->secret),- &out->secret_len) || !out->secret_len)+ hex_decode_max(secret_hex, out->secret, sizeof(out->secret),+ &out->secret_len) || !out->secret_len)return -EINVAL;return 0;}@@ -1396,7 +1380,7 @@ int attest_challenge_make(const char *evidence, size_t len,{ "ek_certificate", JSON_T_STRING, cert_b64,EK_CERT_B64_MAX, 0, NULL, 0 },};- size_t ak_len, ek_public_len, ek_name_len, secret_len = 0, der_len = 0;+ size_t ek_public_len, secret_len = 0, der_len = 0;const char *reason = "";int ret = -EINVAL;bool rsa;@@ -1416,12 +1400,10 @@ int attest_challenge_make(const char *evidence, size_t len,if (json_parse(evidence, len, top, ARRAY_SIZE(top)) ||!ak_sub[0].seen || !ek_sub[0].seen || !ek_sub[1].seen ||!ek_sub[2].seen ||- hex_bytes(ak_hex, ak_name, sizeof(ak_name), &ak_len) ||- ak_len != TPM_NAME_LEN ||- hex_bytes(ek_public_hex, ek_public, sizeof(ek_public),- &ek_public_len) ||- hex_bytes(ek_name_hex, ek_name, sizeof(ek_name), &ek_name_len) ||- ek_name_len != TPM_NAME_LEN)+ hex_decode(ak_hex, ak_name, TPM_NAME_LEN) ||+ hex_decode_max(ek_public_hex, ek_public, sizeof(ek_public),+ &ek_public_len) ||+ hex_decode(ek_name_hex, ek_name, TPM_NAME_LEN))goto out;if (!strcmp(ek_type, "rsa"))rsa = true;diff --git a/util/config.c b/util/config.cindex c22991aa..8452745b 100644--- a/util/config.c+++ b/util/config.c@@ -110,30 +110,17 @@ static void set_owner_auth(struct config *cfg, const char *val)size_t l;if (strncmp(val, "hex:", 4) == 0) {- char auth[sizeof(cfg->tpm_owner_auth)];- const char *h = val + 4;- size_t hl = strlen(h), i;-- if (hl % 2 || hl / 2 > sizeof(cfg->tpm_owner_auth)) {- pr_warn("config: tpm.owner_auth: bad hex length "- "(ignored)");- return;- }- /* An ignored value must leave the one set before intact. */- for (i = 0; i < hl / 2; i++) {- int hi = hex_nibble(h[2 * i]);- int lo = hex_nibble(h[2 * i + 1]);+ __u8 auth[sizeof(cfg->tpm_owner_auth)];+ size_t n;- if (hi < 0 || lo < 0) {- pr_warn("config: tpm.owner_auth: bad hex "- "(ignored)");- memzero_explicit(auth, sizeof(auth));- return;- }- auth[i] = (char)((hi << 4) | lo);+ /* An ignored value must leave the one set before intact. */+ if (!hex_decode_max(val + 4, auth, sizeof(auth), &n)) {+ memcpy(cfg->tpm_owner_auth, auth, n);+ cfg->tpm_owner_auth_len = n;+ } else {+ pr_warn("config: tpm.owner_auth: not an even count of up to %zu hex digits (ignored)",+ 2 * sizeof(auth));}- memcpy(cfg->tpm_owner_auth, auth, hl / 2);- cfg->tpm_owner_auth_len = hl / 2;memzero_explicit(auth, sizeof(auth));return;}diff --git a/util/hex.c b/util/hex.cindex 2b4823eb..0b522791 100644--- a/util/hex.c+++ b/util/hex.c@@ -59,6 +59,19 @@ int hex_decode(const char *hex, __u8 *out, size_t n)return 0;}+int hex_decode_max(const char *hex, __u8 *out, size_t cap, size_t *len)+{+ size_t n = strnlen(hex, size_add(size_mul(cap, 2), 1));+ int ret;++ if (n % 2 || n / 2 > cap)+ return -EINVAL;+ ret = hex_decode(hex, out, n / 2);+ if (!ret)+ *len = n / 2;+ return ret;+}+int sha256_digest_decode(const char *digest, __u8 out[32]){if (!digest || strncmp(digest, "sha256:", 7))diff --git a/util/hex.h b/util/hex.hindex bf54d6c2..04c2c613 100644--- a/util/hex.h+++ b/util/hex.h@@ -22,6 +22,11 @@ int hex_nibble(int c);/* Exactly 2 * n hex digits, either case. 0 or -EINVAL. */int hex_decode(const char *hex, __u8 *out, size_t n);+/*+ * An even count of hex digits, either case and none at all included, of+ * at most cap bytes, into *len bytes at out. 0 or -EINVAL.+ */+int hex_decode_max(const char *hex, __u8 *out, size_t cap, size_t *len);/** An OCI digest, "sha256:" and 64 hex digits of either case, into its 32