From 8854fd70a9ec0e4c084cd565b58a8cd0613bec59 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 12 May 2026 09:52:39 -0700 Subject: [PATCH] Zero key buffers on load failure - ForceZero newKey before WFREE in DoPemKey/DoOpenSshKey, covering both locally-allocated and caller-supplied buffers. - ForceZero PEM file buffer after read in wolfSSH_ReadKey_file - Track newKeySz for caller-supplied buffer path Issue: F-2885, F-3211, F-3212 --- src/ssh.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ssh.c b/src/ssh.c index ec0f6a36..5e261c48 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -1868,7 +1868,7 @@ static int DoPemKey(const byte* in, word32 inSz, byte** out, WOLFSSH_UNUSED(heap); if (*out == NULL) { - newKey = (byte*)WMALLOC(inSz, heap, DYNTYPE_PRIVKEY); + newKey = (byte*)WMALLOC(newKeySz, heap, DYNTYPE_PRIVKEY); if (newKey == NULL) { return WS_MEMORY_E; } @@ -1879,6 +1879,7 @@ static int DoPemKey(const byte* in, word32 inSz, byte** out, return WS_BUFFER_E; } newKey = *out; + newKeySz = *outSz; } /* If it is PEM, convert to ASN1 then process. */ @@ -1914,6 +1915,7 @@ static int DoPemKey(const byte* in, word32 inSz, byte** out, } else { WLOG(WS_LOG_DEBUG, "Unable to identify PEM key"); + ForceZero(newKey, newKeySz); if (*out == NULL) { WFREE(newKey, heap, DYNTYPE_PRIVKEY); } @@ -1943,6 +1945,7 @@ static int DoOpenSshKey(const byte* in, word32 inSz, byte** out, return WS_BUFFER_E; } newKey = *out; + newKeySz = *outSz; } in += WSTRLEN(PrivBeginOpenSSH); @@ -1970,6 +1973,7 @@ static int DoOpenSshKey(const byte* in, word32 inSz, byte** out, } else { WLOG(WS_LOG_DEBUG, "Unable to identify key"); + ForceZero(newKey, newKeySz); if (*out == NULL) { WFREE(newKey, heap, DYNTYPE_PRIVKEY); } @@ -2122,6 +2126,7 @@ int wolfSSH_ReadKey_file(const char* name, } WFCLOSE(NULL, file); + ForceZero(in, inSz); WFREE(in, heap, DYNTYPE_FILE); return ret;