This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 7dd770582c Add the NIO2 fix for BZ 70311
7dd770582c is described below

commit 7dd770582c9a511ca748e76883a0dd4340ebb3c9
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Nov 17 18:58:24 2026 +0000

    Add the NIO2 fix for BZ 70311
---
 .../apache/tomcat/util/net/SecureNio2Channel.java  |  58 +++--
 .../util/net/TestSecureNio2ChannelOverflow.java    | 238 +++++++++++++++++++++
 webapps/docs/changelog.xml                         |   7 +-
 3 files changed, 285 insertions(+), 18 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/SecureNio2Channel.java 
b/java/org/apache/tomcat/util/net/SecureNio2Channel.java
index b39567c5f6..492235b595 100644
--- a/java/org/apache/tomcat/util/net/SecureNio2Channel.java
+++ b/java/org/apache/tomcat/util/net/SecureNio2Channel.java
@@ -842,11 +842,12 @@ public class SecureNio2Channel extends Nio2Channel {
                     } else {
                         // The SSL session has increased the required buffer 
size
                         // since the buffer was created.
+                        int roomBefore = dst.remaining();
                         if (dst == getBufHandler().getReadBuffer()) {
                             // This is the normal case for this code
                             
getBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
                             dst = getBufHandler().getReadBuffer();
-                        } else if (dst == 
getAppReadBufHandler().getByteBuffer()) {
+                        } else if (getAppReadBufHandler() != null && dst == 
getAppReadBufHandler().getByteBuffer()) {
                             
getAppReadBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
                             dst = getAppReadBufHandler().getByteBuffer();
                         } else {
@@ -855,6 +856,15 @@ public class SecureNio2Channel extends Nio2Channel {
                             throw new ExecutionException(new IOException(
                                     
sm.getString("channel.nio.ssl.unwrapFailResize", unwrap.getStatus())));
                         }
+                        // If there is still input to unwrap but unwrap() 
consumed
+                        // nothing and expand() made no more room, the next 
unwrap()
+                        // overflows the same way and this loop never ends. 
Fail the
+                        // read instead. An empty netInBuffer exits the loop 
normally.
+                        if (netInBuffer.position() > 0 && 
unwrap.bytesConsumed() == 0
+                                && dst.remaining() <= roomBefore) {
+                            throw new ExecutionException(new IOException(
+                                    
sm.getString("channel.nio.ssl.unwrapFailNoRoom", unwrap.getStatus())));
+                        }
                     }
                 } else {
                     // Something else went wrong
@@ -1045,6 +1055,7 @@ public class SecureNio2Channel extends Nio2Channel {
                                 } else {
                                     // The SSL session has increased the 
required buffer size
                                     // since the buffer was created.
+                                    int roomBefore = dst2.remaining();
                                     if (dst2 == 
getBufHandler().getReadBuffer()) {
                                         // This is the normal case for this 
code
                                         
getBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
@@ -1060,6 +1071,16 @@ public class SecureNio2Channel extends Nio2Channel {
                                         throw new IOException(
                                                 
sm.getString("channel.nio.ssl.unwrapFailResize", unwrap.getStatus()));
                                     }
+                                    // If there is still input to unwrap but 
unwrap()
+                                    // consumed nothing and expand() made no 
more room, the
+                                    // next unwrap() overflows the same way 
and this loop
+                                    // never ends. Fail the read instead. An 
empty
+                                    // netInBuffer exits the loop normally.
+                                    if (netInBuffer.position() > 0 && 
unwrap.bytesConsumed() == 0
+                                            && dst2.remaining() <= roomBefore) 
{
+                                        throw new IOException(
+                                                
sm.getString("channel.nio.ssl.unwrapFailNoRoom", unwrap.getStatus()));
+                                    }
                                 }
                             } else {
                                 // Something else went wrong
@@ -1088,6 +1109,14 @@ public class SecureNio2Channel extends Nio2Channel {
         }
     }
 
+    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 <A> void read(final ByteBuffer[] dsts, final int offset, final int 
length, final long timeout,
             final TimeUnit unit, final A attachment, final 
CompletionHandler<Long,? super A> handler) {
@@ -1161,32 +1190,33 @@ public class SecureNio2Channel extends Nio2Channel {
                                 // in the constructor
                                 ByteBuffer readBuffer = 
getBufHandler().getReadBuffer();
                                 boolean found = false;
-                                boolean resized = true;
+                                // Use dsts2 and length2 since the overflow 
buffer may have been added to the
+                                // destinations by a previous overflow
+                                long roomBefore = remaining(dsts2, offset, 
length2);
                                 for (int i = 0; i < length2; i++) {
                                     // The SSL session has increased the 
required buffer size
                                     // since the buffer was created.
-                                    if (dsts[offset + i] == 
getBufHandler().getReadBuffer()) {
+                                    if (dsts2[offset + i] == 
getBufHandler().getReadBuffer()) {
                                         
getBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
-                                        if (dsts[offset + i] == 
getBufHandler().getReadBuffer()) {
-                                            resized = false;
-                                        }
-                                        dsts[offset + i] = 
getBufHandler().getReadBuffer();
+                                        dsts2[offset + i] = 
getBufHandler().getReadBuffer();
                                         found = true;
                                     } else if (getAppReadBufHandler() != null 
&&
-                                            dsts[offset + i] == 
getAppReadBufHandler().getByteBuffer()) {
+                                            dsts2[offset + i] == 
getAppReadBufHandler().getByteBuffer()) {
                                         getAppReadBufHandler()
                                                 
.expand(sslEngine.getSession().getApplicationBufferSize());
-                                        if (dsts[offset + i] == 
getAppReadBufHandler().getByteBuffer()) {
-                                            resized = false;
-                                        }
-                                        dsts[offset + i] = 
getAppReadBufHandler().getByteBuffer();
+                                        dsts2[offset + i] = 
getAppReadBufHandler().getByteBuffer();
                                         found = true;
                                     }
                                 }
                                 if (found) {
-                                    if (!resized) {
+                                    // If there is still input to unwrap but 
unwrap() consumed nothing
+                                    // and expand() made no more room, the 
next unwrap() overflows the
+                                    // same way and this loop never ends. Fail 
the read instead. An empty
+                                    // netInBuffer exits the loop normally.
+                                    if (netInBuffer.position() > 0 && 
unwrap.bytesConsumed() == 0 &&
+                                            remaining(dsts2, offset, length2) 
<= roomBefore) {
                                         throw new IOException(
-                                                
sm.getString("channel.nio.ssl.unwrapFail", unwrap.getStatus()));
+                                                
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/TestSecureNio2ChannelOverflow.java 
b/test/org/apache/tomcat/util/net/TestSecureNio2ChannelOverflow.java
new file mode 100644
index 0000000000..74ca1a6b76
--- /dev/null
+++ b/test/org/apache/tomcat/util/net/TestSecureNio2ChannelOverflow.java
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.util.net;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousServerSocketChannel;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.channels.CompletionHandler;
+import java.util.concurrent.TimeUnit;
+
+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 the vectored {@link SecureNio2Channel#read(ByteBuffer[], int, int, 
long, TimeUnit, Object, CompletionHandler)}
+ * when {@code unwrap()} returns {@code BUFFER_OVERFLOW} and {@code expand()} 
does not make enough room for the record.
+ * <p>
+ * Each test writes its TLS record straight into the channel's {@code 
netInBuffer}. Nothing is sent over the socket, so
+ * the read completes on the calling thread and the tests do not depend on 
timing.
+ */
+public class TestSecureNio2ChannelOverflow {
+
+    private static final StringManager sm = 
StringManager.getManager(SecureNio2Channel.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 JVM for this test class exits. build.xml forks 
a JVM per test class.
+     */
+    private static final long TIMEOUT_MS = 10000;
+
+    private TesterSslEnginePair engines;
+    private AsynchronousServerSocketChannel listener;
+    private AsynchronousSocketChannel clientSock;
+    private AsynchronousSocketChannel serverSock;
+
+
+    @Before
+    public void setUp() throws Exception {
+        engines = new TesterSslEnginePair();
+        listener = AsynchronousServerSocketChannel.open();
+        listener.bind(new InetSocketAddress("127.0.0.1", 0));
+        clientSock = AsynchronousSocketChannel.open();
+        clientSock.connect(listener.getLocalAddress()).get();
+        serverSock = listener.accept().get();
+    }
+
+
+    @After
+    public void tearDown() throws IOException {
+        clientSock.close();
+        serverSock.close();
+        listener.close();
+    }
+
+
+    /*
+     * Regression guard: passes with and without the fix.
+     */
+    @Test
+    public void testNormalVectoredRead() throws Exception {
+        int appSize = engines.getApplicationBufferSize();
+        SocketBufferHandler bh = new SocketBufferHandler(appSize, appSize, 
false);
+        SecureNio2Channel ch = newChannel(bh);
+        byte[] payload = engines.wrapRecord(1000, ch.netInBuffer);
+
+        ByteBuffer dst = bh.getReadBuffer();
+        Result result = read(ch, dst);
+
+        Assert.assertNull(result.failure);
+        Assert.assertEquals(payload.length, result.read);
+        TesterSslEnginePair.assertContent(payload, dst);
+    }
+
+
+    /*
+     * Regression guard: 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);
+        SecureNio2Channel ch = newChannel(bh);
+        byte[] payload = engines.wrapRecord(appSize, ch.netInBuffer);
+        Assert.assertTrue(payload.length > appSize / 2);
+
+        Result result = read(ch, bh.getReadBuffer());
+
+        Assert.assertNull(result.failure);
+        Assert.assertEquals(payload.length, result.read);
+        ByteBuffer dst = bh.getReadBuffer();
+        Assert.assertTrue(dst.capacity() >= appSize);
+        TesterSslEnginePair.assertContent(payload, dst);
+    }
+
+
+    /*
+     * The socket read buffer is full and already at the application buffer 
size, so expand() returns the same buffer.
+     */
+    @Test(timeout = TIMEOUT_MS)
+    public void testVectoredNoRoomSocketBuffer() throws Exception {
+        int appSize = engines.getApplicationBufferSize();
+        SocketBufferHandler bh = new SocketBufferHandler(appSize, appSize, 
false);
+        SecureNio2Channel ch = newChannel(bh);
+        engines.wrapRecord(appSize, ch.netInBuffer);
+
+        ByteBuffer dst = bh.getReadBuffer();
+        dst.position(dst.limit());
+        assertNoRoom(read(ch, dst));
+    }
+
+
+    /*
+     * The application buffer has no room and expand() does nothing.
+     */
+    @Test(timeout = TIMEOUT_MS)
+    public void testVectoredNoRoomAppBuffer() throws Exception {
+        int appSize = engines.getApplicationBufferSize();
+        SecureNio2Channel ch = newChannel(new SocketBufferHandler(appSize, 
appSize, false));
+        engines.wrapRecord(appSize, ch.netInBuffer);
+        TesterAppBuffer app = new TesterAppBuffer(appSize, appSize, appSize, 
false);
+        ch.setAppReadBufHandler(app);
+
+        assertNoRoom(read(ch, app.getByteBuffer()));
+    }
+
+
+    /*
+     * The application buffer is large enough but has little room left, and 
expand() only moves the limit to the
+     * capacity. The first expand() adds some room, but not enough for the 
record, and the second adds none.
+     */
+    @Test(timeout = TIMEOUT_MS)
+    public void testVectoredAppBufferExpandOnlyMovesLimit() throws Exception {
+        int appSize = engines.getApplicationBufferSize();
+        SecureNio2Channel 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);
+
+        assertNoRoom(read(ch, app.getByteBuffer()));
+    }
+
+
+    /*
+     * The destination is not one of the channel's buffers so the socket read 
buffer is added as an overflow buffer.
+     * It is too small for the record, so the next overflow must expand it (it 
is one of the destinations by then)
+     * rather than add it again.
+     */
+    @Test(timeout = TIMEOUT_MS)
+    public void testVectoredOverflowBufferTooSmall() throws Exception {
+        int appSize = engines.getApplicationBufferSize();
+        SocketBufferHandler bh = new SocketBufferHandler(appSize / 2, appSize 
/ 2, false);
+        SecureNio2Channel ch = newChannel(bh);
+        byte[] payload = engines.wrapRecord(appSize, ch.netInBuffer);
+        Assert.assertTrue(payload.length > appSize / 2);
+
+        Result result = read(ch, ByteBuffer.allocate(0));
+
+        // The read must complete rather than loop. Both outcomes are 
acceptable here: the overflow buffer was expanded
+        // and the data is read later, or the read fails because no more room 
could be made.
+        Assert.assertTrue(result.completed);
+        Assert.assertTrue(bh.getReadBuffer().capacity() >= appSize);
+    }
+
+
+    private SecureNio2Channel newChannel(SocketBufferHandler bh) throws 
Exception {
+        SecureNio2Channel ch = new SecureNio2Channel(bh, new Nio2Endpoint());
+        ch.sc = serverSock;
+        ch.sslEngine = engines.getServer();
+        // The field is private. Setting it avoids performing a real handshake.
+        Field handshakeComplete = 
SecureNio2Channel.class.getDeclaredField("handshakeComplete");
+        handshakeComplete.setAccessible(true);
+        handshakeComplete.setBoolean(ch, true);
+        return ch;
+    }
+
+
+    private static Result read(SecureNio2Channel ch, ByteBuffer dst) {
+        Result result = new Result();
+        // netInBuffer holds the record so the read does not wait for the 
socket and completes on this thread.
+        ch.read(new ByteBuffer[] { dst }, 0, 1, 1, TimeUnit.SECONDS, null, new 
CompletionHandler<Long,Object>() {
+            @Override
+            public void completed(Long read, Object attachment) {
+                result.completed = true;
+                result.read = read.longValue();
+            }
+
+            @Override
+            public void failed(Throwable exc, Object attachment) {
+                result.completed = true;
+                result.failure = exc;
+            }
+        });
+        return result;
+    }
+
+
+    /*
+     * The message must be unwrapFailNoRoom, not another failure such as 
unwrapFail, 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 assertNoRoom(Result result) {
+        Assert.assertTrue(result.completed);
+        Assert.assertTrue(String.valueOf(result.failure), result.failure 
instanceof IOException);
+        Assert.assertEquals(sm.getString("channel.nio.ssl.unwrapFailNoRoom", 
Status.BUFFER_OVERFLOW),
+                result.failure.getMessage());
+    }
+
+
+    private static class Result {
+        boolean completed;
+        long read;
+        Throwable failure;
+    }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index ecf3287fab..da4a9c9374 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -199,10 +199,9 @@
         HTTP/1.1. Pull request <pr>1154</pr> provided by Arturo Bernal. (markt)
       </fix>
       <fix>
-        <bug>70311</bug>: Fix an infinite loop in
-        <code>SecureNioChannel.read(ByteBuffer)</code> when the TLS engine
-        returns <code>BUFFER_OVERFLOW</code> without consuming any input and
-        the destination buffer cannot be expanded. The read now fails with an
+        <bug>70311</bug>: Fix an infinite loop when the TLS engine returns
+        <code>BUFFER_OVERFLOW</code> without consuming any input and the
+        destination buffer cannot be expanded. The read now fails with an
         <code>IOException</code>. Pull request <pr>1157</pr> provided by
         Pranav N V. (markt)
       </fix>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to