Free OpenSSL's buffers only where CVE-2024-4741 is fixed

Before 3.0.14, 3.1.6, 3.2.2 and 3.3.1, SSL_free_buffers() could free the
read buffer while a record whose header had been read still waited for its
body. A client can bring that about before its connection is parked, since
writing a response peeks at the socket. Check the OpenSSL that runs, not
the one built against.
This commit is contained in:
yhirose committed 2026-11-21 13:46:34 -05:00
1 parent 7e32283df3
commit 114e3d0e46
2 files changed
+39 -3

No files matched your search

+20 -3
View File
@@ -20150,6 +20150,21 @@ inline bool SSLServer::is_valid() const {
return ctx_ != nullptr && Server::is_valid();
}
namespace detail {
// Before these, SSL_free_buffers() could free a buffer that a record whose
// body has not arrived yet still uses (CVE-2024-4741). `version` is
// OpenSSL_version_num().
inline bool ssl_free_buffers_is_safe(unsigned long version) {
if (version >= 0x30400000UL) { return true; }
if (version >= 0x30300000UL) { return version >= 0x30300010UL; } // 3.3.1
if (version >= 0x30200000UL) { return version >= 0x30200020UL; } // 3.2.2
if (version >= 0x30100000UL) { return version >= 0x30100060UL; } // 3.1.6
return version >= 0x300000e0UL; // 3.0.14
}
} // namespace detail
// A connection that keep-alive parking keeps for SSLServer. The worker that
// first resumes it runs the handshake, so one that has not sent its
// ClientHello yet waits in the event loop like an idle connection.
@@ -20180,9 +20195,11 @@ struct SSLServer::TlsConnection final : Server::KeepAliveConnection {
void release_buffers() override {
#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \
!defined(LIBRESSL_VERSION_NUMBER)
// The record buffers are most of an idle session's memory. It keeps them
// when a record is still pending, and makes new ones on the next read.
SSL_free_buffers(static_cast<SSL *>(session));
// The record buffers are most of an idle session's memory, and the next
// read makes new ones. The library that runs decides whether that is safe.
static const auto safe =
detail::ssl_free_buffers_is_safe(OpenSSL_version_num());
if (safe) { SSL_free_buffers(static_cast<SSL *>(session)); }
#endif
}