-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_state.cpp
444 lines (407 loc) · 10.2 KB
/
process_state.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
#include "process_state.h"
#include <stdio.h>
#include <string.h>
#define NF 0
#define TD 0
#define TF 0
#define TI 0
#define TN 0
#define TP 0
#define TS 0
struct sysent {
size_t nargs;
int sys_flags;
enum SYSCALL sysid;
const char *sys_name;
};
/* All of the modes we support have the same error table. If we support more in
* the future this may need to change. As of writing this, this number is only
* used to determine if a result from a syscall is an error. */
static size_t nerrnos = 531;
#if defined(I386)
static unsigned long scno_tab0[NUM_SYSCALLS];
static const struct sysent sysent0[] = {
#include "linux/i386/syscallent.h"
};
#elif defined(X32)
#define HAVE_SYSENT1
static unsigned long scno_tab0[NUM_SYSCALLS];
static const struct sysent sysent0[] = {
#include "linux/x32/syscallent.h"
};
static unsigned long scno_tab1[NUM_SYSCALLS];
static const struct sysent sysent1[] = {
#include "linux/x32/syscallent1.h"
};
#elif defined(X86_64)
#define HAVE_SYSENT1
#define HAVE_SYSENT2
static unsigned long scno_tab0[NUM_SYSCALLS];
static const struct sysent sysent0[] = {
#include "linux/x86_64/syscallent.h"
};
static unsigned long scno_tab1[NUM_SYSCALLS];
static const struct sysent sysent1[] = {
#include "linux/x86_64/syscallent1.h"
};
static unsigned long scno_tab2[NUM_SYSCALLS];
static const struct sysent sysent2[] = {
#include "linux/x86_64/syscallent2.h"
};
#else
#error "unknown architecture"
#endif
void init_process_state() {
size_t i;
memset(scno_tab0, -1, sizeof(scno_tab0));
for(i = 0; i < sizeof(sysent0) / sizeof(sysent); i++) {
scno_tab0[sysent0[i].sysid] = i;
}
#ifdef HAVE_SYSENT1
memset(scno_tab1, -1, sizeof(scno_tab1));
for(i = 0; i < sizeof(sysent1) / sizeof(sysent); i++) {
scno_tab1[sysent1[i].sysid] = i;
}
#endif
#ifdef HAVE_SYSENT2
memset(scno_tab2, -1, sizeof(scno_tab2));
for(i = 0; i < sizeof(sysent2) / sizeof(sysent); i++) {
scno_tab2[sysent2[i].sysid] = i;
}
#endif
}
process_state::process_state(pid_t pid) : pid(pid), error_state(0), pers(0) {
long res;
#if defined(I386)
res = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
#else
res = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
#endif
if(res) {
error_state |= 1;
return;
}
#if defined(X86_64) || defined(X32)
/* Check CS register value. On x86-64 linux it is:
* 0x33 for long mode (64 bit)
* 0x23 for compatibility mode (32 bit)
* Check DS register value. On x86-64 linux it is:
* 0x2b for x32 mode (x86-64 in 32 bit)
*/
switch (x86_64_regs.cs) {
case 0x23: pers = 1; break;
case 0x33:
if (x86_64_regs.ds == 0x2b) {
pers = 2;
} else {
pers = 0;
}
break;
default:
fprintf(stderr, "Unknown execution mode\n");
error_state |= 1;
return;
}
# ifdef X32
/* Value of pers:
* 0: 64 bit
* 1: 32 bit
* 2: X32
* Transform to:
* 0: X32
* 1: 32 bit
*/
switch (pers) {
case 0:
fprintf(stderr, "64-bit mode not supported for x32 sjail\n");
error_state |= 1;
break;
case 2:
pers = 0;
break;
}
# endif
#endif
unsigned long scno;
#if defined(I386)
scno = i386_regs.orig_eax;
#else
# ifndef __X32_SYSCALL_BIT
# define __X32_SYSCALL_BIT 0x40000000
# endif
# ifndef __X32_SYSCALL_MASK
# define __X32_SYSCALL_MASK __X32_SYSCALL_BIT
# endif
scno = x86_64_regs.orig_rax;
if(x86_64_regs.cs == 0x33 && x86_64_regs.ds == 0x2b) {
scno &= ~__X32_SYSCALL_MASK;
}
#endif
sys = sys_none;
if(pers == 0) {
if(scno < sizeof(sysent0) / sizeof(sysent)) {
sys = sysent0[scno].sysid;
} else {
error_state |= 1;
fprintf(stderr, "Bad syscall number\n");
}
#ifdef HAVE_SYSENT1
} else if(pers == 1) {
if(scno < sizeof(sysent1) / sizeof(sysent)) {
sys = sysent1[scno].sysid;
} else {
error_state |= 1;
fprintf(stderr, "Bad syscall number\n");
}
#endif
#ifdef HAVE_SYSENT2
} else if(pers == 2) {
if(scno < sizeof(sysent2) / sizeof(sysent)) {
sys = sysent2[scno].sysid;
} else {
error_state |= 1;
fprintf(stderr, "Bad syscall number\n");
}
#endif
} else {
error_state |= 1;
fprintf(stderr, "Bad personality\n");
}
}
enum SYSCALL process_state::get_syscall() {
return sys;
}
void process_state::set_syscall(enum SYSCALL sys) {
this->sys = sys;
unsigned long scno;
if(pers == 0) {
scno = scno_tab0[sys];
#ifdef HAVE_SYSENT1
} else if(pers == 1) {
scno = scno_tab1[sys];
#endif
#ifdef HAVE_SYSENT2
} else if(pers == 2) {
scno = scno_tab2[sys];
#endif
} else {
error_state |= 1;
fprintf(stderr, "Bad personality\n");
return;
}
if((long)scno == -1) {
error_state |= 1;
fprintf(stderr, "No syscall on architecture\n");
return;
}
#if defined(I386)
i386_regs.orig_eax = scno;
#else
# ifndef __X32_SYSCALL_BIT
# define __X32_SYSCALL_BIT 0x40000000
# endif
# ifndef __X32_SYSCALL_MASK
# define __X32_SYSCALL_MASK __X32_SYSCALL_BIT
# endif
if(x86_64_regs.cs == 0x33 && x86_64_regs.ds == 0x2b) {
x86_64_regs.orig_rax = (x86_64_regs.orig_rax & __X32_SYSCALL_MASK) | scno;
} else {
x86_64_regs.orig_rax = scno;
}
#endif
}
size_t process_state::get_num_params(SYSCALL sys) {
if(pers == 0) {
if(scno_tab0[sys] != (unsigned long)-1) {
return sysent0[scno_tab0[sys]].nargs;
}
#ifdef HAVE_SYSENT1
} else if(pers == 1) {
if(scno_tab1[sys] != (unsigned long)-1) {
return sysent1[scno_tab1[sys]].nargs;
}
#endif
#ifdef HAVE_SYSENT2
} else if(pers == 2) {
if(scno_tab2[sys] != (unsigned long)-1) {
return sysent2[scno_tab2[sys]].nargs;
}
#endif
} else {
error_state |= 1;
fprintf(stderr, "Bad personality\n");
return 0U;
}
error_state |= 1;
fprintf(stderr, "No syscall on architecture\n");
return 0U;
}
const char* process_state::get_syscall_name(SYSCALL sys) {
if(pers == 0) {
if(scno_tab0[sys] != (unsigned long)-1) {
return sysent0[scno_tab0[sys]].sys_name;
}
#ifdef HAVE_SYSENT1
} else if(pers == 1) {
if(scno_tab1[sys] != (unsigned long)-1) {
return sysent1[scno_tab1[sys]].sys_name;
}
#endif
#ifdef HAVE_SYSENT2
} else if(pers == 2) {
if(scno_tab2[sys] != (unsigned long)-1) {
return sysent2[scno_tab2[sys]].sys_name;
}
#endif
} else {
error_state |= 1;
fprintf(stderr, "Bad personality\n");
return NULL;
}
error_state |= 1;
fprintf(stderr, "No syscall on architecture\n");
return NULL;
}
bool process_state::is_error_result() {
param_t val = get_result();
param_t max = -(long int) nerrnos;
#if defined(X86_64)
if (pers != 0) {
val = (unsigned int) val;
max = (unsigned int) max;
}
#endif
return val > max;
}
param_t process_state::get_result() {
#if defined(I386)
return i386_regs.eax;
#else
if(pers == 1) {
return (unsigned int)x86_64_regs.rax;
} else {
return x86_64_regs.rax;
}
#endif
}
void process_state::set_result(param_t v) {
#if defined(I386)
i386_regs.eax = v;
#else
x86_64_regs.rax = v;
#endif
}
param_t process_state::get_param(size_t i) {
#if defined(I386)
switch(i) {
case 0: return i386_regs.ebx;
case 1: return i386_regs.ecx;
case 2: return i386_regs.edx;
case 3: return i386_regs.esi;
case 4: return i386_regs.edi;
case 5: return i386_regs.ebp;
}
#else
if(pers == 1) {
switch(i) {
/* i386 ABI */
case 0: return (unsigned int)x86_64_regs.rbx;
case 1: return (unsigned int)x86_64_regs.rcx;
case 2: return (unsigned int)x86_64_regs.rdx;
case 3: return (unsigned int)x86_64_regs.rsi;
case 4: return (unsigned int)x86_64_regs.rdi;
case 5: return (unsigned int)x86_64_regs.rbp;
}
} else switch(i) {
case 0: return x86_64_regs.rdi;
case 1: return x86_64_regs.rsi;
case 2: return x86_64_regs.rdx;
case 3: return x86_64_regs.r10;
case 4: return x86_64_regs.r8;
case 5: return x86_64_regs.r9;
}
#endif
fprintf(stderr, "bad parameter index\n");
return 0;
}
void process_state::set_param(size_t i, param_t val) {
#if defined(I386)
switch(i) {
case 0: i386_regs.ebx = val; break;
case 1: i386_regs.ecx = val; break;
case 2: i386_regs.edx = val; break;
case 3: i386_regs.esi = val; break;
case 4: i386_regs.edi = val; break;
case 5: i386_regs.ebp = val; break;
default:
error_state |= 1;
fprintf(stderr, "bad parameter index\n");
}
#else
if(pers == 1) {
/* i386 ABI */
unsigned long I386ABI_MASK = 0xFFFFFFFF00000000UL;
unsigned int v = (unsigned int)val;
switch(i) {
case 0: x86_64_regs.rbx = (x86_64_regs.rbx & I386ABI_MASK) | v; break;
case 1: x86_64_regs.rcx = (x86_64_regs.rcx & I386ABI_MASK) | v; break;
case 2: x86_64_regs.rdx = (x86_64_regs.rdx & I386ABI_MASK) | v; break;
case 3: x86_64_regs.rsi = (x86_64_regs.rsi & I386ABI_MASK) | v; break;
case 4: x86_64_regs.rdi = (x86_64_regs.rdi & I386ABI_MASK) | v; break;
case 5: x86_64_regs.rbp = (x86_64_regs.rbp & I386ABI_MASK) | v; break;
default:
error_state |= 1;
fprintf(stderr, "bad parameter index\n");
}
} else switch(i) {
case 0: x86_64_regs.rdi = val; break;
case 1: x86_64_regs.rsi = val; break;
case 2: x86_64_regs.rdx = val; break;
case 3: x86_64_regs.r10 = val; break;
case 4: x86_64_regs.r8 = val; break;
case 5: x86_64_regs.r9 = val; break;
default:
error_state |= 1;
fprintf(stderr, "bad parameter index\n");
}
#endif
}
pid_t process_state::get_pid() {
return pid;
}
int process_state::error() {
return error_state;
}
void process_state::save() {
#if defined(I386)
long res = ptrace(PTRACE_SETREGS, pid, NULL, &i386_regs);
#else
long res = ptrace(PTRACE_SETREGS, pid, NULL, &x86_64_regs);
#endif
error_state |= res != 0;
}
size_t process_state::word_width() {
#if defined(I386)
return sizeof(param_t);
#else
return pers == 1 ? sizeof(unsigned int) : sizeof(param_t);
#endif
}
param_t process_state::read_uword(void* addr) {
#if defined(I386)
return *(param_t*)addr;
#else
return pers == 1 ? *(unsigned int*)addr : *(param_t*)addr;
#endif
}
void process_state::write_uword(void* addr, param_t v) {
#if defined(I386)
*(param_t*)addr = v;
#else
if(pers == 1) {
*(unsigned int*)addr = (unsigned int)v;
} else {
*(param_t*)addr = v;
}
#endif
}