From ac6d5cca772729892f6fae3dfd46463d17027b9f Mon Sep 17 00:00:00 2001 From: Michiel ten Hagen Date: Fri, 21 May 2021 22:26:17 +0200 Subject: [PATCH 1/3] Support writing unsigned integers to buffer, this is required to support channel ids greater than Integer.MAX_VALUE fixes hierynomus/sshj#690 --- .../java/net/schmizz/sshj/common/Buffer.java | 17 ++++++++++++++++ .../sshj/connection/ConnectionImpl.java | 2 +- .../connection/channel/AbstractChannel.java | 2 +- .../channel/ChannelInputStream.java | 2 +- .../channel/ChannelOutputStream.java | 2 +- .../net/schmizz/sshj/common/BufferTest.java | 20 +++++++++++++++++++ 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/schmizz/sshj/common/Buffer.java b/src/main/java/net/schmizz/sshj/common/Buffer.java index 169a83490..a47ddd375 100644 --- a/src/main/java/net/schmizz/sshj/common/Buffer.java +++ b/src/main/java/net/schmizz/sshj/common/Buffer.java @@ -306,6 +306,23 @@ public long readUInt32() data[rpos++] & 0x000000ffL; } + /** + * Writes a uint32 integer + * + * @param uint32 + * + * @return this + */ + @SuppressWarnings("unchecked") + public T putUInt32FromInt(int uint32) { + ensureCapacity(4); + data[wpos++] = (byte) (uint32 >> 24); + data[wpos++] = (byte) (uint32 >> 16); + data[wpos++] = (byte) (uint32 >> 8); + data[wpos++] = (byte) uint32; + return (T) this; + } + /** * Writes a uint32 integer * diff --git a/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java b/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java index cfaee366e..59e470326 100644 --- a/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java +++ b/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java @@ -245,7 +245,7 @@ private void gotChannelOpen(SSHPacket buf) public void sendOpenFailure(int recipient, Reason reason, String message) throws TransportException { trans.write(new SSHPacket(Message.CHANNEL_OPEN_FAILURE) - .putUInt32(recipient) + .putUInt32FromInt(recipient) .putUInt32(reason.getCode()) .putString(message)); } diff --git a/src/main/java/net/schmizz/sshj/connection/channel/AbstractChannel.java b/src/main/java/net/schmizz/sshj/connection/channel/AbstractChannel.java index e31d0f4ee..1cd3e5c9e 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/AbstractChannel.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/AbstractChannel.java @@ -352,7 +352,7 @@ protected void handleRequest(String reqType, SSHPacket buf) } protected SSHPacket newBuffer(Message cmd) { - return new SSHPacket(cmd).putUInt32(recipient); + return new SSHPacket(cmd).putUInt32FromInt(recipient); } protected void receiveInto(ChannelInputStream stream, SSHPacket buf) diff --git a/src/main/java/net/schmizz/sshj/connection/channel/ChannelInputStream.java b/src/main/java/net/schmizz/sshj/connection/channel/ChannelInputStream.java index 6e79df0fc..85f0024d0 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/ChannelInputStream.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/ChannelInputStream.java @@ -151,7 +151,7 @@ private void checkWindow() if (adjustment > 0) { log.debug("Sending SSH_MSG_CHANNEL_WINDOW_ADJUST to #{} for {} bytes", chan.getRecipient(), adjustment); trans.write(new SSHPacket(Message.CHANNEL_WINDOW_ADJUST) - .putUInt32(chan.getRecipient()).putUInt32(adjustment)); + .putUInt32FromInt(chan.getRecipient()).putUInt32(adjustment)); win.expand(adjustment); } } diff --git a/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java b/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java index ad30493fd..f33223896 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java @@ -90,7 +90,7 @@ boolean flush(int bufferSize, boolean canAwaitExpansion) throws TransportExcepti packet.wpos(headerOffset); packet.putMessageID(Message.CHANNEL_DATA); - packet.putUInt32(chan.getRecipient()); + packet.putUInt32FromInt(chan.getRecipient()); packet.putUInt32(writeNow); packet.wpos(dataOffset + writeNow); diff --git a/src/test/java/net/schmizz/sshj/common/BufferTest.java b/src/test/java/net/schmizz/sshj/common/BufferTest.java index 7e61ad9a5..d22e51a11 100644 --- a/src/test/java/net/schmizz/sshj/common/BufferTest.java +++ b/src/test/java/net/schmizz/sshj/common/BufferTest.java @@ -25,6 +25,26 @@ public class BufferTest { + @Test + public void testNegativeInteger() throws BufferException { + byte[] negativeInt = new byte[] { (byte) 0xB8, + (byte) 0x4B, + (byte) 0xF4, + (byte) 0x38, + }; + PlainBuffer buffer = new PlainBuffer(negativeInt); + assertEquals(buffer.readUInt32AsInt(),-1202981832); + + PlainBuffer buff = new PlainBuffer(); + buff.ensureCapacity(4); + buff.putUInt32(-1202981832); + byte[] data = buff.getCompactData(); + assertEquals(data[0], (byte) 0xB8); + assertEquals(data[1], (byte) 0x4B); + assertEquals(data[2], (byte) 0xF4); + assertEquals(data[3], (byte) 0x38); + } + // Issue 72: previously, it entered an infinite loop trying to establish the buffer size @Test public void shouldThrowOnTooLargeCapacity() { From 613c782ded5a5a30eee495ed859315208dee3a69 Mon Sep 17 00:00:00 2001 From: Michiel ten Hagen Date: Fri, 21 May 2021 22:32:22 +0200 Subject: [PATCH 2/3] Fix incorrect test --- src/test/java/net/schmizz/sshj/common/BufferTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/schmizz/sshj/common/BufferTest.java b/src/test/java/net/schmizz/sshj/common/BufferTest.java index d22e51a11..3cdbece40 100644 --- a/src/test/java/net/schmizz/sshj/common/BufferTest.java +++ b/src/test/java/net/schmizz/sshj/common/BufferTest.java @@ -37,7 +37,7 @@ public void testNegativeInteger() throws BufferException { PlainBuffer buff = new PlainBuffer(); buff.ensureCapacity(4); - buff.putUInt32(-1202981832); + buff.putUInt32FromInt(-1202981832); byte[] data = buff.getCompactData(); assertEquals(data[0], (byte) 0xB8); assertEquals(data[1], (byte) 0x4B); From 6c6c56d3263fc904c90bdc5e6130cf2e809ee975 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Wed, 26 May 2021 12:08:53 +0200 Subject: [PATCH 3/3] Fix indentation to make codacy happy --- src/test/java/net/schmizz/sshj/common/BufferTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/net/schmizz/sshj/common/BufferTest.java b/src/test/java/net/schmizz/sshj/common/BufferTest.java index 3cdbece40..15acb219f 100644 --- a/src/test/java/net/schmizz/sshj/common/BufferTest.java +++ b/src/test/java/net/schmizz/sshj/common/BufferTest.java @@ -28,10 +28,9 @@ public class BufferTest { @Test public void testNegativeInteger() throws BufferException { byte[] negativeInt = new byte[] { (byte) 0xB8, - (byte) 0x4B, - (byte) 0xF4, - (byte) 0x38, - }; + (byte) 0x4B, + (byte) 0xF4, + (byte) 0x38 }; PlainBuffer buffer = new PlainBuffer(negativeInt); assertEquals(buffer.readUInt32AsInt(),-1202981832);