-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(taken from jsch-users): Possible deadlock in KnownHosts #131
Comments
I'm not sure I see how this deadlock could still be occurring, at least as described in the original JSch-users thread: the Are you still able to reproduce this problem with current release? |
Um... jsch/src/main/java/com/jcraft/jsch/KnownHosts.java Lines 551 to 558 in 7e05876
jsch/src/main/java/com/jcraft/jsch/KnownHosts.java Lines 582 to 588 in 7e05876
That's what I see in trunk right at this moment.
The problem was reported by somebody else and I just provided a solution by looking very closely at the deadlock-message in that mail and its stacktraces. We've had occasional deadlocks as well "out there" and after this fix never heard of one again (but I might remember wrong and this was fixed by me at some other place back then). As mentioned before in another issue I'm currently doing a lot of customization of the original sources when a new release is available (a lot of effort I try to reduce by contributing the useful stuff to this project - the logging issue being one of them). So if I do testing with SSH I always use a version without above synchronized blocks. Above synchronized blocks are completely unnecessary. |
Neither of those are synchronizing on the The original deadlock as you linked from JSch-Users archive is caused by a deadlock between Thread 1: locks the Thread 2: locks the So you end up with Thread 1 & Thread 2 waiting on for locks owned by each other. However, current version of Therefore, if you look at how the deadlock originally occurred in that old release of JSch, you will see it can no longer happen, because the call by Thread 2 to Thread 1: locks the Thread 2: locks the |
Btw, I'm pretty sure this was noted as being fixed in the old 0.1.54 release as documented here: Line 31 in 7e05876
|
Btw, I do agree that cleaning up the calls to Also, it probably would be an improvement to stop calling In fact, you can see that it's even possible to cause NPE's, because |
As said, Here[TM] I'm testing with a customized version of JSch that contain the complete patch as shared on the mail list back then but I assume that you're right with your analysis that having an hash-instance per HashedHostKey-instance is sufficient to prevent occurences. I'll take your other answer as an invitation for implementation ;-) so I'll have a look into it. |
@norrisjeremy I think I've found a bug unrelated by this issue but that came up while I've put the class under test. In the instantiation of jsch/src/main/java/com/jcraft/jsch/KnownHosts.java Lines 534 to 539 in 7e05876
This worked well at a time where SHA-1 was the hashing algorithm. Now the hashing algorithm can be configured, so the length of the hash-text can vary. When actually testing this, the testcase fails (as expected):
I can do the fix and change the subject of this issue accordingly so that the PR stays "on topic" |
I'm confused, why are you using |
That way the effect can be shown. If it's forbidden to use a non-SHA-1-hashing algorithm, setting one via JSch.setConfig should lead to an IllegalArgumentException (doesn't happen, that's another test I've already written) or hashing algorithms with different blocksizes than 20 should be supported at the code location I've quoted. |
I think you've misunderstood the purpose of There's no need to make any changes here: if a user incorrectly execute |
Misunderstanding is the wrong word. I've looked at the source, checked what's possible and wrote tests around that. Edit: I assumed that non-SHA-256-hashes are supported because As I said: If the use of non-SHA-1-hashes shouldn't be allowed, trying to set one should lead to an exception. Otherwise - looking at KnownHosts - you will corrupt your known-hosts-file e.g. as soon as you do a remove-call, that leads to a sync where the wrong hashes are written to the file. |
Again, there is no need to do anything here: if you incorrectly execute Also, I'm not really sure how you would propose to to implement any sort of runtime detection of what a user configures I.e. it's impossible to check that when a user executes |
Why shouldn't that be possible? HMACSHA1 is a deterministic function, so given the same input you get the same result. If it differs you can complain. |
Personally I see a corrupt hosts-file much more serious than a handshake-failure. After fixing the MAC-configuration the latter starts working again while the former will still leave you with a corrupt hosts-file. Edit: can you show me where |
https://github.com/mwiede/jsch/blob/master/src/main/java/com/jcraft/jsch/Session.java#L1497 |
That explains why I haven't found anything ;-) I'll remove the test with HMACSHA256 then but I still like to add a "is-this-SHA-1" check to prevent the corrupt-known-hosts-file-situation from happening. Is that OK? |
I would rather not, as it would simply degrade performance. If a user incorrectly sets |
Loading the known-hosts-file isn't part of a performance-critical operation. The read-operation of the file should take magnitudes longer than a single hash-operation with given input. I'm not talking about all occurrences where the SHA-1-class is used (e.g. during MAC negotiations). |
I strongly disagree with adding any sort of runtime testing of MAC operations. We simply need to trust that users know what they are doing and if they do the wrong thing, well things can get corrupted. |
* log exceptions to logger instead of STDOUT/ERR, put class under test, fixed bugs that where found in the process - fixed bug in remove(String, String, String) leading to only every second host key being removed - ensure that there is a SHA1-hash-instance to prevent NullPointerExceptions - replaced all occurrences of STDOUT/ERR outputs in case of exceptions with log entries - added a default method to Logger to allow passing the exception to be logged with the message * create the exception we expect to get the exception message of that particular JVM * use platform dependent linebreak for logging the stack trace added log-framework-specific implementations of log(int, String, Throwable) to pass the cause to the framework in a fitting way added tests for all log frameworks that can be configured in a programmatic way. * added * "normalize" line breaks * enforce \r\n when printing linebreaks * fixed test to check the line break between message and stacktrace (that actually is system dependent) * make members final use slf4j 1.x API for logging to keep backward compatibility * throw a RuntimeException (to keep the signature of KnownHost's constructor) if the HMAC-SHA1-class can't be instantiated.
Hi,
I've remembered that there was an issue reported on jsch-users where I've answered with a patch. I don't have the original mail in my archive on my current computer but the internet doesn't forget.
I've checked the current source and it seems that a part of my fix got included into the source base but not all, especially the removal of the synchronization that lead to the deadlock in the mentioned report above.
Also KnownHost contains a lot of
System.out
andSystem.err.println
calls in case of errors that should be changed to logger.log-calls in my eyes.The text was updated successfully, but these errors were encountered: