-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker.c
252 lines (203 loc) · 5.64 KB
/
linker.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
/******************************************/
#include "splinter.h"
#define __USE_GNU
#define __USE_LARGEFILE64
#define __USE_XOPEN
#include <link-private.h>
/******************************************/
uint_t stats_memory_used = 0;
void * splinter_memory_alloc(uint_t size) {
void * ptr;
if (!size) return NULL;
if ((ptr = calloc(1, size)) != NULL) {
stats_memory_used += 1;
} else {
debug(DEBUG_ALL, "could not alloc %lu bytes", size);
}
return ptr;
}
void * splinter_memory_free(void *p) {
if (p) {
free(p);
stats_memory_used -= 1;
}
return NULL;
}
int splinter_debug_level = 99;
int splinter_test_mode = 0;
static char socket_path[1024];
static uint_t socket_handle = -1;
static void __io_close() {
DEBUG();
if (socket_handle >= 0) {
close(socket_handle);
socket_handle = -1;
}
}
static void __io_cleanup() {
struct stat filestat;
DEBUG();
__io_close();
if (socket_path[0]) {
if (!stat(socket_path, &filestat)) {
if (S_ISSOCK(filestat.st_mode)) {
debug(DEBUG_ALL, "socket [%s] exists, deleting", socket_path);
unlink(socket_path);
}
}
bzero(socket_path, sizeof(socket_path));
}
}
static char * __io_init() {
int i, j;
char * path_pattern;
path_pattern = getenv("SPLINTER_SOCKET");
if (!path_pattern) path_pattern = DEFAULT_SOCKET;
for(i = j = 0; path_pattern[i];) {
if (path_pattern[i] == '%') {
switch(path_pattern[++i]) {
case 0:
break;
case 'p': // Drop PID
j += sprintf(socket_path + j, "%d", getpid());
i++;
break;
case 'l': // Drop LOGIN
j += sprintf(socket_path + j, "%s", getlogin());
i++;
break;
default:
socket_path[j++] = path_pattern[i++];
break;
}
} else {
socket_path[j++] = path_pattern[i++];
}
}
socket_path[j] = 0;
return socket_path;
}
static int __io_open(char * path) {
int addr_len, fd;
struct sockaddr_un addr;
struct stat filestat;
DEBUG();
if (!stat(path, &filestat)) {
if (S_ISSOCK(filestat.st_mode)) {
DEBUG("socket [%s] exists, deleting", path);
unlink(path);
}
}
DEBUG("opening socket [%s]", path);
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) return -1;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, path);
addr_len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
if (bind(fd, (struct sockaddr *)&addr, addr_len) || listen(fd, 1)) {
close(fd);
return -1;
}
return fd;
}
int __io_loop(char * path) {
uint_t session_handle;
int len;
static char __in[IO_BUFF];
static char __out[IO_BUFF];
DEBUG();
socket_handle = __io_open(path);
if (socket_handle < 0) return -1;
DEBUG("socket handle = %lu", socket_handle);
while(1) {
session_handle = accept(socket_handle, NULL, 0);
if (session_handle < 0) break;
while(1) {
len = read(session_handle, __in, IO_BUFF - 1);
if (len <= 0) break;
len = splinter_handle_request(__in, len, __out, sizeof(__out));
if (len <= 0) break;
write(session_handle, __out, len);
}
close(session_handle);
}
close(socket_handle);
socket_handle = -1;
return 0;
}
static hook_p main_hook;
static uint_t elf_entry;
static struct link_map * main_handle = NULL;
static int child_pid = 0;
uint_t splinter_find_symbol(char * name) {
if (!name) return 0;
if (!main_handle) return 0;
return (uint_t)dlsym(main_handle, name);
}
char * splinter_find_variable(char * name) {
return NULL;
}
static pthread_t worker_thread;
void * worker(void * arg) {
DEBUG("pid = %d", getpid());
int err = __io_loop(socket_path);
DEBUG("err = %d", err);
while(1) sleep(1); // Just loop indifinietly
}
void do_fork() {
DEBUG("pid = %d", getpid());
__io_close();
__io_init();
ringbuf_reset();
pthread_create(&worker_thread, NULL, worker, NULL);
}
extern int do_clone(int clone_flags, void *parent_code, void *child_code, void *child_stack, void *dx);
void do_parent(int pid) {
DEBUG("pid = %d", getpid());
atexit(__io_cleanup);
pthread_atfork(NULL, NULL, do_fork);
__io_init();
// kill(child_pid = pid, SIGCONT);
worker(NULL);
}
static uint_t __sp;
static uint_t __dx;
static void split() {
int stack_size, args_size;
void * stack_data;
struct rlimit limits;
DEBUG("pid = %d", getpid());
main_hook->refcount = 0; // How bad is that??
hook_uninstall(main_hook->id);
main_hook = NULL;
splinter_debug_level = 99;
getrlimit(RLIMIT_STACK, &limits);
stack_size = limits.rlim_cur;
stack_data = malloc(stack_size);
uint_t * stack = (uint_t *)__sp;
for(stack++; *stack; stack++); // argc + args
for(stack++; *stack; stack++); // NULL + env vars
stack++; // terminal NULL
args_size = ((void *)stack) - ((void *)__sp);
memcpy(stack_data + stack_size - args_size, (void *)__sp, args_size);
do_clone(CLONE_VM | CLONE_STOPPED | CLONE_THREAD | CLONE_SIGHAND | SIGCHLD, do_parent,
(void *)elf_entry, stack_data + stack_size - args_size, (void *)__dx);
}
void _init() {
char rule[256];
if ((main_handle = dlopen("", RTLD_LAZY | RTLD_NOLOAD)) != NULL) {
if (strings_init(STRING_BUFF)
|| atoms_init(MAX_ATOMS)
|| symbols_init(MAX_SYMBOLS)
|| ringbuf_init(128)
|| hooks_init(4, NULL, NULL))
exit(-1);
splinter_stats_dump();
sleep(10);
sprintf(rule, "{exec [mem %p [reg 3]] [mem %p [reg 7]] (call %p)}", &__dx, &__sp, split);
elf_entry = (uint_t)main_handle->l_entry;
main_hook = hook_install(elf_entry, rule, NULL, NULL, NULL, 0);
main_hook->enabled = 1;
} else {
fprintf(stderr, "Cannot find ELF start code address, splinter gives up...\n");
}
}