| 1 | // Copyright (c) 2017-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file Copyright (c) 2009-2015 The Bitcoin Core developers |
| 30 | |
| 31 | #define __STDC_WANT_LIB_EXT1__ 1 |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <unistd.h> |
| 36 | #ifdef HAVE_EXPLICIT_BZERO |
| 37 | #include <strings.h> |
| 38 | #endif |
| 39 | #include "memwipe.h" |
| 40 | |
| 41 | #if defined(_MSC_VER) |
| 42 | #define SCARECROW \ |
| 43 | __asm; |
| 44 | #else |
| 45 | #define SCARECROW \ |
| 46 | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
| 47 | #endif |
| 48 | |
| 49 | #ifdef HAVE_MEMSET_S |
| 50 | |
| 51 | void *memwipe(void *ptr, size_t n) |
| 52 | { |
| 53 | if (n > 0 && memset_s(ptr, n, 0, n)) |
| 54 | { |
| 55 | #ifdef NDEBUG |
| 56 | fprintf(stderr, "Error: memset_s failed\n"); |
| 57 | _exit(1); |
| 58 | #else |
| 59 | abort(); |
| 60 | #endif |
| 61 | } |
| 62 | SCARECROW // might as well... |
| 63 | return ptr; |
| 64 | } |
| 65 | |
| 66 | #elif defined HAVE_EXPLICIT_BZERO |
| 67 | |
| 68 | void *memwipe(void *ptr, size_t n) |
| 69 | { |
| 70 | if (n > 0) |
| 71 | explicit_bzero(ptr, n); |
| 72 | SCARECROW |
| 73 | return ptr; |
| 74 | } |
| 75 | |
| 76 | #else |
| 77 | |
| 78 | /* The memory_cleanse implementation is taken from Bitcoin */ |
| 79 | |
| 80 | /* Compilers have a bad habit of removing "superfluous" memset calls that |
| 81 | * are trying to zero memory. For example, when memset()ing a buffer and |
| 82 | * then free()ing it, the compiler might decide that the memset is |
| 83 | * unobservable and thus can be removed. |
| 84 | * |
| 85 | * Previously we used OpenSSL which tried to stop this by a) implementing |
| 86 | * memset in assembly on x86 and b) putting the function in its own file |
| 87 | * for other platforms. |
| 88 | * |
| 89 | * This change removes those tricks in favour of using asm directives to |
| 90 | * scare the compiler away. As best as our compiler folks can tell, this is |
| 91 | * sufficient and will continue to be so. |
| 92 | * |
| 93 | * Adam Langley <[email protected]> |
| 94 | * Commit: ad1907fe73334d6c696c8539646c21b11178f20f |
| 95 | * BoringSSL (LICENSE: ISC) |
| 96 | */ |
| 97 | static void memory_cleanse(void *ptr, size_t len) |
| 98 | { |
| 99 | memset(ptr, 0, len); |
| 100 | |
| 101 | /* As best as we can tell, this is sufficient to break any optimisations that |
| 102 | might try to eliminate "superfluous" memsets. If there's an easy way to |
| 103 | detect memset_s, it would be better to use that. */ |
| 104 | SCARECROW |
| 105 | } |
| 106 | |
| 107 | void *memwipe(void *ptr, size_t n) |
| 108 | { |
| 109 | if (n > 0) |
| 110 | memory_cleanse(ptr, n); |
| 111 | SCARECROW |
| 112 | return ptr; |
| 113 | } |
| 114 | |
| 115 | #endif |