From a4a123eb4fc621472440fbe45229149b89d43138 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Wed, 8 Jul 2026 23:42:31 +0200 Subject: [PATCH] hasher: Avoid theoretical memory leaks for hashers that could potentially fail These `get_hash()` implementations could potentially fail (realistically only for serious system errors like OOM). This change ensures we comply with the documented behavior (i.e. only allocate memory on success), as no callers currently expect they have to clean up on failure. --- src/libstrongswan/plugins/af_alg/af_alg_hasher.c | 7 ++++++- src/libstrongswan/plugins/botan/botan_hasher.c | 7 ++++++- src/libstrongswan/plugins/openssl/openssl_hasher.c | 7 ++++++- src/libstrongswan/plugins/wolfssl/wolfssl_hasher.c | 7 ++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/libstrongswan/plugins/af_alg/af_alg_hasher.c b/src/libstrongswan/plugins/af_alg/af_alg_hasher.c index 7fac236d4..3b07472fb 100644 --- a/src/libstrongswan/plugins/af_alg/af_alg_hasher.c +++ b/src/libstrongswan/plugins/af_alg/af_alg_hasher.c @@ -119,7 +119,12 @@ METHOD(hasher_t, allocate_hash, bool, if (hash) { *hash = chunk_alloc(get_hash_size(this)); - return get_hash(this, chunk, hash->ptr); + if (!get_hash(this, chunk, hash->ptr)) + { + chunk_free(hash); + return FALSE; + } + return TRUE; } return get_hash(this, chunk, NULL); } diff --git a/src/libstrongswan/plugins/botan/botan_hasher.c b/src/libstrongswan/plugins/botan/botan_hasher.c index d574db0dc..0675fee80 100644 --- a/src/libstrongswan/plugins/botan/botan_hasher.c +++ b/src/libstrongswan/plugins/botan/botan_hasher.c @@ -89,7 +89,12 @@ METHOD(hasher_t, allocate_hash, bool, if (hash) { *hash = chunk_alloc(get_hash_size(this)); - return get_hash(this, chunk, hash->ptr); + if (!get_hash(this, chunk, hash->ptr)) + { + chunk_free(hash); + return FALSE; + } + return TRUE; } return get_hash(this, chunk, NULL); } diff --git a/src/libstrongswan/plugins/openssl/openssl_hasher.c b/src/libstrongswan/plugins/openssl/openssl_hasher.c index cc3b1d1ac..fce1e329a 100644 --- a/src/libstrongswan/plugins/openssl/openssl_hasher.c +++ b/src/libstrongswan/plugins/openssl/openssl_hasher.c @@ -77,7 +77,12 @@ METHOD(hasher_t, allocate_hash, bool, if (hash) { *hash = chunk_alloc(get_hash_size(this)); - return get_hash(this, chunk, hash->ptr); + if (!get_hash(this, chunk, hash->ptr)) + { + chunk_free(hash); + return FALSE; + } + return TRUE; } return get_hash(this, chunk, NULL); } diff --git a/src/libstrongswan/plugins/wolfssl/wolfssl_hasher.c b/src/libstrongswan/plugins/wolfssl/wolfssl_hasher.c index 5e9d851c2..cabd06d87 100644 --- a/src/libstrongswan/plugins/wolfssl/wolfssl_hasher.c +++ b/src/libstrongswan/plugins/wolfssl/wolfssl_hasher.c @@ -84,7 +84,12 @@ METHOD(hasher_t, allocate_hash, bool, if (hash) { *hash = chunk_alloc(get_hash_size(this)); - return get_hash(this, chunk, hash->ptr); + if (!get_hash(this, chunk, hash->ptr)) + { + chunk_free(hash); + return FALSE; + } + return TRUE; } return get_hash(this, chunk, NULL); }