Newer
Older
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
#include "scfi_instr.hpp"
#include "Rewrite_Utility.hpp"
#include <stdlib.h>
using namespace std;
using namespace libIRDB;
virtual_offset_t getAvailableAddress(FileIR_t *p_virp)
{
static int counter = -16;
counter += 16;
return 0xf0020000 + counter;
}
static Instruction_t* addNewAssembly(FileIR_t* firp, Instruction_t *p_instr, string p_asm)
{
Instruction_t* newinstr;
if (p_instr)
newinstr = allocateNewInstruction(firp,p_instr->GetAddress()->GetFileID(), p_instr->GetFunction());
else
newinstr = allocateNewInstruction(firp,BaseObj_t::NOT_IN_DATABASE, NULL);
firp->RegisterAssembly(newinstr, p_asm);
if (p_instr)
{
newinstr->SetFallthrough(p_instr->GetFallthrough());
p_instr->SetFallthrough(newinstr);
}
return newinstr;
}
static Instruction_t* registerCallbackHandler64(FileIR_t* firp, Instruction_t *p_orig, string p_callbackHandler, int p_numArgs)
{
Instruction_t *instr;
Instruction_t *first;
char tmpbuf[1024];
// save flags and 16 registers (136 bytes)
// call pushes 8 bytes
// Total: 8 * 18 = 144
first = instr = addNewAssembly(firp,NULL, "push rsp");
instr = addNewAssembly(firp,instr, "push rbp");
instr = addNewAssembly(firp,instr, "push rdi");
instr = addNewAssembly(firp,instr, "push rsi");
instr = addNewAssembly(firp,instr, "push rdx");
instr = addNewAssembly(firp,instr, "push rcx");
instr = addNewAssembly(firp,instr, "push rbx");
instr = addNewAssembly(firp,instr, "push rax");
instr = addNewAssembly(firp,instr, "push r8");
instr = addNewAssembly(firp,instr, "push r9");
instr = addNewAssembly(firp,instr, "push r10");
instr = addNewAssembly(firp,instr, "push r11");
instr = addNewAssembly(firp,instr, "push r12");
instr = addNewAssembly(firp,instr, "push r13");
instr = addNewAssembly(firp,instr, "push r14");
instr = addNewAssembly(firp,instr, "push r15");
instr = addNewAssembly(firp,instr, "pushf");
// handle the arguments (if any): rdi, rsi, rdx, rcx, r8, r9
// first arg starts at byte +144
instr = addNewAssembly(firp,instr, "mov rdi, rsp");
if (p_numArgs >= 1)
instr = addNewAssembly(firp,instr, "mov rsi, [rsp+144]");
if (p_numArgs >= 2)
instr = addNewAssembly(firp,instr, "mov rdx, [rsp+152]");
if (p_numArgs >= 3)
instr = addNewAssembly(firp,instr, "mov rcx, [rsp+160]");
if (p_numArgs >= 4)
instr = addNewAssembly(firp,instr, "mov r8, [rsp+168]");
if (p_numArgs > 4)
assert(0); // only handle up to 5 args
// pin the instruction that follows the callback handler
Instruction_t* postCallback = allocateNewInstruction(firp, BaseObj_t::NOT_IN_DATABASE, NULL);
virtual_offset_t postCallbackReturn = getAvailableAddress(firp);
postCallback->GetAddress()->SetVirtualOffset(postCallbackReturn);
// push the address to return to once the callback handler is invoked
sprintf(tmpbuf,"mov rax, 0x%x", postCallbackReturn);
instr = addNewAssembly(firp,instr, tmpbuf);
instr = addNewAssembly(firp,instr, "push rax");
// use a nop instruction for the actual callback
instr = addNewAssembly(firp,instr, "nop");
instr->SetComment(" -- callback: " + p_callbackHandler);
instr->SetCallback(p_callbackHandler);
instr->SetFallthrough(postCallback);
// need to make sure the post callback address is pinned
// (so that ILR and other transforms do not relocate it)
AddressID_t *indTarg = new AddressID_t();
firp->GetAddresses().insert(indTarg);
indTarg->SetVirtualOffset(postCallback->GetAddress()->GetVirtualOffset());
indTarg->SetFileID(BaseObj_t::NOT_IN_DATABASE); // SPRI global namespace
postCallback->SetIndirectBranchTargetAddress(indTarg);
// restore registers
firp->RegisterAssembly(postCallback, "popf");
instr = addNewAssembly(firp,postCallback, "pop r15");
instr = addNewAssembly(firp,instr, "pop r14");
instr = addNewAssembly(firp,instr, "pop r13");
instr = addNewAssembly(firp,instr, "pop r12");
instr = addNewAssembly(firp,instr, "pop r11");
instr = addNewAssembly(firp,instr, "pop r10");
instr = addNewAssembly(firp,instr, "pop r9");
instr = addNewAssembly(firp,instr, "pop r8");
instr = addNewAssembly(firp,instr, "pop rax");
instr = addNewAssembly(firp,instr, "pop rbx");
instr = addNewAssembly(firp,instr, "pop rcx");
instr = addNewAssembly(firp,instr, "pop rdx");
instr = addNewAssembly(firp,instr, "pop rsi");
instr = addNewAssembly(firp,instr, "pop rdi");
instr = addNewAssembly(firp,instr, "pop rbp");
instr = addNewAssembly(firp,instr, "lea rsp, [rsp+8]");
instr = addNewAssembly(firp,instr, "ret");
// return first instruction in the callback handler chain
return first;
}
// x86-64
// 20140421
static void ConvertCallToCallbackHandler64(FileIR_t* firp, Instruction_t *p_orig, string p_callbackHandler, int p_numArgs)
{
static std::map<std::string, Instruction_t*> m_handlerMap;
// nb: if first time, register and cache callback handler sequence
if (m_handlerMap.count(p_callbackHandler) == 0)
{
m_handlerMap[p_callbackHandler] = registerCallbackHandler64(firp,p_orig, p_callbackHandler, p_numArgs);
}
if (p_orig)
p_orig->SetTarget(m_handlerMap[p_callbackHandler]);
}
static Instruction_t* addCallbackHandlerSequence
(
FileIR_t* firp,
Instruction_t *p_orig,
bool before,
std::string p_detector
)
{
if(before)
insertAssemblyBefore(firp,p_orig,"lea rsp, [rsp-128]");
else
assert(0); // add handling for inserting lea after given insn
p_orig->SetComment("callback: " + p_detector);
Instruction_t* call =insertAssemblyAfter(firp,p_orig,"call 0");
ConvertCallToCallbackHandler64(firp, call, p_detector, 0); // no args for now
insertAssemblyAfter(firp,call,"lea rsp, [rsp + 128 + 0]"); // no args for nwo
return p_orig;
}
Relocation_t* SCFI_Instrument::create_reloc(Instruction_t* insn)
{
Relocation_t* reloc=new Relocation_t;
insn->GetRelocations().insert(reloc);
firp->GetRelocations().insert(reloc);
return reloc;
}
bool SCFI_Instrument::add_scfi_instrumentation(Instruction_t* insn)
{
bool success=true;
if(getenv("SCFI_VERBOSE")!=NULL)
;
return success;
}
bool SCFI_Instrument::needs_scfi_instrumentation(Instruction_t* insn)
{
return false;
}
unsigned int SCFI_Instrument::GetNonce(Instruction_t* insn)
{
/* in time we look up the nonce category for this insn */
/* for now, it's just f4 as the nonce */
return 0xf4;
}
unsigned int SCFI_Instrument::GetNonceSize(Instruction_t* insn)
{
/* in time we look up the nonce size for this insn */
/* for now, it's just f4 as the nonce */
return 1;
}
for(InstructionSet_t::iterator it=firp->GetInstructions().begin();
it!=firp->GetInstructions().end();
++it)
{
Instruction_t* insn=*it;
if(insn->GetIndirectBranchTargetAddress())
{
string type;
type="cfi_nonce=";
type+=to_string(GetNonce(insn));
Relocation_t* reloc=create_reloc(insn);
reloc->SetOffset(-GetNonceSize(insn));
reloc->SetType(type);
}
}
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
void SCFI_Instrument::AddReturnCFI(Instruction_t* insn)
{
string reg="rcx";
string decoration="";
int nonce_size=GetNonceSize(insn);
unsigned int nonce=GetNonce(insn);
Instruction_t* jne=NULL, *tmp=NULL;
// convert a return to:
// pop rcx
// cmp <nonce size> PTR [rcx-<nonce size>], Nonce
// jne slow ; reloc such that strata/zipr can convert slow to new code
// ; to handle places where nonce's can't be placed.
// jmp rcx
switch(nonce_size)
{
case 1:
decoration="byte ";
break;
case 2: // handle later
case 4: // handle later
default:
cerr<<"Cannot handle nonce of size "<<std::dec<<nonce_size<<endl;
assert(0);
}
// insert the pop/checking code.
insertAssemblyBefore(firp,insn,string("pop ")+reg);
tmp=insertAssemblyAfter(firp,insn,string("cmp ")+decoration+" ["+reg+"], "+to_string(nonce));
jne=tmp=insertAssemblyAfter(firp,tmp,"jne 0");
// convert the ret instruction to a jmp ecx
setInstructionAssembly(firp,tmp->GetFallthrough(), string("jmp ")+reg, NULL,NULL);
// set the jne's target to itself, and create a reloc that zipr/strata will have to resolve.
jne->SetTarget(jne); // needed so spri/spasm/irdb don't freak out about missing target for new insn.
Relocation_t* reloc=create_reloc(jne);
reloc->SetType("slow_cfi_path");
reloc->SetOffset(0);
return;
}
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
InstructionSet_t to_instrument;
// we do this in two passes. first pass: find instructions.
for(InstructionSet_t::iterator it=firp->GetInstructions().begin();
it!=firp->GetInstructions().end();
++it)
{
Instruction_t* insn=*it;
DISASM d;
insn->Disassemble(d);
switch(d.Instruction.BranchType)
{
case JmpType:
case CallType:
// not yet implemented.
break;
case RetType: to_instrument.insert(insn);
break;
default:
break;
}
}
// second pass, insert checking. can't do this on pass one because
// we add IBs, which we'd later decide instrument
for(InstructionSet_t::iterator it=to_instrument.begin();
it!=to_instrument.end();
++it)
{
Instruction_t* insn=*it;
AddReturnCFI(insn);
}