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