Skip to content

Return BAD_ALIGN_E from init for misaligned contexts on x86_64 - #12035

Open
Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:init-align-check
Open

Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:init-align-check

Conversation

@Frauschi

@Frauschi Frauschi commented Nov 6, 2026

Copy link
Copy Markdown
Member

Description

On x86_64, wc_Sha256, Aes, Hmac, curve25519_key and ed25519_key are 16-byte aligned types, and the compiler may zero them with aligned SSE stores. A context at an 8-byte offset, as in curl's HMAC code (curl/curl#23690), crashes inside init; init now returns BAD_ALIGN_E instead.

Changes

  • Add WC_ALIGNOF, WC_IS_ALIGNED and WC_CTX_IS_ALIGNED to types.h
  • Check alignment in the SHA-256/224, AES, HMAC, curve25519 and ed25519 init functions, including the crypto-cb-only, KCAPI and devcrypto variants
  • Copy functions reject a misaligned destination
  • Free functions wipe a misaligned context with ForceZero and return
  • Check is x86_64 only, off for kernel, freestanding and static memory builds, and disabled by WC_NO_CTX_ALIGN_CHECK
  • Toolchains without an alignof operator (_Alignof, __alignof__, __alignof) compile the check out; ALIGN16 is empty there too, so the types are not over-aligned
  • Document BAD_ALIGN_E in doxygen and the 16-byte allocator requirement in wolfSSL_SetAllocators

Limitations

  • Wrappers that memset an embedded context before calling its init (Cmac, WC_RNG, wc_HashAlg) are not covered

Testing

  • New test_wc_CtxInit_misaligned in tests/api.c runs init, copy and free on contexts 8 bytes past a 16-byte boundary

Copilot AI balanced review requested due to automatic review settings November 6, 2026 07:32
@Frauschi Frauschi self-assigned this Nov 6, 2026
@Frauschi

Frauschi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

@claude review

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new copy tests can crash or fail in supported C-only x86_64 configurations, and the documentation misleadingly suggests disabling runtime checks relaxes allocator alignment requirements.

3 open issues
What changed in this PR

This PR adds x86_64 context-alignment checks to wolfCrypt so affected initialization and copy APIs return BAD_ALIGN_E instead of risking aligned-store crashes, addressing the integration problem reported in curl.

Changes:

  • Add shared alignment macros and guards across selected cryptographic APIs and backends.
  • Wipe rejected misaligned contexts safely during cleanup and add regression tests.
  • Document alignment errors and custom allocator requirements.
File Description
wolfssl/​wolfcrypt/​types.h Define alignment helpers and platform guards.
wolfcrypt/​src/​sha256.c Guard SHA-256/224 initialization, copy, and cleanup.
wolfcrypt/​src/​port/​kcapi/​kcapi_hmac.c Guard KCAPI HMAC initialization and cleanup.
wolfcrypt/​src/​port/​kcapi/​kcapi_hash.c Guard KCAPI SHA initialization and copy.
wolfcrypt/​src/​port/​devcrypto/​devcrypto_hash.c Guard devcrypto SHA-256 initialization and copy.
wolfcrypt/​src/​hmac.c Guard HMAC initialization, copy, and cleanup.
wolfcrypt/​src/​ed25519.c Guard Ed25519 initialization and cleanup.
wolfcrypt/​src/​curve25519.c Guard Curve25519 initialization and cleanup.
wolfcrypt/​src/​aes.c Guard AES initialization and cleanup.
tests/​api.c Add misaligned-context regression tests.
doc/​dox_comments/​header_files/​sha256.h Document SHA initialization and copy alignment errors.
doc/​dox_comments/​header_files/​memory.h Document custom allocator alignment requirements.
doc/​dox_comments/​header_files/​ed25519.h Document Ed25519 initialization alignment errors.
doc/​dox_comments/​header_files/​curve25519.h Document Curve25519 initialization alignment errors.
doc/​dox_comments/​header_files/​aes.h Document AES initialization alignment errors.

Review effort: Balanced

