-
Notifications
You must be signed in to change notification settings - Fork 115
/
mem_mgr.cpp
420 lines (395 loc) · 10.4 KB
/
mem_mgr.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
/*
Source for Sk3wlDbg IdaPro plugin
Copyright (c) 2016 Chris Eagle
This program 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 2 of the License, or (at your option)
any later version.
This program 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
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __NT__
#include <unistd.h>
#endif
#include <stdlib.h>
#include <ida.hpp>
#include <kernwin.hpp>
#include "mem_mgr.h"
//#define DEBUG 1
mem_mgr::mem_mgr(uc_engine *uc, uint64_t map_min, uint64_t map_max) {
root = NULL;
this->uc = uc;
set_mmap_region(map_min, map_max);
}
mem_mgr::mem_mgr(uc_engine *uc) {
root = NULL;
this->uc = uc;
map_min = 0;
map_max = 0;
max_block = 0;
}
mem_mgr::~mem_mgr() {
//need to free/unmap all the pages
}
void mem_mgr::set_mmap_region(uint64_t map_min, uint64_t map_max) {
if (map_min > map_max) {
this->map_min = map_max;
this->map_max = map_min;
}
else {
this->map_min = map_min;
this->map_max = map_max;
}
max_block = this->map_max - this->map_min;
}
void mem_mgr::insert(map_block *tree, map_block *node) {
if (root == NULL) {
root = node;
}
else if (node->guest <= tree->guest) {
if (tree->left == NULL) {
tree->left = node;
}
else {
insert(tree->left, node);
}
}
else {
if (tree->right == NULL) {
tree->right = node;
}
else {
insert(tree->right, node);
}
}
}
map_block *mem_mgr::add_block(void *host, uint64_t guest, uint32_t length) {
map_block *m = (map_block*)calloc(sizeof(map_block), 1);
if (m == NULL) {
return NULL;
}
m->host = host;
m->guest = guest;
m->length = length;
insert(root, m);
return m;
}
map_block *mem_mgr::find(map_block *tree, uint64_t addr) {
if (tree == NULL) {
return NULL;
}
if (addr < tree->guest) {
return find(tree->left, addr);
}
else if (addr >= (tree->guest + tree->length)) {
return find(tree->right, addr);
}
return tree;
}
map_block *mem_mgr::find_block(uint64_t addr) {
return find(root, addr);
}
map_block *mem_mgr::next(map_block *tree, uint64_t addr) {
if (tree == NULL) {
return NULL;
}
if (addr < tree->guest) {
//this might be the "next" block, but look for something lower
map_block *b = next(tree->left, addr);
return b ? b : tree;
}
//next must follow this block
return next(tree->right, addr);
}
map_block *mem_mgr::next_block(uint64_t addr) {
return next(root, addr);
}
map_block *mem_mgr::prev(map_block *tree, uint64_t addr) {
if (tree == NULL) {
return NULL;
}
if (addr >= (tree->guest + tree->length)) {
//this might be the "prev" block, but look for something higher
map_block *b = prev(tree->right, addr);
return b ? b : tree;
}
//prev must precede this block
return prev(tree->left, addr);
}
map_block *mem_mgr::prev_block(uint64_t addr) {
return prev(root, addr);
}
void *mem_mgr::to_host_ptr(uint64_t addr) {
map_block *b = find_block(addr);
if (b) {
return (addr - b->guest) + (char*)b->host;
}
return NULL;
}
//node must be non-null and in the tree (returned by find)
void mem_mgr::remove(map_block *tree, map_block *node) {
if (tree == NULL || node == NULL) {
//safety check for invalid args
return;
}
if (node == root) {
if (root->right) {
if (root->left) {
insert(root->right, root->left);
}
root = root->right;
}
else {
root = root->left;
}
}
else if (node->guest > tree->guest) {
if (tree->right == node) {
tree->right = node->left;
if (node->right != NULL) {
insert(tree, node->right);
}
}
else {
remove(tree->right, node);
}
}
else {
if (tree->left == node) {
tree->left = node->left;
if (node->right != NULL) {
insert(tree, node->right);
}
}
else {
remove(tree->left, node);
}
}
node->left = node->right = NULL;
}
/*
//Unicorn permissions are the same as Linux permissions
#define PROT_READ 0x1 // page can be read
#define PROT_WRITE 0x2 // page can be written
#define PROT_EXEC 0x4 // page can be executed
*/
map_block *mem_mgr::mmap(uint64_t addr, uint32_t length, uint32_t perms, uint32_t flags) {
uint64_t guest = 0;
bool found = false;
uint64_t orig = addr;
map_block *b = NULL;
if (length & 0xfff) {
//length must be multiple of page size
#ifdef DEBUG
msg("mmap: non-aligned size\n");
#endif
return NULL;
}
if (addr & 0xfff) {
//addr must be page aligned
#ifdef DEBUG
msg("mmap: non-aligned address\n");
#endif
return NULL;
}
if (length > max_block) {
//out of memory
#ifdef DEBUG
msg("mmap: out of memory\n");
#endif
return NULL;
}
//highest address at which this allocation can be made
uint64_t max_alloc = map_max - length;
if (addr > max_alloc) {
if (flags & SDB_MAP_FIXED) {
//can't do what user wants
#ifdef DEBUG
msg("mmap: address unavailable for fixed allocation\n");
#endif
return NULL;
}
//just use top down allocation
addr = 0;
}
if (addr || (flags & SDB_MAP_FIXED) != 0) {
// if (addr) {
//user wants a specific address
if (addr < map_min) {
addr = map_min;
orig = map_min;
}
map_block *n = next_block(addr);
while (!found && addr >= map_min && addr <= max_alloc) {
b = find_block(addr);
if (b) {
//desired address is already in use
if (flags & SDB_MAP_FIXED) {
return NULL; //can't grant user request
}
//compute the next lower address that might fit the entire request block
addr = b->guest - length;
#ifdef DEBUG
msg("mmap1: addr set to %p\n", (void*)addr);
#endif
}
else if (n == NULL) {
guest = addr;
#ifdef DEBUG
msg("mmap2: guest set to %p\n", (void*)guest);
#endif
found = true;
}
else {
b = prev_block(addr);
if (b) {
uint64_t end = b->guest + b->length;
uint64_t gap = n->guest - addr;
if (gap >= length) {
guest = addr;
#ifdef DEBUG
msg("mmap3: guest set to %p\n", (void*)guest);
#endif
found = true;
}
else {
n = b;
}
}
else {
uint64_t gap = n->guest - addr;
if (gap >= length) {
guest = addr;
#ifdef DEBUG
msg("mmap4: guest set to %p\n", (void*)guest);
#endif
found = true;
}
else {
//hit bottom;
break;
}
}
}
}
addr = orig;
#ifdef DEBUG
msg("mmap5: addr set to %p\n", (void*)addr);
#endif
while (!found && addr <= max_alloc) {
b = find_block(addr);
n = next_block(addr);
if (b) {
addr = b->guest + b->length;
#ifdef DEBUG
msg("mmap6: addr set to %p\n", (void*)addr);
#endif
}
else if (n == NULL) {
guest = addr;
#ifdef DEBUG
msg("mmap7: guest set to %p\n", (void*)guest);
#endif
found = true;
}
else {
uint64_t gap = n->guest - addr;
if (gap >= length) {
guest = addr;
#ifdef DEBUG
msg("mmap8: guest set to %p\n", (void*)guest);
#endif
found = true;
}
else {
addr = n->guest + n->length;
#ifdef DEBUG
msg("mmap9: addr set to %p\n", (void*)addr);
#endif
}
}
}
}
else {
uint64_t top = map_max;
while (1) {
b = prev_block(top);
if (b == NULL) {
if (top >= (length + map_min)) {
guest = top - length;
#ifdef DEBUG
msg("mmap10: guest set to %p\n", (void*)guest);
#endif
found = true;
}
break;
}
else {
if ((top - (b->guest + b->length)) > length) {
//fits in the gap
guest = top - length;
#ifdef DEBUG
msg("mmap11: guest set to %p\n", (void*)guest);
#endif
found = true;
break;
}
top = b->guest;
}
}
}
if (!found) {
return NULL;
}
void *host = calloc(length, 1);
if (host == NULL) {
return NULL;
}
#ifdef DEBUG
msg("add_block(ptr, %p, 0x%x)\n", (void*)guest, (uint32_t)length);
#endif
b = add_block(host, guest, length);
uc_err err = uc_mem_map_ptr(uc, b->guest, b->length, perms, b->host);
#ifdef DEBUG
msg("uc_mem_map_ptr(%p, 0x%x, %d)\n", (void*)b->guest, (uint32_t)b->length, perms);
#endif
if (err != UC_ERR_OK) {
msg("Failed on uc_mem_map_ptr() with error returned %u: %s\n", err, uc_strerror(err));
}
else {
}
return b;
}
void mem_mgr::mprotect(uint64_t addr, uint32_t length, uint32_t perms) {
}
void mem_mgr::munmap(uint64_t addr, uint32_t length) {
uint64_t end = addr + length;
map_block *b = find_block(addr);
if (b) {
uc_mem_unmap(uc, b->guest, b->length);
}
do {
if (b) {
uint64_t bend = b->guest + b->length;
remove(root, b);
if (b->guest < addr) {
add_block(b->host, b->guest, (uint32_t)(addr - b->guest));
}
if (end < bend) {
add_block((end - b->guest) + (char*)b->host, end, (uint32_t)(bend - end));
free(b);
break;
}
if (end == bend) {
free(b);
break;
}
free(b);
}
} while ((b = next_block(addr)) != NULL && end > b->guest);
}