forked from felipensp/itrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolv.c
168 lines (135 loc) · 2.94 KB
/
resolv.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
/*
* itrace
*
* Address resolver specific routines
*
*/
#include <stdio.h>
#include <limits.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "resolv.h"
#include "trace.h"
#include "ptrace.h"
#include "elfsym.h"
#include "disas.h"
resolv_info r_info;
static int _map_region(const char *prog, const char *line)
{
uintptr_t start, end;
char perms[5], fname[PATH_MAX] = {0};
int offset, dmajor, dminor, inode;
const size_t n = r_info.num_segments;
resolv_segment *segments = r_info.segments;
if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %s %x %x:%x %u %s",
&start, &end, perms, &offset, &dmajor, &dminor, &inode, fname) < 6 ||
(end - start) == 0) {
return 0;
}
if (segments == NULL || n % 5 == 0) {
segments = realloc(segments, sizeof(resolv_segment) * (n + 5));
if (segments == NULL) {
return 0;
}
}
segments[n].saddr = start;
segments[n].eaddr = end;
segments[n].is_dynamic = 1;
if (memcmp(fname, prog, strlen(prog)+1) == 0) {
if (r_info.baddr == 0) {
r_info.baddr = start;
}
segments[n].is_dynamic = 0;
}
memcpy(segments[n].fname, fname, sizeof(fname));
memcpy(segments[n].perms, perms, sizeof(perms));
r_info.num_segments++;
r_info.segments = segments;
return 1;
}
static int _map_segments()
{
char fname[PATH_MAX], lname[PATH_MAX], *line = NULL;
FILE *fp;
size_t size;
if (r_info.segments) {
free(r_info.segments);
r_info.segments = NULL;
r_info.num_segments = 0;
}
if (tracee.prog) {
if (realpath(tracee.prog, lname) == NULL) {
return 0;
}
} else {
snprintf(fname, sizeof(fname), "/proc/%d/exe", tracee.pid);
if (readlink(fname, lname, sizeof(lname)) != -1) {
return 0;
}
}
snprintf(fname, sizeof(fname), "/proc/%d/maps", tracee.pid);
if ((fp = fopen(fname, "r")) == NULL) {
return 0;
}
while (getline(&line, &size, fp) != -1) {
_map_region(lname, line);
}
free(line);
fclose(fp);
return 1;
}
void resolv_show_maps()
{
int i;
iprintf("Maps:\n");
for (i = 0; i < r_info.num_segments; ++i) {
iprintf("%" ADDR_FMT "-%" ADDR_FMT " - %s (%s) %d\n",
r_info.segments[i].saddr,
r_info.segments[i].eaddr,
r_info.segments[i].fname,
r_info.segments[i].perms,
r_info.segments[i].is_dynamic);
}
}
void resolv_startup()
{
if (!_map_segments()) {
iprintf("[!] Failed to read /proc/%d/maps file!\n", tracee.pid);
return;
}
elfsym_startup(r_info.baddr);
}
void resolv_shutdown()
{
free(r_info.segments);
elfsym_shutdown();
}
int resolv_is_dynamic(uintptr_t addr)
{
int i = 0, try = 0;
resolv_segment *segs;
find:
segs = r_info.segments;
while (i < r_info.num_segments) {
if (segs[i].saddr <= addr && segs[i].eaddr >= addr) {
return segs[i].is_dynamic;
}
++i;
}
if (!try) {
/*
* Remaps the segments when the addr is not found
*/
_map_segments();
try = 1;
goto find;
}
return 0;
}
const char *resolv_symbol(uintptr_t addr)
{
const elfsym_sym *sym = elfsym_resolv(addr);
return sym ? sym->name : NULL;
}