Selaa lähdekoodia

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.
Andreas Kling 4 vuotta sitten
vanhempi
commit
9a842ec419
2 muutettua tiedostoa jossa 8 lisäystä ja 0 poistoa
  1. 7 0
      Libraries/LibC/string.cpp
  2. 1 0
      Libraries/LibC/string.h

+ 7 - 0
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");
+}
 }

+ 1 - 0
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);