Skip to content

eBPF-based security platform

crypto: leave a zeroed digest where sha256_buffer() fails

Daniel Borkmann225568f2c365
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 8c803b6f..11f1c842 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -777,10 +777,10 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 30 of the modules in
+The unit suites in :src:`testing/unit/` cover 31 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
-``chain_history``, ``conditions``,
+``chain_history``, ``conditions``, ``crypto_openssl``,
``eventlog``, ``format``, ``hex``, ``io``, ``json``, ``kernel_abi``,
``keepalive``, ``keepalive_quote``, ``kernel_build_id``,
``kernel_release``, ``measured``,
diff --git a/testing/unit/crypto_openssl.c b/testing/unit/crypto_openssl.c
new file mode 100644
index 00000000..b5bfc812
--- /dev/null
+++ b/testing/unit/crypto_openssl.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <string.h>
+
+#include "crypto.h"
+#include "unit.h"
+
+static void sha256_buffer_zeroes_a_failed_digest(void)
+{
+ __u8 out[CRYPTO_SHA256_LEN], zero[CRYPTO_SHA256_LEN] = {};
+
+ memset(out, 0xff, sizeof(out));
+ EXPECT_NE(sha256_buffer(NULL, 3, out), 0);
+ EXPECT_MEMEQ(out, zero, sizeof(out));
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(sha256_buffer_zeroes_a_failed_digest),
+ {}
+};
+
+UNIT_SUITE(crypto_openssl, cases);
diff --git a/util/crypto.h b/util/crypto.h
index f6655787..b4419856 100644
--- a/util/crypto.h
+++ b/util/crypto.h
@@ -29,6 +29,7 @@ int sha256_update(struct sha256_ctx *ctx,
int sha256_final(struct sha256_ctx *ctx,
__u8 out[CRYPTO_SHA256_LEN]) __must_check;
void sha256_free(struct sha256_ctx *ctx);
+/* Where it fails, out is all zeros. */
int sha256_buffer(const void *data, size_t len,
__u8 out[CRYPTO_SHA256_LEN]) __must_check;
/*
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index 3524712f..211f6180 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -131,6 +131,8 @@ int sha256_buffer(const void *data, size_t len,
ret = sha256_final(&ctx, out);
else
sha256_free(&ctx);
+ if (ret && out)
+ memset(out, 0, CRYPTO_SHA256_LEN);
return ret;
}