jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 1 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- User-mode execve() ume.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
jseward | 2886b0e | 2004-01-04 03:46:11 +0000 | [diff] [blame] | 6 | /* |
| 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
| 9 | |
| 10 | Copyright (C) 2000-2004 Julian Seward |
| 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 31 | |
| 32 | #define _GNU_SOURCE |
| 33 | #define _FILE_OFFSET_BITS 64 |
| 34 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 35 | #include "core.h" |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 36 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 37 | #include <sys/mman.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <errno.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <unistd.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 44 | #include <assert.h> |
| 45 | |
| 46 | #include "ume.h" |
| 47 | |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 48 | struct elfinfo |
| 49 | { |
| 50 | ESZ(Ehdr) e; |
| 51 | ESZ(Phdr) *p; |
| 52 | int fd; |
| 53 | }; |
| 54 | |
nethercote | 30d3784 | 2004-07-26 10:05:55 +0000 | [diff] [blame] | 55 | static void check_mmap(void* res, void* base, int len) |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 56 | { |
| 57 | if ((void*)-1 == res) { |
| 58 | fprintf(stderr, "valgrind: mmap(%p, %d) failed during startup.\n" |
| 59 | "valgrind: is there a hard virtual memory limit set?\n", |
| 60 | base, len); |
| 61 | exit(1); |
| 62 | } |
| 63 | } |
| 64 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 65 | // 'extra' allows the caller to pass in extra args to 'fn', like free |
| 66 | // variables to a closure. |
| 67 | void foreach_map(int (*fn)(char *start, char *end, |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 68 | const char *perm, off_t offset, |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 69 | int maj, int min, int ino, void* extra), |
| 70 | void* extra) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 71 | { |
| 72 | static char buf[10240]; |
| 73 | char *bufptr = buf; |
| 74 | int ret, fd; |
| 75 | |
| 76 | fd = open("/proc/self/maps", O_RDONLY); |
| 77 | |
| 78 | if (fd == -1) { |
| 79 | perror("open /proc/self/maps"); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | ret = read(fd, buf, sizeof(buf)); |
| 84 | |
| 85 | if (ret == -1) { |
| 86 | perror("read /proc/self/maps"); |
| 87 | close(fd); |
| 88 | return; |
| 89 | } |
| 90 | close(fd); |
| 91 | |
| 92 | if (ret == sizeof(buf)) { |
| 93 | fprintf(stderr, "buf too small\n"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | while(bufptr && bufptr < buf+ret) { |
| 98 | char perm[5]; |
| 99 | off_t offset; |
| 100 | int maj, min; |
| 101 | int ino; |
| 102 | void *segstart, *segend; |
| 103 | |
| 104 | sscanf(bufptr, "%p-%p %s %Lx %x:%x %d", |
| 105 | &segstart, &segend, perm, &offset, &maj, &min, &ino); |
| 106 | bufptr = strchr(bufptr, '\n'); |
| 107 | if (bufptr != NULL) |
| 108 | bufptr++; /* skip \n */ |
| 109 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 110 | if (!(*fn)(segstart, segend, perm, offset, maj, min, ino, extra)) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 115 | typedef struct { |
| 116 | char* fillgap_start; |
| 117 | char* fillgap_end; |
| 118 | int fillgap_padfile; |
| 119 | } fillgap_extra; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 120 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 121 | static int fillgap(char *segstart, char *segend, const char *perm, off_t off, |
| 122 | int maj, int min, int ino, void* e) |
| 123 | { |
| 124 | fillgap_extra* extra = e; |
| 125 | |
| 126 | if (segstart >= extra->fillgap_end) |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 129 | if (segstart > extra->fillgap_start) { |
| 130 | void* res = mmap(extra->fillgap_start, segstart - extra->fillgap_start, |
fitzhardinge | 72ddff6 | 2004-09-08 20:03:51 +0000 | [diff] [blame] | 131 | PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_NORESERVE, |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 132 | extra->fillgap_padfile, 0); |
| 133 | check_mmap(res, extra->fillgap_start, segstart - extra->fillgap_start); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 134 | } |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 135 | extra->fillgap_start = segend; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 136 | |
| 137 | return 1; |
| 138 | } |
| 139 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 140 | // Choose a name for the padfile, open it. |
| 141 | int as_openpadfile(void) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 142 | { |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 143 | char buf[256]; |
| 144 | int padfile; |
| 145 | int seq = 1; |
| 146 | do { |
| 147 | snprintf(buf, 256, "/tmp/.pad.%d.%d", getpid(), seq++); |
| 148 | padfile = open(buf, O_RDWR|O_CREAT|O_EXCL, 0); |
| 149 | unlink(buf); |
| 150 | if (padfile == -1 && errno != EEXIST) { |
| 151 | fprintf(stderr, "valgrind: couldn't open padfile\n"); |
| 152 | exit(44); |
| 153 | } |
| 154 | } while(padfile == -1); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 155 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 156 | return padfile; |
| 157 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 158 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 159 | // Pad all the empty spaces in a range of address space to stop interlopers. |
| 160 | void as_pad(void *start, void *end, int padfile) |
| 161 | { |
| 162 | fillgap_extra extra; |
| 163 | extra.fillgap_start = start; |
| 164 | extra.fillgap_end = end; |
| 165 | extra.fillgap_padfile = padfile; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 166 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 167 | foreach_map(fillgap, &extra); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 168 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 169 | if (extra.fillgap_start < extra.fillgap_end) { |
| 170 | void* res = mmap(extra.fillgap_start, |
| 171 | extra.fillgap_end - extra.fillgap_start, |
fitzhardinge | 72ddff6 | 2004-09-08 20:03:51 +0000 | [diff] [blame] | 172 | PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_NORESERVE, padfile, 0); |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 173 | check_mmap(res, extra.fillgap_start, |
| 174 | extra.fillgap_end - extra.fillgap_start); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 175 | } |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 176 | } |
| 177 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 178 | typedef struct { |
| 179 | char* killpad_start; |
| 180 | char* killpad_end; |
| 181 | struct stat* killpad_padstat; |
| 182 | } killpad_extra; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 183 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 184 | static int killpad(char *segstart, char *segend, const char *perm, off_t off, |
| 185 | int maj, int min, int ino, void* ex) |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 186 | { |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 187 | killpad_extra* extra = ex; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 188 | void *b, *e; |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 189 | int res; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 190 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 191 | assert(NULL != extra->killpad_padstat); |
| 192 | |
| 193 | if (extra->killpad_padstat->st_dev != makedev(maj, min) || |
| 194 | extra->killpad_padstat->st_ino != ino) |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 195 | return 1; |
| 196 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 197 | if (segend <= extra->killpad_start || segstart >= extra->killpad_end) |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 198 | return 1; |
| 199 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 200 | if (segstart <= extra->killpad_start) |
| 201 | b = extra->killpad_start; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 202 | else |
| 203 | b = segstart; |
| 204 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 205 | if (segend >= extra->killpad_end) |
| 206 | e = extra->killpad_end; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 207 | else |
| 208 | e = segend; |
| 209 | |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 210 | res = munmap(b, (char *)e-(char *)b); |
| 211 | assert(0 == res); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 212 | |
| 213 | return 1; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 214 | } |
| 215 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 216 | // Remove padding of 'padfile' from a range of address space. |
| 217 | void as_unpad(void *start, void *end, int padfile) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 218 | { |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 219 | static struct stat padstat; |
| 220 | killpad_extra extra; |
| 221 | int res; |
| 222 | |
| 223 | assert(padfile > 0); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 224 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 225 | res = fstat(padfile, &padstat); |
| 226 | assert(0 == res); |
| 227 | extra.killpad_padstat = &padstat; |
| 228 | extra.killpad_start = start; |
| 229 | extra.killpad_end = end; |
| 230 | foreach_map(killpad, &extra); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 231 | } |
| 232 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 233 | void as_closepadfile(int padfile) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 234 | { |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 235 | int res = close(padfile); |
| 236 | assert(0 == res); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 237 | } |
| 238 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 239 | /*------------------------------------------------------------*/ |
| 240 | /*--- Finding auxv on the stack ---*/ |
| 241 | /*------------------------------------------------------------*/ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 242 | |
| 243 | struct ume_auxv *find_auxv(int *esp) |
| 244 | { |
| 245 | esp++; /* skip argc */ |
| 246 | |
| 247 | while(*esp != 0) /* skip argv */ |
| 248 | esp++; |
| 249 | esp++; |
| 250 | |
| 251 | while(*esp != 0) /* skip env */ |
| 252 | esp++; |
| 253 | esp++; |
| 254 | |
| 255 | return (struct ume_auxv *)esp; |
| 256 | } |
| 257 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 258 | /*------------------------------------------------------------*/ |
| 259 | /*--- Loading ELF files ---*/ |
| 260 | /*------------------------------------------------------------*/ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 261 | |
| 262 | struct elfinfo *readelf(int fd, const char *filename) |
| 263 | { |
| 264 | struct elfinfo *e = malloc(sizeof(*e)); |
| 265 | int phsz; |
| 266 | |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 267 | assert(e); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 268 | e->fd = fd; |
| 269 | |
| 270 | if (pread(fd, &e->e, sizeof(e->e), 0) != sizeof(e->e)) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 271 | fprintf(stderr, "valgrind: %s: can't read elf header: %s\n", |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 272 | filename, strerror(errno)); |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | if (memcmp(&e->e.e_ident[0], ELFMAG, SELFMAG) != 0) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 277 | fprintf(stderr, "valgrind: %s: bad ELF magic\n", filename); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 278 | return NULL; |
| 279 | } |
| 280 | if (e->e.e_ident[EI_CLASS] != ELFCLASS32) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 281 | fprintf(stderr, "valgrind: Can only handle 32-bit executables\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | if (e->e.e_ident[EI_DATA] != ELFDATA2LSB) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 285 | fprintf(stderr, "valgrind: Expecting little-endian\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 286 | return NULL; |
| 287 | } |
| 288 | if (!(e->e.e_type == ET_EXEC || e->e.e_type == ET_DYN)) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 289 | fprintf(stderr, "valgrind: need executable\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | if (e->e.e_machine != EM_386) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 294 | fprintf(stderr, "valgrind: need x86\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | if (e->e.e_phentsize != sizeof(ESZ(Phdr))) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 299 | fprintf(stderr, "valgrind: sizeof Phdr wrong\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | phsz = sizeof(ESZ(Phdr)) * e->e.e_phnum; |
| 304 | e->p = malloc(phsz); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 305 | assert(e->p); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 306 | |
| 307 | if (pread(fd, e->p, phsz, e->e.e_phoff) != phsz) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 308 | fprintf(stderr, "valgrind: can't read phdr: %s\n", strerror(errno)); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | return e; |
| 313 | } |
| 314 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 315 | /* Map an ELF file. Returns the brk address. */ |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 316 | ESZ(Addr) mapelf(struct elfinfo *e, ESZ(Addr) base) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 317 | { |
| 318 | int i; |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 319 | void* res; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 320 | ESZ(Addr) elfbrk = 0; |
| 321 | |
| 322 | for(i = 0; i < e->e.e_phnum; i++) { |
| 323 | ESZ(Phdr) *ph = &e->p[i]; |
| 324 | ESZ(Addr) addr, brkaddr; |
| 325 | ESZ(Word) memsz; |
| 326 | |
| 327 | if (ph->p_type != PT_LOAD) |
| 328 | continue; |
| 329 | |
| 330 | addr = ph->p_vaddr+base; |
| 331 | memsz = ph->p_memsz; |
| 332 | brkaddr = addr+memsz; |
| 333 | |
| 334 | if (brkaddr > elfbrk) |
| 335 | elfbrk = brkaddr; |
| 336 | } |
| 337 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 338 | for(i = 0; i < e->e.e_phnum; i++) { |
| 339 | ESZ(Phdr) *ph = &e->p[i]; |
| 340 | ESZ(Addr) addr, bss, brkaddr; |
| 341 | ESZ(Off) off; |
| 342 | ESZ(Word) filesz; |
| 343 | ESZ(Word) memsz; |
| 344 | ESZ(Word) align; |
| 345 | unsigned prot = 0; |
| 346 | |
| 347 | if (ph->p_type != PT_LOAD) |
| 348 | continue; |
| 349 | |
| 350 | if (ph->p_flags & PF_X) |
| 351 | prot |= PROT_EXEC; |
| 352 | if (ph->p_flags & PF_W) |
| 353 | prot |= PROT_WRITE; |
| 354 | if (ph->p_flags & PF_R) |
| 355 | prot |= PROT_READ; |
| 356 | |
| 357 | align = ph->p_align; |
| 358 | |
| 359 | addr = ph->p_vaddr+base; |
| 360 | off = ph->p_offset; |
| 361 | filesz = ph->p_filesz; |
| 362 | bss = addr+filesz; |
| 363 | memsz = ph->p_memsz; |
| 364 | brkaddr = addr+memsz; |
| 365 | |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 366 | res = mmap((char *)ROUNDDN(addr, align), |
| 367 | ROUNDUP(bss, align)-ROUNDDN(addr, align), |
| 368 | prot, MAP_FIXED|MAP_PRIVATE, e->fd, ROUNDDN(off, align)); |
| 369 | check_mmap(res, (char*)ROUNDDN(addr,align), |
| 370 | ROUNDUP(bss, align)-ROUNDDN(addr, align)); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 371 | |
| 372 | /* if memsz > filesz, then we need to fill the remainder with zeroed pages */ |
| 373 | if (memsz > filesz) { |
| 374 | UInt bytes; |
| 375 | |
| 376 | bytes = ROUNDUP(brkaddr, align)-ROUNDUP(bss, align); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 377 | if (bytes > 0) { |
| 378 | res = mmap((char *)ROUNDUP(bss, align), bytes, |
| 379 | prot, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 380 | check_mmap(res, (char*)ROUNDUP(bss,align), bytes); |
| 381 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 382 | |
| 383 | bytes = bss & (VKI_BYTES_PER_PAGE - 1); |
| 384 | if (bytes > 0) { |
| 385 | bytes = VKI_BYTES_PER_PAGE - bytes; |
| 386 | memset((char *)bss, 0, bytes); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return elfbrk; |
| 392 | } |
| 393 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 394 | // Forward declaration. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 395 | static int do_exec_inner(const char *exe, struct exeinfo *info); |
| 396 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 397 | static int match_ELF(const char *hdr, int len) |
| 398 | { |
| 399 | ESZ(Ehdr) *e = (ESZ(Ehdr) *)hdr; |
| 400 | return (len > sizeof(*e)) && memcmp(&e->e_ident[0], ELFMAG, SELFMAG) == 0; |
| 401 | } |
| 402 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 403 | static int load_ELF(char *hdr, int len, int fd, const char *name, |
| 404 | struct exeinfo *info) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 405 | { |
| 406 | struct elfinfo *e; |
| 407 | struct elfinfo *interp = NULL; |
fitzhardinge | ca9bd9c | 2004-09-08 20:05:02 +0000 | [diff] [blame] | 408 | ESZ(Addr) exeoff = 0; /* offset between link address and load address */ |
| 409 | ESZ(Addr) minaddr = ~0; /* lowest mapped address */ |
| 410 | ESZ(Addr) maxaddr = 0; /* highest mapped address */ |
| 411 | ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */ |
| 412 | ESZ(Word) interp_size = 0; /* interpreter size */ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 413 | int i; |
| 414 | void *entry; |
| 415 | |
| 416 | e = readelf(fd, name); |
| 417 | |
| 418 | if (e == NULL) |
| 419 | return ENOEXEC; |
| 420 | |
| 421 | info->phnum = e->e.e_phnum; |
| 422 | info->entry = e->e.e_entry; |
| 423 | |
| 424 | for(i = 0; i < e->e.e_phnum; i++) { |
| 425 | ESZ(Phdr) *ph = &e->p[i]; |
| 426 | |
| 427 | switch(ph->p_type) { |
| 428 | case PT_PHDR: |
| 429 | info->phdr = ph->p_vaddr; |
| 430 | break; |
| 431 | |
| 432 | case PT_LOAD: |
| 433 | if (ph->p_vaddr < minaddr) |
| 434 | minaddr = ph->p_vaddr; |
| 435 | if (ph->p_vaddr+ph->p_memsz > maxaddr) |
| 436 | maxaddr = ph->p_vaddr+ph->p_memsz; |
| 437 | break; |
| 438 | |
| 439 | case PT_INTERP: { |
| 440 | char *buf = malloc(ph->p_filesz+1); |
| 441 | int j; |
| 442 | int intfd; |
| 443 | int baseaddr_set; |
| 444 | |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 445 | assert(buf); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 446 | pread(fd, buf, ph->p_filesz, ph->p_offset); |
| 447 | buf[ph->p_filesz] = '\0'; |
| 448 | |
| 449 | intfd = open(buf, O_RDONLY); |
| 450 | if (intfd == -1) { |
| 451 | perror("open interp"); |
| 452 | exit(1); |
| 453 | } |
| 454 | |
| 455 | interp = readelf(intfd, buf); |
| 456 | if (interp == NULL) { |
| 457 | fprintf(stderr, "Can't read interpreter\n"); |
| 458 | return 1; |
| 459 | } |
| 460 | free(buf); |
| 461 | |
| 462 | baseaddr_set = 0; |
| 463 | for(j = 0; j < interp->e.e_phnum; j++) { |
| 464 | ESZ(Phdr) *iph = &interp->p[j]; |
| 465 | ESZ(Addr) end; |
| 466 | |
| 467 | if (iph->p_type != PT_LOAD) |
| 468 | continue; |
| 469 | |
| 470 | if (!baseaddr_set) { |
| 471 | interp_addr = iph->p_vaddr; |
| 472 | baseaddr_set = 1; |
| 473 | } |
| 474 | |
| 475 | /* assumes that all segments in the interp are close */ |
| 476 | end = (iph->p_vaddr - interp_addr) + iph->p_memsz; |
| 477 | |
| 478 | if (end > interp_size) |
| 479 | interp_size = end; |
| 480 | } |
| 481 | break; |
nethercote | b24cbc8 | 2004-09-03 23:25:33 +0000 | [diff] [blame] | 482 | |
| 483 | default: |
| 484 | // do nothing |
| 485 | break; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
fitzhardinge | ca9bd9c | 2004-09-08 20:05:02 +0000 | [diff] [blame] | 490 | if (e->e.e_type == ET_DYN) { |
| 491 | /* PIE executable */ |
| 492 | exeoff = info->exe_base - minaddr; |
| 493 | } |
| 494 | |
| 495 | minaddr += exeoff; |
| 496 | maxaddr += exeoff; |
| 497 | info->phdr += exeoff; |
| 498 | info->entry += exeoff; |
| 499 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 500 | if (info->exe_base != info->exe_end) { |
| 501 | if (minaddr >= maxaddr || |
| 502 | (minaddr < info->exe_base || |
| 503 | maxaddr > info->exe_end)) { |
nethercote | 2e1cb4c | 2004-08-05 12:16:13 +0000 | [diff] [blame] | 504 | fprintf(stderr, "Executable range %p-%p is outside the\n" |
| 505 | "acceptable range %p-%p\n", |
| 506 | (void *)minaddr, (void *)maxaddr, |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 507 | (void *)info->exe_base, (void *)info->exe_end); |
| 508 | return ENOMEM; |
| 509 | } |
| 510 | } |
| 511 | |
fitzhardinge | ca9bd9c | 2004-09-08 20:05:02 +0000 | [diff] [blame] | 512 | info->brkbase = mapelf(e, exeoff); /* map the executable */ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 513 | |
fitzhardinge | 9236079 | 2003-12-24 10:11:11 +0000 | [diff] [blame] | 514 | if (info->brkbase == 0) |
| 515 | return ENOMEM; |
| 516 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 517 | if (interp != NULL) { |
| 518 | /* reserve a chunk of address space for interpreter */ |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 519 | void* res; |
| 520 | char* base = (char *)info->exe_base; |
| 521 | char* baseoff; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 522 | int flags = MAP_PRIVATE|MAP_ANONYMOUS; |
| 523 | |
| 524 | if (info->map_base != 0) { |
| 525 | base = (char *)info->map_base; |
| 526 | flags |= MAP_FIXED; |
| 527 | } |
| 528 | |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 529 | res = mmap(base, interp_size, PROT_NONE, flags, -1, 0); |
| 530 | check_mmap(res, base, interp_size); |
| 531 | base = res; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 532 | |
| 533 | baseoff = base - interp_addr; |
| 534 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 535 | mapelf(interp, (ESZ(Addr))baseoff); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 536 | |
| 537 | close(interp->fd); |
| 538 | free(interp); |
| 539 | |
| 540 | entry = baseoff + interp->e.e_entry; |
| 541 | info->interp_base = (ESZ(Addr))base; |
| 542 | } else |
fitzhardinge | ca9bd9c | 2004-09-08 20:05:02 +0000 | [diff] [blame] | 543 | entry = (void *)e->e.e_entry + exeoff; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 544 | |
| 545 | info->exe_base = minaddr; |
| 546 | info->exe_end = maxaddr; |
| 547 | |
| 548 | info->init_eip = (addr_t)entry; |
| 549 | |
| 550 | free(e); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | static int match_script(const char *hdr, Int len) |
| 557 | { |
| 558 | return (len > 2) && memcmp(hdr, "#!", 2) == 0; |
| 559 | } |
| 560 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 561 | static int load_script(char *hdr, int len, int fd, const char *name, |
| 562 | struct exeinfo *info) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 563 | { |
| 564 | char *interp; |
| 565 | char *const end = hdr+len; |
| 566 | char *cp; |
| 567 | char *arg = NULL; |
| 568 | int eol; |
| 569 | |
| 570 | interp = hdr + 2; |
| 571 | while(interp < end && (*interp == ' ' || *interp == '\t')) |
| 572 | interp++; |
| 573 | |
| 574 | if (*interp != '/') |
| 575 | return ENOEXEC; /* absolute path only for interpreter */ |
| 576 | |
| 577 | /* skip over interpreter name */ |
| 578 | for(cp = interp; cp < end && *cp != ' ' && *cp != '\t' && *cp != '\n'; cp++) |
| 579 | ; |
| 580 | |
| 581 | eol = (*cp == '\n'); |
| 582 | |
| 583 | *cp++ = '\0'; |
| 584 | |
| 585 | if (!eol && cp < end) { |
| 586 | /* skip space before arg */ |
| 587 | while (cp < end && (*cp == '\t' || *cp == ' ')) |
| 588 | cp++; |
| 589 | |
| 590 | /* arg is from here to eol */ |
| 591 | arg = cp; |
| 592 | while (cp < end && *cp != '\n') |
| 593 | cp++; |
| 594 | *cp = '\0'; |
| 595 | } |
| 596 | |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 597 | info->interp_name = strdup(interp); |
| 598 | assert(NULL != info->interp_name); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 599 | if (arg != NULL && *arg != '\0') { |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 600 | info->interp_args = strdup(arg); |
| 601 | assert(NULL != info->interp_args); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 602 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 603 | |
| 604 | if (info->argv && info->argv[0] != NULL) |
| 605 | info->argv[0] = (char *)name; |
| 606 | |
| 607 | if (0) |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 608 | printf("#! script: interp_name=\"%s\" interp_args=\"%s\"\n", |
| 609 | info->interp_name, info->interp_args); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 610 | |
| 611 | return do_exec_inner(interp, info); |
| 612 | } |
| 613 | |
fitzhardinge | fd7da3a | 2004-09-08 20:05:29 +0000 | [diff] [blame] | 614 | /* |
| 615 | Emulate the normal Unix permissions checking algorithm. |
| 616 | |
| 617 | If owner matches, then use the owner permissions, else |
| 618 | if group matches, then use the group permissions, else |
| 619 | use other permissions. |
| 620 | |
| 621 | Note that we can't deal with SUID/SGID, so we refuse to run them |
| 622 | (otherwise the executable may misbehave if it doesn't have the |
| 623 | permissions it thinks it does). |
| 624 | */ |
| 625 | static int check_perms(int fd) |
| 626 | { |
| 627 | struct stat st; |
| 628 | |
| 629 | if (fstat(fd, &st) == -1) |
| 630 | return errno; |
| 631 | |
| 632 | if (st.st_mode & (S_ISUID | S_ISGID)) { |
| 633 | //fprintf(stderr, "Can't execute suid/sgid executable %s\n", exe); |
| 634 | return EACCES; |
| 635 | } |
| 636 | |
| 637 | if (geteuid() == st.st_uid) { |
| 638 | if (!(st.st_mode & S_IXUSR)) |
| 639 | return EACCES; |
| 640 | } else { |
| 641 | int grpmatch = 0; |
| 642 | |
| 643 | if (getegid() == st.st_gid) |
| 644 | grpmatch = 1; |
| 645 | else { |
| 646 | gid_t groups[32]; |
| 647 | int ngrp = getgroups(32, groups); |
| 648 | int i; |
| 649 | |
| 650 | for(i = 0; i < ngrp; i++) |
| 651 | if (groups[i] == st.st_gid) { |
| 652 | grpmatch = 1; |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (grpmatch) { |
| 658 | if (!(st.st_mode & S_IXGRP)) |
| 659 | return EACCES; |
| 660 | } else if (!(st.st_mode & S_IXOTH)) |
| 661 | return EACCES; |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 667 | static int do_exec_inner(const char *exe, struct exeinfo *info) |
| 668 | { |
| 669 | int fd; |
fitzhardinge | d2e3711 | 2004-09-09 08:10:42 +0000 | [diff] [blame^] | 670 | int err; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 671 | char buf[VKI_BYTES_PER_PAGE]; |
| 672 | int bufsz; |
| 673 | int i; |
| 674 | int ret; |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 675 | static const struct { |
| 676 | int (*match)(const char *hdr, int len); |
| 677 | int (*load) ( char *hdr, int len, int fd2, const char *name, |
| 678 | struct exeinfo *); |
| 679 | } formats[] = { |
| 680 | { match_ELF, load_ELF }, |
| 681 | { match_script, load_script }, |
| 682 | }; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 683 | |
| 684 | fd = open(exe, O_RDONLY); |
| 685 | if (fd == -1) { |
| 686 | if (0) |
| 687 | fprintf(stderr, "Can't open executable %s: %s\n", |
| 688 | exe, strerror(errno)); |
| 689 | return errno; |
| 690 | } |
| 691 | |
fitzhardinge | d2e3711 | 2004-09-09 08:10:42 +0000 | [diff] [blame^] | 692 | err = check_perms(fd); |
fitzhardinge | fd7da3a | 2004-09-08 20:05:29 +0000 | [diff] [blame] | 693 | if (err != 0) { |
| 694 | close(fd); |
| 695 | return err; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | bufsz = pread(fd, buf, sizeof(buf), 0); |
| 699 | if (bufsz < 0) { |
| 700 | fprintf(stderr, "Can't read executable header: %s\n", |
| 701 | strerror(errno)); |
| 702 | close(fd); |
| 703 | return errno; |
| 704 | } |
| 705 | |
| 706 | ret = ENOEXEC; |
| 707 | for(i = 0; i < sizeof(formats)/sizeof(*formats); i++) { |
| 708 | if ((formats[i].match)(buf, bufsz)) { |
| 709 | ret = (formats[i].load)(buf, bufsz, fd, exe, info); |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | close(fd); |
| 715 | |
| 716 | return ret; |
| 717 | } |
| 718 | |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 719 | // See ume.h for an indication of which entries of 'info' are inputs, which |
| 720 | // are outputs, and which are both. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 721 | int do_exec(const char *exe, struct exeinfo *info) |
| 722 | { |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 723 | info->interp_name = NULL; |
| 724 | info->interp_args = NULL; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 725 | |
| 726 | return do_exec_inner(exe, info); |
| 727 | } |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 728 | |
| 729 | /*--------------------------------------------------------------------*/ |
| 730 | /*--- end ume.c ---*/ |
| 731 | /*--------------------------------------------------------------------*/ |