Skip to content

Servers that do not implement RFC 4252 correctly may lead to stack overflow #306

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

Closed
shahsumit opened this issue Sep 26, 2017 · 19 comments
Closed
Assignees

Comments

@shahsumit
Copy link

We are using SSH.NET(2014.4.6.0) dll to connect to the sftp sites in our application. If all the required parameters are correct the code works as desired. But if the sftp password is incorrect the system crashes. The try catch block is not able to catch the error. An log is written in the event viewer “The process was terminated due to stack overflow.”

Added the reference of the latest Renci dll from package manager console. Have tried using the beta version of the dll as well with no success(https://www.nuget.org/packages/SSH.NET)

Debugged the code when the sftp password is incorrect and below are the findings:

  • The authenticationMethod.Authenticate(session) always returns authentication result as "PartialSuccess"
  • Because of this the "TryAuthenticate" methods goes into an recursive loop
  • We are getting the stack overflow exception and the system crashes.

This being an production issue, we have deployed a quick fix.

  • In the PasswordAuthenticationMethod.cs class file, commented the below 3 lines of code in the "Session_UserAuthenticationFailureReceived" method:
    //if (e.Message.PartialSuccess)
    // _authenticationResult = AuthenticationResult.PartialSuccess;
    //else
  • Doing this the authentication result returned for the Password method will either be success or failure.
  • Also caught the "SshAuthenticationException" exception.

Note: This is not happening all the time but for certain sftp sites.

Requesting you to please look into this issue.

@darkoperator
Copy link

darkoperator commented Sep 26, 2017 via email

@jbpatelgit
Copy link

Yes. We downloaded latest beta3 and tried. It still has the issue.

@darkoperator
Copy link

darkoperator commented Sep 26, 2017 via email

@jbpatelgit
Copy link

Try.Catch doesn't catch stack overflow errors. It just crashes the process, which is a windows service in our case.

@drieseng
Copy link
Member

@jbpatelgit I think the stack overflow happens when the SSH server expects two factor authentication with 2x password, and the password is not correct. I'll try to reproduce it tomorrow.

@drieseng
Copy link
Member

@shahsumit Would it be possible for you to build SSH.NET from source? In the case, I could send a modified version of our ClientAuthentication class with extra tracing.

I haven't yet managed to reproduce this issue.

@jbpatelgit
Copy link

Sure. Please send us the class and details on how to enable the trace.

@drieseng
Copy link
Member

I replied to your initial email.

@shahsumit
Copy link
Author

Replaced the ClientAuthentication.cs class with the one emailed. Attached is the console log. Basically the authentication result being returned is "PartialSuccess" which again triggers the TryAuthenticate method. This goes into an recursive loop which lastly gives StackOverflow exception.

SftpErrorLog.txt

@drieseng
Copy link
Member

drieseng commented Oct 1, 2017

@shahsumit I'm working on a fix for the stack overflow.

The underlying cause is bad behavior from the SSH server. If any given authentication request fails, it should respond with a SSH_MSG_USERAUTH_FAILURE message with 'partial success' set to FALSE. The server you're using appears to set the value of 'partial success' to TRUE for an authentication request that fails (due to wrong password).

From RFC 4252:

The value of 'partial success' MUST be TRUE if the authentication request to which this is a response was successful. It MUST be FALSE if the request was not successfully processed.

I will guard against a SSH server that does not respect this RFC by introducing a limit for the number of times we attempt to authenticate using a given method for which we received a 'partial success' response.

Nonethless, I would still like to report this as an issue against this SSH server. What SSH server (and version) are you using?

@drieseng drieseng self-assigned this Oct 1, 2017
@drieseng drieseng added this to the 2016.1.0-beta4 milestone Oct 1, 2017
@jbpatelgit
Copy link

jbpatelgit commented Oct 1, 2017 via email

@drieseng
Copy link
Member

drieseng commented Oct 2, 2017

@jbpatelgit I'm working & commuting between 11 and 14 hours a day, and have 2 kids and a gf that don't nearly get the attention they deserve. Let's just say that I'm going as fast as I can.

The stack overflow issue is fixed locally, and I'm mostly working on adding unit tests to avoid future regressions.

Can you use these instructions to get me the name and version of the SSH software that your client is using?

@jbpatelgit
Copy link

Gert,

I never meant to rush you in anyway. I was just asking for the status. Take all the time you need as we have put a work around for now.

I will get you the server version tomorrow.

Thank you very much for all efforts to keep this nice open source library alive.

@drieseng
Copy link
Member

drieseng commented Oct 3, 2017

never meant to rush you in anyway. I was just asking for the status. Take all the time you need as we have put a work around for now.

I know. There are just days where it gets a little too much.
I'll try to commit the fix tomorrow, and release a new beta this weekend.

@shahsumit
Copy link
Author

Hi Gert,

Please find below, the Sftp server version and name:

SSH-2.0-1.82_sshlib Globalscape

Please let us know if any more information is required.

@drieseng
Copy link
Member

drieseng commented Oct 4, 2017

@shahsumit Thanks for the info. I'll look into that server later.

@jbpatelgit I created PR #311 for the fix. It's not yet complete, but should work for you.

@drieseng
Copy link
Member

drieseng commented Oct 5, 2017

@jbpatelgit I've merged the fix in the develop branch.
Let me know if it fixes the problem for you.

@shahsumit
Copy link
Author

Hi Gert,

We downloaded the code from the develop branch and the fix is working for us now. Could you please let us know when will this be released so that we can incorporate the new dll in our production environment.

@drieseng drieseng changed the title Sftp Client: Stack overflow exception in case of incorrect password. Stack overflow exception with servers that do not implement RFC 4252 correctly Oct 7, 2017
@drieseng drieseng changed the title Stack overflow exception with servers that do not implement RFC 4252 correctly Servers that do not implement RFC 4252 correctly may lead to stack overflow Oct 7, 2017
@drieseng
Copy link
Member

drieseng commented Oct 7, 2017

Beta 4 of SSH.NET version 2016.1.0 is now available on GitHub and via NuGet.

@drieseng drieseng closed this as completed Oct 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants