From 1a9b97a29e94b3b125c52e2434c4511b6dd88de2 Mon Sep 17 00:00:00 2001 From: Dhanraj E Date: Sun, 15 Nov 2026 11:20:01 +0530 Subject: [PATCH] Fixed heap buffer overflow decrypting server counter-challenge In the classic protocol key exchange, AuthenticateAgent() allocated the buffer for the decrypted counter-challenge with the length of the ciphertext the server sent: decrypted_cchall = xmalloc(encrypted_len); RSA_private_decrypt(encrypted_len, in, decrypted_cchall, PRIVKEY, ...) RSA_private_decrypt() can write up to RSA_size(PRIVKEY) bytes, and its documentation requires an output buffer that large; the ciphertext length is whatever the server chose to send. With OpenSSL 3.2 and later, PKCS#1 v1.5 decryption uses implicit rejection and returns a synthetic message of pseudo-random length instead of failing on bad padding, so a server that answers with a 1 byte counter-challenge makes the agent write up to about 240 bytes past a 1 byte allocation (2048-bit key). The digest that follows also read nonce_len bytes from that buffer no matter how much had been decrypted. Size the buffer with RSA_size(PRIVKEY), refuse a counter-challenge that decrypts to fewer bytes than the nonce being hashed, and free the buffer on the error paths. Changelog: Title Signed-off-by: Dhanraj E --- libcfnet/client_protocol.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/libcfnet/client_protocol.c b/libcfnet/client_protocol.c index 110c91cd1b8..41f82ebf7a4 100644 --- a/libcfnet/client_protocol.c +++ b/libcfnet/client_protocol.c @@ -398,13 +398,34 @@ bool AuthenticateAgent(AgentConnection *conn, bool trust_key) return false; } - decrypted_cchall = xmalloc(encrypted_len); - - if (RSA_private_decrypt(encrypted_len, in, (unsigned char *) decrypted_cchall, PRIVKEY, RSA_PKCS1_PADDING) <= 0) + /* RSA_private_decrypt() can write up to RSA_size() bytes, however short + * the ciphertext the server sent us is. */ + decrypted_cchall = xmalloc(RSA_size(PRIVKEY)); + + const int decrypted_len = RSA_private_decrypt( + encrypted_len, + in, + (unsigned char *) decrypted_cchall, + PRIVKEY, + RSA_PKCS1_PADDING); + if (decrypted_len <= 0) { Log(LOG_LEVEL_ERR, "Private decrypt failed, abandoning. (RSA_private_decrypt: %s)", CryptoLastErrorString()); + free(decrypted_cchall); + RSA_free(server_pubkey); + return false; + } + + if (decrypted_len < nonce_len) + { + Log(LOG_LEVEL_ERR, + "Counter-challenge from server '%s' is too short (%d bytes, expected %d)", + conn->this_server, + decrypted_len, + nonce_len); + free(decrypted_cchall); RSA_free(server_pubkey); return false; }