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

Contents of /imapfilter/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations)
Thu Jul 31 15:53:19 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
CVS Tags: release-0_9
Branch point for: release-0_9-patches
Changes since 1.15: +15 -4 lines
File MIME type: text/plain
Broke up program files and created some new header files.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26