memcpy is leaking secret data through ZMM vector registers
Florian Weimer
fweimer@redhat.com
Wed Aug 12 16:28:24 GMT 2026
* Cristian Rodriguez:
> c
>
> On Fri, Apr 19, 2024 at 10:08 AM Mikulas Patocka <mpatocka@redhat.com> wrote:
>>
>> Hi
>>
>> As a part of LVM2, we are developing the libdevmapper library. The library
>> may be used to load cryptographic keys to the kernel, so it avoids leaking
>> the data to kernel memory and to the swap partition.
>>
>> After the use of cryptographic data, the libdevmapper library clears them
>> with memset and frees them afterwards. It executes __asm__ volatile("" :::
>> "memory") to thwart some compiler optimization regarding writing to
>> to-be-freed memory.
>>
>> We have a test "dmsecuretest.sh" that loads cryptographic keys into the
>> kernel, dumps a core, the core file is analyzed and if it contains the
>> key, the test fails.
>>
>> This test fails on AMD Zen 4 - the reason for the failure is that the
>> "memcpy" function uses ZMM registers for data copying. When memcpy exits,
>> the encryption key is present in the ZMM registers and the key remains
>> there even after both source and destination buffers of memcpy were
>> cleared.
>
> Isn't this what -fzero-call-used-regs thing is all about? can-t you
> just apply the same technique in the memcpy implementation and zero
> them out on return?
> vector registers are volatile after all.
The application might not know which vector registers glibc uses.
Ideally, we would zap the registers that we know we use in memset. It's
probably most straightforward to do in the memset assembler
implementation, as a build variant, but I haven't tried actually
implementing it.
Thanks,
Florian
More information about the Libc-alpha
mailing list