The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/pecoff/pecoff_exec.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: pecoff_exec.c,v 1.38 2007/12/08 18:36:22 dsl Exp $     */
    2 
    3 /*
    4  * Copyright (c) 2000 Masaru OKI
    5  * Copyright (c) 1994, 1995, 1998 Scott Bartram
    6  * Copyright (c) 1994 Adam Glass
    7  * Copyright (c) 1993, 1994 Christopher G. Demetriou
    8  * All rights reserved.
    9  *
   10  * from compat/ibcs2/ibcs2_exec.c
   11  * originally from kern/exec_ecoff.c
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. All advertising materials mentioning features or use of this software
   22  *    must display the following acknowledgement:
   23  *      This product includes software developed by Scott Bartram.
   24  * 4. The name of the author may not be used to endorse or promote products
   25  *    derived from this software without specific prior written permission
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: pecoff_exec.c,v 1.38 2007/12/08 18:36:22 dsl Exp $");
   41 
   42 /*#define DEBUG_PECOFF*/
   43 
   44 #include <sys/param.h>
   45 #include <sys/proc.h>
   46 #include <sys/malloc.h>
   47 #include <sys/namei.h>
   48 #include <sys/vnode.h>
   49 #include <sys/mount.h>
   50 #include <sys/exec.h>
   51 #include <sys/syscall.h>
   52 #include <sys/signalvar.h>
   53 #include <sys/resourcevar.h>
   54 #include <sys/stat.h>
   55 
   56 #include <sys/exec_coff.h>
   57 #include <machine/coff_machdep.h>
   58 
   59 #include <compat/pecoff/pecoff_exec.h>
   60 #include <compat/pecoff/pecoff_util.h>
   61 
   62 int pecoff_signature (struct lwp *l, struct vnode *vp,
   63                       struct pecoff_dos_filehdr *dp);
   64 int pecoff_load_file (struct lwp *l, struct exec_package *epp,
   65                       const char *path, struct exec_vmcmd_set *vcset,
   66                       u_long *entry, struct pecoff_args *argp);
   67 void pecoff_load_section (struct exec_vmcmd_set *vcset, struct vnode *vp,
   68                           struct coff_scnhdr *sh, long *addr,
   69                           u_long *size, int *prot);
   70 int exec_pecoff_makecmds (struct lwp *l, struct exec_package *epp);
   71 int exec_pecoff_coff_makecmds (struct lwp *l, struct exec_package *epp,
   72                                struct coff_filehdr *fp, int peofs);
   73 int exec_pecoff_prep_omagic (struct proc *p, struct exec_package *epp,
   74                              struct coff_filehdr *fp,
   75                              struct coff_aouthdr *ap, int peofs);
   76 int exec_pecoff_prep_nmagic (struct proc *p, struct exec_package *epp,
   77                              struct coff_filehdr *fp,
   78                              struct coff_aouthdr *ap, int peofs);
   79 int exec_pecoff_prep_zmagic (struct lwp *l, struct exec_package *epp,
   80                              struct coff_filehdr *fp,
   81                              struct coff_aouthdr *ap, int peofs);
   82 
   83 
   84 int
   85 pecoff_copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo, char **stackp, void *argp)
   86 {
   87         int len = sizeof(struct pecoff_args);
   88         struct pecoff_args *ap;
   89         int error;
   90 
   91         if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
   92                 return error;
   93 
   94         ap = (struct pecoff_args *)pack->ep_emul_arg;
   95         if ((error = copyout(ap, *stackp, len)) != 0)
   96                 return error;
   97 
   98 #if 0 /*  kern_exec.c? */
   99         free(ap, M_TEMP);
  100         pack->ep_emul_arg = 0;
  101 #endif
  102 
  103         *stackp += len;
  104         return error;
  105 }
  106 
  107 #define PECOFF_SIGNATURE "PE\0\0"
  108 static const char signature[] = PECOFF_SIGNATURE;
  109 
  110 /*
  111  * Check PE signature.
  112  */
  113 int
  114 pecoff_signature(struct lwp *l, struct vnode *vp, struct pecoff_dos_filehdr *dp)
  115 {
  116         int error;
  117         char tbuf[sizeof(signature) - 1];
  118 
  119         if (DOS_BADMAG(dp)) {
  120                 return ENOEXEC;
  121         }
  122         error = exec_read_from(l, vp, dp->d_peofs, tbuf, sizeof(tbuf));
  123         if (error) {
  124                 return error;
  125         }
  126         if (memcmp(tbuf, signature, sizeof(signature) - 1) == 0) {
  127                 return 0;
  128         }
  129         return EFTYPE;
  130 }
  131 
  132 /*
  133  * load(mmap) file.  for dynamic linker (ld.so.dll)
  134  */
  135 int
  136 pecoff_load_file(struct lwp *l, struct exec_package *epp, const char *path, struct exec_vmcmd_set *vcset, u_long *entry, struct pecoff_args *argp)
  137 {
  138         int error, peofs, scnsiz, i;
  139         struct vnode *vp;
  140         struct vattr attr;
  141         struct pecoff_dos_filehdr dh;
  142         struct coff_filehdr *fp = 0;
  143         struct coff_aouthdr *ap;
  144         struct pecoff_opthdr *wp;
  145         struct coff_scnhdr *sh = 0;
  146 
  147         error = emul_find_interp(l, epp, path);
  148         if (error != 0)
  149                 return error;
  150 
  151         vp = epp->ep_interp;
  152         epp->ep_interp = NULL;
  153         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  154 
  155         /*
  156          * If it's not marked as executable, or it's not a regular
  157          * file, we don't allow it to be used.
  158          */
  159         if (vp->v_type != VREG) {
  160                 error = EACCES;
  161                 goto badunlock;
  162         }
  163         if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
  164                 goto badunlock;
  165 
  166         /* get attributes */
  167         if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
  168                 goto badunlock;
  169 
  170         /*
  171          * Check mount point.  Though we're not trying to exec this binary,
  172          * we will be executing code from it, so if the mount point
  173          * disallows execution or set-id-ness, we punt or kill the set-id.
  174          */
  175         if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
  176                 error = EACCES;
  177                 goto badunlock;
  178         }
  179         if (vp->v_mount->mnt_flag & MNT_NOSUID)
  180                 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
  181 
  182         if ((error = vn_marktext(vp)))
  183                 goto badunlock;
  184 
  185         VOP_UNLOCK(vp, 0);
  186         /*
  187          * Read header.
  188          */
  189         error = exec_read_from(l, vp, 0, &dh, sizeof(dh));
  190         if (error != 0)
  191                 goto bad;
  192         if ((error = pecoff_signature(l, vp, &dh)) != 0)
  193                 goto bad;
  194         fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
  195         peofs = dh.d_peofs + sizeof(signature) - 1;
  196         error = exec_read_from(l, vp, peofs, fp, PECOFF_HDR_SIZE);
  197         if (error != 0)
  198                 goto bad;
  199         if (COFF_BADMAG(fp)) {
  200                 error = ENOEXEC;
  201                 goto bad;
  202         }
  203         ap = (void *)((char *)fp + sizeof(struct coff_filehdr));
  204         wp = (void *)((char *)ap + sizeof(struct coff_aouthdr));
  205         /* read section header */
  206         scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
  207         sh = malloc(scnsiz, M_TEMP, M_WAITOK);
  208         if ((error = exec_read_from(l, vp, peofs + PECOFF_HDR_SIZE, sh,
  209             scnsiz)) != 0)
  210                 goto bad;
  211 
  212         /*
  213          * Read section header, and mmap.
  214          */
  215         for (i = 0; i < fp->f_nscns; i++) {
  216                 int prot = 0;
  217                 long addr;
  218                 u_long size;
  219 
  220                 if (sh[i].s_flags & COFF_STYP_DISCARD)
  221                         continue;
  222                 /* XXX ? */
  223                 if ((sh[i].s_flags & COFF_STYP_TEXT) &&
  224                     (sh[i].s_flags & COFF_STYP_EXEC) == 0)
  225                         continue;
  226                 if ((sh[i].s_flags & (COFF_STYP_TEXT|COFF_STYP_DATA|
  227                                       COFF_STYP_BSS|COFF_STYP_READ)) == 0)
  228                         continue;
  229                 sh[i].s_vaddr += wp->w_base; /* RVA --> VA */
  230                 pecoff_load_section(vcset, vp, &sh[i], &addr, &size, &prot);
  231         }
  232         *entry = wp->w_base + ap->a_entry;
  233         argp->a_ldbase = wp->w_base;
  234         argp->a_ldexport = wp->w_imghdr[0].i_vaddr + wp->w_base;
  235 
  236         free(fp, M_TEMP);
  237         free(sh, M_TEMP);
  238         /*XXXUNCONST*/
  239         vrele(vp);
  240         return 0;
  241 
  242 badunlock:
  243         VOP_UNLOCK(vp, 0);
  244 
  245 bad:
  246         if (fp != 0)
  247                 free(fp, M_TEMP);
  248         if (sh != 0)
  249                 free(sh, M_TEMP);
  250         /*XXXUNCONST*/
  251         vrele(vp);
  252         return error;
  253 }
  254 
  255 /*
  256  * mmap one section.
  257  */
  258 void
  259 pecoff_load_section(struct exec_vmcmd_set *vcset, struct vnode *vp, struct coff_scnhdr *sh, long *addr, u_long *size, int *prot)
  260 {
  261         u_long diff, offset;
  262 
  263         *addr = COFF_ALIGN(sh->s_vaddr);
  264         diff = (sh->s_vaddr - *addr);
  265         offset = sh->s_scnptr - diff;
  266         *size = COFF_ROUND(sh->s_size + diff, COFF_LDPGSZ);
  267 
  268         *prot |= (sh->s_flags & COFF_STYP_EXEC) ? VM_PROT_EXECUTE : 0;
  269         *prot |= (sh->s_flags & COFF_STYP_READ) ? VM_PROT_READ : 0;
  270         *prot |= (sh->s_flags & COFF_STYP_WRITE) ? VM_PROT_WRITE : 0;
  271 
  272         if (diff == 0 && offset == COFF_ALIGN(offset))
  273                 NEW_VMCMD(vcset, vmcmd_map_pagedvn, *size, *addr, vp,
  274                           offset, *prot);
  275         else
  276                 NEW_VMCMD(vcset, vmcmd_map_readvn, sh->s_size, sh->s_vaddr, vp,
  277                           sh->s_scnptr, *prot);
  278 
  279         if (*size < sh->s_paddr) {
  280                 u_long baddr, bsize;
  281 
  282                 baddr = *addr + COFF_ROUND(*size, COFF_LDPGSZ);
  283                 bsize = sh->s_paddr - COFF_ROUND(*size, COFF_LDPGSZ);
  284                 DPRINTF(("additional zero space (addr %lx size %ld)\n",
  285                          baddr, bsize));
  286                 NEW_VMCMD(vcset, vmcmd_map_zero, bsize, baddr,
  287                           NULLVP, 0, *prot);
  288                 *size = COFF_ROUND(sh->s_paddr, COFF_LDPGSZ);
  289         }
  290         DPRINTF(("section %s loaded. (addr %lx size %ld prot %d)\n",
  291                  sh->s_name, sh->s_vaddr, sh->s_size, *prot));
  292 }
  293 
  294 /*
  295  */
  296 int
  297 exec_pecoff_makecmds(struct lwp *l, struct exec_package *epp)
  298 {
  299         int error, peofs;
  300         struct pecoff_dos_filehdr *dp = epp->ep_hdr;
  301         struct coff_filehdr *fp;
  302         struct proc *p;
  303 
  304         p = l->l_proc;
  305         /*
  306          * mmap EXE file (PE format)
  307          * 1. read header (DOS,PE)
  308          * 2. mmap code section (READ|EXEC)
  309          * 3. mmap other section, such as data (READ|WRITE|EXEC)
  310          */
  311         if (epp->ep_hdrvalid < PECOFF_DOS_HDR_SIZE) {
  312                 return ENOEXEC;
  313         }
  314         if ((error = pecoff_signature(l, epp->ep_vp, dp)) != 0)
  315                 return error;
  316 
  317         if ((error = vn_marktext(epp->ep_vp)) != 0)
  318                 return error;
  319 
  320         peofs = dp->d_peofs + sizeof(signature) - 1;
  321         fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
  322         error = exec_read_from(l, epp->ep_vp, peofs, fp, PECOFF_HDR_SIZE);
  323         if (error) {
  324                 free(fp, M_TEMP);
  325                 return error;
  326         }
  327         error = exec_pecoff_coff_makecmds(l, epp, fp, peofs);
  328 
  329         if (error != 0)
  330                 kill_vmcmds(&epp->ep_vmcmds);
  331 
  332         free(fp, M_TEMP);
  333         return error;
  334 }
  335 
  336 /*
  337  */
  338 int
  339 exec_pecoff_coff_makecmds(struct lwp *l, struct exec_package *epp, struct coff_filehdr *fp, int peofs)
  340 {
  341         struct coff_aouthdr *ap;
  342         struct proc *p;
  343         int error;
  344 
  345         if (COFF_BADMAG(fp)) {
  346                 return ENOEXEC;
  347         }
  348         p = l->l_proc;
  349         ap = (void *)((char *)fp + sizeof(struct coff_filehdr));
  350         switch (ap->a_magic) {
  351         case COFF_OMAGIC:
  352                 error = exec_pecoff_prep_omagic(p, epp, fp, ap, peofs);
  353                 break;
  354         case COFF_NMAGIC:
  355                 error = exec_pecoff_prep_nmagic(p, epp, fp, ap, peofs);
  356                 break;
  357         case COFF_ZMAGIC:
  358                 error = exec_pecoff_prep_zmagic(l, epp, fp, ap, peofs);
  359                 break;
  360         default:
  361                 return ENOEXEC;
  362         }
  363 
  364         return error;
  365 }
  366 
  367 /*
  368  */
  369 int
  370 exec_pecoff_prep_omagic(struct proc *p,
  371     struct exec_package *epp, struct coff_filehdr *fp,
  372     struct coff_aouthdr *ap, int peofs)
  373 {
  374         return ENOEXEC;
  375 }
  376 
  377 /*
  378  */
  379 int
  380 exec_pecoff_prep_nmagic(struct proc *p,
  381     struct exec_package *epp, struct coff_filehdr *fp,
  382     struct coff_aouthdr *ap, int peofs)
  383 {
  384         return ENOEXEC;
  385 }
  386 
  387 /*
  388  */
  389 int
  390 exec_pecoff_prep_zmagic(struct lwp *l, struct exec_package *epp, struct coff_filehdr *fp, struct coff_aouthdr *ap, int peofs)
  391 {
  392         int error, i;
  393         struct pecoff_opthdr *wp;
  394         long daddr, baddr, bsize;
  395         u_long tsize, dsize;
  396         struct coff_scnhdr *sh;
  397         struct pecoff_args *argp;
  398         int scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
  399 
  400         wp = (void *)((char *)ap + sizeof(struct coff_aouthdr));
  401 
  402         epp->ep_tsize = ap->a_tsize;
  403         epp->ep_daddr = VM_MAXUSER_ADDRESS;
  404         epp->ep_dsize = 0;
  405         /* read section header */
  406         sh = malloc(scnsiz, M_TEMP, M_WAITOK);
  407         error = exec_read_from(l, epp->ep_vp, peofs + PECOFF_HDR_SIZE, sh,
  408             scnsiz);
  409         if (error) {
  410                 free(sh, M_TEMP);
  411                 return error;
  412         }
  413         /*
  414          * map section
  415          */
  416         for (i = 0; i < fp->f_nscns; i++) {
  417                 int prot = /**/VM_PROT_WRITE;
  418                 long s_flags = sh[i].s_flags;
  419 
  420                 if ((s_flags & COFF_STYP_DISCARD) != 0)
  421                         continue;
  422                 sh[i].s_vaddr += wp->w_base; /* RVA --> VA */
  423 
  424                 if ((s_flags & COFF_STYP_TEXT) != 0) {
  425                         /* set up command for text segment */
  426 /*                      DPRINTF(("COFF text addr %lx size %ld offset %ld\n",
  427                                  sh[i].s_vaddr, sh[i].s_size, sh[i].s_scnptr));
  428 */                      pecoff_load_section(&epp->ep_vmcmds, epp->ep_vp,
  429                                            &sh[i], (long *)&epp->ep_taddr,
  430                                            &tsize, &prot);
  431                 } else if ((s_flags & COFF_STYP_BSS) != 0) {
  432                         /* set up command for bss segment */
  433                         baddr = sh[i].s_vaddr;
  434                         bsize = sh[i].s_paddr;
  435                         if (bsize)
  436                                 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
  437                                     bsize, baddr, NULLVP, 0,
  438                                     VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
  439                         epp->ep_daddr = min(epp->ep_daddr, baddr);
  440                         bsize = baddr + bsize - epp->ep_daddr;
  441                         epp->ep_dsize = max(epp->ep_dsize, bsize);
  442                 } else if ((s_flags & (COFF_STYP_DATA|COFF_STYP_READ)) != 0) {
  443                         /* set up command for data segment */
  444 /*                      DPRINTF(("COFF data addr %lx size %ld offset %ld\n",
  445                         sh[i].s_vaddr, sh[i].s_size, sh[i].s_scnptr));*/
  446                         pecoff_load_section(&epp->ep_vmcmds, epp->ep_vp,
  447                                            &sh[i], &daddr, &dsize, &prot);
  448                         epp->ep_daddr = min(epp->ep_daddr, daddr);
  449                         dsize = daddr + dsize - epp->ep_daddr;
  450                         epp->ep_dsize = max(epp->ep_dsize, dsize);
  451                 }
  452         }
  453         /* set up ep_emul_arg */
  454         argp = malloc(sizeof(struct pecoff_args), M_TEMP, M_WAITOK);
  455         epp->ep_emul_arg = argp;
  456         argp->a_abiversion = NETBSDPE_ABI_VERSION;
  457         argp->a_zero = 0;
  458         argp->a_entry = wp->w_base + ap->a_entry;
  459         argp->a_end = epp->ep_daddr + epp->ep_dsize;
  460         argp->a_opthdr = *wp;
  461 
  462         /*
  463          * load dynamic linker
  464          */
  465         error = pecoff_load_file(l, epp, "/usr/libexec/ld.so.dll",
  466                                 &epp->ep_vmcmds, &epp->ep_entry, argp);
  467         if (error) {
  468                 free(sh, M_TEMP);
  469                 return error;
  470         }
  471 
  472 #if 0
  473         DPRINTF(("text addr: %lx size: %ld data addr: %lx size: %ld entry: %lx\n",
  474                  epp->ep_taddr, epp->ep_tsize,
  475                  epp->ep_daddr, epp->ep_dsize,
  476                  epp->ep_entry));
  477 #endif
  478 
  479         free(sh, M_TEMP);
  480         return (*epp->ep_esch->es_setup_stack)(l, epp);
  481 }

Cache object: b2a619ec180c13cde2cece5c89106bce


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.