-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps.c
372 lines (311 loc) · 10.7 KB
/
maps.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
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <glib.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include "sysfuzz.h"
#include "typelib.h"
#include "maps.h"
gboolean maps_contains_address(GSList *maps, guintptr address)
{
while (maps) {
struct map *map = maps->data;
maps = maps->next;
if (map->start <= address && address <= map->end) {
g_debug("found %#lx - %#lx, %c%c%c%c",
map->start,
map->end,
map->perms.r,
map->perms.w,
map->perms.x,
map->perms.p);
return true;
}
}
return false;
}
static gint maps_full_compare(gconstpointer a, gconstpointer b)
{
const struct map *x = a;
const struct map *y = b;
if (x->start != y->start
|| x->end != y->end
|| x->perms.r != y->perms.r
|| x->perms.w != y->perms.w
|| x->perms.x != y->perms.x
|| x->perms.p != y->perms.p
|| x->offset != y->offset
|| x->device.major != y->device.major
|| x->device.minor != y->device.minor
|| x->inode != y->inode
|| g_strcmp0(x->pathname, y->pathname) != 0)
return 1;
return 0;
}
#if 0
static gint maps_addr_compare(gconstpointer a, gconstpointer b)
{
return ((const struct map *)(a))->start
- ((const struct map *)(b))->start;
}
#endif
void maps_print_diff(GSList *before, GSList *after)
{
GSList *i;
// Find the changes / deletions
for (i = before; i; i = i->next) {
struct map *m = i->data;
if (!g_slist_find_custom(after,
i->data,
maps_full_compare)) {
g_debug("Map was modified or removed: %#" G_GINTPTR_MODIFIER "x-%#" G_GINTPTR_MODIFIER "x", m->start, m->end);
}
}
// Find the additions
for (i = after; i; i = i->next) {
struct map *m = i->data;
if (!g_slist_find_custom(before,
i->data,
maps_full_compare)) {
g_debug("Map was added or split: %#" G_GINTPTR_MODIFIER "x-%#" G_GINTPTR_MODIFIER "x", m->start, m->end);
}
}
return;
}
gboolean maps_sanity_check(GSList *maps)
{
GSList *node;
// 1. Check each map makes sense.
for (node = maps; node; node = node->next) {
struct map *map = node->data;
// Page aligned.
g_assert_cmpint(map->start & (PAGE_SIZE - 1), ==, 0);
g_assert_cmpint(map->end & (PAGE_SIZE - 1), ==, 0);
// In userspace.
g_assert_cmpint(map->start, <=, 0xC0000000);
g_assert_cmpint(map->end, <=, 0xC0000000);
// Not at NULL (mmap_min_addr)
g_assert_cmpint(map->start, >, 0);
// Size makes sense.
g_assert_cmpint(map->start, <, map->end);
g_assert_cmpint(map->end, >, map->start);
}
// doesnt overlap, etc.
return true;
}
void maps_destroy_list(GSList *maps)
{
GSList *node;
// Destroy the elements;
for (node = maps; node; node = node->next) {
g_free(node->data);
}
// Clean up the list.
g_slist_free(maps);
return;
}
gchar * maps_get_entry(guintptr address)
{
gchar *contents = NULL;
gchar **split = NULL;
gchar *entry = NULL;
guint i;
// Read /proc/self/maps.
if (g_file_get_contents("/proc/self/maps", &contents, NULL, NULL) == false) {
g_critical("failed to read maps file");
}
// Split into individual maps.
if ((split = g_strsplit(contents, "\n", -1)) == NULL) {
g_critical("failed to split maps contents");
}
// Parse each split line.
for (i = 0; i < g_strv_length(split); i++) {
guintptr start = 0;
guintptr end = 0;
// XXX: Note that it's possible for an address to appear more than
// once in maps,as it's possible to get duplicate zero length
// maps.
// Parse map.
if (sscanf(split[i], "%" G_GINTPTR_MODIFIER "x-%" G_GINTPTR_MODIFIER "x %*c%*c%*c%*c %*x %*x:%*x %*u %*[^\n]", &start, &end) == 2) {
// Check for match with address.
if (start <= address && end >= address) {
// Looks good, we found a match.
entry = g_strdup(split[i]);
break;
}
} else {
// Malformed.
break;
}
}
// Make sure that worked
if (entry == NULL) {
//g_warning("failed to find vma %#x, this is a bug (split len is %u)", address, g_strv_length(split));
//g_debug("------");
//g_debug("%s", contents);
//g_debug("------");
}
// Clean up.
g_free(contents);
g_strfreev(split);
return entry;
}
GSList *maps_take_snapshot(void)
{
gchar *contents = NULL;
gchar **split = NULL;
GSList *snapshot = NULL;
guint i;
// Read /proc/self/maps.
if (g_file_get_contents("/proc/self/maps", &contents, NULL, NULL) == false) {
g_critical("failed to read maps file");
}
// Split into individual maps.
if ((split = g_strsplit(contents, "\n", -1)) == NULL) {
g_critical("failed to split maps contents");
}
// Parse each split line.
for (i = 0; i < g_strv_length(split) - 1; i++) {
struct map *record = g_malloc0(sizeof(struct map) + strlen(split[i]));
// XXX: Note that it's possible for an address to appear more than
// once in maps,as it's possible to get duplicate zero length
// maps.
// Parse map.
// 00654000-00672000 r-xp 00000000 fd:01 19824 /lib/ld-2.12.so
if (sscanf(split[i], "%" G_GINTPTR_MODIFIER "x-%" G_GINTPTR_MODIFIER "x %c%c%c%c %x %hhx:%hhx %u %[^\n]",
&record->start,
&record->end,
&record->perms.r,
&record->perms.w,
&record->perms.x,
&record->perms.p,
&record->offset,
&record->device.major,
&record->device.minor,
&record->inode,
record->pathname) >= 10) {
//g_message("successfully parsed map line %s", split[i]);
// Add to list
snapshot = g_slist_append(snapshot, record);
} else {
//g_message("unable to parse this map line: %s", split[i]);
abort();
}
}
// Clean up.7
g_free(contents);
g_strfreev(split);
return snapshot;
}
void maps_pretty_print_snapshot(GSList *snapshot)
{
while (snapshot) {
struct map *data = snapshot->data;
g_message("%08" G_GINTPTR_MODIFIER "x-%08" G_GINTPTR_MODIFIER "x %c%c%c%c %08x %hhx:%hhx %10u %s",
data->start,
data->end,
data->perms.r,
data->perms.w,
data->perms.x,
data->perms.p,
data->offset,
data->device.major,
data->device.minor,
data->inode,
data->pathname);
snapshot = snapshot->next;
}
}
#ifndef MAP_UNINITIALIZED
# define MAP_UNINITIALIZED 0x4000000
#endif
#ifndef MAP_HUGETLB
# define MAP_HUGETLB 0x40000
#endif
#ifndef MAP_STACK
# define MAP_STACK 0
#endif
// Clean out unrecognised flags to make debugging easier.
guint maps_sanitise_flags(guint flags)
{
return flags & (MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS |
MAP_UNINITIALIZED | MAP_GROWSDOWN | MAP_DENYWRITE |
MAP_EXECUTABLE | MAP_LOCKED | MAP_NORESERVE |
MAP_POPULATE | MAP_NONBLOCK | MAP_STACK | MAP_HUGETLB);
}
void maps_decode_flags(guint flags)
{
g_message("Decoding mmap() flags %#x...", flags);
if (flags & MAP_SHARED) g_message("\tMAP_SHARED");
if (flags & MAP_PRIVATE) g_message("\tMAP_PRIVATE");
if (flags & MAP_FIXED) g_message("\tMAP_FIXED");
if (flags & MAP_ANONYMOUS) g_message("\tMAP_ANONYMOUS");
if (flags & MAP_UNINITIALIZED) g_message("\tMAP_UNINITIALIZED");
if (flags & MAP_GROWSDOWN) g_message("\tMAP_GROWSDOWN");
if (flags & MAP_DENYWRITE) g_message("\tMAP_DENYWRITE");
if (flags & MAP_EXECUTABLE) g_message("\tMAP_EXECUTABLE");
if (flags & MAP_LOCKED) g_message("\tMAP_LOCKED");
if (flags & MAP_NORESERVE) g_message("\tMAP_NORESERVE");
if (flags & MAP_POPULATE) g_message("\tMAP_POPULATE");
if (flags & MAP_NONBLOCK) g_message("\tMAP_NONBLOCK");
if (flags & MAP_STACK) g_message("\tMAP_STACK");
if (flags & MAP_HUGETLB) g_message("\tMAP_HUGETLB");
flags &= ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS |
MAP_UNINITIALIZED | MAP_GROWSDOWN | MAP_DENYWRITE |
MAP_EXECUTABLE | MAP_LOCKED | MAP_NORESERVE |
MAP_POPULATE | MAP_NONBLOCK | MAP_STACK | MAP_HUGETLB);
g_message("Unrecognised flags: %#x", flags);
return;
}
// There must be only one notable difference.
bool maps_compare_snapshots(GSList *before,
GSList *after,
guintptr address,
gsize size,
guint flags,
guint prot,
void *important)
{
GSList *snapshot = maps_take_snapshot();
GSList *i = snapshot;
if (GUINT_TO_POINTER(address) == MAP_FAILED) {
maps_destroy_list(snapshot);
return true;
}
g_assert_cmpint(address & (PAGE_SIZE - 1), ==, 0);
// First, round size up to PAGE_SIZE
size = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
// Adjust address if this grows down.
address = flags & MAP_GROWSDOWN
? address + PAGE_SIZE
: address;
// And adjust the address accordingly.
size = flags & MAP_GROWSDOWN
? size - PAGE_SIZE
: size;
// Scan for this map.
for (i = snapshot; i; i = i->next) {
struct map *data = i->data;
if (address >= data->start && address + size <= data->end) {
if (flags & MAP_HUGETLB) {
g_assert(strstr(data->pathname, "anon_hugepage"));
} else {
g_assert(!strstr(data->pathname, "anon_hugepage"));
}
maps_destroy_list(snapshot);
return true;
}
}
G_BREAKPOINT();
return true;
}