Repository navigation
Realloc scrubbed - #12128
Realloc scrubbed#12128aidankeefe2022 wants to merge 3 commits into
Conversation
wolfSSL_Malloc() handed out an IO buffer for any IN/OUT_BUFFER request without checking the requested size against the buffer's. A request larger than WOLFMEM_IO_SZ would get a buffer too small for it. - Fixed IO buffers (WOLFMEM_IO_POOL_FIXED) cannot grow, so fail such a request instead. - For the shared IO pool (WOLFMEM_IO_POOL), leave the IO buffer on its list and fall through to the general buckets, which may hold a block large enough. Add api.c coverage for both pool kinds.
Native realloc() may move a block and release the old one without clearing it, leaving secret bytes on the free list. Add XREALLOC_SCRUBBED(p, old_size, size, heap, type), backed by wc_ReallocScrubbed() in wc_port.c (built in every configuration), which allocates with XMALLOC(), copies, ForceZero()s the old block and then XFREE()s it. It never calls native realloc(), so it is also available with WOLFSSL_NO_REALLOC, and it handles fixed IO pools that hand back the same block. Use it where secrets are reallocated: - der_to_enc_pem_alloc() (pk.c): the DER being padded is a plaintext private key. - mp_grow() (integer.c): mp_ints routinely hold private values. Add wolfcrypt tests, including a WOLFSSL_CHECK_MEM_ZERO test that checks the old block is zero when freed (wc_MemZero_Add() is now WOLFSSL_TEST_VIS for this), and the doxygen entries.
There was a problem hiding this comment.
🟡 Changes recommended
The new conditional in memory.h leaves the header guard unterminated, causing every inclusion to fail preprocessing.
1 open issue
What changed in this PR
This PR adds scrubbed reallocation to wolfSSL’s memory layer so sensitive data is cleared before old buffers are released. It also prevents static I/O pools from returning undersized buffers.
Changes:
- Introduces
XREALLOC_SCRUBBEDand applies it to big-integer growth and private-key DER buffers. - Adds size checks for fixed and shared I/O pool allocations.
- Adds regression tests and API documentation.
| File | Description |
|---|---|
wolfssl/wolfcrypt/types.h |
Defines the scrubbed reallocation macro. |
wolfssl/wolfcrypt/memory.h |
Declares the new API and exposes a scrubbing test hook. |
wolfcrypt/test/test.c |
Tests reallocation, data preservation, failure handling, and scrubbing. |
wolfcrypt/src/wc_port.c |
Implements scrubbed reallocation using existing allocator hooks. |
wolfcrypt/src/memory.c |
Checks I/O buffer capacity before allocation. |
wolfcrypt/src/integer.c |
Scrubs old digit buffers during big-integer growth. |
tests/api.c |
Tests fixed and shared static I/O pool behavior. |
src/pk.c |
Uses scrubbed reallocation for private-key DER buffers. |
doc/dox_comments/header_files/types.h |
Documents XREALLOC_SCRUBBED. |
doc/dox_comments/header_files/memory.h |
Documents wc_ReallocScrubbed and provides a usage example. |
Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12128
Error: TimeoutExpired
|
retest this please |
|
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12128
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/memory.h, doc/dox_comments/header_files/types.h
Coverage: 8 of 8 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Max
|
retest this please |
dgarske
left a comment
There was a problem hiding this comment.
Tested on x86_64 (default, --disable-memory, --enable-staticmemory, heapmath + opensslextra with and without ASan/UBSan, --enable-all with WOLFSSL_CHECK_MEM_ZERO, C89 cryptonly), armhf heapmath under qemu, and the MemBrowse Cortex-M4/M0+ builds. All pass.
The wolfSSL_Malloc() IO pool change fixes a heap overflow: an IN/OUT buffer request larger than the IO block used to get the IO block anyway. Please say so in the PR title/body (or split it into its own PR) so it can be tracked and backported. It also needs a ChangeLog note: static-memory users without headroom in the general buckets will now see an out-of-memory error where they used to get silent corruption. The fallback is safe on free, since a fallback block is never WOLFMEM_IO_SZ and goes back to the general buckets.
On XREALLOC_SCRUBBED:
wc_ReallocScrubbedis only called from inside the library, so I would make itWOLFSSL_LOCAL(withWOLFSSL_TEST_VISfor test.c) rather thanWOLFSSL_API. As public API it is a permanent exported symbol whoseoldSzargument overruns the block if a caller gets it wrong. It is also linked into every shared library and every embedded link without--gc-sections, which is where the MemBrowse +128..192 B comes from: none of those configs call it.- Under
WOLFSSL_NO_FORCE_ZERO,memory.halready expects a user-suppliedForceZero(). Including misc.c unconditionally (as memory.c does) and callingForceZero()would dropwc_ReallocScrub()and the private volatile loop, which currently bypasses the user's implementation. mp_grow()loses glibc's in-place growth. On an i9 with--enable-heapmath, RSA-2048 is about 5-7% slower and DH-2048 agree about 5-10%. That may be fine for heapmath, but the comment calling it "an acceptable cost" should be a conscious decision.size == 0freesptrand returns NULL, which a caller cannot tell from a failure that leftptrvalid. No current caller passes 0, but rejecting it would be safer for a scrubbing helper.- Nits: put the macro next to the
XMALLOC/XREALLOCdefinitions in types.h;XMEMCPYinstead ofXMEMMOVE; trim thepk.c/integer.ccomments to one line.

Description
When reallocating, allow for lost memory to be scrubbed. This also comes with a fix for IO pool size checking in wc_malloc, which used to just under-allocate instead of failing.
Testing
Added tests to make sure that the IO pool change works and does not fail or corrupt memory.
Added tests to make sure that scrubbing is occurring and that errors are caught.
Checklist