From nobody Wed Oct 23 00:38:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1729610859; cv=none; d=zohomail.com; s=zohoarc; b=X0DtUGBDGE9q2gw8+w44QTSk/NQ+qn8T5qXB6c7w53Qjpfhx5b8K42oTWCd+bDzE6AQm20AWaU42EZPoYOJDSEiaSORhMoCNd8Inb12IMaADqsGRcyOfTJPjNG/jCCjZ/VP8C9AoB63YkDZVqNWxou3YHuhATvBXzQKrP+lxrXM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729610859; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=84N2rxqupBUcdaTpHle3WoL4A06cF9MmgU6vQgWoN4s=; b=ZN+frVy9gq40ik9Mfoz9oUaARUFEs3E4nsjE+/oetAsPpBupocW90H8Ley7GZggBc5Q/mZ7rEoeRCDDcHw41oShd2LSzNxh9rThG6DlEvLCMEab2H7BeK6yccy8T/Fim5aDwsO4PvcJBZGW1ICLg+Yv115p1K8C8jQWLOSm/XAY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1729610859529724.6227852834727; Tue, 22 Oct 2024 08:27:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t3GkY-0007j1-RX; Tue, 22 Oct 2024 11:24:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t3GkR-0007i6-Ci for qemu-devel@nongnu.org; Tue, 22 Oct 2024 11:24:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t3GkP-0004za-65 for qemu-devel@nongnu.org; Tue, 22 Oct 2024 11:24:38 -0400 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-jL6oenC3MIe6J0JtEi16FQ-1; Tue, 22 Oct 2024 11:24:32 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5FBEE1953978; Tue, 22 Oct 2024 15:24:31 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.59]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EDBA219560A2; Tue, 22 Oct 2024 15:24:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729610676; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=84N2rxqupBUcdaTpHle3WoL4A06cF9MmgU6vQgWoN4s=; b=erHES6USrv1jPkAXxtQ2tBJ/BYGAz0UB1A8fphqjwqfCoASpaqGRaTpy4xWZI2OiVl1Ttq 7OQDorOgiPl8NKMCn6t6Jh1usmL2s93ZgCu6wR+gsTETGDGc2AuuCAQjkqoMFv8N7ko1le BCY9hy8DlftucLKpWf9rWRFWUoWY+xo= X-MC-Unique: jL6oenC3MIe6J0JtEi16FQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Wainer dos Santos Moschetta , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Dorjoy Chowdhury Subject: [PULL 04/14] crypto/hash: avoid overwriting user supplied result pointer Date: Tue, 22 Oct 2024 16:24:05 +0100 Message-ID: <20241022152415.1632556-5-berrange@redhat.com> In-Reply-To: <20241022152415.1632556-1-berrange@redhat.com> References: <20241022152415.1632556-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.519, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1.697, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1729610861729116600 If the user provides a pre-allocated buffer for the hash result, we must use that rather than re-allocating a new buffer. Reported-by: Dorjoy Chowdhury Signed-off-by: Daniel P. Berrang=C3=A9 --- crypto/hash-gcrypt.c | 15 ++++++++++++--- crypto/hash-glib.c | 11 +++++++++-- crypto/hash-gnutls.c | 16 +++++++++++++--- crypto/hash-nettle.c | 14 +++++++++++--- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/crypto/hash-gcrypt.c b/crypto/hash-gcrypt.c index ccc3cce3f8..73533a4949 100644 --- a/crypto/hash-gcrypt.c +++ b/crypto/hash-gcrypt.c @@ -103,16 +103,25 @@ int qcrypto_gcrypt_hash_finalize(QCryptoHash *hash, size_t *result_len, Error **errp) { + int ret; unsigned char *digest; gcry_md_hd_t *ctx =3D hash->opaque; =20 - *result_len =3D gcry_md_get_algo_dlen(qcrypto_hash_alg_map[hash->alg]); - if (*result_len =3D=3D 0) { + ret =3D gcry_md_get_algo_dlen(qcrypto_hash_alg_map[hash->alg]); + if (ret =3D=3D 0) { error_setg(errp, "Unable to get hash length"); return -1; } =20 - *result =3D g_new(uint8_t, *result_len); + if (*result_len =3D=3D 0) { + *result_len =3D ret; + *result =3D g_new(uint8_t, *result_len); + } else if (*result_len !=3D ret) { + error_setg(errp, + "Result buffer size %zu is smaller than hash %d", + *result_len, ret); + return -1; + } =20 /* Digest is freed by gcry_md_close(), copy it */ digest =3D gcry_md_read(*ctx, 0); diff --git a/crypto/hash-glib.c b/crypto/hash-glib.c index 02a6ec1edf..809cef98ae 100644 --- a/crypto/hash-glib.c +++ b/crypto/hash-glib.c @@ -99,8 +99,15 @@ int qcrypto_glib_hash_finalize(QCryptoHash *hash, return -1; } =20 - *result_len =3D ret; - *result =3D g_new(uint8_t, *result_len); + if (*result_len =3D=3D 0) { + *result_len =3D ret; + *result =3D g_new(uint8_t, *result_len); + } else if (*result_len !=3D ret) { + error_setg(errp, + "Result buffer size %zu is smaller than hash %d", + *result_len, ret); + return -1; + } =20 g_checksum_get_digest(ctx, *result, result_len); return 0; diff --git a/crypto/hash-gnutls.c b/crypto/hash-gnutls.c index 34a63994c9..99fbe824ea 100644 --- a/crypto/hash-gnutls.c +++ b/crypto/hash-gnutls.c @@ -115,14 +115,24 @@ int qcrypto_gnutls_hash_finalize(QCryptoHash *hash, Error **errp) { gnutls_hash_hd_t *ctx =3D hash->opaque; + int ret; =20 - *result_len =3D gnutls_hash_get_len(qcrypto_hash_alg_map[hash->alg]); - if (*result_len =3D=3D 0) { + ret =3D gnutls_hash_get_len(qcrypto_hash_alg_map[hash->alg]); + if (ret =3D=3D 0) { error_setg(errp, "Unable to get hash length"); return -1; } =20 - *result =3D g_new(uint8_t, *result_len); + if (*result_len =3D=3D 0) { + *result_len =3D ret; + *result =3D g_new(uint8_t, *result_len); + } else if (*result_len !=3D ret) { + error_setg(errp, + "Result buffer size %zu is smaller than hash %d", + *result_len, ret); + return -1; + } + gnutls_hash_output(*ctx, *result); return 0; } diff --git a/crypto/hash-nettle.c b/crypto/hash-nettle.c index 3b847aa60e..c78624b347 100644 --- a/crypto/hash-nettle.c +++ b/crypto/hash-nettle.c @@ -150,9 +150,17 @@ int qcrypto_nettle_hash_finalize(QCryptoHash *hash, Error **errp) { union qcrypto_hash_ctx *ctx =3D hash->opaque; - - *result_len =3D qcrypto_hash_alg_map[hash->alg].len; - *result =3D g_new(uint8_t, *result_len); + int ret =3D qcrypto_hash_alg_map[hash->alg].len; + + if (*result_len =3D=3D 0) { + *result_len =3D ret; + *result =3D g_new(uint8_t, *result_len); + } else if (*result_len !=3D ret) { + error_setg(errp, + "Result buffer size %zu is smaller than hash %d", + *result_len, ret); + return -1; + } =20 qcrypto_hash_alg_map[hash->alg].result(ctx, *result_len, *result); =20 --=20 2.46.0