Marc
Marc

Reputation: 998

Core dump when trying libssh authentication

I'm following the tutorial to try to open and authenticate a SSH connection with libssh but after ssh_connect, whether I try password auth, public key auth or even none auth, the application crashes with a core dump.

      t@1 (l@1) program terminated by signal BUS (invalid address alignment)
      0xffffffff74cab0cc: CRYPTO_ctr128_encrypt+0x0124:       ldx      [%l5 + %i5], %o1
      [1] CRYPTO_ctr128_encrypt(0x100151320, 0x10014e5a0, 0x20, 0x10014f410, 0x10014e360, 0xffffffff7fffebdc), at 0xffffffff74cab0cc
      [2] AES_ctr128_encrypt(0x100151320, 0x10014e5a0, 0x20, 0x10014f410, 0xffffffff74c971c0, 0xffffffff7fffebdc), at 0xffffffff74c92908
      [3] aes_ctr128_encrypt(0x1001502a0, 0x100151320, 0x10014e5a0, 0xffffffff7fffebec, 0xffffffff72d00240, 0x0), at 0xffffffff7d42aa64
      [4] packet_encrypt(0x1001479f0, 0x100151320, 0x10014fab0, 0x1, 0x1001502a0, 0x20), at 0xffffffff7d43454c
      [5] packet_send2(0x1001479f0, 0x100151320, 0xffffffff7d462759, 0x125c00, 0x11, 0x11), at 0xffffffff7d433958
      [6] ssh_service_request(0x1001479f0, 0xffffffff7d482520, 0x100001440, 0x169bf8, 0x0, 0xffffffff7d4839a0), at 0xffffffff7d41eac0
      [7] ssh_userauth_request_service(0x1001479f0, 0xffffffffffef9ee0, 0x106000, 0xffffffff7d588640, 0x1751fc, 0xffffffff76e4f67c), at 0xffffffff7d413468
      [8] ssh_userauth_password(0x1001479f0, 0x1000017b6, 0x1000017cb, 0x31, 0xffffffff7fffefc0, 0xffffffff7d588640), at 0xffffffff7d414ce0
    =>[9] main(argc = 1, argv = 0xffffffff7ffff178), line 33 in "testSSH.cc"

I'm using libssh v.0.7.3 from C++ on Solaris 10. Here is the code that reproduce the problem:

#include <libssh/libssh.h> 
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    ssh_session m_ssh_session;

    m_ssh_session = ssh_new();
    if (m_ssh_session == NULL) {
        cerr << "Failed to create SSH session : returned null" << endl;
        exit(1);
    }

    int port = 22;
    ssh_options_set(m_ssh_session, SSH_OPTIONS_HOST, "my_hostname");
    ssh_options_set(m_ssh_session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(m_ssh_session, SSH_OPTIONS_USER, "my_username");

    int rc = ssh_connect(m_ssh_session);
    if (rc != SSH_OK) {
        cerr << "Failed to connect SSH session: (" << rc << ") " << ssh_get_error(m_ssh_session) << endl;
        exit(1);
    }

    int state = ssh_is_server_known(m_ssh_session);
    if (state != SSH_SERVER_KNOWN_OK) {
        cerr << "Server unknown (" << state << ")" << endl;
        exit(1);
    }

    rc = ssh_userauth_password(m_ssh_session, NULL, "my_password");
    if (rc == SSH_AUTH_ERROR) {
        cerr << "Authentication failed: (" << rc << ") " << ssh_get_error(m_ssh_session) << endl;
    }
}

Am I missing something ?

Upvotes: 0

Views: 378

Answers (2)

omar Enayet
omar Enayet

Reputation: 61

I had the very same problem. Turns out that the OpenSSL library 'libeay32.lib' that i linked into ssh.dll was not compatible and caused the crash. Validate OpenSSL files 'libeay32.lib' & 'libay32.dll' used.

Upvotes: 1

learning_frog
learning_frog

Reputation: 315

After your edit, I can compile and run your code without any problems.

The only suggestion I can make is for you to change from:

if (rc == SSH_AUTH_ERROR) {
    cerr << "Authentication failed: (" << rc << ") " << ssh_get_error(m_ssh_session) << endl;
}

To

if (rc != SSH_AUTH_SUCCESS) {
    cerr << "Authentication failed: (" << rc << ") " << ssh_get_error(m_ssh_session) << endl;
}

Also, you should invoke ssh_free at the end:

ssh_free(ssh_session);

Upvotes: 0

Related Questions