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