/[imapfilter]/imapfilter/memory.c
ViewVC logotype

Annotation of /imapfilter/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11.2.1 - (hide annotations)
Tue Sep 24 18:40:38 2002 UTC (21 years, 6 months ago) by lefcha
Branch: release-0_8-patches
Changes since 1.11: +12 -1 lines
File MIME type: text/plain
Option to disable memory locking during compilation.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26