Skip to content

publickey: fix leaks when publickey_response_success() received <8 bytes - #2144

Closed
vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:pkeyleak
Closed

vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:pkeyleak

Conversation

@vszakats

@vszakats vszakats commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

@vszakats vszakats changed the title publickey: fix leak when publickey_response_success() received <3 bytes publickey: fix leaks when publickey_response_success() received <8 bytes Jun 30, 2026
@vszakats
vszakats requested a review from Copilot June 30, 2026 01:45

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 tightens error-path cleanup in the publickey subsystem by freeing the received response buffer when the response is shorter than expected, preventing leaks on malformed/truncated packets.

Changes:

  • Free data before returning on data_len < 4 in publickey_response_success().
  • Free data before returning on data_len < 8 in the STATUS response path in publickey_response_success().

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

Comment thread src/publickey.c
Comment on lines +239 to +243
if(data_len < 8) {
SSH2_FREE(session, data);
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
"Publickey response too small");
}

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.

→ PR #2147

@vszakats vszakats closed this in 1b3af90 Jun 30, 2026
@vszakats
vszakats deleted the pkeyleak branch June 30, 2026 02:19
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jun 30, 2026
"The size check for reading the 4-byte status field is incorrect:
`data_len < 8` doesn't account for the variable-length response name
that `publickey_response_id()` advances `s` past. A truncated packet
like `[len=6]["status"]` (10 bytes total) will pass the current check
but still cause an out-of-bounds read in `ssh2_ntohu32(s)`. The check
should ensure there are at least 4 bytes remaining at `s`."

Bug: libssh2#2144 (comment)
vszakats added a commit that referenced this pull request Jun 30, 2026
"The size check for reading the 4-byte status field is incorrect:
`data_len < 8` doesn't account for the variable-length response name
that `publickey_response_id()` advances `s` past. A truncated packet
like `[len=6]["status"]` (10 bytes total) will pass the current check
but still cause an out-of-bounds read in `ssh2_ntohu32(s)`. The check
should ensure there are at least 4 bytes remaining at `s`."

Reported by Copilot
Bug: #2144 (comment)

Closes #2147
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.

2 participants