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)) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +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) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame^] | 161 | fprintf(stderr, "valgrind: %s: bad ELF magic number\n", filename); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 164 | if (e->e.e_ident[EI_CLASS] != VGA_ELF_CLASS) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame^] | 165 | fprintf(stderr, |
| 166 | "valgrind: wrong ELF executable class " |
| 167 | "(eg. 32-bit instead of 64-bit)\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 168 | return NULL; |
| 169 | } |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 170 | if (e->e.e_ident[EI_DATA] != VGA_ELF_ENDIANNESS) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame^] | 171 | fprintf(stderr, "valgrind: executable has wrong endian-ness\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 172 | return NULL; |
| 173 | } |
| 174 | if (!(e->e.e_type == ET_EXEC || e->e.e_type == ET_DYN)) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame^] | 175 | fprintf(stderr, "valgrind: this is not an executable\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 179 | if (e->e.e_machine != VGA_ELF_MACHINE) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame^] | 180 | fprintf(stderr, "valgrind: executable is not for " |
| 181 | "this architecture\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | if (e->e.e_phentsize != sizeof(ESZ(Phdr))) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame^] | 186 | fprintf(stderr, "valgrind: sizeof ELF Phdr wrong\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | phsz = sizeof(ESZ(Phdr)) * e->e.e_phnum; |
| 191 | e->p = malloc(phsz); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 192 | assert(e->p); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 193 | |
| 194 | if (pread(fd, e->p, phsz, e->e.e_phoff) != phsz) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 195 | fprintf(stderr, "valgrind: can't read phdr: %s\n", strerror(errno)); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | return e; |
| 200 | } |
| 201 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 202 | /* Map an ELF file. Returns the brk address. */ |
sewardj | 2c5ffbe | 2005-03-12 13:32:06 +0000 | [diff] [blame] | 203 | static |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 204 | ESZ(Addr) mapelf(struct elfinfo *e, ESZ(Addr) base) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 205 | { |
| 206 | int i; |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 207 | void* res; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 208 | ESZ(Addr) elfbrk = 0; |
| 209 | |
| 210 | for(i = 0; i < e->e.e_phnum; i++) { |
| 211 | ESZ(Phdr) *ph = &e->p[i]; |
| 212 | ESZ(Addr) addr, brkaddr; |
| 213 | ESZ(Word) memsz; |
| 214 | |
| 215 | if (ph->p_type != PT_LOAD) |
| 216 | continue; |
| 217 | |
nethercote | 6c3cf41 | 2004-10-26 13:32:11 +0000 | [diff] [blame] | 218 | addr = ph->p_vaddr+base; |
| 219 | memsz = ph->p_memsz; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 220 | brkaddr = addr+memsz; |
| 221 | |
| 222 | if (brkaddr > elfbrk) |
| 223 | elfbrk = brkaddr; |
| 224 | } |
| 225 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 226 | for(i = 0; i < e->e.e_phnum; i++) { |
| 227 | ESZ(Phdr) *ph = &e->p[i]; |
| 228 | ESZ(Addr) addr, bss, brkaddr; |
| 229 | ESZ(Off) off; |
| 230 | ESZ(Word) filesz; |
| 231 | ESZ(Word) memsz; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 232 | unsigned prot = 0; |
| 233 | |
| 234 | if (ph->p_type != PT_LOAD) |
| 235 | continue; |
| 236 | |
nethercote | 6c3cf41 | 2004-10-26 13:32:11 +0000 | [diff] [blame] | 237 | if (ph->p_flags & PF_X) prot |= PROT_EXEC; |
| 238 | if (ph->p_flags & PF_W) prot |= PROT_WRITE; |
| 239 | if (ph->p_flags & PF_R) prot |= PROT_READ; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 240 | |
nethercote | 6c3cf41 | 2004-10-26 13:32:11 +0000 | [diff] [blame] | 241 | addr = ph->p_vaddr+base; |
| 242 | off = ph->p_offset; |
| 243 | filesz = ph->p_filesz; |
| 244 | bss = addr+filesz; |
| 245 | memsz = ph->p_memsz; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 246 | brkaddr = addr+memsz; |
| 247 | |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 248 | // Tom says: In the following, do what the Linux kernel does and only |
| 249 | // map the pages that are required instead of rounding everything to |
| 250 | // the specified alignment (ph->p_align). (AMD64 doesn't work if you |
| 251 | // use ph->p_align -- part of stage2's memory gets trashed somehow.) |
njn | 098da06 | 2005-03-26 16:22:43 +0000 | [diff] [blame] | 252 | // |
| 253 | // The condition handles the case of a zero-length segment. |
| 254 | if (PGROUNDUP(bss)-PGROUNDDN(addr) > 0) { |
| 255 | res = mmap((char *)PGROUNDDN(addr), PGROUNDUP(bss)-PGROUNDDN(addr), |
| 256 | prot, MAP_FIXED|MAP_PRIVATE, e->fd, PGROUNDDN(off)); |
| 257 | check_mmap(res, (char*)PGROUNDDN(addr), |
| 258 | PGROUNDUP(bss)-PGROUNDDN(addr)); |
| 259 | } |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 260 | |
| 261 | // if memsz > filesz, fill the remainder with zeroed pages |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 262 | if (memsz > filesz) { |
| 263 | UInt bytes; |
| 264 | |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 265 | bytes = PGROUNDUP(brkaddr)-PGROUNDUP(bss); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 266 | if (bytes > 0) { |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 267 | res = mmap((char *)PGROUNDUP(bss), bytes, |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 268 | prot, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 269 | check_mmap(res, (char*)PGROUNDUP(bss), bytes); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 270 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 271 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 272 | bytes = bss & (VKI_PAGE_SIZE - 1); |
njn | 098da06 | 2005-03-26 16:22:43 +0000 | [diff] [blame] | 273 | |
| 274 | // The 'prot' condition allows for a read-only bss |
| 275 | if ((prot & PROT_WRITE) && (bytes > 0)) { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 276 | bytes = VKI_PAGE_SIZE - bytes; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 277 | memset((char *)bss, 0, bytes); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return elfbrk; |
| 283 | } |
| 284 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 285 | // Forward declaration. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 286 | static int do_exec_inner(const char *exe, struct exeinfo *info); |
| 287 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 288 | static int match_ELF(const char *hdr, int len) |
| 289 | { |
| 290 | ESZ(Ehdr) *e = (ESZ(Ehdr) *)hdr; |
| 291 | return (len > sizeof(*e)) && memcmp(&e->e_ident[0], ELFMAG, SELFMAG) == 0; |
| 292 | } |
| 293 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 294 | static int load_ELF(char *hdr, int len, int fd, const char *name, |
| 295 | struct exeinfo *info) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 296 | { |
| 297 | struct elfinfo *e; |
| 298 | struct elfinfo *interp = NULL; |
fitzhardinge | ca9bd9c | 2004-09-08 20:05:02 +0000 | [diff] [blame] | 299 | ESZ(Addr) minaddr = ~0; /* lowest mapped address */ |
| 300 | ESZ(Addr) maxaddr = 0; /* highest mapped address */ |
| 301 | ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */ |
| 302 | ESZ(Word) interp_size = 0; /* interpreter size */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 303 | ESZ(Word) interp_align = VKI_PAGE_SIZE; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 304 | int i; |
| 305 | void *entry; |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 306 | ESZ(Addr) ebase = 0; |
| 307 | |
| 308 | #ifdef HAVE_PIE |
| 309 | ebase = info->exe_base; |
| 310 | #endif |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 311 | |
| 312 | e = readelf(fd, name); |
| 313 | |
| 314 | if (e == NULL) |
| 315 | return ENOEXEC; |
| 316 | |
| 317 | info->phnum = e->e.e_phnum; |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 318 | info->entry = e->e.e_entry + ebase; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 319 | info->phdr = 0; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 320 | |
| 321 | for(i = 0; i < e->e.e_phnum; i++) { |
| 322 | ESZ(Phdr) *ph = &e->p[i]; |
| 323 | |
| 324 | switch(ph->p_type) { |
| 325 | case PT_PHDR: |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 326 | info->phdr = ph->p_vaddr + ebase; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 327 | break; |
| 328 | |
| 329 | case PT_LOAD: |
| 330 | if (ph->p_vaddr < minaddr) |
| 331 | minaddr = ph->p_vaddr; |
| 332 | if (ph->p_vaddr+ph->p_memsz > maxaddr) |
| 333 | maxaddr = ph->p_vaddr+ph->p_memsz; |
| 334 | break; |
| 335 | |
| 336 | case PT_INTERP: { |
| 337 | char *buf = malloc(ph->p_filesz+1); |
| 338 | int j; |
| 339 | int intfd; |
| 340 | int baseaddr_set; |
| 341 | |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 342 | assert(buf); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 343 | pread(fd, buf, ph->p_filesz, ph->p_offset); |
| 344 | buf[ph->p_filesz] = '\0'; |
| 345 | |
| 346 | intfd = open(buf, O_RDONLY); |
| 347 | if (intfd == -1) { |
| 348 | perror("open interp"); |
| 349 | exit(1); |
| 350 | } |
| 351 | |
| 352 | interp = readelf(intfd, buf); |
| 353 | if (interp == NULL) { |
| 354 | fprintf(stderr, "Can't read interpreter\n"); |
| 355 | return 1; |
| 356 | } |
| 357 | free(buf); |
| 358 | |
| 359 | baseaddr_set = 0; |
| 360 | for(j = 0; j < interp->e.e_phnum; j++) { |
| 361 | ESZ(Phdr) *iph = &interp->p[j]; |
| 362 | ESZ(Addr) end; |
| 363 | |
| 364 | if (iph->p_type != PT_LOAD) |
| 365 | continue; |
| 366 | |
| 367 | if (!baseaddr_set) { |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 368 | interp_addr = iph->p_vaddr; |
| 369 | interp_align = iph->p_align; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 370 | baseaddr_set = 1; |
| 371 | } |
| 372 | |
| 373 | /* assumes that all segments in the interp are close */ |
| 374 | end = (iph->p_vaddr - interp_addr) + iph->p_memsz; |
| 375 | |
| 376 | if (end > interp_size) |
| 377 | interp_size = end; |
| 378 | } |
| 379 | break; |
nethercote | b24cbc8 | 2004-09-03 23:25:33 +0000 | [diff] [blame] | 380 | |
| 381 | default: |
| 382 | // do nothing |
| 383 | break; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 388 | if (info->phdr == 0) |
| 389 | info->phdr = minaddr + e->e.e_phoff; |
| 390 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 391 | if (info->exe_base != info->exe_end) { |
| 392 | if (minaddr >= maxaddr || |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 393 | (minaddr + ebase < info->exe_base || |
| 394 | maxaddr + ebase > info->exe_end)) { |
nethercote | 2e1cb4c | 2004-08-05 12:16:13 +0000 | [diff] [blame] | 395 | fprintf(stderr, "Executable range %p-%p is outside the\n" |
| 396 | "acceptable range %p-%p\n", |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 397 | (void *)minaddr + ebase, (void *)maxaddr + ebase, |
| 398 | (void *)info->exe_base, (void *)info->exe_end); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 399 | return ENOMEM; |
| 400 | } |
| 401 | } |
| 402 | |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 403 | info->brkbase = mapelf(e, ebase); /* map the executable */ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 404 | |
fitzhardinge | 9236079 | 2003-12-24 10:11:11 +0000 | [diff] [blame] | 405 | if (info->brkbase == 0) |
| 406 | return ENOMEM; |
| 407 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 408 | if (interp != NULL) { |
| 409 | /* reserve a chunk of address space for interpreter */ |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 410 | void* res; |
| 411 | char* base = (char *)info->exe_base; |
| 412 | char* baseoff; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 413 | int flags = MAP_PRIVATE|MAP_ANONYMOUS; |
| 414 | |
| 415 | if (info->map_base != 0) { |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 416 | base = (char *)ROUNDUP(info->map_base, interp_align); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 417 | flags |= MAP_FIXED; |
| 418 | } |
| 419 | |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 420 | res = mmap(base, interp_size, PROT_NONE, flags, -1, 0); |
| 421 | check_mmap(res, base, interp_size); |
| 422 | base = res; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 423 | |
| 424 | baseoff = base - interp_addr; |
| 425 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 426 | mapelf(interp, (ESZ(Addr))baseoff); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 427 | |
| 428 | close(interp->fd); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 429 | |
| 430 | entry = baseoff + interp->e.e_entry; |
| 431 | info->interp_base = (ESZ(Addr))base; |
thughes | 54d0859 | 2004-09-26 14:42:47 +0000 | [diff] [blame] | 432 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 433 | free(interp->p); |
thughes | 54d0859 | 2004-09-26 14:42:47 +0000 | [diff] [blame] | 434 | free(interp); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 435 | } else |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 436 | entry = (void *)e->e.e_entry; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 437 | |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 438 | info->exe_base = minaddr + ebase; |
| 439 | info->exe_end = maxaddr + ebase; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 440 | |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 441 | info->init_eip = (Addr)entry; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 442 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 443 | free(e->p); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 444 | free(e); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static int match_script(const char *hdr, Int len) |
| 451 | { |
| 452 | return (len > 2) && memcmp(hdr, "#!", 2) == 0; |
| 453 | } |
| 454 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 455 | static int load_script(char *hdr, int len, int fd, const char *name, |
| 456 | struct exeinfo *info) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 457 | { |
| 458 | char *interp; |
| 459 | char *const end = hdr+len; |
| 460 | char *cp; |
| 461 | char *arg = NULL; |
| 462 | int eol; |
| 463 | |
| 464 | interp = hdr + 2; |
| 465 | while(interp < end && (*interp == ' ' || *interp == '\t')) |
| 466 | interp++; |
| 467 | |
| 468 | if (*interp != '/') |
| 469 | return ENOEXEC; /* absolute path only for interpreter */ |
| 470 | |
| 471 | /* skip over interpreter name */ |
| 472 | for(cp = interp; cp < end && *cp != ' ' && *cp != '\t' && *cp != '\n'; cp++) |
| 473 | ; |
| 474 | |
| 475 | eol = (*cp == '\n'); |
| 476 | |
| 477 | *cp++ = '\0'; |
| 478 | |
| 479 | if (!eol && cp < end) { |
| 480 | /* skip space before arg */ |
| 481 | while (cp < end && (*cp == '\t' || *cp == ' ')) |
| 482 | cp++; |
| 483 | |
| 484 | /* arg is from here to eol */ |
| 485 | arg = cp; |
| 486 | while (cp < end && *cp != '\n') |
| 487 | cp++; |
| 488 | *cp = '\0'; |
| 489 | } |
| 490 | |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 491 | info->interp_name = strdup(interp); |
| 492 | assert(NULL != info->interp_name); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 493 | if (arg != NULL && *arg != '\0') { |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 494 | info->interp_args = strdup(arg); |
| 495 | assert(NULL != info->interp_args); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 496 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 497 | |
| 498 | if (info->argv && info->argv[0] != NULL) |
| 499 | info->argv[0] = (char *)name; |
| 500 | |
| 501 | if (0) |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 502 | printf("#! script: interp_name=\"%s\" interp_args=\"%s\"\n", |
| 503 | info->interp_name, info->interp_args); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 504 | |
| 505 | return do_exec_inner(interp, info); |
| 506 | } |
| 507 | |
fitzhardinge | fd7da3a | 2004-09-08 20:05:29 +0000 | [diff] [blame] | 508 | /* |
| 509 | Emulate the normal Unix permissions checking algorithm. |
| 510 | |
| 511 | If owner matches, then use the owner permissions, else |
| 512 | if group matches, then use the group permissions, else |
| 513 | use other permissions. |
| 514 | |
| 515 | Note that we can't deal with SUID/SGID, so we refuse to run them |
| 516 | (otherwise the executable may misbehave if it doesn't have the |
| 517 | permissions it thinks it does). |
| 518 | */ |
| 519 | static int check_perms(int fd) |
| 520 | { |
| 521 | struct stat st; |
| 522 | |
| 523 | if (fstat(fd, &st) == -1) |
| 524 | return errno; |
| 525 | |
| 526 | if (st.st_mode & (S_ISUID | S_ISGID)) { |
| 527 | //fprintf(stderr, "Can't execute suid/sgid executable %s\n", exe); |
| 528 | return EACCES; |
| 529 | } |
| 530 | |
| 531 | if (geteuid() == st.st_uid) { |
| 532 | if (!(st.st_mode & S_IXUSR)) |
| 533 | return EACCES; |
| 534 | } else { |
| 535 | int grpmatch = 0; |
| 536 | |
| 537 | if (getegid() == st.st_gid) |
| 538 | grpmatch = 1; |
| 539 | else { |
| 540 | gid_t groups[32]; |
| 541 | int ngrp = getgroups(32, groups); |
| 542 | int i; |
| 543 | |
| 544 | for(i = 0; i < ngrp; i++) |
| 545 | if (groups[i] == st.st_gid) { |
| 546 | grpmatch = 1; |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (grpmatch) { |
| 552 | if (!(st.st_mode & S_IXGRP)) |
| 553 | return EACCES; |
| 554 | } else if (!(st.st_mode & S_IXOTH)) |
| 555 | return EACCES; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 561 | static int do_exec_inner(const char *exe, struct exeinfo *info) |
| 562 | { |
| 563 | int fd; |
fitzhardinge | d2e3711 | 2004-09-09 08:10:42 +0000 | [diff] [blame] | 564 | int err; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 565 | char buf[VKI_PAGE_SIZE]; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 566 | int bufsz; |
| 567 | int i; |
| 568 | int ret; |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 569 | static const struct { |
| 570 | int (*match)(const char *hdr, int len); |
| 571 | int (*load) ( char *hdr, int len, int fd2, const char *name, |
| 572 | struct exeinfo *); |
| 573 | } formats[] = { |
| 574 | { match_ELF, load_ELF }, |
| 575 | { match_script, load_script }, |
| 576 | }; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 577 | |
| 578 | fd = open(exe, O_RDONLY); |
| 579 | if (fd == -1) { |
| 580 | if (0) |
| 581 | fprintf(stderr, "Can't open executable %s: %s\n", |
| 582 | exe, strerror(errno)); |
| 583 | return errno; |
| 584 | } |
| 585 | |
fitzhardinge | d2e3711 | 2004-09-09 08:10:42 +0000 | [diff] [blame] | 586 | err = check_perms(fd); |
fitzhardinge | fd7da3a | 2004-09-08 20:05:29 +0000 | [diff] [blame] | 587 | if (err != 0) { |
| 588 | close(fd); |
| 589 | return err; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | bufsz = pread(fd, buf, sizeof(buf), 0); |
| 593 | if (bufsz < 0) { |
| 594 | fprintf(stderr, "Can't read executable header: %s\n", |
| 595 | strerror(errno)); |
| 596 | close(fd); |
| 597 | return errno; |
| 598 | } |
| 599 | |
| 600 | ret = ENOEXEC; |
| 601 | for(i = 0; i < sizeof(formats)/sizeof(*formats); i++) { |
| 602 | if ((formats[i].match)(buf, bufsz)) { |
| 603 | ret = (formats[i].load)(buf, bufsz, fd, exe, info); |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | close(fd); |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 613 | // See ume.h for an indication of which entries of 'info' are inputs, which |
| 614 | // are outputs, and which are both. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 615 | int do_exec(const char *exe, struct exeinfo *info) |
| 616 | { |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 617 | info->interp_name = NULL; |
| 618 | info->interp_args = NULL; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 619 | |
| 620 | return do_exec_inner(exe, info); |
| 621 | } |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 622 | |
| 623 | /*--------------------------------------------------------------------*/ |
| 624 | /*--- end ume.c ---*/ |
| 625 | /*--------------------------------------------------------------------*/ |