Repository navigation
Fix AES context alignment in heap allocations - #12110
danielinux wants to merge 13 commits into
Conversation
|
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12110
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Findings: 1
1 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: Max
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12110
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Coverage: 2 of 2 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
Fenrir's latest completed scan found no issues; clearing the prior automated change request.
|
@claude review |
There was a problem hiding this comment.
Beyond the inline findings, I also checked the pointer arithmetic in AesAlloc/AesAllocFree and found no problem with it. Rounding raw + sizeof(void*) up leaves room for the stored base pointer. The Aes object fits in the extra align - 1 + sizeof(void*) bytes. The base-pointer slot is pointer-aligned. The key-wrap allocation-failure paths return before reaching out:, so they never free a NULL aes.
Extended reasoning...
I checked the AesAlloc/AesAllocFree pointer arithmetic and the key-wrap small-stack cleanup paths in wolfcrypt/src/aes.c and found no problem in either. The inline comments already cover the open problems.
Findings marked 🟡 are optional suggestions and need no follow-up push.
dgarske
left a comment
There was a problem hiding this comment.
Reproduced F-11986 on a Raspberry Pi 4 (32-bit armhf binary, clang 18, -march=armv8-a -mfpu=crypto-neon-fp-armv8) and under qemu-arm. gcc 13 does not fault: it only puts :64 hints on stores into Aes. clang emits vst1.64 {d16-d17}, [r0 :128] on the iv == NULL branch of wc_AesSetIV() and in wc_AesKeyWrap(), so any NULL-IV wc_AesSetKey()/wc_AesSetIV() on a heap Aes that is 8 mod 16 faults.
With this PR applied and an allocator returning blocks at 8 mod 16, these still SIGBUS on the Pi 4:
wc_AesSetKey(aes, key, len, NULL, dir)on anXMALLOC(sizeof(Aes))wc_InitCmac()on anXMALLOC(sizeof(Cmac)), as inkdf.candssl_crypto.cwc_AesCmacGenerate()withWOLFSSL_SMALL_STACKwc_SRTP_KDF()
So this fixes key wrap and wc_AesNew(), but the class stays open across the other heap Aes/Cmac allocations (TLS record layer, DTLS 1.3, PKCS7, ECIES, HPKE, SHE, KDF, EVP).
The root cause is the declaration rather than the call sites. On 32-bit Arm, ALIGN16 promises the compiler more than malloc guarantees, and the 32-bit Arm AES/GCM assembly uses no alignment hints, so the over-alignment buys nothing there. Capping WOLFSSL_ALIGN() at 8 on 32-bit Arm with NEON, next to the existing IAR cap in types.h, removes every one of these faults on the Pi 4 and under qemu, and AES/GCM/CMAC throughput on the Pi 4 is unchanged. It leaves Cortex-M alone (no NEON). It does change the Aes/Cmac/Gcm layout on those builds, so it needs to land at a release boundary. With it in place, the helper in this PR is not needed.
Suggested patch, against current master:
diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h
--- a/wolfssl/wolfcrypt/types.h
+++ b/wolfssl/wolfcrypt/types.h
@@ -2005,6 +2005,10 @@ WOLFSSL_API word32 CheckRunTimeSettings(void);
* higher alignment. */
#if defined(__ICCARM__)
#define WOLFSSL_ALIGN(x) XALIGNED(8)
+ #elif defined(__arm__) && (defined(__ARM_NEON) || defined(__ARM_NEON__))
+ /* 32-bit Arm malloc only guarantees 8 bytes. Over-aligned heap
+ * contexts let the compiler emit faulting :128 NEON stores. */
+ #define WOLFSSL_ALIGN(x) XALIGNED(((x) > 8) ? 8 : (x))
#else
#define WOLFSSL_ALIGN(x) XALIGNED(x)
#endifNotes on the patch:
min(x, 8)rather than the flatXALIGNED(8)used for IAR, soALIGN4members keep their layout. Only types that were over-aligned change.- Every
ALIGNxxmacro is still#ifndef-guarded, so a port that really needs a 16-byte buffer on armhf can override it. - On armhf with the patch: no
:128/:256-hinted stores remain in the library, all of the cases above pass on the Pi 4 with every allocation at 8 mod 16, and--enable-all --enable-smallstacktestwolfcrypt and the API tests pass under the same allocator. - One port to check:
se050_port.cdeclaresalignedIn/alignedOutasALIGN16, and they become 8-aligned on an armhf NEON host build. - #11969 documents 16-byte alignment for these contexts. On 32-bit Arm with NEON that becomes 8, so the note needs that exception.
7da1ac7 to
36acd74
Compare
| WC_ALLOC_VAR_EX(cmac, Cmac, 1, NULL, DYNAMIC_TYPE_CMAC, | ||
| ret=MEMORY_E); | ||
| #ifdef WOLFSSL_SMALL_STACK | ||
| if ((cmac = CmacAlloc(NULL)) == NULL) |
There was a problem hiding this comment.
Why did you use CmacAlloc as the name? Its called from KDF and AES as well? Seems odd. Perhaps WC_CALLOC_VAR_EX? Making sure this feedback is public. I've shared this private.
There was a problem hiding this comment.
The object allocated is a Cmac struct in all three modules. The name is not prefixed because it's a static inline coming from cmac.h. The extra allocator layer is what can contain the requirement of potentially adding more headroom (which also needs to keep track of, thus the free path should be intercepted too).
Edit: I've tried to understand what you are suggesting here. The approach with WC_ALLOC_VAR_EX like in #11949 is too generic: there are only a few allocations that are affected by the finding.
I agree that the previous naming could be a bit confusing, so I've refactored it to have the inline allocators contained in their own header, and ready to replace XMALLOC/XFREE only for the affected locations without impacting on existing heap alignments.
A more generic approach can be easily swapped-in later on if #11949 really has to be merged, but so far I did not hear about any other places where the machine alignment is mismatched with its own accelerators, except the AES on NEON addressed here.
22e79d9 to
2c69d22
Compare
Description
Summary
Fix heap allocation alignment for Aes contexts used by wc_AesNew and the small-stack AES key-wrap wrappers.
Root cause
The reported armhf SIGBUS was initially attributed to _sp_div because the runtime PC was translated using the executable’s file offset. This PIE executable’s text virtual address is 0x10000 higher than that offset. Correcting the translation places the fault in wc_AesSetKeyLocal, at a NEON store to aes->reg that requires 16-byte alignment.
The fault address was only 8-byte aligned. Aes has 16-byte-aligned members, but XMALLOC can return an 8-byte-aligned pointer on 32-bit Arm. Passing that pointer as an Aes* lets the compiler emit an alignment-qualified store that faults.
Changes
The helper applies wherever alignment is enabled, regardless of architecture or assembly settings. Builds without WOLFSSL_USE_ALIGN continue to use ordinary XMALLOC and XFREE. The Aes structure layout is unchanged.
Relationship to #11949
PR 11949 adds general aligned-allocation APIs, but it does not switch wc_AesNew or the AES key-wrap wrappers to those APIs. It converts one test helper that already aligned its allocation manually. Merging PR 11949 alone therefore leaves the faulting allocations unchanged. If PR 11949 lands first, this change could use its XMALLOCALIGN/XFREEALIGN pair instead of the local helper; the AES callers still need to be updated.
Validation
Tracking