You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/10/02 09:04:08 UTC

mina-sshd git commit: [SSHD-846] Nullify KEX instance in AbstractSession once key negotiations completed

Repository: mina-sshd
Updated Branches:
  refs/heads/master a099c68b2 -> 28c52e91c


[SSHD-846] Nullify KEX instance in AbstractSession once key negotiations completed


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

Branch: refs/heads/master
Commit: 28c52e91cb29d4311b04db8a10c6c80c8e99f6ac
Parents: a099c68
Author: Goldstein Lyor <ly...@cb4.com>
Authored: Tue Oct 2 12:03:55 2018 +0300
Committer: Goldstein Lyor <ly...@cb4.com>
Committed: Tue Oct 2 12:03:55 2018 +0300

----------------------------------------------------------------------
 .../common/session/helpers/AbstractSession.java | 26 ++++++++++----------
 .../apache/sshd/deprecated/UserAuthAgent.java   |  5 +++-
 .../sshd/deprecated/UserAuthPublicKey.java      | 11 ++++++---
 3 files changed, 24 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/28c52e91/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 a739c33..287c8cd 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
@@ -681,15 +681,16 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
     protected void handleKexMessage(int cmd, Buffer buffer) throws Exception {
         validateKexState(cmd, KexState.RUN);
 
+        boolean debugEnabled = log.isDebugEnabled();
         if (kex.next(cmd, buffer)) {
-            if (log.isDebugEnabled()) {
+            if (debugEnabled) {
                 log.debug("handleKexMessage({})[{}] KEX processing complete after cmd={}", this, kex.getName(), cmd);
             }
             checkKeys();
             sendNewKeys();
             kexState.set(KexState.KEYS);
         } else {
-            if (log.isDebugEnabled()) {
+            if (debugEnabled) {
                 log.debug("handleKexMessage({})[{}] more KEX packets expected after cmd={}", this, kex.getName(), cmd);
             }
         }
@@ -889,6 +890,7 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
         synchronized (pendingPackets) {
             pendingWrites = sendPendingPackets(pendingPackets);
             kexState.set(KexState.DONE);
+            kex = null; // discard and GC since KEX is completed
         }
 
         int pendingCount = pendingWrites.size();
@@ -934,7 +936,7 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
         KexState actual = kexState.get();
         if (!expected.equals(actual)) {
             throw new IllegalStateException("Received KEX command=" + SshConstants.getCommandMessageName(cmd)
-                                          + " while in state=" + actual + " instead of " + expected);
+                  + " while in state=" + actual + " instead of " + expected);
         }
     }
 
@@ -1022,9 +1024,9 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
     @Override
     protected Closeable getInnerCloseable() {
         return builder()
-                .parallel(toString(), getServices())
-                .close(getIoSession())
-                .build();
+            .parallel(toString(), getServices())
+            .close(getIoSession())
+            .build();
     }
 
     @Override
@@ -2016,11 +2018,11 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
             if (log.isDebugEnabled()) {
                 if (t == null) {
                     log.debug("disconnect({}) operation successfully completed for reason={} [{}]",
-                              AbstractSession.this, SshConstants.getDisconnectReasonName(reason), msg);
+                          AbstractSession.this, SshConstants.getDisconnectReasonName(reason), msg);
                 } else {
                     log.debug("disconnect({}) operation failed ({}) for reason={} [{}]: {}",
-                               AbstractSession.this, t.getClass().getSimpleName(),
-                               SshConstants.getDisconnectReasonName(reason), msg, t.getMessage());
+                           AbstractSession.this, t.getClass().getSimpleName(),
+                           SshConstants.getDisconnectReasonName(reason), msg, t.getMessage());
                 }
             }
 
@@ -2832,11 +2834,9 @@ public abstract class AbstractSession extends AbstractKexFactoryManager implemen
      * Checks if idle timeout expired
      *
      * @param now           The current time in millis
-     * @param idleTimeoutMs The configured timeout in millis - if non-positive
-     *                      then no timeout
+     * @param idleTimeoutMs The configured timeout in millis - if non-positive then no timeout
      * @return A {@link SimpleImmutableEntry} specifying the timeout status and disconnect reason
-     * message if timeout expired, {@code null} or {@code NoTimeout} if no timeout
-     * occurred
+     * message if timeout expired, {@code null} or {@code NoTimeout} if no timeout occurred
      * @see #getIdleTimeout()
      */
     protected SimpleImmutableEntry<TimeoutStatus, String> checkIdleTimeout(long now, long idleTimeoutMs) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/28c52e91/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java b/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
index 4281092..94959ec 100644
--- a/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
+++ b/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthAgent.java
@@ -29,6 +29,7 @@ import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.session.ClientSessionImpl;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.config.keys.KeyUtils;
+import org.apache.sshd.common.kex.KeyExchange;
 import org.apache.sshd.common.util.buffer.Buffer;
 import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
 
@@ -63,8 +64,10 @@ public class UserAuthAgent extends AbstractUserAuth {
             buffer.putString(KeyUtils.getKeyType(key));
             buffer.putPublicKey(key);
 
+            KeyExchange kexValue = session.getKex();
+            byte[] hValue = kexValue.getH();
             Buffer bs = new ByteArrayBuffer();
-            bs.putBytes(session.getKex().getH());
+            bs.putBytes(hValue);
             bs.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             bs.putString(session.getUsername());
             bs.putString(service);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/28c52e91/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthPublicKey.java b/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthPublicKey.java
index d207e84..447e30b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthPublicKey.java
+++ b/sshd-core/src/test/java/org/apache/sshd/deprecated/UserAuthPublicKey.java
@@ -26,6 +26,7 @@ import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.config.keys.KeyUtils;
+import org.apache.sshd.common.kex.KeyExchange;
 import org.apache.sshd.common.signature.Signature;
 import org.apache.sshd.common.util.ValidateUtils;
 import org.apache.sshd.common.util.buffer.Buffer;
@@ -65,13 +66,15 @@ public class UserAuthPublicKey extends AbstractUserAuth {
                 buffer.putPublicKey(key.getPublic());
 
                 Signature verif =
-                        ValidateUtils.checkNotNull(NamedFactory.create(session.getSignatureFactories(), alg),
-                                "No signature factory located for algorithm=%s",
-                                alg);
+                    ValidateUtils.checkNotNull(NamedFactory.create(session.getSignatureFactories(), alg),
+                        "No signature factory located for algorithm=%s",
+                        alg);
                 verif.initSigner(key.getPrivate());
 
+                KeyExchange kexValue = session.getKex();
+                byte[] hValue = kexValue.getH();
                 Buffer bs = new ByteArrayBuffer();
-                bs.putBytes(session.getKex().getH());
+                bs.putBytes(hValue);
                 bs.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
                 bs.putString(session.getUsername());
                 bs.putString(service);