Repository navigation
Aligned allocators - #11949
Aligned allocators#11949aidankeefe2022 wants to merge 5 commits into
Conversation
New set of tests and clean up
39dea99 to
9e875ab
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Memory-callback-disabled builds omit required allocator implementations, causing undefined references, while the new 4096-byte allocation test incorrectly fails valid low-resource static-memory configurations.
2 open issues
What changed in this PR
This PR adds aligned allocation and scrubbed reallocation APIs to wolfSSL’s memory layer, using existing allocator backends to preserve heap and allocation-type handling.
Changes:
- Adds aligned allocation APIs, override macros, and stack/heap declaration helpers.
- Uses scrubbed reallocation for big-integer storage and plaintext private-key buffers.
- Adds allocator tests and documents the new APIs and macros.
| File | Description |
|---|---|
wolfssl/wolfcrypt/types.h |
Adds allocator macros and aligned stack/heap helpers. |
wolfssl/wolfcrypt/memory.h |
Declares new APIs and exposes the zeroization test hook. |
wolfcrypt/test/test.c |
Adds alignment, reallocation, and scrubbing tests; adopts aligned AES allocation. |
wolfcrypt/src/memory.c |
Implements aligned allocation and scrubbed reallocation. |
wolfcrypt/src/integer.c |
Scrubs old digit storage when growing big integers. |
src/pk.c |
Scrubs old private-key DER storage during buffer growth. |
doc/dox_comments/header_files/types.h |
Documents allocator macros and their contracts. |
doc/dox_comments/header_files/memory.h |
Documents the five new public memory APIs. |
Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11949
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: 6 of 6 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: Lite
|
retest this please |
|
| #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) | ||
| #ifndef XMALLOCALIGN | ||
| #define XMALLOCALIGN(a, s, h, t) ((void)(a), (void)(h), (void)(t), malloc(s)) | ||
| #define XFREEALIGN(p, h, t) do { (void)(h); (void)(t); free(p); } while (0) | ||
| /* reallocation for aligned allocations */ | ||
| #define XREALLOCALIGN(p, align, size, h, t) \ | ||
| ((void)(align), (void)(h), (void)(t), realloc(p, size)) | ||
| /* reallocation with scrubbing of lost blocks for aligned allocations */ | ||
| #define XREALLOCALIGN_SCRUBBED(p, align, old_size, size, h, t) \ | ||
| ((void)(align), wc_ReallocScrubbed(p, old_size, size, h, t)) | ||
| #endif /* XMALLOCALIGN */ | ||
| #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) */ |
| XMEMCPY(new_p, ptr, (old_size < size) ? old_size : size); | ||
| wc_ReallocScrub(ptr, old_size); | ||
| XFREE(ptr, heap, tag); |
| wc_ReallocAlign(p, align, size, h, t) | ||
| /* reallocation with scrubbing of lost blocks for aligned allocations */ | ||
| #define XREALLOCALIGN_SCRUBBED(p, align, old_size, size, h, t) \ | ||
| ((void)old_size, wc_ReallocAlignScrubbed(p, align, size, h, t)) |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11949
Scan targets checked: linuxkm-src, linuxkm-bugs, wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): cmake/functions.cmake, doc/dox_comments/header_files/memory.h, doc/dox_comments/header_files/types.h, src/include.am
Coverage: 3 of 3 in-scope changed file(s) opened by the reviewer
Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
| #endif /* !XMALLOC_USER && !XMALLOC_OVERRIDE */ | ||
| #endif | ||
|
|
||
| #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0) |
There was a problem hiding this comment.
Kernel 5.4 XMALLOCALIGN override drops alignment on kernels without natural kmalloc alignment · Kernel memory safety
On kernels before 5.4, XMALLOCALIGN/XREALLOCALIGN throw away a/align and call plain malloc/realloc (kmalloc/krealloc). Those kernels only guarantee ARCH_KMALLOC_MINALIGN, so callers that need 16/64-byte alignment (e.g. for AES-NI) get misaligned buffers. The gate looks inverted.
Suggested fix: Remove the pre-5.4 override so these kernels use the generic wc_MallocAlign family, which enforces the requested alignment.
Basis: Linux Documentation/core-api/memory-allocation.rst (since v5.4): kmalloc gives at least ARCH_KMALLOC_MINALIGN, plus natural alignment for power-of-two sizes; earlier kernels only promise ARCH_KMALLOC_MINALIGN.
Related known findings (similar but distinct; listed for context, not part of this finding)
- F-6395 (duplicate): Closest match is [Bug]: STM32 ECC broken on all curves except four #6396 (canonical F-6395), WC_LINUXKM_ROUND_UP_P_OF_2. (1) Function: the candidate is the XMALLOCALIGN/XREALLOCALIGN macros in the pre-5.4 branch. [Bug]: STM32 ECC broken on all curves except four #6396 is the size-rounding macro used by the kmalloc/kvmalloc wrappers. The two are different macros at different lines. (2) Operation: the candidate discards the requested alignment and calls plain malloc/realloc. [Bug]: STM32 ECC broken on all curves except four #6396 evaluates __builtin_clzl(0) when the size is 1. (3) Root cause: the candidate is an inverted kernel-version gate that
|
|
||
| /* non-power-of-2 alignment: rejected, original intact. */ | ||
| if (ret == 0) { | ||
| q = (byte*)XREALLOCALIGN(p, 24U, 1600U, HEAP_HINT, |
There was a problem hiding this comment.
memalign_realloc_test fails under the in-tree linuxkm (5.4) XREALLOCALIGN override, which ignores alignment · Logic errors
The test expects XREALLOCALIGN(p, 24U, ...) to fail. On kernels older than 5.4, the new linuxkm override maps it to realloc(p, size) and drops align, so the call succeeds and the linuxkm module self-test fails. The same override also leaves XMALLOCALIGN without any alignment guarantee there, and the alignment checks in simple_memalign_test depend on that guarantee.
Suggested fix: Make the linuxkm <5.4 override honor alignment (or drop it so wc_MallocAlign/wc_ReallocAlign are used), or guard these macro-level assertions to builds where the defaults are in effect.


Description
This is a set of allocators that manually align the allocation to what ever alignment is passed in.
There is also a new XREALLOC_SCRUBBED that is introduced.
This PR also changes the configurations to always compile memory.c this adds 1.9 KB to the binary size in --disable-memory builds. Let me know if we want to find a fix for this
Align Allocators
The purpose of these allocators is to enforce proper alignment when it is not handled for us https://fenrir.wolfssl.com/finding/11986 is an example.
This comes with 4 new public APIs
Each has a matching macro like XMALLOC etc.
These can be overridden by users same as XMALLOC but they have to be done as a group no mixing and matching implementations.
The alignment metadata is keep track of via a hidden header allocated before the start of the requested memory.
This allows for the use of XMALLOC and XFREE as our allocation backend meaning we automatically inherit any configuration such as static memory.
Scrubbed Realloc
This is somewhat not related to the first change. The scrubbed realloc wipes any chunk lost during reallocation.
Downside is that there is never an inplace reallocation. This is also true for ReallocAlign.
This is also another new API and Macro
Testing
A new battery of test was added along with this change to make sure every corner is covered
Checklist