The Witchcraft Compiler Collection
WCC
 All Data Structures Files Functions Variables Typedefs Macros
helper.c
Go to the documentation of this file.
1 /*
2 *
3 * Post memory corruption memory analyzer
4 *
5 *
6 *
7 * Copyright 2011 Toucan System SARL
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *
22 */
23 
24 #define _XOPEN_SOURCE 500
25 #define _FILE_OFFSET_BITS 64
26 #include <math.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <regex.h>
36 #include <sys/ptrace.h>
37 #include <signal.h>
38 #include <string.h>
39 
40 #include <libwitch/helper.h>
41 
42 extern unsigned int lastsignal;
43 
44 #ifndef HAS_ZFIRST
45 #define HAS_ZFIRST 1
46 struct section *zfirst = 0;
47 int nsections=0;
48 #else
49 extern struct section *zfirst;
50 extern int nsections;
51 #endif
52 
53 /*
54 * Is a given address mapped ?
55 */
56 int is_mapped(unsigned long int addr){
57 
58  struct section *tmpsection = zfirst;
59  while (tmpsection != 0x00) {
60  if ((tmpsection->init <= addr) && (tmpsection->end > addr)) {
61  // return size to end of section
62  return tmpsection->end - addr;
63  }
64  tmpsection = tmpsection->next;
65  }
66  return 0;
67 }
68 
69 /*
70 * read /proc/pid/map
71 */
72 int read_maps(int pid)
73 {
74  char mpath[255];
75  FILE *fmaps;
76  char line[1000];
77 #ifdef __x86_64__
78  unsigned long long int initz, endz, size;
79 #else
80  unsigned long int initz, endz, size;
81 #endif
82  char *name;
83  unsigned int counter=1;
84  struct section *zptr;
85  unsigned int perms, t;
86  int delta;
87 
88  // path to maps file
89  sprintf(mpath, "/proc/%d/maps", pid);
90  fmaps = fopen(mpath, "r");
91 
92  if (fmaps == NULL) {
93  perror("[!!] Error reading maps file");
94  exit(1);
95  }
96 
97  while ( fgets ( line, sizeof line, fmaps ) != NULL ) {
98 #ifdef __x86_64__
99 
100  // we first need to check if the possible address is a 32 or 64 one
101  initz = strtoul(line, NULL, 16);
102  endz = strtoul(strchr(line, '-')+1, NULL, 16);
103  size = endz - initz;
104 
105  delta=strchr(line, ' ')-line;
106 #else
107  delta=18;
108  endz = strtoull(line + 9, NULL, 16);
109  initz = strtoull(line + 0, NULL, 16);
110  size = endz - initz;
111 #endif
112 
113  // find permissions
114  perms = 0;
115  char hperms[5];
116  memset(hperms,0x00,5);
117  memcpy(hperms,line+delta,4);
118  for (t = 0; t < 4; t++) {
119  switch (line[t + delta]) {
120  case 'r':
121  perms += 2; /*printf(" read "); */
122  break;
123  case 'w':
124  perms += 4; /*printf(" write "); */
125  break;
126  case 'x':
127  perms += 1; /*printf(" execute "); */
128  break;
129  case 'p': /*printf(" private "); */
130  break;
131  case 's':
132  perms += 8; /*printf(" shared "); */
133  break;
134  }
135  }
136 
137  // find name
138  strtok(line, " ");
139  for (t=0;t<5;t++) {
140  name = strtok(NULL, " ");
141  }
142  // Remove leading spaces
143  while(*name != '\0' && isspace(*name))
144  {
145  ++name;
146  }
147  // Remove trailing newline
148  name[strlen(name) - 1] = '\0';
149 
150  // Omit vsyscall as pread fails for the last address
151  if (!strncmp("[vsyscall]", name,10))
152  continue;
153 
154  // add to linked list
155  zptr = (struct section *)malloc(sizeof(struct section));
156  memset(zptr, 0x00, sizeof(struct section));
157  zptr->init = initz;
158  zptr->end = endz;
159  zptr->size = size;
160  zptr->perms = perms;
161  strcpy(zptr->hperms, hperms);
162  zptr->num=counter++;
163  strcpy(zptr->name, name);
164 
165  if (zfirst == 0x00) { // we are first
166  zfirst = zptr;
167 
168  } else { // append
169  struct section *tmpsection = zfirst;
170  while (tmpsection->next != 0x00) {
171  tmpsection = tmpsection->next;
172  }
173  tmpsection->next = zptr;
174 
175  }
176  }
177 
178  fclose(fmaps);
179  nsections=counter-1;
180  return 0;
181 }
182 
183 
struct section * zfirst
Definition: helper.c:46
int perms
Definition: helper.h:15
void exit(int status)
Definition: wsh.c:3137
int read_maps(int pid)
Definition: helper.c:72
Definition: helper.h:11
int nsections
Definition: helper.c:47
unsigned long long int init
Definition: helper.h:12
int num
Definition: helper.h:20
char name[255]
Definition: helper.h:16
unsigned int lastsignal
void * next
Definition: helper.h:18
char hperms[10]
Definition: helper.h:17
int size
Definition: helper.h:14
int is_mapped(unsigned long int addr)
Definition: helper.c:56
unsigned long long int end
Definition: helper.h:13