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: emit errors on close whilst async action #1702

Closed
wants to merge 3 commits into from

Conversation

indutny
Copy link
Member

@indutny indutny commented May 14, 2015

When loading session, OCSP response, SNI, always check that the
self._handle is present. If it is not - the socket was closed - and we
should emit the error instead of throwing an uncaught exception.

Fix: nodejs/node-v0.x-archive#8780
Fix: #1696

@indutny
Copy link
Member Author

indutny commented May 14, 2015

cc @iojs/crypto

@migounette
Copy link

@indutny You fixed at the JS part by checking the _handle, but what I found during an intensive play with TLS is that the _encout was NULL (a C++ level). It is a kind of SSL Close issue race condition (for me), but I need to change the test case because it's very difficult for me to reproduce.

Thanks for the fix

@mscdex mscdex added the tls Issues and PRs related to the tls subsystem. label May 14, 2015
@indutny
Copy link
Member Author

indutny commented May 18, 2015

@migounette @EricTheOne: I have updated the PR, please take a look

@EricTheOne
Copy link

@indutny thanks, applied both patches in order on v2.0.2, getting a segfault elsewhere (maybe it's progress):

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000d18d00 in node::StreamBase::EmitData(long, v8::Local<v8::Object>, v8::Local<v8::Object>) ()
(gdb) bt
#0  0x0000000000d18d00 in node::StreamBase::EmitData(long, v8::Local<v8::Object>, v8::Local<v8::Object>) ()
#1  0x0000000000d3cc8a in node::TLSWrap::OnReadSelf(long, uv_buf_t const*, uv_handle_type, void*) ()
#2  0x0000000000d40d19 in node::TLSWrap::OnReadImpl(long, uv_buf_t const*, uv_handle_type, void*) ()
#3  0x000000000084fa02 in v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) ()
#4  0x0000000000876c1b in v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) ()

@indutny
Copy link
Member Author

indutny commented May 19, 2015

@EricTheOne pushed one more commit, PTAL

@EricTheOne
Copy link

@indutny I've applied all three commits in order, on v2.0.2. Getting a different segfault now:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000030f1490 in ?? ()
(gdb) bt
#0  0x00000000030f1490 in ?? ()
#1  0x0000000000d40d29 in node::TLSWrap::OnReadImpl(long, uv_buf_t const*, uv_handle_type, void*) ()
#2  0x000000000084fa02 in v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) ()
#3  0x0000000000876c1b in v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) ()

@indutny
Copy link
Member Author

indutny commented May 22, 2015

@EricTheOne looks like I don't fully understand problem yet. Any way I can reproduce it locally?

@indutny
Copy link
Member Author

indutny commented May 29, 2015

Just a heads up, @evanlucas discovered a crash in a test: https://gist.github.com/evanlucas/6ab0a62ebe7ffa5d0ddf

It is reproducible after patching the test suite:

diff --git a/test/parallel/test-tls-js-stream.js b/test/parallel/test-tls-js-stream.js
index e156f44..292bd4f 100644
--- a/test/parallel/test-tls-js-stream.js
+++ b/test/parallel/test-tls-js-stream.js
@@ -61,6 +61,7 @@ var server = tls.createServer({

     socket.end('hello');
     socket.resume();
+    socket.destroy();
   });

   socket.once('close', function() {

Going to investigate it in detail later.

@indutny indutny force-pushed the fix/gh-node-8780 branch from a9e9b9a to ad3a799 Compare May 29, 2015 18:28
@indutny
Copy link
Member Author

indutny commented May 29, 2015

@evanlucas and it is actually fixed by this commit ad3a799

@indutny
Copy link
Member Author

indutny commented May 29, 2015

Erm, not by commit, rather by this PR.

@evanlucas
Copy link
Contributor

yay!

@indutny
Copy link
Member Author

indutny commented May 29, 2015

Which is kind of sad for the @EricTheOne's thing...

@indutny
Copy link
Member Author

indutny commented May 29, 2015

indutny added 3 commits June 1, 2015 18:34
When loading session, OCSP response, SNI, always check that the
`self._handle` is present. If it is not - the socket was closed - and we
should emit the error instead of throwing an uncaught exception.

Fix: nodejs/node-v0.x-archive#8780
Fix: nodejs#1696
* Destroy `SSL*` and friends on a next tick to make sure that we are not
  doing it in one of the OpenSSL callbacks
* Add more checks to the C++ methods that might be invoked during
  destructor's pending queue cleanup

Fix: nodejs/node-v0.x-archive#8780
Fix: nodejs#1696
@indutny indutny force-pushed the fix/gh-node-8780 branch from ad3a799 to 3533378 Compare June 1, 2015 16:38
@indutny
Copy link
Member Author

indutny commented Jun 1, 2015

@migounette @EricTheOne any news? ;)

@indutny
Copy link
Member Author

indutny commented Jun 1, 2015

@indutny
Copy link
Member Author

indutny commented Jun 1, 2015

CI is green.

@trevnorris
Copy link
Contributor

@indutny going to merge this?

@indutny
Copy link
Member Author

indutny commented Jun 4, 2015

Yeah, let's land it. @trevnorris LGTY?

@trevnorris
Copy link
Contributor

LGTM

indutny added a commit that referenced this pull request Jun 4, 2015
When loading session, OCSP response, SNI, always check that the
`self._handle` is present. If it is not - the socket was closed - and we
should emit the error instead of throwing an uncaught exception.

Fix: nodejs/node-v0.x-archive#8780
Fix: #1696
PR-URL: #1702
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
indutny added a commit that referenced this pull request Jun 4, 2015
* Destroy `SSL*` and friends on a next tick to make sure that we are not
  doing it in one of the OpenSSL callbacks
* Add more checks to the C++ methods that might be invoked during
  destructor's pending queue cleanup

Fix: nodejs/node-v0.x-archive#8780
Fix: #1696
PR-URL: #1702
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
indutny added a commit that referenced this pull request Jun 4, 2015
PR-URL: #1702
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@indutny
Copy link
Member Author

indutny commented Jun 4, 2015

Landed in 5795e83, 75930bb, and 59d9734. Thank you!

@rvagg rvagg mentioned this pull request Jun 11, 2015
@indutny indutny deleted the fix/gh-node-8780 branch June 21, 2015 01:49
@@ -747,6 +750,10 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) {
void TLSWrap::EnableSessionCallbacks(
const FunctionCallbackInfo<Value>& args) {
TLSWrap* wrap = Unwrap<TLSWrap>(args.Holder());
if (wrap->ssl_ == nullptr) {
return wrap->env()->ThrowTypeError(
"EnableSessionCallbacks after destroySSL");
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if the error message sounds like a TypeError

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think it is worth submitting a follow-up fix PR. ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

@indutny I feel that the default types of Errors is not enough. We should at least have something like ValueError. What do you think?

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'd say it might be too expensive to create them in C++, unless I'm missing something obvious.

Copy link
Contributor

Choose a reason for hiding this comment

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

I locally created a patch which fixes all the inconsistent error messages like this, in the js files. And I had an internal/errors.js file which defined ValueError. Looks like that solves most of the wrong data error messages. But I am not sure if it is worthy introducing a new Error type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I don't think anyone really checks the type of error. It kind of serves only an informational purpose when it pops up in the stderr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tls Issues and PRs related to the tls subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

segfault in TLS Segfault node v0.11.14 related to TLS
8 participants