8 |
#include <sys/resource.h> |
#include <sys/resource.h> |
9 |
|
|
10 |
|
|
11 |
|
#include "config.h" |
12 |
#include "imapfilter.h" |
#include "imapfilter.h" |
13 |
|
|
14 |
|
|
15 |
extern unsigned int options; |
extern unsigned int options; |
16 |
|
#ifdef MEMORY_LOCK |
17 |
extern uid_t ruid, euid; |
extern uid_t ruid, euid; |
18 |
|
#endif |
19 |
|
|
20 |
static secmem_t *smem = NULL; /* First node of secure memory linked list. */ |
static secmem_t *smem = NULL; /* First node of secure memory linked list. */ |
21 |
|
|
87 |
*/ |
*/ |
88 |
void *smalloc(size_t size) |
void *smalloc(size_t size) |
89 |
{ |
{ |
90 |
|
#ifdef MEMORY_LOCK |
91 |
int r; |
int r; |
|
void *ptr; |
|
92 |
static int w = 0; |
static int w = 0; |
93 |
|
#endif |
94 |
|
void *ptr; |
95 |
secmem_t *node; |
secmem_t *node; |
96 |
|
|
97 |
ptr = xmalloc(size); |
ptr = xmalloc(size); |
98 |
|
|
99 |
|
#ifdef MEMORY_LOCK |
100 |
seteuid(euid); /* Gain root privileges. */ |
seteuid(euid); /* Gain root privileges. */ |
101 |
r = mlock(ptr, size); |
r = mlock(ptr, size); |
102 |
seteuid(ruid); /* Drop root privileges. */ |
seteuid(ruid); /* Drop root privileges. */ |
104 |
if (getuid() != geteuid()) |
if (getuid() != geteuid()) |
105 |
fatal(ERROR_SETUID, "imapfilter: failed to drop privileges\n"); |
fatal(ERROR_SETUID, "imapfilter: failed to drop privileges\n"); |
106 |
|
|
107 |
|
|
108 |
if (options & OPTION_WARNING && r && !w) { |
if (options & OPTION_WARNING && r && !w) { |
109 |
error("imapfilter: warning: using insecure memory\n"); |
error("imapfilter: warning: using insecure memory\n"); |
110 |
w = 1; |
w = 1; |
111 |
} |
} |
112 |
|
#endif |
113 |
node = (secmem_t *) xmalloc(sizeof(secmem_t)); |
node = (secmem_t *) xmalloc(sizeof(secmem_t)); |
114 |
|
|
115 |
node->buf = ptr; |
node->buf = ptr; |
241 |
} |
} |
242 |
|
|
243 |
|
|
244 |
|
#ifdef MEMORY_LOCK |
245 |
/* |
/* |
246 |
* Lock memory of allocated buffers. |
* Lock memory of allocated buffers. |
247 |
*/ |
*/ |
258 |
fatal(ERROR_SETUID, "imapfilter: failed to drop privileges\n"); |
fatal(ERROR_SETUID, "imapfilter: failed to drop privileges\n"); |
259 |
} |
} |
260 |
|
|
261 |
|
#endif |
262 |
|
|
263 |
|
|
264 |
/* |
/* |
265 |
* Disable core file dumping. |
* Disable core file dumping. |