Skip to content

publickey: fix potential heap OOB reads in publickey_init() - #2197

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

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

Conversation

@vszakats

@vszakats vszakats commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Reported-by: Mateusz Gierblinski
Reported-by: Haruki Oyama
Follow-up to ceaa7e1 #2147

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 parsing of publickey subsystem packets in publickey_init() to prevent potential heap out-of-bounds reads when handling truncated or malformed responses (follow-up to earlier response-length validation fixes).

Changes:

  • Update length checks to account for how publickey_response_id() advances the parsing pointer s (i.e., validate remaining bytes at s, not just total packet length).
  • Tighten bounds validation for STATUS response parsing before reading fixed-width fields.
  • Add an explicit bounds check before reading the VERSION response’s 4-byte version field.
Comments suppressed due to low confidence (1)

src/publickey.c:425

  • The new pre-switch length check applies to all response types, including ones that are intentionally ignored in the default: branch (and potentially SSH2_PUBLICKEY_RESPONSE_PUBLICKEY). This changes behavior: a packet containing only the response name (no trailing fields) would previously be ignored for unknown responses, but will now abort with BUFFER_TOO_SMALL before reaching the default: handler. Since each handled case already performs its own length validation, limit this check to the response types that actually require a 4-byte field here (STATUS/VERSION), or remove it entirely and rely on the per-case checks.
            if(session->pkeyInit_data_len <
               (size_t)(s - session->pkeyInit_data) + 4) {
                ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
                         "Public key init data too small");
                goto err_exit;
            }

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

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 vszakats closed this in edec1cc Jul 18, 2026
@vszakats
vszakats deleted the pubkeymore branch July 18, 2026 14:18
@vszakats

Copy link
Copy Markdown
Member Author

Also reported by Mateusz Gierblinski.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants