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> /* IEEE Std 1003.1-2001 transitional. */ |
7 |
#include <sys/time.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
#include <sys/time.h> /* IEEE Std 1003.1-2001 transitional. */ |
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; |
71 |
xfree(void *ptr) |
xfree(void *ptr) |
72 |
{ |
{ |
73 |
if (ptr == NULL) |
if (ptr == NULL) |
74 |
fatal(ERROR_MEMORY_ALLOCATION, |
fatal(ERROR_MEMALLOC, |
75 |
"NULL pointer given as argument"); |
"NULL pointer given as argument"); |
76 |
free(ptr); |
free(ptr); |
77 |
} |
} |
88 |
cp = strdup(s); |
cp = strdup(s); |
89 |
|
|
90 |
if (cp == NULL) |
if (cp == NULL) |
91 |
fatal(ERROR_MEMORY_ALLOCATION, |
fatal(ERROR_MEMALLOC, "allocating memory; %s\n", strerror(errno)); |
|
"allocating memory; %s\n", strerror(errno)); |
|
92 |
|
|
93 |
return cp; |
return cp; |
94 |
} |
} |
102 |
smalloc(size_t size) |
smalloc(size_t size) |
103 |
{ |
{ |
104 |
void *ptr; |
void *ptr; |
105 |
secmem_t *node; |
secmem_t *sm; |
106 |
|
|
107 |
ptr = xmalloc(size); |
ptr = xmalloc(size); |
108 |
|
|
109 |
node = (secmem_t *) xmalloc(sizeof(secmem_t)); |
sm = (secmem_t *) xmalloc(sizeof(secmem_t)); |
110 |
|
|
111 |
node->buf = ptr; |
sm->buf = ptr; |
112 |
node->size = size; |
sm->size = size; |
113 |
node->prev = node->next = NULL; |
sm->prev = sm->next = NULL; |
114 |
|
|
115 |
secmem_append(node); |
secmem_append(sm); |
116 |
|
|
117 |
return ptr; |
return ptr; |
118 |
} |
} |
126 |
srealloc(void *ptr, size_t size) |
srealloc(void *ptr, size_t size) |
127 |
{ |
{ |
128 |
void *p; |
void *p; |
129 |
secmem_t *node; |
secmem_t *sm; |
130 |
|
|
131 |
if (!(node = (secmem_t *) secmem_find(ptr))) { |
if (!(sm = (secmem_t *) secmem_find(ptr))) { |
132 |
ptr = xrealloc(ptr, size); |
ptr = xrealloc(ptr, size); |
133 |
return ptr; |
return ptr; |
134 |
} |
} |
135 |
p = smalloc(size); |
p = smalloc(size); |
136 |
memcpy(p, node->buf, min(node->size, size)); |
memcpy(p, sm->buf, min(sm->size, size)); |
137 |
|
|
138 |
memset(node->buf, 0, node->size); |
memset(sm->buf, 0, sm->size); |
139 |
secmem_remove(node); |
secmem_remove(sm); |
140 |
xfree(node->buf); |
xfree(sm->buf); |
141 |
xfree(node); |
xfree(sm); |
142 |
|
|
143 |
return p; |
return p; |
144 |
} |
} |
150 |
void |
void |
151 |
sfree(void *ptr) |
sfree(void *ptr) |
152 |
{ |
{ |
153 |
secmem_t *node; |
secmem_t *sm; |
154 |
|
|
155 |
if (!(node = (secmem_t *) secmem_find(ptr))) { |
if (!(sm = (secmem_t *) secmem_find(ptr))) { |
156 |
xfree(ptr); |
xfree(ptr); |
157 |
return; |
return; |
158 |
} |
} |
159 |
memset(node->buf, 0, node->size); |
memset(sm->buf, 0, sm->size); |
160 |
secmem_remove(node); |
secmem_remove(sm); |
161 |
xfree(node->buf); |
xfree(sm->buf); |
162 |
xfree(node); |
xfree(sm); |
163 |
} |
} |
164 |
|
|
165 |
|
|
182 |
* Append information about the newly allocated memory buffer. |
* Append information about the newly allocated memory buffer. |
183 |
*/ |
*/ |
184 |
void |
void |
185 |
secmem_append(secmem_t * node) |
secmem_append(secmem_t * sm) |
186 |
{ |
{ |
187 |
secmem_t *pos; |
secmem_t *pos; |
188 |
secmem_t **app; |
secmem_t **app; |
189 |
|
|
190 |
app = &smem; |
app = &secmem; |
191 |
pos = smem; |
pos = secmem; |
192 |
|
|
193 |
while (pos) { |
while (pos) { |
194 |
node->prev = pos; |
sm->prev = pos; |
195 |
app = &(pos->next); |
app = &(pos->next); |
196 |
pos = pos->next; |
pos = pos->next; |
197 |
} |
} |
198 |
|
|
199 |
*app = node; |
*app = sm; |
200 |
} |
} |
201 |
|
|
202 |
|
|
208 |
{ |
{ |
209 |
secmem_t *pos; |
secmem_t *pos; |
210 |
|
|
211 |
pos = smem; |
pos = secmem; |
212 |
|
|
213 |
while (pos != NULL && pos->buf != ptr) |
while (pos != NULL && pos->buf != ptr) |
214 |
pos = pos->next; |
pos = pos->next; |
221 |
* Remove a record of a secure memory buffer. |
* Remove a record of a secure memory buffer. |
222 |
*/ |
*/ |
223 |
void |
void |
224 |
secmem_remove(secmem_t * node) |
secmem_remove(secmem_t * sm) |
225 |
{ |
{ |
226 |
if (node->prev != NULL) |
if (sm->prev != NULL) |
227 |
node->prev->next = node->next; |
sm->prev->next = sm->next; |
228 |
if (node->next != NULL) |
if (sm->next != NULL) |
229 |
node->next->prev = node->prev; |
sm->next->prev = sm->prev; |
230 |
if (smem == node) |
if (secmem == sm) |
231 |
smem = node->next; |
secmem = sm->next; |
232 |
|
|
233 |
} |
} |
234 |
|
|
241 |
{ |
{ |
242 |
secmem_t *p, *t; |
secmem_t *p, *t; |
243 |
|
|
244 |
for (p = smem; p != NULL; p = t) { |
for (p = secmem; p != NULL; p = t) { |
245 |
t = p->next; |
t = p->next; |
246 |
sfree(p->buf); |
sfree(p->buf); |
247 |
} |
} |