Skip to content

AES and incorrect use or _malloca() and _freea() under Microsoft compilers #302

@noloader

Description

@noloader

John Byrd privately reported a crash in AES under Microsoft compilers due to use of _malloca, AliasedWithTables and _freea. _malloca and _freea are Microsoft SDLC functions (alloca is on Microsoft's SDLC banned function list). Microsoft sometimes uses the heap rather than the stack for _malloca, and that's the reason _freea is needed.

The bug is specific to Windows and Microsoft compilers because its guarded by _MSC_VER. The bug does not affect Unix and Linux; and does not affect non-Microsoft compilers, like ICC and Borland. The bug was introduced at Commit 823bc93357da32a3 and only affects Crypto++ 5.6.4.

The code in question asks _malloca for a block of memory. The code also over-commits the size and adjust the pointer to a 256-byte boundary. The pointer is sent AliasedWithTables to flush cache lines. If AliasedWithTables cannot perform the flush, then it returns false. Upon the false return, the code reallocates, adjusts the pointer and calls AliasedWithTables again. Eventually the call succeeds. After the code in question completes, it frees the adjusted pointer and not the original pointer using _freea.

We believe we failed to detect the issue during testing because the pointer did not require adjustment. We got [un]lucky, but Byrd's testing revealed it.

Fortune favored us, and we picked up Byrd's suggestion of avoiding unaligned data access last week at at Commit f57c4dced5bfbcd1. The issue is side stepped "out of the box" in Master. However, an unsuspecting user could wander back into the configuration.

We immediately disclosed the memory bug so decision makers could assess the risk, provide feedback, and select a remediation commensurate with the security posture and comfort level:

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions