Line 0
Link Here
|
|
|
1 |
--- injector.c.orig 2017-07-27 22:17:30.000000000 +0300 |
2 |
+++ injector.c 2017-08-01 01:54:04.885858000 +0300 |
3 |
@@ -77,10 +77,24 @@ |
4 |
|
5 |
/* 32 vs 64 */ |
6 |
|
7 |
-#if __x86_64__ |
8 |
- #define IP REG_RIP |
9 |
+#ifdef __linux__ /* Linux specific code. */ |
10 |
+# define PAGE_SIZE 4096 |
11 |
+# define EFL gregs[REG_EFL] |
12 |
+# if __x86_64__ |
13 |
+# define IP gregs[REG_RIP] |
14 |
+# else |
15 |
+# define IP gregs[REG_EIP] |
16 |
+# endif |
17 |
#else |
18 |
- #define IP REG_EIP |
19 |
+# include <pthread_np.h> |
20 |
+ typedef cpuset_t cpu_set_t; |
21 |
+# if __x86_64__ |
22 |
+# define IP mc_rip |
23 |
+# define EFL mc_rflags |
24 |
+# else |
25 |
+# define IP mc_eip |
26 |
+# define EFL mc_eflags |
27 |
+# endif |
28 |
#endif |
29 |
|
30 |
/* leave state as 0 */ |
31 |
@@ -155,7 +169,6 @@ |
32 |
/* x86/64 */ |
33 |
|
34 |
#define UD2_SIZE 2 |
35 |
-#define PAGE_SIZE 4096 |
36 |
#define TF 0x100 |
37 |
|
38 |
/* injection */ |
39 |
@@ -850,7 +863,7 @@ |
40 |
void state_handler(int signum, siginfo_t* si, void* p) |
41 |
{ |
42 |
fault_context=((ucontext_t*)p)->uc_mcontext; |
43 |
- ((ucontext_t*)p)->uc_mcontext.gregs[IP]+=UD2_SIZE; |
44 |
+ ((ucontext_t*)p)->uc_mcontext.IP+=UD2_SIZE; |
45 |
} |
46 |
|
47 |
void fault_handler(int signum, siginfo_t* si, void* p) |
48 |
@@ -863,7 +876,7 @@ |
49 |
|
50 |
/* make an initial estimate on the instruction length from the fault address */ |
51 |
insn_length= |
52 |
- (uintptr_t)uc->uc_mcontext.gregs[IP]-(uintptr_t)packet-preamble_length; |
53 |
+ (uintptr_t)uc->uc_mcontext.IP-(uintptr_t)packet-preamble_length; |
54 |
|
55 |
if (insn_length<0) { |
56 |
insn_length=JMP_LENGTH; |
57 |
@@ -880,9 +893,13 @@ |
58 |
(signum==SIGSEGV||signum==SIGBUS)?(uint32_t)(uintptr_t)si->si_addr:(uint32_t)-1 |
59 |
}; |
60 |
|
61 |
+#ifdef __linux__ /* Linux specific code. */ |
62 |
memcpy(uc->uc_mcontext.gregs, fault_context.gregs, sizeof(fault_context.gregs)); |
63 |
- uc->uc_mcontext.gregs[IP]=(uintptr_t)&resume; |
64 |
- uc->uc_mcontext.gregs[REG_EFL]&=~TF; |
65 |
+#else |
66 |
+ memcpy(&uc->uc_mcontext, &fault_context, sizeof(fault_context)); |
67 |
+#endif |
68 |
+ uc->uc_mcontext.IP=(uintptr_t)&resume; |
69 |
+ uc->uc_mcontext.EFL&=~TF; |
70 |
} |
71 |
|
72 |
void configure_sig_handler(void (*handler)(int, siginfo_t*, void*)) |
73 |
@@ -1341,7 +1358,13 @@ |
74 |
cpu_set_t mask; |
75 |
CPU_ZERO(&mask); |
76 |
CPU_SET(config.core,&mask); |
77 |
- if (sched_setaffinity(0, sizeof(mask), &mask)) { |
78 |
+#ifdef __linux__ /* Linux specific code. */ |
79 |
+ if (sched_setaffinity(0, sizeof(mask), &mask)) |
80 |
+#else |
81 |
+ if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, |
82 |
+ -1, sizeof(mask), &mask)) |
83 |
+#endif |
84 |
+ { |
85 |
printf("error: failed to set cpu\n"); |
86 |
exit(1); |
87 |
} |
88 |
@@ -1439,7 +1462,7 @@ |
89 |
null_p=mmap(0, PAGE_SIZE, PROT_READ|PROT_WRITE, |
90 |
MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
91 |
if (null_p==MAP_FAILED) { |
92 |
- printf("null access requires running as root\n"); |
93 |
+ printf("null access requires running as root, %i\n", errno); |
94 |
exit(1); |
95 |
} |
96 |
} |