Skip to content

Crash on decrypt operation using OpenSSL backend #1491

Closed

Description

@Trzik

Hi, we noticed some crashes from our userbase after updating to v1.11.1. The stack trace seems consistent in all cases, the crash happens during initial handshake on memcpy call:

memcpy() at memcpy.asm(436)
_libssh2_cipher_crypt(evp_cipher_ctx_st * * ctx, const evp_cipher_st *(*)() algo, int encrypt, unsigned char * block, unsigned __int64 blocksize, int firstlast) at openssl.c(1132)
decrypt(_LIBSSH2_SESSION * session, unsigned char * source, unsigned char * dest, __int64 len, int firstlast) at transport.c(159)
_libssh2_transport_read(_LIBSSH2_SESSION * session) at transport.c(827)
_libssh2_packet_require(_LIBSSH2_SESSION * session, unsigned char packet_type, unsigned char * * data, unsigned __int64 * data_len, int match_ofs, const unsigned char * match_buf, unsigned __int64 match_len, packet_require_state_t * state) at packet.c(1503)
session_startup(_LIBSSH2_SESSION * session, unsigned __int64 sock) at session.c(830)
libssh2_session_handshake(_LIBSSH2_SESSION * session, unsigned __int64 sock) at session.c(878)

We're using OpenSSL 3.2.2. The issue was observed on Windows 10 and 11, 64b.
Reproduction steps are not currently known but the issue does not seem to be super rare, we already have couple of reports.

From the minidump I have at hand, I noticed that the size passed to memcpy function is negative. It comes from this piece of code in openssl.c:

/* length to encrypt */
const int cryptlen = (unsigned int)blocksize - aadlen - authenticationtag;

and is calculated as 4 - 0 - 16 and ends up being -12 which is definitely not supposed to happen. Looks like the input variable blocksize or algo (which affects authenticationtag) is not correct.

Activity

  1. vszakats commented on Nov 8, 2024

    @vszakats
    Member

    Thanks for your report. This calculation appeared in 1.11.0 with AES-GCM support, did you upgrade from that or from an earlier version?

  2. Trzik commented on Nov 8, 2024

    @Trzik
    Author

    We were using 1.11.0 for quite a while, the issue did not appear there.

  3. vszakats commented on Nov 8, 2024

    @vszakats
    Member

    It seems _libssh2_transport_read() is passing a too small numdecrypt value to the decrypt function?
    That area has seen modifications in 492bc54. If you can test
    without it or roll back before that patch, it might help locating this.

  4. vszakats commented on Mar 19, 2025

    @vszakats
    Member

    clang-tidy is reporting issues in this code part. Unclear if it's related, but 4 warnings in these functions may be worthy of attention.

  5. willco007 commented on Mar 20, 2025

    @willco007
    Member

    Link to clang tidy logs. #1561 (comment)

  6. afonsojanu commented on Sep 4, 2026

    @afonsojanu

    Looks like this is the same underlying issue as GHSA-c4f7-cvfc-33j7, which got fixed back in July (a2ed82d, closing #2401). The root cause matches exactly: cryptlen used to be guarded only by assert(cryptlen >= 0), and asserts get compiled out of release/NDEBUG builds, which is presumably why this only showed up for your userbase and not in debug testing. Current master replaces those asserts with a real bounds check before computing cryptlen, so a crafted or malformed packet can no longer drive it negative.

    Might be worth closing this as a duplicate of #2401 once a release with that fix ships, since the code path and the crash signature (negative size into memcpy inside ssh2_cipher_crypt) line up.

  7. vszakats commented on Sep 4, 2026

    @vszakats
    Member

    Closing as confirmed fixed by #2401. I'll also link that commit to this issue in the release notes.

    Thank you @afonsojanu!

    edit: Ref: 260fe5b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions