jclouds sshj hang with simple config

118 views
Skip to first unread message

Alex Heneveld

unread,
Apr 25, 2012, 8:15:43 AM4/25/12
to jclou...@googlegroups.com

I've been seeing some jclouds hanging, under 1.4.0, in net.schmizz.sshj.common.Buffer.getNextPowerOf2(Buffer.java:80)

I've boiled it down to a simple test case at https://gist.github.com/2489273 including logs and stack trace.  Note the stack trace was some 20m after the test ran.

I think I am using the wrong credentials, based on a log message, but I think the credentials is confusing so maybe that could be cleaned up.

And I wouldn't expect it to hang doing sftp no matter what I've done with my credentials!

Thanks.
--A

Aled Sage

unread,
Apr 25, 2012, 8:52:32 AM4/25/12
to jclou...@googlegroups.com, Alex Heneveld
Hi Alex,

The impl of net.schmizz.sshj.common.Buffer.getNextPowerOf2 certainly looks risky.

protected static int getNextPowerOf2(int i) {
int j = 1;
while (j < i)
j <<= 1;
return j;
}

For any value of i greater than
1073741824, it will spin forever. Shifting that gives a negative number, and subsequently gives zero.

The value of i is determined by PacketReader:

private int getPacketLength()
throws IOException {
readIntoBuffer(lenBuf, 0, lenBuf.length);

return (int) (lenBuf[0] << 24 & 0xff000000L
| lenBuf[1] << 16 & 0x00ff0000L
| lenBuf[2] << 8 & 0x0000ff00L
| lenBuf[3] & 0x000000ffL);
}

Perhaps if garbage is being sent down the stream then it can get some inappropriate bytes for the lenBuf?

---
As a quick test, try putting the attached jar at the head of your classpath. It changes getNextPowerOf2 to:

protected static int getNextPowerOf2(int i) {
int j = 1;
while (j < i) {
j <<= 1;
if (j <= 0) throw new IllegalArgumentException("Cannot get next power of 2; "+i+" is too large");
}
return j;
}

Hopefully then it won't hang, but it will presumably still fail in an ugly way...

Aled


--
You received this message because you are subscribed to the Google Groups "jclouds-dev" group.
To post to this group, send email to jclou...@googlegroups.com.
To unsubscribe from this group, send email to jclouds-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jclouds-dev?hl=en.
sshj-patch.jar

Adrian Cole

unread,
Apr 25, 2012, 10:23:56 AM4/25/12
to jclou...@googlegroups.com, Alex Heneveld

great detail, guys.  I think it would be more effective to send this to the sshj issues list, right?  They use github issues fwiw.

-A

Adrian Cole

unread,
Apr 25, 2012, 10:39:23 AM4/25/12
to jclou...@googlegroups.com, Alex Heneveld

p.s. let us know the issue/pull request link so we can followup. Generally, sshj will release a new version when asked.

Also, I noticed we are a few revs back on our jsch driver, so will update this.

cheers!

Andrew Phillips

unread,
Apr 25, 2012, 11:15:30 AM4/25/12
to jclou...@googlegroups.com
Thanks for picking that up, Aled! You may want to raise an issue at

https://github.com/shikhar/sshj

In my experience they're usually pretty responsive...

ap

Quoting Aled Sage <aled...@gmail.com>:

> Hi Alex,
>
> The impl of net.schmizz.sshj.common.Buffer.getNextPowerOf2 certainly
> looks risky.
>
> protected static int getNextPowerOf2(int i) {
> int j = 1;
> while (j < i)
> j <<= 1;
> return j;
> }
>
> For any value of i greater than 1073741824, it will spin
> forever.Shifting that gives a negative number, and subsequently gives
--
Andrew Phillips
qrmedia

Unless expressly stated otherwise, this message is confidential.
Access to this e-mail by anyone else is unauthorised. If you are
not an addressee, any disclosure or copying of the contents of
this e-mail or any action taken (or not taken) in reliance on it
is unauthorised and may be unlawful. If you are not an addressee,
please inform the sender immediately.

This message is confidential and may not be redistributed or
broadcast in whole or part in any form, including but not limited
to any form of internet transmission including email, usenet,
newsgroups, www, irc, icq, etc.
All liability for errors and viruses is disclaimed.

Alex Heneveld

unread,
Apr 25, 2012, 11:28:06 AM4/25/12
to jclou...@googlegroups.com, Andrew Phillips

+1

Nice work Aled, this patch does indeed solve the problem.

I guess there is some guff sent by the server...

I have observed before that sshd isn't fully operational when the VM is
up, takes a bit before it is sensible, but this error occurs repeatedly,
in each retry in my loop, though not in jclouds's retry. Perhaps
because I am logging in as root when ubuntu is desired...

The test case below now proceeds to try 120 times to ssh in (actually
120 x 40 or something due to jclouds retry -- although I haven't waited
that long!)

--A

Aled Sage

unread,
Apr 25, 2012, 5:20:16 PM4/25/12
to jclou...@googlegroups.com
Issue link is https://github.com/shikhar/sshj/issues/72

I haven't created a pull request yet. I'd like to catch the error higher up (probably in PacketReader.readPacket) so that it can give a more meaningful error message, such as "Invalid packet: length %d too long". I'll hopefully find time tomorrow to look at that...

Aled

Aled Sage

unread,
Apr 26, 2012, 6:47:47 AM4/26/12
to jclou...@googlegroups.com
Pull request submitted to sshj at https://github.com/shikhar/sshj/pull/73; issue updated at https://github.com/shikhar/sshj/issues/72.


Aled


On 25/04/2012 15:39, Adrian Cole wrote:

Adrian Cole

unread,
Apr 26, 2012, 10:58:06 AM4/26/12
to jclou...@googlegroups.com
<applause>

Andrew Phillips

unread,
Apr 26, 2012, 1:44:52 PM4/26/12
to jclou...@googlegroups.com
Cool! Thanks, Aled!

ap

Alex Heneveld

unread,
May 22, 2012, 1:13:38 PM5/22/12
to jclou...@googlegroups.com, Adrian Cole

sshj 0.8.0 has been released, fixing the power of 2 bug.
this bit me again just now. :(

pull 648 applies this to 1.4.x

--a
>>>> | lenBuf[2]<< 8& 0x0000ff00L

Adrian Cole

unread,
May 22, 2012, 2:58:27 PM5/22/12
to Alex Heneveld, jclou...@googlegroups.com

Thx. Cherry picked into master.


For more options, visit this group at
http://groups.google.com/group/jclouds-dev?hl=en.

--
You received this message because you are subscribed to the Google Groups
"jclouds-dev" group.
To post to this group, send email to jclou...@googlegroups.com.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/jclouds-dev?hl=en.
--
You received this message because you are subscribed to the Google Groups
"jclouds-dev" group.
To post to this group, send email to jclou...@googlegroups.com.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/jclouds-dev?hl=en.

--
You received this message because you are subscribed to the Google Groups
"jclouds-dev" group.
To post to this group, send email to jclou...@googlegroups.com.
To unsubscribe from this group, send email to
Reply all
Reply to author
Forward
0 new messages