-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathaddr_objdump.c
More file actions
189 lines (172 loc) · 6.14 KB
/
Copy pathaddr_objdump.c
File metadata and controls
189 lines (172 loc) · 6.14 KB
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
#include "phpspy.h"
#include <assert.h>
static int get_php_bin_path(pid_t pid, char *path_root, char *path);
static int get_php_base_addr(pid_t pid, char *path_root, char *path, uint64_t *raddr);
static int get_symbol_offset(char *path_root, const char *symbol, uint64_t *raddr);
static int popen_read_line(char *buf, size_t buf_size, char *cmd_fmt, ...);
int shell_escape(const char *arg, char *buf, size_t buf_size, const char *what) {
int rv = 0;
char *const buf_end = buf + buf_size;
assert(buf_size >= 1);
*buf++ = '\'';
while (*arg) {
/* logic based on https://stackoverflow.com/a/3669819 */
if (*arg == '\'') {
if (buf_end - buf < 4) {
rv = 1;
goto shell_escape_end;
}
*buf++ = '\''; /* close quoting */
*buf++ = '\\'; /* escape ... */
*buf++ = '\''; /* ... a single quote */
*buf++ = '\''; /* reopen quoting */
arg++;
} else {
if (buf_end - buf < 1) {
rv = 1;
goto shell_escape_end;
}
*buf++ = *arg++;
}
}
if (buf_end - buf < 2) {
rv = 1;
goto shell_escape_end;
}
*buf++ = '\'';
*buf = '\0';
shell_escape_end:
if (rv != 0) {
log_error("shell_escape: Buffer too small to escape %s: %s\n", what, arg);
}
return rv;
}
int get_symbol_addr(addr_memo_t *memo, pid_t pid, const char *symbol, uint64_t *raddr) {
char *php_bin_path, *php_bin_path_root;
uint64_t *php_base_addr;
uint64_t addr_offset;
php_bin_path = memo->php_bin_path;
php_bin_path_root = memo->php_bin_path_root;
php_base_addr = &memo->php_base_addr;
if (*php_bin_path == '\0' && get_php_bin_path(pid, php_bin_path_root, php_bin_path) != 0) {
return 1;
} else if (*php_base_addr == 0 && get_php_base_addr(pid, php_bin_path_root, php_bin_path, php_base_addr) != 0) {
return 1;
} else if (get_symbol_offset(php_bin_path_root, symbol, &addr_offset) != 0) {
return 1;
}
*raddr = *php_base_addr + addr_offset;
return 0;
}
static int get_php_bin_path(pid_t pid, char *path_root, char *path) {
char buf[PHPSPY_STR_SIZE];
char libname[PHPSPY_STR_SIZE];
if (shell_escape(opt_libname_awk_patt, libname, sizeof(libname), "opt_libname_awk_patt")) {
return 1;
}
char *cmd_fmt = "awk -ve=1 -vp=%s '$0~p{print $NF; e=0; exit} END{exit e}' /proc/%d/maps"
" || readlink /proc/%d/exe";
if (popen_read_line(buf, sizeof(buf), cmd_fmt, libname, (int)pid, (int)pid) != 0) {
log_error("get_php_bin_path: Failed\n");
return 1;
}
int n = snprintf(path_root, PHPSPY_STR_SIZE, "/proc/%d/root/%s", (int)pid, buf);
if (n < 0 || n >= PHPSPY_STR_SIZE) {
log_error("get_php_bin_path: snprintf overflow\n");
return 1;
}
if (access(path_root, F_OK) != 0) {
snprintf(path_root, PHPSPY_STR_SIZE, "/proc/%d/exe", (int)pid);
}
strcpy(path, buf);
return 0;
}
static int get_php_base_addr(pid_t pid, char *path_root, char *path, uint64_t *raddr) {
/* Note: This may be incomplete. It seems to work on x86_64 and aarch64.
* It may not work if the PHP binary has multiple executable segments
* for some reason. */
char buf[PHPSPY_STR_SIZE];
char arg_buf[PHPSPY_STR_SIZE];
uint64_t start_addr;
uint64_t virt_addr;
/* find start addr of executable mapping */
char *cmd_fmt = "awk -vp=%s '$NF==p{if(index($2,\"x\")){print $1;exit}}' /proc/%d/maps";
if (shell_escape(path, arg_buf, sizeof(arg_buf), "path")) {
return 1;
}
if (popen_read_line(buf, sizeof(buf), cmd_fmt, arg_buf, (int)pid) != 0) {
log_error("get_php_base_addr: Failed to get start_addr\n");
return 1;
}
start_addr = strtoull(buf, NULL, 16);
/* find vaddr of executable segment */
cmd_fmt = "objdump -p %s | awk -va=0 '$1==\"LOAD\"{a=$5} $5==\"flags\"{if(index($6,\"x\")){print a;exit}}'";
if (shell_escape(path_root, arg_buf, sizeof(arg_buf), "path_root")) {
return 1;
}
if (popen_read_line(buf, sizeof(buf), cmd_fmt, arg_buf) != 0) {
log_error("get_php_base_addr: Failed to get virt_addr\n");
return 1;
}
virt_addr = strtoull(buf, NULL, 16);
*raddr = start_addr - virt_addr;
return 0;
}
static int get_symbol_offset(char *path_root, const char *symbol, uint64_t *raddr) {
char buf[PHPSPY_STR_SIZE];
char arg_buf[PHPSPY_STR_SIZE];
char *cmd_fmt = "objdump -Tt %s | awk '/ %s$/{print $1; exit}'";
if (shell_escape(path_root, arg_buf, sizeof(arg_buf), "path_root")) {
return 1;
}
if (popen_read_line(buf, sizeof(buf), cmd_fmt, arg_buf, symbol) != 0) {
log_error("get_symbol_offset: Failed\n");
return 1;
}
*raddr = strtoull(buf, NULL, 16);
return 0;
}
static int popen_read_line(char *buf, size_t buf_size, char *cmd_fmt, ...) {
FILE *fp;
char cmd[PHPSPY_STR_SIZE];
char cmd_quiet[PHPSPY_STR_SIZE];
char *cmd_actual;
int rv, buf_len;
va_list cmd_args;
va_start(cmd_args, cmd_fmt);
rv = vsnprintf(cmd, sizeof(cmd), cmd_fmt, cmd_args);
va_end(cmd_args);
if (rv < 0 || rv >= (int)sizeof(cmd)) {
log_error("popen_read_line: vsnprintf overflow\n");
return PHPSPY_ERR;
}
cmd_actual = cmd;
if (opt_quiet) {
rv = snprintf(cmd_quiet, sizeof(cmd_quiet), "{ %s; } 2>/dev/null", cmd);
if (rv < 0 || rv >= (int)sizeof(cmd_quiet)) {
log_error("popen_read_line: snprintf overflow\n");
return PHPSPY_ERR;
}
cmd_actual = cmd_quiet;
}
if (!(fp = popen(cmd_actual, "r"))) {
log_perror("popen_read_line: popen");
return 1;
}
if (fgets(buf, buf_size-1, fp) == NULL) {
log_error("popen_read_line: No stdout; cmd=%s\n", cmd);
pclose(fp);
return 1;
}
pclose(fp);
buf_len = strlen(buf);
while (buf_len > 0 && buf[buf_len-1] == '\n') {
--buf_len;
}
if (buf_len < 1) {
log_error("popen_read_line: Expected strlen(buf)>0; cmd=%s\n", cmd);
return 1;
}
buf[buf_len] = '\0';
return 0;
}