Skip to content

openssl: fix potential OOB read/write with AES-GCM in ssh2_cipher_crypt() - #2401

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:openssl-cipher-ndebug-bounds-check
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:openssl-cipher-ndebug-bounds-check

Conversation

@vszakats

@vszakats vszakats commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

By applying two bounds checks to non-debug builds.

Reported-by: Vladimir Eli Tokarev
Fixes GHSA-c4f7-cvfc-33j7
Reported-by: Trzik on github
Fixes #1491
Thanks-to: Afonso Januário
Follow-up to 3c953c0 #797

By applying two bound checks to non-debug builds.

Reported-by: Vladimir Eli Tokarev
Fixes GHSA-c4f7-cvfc-33j7
Follow-up to 3c953c0 libssh2#797
Copilot AI review requested due to automatic review settings July 23, 2026 08:54
@vszakats
vszakats marked this pull request as draft July 23, 2026 08:54
@vszakats vszakats changed the title openssl: fix potential OOB read/write in ssh2_cipher_crypt() openssl: fix potential OOB read/write with AES-GCM in ssh2_cipher_crypt() Jul 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the OpenSSL cipher implementation by replacing debug-only assertions in ssh2_cipher_crypt() with runtime bounds checks, preventing potential out-of-bounds reads/writes in non-debug builds (including the AES-GCM first/last-block handling added in #797).

Changes:

  • Add runtime validation that blocksize fits within the local stack buffer and cannot underflow when subtracting AAD and authentication tag lengths.
  • Compute cryptlen only after validating bounds to avoid wrap/underflow scenarios in release builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vszakats
vszakats marked this pull request as ready for review July 23, 2026 09:07
@vszakats vszakats closed this in a2ed82d Jul 24, 2026
@vszakats
vszakats deleted the openssl-cipher-ndebug-bounds-check branch July 24, 2026 15:20
shivammathur added a commit to winlibs/libssh2 that referenced this pull request Aug 2, 2026
Backport upstream libssh2 runtime bounds checks for AES-GCM block processing in src/openssl.c.

CVE-2026-66033: pre-authentication integer underflow and out-of-bounds access in _libssh2_cipher_crypt via AES-GCM negotiation, remotely triggerable by a malicious SSH server in libssh2 through 1.11.1.

CWE: CWE-125, CWE-191

CVSS: 4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N (8.7 HIGH)

Upstream-Commit: a2ed82d40964bbc0d64cd717aa0a5a892117d2e6

Upstream-PR: libssh2/libssh2#2401
vszakats added a commit that referenced this pull request Aug 4, 2026
In addition to the original patch:
- simplify cipher object. drop the allocation and double redirection,
  drop redundant `encrypt` member, shorten/dedupe names, replace `algo`
  with `is_gcm`.
- replace heap alloc with stack buffer for GCM in `ssh2_cipher_crypt()`.
- use the algo argument instead of the context copy.
  (for speed and to match OpenSSL sibling code.)
- fix returned error value on GCM auth fail.
- increment IV on decrypt auth fail. Copilot suggestion. I cannot
  confirm this, so erring on the side caution to avoid a potential IV
  reuse.
- support mbedTLS builds without GCM built-in, via `MBEDTLS_GCM_C`.
- retrofit bounds check applied to OpenSSL backend earlier.
  Follow-up to a2ed82d #2401
- misc tidy-ups.

Original-patch-by: Adrian Moran
Closes #1764
Closes #2505
@vszakats

vszakats commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

Updated the PR message to mention that this patch also fixes #1491.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on decrypt operation using OpenSSL backend

2 participants