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

Annotation of /imapfilter/memory.c

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26