-
Notifications
You must be signed in to change notification settings - Fork 241
/
syscall.c
288 lines (236 loc) · 6.73 KB
/
syscall.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
/*
* Functions for actually doing the system calls.
*/
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "arch.h"
#include "child.h"
#include "ftrace.h"
#include "params.h"
#include "pids.h"
#include "random.h"
#include "results.h"
#include "sanitise.h"
#include "shm.h"
#include "syscall.h"
#include "tables.h"
#include "taint.h"
#include "trinity.h"
#include "uid.h"
#include "utils.h"
#ifdef ARCH_IS_BIARCH
/*
* This routine does 32 bit syscalls on 64 bit kernel.
* 32-on-32 will just use syscall() directly from do_syscall() because do32bit flag is biarch only.
*/
static long syscall32(unsigned int call,
unsigned long a1, unsigned long a2, unsigned long a3,
unsigned long a4, unsigned long a5, unsigned long a6)
{
long __res = 0;
#if defined(DO_32_SYSCALL)
/* If we have CONFIG_IA32_EMULATION unset, we will segfault.
* Detect this case, and force 64-bit only.
*/
if (shm->syscalls32_succeeded == FALSE) {
if (shm->syscalls32_attempted >= (max_children * 2)) {
unsigned int i;
lock(&shm->syscalltable_lock);
/* check another thread didn't already do this. */
if (shm->nr_active_32bit_syscalls == 0)
goto already_done;
output(0, "Tried %d 32-bit syscalls unsuccessfully. Disabling all 32-bit syscalls.\n",
shm->syscalls32_attempted);
for (i = 0; i < max_nr_32bit_syscalls; i++) {
struct syscallentry *entry = syscalls[i].entry;
if (entry->active_number != 0)
deactivate_syscall(i, TRUE);
}
already_done:
unlock(&shm->syscalltable_lock);
}
shm->syscalls32_attempted++;
}
DO_32_SYSCALL
if ((unsigned long)(__res) >= (unsigned long)(-133)) {
errno = -(__res);
__res = -1;
}
shm->syscalls32_succeeded = TRUE;
#else
#error Implement 32-on-64 syscall macro for this architecture.
#endif
return __res;
}
#else
#define syscall32(a,b,c,d,e,f,g) 0
#endif /* ARCH_IS_BIARCH */
static void __do_syscall(struct syscallrecord *rec, enum syscallstate state)
{
unsigned long ret = 0;
errno = 0;
shm->stats.op_count++;
if (dry_run == FALSE) {
int nr, call;
bool needalarm;
nr = rec->nr;
/* Some architectures (IA64/MIPS) start their Linux syscalls
* At non-zero, and have other ABIs below.
*/
call = nr + SYSCALL_OFFSET;
needalarm = syscalls[nr].entry->flags & NEED_ALARM;
if (needalarm)
(void)alarm(1);
lock(&rec->lock);
rec->state = state;
unlock(&rec->lock);
if (rec->do32bit == FALSE) {
ret = syscall(call, rec->a1, rec->a2, rec->a3, rec->a4, rec->a5, rec->a6);
} else {
ret = syscall32(call, rec->a1, rec->a2, rec->a3, rec->a4, rec->a5, rec->a6);
}
/* If we became tainted, get out as fast as we can. */
if (is_tainted() == TRUE) {
stop_ftrace();
panic(EXIT_KERNEL_TAINTED);
_exit(EXIT_FAILURE);
}
if (needalarm)
(void)alarm(0);
}
lock(&rec->lock);
rec->errno_post = errno;
rec->retval = ret;
rec->state = AFTER;
unlock(&rec->lock);
}
/* This is a special case for things like execve, which would replace our
* child process with something unknown to us. We use a 'throwaway' process
* to do the execve in, and let it run for a max of a second before we kill it
*/
static void do_extrafork(struct syscallrecord *rec)
{
pid_t pid = 0;
pid_t extrapid;
extrapid = fork();
if (extrapid == 0) {
/* grand-child */
char childname[]="trinity-subchild";
prctl(PR_SET_NAME, (unsigned long) &childname);
__do_syscall(rec, GOING_AWAY);
/* if this was for eg. an successful execve, we should never get here.
* if it failed though... */
_exit(EXIT_SUCCESS);
}
/* misc failure. */
if (extrapid == -1) {
//debugf("Couldn't fork grandchild: %s\n", strerror(errno));
return;
}
/* small pause to let grandchild do some work. */
if (pid_alive(extrapid) == TRUE)
usleep(100);
/* We take the rec lock here even though we don't obviously use it.
* The reason, is that the grandchild is using it. */
lock(&rec->lock);
while (pid == 0) {
int childstatus;
pid = waitpid(extrapid, &childstatus, WUNTRACED | WCONTINUED | WNOHANG);
if (pid_alive(extrapid) == TRUE)
kill(extrapid, SIGKILL);
usleep(1000);
}
unlock(&rec->lock);
}
void do_syscall(struct syscallrecord *rec)
{
struct syscallentry *entry;
unsigned int call;
call = rec->nr;
entry = syscalls[call].entry;
if (entry->flags & EXTRA_FORK)
do_extrafork(rec);
else
/* common-case, do the syscall in this child process. */
__do_syscall(rec, BEFORE);
/* timestamp again for when we returned */
clock_gettime(CLOCK_MONOTONIC, &rec->tp);
}
/*
* If the syscall doesn't exist don't bother calling it next time.
* Some syscalls return ENOSYS depending on their arguments, we mark
* those as IGNORE_ENOSYS and keep calling them.
*/
static void deactivate_enosys(struct syscallrecord *rec, struct syscallentry *entry, unsigned int call)
{
/* some syscalls return ENOSYS instead of EINVAL etc (futex for eg) */
if (entry->flags & IGNORE_ENOSYS)
return;
lock(&shm->syscalltable_lock);
/* check another thread didn't already do this. */
if (entry->active_number == 0)
goto already_done;
output(1, "%s (%d%s) returned ENOSYS, marking as inactive.\n",
entry->name,
call + SYSCALL_OFFSET,
rec->do32bit == TRUE ? ":[32BIT]" : "");
deactivate_syscall(call, rec->do32bit);
already_done:
unlock(&shm->syscalltable_lock);
}
static void generic_post(const enum argtype type, unsigned long reg)
{
void *ptr = (void *) reg;
if ((type == ARG_PATHNAME) && (ptr != NULL))
free(ptr);
}
void handle_syscall_ret(struct syscallrecord *rec)
{
struct syscallentry *entry;
unsigned int call;
call = rec->nr;
entry = syscalls[call].entry;
if (rec->retval == -1UL) {
int err = rec->errno_post;
/* only check syscalls that completed. */
//FIXME: how else would we get here?
if (rec->state == AFTER) {
if (err == ENOSYS)
deactivate_enosys(rec, entry, call);
entry->failures++;
if (err < NR_ERRNOS) {
entry->errnos[err]++;
} else {
// "These should never be seen by user programs."
// But trinity isn't a 'normal' user program, we're doing
// stuff that libc hides from apps.
if (err < 512 || err > 530)
printf("errno out of range after doing %s: %d:%s\n",
entry->name,
err, strerror(err));
}
shm->stats.failures++;
}
} else {
handle_success(rec); // Believe me folks, you'll never get bored with winning
entry->successes++;
shm->stats.successes++;
}
entry->attempted++;
generic_post(entry->arg1type, rec->a1);
generic_post(entry->arg2type, rec->a2);
generic_post(entry->arg3type, rec->a3);
generic_post(entry->arg4type, rec->a4);
generic_post(entry->arg5type, rec->a5);
generic_post(entry->arg6type, rec->a6);
if (entry->post)
entry->post(rec);
check_uid();
generic_free_arg(rec);
}