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 | |
njn | f844cb6 | 2005-06-19 16:07:49 +0000 | [diff] [blame^] | 36 | // It seems that on SuSE 9.1 (x86) something in <fcntl.h> messes up stuff |
| 37 | // acquired indirectly from vki-x86-linux.h. Therefore our headers must be |
| 38 | // included ahead of the glibc ones. This fix is a kludge; the right |
| 39 | // solution is to entirely remove the glibc dependency. |
sewardj | 4b0c071 | 2005-06-19 15:58:33 +0000 | [diff] [blame] | 40 | #include "pub_core_basics.h" |
| 41 | #include "pub_core_libcbase.h" |
| 42 | #include "pub_core_machine.h" |
| 43 | #include "ume.h" |
| 44 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 45 | #include <sys/mman.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <errno.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 48 | #include <stdio.h> |
| 49 | #include <string.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <unistd.h> |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 52 | #include <assert.h> |
| 53 | |
nethercote | 3f45815 | 2004-11-01 18:42:23 +0000 | [diff] [blame] | 54 | #if ELFSZ == 64 |
| 55 | #define ESZ(x) Elf64_##x |
| 56 | #elif ELFSZ == 32 |
| 57 | #define ESZ(x) Elf32_##x |
| 58 | #else |
| 59 | #error ELFSZ needs to ==32 or ==64 |
| 60 | #endif |
| 61 | |
nethercote | 1fe5450 | 2004-07-26 15:28:33 +0000 | [diff] [blame] | 62 | struct elfinfo |
| 63 | { |
| 64 | ESZ(Ehdr) e; |
| 65 | ESZ(Phdr) *p; |
| 66 | int fd; |
| 67 | }; |
| 68 | |
nethercote | 30d3784 | 2004-07-26 10:05:55 +0000 | [diff] [blame] | 69 | static void check_mmap(void* res, void* base, int len) |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 70 | { |
| 71 | if ((void*)-1 == res) { |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 72 | fprintf(stderr, "valgrind: mmap(%p, %d) failed in UME.\n", base, len); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 73 | exit(1); |
| 74 | } |
| 75 | } |
| 76 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 77 | // 'extra' allows the caller to pass in extra args to 'fn', like free |
| 78 | // variables to a closure. |
| 79 | void foreach_map(int (*fn)(char *start, char *end, |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 80 | const char *perm, off_t offset, |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 81 | int maj, int min, int ino, void* extra), |
| 82 | void* extra) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 83 | { |
| 84 | static char buf[10240]; |
| 85 | char *bufptr = buf; |
| 86 | int ret, fd; |
| 87 | |
| 88 | fd = open("/proc/self/maps", O_RDONLY); |
| 89 | |
| 90 | if (fd == -1) { |
| 91 | perror("open /proc/self/maps"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | ret = read(fd, buf, sizeof(buf)); |
| 96 | |
| 97 | if (ret == -1) { |
| 98 | perror("read /proc/self/maps"); |
| 99 | close(fd); |
| 100 | return; |
| 101 | } |
| 102 | close(fd); |
| 103 | |
| 104 | if (ret == sizeof(buf)) { |
| 105 | fprintf(stderr, "buf too small\n"); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | while(bufptr && bufptr < buf+ret) { |
| 110 | char perm[5]; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 111 | ULong offset; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 112 | int maj, min; |
| 113 | int ino; |
| 114 | void *segstart, *segend; |
| 115 | |
nethercote | 545fe67 | 2004-11-01 16:52:43 +0000 | [diff] [blame] | 116 | sscanf(bufptr, "%p-%p %s %llx %x:%x %d", |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 117 | &segstart, &segend, perm, &offset, &maj, &min, &ino); |
| 118 | bufptr = strchr(bufptr, '\n'); |
| 119 | if (bufptr != NULL) |
| 120 | bufptr++; /* skip \n */ |
| 121 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 122 | if (!(*fn)(segstart, segend, perm, offset, maj, min, ino, extra)) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 127 | /*------------------------------------------------------------*/ |
njn | fcb7c3e | 2005-06-18 15:54:25 +0000 | [diff] [blame] | 128 | /*--- Stack switching ---*/ |
| 129 | /*------------------------------------------------------------*/ |
| 130 | |
| 131 | // __attribute__((noreturn)) |
| 132 | // void jump_and_switch_stacks ( Addr stack, Addr dst ); |
| 133 | #if defined(VGA_x86) |
| 134 | // 4(%esp) == stack |
| 135 | // 8(%esp) == dst |
| 136 | asm( |
| 137 | ".global jump_and_switch_stacks\n" |
| 138 | "jump_and_switch_stacks:\n" |
| 139 | " movl %esp, %esi\n" // remember old stack pointer |
| 140 | " movl 4(%esi), %esp\n" // set stack |
| 141 | " pushl 8(%esi)\n" // dst to stack |
| 142 | " movl $0, %eax\n" // zero all GP regs |
| 143 | " movl $0, %ebx\n" |
| 144 | " movl $0, %ecx\n" |
| 145 | " movl $0, %edx\n" |
| 146 | " movl $0, %esi\n" |
| 147 | " movl $0, %edi\n" |
| 148 | " movl $0, %ebp\n" |
| 149 | " ret\n" // jump to dst |
| 150 | " ud2\n" // should never get here |
| 151 | ); |
| 152 | #elif defined(VGA_amd64) |
| 153 | // %rdi == stack |
| 154 | // %rsi == dst |
| 155 | asm( |
| 156 | ".global jump_and_switch_stacks\n" |
| 157 | "jump_and_switch_stacks:\n" |
| 158 | " movq %rdi, %rsp\n" // set stack |
| 159 | " pushq %rsi\n" // dst to stack |
| 160 | " movq $0, %rax\n" // zero all GP regs |
| 161 | " movq $0, %rbx\n" |
| 162 | " movq $0, %rcx\n" |
| 163 | " movq $0, %rdx\n" |
| 164 | " movq $0, %rsi\n" |
| 165 | " movq $0, %rdi\n" |
| 166 | " movq $0, %rbp\n" |
| 167 | " movq $0, %r8\n"\ |
| 168 | " movq $0, %r9\n"\ |
| 169 | " movq $0, %r10\n" |
| 170 | " movq $0, %r11\n" |
| 171 | " movq $0, %r12\n" |
| 172 | " movq $0, %r13\n" |
| 173 | " movq $0, %r14\n" |
| 174 | " movq $0, %r15\n" |
| 175 | " ret\n" // jump to dst |
| 176 | " ud2\n" // should never get here |
| 177 | ); |
| 178 | #else |
| 179 | # error Unknown architecture |
| 180 | #endif |
| 181 | |
| 182 | /*------------------------------------------------------------*/ |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 183 | /*--- Finding auxv on the stack ---*/ |
| 184 | /*------------------------------------------------------------*/ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 185 | |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 186 | struct ume_auxv *find_auxv(UWord* sp) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 187 | { |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 188 | sp++; // skip argc (Nb: is word-sized, not int-sized!) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 189 | |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 190 | while (*sp != 0) // skip argv |
| 191 | sp++; |
| 192 | sp++; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 193 | |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 194 | while (*sp != 0) // skip env |
| 195 | sp++; |
| 196 | sp++; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 197 | |
nethercote | ebf1d86 | 2004-11-01 18:22:05 +0000 | [diff] [blame] | 198 | return (struct ume_auxv *)sp; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 199 | } |
| 200 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 201 | /*------------------------------------------------------------*/ |
| 202 | /*--- Loading ELF files ---*/ |
| 203 | /*------------------------------------------------------------*/ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 204 | |
sewardj | 2c5ffbe | 2005-03-12 13:32:06 +0000 | [diff] [blame] | 205 | static |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 206 | struct elfinfo *readelf(int fd, const char *filename) |
| 207 | { |
| 208 | struct elfinfo *e = malloc(sizeof(*e)); |
| 209 | int phsz; |
| 210 | |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 211 | assert(e); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 212 | e->fd = fd; |
| 213 | |
| 214 | if (pread(fd, &e->e, sizeof(e->e), 0) != sizeof(e->e)) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 215 | fprintf(stderr, "valgrind: %s: can't read ELF header: %s\n", |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 216 | filename, strerror(errno)); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | if (memcmp(&e->e.e_ident[0], ELFMAG, SELFMAG) != 0) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 221 | fprintf(stderr, "valgrind: %s: bad ELF magic number\n", filename); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 222 | return NULL; |
| 223 | } |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 224 | if (e->e.e_ident[EI_CLASS] != VGA_ELF_CLASS) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 225 | fprintf(stderr, |
| 226 | "valgrind: wrong ELF executable class " |
| 227 | "(eg. 32-bit instead of 64-bit)\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 228 | return NULL; |
| 229 | } |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 230 | if (e->e.e_ident[EI_DATA] != VGA_ELF_ENDIANNESS) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 231 | fprintf(stderr, "valgrind: executable has wrong endian-ness\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | if (!(e->e.e_type == ET_EXEC || e->e.e_type == ET_DYN)) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 235 | fprintf(stderr, "valgrind: this is not an executable\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 236 | return NULL; |
| 237 | } |
| 238 | |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 239 | if (e->e.e_machine != VGA_ELF_MACHINE) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 240 | fprintf(stderr, "valgrind: executable is not for " |
| 241 | "this architecture\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | if (e->e.e_phentsize != sizeof(ESZ(Phdr))) { |
sewardj | 703eec5 | 2005-04-07 02:24:23 +0000 | [diff] [blame] | 246 | fprintf(stderr, "valgrind: sizeof ELF Phdr wrong\n"); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | phsz = sizeof(ESZ(Phdr)) * e->e.e_phnum; |
| 251 | e->p = malloc(phsz); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 252 | assert(e->p); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 253 | |
| 254 | if (pread(fd, e->p, phsz, e->e.e_phoff) != phsz) { |
nethercote | 08eaff3 | 2004-07-22 12:41:12 +0000 | [diff] [blame] | 255 | fprintf(stderr, "valgrind: can't read phdr: %s\n", strerror(errno)); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | return e; |
| 260 | } |
| 261 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 262 | /* Map an ELF file. Returns the brk address. */ |
sewardj | 2c5ffbe | 2005-03-12 13:32:06 +0000 | [diff] [blame] | 263 | static |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 264 | ESZ(Addr) mapelf(struct elfinfo *e, ESZ(Addr) base) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 265 | { |
| 266 | int i; |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 267 | void* res; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 268 | ESZ(Addr) elfbrk = 0; |
| 269 | |
| 270 | for(i = 0; i < e->e.e_phnum; i++) { |
| 271 | ESZ(Phdr) *ph = &e->p[i]; |
| 272 | ESZ(Addr) addr, brkaddr; |
| 273 | ESZ(Word) memsz; |
| 274 | |
| 275 | if (ph->p_type != PT_LOAD) |
| 276 | continue; |
| 277 | |
nethercote | 6c3cf41 | 2004-10-26 13:32:11 +0000 | [diff] [blame] | 278 | addr = ph->p_vaddr+base; |
| 279 | memsz = ph->p_memsz; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 280 | brkaddr = addr+memsz; |
| 281 | |
| 282 | if (brkaddr > elfbrk) |
| 283 | elfbrk = brkaddr; |
| 284 | } |
| 285 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 286 | for(i = 0; i < e->e.e_phnum; i++) { |
| 287 | ESZ(Phdr) *ph = &e->p[i]; |
| 288 | ESZ(Addr) addr, bss, brkaddr; |
| 289 | ESZ(Off) off; |
| 290 | ESZ(Word) filesz; |
| 291 | ESZ(Word) memsz; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 292 | unsigned prot = 0; |
| 293 | |
| 294 | if (ph->p_type != PT_LOAD) |
| 295 | continue; |
| 296 | |
nethercote | 6c3cf41 | 2004-10-26 13:32:11 +0000 | [diff] [blame] | 297 | if (ph->p_flags & PF_X) prot |= PROT_EXEC; |
| 298 | if (ph->p_flags & PF_W) prot |= PROT_WRITE; |
| 299 | if (ph->p_flags & PF_R) prot |= PROT_READ; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 300 | |
nethercote | 6c3cf41 | 2004-10-26 13:32:11 +0000 | [diff] [blame] | 301 | addr = ph->p_vaddr+base; |
| 302 | off = ph->p_offset; |
| 303 | filesz = ph->p_filesz; |
| 304 | bss = addr+filesz; |
| 305 | memsz = ph->p_memsz; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 306 | brkaddr = addr+memsz; |
| 307 | |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 308 | // Tom says: In the following, do what the Linux kernel does and only |
| 309 | // map the pages that are required instead of rounding everything to |
| 310 | // the specified alignment (ph->p_align). (AMD64 doesn't work if you |
| 311 | // use ph->p_align -- part of stage2's memory gets trashed somehow.) |
njn | 098da06 | 2005-03-26 16:22:43 +0000 | [diff] [blame] | 312 | // |
| 313 | // The condition handles the case of a zero-length segment. |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 314 | if (VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr) > 0) { |
| 315 | res = mmap((char *)VG_PGROUNDDN(addr), |
| 316 | VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr), |
| 317 | prot, MAP_FIXED|MAP_PRIVATE, e->fd, VG_PGROUNDDN(off)); |
| 318 | check_mmap(res, (char*)VG_PGROUNDDN(addr), |
| 319 | VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr)); |
njn | 098da06 | 2005-03-26 16:22:43 +0000 | [diff] [blame] | 320 | } |
njn | f7fbe6c | 2004-11-30 11:40:24 +0000 | [diff] [blame] | 321 | |
| 322 | // if memsz > filesz, fill the remainder with zeroed pages |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 323 | if (memsz > filesz) { |
| 324 | UInt bytes; |
| 325 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 326 | bytes = VG_PGROUNDUP(brkaddr)-VG_PGROUNDUP(bss); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 327 | if (bytes > 0) { |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 328 | res = mmap((char *)VG_PGROUNDUP(bss), bytes, |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 329 | prot, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 330 | check_mmap(res, (char*)VG_PGROUNDUP(bss), bytes); |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 331 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 332 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 333 | bytes = bss & (VKI_PAGE_SIZE - 1); |
njn | 098da06 | 2005-03-26 16:22:43 +0000 | [diff] [blame] | 334 | |
| 335 | // The 'prot' condition allows for a read-only bss |
| 336 | if ((prot & PROT_WRITE) && (bytes > 0)) { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 337 | bytes = VKI_PAGE_SIZE - bytes; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 338 | memset((char *)bss, 0, bytes); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return elfbrk; |
| 344 | } |
| 345 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 346 | // Forward declaration. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 347 | static int do_exec_inner(const char *exe, struct exeinfo *info); |
| 348 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 349 | static int match_ELF(const char *hdr, int len) |
| 350 | { |
| 351 | ESZ(Ehdr) *e = (ESZ(Ehdr) *)hdr; |
| 352 | return (len > sizeof(*e)) && memcmp(&e->e_ident[0], ELFMAG, SELFMAG) == 0; |
| 353 | } |
| 354 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 355 | static int load_ELF(char *hdr, int len, int fd, const char *name, |
| 356 | struct exeinfo *info) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 357 | { |
| 358 | struct elfinfo *e; |
| 359 | struct elfinfo *interp = NULL; |
fitzhardinge | ca9bd9c | 2004-09-08 20:05:02 +0000 | [diff] [blame] | 360 | ESZ(Addr) minaddr = ~0; /* lowest mapped address */ |
| 361 | ESZ(Addr) maxaddr = 0; /* highest mapped address */ |
| 362 | ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */ |
| 363 | ESZ(Word) interp_size = 0; /* interpreter size */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 364 | ESZ(Word) interp_align = VKI_PAGE_SIZE; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 365 | int i; |
| 366 | void *entry; |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 367 | ESZ(Addr) ebase = 0; |
| 368 | |
| 369 | #ifdef HAVE_PIE |
| 370 | ebase = info->exe_base; |
| 371 | #endif |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 372 | |
| 373 | e = readelf(fd, name); |
| 374 | |
| 375 | if (e == NULL) |
| 376 | return ENOEXEC; |
| 377 | |
| 378 | info->phnum = e->e.e_phnum; |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 379 | info->entry = e->e.e_entry + ebase; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 380 | info->phdr = 0; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 381 | |
| 382 | for(i = 0; i < e->e.e_phnum; i++) { |
| 383 | ESZ(Phdr) *ph = &e->p[i]; |
| 384 | |
| 385 | switch(ph->p_type) { |
| 386 | case PT_PHDR: |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 387 | info->phdr = ph->p_vaddr + ebase; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case PT_LOAD: |
| 391 | if (ph->p_vaddr < minaddr) |
| 392 | minaddr = ph->p_vaddr; |
| 393 | if (ph->p_vaddr+ph->p_memsz > maxaddr) |
| 394 | maxaddr = ph->p_vaddr+ph->p_memsz; |
| 395 | break; |
| 396 | |
| 397 | case PT_INTERP: { |
| 398 | char *buf = malloc(ph->p_filesz+1); |
| 399 | int j; |
| 400 | int intfd; |
| 401 | int baseaddr_set; |
| 402 | |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 403 | assert(buf); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 404 | pread(fd, buf, ph->p_filesz, ph->p_offset); |
| 405 | buf[ph->p_filesz] = '\0'; |
| 406 | |
| 407 | intfd = open(buf, O_RDONLY); |
| 408 | if (intfd == -1) { |
| 409 | perror("open interp"); |
| 410 | exit(1); |
| 411 | } |
| 412 | |
| 413 | interp = readelf(intfd, buf); |
| 414 | if (interp == NULL) { |
| 415 | fprintf(stderr, "Can't read interpreter\n"); |
| 416 | return 1; |
| 417 | } |
| 418 | free(buf); |
| 419 | |
| 420 | baseaddr_set = 0; |
| 421 | for(j = 0; j < interp->e.e_phnum; j++) { |
| 422 | ESZ(Phdr) *iph = &interp->p[j]; |
| 423 | ESZ(Addr) end; |
| 424 | |
| 425 | if (iph->p_type != PT_LOAD) |
| 426 | continue; |
| 427 | |
| 428 | if (!baseaddr_set) { |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 429 | interp_addr = iph->p_vaddr; |
| 430 | interp_align = iph->p_align; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 431 | baseaddr_set = 1; |
| 432 | } |
| 433 | |
| 434 | /* assumes that all segments in the interp are close */ |
| 435 | end = (iph->p_vaddr - interp_addr) + iph->p_memsz; |
| 436 | |
| 437 | if (end > interp_size) |
| 438 | interp_size = end; |
| 439 | } |
| 440 | break; |
nethercote | b24cbc8 | 2004-09-03 23:25:33 +0000 | [diff] [blame] | 441 | |
| 442 | default: |
| 443 | // do nothing |
| 444 | break; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 449 | if (info->phdr == 0) |
| 450 | info->phdr = minaddr + e->e.e_phoff; |
| 451 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 452 | if (info->exe_base != info->exe_end) { |
| 453 | if (minaddr >= maxaddr || |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 454 | (minaddr + ebase < info->exe_base || |
| 455 | maxaddr + ebase > info->exe_end)) { |
nethercote | 2e1cb4c | 2004-08-05 12:16:13 +0000 | [diff] [blame] | 456 | fprintf(stderr, "Executable range %p-%p is outside the\n" |
| 457 | "acceptable range %p-%p\n", |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 458 | (void *)minaddr + ebase, (void *)maxaddr + ebase, |
| 459 | (void *)info->exe_base, (void *)info->exe_end); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 460 | return ENOMEM; |
| 461 | } |
| 462 | } |
| 463 | |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 464 | info->brkbase = mapelf(e, ebase); /* map the executable */ |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 465 | |
fitzhardinge | 9236079 | 2003-12-24 10:11:11 +0000 | [diff] [blame] | 466 | if (info->brkbase == 0) |
| 467 | return ENOMEM; |
| 468 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 469 | if (interp != NULL) { |
| 470 | /* reserve a chunk of address space for interpreter */ |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 471 | void* res; |
| 472 | char* base = (char *)info->exe_base; |
| 473 | char* baseoff; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 474 | int flags = MAP_PRIVATE|MAP_ANONYMOUS; |
| 475 | |
| 476 | if (info->map_base != 0) { |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 477 | base = (char *)VG_ROUNDUP(info->map_base, interp_align); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 478 | flags |= MAP_FIXED; |
| 479 | } |
| 480 | |
nethercote | bfed1c8 | 2004-07-17 12:57:44 +0000 | [diff] [blame] | 481 | res = mmap(base, interp_size, PROT_NONE, flags, -1, 0); |
| 482 | check_mmap(res, base, interp_size); |
| 483 | base = res; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 484 | |
| 485 | baseoff = base - interp_addr; |
| 486 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 487 | mapelf(interp, (ESZ(Addr))baseoff); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 488 | |
| 489 | close(interp->fd); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 490 | |
| 491 | entry = baseoff + interp->e.e_entry; |
| 492 | info->interp_base = (ESZ(Addr))base; |
thughes | 54d0859 | 2004-09-26 14:42:47 +0000 | [diff] [blame] | 493 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 494 | free(interp->p); |
thughes | 54d0859 | 2004-09-26 14:42:47 +0000 | [diff] [blame] | 495 | free(interp); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 496 | } else |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 497 | entry = (void *)e->e.e_entry; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 498 | |
nethercote | 7f39002 | 2004-10-25 17:18:24 +0000 | [diff] [blame] | 499 | info->exe_base = minaddr + ebase; |
| 500 | info->exe_end = maxaddr + ebase; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 501 | |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 502 | info->init_eip = (Addr)entry; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 503 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 504 | free(e->p); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 505 | free(e); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | static int match_script(const char *hdr, Int len) |
| 512 | { |
| 513 | return (len > 2) && memcmp(hdr, "#!", 2) == 0; |
| 514 | } |
| 515 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 516 | static int load_script(char *hdr, int len, int fd, const char *name, |
| 517 | struct exeinfo *info) |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 518 | { |
| 519 | char *interp; |
| 520 | char *const end = hdr+len; |
| 521 | char *cp; |
| 522 | char *arg = NULL; |
| 523 | int eol; |
| 524 | |
| 525 | interp = hdr + 2; |
| 526 | while(interp < end && (*interp == ' ' || *interp == '\t')) |
| 527 | interp++; |
| 528 | |
| 529 | if (*interp != '/') |
| 530 | return ENOEXEC; /* absolute path only for interpreter */ |
| 531 | |
| 532 | /* skip over interpreter name */ |
| 533 | for(cp = interp; cp < end && *cp != ' ' && *cp != '\t' && *cp != '\n'; cp++) |
| 534 | ; |
| 535 | |
| 536 | eol = (*cp == '\n'); |
| 537 | |
| 538 | *cp++ = '\0'; |
| 539 | |
| 540 | if (!eol && cp < end) { |
| 541 | /* skip space before arg */ |
| 542 | while (cp < end && (*cp == '\t' || *cp == ' ')) |
| 543 | cp++; |
| 544 | |
| 545 | /* arg is from here to eol */ |
| 546 | arg = cp; |
| 547 | while (cp < end && *cp != '\n') |
| 548 | cp++; |
| 549 | *cp = '\0'; |
| 550 | } |
| 551 | |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 552 | info->interp_name = strdup(interp); |
| 553 | assert(NULL != info->interp_name); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 554 | if (arg != NULL && *arg != '\0') { |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 555 | info->interp_args = strdup(arg); |
| 556 | assert(NULL != info->interp_args); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 557 | } |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 558 | |
| 559 | if (info->argv && info->argv[0] != NULL) |
| 560 | info->argv[0] = (char *)name; |
| 561 | |
| 562 | if (0) |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 563 | printf("#! script: interp_name=\"%s\" interp_args=\"%s\"\n", |
| 564 | info->interp_name, info->interp_args); |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 565 | |
| 566 | return do_exec_inner(interp, info); |
| 567 | } |
| 568 | |
fitzhardinge | fd7da3a | 2004-09-08 20:05:29 +0000 | [diff] [blame] | 569 | /* |
| 570 | Emulate the normal Unix permissions checking algorithm. |
| 571 | |
| 572 | If owner matches, then use the owner permissions, else |
| 573 | if group matches, then use the group permissions, else |
| 574 | use other permissions. |
| 575 | |
| 576 | Note that we can't deal with SUID/SGID, so we refuse to run them |
| 577 | (otherwise the executable may misbehave if it doesn't have the |
| 578 | permissions it thinks it does). |
| 579 | */ |
| 580 | static int check_perms(int fd) |
| 581 | { |
| 582 | struct stat st; |
| 583 | |
| 584 | if (fstat(fd, &st) == -1) |
| 585 | return errno; |
| 586 | |
| 587 | if (st.st_mode & (S_ISUID | S_ISGID)) { |
| 588 | //fprintf(stderr, "Can't execute suid/sgid executable %s\n", exe); |
| 589 | return EACCES; |
| 590 | } |
| 591 | |
| 592 | if (geteuid() == st.st_uid) { |
| 593 | if (!(st.st_mode & S_IXUSR)) |
| 594 | return EACCES; |
| 595 | } else { |
| 596 | int grpmatch = 0; |
| 597 | |
| 598 | if (getegid() == st.st_gid) |
| 599 | grpmatch = 1; |
| 600 | else { |
| 601 | gid_t groups[32]; |
| 602 | int ngrp = getgroups(32, groups); |
| 603 | int i; |
| 604 | |
| 605 | for(i = 0; i < ngrp; i++) |
| 606 | if (groups[i] == st.st_gid) { |
| 607 | grpmatch = 1; |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (grpmatch) { |
| 613 | if (!(st.st_mode & S_IXGRP)) |
| 614 | return EACCES; |
| 615 | } else if (!(st.st_mode & S_IXOTH)) |
| 616 | return EACCES; |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 622 | static int do_exec_inner(const char *exe, struct exeinfo *info) |
| 623 | { |
| 624 | int fd; |
fitzhardinge | d2e3711 | 2004-09-09 08:10:42 +0000 | [diff] [blame] | 625 | int err; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 626 | char buf[VKI_PAGE_SIZE]; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 627 | int bufsz; |
| 628 | int i; |
| 629 | int ret; |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 630 | static const struct { |
| 631 | int (*match)(const char *hdr, int len); |
| 632 | int (*load) ( char *hdr, int len, int fd2, const char *name, |
| 633 | struct exeinfo *); |
| 634 | } formats[] = { |
| 635 | { match_ELF, load_ELF }, |
| 636 | { match_script, load_script }, |
| 637 | }; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 638 | |
| 639 | fd = open(exe, O_RDONLY); |
| 640 | if (fd == -1) { |
| 641 | if (0) |
| 642 | fprintf(stderr, "Can't open executable %s: %s\n", |
| 643 | exe, strerror(errno)); |
| 644 | return errno; |
| 645 | } |
| 646 | |
fitzhardinge | d2e3711 | 2004-09-09 08:10:42 +0000 | [diff] [blame] | 647 | err = check_perms(fd); |
fitzhardinge | fd7da3a | 2004-09-08 20:05:29 +0000 | [diff] [blame] | 648 | if (err != 0) { |
| 649 | close(fd); |
| 650 | return err; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | bufsz = pread(fd, buf, sizeof(buf), 0); |
| 654 | if (bufsz < 0) { |
| 655 | fprintf(stderr, "Can't read executable header: %s\n", |
| 656 | strerror(errno)); |
| 657 | close(fd); |
| 658 | return errno; |
| 659 | } |
| 660 | |
| 661 | ret = ENOEXEC; |
| 662 | for(i = 0; i < sizeof(formats)/sizeof(*formats); i++) { |
| 663 | if ((formats[i].match)(buf, bufsz)) { |
| 664 | ret = (formats[i].load)(buf, bufsz, fd, exe, info); |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | close(fd); |
| 670 | |
| 671 | return ret; |
| 672 | } |
| 673 | |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 674 | // See ume.h for an indication of which entries of 'info' are inputs, which |
| 675 | // are outputs, and which are both. |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 676 | int do_exec(const char *exe, struct exeinfo *info) |
| 677 | { |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 678 | info->interp_name = NULL; |
| 679 | info->interp_args = NULL; |
fitzhardinge | 7e343cd | 2003-12-16 02:14:00 +0000 | [diff] [blame] | 680 | |
| 681 | return do_exec_inner(exe, info); |
| 682 | } |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 683 | |
| 684 | /*--------------------------------------------------------------------*/ |
| 685 | /*--- end ume.c ---*/ |
| 686 | /*--------------------------------------------------------------------*/ |