Author: markt
Date: Thu Jul 11 10:11:56 2013
New Revision: 1502176

URL: http://svn.apache.org/r1502176
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=55233
Fixed buffer overflow error when a client needs to unwrap more encrypted data 
than the read buffer has space for.
The fix is to increase the read buffer to the current maximum size of output 
expected by the SSL engine.

Added:
    
tomcat/trunk/java/org/apache/tomcat/websocket/ReadBufferOverflowException.java  
 (with props)
Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelWrapperSecure.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java

Modified: 
tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelWrapperSecure.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelWrapperSecure.java?rev=1502176&r1=1502175&r2=1502176&view=diff
==============================================================================
--- 
tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelWrapperSecure.java 
(original)
+++ 
tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelWrapperSecure.java 
Thu Jul 11 10:11:56 2013
@@ -293,13 +293,17 @@ public class AsyncChannelWrapperSecure i
                             // partial data on the next read
                         } else if (s == Status.BUFFER_OVERFLOW) {
                             // Not enough space in the destination buffer to
-                            // store all of the data
-                            throw new IOException(sm.getString(
-                                    "asyncChannelWrapperSecure.readOverflow",
-                                    Integer.valueOf( sslEngine.getSession().
-                                            getApplicationBufferSize()),
-                                    Integer.valueOf(dest.limit()),
-                                    Integer.valueOf(dest.position())));
+                            // store all of the data. We could use a bytes read
+                            // value of -bufferSizeRequired to signal the new
+                            // buffer size required but an explicit exception 
is
+                            // clearer.
+                            if (reading.compareAndSet(true, false)) {
+                                throw new 
ReadBufferOverflowException(sslEngine.
+                                        
getSession().getApplicationBufferSize());
+                            } else {
+                                future.fail(new 
IllegalStateException(sm.getString(
+                                        
"asyncChannelWrapperSecure.wrongStateRead")));
+                            }
                         } else {
                             // Status.CLOSED - unexpected
                             throw new IllegalStateException(sm.getString(

Added: 
tomcat/trunk/java/org/apache/tomcat/websocket/ReadBufferOverflowException.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/ReadBufferOverflowException.java?rev=1502176&view=auto
==============================================================================
--- 
tomcat/trunk/java/org/apache/tomcat/websocket/ReadBufferOverflowException.java 
(added)
+++ 
tomcat/trunk/java/org/apache/tomcat/websocket/ReadBufferOverflowException.java 
Thu Jul 11 10:11:56 2013
@@ -0,0 +1,34 @@
+/*
+ * 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.websocket;
+
+import java.io.IOException;
+
+public class ReadBufferOverflowException extends IOException {
+
+    private static final long serialVersionUID = 1L;
+
+    private final int minBufferSize;
+
+    public ReadBufferOverflowException(int minBufferSize) {
+        this.minBufferSize = minBufferSize;
+    }
+
+    public int getMinBufferSize() {
+        return minBufferSize;
+    }
+}

Propchange: 
tomcat/trunk/java/org/apache/tomcat/websocket/ReadBufferOverflowException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java?rev=1502176&r1=1502175&r2=1502176&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java Thu Jul 11 
10:11:56 2013
@@ -25,9 +25,10 @@ import javax.websocket.CloseReason.Close
 
 public class WsFrameClient extends WsFrameBase {
 
-    private final ByteBuffer response;
     private final AsyncChannelWrapper channel;
     private final CompletionHandler<Integer,Void> handler;
+    // Not final as it may need to be re-sized
+    private ByteBuffer response;
 
     public WsFrameClient(ByteBuffer response, AsyncChannelWrapper channel,
             WsSession wsSession) {
@@ -107,7 +108,19 @@ public class WsFrameClient extends WsFra
 
         @Override
         public void failed(Throwable exc, Void attachment) {
-            close(exc);
+            if (exc instanceof ReadBufferOverflowException) {
+                // response will be empty if this exception is thrown
+                response = ByteBuffer.allocate(
+                        ((ReadBufferOverflowException) 
exc).getMinBufferSize());
+                response.flip();
+                try {
+                    processSocketRead();
+                } catch (IOException e) {
+                    close(e);
+                }
+            } else {
+                close(exc);
+            }
         }
     }
 }



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

Reply via email to