Skip to contentSkip to conversation

cipher: fix leak on initialization error

PATCH In review Marc-André Lureau 2 participants 2 messages Nov 9, 2016 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cipher: fix leak on initialization error

On error path, ctx may be leaked. Assign ctx earlier, and call qcrypto_cipher_free() on error.

Spotted thanks to ASAN.

---
 crypto/cipher-nettle.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
crypto/cipher-nettle.c +2 -3
index cd094cd..5798910 100644
@@ -254,6 +254,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     cipher->mode = mode;
 
     ctx = g_new0(QCryptoCipherNettle, 1);
+    cipher->opaque = ctx;
 
     switch (alg) {
     case QCRYPTO_CIPHER_ALG_DES_RFB:
@@ -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;
 }
 
Signature · 1 line
2.10.0
#1 lore raw
Quoted 9 lines · Marc-André Lureau

On error path, ctx may be leaked. Assign ctx earlier, and call qcrypto_cipher_free() on error.

Spotted thanks to ASAN.

---
 crypto/cipher-nettle.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Thanks, queued on my crypto-next branch

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/ :|