| 1 | /*- | ||
| 2 | * Copyright 2014 Colin Percival | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * | ||
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 24 | * SUCH DAMAGE. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #ifndef _INSECURE_MEMZERO_H_ | ||
| 28 | #define _INSECURE_MEMZERO_H_ | ||
| 29 | |||
| 30 | #ifdef SKIP_MEMZERO | ||
| 31 | #define insecure_memzero(buf, len) /* empty */ | ||
| 32 | #else | ||
| 33 | |||
| 34 | #include <stddef.h> | ||
| 35 | |||
| 36 | /* Pointer to memory-zeroing function. */ | ||
| 37 | extern void (* volatile insecure_memzero_ptr)(volatile void *, size_t); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * insecure_memzero(buf, len): | ||
| 41 | * Attempt to zero ${len} bytes at ${buf} in spite of optimizing compilers' | ||
| 42 | * best (standards-compliant) attempts to remove the buffer-zeroing. In | ||
| 43 | * particular, to avoid performing the zeroing, a compiler would need to | ||
| 44 | * use optimistic devirtualization; recognize that non-volatile objects do not | ||
| 45 | * need to be treated as volatile, even if they are accessed via volatile | ||
| 46 | * qualified pointers; and perform link-time optimization; in addition to the | ||
| 47 | * dead-code elimination which often causes buffer-zeroing to be elided. | ||
| 48 | * | ||
| 49 | * Note however that zeroing a buffer does not guarantee that the data held | ||
| 50 | * in the buffer is not stored elsewhere; in particular, there may be copies | ||
| 51 | * held in CPU registers or in anonymous allocations on the stack, even if | ||
| 52 | * every named variable is successfully sanitized. Solving the "wipe data | ||
| 53 | * from the system" problem will require a C language extension which does not | ||
| 54 | * yet exist. | ||
| 55 | * | ||
| 56 | * For more information, see: | ||
| 57 | * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html | ||
| 58 | * http://www.daemonology.net/blog/2014-09-06-zeroing-buffers-is-insufficient.html | ||
| 59 | */ | ||
| 60 | static inline void | ||
| 61 | 42 | insecure_memzero(volatile void * buf, size_t len) | |
| 62 | { | ||
| 63 | |||
| 64 | 42 | (insecure_memzero_ptr)(buf, len); | |
| 65 | 42 | } | |
| 66 | |||
| 67 | #endif | ||
| 68 | |||
| 69 | #endif /* !_INSECURE_MEMZERO_H_ */ |