-
Notifications
You must be signed in to change notification settings - Fork 42
/
tnt_syswrap.c
669 lines (581 loc) · 21.3 KB
/
tnt_syswrap.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*--------------------------------------------------------------------*/
/*--- Wrappers for tainting syscalls ---*/
/*--- tnt_syswrap.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Taintgrind, a Valgrind tool for
tracking marked/tainted data through memory.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "pub_tool_basics.h"
#include "pub_tool_vki.h"
#include "pub_tool_vkiscnums.h"
#include "pub_tool_hashtable.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_libcbase.h"
#include "pub_tool_libcassert.h"
#include "pub_tool_libcprint.h"
#include "pub_tool_libcproc.h"
#include "pub_tool_libcfile.h"
#include "pub_tool_machine.h"
#include "pub_tool_aspacemgr.h"
#include "pub_tool_threadstate.h"
#include "pub_tool_stacktrace.h" // for VG_(get_and_pp_StackTrace)
#include "pub_tool_debuginfo.h" // VG_(describe_IP), VG_(get_fnname)
#include "valgrind.h"
#include "tnt_include.h"
// macros
#define KRED "\e[31m"
#define KGRN "\e[32m"
#define KMAG "\e[35m"
#define KNRM "\e[0m"
static
void resolve_filename(UWord fd, HChar *path, Int max)
{
HChar src[FD_MAX_PATH];
Int len = 0;
// TODO: Cache resolved fds by also catching open()s and close()s
VG_(sprintf)(src, "/proc/%d/fd/%d", VG_(getpid)(), (int)fd);
len = VG_(readlink)(src, path, max);
// Just give emptiness on error.
if (len == -1) len = 0;
path[len] = '\0';
}
/* enforce an arbitrary maximum */
static Bool tainted_fds[FD_MAX];
void TNT_(setup_tainted_map)( void ) {
VG_(memset)(tainted_fds, False, sizeof(tainted_fds));
/* Taint stdin if specified */
if (TNT_(clo_taint_stdin))
tainted_fds[0] = True;
}
static UInt read_offset = 0;
void TNT_(syscall_lseek)(ThreadId tid, UWord* args, UInt nArgs,
SysRes res) {
// off_t lseek(int fd, off_t offset, int whence);
Int fd = args[0];
ULong offset = args[1];
UInt whence = args[2];
Bool verbose = False;
if (fd >= FD_MAX || tainted_fds[fd] == False)
return;
Int retval = sr_Res(res);
if ( verbose )
{
VG_(printf)("syscall _lseek %d %d ", (int)tid, fd);
VG_(printf)("offset: 0x%x whence: 0x%x ", (UInt)offset, whence);
VG_(printf)("retval: 0x%x read_offset: 0x%x\n", (UInt)retval, read_offset);
}
if( whence == 0/*SEEK_SET*/ )
read_offset = 0 + (UInt)offset;
else if( whence == 1/*SEEK_CUR*/ )
read_offset += (UInt)offset;
else if( whence == 2/*SEEK_END*/ )
read_offset = retval;
else {
VG_(printf)("whence %x\n", whence);
tl_assert(0);
}
}
void TNT_(syscall_llseek)(ThreadId tid, UWord* args, UInt nArgs,
SysRes res) {
// int _llseek(int fildes, ulong offset_high, ulong offset_low, loff_t *result,, uint whence);
Int fd = args[0];
ULong offset_high = args[1];
ULong offset_low = args[2];
UInt result = args[3];
UInt whence = args[4];
ULong offset;
Bool verbose = False;
if (fd >= FD_MAX || tainted_fds[fd] == False)
return;
Int retval = sr_Res(res);
if ( verbose )
{
VG_(printf)("syscall _llseek %d %d ", (int)tid, fd);
VG_(printf)("0x%x 0x%x 0x%x 0x%x\n", (UInt)offset_high, (UInt)offset_low, result, whence);
VG_(printf)("0x%x\n", (UInt)retval);
}
offset = (offset_high<<32) | offset_low;
if( whence == 0/*SEEK_SET*/ )
read_offset = 0 + (UInt)offset;
else if( whence == 1/*SEEK_CUR*/ )
read_offset += (UInt)offset;
else {//if( whence == 2/*SEEK_END*/ )
VG_(printf)("whence %x\n", whence);
tl_assert(0);
}
}
Bool TNT_(syscall_allowed_check)(ThreadId tid, int syscallno) {
if (IN_SANDBOX && IS_SYSCALL_ALLOWED(syscallno)) {
const HChar *fnname;
TNT_(get_fnname)(tid, &fnname);
VG_(printf)("*** Sandbox performed system call %s (%d) in method %s, but it is not allowed to. ***\n", syscallnames[syscallno], syscallno, fnname);
VG_(get_and_pp_StackTrace)(tid, STACK_TRACE_SIZE);
VG_(printf)("\n");
return False;
}
return True;
}
static
void read_common ( UInt taint_offset, Int taint_len,
UInt curr_offset, Int curr_len,
HChar *data ) {
UWord addr;
Int len;
if( TNT_(clo_taint_all) ){
addr = (UWord)data;
len = curr_len;
}else
//VG_(printf)("curr_offset 0x%x\n", curr_offset);
//VG_(printf)("curr_len 0x%x\n", curr_len);
//VG_(printf)("tnt_offset 0x%x\n", taint_offset);
//VG_(printf)("tnt_len 0x%x\n", taint_len);
/* Here we determine what bytes to taint
We have 4 variables -
taint_offset Starting file offset to taint
taint_len Number of bytes to taint
curr_offset Starting file offset currently read
curr_len Number of bytes currently read
We have to deal with 4 cases: (= refers to the region to be tainted)
Case 1:
taint_len
taint_offset |-----------------|
curr_len
curr_offset |---=================---|
Case 2:
taint_len
taint_offset |-----------------------|
curr_len
curr_offset |---====================|
Case 3:
taint_len
taint_offset |----------------------|
curr_len
curr_offset |====================---|
Case 4:
taint_len
taint_offset |-----------------------|
curr_len
curr_offset |====================|
*/
if( taint_offset >= curr_offset &&
taint_offset <= curr_offset + curr_len ){
if( (taint_offset + taint_len) <= (curr_offset + curr_len) ){
// Case 1
addr = (UWord)(data + taint_offset - curr_offset);
len = taint_len;
}else{
// Case 2
addr = (UWord)(data + taint_offset - curr_offset);
len = curr_len - taint_offset + curr_offset;
}
}else if( ( ( taint_offset + taint_len ) >= curr_offset ) &&
( ( taint_offset + taint_len ) <= (curr_offset + curr_len ) ) ){
// Case 3
addr = (UWord)data;
len = taint_len - curr_offset + taint_offset;
}else if( ( taint_offset <= curr_offset ) &&
( taint_offset + taint_len ) >= ( curr_offset + curr_len ) ){
// Case 4
addr = (UWord)data;
len = curr_len;
}else{
return;
}
if ( TNT_(clo_smt2) )
TNT_(make_mem_tainted_named)( addr, len, "read" );
else
TNT_(make_mem_tainted)( addr, len );
}
extern HChar *varname;
void TNT_(syscall_read)(ThreadId tid, UWord* args, UInt nArgs,
SysRes res) {
// ssize_t read(int fildes, void *buf, size_t nbyte);
Int fd = args[0];
HChar *data = (HChar *)args[1];
UInt curr_offset = read_offset;
Int curr_len = sr_Res(res);
UInt taint_offset = TNT_(clo_taint_start);
Int taint_len = TNT_(clo_taint_len);
Bool verbose = False;
TNT_(check_fd_access)(tid, fd, FD_READ);
if (curr_len == 0) return;
TNT_(make_mem_untainted)( (UWord)data, curr_len );
if (fd >= FD_MAX || tainted_fds[fd] == False)
return;
if(verbose){
//VG_(printf)("taint_offset: 0x%x\ttaint_len: 0x%x\n", taint_offset, taint_len);
//VG_(printf)("curr_offset : 0x%x\tcurr_len : 0x%x\n", curr_offset, curr_len);
VG_(printf)("syscall read %d %d ", (int)tid, fd);
#ifdef VGA_amd64
VG_(printf)("0x%x 0x%x 0x%llx 0x%x\n", curr_offset, (unsigned)curr_len, (ULong)data,
*(HChar *)data);
#else
VG_(printf)("0x%x 0x%x 0x%x 0x%x\n", curr_offset, (unsigned)curr_len, (UInt)data,
*(HChar *)data);
#endif
}
if ( !TNT_(clo_smt2) ) {
Int success = TNT_(describe_data)(data, varname, VARNAMESIZE);
if ( istty ) VG_(printf)("%s", KMAG);
if (TNT_(clo_compact))
VG_(printf)("0xFFFFFFFF ");
else
VG_(printf)("0xFFFFFFFF: _syscall_read ");
if ( istty ) VG_(printf)("%s", KNRM);
VG_(printf)("| Read:%d | ", curr_len);
if ( istty ) VG_(printf)("%s", KRED);
VG_(printf)("0x%x", curr_offset);
if ( istty ) VG_(printf)("%s", KNRM);
if (success) VG_(printf)( " | %s:%x\n", varname, data);
else VG_(printf)( " | %s\n", varname );
}
read_common ( taint_offset, taint_len, curr_offset, curr_len, data );
// Update file position
read_offset += curr_len;
// DEBUG
//tnt_read = 1;
}
void TNT_(syscall_pread)(ThreadId tid, UWord* args, UInt nArgs,
SysRes res) {
// ssize_t pread(int fildes, void *buf, size_t nbyte, size_t offset);
Int fd = args[0];
HChar *data = (HChar *)args[1];
UInt curr_offset = (Int)args[3];
Int curr_len = sr_Res(res);
UInt taint_offset = TNT_(clo_taint_start);
Int taint_len = TNT_(clo_taint_len);
Bool verbose = False;
if (curr_len == 0) return;
TNT_(make_mem_untainted)( (UWord)data, curr_len );
if (fd >= FD_MAX || tainted_fds[fd] == False)
return;
if(verbose){
//VG_(printf)("taint_offset: 0x%x\ttaint_len: 0x%x\n", taint_offset, taint_len);
//VG_(printf)("curr_offset : 0x%x\tcurr_len : 0x%x\n", curr_offset, curr_len);
VG_(printf)("syscall pread %d %d ", (int)tid, fd);
#ifdef VGA_amd64
VG_(printf)("0x%x 0x%x 0x%llx\n", curr_offset, (unsigned)curr_len, (ULong)data);
#else
VG_(printf)("0x%x 0x%x 0x%x\n", curr_offset, (unsigned)curr_len, (UInt)data);
#endif
}
if ( !TNT_(clo_smt2) ) {
Int success = TNT_(describe_data)(data, varname, VARNAMESIZE);
if ( istty ) VG_(printf)("%s", KMAG);
if (TNT_(clo_compact))
VG_(printf)("0xFFFFFFFF ");
else
VG_(printf)("0xFFFFFFFF: _syscall_pread ");
if ( istty ) VG_(printf)("%s", KNRM);
VG_(printf)("| Read:%d | ", curr_len);
if ( istty ) VG_(printf)("%s", KRED);
VG_(printf)("0x%x", curr_offset);
if ( istty ) VG_(printf)("%s", KNRM);
if (success) VG_(printf)( " | %s:%x\n", varname, data);
else VG_(printf)( " | %s\n", varname );
}
read_common ( taint_offset, taint_len, curr_offset, curr_len, data );
}
void TNT_(syscall_open)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// int open (const char *filename, int flags[, mode_t mode])
HChar fdpath[FD_MAX_PATH];
Int fd = sr_Res(res);
Bool verbose = False;
// check if we have already created a sandbox
if (have_created_sandbox && !IN_SANDBOX) {
#ifdef VGO_freebsd
VG_(resolve_filename)(fd, fdpath, FD_MAX_PATH-1);
#elif defined VGO_linux
resolve_filename(fd, fdpath, FD_MAX_PATH-1);
#elif defined VGO_darwin
resolve_filename(fd, fdpath, FD_MAX_PATH-1);
#else
#error OS unknown
#endif
const HChar *fnname;
TNT_(get_fnname)(tid, &fnname);
VG_(printf)("*** The file %s (fd: %d) was opened in method %s after a sandbox was created, hence it will not be accessible to the sandbox. It will need to be passed to the sandbox using sendmsg. ***\n", fdpath, fd, fnname);
VG_(get_and_pp_StackTrace)(tid, STACK_TRACE_SIZE);
VG_(printf)("\n");
}
// Nothing to do if no file tainting
if ( VG_(strlen)( TNT_(clo_file_filter)) == 0 && (fd != 0 || !TNT_(clo_taint_stdin)) )
return;
if (fd > -1 && fd < FD_MAX) {
#ifdef VGO_freebsd
VG_(resolve_filename)(fd, fdpath, FD_MAX_PATH-1);
#elif defined VGO_linux
resolve_filename(fd, fdpath, FD_MAX_PATH-1);
#elif defined VGO_darwin
resolve_filename(fd, fdpath, FD_MAX_PATH-1);
#else
#error OS unknown
#endif
if( TNT_(clo_taint_all) ){
// Skip /dev/pts/0
// Skip executables
if ( VG_(strncmp)(fdpath, "/dev/pts/0",
VG_(strlen)("/dev/pts/0")) == 0 ) {
//} else if ( VG_(strncmp)(fdpath + VG_(strlen)(fdpath) - 3, ".so", 3) == 0 ) {
//} else if ( VG_(strncmp)(fdpath + VG_(strlen)(fdpath) - 3, ".py", 3) == 0 ) {
//} else if ( VG_(strncmp)(fdpath + VG_(strlen)(fdpath) - 4, ".pyc", 4) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/usr/bin/", 9) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/usr/local/bin/", 15) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/usr/lib/", 9) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/usr/local/lib/", 15) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/bin/", 5) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/lib/", 5) == 0 ) {
} else if ( VG_(strncmp)(fdpath, "/etc/", 5) == 0 ) {
} else {
tainted_fds[fd] = True;
if ( verbose )
VG_(printf)("syscall open %d %s %lx %d\n", (int)tid, fdpath, args[1], fd);
//if ( verbose )
// VG_(printf)("%s\n", fdpath + VG_(strlen)(fdpath) - 3);
read_offset = 0;
}
} else if ( VG_(strncmp)(fdpath, TNT_(clo_file_filter),
VG_(strlen)( TNT_(clo_file_filter))) == 0 ) {
tainted_fds[fd] = True;
if ( verbose )
VG_(printf)("syscall open %d %s %lx %d\n", (int)tid, fdpath, args[1], fd);
read_offset = 0;
} else if ( TNT_(clo_file_filter)[0] == '*' &&
VG_(strncmp)( fdpath + VG_(strlen)(fdpath)
- VG_(strlen)( TNT_(clo_file_filter) ) + 1,
TNT_(clo_file_filter) + 1,
VG_(strlen)( TNT_(clo_file_filter)) - 1 ) == 0 ) {
// Example: --file-filter=*.pdf
tainted_fds[fd] = True;
if ( verbose )
VG_(printf)("syscall open %d %s %lx %d\n", (int)tid, fdpath, args[1], fd);
read_offset = 0;
} else if (TNT_(clo_taint_stdin)) {
tainted_fds[fd] = True;
if ( verbose )
VG_(printf)("syscall open %d %s %lx %d\n", (int)tid, fdpath, args[1], fd);
} else
tainted_fds[fd] = False;
}
}
void TNT_(syscall_close)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// int close (int filedes)
Int fd = args[0];
if (fd > -1 && fd < FD_MAX){
//if (tainted_fds[tid][fd] == True)
// VG_(printf)("syscall close %d %d\n", tid, fd);
shared_fds[fd] = 0;
tainted_fds[fd] = False;
}
}
void TNT_(syscall_write)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
Int fd = args[0];
TNT_(check_fd_access)(tid, fd, FD_WRITE);
}
void TNT_(get_fnname)(ThreadId tid, const HChar** buf) {
UInt pc = VG_(get_IP)(tid);
DiEpoch ep = VG_(current_DiEpoch)();
VG_(get_fnname)(ep, pc, buf);
}
void TNT_(check_fd_access)(ThreadId tid, UInt fd, Int fd_request) {
if (IN_SANDBOX) {
Bool allowed = shared_fds[fd] & fd_request;
// VG_(printf)("checking if allowed to %s from fd %d ... %d\n", (fd_request == FD_READ ? "read" : "write"), fd, allowed);
if (!allowed) {
const HChar* access_str;
switch (fd_request) {
case FD_READ: {
access_str = "read from";
break;
}
case FD_WRITE: {
access_str = "wrote to";
break;
}
default: {
tl_assert(0);
break;
}
}
HChar fdpath[FD_MAX_PATH];
#ifdef VGO_freebsd
VG_(resolve_filename)(fd, fdpath, FD_MAX_PATH-1);
#elif defined VGO_linux
resolve_filename(fd, fdpath, FD_MAX_PATH-1);
#elif defined VGO_darwin
resolve_filename(fd, fdpath, FD_MAX_PATH-1);
#else
#error OS unknown
#endif
const HChar *fnname;
TNT_(get_fnname)(tid, &fnname);
VG_(printf)("*** Sandbox %s %s (fd: %d) in method %s, but it is not allowed to. ***\n", access_str, fdpath, (int)fd, fnname);
VG_(get_and_pp_StackTrace)(tid, STACK_TRACE_SIZE);
VG_(printf)("\n");
}
}
}
/*** Networking syscalls ***/
void TNT_(syscall_socketcall)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// int socketcall(int call, unsigned long *args);
switch (args[0]) {
#ifdef VKI_SYS_SOCKET
case VKI_SYS_SOCKET:
//VG_(printf)("syscall_socketcall: SOCKET\n");
TNT_(syscall_socket)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_LISTEN
case VKI_SYS_LISTEN:
//TNT_(syscall_listen)(tid, res);
break;
#endif
#ifdef VKI_SYS_ACCEPT
case VKI_SYS_ACCEPT:
//VG_(printf)("syscall_socketcall: ACCEPT\n");
TNT_(syscall_accept)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_CONNECT
case VKI_SYS_CONNECT:
//VG_(printf)("syscall_socketcall: CONNECT\n");
// TODO: submit a syscall hooking patch to valgrind to avoid this.
TNT_(syscall_connect)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_GETPEERNAME
case VKI_SYS_GETPEERNAME:
//VG_(printf)("syscall_socketcall: GETPEERNAME\n");
break;
#endif
#ifdef VKI_SYS_GETSOCKNAME
case VKI_SYS_GETSOCKNAME:
//VG_(printf)("syscall_socketcall: GETSOCKNAME\n");
break;
#endif
#ifdef VKI_SYS_SOCKETPAIR
case VKI_SYS_SOCKETPAIR:
//VG_(printf)("syscall_socketcall: SOCKETPAIR\n");
TNT_(syscall_socketpair)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_RECV
case VKI_SYS_RECV:
// VG_(printf)("syscall_socketcall: RECV\n");
TNT_(syscall_recv)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_RECVMSG
case VKI_SYS_RECVMSG:
//VG_(printf)("syscall_socketcall: RECVMSG\n");
TNT_(syscall_recvmsg)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_RECVFROM
case VKI_SYS_RECVFROM:
//VG_(printf)("syscall_socketcall: RECVFROM\n");
TNT_(syscall_recvfrom)(tid, args, nArgs, res);
break;
#endif
#ifdef VKI_SYS_SHUTDOWN
case VKI_SYS_SHUTDOWN:
// VG_(printf)("syscall_socketcall: SHUTDOWN\n");
break;
#endif
default:
return;
}
}
void TNT_(syscall_socket)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
Int fd = sr_Res(res);
// Nothing to do if no network tainting
if (!TNT_(clo_taint_network))
return;
if (fd > -1 && fd < FD_MAX) {
tainted_fds[fd] = True;
//VG_(printf)("syscall_socket: tainting %d\n", fd);
}
}
void TNT_(syscall_connect)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// Assume this is called directly after arguments have been populated.
Int fd = args[0];
// Nothing to do if no network tainting
if (!TNT_(clo_taint_network))
return;
if (fd > -1 && fd < FD_MAX) {
tainted_fds[fd] = True;
// VG_(printf)("syscall_connect: tainting %d\n", fd);
}
}
void TNT_(syscall_socketpair)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// int socketpair(int domain, int type, int protocol, int sv[2]);
// Assume this is called directly after arguments have been populated.
Int fd = ((Int *)args[3])[0];
// Nothing to do if no network tainting
if (!TNT_(clo_taint_network))
return;
if (fd > -1 && fd < FD_MAX) {
tainted_fds[fd] = True;
// VG_(printf)("syscall_socketpair: tainting fd %d\n", fd);
}
}
void TNT_(syscall_accept)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
Int fd = sr_Res(res);
// Nothing to do if no network tainting
if (!TNT_(clo_taint_network))
return;
if (fd > -1 && fd < FD_MAX) {
tainted_fds[fd] = True;
// VG_(printf)("syscall_connect: tainting %d\n", fd);
}
}
void TNT_(syscall_recv)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// ssize_t recv(int sockfd, void *buf, size_t len, int flags)
Int fd = args[0];
HChar *data = (HChar *)args[1];
Int msglen = sr_Res(res);
//VG_(printf)("syscall recv %d 0x%x 0x%02x\n", tid, msglen, data[0]);
if (fd > -1 && fd < FD_MAX && tainted_fds[fd] == True && msglen > 0) {
TNT_(make_mem_tainted)((UWord)data, msglen);
}
}
void TNT_(syscall_recvfrom)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
// ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
// struct sockaddr *src_addr, socklen_t *addrlen)
// TODO: #include <arpa/inet.h> inet_ntop to pretty print IP address
Int fd = args[0];
HChar *data = (HChar *)args[1];
Int msglen = sr_Res(res);
//VG_(printf)("syscall recvfrom %d 0x%x 0x%02x\n", tid, msglen, data[0]);
if (fd > -1 && fd < FD_MAX && tainted_fds[fd] == True && msglen > 0) {
TNT_(make_mem_tainted)((UWord)data, msglen);
}
}
/* Annoyingly uses the struct msghdr from sys/socket.h
* XXX: scatter gather array and readv() not yet supported.d
*/
void TNT_(syscall_recvmsg)(ThreadId tid, UWord* args, UInt nArgs, SysRes res) {
Int fd = args[0];
struct vki_msghdr *msg = (struct vki_msghdr *)args[1];
if (fd > -1 && fd < FD_MAX && tainted_fds[fd] == True && sr_Res(res) > 0) {
// XXX: if MSG_TRUNC, this will taint more memory than it should.
TNT_(make_mem_tainted)((UWord)msg->msg_control, sr_Res(res));
}
}
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/