-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlibfi.cpp
629 lines (547 loc) · 17.1 KB
/
libfi.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
/*
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 <iostream>
#include <fstream>
#include <set>
#include <string.h>
#include <assert.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
using namespace std;
#define STUBC ((char *) "intercept.stub.cpp")
#ifdef __APPLE__
#define STUBEX ((char *) "intercept.stub.dylib")
#else
#define STUBEX ((char *) "intercept.stub.so")
#endif
#define LOGFILE "inject.log"
#define REPLAYFILE "replay.xml"
#define CRASH_METRIC (int)1e8
#define FAILURE_METRIC (int)1e6
#define TIME_MULTIPLIER 1
static void
usage(char* me)
{
cout << "Usage: ";
cout << me << " [-t <targetExecutable>] <configurationFile>" << endl;
}
static void
print_attribute_names(xmlAttrPtr a_node, ofstream& out)
{
for (; a_node; a_node = a_node->next) {
out << " " << a_node->name << "=\\\"" << a_node->children->content << "\\\"";
if (a_node->next)
out << " ";
}
}
static void
print_element_names(xmlNodePtr a_node, ofstream& out)
{
xmlNodePtr cur_node = NULL;
xmlChar ch;
int i;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
out << "<" << cur_node->name;
print_attribute_names(cur_node->properties, out);
out << ">";
} else if (cur_node->type == XML_TEXT_NODE) {
i = 0;
while (0 != (ch = cur_node->content[i++]))
{
if (ch == '\r' || ch == '\n')
out << "\\";
out << ch;
}
}
print_element_names(cur_node->children, out);
if (cur_node->type == XML_ELEMENT_NODE) {
out << "</" << cur_node->name << ">";
}
}
}
static void
print_triggers(xmlNodeSetPtr nodes, ofstream& out)
{
xmlNodePtr cur;
xmlChar *triggerId;
xmlChar *triggerClass;
int size;
int i, fn_count;
set<string> functionsUsed;
size = (nodes) ? nodes->nodeNr : 0;
fn_count = 0;
for(i = 0; i < size; ++i)
{
assert(nodes->nodeTab[i]);
cur = nodes->nodeTab[i];
if(cur->type == XML_ELEMENT_NODE)
{
triggerId = xmlGetProp(cur, (xmlChar*)"id");
triggerClass = xmlGetProp(cur, (xmlChar*)"class");
if (triggerId && triggerClass)
{
out << "struct TriggerDesc trigger_" << triggerId << " = { \"" << triggerId << "\", ";
out << "\"" << triggerClass << "\", NULL, ";
xmlFree(triggerId);
xmlFree(triggerClass);
cur = cur->children;
while (cur && xmlStrcmp(cur->name, (const xmlChar *)"args"))
{
cur = cur->next;
}
if (cur)
{
out << "\"";
print_element_names(cur, out);
out << "\"";
} else {
out << "\"\"";
}
out << " };" << endl;
}
}
}
}
static void
print_function(xmlNodePtr fn, int triggerListId, ofstream& out)
{
const char defErrno[] = "0";
const char defCallOriginal[] = "0";
const char defArgc[] = "0";
xmlChar* functionName, *return_value,
*errno_value, *call_original, *argc;
functionName = xmlGetProp(fn, (xmlChar*)"name");
errno_value = xmlGetProp(fn, (xmlChar*)"errno");
return_value = xmlGetProp(fn, (xmlChar*)"retval");
call_original = xmlGetProp(fn, (xmlChar*)"calloriginal");
argc = xmlGetProp(fn, (xmlChar*)"argc");
if (functionName && return_value)
{
out << "\t{ \"" << functionName << "\", ";
out << return_value << ", ";
out << (errno_value ? (char*)errno_value : defErrno) << ", ";
out << (call_original ? (char*)call_original : defCallOriginal) << ", ";
out << (argc ? (char*)argc : defArgc) << ", ";
out << "triggerList_" << triggerListId;
out << " }," << endl;
}
if (functionName)
xmlFree(functionName);
if (errno_value)
xmlFree(errno_value);
if (return_value)
xmlFree(return_value);
if (call_original)
xmlFree(call_original);
if (argc)
xmlFree(argc);
}
static void
print_trigger_list(xmlNodePtr fn, int triggerListId, ofstream& out)
{
xmlNodePtr cur;
xmlChar *triggerId;
cur = fn->children;
out << "TriggerDesc* triggerList_" << triggerListId << "[] = { ";
while (cur)
{
if (cur->type == XML_ELEMENT_NODE &&
0 == xmlStrcmp(cur->name, (const xmlChar *)"triggerx"))
{
triggerId = xmlGetProp(cur, (xmlChar*)"ref");
if (triggerId)
{
out << "&trigger_" << triggerId << ", ";
xmlFree(triggerId);
}
}
cur = cur->next;
}
out << "NULL };" << endl;
}
static void
print_stubs(xmlNodeSetPtr nodes, ofstream& out)
{
xmlNodePtr cur;
xmlChar *functionName;
xmlChar *functionName2;
int size;
int i, j, triggerListId, triggerListIdBase;
set<string> functionsUsed;
size = (nodes) ? nodes->nodeNr : 0;
triggerListId = 1;
for(i = 0; i < size; ++i)
{
assert(nodes->nodeTab[i]);
if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE)
{
cur = nodes->nodeTab[i];
functionName = xmlGetProp(cur, (xmlChar*)"name");
if (!functionName || functionsUsed.find((char*)functionName) != functionsUsed.end())
continue;
functionsUsed.insert((char*)functionName);
triggerListIdBase = triggerListId;
print_trigger_list(cur, triggerListId++, out);
for(j = i+1; j < size; ++j)
{
assert(nodes->nodeTab[j]);
if(nodes->nodeTab[j]->type == XML_ELEMENT_NODE)
{
cur = nodes->nodeTab[j];
functionName2 = xmlGetProp(cur, (xmlChar*)"name");
if (functionName2)
{
if (0 == strcmp((char*)functionName, (char*)functionName2))
{
print_trigger_list(cur, triggerListId++, out);
}
xmlFree(functionName2);
}
}
}
out << "struct fninfov2 function_info_" << functionName << "[] = {\n";
triggerListId = triggerListIdBase;
cur = nodes->nodeTab[i];
print_function(cur, triggerListId++, out);
for(j = i+1; j < size; ++j)
{
assert(nodes->nodeTab[j]);
if(nodes->nodeTab[j]->type == XML_ELEMENT_NODE)
{
cur = nodes->nodeTab[j];
functionName2 = xmlGetProp(cur, (xmlChar*)"name");
if (functionName2)
{
if (0 == strcmp((char*)functionName, (char*)functionName2))
{
print_function(cur, triggerListId++, out);
}
xmlFree(functionName2);
}
}
}
out << "\t{ \"\", 0, 0, 0, 0, NULL }" << endl;
out << "};\n";
xmlFree(functionName);
}
}
ofstream symbols("symbols");
out << "extern \"C\" {" << endl;
set<string> generated_stubs;
for(i = 0; i < size; ++i)
{
assert(nodes->nodeTab[i]);
if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE)
{
cur = nodes->nodeTab[i];
functionName = xmlGetProp(cur, (xmlChar*)"name");
if (functionName && generated_stubs.end() == generated_stubs.find((char*)functionName))
{
xmlChar* aliasName = xmlGetProp(cur, (xmlChar*)"alias");
out << "#ifdef __x86_64__" << endl;
if (aliasName) {
out << "GENERATE_STUB_x64(" << (char*)functionName << ", " << (char*)aliasName << ")" << endl;
symbols << "_" << aliasName << endl;
} else {
out << "GENERATE_STUB_x64(" << (char*)functionName << ", " << (char*)functionName << ")" << endl;
symbols << "_" << functionName << endl;
}
out << "#else" << endl;
out << "GENERATE_STUBv2(" << (char*)functionName << ")" << endl;
out << "#endif" << endl << endl;
generated_stubs.insert((char*)functionName);
xmlFree(functionName);
if (aliasName)
xmlFree(aliasName);
}
}
}
out << "}" << endl;
}
/************************************************************************/
/* int compile_file(char* cfile, char* outfile) */
/* */
/* Compiles cfile (dynamically generated) along with its dependencies */
/* to outfile as a shared object using appropriate flags */
/************************************************************************/
int compile_file(char* cfile, char* outfile)
{
char cmd[1024];
int status;
#ifdef __APPLE__
sprintf(cmd, "g++ -g -o %s %s inter.cpp Trigger.cpp triggers/*.cpp `xml2-config --cflags` `xml2-config --libs` -O0 -shared -Xlinker -exported_symbols_list -Xlinker symbols", outfile, cfile);
#else
sprintf(cmd, "g++ -g -o %s %s inter.cpp Trigger.cpp triggers/*.cpp `xml2-config --cflags` `xml2-config --libs` -O0 -shared -fPIC -lrt -ldl", outfile, cfile);
#endif
// use the following line instead if you need debug information support (after installing libelf, libdwarf and the appropriate trigger)
//sprintf(cmd, "g++ -g -o %s %s inter.cpp Trigger.cpp triggers/*.cpp `xml2-config --cflags` `xml2-config --libs` -O0 -shared -fPIC -lrt -ldl -ldwarf -lelf", outfile, cfile);
cerr << "Compiling stub library " << outfile << " from " << cfile << endl;
cerr << cmd << " ..." << endl;
status = system(cmd);
if (0 != WEXITSTATUS(status))
{
cerr << "Compile failed" << endl;
return -1;
}
else
{
cerr << "Compiled successfully..." << endl;
}
return 0;
}
int generate_stub(char* config)
{
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObjTriggers;
xmlXPathObjectPtr xpathObj;
xmlChar *xpathExpr = (xmlChar*)"//function";
xmlChar *xpathExprTriggers = (xmlChar*)"//trigger";
cerr << "Generating stub file " << STUBC << " from " << config << endl;
/* Print results */
ofstream outf(STUBC);
outf << "#include \"inter.h\"" << endl;
outf << "STUB_VAR_DECL" << endl << endl;
doc = xmlParseFile(config);
if (doc == NULL) {
cerr << "Unable to open " << config << endl;
return -1;
}
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL) {
cerr << "Error: unable to create new XPath context" << endl;
xmlFreeDoc(doc);
return(-1);
}
xpathObjTriggers = xmlXPathEvalExpression(xpathExprTriggers, xpathCtx);
if(xpathObjTriggers == NULL) {
cerr << "Error: unable to evaluate xpath expression \"" << xpathExprTriggers << "\"" << endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return(-1);
}
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
if(xpathObj == NULL) {
cerr << "Error: unable to evaluate xpath expression \"" << xpathExpr << "\"" << endl;
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return(-1);
}
print_triggers(xpathObjTriggers->nodesetval, outf);
print_stubs(xpathObj->nodesetval, outf);
/* Cleanup */
xmlXPathFreeObject(xpathObj);
xmlXPathFreeObject(xpathObjTriggers);
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return compile_file(STUBC, STUBEX);
}
/***************************************************************************/
/* run_subject(int argc, char** argv, */
/* char* preload_library, char *envp[]) */
/* */
/* Runs subject program defined by (argc, argv[]) in the parent's */
/* (our) environment + LD_PRELOAD */
/* */
/* Returns: */
/* -1 - failed to start program */
/* 0 - child program exited normally with a 0 return code */
/* FAILURE_METRIC - child program exited normally with a non 0 */
/* return code */
/* CRASH_METRIC - child program was terminated by a signal */
/***************************************************************************/
int run_subject(int argc, char** argv, char* preload_library, char *envp[])
{
#ifdef __APPLE__
const char *preload = "DYLD_INSERT_LIBRARIES";
const char *apple_env_flat = "DYLD_FORCE_FLAT_NAMESPACE";
const char *apple_no_dyldcache = "DYLD_SHARED_REGION";
/* this needs to be specified explictly in the flat namespace */
const char *apple_explicit_libs = "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontRegistry.dylib";
#else
const char *preload = "LD_PRELOAD";
#endif
char preload_path[1024];
char **newarg;
int i, return_value;
int fd;
pid_t monitor;
int status, exit_status, exit_signal;
struct timeval tvstart, tvend;
key_t key = getpid();
int* runstatus;
int shmid = -1;
if ((shmid = shmget( key, 1024, IPC_CREAT | 0666 )) < 0 )
perror("shmget");
if ((runstatus = (int*)shmat( shmid, NULL, 0 )) == (int*) -1)
perror("shmat");
return_value = CRASH_METRIC;
if (getcwd(preload_path, sizeof(preload_path) - strlen(preload_library) - 1))
{
strcat(preload_path, "/");
strcat(preload_path, preload_library);
#if __APPLE__
strlcat(preload_path, ":", sizeof(preload_path));
strlcat(preload_path, apple_explicit_libs, sizeof(preload_path));
#endif
cerr << "[LFI] Preloading " << preload_path << endl;
newarg = (char**)malloc((argc+1)*sizeof(char*));
for (i = 0; i < argc; ++i)
newarg[i] = argv[i];
newarg[argc] = NULL;
gettimeofday(&tvstart, NULL);
if (0 == (monitor = fork()))
{
#ifdef __APPLE__
setenv(apple_env_flat, "", 1);
setenv(apple_no_dyldcache, "avoid", 1);
#endif
setenv(preload, preload_path, 1);
execv(argv[0], newarg);
*runstatus = 1;
shmdt(runstatus);
_exit(0);
}
else
{
/* monitor */
if (-1 == monitor)
{
cerr << "Unable to fork" << endl;
}
else
{
waitpid(monitor, &status, 0);
gettimeofday(&tvend, NULL);
if (0 != *runstatus)
{
return_value = -1;
}
else
{
if (WIFEXITED(status))
{
exit_status = WEXITSTATUS(status);
cerr << "Process exited normally. Exit status: " << exit_status << endl;
if (exit_status)
{
return_value = exit_status;
}
else
{
/* leave the timing to the external sensor
return_value = (tvend.tv_sec-tvstart.tv_sec)*100 + (tvend.tv_usec - tvstart.tv_usec)/10000;
*/
return_value = 0;
}
}
else if (WIFSIGNALED(status))
{
exit_signal = WTERMSIG(status);
cerr << "Process terminated by signal " << exit_signal << endl;
fd = open(REPLAYFILE, O_WRONLY|O_APPEND);
write(fd, "</plan>\n", 8);
close(fd);
return_value = 128+WTERMSIG(status);
}
}
}
}
}
if (shmdt(runstatus) < 0)
{
perror("shmdt");
}
if ( shmctl( shmid, IPC_RMID, NULL ) < 0 )
{
perror("shmctl");
}
return return_value;
}
int main(int argc, char* argv[], char* envp[])
{
char *crash_create, *run_target, *token;
char *run_argv[64];
int run_argc;
int status, crash_check, test_score;
int c;
crash_check = 0;
run_target = NULL;
opterr = 0;
while ((c = getopt (argc, argv, "t:f:")) != -1)
{
switch (c)
{
case 'f':
crash_check = 1;
crash_create = optarg;
break;
case 't':
run_target = optarg;
break;
case '?':
if (optopt == 'f')
fprintf (stderr, "Option -f requires an argument.\n");
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
abort ();
}
}
if (optind >= argc)
{
usage(argv[0]);
return -1;
}
//++optind
run_argc = 0;
token = strtok(run_target, "\t ");
while (token)
{
run_argv[run_argc++] = token;
token = strtok(NULL, "\t ");
}
LIBXML_TEST_VERSION
status = generate_stub(argv[optind]);
xmlCleanupParser();
test_score = 0;
if (run_target) {
if (0 == status) {
if ((test_score = run_subject(run_argc, run_argv, STUBEX, envp)) < 0)
cerr << "A problem occurred starting the target" << endl;
}
}
return test_score;
}