-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_overflow.cpp
284 lines (250 loc) · 8.01 KB
/
stack_overflow.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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "pin.H"
#include <unordered_map>
#include <stack>
// key to open the main Routine
static uint32_t key = 0;
// To save the malloc area
struct mallocArea
{
UINT64 base;
UINT64 size;
BOOL status;
};
// Main stack to store the stack size
struct Node
{
// Total stack size allocated
uint64_t value;
// Upper limit
uint64_t cu;
// Lower limit
uint64_t cl;
};
std::stack<Node> mainStack;
// Ins object mapping
class Insr
{
private:
// Disassembled instruction
string insDis;
INS ins;
public:
Insr(string insDis, INS ins) { this->insDis = insDis; this->ins = ins;}
string get_insDis() { return insDis;}
INS get_ins() { return ins;}
};
// Stack for the Insr structure
static std::unordered_map<ADDRINT, Insr*> insstack;
// This function is called before every instruction is executed
VOID protect(uint64_t addr)
{
if (addr > 0x700000000000)
return;
if (!key)
return;
// Initialize the diassembled instruction
string insdis = insstack[addr]->get_insDis();
INS ins = insstack[addr]->get_ins();
// If the function is returned then pop the value off the stack
if (INS_Opcode(ins) == XED_ICLASS_LEAVE && mainStack.size() >= 2)
{
std::cout << "Popping out: " << mainStack.top().value << '\n';
mainStack.pop();
std::cout << "stack top: " << mainStack.top().value << " as leaving" << '\n';
}
// For memory store operations
if ((INS_Opcode(ins) == XED_ICLASS_MOV) && INS_OperandIsMemory(ins, 0)
&& ((INS_OperandWidth(ins, 0) == 32)
|| (INS_OperandWidth(ins, 0) == 64))
&& ((INS_OperandMemoryBaseReg(ins, 0) == REG_RBP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_EBP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_RSP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_ESP)))
{
// Check for array bounds
if (INS_MemoryDisplacement(ins) >= 0
&& ((INS_OperandMemoryBaseReg(ins, 0) == REG_RBP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_EBP)))
{
std::cout << "Access over allowed bounds detected!" << '\n';
std::cout << hex <<addr << "\t" << insdis << std::endl;
}
else if (INS_MemoryDisplacement(ins) <= 0
&& ((INS_OperandMemoryBaseReg(ins, 0) == REG_RSP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_ESP)))
{
std::cout << "Access over allowed bounds detected!" << '\n';
std::cout << hex <<addr << "\t" << insdis << std::endl;
}
else
{
if (INS_MemoryDisplacement(ins) >= static_cast<int64_t>(mainStack.top().value))
std::cout << "Overflow detected" << '\n';
}
}
// For memory load operations
if ((INS_Opcode(ins) == XED_ICLASS_MOV) && INS_OperandIsMemory(ins, 1)
&& ((INS_OperandWidth(ins, 1) == 32)
|| (INS_OperandWidth(ins, 0) == 64))
&& ((INS_OperandMemoryBaseReg(ins, 1) == REG_RBP)
|| (INS_OperandMemoryBaseReg(ins, 1) == REG_EBP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_RSP)
|| (INS_OperandMemoryBaseReg(ins, 0) == REG_ESP)))
{
// Check for array bounds
if (INS_MemoryDisplacement(ins) >= 0
&& ((INS_OperandMemoryBaseReg(ins, 1) == REG_RBP)
|| (INS_OperandMemoryBaseReg(ins, 1) == REG_EBP)))
{
std::cout << "Access over allowed bounds detected!" << '\n';
std::cout << hex <<addr << "\t" << insdis << std::endl;
}
else if (INS_MemoryDisplacement(ins) <= 0
&& ((INS_OperandMemoryBaseReg(ins, 1) == REG_RSP)
|| (INS_OperandMemoryBaseReg(ins, 1) == REG_ESP)))
{
std::cout << "Access over allowed bounds detected!" << '\n';
std::cout << hex <<addr << "\t" << insdis << std::endl;
}
else
{
if (INS_MemoryDisplacement(ins) >= static_cast<int64_t>(mainStack.top().value))
std::cout << "Overflow detected" << '\n';
}
}
// instructions executed in the main routine
std::cout << hex <<addr << "\t" << insdis << std::endl;
}
VOID stack_size(uint64_t addr, CONTEXT * ctxt)
{
if (addr > 0x700000000000)
return;
if (!key)
return;
// Get the corresponsing instruction to print
string insdis = insstack[addr]->get_insDis();
// Print stack and base pointer registers
std::cout << hex << "Reg::" << PIN_GetContextReg(ctxt, REG_RSP) << '\n';
std::cout << hex << "Reg::" << PIN_GetContextReg(ctxt, REG_RBP) << '\n';
// MPX mpx takes the total size size as a upper bound for the last allocated array
// We don't know the individual array bounds
// We will define rsp as lower bound and rbp as the upper bound
// Total stack size
uint64_t value = PIN_GetContextReg(ctxt, REG_RBP) - PIN_GetContextReg(ctxt, REG_RSP);
Node node{value, PIN_GetContextReg(ctxt, REG_RBP), PIN_GetContextReg(ctxt, REG_RSP)};
mainStack.push(node);
std::cout << "Pushed the value: " << dec << node.value << " on the stack" << '\n';
// Print the disassembly
//std::cout << insdis << '\n';
}
// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
// if (INS_Address(ins) > 0x700000000000)
// return;
insstack.insert(std::make_pair(INS_Address(ins), new Insr(string(INS_Disassemble(ins)),
ins)));
// if (REG_valid_for_iarg_reg_value(INS_MemoryIndexReg(ins)))
// std::cout << "true" << '\n';
if((INS_Opcode(ins) == XED_ICLASS_ADD || INS_Opcode(ins) == XED_ICLASS_SUB) &&
REG(INS_OperandReg(ins, 0)) == REG_STACK_PTR && INS_OperandIsImmediate(ins, 1))
{
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR)stack_size, IARG_ADDRINT, INS_Address(ins),
IARG_CONTEXT,
IARG_END);
// Obtain the immediate operand information as shown above.
// You can obtain the RSP register value before or after the instruction by
// passing IARG_REG_VALUE, REG_STACK_PTR to INS_Insert*.
}
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)protect, IARG_ADDRINT, INS_Address(ins),
IARG_END);
// Insert a call to docount before every instruction, no arguments are passed
}
// Lock Routine
void mutex_lock()
{
key = 0;
std::cout<<"out\n";
}
void mutex_unlock()
{
key = 1;
std::cout<<"in\n";
}
VOID malloc_before(CHAR * name, ADDRINT size)
{
if (!key)
return;
std::cout << name << "(" << size << ")" << '\n';
}
VOID malloc_after(ADDRINT ret)
{
if (!key)
return;
std::cout << "\treturns " << ret << '\n';
}
void Routine(RTN rtn, VOID *V)
{
if (RTN_Name(rtn) == "main")
{
RTN_Open(rtn);
RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)mutex_unlock, IARG_END);
RTN_InsertCall(rtn, IPOINT_AFTER, (AFUNPTR)mutex_lock, IARG_END);
RTN_Close(rtn);
}
}
void Image(IMG img, VOID *v)
{
// Find the malloc function
RTN mallocRtn = RTN_FindByName(img, "malloc");
// Find the free() function.
RTN freeRtn = RTN_FindByName(img, "free");
if (RTN_Valid(mallocRtn))
{
RTN_Open(mallocRtn);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)malloc_before,
IARG_ADDRINT, "malloc",
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_END);
RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR)malloc_after,
IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
RTN_Close(mallocRtn);
}
if (RTN_Valid(freeRtn))
{
RTN_Open(freeRtn);
// Instrument free() to print the input argument value.
RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR)malloc_before,
IARG_ADDRINT, "free",
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_END);
RTN_Close(freeRtn);
}
}
INT32 Usage()
{
cerr << "This tool counts the number of dynamic instructions executed" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
int main(int argc, char * argv[])
{
// Initialize the symbol table
PIN_InitSymbols();
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
PIN_SetSyntaxIntel();
// Routine instrumentation
RTN_AddInstrumentFunction(Routine, 0);
// Image instrumentation
IMG_AddInstrumentFunction(Image, 0);
// Register Instruction to be called to instrument instructions
INS_AddInstrumentFunction(Instruction, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}