-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathredflag.cpp
511 lines (416 loc) · 11.3 KB
/
redflag.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* redflag.c: detect writes to heap memory outside allocated chunks
*
* (c) 2011, the grugq <the.grugq@gmail.com>
*/
#include "pin.H"
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
namespace WINDOWS
{
#include <windows.h>
}
/*
* GLOBALS
*/
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "redflag.txt", "specifity trace file name");
static FILE * LogFile;
// forward declaration
class chunklist_t;
/*
* housekeeping classes
*/
class chunk_t {
public:
chunk_t(unsigned long address, unsigned long size)
: mAddress(address), mSize(size)
{};
chunk_t(const chunk_t &chunk)
: mAddress(chunk.mAddress), mSize(chunk.mSize)
{};
chunk_t(const chunk_t *chunk)
: mAddress(chunk->mAddress), mSize(chunk->mSize)
{};
~chunk_t() {};
bool contains(unsigned long addr) {
if (addr >= mAddress && addr <= (mAddress+mSize))
return true;
return false;
}
bool operator< (chunk_t *rhs) {
return mAddress < rhs->mAddress;
}
bool operator< (const chunk_t &rhs) {
return mAddress < rhs.mAddress;
};
bool operator< (const unsigned long address) {
return mAddress < address;
}
const unsigned long address() { return mAddress; };
const unsigned long size() { return mSize; };
private:
unsigned long mAddress;
unsigned long mSize;
};
class chunklist_t {
public:
chunklist_t()
: mInserts(0), mRemoves(0)
{};
~chunklist_t() {};
void insert(unsigned long address, unsigned long size);
void remove(unsigned long address);
bool contains(unsigned long address);
bool has_address(unsigned long address);
bool in_range(unsigned long address);
void set_ntdll(unsigned long low, unsigned long high) {
mNTDLL_low = low; mNTDLL_high =high;
};
bool is_ntdll(unsigned long address) {
if (address >= mNTDLL_low && address <= mNTDLL_high)
return true;
return false;
};
std::list<chunk_t>::iterator begin() {return mChunks.begin(); };
std::list<chunk_t>::iterator end() {return mChunks.end(); };
unsigned int size() const { return mChunks.size(); }
unsigned long ntdll_low() const { return mNTDLL_low; }
unsigned long ntdll_high() const { return mNTDLL_high; }
private:
std::list<chunk_t> mChunks;
unsigned int mInserts;
unsigned int mRemoves;
unsigned long mNTDLL_low;
unsigned long mNTDLL_high;
};
class heap_t {
public:
heap_t()
: mStart(0), mEnd(0)
{};
heap_t(const heap_t &heap)
: mStart(heap.mStart), mEnd(heap.mEnd)
{};
~heap_t() {};
unsigned long start() const { return mStart; };
unsigned long end() const { return mEnd; };
unsigned long start(unsigned long s) { mStart = s; return mStart; };
unsigned long end(unsigned long s) { mEnd = s; return mEnd; };
private:
unsigned long mStart;
unsigned long mEnd;
};
class heaplist_t
{
public:
heaplist_t() {};
~heaplist_t() {};
void update(unsigned long handle, unsigned long addr, unsigned long size) {
heap_t &heap = mHeaps[handle];
if (heap.start() == 0)
heap.start(addr);
if (heap.end() == 0)
heap.end(addr+size);
if (heap.end() < (addr+size))
heap.end(addr+size);
else if (heap.start() > addr)
heap.start(addr);
};
bool contains(unsigned long addr) {
std::map<unsigned long, heap_t>::iterator it;
for (it = mHeaps.begin(); it != mHeaps.end(); it++) {
heap_t & heap = (*it).second;
if (addr >= heap.start() && addr <= heap.end())
return true;
}
return false;
};
std::map<unsigned long, heap_t>::iterator begin() { return mHeaps.begin(); };
std::map<unsigned long, heap_t>::iterator end() { return mHeaps.end(); };
private:
std::map<unsigned long, heap_t> mHeaps;
};
/*
* GLOBALS (again)
*/
chunklist_t ChunksList;
heaplist_t HeapsList;
void
chunklist_t::insert(unsigned long address, unsigned long size)
{
chunk_t chunk(address, size);
std::list<chunk_t>::iterator low;
low = std::lower_bound(mChunks.begin(), mChunks.end(), chunk);
mChunks.insert(low, chunk);
}
void
chunklist_t::remove(unsigned long address)
{
std::list<chunk_t>::iterator low;
low = std::lower_bound(mChunks.begin(), mChunks.end(), address);
if (low != mChunks.end() && (*low).address() == address)
mChunks.erase(low);
}
// address is in a chunk range
bool
chunklist_t::contains(unsigned long address)
{
std::list<chunk_t>::iterator low;
low = std::lower_bound(mChunks.begin(), mChunks.end(), address);
if (low != mChunks.end() && ((*low).contains(address)))
return true;
low--; // preceding chunk ?
if (low != mChunks.end() && (*low).contains(address))
return true;
return false;
}
// has the exact address
bool
chunklist_t::has_address(unsigned long address)
{
std::list<chunk_t>::iterator low;
low = std::lower_bound(mChunks.begin(), mChunks.end(), address);
if (low != mChunks.end() && ((*low).address() == address))
return true;
return false;
}
bool
chunklist_t::in_range(unsigned long address)
{
if (address >= mChunks.front().address() &&
address <= (mChunks.back().address() + mChunks.back().size()))
return true;
return false;
}
static void
log_redflag(ADDRINT address, ADDRINT ea, unsigned int size)
{
unsigned long long value;
switch (size) {
case 1: value = *(unsigned char *)ea; break;
case 2: value = *(unsigned short *)ea; break;
case 4: value = *(unsigned int *)ea; break;
case 8: value = *(unsigned long long *)ea; break;
default: value = *(unsigned long *)ea; break;
}
fprintf(LogFile, "%#x %#x %#x\n", address, ea, value);
}
#define STACKSHIFT (3*8)
#define is_stack(EA, SP) (((SP)>>STACKSHIFT)==((EA)>>STACKSHIFT))
static void
write_ins(ADDRINT eip, ADDRINT esp, ADDRINT ea, unsigned int size)
{
// is it on the stack?
if (is_stack(ea, esp))
return;
// is it in a known heap region?
if (!HeapsList.contains(ea))
return;
// is it in the heap?
if (ChunksList.contains(ea))
return;
// is it normal access by heap management code?
if (ChunksList.is_ntdll(eip))
return;
// is it in a mmap() file?
// ... not sure how to test this
log_redflag(eip, ea, size);
}
#undef STACKSHIFT
#undef is_stack
static void
trace_instructions(INS ins, VOID *arg)
{
if (INS_IsMemoryWrite(ins))
INS_InsertCall(ins, IPOINT_BEFORE,
AFUNPTR(write_ins),
IARG_INST_PTR,
IARG_REG_VALUE, REG_STACK_PTR,
IARG_MEMORYWRITE_EA,
IARG_MEMORYWRITE_SIZE,
IARG_END
);
}
// on malloc() chunklist.insert(address, size)
// on free() chunklist.remove(address)
// on realloc() chunklist.remove(address), insert(naddress, size)
static WINDOWS::PVOID
replacementRtlAllocateHeap(
AFUNPTR rtlAllocateHeap,
WINDOWS::PVOID heapHandle,
WINDOWS::ULONG flags,
WINDOWS::SIZE_T size,
CONTEXT * ctx)
{
WINDOWS::PVOID retval;
PIN_CallApplicationFunction(ctx, PIN_ThreadId(),
CALLINGSTD_STDCALL, rtlAllocateHeap,
PIN_PARG(void *), &retval,
PIN_PARG(WINDOWS::PVOID), heapHandle,
PIN_PARG(WINDOWS::ULONG), flags,
PIN_PARG(WINDOWS::SIZE_T), size,
PIN_PARG_END()
);
// adjust for heap management structures
ChunksList.insert((unsigned long) retval-8, size+8);
HeapsList.update((unsigned long) heapHandle, (unsigned long) retval-8,
size+8);
return retval;
};
static WINDOWS::PVOID
replacementRtlReAllocateHeap(
AFUNPTR rtlReAllocateHeap,
WINDOWS::PVOID heapHandle,
WINDOWS::ULONG flags,
WINDOWS::PVOID memoryPtr,
WINDOWS::SIZE_T size,
CONTEXT *ctx)
{
WINDOWS::PVOID retval;
PIN_CallApplicationFunction(ctx, PIN_ThreadId(),
CALLINGSTD_STDCALL, rtlReAllocateHeap,
PIN_PARG(void *), &retval,
PIN_PARG(WINDOWS::PVOID), heapHandle,
PIN_PARG(WINDOWS::ULONG), flags,
PIN_PARG(WINDOWS::PVOID), memoryPtr,
PIN_PARG(WINDOWS::SIZE_T), size,
PIN_PARG_END()
);
// XXX should we check for retval == NULL ?
ChunksList.remove((unsigned long)memoryPtr-8);
ChunksList.insert((unsigned long)retval-8, size+8);
HeapsList.update((unsigned long) heapHandle, (unsigned long) retval-8,
size+8);
return retval;
}
static WINDOWS::BOOL
replacementRtlFreeHeap(
AFUNPTR rtlFreeHeap,
WINDOWS::PVOID heapHandle,
WINDOWS::ULONG flags,
WINDOWS::PVOID memoryPtr,
CONTEXT *ctx)
{
WINDOWS::BOOL retval;
PIN_CallApplicationFunction(ctx, PIN_ThreadId(),
CALLINGSTD_STDCALL, rtlFreeHeap,
PIN_PARG(WINDOWS::BOOL), &retval,
PIN_PARG(WINDOWS::PVOID), heapHandle,
PIN_PARG(WINDOWS::ULONG), flags,
PIN_PARG(WINDOWS::PVOID), memoryPtr,
PIN_PARG_END()
);
ChunksList.remove((unsigned long)memoryPtr);
return retval;
}
VOID
image_load(IMG img, VOID *v)
{
RTN rtn;
// check this image actually has the heap functions.
if ((rtn = RTN_FindByName(img, "RtlAllocateHeap")) == RTN_Invalid())
return;
ChunksList.set_ntdll(IMG_LowAddress(img), IMG_HighAddress(img));
// hook RtlAllocateHeap
RTN rtlAllocate = RTN_FindByName(img, "RtlAllocateHeap");
PROTO protoRtlAllocateHeap =
PROTO_Allocate( PIN_PARG(void *),
CALLINGSTD_STDCALL,
"RtlAllocateHeap",
PIN_PARG(WINDOWS::PVOID), // HeapHandle
PIN_PARG(WINDOWS::ULONG), // Flags
PIN_PARG(WINDOWS::SIZE_T), // Size
PIN_PARG_END()
);
RTN_ReplaceSignature(rtlAllocate,(AFUNPTR)replacementRtlAllocateHeap,
IARG_PROTOTYPE, protoRtlAllocateHeap,
IARG_ORIG_FUNCPTR,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_FUNCARG_ENTRYPOINT_VALUE, 2,
IARG_CONTEXT,
IARG_END
);
PROTO_Free(protoRtlAllocateHeap);
// replace RtlReAllocateHeap()
RTN rtlReallocate = RTN_FindByName(img, "RtlReAllocateHeap");
PROTO protoRtlReAllocateHeap =
PROTO_Allocate( PIN_PARG(void *), CALLINGSTD_STDCALL,
"RtlReAllocateHeap",
PIN_PARG(WINDOWS::PVOID), // HeapHandle
PIN_PARG(WINDOWS::ULONG), // Flags
PIN_PARG(WINDOWS::PVOID), // MemoryPtr
PIN_PARG(WINDOWS::SIZE_T),// Size
PIN_PARG_END()
);
RTN_ReplaceSignature(rtlReallocate,
(AFUNPTR)replacementRtlReAllocateHeap,
IARG_PROTOTYPE, protoRtlReAllocateHeap,
IARG_ORIG_FUNCPTR,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_FUNCARG_ENTRYPOINT_VALUE, 2,
IARG_FUNCARG_ENTRYPOINT_VALUE, 3,
IARG_CONTEXT,
IARG_END
);
PROTO_Free(protoRtlReAllocateHeap);
// replace RtlFreeHeap
RTN rtlFree = RTN_FindByName(img, "RtlFreeHeap");
PROTO protoRtlFreeHeap =
PROTO_Allocate( PIN_PARG(void *),
CALLINGSTD_STDCALL,
"RtlFreeHeap",
PIN_PARG(WINDOWS::PVOID), // HeapHandle
PIN_PARG(WINDOWS::ULONG), // Flags
PIN_PARG(WINDOWS::PVOID), // MemoryPtr
PIN_PARG_END()
);
RTN_ReplaceSignature(rtlFree,(AFUNPTR)replacementRtlFreeHeap,
IARG_PROTOTYPE, protoRtlFreeHeap,
IARG_ORIG_FUNCPTR,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_FUNCARG_ENTRYPOINT_VALUE, 2,
IARG_CONTEXT,
IARG_END
);
PROTO_Free(protoRtlFreeHeap);
}
VOID
finish(int ignored, VOID *arg)
{
std::map<unsigned long, heap_t>::iterator it;
for (it = HeapsList.begin(); it != HeapsList.end(); it++)
fprintf(LogFile, "# %#x %#x\n",
(*it).second.start(), (*it).second.end());
fflush(LogFile);
fclose(LogFile);
}
UINT32
usage()
{
std::cout << "RedFlag: tool to detecting out of bounds heap writes";
std::cout << std::endl;
std::cout << KNOB_BASE::StringKnobSummary();
std::cout << std::endl;
return 2;
}
int
main(int argc, char **argv)
{
PIN_InitSymbols();
if (PIN_Init(argc, argv))
return usage();
LogFile = fopen(KnobOutputFile.Value().c_str(), "wb+");
INS_AddInstrumentFunction(trace_instructions, NULL);
IMG_AddInstrumentFunction(image_load, NULL);
PIN_AddFiniFunction(finish, NULL);
// never returns..
PIN_StartProgram();
return 0;
}