-
Notifications
You must be signed in to change notification settings - Fork 84
SSLSocket write and non-block fixes #354
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
Open
kares
wants to merge
7
commits into
master
Choose a base branch
from
gem-error-minimal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+573
−29
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfcc6d1
[fix] SSL write loss on non-blocking partial flush (#242)
jsvd f7fd8a7
[fix] read_nonblock(exception: false) raising SSLErrorWaitReadable
kares 5a75fdb
[test] introduce a helper for loading in JUnit
kares b625d0e
[test] introduce SSLSocketTest (unit test)
kares 736ac5f
[build] bump jruby to 9.2.1.0 due load_ext
kares 5f09ae8
[fix] return wait_writable on non-blocking reads
kares f31f323
[refactor] share empty read-only byte-buffer
kares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,14 +141,15 @@ private static CallSite callSite(final CallSite[] sites, final CallSiteIndex ind | |
| return sites[ index.ordinal() ]; | ||
| } | ||
|
|
||
| private SSLContext sslContext; | ||
| private static final ByteBuffer EMPTY_DATA = ByteBuffer.allocate(0).asReadOnlyBuffer(); | ||
|
|
||
| SSLContext sslContext; | ||
| private SSLEngine engine; | ||
| private RubyIO io; | ||
|
|
||
| private ByteBuffer appReadData; | ||
| private ByteBuffer netReadData; | ||
| private ByteBuffer netWriteData; | ||
| private final ByteBuffer dummy = ByteBuffer.allocate(0); // could be static | ||
| ByteBuffer appReadData; | ||
| ByteBuffer netReadData; | ||
| ByteBuffer netWriteData; | ||
|
|
||
| private boolean initialHandshake = false; | ||
| private transient long initializeTime; | ||
|
|
@@ -209,7 +210,7 @@ private IRubyObject fallback_set_io_nonblock_checked(ThreadContext context, Ruby | |
|
|
||
| private static final String SESSION_SOCKET_ID = "socket_id"; | ||
|
|
||
| private SSLEngine ossl_ssl_setup(final ThreadContext context, final boolean server) { | ||
| SSLEngine ossl_ssl_setup(final ThreadContext context, final boolean server) { | ||
| SSLEngine engine = this.engine; | ||
| if ( engine != null ) return engine; | ||
|
|
||
|
|
@@ -553,10 +554,6 @@ private static void writeWouldBlock(final Ruby runtime, final boolean exception, | |
| result[0] = WRITE_WOULD_BLOCK_RESULT; | ||
| } | ||
|
|
||
| private void doHandshake(final boolean blocking) throws IOException { | ||
| doHandshake(blocking, true); | ||
| } | ||
|
|
||
| // might return :wait_readable | :wait_writable in case (true, false) | ||
| private IRubyObject doHandshake(final boolean blocking, final boolean exception) throws IOException { | ||
| while (true) { | ||
|
|
@@ -578,7 +575,7 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception) | |
| doTasks(); | ||
| break; | ||
| case NEED_UNWRAP: | ||
| if (readAndUnwrap(blocking) == -1 && handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED) { | ||
| if (readAndUnwrap(blocking, exception) == -1 && handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED) { | ||
| throw new SSLHandshakeException("Socket closed"); | ||
| } | ||
| // during initialHandshake, calling readAndUnwrap that results UNDERFLOW does not mean writable. | ||
|
|
@@ -614,7 +611,7 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception) | |
|
|
||
| private void doWrap(final boolean blocking) throws IOException { | ||
| netWriteData.clear(); | ||
| SSLEngineResult result = engine.wrap(dummy, netWriteData); | ||
| SSLEngineResult result = engine.wrap(EMPTY_DATA.duplicate(), netWriteData); | ||
| netWriteData.flip(); | ||
| handshakeStatus = result.getHandshakeStatus(); | ||
| status = result.getStatus(); | ||
|
|
@@ -689,7 +686,9 @@ public int write(ByteBuffer src, boolean blocking) throws SSLException, IOExcept | |
| if ( netWriteData.hasRemaining() ) { | ||
| flushData(blocking); | ||
| } | ||
| netWriteData.clear(); | ||
| // compact() to preserve encrypted bytes flushData could not send (non-blocking partial write) | ||
| // clear() would discard them, corrupting the TLS record stream: | ||
| netWriteData.compact(); | ||
| final SSLEngineResult result = engine.wrap(src, netWriteData); | ||
| if ( result.getStatus() == SSLEngineResult.Status.CLOSED ) { | ||
| throw getRuntime().newIOError("closed SSL engine"); | ||
|
|
@@ -704,11 +703,15 @@ public int write(ByteBuffer src, boolean blocking) throws SSLException, IOExcept | |
| } | ||
|
|
||
| public int read(final ByteBuffer dst, final boolean blocking) throws IOException { | ||
| return read(dst, blocking, true); | ||
| } | ||
|
|
||
| private int read(final ByteBuffer dst, final boolean blocking, final boolean exception) throws IOException { | ||
| if ( initialHandshake ) return 0; | ||
| if ( engine.isInboundDone() ) return -1; | ||
|
|
||
| if ( ! appReadData.hasRemaining() ) { | ||
| int appBytesProduced = readAndUnwrap(blocking); | ||
| int appBytesProduced = readAndUnwrap(blocking, exception); | ||
| if (appBytesProduced == -1 || appBytesProduced == 0) { | ||
| return appBytesProduced; | ||
| } | ||
|
|
@@ -719,17 +722,16 @@ public int read(final ByteBuffer dst, final boolean blocking) throws IOException | |
| return limit; | ||
| } | ||
|
|
||
| private int readAndUnwrap(final boolean blocking) throws IOException { | ||
| private int readAndUnwrap(final boolean blocking, final boolean exception) throws IOException { | ||
| final int bytesRead = socketChannelImpl().read(netReadData); | ||
| if ( bytesRead == -1 ) { | ||
| if ( ! netReadData.hasRemaining() || | ||
| ( status == SSLEngineResult.Status.BUFFER_UNDERFLOW ) ) { | ||
| closeInbound(); | ||
| return -1; | ||
| } | ||
| // inbound channel has been already closed but closeInbound() must | ||
| // be defered till the last engine.unwrap() call. | ||
| // peerNetData could not be empty. | ||
| // inbound channel has been already closed but closeInbound() must be defered till | ||
| // the last engine.unwrap() call; peerNetData could not be empty | ||
| } | ||
| appReadData.clear(); | ||
| netReadData.flip(); | ||
|
|
@@ -768,7 +770,7 @@ private int readAndUnwrap(final boolean blocking) throws IOException { | |
| handshakeStatus == SSLEngineResult.HandshakeStatus.NEED_TASK || | ||
| handshakeStatus == SSLEngineResult.HandshakeStatus.NEED_WRAP || | ||
| handshakeStatus == SSLEngineResult.HandshakeStatus.FINISHED ) ) { | ||
| doHandshake(blocking); | ||
| doHandshake(blocking, exception); | ||
| } | ||
| return appReadData.remaining(); | ||
| } | ||
|
|
@@ -793,7 +795,7 @@ private void doShutdown() throws IOException { | |
| } | ||
| netWriteData.clear(); | ||
| try { | ||
| engine.wrap(dummy, netWriteData); // send close (after sslEngine.closeOutbound) | ||
| engine.wrap(EMPTY_DATA.duplicate(), netWriteData); // send close (after sslEngine.closeOutbound) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here, why duplicate? |
||
| } | ||
| catch (SSLException e) { | ||
| debug(getRuntime(), "SSLSocket.doShutdown", e); | ||
|
|
@@ -808,10 +810,10 @@ private void doShutdown() throws IOException { | |
| } | ||
|
|
||
| /** | ||
| * @return the (@link RubyString} buffer or :wait_readable / :wait_writeable {@link RubySymbol} | ||
| * @return the {@link RubyString} buffer or :wait_readable / :wait_writeable {@link RubySymbol} | ||
| */ | ||
| private IRubyObject sysreadImpl(final ThreadContext context, final IRubyObject len, final IRubyObject buff, | ||
| final boolean blocking, final boolean exception) { | ||
| private IRubyObject sysreadImpl(final ThreadContext context, | ||
| final IRubyObject len, final IRubyObject buff, final boolean blocking, final boolean exception) { | ||
| final Ruby runtime = context.runtime; | ||
|
|
||
| final int length = RubyNumeric.fix2int(len); | ||
|
|
@@ -831,6 +833,14 @@ private IRubyObject sysreadImpl(final ThreadContext context, final IRubyObject l | |
| } | ||
|
|
||
| try { | ||
| // Flush pending write data before reading (after write_nonblock encrypted bytes may still be buffered) | ||
| if ( engine != null && netWriteData.hasRemaining() ) { | ||
| if ( flushData(blocking) && ! blocking ) { | ||
| if ( exception ) throw newSSLErrorWaitWritable(runtime, "write would block"); | ||
| return runtime.newSymbol("wait_writable"); | ||
| } | ||
| } | ||
|
|
||
| // So we need to make sure to only block when there is no data left to process | ||
| if ( engine == null || ! ( appReadData.hasRemaining() || netReadData.position() > 0 ) ) { | ||
| final Object ex = waitSelect(SelectionKey.OP_READ, blocking, exception); | ||
|
|
@@ -839,12 +849,12 @@ private IRubyObject sysreadImpl(final ThreadContext context, final IRubyObject l | |
|
|
||
| final ByteBuffer dst = ByteBuffer.allocate(length); | ||
| int read = -1; | ||
| // ensure >0 bytes read; sysread is blocking read. | ||
| // ensure > 0 bytes read; sysread is blocking read | ||
| while ( read <= 0 ) { | ||
| if ( engine == null ) { | ||
| read = socketChannelImpl().read(dst); | ||
| } else { | ||
| read = read(dst, blocking); | ||
| read = read(dst, blocking, exception); | ||
| } | ||
|
|
||
| if ( read == -1 ) { | ||
|
|
@@ -1226,7 +1236,7 @@ public IRubyObject ssl_version(ThreadContext context) { | |
| return context.runtime.newString( engine.getSession().getProtocol() ); | ||
| } | ||
|
|
||
| private transient SocketChannelImpl socketChannel; | ||
| transient SocketChannelImpl socketChannel; | ||
|
|
||
| private SocketChannelImpl socketChannelImpl() { | ||
| if ( socketChannel != null ) return socketChannel; | ||
|
|
@@ -1241,7 +1251,7 @@ private SocketChannelImpl socketChannelImpl() { | |
| throw new IllegalStateException("unknow channel impl: " + channel + " of type " + channel.getClass().getName()); | ||
| } | ||
|
|
||
| private interface SocketChannelImpl { | ||
| interface SocketChannelImpl { | ||
|
|
||
| boolean isOpen() ; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package org.jruby.ext.openssl; | ||
|
|
||
| import org.jruby.Ruby; | ||
| import org.jruby.runtime.ThreadContext; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| abstract class OpenSSLHelper { | ||
|
|
||
| protected Ruby runtime; | ||
|
|
||
| void setUpRuntime() throws ClassNotFoundException { | ||
| runtime = Ruby.newInstance(); | ||
| loadOpenSSL(runtime); | ||
| } | ||
|
|
||
| void tearDownRuntime() { | ||
| if (runtime != null) runtime.tearDown(false); | ||
| } | ||
|
|
||
| protected void loadOpenSSL(final Ruby runtime) throws ClassNotFoundException { | ||
| // prepend lib/ so openssl.rb + jopenssl/ are loaded instead of bundled OpenSSL in jruby-stdlib | ||
| final String libDir = new File("lib").getAbsolutePath(); | ||
| runtime.evalScriptlet("$LOAD_PATH.unshift '" + libDir + "'"); | ||
| runtime.evalScriptlet("require 'openssl'"); | ||
|
|
||
| // sanity: verify openssl was loaded from the project, not jruby-stdlib : | ||
| final String versionFile = new File(libDir, "jopenssl/version.rb").getAbsolutePath(); | ||
| final String expectedVersion = runtime.evalScriptlet( | ||
| "File.read('" + versionFile + "').match( /.*\\sVERSION\\s*=\\s*['\"](.*)['\"]/ )[1]") | ||
| .toString(); | ||
| final String loadedVersion = runtime.evalScriptlet("JOpenSSL::VERSION").toString(); | ||
| assertEquals("OpenSSL must be loaded from project (got version " + loadedVersion + | ||
| "), not from jruby-stdlib", expectedVersion, loadedVersion); | ||
|
|
||
| // Also check the Java extension classes were resolved from the project, not jruby-stdlib : | ||
| final String classOrigin = runtime.getJRubyClassLoader() | ||
| .loadClass("org.jruby.ext.openssl.OpenSSL") | ||
| .getProtectionDomain().getCodeSource().getLocation().toString(); | ||
| assertTrue("OpenSSL.class (via JRuby classloader) come from project, got: " + classOrigin, | ||
| classOrigin.endsWith("/pkg/classes/")); | ||
| } | ||
|
|
||
| // HELPERS | ||
|
|
||
| public ThreadContext currentContext() { | ||
| return runtime.getCurrentContext(); | ||
| } | ||
|
|
||
| public static String readResource(final String resource) { | ||
| int n; | ||
| try (InputStream in = SSLSocketTest.class.getResourceAsStream(resource)) { | ||
| if (in == null) throw new IllegalArgumentException(resource + " not found on classpath"); | ||
|
|
||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| byte[] buf = new byte[8192]; | ||
| while ((n = in.read(buf)) != -1) out.write(buf, 0, n); | ||
| return new String(out.toByteArray(), StandardCharsets.UTF_8); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException("failed to load" + resource, e); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to duplicate EMPTY_DATA? It is already zero-length and marked as read-only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't be, except
engine.wrapin theory does setlimitalthough likely not on an empty buffer.