Skip to content

Correctly not try to call handshake() when engine is already closed. - #6090

Closed
normanmaurer wants to merge 1 commit into
4.1from
ssl_shutdown
Closed

normanmaurer wants to merge 1 commit into
4.1from
ssl_shutdown

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

Motivation:

We need to ensure we not call handshake() when the engine is already closed. Beside this our implementation of isOutboundDone() was not correct as it not took the pending data in the outbound buffer into acount (which may be also generated as part of an ssl alert). Beside this we also called SSL_shutdown(...) while we were still in init state which will produce an error and so noise in the log with openssl later versions.

This is also in some extend related to #5931 .

Modifications:

  • Ensure we not call handshake() when already closed
  • Correctly implement isOutboundDone()
  • Not call SSL_shutdown(...) when still in init state
  • Added test-cases

Result:

More correct behaviour of our openssl SSLEngine implementation.

@normanmaurer

Copy link
Copy Markdown
Member Author

@Scottmitch @nmittler @trustin PTAL

Also @johnou FYI

@nmittler

nmittler commented Dec 1, 2016

Copy link
Copy Markdown
Member

@normanmaurer changes LGTM

@davidben @kruton FYI ... We'll want to port this change over to conscrypt

@johnou

johnou commented Dec 1, 2016

Copy link
Copy Markdown
Contributor

Lgtm, many issues left before next 4.0?

@normanmaurer

Copy link
Copy Markdown
Member Author

@johnou nope... just wrapping up stuff


if (engineClosed) {
return NEED_UNWRAP_CLOSED;
if (outboundClosed) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

consider adding a comment here that we explicitly want to use outboundClosed instead of calling outboundClosed() because we want to drain any bytes if they are available.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1

}
if (engineClosed) {
// Check if the inbound is considered to be closed if so let us try to wrap again.
if (isInboundDone()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use isInboundDone directly to be consistent with the wrap code?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1

public final synchronized boolean isOutboundDone() {
return isOutboundDone;
// check if there is anything left in the outbound buffer.
return outboundClosed && (isDestroyed() || SSL.pendingWrittenBytesInBIO(networkBIO) == 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is checking isDestroyed() necessary? Setting the destroyed variable to "true" also means that outboundClosed will be set to true.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes as we only want to call SSL.pendingWrittenBytesInBIO(...) if the engine was not destroyed before. If we not check this it may result in a segfault as the SSL pointer was freed before.

getEngineStatus(), mayFinishHandshake(status != FINISHED ? getHandshakeStatus() : status)
, bytesConsumed, bytesProduced);
return new SSLEngineResult(resultStatus, mayFinishHandshake(status != FINISHED ? getHandshakeStatus() : status),
bytesConsumed, bytesProduced);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: line up these args with the args above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1

@normanmaurer normanmaurer self-assigned this Dec 2, 2016
@normanmaurer normanmaurer added this to the 4.0.43.Final milestone Dec 2, 2016
@normanmaurer

Copy link
Copy Markdown
Member Author

@Scottmitch PTAL again

@@ -367,7 +366,7 @@ public final synchronized void shutdown() {
ssl = networkBIO = 0;

// internal errors can cause shutdown without marking the engine closed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this comment is obsolete now right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes let me remove it

return isOutboundDone;
// Check if there is anything left in the outbound buffer.
// We need to ensure we only call SSL.pendingWrittenBytesInBIO(...) if the engine was not destroyed yet.
return outboundClosed && (isDestroyed() || SSL.pendingWrittenBytesInBIO(networkBIO) == 0);

@Scottmitch Scottmitch Dec 2, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

brain fart ... I see isDestroyed() is necessary now. Below is an alternative to avoid a volatile read:

return outboundClosed && (networkBIO == 0 || SSL.pendingWrittenBytesInBIO(networkBIO) == 0)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok can do this ... anything else?

Motivation:

We need to ensure we not call handshake() when the engine is already closed. Beside this our implementation of isOutboundDone() was not correct as it not took the pending data in the outbound buffer into acount (which may be also generated as part of an ssl alert). Beside this we also called SSL_shutdown(...) while we were still in init state which will produce an error and so noise in the log with openssl later versions.

This is also in some extend related to #5931 .

Modifications:

- Ensure we not call handshake() when already closed
- Correctly implement isOutboundDone()
- Not call SSL_shutdown(...) when still in init state
- Added test-cases

Result:

More correct behaviour of our openssl SSLEngine implementation.
@normanmaurer

Copy link
Copy Markdown
Member Author

@Scottmitch all addressed... PTAL again

@Scottmitch Scottmitch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

1 tiny nit ... lgtm

// Check to make sure the engine has not been closed
if (isDestroyed()) {
// Check to make sure the engine has not been closed and the inbound was not marked as done.
if (isDestroyed() || (handshakeState == HandshakeState.NOT_STARTED && isInboundDone())) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: you have parens here but not on line 590 ... can we be consistent? strictly speaking they aren't necessary but as long as we are consistent its all good.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added them... easier to read.

@normanmaurer

Copy link
Copy Markdown
Member Author

Cherry-picked into 4.1 (7ce0f35) and 4.0 (78fb4ec)

@johnou

johnou commented Feb 20, 2017

Copy link
Copy Markdown
Contributor

@normanmaurer might be that we need similar guards in place for wrap too? I've only seen this happen once in production so if I deploy a canary build we might be waiting awhile..

javax.net.ssl.SSLException: SSLEngine closed already
	at io.netty.handler.ssl.SslHandler.wrap(...)(Unknown Source) ~[netty-handler-4.1.8.Final.jar:?]

@Scottmitch

Copy link
Copy Markdown
Member

@johnou - Would you mind filing another issue and provide any more details that you may have?

@johnou

johnou commented Feb 20, 2017

Copy link
Copy Markdown
Contributor

@Scottmitch sounds good.

@johnou

johnou commented Feb 20, 2017

Copy link
Copy Markdown
Contributor

@Scottmitch #6419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants