-
Notifications
You must be signed in to change notification settings - Fork 10
/
inter.h
304 lines (274 loc) · 9.23 KB
/
inter.h
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
/*
Created by Paul Marinescu and George Candea
Copyright (C) 2009 EPFL (Ecole Polytechnique Federale de Lausanne)
This file is part of LFI (Library-level Fault Injector).
LFI is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
LFI is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with LFI. If not, see http://www.gnu.org/licenses/.
EPFL
Dependable Systems Lab (DSLAB)
Room 330, Station 14
1015 Lausanne
Switzerland
*/
#include <errno.h>
#include <stdint.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <dlfcn.h>
#include <execinfo.h>
#include <time.h>
class Trigger;
/* the maximum number of frames in a stack trace */
#define TRACE_SIZE 100
/* human readable log file (overwritten at each run) */
#define LOGFILE "inject.log"
#define LOGGING 0
/* machine readable log file used for injection replay (overwritten at each run) */
#define REPLAYFILE "replay.xml"
#define MAXINJECT 2000000
/* only used to enhance readbility */
#ifndef __in
#define __in
#endif
#ifndef __out
#define __out
#endif
#ifndef NULL
#define NULL (void*)0
#endif
struct TriggerDesc
{
char id[128];
char tclass[128];
Trigger* trigger;
char init[4096];
};
struct fninfov2
{
char function_name[256];
int return_value;
int errno_value;
int call_original;
int argc;
/* custom triggers */
TriggerDesc **triggers;
};
/* stores the return address across the original library function call
#ifdef __APPLE__
pthread_key_t return_address_key;
#else
static __thread long return_address;
#endif
*/
/* avoid intercepting our function calls
#ifdef __APPLE__
pthread_key_t no_intercept_key;
#else
static __thread int no_intercept;
#endif
*/
long get_return_address();
void set_return_address(long);
long get_no_intercept();
void set_no_intercept(long);
/*
avoid including the standard headers because the compiler will likely
generate warnings when injecting faults in an already declared function.
However, this won't allow us to inject faults in these functions
ALTERNATIVE: use dlsym to get pointers to the functions
*/
extern "C" {
int printf(const char * _Format, ...);
}
void determine_action(struct fninfov2 fn_details[],
__in const char* function_name,
__in void* arg1,
__in void* arg2,
__in void* arg3,
__in void* arg4,
__in void* arg5,
__in void* arg6,
__out int* call_original,
__out int *return_error,
__out int* return_code,
__out int* return_errno);
void print_backtrace(void* bt[], int nptrs, int log_fd);
/************************************************************************/
/* GENERATE_STUBv2 - macro to generate stub functions targeted for x86 */
/* UNSAFE: clobbered non-volatile registeres may not be restored */
/* when `forcing' an exit (i.e. when doing a leave/ret or leave/jmp) */
/* SOLUTION: manually push/pop all non-volatile registers OR */
/* check assembly listing generated by the compiler to determine if */
/* any registers need to be `pop`-ed (per-compiler solution) */
/************************************************************************/
#define GENERATE_STUBv2(FUNCTION_NAME) \
void FUNCTION_NAME (void) \
{ \
int call_original, return_error; \
int return_code, return_errno; \
int initial_no_intercept; \
static void * (*original_fn_ptr)(); \
\
/* defaults */ \
call_original = 1; \
return_error = 0; \
return_code = 0; \
return_errno = 0; \
\
initial_no_intercept = get_no_intercept(); \
if (0 == initial_no_intercept && init_done /* don't hook open or write in the constructor */) { \
set_no_intercept(1); /* allow all determine_action-called functions to pass-through */ \
determine_action(function_info_ ## FUNCTION_NAME, #FUNCTION_NAME, \
0, 0, 0, 0, 0, 0, \
&call_original, &return_error, &return_code, &return_errno); \
} \
\
if(!original_fn_ptr) { \
original_fn_ptr = (void *(*)()) dlsym(RTLD_NEXT, #FUNCTION_NAME); \
if(!original_fn_ptr) \
printf("Unable to get address for function %s\n", #FUNCTION_NAME); \
} \
\
set_no_intercept(initial_no_intercept); \
if (return_error) \
{ \
errno = return_errno; \
__asm__ ("" : : "a"(return_code)); \
return; \
} \
else if (call_original) \
{ \
/* this must correspond to the compiler-generated prologue for this function */ \
__asm__ ("nop" : : "a"(original_fn_ptr)); \
__asm__ ("mov %ebp, %esp"); \
__asm__ ("sub $0x4, %esp"); /* assuming the compiler-generated prologue saves only ebx */ \
__asm__ ("pop %ebx"); \
__asm__ ("pop %ebp"); \
__asm__ ("jmp *%eax"); \
} \
}
struct reg_backup {
void* r15;
void* r14;
void* r13;
void* r12;
void* rdi;
void* rsi;
void* rbx;
void* rcx;
void* rdx;
void* r8;
void* r9;
};
/***********************************************************************/
/* GENERATE_STUB_x64 - macro to generate stub functions on x64 */
/* for Linux/MacOS x86_64 */
/***********************************************************************/
#ifdef __APPLE__
#define GENERATE_STUB_x64(FUNCTION_NAME, SYMBOL_NAME) \
void FUNCTION_NAME (void) __asm__ ( "_" #SYMBOL_NAME ); \
FUNCTION_BODY_x64(FUNCTION_NAME, SYMBOL_NAME)
#else
#define GENERATE_STUB_x64(FUNCTION_NAME, SYMBOL_NAME) \
void FUNCTION_NAME (void) __asm__ ( #SYMBOL_NAME ); \
FUNCTION_BODY_x64(FUNCTION_NAME, SYMBOL_NAME)
#endif
#define FUNCTION_BODY_x64(FUNCTION_NAME, SYMBOL_NAME) \
void FUNCTION_NAME (void) \
{ \
int nptrs; \
void* buffer[TRACE_SIZE]; \
int call_original, return_error; \
int return_code, return_errno; \
reg_backup regs; \
static void * (*original_fn_ptr)(); \
/* we can't call write directly because it would prevent us for injecting faults in `write`
(injecting requires the creation of a function with the same name but the prototype is
different). We, use dlsym instead
*/ \
static int * (*original_write_ptr)(int, const void*, int); \
int initial_no_intercept; \
\
/* save non-volatiles */ \
__asm__ __volatile__ ("movq %%r9, %0" : "=m"(regs.r9) :); \
__asm__ __volatile__ ("movq %%r8, %0" : "=m"(regs.r8) :); \
__asm__ __volatile__ ("movq %%rdx, %0" : "=m"(regs.rdx) :); \
__asm__ __volatile__ ("movq %%rcx, %0" : "=m"(regs.rcx) :); \
/* restore non-volatiles */ \
__asm__ __volatile__ ("movq %%rbx, %0" : "=m"(regs.rbx) :); \
__asm__ __volatile__ ("movq %%rsi, %0" : "=m"(regs.rsi) :); \
__asm__ __volatile__ ("movq %%rdi, %0" : "=m"(regs.rdi) :); \
__asm__ __volatile__ ("movq %%r12, %0" : "=m"(regs.r12) :); \
__asm__ __volatile__ ("movq %%r13, %0" : "=m"(regs.r13) :); \
__asm__ __volatile__ ("movq %%r14, %0" : "=m"(regs.r14) :); \
__asm__ __volatile__ ("movq %%r15, %0" : "=m"(regs.r15) :); \
\
/* defaults */ \
call_original = 1; \
return_error = 0; \
return_code = 0; \
return_errno = 0; \
nptrs = 0; \
/* printf("intercepted %s\n", #FUNCTION_NAME); */ \
\
if (init_done) { \
initial_no_intercept = get_no_intercept(); \
if (0 == initial_no_intercept) { \
set_no_intercept(1); \
determine_action(function_info_ ## FUNCTION_NAME, #FUNCTION_NAME, \
regs.rdi, regs.rsi, regs.rcx, regs.rdx, regs.r8, regs.r9, \
&call_original, &return_error, &return_code, &return_errno); \
} \
} \
\
\
if(!original_fn_ptr) { \
original_fn_ptr = (void *(*)()) dlsym(RTLD_NEXT, #SYMBOL_NAME); \
if(!original_fn_ptr) \
printf("Unable to get address for function %s\n", #SYMBOL_NAME); \
} \
\
if (init_done) \
set_no_intercept(initial_no_intercept); \
\
if (return_error) \
{ \
errno = return_errno; \
__asm__ ("" : : "a"(return_code)); \
return; \
} \
else if (call_original) \
{ \
/* restore function arguments */ \
__asm__ __volatile__ ("movq %0, %%r9" : : "m"(regs.r9)); \
__asm__ __volatile__ ("movq %0, %%r8" : : "m"(regs.r8)); \
__asm__ __volatile__ ("movq %0, %%rdx" : : "m"(regs.rdx)); \
__asm__ __volatile__ ("movq %0, %%rcx" : : "m"(regs.rcx)); \
/* restore non-volatiles */ \
__asm__ __volatile__ ("movq %0, %%rbx" : : "m"(regs.rbx)); \
__asm__ __volatile__ ("movq %0, %%rsi" : : "m"(regs.rsi)); \
__asm__ __volatile__ ("movq %0, %%rdi" : : "m"(regs.rdi)); \
__asm__ __volatile__ ("movq %0, %%r12" : : "m"(regs.r12)); \
__asm__ __volatile__ ("movq %0, %%r13" : : "m"(regs.r13)); \
__asm__ __volatile__ ("movq %0, %%r14" : : "m"(regs.r14)); \
__asm__ __volatile__ ("movq %0, %%r15" : : "m"(regs.r15)); \
\
__asm__ ("leave"); \
__asm__ ("jmp *%%rax" : : "a"(original_fn_ptr)); \
} \
}
#define STUB_VAR_DECL \
int log_fd, replay_fd; \
int init_done;