Skip to contentSkip to conversation

cipher: fix leak on initialization error

PATCH Superseded Marc-André Lureau 3 participants 3 messages Nov 9, 2016 Crypto API

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cipher: fix leak on initialization error

If ctx->blocksize != XTS_BLOCK_SIZE, ctx will be leaked. Assign ctx earler, and call qcrypto_cipher_free() on error.

Spotted thanks to ASAN.

---
 crypto/cipher-nettle.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
crypto/cipher-nettle.c +8 -7
index cd094cd..593962c 100644
@@ -376,6 +376,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         goto error;
     }
 
+    cipher->opaque = ctx;
     if (mode == QCRYPTO_CIPHER_MODE_XTS &&
         ctx->blocksize != XTS_BLOCK_SIZE) {
         error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
@@ -384,13 +385,11 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     }
 
     ctx->iv = g_new0(uint8_t, ctx->blocksize);
-    cipher->opaque = ctx;
 
     return cipher;
 
  error:
-    g_free(cipher);
-    g_free(ctx);
+    qcrypto_cipher_free(cipher);
     return NULL;
 }
 
@@ -404,10 +403,12 @@ void qcrypto_cipher_free(QCryptoCipher *cipher)
     }
 
     ctx = 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);
 }
 
Signature · 1 line
2.10.0
#1 lore raw
Quoted 15 lines · Marc-André Lureau

If ctx->blocksize != XTS_BLOCK_SIZE, ctx will be leaked. Assign ctx earler, and call qcrypto_cipher_free() on error.

Spotted thanks to ASAN.

---
 crypto/cipher-nettle.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
crypto/cipher-nettle.c +0 -0
index cd094cd..593962c 100644

crypto/cipher-nettle.c QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, · line 377

Quoted 2 lines
crypto/cipher-nettle.c +0 -0
@@ -376,6 +376,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         goto error;
     }

'ctx' is non-NULL at this point and there's a 'goto error' just above here....

Quoted 9 lines
crypto/cipher-nettle.c +1 -0
+    cipher->opaque = ctx;
     if (mode == QCRYPTO_CIPHER_MODE_XTS &&
         ctx->blocksize != XTS_BLOCK_SIZE) {
         error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
@@ -384,13 +385,11 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     }
 
     ctx->iv = g_new0(uint8_t, ctx->blocksize);

crypto/cipher-nettle.c QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, · line 394

Quoted 10 lines
crypto/cipher-nettle.c +1 -3
-    cipher->opaque = ctx;
 
     return cipher;
 
  error:
-    g_free(cipher);
-    g_free(ctx);
+    qcrypto_cipher_free(cipher);
     return NULL;
 }

...so you're leaking 'ctx' now, since it hasn't been assigned to cipher->ctx.

You need to move 'cipher->opque = ctx' to the place where we initially allocate 'ctx', before any gotos at which point....

Quoted 7 lines
crypto/cipher-nettle.c +0 -2
@@ -404,10 +403,12 @@ void qcrypto_cipher_free(QCryptoCipher *cipher)
     }
 
     ctx = cipher->opaque;
-    g_free(ctx->iv);
-    g_free(ctx->ctx);

crypto/cipher-nettle.c void qcrypto_cipher_free(QCryptoCipher *cipher) · line 413

Quoted 10 lines
crypto/cipher-nettle.c +6 -2
-    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);
 }

...this change is not needed

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#2 lore raw

Hi

----- Original Message -----

Quoted 11 lines · Marc-André Lureau
Quoted 10 lines

If ctx->blocksize != XTS_BLOCK_SIZE, ctx will be leaked. Assign ctx earler, and call qcrypto_cipher_free() on error.

Spotted thanks to ASAN.

---
 crypto/cipher-nettle.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
Quoted 12 lines

crypto/cipher-nettle.c QCryptoCipher · line 378

Quoted 8 lines
crypto/cipher-nettle.c +0 -0
index cd094cd..593962c 100644
@@ -376,6 +376,7 @@ QCryptoCipher
 *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         goto error;
     }

'ctx' is non-NULL at this point and there's a 'goto error' just above here....

Right, fixing it sending v2

Quoted 56 lines
Quoted 11 lines
crypto/cipher-nettle.c +1 -0
+    cipher->opaque = ctx;
     if (mode == QCRYPTO_CIPHER_MODE_XTS &&
         ctx->blocksize != XTS_BLOCK_SIZE) {
         error_setg(errp, "Cipher block size %zu must equal XTS block size
         %d",
crypto/cipher-nettle.c +0 -0
@@ -384,13 +385,11 @@ QCryptoCipher
 *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     }
 
     ctx->iv = g_new0(uint8_t, ctx->blocksize);

crypto/cipher-nettle.c QCryptoCipher · line 395

Quoted 10 lines
crypto/cipher-nettle.c +1 -3
-    cipher->opaque = ctx;
 
     return cipher;
 
  error:
-    g_free(cipher);
-    g_free(ctx);
+    qcrypto_cipher_free(cipher);
     return NULL;
 }

...so you're leaking 'ctx' now, since it hasn't been assigned to cipher->ctx.

You need to move 'cipher->opque = ctx' to the place where we initially allocate 'ctx', before any gotos at which point....

Quoted 7 lines
crypto/cipher-nettle.c +0 -2
@@ -404,10 +403,12 @@ void qcrypto_cipher_free(QCryptoCipher *cipher)
     }
 
     ctx = cipher->opaque;
-    g_free(ctx->iv);
-    g_free(ctx->ctx);

crypto/cipher-nettle.c void qcrypto_cipher_free(QCryptoCipher *cipher) · line 413

Quoted 10 lines
crypto/cipher-nettle.c +6 -2
-    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);
 }

...this change is not needed

Regards,
Daniel
--

|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|