-
Notifications
You must be signed in to change notification settings - Fork 60
/
sysc.c
387 lines (341 loc) · 9.87 KB
/
sysc.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/stat.h>
#include "drv.h"
#include "sysc.h"
extern int verbose;
/* internal syscall arg parsing state */
#define NSLICES 7
#define STKSZ 256
struct parseState {
struct sysRec *calls;
int ncalls;
struct slice slices[NSLICES];
u_int64_t sizeStk[STKSZ];
size_t nslices, bufpos, stkpos;
};
static int parseArg(struct slice *b, struct parseState *st, u_int64_t *x);
static int pushSize(struct parseState *st, u_int64_t sz)
{
if(st->stkpos >= STKSZ)
return -1;
size_t stkpos = st->stkpos++;
st->sizeStk[stkpos] = sz;
return 0;
}
static int popSize(struct parseState *st, u_int64_t *sz)
{
if(st->stkpos == 0)
return -1;
size_t stkpos = --st->stkpos;
*sz = st->sizeStk[stkpos];
return 0;
}
static void dumpContents(unsigned char *buf, size_t sz)
{
size_t i;
if(verbose > 1) {
printf("contents: ");
for(i = 0; i < sz; i++)
printf("%02x", buf[i]);
printf("\n");
}
}
static int parseArgNum(struct slice *b, struct parseState *st, u_int64_t *x)
{
if(getU64(b, x) == -1)
return -1;
if(verbose) printf("argNum %llx\n", (unsigned long long)*x);
return 0;
}
/* pass a buffer as an argument */
static int parseArgAlloc(struct slice *b, struct parseState *st, u_int64_t *x)
{
void *p;
u_int32_t sz;
if(getU32(b, &sz) == -1)
return -1;
p = malloc(sz); /* note: we ignore memory leaks - exit/doneWork are perfect GCs */
if(!p
|| pushSize(st, sz) == -1)
return -1;
memset(p, 0, sz);
*x = (u_int64_t)(u_long)p;
if(verbose) printf("argAlloc %llx - allocated %x bytes\n", (unsigned long long)*x, sz);
return 0;
}
/* pass a buffer as an argument */
static int parseArgBuf(struct slice *b, struct parseState *st, u_int64_t *x)
{
if(st->bufpos >= st->nslices)
return -1;
size_t pos = st->bufpos++;
struct slice *bslice = st->slices + pos;
size_t sz = sliceSize(bslice);
if(pushSize(st, sz) == -1)
return -1;
*x = (u_int64_t)(u_long)sliceBuf(bslice);
if(verbose) printf("argBuf %llx from %ld bytes\n", (unsigned long long)*x, sz);
dumpContents(sliceBuf(bslice), sz);
return 0;
}
/* pass a buffer length as an argument */
static int parseArgBuflen(struct slice *b, struct parseState *st, u_int64_t *x)
{
if(popSize(st, x) == -1)
return -1;
if(verbose) printf("argBuflen %llx\n", (unsigned long long)*x);
return 0;
}
/* make a file with buffer contents and set arg to fd open to the start of that file */
static int parseArgFile(struct slice *b, struct parseState *st, u_int64_t *x)
{
static int num = 0;
char namebuf[128];
int fd;
if(st->bufpos >= st->nslices)
return -1;
size_t pos = st->bufpos++;
struct slice *bslice = st->slices + pos;
snprintf(namebuf, sizeof namebuf - 1, "/tmp/file%d", num++);
fd = open(namebuf, O_RDWR | O_CREAT | O_TRUNC, 0777);
if(fd == -1
|| write(fd, sliceBuf(bslice), sliceSize(bslice)) == -1
|| lseek(fd, 0, SEEK_SET) == -1) {
perror(namebuf);
exit(1);
}
fchmod(fd, 0777); // just in case it previously existed with other mode
*x = fd;
if(verbose) printf("argFile %llx - %ld bytes from %s\n", (unsigned long long)*x, (u_long)sliceSize(bslice), namebuf);
dumpContents(sliceBuf(bslice), sliceSize(bslice));
return 0;
}
static int parseArgStdFile(struct slice *b, struct parseState *st, u_int64_t *x)
{
int fd;
unsigned short typ;
if(getU16(b, &typ) == -1)
return -1;
fd = getStdFile(typ);
if(fd == -1)
return -1;
*x = fd;
if(verbose) printf("argStdFile %llx - type %d\n", (unsigned long long)*x, typ);
return 0;
}
static int parseArgVec64(struct slice *b, struct parseState *st, u_int64_t *x)
{
u_int64_t *vec;
int i;
u_int8_t sz;
if(getU8(b, &sz) == -1)
return -1;
vec = malloc(sz * sizeof vec[0]); /* note: we ignore memory leaks - exit/doneWork are perfect GCs */
if(sz && !vec)
return -1;
if(verbose) printf("argVec64 %llx - size %d\n", (unsigned long long)(u_long)vec, sz);
for(i = 0; i < sz; i++) {
if(verbose) printf("vec %d: ", i);
if(parseArg(b, st, &vec[i]) == -1)
return -1;
}
if(pushSize(st, sz) == -1)
return -1;
*x = (u_int64_t)(u_long)vec;
return 0;
}
/* make a file with buffer contents and set arg to point to its filename */
static int parseArgFilename(struct slice *b, struct parseState *st, u_int64_t *x)
{
static int num = 0;
char namebuf[128];
int fd;
if(st->bufpos >= st->nslices)
return -1;
size_t pos = st->bufpos++;
struct slice *bslice = st->slices + pos;
snprintf(namebuf, sizeof namebuf - 1, "/tmp/file%d", num++);
fd = open(namebuf, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if(fd == -1
|| write(fd, sliceBuf(bslice), sliceSize(bslice)) == -1
|| close(fd) == -1) {
perror(namebuf);
exit(1);
}
*x = (u_int64_t)(u_long)strdup(namebuf); /* note: we ignore memory leaks - exit/doneWork are perfect GCs */
if(verbose) printf("argFilename %llx - %ld bytes from %s\n", (unsigned long long)*x, (u_long)sliceSize(bslice), namebuf);
dumpContents(sliceBuf(bslice), sliceSize(bslice));
return 0;
}
static int
mkChild(u_int64_t *retPid)
{
pid_t pid;
int i;
fflush(stdout);
pid = fork();
switch(pid) {
case -1: return -1;
case 0:
break;
default:
*retPid = pid;
return 0;
}
/* child process */
for(i = 0; i < 3; i++)
sleep(1);
exit(0);
}
/* use a pid related to our process as an arg */
static int parseArgPid(struct slice *b, struct parseState *st, u_int64_t *x)
{
unsigned char typ;
if(getU8(b, &typ) == -1)
return -1;
switch(typ) {
case 0: // my pid
*x = getpid();
break;
case 1: // parent pid
*x = getppid();
break;
case 2: // child pid
if(mkChild(x) == -1)
return -1;
break;
default:
return -1;
}
if(verbose) printf("argPid %llx - %d\n", (unsigned long long)*x, typ);
return 0;
}
/* Reference a previously defined argument as a new arg */
static int parseArgRef(struct slice *b, struct parseState *st, u_int64_t *x)
{
unsigned char ncall, narg;
if(getU8(b, &ncall) == -1
|| getU8(b, &narg) == -1
|| ncall >= st->ncalls
|| narg >= 6)
return -1;
*x = st->calls[ncall].args[narg];
if(verbose) printf("argRef %llx - %d %d\n", (unsigned long long)*x, ncall, narg);
return 0;
}
static int parseArgVec32(struct slice *b, struct parseState *st, u_int64_t *x)
{
u_int64_t elem;
u_int32_t *vec;
int i;
u_int8_t sz;
if(getU8(b, &sz) == -1)
return -1;
vec = malloc(sz * sizeof vec[0]); /* note: we ignore memory leaks - exit/doneWork are perfect GCs */
if(sz && !vec)
return -1;
if(verbose) printf("argVec32 %llx - size %d\n", (unsigned long long)(u_long)vec, sz);
for(i = 0; i < sz; i++) {
if(verbose) printf("vec %d: ", i);
if(parseArg(b, st, &elem) == -1)
return -1;
vec[i] = elem;
}
if(pushSize(st, sz) == -1)
return -1;
*x = (u_int64_t)(u_long)vec;
return 0;
}
static int parseArg(struct slice *b, struct parseState *st, u_int64_t *x)
{
unsigned char typ;
if(getU8(b, &typ) == -1)
return -1;
switch(typ) {
case 0: return parseArgNum(b, st, x);
case 1: return parseArgAlloc(b, st, x);
case 2: return parseArgBuf(b, st, x);
case 3: return parseArgBuflen(b, st, x);
case 4: return parseArgFile(b, st, x);
case 5: return parseArgStdFile(b, st, x);
case 7: return parseArgVec64(b, st, x);
case 8: return parseArgFilename(b, st, x);
case 9: return parseArgPid(b, st, x);
case 10: return parseArgRef(b, st, x);
case 11: return parseArgVec32(b, st, x);
default: return -1;
}
}
int parseSysRec(struct sysRec *calls, int ncalls, struct slice *b, struct sysRec *x)
{
struct parseState st;
int i;
/* chop input into several slices */
if(getDelimSlices(b, BUFDELIM, sizeof BUFDELIM-1, NSLICES, st.slices, &st.nslices) == -1
|| st.nslices < 1)
return -1;
b = &st.slices[0];
st.bufpos = 1;
st.stkpos = 0;
st.calls = calls;
st.ncalls = ncalls;
if(getU16(b, &x->nr) == -1)
return -1;
if(verbose) printf("call %d\n", x->nr);
for(i = 0; i < 6; i++) {
if(verbose) printf("arg %d: ", i);
if(parseArg(b, &st, &x->args[i]) == -1)
return -1;
}
return 0;
}
int parseSysRecArr(struct slice *b, int maxRecs, struct sysRec *x, int *nRecs)
{
struct slice slices[10];
size_t i, nslices;
if(maxRecs > 10)
maxRecs = 10;
if(getDelimSlices(b, CALLDELIM, sizeof CALLDELIM-1, maxRecs, slices, &nslices) == -1)
return -1;
for(i = 0; i < nslices; i++) {
if(parseSysRec(x, i, slices + i, x + i) == -1)
return -1;
}
*nRecs = nslices;
return 0;
}
void
showSysRec(struct sysRec *x)
{
printf("syscall %d (%lx, %lx, %lx, %lx, %lx, %lx)\n", x->nr, (u_long)x->args[0], (u_long)x->args[1], (u_long)x->args[2], (u_long)x->args[3], (u_long)x->args[4], (u_long)x->args[5]);
}
void
showSysRecArr(struct sysRec *x, int n)
{
int i;
for(i = 0; i < n; i++)
showSysRec(x + i);
}
unsigned long
doSysRec(struct sysRec *x)
{
/* XXX consider doing this in asm so we can use the real syscall entry instead of the syscall() function entry */
return syscall(x->nr, x->args[0], x->args[1], x->args[2], x->args[3], x->args[4], x->args[5]);
}
unsigned long
doSysRecArr(struct sysRec *x, int n)
{
unsigned long ret;
int i;
ret = 0;
for(i = 0; i < n; i++)
ret = doSysRec(x + i);
return ret;
}