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

Cache object: 12e3f0f91ab01a4d1e91661a505c653d


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