insecure_memzero.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #ifndef INSECURE_MEMZERO_H_
  2. #define INSECURE_MEMZERO_H_
  3. #include <stddef.h>
  4. /* Pointer to memory-zeroing function. */
  5. extern void (* volatile insecure_memzero_ptr)(volatile void *, size_t);
  6. /**
  7. * insecure_memzero(buf, len):
  8. * Attempt to zero ${len} bytes at ${buf} in spite of optimizing compilers'
  9. * best (standards-compliant) attempts to remove the buffer-zeroing. In
  10. * particular, to avoid performing the zeroing, a compiler would need to
  11. * use optimistic devirtualization; recognize that non-volatile objects do not
  12. * need to be treated as volatile, even if they are accessed via volatile
  13. * qualified pointers; and perform link-time optimization; in addition to the
  14. * dead-code elimination which often causes buffer-zeroing to be elided.
  15. *
  16. * Note however that zeroing a buffer does not guarantee that the data held
  17. * in the buffer is not stored elsewhere; in particular, there may be copies
  18. * held in CPU registers or in anonymous allocations on the stack, even if
  19. * every named variable is successfully sanitized. Solving the "wipe data
  20. * from the system" problem will require a C language extension which does not
  21. * yet exist.
  22. *
  23. * For more information, see:
  24. * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
  25. * http://www.daemonology.net/blog/2014-09-06-zeroing-buffers-is-insufficient.html
  26. */
  27. static inline void
  28. insecure_memzero(volatile void * buf, size_t len)
  29. {
  30. (insecure_memzero_ptr)(buf, len);
  31. }
  32. #endif /* !INSECURE_MEMZERO_H_ */