forked from kernelslacker/trinity
/
trinity.c
269 lines (203 loc) · 5.99 KB
/
trinity.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
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "arch.h"
#include "trinity.h"
#include "files.h"
#include "log.h"
#include "maps.h"
#include "pids.h"
#include "params.h"
#include "random.h"
#include "signals.h"
#include "shm.h"
#include "tables.h"
#include "ioctls.h"
#include "protocols.h"
#include "config.h" // for VERSION
char *progname = NULL;
unsigned int page_size;
unsigned int num_online_cpus;
unsigned int max_children;
char *page_zeros;
char *page_0xff;
char *page_rand;
char *page_allocs;
static void init_buffers(void)
{
unsigned int i;
output(2, "shm is at %p\n", shm);
page_zeros = memalign(page_size, page_size * 2);
if (!page_zeros)
exit(EXIT_FAILURE);
memset(page_zeros, 0, page_size);
output(2, "page_zeros @ %p\n", page_zeros);
page_0xff = memalign(page_size, page_size * 2);
if (!page_0xff)
exit(EXIT_FAILURE);
memset(page_0xff, 0xff, page_size);
output(2, "page_0xff @ %p\n", page_0xff);
page_rand = memalign(page_size, page_size * 2);
if (!page_rand)
exit(EXIT_FAILURE);
memset(page_rand, 0x55, page_size); /* overwritten below */
output(2, "page_rand @ %p\n", page_rand);
page_allocs = memalign(page_size, page_size * 2);
if (!page_allocs)
exit(EXIT_FAILURE);
memset(page_allocs, 0xff, page_size);
output(2, "page_allocs @ %p\n", page_allocs);
for (i = 0; i < (page_size / sizeof(unsigned long *)); i++)
page_allocs[i] = (unsigned long) malloc(page_size);
setup_global_mappings();
// generate_random_page may end up using global_mappings, so has to be last.
generate_random_page(page_rand);
}
/* This is run *after* we've parsed params */
static int munge_tables(void)
{
unsigned int ret;
/* By default, all syscall entries will be disabled.
* If we didn't pass -c, -x or -r, mark all syscalls active.
*/
if ((do_specific_syscall == FALSE) && (do_exclude_syscall == FALSE) && (random_selection == FALSE) && (desired_group == GROUP_NONE))
mark_all_syscalls_active();
if (desired_group != GROUP_NONE) {
ret = setup_syscall_group(desired_group);
if (ret == FALSE)
return FALSE;
}
if (random_selection == TRUE)
enable_random_syscalls();
/* If we saw a '-x', set all syscalls to enabled, then selectively disable.
* Unless we've started enabling them already (with -r) (or if we specified a group -g)
*/
if (do_exclude_syscall == TRUE) {
if ((random_selection == FALSE) && (desired_group == GROUP_NONE))
mark_all_syscalls_active();
deactivate_disabled_syscalls();
}
/* if we passed -n, make sure there's no VM/VFS syscalls enabled. */
if (no_files == TRUE)
disable_non_net_syscalls();
sanity_check_tables();
count_syscalls_enabled();
if (verbose == TRUE)
display_enabled_syscalls();
if (validate_syscall_tables() == FALSE) {
outputstd("No syscalls were enabled!\n");
outputstd("Use 32bit:%d 64bit:%d\n", use_32bit, use_64bit);
return FALSE;
}
return TRUE;
}
/*
* just in case we're not using the test.sh harness, we
* change to the tmp dir if it exists.
*/
static void change_tmp_dir(void)
{
struct stat sb;
const char tmpdir[]="tmp/";
int ret;
/* Check if it exists, bail early if it doesn't */
ret = (lstat(tmpdir, &sb));
if (ret == -1)
return;
/* Just in case a previous run screwed the perms. */
ret = chmod(tmpdir, 0755);
if (ret == -1)
output(0, "Couldn't chmod %s to 0755.\n", tmpdir);
ret = chdir(tmpdir);
if (ret == -1)
output(0, "Couldn't change to %s\n", tmpdir);
}
int main(int argc, char* argv[])
{
int ret = EXIT_SUCCESS;
int childstatus;
unsigned int i;
outputstd("Trinity v" __stringify(VERSION) " Dave Jones <davej@redhat.com>\n");
progname = argv[0];
initpid = getpid();
page_size = getpagesize();
num_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
select_syscall_tables();
if (create_shm())
exit(EXIT_FAILURE);
parse_args(argc, argv);
outputstd("Done parsing arguments.\n");
if (kernel_taint_mask != (int)0xFFFFFFFF) {
outputstd("Custom kernel taint mask has been specified: 0x%08x (%d).\n", kernel_taint_mask, kernel_taint_mask);
}
if (user_specified_children != 0)
max_children = user_specified_children;
else
max_children = sysconf(_SC_NPROCESSORS_ONLN);
if (max_children > MAX_NR_CHILDREN) {
outputerr("Increase MAX_NR_CHILDREN!\n");
exit(EXIT_FAILURE);
}
setup_shm_postargs();
if (logging == TRUE)
open_logfiles();
if (munge_tables() == FALSE) {
ret = EXIT_FAILURE;
goto out;
}
if (show_syscall_list == TRUE) {
dump_syscall_tables();
goto out;
}
init_syscalls();
if (show_ioctl_list == TRUE) {
dump_ioctls();
goto out;
}
if (getuid() == 0) {
if (dangerous == TRUE) {
outputstd("DANGER: RUNNING AS ROOT.\n");
outputstd("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n");
outputstd("or similar which could potentially make this machine unbootable without a firmware reset.\n\n");
outputstd("ctrl-c now unless you really know what you are doing.\n");
for (i = 10; i > 0; i--) {
outputstd("Continuing in %d seconds.\r", i);
(void)fflush(stdout);
sleep(1);
}
} else {
outputstd("Don't run as root (or pass --dangerous if you know what you are doing).\n");
exit(EXIT_FAILURE);
}
}
if (do_specific_proto == TRUE)
find_specific_proto(specific_proto_optarg);
init_buffers();
parse_devices();
pids_init();
setup_main_signals();
kernel_taint_initial = check_tainted();
if (kernel_taint_initial != 0) {
output(0, "Kernel was tainted on startup. Will ignore flags that are already set.\n");
}
change_tmp_dir();
/* check if we ctrl'c or something went wrong during init. */
if (shm->exit_reason != STILL_RUNNING)
goto cleanup_fds;
init_watchdog();
do_main_loop();
/* Shutting down. */
waitpid(watchdog_pid, &childstatus, 0);
output(0, "\nRan %ld syscalls. Successes: %ld Failures: %ld\n",
shm->total_syscalls_done - 1, shm->successes, shm->failures);
ret = EXIT_SUCCESS;
cleanup_fds:
close_sockets();
destroy_global_mappings();
if (logging == TRUE)
close_logfiles();
out:
exit(ret);
}