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