-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlibdft-dta.cpp
821 lines (718 loc) · 20 KB
/
libdft-dta.cpp
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*-
* Copyright (c) 2010, 2011, 2012, 2013, Columbia University
* All rights reserved.
*
* This software was developed by Vasileios P. Kemerlis <[email protected]>
* at Columbia University, New York, NY, USA, in October 2010.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Columbia University nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* TODO:
* - add support for file descriptor duplication via fcntl(2)
* - add support for non PF_INET* sockets
* - add support for recvmmsg(2)
*/
#include <errno.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <set>
#include <unistd.h>
#include <linux/net.h>
#include "branch_pred.h"
#include "libdft_api.h"
#include "libdft_core.h"
#include "syscall_desc.h"
#include "tagmap.h"
#define WORD_LEN 4 /* size in bytes of a word value */
#define SYS_SOCKET 1 /* socket(2) demux index for socketcall */
/* default path for the log file (audit) */
#define LOGFILE_DFL "/tmp/libdft-dta.log"
/* default suffixes for dynamic shared libraries */
#define DLIB_SUFF ".so"
#define DLIB_SUFF_ALT ".so."
/* thread context */
extern REG thread_ctx_ptr;
/* ins descriptors */
extern ins_desc_t ins_desc[XED_ICLASS_LAST];
/* syscall descriptors */
extern syscall_desc_t syscall_desc[SYSCALL_MAX];
/* socket related syscalls */
static int sock_syscalls[] = {
__NR_socket,
__NR_accept,
__NR_accept4,
__NR_getsockname,
__NR_getpeername,
__NR_socketpair,
__NR_recvfrom,
__NR_getsockopt,
__NR_recvmsg,
__NR_recvmmsg,
};
/* set of interesting descriptors (sockets) */
static std::set<int> fdset;
/* the tag value used for tainting */
static tag_traits<tag_t>::type dta_tag = 1;
/* log file path (auditing) */
static KNOB<std::string> logpath(KNOB_MODE_WRITEONCE, "pintool", "l",
LOGFILE_DFL, "");
/*
* flag variables
*
* 0 : feature disabled
* >= 1 : feature enabled
*/
/* track stdin (enabled by default) */
static KNOB<size_t> stdin_(KNOB_MODE_WRITEONCE, "pintool", "s", "1", "");
/* track fs (enabled by default) */
static KNOB<size_t> fs(KNOB_MODE_WRITEONCE, "pintool", "f", "1", "");
/* track net (enabled by default) */
static KNOB<size_t> net(KNOB_MODE_WRITEONCE, "pintool", "n", "1", "");
/*
* DTA/DFT alert
*
* @ins: address of the offending instruction
* @bt: address of the branch target
*/
static void PIN_FAST_ANALYSIS_CALL
alert(ADDRINT ins, ADDRINT bt)
{
/* log file */
FILE *logfile;
/* auditing */
if (likely((logfile = fopen(logpath.Value().c_str(), "a")) != NULL)) {
/* hilarious :) */
(void)fprintf(logfile, " ____ ____ ____ ____\n");
(void)fprintf(logfile, "||w |||o |||o |||t ||\n");
(void)fprintf(logfile, "||__|||__|||__|||__||\t");
(void)fprintf(logfile, "[%d]: 0x%08lx --> 0x%08lx\n",
getpid(), ins, bt);
(void)fprintf(logfile, "|/__\\|/__\\|/__\\|/__\\|\n");
/* cleanup */
(void)fclose(logfile);
}
else
/* failed */
LOG(std::string(__func__) +
": failed while trying to open " +
logpath.Value().c_str() + " (" +
std::string(strerror(errno)) + ")\n");
/* terminate */
exit(EXIT_FAILURE);
}
/*
* 32-bit register assertion (taint-sink, DFT-sink)
*
* called before an instruction that uses a register
* for an indirect branch; returns a positive value
* whenever the register value or the target address
* are tainted
*
* returns: 0 (clean), >0 (tainted)
*/
static ADDRINT PIN_FAST_ANALYSIS_CALL
assert_reg32(thread_ctx_t *thread_ctx, uint32_t reg, uint32_t addr)
{
/*
* combine the register tag along with the tag
* markings of the target address
*/
return tag_combine(thread_ctx->vcpu.gpr[reg][0], tagmap_getl(addr));
}
/*
* 16-bit register assertion (taint-sink, DFT-sink)
*
* called before an instruction that uses a register
* for an indirect branch; returns a positive value
* whenever the register value or the target address
* are tainted
*
* returns: 0 (clean), >0 (tainted)
*/
static ADDRINT PIN_FAST_ANALYSIS_CALL
assert_reg16(thread_ctx_t *thread_ctx, uint32_t reg, uint32_t addr)
{
/*
* combine the register tag along with the tag
* markings of the target address
*/
return tag_combine(thread_ctx->vcpu.gpr[reg][0] & VCPU_MASK16, tagmap_getw(addr));
}
/*
* 32-bit memory assertion (taint-sink, DFT-sink)
*
* called before an instruction that uses a memory
* location for an indirect branch; returns a positive
* value whenever the memory value (i.e., effective address),
* or the target address, are tainted
*
* returns: 0 (clean), >0 (tainted)
*/
static ADDRINT PIN_FAST_ANALYSIS_CALL
assert_mem32(ADDRINT paddr, ADDRINT taddr)
{
return tagmap_getl(paddr) | tagmap_getl(taddr);
}
/*
* 16-bit memory assertion (taint-sink, DFT-sink)
*
* called before an instruction that uses a memory
* location for an indirect branch; returns a positive
* value whenever the memory value (i.e., effective address),
* or the target address, are tainted
*
* returns: 0 (clean), >0 (tainted)
*/
static ADDRINT PIN_FAST_ANALYSIS_CALL
assert_mem16(ADDRINT paddr, ADDRINT taddr)
{
return tagmap_getw(paddr) | tagmap_getw(taddr);
}
/*
* instrument the jmp/call instructions
*
* install the appropriate DTA/DFT logic (sinks)
*
* @ins: the instruction to instrument
*/
static void
dta_instrument_jmp_call(INS ins)
{
/* temporaries */
REG reg;
/*
* we only care about indirect calls;
* optimized branch
*/
if (unlikely(INS_IsIndirectBranchOrCall(ins))) {
/* perform operand analysis */
/* call via register */
if (INS_OperandIsReg(ins, 0)) {
/* extract the register from the instruction */
reg = INS_OperandReg(ins, 0);
/* size analysis */
/* 32-bit register */
if (REG_is_gr32(reg))
/*
* instrument assert_reg32() before branch;
* conditional instrumentation -- if
*/
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)assert_reg32,
IARG_FAST_ANALYSIS_CALL,
IARG_REG_VALUE, thread_ctx_ptr,
IARG_UINT32, REG_INDX(reg),
IARG_REG_VALUE, reg,
IARG_END);
else
/* 16-bit register */
/*
* instrument assert_reg16() before branch;
* conditional instrumentation -- if
*/
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)assert_reg16,
IARG_FAST_ANALYSIS_CALL,
IARG_REG_VALUE, thread_ctx_ptr,
IARG_UINT32, REG_INDX(reg),
IARG_REG_VALUE, reg,
IARG_END);
}
else {
/* call via memory */
/* size analysis */
/* 32-bit */
if (INS_MemoryReadSize(ins) == WORD_LEN)
/*
* instrument assert_mem32() before branch;
* conditional instrumentation -- if
*/
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)assert_mem32,
IARG_FAST_ANALYSIS_CALL,
IARG_MEMORYREAD_EA,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
/* 16-bit */
else
/*
* instrument assert_mem16() before branch;
* conditional instrumentation -- if
*/
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)assert_mem16,
IARG_FAST_ANALYSIS_CALL,
IARG_MEMORYREAD_EA,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
}
/*
* instrument alert() before branch;
* conditional instrumentation -- then
*/
INS_InsertThenCall(ins,
IPOINT_BEFORE,
(AFUNPTR)alert,
IARG_FAST_ANALYSIS_CALL,
IARG_INST_PTR,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
}
}
/*
* instrument the ret instruction
*
* install the appropriate DTA/DFT logic (sinks)
*
* @ins: the instruction to instrument
*/
static void
dta_instrument_ret(INS ins)
{
/* size analysis */
/* 32-bit */
if (INS_MemoryReadSize(ins) == WORD_LEN)
/*
* instrument assert_mem32() before ret;
* conditional instrumentation -- if
*/
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)assert_mem32,
IARG_FAST_ANALYSIS_CALL,
IARG_MEMORYREAD_EA,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
/* 16-bit */
else
/*
* instrument assert_mem16() before ret;
* conditional instrumentation -- if
*/
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)assert_mem16,
IARG_FAST_ANALYSIS_CALL,
IARG_MEMORYREAD_EA,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
/*
* instrument alert() before ret;
* conditional instrumentation -- then
*/
INS_InsertThenCall(ins,
IPOINT_BEFORE,
(AFUNPTR)alert,
IARG_FAST_ANALYSIS_CALL,
IARG_INST_PTR,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
}
/*
* read(2) handler (taint-source)
*/
static void
post_read_hook(THREADID tid, syscall_ctx_t *ctx)
{
/* read() was not successful; optimized branch */
if (unlikely((long)ctx->ret <= 0))
return;
/* taint-source */
if (fdset.find(ctx->arg[SYSCALL_ARG0]) != fdset.end())
/* set the tag markings */
tagmap_setn(ctx->arg[SYSCALL_ARG1], (size_t)ctx->ret, dta_tag);
else
/* clear the tag markings */
tagmap_clrn(ctx->arg[SYSCALL_ARG1], (size_t)ctx->ret);
}
/*
* readv(2) handler (taint-source)
*/
static void
post_readv_hook(THREADID tid, syscall_ctx_t *ctx)
{
/* iterators */
int i;
struct iovec *iov;
std::set<int>::iterator it;
/* bytes copied in a iovec structure */
size_t iov_tot;
/* total bytes copied */
size_t tot = (size_t)ctx->ret;
/* readv() was not successful; optimized branch */
if (unlikely((long)ctx->ret <= 0))
return;
/* get the descriptor */
it = fdset.find((int)ctx->arg[SYSCALL_ARG0]);
/* iterate the iovec structures */
for (i = 0; i < (int)ctx->arg[SYSCALL_ARG2] && tot > 0; i++) {
/* get an iovec */
iov = ((struct iovec *)ctx->arg[SYSCALL_ARG1]) + i;
/* get the length of the iovec */
iov_tot = (tot >= (size_t)iov->iov_len) ?
(size_t)iov->iov_len : tot;
/* taint interesting data and zero everything else */
if (it != fdset.end())
/* set the tag markings */
tagmap_setn((size_t)iov->iov_base, iov_tot, dta_tag);
else
/* clear the tag markings */
tagmap_clrn((size_t)iov->iov_base, iov_tot);
/* housekeeping */
tot -= iov_tot;
}
}
/*
* socketcall(2) handler
*
* attach taint-sources in the following
* syscalls:
* socket(2), accept(2), recv(2),
* recvfrom(2), recvmsg(2)
*
* everything else is left intact in order
* to avoid taint-leaks
*/
static void
post_socketcall_hook(THREADID tid, syscall_ctx_t *ctx)
{
/* message header; recvmsg(2) */
struct msghdr *msg;
/* iov bytes copied; recvmsg(2) */
size_t iov_tot;
/* iterators */
size_t i;
struct iovec *iov;
std::set<int>::iterator it;
/* total bytes received */
size_t tot;
/* socket call arguments */
unsigned long *args = (unsigned long *)ctx->arg[SYSCALL_ARG0];
/* demultiplex the socketcall */
switch (ctx->nr) {
case __NR_socket:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/*
* PF_INET and PF_INET6 descriptors are
* considered interesting
*/
if (likely(args[SYSCALL_ARG0] == PF_INET ||
args[SYSCALL_ARG0] == PF_INET6))
/* add the descriptor to the monitored set */
fdset.insert((int)ctx->ret);
/* done */
break;
case __NR_accept:
case __NR_accept4:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/*
* if the socket argument is interesting,
* the returned handle of accept(2) is also
* interesting
*/
if (likely(fdset.find(args[SYSCALL_ARG0]) !=
fdset.end()))
/* add the descriptor to the monitored set */
fdset.insert((int)ctx->ret);
case __NR_getsockname:
case __NR_getpeername:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/* addr argument is provided */
if ((void *)args[SYSCALL_ARG1] != NULL) {
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG1],
*((int *)args[SYSCALL_ARG2]));
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG2], sizeof(int));
}
break;
case __NR_socketpair:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG3], (sizeof(int) * 2));
break;
case __NR_recvfrom:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret <= 0))
return;
/* taint-source */
if (fdset.find((int)args[SYSCALL_ARG0]) != fdset.end())
/* set the tag markings */
tagmap_setn(args[SYSCALL_ARG1],
(size_t)ctx->ret,
dta_tag);
else
/* clear the tag markings */
tagmap_clrn(args[SYSCALL_ARG1],
(size_t)ctx->ret);
/* sockaddr argument is specified */
if ((void *)args[SYSCALL_ARG4] != NULL) {
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG4],
*((int *)args[SYSCALL_ARG5]));
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG5], sizeof(int));
}
break;
case __NR_getsockopt:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG3],
*((int *)args[SYSCALL_ARG4]));
/* clear the tag bits */
tagmap_clrn(args[SYSCALL_ARG4], sizeof(int));
break;
case __NR_recvmsg:
/* not successful; optimized branch */
if (unlikely((long)ctx->ret <= 0))
return;
/* get the descriptor */
it = fdset.find((int)ctx->arg[SYSCALL_ARG0]);
/* extract the message header */
msg = (struct msghdr *)args[SYSCALL_ARG1];
/* source address specified */
if (msg->msg_name != NULL) {
/* clear the tag bits */
tagmap_clrn((size_t)msg->msg_name,
msg->msg_namelen);
/* clear the tag bits */
tagmap_clrn((size_t)&msg->msg_namelen,
sizeof(int));
}
/* ancillary data specified */
if (msg->msg_control != NULL) {
/* taint-source */
if (it != fdset.end())
/* set the tag markings */
tagmap_setn((size_t)msg->msg_control,
msg->msg_controllen,
dta_tag);
else
/* clear the tag markings */
tagmap_clrn((size_t)msg->msg_control,
msg->msg_controllen);
/* clear the tag bits */
tagmap_clrn((size_t)&msg->msg_controllen,
sizeof(int));
}
/* flags; clear the tag bits */
tagmap_clrn((size_t)&msg->msg_flags, sizeof(int));
/* total bytes received */
tot = (size_t)ctx->ret;
/* iterate the iovec structures */
for (i = 0; i < msg->msg_iovlen && tot > 0; i++) {
/* get the next I/O vector */
iov = &msg->msg_iov[i];
/* get the length of the iovec */
iov_tot = (tot > (size_t)iov->iov_len) ?
(size_t)iov->iov_len : tot;
/* taint-source */
if (it != fdset.end())
/* set the tag markings */
tagmap_setn((size_t)iov->iov_base,
iov_tot,
dta_tag);
else
/* clear the tag markings */
tagmap_clrn((size_t)iov->iov_base,
iov_tot);
/* housekeeping */
tot -= iov_tot;
}
break;
#if LINUX_KERNEL >= 2633
case __NR_recvmmsg:
#endif
default:
/* nothing to do */
return;
}
}
/*
* auxiliary (helper) function
*
* duplicated descriptors are added into
* the monitored set
*/
static void
post_dup_hook(THREADID tid, syscall_ctx_t *ctx)
{
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/*
* if the old descriptor argument is
* interesting, the returned handle is
* also interesting
*/
if (likely(fdset.find((int)ctx->arg[SYSCALL_ARG0]) != fdset.end()))
fdset.insert((int)ctx->ret);
}
/*
* auxiliary (helper) function
*
* whenever close(2) is invoked, check
* the descriptor and remove if it was
* inside the monitored set of descriptors
*/
static void
post_close_hook(THREADID tid, syscall_ctx_t *ctx)
{
/* iterator */
std::set<int>::iterator it;
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/*
* if the descriptor (argument) is
* interesting, remove it from the
* monitored set
*/
it = fdset.find((int)ctx->arg[SYSCALL_ARG0]);
if (likely(it != fdset.end()))
fdset.erase(it);
}
/*
* auxiliary (helper) function
*
* whenever open(2)/creat(2) is invoked,
* add the descriptor inside the monitored
* set of descriptors
*
* NOTE: it does not track dynamic shared
* libraries
*/
static void
post_open_hook(THREADID tid, syscall_ctx_t *ctx)
{
/* not successful; optimized branch */
if (unlikely((long)ctx->ret < 0))
return;
/* ignore dynamic shared libraries */
if (strstr((char *)ctx->arg[SYSCALL_ARG0], DLIB_SUFF) == NULL &&
strstr((char *)ctx->arg[SYSCALL_ARG0], DLIB_SUFF_ALT) == NULL)
fdset.insert((int)ctx->ret);
}
/*
* DTA
*
* used for demonstrating how to implement
* a practical dynamic taint analysis (DTA)
* tool using libdft
*/
int
main(int argc, char **argv)
{
/* initialize symbol processing */
PIN_InitSymbols();
/* initialize Pin; optimized branch */
if (unlikely(PIN_Init(argc, argv)))
/* Pin initialization failed */
goto err;
/* initialize the core tagging engine */
if (unlikely(libdft_init() != 0))
/* failed */
goto err;
/*
* handle control transfer instructions
*
* instrument the branch instructions, accordingly,
* for installing taint-sinks (DFT-logic) that check
* for tainted targets (i.e., tainted operands or
* tainted branch targets) -- For brevity I omitted
* checking the result of each instrumentation for
* success or failure
*/
/* instrument call */
(void)ins_set_post(&ins_desc[XED_ICLASS_CALL_NEAR],
dta_instrument_jmp_call);
/* instrument jmp */
(void)ins_set_post(&ins_desc[XED_ICLASS_JMP],
dta_instrument_jmp_call);
/* instrument ret */
(void)ins_set_post(&ins_desc[XED_ICLASS_RET_NEAR],
dta_instrument_ret);
/*
* install taint-sources
*
* all network-related I/O calls are
* assumed to be taint-sources; we
* install the appropriate wrappers
* for tagging the received data
* accordingly -- Again, for brevity
* I assume that all calls to
* syscall_set_post() are successful
*/
/* read(2) */
(void)syscall_set_post(&syscall_desc[__NR_read], post_read_hook);
/* readv(2) */
(void)syscall_set_post(&syscall_desc[__NR_readv], post_readv_hook);
/* socket(2), accept(2), recv(2), recvfrom(2), recvmsg(2) */
if (net.Value() != 0)
for (int sock_nr : sock_syscalls)
(void)syscall_set_post(&syscall_desc[sock_nr],
post_socketcall_hook);
/* dup(2), dup2(2) */
(void)syscall_set_post(&syscall_desc[__NR_dup], post_dup_hook);
(void)syscall_set_post(&syscall_desc[__NR_dup2], post_dup_hook);
/* close(2) */
(void)syscall_set_post(&syscall_desc[__NR_close], post_close_hook);
/* open(2), creat(2) */
if (fs.Value() != 0) {
(void)syscall_set_post(&syscall_desc[__NR_open],
post_open_hook);
(void)syscall_set_post(&syscall_desc[__NR_creat],
post_open_hook);
}
/* add stdin to the interesting descriptors set */
if (stdin_.Value() != 0)
fdset.insert(STDIN_FILENO);
/* start Pin */
PIN_StartProgram();
/* typically not reached; make the compiler happy */
return EXIT_SUCCESS;
err: /* error handling */
/* detach from the process */
libdft_die();
/* return */
return EXIT_FAILURE;
}