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