Skip to content

transport: fix potential heap overflow on ETM decrypt - #2198

Closed
vszakats wants to merge 6 commits into
libssh2:masterfrom
vszakats:transport
Closed

vszakats wants to merge 6 commits into
libssh2:masterfrom
vszakats:transport

Conversation

@vszakats

@vszakats vszakats commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Reported-by: RuiZhi Zhang
Reported-by: Hananto Adi
Reported-by: Vladimir Eli Tokarev
Fixes GHSA-6c79-444r-wx26

@VladimirEliTokarev

Copy link
Copy Markdown

looking good to me

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 pull request addresses a security issue in the SSH transport receive path by adding a size guard during Encrypt-Then-MAC (ETM) packet decryption to prevent a potential heap overflow when allocating/copying decrypted data.

Changes:

  • Add a lower-bound check for decrypt_size before allocating and copying blocksize - 1 bytes into the decrypt buffer.
  • Return LIBSSH2_ERROR_DECRYPT when the ETM decrypt buffer would be undersized.

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

Comment thread src/transport.c Outdated
Comment on lines +246 to +247
if(decrypt_size < blocksize - 1)
return LIBSSH2_ERROR_DECRYPT;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like a pre-existing issue to me, where several of the non-EAGAIN fail paths from
this function leak the p→payload buffer.

vszakats added 3 commits July 18, 2026 16:28
Reported-by: Vladimir Eli Tokarev
Fixes GHSA-6c79-444r-wx26

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/transport.c:242

  • decrypt_size is derived from attacker-controlled packet length in ETM mode, but the new guard only checks < blocksize - 1. That still allows values like decrypt_size == blocksize - 1 (or other non-block-aligned sizes), causing transport_decrypt(..., blocksize, ...) to consume bytes beyond the ciphertext boundary (into the MAC region) and potentially triggering the assert((len % blocksize) == 0) invariants on the follow-up decrypt. Also, the new early-return path leaves p->payload allocated.

Tighten the validation to require at least one full block and block alignment, and free p->payload before returning on failure.

                decrypt_size = p->total_num - mac_len - 4;
                if(decrypt_size < blocksize - 1)
                    return LIBSSH2_ERROR_DECRYPT;

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/transport.c Resolved
"`decrypt_size` is computed from `p->total_num` (size_t) and `mac_len`
(size_t) using unsigned arithmetic, then assigned to `ssize_t`. If
`p->total_num < mac_len + 4`, the subtraction underflows before the
assignment, and the conversion to `ssize_t` is implementation-defined.
That can make the subsequent bounds check unreliable and still allow
decrypting/allocating with an invalid size. Also, the first-block
decrypt reads `blocksize` bytes, so the encrypted portion should be
validated to be at least `blocksize` bytes (not just `blocksize - 1`)."

libssh2#2198 (comment)

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@vszakats

Copy link
Copy Markdown
Member Author

Also reported by: RuiZhi Zhang

vszakats added a commit that referenced this pull request Jul 22, 2026
Also added missed reported-by credits for:

Mateusz Gierblinski:
edec1cc #2197

RuiZhi Zhang from Gitee Codepecker Lab:
42e33d8 #2198
vszakats added a commit that referenced this pull request Jul 23, 2026
Also added missed reported-by credits for (in order reporting):

Hananto Adi:
42e33d8 #2198

Also:
- try sorting without taking into account the number of contributions.
shivammathur added a commit to winlibs/libssh2 that referenced this pull request Aug 2, 2026
Backport upstream libssh2 lower-bound validation for Encrypt-then-MAC packet decryption in src/transport.c.

CVE-2026-66035: pre-authentication heap buffer overflow via a packet length smaller than the negotiated cipher block size, remotely triggerable by a malicious SSH server in libssh2 through 1.11.1.

CWE: CWE-122

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

Upstream-Commit: 42e33d81577ed4b95d4b4f6f845e5ee8efe5eeb4

Upstream-PR: libssh2/libssh2#2198
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.

3 participants