Import the helper Linux uses to clear a buffer the compiler must not remove. include/linux/string.h has it as a static inline with a kernel-doc comment above it; this is that code, copied as it is, so a later fix upstream can be picked up the same way.
barrier_data() comes from <linux/compiler.h>, which Linux includes from that header too. Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=bec500777089b3c96c53681fc0aa6fee59711d4a Cc: Tom Rini <[email protected]> Signed-off-by: Xiaoyi Suo <[email protected]> --- include/linux/string.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 986499dfc..a51b39e5d 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -3,6 +3,7 @@ #include <linux/types.h> /* for size_t */ #include <linux/stddef.h> /* for NULL */ +#include <linux/compiler.h> /* for barrier_data() */ #ifdef __cplusplus extern "C" { @@ -106,6 +107,26 @@ extern void kfree_const(const void *x); #ifndef __HAVE_ARCH_MEMSET extern void * memset(void *,int,__kernel_size_t); + +/** + * memzero_explicit - Fill a region of memory (e.g. sensitive + * keying data) with 0s. + * @s: Pointer to the start of the area. + * @count: The size of the area. + * + * Note: usually using memset() is just fine (!), but in cases + * where clearing out _local_ data at the end of a scope is + * necessary, memzero_explicit() should be used instead in + * order to prevent the compiler from optimising away zeroing. + * + * memzero_explicit() doesn't need an arch-specific version as + * it just invokes the one of memset() implicitly. + */ +static inline void memzero_explicit(void *s, size_t count) +{ + memset(s, 0, count); + barrier_data(s); +} #endif #ifndef __HAVE_ARCH_MEMCPY extern void * memcpy(void *,const void *,__kernel_size_t); -- 2.43.0
