-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpin_pass.cpp
executable file
·1137 lines (1087 loc) · 40.6 KB
/
pin_pass.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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include "pin.H"
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <unordered_map>
#include <exception>
#include <sstream>
#include <stack>
using std::string;
using std::cerr;
using std::endl;
// debug macros
#define DEBUG
// // instruction categories
#define INST_CAT
// exit on overflow
#define OVERFLOW
// ext-2 i.e. unknown instruction support
#define UNOWNED
// Argv[6] is the program image
std::string ProgramImage;
// output
std::ofstream OutFile;
// to string patch
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
// implementing split function, which is analogous to the boost split function
template <typename Split>
void split(const string &s, char delim, Split result) {
std::istringstream iss(s);
string item;
while (std::getline(iss, item, delim)) {
*result++ = item;
}
}
std::vector<string> split(const string &s, char delim) {
std::vector<string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
// count instrumentation category reached
#ifdef INST_CAT
static int64_t count_1 = 0;
static int64_t count_2 = 0;
static int64_t count_3 = 0;
static int64_t count_4 = 0;
static int64_t count_5 = 0;
static int64_t count_6 = 0;
static int64_t count_7 = 0;
#endif
// argv adress pointers
std::vector<int> argv_sizes;
// Object to store the malloc/ calloc/ realloc information per allocation
class MallocMap
{
uint64_t size;
uint64_t address;
// todo: experimental
bool check;
public:
MallocMap(uint64_t address, uint64_t size, bool check){this->address = address; this->size = size; this->check=check;}
void setmem(uint64_t address, uint64_t size, bool check){this->address = address; this->size = size; this->check=check;}
void setsize(uint64_t size) {this->size = size;}
void setcheck(uint64_t check) {this->check = check;}
uint64_t getsize() {return this->size;}
uint64_t getaddress() {return this->address;}
bool getcheck() {return this->check;}
};
// To hold each allocation
std::map<uint64_t, MallocMap*> mallocmap;
// access bounds
class AccessBounds
{
private:
// base and bounds of the object pointer to
std::stack <uint64_t> base;
std::stack <uint64_t> bound;
// set location information
// stacks store bounds information per object
std::stack <uint64_t> mem_base;
std::stack <uint64_t> mem_bound;
// isempty
public:
AccessBounds(uint64_t mem_base, uint64_t mem_bound)
{this->mem_base.push(mem_base); this->mem_bound.push(mem_bound); this->base.push(0); this->bound.push(0);}
void set_membounds(uint64_t mem_base, uint64_t mem_bound)
{this->mem_base.push(mem_base); this->mem_bound.push(mem_bound); this->base.push(0); this->bound.push(0);}
void set_bounds(uint64_t base, uint64_t bound)
{this->base.pop(); this->bound.pop(); this->base.push(base); this->bound.push(bound);}
uint64_t get_base(){return this->base.top();}
uint64_t get_bound(){return this->bound.top();}
uint64_t get_membase(){return this->mem_base.top();}
uint64_t get_membound(){return this->mem_bound.top();}
// this will remove top bounds and location information
void remove_bounds(){this->mem_base.pop();this->mem_bound.pop();this->base.pop();this->bound.pop();}
// check if all the stacks are empty
// if an object is empty, then it can be removed in stack epilogue
bool is_empty(){return (this->mem_base.empty()==true) && (this->mem_bound.empty()==true);}
};
// Map to store all bound information globally
// key: owner
std::unordered_map <std::string, AccessBounds*> accessboundsmap;
// contains the information of all the global objects (like data of bss section)
class GlobObjInfo
{
private:
// Location from the base pointer and the upper bound
int64_t ub;
// Object Type
std::string obj;
// Object name
std::string owner;
// Object size
int64_t obj_size;
// lower bound
int64_t lb;
public:
GlobObjInfo(int64_t lb, std::string obj, string owner, int64_t obj_size)
{
this->lb = lb + obj_size;
this->obj = obj;
this->owner = owner;
this->obj_size = obj_size;
// Lower bounds calculated here
this->ub = lb;
}
int64_t get_ub() {return ub;}
std::string get_obj() {return obj;}
std::string get_owner() {return owner;}
int64_t get_obj_size() {return obj_size;}
int64_t get_lb() {return lb;}
};
// A stack to store all the global variables
std::unordered_map <std::string, GlobObjInfo*> globalobjinfostack;
// Contains the information of all the objects
class ObjInfo
{
private:
// Location from the base pointer and the upper bound
int64_t ub;
// Object Type
std::string obj;
// Object name
std::string owner;
// Object size
int64_t obj_size;
// lower bound
int64_t lb;
public:
ObjInfo(int64_t ub, std::string obj, string owner, int64_t obj_size)
{
this->ub = ub;
this->obj = obj;
this->owner = owner;
this->obj_size = obj_size;
// Lower bounds calculated here
this->lb = ub + obj_size;
}
int64_t get_ub() {return ub;}
std::string get_obj() {return obj;}
std::string get_owner() {return owner;}
int64_t get_obj_size() {return obj_size;}
int64_t get_lb() {return lb;}
};
// Owner infomation of each location (address)
class InsInfo
{
private:
ADDRINT address;
std::string owner;
public:
InsInfo(ADDRINT address, std::string owner) { this->address = address; this->owner = owner;}
ADDRINT get_address() {return address;}
std::string get_owner() {return owner;}
};
// A structure to store all the file related information
struct Block
{
// Block name
std::string name;
// Set the rbp value for the particular block
uint64_t rbp_value;
// Set the rsp value for the particular block
uint64_t rsp_value;
// check if rbp relative addressing is used
ADDRINT fun_entry;
// function exit address
ADDRINT fun_exit;
// rsp/rbp
uint64_t rsp_rbp;
// argstack size
uint64_t parameter;
// stack size
uint64_t stack_size;
// Object information hash map
std::unordered_map <std::string, ObjInfo*> objinfostack;
// static code locations hash map
std::unordered_map <ADDRINT, InsInfo*> inscodestack;
};
// Map containing Blocks
// The keys are function name and the values are blocks per function
std::unordered_map <std::string, struct Block*> blocks;
VOID set_stack(CONTEXT * ctxt, Block &i)
{
#ifdef INST_CAT
count_1++;
#endif
i.rsp_value = PIN_GetContextReg(ctxt, REG_RSP) - i.rsp_rbp;
// i.rsp_value = PIN_GetContextReg(ctxt, REG_RSP) + 8;
i.parameter = i.parameter+i.rsp_value;
i.stack_size = i.rsp_value - i.stack_size;
std::cout << "setting stack for: " << i.name << ", at " <<std::hex<< i.rsp_value << std::dec << '\n';
for(std::unordered_map<std::string, ObjInfo*>::iterator iter = i.objinfostack.begin(); iter != i.objinfostack.end(); ++iter)
{
std::string k = iter->first;
ObjInfo* v = iter->second;
// set variables and their bounds in a global structure
if(accessboundsmap.find(k) == accessboundsmap.end())
{
accessboundsmap.insert(std::make_pair(k, new AccessBounds(v->get_lb() +
i.rsp_value, v->get_ub() + i.rsp_value)));
}
else
{
accessboundsmap[k]->set_membounds(v->get_lb() + i.rsp_value,
v->get_ub() + i.rsp_value);
}
}
// commandline arguments
// only for function main
if (i.name == "main")
{
uint64_t effective_dispacement = 0;
effective_dispacement = PIN_GetContextReg(ctxt, REG_RSI);
accessboundsmap.insert(std::make_pair("main_cmdarg", new AccessBounds(effective_dispacement
+ PIN_GetContextReg(ctxt, REG_RDI) * 8, effective_dispacement)));
// insert each commandline argument as an object
for (ADDRINT j=0; j<PIN_GetContextReg(ctxt, REG_RDI);++j)
{
ADDRINT * addr_ptr = (ADDRINT*)effective_dispacement+j;
ADDRINT value;
PIN_SafeCopy(&value,addr_ptr, sizeof(ADDRINT));
// if(accessboundsmap.find("main_argv_"+patch::to_string(j)) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair("main_argv_"+patch::to_string(j), new AccessBounds(
value+argv_sizes[j]+1, value)));
}
}
}
VOID unset_stack(CONTEXT * ctxt, Block &i)
{
#ifdef INST_CAT
count_2++;
#endif
for(auto iter = accessboundsmap.begin(); iter != accessboundsmap.end();)
{
// find key with functionname_ substring and remove objects
std::string k = iter->first;
AccessBounds* v = iter->second;
if (k.find(i.name+"_") != string::npos)
{
v->remove_bounds();
if (v->is_empty())
{
#ifdef DEBUG
std::cout << "cleared!" << '\n';
#endif
accessboundsmap.erase(iter++);
continue;
}
}
++iter;
}
}
////////////// store routines //////////////
// mov DWORD PTR [rbp-ptr],address
VOID store_ptr_xfer_mem(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg,
int64_t ins_size, ADDRINT imm)
{
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
#endif
#ifdef INST_CAT
count_3++;
#endif
// support mov address instructions - this is not full proof as immediate may not be an address
for(std::unordered_map<std::string, AccessBounds*>::iterator iter = accessboundsmap.begin(); iter != accessboundsmap.end(); ++iter)
{
std::string k = iter->first;
AccessBounds* v = iter->second;
if (imm == v->get_membound())
{accessboundsmap[owner]->set_bounds(accessboundsmap[k]->get_membase(),
accessboundsmap[k]->get_membound());
#ifdef DEBUG
std::cout << "bounds transfered from " << k << '\n';
#endif
return;}
}
if (mallocmap.find(imm) != mallocmap.end())
{
#ifdef DEBUG
std::cout << "mallocmap find " << std::hex << imm << std::dec << '\n';
#endif
accessboundsmap[owner]->set_bounds(
imm+mallocmap[imm]->getsize(),
imm);
return;
}
// check for in block accesses
for (std::map<uint64_t, MallocMap*>::iterator iter = mallocmap.begin(); iter != mallocmap.end(); ++iter)
{
MallocMap* v = iter->second;
if (imm < v->getaddress()+v->getsize() && imm >= v->getaddress())
{
#ifdef DEBUG
std::cout << "mallocmap find " << std::hex << imm << std::dec << '\n';
#endif
accessboundsmap[owner]->set_bounds(
v->getaddress()+v->getsize(),
v->getaddress());
}
}
}
// mov DWORD PTR [rbp-ptr],reg
VOID store_ptr_xfer_reg(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg,
int64_t ins_size, REG reg)
{
#ifdef INST_CAT
count_3++;
#endif
// now check if this instruction leads to pointer propagation
if (REG_Width(reg)!=3)
return;
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
// std::cout << std::hex << "reg: " << PIN_GetContextReg(ctxt, reg) << std::dec << '\n';
#endif
// todo: possible optimization just like immediate
for(std::unordered_map<std::string, AccessBounds*>::iterator iter = accessboundsmap.begin(); iter != accessboundsmap.end(); ++iter)
{
std::string k = iter->first;
AccessBounds* v = iter->second;
if (PIN_GetContextReg(ctxt, reg) < v->get_membase() && PIN_GetContextReg(ctxt, reg) >= v->get_membound())
{accessboundsmap[owner]->set_bounds(accessboundsmap[k]->get_membase(),
accessboundsmap[k]->get_membound());
#ifdef DEBUG
std::cout << "bounds transfered from " << k << '\n';
#endif
return;}
}
// check if mem pointer
if (mallocmap.find(PIN_GetContextReg(ctxt, reg)) != mallocmap.end())
{
#ifdef DEBUG
std::cout << "mallocmap find " << std::hex << PIN_GetContextReg(ctxt, reg) << std::dec << '\n';
#endif
accessboundsmap[owner]->set_bounds(
PIN_GetContextReg(ctxt, reg)+mallocmap[PIN_GetContextReg(ctxt, reg)]->getsize(),
PIN_GetContextReg(ctxt, reg));
return;
}
// check for in block accesses
for (std::map<uint64_t, MallocMap*>::iterator iter = mallocmap.begin(); iter != mallocmap.end(); ++iter)
{
MallocMap* v = iter->second;
if (PIN_GetContextReg(ctxt, reg) < v->getaddress()+v->getsize() && PIN_GetContextReg(ctxt, reg) >= v->getaddress())
{
#ifdef DEBUG
std::cout << "mallocmap find " << std::hex << PIN_GetContextReg(ctxt, reg) << std::dec << '\n';
#endif
accessboundsmap[owner]->set_bounds(
v->getaddress()+v->getsize(),
v->getaddress());
}
}
}
// mov DWORD PTR [*ptr],imm
// mov DWORD PTR [rax],imm
VOID store_ptr_bnd_chk(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg,
int64_t ins_size)
{
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
#endif
#ifdef INST_CAT
count_4++;
#endif
uint64_t effective_dispacement = 0;
// if the owner is on stack
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
if (REG_valid(index_reg))
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
else
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
// if owner is global
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
if (REG_valid(index_reg))
{ // if index register is present, add it
if (base_reg == REG_RIP)
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale) + ins_size;
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{
// if index register is not present
if (base_reg == REG_RIP)
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement + ins_size;
else
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
}
#ifdef DEBUG
std::cout << "effective_dispacement: " << std::hex << effective_dispacement << std::dec << '\n';
std::cout << "Upper bounds: " << std::hex << accessboundsmap[owner]->get_membound() << std::dec << '\n';
std::cout << "Lower bounds: " << std::hex << accessboundsmap[owner]->get_membase() << std::dec << '\n';
std::cout << "ub: " << std::hex << accessboundsmap[owner]->get_bound() << std::dec << '\n';
std::cout << "lb: " << std::hex << accessboundsmap[owner]->get_base() << std::dec << '\n';
#endif
// bound check
if (accessboundsmap[owner]->get_base()==0)
return;
if ((effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound()))
{
std::cout << "Boundover accessed by " << owner << " in store_ptr_bnd_chk, at "
<< std::hex << addr << std::dec << '\n';
// std::exit(1);
#ifdef OVERFLOW
PIN_ExitApplication(1);
#endif
}
}
// mov DWORD PTR [rbp-arr],imm
VOID store_arr_bnd_chk(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg,
int64_t ins_size)
{
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
#endif
#ifdef INST_CAT
count_5++;
#endif
uint64_t effective_dispacement = 0;
// if the owner is on stack
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
if (REG_valid(index_reg))
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
else
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
// if owner is global
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
if (REG_valid(index_reg))
{ // if index register is present, add it
if (base_reg == REG_RIP)
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale) + ins_size;
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{
// if index register is not present
if (base_reg == REG_RIP)
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement + ins_size;
else
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
}
// if owner is nowhere to be found!
else return;
#ifdef DEBUG
std::cout << "effective_dispacement: " << std::hex << effective_dispacement << std::dec << '\n';
std::cout << "Upper bounds: " << std::hex << accessboundsmap[owner]->get_membound() << std::dec << '\n';
std::cout << "Lower bounds: " << std::hex << accessboundsmap[owner]->get_membase() << std::dec << '\n';
std::cout << "ub: " << std::hex << accessboundsmap[owner]->get_bound() << std::dec << '\n';
std::cout << "lb: " << std::hex << accessboundsmap[owner]->get_base() << std::dec << '\n';
#endif
if ((effective_dispacement >= accessboundsmap[owner]->get_membase() ||
effective_dispacement < accessboundsmap[owner]->get_membound()))
{
std::cout << "Boundover accessed by " << owner << " in store_arr_bnd_chk, at "
<< std::hex << addr << std::dec << '\n';
#ifdef OVERFLOW
PIN_ExitApplication(1);
#endif
}
}
////////////// load routines //////////////
// mov eax,DWORD PTR [rbp-ptr]
VOID load_ptr_bnd_chk(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg, int64_t ins_size)
{
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
#endif
#ifdef INST_CAT
count_4++;
#endif
// initialize the effective displacement
uint64_t effective_dispacement = 0;
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
if (REG_valid(index_reg))
effective_dispacement = displacement + (PIN_GetContextReg(ctxt, base_reg))
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg);
}
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
if (REG_valid(index_reg))
{ // if index register is present, add it
if (base_reg == REG_RIP)
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale) + ins_size;
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{
// if index register is not present
if (base_reg == REG_RIP)
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement + ins_size;
else
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
}
#ifdef DEBUG
std::cout << "effective_dispacement: " << std::hex << effective_dispacement << std::dec << '\n';
std::cout << "Upper bounds: " << std::hex << accessboundsmap[owner]->get_membound() << std::dec << '\n';
std::cout << "Lower bounds: " << std::hex << accessboundsmap[owner]->get_membase() << std::dec << '\n';
std::cout << "Ub: " << std::hex << accessboundsmap[owner]->get_bound() << std::dec << '\n';
std::cout << "Lb: " << std::hex << accessboundsmap[owner]->get_base() << std::dec << '\n';
std::cout << "in mem_load" << '\n';
#endif
// bound check
if (accessboundsmap[owner]->get_base()==0)
return;
if ((effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound()))
{
std::cout << "Boundover accessed by " << owner << " in load_ptr_bnd_chk, at "
<< std::hex << addr << std::dec << '\n';
#ifdef OVERFLOW
PIN_ExitApplication(1);
#endif
}
}
VOID load_arr_bnd_chk(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg, int64_t ins_size)
{
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
#endif
#ifdef INST_CAT
count_5++;
#endif
// initialize the effective displacement
uint64_t effective_dispacement = 0;
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
if (REG_valid(index_reg))
effective_dispacement = displacement + (PIN_GetContextReg(ctxt, base_reg))
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg);
}
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
if (REG_valid(index_reg))
{ // if index register is present, add it
if (base_reg == REG_RIP)
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale) + ins_size;
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{
// if index register is not present
if (base_reg == REG_RIP)
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement + ins_size;
else
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
}
#ifdef DEBUG
std::cout << "effective_dispacement: " << std::hex << effective_dispacement << std::dec << '\n';
std::cout << "Upper bounds: " << std::hex << accessboundsmap[owner]->get_membound() << std::dec << '\n';
std::cout << "Lower bounds: " << std::hex << accessboundsmap[owner]->get_membase() << std::dec << '\n';
std::cout << "Ub: " << std::hex << accessboundsmap[owner]->get_bound() << std::dec << '\n';
std::cout << "Lb: " << std::hex << accessboundsmap[owner]->get_base() << std::dec << '\n';
std::cout << "in mem_load" << '\n';
#endif
if (effective_dispacement >= accessboundsmap[owner]->get_membase() ||
effective_dispacement < accessboundsmap[owner]->get_membound())
{
std::cout << "Boundover access detected. By " << owner << " in load_arr_bnd_chk, at "
<< std::hex << addr << std::dec << '\n';
#ifdef OVERFLOW
PIN_ExitApplication(1);
#endif
}
}
VOID unknown_loadstore(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, int64_t scale, REG index_reg, REG base_reg, int64_t ins_size)
{
#ifdef DEBUG
std::cout << "ADD: " << std::hex << addr << ", Diss: " << disassins << std::dec << '\n';
std::cout << "disp: " << displacement << '\n';
#endif
#ifdef INST_CAT
count_7++;
#endif
// initialize the effective displacement
uint64_t effective_dispacement = 0;
if (REG_valid(index_reg))
effective_dispacement = displacement + (PIN_GetContextReg(ctxt, base_reg))
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
else
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg);
std::cout << "effective_dispacement: " << std::hex << effective_dispacement << std::dec << '\n';
// std::cout << "i.stack_size: " << i.stack_size << '\n';
// std::cout << "i.parameter: " << i.parameter << '\n';
if ((effective_dispacement > i.parameter) || (effective_dispacement < i.rsp_value+8 && effective_dispacement >= i.rsp_value) || (effective_dispacement < i.stack_size))
{
std::cout << "Boundover access detected. By " << owner << " in unknown_check, at "
<< std::hex << addr << std::dec << '\n';
#ifdef OVERFLOW
PIN_ExitApplication(1);
#endif
}
}
// instruction instrumentation
VOID Instruction(INS ins, VOID *v)
{
// instruction address
ADDRINT insaddress = INS_Address(ins);
// skip if the address is over 0x700000000000
if (insaddress > 0x700000000000)
return;
// First check if the routine is valid
if (!RTN_Valid(RTN_FindByAddress(insaddress)))
return;
// collect function block if valid
if ( blocks.find(RTN_Name(RTN_FindByAddress(insaddress))) == blocks.end())
return;
struct Block *i = blocks[RTN_Name(RTN_FindByAddress(insaddress))];
// check if address is within the boundary
if ((insaddress < i->fun_entry) || (insaddress > i->fun_exit))
return;
// if first instruction, then set the stack
if (insaddress == i->fun_entry)
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)set_stack, IARG_CONTEXT, IARG_PTR, &(*i), IARG_END);
if ((insaddress == i->fun_exit) or INS_IsRet(ins))
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)unset_stack, IARG_CONTEXT, IARG_PTR, &(*i), IARG_END);
// print instruction with address
// std::cout << string(INS_Disassemble(ins)) << " :" << std::hex << insaddress << std::dec << '\n';
// collect owner
std::string owner;
if (i->inscodestack.find(insaddress) == i->inscodestack.end())
return;
else
owner = i->inscodestack[insaddress]->get_owner();
if (owner == "unknown")
#ifdef UNOWNED
{
// instrument store instructions
if (INS_OperandIsMemory(ins, 0))
{
if (REG_StringShort(INS_OperandMemoryBaseReg(ins, 0)) == "rbp" or REG_StringShort(INS_OperandMemoryBaseReg(ins, 0))== "rsp")
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)unknown_loadstore, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 0), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 0)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins), IARG_END);
}
else if (INS_OperandIsMemory(ins, 1))
{
if (REG_StringShort(INS_OperandMemoryBaseReg(ins, 1)) == "rbp" or REG_StringShort(INS_OperandMemoryBaseReg(ins, 1))== "rsp")
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)unknown_loadstore, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 1),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 1), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 1)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 1)),
IARG_UINT32, INS_Size(ins), IARG_END);
}
}
#else
{return;}
#endif
std::string obj;
// int64_t obj_size;
if (i->objinfostack.find(owner) != i->objinfostack.end())
{
obj = i->objinfostack[owner]->get_obj();
// obj_size = i->objinfostack[owner]->get_obj_size();
}
else if(globalobjinfostack.find(owner) != globalobjinfostack.end())
{
obj = globalobjinfostack[owner]->get_obj();
// obj_size = globalobjinfostack[owner]->get_obj_size();
}
else return;
// don't instrument if scalar (todo: this can be removed later)
if (obj == "scalar")
return;
// instrument store instructions
if (INS_OperandIsMemory(ins, 0))
{
// if pointer
if (obj == "PTR")
{
// this pattern make sure that it is not a pointer dereference
REG base_reg = INS_OperandMemoryBaseReg(ins, 0);
if (REG_StringShort(base_reg) == "rbp" or REG_StringShort(base_reg)== "rsp" or REG_StringShort(base_reg)== "rip")
{
if (INS_OperandIsImmediate(ins, 1))
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)store_ptr_xfer_mem, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 0), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 0)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins), IARG_ADDRINT, INS_OperandImmediate(ins, 1), IARG_END);
}
else if (INS_OperandIsReg(ins, 1))
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)store_ptr_xfer_reg, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 0), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 0)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins), IARG_UINT32, REG(INS_OperandReg(ins, 1)), IARG_END);
}
}
else
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)store_ptr_bnd_chk, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 0), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 0)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins), IARG_END);
}
}
else
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)store_arr_bnd_chk, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 0), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 0)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins), IARG_END);
}
}
// instrument load instructions
else if (INS_OperandIsMemory(ins, 1))
{
// if pointer
if (obj == "PTR")
{
REG base_reg = INS_OperandMemoryBaseReg(ins, 1);
if (!(REG_StringShort(base_reg) == "rbp" or REG_StringShort(base_reg)== "rsp" or REG_StringShort(base_reg)== "rip"))
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)load_ptr_bnd_chk, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 1),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 1), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 1)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 1)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
}
else
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)load_arr_bnd_chk, IARG_ADDRINT,
insaddress, IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 1),
IARG_ADDRINT, INS_OperandMemoryScale(ins, 1), IARG_UINT32, REG(INS_OperandMemoryIndexReg(ins, 1)),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 1)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
}
}
// Lazy size allocation
ADDRINT lazyallocatedsize = 0;
VOID malloc_before(char *name, ADDRINT count, ADDRINT size)
{
#ifdef INST_CAT
count_6++;
#endif
// if (name == "malloc" or name == "realloc")
// lazyallocatedsize = size;
// else if (name == "calloc")
lazyallocatedsize = count * size;
}
VOID malloc_after(ADDRINT addrs)
{
#ifdef INST_CAT
count_6++;
#endif
if (addrs == '\0')
{
cerr << "Heap full!\n";
return;
}
if (mallocmap.find(addrs) == mallocmap.end())
{
#ifdef DEBUG
std::cout << "size set: " << lazyallocatedsize << " at address: " << std::hex << addrs << std::dec << '\n';
#endif
mallocmap.insert(std::make_pair(addrs, new MallocMap(addrs, lazyallocatedsize, true)));
}
else
{
#ifdef DEBUG
std::cout << "new size set: " << lazyallocatedsize << " at address: " << std::hex << addrs << std::dec << '\n';
#endif
mallocmap[addrs]->setmem(addrs, lazyallocatedsize, true);
}
lazyallocatedsize = 0;
}
VOID free_before(ADDRINT ret)
{
#ifdef INST_CAT
count_6++;
#endif
std::cout << "returns: " << ret << '\n';
}
// image instrumentation
void Image(IMG img, VOID *v)
{
// instrument main image only
if (IMG_IsMainExecutable(img))
{
// create routines per each user level function
for(std::unordered_map<std::string, struct Block*>::iterator iter = blocks.begin(); iter != blocks.end(); ++iter)
{
std::string k = iter->first;
struct Block* v = iter->second;
RTN_CreateAt(v->fun_entry, v->name);
}
}
// set library path accordingly
// if (IMG_Name(img) == "/lib/x86_64-linux-gnu/libc.so.6")
if (IMG_Name(img).find("libc") != std::string::npos)
{
RTN mallocRtn = RTN_FindByName(img, "malloc");
if (RTN_Valid(mallocRtn))
{
RTN_Open(mallocRtn);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)malloc_before,
IARG_ADDRINT, "malloc",
IARG_ADDRINT, 1,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_END);
RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR)malloc_after,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END);
RTN_Close(mallocRtn);
}
RTN callocRtn = RTN_FindByName(img, "calloc");
if (RTN_Valid(callocRtn))
{
RTN_Open(callocRtn);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(callocRtn, IPOINT_BEFORE, (AFUNPTR)malloc_before,
IARG_ADDRINT, "calloc",
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_END);
RTN_InsertCall(callocRtn, IPOINT_AFTER, (AFUNPTR)malloc_after,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END);
RTN_Close(callocRtn);
}
RTN reallocRtn = RTN_FindByName(img, "realloc");
if (RTN_Valid(reallocRtn))
{
RTN_Open(reallocRtn);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(reallocRtn, IPOINT_BEFORE, (AFUNPTR)malloc_before,
IARG_ADDRINT, "realloc",
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_END);
RTN_InsertCall(reallocRtn, IPOINT_AFTER, (AFUNPTR)malloc_after,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END);
RTN_Close(reallocRtn);
}
RTN freeRtn = RTN_FindByName(img, "free");
if (RTN_Valid(freeRtn))
{
RTN_Open(freeRtn);
// Instrument malloc() to print the input argument value and the return value.
RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR)free_before,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END);
RTN_Close(freeRtn);
}
}
}
// read the input text file
void readInput(const char *filename)
{
std::string line;
std::ifstream myfile(filename);
if (myfile.is_open())
{
// Get total number of functions or blocks
getline (myfile,line);
int64_t count = atoi(line.c_str());
while (count)
{
// Initialize the structure
struct Block *block = new Block;
// for the function name
getline (myfile,line);
block->name = line;
// todo: experimental
block->rbp_value = 0;
block->rsp_value = 0;
// experimental line - to denote addressing mode
// getline (myfile,line);
getline(myfile,line);
// get the function boundary information
block->fun_entry = strtol(line.c_str(), NULL, 16);