Hi,
I've received a bug report saying that Nettle functions leave secret values lying around on the stack or in deallocated heap memory when they return.
Nettle generally does not take any steps to wipe or cleanup memory that might hold secrets when no longer used. Which I think differs from many other crypto libraries.
In the past, I have argued that an application can't really know which parts of its user data is secret and which isn't. Not only cryptographic keys are sensitive, the plaintext protected by those keys might be as sensitive. And sensitive data is typically handled also by non-cryptographic applications, say your text editor or email program or ai assistant. For this reason, it ought to be the responsibility of the operating system to protect each application's memory, with techniques like MMU-based memory protection, and encryption of any memory pages swapped out to disk.
So my question: Would it improve security if Nettle is changed to make more of an effort of explicitly clear deallocated (or otherwise no longer used) memory on the heap and the stack? Or maybe make an mainly regarding the stack; an applications can configure custom heap allocation functions, and that way ensure that memory is cleared on free, but there's no corresponding application control over Nettle's stack usage.
Regards, /Niels
Hi Niels,
Niels Möller nisse@lysator.liu.se writes:
So my question: Would it improve security if Nettle is changed to make more of an effort of explicitly clear deallocated (or otherwise no longer used) memory on the heap and the stack? Or maybe make an mainly regarding the stack; an applications can configure custom heap allocation functions, and that way ensure that memory is cleared on free, but there's no corresponding application control over Nettle's stack usage.
While I cannot speak extensively to the security implications of this approach, we include this patch[1] in the gnutls package in CentOS Stream, to clear stack-allocated objects in Nettle to satisfy FIPS 140-3 zeroization requirements.
For heap allocated objects, applications indeed have an option to install custom memory allocation functions, though implementing this is usually not straightforward[2][3].
Footnotes: [1] https://gitlab.com/redhat/centos-stream/rpms/gnutls/-/blob/c10s/nettle-3.8-z...
[2] https://gitlab.com/gnutls/gnutls/-/merge_requests/1554
[3] https://gitlab.com/gnutls/gnutls/-/merge_requests/1635
Regards,
nettle-bugs@lists.lysator.liu.se