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

Contents of /imapfilter/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26