Wipe (some) secrets from memory after use
[tinc] / src / xalloc.h
1 #ifndef TINC_XALLOC_H
2 #define TINC_XALLOC_H
3
4 /*
5    xalloc.h -- malloc and related functions with out of memory checking
6    Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
7    Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc., Foundation,
21    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "system.h"
25
26 #include <assert.h>
27
28 static inline void *xmalloc(size_t n) ATTR_MALLOC;
29 static inline void *xmalloc(size_t n) {
30         void *p = malloc(n);
31
32         if(!p) {
33                 abort();
34         }
35
36         return p;
37 }
38
39 static inline void *xzalloc(size_t n) ATTR_MALLOC;
40 static inline void *xzalloc(size_t n) {
41         void *p = calloc(1, n);
42
43         if(!p) {
44                 abort();
45         }
46
47         return p;
48 }
49
50 static inline void *xrealloc(void *p, size_t n) {
51         p = realloc(p, n);
52
53         if(!p) {
54                 abort();
55         }
56
57         return p;
58 }
59
60 static inline char *xstrdup(const char *s) ATTR_MALLOC ATTR_NONNULL;
61 static inline char *xstrdup(const char *s) {
62         char *p = strdup(s);
63
64         if(!p) {
65                 abort();
66         }
67
68         return p;
69 }
70
71 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
72 #ifdef HAVE_WINDOWS
73         char buf[1024];
74         int result = vsnprintf(buf, sizeof(buf), fmt, ap);
75
76         if(result < 0) {
77                 abort();
78         }
79
80         *strp = xstrdup(buf);
81 #else
82         int result = vasprintf(strp, fmt, ap);
83
84         if(result < 0) {
85                 abort();
86         }
87
88 #endif
89         return result;
90 }
91
92 static inline int xasprintf(char **strp, const char *fmt, ...) ATTR_FORMAT(printf, 2, 3);
93 static inline int xasprintf(char **strp, const char *fmt, ...) {
94         va_list ap;
95         va_start(ap, fmt);
96         int result = xvasprintf(strp, fmt, ap);
97         va_end(ap);
98         return result;
99 }
100
101 // Zero out a block of memory containing sensitive information using whatever secure
102 // erase function is available on the platform (or an unreliable fallback if none are).
103 // The pointer must not be NULL. Length can be zero, in which case the call is a noop.
104 static inline void memzero(void *buf, size_t buflen) ATTR_NONNULL;
105 static inline void memzero(void *buf, size_t buflen) {
106         assert(buf);
107
108         if(!buflen) {
109                 return;
110         }
111
112 #if defined(HAVE_EXPLICIT_BZERO)
113         explicit_bzero(buf, buflen);
114 #elif defined(HAVE_EXPLICIT_MEMSET)
115         explicit_memset(buf, 0, buflen);
116 #elif defined(HAVE_MEMSET_S)
117         errno_t err = memset_s(buf, buflen, 0, buflen);
118         assert(err == 0);
119 #elif defined(HAVE_WINDOWS)
120         SecureZeroMemory(buf, buflen);
121 #else
122         volatile uint8_t *p = buf;
123
124         while(buflen--) {
125                 *p++ = 0;
126         }
127
128 #endif
129 }
130
131 // Zero out a buffer of size `len` located at `ptr` and free() it.
132 // Does nothing if called on NULL.
133 static inline void xzfree(void *ptr, size_t len) {
134         if(ptr) {
135                 memzero(ptr, len);
136                 free(ptr);
137         }
138 }
139
140 // Zero out a NULL-terminated string using memzero() and then free it.
141 // Does nothing if called on NULL.
142 static inline void free_string(char *str) {
143         if(str) {
144                 xzfree(str, strlen(str));
145         }
146 }
147
148 #endif // TINC_XALLOC_H