eBPF-based security platform
hex: parse a sha256: digest in one place
Daniel Borkmann91f7e8d4544f
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 494b2b05..efb902ed 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1724,18 +1724,11 @@ static int load_tree_config(struct bpm_tree_config *cfg)return 0;}-static int tree_digest_hex(const char *digest, __u8 out[32])-{- if (!digest || strncmp(digest, "sha256:", 7))- return -EINVAL;- return hex_decode(digest + 7, out, 32);-}-static bool tree_digest_valid(const char *digest){__u8 raw[32];- return !tree_digest_hex(digest, raw);+ return !sha256_digest_decode(digest, raw);}static int tree_fetch_publication(const struct bpm_tree_config *cfg,@@ -2070,7 +2063,7 @@ static int tree_recover_pending(const struct tree_publication *pub,unlink(BPM_TREE_PENDING_PATH);return 0;}- if (tree_digest_hex(pending.operation_digest, digest) ||+ if (sha256_digest_decode(pending.operation_digest, digest) ||hex_decode(state->projection_root, previous, 32))return -EINVAL;ret = sha256_2(previous, sizeof(previous), digest, sizeof(digest),@@ -2121,10 +2114,10 @@ static int tree_validate_operation(const struct tree_operation *op)if (!op || op->seq <= 0 || !upatch_name_valid(op->id) ||(strcmp(op->op, "add") && strcmp(op->op, "delete")))return -EINVAL;- if (tree_digest_hex(op->operation_digest, digest) ||- tree_digest_hex(op->artifact.manifest_digest, digest) ||- tree_digest_hex(op->artifact.layer_digest, digest) ||- tree_digest_hex(op->artifact.payload_sha256, digest))+ if (sha256_digest_decode(op->operation_digest, digest) ||+ sha256_digest_decode(op->artifact.manifest_digest, digest) ||+ sha256_digest_decode(op->artifact.layer_digest, digest) ||+ sha256_digest_decode(op->artifact.payload_sha256, digest))return -EINVAL;if (tree_operation_digest(op, expected_digest))return -EINVAL;@@ -2321,7 +2314,7 @@ static int cmd_bpm_tree_sync(int argc, const char **argv)selected++;if (dry_run)continue;- ret = tree_digest_hex(op->operation_digest, digest);+ ret = sha256_digest_decode(op->operation_digest, digest);if (ret) {tree_sync_report_error("operation_digest_parse", op, ret);ret = -EINVAL;diff --git a/testing/unit/hex.c b/testing/unit/hex.cindex 799ebf9f..8858eff1 100644--- a/testing/unit/hex.c+++ b/testing/unit/hex.c@@ -42,11 +42,33 @@ static void decode_refuses_non_hex(void)EXPECT_EQ(hex_decode("zz", out, 1), -EINVAL);}+#define HEX64 "00112233445566778899aabbccddeeffFFEEDDCCBBAA99887766554433221100"+#define NOTHEX64 "g0112233445566778899aabbccddeeffFFEEDDCCBBAA99887766554433221100"++static void sha256_digest_is_prefix_and_64_digits(void)+{+ __u8 out[32];++ EXPECT_EQ(sha256_digest_decode("sha256:" HEX64, out), 0);+ EXPECT_EQ(out[0], 0x00);+ EXPECT_EQ(out[1], 0x11);+ EXPECT_EQ(out[16], 0xff);+ EXPECT_EQ(out[31], 0x00);+ EXPECT_EQ(sha256_digest_decode(NULL, out), -EINVAL);+ EXPECT_EQ(sha256_digest_decode(HEX64, out), -EINVAL);+ EXPECT_EQ(sha256_digest_decode("sha512:" HEX64, out), -EINVAL);+ EXPECT_EQ(sha256_digest_decode("SHA256:" HEX64, out), -EINVAL);+ EXPECT_EQ(sha256_digest_decode("sha256:" HEX64 "0", out), -EINVAL);+ EXPECT_EQ(sha256_digest_decode("sha256:", out), -EINVAL);+ EXPECT_EQ(sha256_digest_decode("sha256:" NOTHEX64, out), -EINVAL);+}+static const struct unit_case cases[] = {UNIT_CASE(encode_is_lower_case),UNIT_CASE(decode_takes_either_case),UNIT_CASE(decode_wants_the_exact_length),UNIT_CASE(decode_refuses_non_hex),+ UNIT_CASE(sha256_digest_is_prefix_and_64_digits),{}};diff --git a/util/attest.c b/util/attest.cindex f3928175..864041c5 100644--- a/util/attest.c+++ b/util/attest.c@@ -591,8 +591,7 @@ static int on_record(void *ctx)(__s64)p->tree_seq) < 0)return -E2BIG;if (p->tree_seq > 0 &&- !strncmp(p->tree_digest_hex, "sha256:", 7) &&- !hex_decode(p->tree_digest_hex + 7, operation, 32) &&+ !sha256_digest_decode(p->tree_digest_hex, operation) &&!chain_tree_event(p->tree_seq, operation, event)) {r->derived = !memneq_explicit(event, r->digest, 32);if (strsprintf(r->label, sizeof(r->label),diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 37a065a8..e4a1acec 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -922,8 +922,7 @@ static int on_descriptor(void *ud)return -E2BIG;/* The registry wrote this: a layer is all three or none. */if (!s->scratch.media[0] || s->scratch.size < 0 ||- strncmp(s->scratch.digest, "sha256:", 7) ||- hex_decode(s->scratch.digest + 7, digest, sizeof(digest)))+ sha256_digest_decode(s->scratch.digest, digest))return -EINVAL;s->items[s->n++] = s->scratch;memset(&s->scratch, 0, sizeof(s->scratch));diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex 797ceccd..320cf755 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -572,7 +572,7 @@ static bool hex32_valid(const char *hex)int tree_state_read(struct tree_state *out){- __u8 *body;+ __u8 *body, digest[32];size_t len;__s64 schema = 0;struct json_field f[] = {@@ -609,9 +609,7 @@ int tree_state_read(struct tree_state *out)!hex32_valid(out->projection_root_tpm)))))ret = -EINVAL;if (!ret && out->manifest_digest[0] &&- (strlen(out->manifest_digest) != 71 ||- strncmp(out->manifest_digest, "sha256:", 7) ||- !hex32_valid(out->manifest_digest + 7)))+ sha256_digest_decode(out->manifest_digest, digest))ret = -EINVAL;out->schema = (int)schema;xfree(body, len + 1);@@ -660,10 +658,10 @@ int tree_event_append(__s64 seq, const char *operation_digest,{char buf[1024], root[65];struct json_emit e = { .buf = buf, .cap = sizeof(buf) };+ __u8 digest[32];int fd, ret;- if (!operation_digest || strncmp(operation_digest, "sha256:", 7) ||- !hex32_valid(operation_digest + 7))+ if (sha256_digest_decode(operation_digest, digest))return -EINVAL;hex_encode(projection_root, 32, root);json_obj_open(&e);@@ -734,8 +732,7 @@ int tree_event_replay(__u8 out[32])break;}previous_seq = seq;- if (strncmp(digest, "sha256:", 7) ||- hex_decode(digest + 7, d, sizeof(d))) {+ if (sha256_digest_decode(digest, d)) {ret = -EINVAL;break;}diff --git a/util/hex.c b/util/hex.cindex a7a025f0..2b4823eb 100644--- a/util/hex.c+++ b/util/hex.c@@ -58,3 +58,10 @@ int hex_decode(const char *hex, __u8 *out, size_t n)}return 0;}++int sha256_digest_decode(const char *digest, __u8 out[32])+{+ if (!digest || strncmp(digest, "sha256:", 7))+ return -EINVAL;+ return hex_decode(digest + 7, out, 32);+}diff --git a/util/hex.h b/util/hex.hindex c87fb559..bf54d6c2 100644--- a/util/hex.h+++ b/util/hex.h@@ -23,4 +23,10 @@ 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 OCI digest, "sha256:" and 64 hex digits of either case, into its 32+ * bytes. 0 or -EINVAL.+ */+int sha256_digest_decode(const char *digest, __u8 out[32]);+#endif /* SHIRUDO_HEX_H */