-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscalltrace.c
More file actions
326 lines (283 loc) · 7.94 KB
/
syscalltrace.c
File metadata and controls
326 lines (283 loc) · 7.94 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include "syscalltrace.h"
#define DEFAULT_FORMAT "%ld(?)"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*arr))
#define handle_error(msg) { perror(msg); exit(EXIT_FAILURE); }
#define __print(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
static _Bool is_struct(param_t param) {
if (param < STRUCT___AIO_SIGSET_PTR || param > STRUCT_VM86_STRUCT_PTR) {
return 0;
}
return 1;
}
static long safe_ptrace(enum __ptrace_request request, pid_t pid,
void *addr, void *data) {
long ret;
if ((ret = ptrace(request, pid, addr, data)) == -1) {
handle_error("ptrace");
}
return ret;
}
static void *read_addr(pid_t pid, uint64_t addr, size_t size) {
char *s = calloc(size + 1, sizeof(char));
size_t i = 0;
long ret;
while (i < size) {
ret = safe_ptrace(PTRACE_PEEKDATA, pid, (void *) (addr + i), NULL);
memcpy((s + i), &ret, i + sizeof(long) > size ? size - i
: sizeof(long));
i += sizeof(long);
}
s[size] = '\0';
return ((void *) s);
}
static char *read_str(pid_t pid, uint64_t addr) {
size_t size = sizeof(long);
char *buf = calloc(size, sizeof(char));
long ret;
for (size_t i = 0;; i++) {
size_t len = i * sizeof(long);
ret = safe_ptrace(PTRACE_PEEKDATA, pid, (void *) (addr + len), NULL);
if (len >= size) {
size *= 2;
buf = realloc(buf, size * sizeof(char));
if (!buf) {
exit(EXIT_FAILURE);
}
}
memcpy((buf + len), &ret, sizeof(long));
for (uint8_t j = 0; j < sizeof(long); j++) {
char c = *(((char *) &ret) + j);
if (!c) {
goto END;
}
}
}
END:
return buf;
}
static syscall_t *find_syscall(uint64_t nr) {
if (nr < ARRAY_SIZE(syscalls)) {
return (syscalls + nr);
}
return NULL;
}
static size_t get_size(uint64_t nr, uint64_t args[6], param_t param) {
// TODO: implement
switch (param) {
case INT:
return sizeof(int);
case __S32:
return sizeof(int32_t);
case LONG:
return sizeof(long);
case UINT:
case UNSIGNED_INT:
return sizeof(unsigned int);
case UNSIGNED_LONG:
return sizeof(unsigned long);
case __U32:
case U32:
return sizeof(uint32_t);
case __U64:
case U64:
return sizeof(uint64_t);
case CHAR_PTR:
return sizeof(char *);
case VOID_PTR:
return sizeof(void *);
case VOID_PTR_PTR:
return sizeof(void **);
default:
return 1;
}
}
static void print_str(char *s, size_t size) {
__print("\"");
for (char *i = s; *i; i++) {
if (size && (s + size == i)) {
break;
}
char c = *i;
_Bool escape = 1;
switch (c) {
case '\a':
c = 'a';
break;
case '\b':
c = 'b';
break;
case '\f':
c = 'f';
break;
case '\n':
c = 'n';
break;
case '\r':
c = 'r';
break;
case '\t':
c = 't';
break;
case '\v':
c = 'v';
break;
case 0x1A:
__print("\\0x1a");
goto INNER_LOOP_END;
case 0x1B:
__print("\\0x1b");
goto INNER_LOOP_END;
default:
escape = 0;
break;
}
if (escape) {
__print("\\");
}
__print("%c", c);
INNER_LOOP_END:
continue;
}
__print("\"");
}
static void parse_param(uint64_t nr, uint64_t args[6],
pid_t pid, param_t param, uint64_t val) {
size_t size;
char *s = NULL;
if (is_struct(param)) {
__print("{");
}
switch (param) {
case INT:
__print("%d", (int) val);
break;
case __S32:
__print("%" PRId32, (int32_t) val);
break;
case LONG:
__print("%ld", (long) val);
break;
case UINT:
case UNSIGNED_INT:
__print("%u", (unsigned int) val);
break;
case UNSIGNED_LONG:
__print("%lu", (unsigned long) val);
break;
case __U32:
case U32:
__print("%" PRIu32, (uint32_t) val);
break;
case __U64:
case U64:
__print("%" PRIu64, val);
break;
case CHAR_PTR:
size = (nr == SYS_write || nr == SYS_read) ? args[2] : 0;
if (!size) {
s = read_str(pid, val);
} else {
s = read_addr(pid, val, size);
}
print_str(s, size);
break;
case VOID_PTR:
case VOID_PTR_PTR:
if (!val) {
__print("NULL");
} else {
__print("%#" PRIxPTR, (uintptr_t) val);
}
break;
default:
__print("<unimpl>");
break;
}
if (is_struct(param)) {
__print("}");
}
free(s);
}
static void parse_syscall(pid_t pid, uint64_t nr, uint64_t args[6]) {
syscall_t *syscall = find_syscall(nr);
char *s;
if (!syscall) {
__print(DEFAULT_FORMAT, nr);
return;
}
__print("%s(", syscall->name);
for (uint8_t i = 0; i < syscall->n_params; i++) {
parse_param(nr, args, pid, syscall->params[i], args[i]);
if ((i + 1) != syscall->n_params) {
__print(", ");
}
}
__print(")");
}
int main(int argc, char **argv) {
if (argc < 2) {
__print("Usage: %s <cmd> [args...]\n", argv[0]);
return 1;
}
pid_t pid = fork();
if (!pid) {
safe_ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execvp(argv[1], (argv + 1));
perror("exec");
} else if (pid > 0) {
__print("pid: %d\n", pid);
int status;
struct __ptrace_syscall_info info;
if (waitpid(pid, &status, 0) == -1) {
handle_error("waitpid");
}
safe_ptrace(PTRACE_SETOPTIONS, pid, NULL,
(void *) PTRACE_O_TRACESYSGOOD);
safe_ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
do {
if (waitpid(pid, &status, 0) == -1) {
handle_error("waitpid");
}
if (WIFSTOPPED(status)) {
safe_ptrace(PTRACE_GET_SYSCALL_INFO, pid,
(void *) sizeof(info), &info);
switch (info.op) {
case PTRACE_SYSCALL_INFO_ENTRY:
parse_syscall(pid, info.entry.nr, info.entry.args);
break;
case PTRACE_SYSCALL_INFO_EXIT:
__print(" = %ld%s\n", info.exit.rval,
info.exit.is_error ? " [ERR]" : "");
break;
case PTRACE_SYSCALL_INFO_SECCOMP:
parse_syscall(pid, info.seccomp.nr, info.seccomp.args);
break;
default:
break;
}
safe_ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
__print("\n--- exited with ");
if (WIFEXITED(status)) {
__print("%d", WEXITSTATUS(status));
} else {
__print("?");
}
__print(" ---\n");
return 0;
} else {
perror("fork");
}
return EXIT_FAILURE;
}