-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrap.c
412 lines (325 loc) · 9.93 KB
/
wrap.c
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
/*
* Copyright 2012 Iain Bullard <iain.bullard@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Based on code from lima driver wrapper by Luc Verhaegen <libv@codethink.co.uk>
* and libsegfault by Jerome Glisse <j.glisse@gmail.com>
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <dlfcn.h>
#include <string.h>
#include <signal.h>
#include <ucontext.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include "wrap.h"
#include "log.h"
#include "mappings.h"
#include "instructions.h"
#include "libve.h"
#include "wrap_libve.h"
static pthread_mutex_t serializer[1] = { PTHREAD_MUTEX_INITIALIZER };
/*
* functions for locking
*/
static inline void serialized_start(const char *func)
{
pthread_mutex_lock(serializer);
}
static inline void serialized_stop(void)
{
pthread_mutex_unlock(serializer);
}
/*
* Wrap around the libc calls that are crucial for capturing our
* command stream, namely, open, ioctl, and mmap.
*/
static void *libc_dl;
static int initalised = 0;
static instruction_info_t *ins_info;
static int
libc_dlopen(void)
{
libc_dl = dlopen("libc.so.6", RTLD_NOW);
if (!libc_dl) {
printf("Failed to dlopen %s: %s\n",
"libc.so", dlerror());
exit(-1);
}
return 0;
}
static void *
libc_dlsym(const char *name)
{
void *func;
if (!libc_dl)
libc_dlopen();
func = dlsym(libc_dl, name);
if (!func) {
printf("Failed to find %s in %s: %s\n",
name, "libc.so", dlerror());
exit(-1);
}
return func;
}
/*
* wrap open, catch opening /dev/cedar_dev
*/
static int (*orig_open)(const char* path, int mode, ...);
int
open(const char* path, int flags, int mode,...)
{
int ret;
serialized_start(__func__);
if (!orig_open) {
orig_open = libc_dlsym(__func__);
wrap_init();
}
ret = orig_open(path,flags,mode);
if (!strcmp(path, "/dev/cedar_dev")) {
// trace this open call and follow the fd returned by open
mappings_trace_fd(ret);
wrap_log("func\topen\tfd:%d\n",ret);
}
serialized_stop();
return ret;
}
/*
* wrap ioctl
*/
static int (*orig_ioctl)(int fd, unsigned long int request, ...);
int
ioctl(int fd, unsigned long int request, ...)
{
int ret,trace;
serialized_start(__func__);
if (!orig_ioctl)
orig_ioctl = libc_dlsym(__func__);
trace = mappings_is_fd_traced(fd);
va_list args;
void *ptr;
va_start(args, request);
ptr = va_arg(args, void *);
va_end(args);
if (trace == 1) {
ret = orig_ioctl(fd, request, ptr);
wrap_log("func\tioctl\t0x%03x\t0x%08x\t0x%08x\n",request,ptr,ret);
} else {
ret = orig_ioctl(fd, request, ptr);
}
serialized_stop();
return ret;
}
/*
* wrap mmap
*/
void *(*orig_mmap)(void *addr, size_t length, int prot,
int flags, int fd, off_t offset);
void *
mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
void *ret_addr;
int trace = 0;
int regs = 0;
int only_regs = 0;
int id;
serialized_start(__func__);
if (!orig_mmap) {
orig_mmap = libc_dlsym(__func__);
}
/* catch all mmaps against /dev/cedar_dev */
trace = mappings_is_fd_traced(fd);
/* don't trace if its not regs and only_regs is 1 */
if (offset != special_offset && only_regs == 1) {
trace = 0;
}
if (trace == 1) {
/* tracking this mmap call */
if (offset == special_offset) {
regs = 1;
}
ret_addr = orig_mmap(addr, length, PROT_NONE, flags, fd, offset);
mappings_trace_mmap((uint32_t) ret_addr,offset,length,regs);
id = mappings_get_id((uint32_t) ret_addr);
wrap_log("func\tmmap\t%d\taddr:0x%08x\tlen:0x%08x\tflags:%d\tfd:%d\toffset:0x%08x\tto:0x%08x\n",id,addr,length,flags,fd,offset,ret_addr);
} else {
ret_addr = orig_mmap(addr, length, prot, flags, fd, offset);
}
serialized_stop();
return ret_addr;
}
/*
* wrap memcpy
*/
void *(*orig_memcpy)(void *dest, const void *src, size_t n);
void *memcpy(void *dest, const void *src, size_t n)
{
void *result;
uint32_t dest_32 = (uint32_t) dest;
uint32_t src_32 = (uint32_t) src;
int dest_id,src_id,traced;
uint32_t dest_base,dest_phy,src_base,src_phy;
if (!orig_memcpy) {
orig_memcpy = libc_dlsym(__func__);
}
if (initalised == 1) {
dest_id = mappings_get_id(dest_32);
src_id = mappings_get_id(src_32);
if (dest_id != 0 || src_id != 0) {
traced = 1;
if (dest_id != 0) {
//mappings_unprotect_mapping(dest_32);
dest_base = mappings_addr_to_base(dest_32);
dest_phy = mappings_addr_to_phys(dest_32);
} else {
dest_base = 0xFFFFFFFF;
dest_phy = 0xFFFFFFFF;
}
if (src_id != 0 ) {
//mappings_unprotect_mapping(src_32);
src_base = mappings_addr_to_base(src_32);
src_phy = mappings_addr_to_phys(src_32);
} else {
src_base = 0xFFFFFFFF;
src_phy = 0xFFFFFFFF;
}
wrap_log("func\tmemcpy\tsrc\t%d\t0x%08x\t0x%08x\tdest\t%d\t0x%08x\t0x%08x\t0x%08x\n",src_id,src_phy,src_base,dest_id,dest_phy,dest_base,n);
}
}
result = orig_memcpy(dest,src,n);
if (initalised == 1 && traced == 1) {
//hexdump(dest,n);
if (dest_id != 0) {
//mappings_protect_mapping(dest_32);
}
if (src_id != 0) {
// mappings_protect_mapping(src_32);
}
}
return result;
}
/*
* wrap memset
*/
void *(*orig_memset)(void *s, int c, size_t n);
void *memset(void *s, int c, size_t n)
{
void *result;
uint32_t id,dest_addr,dest_base,dest_phy,traced;
dest_addr = (uint32_t) s;
if (!orig_memset) {
orig_memset = libc_dlsym(__func__);
}
if (initalised == 1) {
id = mappings_get_id(dest_addr);
if (id != 0) {
serialized_start(__func__);
traced = 1;
dest_base = mappings_addr_to_base(dest_addr);
dest_phy = mappings_addr_to_phys(dest_addr);
mappings_unprotect_mapping(dest_addr);
wrap_log("func\tmemset\t%d\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n",id,dest_phy,dest_base,c, n);
} else {
traced = 0;
}
}
result = orig_memset(s,c,n);
if (initalised == 1 && traced == 1) {
serialized_stop();
mappings_protect_mapping(dest_addr);
}
return result;
}
/*
* wrap signal
*/
__sighandler_t (*orig_signal)(int signum, __sighandler_t handler);
__sighandler_t signal(int signum, __sighandler_t handler)
{
if (!orig_signal) {
orig_signal = libc_dlsym(__func__);
}
if (signum == SIGSEGV) {
//wrap_log("signal(SIGSEGV, ...) intercepted [%d]\n", getpid());
return 0;
}
return orig_signal(signum,handler);
}
/*
* tracing signal handler for segfaults
*/
void trace_sighandler(int sig, siginfo_t *info, void *ptr)
{
serialized_start(__func__);
int i;
uint32_t fault_address;
uint32_t* instruction_addr;
uint32_t instruction;
uint32_t id,from_base,phys_addr,virt_addr;
ucontext_t *uc = (ucontext_t *)ptr;
fault_address = (uint32_t) info->si_addr;
instruction_addr = (uint32_t*) uc->uc_mcontext.arm_pc;
instruction = (uint32_t) *instruction_addr;
if (!mappings_is_addr_traced(fault_address)) {
wrap_log("trace_sighandler 0x%08x is not an address we are tracing\n",fault_address);
wrap_log("trace_sighandler instruction addr: 0x%08x\n", uc->uc_mcontext.arm_pc);
wrap_log("trace_sighandler\t0x%08x\n",instruction);
serialized_stop();
exit(1);
}
// set number of memory transfers to zero.
ins_info->count = 0;
mappings_unprotect_mapping(fault_address);
handle_instruction(instruction,fault_address,uc,ins_info);
mappings_protect_mapping(fault_address);
for (i=0;i < ins_info->count ; i++ ) {
virt_addr = ins_info->data[i].address;
id = mappings_get_id(virt_addr);
from_base = mappings_addr_to_base(virt_addr);
phys_addr = mappings_addr_to_phys(virt_addr);
wrap_log("mem\t%s\t0x%016llx\t%d\t0x%08x\t0x%08x\t0x%08x\n",ins_info->type,ins_info->data[i].value,id,phys_addr,from_base,virt_addr);
}
// advnace program counter to next instruction
uc->uc_mcontext.arm_pc = uc->uc_mcontext.arm_pc + 4;
serialized_stop();
}
void wrap_init(void)
{
struct sigaction sa;
// init mappings
mappings_init();
// wrap libve
wrap_libve_init();
// install sighandler
sa.sa_sigaction = (void *) trace_sighandler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
ins_info = malloc(sizeof(instruction_info_t));
memset(ins_info,0,sizeof(instruction_info_t));
initalised = 1;
}