Repository navigation
Conversation
When using the 'mlkem1024nistp384' KEX method, where only 768 bytes were zeroed. Also generalize to store key size on generation and zero that size on cleanup. Reported by GitHub Code Quality === Original LLM-generated report: "`kex_method_mlkem_nistp_cleanup` is shared between both `mlkem768nistp256` and `mlkem1024nistp384` key exchange methods, but it hardcodes `SSH2_MLKEM_768_PUBLIC_KEY_LEN` and `SSH2_MLKEM_768_PRIVATE_KEY_LEN` when zeroing the key buffers. For the 1024 variant, the actual allocations use `SSH2_MLKEM_1024_PUBLIC_KEY_LEN` and `SSH2_MLKEM_1024_PRIVATE_KEY_LEN`, which are larger. This means `ssh2_explicit_zero` will zero fewer bytes than were allocated, leaving residual sensitive key material in memory. The cleanup function needs to be aware of which variant is in use and apply the correct key length constants, or the caller should pass the key length as a parameter."
There was a problem hiding this comment.
Pull request overview
This PR fixes a sensitive cleanup bug in the ML-KEM hybrid KEX implementations by ensuring the full allocated ML-KEM public/private key buffers are explicitly zeroed before being freed, including the mlkem1024nistp384 variant.
Changes:
- Track ML-KEM public/private key buffer lengths in
key_exchange_state_lowat generation time. - Use the stored lengths during cleanup to zero the full key buffers (rather than hardcoding the 768-byte variant sizes).
- Reset stored ML-KEM key lengths to
0after freeing to avoid stale state.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libssh2_priv.h | Adds per-key ML-KEM buffer length fields to the key exchange state so cleanup can wipe the correct number of bytes. |
| src/kex.c | Stores the correct ML-KEM key lengths during key generation and uses them during cleanup to fully zero keys for both 768 and 1024 variants. |
💡 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.
When using the 'mlkem1024nistp384' KEX method, where the zeroing was
done with mlkem768 key lengths.
Also generalize to store key size on generation and zero that size on
cleanup.
Reported by GitHub Code Quality
Follow-up to 3ba252e #1644
Original LLM-generated report:
"
kex_method_mlkem_nistp_cleanupis shared between bothmlkem768nistp256andmlkem1024nistp384key exchange methods, but ithardcodes
SSH2_MLKEM_768_PUBLIC_KEY_LENandSSH2_MLKEM_768_PRIVATE_KEY_LENwhen zeroing the key buffers. For the1024 variant, the actual allocations use
SSH2_MLKEM_1024_PUBLIC_KEY_LENandSSH2_MLKEM_1024_PRIVATE_KEY_LEN,which are larger. This means
ssh2_explicit_zerowill zero fewer bytesthan were allocated, leaving residual sensitive key material in memory.
The cleanup function needs to be aware of which variant is in use and
apply the correct key length constants, or the caller should pass the
key length as a parameter."