-
Notifications
You must be signed in to change notification settings - Fork 5
/
tnt_syswrap.c
455 lines (393 loc) · 14.8 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
/*--------------------------------------------------------------------*/
/*--- 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"
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[VG_N_THREADS][FD_MAX];
static UInt read_offset = 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;
if (fd >= FD_MAX || tainted_fds[tid][fd] == False)
return;
VG_(printf)("syscall _llseek %d %d ", tid, fd);
VG_(printf)("0x%x 0x%x 0x%x 0x%x\n", (UInt)offset_high, (UInt)offset_low, result, whence);
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*/ )
tl_assert(0);
}
Bool TNT_(syscall_allowed_check)(ThreadId tid, int syscallno) {
if (IN_SANDBOX && IS_SYSCALL_ALLOWED(syscallno)) {
HChar fnname[FNNAME_MAX];
TNT_(get_fnname)(tid, fnname, FNNAME_MAX);
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;
}
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];
Char *data = (Char *)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);
UWord addr;
Int len;
Int i;
TNT_(check_fd_access)(tid, fd, FD_READ);
if (curr_len == 0) return;
TNT_(make_mem_defined)( (UWord)data, curr_len );
if (fd >= FD_MAX || tainted_fds[tid][fd] == False)
return;
if(1){
//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 ", tid, fd);
VG_(printf)("0x%x 0x%x 0x%x 0x%x\n", curr_offset, curr_len, (Int)data,
*(Char *)data);
}
if( TNT_(clo_taint_all) ){
// Turn instrumentation on
TNT_(instrument_start) = True;
addr = (UWord)data;
len = curr_len;
}else
/* 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
// Turn instrumentation on
TNT_(instrument_start) = True;
addr = (UWord)(data + taint_offset - curr_offset);
len = taint_len;
}else{
// Case 2
TNT_(instrument_start) = True;
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
TNT_(instrument_start) = True;
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
TNT_(instrument_start) = True;
addr = (UWord)data;
len = curr_len;
}else{
// Update file position
read_offset += curr_len;
return;
}
TNT_(make_mem_tainted)( addr, len );
// if(isPread)
// VG_(printf)("syscall pread %d %d ", tid, fd);
// else
// VG_(printf)("syscall read %d %d ", tid, fd);
// VG_(printf)("0x%x 0x%x 0x%x 0x%x\n", curr_offset, curr_len, (Int)data, len);
for( i=0; i<len; i++)
VG_(printf)("taint_byte 0x%08lx 0x%x\n", addr+i, *(Char *)(addr+i));
// Update file position
read_offset += curr_len;
}
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];
Char *data = (Char *)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);
UWord addr;
Int len;
Int i;
if (curr_len == 0) return;
TNT_(make_mem_defined)( (UWord)data, curr_len );
if (fd >= FD_MAX || tainted_fds[tid][fd] == False)
return;
if(1){
//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 ", tid, fd);
VG_(printf)("0x%x 0x%x 0x%x\n", curr_offset, curr_len, (Int)data);
}
if( TNT_(clo_taint_all) ){
// Turn instrumentation on
TNT_(instrument_start) = True;
addr = (UWord)data;
len = curr_len;
}else
/* 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
// Turn instrumentation on
TNT_(instrument_start) = True;
addr = (UWord)(data + taint_offset - curr_offset);
len = taint_len;
}else{
// Case 2
TNT_(instrument_start) = True;
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
TNT_(instrument_start) = True;
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
TNT_(instrument_start) = True;
addr = (UWord)data;
len = curr_len;
}else{
return;
}
TNT_(make_mem_tainted)( addr, len );
// if(isPread)
// VG_(printf)("syscall pread %d %d ", tid, fd);
// else
// VG_(printf)("syscall read %d %d ", tid, fd);
// VG_(printf)("0x%x 0x%x 0x%x 0x%x\n", curr_offset, curr_len, (Int)data, len);
for( i=0; i<len; i++)
VG_(printf)("taint_byte 0x%08lx 0x%x\n", addr+i, *(Char *)(addr+i));
}
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);
// 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);
#else
#error OS unknown
#endif
HChar fnname[FNNAME_MAX];
TNT_(get_fnname)(tid, fnname, FNNAME_MAX);
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 )
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);
#else
#error OS unknown
#endif
if( TNT_(clo_taint_all) ){
// Turn instrumentation on
TNT_(instrument_start) = True;
tainted_fds[tid][fd] = True;
VG_(printf)("syscall open %d %s %lx %d\n", tid, fdpath, args[1], fd);
read_offset = 0;
} else if ( VG_(strncmp)(fdpath, TNT_(clo_file_filter),
VG_(strlen)( TNT_(clo_file_filter))) == 0 ) {
// Turn instrumentation on
TNT_(instrument_start) = True;
tainted_fds[tid][fd] = True;
VG_(printf)("syscall open %d %s %lx %d\n", 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 ) {
// Turn instrumentation on
TNT_(instrument_start) = True;
tainted_fds[tid][fd] = True;
VG_(printf)("syscall open %d %s %lx %d\n", tid, fdpath, args[1], fd);
read_offset = 0;
} else
tainted_fds[tid][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[tid][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, HChar* buf, UInt buf_size) {
UInt pc = VG_(get_IP)(tid);
VG_(get_fnname)(pc, buf, buf_size);
}
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) {
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);
#else
#error OS unknown
#endif
HChar fnname[FNNAME_MAX];
TNT_(get_fnname)(tid, fnname, FNNAME_MAX);
VG_(printf)("*** Sandbox %s %s (fd: %d) in method %s, but it is not allowed to. ***\n", access_str, fdpath, fd, fnname);
VG_(get_and_pp_StackTrace)(tid, STACK_TRACE_SIZE);
VG_(printf)("\n");
}
}
}
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/