Skip to content

pem, wincng: fix checking for OpenSSH PEM magic string - #2022

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

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

Conversation

@vszakats

@vszakats vszakats commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

To make sure the null-terminator is present after the magic string.

Refs:
https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key
https://anongit.mindrot.org/openssh.git/tree/PROTOCOL.key

Reported by Copilot
Bug: #2014 (comment)
Bug: #2014 (comment)

Follow-up to 3e72343 #1315
Follow-up to 0309229 #248


Original reports:

pem.c:
decoded.dataptr is advanced by strlen(AUTH_MAGIC) + 1 but the preceding length check only enforces decoded.len >= strlen(AUTH_MAGIC). If the decoded key is truncated to exactly strlen(AUTH_MAGIC) bytes (or missing the required NUL after the magic), this will move dataptr past the end of the buffer and later pointer subtraction in ssh2_check_length() becomes undefined behavior. Please require at least strlen(AUTH_MAGIC)+1 bytes and validate the NUL terminator before incrementing.

wincng.c:
The auth-magic check can succeed for truncated input because it compares only data_len bytes; if data_len is shorter than the required strlen(OPENSSL_PRIVATEKEY_AUTH_MAGIC)+1, strncmp() may return 0 and the code will still advance data_buffer.dataptr past the end of data. That can lead to undefined behavior when later parsing reads from the buffer. Please enforce a minimum length and validate the required NUL terminator before incrementing the pointer.

@vszakats
vszakats requested a review from Copilot June 10, 2026 14:13
@vszakats vszakats changed the title openssh: fix checking for OpenSSH PEM magic string openssh: fix checking for OpenSSH PEM magic string in pem an wincng Jun 10, 2026
@vszakats vszakats changed the title openssh: fix checking for OpenSSH PEM magic string in pem an wincng openssh: fix checking for OpenSSH PEM magic string in pem and wincng Jun 10, 2026
@vszakats vszakats changed the title openssh: fix checking for OpenSSH PEM magic string in pem and wincng pem, wincng: fix checking for OpenSSH PEM magic string Jun 10, 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 pull request fixes unsafe OpenSSH “auth-magic” validation for OpenSSH-format private keys in both the generic PEM parser and the WinCNG ECDSA key loader, preventing out-of-bounds pointer advancement and subsequent undefined behavior on truncated inputs.

Changes:

  • Strengthen OpenSSH magic preflight checks to require the trailing NUL byte (by using sizeof("openssh-key-v1") instead of strlen(...)-based checks).
  • Replace strncmp(..., data_len) with a fixed-size memcmp guarded by a minimum-length check in wincng.c.
  • Advance parsing pointers by the validated magic length including NUL terminator, avoiding pointer-beyond-buffer UB for truncated keys.

Reviewed changes

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

File Description
src/pem.c Requires AUTH_MAGIC + NUL to be present and compares it with memcmp, preventing OOB pointer advancement on truncated decoded key data.
src/wincng.c Ensures OpenSSH magic comparison can’t succeed for truncated input and advances dataptr safely using the validated magic length.

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

@vszakats vszakats closed this in 56c12e9 Jun 10, 2026
@vszakats
vszakats deleted the bugs branch June 10, 2026 22:58
vszakats added a commit that referenced this pull request Jun 11, 2026
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