Skip to content

Commit d5b2135

Browse files
davidbentargos
authored andcommitted
tls: fix malloc mismatch in SSL_set_tlsext_status_ocsp_resp call
SSL_set_tlsext_status_ocsp_resp expects the data to be allocated with OPENSSL_malloc, not libc malloc, so use OpenSSLMalloc. Additionally, though OpenSSL doesn't type-check due to it being a macro, the function is documented to take an unsigned char pointer: https://www.openssl.org/docs/man1.1.0/ssl/SSL_set_tlsext_status_ocsp_resp.html (By default, OPENSSL_malloc is the same as libc malloc, but it is possible to customize this.) PR-URL: #25706 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ae19f94 commit d5b2135

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/node_crypto.cc

+10-9
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ bool EntropySource(unsigned char* buffer, size_t length) {
324324
}
325325

326326

327+
template <typename T>
328+
static T* MallocOpenSSL(size_t count) {
329+
void* mem = OPENSSL_malloc(MultiplyWithOverflowCheck(count, sizeof(T)));
330+
CHECK_IMPLIES(mem == nullptr, count == 0);
331+
return static_cast<T*>(mem);
332+
}
333+
334+
327335
void SecureContext::Initialize(Environment* env, Local<Object> target) {
328336
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
329337
t->InstanceTemplate()->SetInternalFieldCount(1);
@@ -2446,11 +2454,11 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
24462454
size_t len = Buffer::Length(obj);
24472455

24482456
// OpenSSL takes control of the pointer after accepting it
2449-
char* data = node::Malloc(len);
2457+
unsigned char* data = MallocOpenSSL<unsigned char>(len);
24502458
memcpy(data, resp, len);
24512459

24522460
if (!SSL_set_tlsext_status_ocsp_resp(s, data, len))
2453-
free(data);
2461+
OPENSSL_free(data);
24542462
w->ocsp_response_.Reset();
24552463

24562464
return SSL_TLSEXT_ERR_OK;
@@ -2672,13 +2680,6 @@ static bool IsSupportedAuthenticatedMode(const EVP_CIPHER_CTX* ctx) {
26722680
return IsSupportedAuthenticatedMode(cipher);
26732681
}
26742682

2675-
template <typename T>
2676-
static T* MallocOpenSSL(size_t count) {
2677-
void* mem = OPENSSL_malloc(MultiplyWithOverflowCheck(count, sizeof(T)));
2678-
CHECK_IMPLIES(mem == nullptr, count == 0);
2679-
return static_cast<T*>(mem);
2680-
}
2681-
26822683
enum class ParsePublicKeyResult {
26832684
kParsePublicOk,
26842685
kParsePublicNotRecognized,

0 commit comments

Comments
 (0)