Skip to content

kex: fix to zero the full ML-KEM keys on cleanup - #2192

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:mlkemsize
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:mlkemsize

Conversation

@vszakats

@vszakats vszakats commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

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_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."

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."

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 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_low at 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 0 after 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.

@vszakats vszakats closed this in ae6697a Jul 3, 2026
@vszakats
vszakats deleted the mlkemsize branch July 3, 2026 10:52
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