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

tls_wrap: fix BIO leak on SSL error #1244

Closed
wants to merge 5 commits into from
Closed
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
13 changes: 9 additions & 4 deletions src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ TLSWrap::~TLSWrap() {
MakePending();

// And destroy
while (WriteItem* wi = pending_write_items_.PopFront())
delete wi;
InvokeQueued(UV_ECANCELED);

ClearError();
}
Expand Down Expand Up @@ -310,10 +309,12 @@ void TLSWrap::EncOut() {
write_req->Dispatched();

// Ignore errors, this should be already handled in js
if (err)
if (err) {
write_req->Dispose();
else
InvokeQueued(err);
} else {
NODE_COUNT_NET_BYTES_SENT(write_size_);
}
}


Expand All @@ -335,6 +336,9 @@ void TLSWrap::EncOutCb(WriteWrap* req_wrap, int status) {
// Commit
NodeBIO::FromBIO(wrap->enc_out_)->Read(nullptr, wrap->write_size_);

// Ensure that the progress will be made and `InvokeQueued` will be called.
wrap->ClearIn();

// Try writing more data
wrap->write_size_ = 0;
wrap->EncOut();
Expand Down Expand Up @@ -375,6 +379,7 @@ Local<Value> TLSWrap::GetSSLError(int status, int* err, const char** msg) {
*msg = buf;
}
static_cast<void>(BIO_reset(bio));
BIO_free_all(bio);
Copy link
Member

Choose a reason for hiding this comment

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

Does that mean that all the BIO_new + BIO_reset calls in src/node_crypto.cc are leaking memory as well?

Copy link
Member

Choose a reason for hiding this comment

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

After looking at deps/openssl/openssl/crypto/bio/bss_mem.c I'm guessing the answer is 'yes'. Instead of blindly adding BIO_free_all calls everywhere, I suggest introducing a small RAII class that fixes the issue once and for all.

Copy link
Member Author

Choose a reason for hiding this comment

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

I doubt we have any other places like this.


return scope.Escape(exception);
}
Expand Down