Received: from eggs.gnu.org ([2001:4830:134:3::10]:55731) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c4Pxf-0001mw-UE for qemu-devel@nongnu.org; Wed, 09 Nov 2016 05:18:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c4Pxc-0001Sb-OB for qemu-devel@nongnu.org; Wed, 09 Nov 2016 05:18:31 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60714) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c4Pxc-0001SG-JG for qemu-devel@nongnu.org; Wed, 09 Nov 2016 05:18:28 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1589661B97 for ; Wed, 9 Nov 2016 10:18:27 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 9 Nov 2016 14:18:15 +0400 Message-Id: <20161109101815.9057-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH] cipher: fix leak on initialization error List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: berrange@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= If ctx->blocksize !=3D XTS_BLOCK_SIZE, ctx will be leaked. Assign ctx earler, and call qcrypto_cipher_free() on error. Spotted thanks to ASAN. Signed-off-by: Marc-Andr=C3=A9 Lureau --- crypto/cipher-nettle.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index cd094cd..593962c 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -376,6 +376,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgori= thm alg, goto error; } =20 + cipher->opaque =3D ctx; if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS && ctx->blocksize !=3D XTS_BLOCK_SIZE) { error_setg(errp, "Cipher block size %zu must equal XTS block siz= e %d", @@ -384,13 +385,11 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgo= rithm alg, } =20 ctx->iv =3D g_new0(uint8_t, ctx->blocksize); - cipher->opaque =3D ctx; =20 return cipher; =20 error: - g_free(cipher); - g_free(ctx); + qcrypto_cipher_free(cipher); return NULL; } =20 @@ -404,10 +403,12 @@ void qcrypto_cipher_free(QCryptoCipher *cipher) } =20 ctx =3D cipher->opaque; - g_free(ctx->iv); - g_free(ctx->ctx); - g_free(ctx->ctx_tweak); - g_free(ctx); + if (ctx) { + g_free(ctx->iv); + g_free(ctx->ctx); + g_free(ctx->ctx_tweak); + g_free(ctx); + } g_free(cipher); } =20 --=20 2.10.0