1 |
#include <stdlib.h> |
2 |
#include <unistd.h> |
3 |
#include <sys/types.h> |
4 |
#include <string.h> |
5 |
#include <errno.h> |
6 |
#include <sys/mman.h> |
7 |
#include <sys/time.h> |
8 |
#include <sys/resource.h> |
9 |
|
10 |
|
11 |
#include "config.h" |
12 |
#include "imapfilter.h" |
13 |
|
14 |
|
15 |
extern unsigned int options; |
16 |
|
17 |
static secmem_t *smem = NULL; /* First node of secure memory linked list. */ |
18 |
|
19 |
|
20 |
/* |
21 |
* A malloc() that checks the results and dies in case of error. |
22 |
*/ |
23 |
void * |
24 |
xmalloc(size_t size) |
25 |
{ |
26 |
void *ptr; |
27 |
|
28 |
ptr = (void *)malloc(size); |
29 |
|
30 |
if (ptr == NULL) |
31 |
fatal(ERROR_MEMORY_ALLOCATION, |
32 |
"allocating memory; %s\n", strerror(errno)); |
33 |
|
34 |
return ptr; |
35 |
} |
36 |
|
37 |
|
38 |
/* |
39 |
* A realloc() that checks the results and dies in case of error. |
40 |
*/ |
41 |
void * |
42 |
xrealloc(void *ptr, size_t size) |
43 |
{ |
44 |
ptr = (void *)realloc(ptr, size); |
45 |
|
46 |
if (ptr == NULL) |
47 |
fatal(ERROR_MEMORY_ALLOCATION, |
48 |
"allocating memory; %s\n", strerror(errno)); |
49 |
|
50 |
return ptr; |
51 |
} |
52 |
|
53 |
|
54 |
/* |
55 |
* A free() that dies if fed with NULL pointer. |
56 |
*/ |
57 |
void |
58 |
xfree(void *ptr) |
59 |
{ |
60 |
if (ptr == NULL) |
61 |
fatal(ERROR_MEMORY_ALLOCATION, |
62 |
"NULL pointer given as argument"); |
63 |
free(ptr); |
64 |
} |
65 |
|
66 |
|
67 |
/* |
68 |
* A strdup() that checks the results and dies in case of error. |
69 |
*/ |
70 |
char * |
71 |
xstrdup(const char *s) |
72 |
{ |
73 |
char *cp; |
74 |
|
75 |
cp = strdup(s); |
76 |
|
77 |
if (cp == NULL) |
78 |
fatal(ERROR_MEMORY_ALLOCATION, |
79 |
"allocating memory; %s\n", strerror(errno)); |
80 |
|
81 |
return cp; |
82 |
} |
83 |
|
84 |
|
85 |
/* |
86 |
* Secure memory malloc(). Locks memory and keeps information about the |
87 |
* chunk that was allocated. |
88 |
*/ |
89 |
void * |
90 |
smalloc(size_t size) |
91 |
{ |
92 |
void *ptr; |
93 |
secmem_t *node; |
94 |
|
95 |
ptr = xmalloc(size); |
96 |
|
97 |
node = (secmem_t *) xmalloc(sizeof(secmem_t)); |
98 |
|
99 |
node->buf = ptr; |
100 |
node->size = size; |
101 |
node->prev = node->next = NULL; |
102 |
|
103 |
secmem_append(node); |
104 |
|
105 |
return ptr; |
106 |
} |
107 |
|
108 |
|
109 |
/* |
110 |
* Secure memory realloc(). Resize memory by allocating a new memory chunk |
111 |
* and NULL fill old memory, in order to protect sensitive data. |
112 |
*/ |
113 |
void * |
114 |
srealloc(void *ptr, size_t size) |
115 |
{ |
116 |
void *p; |
117 |
secmem_t *node; |
118 |
|
119 |
if (!(node = (secmem_t *) secmem_find(ptr))) { |
120 |
ptr = xrealloc(ptr, size); |
121 |
return ptr; |
122 |
} |
123 |
p = smalloc(size); |
124 |
memcpy(p, node->buf, min(node->size, size)); |
125 |
|
126 |
memset(node->buf, 0, node->size); |
127 |
secmem_remove(node); |
128 |
xfree(node->buf); |
129 |
xfree(node); |
130 |
|
131 |
return p; |
132 |
} |
133 |
|
134 |
|
135 |
/* |
136 |
* Secure memory free(). NULL fill memory before freeing it. |
137 |
*/ |
138 |
void |
139 |
sfree(void *ptr) |
140 |
{ |
141 |
secmem_t *node; |
142 |
|
143 |
if (!(node = (secmem_t *) secmem_find(ptr))) { |
144 |
xfree(ptr); |
145 |
return; |
146 |
} |
147 |
memset(node->buf, 0, node->size); |
148 |
secmem_remove(node); |
149 |
xfree(node->buf); |
150 |
xfree(node); |
151 |
} |
152 |
|
153 |
|
154 |
/* |
155 |
* Secure memory strdup(). Uses secure memory allocation. |
156 |
*/ |
157 |
char * |
158 |
sstrdup(const char *s) |
159 |
{ |
160 |
char *p; |
161 |
|
162 |
p = (char *)smalloc(strlen(s) + 1); |
163 |
xstrncpy(p, s, strlen(s)); |
164 |
|
165 |
return p; |
166 |
} |
167 |
|
168 |
|
169 |
/* |
170 |
* Append information about the newly allocated memory buffer. |
171 |
*/ |
172 |
void |
173 |
secmem_append(secmem_t * node) |
174 |
{ |
175 |
secmem_t *pos; |
176 |
secmem_t **app; |
177 |
|
178 |
app = &smem; |
179 |
pos = smem; |
180 |
|
181 |
while (pos) { |
182 |
node->prev = pos; |
183 |
app = &(pos->next); |
184 |
pos = pos->next; |
185 |
} |
186 |
|
187 |
*app = node; |
188 |
} |
189 |
|
190 |
|
191 |
/* |
192 |
* Find the record of a memory buffer in the secure memory linked list. |
193 |
*/ |
194 |
secmem_t * |
195 |
secmem_find(void *ptr) |
196 |
{ |
197 |
secmem_t *pos; |
198 |
|
199 |
pos = smem; |
200 |
|
201 |
while (pos != NULL && pos->buf != ptr) |
202 |
pos = pos->next; |
203 |
|
204 |
return pos; |
205 |
} |
206 |
|
207 |
|
208 |
/* |
209 |
* Remove a record of a secure memory buffer. |
210 |
*/ |
211 |
void |
212 |
secmem_remove(secmem_t * node) |
213 |
{ |
214 |
if (node->prev != NULL) |
215 |
node->prev->next = node->next; |
216 |
if (node->next != NULL) |
217 |
node->next->prev = node->prev; |
218 |
if (smem == node) |
219 |
smem = node->next; |
220 |
|
221 |
} |
222 |
|
223 |
|
224 |
/* |
225 |
* Overwrite/clear all secure memory. |
226 |
*/ |
227 |
void |
228 |
secmem_clear(void) |
229 |
{ |
230 |
secmem_t *p, *t; |
231 |
|
232 |
for (p = smem; p != NULL; p = t) { |
233 |
t = p->next; |
234 |
sfree(p->buf); |
235 |
} |
236 |
} |
237 |
|
238 |
|
239 |
/* |
240 |
* Disable core file dumping. |
241 |
*/ |
242 |
void |
243 |
corefile_disable(void) |
244 |
{ |
245 |
struct rlimit rl; |
246 |
|
247 |
getrlimit(RLIMIT_CORE, &rl); |
248 |
|
249 |
rl.rlim_cur = rl.rlim_max = 0; |
250 |
setrlimit(RLIMIT_CORE, &rl); |
251 |
} |