This repository was archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcfg_guard_ignore.cpp
More file actions
84 lines (67 loc) · 2.75 KB
/
cfg_guard_ignore.cpp
File metadata and controls
84 lines (67 loc) · 2.75 KB
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
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <intrin.h>
using namespace std;
typedef int (*fnptr)(int);
static int not_entry_point(int arg) {
// nop sled for x86 / x86-64
// these instructions act as a buffer
// for an indirect control flow transfer to skip
// a valid function entry point, but continue
// to execute normal code
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
printf("If CFG is enabled, we should not reach this statement.\n");
printf("CFG ensures control flow only transfers to potentially valid destinations.\n");
printf("In %s: (%d)\n", __FUNCTION__, arg);
// need to exit or the program will segfault anyway,
// since the indirect call skipped the function preamble
exit(arg);
}
// the declspec(guard(ignore)) ensures that CFG is disabled for indirect calls in this function
__declspec(guard(ignore)) static int unsafe_calls(fnptr f) {
printf("CFG is disabled for indirect calls in this function\n");
return f(0);
}
// CFG is enbled for indirect calls in this function
static int safe_calls(fnptr f) {
printf("CFG will protect calls in this function\n");
return f(1);
}
static void usage(const char *app) {
printf("Usage: %s <option>\n", app);
printf("Option values:\n");
printf("\t0\tCall using safe dispatcher (will trigger CFG)\n");
printf("\t1\tCall using CFG-disabled dispatcher (will not trigger CFG)\n");
exit(1);
}
int main(int argc, const char *argv[]) {
// need at least one argument
if( argc != 2 ) {
usage(argv[0]);
}
// and the argument must be '0' or '1'
int idx = argv[1][0] - '0';
if(idx != 0 && idx != 1 ) {
usage(argv[0]);
}
// make sure we get an address that is not valid for CFG
// but still in the nop sled
uintptr_t base = (uintptr_t)not_entry_point;
fnptr fp = (fnptr)(base+0x20);
if(idx == 0) {
// use the CFG enabled dispatcher
safe_calls(fp);
} else {
// use the CFG disabled dispatcher
unsafe_calls(fp);
}
return 0;
}