exec_elf32.c revision 1.15
1/* $NetBSD: exec_elf32.c,v 1.15 1996/10/07 18:29:24 cgd Exp $ */ 2 3/* 4 * Copyright (c) 1996 Christopher G. Demetriou 5 * Copyright (c) 1994 Christos Zoulas 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 32#ifndef ELFSIZE 33#define ELFSIZE 32 34#endif 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/kernel.h> 39#include <sys/proc.h> 40#include <sys/malloc.h> 41#include <sys/namei.h> 42#include <sys/vnode.h> 43#include <sys/exec.h> 44#include <sys/exec_elf.h> 45#include <sys/fcntl.h> 46#include <sys/syscall.h> 47#include <sys/signalvar.h> 48#include <sys/mount.h> 49#include <sys/stat.h> 50 51#include <sys/mman.h> 52#include <vm/vm.h> 53#include <vm/vm_param.h> 54#include <vm/vm_map.h> 55 56#include <machine/cpu.h> 57#include <machine/reg.h> 58#include <machine/exec.h> 59 60#ifdef COMPAT_LINUX 61#include <compat/linux/linux_exec.h> 62#endif 63 64#ifdef COMPAT_SVR4 65#include <compat/svr4/svr4_exec.h> 66#endif 67 68#define CONCAT(x,y) __CONCAT(x,y) 69#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) 70#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) 71#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) 72#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) 73 74static int ELFNAME(check_header) __P((Elf_Ehdr *, int)); 75static int ELFNAME(load_file) __P((struct proc *, struct exec_package *, 76 char *, struct exec_vmcmd_set *, u_long *, struct elf_args *, 77 Elf_Addr *)); 78static void ELFNAME(load_psection) __P((struct exec_vmcmd_set *, 79 struct vnode *, Elf_Phdr *, Elf_Addr *, u_long *, int *)); 80 81extern char sigcode[], esigcode[]; 82#ifdef SYSCALL_DEBUG 83extern char *syscallnames[]; 84#endif 85 86struct emul ELFNAMEEND(emul_netbsd) = { 87 "netbsd", 88 NULL, 89 sendsig, 90 SYS_syscall, 91 SYS_MAXSYSCALL, 92 sysent, 93#ifdef SYSCALL_DEBUG 94 syscallnames, 95#else 96 NULL, 97#endif 98 ELF_AUX_ENTRIES * sizeof(AuxInfo), 99 ELFNAME(copyargs), 100 setregs, 101 sigcode, 102 esigcode, 103}; 104 105int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *, 106 Elf_Ehdr *, char *, Elf_Addr *)) = { 107#if defined(COMPAT_SVR4) && (ELFSIZE == 32) 108 ELFNAME2(svr4,probe), /* XXX not 64-bit safe */ 109#endif 110#if defined(COMPAT_LINUX) && (ELFSIZE == 32) 111 ELFNAME2(linux,probe), /* XXX not 64-bit safe */ 112#endif 113}; 114 115#define ELF_ALIGN(a, b) ((a) & ~((b) - 1)) 116 117/* 118 * Copy arguments onto the stack in the normal way, but add some 119 * extra information in case of dynamic binding. 120 */ 121void * 122ELFNAME(copyargs)(pack, arginfo, stack, argp) 123 struct exec_package *pack; 124 struct ps_strings *arginfo; 125 void *stack; 126 void *argp; 127{ 128 size_t len; 129 AuxInfo ai[ELF_AUX_ENTRIES], *a; 130 struct elf_args *ap; 131 132 stack = copyargs(pack, arginfo, stack, argp); 133 if (!stack) 134 return NULL; 135 136 /* 137 * Push extra arguments on the stack needed by dynamically 138 * linked binaries 139 */ 140 if ((ap = (struct elf_args *) pack->ep_emul_arg)) { 141 a = ai; 142 143 a->au_id = AUX_phdr; 144 a->au_v = ap->arg_phaddr; 145 a++; 146 147 a->au_id = AUX_phent; 148 a->au_v = ap->arg_phentsize; 149 a++; 150 151 a->au_id = AUX_phnum; 152 a->au_v = ap->arg_phnum; 153 a++; 154 155 a->au_id = AUX_pagesz; 156 a->au_v = NBPG; 157 a++; 158 159 a->au_id = AUX_base; 160 a->au_v = ap->arg_interp; 161 a++; 162 163 a->au_id = AUX_flags; 164 a->au_v = 0; 165 a++; 166 167 a->au_id = AUX_entry; 168 a->au_v = ap->arg_entry; 169 a++; 170 171 a->au_id = AUX_null; 172 a->au_v = 0; 173 a++; 174 175 free((char *) ap, M_TEMP); 176 pack->ep_emul_arg = NULL; 177 len = ELF_AUX_ENTRIES * sizeof (AuxInfo); 178 if (copyout(ai, stack, len)) 179 return NULL; 180 stack += len; 181 } 182 return stack; 183} 184 185/* 186 * elf_check_header(): 187 * 188 * Check header for validity; return 0 of ok ENOEXEC if error 189 */ 190static int 191ELFNAME(check_header)(eh, type) 192 Elf_Ehdr *eh; 193 int type; 194{ 195 196 if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0) 197 return ENOEXEC; 198 199 switch (eh->e_machine) { 200 201 ELFDEFNNAME(MACHDEP_ID_CASES) 202 203 default: 204 return ENOEXEC; 205 } 206 207 if (eh->e_type != type) 208 return ENOEXEC; 209 210 return 0; 211} 212 213/* 214 * elf_load_psection(): 215 * 216 * Load a psection at the appropriate address 217 */ 218static void 219ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot) 220 struct exec_vmcmd_set *vcset; 221 struct vnode *vp; 222 Elf_Phdr *ph; 223 Elf_Addr *addr; 224 u_long *size; 225 int *prot; 226{ 227 u_long uaddr, msize, psize, rm, rf; 228 long diff, offset; 229 230 /* 231 * If the user specified an address, then we load there. 232 */ 233 if (*addr != ELFDEFNNAME(NO_ADDR)) { 234 if (ph->p_align > 1) { 235 *addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align); 236 uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align); 237 } else 238 uaddr = ph->p_vaddr; 239 diff = ph->p_vaddr - uaddr; 240 } else { 241 *addr = uaddr = ph->p_vaddr; 242 if (ph->p_align > 1) 243 *addr = ELF_ALIGN(uaddr, ph->p_align); 244 diff = uaddr - *addr; 245 } 246 247 *prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0; 248 *prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0; 249 *prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0; 250 251 offset = ph->p_offset - diff; 252 *size = ph->p_filesz + diff; 253 msize = ph->p_memsz + diff; 254 psize = round_page(*size); 255 256 if ((ph->p_flags & Elf_pf_w) != 0) { 257 /* 258 * Because the pagedvn pager can't handle zero fill of the last 259 * data page if it's not page aligned we map the last page 260 * readvn. 261 */ 262 psize = trunc_page(*size); 263 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 264 offset, *prot); 265 if(psize != *size) 266 NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize, 267 *addr + psize, vp, offset + psize, *prot); 268 } else 269 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 270 offset, *prot); 271 272 /* 273 * Check if we need to extend the size of the segment 274 */ 275 rm = round_page(*addr + msize); 276 rf = round_page(*addr + *size); 277 278 if (rm != rf) { 279 NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot); 280 *size = msize; 281 } 282} 283 284/* 285 * elf_read_from(): 286 * 287 * Read from vnode into buffer at offset. 288 */ 289int 290ELFNAME(read_from)(p, vp, off, buf, size) 291 struct vnode *vp; 292 u_long off; 293 struct proc *p; 294 caddr_t buf; 295 int size; 296{ 297 int error; 298 int resid; 299 300 if ((error = vn_rdwr(UIO_READ, vp, buf, size, 301 off, UIO_SYSSPACE, 0, p->p_ucred, 302 &resid, p)) != 0) 303 return error; 304 /* 305 * See if we got all of it 306 */ 307 if (resid != 0) 308 return ENOEXEC; 309 return 0; 310} 311 312/* 313 * elf_load_file(): 314 * 315 * Load a file (interpreter/library) pointed to by path 316 * [stolen from coff_load_shlib()]. Made slightly generic 317 * so it might be used externally. 318 */ 319static int 320ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last) 321 struct proc *p; 322 struct exec_package *epp; 323 char *path; 324 struct exec_vmcmd_set *vcset; 325 u_long *entry; 326 struct elf_args *ap; 327 Elf_Addr *last; 328{ 329 int error, i; 330 struct nameidata nd; 331 struct vnode *vp; 332 struct vattr attr; 333 Elf_Ehdr eh; 334 Elf_Phdr *ph = NULL; 335 u_long phsize; 336 char *bp = NULL; 337 Elf_Addr addr = *last; 338 339 bp = path; 340 /* 341 * 1. open file 342 * 2. read filehdr 343 * 3. map text, data, and bss out of it using VM_* 344 */ 345 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 346 if ((error = namei(&nd)) != 0) 347 return error; 348 vp = nd.ni_vp; 349 350 /* check for regular file */ 351 if (vp->v_type != VREG) { 352 error = EACCES; 353 goto badunlock; 354 } 355 356 /* get attributes */ 357 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 358 goto badunlock; 359 360 /* 361 * Check mount point. Though we're not trying to exec this binary, 362 * we will be executing code from it, so if the mount point 363 * disallows execution or set-id-ness, we punt or kill the set-id. 364 */ 365 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 366 error = EACCES; 367 goto badunlock; 368 } 369 if (vp->v_mount->mnt_flag & MNT_NOSUID) 370 epp->ep_vap->va_mode &= ~(VSUID | VSGID); 371 372 /* 373 * Similarly, if it's not marked as executable, we don't allow 374 * it to be used. For root we have to see if any exec bit on. 375 */ 376 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 377 goto badunlock; 378 if ((attr.va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 379 error = EACCES; 380 goto badunlock; 381 } 382 383#ifdef notyet /* XXX cgd 960926 */ 384 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 385#endif 386 VOP_UNLOCK(vp); 387 388 if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh, 389 sizeof(eh))) != 0) 390 goto bad; 391 392 if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0) 393 goto bad; 394 395 phsize = eh.e_phnum * sizeof(Elf_Phdr); 396 ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK); 397 398 if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff, 399 (caddr_t) ph, phsize)) != 0) 400 goto bad; 401 402 /* 403 * Load all the necessary sections 404 */ 405 for (i = 0; i < eh.e_phnum; i++) { 406 u_long size = 0; 407 int prot = 0; 408 409 switch (ph[i].p_type) { 410 case Elf_pt_load: 411 ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 412 &size, &prot); 413 /* If entry is within this section it must be text */ 414 if (eh.e_entry >= ph[i].p_vaddr && 415 eh.e_entry < (ph[i].p_vaddr + size)) { 416 *entry = addr + eh.e_entry; 417 ap->arg_interp = addr; 418 } 419 addr += size; 420 break; 421 422 case Elf_pt_dynamic: 423 case Elf_pt_phdr: 424 case Elf_pt_note: 425 break; 426 427 default: 428 break; 429 } 430 } 431 432 free((char *) ph, M_TEMP); 433 *last = addr; 434 vrele(vp); 435 return 0; 436 437badunlock: 438 VOP_UNLOCK(vp); 439 440bad: 441 if (ph != NULL) 442 free((char *) ph, M_TEMP); 443#ifdef notyet /* XXX cgd 960926 */ 444 (maybe) VOP_CLOSE it 445#endif 446 vrele(vp); 447 return error; 448} 449 450/* 451 * exec_elf_makecmds(): Prepare an Elf binary's exec package 452 * 453 * First, set of the various offsets/lengths in the exec package. 454 * 455 * Then, mark the text image busy (so it can be demand paged) or error 456 * out if this is not possible. Finally, set up vmcmds for the 457 * text, data, bss, and stack segments. 458 */ 459int 460ELFNAME2(exec,makecmds)(p, epp) 461 struct proc *p; 462 struct exec_package *epp; 463{ 464 Elf_Ehdr *eh = epp->ep_hdr; 465 Elf_Phdr *ph, *pp; 466 Elf_Addr phdr = 0, pos = 0; 467 int error, i, n, nload; 468 char interp[MAXPATHLEN]; 469 u_long phsize; 470 471 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 472 return ENOEXEC; 473 474 if (ELFNAME(check_header)(eh, Elf_et_exec)) 475 return ENOEXEC; 476 477 /* 478 * check if vnode is in open for writing, because we want to 479 * demand-page out of it. if it is, don't do it, for various 480 * reasons 481 */ 482 if (epp->ep_vp->v_writecount != 0) { 483#ifdef DIAGNOSTIC 484 if (epp->ep_vp->v_flag & VTEXT) 485 panic("exec: a VTEXT vnode has writecount != 0\n"); 486#endif 487 return ETXTBSY; 488 } 489 /* 490 * Allocate space to hold all the program headers, and read them 491 * from the file 492 */ 493 phsize = eh->e_phnum * sizeof(Elf_Phdr); 494 ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK); 495 496 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 497 (caddr_t) ph, phsize)) != 0) 498 goto bad; 499 500 epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 501 epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 502 503 interp[0] = '\0'; 504 505 for (i = 0; i < eh->e_phnum; i++) { 506 pp = &ph[i]; 507 if (pp->p_type == Elf_pt_interp) { 508 if (pp->p_filesz >= sizeof(interp)) 509 goto bad; 510 if ((error = ELFNAME(read_from)(p, epp->ep_vp, pp->p_offset, 511 (caddr_t) interp, pp->p_filesz)) != 0) 512 goto bad; 513 break; 514 } 515 } 516 517 /* 518 * Setup things for native emulation. 519 */ 520 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 521 pos = ELFDEFNNAME(NO_ADDR); 522 523 /* 524 * On the same architecture, we may be emulating different systems. 525 * See which one will accept this executable. This currently only 526 * applies to Linux and SVR4 on the i386. 527 * 528 * Probe functions would normally see if the interpreter (if any) 529 * exists. Emulation packages may possibly replace the interpreter in 530 * interp[] with a changed path (/emul/xxx/<path>), and also 531 * set the ep_emul field in the exec package structure. 532 */ 533 if ((n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0])) { 534 error = ENOEXEC; 535 for (i = 0; i < n && error; i++) 536 error = ELFNAME(probe_funcs)[i](p, epp, eh, interp, &pos); 537 538#ifdef notyet 539 /* 540 * We should really use a signature in our native binaries 541 * and have our own probe function for matching binaries, 542 * before trying the emulations. For now, if the emulation 543 * probes failed we default to native. 544 */ 545 if (error) 546 goto bad; 547#endif 548 } 549 550 /* 551 * Load all the necessary sections 552 */ 553 for (i = nload = 0; i < eh->e_phnum; i++) { 554 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 555 u_long size = 0; 556 int prot = 0; 557 558 pp = &ph[i]; 559 560 switch (ph[i].p_type) { 561 case Elf_pt_load: 562 /* 563 * XXX 564 * Can handle only 2 sections: text and data 565 */ 566 if (nload++ == 2) 567 goto bad; 568 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 569 &ph[i], &addr, &size, &prot); 570 /* 571 * Decide whether it's text or data by looking 572 * at the entry point. 573 */ 574 if (eh->e_entry >= addr && eh->e_entry < (addr + size)){ 575 epp->ep_taddr = addr; 576 epp->ep_tsize = size; 577 } else { 578 epp->ep_daddr = addr; 579 epp->ep_dsize = size; 580 } 581 break; 582 583 case Elf_pt_shlib: 584 error = ENOEXEC; 585 goto bad; 586 587 case Elf_pt_interp: 588 /* Already did this one */ 589 case Elf_pt_dynamic: 590 case Elf_pt_note: 591 break; 592 593 case Elf_pt_phdr: 594 /* Note address of program headers (in text segment) */ 595 phdr = pp->p_vaddr; 596 break; 597 598 default: 599 /* 600 * Not fatal; we don't need to understand everything. 601 */ 602 break; 603 } 604 } 605 606 /* 607 * If no position to load the interpreter was set by a probe 608 * function, pick the same address that a non-fixed mmap(0, ..) 609 * would (i.e. something safely out of the way). 610 */ 611 if (pos == ELFDEFNNAME(NO_ADDR) && 612 epp->ep_emul == &ELFNAMEEND(emul_netbsd)) 613 pos = round_page(epp->ep_daddr + MAXDSIZ); 614 615 /* 616 * Check if we found a dynamically linked binary and arrange to load 617 * it's interpreter 618 */ 619 if (interp[0]) { 620 struct elf_args *ap; 621 622 ap = (struct elf_args *) malloc(sizeof(struct elf_args), 623 M_TEMP, M_WAITOK); 624 if ((error = ELFNAME(load_file)(p, epp, interp, 625 &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) { 626 free((char *) ap, M_TEMP); 627 goto bad; 628 } 629 pos += phsize; 630 ap->arg_phaddr = phdr; 631 632 ap->arg_phentsize = eh->e_phentsize; 633 ap->arg_phnum = eh->e_phnum; 634 ap->arg_entry = eh->e_entry; 635 636 epp->ep_emul_arg = ap; 637 } else 638 epp->ep_entry = eh->e_entry; 639 640#ifdef ELF_MAP_PAGE_ZERO 641 /* Dell SVR4 maps page zero, yeuch! */ 642 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0, 643 VM_PROT_READ); 644#endif 645 free((char *) ph, M_TEMP); 646 epp->ep_vp->v_flag |= VTEXT; 647 return exec_elf_setup_stack(p, epp); 648 649bad: 650 free((char *) ph, M_TEMP); 651 kill_vmcmds(&epp->ep_vmcmds); 652 return ENOEXEC; 653} 654