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

Annotation of /imapfilter/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (hide annotations)
Fri Feb 13 12:17:16 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.18: +42 -43 lines
File MIME type: text/plain
Stylistic changes.

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.19 #include <sys/types.h> /* IEEE Std 1003.1-2001 transitional. */
7     #include <sys/time.h> /* IEEE Std 1003.1-2001 transitional. */
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.13 if (ptr == NULL)
74 lefcha 1.19 fatal(ERROR_MEMALLOC,
75 lefcha 1.13 "NULL pointer given as argument");
76     free(ptr);
77 lefcha 1.5 }
78    
79    
80     /*
81 lefcha 1.3 * A strdup() that checks the results and dies in case of error.
82 lefcha 1.1 */
83 lefcha 1.13 char *
84     xstrdup(const char *s)
85 lefcha 1.1 {
86 lefcha 1.13 char *cp;
87 lefcha 1.2
88 lefcha 1.13 cp = strdup(s);
89 lefcha 1.2
90 lefcha 1.13 if (cp == NULL)
91 lefcha 1.19 fatal(ERROR_MEMALLOC, "allocating memory; %s\n", strerror(errno));
92 lefcha 1.2
93 lefcha 1.13 return cp;
94 lefcha 1.6 }
95    
96    
97     /*
98     * Secure memory malloc(). Locks memory and keeps information about the
99     * chunk that was allocated.
100     */
101 lefcha 1.13 void *
102     smalloc(size_t size)
103 lefcha 1.6 {
104 lefcha 1.13 void *ptr;
105 lefcha 1.19 secmem_t *sm;
106 lefcha 1.10
107 lefcha 1.13 ptr = xmalloc(size);
108 lefcha 1.10
109 lefcha 1.19 sm = (secmem_t *) xmalloc(sizeof(secmem_t));
110 lefcha 1.10
111 lefcha 1.19 sm->buf = ptr;
112     sm->size = size;
113     sm->prev = sm->next = NULL;
114 lefcha 1.10
115 lefcha 1.19 secmem_append(sm);
116 lefcha 1.10
117 lefcha 1.13 return ptr;
118 lefcha 1.6 }
119    
120    
121     /*
122     * Secure memory realloc(). Resize memory by allocating a new memory chunk
123     * and NULL fill old memory, in order to protect sensitive data.
124     */
125 lefcha 1.13 void *
126     srealloc(void *ptr, size_t size)
127 lefcha 1.6 {
128 lefcha 1.13 void *p;
129 lefcha 1.19 secmem_t *sm;
130 lefcha 1.10
131 lefcha 1.19 if (!(sm = (secmem_t *) secmem_find(ptr))) {
132 lefcha 1.13 ptr = xrealloc(ptr, size);
133     return ptr;
134     }
135     p = smalloc(size);
136 lefcha 1.19 memcpy(p, sm->buf, min(sm->size, size));
137 lefcha 1.13
138 lefcha 1.19 memset(sm->buf, 0, sm->size);
139     secmem_remove(sm);
140     xfree(sm->buf);
141     xfree(sm);
142 lefcha 1.10
143 lefcha 1.13 return p;
144 lefcha 1.6 }
145    
146    
147     /*
148     * Secure memory free(). NULL fill memory before freeing it.
149     */
150 lefcha 1.13 void
151     sfree(void *ptr)
152 lefcha 1.6 {
153 lefcha 1.19 secmem_t *sm;
154 lefcha 1.10
155 lefcha 1.19 if (!(sm = (secmem_t *) secmem_find(ptr))) {
156 lefcha 1.13 xfree(ptr);
157     return;
158     }
159 lefcha 1.19 memset(sm->buf, 0, sm->size);
160     secmem_remove(sm);
161     xfree(sm->buf);
162     xfree(sm);
163 lefcha 1.6 }
164    
165    
166     /*
167 lefcha 1.9 * Secure memory strdup(). Uses secure memory allocation.
168 lefcha 1.6 */
169 lefcha 1.13 char *
170     sstrdup(const char *s)
171 lefcha 1.6 {
172 lefcha 1.13 char *p;
173 lefcha 1.10
174 lefcha 1.13 p = (char *)smalloc(strlen(s) + 1);
175     xstrncpy(p, s, strlen(s));
176 lefcha 1.10
177 lefcha 1.13 return p;
178 lefcha 1.6 }
179    
180    
181     /*
182     * Append information about the newly allocated memory buffer.
183     */
184 lefcha 1.13 void
185 lefcha 1.19 secmem_append(secmem_t * sm)
186 lefcha 1.6 {
187 lefcha 1.13 secmem_t *pos;
188     secmem_t **app;
189 lefcha 1.10
190 lefcha 1.19 app = &secmem;
191     pos = secmem;
192 lefcha 1.6
193 lefcha 1.13 while (pos) {
194 lefcha 1.19 sm->prev = pos;
195 lefcha 1.13 app = &(pos->next);
196     pos = pos->next;
197     }
198 lefcha 1.10
199 lefcha 1.19 *app = sm;
200 lefcha 1.6 }
201    
202    
203     /*
204     * Find the record of a memory buffer in the secure memory linked list.
205     */
206 lefcha 1.13 secmem_t *
207     secmem_find(void *ptr)
208 lefcha 1.6 {
209 lefcha 1.13 secmem_t *pos;
210 lefcha 1.10
211 lefcha 1.19 pos = secmem;
212 lefcha 1.10
213 lefcha 1.13 while (pos != NULL && pos->buf != ptr)
214     pos = pos->next;
215 lefcha 1.10
216 lefcha 1.13 return pos;
217 lefcha 1.6 }
218 lefcha 1.10
219 lefcha 1.6
220     /*
221     * Remove a record of a secure memory buffer.
222     */
223 lefcha 1.13 void
224 lefcha 1.19 secmem_remove(secmem_t * sm)
225 lefcha 1.6 {
226 lefcha 1.19 if (sm->prev != NULL)
227     sm->prev->next = sm->next;
228     if (sm->next != NULL)
229     sm->next->prev = sm->prev;
230     if (secmem == sm)
231     secmem = sm->next;
232 lefcha 1.14
233 lefcha 1.6 }
234    
235    
236     /*
237     * Overwrite/clear all secure memory.
238     */
239 lefcha 1.13 void
240     secmem_clear(void)
241 lefcha 1.6 {
242 lefcha 1.14 secmem_t *p, *t;
243 lefcha 1.10
244 lefcha 1.19 for (p = secmem; p != NULL; p = t) {
245 lefcha 1.14 t = p->next;
246     sfree(p->buf);
247     }
248 lefcha 1.6 }
249 lefcha 1.8
250    
251     /*
252 lefcha 1.9 * Disable core file dumping.
253 lefcha 1.6 */
254 lefcha 1.13 void
255     corefile_disable(void)
256 lefcha 1.6 {
257 lefcha 1.13 struct rlimit rl;
258 lefcha 1.7
259 lefcha 1.13 getrlimit(RLIMIT_CORE, &rl);
260 lefcha 1.10
261 lefcha 1.13 rl.rlim_cur = rl.rlim_max = 0;
262     setrlimit(RLIMIT_CORE, &rl);
263 lefcha 1.2 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26