-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.c
1591 lines (1432 loc) · 39.4 KB
/
loader.c
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <asm/ldt.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <libelf.h> /* on some other systems, <libelf/libelf.h> */
#include <assert.h>
#include "sizes.h"
#ifdef VERIFY
#include "libdis.h"
#endif
#define CODE_END CODE_START + CODE_SIZE
#define DATA_END DATA_START + DATA_SIZE
int outside_esp;
int inside_esp;
int outside_ebp;
int inside_ebp;
/* It seems better from an isolation standpoint to use a separate
%gs for stack checks inside the sandbox, but that would require
switching the outside one back before calling any libc functions,
since it also expects to access thread information in that segment.
So for the moment we share the %gs segment, but check that only
offset 0x14 is accessed. */
/* short outside_gs;
short inside_gs; */
int outside_ebx;
int outside_esi;
int outside_edi;
jmp_buf exit_buf;
int exit_value;
int call_in(void *addr, int argc, char **argv) {
int ret;
if (!setjmp(exit_buf)) {
/* XXX This is brittle wrt. GCC's code generation strategy. */
asm("movl %0, %%ecx" :: "r" (argc));
asm("movl %0, %%edx" :: "r" (argv));
asm("movl %0, %%eax" :: "r" (addr));
asm("movl %esp, outside_esp");
asm("movl %ebp, outside_ebp");
asm("movl %ebx, outside_ebx");
asm("movl %esi, outside_esi");
asm("movl %edi, outside_edi");
/* asm("movw %gs, outside_gs"); */
asm("movl inside_esp, %esp");
asm("movl inside_ebp, %ebp");
/* asm("movw inside_gs, %gs"); */
asm("call *%eax");
asm("movl %esp, inside_esp");
asm("movl %ebp, inside_ebp");
/* asm("movw %gs, inside_gs"); */
asm("movl outside_esp, %esp");
asm("movl outside_ebp, %ebp");
asm("movl outside_ebx, %ebx");
asm("movl outside_esi, %esi");
asm("movl outside_edi, %edi");
/* asm("movw outside_gs, %gs"); */
asm("movl %%eax, %0" : "=r" (ret));
return ret;
}
return exit_value;
}
/* Is this declared anywhere? */
extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
/* Set up a segment descriptor for a one-page segment starting at the
given linear address, and return its selector. */
short setup_gs_ldt(unsigned addr) {
struct user_desc desc;
short selector = 16 + 4 + 3; /* 4=LDT, 3=RPL */
memset(&desc, 0, sizeof(desc));
desc.seg_32bit = 1;
desc.read_exec_only = 0;
desc.limit_in_pages = 1;
desc.seg_not_present = 0;
desc.useable = 1;
desc.entry_number = selector / 8;
desc.base_addr = addr;
desc.limit = 1;
desc.contents = MODIFY_LDT_CONTENTS_DATA;
if (modify_ldt(1, &desc, sizeof(desc)) < 0) {
fprintf(stderr, "Segment descriptor setup failed\n");
exit(-1);
}
return selector;
}
#ifdef __cplusplus
extern "C" {
#endif
extern unsigned long long
__udivdi3(unsigned long long a, unsigned long long b);
extern long long
__divdi3(long long a, long long b);
extern unsigned long long
__umoddi3(unsigned long long a, unsigned long long b);
extern long long
__moddi3(long long a, long long b);
extern double
__floatdidf(long long a);
extern long long
__fixdfdi(double a);
#ifdef __cplusplus
} /* extern "C" */
#endif
unsigned long long wrap___udivdi3(unsigned long long a, unsigned long long b) {
return __udivdi3(a, b);
}
long long wrap___divdi3(long long a, long long b) {
return __divdi3(a, b);
}
unsigned long long wrap___umoddi3(unsigned long long a, unsigned long long b) {
return __umoddi3(a, b);
}
long long wrap___moddi3(long long a, long long b) {
return __moddi3(a, b);
}
double wrap___floatdidf(long long a) {
return __floatdidf(a);
}
long long wrap___fixdfdi(double a) {
return __fixdfdi(a);
}
int wrap_printf(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
return vprintf(fmt, ap);
}
int wrap_print_int(int i) {
return printf("%d\n", i);
}
long long wrap_atoll(const char *nptr) {
return atoll(nptr);
}
int wrap_putchar(int c) {
return putchar(c);
}
void *data_break;
void *wrap_sbrk(long incr) {
void *new_brk = (void *)((long)data_break + incr);
if ((unsigned)new_brk > (unsigned)DATA_START &&
(unsigned)new_brk < (unsigned)DATA_END) {
void *old_brk = data_break;
/* printf("Moving break by 0x%lx to %p\n", incr, new_brk); */
data_break = new_brk;
return old_brk;
} else {
/* printf("Rejecting sbrk by 0x%lx to %p\n", incr, new_brk); */
return (void *)-1;
}
}
char *wrap_outside_strerror(int errnum) {
return strerror(errnum);
}
void wrap_exit(int val) {
exit_value = val;
longjmp(exit_buf, 1);
}
FILE *files[16] = {/*stdin, stdout, stderr,*/ 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int wrap_outside_fopen(const char *path, const char *mode) {
int i;
for (i = 0; i < 16; i++)
if (!files[i])
break;
if (i == 16)
abort();
files[i] = fopen(path, mode);
if (!files[i])
return -1;
return i;
}
int wrap_outside_fdopen(int fd, const char *mode) {
int i;
for (i = 0; i < 16; i++)
if (!files[i])
break;
if (i == 16)
abort();
files[i] = fdopen(fd, mode);
if (!files[i])
return -1;
return i;
}
int wrap_outside_fclose(int fi) {
int ret = fclose(files[fi]);
files[fi] = 0;
return ret;
}
int wrap_outside_errno(void) {
return errno;
}
int wrap_outside_ferror(int fi) {
return ferror(files[fi]);
}
int wrap_outside_fflush(int fi) {
return fflush(files[fi]);
}
int wrap_outside_fileno(int fi) {
return fileno(files[fi]);
}
int wrap_outside_feof(int fi) {
return feof(files[fi]);
}
int wrap_outside_fgetc(int fi) {
return fgetc(files[fi]);
}
int wrap_outside_vfprintf(int fi, const char *fmt, va_list ap) {
return vfprintf(files[fi], fmt, ap);
}
int wrap_outside_vfscanf(int fi, const char *fmt, va_list ap) {
return vfscanf(files[fi], fmt, ap);
}
size_t wrap_outside_fread(void *ptr, size_t size, size_t num, int fi) {
return fread(ptr, size, num, files[fi]);
}
size_t wrap_outside_fwrite(const void *ptr, size_t size, size_t num, int fi) {
return fwrite(ptr, size, num, files[fi]);
}
void wrap_outside_rewind(int fi) {
rewind(files[fi]);
}
int wrap_outside_ungetc(int c, int fi) {
return ungetc(c, files[fi]);
}
int wrap_gettimeofday(struct timeval *tv, struct timezone *tz) {
return gettimeofday(tv, tz);
}
int wrap_sscanf(const char *str, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
return vsscanf(str, fmt, ap);
}
double wrap_atof(const char *nptr) {
return atof(nptr);
}
int wrap_outside_fseek(int fi, long offset, int whence) {
return fseek(files[fi], offset, whence);
}
double wrap_ldexp(double x, int exp) {
return ldexp(x, exp);
}
long wrap_outside_ftell(int fi) {
return ftell(files[fi]);
}
double wrap_floor(double x) {
return floor(x);
}
float wrap_floorf(float x) {
return floorf(x);
}
double wrap_sin(double x) {
return sin(x);
}
double wrap_sqrt(double x) {
return sqrt(x);
}
float wrap_sqrtf(float x) {
return sqrtf(x);
}
void wrap_abort(void) {
abort();
}
double wrap_rint(double x) {
return rint(x);
}
long wrap_lrint(double x) {
return lrint(x);
}
long wrap_lrintf(float x) {
return lrintf(x);
}
double wrap_acos(double x) {
return acos(x);
}
double wrap_asin(double x) {
return asin(x);
}
double wrap_atan(double x) {
return atan(x);
}
double wrap_cos(double x) {
return cos(x);
}
double wrap_exp(double x) {
return exp(x);
}
float wrap_expf(float x) {
return expf(x);
}
double wrap_fabs(double x) {
return fabs(x);
}
double wrap_log(double x) {
return log(x);
}
double wrap_log10(double x) {
return log10(x);
}
double wrap_ceil(double x) {
return ceil(x);
}
float wrap_ceilf(float x) {
return ceilf(x);
}
double wrap_pow(double x, double y) {
return pow(x, y);
}
double wrap_frexp(double x, int *exp) {
return frexp(x, exp);
}
int wrap_sprintf(char *str, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
return vsprintf(str, fmt, ap);
}
char *wrap_outside_getenv(const char *name) {
return getenv(name);
}
int wrap_outside_access(const char *pathname, int mode) {
return access(pathname, mode);
}
int wrap_getrusage(int who, struct rusage *usage) {
return getrusage(who, usage);
}
int wrap_outside_close(int fd) {
return close(fd);
}
char *wrap_outside_fgets(char *buf, int size, int fi) {
return fgets(buf, size, files[fi]);
}
int wrap_outside_fputc(int c, int fi) {
return fputc(c, files[fi]);
}
int wrap_outside_fputs(const char *str, int fi) {
return fputs(str, files[fi]);
}
int wrap_outside_open(const char *pathname, int flags, int mode) {
return open(pathname, flags, mode);
}
off_t wrap_outside_lseek(int fd, off_t offset, int whence) {
return lseek(fd, offset, whence);
}
int wrap_outside_read(int fd, void *buf, size_t count) {
return read(fd, buf, count);
}
DIR *wrap_outside_opendir(const char *name) {
return opendir(name);
}
int wrap_outside_closedir(DIR *dir) {
return closedir(dir);
}
struct dirent *wrap_outside_readdir(DIR *dir) {
return readdir(dir);
}
struct inside_stat {
off_t inside_st_size;
int inside_st_mode;
time_t inside_st_mtime;
nlink_t inside_st_nlink;
ino_t inside_st_ino;
dev_t inside_st_dev;
uid_t inside_st_uid;
gid_t inside_st_gid;
dev_t inside_st_rdev;
time_t inside_st_atime;
time_t inside_st_ctime;
};
int wrap_outside_stat(const char *fname, struct inside_stat *in_buf) {
struct stat buf;
int ret = stat(fname, &buf);
if (!ret) {
in_buf->inside_st_size = buf.st_size;
in_buf->inside_st_mode = buf.st_mode;
in_buf->inside_st_mtime = buf.st_mtime;
in_buf->inside_st_nlink = buf.st_nlink;
in_buf->inside_st_ino = buf.st_ino;
in_buf->inside_st_dev = buf.st_dev;
in_buf->inside_st_uid = buf.st_uid;
in_buf->inside_st_gid = buf.st_gid;
in_buf->inside_st_rdev = buf.st_rdev;
in_buf->inside_st_atime = buf.st_atime;
in_buf->inside_st_ctime = buf.st_ctime;
}
return ret;
}
int wrap_outside_lstat(const char *fname, struct inside_stat *in_buf) {
struct stat buf;
int ret = lstat(fname, &buf);
if (!ret) {
in_buf->inside_st_size = buf.st_size;
in_buf->inside_st_mode = buf.st_mode;
in_buf->inside_st_mtime = buf.st_mtime;
in_buf->inside_st_nlink = buf.st_nlink;
in_buf->inside_st_ino = buf.st_ino;
in_buf->inside_st_dev = buf.st_dev;
in_buf->inside_st_uid = buf.st_uid;
in_buf->inside_st_gid = buf.st_gid;
in_buf->inside_st_rdev = buf.st_rdev;
in_buf->inside_st_atime = buf.st_atime;
in_buf->inside_st_ctime = buf.st_ctime;
}
return ret;
}
int wrap_outside_fstat(int fd, struct inside_stat *in_buf) {
struct stat buf;
int ret = fstat(fd, &buf);
if (!ret) {
in_buf->inside_st_size = buf.st_size;
in_buf->inside_st_mode = buf.st_mode;
in_buf->inside_st_mtime = buf.st_mtime;
in_buf->inside_st_nlink = buf.st_nlink;
in_buf->inside_st_ino = buf.st_ino;
in_buf->inside_st_dev = buf.st_dev;
in_buf->inside_st_uid = buf.st_uid;
in_buf->inside_st_gid = buf.st_gid;
in_buf->inside_st_rdev = buf.st_rdev;
in_buf->inside_st_atime = buf.st_atime;
in_buf->inside_st_ctime = buf.st_ctime;
}
return ret;
}
void wrap_outside_free(void *buf) {
free(buf);
}
int wrap_outside_vasprintf(char **strp, const char *fmt, va_list ap) {
return vasprintf(strp, fmt, ap);
}
int wrap_outside_vsnprintf(char *str, size_t size, const char *format,
va_list ap) {
return vsnprintf(str, size, format, ap);
}
clock_t wrap_clock(void) {
return clock();
}
clock_t wrap_outside_times(struct tms *buf) {
return times(buf);
}
int wrap_outside_unlink(const char *path) {
return unlink(path);
}
int wrap_outside_chmod(const char *path, mode_t mode) {
return chmod(path, mode);
}
int wrap_outside_isatty(int fd) {
return isatty(fd);
}
int wrap_outside_select(int n, fd_set *rfds, fd_set *wfds, fd_set *xfds,
struct timeval *tv) {
return select(n, rfds, wfds, xfds, tv);
}
int wrap_outside_dup(int oldfd) {
return dup(oldfd);
}
int wrap_outside_dup2(int oldfd, int newfd) {
return dup2(oldfd, newfd);
}
void wrap_outside_clearerr(int fi) {
clearerr(files[fi]);
}
int wrap_outside_rename(const char *oldpath, const char *newpath) {
return rename(oldpath, newpath);
}
int wrap_outside_chdir(const char *path) {
return chdir(path);
}
int wrap_vsprintf(char *str, const char *format, va_list ap) {
return vsprintf(str, format, ap);
}
double wrap_atan2(double y, double x) {
return atan2(y, x);
}
double wrap_modf(double x, double *iptr) {
return modf(x, iptr);
}
unsigned int wrap_sleep(unsigned int secs) {
return sleep(secs);
}
pid_t wrap_outside_wait(int *status) {
return wait(status);
}
mode_t wrap_umask(mode_t mask) {
return umask(mask);
}
int wrap_outside_write(int fd, const void *buf, size_t count) {
return write(fd, buf, count);
}
int wrap_outside_truncate(const char *path, off_t length) {
return truncate(path, length);
}
int wrap_outside_ftruncate(int fd, off_t length) {
return ftruncate(fd, length);
}
int wrap_outside_mkdir(const char *pathname, mode_t mode) {
return mkdir(pathname, mode);
}
int wrap_outside_rmdir(const char *path) {
return rmdir(path);
}
void wrap_outside_setbuf(int fi, char *buf) {
setbuf(files[fi], buf);
}
double wrap_tan(double theta) {
return tan(theta);
}
pid_t wrap_outside_fork(void) {
return fork();
}
int wrap_outside_execvp(const char *file, char *const argv[]) {
return execvp(file, argv);
}
int wrap_outside_execv(const char *file, char *const argv[]) {
return execvp(file, argv);
}
int wrap_outside_pipe(int fds[2]) {
return pipe(fds);
}
struct tm *wrap_gmtime(const time_t *timep) {
return gmtime(timep);
}
void wrap_fail_check(void) {
printf("Program has performed an illegal operation "
"and will be terminated\n");
exit(1);
}
static int errno_to_vx32(int host_errno) {
switch (host_errno) {
case EINVAL: return 1;
case ENOMEM: return 2;
case EDOM: return 3;
case ERANGE: return 4;
case EBADF: return 5;
case EPROTO: return 6;
case EIO: return 7;
case ENOENT: return 8;
case EPIPE: return 9;
case EAGAIN: return 10;
default: return 1;
}
}
void wrap_vx32__exit(int status) {
exit_value = status;
longjmp(exit_buf, 1);
}
int wrap_vx32_read(int fd, void *buf, size_t count) {
int rc = read(fd, buf, count);
if (rc == -1) {
return -errno_to_vx32(errno);
} else {
return rc;
}
}
void *wrap_vx32_sbrk(long inc) {
return wrap_sbrk(inc);
}
int wrap_vx32_write(int fd, const void *buf, size_t count) {
int rc = write(fd, buf, count);
if (rc == -1) {
return -errno_to_vx32(errno);
} else {
return rc;
}
}
void init_wrappers() {
files[0] = stdin;
files[1] = stdout;
files[2] = stderr;
}
int (*stubs[])() = {
#include "wrappers.h"
0};
void install_stubs() {
int addr = CODE_START + 2*CHUNK_SIZE;
int (**p)();
for (p = stubs; *p; p++) {
/* jmp is PC relative; specificially, relative to the PC after
the end of the jump instruction. */
*((unsigned char *)addr) = 0xe9;
int offset = (int)*p - (addr + 5);
*((int *)(addr + 1)) = offset;
addr += STUB_SIZE;
}
}
#ifdef VERIFY
void fail_verify(const char *msg, int offset) {
printf("Verification failed: %s at offset 0x%08x\n", msg, offset);
exit(1);
}
void fail_verify_int(const char *msg, int arg, int offset) {
printf("Verification failed: %s (0x%x) at offset 0x%08x\n",
msg, arg, offset);
exit(1);
}
void fail_verify_insn(const char *msg, x86_insn_t *insn, int arg, int offset)
{
char buf[100];
x86_format_insn(insn, buf, sizeof(buf), att_syntax);
printf("Verification failed: %s (0x%x) at offset 0x%08x\n",
msg, arg, offset);
printf("Bad instruction %s\n", buf);
exit(1);
}
#define REGISTER_EAX 1
#define REGISTER_ECX 2
#define REGISTER_EDX 3
#define REGISTER_EBX 4
#define REGISTER_ESP 5
#define REGISTER_EBP 6
#define REGISTER_ESI 7
#define REGISTER_EDI 8
enum eff_addr_type {
EA_EBX_EXACT, /* (%ebx) */
EA_ESP_EXACT, /* (%esp), sometimes disassembled as (%esp,1) to
match encoding */
EA_EBP_OFFSET, /* x(%ebp), where |x| < 2**16 */
EA_ESP_OFFSET, /* x(%esp), where |x| < 2**8 */
EA_DIRECT, /* 0x...., but in ModRM encoding */
EA_OTHER, /* anything else */
};
enum eff_addr_type classify_ea(x86_ea_t ea) {
if (ea.base.id == REGISTER_EBX &&
ea.index.id == 0 && ea.disp_size == 0)
return EA_EBX_EXACT;
else if (ea.base.id == REGISTER_ESP &&
ea.index.id == 0 && ea.disp_size == 0)
return EA_ESP_EXACT;
else if (ea.base.id == REGISTER_EBP &&
ea.index.id == 0 &&
(ea.disp_size <= 2 ||
(ea.disp_sign && ea.disp > -32768) ||
((unsigned)ea.disp < 32768)))
return EA_EBP_OFFSET;
else if (ea.base.id == REGISTER_ESP &&
ea.index.id == 0 &&
ea.disp_size <= 1)
return EA_ESP_OFFSET;
else if (ea.base.id == 0 && ea.index.id == 0 &&
ea.disp_size == 4)
return EA_DIRECT;
else
return EA_OTHER;
}
#define BUMP_ESP 0x1
#define CHANGE_EBP 0x2
#define CHANGE_ESP 0x4
#define EBP_DATA_SAFE 0x8
#define EBX_CODE_SAFE 0x10
#define EBX_DATA_SAFE 0x20
#define IJUMP 0x40
#define IREAD 0x80
#define IWRITE 0x100
#define JUMP 0x200
#define STACK_TOP_SAFE 0x400
#define USE_ESP 0x800
#define USE_EBP 0x1000
#define DATA_SAFETY EBX_DATA_SAFE
#define CODE_SAFETY EBX_CODE_SAFE
#define STACK_TOP_SAFETY STACK_TOP_SAFE
#define EBP_SAFETY EBP_DATA_SAFE
#define ESP_SAFETY USE_ESP
void verify(int code_len) {
int offset = 0;
int next_aligned = offset;
int safety = 0, unsafety = 0;
int bump_count = 0;
x86_init(opt_none, 0, 0);
while (offset < code_len) {
x86_insn_t insn;
int len, i, my_explicit_count;
int arg_types;
int flags;
x86_oplist_t args[10], *arg;
if (offset == next_aligned) {
next_aligned += CHUNK_SIZE;
} else if (offset > next_aligned) {
fail_verify("Bad chunk alignment", offset);
}
len = x86_disasm((unsigned char *)CODE_START, code_len,
(unsigned long)CODE_START, offset, &insn);
if (len <= 0) {
fail_verify("Illegal instruction", offset);
}
switch (insn.group) {
case insn_controlflow:
case insn_arithmetic:
case insn_logic:
case insn_stack:
case insn_comparison:
case insn_move:
case insn_bit_manip:
case insn_fpu:
case insn_interrupt:
case insn_other:
break;
default:
fail_verify_int("Illegal insn group", insn.group, offset);
}
switch (insn.type) {
/* insn_controlflow */
case insn_jmp:
case insn_jcc:
case insn_call:
case insn_return:
/* insn_arithmetic */
case insn_add:
case insn_sub:
case insn_mul:
case insn_div:
case insn_inc:
case insn_dec:
case insn_shl:
case insn_shr:
case insn_rol:
case insn_ror:
/* insn_logic */
case insn_and:
case insn_or:
case insn_xor:
case insn_not:
case insn_neg:
/* insn_stack */
case insn_push:
case insn_pop:
case insn_pushflags:
case insn_popflags:
case insn_leave:
/* insn_comparison */
case insn_test:
case insn_cmp:
/* insn_move */
case insn_mov:
case insn_movcc:
/* insn_bit_manip */
case insn_bittest:
/* insn_fpu */
case 0xa000: /* XXX all FPU */
/* insn_interrupt */
case insn_debug: /* int3 */
/* insn_other */
case insn_nop:
case insn_szconv:
break;
default:
{
char buf[100];
x86_format_mnemonic(&insn, buf, sizeof(buf), att_syntax);
printf("Unknown instruction %s\n", buf);
fail_verify_int("Illegal insn type", insn.type, offset);
}
}
/* We do all of our calculation based on the explicit operands
to make the parallel with verify.pl clearer, even though in
a clean-slate implementation, it might be better to treat
the implicit operands uniformly. */
assert(insn.explicit_count <= 3);
i = 0;
for (arg = insn.operands; arg; arg = arg->next) {
if (!(arg->op.flags & op_implied)) {
args[i] = *arg;
args[i].next = &args[i+1];
i++;
}
}
if (i > 0)
args[i-1].next = 0;
/* The insn.explicit_count field seems to be broken in
libdisas 0.23, giving 2 rather than 1 for "imul %ecx". */
/* assert(i == insn.explicit_count); */
my_explicit_count = i;
#define UNARY(op1) ((op1) << 4 | 1)
#define BINARY(op1, op2) ((op1) << 8 | (op2) << 4 | 2)
#define TERNARY(op1, op2, op3) ((op1) << 12 | (op2) << 8 | (op1) << 4 | 3)
if (my_explicit_count == 0) {
arg_types = 0;
} else if (my_explicit_count == 1) {
arg_types = UNARY(args[0].op.type);
} else if (my_explicit_count == 2) {
arg_types = BINARY(args[0].op.type, args[1].op.type);
} else if (my_explicit_count == 3) {
arg_types = TERNARY(args[0].op.type, args[1].op.type,
args[2].op.type);
} else {
printf("%d operands!\n", my_explicit_count);
arg_types = -1;
}
switch (arg_types) {
case 0:
switch (insn.type) {
case insn_nop:
flags = 0;
break;
case insn_return:
if (safety & STACK_TOP_SAFETY)
flags = JUMP|USE_ESP|CHANGE_ESP;
else
flags = JUMP|IJUMP|USE_ESP|CHANGE_ESP;
break;
case insn_leave:
if (!(unsafety & CHANGE_EBP))
flags = USE_ESP|CHANGE_EBP;
else
flags = USE_ESP|CHANGE_EBP|USE_ESP;
break;
case insn_pushflags: case insn_popflags:
flags = USE_ESP|CHANGE_ESP;
break;
case insn_szconv: /* e.g., cdq */
flags = 0;
break;
case 0xa000: /* e.g., fldz */
flags = 0;
break;
case insn_debug: /* int3 */
flags = 0;
break;
case insn_mov: /* e.g., sahf */
flags = 0;
break;
default:
fail_verify_insn("Unknown no-argument insn", &insn,
insn.type, offset);
assert(0); flags = -1;
}
break;
case UNARY(op_relative_near):
case UNARY(op_relative_far):
{
unsigned long target;
target = CODE_START + offset + insn.size;
if (args[0].op.type == op_relative_near) {
target += args[0].op.data.relative_near;
} else if (args[0].op.type == op_relative_far) {
target += args[0].op.data.relative_far;
} else {
assert(0);
}
switch (insn.type) {
case insn_jcc:
case insn_jmp: