3 |
#include <unistd.h> |
#include <unistd.h> |
4 |
#include <string.h> |
#include <string.h> |
5 |
#include <errno.h> |
#include <errno.h> |
6 |
#include <sys/types.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
#include <sys/types.h> /* For POSIX.1-2001 non-conformant systems. */ |
7 |
#include <sys/time.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
#include <sys/time.h> /* For POSIX.1-2001 non-conformant systems. */ |
8 |
#include <sys/resource.h> |
#include <sys/resource.h> |
9 |
|
|
10 |
#include "config.h" |
#include "config.h" |
22 |
|
|
23 |
extern unsigned int options; |
extern unsigned int options; |
24 |
|
|
25 |
static secmem_t *smem = NULL; /* First node of secure memory linked list. */ |
static secmem_t *secmem = NULL; /* First node of secure memory linked list. */ |
26 |
|
|
27 |
|
|
28 |
void secmem_append(secmem_t * node); |
void secmem_append(secmem_t * sm); |
29 |
secmem_t *secmem_find(void *ptr); |
secmem_t *secmem_find(void *ptr); |
30 |
void secmem_remove(secmem_t * node); |
void secmem_remove(secmem_t * sm); |
31 |
|
|
32 |
|
|
33 |
/* |
/* |
41 |
ptr = (void *)malloc(size); |
ptr = (void *)malloc(size); |
42 |
|
|
43 |
if (ptr == NULL) |
if (ptr == NULL) |
44 |
fatal(ERROR_MEMORY_ALLOCATION, |
fatal(ERROR_MEMALLOC, |
45 |
"allocating memory; %s\n", strerror(errno)); |
"allocating memory; %s\n", strerror(errno)); |
46 |
|
|
47 |
return ptr; |
return ptr; |
57 |
ptr = (void *)realloc(ptr, size); |
ptr = (void *)realloc(ptr, size); |
58 |
|
|
59 |
if (ptr == NULL) |
if (ptr == NULL) |
60 |
fatal(ERROR_MEMORY_ALLOCATION, |
fatal(ERROR_MEMALLOC, |
61 |
"allocating memory; %s\n", strerror(errno)); |
"allocating memory; %s\n", strerror(errno)); |
62 |
|
|
63 |
return ptr; |
return ptr; |
70 |
void |
void |
71 |
xfree(void *ptr) |
xfree(void *ptr) |
72 |
{ |
{ |
73 |
|
|
74 |
if (ptr == NULL) |
if (ptr == NULL) |
75 |
fatal(ERROR_MEMORY_ALLOCATION, |
fatal(ERROR_MEMALLOC, |
76 |
"NULL pointer given as argument"); |
"NULL pointer given as argument"); |
77 |
free(ptr); |
free(ptr); |
78 |
} |
} |
89 |
cp = strdup(s); |
cp = strdup(s); |
90 |
|
|
91 |
if (cp == NULL) |
if (cp == NULL) |
92 |
fatal(ERROR_MEMORY_ALLOCATION, |
fatal(ERROR_MEMALLOC, "allocating memory; %s\n", |
93 |
"allocating memory; %s\n", strerror(errno)); |
strerror(errno)); |
94 |
|
|
95 |
return cp; |
return cp; |
96 |
} |
} |
104 |
smalloc(size_t size) |
smalloc(size_t size) |
105 |
{ |
{ |
106 |
void *ptr; |
void *ptr; |
107 |
secmem_t *node; |
secmem_t *sm; |
108 |
|
|
109 |
ptr = xmalloc(size); |
ptr = xmalloc(size); |
110 |
|
|
111 |
node = (secmem_t *) xmalloc(sizeof(secmem_t)); |
sm = (secmem_t *) xmalloc(sizeof(secmem_t)); |
112 |
|
|
113 |
node->buf = ptr; |
sm->buf = ptr; |
114 |
node->size = size; |
sm->size = size; |
115 |
node->prev = node->next = NULL; |
sm->prev = sm->next = NULL; |
116 |
|
|
117 |
secmem_append(node); |
secmem_append(sm); |
118 |
|
|
119 |
return ptr; |
return ptr; |
120 |
} |
} |
128 |
srealloc(void *ptr, size_t size) |
srealloc(void *ptr, size_t size) |
129 |
{ |
{ |
130 |
void *p; |
void *p; |
131 |
secmem_t *node; |
secmem_t *sm; |
132 |
|
|
133 |
if (!(node = (secmem_t *) secmem_find(ptr))) { |
if (!(sm = (secmem_t *) secmem_find(ptr))) { |
134 |
ptr = xrealloc(ptr, size); |
ptr = xrealloc(ptr, size); |
135 |
return ptr; |
return ptr; |
136 |
} |
} |
137 |
p = smalloc(size); |
p = smalloc(size); |
138 |
memcpy(p, node->buf, min(node->size, size)); |
memcpy(p, sm->buf, min(sm->size, size)); |
139 |
|
|
140 |
memset(node->buf, 0, node->size); |
memset(sm->buf, 0, sm->size); |
141 |
secmem_remove(node); |
secmem_remove(sm); |
142 |
xfree(node->buf); |
xfree(sm->buf); |
143 |
xfree(node); |
xfree(sm); |
144 |
|
|
145 |
return p; |
return p; |
146 |
} |
} |
152 |
void |
void |
153 |
sfree(void *ptr) |
sfree(void *ptr) |
154 |
{ |
{ |
155 |
secmem_t *node; |
secmem_t *sm; |
156 |
|
|
157 |
if (!(node = (secmem_t *) secmem_find(ptr))) { |
if (!(sm = (secmem_t *) secmem_find(ptr))) { |
158 |
xfree(ptr); |
xfree(ptr); |
159 |
return; |
return; |
160 |
} |
} |
161 |
memset(node->buf, 0, node->size); |
memset(sm->buf, 0, sm->size); |
162 |
secmem_remove(node); |
secmem_remove(sm); |
163 |
xfree(node->buf); |
xfree(sm->buf); |
164 |
xfree(node); |
xfree(sm); |
165 |
} |
} |
166 |
|
|
167 |
|
|
184 |
* Append information about the newly allocated memory buffer. |
* Append information about the newly allocated memory buffer. |
185 |
*/ |
*/ |
186 |
void |
void |
187 |
secmem_append(secmem_t * node) |
secmem_append(secmem_t * sm) |
188 |
{ |
{ |
189 |
secmem_t *pos; |
secmem_t *pos; |
190 |
secmem_t **app; |
secmem_t **app; |
191 |
|
|
192 |
app = &smem; |
app = &secmem; |
193 |
pos = smem; |
pos = secmem; |
194 |
|
|
195 |
while (pos) { |
while (pos) { |
196 |
node->prev = pos; |
sm->prev = pos; |
197 |
app = &(pos->next); |
app = &(pos->next); |
198 |
pos = pos->next; |
pos = pos->next; |
199 |
} |
} |
200 |
|
|
201 |
*app = node; |
*app = sm; |
202 |
} |
} |
203 |
|
|
204 |
|
|
210 |
{ |
{ |
211 |
secmem_t *pos; |
secmem_t *pos; |
212 |
|
|
213 |
pos = smem; |
pos = secmem; |
214 |
|
|
215 |
while (pos != NULL && pos->buf != ptr) |
while (pos != NULL && pos->buf != ptr) |
216 |
pos = pos->next; |
pos = pos->next; |
223 |
* Remove a record of a secure memory buffer. |
* Remove a record of a secure memory buffer. |
224 |
*/ |
*/ |
225 |
void |
void |
226 |
secmem_remove(secmem_t * node) |
secmem_remove(secmem_t * sm) |
227 |
{ |
{ |
228 |
if (node->prev != NULL) |
|
229 |
node->prev->next = node->next; |
if (sm->prev != NULL) |
230 |
if (node->next != NULL) |
sm->prev->next = sm->next; |
231 |
node->next->prev = node->prev; |
if (sm->next != NULL) |
232 |
if (smem == node) |
sm->next->prev = sm->prev; |
233 |
smem = node->next; |
if (secmem == sm) |
234 |
|
secmem = sm->next; |
235 |
|
|
236 |
} |
} |
237 |
|
|
244 |
{ |
{ |
245 |
secmem_t *p, *t; |
secmem_t *p, *t; |
246 |
|
|
247 |
for (p = smem; p != NULL; p = t) { |
for (p = secmem; p != NULL; p = t) { |
248 |
t = p->next; |
t = p->next; |
249 |
sfree(p->buf); |
sfree(p->buf); |
250 |
} |
} |