[SSHD-699] Ignore also malformed SSH_MSG_UNIMPLEMENTED messages

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/fad77cd3
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/fad77cd3
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/fad77cd3

Branch: refs/heads/master
Commit: fad77cd31063f880a1a4a273d429df05ccf217ce
Parents: 9e56da0
Author: Lyor Goldstein <lyor.goldst...@gmail.com>
Authored: Fri Sep 16 18:27:20 2016 +0300
Committer: Lyor Goldstein <lyor.goldst...@gmail.com>
Committed: Fri Sep 16 18:27:20 2016 +0300

----------------------------------------------------------------------
 .../session/ReservedSessionMessagesHandler.java   | 12 ++++++++++++
 .../common/session/helpers/AbstractSession.java   | 18 ++++++++----------
 .../ReservedSessionMessagesHandlerAdapter.java    | 11 +++++++++++
 .../session/helpers/AbstractSessionTest.java      | 18 +++++++++++++++++-
 4 files changed, 48 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/fad77cd3/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
index 6138ae8..08292ea 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/session/ReservedSessionMessagesHandler.java
@@ -52,4 +52,16 @@ public interface ReservedSessionMessagesHandler extends 
SshdEventListener {
     default void handleDebugMessage(Session session, Buffer buffer) throws 
Exception {
         // ignored
     }
+
+    /**
+     * Invoked when an {@code SSH_MSG_UNIMPLEMENTED} packet is received
+     *
+     * @param session The {@code Session} through which the message was 
received
+     * @param buffer The {@code Buffer} containing the data
+     * @throws Exception If failed to handle the message
+     * @see <A HREF="https://tools.ietf.org/html/rfc4253#section-11.4";>RFC 
4253 - section 11.4</A>
+     */
+    default void handleUnimplementedMessage(Session session, Buffer buffer) 
throws Exception {
+        // ignored
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/fad77cd3/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
index d101e37..1377a0e 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
@@ -609,7 +609,7 @@ public abstract class AbstractSession extends 
AbstractKexFactoryManager implemen
         // malformed ignore message - ignore (even though we don't have to, 
but we can be tolerant in this case)
         if (!buffer.isValidMessageStructure(byte[].class)) {
             if (log.isTraceEnabled()) {
-                log.trace("handleDebug({}) ignore malformed message", this);
+                log.trace("handleIgnore({}) ignore malformed message", this);
             }
             return;
         }
@@ -619,17 +619,15 @@ public abstract class AbstractSession extends 
AbstractKexFactoryManager implemen
     }
 
     protected void handleUnimplemented(Buffer buffer) throws Exception {
-        handleUnimplemented(buffer.getInt(), buffer);
-    }
-
-    protected void handleUnimplemented(int seqNo, Buffer buffer) throws 
Exception {
-        if (log.isDebugEnabled()) {
-            log.debug("handleUnimplented({}) SSH_MSG_UNIMPLEMENTED #{}", this, 
seqNo);
+        if (!buffer.isValidMessageStructure(int.class)) {
+            if (log.isTraceEnabled()) {
+                log.trace("handleUnimplemented({}) ignore malformed message", 
this);
+            }
+            return;
         }
 
-        if (log.isTraceEnabled()) {
-            log.trace("handleUnimplemented({}) data: {}", this, 
buffer.toHex());
-        }
+        ReservedSessionMessagesHandler handler = 
resolveReservedSessionMessagesHandler();
+        handler.handleUnimplementedMessage(this, buffer);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/fad77cd3/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
index 0e62bb0..bcc0a5f 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/ReservedSessionMessagesHandlerAdapter.java
@@ -66,4 +66,15 @@ public class ReservedSessionMessagesHandlerAdapter
                       session, display, lang, msg);
         }
     }
+
+    @Override
+    public void handleUnimplementedMessage(Session session, Buffer buffer) 
throws Exception {
+        handleUnimplementedMessage(session, buffer, buffer.getUInt());
+    }
+
+    public void handleUnimplementedMessage(Session session, Buffer buffer, 
long seqNo) throws Exception {
+        if (log.isDebugEnabled()) {
+            log.debug("handleUnimplementedMessage({}) SSH_MSG_UNIMPLEMENTED - 
seqNo={}", session, seqNo);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/fad77cd3/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
 
b/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
index ced4eb9..dbb4051 100644
--- 
a/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
+++ 
b/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
@@ -194,6 +194,22 @@ public class AbstractSessionTest extends BaseTestSupport {
     }
 
     @Test   // see SSHD-699
+    public void testMalformedUnimplementedMessage() throws Exception {
+        session.setReservedSessionMessagesHandler(new 
ReservedSessionMessagesHandler() {
+            @Override
+            public void handleUnimplementedMessage(Session session, Buffer 
buffer) throws Exception {
+                fail("Unexpected invocation: available=" + buffer.available());
+            }
+        });
+
+        Buffer buffer = new ByteArrayBuffer(Long.SIZE);
+        for (int index = 0; index < (Integer.BYTES - 1); index++) {
+            buffer.putByte((byte) index);
+            session.handleUnimplemented(buffer);
+        }
+    }
+
+    @Test   // see SSHD-699
     public void testMalformedIgnoreMessageBadLength() throws Exception {
         session.setReservedSessionMessagesHandler(new 
ReservedSessionMessagesHandler() {
             @Override
@@ -203,7 +219,7 @@ public class AbstractSessionTest extends BaseTestSupport {
         });
 
         Buffer buffer = new ByteArrayBuffer(Long.SIZE);
-        for (int index = 0; index < Integer.BYTES; index++) {
+        for (int index = 0; index < (Integer.BYTES - 1); index++) {
             buffer.putByte((byte) index);
             session.handleIgnore(buffer);
         }

Reply via email to