forked from kernelslacker/trinity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyscall.c
283 lines (225 loc) · 6.37 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
/*
* Functions for actually doing the system calls.
*/
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "arch.h"
#include "child.h"
#include "pids.h"
#include "random.h"
#include "sanitise.h"
#include "shm.h"
#include "syscall.h"
#include "log.h"
#include "tables.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)
{
int nr, call;
unsigned long ret = 0;
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);
errno = 0;
shm_ro();
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);
shm_rw();
/* We returned! */
shm->stats.total_syscalls_done++;
lock(&rec->lock);
(void)gettimeofday(&rec->tv, NULL);
rec->op_nr++;
rec->errno_post = errno;
rec->retval = ret;
rec->state = AFTER;
unlock(&rec->lock);
if (IS_ERR(ret))
shm->stats.failures++;
else
shm->stats.successes++;
if (needalarm)
(void)alarm(0);
}
/* 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 seconds 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);
rec->state = GOING_AWAY;
__do_syscall(rec);
/* if this was for eg. an successful execve, we should never get here.
* if it failed though... */
shm->stats.failures++;
_exit(EXIT_SUCCESS);
}
/* child */
while (pid == 0) {
int childstatus;
pid = waitpid(extrapid, &childstatus, WUNTRACED | WCONTINUED | WNOHANG);
if (pid_alive(extrapid) == TRUE)
kill(extrapid, SIGKILL);
}
shm->stats.successes++;
}
void do_syscall(struct syscallrecord *rec)
{
struct syscallentry *entry;
unsigned int call;
call = rec->nr;
entry = syscalls[call].entry;
rec->state = BEFORE;
if (entry->flags & EXTRA_FORK)
do_extrafork(rec);
else
/* common-case, do the syscall in this child process. */
__do_syscall(rec);
}
static void check_retval_documented(struct syscallrecord *rec, struct syscallentry *entry)
{
struct errnos *errnos;
unsigned int i;
/* Just return silently if ENOSYS, we'll disable it immediately afterwards. */
if (rec->errno_post == ENOSYS)
return;
/* Only check syscalls we've documented so far. */
errnos = &entry->errnos;
if (errnos->num == 0)
return;
lock(&shm->syscalltable_lock);
/* Check against the list of known return values. */
for (i = 0; i < errnos->num; i++) {
if (rec->errno_post == errnos->values[i])
goto out;
}
/* if we get here, we have a return value we don't know.
* find space for it, and store it so we don't warn again */
if (errnos->values[i] == 0) {
errnos->values[i] = rec->errno_post;
errnos->num++;
//TODO: if this was the 32bit syscall, we should adjust the 64bit one too.
// and vice-versa.
//output(0, "%s%s\n", rec->prebuffer, rec->postbuffer);
output(0, "%s%s returned an undocumented return value (%d:%s)\n",
entry->name,
rec->do32bit == TRUE ? ":[32BIT]" : "",
rec->errno_post, strerror(rec->errno_post));
}
out:
unlock(&shm->syscalltable_lock);
}
/*
* 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)
{
if (rec->errno_post != ENOSYS)
return;
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);
}
void handle_syscall_ret(struct syscallrecord *rec)
{
struct syscallentry *entry;
struct syscallrecord *previous;
unsigned int call;
call = rec->nr;
entry = syscalls[call].entry;
if (rec->retval == -1UL) {
/* only check syscalls that completed. */
if (rec->state == AFTER) {
check_retval_documented(rec, entry);
deactivate_enosys(rec, entry, call);
}
}
if (entry->post)
entry->post(rec);
/* store info for debugging. */
previous = &this_child->previous;
memcpy(previous, rec, sizeof(struct syscallrecord));
previous->state = DONE;
check_uid();
generic_free_arg(rec);
}