Repository navigation
Description
Summary
When libssh2 is built against OpenSSL 3 (USE_OPENSSL_3), the shared-secret function
_libssh2_ecdh_gen_k() in src/openssl.c (named ssh2_ecdh_gen_k() on current master)
never releases three objects it allocates:
bn_ctx(BN_CTX_new()), which is allocated in the OpenSSL 3 branch but only freed in the#elsebranch,key_fromdata_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)),peer_key(created byEVP_PKEY_fromdata()).
Only group_name, out_shared_key and server_key_ctx are freed in clean_exit. Every
ecdh-sha2-nistp256/384/521 key exchange therefore leaks about 2 kB (three direct allocations
plus the provider-side key data owned by peer_key). The curve25519-sha256 path is not affected,
so the leak only shows up against servers that do not offer curve25519 - for example ProFTPD
mod_sftp - which is why it is easy to miss in tests against OpenSSH.
Long-running clients that open one session per operation grow without bound. In our case a device
polling an SFTP server once a minute (six sessions per check cycle) grew by roughly 0.8 MB per hour,
about 38 MB after two days.
Version
- libssh2 1.11.1 (tarball), crypto backend OpenSSL 3.5.7;
src/openssl.clines 4193-4374. - Current
mastersrc/openssl.c,ssh2_ecdh_gen_k(), has the sameclean_exitblock. - Client: libcurl 8.21.0 (
sftp://HEAD-style request,CURLOPT_NOBODY), onecurl_easy_perform
per session.
Steps to reproduce
- Run any SFTP server that only offers
ecdh-sha2-nistp*key exchange, e.g. ProFTPD 1.3.6 with
mod_sftpand an RSA/ECDSA host key (SSH-2.0-mod_sftp). - Build a small libcurl client against libssh2 1.11.1 + OpenSSL 3 and loop
curl_easy_perform()
onsftp://user:pass@host:port/some/filewithCURLOPT_NOBODY(the file does not need to exist). - Observe
RssAnonof the process growing by ~2 kB per iteration, or run it under
AddressSanitizer/LeakSanitizer.
LeakSanitizer output (10 sessions)
Direct leak of 1760 byte(s) in 10 object(s) allocated from:
#3 EVP_PKEY_CTX_new_id
#4 _libssh2_ecdh_gen_k src/openssl.c:4218
#5 ecdh_sha2_nistp src/kex.c:2047
#6 kex_method_ecdh_key_exchange src/kex.c:2468
#7 _libssh2_kex_exchange src/kex.c:4153
#8 session_startup src/session.c:787
#9 libssh2_session_handshake src/session.c:878
Direct leak of 1520 byte(s) in 10 object(s) allocated from:
#3 EVP_PKEY_new
#4 EVP_PKEY_fromdata
#5 _libssh2_ecdh_gen_k src/openssl.c:4263
Direct leak of 720 byte(s) in 10 object(s) allocated from:
#3 BN_CTX_new_ex
#4 _libssh2_ecdh_gen_k src/openssl.c:4214
Indirect leak of 15302 byte(s) in 371 object(s) (provider key data owned by the EVP_PKEY)
SUMMARY: AddressSanitizer: 19304 byte(s) leaked in 402 allocation(s).
With the same server but OpenSSH (curve25519-sha256) there are no leaks.
Fix
Free the three objects in clean_exit of the USE_OPENSSL_3 branch (against 1.11.1; the same
hunk applies to ssh2_ecdh_gen_k() on master):
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4353,6 +4353,15 @@ clean_exit:
if(server_key_ctx)
EVP_PKEY_CTX_free(server_key_ctx);
+
+ if(peer_key)
+ EVP_PKEY_free(peer_key);
+
+ if(key_fromdata_ctx)
+ EVP_PKEY_CTX_free(key_fromdata_ctx);
+
+ if(bn_ctx)
+ BN_CTX_free(bn_ctx);
#else
if(server_public_key_point)
EC_POINT_free(server_public_key_point);(bn_ctx is in fact unused in the OpenSSL 3 branch, so dropping its allocation there is an
equally valid fix.)
After the change LeakSanitizer reports no leaks and the process RSS stays flat over 200 sessions
against the same mod_sftp server.
Activity
vszakats commentedon Oct 28, 2026
Thanks for your report! Fixing in #2696 and #2697.
openssl: drop redundant allocation and leak in `ssh2_ecdh_gen_k()` wi…
openssl: fix two memory leaks in `ssh2_ecdh_gen_k()` with OpenSSL3+