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
|
#define _GNU_SOURCE
#include <sys/syscall.h>
#include <sched.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/personality.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/resource.h>
#include <malloc.h>
#include "os.h"
#include "signals.h"
#include "emu/x64int_private.h"
#include "bridge.h"
#include "elfloader.h"
#include "env.h"
#include "debug.h"
#include "x64tls.h"
#include "librarian.h"
#include "emu/x64emu_private.h"
int GetTID(void)
{
return syscall(SYS_gettid);
}
int SchedYield(void)
{
return sched_yield();
}
int IsBridgeSignature(char s, char c)
{
return s == 'S' && c == 'C';
}
void EmuInt3(void* emu, void* addr)
{
return x64Int3((x64emu_t*)emu, (uintptr_t*)addr);
}
int IsNativeCall(uintptr_t addr, int is32bits, uintptr_t* calladdress, uint16_t* retn)
{
return isNativeCallInternal(addr, is32bits, calladdress, retn);
}
void* EmuFork(void* emu, int forktype)
{
return x64emu_fork((x64emu_t*)emu, forktype);
}
void EmuX64Syscall(void* emu)
{
x64Syscall((x64emu_t*)emu);
}
void EmuX86Syscall(void* emu)
{
x86Syscall((x64emu_t*)emu);
}
extern int box64_is32bits;
void* GetSeg43Base(void* emu)
{
tlsdatasize_t* ptr = getTLSData((x64emu_t*)emu);
return ptr->data;
}
void* GetSegmentBase(void* emu, uint32_t desc)
{
if (!desc) {
printf_log(LOG_NONE, "Warning, accessing segment NULL\n");
return NULL;
}
int base = desc >> 3;
int is_ldt = !!(desc&4);
base_segment_t* segs = is_ldt?((x64emu_t*)emu)->segldt:((base>5)?((x64emu_t*)emu)->seggdt:my_context->seggdt);
if(!box64_nolibs) {
if (!box64_is32bits && (base == 0x8) )
return GetSeg43Base((x64emu_t*)emu);
if (box64_is32bits && (base == 0x6))
return GetSeg43Base((x64emu_t*)emu);
}
if (base > 15) {
printf_log(LOG_NONE, "Warning, accessing segment unknown 0x%x or unset\n", desc);
return NULL;
}
void* ptr = (void*)segs[base].base;
return ptr;
}
const char* GetBridgeName(void* p)
{
return getBridgeName(p);
}
const char* GetNativeName(void* p)
{
static char buff[500] = { 0 };
{
const char* n = GetBridgeName(p);
if (n)
return n;
}
Dl_info info;
if (dladdr(p, &info) == 0) {
const char* ret = GetNameOffset(my_context->maplib, p);
if (ret)
return ret;
sprintf(buff, "%s(%p)", "???", p);
return buff;
} else {
if (info.dli_sname) {
strcpy(buff, info.dli_sname);
if (info.dli_fname) {
strcat(buff, "(");
strcat(buff, info.dli_fname);
strcat(buff, ")");
}
} else {
sprintf(buff, "%s(%s+%p)", "???", info.dli_fname, (void*)(p - info.dli_fbase));
return buff;
}
}
return buff;
}
void PersonalityAddrLimit32Bit(void)
{
personality(ADDR_LIMIT_32BIT);
#ifndef ANDROID
/*struct rlimit l;
if(getrlimit(RLIMIT_DATA, &l)<0) return; // failed
if(l.rlim_max>3*1024*1024*1024LL) {
l.rlim_cur = 3*1024*1024*1024LL;
setrlimit(RLIMIT_DATA, &l);
}*/
// setting 32bits malloc options
mallopt(M_ARENA_TEST, 2);
mallopt(M_ARENA_MAX, 2);
mallopt(M_MMAP_THRESHOLD, 128*1024);
#endif
}
int IsAddrElfOrFileMapped(uintptr_t addr)
{
return FindElfAddress(my_context, addr) || IsAddrFileMapped(addr, NULL, NULL);
}
void* InternalMmap(void* addr, unsigned long length, int prot, int flags, int fd, ssize_t offset)
{
#if 1 // def STATICBUILD
void* ret = (void*)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
#else
static int grab = 1;
typedef void* (*pFpLiiiL_t)(void*, unsigned long, int, int, int, size_t);
static pFpLiiiL_t libc_mmap64 = NULL;
if (grab) {
libc_mmap64 = dlsym(RTLD_NEXT, "mmap64");
}
void* ret = libc_mmap64(addr, length, prot, flags, fd, offset);
#endif
return ret;
}
int InternalMunmap(void* addr, unsigned long length)
{
#if 1 // def STATICBUILD
int ret = syscall(__NR_munmap, addr, length);
#else
static int grab = 1;
typedef int (*iFpL_t)(void*, unsigned long);
static iFpL_t libc_munmap = NULL;
if (grab) {
libc_munmap = dlsym(RTLD_NEXT, "munmap");
}
int ret = libc_munmap(addr, length);
#endif
return ret;
}
extern FILE* ftrace;
extern char* ftrace_name;
static void checkFtrace()
{
int fd = fileno(ftrace);
if (fd < 0 || lseek(fd, 0, SEEK_CUR) == (off_t)-1) {
ftrace = fopen(ftrace_name, "a");
printf_log(LOG_INFO, "%04d|Recreated trace because fd was invalid\n", GetTID());
}
}
void PrintfFtrace(int prefix, const char* fmt, ...)
{
if (ftrace_name) {
checkFtrace();
}
static const char* names[2] = { "BOX64", "BOX32" };
if (prefix && ftrace == stdout) {
if (prefix > 1) {
fprintf(ftrace, "[\033[31m%s\033[0m] ", names[box64_is32bits]);
} else {
fprintf(ftrace, "[%s] ", names[box64_is32bits]);
}
}
va_list args;
va_start(args, fmt);
vfprintf(ftrace, fmt, args);
fflush(ftrace);
va_end(args);
}
void* GetEnv(const char* name)
{
return getenv(name);
}
int FileExist(const char* filename, int flags)
{
struct stat sb;
if (stat(filename, &sb) == -1)
return 0;
if (flags == -1)
return 1;
// check type of file? should be executable, or folder
if (flags & IS_FILE) {
if (!S_ISREG(sb.st_mode))
return 0;
} else if (!S_ISDIR(sb.st_mode))
return 0;
if (flags & IS_EXECUTABLE) {
if ((sb.st_mode & S_IXUSR) != S_IXUSR)
return 0; // nope
}
return 1;
}
int MakeDir(const char* folder)
{
int ret = mkdir(folder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(!ret || ret==EEXIST)
return 1;
return 0;
}
size_t FileSize(const char* filename)
{
struct stat sb;
if (stat(filename, &sb) == -1)
return 0;
// check type of file? should be executable, or folder
if (!S_ISREG(sb.st_mode))
return 0;
return sb.st_size;
}
|