Comment thread tests/api.c Resolved
Comment thread tests/api.c Resolved
Comment thread doc/dox_comments/header_files/memory.h Resolved
Comment thread wolfcrypt/src/sha256.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #12035

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfcrypt-port-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/aes.h, doc/dox_comments/header_files/curve25519.h, doc/dox_comments/header_files/ed25519.h, doc/dox_comments/header_files/memory.h, doc/dox_comments/header_files/sha256.h
Coverage: 10 of 10 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: Max

Comment thread wolfcrypt/src/port/kcapi/kcapi_hmac.c Resolved
Comment thread tests/api.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #12035

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfcrypt-port-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): .wolfssl_known_macro_extras, doc/dox_comments/header_files/aes.h, doc/dox_comments/header_files/curve25519.h, doc/dox_comments/header_files/ed25519.h, doc/dox_comments/header_files/memory.h, doc/dox_comments/header_files/sha256.h
Coverage: 3 of 4 in-scope changed file(s) opened by the reviewer; not opened: tests/api.c

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: Lite

Comment thread tests/api.c Resolved
wc_Sha256, Aes, Hmac, curve25519_key and ed25519_key are 16-byte
aligned types in default builds. A caller that places one at an offset
that is only 8-byte aligned, as curl's Curl_HMAC_init() did by putting
the hash contexts after a 24-byte header, crashes on x86_64: the
compiler zeroes the context with aligned SSE stores and the CPU faults.

Add WC_ALIGNOF() and WC_IS_ALIGNED() to types.h, plus
WC_CTX_IS_ALIGNED(), which checks a context pointer against its type's
alignment capped at 16, the alignment malloc() guarantees on x86_64.
WC_ALIGNOF() prefers C11 _Alignof, since GCC's __alignof__ reports the
preferred rather than the required alignment on some targets.
The init functions for these types now return BAD_ALIGN_E before
touching a misaligned context, following the existing BAD_ALIGN_E
checks on AES-NI and MMCAU buffers. This includes the crypto callback
only SHA-256/SHA-224 init, the KCAPI SHA-256, SHA-224 and HMAC init and
the devcrypto SHA-256 init, whose contexts keep the aligned fields. The
AF_ALG SHA-256 init and copy and the SE050 SHA-256/SHA-224 init check as
well: their contexts are not aligned types, but they share the generic
free, which returns early on a context below its type's alignment and
would leak their sockets or SE050 state. KCAPI wc_HmacSetKey() checks
too, so an Hmac keyed without init never owns a handle its free skips.
PSA has its own hash free and needs no check.
wc_Sha256Copy(), wc_Sha224Copy() and wc_HmacCopy(), including the KCAPI
and devcrypto versions, reject a misaligned destination the same way.

A rejected context is left unmodified. The matching free functions
wipe a misaligned context with ForceZero(), which does not assume the
type's alignment, and return without touching its fields, so the usual
cleanup that calls free after a failed init does not run on
uninitialized memory. Before this change that memory was at least
zeroed by init, and builds whose compiler emits unaligned stores did not
crash. The wipe keeps key material from a context that was keyed
without init, such as an Aes passed straight to wc_AesSetKey().

The check is limited to x86_64 user space, the only place a misaligned
context faults. Cortex-M builds that force WOLFSSL_USE_ALIGN get less
than 16-byte alignment from malloc(), so a check on other CPUs would
reject contexts wolfSSL allocates itself. Kernel builds compile without
SSE code generation and are excluded as well. So are freestanding builds
(__STDC_HOSTED__ is 0), whose RTOS or bare-metal malloc() may align to
8 only, and static memory builds, whose blocks are only as aligned as
the configured bucket sizes. Define WC_NO_CTX_ALIGN_CHECK to disable it;
that removes the check, not the alignment the types need.
The wolfSSL_SetAllocators() doxygen states the 16-byte requirement.

The API test covers init and copy into a misaligned context, and checks
that a rejected context is left untouched by init and copy and zeroed by
free. The copy cases run only for types aligned to 16, since copy frees
an accepted destination first. It is skipped for FIPS and selftest
builds, which take these init functions from frozen sources without the
check.

Forming a misaligned pointer is already undefined behavior, so this is
best effort; current gcc and clang keep the check.
@Frauschi

Frauschi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@Frauschi Frauschi assigned wolfSSL-Bot and unassigned Frauschi Nov 6, 2026
@Frauschi
Frauschi requested a review from douzzer November 6, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants