This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
helper.c
93 lines (78 loc) · 1.88 KB
/
helper.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
#include <unistd.h>
#include <linux/types.h>
#include <errno.h>
#include "helper.h"
#include "companion.h"
#include "common.h"
#include "mm.h"
struct memory_op_msg {
__u32 addr;
__u32 len;
};
ssize_t peek_asciiz_request(const int fd, const char *start) {
char * ptr = start;
__u32 i = 0;
while (*ptr++)
i++;
xwrite(fd, &i, sizeof i);
return xwrite(fd, start, i);
}
ssize_t poke_memory_request(const int fd, const struct memory_op_msg * req) {
ssize_t ret;
size_t bytesread;
char *ptr = req->addr;
while (bytesread < req->len) {
ret = xread(fd, ptr, req->len - bytesread);
if (ret < 0) {
PERROR("poke_memory/read failed:");
}
ptr += ret;
bytesread += ret;
}
return bytesread;
}
ssize_t peek_memory_request(const int fd, const struct memory_op_msg * req) {
return xwrite(fd, req->addr, req->len);
}
int wait_for_orders(const int fd) {
struct memory_op_msg req;
int msgtype;
char buf[512];
ssize_t ret = -1;
char *addr;
while (1) {
fxread(fd, &msgtype, sizeof msgtype);
switch (msgtype) {
case PEEK_ASCIIZ:
fxread(fd, &addr, sizeof addr);
peek_asciiz_request(fd, addr);
break;
case PEEK_MEMORY:
ret = fxread(fd, &req, sizeof req);
peek_memory_request(fd, &req);
break;
case POKE_MEMORY:
ret = fxread(fd, &req, sizeof req);
poke_memory_request(fd, &req);
break;
case RETVAL:
fxread(fd, &ret, sizeof ret);
fxread(fd, &errno, sizeof errno);
return ret;
case NATIVE_EXIT:
fxread(fd, &ret, sizeof ret);
asm("mov $1, %%eax\n"
"mov %0, %%ebx\n"
"int $0x80\n"
: /* output */
: "m" (ret));
break;
case RAISE_TRAP:
asm("int3\n");
break;
default:
ERROR("Unknown message type %x\n", msgtype);
break;
}
}
}