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