-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbinder.c
477 lines (426 loc) · 12.6 KB
/
rbinder.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include "uthash.h"
#define SCREAD(number) (number == SYS_read)
#define SCSENDTO(number) (number == SYS_sendto)
#define SCRECVFROM(number) (number == SYS_recvfrom)
#define SCENTRY(code) (code == -ENOSYS)
#ifdef __x86_64__
#define WORD_LENGTH 8
#else
#define WORD_LENGTH 4
#endif
#define REG_SC_NUMBER (WORD_LENGTH * ORIG_RAX)
#define REG_SC_RETCODE (WORD_LENGTH * RAX)
#define REG_SC_FRSTARG (WORD_LENGTH * RDI)
#define REG_SC_SCNDARG (WORD_LENGTH * RSI)
#define REG_SC_THRDARG (WORD_LENGTH * RDX)
#define ARG_SCRW_BUFF 1
#define ARG_SCRW_BUFFSIZE 2
const int long_size = sizeof(long);
const char *fine_headers[] = {
"x-ot-span-context",
"x-request-id",
"x-b3-traceid",
"x-b3-spanid",
"x-b3-parentspanid",
"x-b3-sampled",
"x-b3-flags"
};
/*
* ptrace helper functions.
*/
void peekdata(pid_t child, long addr, char *str, int len) {
char *laddr;
int i, j;
union u {
long val;
char chars[long_size];
}data;
i = 0;
j = len / long_size;
laddr = str;
while(i < j) {
data.val = ptrace(PTRACE_PEEKDATA, child, addr + i * 8, NULL);
memcpy(laddr, data.chars, long_size);
++i;
laddr += long_size;
}
j = len % long_size;
if(j != 0) {
data.val = ptrace(PTRACE_PEEKDATA, child, addr + i * 8, NULL);
memcpy(laddr, data.chars, j);
}
str[len] = '\0';
}
void pokedata(pid_t child, long addr, char *str, int len) {
char *laddr;
int i, j;
union u {
long val;
char chars[long_size];
}data;
i = 0;
j = len / long_size;
laddr = str;
while(i < j) {
memcpy(data.chars, laddr, long_size);
ptrace(PTRACE_POKEDATA, child, addr + i * 8, data.val);
++i;
laddr += long_size;
}
j = len % long_size;
if(j != 0) {
memcpy(data.chars, laddr, j);
ptrace(PTRACE_POKEDATA, child, addr + i * 8, data.val);
}
ptrace(PTRACE_POKEUSER, child, 8 * RDX, len);
}
long peekuser(pid_t cid, unsigned int reg) {
long ret = ptrace(PTRACE_PEEKUSER, cid, reg, NULL);
if(errno < 0) {
perror("ptrace(PTRACE_PEEKUSER)");
exit(1);
}
return ret;
}
void peek_syscall_thrargs(pid_t cid, long *params) {
params[0] = peekuser(cid, REG_SC_FRSTARG);
params[1] = peekuser(cid, REG_SC_SCNDARG);
params[2] = peekuser(cid, REG_SC_THRDARG);
}
/*
* Helper functions for handling requests.
*/
void extract_headers(char *str, char *headers) {
int chidx, hdidx, matchidx, i;
chidx = 0;
matchidx = 0;
const char *cheader = NULL;
char elected[1024] = {'\0'};
int electedidx = 0;
char cchar = '\0';
// Try matching each tracing header.
for(hdidx = 0; hdidx < 7; hdidx++) {
cheader = fine_headers[hdidx];
matchidx = 0;
// Check each char.
for(chidx = 0; chidx < strlen(str); chidx++) {
cchar = str[chidx];
// Get out before reaching HTTP data section.
if(chidx > 0 && cchar == '\r' && str[chidx-1] == '\n' && str[chidx+1] == '\n') {
continue;
}
// Don't care about this char.
if(cchar == '\r') {
continue;
}
// Line break: restart matching info.
if(cchar == '\n') {
matchidx = 0;
continue;
}
// Matching already failed for current line.
if(matchidx == -1) {
continue;
}
// Still didn't match entire header.
if(matchidx < strlen(cheader)) {
if(tolower(cchar) == cheader[matchidx]) {
++matchidx;
} else {
matchidx = -1;
}
}
// Matched entire header.
else {
// Copy header key.
if(matchidx == strlen(cheader)) {
for(i = 0; i < matchidx; i++) {
headers[electedidx] = cheader[i];
++electedidx;
}
}
// Copy header value (including ": ").
headers[electedidx] = cchar;
++electedidx;
++matchidx;
if(str[chidx+1] == '\r') {
headers[electedidx] = '\r';
headers[electedidx+1] = '\n';
electedidx = electedidx + 2;
}
}
}
}
// Fill headers with \0.
for(i = electedidx; i < 1024; i++) {
headers[i] = '\0';
}
}
void inject_headers(char *str, char *headers, char *newstr, int newstrsize) {
int i, j;
int stridx = 0;
int injected = 0;
for(i = 0; i < newstrsize; i++) {
newstr[i] = str[stridx];
if(str[stridx] == '\n' && str[stridx+1] == '\r' && injected == 0) {
for(j = 0; j < strlen(headers); j++) {
newstr[i+1+j] = headers[j];
}
i += strlen(headers);
injected = 1;
}
++stridx;
}
newstr[newstrsize] = '\0';
}
int is_http_request(char *str) {
char *httpmeths[9] = {
"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"
};
int i;
for(i = 0; i < 9; i++) {
if(strncmp(str, httpmeths[i], strlen(httpmeths[i])) == 0) {
return 1;
}
}
return 0;
}
/*
* tracee_t struct types & functions.
*/
struct tracee_t {
pid_t id;
char headers[1024];
UT_hash_handle hh;
};
struct tracee_t *tracees = NULL;
void add_tracee(struct tracee_t *s) {
s->headers[0] = '\0';
HASH_ADD_INT(tracees, id, s);
}
struct tracee_t *find_tracee(int tracee_id) {
struct tracee_t *t;
HASH_FIND_INT(tracees, &tracee_id, t);
return t;
}
void rmtracee(struct tracee_t *tracee) {
HASH_DEL(tracees, tracee);
free(tracee);
}
/*
* rbinder main function. Call with cmd line args:
*
* $ ./rbinder /usr/bin/python server.py
*/
int main(int argc, char **argv) {
pid_t child, cid;
int status, fd, i;
void *buf;
size_t len;
long syscall_number, syscall_return;
long params[3];
char *str;
struct tracee_t *tracee;
unsigned int open_socks[1024];
for(i = 0; i < 1024; i++) {
open_socks[i] = 0;
}
child = fork();
// Start server within traced thread (just like a gdb inferior).
if(child == 0) {
ptrace(PTRACE_TRACEME, NULL, NULL, NULL);
/* Filters for the syscalls we want to trace */
struct sock_filter filter[] = {
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_read, 0, 1),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRACE),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_close, 0, 1),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRACE),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_accept, 0, 1),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRACE),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_sendto, 0, 1),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRACE),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SYS_clone, 0, 1),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_TRACE),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
};
struct sock_fprog prog = {
.filter = filter,
.len = (unsigned short) (sizeof(filter)/sizeof(filter[0])),
};
/* To avoid the need for CAP_SYS_ADMIN */
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
perror("prctl(PR_SET_NO_NEW_PRIVS)");
return 1;
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1) {
perror("prctl(PR_SET_SECCOMP)");
return 1;
}
kill(getpid(), SIGSTOP);
execv(argv[1], argv + 1);
}
// Watch server syscalls for extracting incoming request tracing headers and
// injecting them into outgoing requests performed while request is being
// serviced.
else {
// Setup ptrace for tracing further children threads.
cid = waitpid(-1, &status, __WALL);
if(ptrace(PTRACE_SETOPTIONS, cid, 0, PTRACE_O_TRACEEXEC|PTRACE_O_EXITKILL|\
PTRACE_O_TRACEVFORK|PTRACE_O_TRACECLONE|PTRACE_O_TRACEFORK|\
PTRACE_O_TRACESECCOMP) < 0) {
perror("ptrace(PTRACE_SETOPTIONS)");
exit(1);
}
if(ptrace(PTRACE_CONT, cid, NULL, WSTOPSIG(status)) < 0) {
perror("ptrace(PTRACE_CONT)");
exit(1);
}
while(1) {
// Wait for tracees' activity.
cid = waitpid(-1, &status, __WALL);
if(WIFEXITED(status)) {
tracee = find_tracee(cid);
if(tracee) {
rmtracee(tracee);
}
continue;
}
syscall_number = peekuser(cid, REG_SC_NUMBER);
syscall_return = peekuser(cid, REG_SC_RETCODE);
if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_SECCOMP << 8))) {
//// Skip if
//// - none tracees and syscall does not trigger tracee creation
//if((!SCREAD(syscall_number) && HASH_COUNT(tracees) == 0)) {}
//// Extract headers from incoming request.
if(SCREAD(syscall_number)) {
peek_syscall_thrargs(cid, params);
// Inject a new trap if this is a read on an open socket so we can
// examine syscall results.
if(open_socks[params[0]] == 1) {
if(ptrace(PTRACE_SYSCALL, cid, NULL, WSTOPSIG(status)) < 0) {
perror("ptrace(PTRACE_SYSCALL)");
exit(1);
}
continue;
}
} // SYS_read
else if(syscall_number == SYS_close) {
peek_syscall_thrargs(cid, params);
// Mark socket as not open if it was open and is being closed.
if(open_socks[params[0]] == 1) {
open_socks[params[0]] = 0;
}
} // SYS_close
else if(syscall_number == SYS_accept) {
if(ptrace(PTRACE_SYSCALL, cid, NULL, WSTOPSIG(status)) < 0) {
perror("ptrace(PTRACE_SYSCALL)");
exit(1);
}
continue;
} // SYS_accept
//// Inject headers into outgoing requests.
else if(SCSENDTO(syscall_number)) {
tracee = find_tracee(cid);
peek_syscall_thrargs(cid, params);
str = (char *)calloc(1, (params[ARG_SCRW_BUFFSIZE]+1) * sizeof(char));
peekdata(cid, params[ARG_SCRW_BUFF], str, params[ARG_SCRW_BUFFSIZE]);
// Check if HTTP request.
if(is_http_request(str)) {
if(!tracee) {
perror("Tracee not found when injecting headers into outgoing request");
exit(1);
}
int newstrsize = strlen(str) + strlen(tracee->headers);
char newstr[newstrsize];
inject_headers(str, tracee->headers, newstr, newstrsize);
pokedata(cid, params[1], newstr, newstrsize);
}
free(str);
} // SYS_sendto
else if(syscall_number == SYS_clone) {
if(ptrace(PTRACE_SYSCALL, cid, NULL, WSTOPSIG(status)) < 0) {
perror("ptrace(PTRACE_SYSCALL)");
exit(1);
}
continue;
} // SYS_clone
//} // PTRACE_EVENT_SECCOMP
} else {
if(SCREAD(syscall_number)) {
peek_syscall_thrargs(cid, params);
if(syscall_return != -38) {
str = (char *)calloc(1, (params[ARG_SCRW_BUFFSIZE]+1) * sizeof(char));
peekdata(cid, params[ARG_SCRW_BUFF], str, params[ARG_SCRW_BUFFSIZE]);
if(is_http_request(str)) {
tracee = malloc(sizeof(struct tracee_t));
tracee->id = cid;
add_tracee(tracee);
extract_headers(str, tracee->headers);
}
free(str);
if(ptrace(PTRACE_CONT, cid, NULL, 0) < 0) {
perror("ptrace(PTRACE_CONT)");
exit(1);
}
continue;
}
} // SYS_read
else if(syscall_number == SYS_accept) {
if(syscall_return != -38) {
if(syscall_return > 0) { // Note: 0 is a valid file descriptor.
open_socks[syscall_return] = 1;
}
if(ptrace(PTRACE_CONT, cid, NULL, 0) < 0) {
perror("ptrace(PTRACE_CONT)");
exit(1);
}
continue;
}
} // SYS_accept
else if(syscall_number == SYS_clone) {
if(syscall_return != -38) {
if(syscall_return > 0) {
tracee = find_tracee(cid);
if(tracee) {
struct tracee_t *cloned;
cloned = malloc(sizeof(struct tracee_t));
cloned->id = syscall_return;
add_tracee(cloned);
for(i = 0; i < 1024; i++) {
cloned->headers[i] = tracee->headers[i];
}
}
}
if(ptrace(PTRACE_CONT, cid, NULL, 0) < 0) {
perror("ptrace(PTRACE_CONT)");
exit(1);
}
continue;
} else {
if(ptrace(PTRACE_SYSCALL, cid, NULL, 0) < 0) {
perror("ptrace(PTRACE_SYSCALL)");
exit(1);
}
continue;
}
} // SYS_clone
}
if(ptrace(PTRACE_CONT, cid, NULL, WSTOPSIG(status)) < 0) {
perror("ptrace(PTRACE_CONT)");
exit(1);
}
}
}
return 0;
}