-
Notifications
You must be signed in to change notification settings - Fork 56
/
safe_mem.c
196 lines (165 loc) · 4.72 KB
/
safe_mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2011 Alex Hornung <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tcplay.h"
struct safe_mem_hdr {
struct safe_mem_hdr *prev;
struct safe_mem_hdr *next;
struct safe_mem_tail *tail;
const char *file;
int line;
size_t alloc_sz;
char sig[8]; /* SAFEMEM */
};
struct safe_mem_tail {
char sig[8]; /* SAFEMEM */
};
static struct safe_mem_hdr *safe_mem_hdr_first = NULL;
void *
_alloc_safe_mem(size_t req_sz, const char *file, int line)
{
struct safe_mem_hdr *hdr, *hdrp;
struct safe_mem_tail *tail;
size_t alloc_sz;
char *mem, *user_mem;
alloc_sz = req_sz + sizeof(*hdr) + sizeof(*tail);
if ((mem = malloc(alloc_sz)) == NULL)
return NULL;
if (mlock(mem, alloc_sz) < 0) {
free(mem);
return NULL;
}
memset(mem, 0, alloc_sz);
hdr = (struct safe_mem_hdr *)mem;
tail = (struct safe_mem_tail *)(mem + alloc_sz - sizeof(*tail));
user_mem = mem + sizeof(*hdr);
strcpy(hdr->sig, "SAFEMEM");
strcpy(tail->sig, "SAFEMEM");
hdr->tail = tail;
hdr->alloc_sz = alloc_sz;
hdr->file = file;
hdr->line = line;
hdr->next = NULL;
if (safe_mem_hdr_first == NULL) {
safe_mem_hdr_first = hdr;
} else {
hdrp = safe_mem_hdr_first;
while (hdrp->next != NULL)
hdrp = hdrp->next;
hdr->prev = hdrp;
hdrp->next = hdr;
}
return user_mem;
}
void
_free_safe_mem(void *mem_ptr, const char *file, int line)
{
struct safe_mem_hdr *hdr;
struct safe_mem_tail *tail;
size_t alloc_sz;
char *mem = mem_ptr;
mem -= sizeof(*hdr);
hdr = (struct safe_mem_hdr *)mem;
tail = (struct safe_mem_tail *)(mem + hdr->alloc_sz - sizeof(*tail));
#ifdef DEBUG
fprintf(stderr, "freeing safe_mem (hdr): %#lx (%s:%d)\n",
(unsigned long)(void *)hdr, hdr->file, hdr->line);
#endif
if (hdr->alloc_sz == 0) {
fprintf(stderr, "BUG: double-free at %s:%d !!!\n", file, line);
return;
}
/* Integrity checks */
if ((memcmp(hdr->sig, "SAFEMEM\0", 8) != 0) ||
(memcmp(tail->sig, "SAFEMEM\0", 8) != 0)) {
fprintf(stderr, "BUG: safe_mem buffer under- or overflow at "
"%s:%d !!!\n", file, line);
return;
}
if (safe_mem_hdr_first == NULL) {
fprintf(stderr, "BUG: safe_mem list should not be empty at "
"%s:%d !!!\n", file, line);
return;
}
if (hdr->prev != NULL)
hdr->prev->next = hdr->next;
if (hdr->next != NULL)
hdr->next->prev = hdr->prev;
if (safe_mem_hdr_first == hdr)
safe_mem_hdr_first = hdr->next;
alloc_sz = hdr->alloc_sz;
memset(mem, 0xFF, alloc_sz);
memset(mem, 0, alloc_sz);
free(mem);
}
void *
_strdup_safe_mem(const char *in, const char *file, int line)
{
char *out;
size_t sz;
sz = strlen(in)+1;
if ((out = _alloc_safe_mem(sz, file, line)) == NULL) {
return NULL;
}
memcpy(out, in, sz);
out[sz-1] = '\0';
return out;
}
void
check_and_purge_safe_mem(void)
{
struct safe_mem_hdr *hdr;
char *mem;
#ifdef DEBUG
int ok;
#endif
if (safe_mem_hdr_first == NULL)
return;
hdr = safe_mem_hdr_first;
while ((hdr = safe_mem_hdr_first) != NULL) {
#ifdef DEBUG
if ((hdr->alloc_sz > 0) &&
(memcmp(hdr->sig, "SAFEMEM\0", 8) == 0) &&
(memcmp(hdr->tail->sig, "SAFEMEM\0", 8) == 0))
ok = 1;
else
ok = 0;
fprintf(stderr, "un-freed safe_mem: %#lx (%s:%d) [integrity=%s]\n",
(unsigned long)(void *)hdr, hdr->file, hdr->line,
ok? "ok" : "failed");
#endif
mem = (void *)hdr;
mem += sizeof(*hdr);
_free_safe_mem(mem, "check_and_purge_safe_mem", 0);
}
}