This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 729a5b228b Follow-up to PR #1157
729a5b228b is described below
commit 729a5b228bf4a102167854a02bb76800e9c0fd50
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Nov 17 18:39:54 2026 +0000
Follow-up to PR #1157
---
.../apache/tomcat/util/net/SecureNioChannel.java | 25 +++--
.../util/net/TestSecureNioChannelOverflow.java | 112 ++++++++++++++++++++-
2 files changed, 124 insertions(+), 13 deletions(-)
diff --git a/java/org/apache/tomcat/util/net/SecureNioChannel.java
b/java/org/apache/tomcat/util/net/SecureNioChannel.java
index 76c3ddd58a..70f9af004a 100644
--- a/java/org/apache/tomcat/util/net/SecureNioChannel.java
+++ b/java/org/apache/tomcat/util/net/SecureNioChannel.java
@@ -734,6 +734,14 @@ public class SecureNioChannel extends NioChannel {
return read;
}
+ private static long remaining(ByteBuffer[] dsts, int offset, int length) {
+ long result = 0;
+ for (int i = offset; i < offset + length; i++) {
+ result += dsts[i].remaining();
+ }
+ return result;
+ }
+
@Override
public long read(ByteBuffer[] dsts, int offset, int length) throws
IOException {
// are we in the middle of closing or closed?
@@ -801,30 +809,29 @@ public class SecureNioChannel extends NioChannel {
} else {
ByteBuffer readBuffer = getBufHandler().getReadBuffer();
boolean found = false;
- boolean resized = true;
+ long roomBefore = remaining(dsts, offset, length);
for (int i = 0; i < length; i++) {
// The SSL session has increased the required buffer
size
// since the buffer was created.
if (dsts[offset + i] ==
getBufHandler().getReadBuffer()) {
getBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
- if (dsts[offset + i] ==
getBufHandler().getReadBuffer()) {
- resized = false;
- }
dsts[offset + i] = getBufHandler().getReadBuffer();
found = true;
} else if (getAppReadBufHandler() != null &&
dsts[offset + i] ==
getAppReadBufHandler().getByteBuffer()) {
getAppReadBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
- if (dsts[offset + i] ==
getAppReadBufHandler().getByteBuffer()) {
- resized = false;
- }
dsts[offset + i] =
getAppReadBufHandler().getByteBuffer();
found = true;
}
}
if (found) {
- if (!resized) {
- throw new
IOException(sm.getString("channel.nio.ssl.unwrapFail", unwrap.getStatus()));
+ // Only fail if input remains, unwrap() consumed
nothing
+ // and expand() made no more room. Otherwise the read
has
+ // made progress or the loop exits normally.
+ if (netInBuffer.position() > 0 &&
unwrap.bytesConsumed() == 0
+ && remaining(dsts, offset, length) <=
roomBefore) {
+ throw new IOException(
+
sm.getString("channel.nio.ssl.unwrapFailNoRoom", unwrap.getStatus()));
}
} else {
// Add the main read buffer in the destinations and
try again
diff --git a/test/org/apache/tomcat/util/net/TestSecureNioChannelOverflow.java
b/test/org/apache/tomcat/util/net/TestSecureNioChannelOverflow.java
index 7c10b4309f..377427ef96 100644
--- a/test/org/apache/tomcat/util/net/TestSecureNioChannelOverflow.java
+++ b/test/org/apache/tomcat/util/net/TestSecureNioChannelOverflow.java
@@ -22,11 +22,15 @@ import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
+import javax.net.ssl.SSLEngineResult.Status;
+
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.apache.tomcat.util.res.StringManager;
+
/**
* Tests {@link SecureNioChannel#read(ByteBuffer)} when {@code unwrap()}
* returns {@code BUFFER_OVERFLOW} and {@code expand()} does not make enough
@@ -38,6 +42,8 @@ import org.junit.Test;
*/
public class TestSecureNioChannelOverflow {
+ private static final StringManager sm =
StringManager.getManager(SecureNioChannel.class);
+
/*
* Without the fix, the overflow tests loop forever on one core. The
* timeout fails the test, but the looping thread keeps running until the
@@ -156,6 +162,88 @@ public class TestSecureNioChannelOverflow {
}
+ /*
+ * Regression guard for the vectored read: passes with and without the fix.
+ */
+ @Test
+ public void testNormalVectoredRead() throws Exception {
+ int appSize = engines.getApplicationBufferSize();
+ SocketBufferHandler bh = new SocketBufferHandler(appSize, appSize,
false);
+ SecureNioChannel ch = newChannel(bh);
+ byte[] payload = engines.wrapRecord(1000, ch.netInBuffer);
+
+ ByteBuffer dst = bh.getReadBuffer();
+ Assert.assertEquals(payload.length, ch.read(new ByteBuffer[] { dst },
0, 1));
+ TesterSslEnginePair.assertContent(payload, dst);
+ }
+
+
+ /*
+ * Regression guard for the vectored read: passes with and without the
fix. expand() replaces the socket read
+ * buffer with a larger one and the read succeeds.
+ */
+ @Test
+ public void testVectoredExpandGrowsSocketBuffer() throws Exception {
+ int appSize = engines.getApplicationBufferSize();
+ SocketBufferHandler bh = new SocketBufferHandler(appSize / 2, appSize
/ 2, false);
+ SecureNioChannel ch = newChannel(bh);
+ byte[] payload = engines.wrapRecord(appSize, ch.netInBuffer);
+ Assert.assertTrue(payload.length > appSize / 2);
+
+ Assert.assertEquals(payload.length, ch.read(new ByteBuffer[] {
bh.getReadBuffer() }, 0, 1));
+ ByteBuffer dst = bh.getReadBuffer();
+ Assert.assertTrue(dst.capacity() >= appSize);
+ TesterSslEnginePair.assertContent(payload, dst);
+ }
+
+
+ /*
+ * The vectored equivalent of testNoRoomSocketBuffer().
+ */
+ @Test(timeout = TIMEOUT_MS)
+ public void testVectoredNoRoomSocketBuffer() throws Exception {
+ int appSize = engines.getApplicationBufferSize();
+ SocketBufferHandler bh = new SocketBufferHandler(appSize, appSize,
false);
+ SecureNioChannel ch = newChannel(bh);
+ engines.wrapRecord(appSize, ch.netInBuffer);
+
+ ByteBuffer dst = bh.getReadBuffer();
+ dst.position(dst.limit());
+ assertVectoredReadFails(ch, dst);
+ }
+
+
+ /*
+ * The vectored equivalent of testNoRoomAppBuffer().
+ */
+ @Test(timeout = TIMEOUT_MS)
+ public void testVectoredNoRoomAppBuffer() throws Exception {
+ int appSize = engines.getApplicationBufferSize();
+ SecureNioChannel ch = newChannel(new SocketBufferHandler(appSize,
appSize, false));
+ engines.wrapRecord(appSize, ch.netInBuffer);
+ TesterAppBuffer app = new TesterAppBuffer(appSize, appSize, appSize,
false);
+ ch.setAppReadBufHandler(app);
+
+ assertVectoredReadFails(ch, app.getByteBuffer());
+ }
+
+
+ /*
+ * The vectored equivalent of testAppBufferExpandOnlyMovesLimit(). The
buffer instance does not change but the
+ * first expand() adds room so the read must only fail once expand() adds
no more room.
+ */
+ @Test(timeout = TIMEOUT_MS)
+ public void testVectoredAppBufferExpandOnlyMovesLimit() throws Exception {
+ int appSize = engines.getApplicationBufferSize();
+ SecureNioChannel ch = newChannel(new SocketBufferHandler(appSize,
appSize, false));
+ engines.wrapRecord(appSize, ch.netInBuffer);
+ TesterAppBuffer app = new TesterAppBuffer(appSize, appSize - 100,
appSize - 92, true);
+ ch.setAppReadBufHandler(app);
+
+ assertVectoredReadFails(ch, app.getByteBuffer());
+ }
+
+
private SecureNioChannel newChannel(SocketBufferHandler bh) {
SecureNioChannel ch = new SecureNioChannel(bh, new NioEndpoint());
ch.sc = serverSock;
@@ -170,10 +258,26 @@ public class TestSecureNioChannelOverflow {
ch.read(dst);
Assert.fail("Expected IOException");
} catch (IOException expected) {
- // The message must be unwrapFailNoRoom, not another failure such
as
- // unwrapFailResize, so a stray IOException cannot pass this test.
- Assert.assertTrue(String.valueOf(expected.getMessage()),
- expected.getMessage() != null &&
expected.getMessage().contains("no free space"));
+ assertNoRoomMessage(expected);
+ }
+ }
+
+
+ private static void assertVectoredReadFails(SecureNioChannel ch,
ByteBuffer dst) {
+ try {
+ ch.read(new ByteBuffer[] { dst }, 0, 1);
+ Assert.fail("Expected IOException");
+ } catch (IOException expected) {
+ assertNoRoomMessage(expected);
}
}
+
+
+ /*
+ * The message must be unwrapFailNoRoom, not another failure such as
unwrapFailResize, so a stray IOException cannot
+ * pass the tests. The expected message is looked up so the check does not
depend on the locale.
+ */
+ private static void assertNoRoomMessage(IOException e) {
+ Assert.assertEquals(sm.getString("channel.nio.ssl.unwrapFailNoRoom",
Status.BUFFER_OVERFLOW), e.getMessage());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]