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