Memory leak when using openssl backend
Hi
I have found a memory leak when using openssl backend. I don't know if this is the proper way to report it.
The problem is that crypt_hash_init() and crypt_hash_restart() calls EVP_DigestInit() but crypt_hash_final() calls EVP_DigestFinal_ex(). This results in that the context struct is zeroed when crypt_hash_restart() calls EVP_DigestInit(), and the memory allocated by the previous call to EVP_DigestInit() is lost. The version of openssl we use is 1.0.2g. This leak might not occur in later versions of openssl that has changed how those functions work.
I have attached a suggested fix that only uses the _ex versions of the EVP functions.
/Per
From c706d110be72bb78ff270d23ea9e3c7892bd98ba Mon Sep 17 00:00:00 2001
From: Per x Johansson <[email protected]>
Date: Tue, 11 Oct 2016 10:34:11 +0200
Subject: [PATCH] Fix memory leak when using openssl backend
Fixes a memory leak when using openssl backend caused by mismatched calls to EVP_DigestInit and EVP_DigestFinal_ex.
--- lib/crypto_backend/crypto_openssl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/crypto_backend/crypto_openssl.c +3 -2
@@ -95,7 +95,8 @@ int crypt_hash_init(struct crypt_hash **ctx, const char *name)return -EINVAL ; } -if (EVP_DigestInit (&h->md, h->hash_id) !=1 ) { +EVP_MD_CTX_init (&h->md); +if (EVP_DigestInit_ex (&h->md, h->hash_id,NULL ) !=1 ) {free (h);return -EINVAL ; }
@@ -107,7 +108,7 @@ int crypt_hash_init(struct crypt_hash **ctx, const char *name)static int crypt_hash_restart (struct crypt_hash *ctx) { -if (EVP_DigestInit (&ctx->md, ctx->hash_id) !=1 ) +if (EVP_DigestInit_ex (&ctx->md, ctx->hash_id,NULL ) !=1 )return -EINVAL ;return 0 ;
Signature · 1 line
2.1.4
Quoted 2 lines · Per Johansson
I have found a memory leak when using openssl backend. I don't know if this is the proper way to report it.
Hi,
yes, it is one of ways how to report it (you can use gitlab issues as well).
Thanks for report! (I'll handle that patch in upstream git later this week.)
BTW Do you use some automated tool to find such problems or it was random find?
Milan
Quoted 21 lines
The problem is that
crypt_hash_init()andcrypt_hash_restart()callsEVP_DigestInit()butcrypt_hash_final()callsEVP_DigestFinal_ex(). This results in that the context struct is zeroed whencrypt_hash_restart()callsEVP_DigestInit(), and the memory allocated by the previous call toEVP_DigestInit()is lost. The version of openssl we use is 1.0.2g. This leak might not occur in later versions of openssl that has changed how those functions work.I have attached a suggested fix that only uses the
_exversions of the EVP functions./Per
_______________________________________________
dm-crypt mailing list
[email protected]
http://www.saout.de/mailman/listinfo/dm-crypt