-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripwire.c
More file actions
286 lines (234 loc) · 7.23 KB
/
tripwire.c
File metadata and controls
286 lines (234 loc) · 7.23 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* SPDX-License-Identifier: MIT */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char tw_sentinel[] = {'\001', '\002', '\003', '\004'};
typedef struct tripwire tripwire;
struct tripwire
{
char *alloc;
char *public;
size_t allocated;
const char *file;
int line;
int freed; // or reallocated
tripwire *next;
tripwire *prev;
};
void tripwire_free(void *, const char *, int);
static tripwire *tw_head = NULL;
static tripwire *tw_tail = NULL;
static void fatal1(const char *s, const char *file, int line)
{
fprintf(stderr, "File: %s, Line: %d, \n", file, line);
perror(s);
exit(1);
}
static void fatal2(const char *s, const char *file, int line)
{
fprintf(stderr, "%s: %s: %d\n", s, file, line);
exit(1);
}
static void *tripwire_new(const char *file, int line)
{
tripwire *tw = malloc(sizeof(tripwire));
if (tw == NULL) fatal1("malloc", file, line);
memset(tw, 0, sizeof(tripwire));
if (tw_head == NULL)
tw_head = tw;
if (tw_tail != NULL)
{
tw->prev = tw_tail;
tw_tail->next = tw;
}
tw_tail = tw;
return tw;
}
static void *tripwire_find(void *p)
{
tripwire *t;
// search last created first - important
for (t = tw_tail; t != NULL; t = t->prev)
if (p == t->public)
return t;
return NULL;
}
void *tripwire_malloc(size_t size, const char *file, int line)
{
size_t total = size + 2 * sizeof tw_sentinel;
if (total < size) fatal2("malloc(): size overflow", file, line);
tripwire *tw = tripwire_new(file, line);
tw->alloc = malloc(total);
if (tw->alloc == NULL) fatal1("malloc", file, line);
tw->file = strdup(file);
tw->line = line;
tw->public = tw->alloc + sizeof tw_sentinel;
tw->allocated = size;
memcpy(tw->alloc, tw_sentinel, sizeof tw_sentinel);
memcpy(tw->public + tw->allocated, tw_sentinel, sizeof tw_sentinel);
return tw->public;
}
void *tripwire_realloc(void *p, size_t size, const char *file, int line)
{
if (p == NULL)
return tripwire_malloc(size, file, line);
if (size == 0)
{
tripwire_free(p, file, line);
return NULL;
}
tripwire *t = tripwire_find(p);
if (t == NULL)
fatal2("realloc(): NO BLOCK:", file, line);
if (memcmp(t->alloc, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 0 cracked in %s at %d\n",
file, line);
if (memcmp(t->public + t->allocated, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 1 cracked in %s at %d\n",
file, line);
if (t->freed == 1) fatal2("Realloc of freed mem", file, line);
t->freed = 1;
size_t total = size + 2 * sizeof tw_sentinel;
if (total < size) fatal2("realloc(): size overflow", file, line);
tripwire *tw = tripwire_new(file, line);
tw->alloc = realloc(t->alloc, total);
if (tw->alloc == NULL) fatal1("malloc", file, line);
tw->public = tw->alloc + sizeof tw_sentinel;
tw->allocated = size;
tw->file = strdup(file);
tw->line = line;
memcpy(tw->alloc, tw_sentinel, sizeof tw_sentinel);
memcpy(tw->public + tw->allocated, tw_sentinel, sizeof tw_sentinel);
return tw->public;
}
void tripwire_free(void *p, const char *file, int line)
{
if (p == NULL) return;
tripwire *t = tripwire_find(p);
if (t == NULL)
fatal2("free(): NO BLOCK:", file, line);
if (t->freed == 1) fatal2("free(): DOUBLE FREE:", file, line);
if (memcmp(t->alloc, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 0 cracked in %s at %d\n",
file, line);
if (memcmp(t->public + t->allocated, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 1 cracked in %s at %d\n",
file, line);
t->freed = 1;
free(t->alloc);
t->alloc = NULL;
}
void *tripwire_memset(void *p, int c, size_t l, const char *file, int line)
{
tripwire *t = tripwire_find(p);
if (t == NULL)
{
fprintf(stderr, "BLOCK NOT FOUND: memset(%p, %d, %zu) in %s at %d\n",
p, c, l, file, line);
return memset(p, c, l);
}
if (memcmp(t->alloc, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 0 cracked in %s at %d\n",
file, line);
if (memcmp(t->public + t->allocated, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 1 cracked in %s at %d\n",
file, line);
if (l != t->allocated)
fprintf(stderr,
"MEMSET mismatch: expected %zu got %zu in %s at %d\n",
t->allocated, l, file, line);
return memset(p, c, l);
}
void *tripwire_calloc(size_t nmemb, size_t size, const char *file, int line)
{
if (nmemb && size > (size_t)-1 / nmemb)
fatal2("calloc(): size overflow", file, line);
size_t total = nmemb * size;
void *p = tripwire_malloc(total, file, line);
memset(p, 0, total);
return p;
}
char *tripwire_strdup(const char *s, const char *file, int line)
{
size_t len = strlen(s) + 1;
char *p = tripwire_malloc(len, file, line);
memcpy(p, s, len);
return p;
}
void *tripwire_memcpy(void *dest, const void *src, size_t l, const char *file, int line)
{
tripwire *t = tripwire_find(dest);
if (t == NULL)
{
fprintf(stderr, "BLOCK NOT FOUND: memcpy(%p, %p, %zu) in %s at %d\n",
dest, src, l, file, line);
return memcpy(dest, src, l);
}
if (memcmp(t->alloc, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 0 cracked in %s at %d\n",
file, line);
if (memcmp(t->public + t->allocated, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 1 cracked in %s at %d\n",
file, line);
if (l > t->allocated)
fprintf(stderr,
"MEMCPY overflow: allocated %zu, copying %zu in %s at %d\n",
t->allocated, l, file, line);
return memcpy(dest, src, l);
}
void *tripwire_memmove(void *dest, const void *src, size_t l, const char *file, int line)
{
tripwire *t = tripwire_find(dest);
if (t == NULL)
{
fprintf(stderr, "BLOCK NOT FOUND: memmove(%p, %p, %zu) in %s at %d\n",
dest, src, l, file, line);
return memmove(dest, src, l);
}
if (memcmp(t->alloc, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 0 cracked in %s at %d\n",
file, line);
if (memcmp(t->public + t->allocated, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 1 cracked in %s at %d\n",
file, line);
if (l > t->allocated)
fprintf(stderr,
"MEMMOVE overflow: allocated %zu, moving %zu in %s at %d\n",
t->allocated, l, file, line);
return memmove(dest, src, l);
}
void tripwire_report(void)
{
tripwire *t;
int leaks = 0;
for (t = tw_head; t != NULL; t = t->next)
{
if (t->freed) continue;
if (memcmp(t->alloc, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 0 cracked: %s:%d (%zu bytes)\n",
t->file, t->line, t->allocated);
if (memcmp(t->public + t->allocated, tw_sentinel, sizeof tw_sentinel) != 0)
fprintf(stderr, "SENTINEL 1 cracked: %s:%d (%zu bytes)\n",
t->file, t->line, t->allocated);
fprintf(stderr, "LEAK: %zu bytes at %p allocated in %s:%d\n",
t->allocated, (void *)t->public, t->file, t->line);
leaks++;
}
if (leaks)
fprintf(stderr, "tripwire: %d leak(s) detected\n", leaks);
}
void tripwire_cleanup(void)
{
tripwire *t = tw_head;
while (t != NULL)
{
tripwire *next = t->next;
if (!t->freed)
free(t->alloc);
free((void *)t->file);
free(t);
t = next;
}
tw_head = NULL;
tw_tail = NULL;
}