-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_wrap.c
298 lines (236 loc) · 9.04 KB
/
io_wrap.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
#ifdef IO_SYSCALL_WRAPPING
#include <stdint.h>
#include "io_wrap.h"
#include <alloca.h>
#include "uaccess.h"
#include "syscall.h"
#include "string.h"
#include "edge_syscall.h"
/* Syscalls iozone uses in -i0 mode
*** Fake these
* uname
*** odd
rt_sigaction
rt_sigprocmask
*** hard
brk
mmap
*/
#define MAX_STRACE_PRINT 20
uintptr_t io_syscall_sync(){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
edge_syscall->syscall_num = SYS_sync;
size_t totalsize = (sizeof(struct edge_syscall));
uintptr_t ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
print_strace("[runtime] proxied sync\r\n");
return ret;
}
uintptr_t io_syscall_ftruncate(int fd, off_t offset){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_ftruncate* args = (sargs_SYS_ftruncate*)edge_syscall->data;
edge_syscall->syscall_num = SYS_ftruncate;
args->fd = fd;
args->offset = offset;
size_t totalsize = (sizeof(struct edge_syscall)+
sizeof(sargs_SYS_ftruncate));
uintptr_t ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
print_strace("[runtime] proxied ftruncate (%i) = %li\r\n", fd, ret);
return ret;
}
uintptr_t io_syscall_fsync(int fd){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_fsync* args = (sargs_SYS_fsync*)edge_syscall->data;
edge_syscall->syscall_num = SYS_fsync;
args->fd = fd;
size_t totalsize = (sizeof(struct edge_syscall)+
sizeof(sargs_SYS_fsync));
uintptr_t ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
print_strace("[runtime] proxied fsync (%i) = %li\r\n", fd, ret);
return ret;
}
uintptr_t io_syscall_lseek(int fd, off_t offset, int whence){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_lseek* args = (sargs_SYS_lseek*)edge_syscall->data;
edge_syscall->syscall_num = SYS_lseek;
args->fd = fd;
args->offset = offset;
args->whence = whence;
size_t totalsize = (sizeof(struct edge_syscall)+
sizeof(sargs_SYS_lseek));
uintptr_t ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
print_strace("[runtime] proxied lseek (on fd:%i to %li from %i) = %li\r\n",
fd, offset, whence, ret);
return ret;
}
uintptr_t io_syscall_close(int fd){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_close* args = (sargs_SYS_close*)edge_syscall->data;
edge_syscall->syscall_num = SYS_close;
args->fd = fd;
size_t totalsize = (sizeof(struct edge_syscall)+
sizeof(sargs_SYS_close));
uintptr_t ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
print_strace("[runtime] proxied close (%i) = %li\r\n", fd, ret);
return ret;
}
uintptr_t io_syscall_read(int fd, void* buf, size_t len){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_read* args = (sargs_SYS_read*)edge_syscall->data;
uintptr_t ret = -1;
edge_syscall->syscall_num = SYS_read;
args->fd =fd;
args->len = len;
// Sanity check that the read buffer will fit in the shared memory
if(edge_call_check_ptr_valid((uintptr_t)args->buf, len) != 0){
goto done;
}
size_t totalsize = (sizeof(struct edge_syscall) +
sizeof(sargs_SYS_read) +
len);
ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
if((int)ret < 0){
goto done;
}
// Previously checked that this is staying in untrusted buffer range
copy_to_user(buf, args->buf, ret > len? len: ret);
done:
print_strace("[runtime] proxied read (size: %lu) = %li\r\n",len, ret);
return ret;
}
uintptr_t io_syscall_write(int fd, void* buf, size_t len){
/* print_strace("[write] len :%lu\r\n", len); */
/* if(len > 0){ */
/* size_t stracelen = len > MAX_STRACE_PRINT? MAX_STRACE_PRINT:len; */
/* char* lbuf[MAX_STRACE_PRINT+1]; */
/* memset(lbuf, 0, sizeof(lbuf)); */
/* copy_from_user(lbuf, (void*)buf, stracelen); */
/* print_strace("[write] \"%s\"\r\n", (char*)lbuf); */
/* } */
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_write* args = (sargs_SYS_write*)edge_syscall->data;
uintptr_t ret = -1;
edge_syscall->syscall_num = SYS_write;
args->fd =fd;
args->len = len;
// Sanity check that the write buffer will fit in the shared memory
if(edge_call_check_ptr_valid((uintptr_t)args->buf, len) != 0){
goto done;
}
copy_from_user(args->buf, buf, len);
size_t totalsize = (sizeof(struct edge_syscall) +
sizeof(sargs_SYS_write) +
len);
ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
done:
print_strace("[runtime] proxied write (size: %lu) = %li\r\n",len, ret);
return ret;
}
uintptr_t io_syscall_openat(int dirfd, char* path,
int flags, mode_t mode){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_openat* args = (sargs_SYS_openat*)edge_syscall->data;
edge_syscall->syscall_num = SYS_openat;
args->dirfd = dirfd;
args->flags = flags;
args->mode = mode;
uintptr_t ret = -1;
size_t pathlen;
ALLOW_USER_ACCESS(pathlen = _strlen(path)+1);
// Sanity check that the buffer will fit in the shared memory
if(edge_call_check_ptr_valid((uintptr_t)args->path, pathlen) != 0){
goto done;
}
copy_from_user(args->path, path, pathlen);
size_t totalsize = (sizeof(struct edge_syscall) +
sizeof(sargs_SYS_openat) +
pathlen);
ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
done:
// TODO path print here isn't necessarily correct or even copied!
print_strace("[runtime] proxied openat(path: %.*s) = %li\r\n",
pathlen>MAX_STRACE_PRINT?MAX_STRACE_PRINT:pathlen,args->path, ret);
return ret;
}
uintptr_t io_syscall_unlinkat(int dirfd, char* path,
int flags){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_unlinkat* args = (sargs_SYS_unlinkat*)edge_syscall->data;
uintptr_t ret = -1;
edge_syscall->syscall_num = SYS_unlinkat;
args->dirfd = dirfd;
args->flags = flags;
size_t pathlen;
ALLOW_USER_ACCESS(pathlen = _strlen(path)+1);
// Sanity check that the buffer will fit in the shared memory
if(edge_call_check_ptr_valid((uintptr_t)args->path, pathlen) != 0){
goto done;
}
copy_from_user(args->path, path, pathlen);
size_t totalsize = (sizeof(struct edge_syscall) +
sizeof(sargs_SYS_unlinkat) +
pathlen);
ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
done:
// TODO path print here isn't necessarily correct or even copied!
print_strace("[runtime] proxied unlinkat(path: %.*s) = %li\r\n",
pathlen>MAX_STRACE_PRINT?MAX_STRACE_PRINT:pathlen,args->path, ret);
return ret;
}
uintptr_t io_syscall_writev(int fd, const struct iovec *iov, int iovcnt){
int i=0;
uintptr_t ret = 0;
size_t total = 0;
print_strace("[runtime] Simulating writev (cnt %i) with write calls\r\n",iovcnt);
for(i=0; i<iovcnt && ret >= 0;i++){
struct iovec iov_local;
copy_from_user(&iov_local, &(iov[i]), sizeof(struct iovec));
ret = io_syscall_write(fd,iov_local.iov_base, iov_local.iov_len);
total += ret;
}
ret = total;
print_strace("[runtime] Simulated writev = %li\r\n",ret);
return ret;
}
uintptr_t io_syscall_readv(int fd, const struct iovec *iov, int iovcnt){
int i=0;
uintptr_t ret = 0;
size_t total = 0;
print_strace("[runtime] Simulating readv (cnt %i) with read calls\r\n",iovcnt);
for(i=0; i<iovcnt && ret >= 0;i++){
struct iovec iov_local;
copy_from_user(&iov_local, &(iov[i]), sizeof(struct iovec));
ret = io_syscall_read(fd, iov_local.iov_base, iov_local.iov_len);
total += ret;
}
ret = total;
print_strace("[runtime] Simulated readv = %li\r\n",ret);
return ret;
}
uintptr_t io_syscall_fstatat(int dirfd, char *pathname, struct stat *statbuf,
int flags){
struct edge_syscall* edge_syscall = (struct edge_syscall*)edge_call_data_ptr();
sargs_SYS_fstatat* args = (sargs_SYS_fstatat*)edge_syscall->data;
uintptr_t ret = -1;
edge_syscall->syscall_num = SYS_fstatat;
args->dirfd = dirfd;
args->flags = flags;
size_t pathlen;
ALLOW_USER_ACCESS(pathlen = _strlen(pathname)+1);
// Sanity check that the buffer will fit in the shared memory
if(edge_call_check_ptr_valid((uintptr_t)args->pathname, pathlen) != 0){
goto done;
}
copy_from_user(args->pathname, pathname, pathlen);
size_t totalsize = (sizeof(struct edge_syscall) +
sizeof(sargs_SYS_fstatat) +
pathlen);
ret = dispatch_edgecall_syscall(edge_syscall, totalsize);
if(ret == 0){
copy_to_user(statbuf, &args->stats, sizeof(struct stat));
}
done:
print_strace("[runtime] proxied fstatat (path %.*s) = %li\r\n",
pathlen>MAX_STRACE_PRINT?MAX_STRACE_PRINT:pathlen,args->pathname, ret);
return ret;
}
#endif /* IO_SYSCALL_WRAPPING */