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

Cache object: 837c8f42afef19b91e1f05c2ffe1802d


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