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

8274524: SSLSocket.close() hangs if it is called during the ssl handshake #5760

Closed
wants to merge 4 commits into from

Conversation

alexeybakhtin
Copy link
Contributor

@alexeybakhtin alexeybakhtin commented Sep 29, 2021

Please review the patch for JDK-8274524

The fix just adds locks around InputStream read and skip operations to prevent concurrent read from socket.
sun/security/ssl jtreg tests passed
api/javax_net/ssl/SSLSocket/setUseClientMode jck test passed


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8274524: SSLSocket.close() hangs if it is called during the ssl handshake

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/5760/head:pull/5760
$ git checkout pull/5760

Update a local copy of the PR:
$ git checkout pull/5760
$ git pull https://git.openjdk.java.net/jdk pull/5760/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 5760

View PR using the GUI difftool:
$ git pr show -t 5760

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/5760.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 29, 2021

👋 Welcome back abakhtin! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 29, 2021
@openjdk
Copy link

openjdk bot commented Sep 29, 2021

@alexeybakhtin The following label will be automatically applied to this pull request:

  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the security security-dev@openjdk.org label Sep 29, 2021
@mlbridge
Copy link

mlbridge bot commented Sep 29, 2021

Webrevs

@cliveverghese
Copy link
Contributor

cliveverghese commented Sep 30, 2021

Hi @alexeybakhtin,

Thank you for the PR.

I have a question regarding the lock, Would adding a appInput.readLock.lock() in SSLSocketImpl:decode(ByteBuffer) resolve this issue?

@alexeybakhtin
Copy link
Contributor Author

alexeybakhtin commented Oct 1, 2021

Hi @alexeybakhtin,

Thank you for the PR.

I have a question regarding the lock, Would adding a appInput.readLock.lock() in SSLSocketImpl:decode(ByteBuffer) resolve this issue?

Hi Clive,
Thank you for your review. I think you are right. It can be simplified by using the existing appInput.readLock.
SSLSocketImpl: decode () already protected by appInput.readLock in readApplicationRecord () and waitForClose (), so I've added readLock in the readHandshakeRecord (). These are the only places were decode() is called.

@cliveverghese
Copy link
Contributor

cliveverghese commented Oct 1, 2021

Thank you @alexeybakhtin,

The change looks good to me, but I am not a reviewer.

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 29, 2021

@alexeybakhtin This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@openjdk openjdk bot removed the rfr Pull request is ready for review label Nov 3, 2021
@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 3, 2021
@alexeybakhtin
Copy link
Contributor Author

alexeybakhtin commented Nov 3, 2021

New jtreg test is added to reproduce the issue and verify it.
sun/security/ssl tests passed

@alexeybakhtin
Copy link
Contributor Author

alexeybakhtin commented Nov 10, 2021

Gentle ping.
JDK-8274736 was recently integrated to fix the concurrent read/close issue, but JDK-8274736 does not fix this particular issue with the hanged SSLSocket.close(). JTREG test was added to demonstrate the presence of the issue.

@XueleiFan
Copy link
Member

XueleiFan commented Nov 10, 2021

Did you have a chance to analysis the potential deadlock issues between handshake lock, read lock, write lock and socket lock? The locks used in SSLSocketImpl is complicated. If I remember correctly, there are potential deadlock issues if the read/write locks are used during handshaking. Did you have a dead lock stack for the bug? Alternatively, it might be a direction to improve the close() code.

@alexeybakhtin
Copy link
Contributor Author

alexeybakhtin commented Nov 10, 2021

Did you have a chance to analysis the potential deadlock issues between handshake lock, read lock, write lock and socket lock? The locks used in SSLSocketImpl is complicated. If I remember correctly, there are potential deadlock issues if the read/write locks are used during handshaking. Did you have a dead lock stack for the bug? Alternatively, it might be a direction to improve the close() code.

Hi @XueleiFan,

Thank you for review.
There are two stack traces in the bug report. The one is for SSLSocket.close() and another for SSLSocketImpl.readHandshakeRecord()
SSLSocket.close() is hanged on the InputStream.skip() operation. SSLSocket.close() and SSLSocketImpl.readHandshakeRecord() are called concurrently. SSLSocket.close() calls InputStream.skip() to clear input data from socket but concurrent SSLSocketImpl.readHandshakeRecord() reads some data from the socket. As result InputStream.skip() can not complete operation for previously calculated available bytes and hangs.

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 9, 2021

@alexeybakhtin This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 6, 2022

@alexeybakhtin This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Jan 6, 2022
@XueleiFan
Copy link
Member

XueleiFan commented Feb 9, 2022

With the patch, the readLock is placed in handshakeLock, and the handshake may require lock write lock. These nested lock is pretty risk and will run into deadlocks. That's the underlying reason why readHandshakeRecord() is not placed within read lock and write lock.

It looks like the problem is cause by close() method. Maybe, improving the close() implementation could be an alternative solution. For example, give up the skip operation if no lock can be granted.

@alexeybakhtin
Copy link
Contributor Author

alexeybakhtin commented Feb 10, 2022

Hi @XueleiFan thank you for explanation and suggestions.
We already have code to give up the skip operation if appInput.readLock can no be granted. Unfortunately, read operation during handshake is not protected by appInput.readLock. So it does not help in this case.
I just found, that the issue happens in case of zero SO_TIMEOUT only. I suggest to force non-zero SO_TIMEOUT during the skip(). New pull request is submitted: #7432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfr Pull request is ready for review security security-dev@openjdk.org
3 participants