forked from emeryberger/Malloc-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapshield.cpp
executable file
·380 lines (336 loc) · 8.66 KB
/
heapshield.cpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
HeapShield
Prevents library-based heap overflow attacks
for any memory allocator that maintains object size
GIVEN AN INTERIOR POINTER.
Copyright (C) 2000-2010 by Emery Berger
http://www.cs.umass.edu/~emery
emery@cs.umass.edu
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
fprintf
fputs
fscanf
fwrite
printf
puts
sscanf
strcpy, strlen, sprintf
*/
#if defined(_WIN32)
//#error "This file does not work for Windows. Sorry."
#endif
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <limits.h>
#if !defined(_WIN32)
#include <dlfcn.h>
#endif
#ifndef CUSTOM_PREFIX
#define CUSTOM_PREFIX(x) x
#endif
#undef memcpy
#undef strcpy
extern bool malloc_hooked;
inline static
bool onStack (void * ptr) {
volatile int a;
volatile int b;
if ((size_t) &b > (size_t) &a) {
// Stack grows up.
return ((size_t) ptr < (size_t) &a);
} else {
// Stack grows down.
return ((size_t) ptr > (size_t) &b);
}
}
extern "C" {
size_t CUSTOM_PREFIX(malloc_usable_size)(void *);
}
static size_t local_strlen (const char * str)
{
int len = 0;
char * ch = (char *) str;
size_t maxLen;
if (onStack((void *) str) || !malloc_hooked) {
maxLen = UINT_MAX;
} else {
maxLen = CUSTOM_PREFIX(malloc_usable_size) ((void *) str);
}
const char *p = ch;
/* Loop over the data in s. */
while (*p != '\0')
p++;
len = (size_t)(p - ch);
if (len > maxLen) {
len = maxLen;
}
return len;
}
static char * local_strcat (char * dest, const char * src)
{
size_t d = local_strlen (dest);
char * dptr = dest + d;
size_t s = local_strlen (src);
char * sptr = (char *) src;
for (int i = 0; i <= s; i++) {
*dptr++ = *sptr++;
}
return dest;
}
static char * local_strncat (char * dest, const char * src, size_t sz)
{
size_t d = local_strlen (dest);
char * dptr = dest + d;
size_t s = local_strlen (src);
char * sptr = (char *) src;
for (int i = 0; i <= s && i < sz; i++) {
*dptr++ = *sptr++;
}
// Add a trailing nul.
*dptr = '\0';
return dest;
}
static char * local_strncpy (char * dest, const char * src, size_t n)
{
char * sptr = (char *) src;
char * dptr = dest;
int destLength;
if (!malloc_hooked) {
destLength = n;
} else {
destLength = CUSTOM_PREFIX(malloc_usable_size) (dest) - 1;
}
int lengthToCopy = (n < destLength) ? n : destLength;
while (lengthToCopy && (*dptr++ = *sptr++)) {
lengthToCopy--;
}
*dptr = '\0';
return dest;
}
static char * local_strcpy (char * dest, const char * src)
{
return local_strncpy (dest, src, INT_MAX);
}
static void * local_strdup (const char * s) {
size_t len = local_strlen((char *) s);
char * n = (char *) malloc (len + 1);
if (n) {
local_strncpy (n, s, len);
}
return n;
}
static void * local_memcpy (void * dest, const void * src, size_t n)
{
char * dptr = (char *) dest;
char * sptr = (char *) src;
for (int i = 0; i < n; i++) {
dptr[i] = sptr[i];
}
return dest;
}
static void * local_memset (void * dest, int val, size_t n)
{
char * dptr = (char *) dest;
for (int i = 0; i < n; i++) {
dptr[i] = val;
}
return dest;
}
extern "C" {
#if 0
size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream)
{
if (!malloc_hooked) {
return (*my_fread_fn)(ptr, size, nmemb, stream);
}
size_t sz = CUSTOM_PREFIX(malloc_usable_size) (ptr);
if (sz == -1) {
return (*my_fread_fn)(ptr, size, nmemb, stream);
} else {
size_t total = size * nmemb;
size_t s = (total < sz) ? total : sz;
if (sz < total) {
// Overflow.
//fprintf (stderr, "Overflow detected in fread!\n");
}
return (*my_fread_fn)(ptr, 1, s, stream);
}
}
#endif
void * memcpy (void * dest, const void * src, size_t n) {
size_t sz;
if (onStack(dest) || !malloc_hooked) {
sz = n;
} else {
sz = CUSTOM_PREFIX(malloc_usable_size) (dest);
if (sz == -1) {
sz = n;
} else {
size_t s = (n < sz) ? n : sz;
if (sz < n) {
#if 0
// Overflow.
fprintf (stderr, "Overflow detected in memcpy: dest (%x) size = %d, n = %d\n", dest, sz, n);
#endif
}
sz = s;
}
}
return local_memcpy (dest, src, sz);
}
void * memset (void * dest, int val, size_t n) {
size_t sz;
if (onStack(dest) || !malloc_hooked) {
sz = n;
} else {
sz = CUSTOM_PREFIX(malloc_usable_size) (dest);
if (sz == -1) {
sz = n;
} else {
size_t s = (n < sz) ? n : sz;
if (sz < n) {
#if 1
// Overflow.
fprintf (stderr, "Overflow detected in memset: dest (%x) size = %d, n = %d\n", dest, sz, n);
#endif
}
sz = s;
}
}
return local_memset (dest, val, sz);
}
#if 0
// Crap. something going on here with stack pushing of str.
int sprintf (char * str, const char * format, ...) {
va_list ap;
va_start (ap, format);
if (onStack(str) || !malloc_hooked) {
int r = vsprintf (str, format, ap);
va_end (ap);
return r;
}
else {
int sz = CUSTOM_PREFIX(malloc_usable_size) (str);
if (sz < 0) {
// Not from here.
int r = vsprintf (str, format, ap);
va_end (ap);
return r;
} else {
int r = vsnprintf (str, sz, format, ap);
va_end (ap);
return r;
}
}
}
#endif
int snprintf (char * str, size_t n, const char * format, ...) {
va_list ap;
va_start (ap, format);
if (onStack(str) || !malloc_hooked) {
int r = vsnprintf (str, n, format, ap);
va_end (ap);
return r;
}
size_t sz = CUSTOM_PREFIX(malloc_usable_size) (str);
if (sz == -1) {
int r = vsnprintf (str, n, format, ap);
va_end (ap);
return r;
}
size_t s = (n < sz) ? n : sz;
int r = vsnprintf (str, s, format, ap);
va_end (ap);
return r;
}
char * gets (char * s) {
size_t sz;
if (onStack(s)) {
sz = INT_MAX;
} else {
sz = CUSTOM_PREFIX(malloc_usable_size)(s);
}
return fgets (s, sz, stdin);
}
#if 1
char * strcpy (char * dest, const char * src) {
if (onStack(dest) || !malloc_hooked) {
return local_strcpy (dest, src);
}
size_t sz = CUSTOM_PREFIX(malloc_usable_size) (dest);
if (sz == -1) {
return local_strcpy (dest, src);
} else {
// DieFast: check for an error (would we have overflowed?)
#if 0
if (sz < strlen(src) + 1) {
fprintf (stderr, "Overflow detected in strcpy! dest (%x) size = %d, src (%x) length = %d (%s)\n", dest, sz, src, strlen(src), src);
}
#endif
return local_strncpy (dest, src, sz);
}
}
#endif
char * strncpy (char * dest, const char * src, size_t n) {
if (onStack(dest) || !malloc_hooked) {
return local_strncpy (dest, src, n);
}
size_t sz = CUSTOM_PREFIX(malloc_usable_size) (dest);
if (sz == -1) {
return local_strncpy (dest, src, n);
} else {
size_t s = (n < sz) ? n : sz;
// DieFast: check for an error (would we have overflowed?)
if (n > s) {
// fprintf (stderr, "Overflow detected in strncpy!\n");
}
return local_strncpy (dest, src, s);
}
}
char * strcat (char * dest, const char * src) {
if (onStack(dest) || !malloc_hooked) {
return local_strncat (dest, src, INT_MAX);
}
size_t sz = CUSTOM_PREFIX(malloc_usable_size) (dest + strlen(dest));
if (sz == -1) {
return local_strncat (dest, src, INT_MAX);
} else {
// DieFast: check for an error (would we have overflowed?)
//if (strlen(src) + strlen(dest) > sz) {
// fprintf (stderr, "Overflow detected in strcat!\n");
//}
return local_strncat (dest, src, sz);
}
}
char * strncat (char * dest, const char * src, size_t n) {
if (onStack(dest) || !malloc_hooked) {
return local_strncat (dest, src, n);
}
size_t sz = CUSTOM_PREFIX(malloc_usable_size) (dest + strlen(dest));
if (sz == -1) {
return local_strncat (dest, src, n);
} else {
size_t s = (n < (sz-1)) ? n : (sz-1);
#if 0
// DieFast: check for an error (would we have overflowed?)
if (strlen(dest) + s > sz) {
// fprintf (stderr, "Overflow detected in strncat!\n");
}
#endif
return local_strncat (dest, src, s);
}
}
}