diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-09 20:04:18 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-09 20:04:18 +0100 |
commit | 9a842ec4192ea0cdd4d4e6b6ca45aff09195c64c (patch) | |
tree | d2242e414dd859e134db95fe68726d6477d88cc2 | |
parent | 4a83a37f798788332a06e0cef1bf6a81d5983be4 (diff) | |
download | serenity-9a842ec4192ea0cdd4d4e6b6ca45aff09195c64c.zip |
LibC: Add explicit_bzero()
This is a variant of bzero() that is guaranteed to not get optimized
away by the compiler. Useful for clearing out sensitive data.
-rw-r--r-- | Libraries/LibC/string.cpp | 7 | ||||
-rw-r--r-- | Libraries/LibC/string.h | 1 |
2 files changed, 8 insertions, 0 deletions
diff --git a/Libraries/LibC/string.cpp b/Libraries/LibC/string.cpp index 8daee4ef49..86a72b72a9 100644 --- a/Libraries/LibC/string.cpp +++ b/Libraries/LibC/string.cpp @@ -476,4 +476,11 @@ size_t strxfrm(char* dest, const char* src, size_t n) dest[i] = '\0'; return i; } + +void explicit_bzero(void* ptr, size_t size) +{ + memset(ptr, 0, size); + asm volatile("" :: + : "memory"); +} } diff --git a/Libraries/LibC/string.h b/Libraries/LibC/string.h index f181477ef3..40a2aabe7d 100644 --- a/Libraries/LibC/string.h +++ b/Libraries/LibC/string.h @@ -44,6 +44,7 @@ void* memchr(const void*, int c, size_t); const void* memmem(const void* haystack, size_t, const void* needle, size_t); void* memset(void*, int, size_t); +void explicit_bzero(void*, size_t) __attribute__((nonnull(1))); __attribute__((malloc)) char* strdup(const char*); __attribute__((malloc)) char* strndup(const char*, size_t); |