-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathseh.cpp
357 lines (304 loc) · 9.12 KB
/
seh.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
/*
Source for x86 emulator IdaPro plugin
File: seh.cpp
Copyright (c) 2004-2022, Chris Eagle
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
*/
#include "cpu.h"
#include "seh.h"
static int seh_enable = 0;
static WIN_CONTEXT ctx;
typedef struct _VehNode {
unsigned int handler;
struct _VehNode *next;
} VehNode;
static VehNode *vehList;
struct WIN_CONTEXT *getContext() {
return &ctx;
}
int usingSEH() {
return seh_enable;
}
VehNode *findVehHandler(unsigned int handler) {
for (VehNode *h = vehList; h; h = h->next) {
if (h->handler == handler) {
return h;
}
}
return NULL;
}
void saveSEHState(Buffer &b) {
int dummy;
b.write(&dummy, sizeof(dummy));
b.write(&seh_enable, sizeof(seh_enable));
b.write(&ctx, sizeof(ctx));
}
void loadSEHState(Buffer &b) {
unsigned int dummy;
b.read(&dummy, sizeof(dummy));
b.read(&seh_enable, sizeof(seh_enable));
b.read(&ctx, sizeof(ctx));
}
void saveVEHState(Buffer &b) {
for (VehNode *v = vehList; v; v = v->next) {
b.write(&v->handler, sizeof(unsigned int));
}
}
void loadVEHState(Buffer &b) {
unsigned int dummy;
while (b.read(&dummy, sizeof(dummy)) == 0) {
addVectoredExceptionHandler(0, dummy);
}
b.reset_error();
}
//Copy current CPU state into CONTEXT structure for Windows Exception Handling
//Note that the global ctx struct is the only place that Debug and Floating
//point registers are currently defined
void cpuToContext() {
regsToContext(&cpu, &ctx);
ctx.Eip = cpu.initial_eip; //use address at which exception occurred
}
//Copy from CONTEXT structure into CPU state for Windows Exception Handling
//Note that the global ctx struct is the only place that Debug and Floating
//point registers are currently defined
void contextToCpu() {
contextToRegs(&ctx, &cpu);
}
void initContext() {
initContext(&ctx);
}
void popContext() {
unsigned char *ptr = (unsigned char*) &ctx;
unsigned int addr, i;
unsigned int ctx_size = (sizeof(WIN_CONTEXT) + 3) & ~3; //round up to next unsigned int
addr = esp;
for (i = 0; i < sizeof(WIN_CONTEXT); i++) {
*ptr++ = (unsigned char) readMem(addr++, SIZE_BYTE);
}
esp += ctx_size;
contextToCpu();
}
void getContextToMem(unsigned int addr) {
// unsigned char *ptr = (unsigned char*) &ctx;
cpuToContext();
copyContextToMem(&ctx, addr);
}
unsigned int pushContext() {
unsigned int ctx_size = (sizeof(WIN_CONTEXT) + 3) & ~3; //round up to next unsigned int
unsigned int addr = esp - ctx_size;
getContextToMem(addr);
esp = addr;
return esp;
}
void popExceptionRecord(EXCEPTION_RECORD *rec) {
unsigned char *ptr = (unsigned char*) &rec;
unsigned int addr, i;
unsigned int rec_size = (sizeof(EXCEPTION_RECORD) + 3) & ~3; //round up to next unsigned int
addr = esp;
for (i = 0; i < sizeof(EXCEPTION_RECORD); i++) {
*ptr++ = (unsigned char) readMem(addr++, SIZE_BYTE);
}
esp += rec_size;
}
unsigned int pushExceptionRecord(EXCEPTION_RECORD *rec) {
unsigned char *ptr = (unsigned char*) rec;
unsigned int addr, i;
unsigned int rec_size = (sizeof(EXCEPTION_RECORD) + 3) & ~3; //round up to next unsigned int
addr = esp -= rec_size;
for (i = 0; i < sizeof(EXCEPTION_RECORD); i++) {
writeMem(addr++, *ptr++, SIZE_BYTE);
}
return esp;
}
void doSehException(EXCEPTION_RECORD *rec) {
unsigned int err_ptr = readMem(fsBase, SIZE_DWORD);
unsigned int handler = readMem(err_ptr + 4, SIZE_DWORD); //err->handler
//do sanity checks on handler here?
cpuToContext();
unsigned int ctx_ptr = pushContext();
unsigned int rec_ptr = pushExceptionRecord(rec);
push(ctx_ptr, SIZE_DWORD);
push(err_ptr, SIZE_DWORD); //err_ptr == fsBase??
push(rec_ptr, SIZE_DWORD);
push(SEH_MAGIC, SIZE_DWORD); //handler return address
//need to execute exception handler here setup flag to trap ret
//set eip to start of exception handler and resume fetching
cpu.eip = handler;
}
static unsigned int currentVehHandler;
void doVehException(EXCEPTION_RECORD *rec, unsigned int handler) {
cpuToContext();
unsigned int ctx_ptr = pushContext();
unsigned int rec_ptr = pushExceptionRecord(rec);
push(ctx_ptr, SIZE_DWORD);
push(rec_ptr, SIZE_DWORD);
push(esp, SIZE_DWORD);
push(VEH_MAGIC, SIZE_DWORD); //handler return address
//need to execute exception handler here setup flag to trap ret
//set eip to start of exception handler and resume fetching
cpu.eip = handler;
}
void doException(EXCEPTION_RECORD *rec) {
if (vehList) {
if (currentVehHandler == 0) {
currentVehHandler = vehList->handler;
doVehException(rec, currentVehHandler);
}
else {
VehNode *v = findVehHandler(currentVehHandler);
if (v) {
v = v->next;
}
if (v) {
currentVehHandler = v->handler;
doVehException(rec, currentVehHandler);
}
else {
currentVehHandler = 0xffffffff;
}
}
}
else {
currentVehHandler = 0xffffffff;
}
if (currentVehHandler == 0xffffffff) {
doSehException(rec);
}
}
void sehReturn() {
EXCEPTION_RECORD rec;
//need to check eax here to see if exception was handled
//or if it needs to be kicked up to next SEH handler
esp += 3 * SIZE_DWORD; //clear off exception pointers
popExceptionRecord(&rec);
popContext();
contextToCpu();
//eip is now restored to pre exception location
//need to fake an iret here
doInterruptReturn(); //this clobbers EIP, CS, EFLAGS
//so restore them here from ctx values
cpu.eip = ctx.Eip;
cpu.eflags = ctx.EFlags;
_cs = ctx.SegCs;
msg("Performing SEH return\n");
currentVehHandler = 0;
}
void vehReturn() {
EXCEPTION_RECORD rec;
//need to check eax here to see if exception was handled
//or if it needs to be kicked up to next SEH handler
unsigned int res = eax;
esp += 3 * SIZE_DWORD; //clear off exception pointers
popExceptionRecord(&rec);
popContext();
contextToCpu();
//eip is now restored to pre exception location
//need to fake an iret here
doInterruptReturn(); //this clobbers EIP, CS, EFLAGS
//so restore them here from ctx values
cpu.eip = ctx.Eip;
cpu.eflags = ctx.EFlags;
_cs = ctx.SegCs;
msg("Performing VEH return\n");
if (res == EXCEPTION_CONTINUE_EXECUTION) {
currentVehHandler = 0;
}
else { //res == EXCEPTION_CONTINUE_SEARCH
doException(&rec);
}
}
void generateException(unsigned int code) {
if (seh_enable) {
EXCEPTION_RECORD rec;
rec.exceptionCode = code;
rec.exceptionFlags = CONTINUABLE; //nothing sophisticated here
rec.exceptionRecord = 0; //NULL
rec.exceptionAddress = cpu.initial_eip;
rec.numberParameters = 0;
doException(&rec);
}
}
void breakpointException() {
generateException(BREAKPOINT_EXCEPTION);
}
void debugException() {
generateException(DEBUG_EXCEPTION);
}
void divzeroException() {
generateException(DIV_ZERO_EXCEPTION);
}
void memoryAccessException() {
generateException(MEM_ACCESS);
}
void IllegalOpcodeException() {
generateException(UNDEFINED_OPCODE_EXCEPTION);
}
void enableSEH() {
initContext();
seh_enable = 1;
}
void sehBegin(unsigned int interrupt_number) {
msg("Initiating SEH processing of INT %d\n", interrupt_number);
switch (interrupt_number) {
case 0:
generateException(DIV_ZERO_EXCEPTION);
break;
case 1:
generateException(DEBUG_EXCEPTION);
break;
case 3:
generateException(BREAKPOINT_EXCEPTION);
break;
case 6:
generateException(UNDEFINED_OPCODE_EXCEPTION);
break;
case 14:
generateException(MEM_ACCESS);
break;
}
}
void addVectoredExceptionHandler(bool first, unsigned int handler) {
VehNode *n = (VehNode*)malloc(sizeof(VehNode));
n->handler = handler;
if (first) {
n->next = vehList;
vehList = n;
}
else {
n->next = NULL;
if (vehList) {
VehNode *h;
for (h = vehList; h->next; h = h->next) {}
h->next = n;
}
else {
vehList = n;
}
}
}
void removeVectoredExceptionHandler(unsigned int handler) {
VehNode *p = NULL;
for (VehNode *h = vehList; h->next; h = h->next) {
if (h->handler == handler) {
if (p) {
p->next = h->next;
}
else {
vehList = p->next;
}
free(h);
break;
}
p = h;
}
}