Commit c2e0217c52 for bind

commit c2e0217c5283a821218f4a3d3b5ffcfef06af908
Author: OndÅ™ej Surý <ondrej@isc.org>
Date:   Wed Nov 25 12:11:09 2026 +0100

    Check the size of the output buffer in isc_hmac()

    With the OpenSSL 1.1 API, isc_hmac() ignored the size of the output
    buffer passed in by the caller, because HMAC() only returns the length
    of the MAC it has written.  All the current callers pass a buffer that
    is large enough, but a smaller one would be overrun.  Return
    ISC_R_NOSPACE for a buffer that is too small, with both APIs.

    Assisted-by: Claude:claude-mythos-5-2

diff --git a/lib/isc/crypto/ossl1_1.c b/lib/isc/crypto/ossl1_1.c
index 46622de71a..4566778f90 100644
--- a/lib/isc/crypto/ossl1_1.c
+++ b/lib/isc/crypto/ossl1_1.c
@@ -137,6 +137,11 @@ isc_hmac(isc_md_type_t type, const void *key, const size_t keylen,
 		return ISC_R_NOTIMPLEMENTED;
 	}

+	/* HMAC() is not told how large the output buffer is. */
+	if (*digestlen < (unsigned int)EVP_MD_size(md)) {
+		return ISC_R_NOSPACE;
+	}
+
 	if (HMAC(md, key, keylen, buf, len, digest, digestlen) == NULL) {
 		ERR_clear_error();
 		return ISC_R_CRYPTOFAILURE;
diff --git a/lib/isc/crypto/ossl3.c b/lib/isc/crypto/ossl3.c
index a8641e4454..6da322a54d 100644
--- a/lib/isc/crypto/ossl3.c
+++ b/lib/isc/crypto/ossl3.c
@@ -266,6 +266,10 @@ isc_hmac(isc_md_type_t type, const void *key, const size_t keylen,
 		return ISC_R_NOTIMPLEMENTED;
 	}

+	if (*digestlen < (unsigned int)EVP_MD_size(isc__crypto_md[type])) {
+		return ISC_R_NOSPACE;
+	}
+
 	ctx = EVP_MAC_CTX_new(evp_hmac);
 	RUNTIME_CHECK(ctx != NULL);

diff --git a/lib/isc/include/isc/hmac.h b/lib/isc/include/isc/hmac.h
index b8497c21f2..5ff81bbd17 100644
--- a/lib/isc/include/isc/hmac.h
+++ b/lib/isc/include/isc/hmac.h
@@ -45,6 +45,8 @@ typedef struct isc_hmac_key isc_hmac_key_t;
  * for the hash function output (use ISC_MAX_MD_SIZE if unsure). @digestlen
  * is used to pass in the length of the digest buffer and returns the length
  * of digest written to @digest.
+ *
+ * Returns ISC_R_NOSPACE when @digest is too small for the output.
  */
 isc_result_t
 isc_hmac(isc_md_type_t type, const void *key, const size_t keylen,
diff --git a/tests/isc/hmac_test.c b/tests/isc/hmac_test.c
index 3748b48f6a..9a58ba92a5 100644
--- a/tests/isc/hmac_test.c
+++ b/tests/isc/hmac_test.c
@@ -201,6 +201,34 @@ ISC_RUN_TEST_IMPL(isc_hmac_final) {
 	isc_hmac_key_destroy(&key);
 }

+ISC_RUN_TEST_IMPL(isc_hmac_oneshot) {
+	const unsigned char data[] = "data";
+	unsigned char digest[ISC_MAX_MD_SIZE];
+	unsigned int digestlen;
+
+	UNUSED(state);
+
+	/* The output buffer is large enough */
+	digestlen = sizeof(digest);
+	assert_int_equal(
+		isc_hmac(ISC_MD_SHA256, "key", 3, data, 4, digest, &digestlen),
+		ISC_R_SUCCESS);
+	assert_int_equal(digestlen, ISC_SHA256_DIGESTLENGTH);
+
+	/* The output buffer has exactly the size of the digest */
+	digestlen = ISC_SHA256_DIGESTLENGTH;
+	assert_int_equal(
+		isc_hmac(ISC_MD_SHA256, "key", 3, data, 4, digest, &digestlen),
+		ISC_R_SUCCESS);
+	assert_int_equal(digestlen, ISC_SHA256_DIGESTLENGTH);
+
+	/* The output buffer is too small */
+	digestlen = ISC_SHA256_DIGESTLENGTH - 1;
+	assert_int_equal(
+		isc_hmac(ISC_MD_SHA256, "key", 3, data, 4, digest, &digestlen),
+		ISC_R_NOSPACE);
+}
+
 ISC_RUN_TEST_IMPL(isc_hmac_md5) {
 	isc_hmac_t *hmac_st = *state;

@@ -895,6 +923,7 @@ ISC_TEST_LIST_START

 ISC_TEST_ENTRY(isc_hmac_new)
 ISC_TEST_ENTRY(isc_hmac_key_create)
+ISC_TEST_ENTRY(isc_hmac_oneshot)
 ISC_TEST_ENTRY(isc_hmac_md5)
 ISC_TEST_ENTRY(isc_hmac_sha1)
 ISC_TEST_ENTRY(isc_hmac_sha224)