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/kern/imgact_gzip.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * ----------------------------------------------------------------------------
    3  * "THE BEER-WARE LICENSE" (Revision 42):
    4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
    5  * can do whatever you want with this stuff. If we meet some day, and you think
    6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
    7  * ----------------------------------------------------------------------------
    8  */
    9 
   10 /*
   11  * This module handles execution of a.out files which have been run through
   12  * "gzip".  This saves diskspace, but wastes cpu-cycles and VM.
   13  *
   14  * TODO:
   15  *      text-segments should be made R/O after being filled
   16  *      is the vm-stuff safe ?
   17  *      should handle the entire header of gzip'ed stuff.
   18  *      inflate isn't quite reentrant yet...
   19  *      error-handling is a mess...
   20  *      so is the rest...
   21  *      tidy up unnecessary includes
   22  */
   23 
   24 #include <sys/cdefs.h>
   25 __FBSDID("$FreeBSD: stable/10/sys/kern/imgact_gzip.c 255426 2013-09-09 18:11:59Z jhb $");
   26 
   27 #include <sys/param.h>
   28 #include <sys/exec.h>
   29 #include <sys/imgact.h>
   30 #include <sys/imgact_aout.h>
   31 #include <sys/kernel.h>
   32 #include <sys/lock.h>
   33 #include <sys/mman.h>
   34 #include <sys/mutex.h>
   35 #include <sys/proc.h>
   36 #include <sys/racct.h>
   37 #include <sys/resourcevar.h>
   38 #include <sys/sysent.h>
   39 #include <sys/systm.h>
   40 #include <sys/vnode.h>
   41 #include <sys/inflate.h>
   42 
   43 #include <vm/vm.h>
   44 #include <vm/vm_param.h>
   45 #include <vm/pmap.h>
   46 #include <vm/vm_map.h>
   47 #include <vm/vm_kern.h>
   48 #include <vm/vm_extern.h>
   49 
   50 struct imgact_gzip {
   51         struct image_params *ip;
   52         struct exec     a_out;
   53         int             error;
   54         int             gotheader;
   55         int             where;
   56         u_char         *inbuf;
   57         u_long          offset;
   58         u_long          output;
   59         u_long          len;
   60         int             idx;
   61         u_long          virtual_offset, file_offset, file_end, bss_size;
   62 };
   63 
   64 static int exec_gzip_imgact(struct image_params *imgp);
   65 static int NextByte(void *vp);
   66 static int do_aout_hdr(struct imgact_gzip *);
   67 static int Flush(void *vp, u_char *, u_long siz);
   68 
   69 static int
   70 exec_gzip_imgact(imgp)
   71         struct image_params *imgp;
   72 {
   73         int             error;
   74         const u_char   *p = (const u_char *) imgp->image_header;
   75         struct imgact_gzip igz;
   76         struct inflate  infl;
   77         struct vmspace *vmspace;
   78 
   79         /* If these four are not OK, it isn't a gzip file */
   80         if (p[0] != 0x1f)
   81                 return -1;      /* 0    Simply magic     */
   82         if (p[1] != 0x8b)
   83                 return -1;      /* 1    Simply magic     */
   84         if (p[2] != 0x08)
   85                 return -1;      /* 2    Compression method       */
   86         if (p[9] != 0x03)
   87                 return -1;      /* 9    OS compressed on         */
   88 
   89         /*
   90          * If this one contains anything but a comment or a filename marker,
   91          * we don't want to chew on it
   92          */
   93         if (p[3] & ~(0x18))
   94                 return ENOEXEC; /* 3    Flags            */
   95 
   96         /* These are of no use to us */
   97         /* 4-7  Timestamp                */
   98         /* 8    Extra flags              */
   99 
  100         bzero(&igz, sizeof igz);
  101         bzero(&infl, sizeof infl);
  102         infl.gz_private = (void *) &igz;
  103         infl.gz_input = NextByte;
  104         infl.gz_output = Flush;
  105 
  106         igz.ip = imgp;
  107         igz.idx = 10;
  108 
  109         if (p[3] & 0x08) {      /* skip a filename */
  110                 while (p[igz.idx++])
  111                         if (igz.idx >= PAGE_SIZE)
  112                                 return ENOEXEC;
  113         }
  114         if (p[3] & 0x10) {      /* skip a comment */
  115                 while (p[igz.idx++])
  116                         if (igz.idx >= PAGE_SIZE)
  117                                 return ENOEXEC;
  118         }
  119         igz.len = imgp->attr->va_size;
  120 
  121         error = inflate(&infl);
  122 
  123         /*
  124          * The unzipped file may not even have been long enough to contain
  125          * a header giving Flush() a chance to return error.  Check for this.
  126          */
  127         if ( !igz.gotheader )
  128                 return ENOEXEC;
  129 
  130         if ( !error ) {
  131                 vmspace = imgp->proc->p_vmspace;
  132                 error = vm_map_protect(&vmspace->vm_map,
  133                         (vm_offset_t) vmspace->vm_taddr,
  134                         (vm_offset_t) (vmspace->vm_taddr + 
  135                                       (vmspace->vm_tsize << PAGE_SHIFT)) ,
  136                         VM_PROT_READ|VM_PROT_EXECUTE,0);
  137         }
  138 
  139         if (igz.inbuf)
  140                 kmap_free_wakeup(exec_map, (vm_offset_t)igz.inbuf, PAGE_SIZE);
  141         if (igz.error || error) {
  142                 printf("Output=%lu ", igz.output);
  143                 printf("Inflate_error=%d igz.error=%d where=%d\n",
  144                        error, igz.error, igz.where);
  145         }
  146         if (igz.error)
  147                 return igz.error;
  148         if (error)
  149                 return ENOEXEC;
  150         return 0;
  151 }
  152 
  153 static int
  154 do_aout_hdr(struct imgact_gzip * gz)
  155 {
  156         int             error;
  157         struct vmspace *vmspace;
  158         vm_offset_t     vmaddr;
  159 
  160         /*
  161          * Set file/virtual offset based on a.out variant. We do two cases:
  162          * host byte order and network byte order (for NetBSD compatibility)
  163          */
  164         switch ((int) (gz->a_out.a_midmag & 0xffff)) {
  165         case ZMAGIC:
  166                 gz->virtual_offset = 0;
  167                 if (gz->a_out.a_text) {
  168                         gz->file_offset = PAGE_SIZE;
  169                 } else {
  170                         /* Bill's "screwball mode" */
  171                         gz->file_offset = 0;
  172                 }
  173                 break;
  174         case QMAGIC:
  175                 gz->virtual_offset = PAGE_SIZE;
  176                 gz->file_offset = 0;
  177                 break;
  178         default:
  179                 /* NetBSD compatibility */
  180                 switch ((int) (ntohl(gz->a_out.a_midmag) & 0xffff)) {
  181                 case ZMAGIC:
  182                 case QMAGIC:
  183                         gz->virtual_offset = PAGE_SIZE;
  184                         gz->file_offset = 0;
  185                         break;
  186                 default:
  187                         gz->where = __LINE__;
  188                         return (-1);
  189                 }
  190         }
  191 
  192         gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
  193 
  194         /*
  195          * Check various fields in header for validity/bounds.
  196          */
  197         if (                    /* entry point must lay with text region */
  198             gz->a_out.a_entry < gz->virtual_offset ||
  199             gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
  200 
  201         /* text and data size must each be page rounded */
  202             gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
  203                 gz->where = __LINE__;
  204                 return (-1);
  205         }
  206         /*
  207          * text/data/bss must not exceed limits
  208          */
  209         PROC_LOCK(gz->ip->proc);
  210         if (                    /* text can't exceed maximum text size */
  211             gz->a_out.a_text > maxtsiz ||
  212 
  213         /* data + bss can't exceed rlimit */
  214             gz->a_out.a_data + gz->bss_size >
  215             lim_cur(gz->ip->proc, RLIMIT_DATA) ||
  216             racct_set(gz->ip->proc, RACCT_DATA,
  217             gz->a_out.a_data + gz->bss_size) != 0) {
  218                 PROC_UNLOCK(gz->ip->proc);
  219                 gz->where = __LINE__;
  220                 return (ENOMEM);
  221         }
  222         PROC_UNLOCK(gz->ip->proc);
  223         /* Find out how far we should go */
  224         gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
  225 
  226         /*
  227          * Avoid a possible deadlock if the current address space is destroyed
  228          * and that address space maps the locked vnode.  In the common case,
  229          * the locked vnode's v_usecount is decremented but remains greater
  230          * than zero.  Consequently, the vnode lock is not needed by vrele().
  231          * However, in cases where the vnode lock is external, such as nullfs,
  232          * v_usecount may become zero.
  233          */
  234         VOP_UNLOCK(gz->ip->vp, 0);
  235 
  236         /*
  237          * Destroy old process VM and create a new one (with a new stack)
  238          */
  239         error = exec_new_vmspace(gz->ip, &aout_sysvec);
  240 
  241         vn_lock(gz->ip->vp, LK_EXCLUSIVE | LK_RETRY);
  242         if (error) {
  243                 gz->where = __LINE__;
  244                 return (error);
  245         }
  246 
  247         vmspace = gz->ip->proc->p_vmspace;
  248 
  249         vmaddr = gz->virtual_offset;
  250 
  251         error = vm_mmap(&vmspace->vm_map,
  252                         &vmaddr,
  253                         gz->a_out.a_text + gz->a_out.a_data,
  254                         VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
  255                         OBJT_DEFAULT,
  256                         NULL,
  257                         0);
  258 
  259         if (error) {
  260                 gz->where = __LINE__;
  261                 return (error);
  262         }
  263 
  264         if (gz->bss_size != 0) {
  265                 /*
  266                  * Allocate demand-zeroed area for uninitialized data.
  267                  * "bss" = 'block started by symbol' - named after the 
  268                  * IBM 7090 instruction of the same name.
  269                  */
  270                 vmaddr = gz->virtual_offset + gz->a_out.a_text + 
  271                         gz->a_out.a_data;
  272                 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
  273                     gz->bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL,
  274                     0);
  275                 if (error) {
  276                         gz->where = __LINE__;
  277                         return (error);
  278                 }
  279         }
  280         /* Fill in process VM information */
  281         vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
  282         vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
  283         vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
  284         vmspace->vm_daddr = (caddr_t) (uintptr_t)
  285                             (gz->virtual_offset + gz->a_out.a_text);
  286 
  287         /* Fill in image_params */
  288         gz->ip->interpreted = 0;
  289         gz->ip->entry_addr = gz->a_out.a_entry;
  290 
  291         gz->ip->proc->p_sysent = &aout_sysvec;
  292 
  293         return 0;
  294 }
  295 
  296 static int
  297 NextByte(void *vp)
  298 {
  299         int             error;
  300         struct imgact_gzip *igz = (struct imgact_gzip *) vp;
  301 
  302         if (igz->idx >= igz->len) {
  303                 igz->where = __LINE__;
  304                 return GZ_EOF;
  305         }
  306         if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
  307                 return igz->inbuf[(igz->idx++) - igz->offset];
  308         }
  309         if (igz->inbuf)
  310                 kmap_free_wakeup(exec_map, (vm_offset_t)igz->inbuf, PAGE_SIZE);
  311         igz->offset = igz->idx & ~PAGE_MASK;
  312 
  313         error = vm_mmap(exec_map,       /* map */
  314                         (vm_offset_t *) & igz->inbuf,   /* address */
  315                         PAGE_SIZE,      /* size */
  316                         VM_PROT_READ,   /* protection */
  317                         VM_PROT_READ,   /* max protection */
  318                         0,      /* flags */
  319                         OBJT_VNODE,     /* handle type */
  320                         igz->ip->vp,    /* vnode */
  321                         igz->offset);   /* offset */
  322         if (error) {
  323                 igz->where = __LINE__;
  324                 igz->error = error;
  325                 return GZ_EOF;
  326         }
  327         return igz->inbuf[(igz->idx++) - igz->offset];
  328 }
  329 
  330 static int
  331 Flush(void *vp, u_char * ptr, u_long siz)
  332 {
  333         struct imgact_gzip *gz = (struct imgact_gzip *) vp;
  334         u_char         *p = ptr, *q;
  335         int             i;
  336 
  337         /* First, find an a.out-header. */
  338         if (gz->output < sizeof gz->a_out) {
  339                 q = (u_char *) & gz->a_out;
  340                 i = min(siz, sizeof gz->a_out - gz->output);
  341                 bcopy(p, q + gz->output, i);
  342                 gz->output += i;
  343                 p += i;
  344                 siz -= i;
  345                 if (gz->output == sizeof gz->a_out) {
  346                         gz->gotheader = 1;
  347                         i = do_aout_hdr(gz);
  348                         if (i == -1) {
  349                                 if (!gz->where)
  350                                         gz->where = __LINE__;
  351                                 gz->error = ENOEXEC;
  352                                 return ENOEXEC;
  353                         } else if (i) {
  354                                 gz->where = __LINE__;
  355                                 gz->error = i;
  356                                 return ENOEXEC;
  357                         }
  358                         if (gz->file_offset == 0) {
  359                                 q = (u_char *) (uintptr_t) gz->virtual_offset;
  360                                 copyout(&gz->a_out, q, sizeof gz->a_out);
  361                         }
  362                 }
  363         }
  364         /* Skip over zero-padded first PAGE if needed */
  365         if (gz->output < gz->file_offset &&
  366             gz->output + siz > gz->file_offset) {
  367                 i = min(siz, gz->file_offset - gz->output);
  368                 gz->output += i;
  369                 p += i;
  370                 siz -= i;
  371         }
  372         if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
  373                 i = min(siz, gz->file_end - gz->output);
  374                 q = (u_char *) (uintptr_t)
  375                     (gz->virtual_offset + gz->output - gz->file_offset);
  376                 copyout(p, q, i);
  377                 gz->output += i;
  378                 p += i;
  379                 siz -= i;
  380         }
  381         gz->output += siz;
  382         return 0;
  383 }
  384 
  385 
  386 /*
  387  * Tell kern_execve.c about it, with a little help from the linker.
  388  */
  389 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
  390 EXEC_SET(execgzip, gzip_execsw);

Cache object: ee00c8f6398876e93c2aafdb5155ea58


[ 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.