Repository navigation
Conversation
There was a problem hiding this comment.
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 ofstrlen(...)-based checks). - Replace
strncmp(..., data_len)with a fixed-sizememcmpguarded by a minimum-length check inwincng.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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.dataptris advanced bystrlen(AUTH_MAGIC) + 1but the preceding length check only enforcesdecoded.len >= strlen(AUTH_MAGIC). If the decoded key is truncated to exactlystrlen(AUTH_MAGIC)bytes (or missing the required NUL after the magic), this will movedataptrpast the end of the buffer and later pointer subtraction inssh2_check_length()becomes undefined behavior. Please require at leaststrlen(AUTH_MAGIC)+1bytes and validate the NUL terminator before incrementing.wincng.c:
The auth-magic check can succeed for truncated input because it compares only
data_lenbytes; ifdata_lenis shorter than the requiredstrlen(OPENSSL_PRIVATEKEY_AUTH_MAGIC)+1,strncmp()may return 0 and the code will still advancedata_buffer.dataptrpast the end ofdata. 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.