forked from advanced-microcode-patching/shiva
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshiva_maps.c
222 lines (204 loc) · 5.18 KB
/
shiva_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
#include "shiva.h"
/*
* Get the base address of the target executable.
* The base address is set in the out argument.
*/
bool
shiva_maps_get_base(struct shiva_ctx *ctx, uint64_t *out)
{
FILE *fp;
char buf[PATH_MAX];
char *p;
fp = fopen("/proc/self/maps", "r");
if (fp == NULL) {
perror("fopen");
return false;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (strstr(buf, ctx->path) == NULL)
continue;
p = strchr(buf, '-');
*p = '\0';
*out = strtoul(buf, NULL, 16);
return true;
}
return false;
}
bool
shiva_maps_get_so_base(struct shiva_ctx *ctx, char *so_path,
uint64_t *out)
{
FILE *fp;
char buf[PATH_MAX];
char *p;
fp = fopen("/proc/self/maps", "r");
if (fp == NULL) {
perror("fopen");
return false;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (strstr(buf, so_path) == NULL)
continue;
if (strstr(buf, "r-xp") == NULL)
continue;
p = strchr(buf, '-');
*p = '\0';
*out = strtoul(buf, NULL, 16);
fclose(fp);
return true;
}
fclose(fp);
return false;
}
/*
* Checking if 'addr' is a valid address mapping.
* It's valid if it exists in /proc/pid/maps, as long as it
* doesn't belong to the debuggers text/data segment.
* In the future we may create more restrictions, but currently
* the debugee must be able to access the debuggers heap for
* shared arena's with malloc.
*/
bool
shiva_maps_validate_addr(struct shiva_ctx *ctx, uint64_t addr)
{
struct shiva_mmap_entry *current;
TAILQ_FOREACH(current, &ctx->tailq.mmap_tqlist, _linkage) {
if (current->debugger_mapping == true)
continue;
if (addr >= current->base && addr < current->base + current->len)
return true;
}
return false;
}
/*
* Load /proc/pid/maps into the ctx->tailq.mmap_tqlist
* XXX: Update this function to deal with Shiva when
* it's in interpreter mode... the address space layout
* is different than it would be in standalone mode.
*/
bool
shiva_maps_build_list(struct shiva_ctx *ctx)
{
FILE *fp;
char buf[PATH_MAX];
int i;
if (!TAILQ_EMPTY(&ctx->tailq.mmap_tqlist)) {
fprintf(stderr, "mmap_tqlist is already initialized\n");
return false;
}
TAILQ_INIT(&ctx->tailq.mmap_tqlist);
fp = fopen("/proc/self/maps", "r");
if (fp == NULL) {
perror("fopen");
return false;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
struct shiva_mmap_entry *entry;
unsigned long end;
char *appname = NULL;
char *p, *q;
char tmp[PATH_MAX];
strcpy(tmp, buf);
p = strrchr(buf, '/');
if (p != NULL)
appname = &p[1];
entry = shiva_malloc(sizeof(*entry));
memset(entry, 0, sizeof(*entry));
p = strrchr(buf, '/');
if (p != NULL) {
if (strncmp(&p[1], "shiva", 5) == 0) {
if (strstr(buf, "r----") != NULL) {
if (ctx->shiva_path == NULL) {
p = strchr(tmp, '/');
ctx->shiva_path = shiva_malloc(PATH_MAX + 1);
for (i = 0; *p != '\n' && *p != '\0'; p++, i++)
ctx->shiva_path[i] = *p;
ctx->shiva_path[i] = '\0';
}
}
entry->mmap_type = SHIVA_MMAP_TYPE_SHIVA;
}
} else if (strstr(buf, "[heap]") != NULL) {
entry->mmap_type = SHIVA_MMAP_TYPE_HEAP;
} else if (strstr(buf, "[stack]") != NULL) {
entry->mmap_type = SHIVA_MMAP_TYPE_STACK;
} else if (strstr(buf, "[vdso]") != NULL) {
entry->mmap_type = SHIVA_MMAP_TYPE_VDSO;
} else {
entry->mmap_type = SHIVA_MMAP_TYPE_MISC;
}
p = strchr(buf, '-');
assert(p != NULL);
*p = '\0';
entry->base = strtoul(buf, NULL, 16);
end = strtoul(p + 1, NULL, 16);
assert(end > entry->base);
entry->len = end - entry->base;
shiva_debug("base: %#lx\n", entry->base);
if (appname != NULL) {
if (!strncmp(appname, "shiva", 5)) {
shiva_debug("Debug mapping found: %#lx\n", entry->base);
entry->debugger_mapping = true;
}
}
q = strchr(p + 1, ' ');
assert(q != NULL);
p = q + 1;
while (*p != ' ') {
switch(*p) {
case 'r':
entry->prot |= PROT_READ;
break;
case 'w':
entry->prot |= PROT_WRITE;
break;
case 'x':
entry->prot |= PROT_EXEC;
case 'p':
entry->mapping = MAP_PRIVATE;
break;
case 's':
entry->mapping = MAP_SHARED;
case '-':
default:
break;
}
p++;
}
shiva_debug("Inserted mapping for %#lx - %#lx\n", entry->base,
entry->base + entry->len);
TAILQ_INSERT_TAIL(&ctx->tailq.mmap_tqlist, entry, _linkage);
}
return true;
}
void
shiva_maps_iterator_init(struct shiva_ctx *ctx, struct shiva_maps_iterator *iter)
{
iter->current = TAILQ_FIRST(&ctx->tailq.mmap_tqlist);
iter->ctx = ctx;
return;
}
shiva_iterator_res_t
shiva_maps_iterator_next(struct shiva_maps_iterator *iter, struct shiva_mmap_entry *e)
{
if (iter->current == NULL)
return SHIVA_ITER_DONE;
memcpy(e, iter->current, sizeof(*e));
iter->current = TAILQ_NEXT(iter->current, _linkage);
return ELF_ITER_OK;
}
bool
shiva_maps_prot_by_addr(struct shiva_ctx *ctx, uint64_t addr, int *prot)
{
shiva_maps_iterator_t mmap_iter;
struct shiva_mmap_entry mmap_entry;
shiva_maps_iterator_init(ctx, &mmap_iter);
while (shiva_maps_iterator_next(&mmap_iter, &mmap_entry) == SHIVA_ITER_OK) {
shiva_debug("mmap_entry.base: %#lx mmap_entry.len: %lx\n", mmap_entry.base, mmap_entry.len);
if (addr >= mmap_entry.base && addr < mmap_entry.base + mmap_entry.len) {
*prot = mmap_entry.prot;
return true;
}
}
return false;
}