-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathparse_maps.c
More file actions
307 lines (252 loc) · 8.31 KB
/
parse_maps.c
File metadata and controls
307 lines (252 loc) · 8.31 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
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
#include "libptrace_do.h"
#define PROC_STRING "/proc/"
#define MAPS_STRING "/maps"
// Internal helper functions don't need to make it into the main .h file.*/
struct parse_maps *parse_next_line(char *line);
/***********************************************************************************************************************
*
* get_proc_pid_maps()
*
* Input:
* The process id of the target.
*
* Output:
* Pointer to a struct parse_maps object. NULL on error.
*
* Purpose:
* The parse_maps object pointer will be a pointer to the head of a linked list. This list represents the
* different regions of memory allocated by the kernel. This will be a reflection of the entries in the
* /proc/PID/maps file.
*
**********************************************************************************************************************/
struct parse_maps *get_proc_pid_maps(pid_t target){
struct parse_maps *map_head = NULL, *map_tail = NULL, *map_tmp;
int fd = -1;
int buffer_len;
int ret_int;
char *buffer;
char *tmp_ptr;
// I'm afraid that this function just parses a file and turns it into a linked list. Not very exciting.
buffer_len = getpagesize();
if((buffer = (char *) calloc(buffer_len, sizeof(char))) == NULL){
fprintf(stderr, "calloc(%d, %d): %s\n", buffer_len, (int) sizeof(char), strerror(errno));
goto CLEAN_UP;
}
tmp_ptr = buffer;
memcpy(tmp_ptr, PROC_STRING, strlen(PROC_STRING));
tmp_ptr = strchr(buffer, '\0');
snprintf(tmp_ptr, (PATH_MAX - 1) - (strlen(PROC_STRING) + strlen(MAPS_STRING)), "%d", target);
tmp_ptr = strchr(buffer, '\0');
memcpy(tmp_ptr, MAPS_STRING, strlen(MAPS_STRING));
if((fd = open(buffer, O_RDONLY)) == -1){
fprintf(stderr, "open(%s, O_RDONLY): %s\n", buffer, strerror(errno));
goto CLEAN_UP;
}
memset(buffer, 0, buffer_len);
tmp_ptr = buffer;
while((ret_int = read(fd, tmp_ptr, 1)) > 0){
if(*tmp_ptr == '\n'){
*tmp_ptr = '\0';
if((map_tmp = parse_next_line(buffer)) == NULL){
fprintf(stderr, "parse_next_line(%s): %s\n", buffer, strerror(errno));
goto CLEAN_UP;
}
if(!map_head){
map_head = map_tmp;
map_tail = map_tmp;
}else{
map_tail->next = map_tmp;
map_tmp->previous = map_tail;
map_tail = map_tmp;
}
memset(buffer, 0, buffer_len);
tmp_ptr = buffer;
}else{
tmp_ptr++;
}
}
if(ret_int == -1){
fprintf(stderr, "read(%d, %lx, 1): %s\n", fd, (unsigned long) tmp_ptr, strerror(errno));
goto CLEAN_UP;
}
free(buffer);
if(fd > -1){
close(fd);
}
return(map_head);
CLEAN_UP:
free(buffer);
close(fd);
free_parse_maps_list(map_head);
return(NULL);
}
/***********************************************************************************************************************
*
* parse_next_line()
*
* Input:
* A pointer to the string that represents the next line of the file.
*
* Output:
* A pointer to the next node, as created from this line.
*
* Purpose:
* This is a helper function, not exposed externally. It parses a line and returns a node. Enough said. :)
*
**********************************************************************************************************************/
struct parse_maps *parse_next_line(char *line){
struct parse_maps *node = NULL;
char *token_head, *token_tail, *retval_char_star;
// The comments mentioning data types are just trying to demonstrate
// the type of data we will be parsing in that area.
if((node = (struct parse_maps *) calloc(1, sizeof(struct parse_maps))) == NULL){
fprintf(stderr, "calloc(1, %d): %s\n", (int) sizeof(struct parse_maps), strerror(errno));
goto CLEAN_UP;
}
// unsigned long start_address;
token_head = line;
if((retval_char_star = strchr(token_head, '-')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, '-', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
node->start_address = strtoul(token_head, NULL, 16);
// unsigned long end_address;
token_head = token_tail + 1;
if((retval_char_star = strchr(token_head, ' ')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, ' ', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
node->end_address = strtoul(token_head, NULL, 16);
// unsigned int perms;
token_head = token_tail + 1;
if((retval_char_star = strchr(token_head, ' ')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, ' ', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
if(*(token_head++) == 'r'){
node->perms |= MAPS_READ;
}
if(*(token_head++) == 'w'){
node->perms |= MAPS_WRITE;
}
if(*(token_head++) == 'x'){
node->perms |= MAPS_EXECUTE;
}
if(*token_head == 'p'){
node->perms |= MAPS_PRIVATE;
}else if(*token_head == 's'){
node->perms |= MAPS_SHARED;
}
// unsigned long offset;
token_head = token_tail + 1;
if((retval_char_star = strchr(token_head, ' ')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, ' ', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
node->offset = strtoul(token_head, NULL, 16);
// unsigned int dev_major;
token_head = token_tail + 1;
if((retval_char_star = strchr(token_head, ':')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, ':', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
node->dev_major = strtol(token_head, NULL, 16);
// unsigned int dev_minor;
token_head = token_tail + 1;
if((retval_char_star = strchr(token_head, ' ')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, ' ', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
node->dev_minor = strtol(token_head, NULL, 16);
// unsigned long inode;
token_head = token_tail + 1;
if((retval_char_star = strchr(token_head, ' ')) == NULL){
fprintf(stderr, "strchr(%s, '%c'): %s\n", token_head, ' ', strerror(errno));
goto CLEAN_UP;
}
token_tail = retval_char_star;
*token_tail = '\0';
node->inode = strtol(token_head, NULL, 10);
// char pathname[PATH_MAX];
token_head = token_tail + 1;
if(*token_head){
if((retval_char_star = strrchr(token_head, ' ')) == NULL){
fprintf(stderr, "strrchr(%s, '%c'): %s\n", token_head, ' ', strerror(errno));
goto CLEAN_UP;
}
token_head = retval_char_star;
token_head++;
memcpy(node->pathname, token_head, strlen(token_head));
}
return(node);
CLEAN_UP:
free(node);
return(NULL);
}
/***********************************************************************************************************************
*
* free_parse_maps_list()
*
* Input:
* A pointer to the head of the list.
*
* Output:
* Nothing.
*
* Purpose:
* Free the members of the linked list.
*
**********************************************************************************************************************/
void free_parse_maps_list(struct parse_maps *head){
struct parse_maps *tmp;
while(head){
tmp = head->next;
free(head);
head = tmp;
}
}
/***********************************************************************************************************************
*
* dump_parse_maps_list()
*
* Input:
* A pointer to the head of the list.
*
* Output:
* Nothing, but it will print representations of the internal data to stdout.
*
* Purpose:
* Show us what the linked list looks like. Mostly intended for debugging.
*
**********************************************************************************************************************/
void dump_parse_maps_list(struct parse_maps *head){
while(head){
printf("--------------------------------------------------------------------------------\n");
printf("node: %lx\n", (unsigned long) head);
printf("--------------------------------------------------------------------------------\n");
printf("start_address:\t\t%lx\n", head->start_address);
printf("end_address:\t\t%lx\n", head->end_address);
printf("perms:\t\t\t%05x\n", head->perms);
printf("offset:\t\t\t%lx\n", head->offset);
printf("dev_major:\t\t%x\n", head->dev_major);
printf("dev_minor:\t\t%x\n", head->dev_minor);
printf("inode:\t\t\t%lx\n", head->inode);
printf("pathname:\t\t%s\n", head->pathname);
printf("parse_maps *next:\t%lx\n", (unsigned long) head->next);
printf("parse_maps *previous:\t%lx\n", (unsigned long) head->previous);
printf("\n");
head = head->next;
}
}