From 87e77d6516c6161e6cfe3043fefa22e62d32c885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 11 Aug 2025 10:45:41 +0200 Subject: [PATCH] bignum: fix memory leak in GCD with 0 as an input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- library/bignum.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/bignum.c b/library/bignum.c index 96cade4bfe..358714839c 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1841,10 +1841,12 @@ int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B) /* Handle special cases (that don't happen in crypto usage) */ if (mbedtls_mpi_core_check_zero_ct(TA.p, TA.n) == MBEDTLS_CT_FALSE) { - return mbedtls_mpi_copy(G, &TB); // GCD(0, B) = abs(B) + MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB)); // GCD(0, B) = abs(B) + goto cleanup; } if (mbedtls_mpi_core_check_zero_ct(TB.p, TB.n) == MBEDTLS_CT_FALSE) { - return mbedtls_mpi_copy(G, &TA); // GCD(A, 0) = abs(A) + MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TA)); // GCD(A, 0) = abs(A) + goto cleanup; } const size_t za = mbedtls_mpi_lsb(&TA);