-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarina.cpp
1338 lines (1275 loc) · 48.6 KB
/
marina.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 <boost/algorithm/string.hpp>
#include <unordered_map>
#include <exception>
// TODOs can be searched with ----> TODO:
// #define RPB_DEBUG // comment out to disable rbp debugging
// #define RBP_DETECTION // comment out to disable rbp detection
// #define DISASS_DEBUG // comment out to disable disassembly debugging
// key to detect the main Routine
static uint32_t key = 0;
// To detect malloc, calloc and realloc
#define MALLOC "malloc"
#define CALLOC "calloc"
#define REALLOC "realloc"
#define FREE "free"
#define FGETS "fgets"
#define GETS "gets"
// Object to store the malloc/ calloc/ realloc information per allocation
class MallocMap
{
ADDRINT size;
bool check;
public:
MallocMap(ADDRINT size, bool check){this->size = size; this->check=check;}
void setsize(ADDRINT size) {this->size = size;}
void setcheck(ADDRINT check) {this->check = check;}
ADDRINT getsize() {return this->size;}
bool getcheck() {return this->check;}
};
// To hold each allocation
map<ADDRINT, MallocMap*> mallocmap;
string ProgramImage;
// Lazy size allocation
ADDRINT lazyallocatedsize = 0;
// Number of input parameters or command line arguments - it is the value in rdi
int64_t in_args = 0;
// Map containing Blocks
// The keys are function name and the values are blocks per function
// std::list<struct Block> Blocks;
std::unordered_map <std::string, struct Block*> blocks;
// access bounds
class AccessBounds
{
private:
uint64_t base;
uint64_t bound;
public:
AccessBounds(uint64_t base, uint64_t bound){this->base = base; this->bound = bound;}
void set_base(uint64_t base){this->base = base;}
void set_bound(uint64_t bound){this->bound = bound;}
void set_bounds(uint64_t base, uint64_t bound){this->base = base; this->bound = bound;}
uint64_t get_base(){return this->base;}
uint64_t get_bound(){return this->bound;}
};
// Map to store all bound information globally
// key: owner
std::unordered_map <std::string, AccessBounds*> accessboundsmap;
// Actual stack (positions related to rbp) hash map
std::unordered_map <uint64_t, std::string> relPosStack;
// contains the information of all the global objects (like data of bss section)
// no need to create a separate namespace for each variable, as the
// variables are represented as funname_variable
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;
// Owner infomation of each location
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;}
};
// 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)
{
std::cout << "obj: " << obj << '\n';
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;}
};
// 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;
// Object information hash map
std::unordered_map <std::string, ObjInfo*> objinfostack;
// static code locations hash map
std::unordered_map <ADDRINT, InsInfo*> inscodestack;
};
// rbp value Check
VOID rpb_check(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins)
{
if (addr > 0x700000000000)
return;
#ifdef DISASS_DEBUG
std::cout<<std::hex<<addr<<"\t"<<disassins<<dec<<std::endl;
#endif
// Set the rbp value for the particular function.
// Check if the rbp value is 0 which is equivalent to either return or unset
// If rbp value is changed other than 0 for the function give an error
if (i.rbp_value == PIN_GetContextReg(ctxt, REG_RBP))
{
#ifdef RPB_DEBUG
std::cout << hex << "rbp: " << i.rbp_value << '\n';
#endif
}
else if (i.rbp_value == 0)
{
#ifdef RPB_DEBUG
std::cout << "return: " << i.rbp_value << '\n';
#endif
}
else
{
std::cout << "RBP is changed(!) to: " << i.rbp_value << '\n';
}
}
// This sets up rbp and rsp values and sets the size of the stack for the corresponding block
VOID reg_val_set(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins)
{
// set the rbp value -- This value will stay same throughout the function
i.rbp_value = PIN_GetContextReg(ctxt, REG_RBP);
// set the rsp value -- This value will stay same throughout the function
i.rsp_value = PIN_GetContextReg(ctxt, REG_RSP);
}
// This is needed to check the array bounds
// mov DWORD PTR [rbp-0x20],0x1
// mov DWORD PTR [rbp-0xc],eax
VOID mov_immediate(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)
{
// effective address the instruction is referring to
uint64_t effective_dispacement = 0;
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
// Effective address = Displacement + BaseReg + IndexReg * Scale
if (REG_valid(index_reg))
{
// if index register is present, add it
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{
// if index register is not present
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
// add the effective dispacement into the global map
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
std::cout << i.objinfostack[owner] << '\n';
if (i.objinfostack[owner]->get_obj() == "array" ||
i.objinfostack[owner]->get_obj() == "scalar")
{
// Save the upper and lower bounds
// For example, it can be an array or a scalar
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(i.objinfostack[owner]->get_lb() +
i.rbp_value, i.objinfostack[owner]->get_ub() + i.rbp_value)));
// std::cout << "Upper bounds: " << accessboundsmap[owner]->get_bound() << '\n';
// std::cout << "Lower bounds: " << accessboundsmap[owner]->get_base() << '\n';
// If the type is array and the access is not within the bounds
// If rsp is to be detected and rsp + x is equivalent to ebp - (rsp + x)
if ((effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound()) &&
(i.objinfostack[owner]->get_obj() == "array"))
{
std::cout << "Boundover accessed by " << owner << '\n';
std::exit(1);
}
}
}
else
std::cout << "check your input!" << '\n';
}
// mov DWORD PTR [rax],edi
// mov immediate without rbp
// mov DWORD PTR [rax],0x4
// This is needed for the heap_pointer
VOID mov_immediate2(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, int64_t displacement, REG base_reg, int64_t ins_size)
{
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
// TODO: let's see if the index and scale is needed here
uint64_t effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg);
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
if (i.objinfostack[owner]->get_obj() == "pointer")
{
if (accessboundsmap.find(owner) != accessboundsmap.end())
{
if ((effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound()))
{
std::cout << "Boundover accessed by " << owner << '\n';
std::exit(1);
}
}
else
{
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(i.objinfostack[owner]->get_lb() +
i.rbp_value, i.objinfostack[owner]->get_ub() + i.rbp_value)));
}
}
else if (globalobjinfostack[owner]->get_obj() == "scalar" || globalobjinfostack[owner]->get_obj() == "array")
{
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(i.objinfostack[owner]->get_lb() +
i.rbp_value, i.objinfostack[owner]->get_ub() + i.rbp_value)));
}
}
// for rip addressing mode
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
// instruction size is needed for rip relative addressing
uint64_t effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg) + ins_size;
// set the owner information on the stack
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
if (globalobjinfostack[owner]->get_obj() == "pointer")
{
if (accessboundsmap.find(owner) != accessboundsmap.end())
{
if ((effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound()))
{
std::cout << "Boundover accessed by " << owner << '\n';
std::exit(1);
}
}
else
{
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(globalobjinfostack[owner]->get_lb(),
globalobjinfostack[owner]->get_ub())));
}
}
else if (globalobjinfostack[owner]->get_obj() == "scalar" || globalobjinfostack[owner]->get_obj() == "array")
{
if (accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(globalobjinfostack[owner]->get_lb(),
globalobjinfostack[owner]->get_ub())));
if ((effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound()) &&
(globalobjinfostack[owner]->get_obj() == "array"))
{
std::cout << "Boundover accessed by " << owner << '\n';
std::exit(1);
}
}
}
}
VOID rsi_check(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,
REG reg, int64_t ins_size)
{
std::cout << "rsi: " << PIN_GetContextReg(ctxt, reg) << '\n';
uint64_t effective_dispacement = 0;
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
// Effective address = Displacement + BaseReg + IndexReg * Scale
if (REG_valid(index_reg))
{ // if index register is present, add it
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{ // if index register is not present
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
// set the owner information on the stack
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
if (i.objinfostack[owner]->get_obj() == "pointer")
{
if (relPosStack.find(PIN_GetContextReg(ctxt, reg)) == relPosStack.end())
{
// This is different from effective_dispacement
effective_dispacement = PIN_GetContextReg(ctxt, reg);
if (accessboundsmap.find(owner) == accessboundsmap.end())
{
accessboundsmap.insert(std::make_pair(owner,
new AccessBounds(effective_dispacement + in_args * 8,
effective_dispacement)));
std::cout << "in!" << in_args << '\n';
// std::cout << "Malloc Upper bounds: " << accessboundsmap[owner]->get_bound() << '\n';
// std::cout << "Malloc Lower bounds: " << accessboundsmap[owner]->get_base() << '\n';
}
}
}
}
}
VOID rdi_check(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,
REG reg, int64_t ins_size)
{
in_args = PIN_GetContextReg(ctxt, reg);
}
// mov QWORD PTR [rbp-0x8],rax
// mov DWORD PTR [rbp-0x40],rsi
VOID mov_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,
REG reg, int64_t ins_size)
{
// effective address the instruction is referring to
uint64_t effective_dispacement = 0;
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
// Effective address = Displacement + BaseReg + IndexReg * Scale
if (REG_valid(index_reg))
{ // if index register is present, add it
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg)
+ (PIN_GetContextReg(ctxt, index_reg) * scale);
}
else
{ // if index register is not present
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement;
}
// set the owner information on the stack
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
// Check to see if the owner is a pointer
if (i.objinfostack[owner]->get_obj() == "pointer")
{
// get the Register value
// pointer is getting the address of owner_prop and hence its bounds
if (relPosStack.find(PIN_GetContextReg(ctxt, reg)) == relPosStack.end())
{
// This is different from effective_dispacement
effective_dispacement = PIN_GetContextReg(ctxt, reg);
if(mallocmap.find(effective_dispacement) != mallocmap.end())
{
if (accessboundsmap.find(owner) == accessboundsmap.end())
{
accessboundsmap.insert(std::make_pair(owner,
new AccessBounds(mallocmap[effective_dispacement]->getsize() + effective_dispacement,
effective_dispacement)));
// std::cout << "Malloc Upper bounds: " << accessboundsmap[owner]->get_bound() << '\n';
// std::cout << "Malloc Lower bounds: " << accessboundsmap[owner]->get_base() << '\n';
}
else
{
accessboundsmap[owner]->set_bounds(mallocmap[effective_dispacement]->getsize()
+ effective_dispacement, effective_dispacement);
}
}
else
{
// TODO:: remove if not required
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
}
else
{
std::string owner_prop = relPosStack[PIN_GetContextReg(ctxt, reg)];
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(accessboundsmap[owner_prop]->get_base(),
accessboundsmap[owner_prop]->get_bound())));
else
accessboundsmap[owner]->set_bounds(accessboundsmap[owner_prop]->get_base(),
accessboundsmap[owner_prop]->get_bound());
// std::cout << "lower bounds: " << accessboundsmap[owner]->get_base() <<'\n';
// std::cout << "Upper bounds: " << accessboundsmap[owner]->get_bound() <<'\n';
}
}
// Only if the owner is an array
if (i.objinfostack[owner]->get_obj() == "array")
{
// first check if the bounds have already been set
// if not, then set the bounds
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(i.objinfostack[owner]->get_lb() +
i.rbp_value, i.objinfostack[owner]->get_ub() + i.rbp_value)));
// Check if the access is within the bounds
if (effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound())
{
std::cout << "Boundover accessed by " << owner << '\n';
std::exit(1);
}
}
}
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
effective_dispacement = PIN_GetContextReg(ctxt, base_reg) + displacement + ins_size;
// set the owner information on the stack
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
if (globalobjinfostack[owner]->get_obj() == "pointer")
{
// get the Register value
// pointer is getting the address of owner_prop and hence its bounds
if (relPosStack.find(PIN_GetContextReg(ctxt, reg)) == relPosStack.end())
{
// This is different from effective_dispacement
effective_dispacement = PIN_GetContextReg(ctxt, reg);
if(mallocmap.find(effective_dispacement) != mallocmap.end())
{
if (accessboundsmap.find(owner) == accessboundsmap.end())
{
accessboundsmap.insert(std::make_pair(owner,
new AccessBounds(mallocmap[effective_dispacement]->getsize() + effective_dispacement,
effective_dispacement)));
// std::cout << "Malloc Upper bounds: " << accessboundsmap[owner]->get_bound() << '\n';
// std::cout << "Malloc Lower bounds: " << accessboundsmap[owner]->get_base() << '\n';
}
else
{
accessboundsmap[owner]->set_bounds(mallocmap[effective_dispacement]->getsize()
+ effective_dispacement, effective_dispacement);
}
}
else
{
// TODO:: remove if not required
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
}
else
{
std::string owner_prop = relPosStack[PIN_GetContextReg(ctxt, reg)];
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(accessboundsmap[owner_prop]->get_base(),
accessboundsmap[owner_prop]->get_bound())));
else
accessboundsmap[owner]->set_bounds(accessboundsmap[owner_prop]->get_base(),
accessboundsmap[owner_prop]->get_bound());
// std::cout << "lower bounds: " << accessboundsmap[owner]->get_base() <<'\n';
// std::cout << "Upper bounds: " << accessboundsmap[owner]->get_bound() <<'\n';
}
}
// Only if the owner is an array
if (globalobjinfostack[owner]->get_obj() == "array")
{
// first check if the bounds have already been set
// if not, then set the bounds
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(globalobjinfostack[owner]->get_lb(),
globalobjinfostack[owner]->get_ub())));
// Check if the access is within the bounds
if (effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound())
{
std::cout << "Boundover accessed by " << owner << '\n';
std::exit(1);
}
}
}
}
// mov eax,DWORD PTR [rax+0x28]
// mov eax,DWORD PTR [rbp+0x0]
VOID mov_mem_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)
{
// set 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);
}
if (i.objinfostack[owner]->get_obj() == "pointer"
|| i.objinfostack[owner]->get_obj() == "array")
{
std::cout << "di " << disassins << '\n';
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(i.objinfostack[owner]->get_lb() +
i.rbp_value, i.objinfostack[owner]->get_ub() + i.rbp_value)));
std::cout << "Base: " << accessboundsmap[owner]->get_base() << '\n';
std::cout << "Bound: " << accessboundsmap[owner]->get_bound() << '\n';
std::cout << "effective_dispacement: " << effective_dispacement << '\n';
if (effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound())
{
std::cout << "Boundover access detected. By " << owner << '\n';
std::exit(1);
}
}
}
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
effective_dispacement = displacement + PIN_GetContextReg(ctxt, base_reg) + ins_size;
if (globalobjinfostack[owner]->get_obj() == "pointer"
|| globalobjinfostack[owner]->get_obj() == "array")
{
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(globalobjinfostack[owner]->get_lb(),
globalobjinfostack[owner]->get_ub())));
// std::cout << "Base: " << accessboundsmap[owner]->get_base() << '\n';
// std::cout << "Bound: " << accessboundsmap[owner]->get_bound() << '\n';
if (effective_dispacement >= accessboundsmap[owner]->get_base() ||
effective_dispacement < accessboundsmap[owner]->get_bound())
{
std::cout << "Boundover access detected. By " << owner << '\n';
std::exit(1);
}
}
}
}
VOID lea_arr(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins,
std::string owner, REG reg, int64_t ins_size)
{
uint64_t effective_dispacement = 0;
if (i.objinfostack.find(owner) != i.objinfostack.end())
{
if (i.objinfostack[owner]->get_obj() == "array")
{
effective_dispacement = PIN_GetContextReg(ctxt, reg);
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(i.objinfostack[owner]->get_lb() +
i.rbp_value, i.objinfostack[owner]->get_ub() + i.rbp_value)));
}
}
else if (globalobjinfostack.find(owner) != globalobjinfostack.end())
{
if (globalobjinfostack[owner]->get_obj() == "array")
{
effective_dispacement = PIN_GetContextReg(ctxt, reg);
if(relPosStack.find(effective_dispacement) == relPosStack.end())
{
// set the owner
// There is no need of the absolute value, as the stack can grow both ways
relPosStack.insert(std::make_pair(effective_dispacement, owner));
}
if(accessboundsmap.find(owner) == accessboundsmap.end())
accessboundsmap.insert(std::make_pair(owner, new AccessBounds(globalobjinfostack[owner]->get_lb(),
globalobjinfostack[owner]->get_ub())));
}
}
}
// set the value of rbp after detecting using sub rsp, xx instruction
VOID rbp_set(uint64_t addr, CONTEXT * ctxt, Block &i, std::string disassins)
{
#ifdef RPB_DEBUG
std::cout << "rbp set: " << i.rbp_value << '\n';
std::cout << "rbp_routine name: " << i.name << '\n';
#endif
// set the rbp value -- This value will stay same throughout the function
i.rbp_value = PIN_GetContextReg(ctxt, REG_RBP);
// #ifdef RPB_DEBUG
std::cout << hex << "rbp set: " << i.rbp_value << dec << '\n';
// #endif
}
// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
// Functin variables
std::string opbasereg = "";
// First check if the routine is valid
if (!RTN_Valid(RTN_FindByAddress(INS_Address(ins))))
return;
// skip if the address is over 0x700000000000
if (INS_Address(ins) > 0x700000000000)
return;
// Block hash map access
// Find the current routine
if ( blocks.find(RTN_Name(RTN_FindByAddress(INS_Address(ins)))) == blocks.end())
return;
struct Block *i = blocks[RTN_Name(RTN_FindByAddress(INS_Address(ins)))];
/* set rbp and rsp values */
if (INS_IsCall(ins))
std::cout << "disas: " << INS_Disassemble(ins) << '\n';
// mov rbp, rsp
// This is so that, a function can be detected
// If the below insturction is not detected, rsp and rbp will remain same
// Detect sub rbp, rsp instruction - another way
// if (REG_is_stackptr_type(INS_OperandReg(ins, 0)))
if (INS_Opcode(ins) == XED_ICLASS_MOV && (INS_OperandReg(ins,0) == REG_RBP)
&& (INS_OperandReg(ins,1) == REG_RSP))
{
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR)reg_val_set, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)), IARG_END);
}
if((INS_Opcode(ins) == XED_ICLASS_ADD || INS_Opcode(ins) == XED_ICLASS_SUB)
&&(INS_OperandIsImmediate(ins, 1)) && (INS_OperandReg(ins,0) == REG_RSP))
{
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR)reg_val_set, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)), IARG_END);
}
/* END */
/* Get the owner */
// Get the owner for the particular static address
std::string owner;
if ( i->inscodestack.find(INS_Address(ins)) == i->inscodestack.end())
{
// An exeption can be called here
return;
}
else
{
owner = i->inscodestack[INS_Address(ins)]->get_owner();
}
/* END */
// Returns the name of the block
// std::cout << "RTN: " << RTN_Name(RTN_FindByAddress(INS_Address(ins))) << '\n';
#ifdef RPB_DEBUG
std::cout << "rbp: " << i->rbp_value << '\n';
#endif
#ifdef RBP_DETECTION
// Detect the mov rbp, rsp instruction
// This is so that, a function can be detected
if (INS_Opcode(ins) == XED_ICLASS_MOV && (INS_OperandReg(ins,0) == REG_RBP))
{
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR)rbp_set, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)), IARG_END);
}
#endif
// Detect the return instruction
// For rbp detection
// This can be done after the end of a particular function - but not needed as the
// values of rbp and rsp are specific to the function and not global
#ifdef RBP_DETECTION
if (INS_IsRet(ins))
{
#ifdef RPB_DEBUG
std::cout << "Return instruction detected" << '\n';
#endif
// Make rbp 0 before each return
i->rbp_value = 0;
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)rpb_check, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)), IARG_END);
}
#endif
/**** Detection of instructions starts here ****/
// Detect all memory store instructions (check only fro rbp and not rsp)
// Array bounds check - not related to the softbounds technique
// But it protects the array overflow
// DWORD PTR [rbp-0x20],0x1
if (((INS_Opcode(ins) == XED_ICLASS_MOV) || (INS_Opcode(ins) == XED_ICLASS_MOVSS))
&& INS_OperandIsMemory(ins, 0)
&& ((INS_OperandWidth(ins, 0) == 8)
|| (INS_OperandWidth(ins, 0) == 16)
|| (INS_OperandWidth(ins, 0) == 32)
|| (INS_OperandWidth(ins, 0) == 64))
&& INS_OperandIsImmediate(ins, 1))
{
// Check if the rbp is not changed
#ifdef RBP_DETECTION
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)rpb_check, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_END);
#endif
if (REG_StringShort(REG_FullRegName(INS_OperandMemoryBaseReg(ins, 0))) == "rbp")
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_immediate, IARG_ADDRINT,
INS_Address(ins), 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_END);
}
else
{
// DWORD PTR [rax],0x4
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_immediate2, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner),
IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
}
// mov QWORD PTR [rbp-0x8],rax
// mov DWORD PTR [rax],rax
// If operand is a register and operand width is 64
if (((INS_Opcode(ins) == XED_ICLASS_MOV) || (INS_Opcode(ins) == XED_ICLASS_MOVSS))
&& INS_OperandIsMemory(ins, 0)
&& (INS_OperandWidth(ins, 0) == 64)
&& INS_OperandIsReg(ins, 1)
&& REG_StringShort(REG_FullRegName(INS_OperandReg(ins, 1))) != "rsi")
{
std::cout << "dis" << '\n';
if (REG_StringShort(REG_FullRegName(INS_OperandMemoryBaseReg(ins, 0))) == "rbp")
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_reg, IARG_ADDRINT,
INS_Address(ins), 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, REG(INS_OperandReg(ins, 1)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
else
{
// mov DWORD PTR [rax],eax
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_reg, IARG_ADDRINT,
INS_Address(ins), 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, REG(INS_OperandReg(ins, 1)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
}
// checks for rsi
if (((INS_Opcode(ins) == XED_ICLASS_MOV) || (INS_Opcode(ins) == XED_ICLASS_MOVSS))
&& INS_OperandIsMemory(ins, 0)
&& (INS_OperandWidth(ins, 0) == 64)
&& INS_OperandIsReg(ins, 1)
&& REG_StringShort(REG_FullRegName(INS_OperandReg(ins, 1))) == "rsi")
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)rsi_check, IARG_ADDRINT,
INS_Address(ins), 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, REG(INS_OperandReg(ins, 1)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
// checks for rdi
// mov DWORD PTR [rbp-0xc],eax
// mov DWORD PTR [rax],eax
if (((INS_Opcode(ins) == XED_ICLASS_MOV) || (INS_Opcode(ins) == XED_ICLASS_MOVSS))
&& INS_OperandIsMemory(ins, 0)
&& ((INS_OperandWidth(ins, 0) == 8) || (INS_OperandWidth(ins, 0) == 16)
|| (INS_OperandWidth(ins, 0) == 32))
&& INS_OperandIsReg(ins, 1)
&& REG_StringShort(REG_FullRegName(INS_OperandReg(ins, 1))) == "rdi")
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)rdi_check, IARG_ADDRINT,
INS_Address(ins), 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, REG_FullRegName(REG(INS_OperandReg(ins, 1))),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
// mov DWORD PTR [rbp-0xc],eax
// mov DWORD PTR [rax],eax
if (((INS_Opcode(ins) == XED_ICLASS_MOV) || (INS_Opcode(ins) == XED_ICLASS_MOVSS))
&& INS_OperandIsMemory(ins, 0)
&& ((INS_OperandWidth(ins, 0) == 8) || (INS_OperandWidth(ins, 0) == 16)
|| (INS_OperandWidth(ins, 0) == 32))
&& INS_OperandIsReg(ins, 1)
&& REG_StringShort(REG_FullRegName(INS_OperandReg(ins, 1))) != "rdi")
{
if (REG_StringShort(REG_FullRegName(INS_OperandMemoryBaseReg(ins, 0))) == "rbp")
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_immediate, IARG_ADDRINT,
INS_Address(ins), 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_END);
}
else
{
// DWORD PTR [rax],0x4
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_immediate2, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner),
IARG_ADDRINT, INS_OperandMemoryDisplacement(ins, 0),
IARG_UINT32, REG(INS_OperandMemoryBaseReg(ins, 0)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
}
// This is the actual pointer dereference
// If the owner is an array, then the instruction would generally be like
// mov eax,DWORD PTR [rbp+0x0]
if (((INS_Opcode(ins) == XED_ICLASS_MOV) || (INS_Opcode(ins) == XED_ICLASS_MOVZX))
&& INS_OperandIsMemory(ins, 1)
&& ((INS_OperandWidth(ins, 1) == 8) || (INS_OperandWidth(ins, 1) == 16)
|| (INS_OperandWidth(ins, 1) == 32) || (INS_OperandWidth(ins, 1) == 64)))
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_mem_reg, IARG_ADDRINT,
INS_Address(ins), 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);
}
// TODO: let's see if this instruction has any variations
// mov edi,0x4
// if ((INS_Opcode(ins) == XED_ICLASS_MOV) &&
// REG_FullRegName(INS_OperandReg(ins, 0)) == REG_RDI)
// {
// INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_mal_size, IARG_ADDRINT,
// INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
// IARG_PTR, new string(owner),
// IARG_ADDRINT, INS_OperandImmediate(ins, 1),
// IARG_UINT32, REG(INS_OperandReg(ins, 1)), IARG_END);
// }
if (INS_Opcode(ins) == XED_ICLASS_LEA)
{
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR)lea_arr, IARG_ADDRINT,
INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
IARG_PTR, new string(owner), IARG_UINT32, REG(INS_OperandReg(ins, 0)),
IARG_UINT32, INS_Size(ins),
IARG_END);
}
// // mov esi,0x2c
// if ((INS_Opcode(ins) == XED_ICLASS_MOV) &&
// REG_FullRegName(INS_OperandReg(ins, 0)) == REG_RSI &&
// i->objinfostack[owner]->get_obj() == "heap_pointer")
// {
// return;
// INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_mal_size, IARG_ADDRINT,
// INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
// IARG_PTR, new string(owner),
// IARG_ADDRINT, INS_OperandImmediate(ins, 1),
// IARG_UINT32, REG(INS_OperandReg(ins, 1)), IARG_END);
// }
std::cout << "disas: " << INS_Disassemble(ins) << '\n';
// Detect the instructions like
// mov DWORD PTR [rbp-0xc],eax |or| mov esi,eax
// if ((INS_Opcode(ins) == XED_ICLASS_MOV)
// && (INS_OperandIsMemory(ins, 0)
// || (REG_StringShort(REG_FullRegName(INS_OperandReg(ins, 0))) == "rsi")
// || (REG_StringShort(REG_FullRegName(INS_OperandReg(ins, 0))) == "rdi"))
// && (INS_OperandIsReg(ins, 1)))
// {
// INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)mov_mem_reg_3, IARG_ADDRINT,
// INS_Address(ins), IARG_CONTEXT, IARG_PTR, &(*i), IARG_PTR, new string(INS_Disassemble(ins)),
// IARG_PTR, new string(owner), IARG_ADDRINT, INS_MemoryDisplacement(ins), IARG_UINT32, REG(INS_OperandReg(ins, 1)), IARG_END);
// }
// if (i->objinfostack[owner]->get_obj() == "pointer")
// {
// std::cout << hex << INS_Disassemble(ins) << dec << '\n';
// // if ((INS_Opcode(ins) == XED_ICLASS_MOV) && (INS_OperandIsReg(ins, 1)))
// }
}
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
}
INT32 Usage()
{
cerr << "This tool counts the number of dynamic instructions executed" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
void readInput(char *filename)
{
std::string line;
std::ifstream myfile(filename);
if (myfile.is_open())
{
// Get the count of the total number of 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);