Repository navigation
fix(kex): zeroize intermediate copies of the KEX shared secret - #100
Conversation
The Zeroizing regime covered the combined secret K, the exchange hash, and derive_key's output, but four intermediate copies escaped it (threat model §4.3): - m and k_pq in hybrid_exchange: ml-kem 0.3's SharedKey is a plain array with no zeroize-on-drop, and m regenerates K_PQ given the public EK. K_PQ is now folded into the hash and erased before the classical half runs, so its error paths never leave it live. - derive_key's string(K) buffer, dropped plain on every KEX and re-key (four times per derive_cipher_pair). - exchange_hash's Writer buffer, which embeds K. - The per-round block in derive_key's extension loop. Also replaces ExchangeHashInputs's derive(Debug) — which printed K verbatim — with a redacted manual impl matching HybridOutcome and HostKey, plus a debug_never_leaks_the_shared_secret test following host_key.rs's pattern. Found by security review of the merged Phase-1 code; not remotely exploitable (requires a core dump or process-memory read), defence in depth per §4.3. Claude-Session: https://claude.ai/code/session_017JuwgCHx6Uv5E13g1BHAud Signed-off-by: Gonzalo Fleming <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR hardens the QuantumSSH core KEX implementation against memory disclosure by ensuring intermediate buffers containing KEX shared-secret material are zeroized promptly and never exposed via Debug, aligning with the repo’s threat model §4.3.
Changes:
- Zeroize intermediate ML-KEM and derived buffers in
hybrid_exchange(including wipingmandK_PQbefore entering the X25519 half). - Ensure serialized
string(K)and other transient derivation buffers inderive_keyandexchange_hashare erased on drop (viaZeroizing) and explicitly wipe per-iteration digest blocks. - Replace
ExchangeHashInputs’#[derive(Debug)]with a redacted manualDebugimpl and add a regression test preventing secret leakage in debug output.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
All checks pass: fmt clean, clippy zero warnings, 107 tests green (including the new PR Review:
|
| Criterion | Evidence |
|---|---|
| MANIFIESTO #1 (memory-safe) | No new unsafe; Zeroize/Zeroizing already audited. |
| MANIFIESTO #4 (sharp edges) | No new dependencies; Zeroize already in workspace deps. |
| Threat model §4.3 (key material) | Four copies now erased on drop; debug never leaks K. |
| Pre-auth surface | No changes to parser/wire path. |
| Fail closed | k_pq fold-in moved before classical error paths — early-reject no longer leaks PQ material. |
| Code conventions | Debug impl uses finish_non_exhaustive(); no stubs; clippy clean. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
Qué
Cierra las cuatro copias intermedias del secreto del KEX que escapaban al régimen
Zeroizing(threat model §4.3), todas enkex.rs:hybrid_exchange:m(la aleatoriedad de encapsulación, que junto a la EK pública recomputaK_PQcompleto) yk_pq—SharedKeydeml-kem0.3 es un array plano sin zeroize-on-drop (su featurezeroizesolo cubreDecapsulationKey/DecryptionKey).K_PQahora se incorpora al hash y se borra antes de la mitad clásica, así los early-returns de esa mitad (bad-hybrid-init-length,rng-failure,invalid-x25519-key) tampoco lo dejan vivo.derive_key: el bufferstring(K)se dropeaba plano en cada KEX y cada re-key (4 veces porderive_cipher_pair); ahora vive enZeroizing. El bloque[u8; 32]del loop de extensión también se borra.exchange_hash: el buffer delWriterque embebeKahora se borra on-drop.ExchangeHashInputs: suderive(Debug)imprimíaKverbatim; se reemplaza por un impl manual redactado, consistente conHybridOutcome,HostKeyyAuthorizedKeys, más testdebug_never_leaks_the_shared_secret(patrón dehost_key.rs).Por qué
Defensa en profundidad, no explotable por red: requiere core dump o lectura de memoria del proceso. El threat model §4.3 exige material de claves zeroizado tras uso, y el contraste con lo que sí estaba cubierto (
shared_secret,exchange_hash/session_id, salida dederive_key) hacía de estas copias el hueco restante.Hallazgo de una revisión de seguridad del código Phase-1 mergeado. Sin cambio de comportamiento observable: mismo protocolo, mismos bytes en el wire.
Validación
cargo fmt/clippy --workspace --all-targets -- -D warnings/cargo test --workspace: verdes (107 tests, +1 nuevo).Zeroizeya era dep del workspace.https://claude.ai/code/session_017JuwgCHx6Uv5E13g1BHAud