-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathtables-biarch.c
395 lines (314 loc) · 9.03 KB
/
tables-biarch.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
388
389
390
391
392
393
394
395
/*
* Functions for handling the system call tables.
* These functions are only used on architectures that have both 32 and 64 bit syscalls.
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "arch.h"
#include "syscall.h"
#include "params.h"
#include "random.h"
#include "shm.h"
#include "tables.h"
#define NOTFOUND (unsigned int)-1
const struct syscalltable *syscalls_32bit;
const struct syscalltable *syscalls_64bit;
unsigned int max_nr_32bit_syscalls;
unsigned int max_nr_64bit_syscalls;
bool use_32bit = FALSE;
bool use_64bit = FALSE;
void activate_syscall32(unsigned int calln)
{
activate_syscall_in_table(calln, &shm->nr_active_32bit_syscalls, syscalls_32bit, shm->active_syscalls32);
}
void activate_syscall64(unsigned int calln)
{
activate_syscall_in_table(calln, &shm->nr_active_64bit_syscalls, syscalls_64bit, shm->active_syscalls64);
}
void deactivate_syscall32(unsigned int calln)
{
deactivate_syscall_in_table(calln, &shm->nr_active_32bit_syscalls, syscalls_32bit, shm->active_syscalls32);
}
void deactivate_syscall64(unsigned int calln)
{
deactivate_syscall_in_table(calln, &shm->nr_active_64bit_syscalls, syscalls_64bit, shm->active_syscalls64);
}
int validate_syscall_table_64(void)
{
if (shm->nr_active_64bit_syscalls == 0)
use_64bit = FALSE;
else
use_64bit = TRUE;
return use_64bit;
}
int validate_syscall_table_32(void)
{
if (shm->nr_active_32bit_syscalls == 0)
use_32bit = FALSE;
else
use_32bit = TRUE;
return use_32bit;
}
static void toggle_syscall_biarch_n(int calln, const struct syscalltable *table,
bool onlyflag, bool doflag, bool state,
void (*activate)(unsigned int),
int arch_bits, const char *arg_name)
{
if (calln != -1) {
struct syscallentry *entry = table[calln].entry;
validate_specific_syscall(table, calln);
if ((state == TRUE) && onlyflag && doflag) {
entry->flags |= ACTIVE;
(*activate)(calln);
} else {
entry->flags |= TO_BE_DEACTIVATED;
}
}
if ((arch_bits != 0) && (calln != -1))
output(0, "Marking %d-bit syscall %s (%d) as to be %sabled.\n",
arch_bits, arg_name, calln,
state ? "en" : "dis");
}
void toggle_syscall_biarch(const char *arg, bool state)
{
int specific_syscall32 = 0;
int specific_syscall64 = 0;
char *arg_name = NULL;
bool only_32bit = TRUE;
bool only_64bit = TRUE;
check_user_specified_arch(arg, &arg_name, &only_64bit, &only_32bit);
/* If we found a 64bit syscall, validate it. */
specific_syscall64 = search_syscall_table(syscalls_64bit, max_nr_64bit_syscalls, arg_name);
toggle_syscall_biarch_n(specific_syscall64, syscalls_64bit, only_64bit, do_64_arch, state, &activate_syscall64, 0, arg_name);
/* Search for and validate 32bit */
specific_syscall32 = search_syscall_table(syscalls_32bit, max_nr_32bit_syscalls, arg_name);
toggle_syscall_biarch_n(specific_syscall32, syscalls_32bit, only_32bit, do_32_arch, state, &activate_syscall32, 0, arg_name);
if ((!only_32bit) && (!only_64bit)) {
outputerr("No idea what architecture for syscall (%s) is.\n", arg);
exit(EXIT_FAILURE);
}
if ((specific_syscall64 == -1) && (specific_syscall32 == -1)) {
outputerr("No idea what syscall (%s) is.\n", arg);
exit(EXIT_FAILURE);
}
if ((specific_syscall64 != -1) && (specific_syscall32 != -1)) {
output(0, "Marking syscall %s (64bit:%d 32bit:%d) as to be %sabled.\n",
arg_name, specific_syscall64, specific_syscall32,
state ? "en" : "dis");
goto out;
}
if (specific_syscall64 != -1) {
output(0, "Marking 64-bit syscall %s (%d) as to be %sabled.\n",
arg, specific_syscall64,
state ? "en" : "dis");
goto out;
}
if (specific_syscall32 != -1) {
output(0, "Marking 32-bit syscall %s (%d) as to be %sabled.\n",
arg, specific_syscall32,
state ? "en" : "dis");
}
out:
clear_check_user_specified_arch(arg, &arg_name);
return;
}
void enable_random_syscalls_biarch(void)
{
unsigned int call32 = NOTFOUND, call64 = NOTFOUND;
retry:
//Search for 64 bit version
if (do_64_arch) {
struct syscallentry *entry = NULL;
call64 = rnd() % max_nr_64bit_syscalls;
if (validate_specific_syscall_silent(syscalls_64bit, call64) == FALSE)
goto retry;
entry = syscalls_64bit[call64].entry;
if (entry->flags & TO_BE_DEACTIVATED) {
call64 = NOTFOUND;
goto try32bit;
}
if (entry->active_number != 0) {
call64 = NOTFOUND;
goto try32bit;
}
// If we got so far, then activate it.
toggle_syscall_biarch_n(call64, syscalls_64bit, TRUE, do_64_arch, TRUE,
&activate_syscall64, 64, entry->name);
}
try32bit:
//Search for 32 bit version
if (do_32_arch) {
struct syscallentry *entry = NULL;
/* If we enabled a 64bit syscall above, enable its 32-bit counter-part too */
if (do_64_arch) {
if (call64 == NOTFOUND) {
goto just32;
} else {
call32 = search_syscall_table(syscalls_32bit, max_nr_32bit_syscalls, syscalls_64bit[call64].entry->name);
if (call32 == NOTFOUND)
return; // syscall is 64-bit only.
}
} else {
just32:
call32 = rnd() % max_nr_32bit_syscalls;
}
if (validate_specific_syscall_silent(syscalls_32bit, call32) == FALSE)
return;
entry = syscalls_32bit[call32].entry;
if ((entry->flags & TO_BE_DEACTIVATED) || (entry->active_number != 0))
return;
//If we got so far, then active it.
toggle_syscall_biarch_n(call32, syscalls_32bit, TRUE, do_32_arch, TRUE,
&activate_syscall32, 32, entry->name);
}
}
int setup_syscall_group_biarch(unsigned int group)
{
unsigned int i;
for_each_32bit_syscall(i) {
struct syscallentry *entry = syscalls_32bit[i].entry;
if (entry == NULL)
continue;
if (entry->group == group)
toggle_syscall(entry->name, TRUE);
}
if (shm->nr_active_32bit_syscalls == 0)
outputstd("No 32-bit syscalls in group\n");
else
outputstd("Found %d 32-bit syscalls in group\n", shm->nr_active_32bit_syscalls);
/* now the 64 bit table*/
for_each_64bit_syscall(i) {
struct syscallentry *entry = syscalls_64bit[i].entry;
if (entry == NULL)
continue;
if (entry->group == group)
toggle_syscall(entry->name, TRUE);
}
if (shm->nr_active_64bit_syscalls == 0) {
outputstd("No 64-bit syscalls in group\n");
return FALSE;
} else {
outputstd("Found %d 64-bit syscalls in group\n", shm->nr_active_64bit_syscalls);
}
return TRUE;
}
void mark_all_syscalls_active_biarch(void)
{
unsigned int i;
if (do_32_arch) {
for_each_32bit_syscall(i) {
struct syscallentry *entry = syscalls_32bit[i].entry;
if (entry == NULL)
continue;
entry->flags |= ACTIVE;
activate_syscall32(i);
}
}
if (do_64_arch) {
for_each_64bit_syscall(i) {
struct syscallentry *entry = syscalls_64bit[i].entry;
if (entry == NULL)
continue;
entry->flags |= ACTIVE;
activate_syscall64(i);
}
}
}
void init_syscalls_biarch(void)
{
struct syscallentry *entry;
unsigned int i;
for_each_64bit_syscall(i) {
entry = syscalls_64bit[i].entry;
if (entry == NULL)
continue;
if (entry->flags & ACTIVE)
if (entry->init)
entry->init();
}
for_each_32bit_syscall(i) {
entry = syscalls_32bit[i].entry;
if (entry == NULL)
continue;
if (entry->flags & ACTIVE)
if (entry->init)
entry->init();
}
}
void deactivate_disabled_syscalls_biarch(void)
{
struct syscallentry *entry;
unsigned int i;
for_each_64bit_syscall(i) {
entry = syscalls_64bit[i].entry;
if (entry == NULL)
continue;
if (entry->flags & TO_BE_DEACTIVATED) {
entry->flags &= ~(ACTIVE|TO_BE_DEACTIVATED);
deactivate_syscall64(i);
output(0, "Marked 64-bit syscall %s (%d) as deactivated.\n",
entry->name, entry->number);
}
}
for_each_32bit_syscall(i) {
entry = syscalls_32bit[i].entry;
if (entry == NULL)
continue;
if (entry->flags & TO_BE_DEACTIVATED) {
entry->flags &= ~(ACTIVE|TO_BE_DEACTIVATED);
deactivate_syscall32(i);
output(0, "Marked 32-bit syscall %s (%d) as deactivated.\n",
entry->name, entry->number);
}
}
}
void dump_syscall_tables_biarch(void)
{
struct syscallentry *entry;
unsigned int i;
outputstd("syscalls: %d [32-bit]\n", max_nr_32bit_syscalls);
outputstd("syscalls: %d [64-bit]\n", max_nr_64bit_syscalls);
for_each_32bit_syscall(i) {
entry = syscalls_32bit[i].entry;
if (entry == NULL)
continue;
outputstd("entrypoint %d %s : [32-bit] ",
entry->number, entry->name);
show_state(entry->flags & ACTIVE);
if (entry->flags & AVOID_SYSCALL)
outputstd(" AVOID");
outputstd("\n");
}
for_each_64bit_syscall(i) {
entry = syscalls_64bit[i].entry;
if (entry == NULL)
continue;
outputstd("entrypoint %d %s : [64-bit] ",
entry->number, entry->name);
show_state(entry->flags & ACTIVE);
if (entry->flags & AVOID_SYSCALL)
outputstd(" AVOID");
outputstd("\n");
}
}
void display_enabled_syscalls_biarch(void)
{
struct syscallentry *entry;
unsigned int i;
for_each_64bit_syscall(i) {
entry = syscalls_64bit[i].entry;
if (entry == NULL)
continue;
if (entry->flags & ACTIVE)
output(0, "64-bit syscall %d:%s enabled.\n", i, entry->name);
}
for_each_32bit_syscall(i) {
entry = syscalls_32bit[i].entry;
if (entry == NULL)
continue;
if (entry->flags & ACTIVE)
output(0, "32-bit syscall %d:%s enabled.\n", i, entry->name);
}
}