Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various memory leaks #2375

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {

if (is_dlopen_error) {
Local<String> errmsg = OneByteString(env->isolate(), uv_dlerror(&lib));
uv_dlclose(&lib);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes it partially. The error paths below still leak though.

#ifdef _WIN32
// Windows needs to add the filename into the error message
errmsg = String::Concat(errmsg, args[1]->ToString(env->isolate()));
Expand All @@ -2133,10 +2134,12 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
}

if (mp == nullptr) {
uv_dlclose(&lib);
env->ThrowError("Module did not self-register.");
return;
}
if (mp->nm_version != NODE_MODULE_VERSION) {
uv_dlclose(&lib);
char errmsg[1024];
snprintf(errmsg,
sizeof(errmsg),
Expand All @@ -2146,6 +2149,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
return;
}
if (mp->nm_flags & NM_F_BUILTIN) {
uv_dlclose(&lib);
env->ThrowError("Built-in module self-registered.");
return;
}
Expand All @@ -2162,6 +2166,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
} else if (mp->nm_register_func != nullptr) {
mp->nm_register_func(exports, module, mp->nm_priv);
} else {
uv_dlclose(&lib);
env->ThrowError("Module has no declared entry point.");
return;
}
Expand Down
25 changes: 21 additions & 4 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,12 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
return;

const int keylen = BN_num_bits(dh->p);
if (keylen < 1024)
if (keylen < 1024) {
DH_free(dh);
return env->ThrowError("DH parameter is less than 1024 bits");
else if (keylen < 2048)
} else if (keylen < 2048) {
fprintf(stderr, "WARNING: DH parameter is less than 2048 bits\n");
}

SSL_CTX_set_options(sc->ctx_, SSL_OP_SINGLE_DH_USE);
int r = SSL_CTX_set_tmp_dh(sc->ctx_, dh);
Expand Down Expand Up @@ -1295,6 +1297,7 @@ static bool SafeX509ExtPrint(BIO* out, X509_EXTENSION* ext) {
if (nval == NULL)
return false;
X509V3_EXT_val_prn(out, nval, 0, 0);
sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
}
}
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Expand Down Expand Up @@ -3930,6 +3933,8 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
fatal = false;

exit:
if (x509 != nullptr)
X509_free(x509);
if (pkey != nullptr)
EVP_PKEY_free(pkey);
if (bp != nullptr)
Expand Down Expand Up @@ -4565,8 +4570,12 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
if (priv == nullptr)
return env->ThrowError("Failed to convert Buffer to BN");

if (!EC_KEY_set_private_key(ecdh->key_, priv))
int result = EC_KEY_set_private_key(ecdh->key_, priv);
BN_free(priv);

if (!result) {
return env->ThrowError("Failed to convert BN to a private key");
}
}


Expand Down Expand Up @@ -4615,6 +4624,7 @@ class PBKDF2Request : public AsyncWrap {
}

~PBKDF2Request() override {
release();
persistent().Reset();
}

Expand Down Expand Up @@ -4656,10 +4666,15 @@ class PBKDF2Request : public AsyncWrap {

inline void release() {
free(pass_);
pass_ = nullptr;
passlen_ = 0;

free(salt_);
salt_ = nullptr;
saltlen_ = 0;

free(key_);
key_ = nullptr;
keylen_ = 0;
}

Expand Down Expand Up @@ -4730,7 +4745,6 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) {
Local<Value> argv[2];
EIO_PBKDF2After(req, argv);
req->MakeCallback(env->ondone_string(), ARRAY_SIZE(argv), argv);
req->release();
delete req;
}

Expand Down Expand Up @@ -4841,6 +4855,9 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
Local<Value> argv[2];
EIO_PBKDF2(req);
EIO_PBKDF2After(req, argv);

delete req;

if (argv[0]->IsObject())
env->isolate()->ThrowException(argv[0]);
else
Expand Down