Use memset to zero stack allocations containing unions #16206
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Closes #16135.
Description
C99 6.7.8.17 says that when an undesignated initialiser is used, only the first element of a union is initialised. If the first element is not the largest within the union, how the remaining space is initialised is up to the compiler.
GCC extends the initialiser to the entire union, while Clang treats the remainder as padding, and so initialises according to whatever automatic/implicit initialisation rules are currently active.
When Linux is compiled with
CONFIG_INIT_STACK_ALL_PATTERN
,-ftrivial-auto-var-init=pattern
is added to the kernel CFLAGS. This flag sets the policy for automatic/implicit initialisation of variables on the stack.Taken together, this means that when compiling under
CONFIG_INIT_STACK_ALL_PATTERN
on Clang, the "zero" initialiser will only zero the first element in a union, and the rest will be filled with a pattern. This is significant foraes_ctx_t
, which inaes_encrypt_atomic()
andaes_decrypt_atomic()
is initialised to zero, but then used as agcm_ctx_t
, which is the fifth element in the union, and thus gets pattern initialisation. Later, it's assumed to be zero, resulting in a hang.As confusing and undiscoverable as it is, by the spec, we are at fault when we initialise a structure containing a union with the zero initializer. As such, this commit replaces these uses with an explicit
memset(0)
.How Has This Been Tested?
Full test suite run succeeded against a 6.7.12 kernel and this patch, both compiled using Clang 18.
Types of changes
Checklist:
Signed-off-by
.