imgact_gzip.c revision 37656
13332Sphk/* 23332Sphk * ---------------------------------------------------------------------------- 33332Sphk * "THE BEER-WARE LICENSE" (Revision 42): 43784Sphk * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you 53332Sphk * can do whatever you want with this stuff. If we meet some day, and you think 63332Sphk * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 73332Sphk * ---------------------------------------------------------------------------- 83332Sphk * 937656Sbde * $Id: imgact_gzip.c,v 1.33 1998/06/16 14:36:40 bde Exp $ 103332Sphk * 113332Sphk * This module handles execution of a.out files which have been run through 123784Sphk * "gzip". This saves diskspace, but wastes cpu-cycles and VM. 133332Sphk * 143332Sphk * TODO: 153332Sphk * text-segments should be made R/O after being filled 163332Sphk * is the vm-stuff safe ? 173332Sphk * should handle the entire header of gzip'ed stuff. 183332Sphk * inflate isn't quite reentrant yet... 193332Sphk * error-handling is a mess... 203332Sphk * so is the rest... 213417Scsgr * tidy up unnecesary includes 223332Sphk */ 233332Sphk 243784Sphk#include <sys/param.h> 253332Sphk#include <sys/exec.h> 263332Sphk#include <sys/imgact.h> 273332Sphk#include <sys/imgact_aout.h> 283332Sphk#include <sys/kernel.h> 293507Scsgr#include <sys/mman.h> 3015494Sbde#include <sys/proc.h> 313507Scsgr#include <sys/resourcevar.h> 323332Sphk#include <sys/sysent.h> 333507Scsgr#include <sys/systm.h> 3415494Sbde#include <sys/vnode.h> 353784Sphk#include <sys/inflate.h> 363332Sphk 373332Sphk#include <vm/vm.h> 3812662Sdg#include <vm/vm_param.h> 3912662Sdg#include <vm/vm_prot.h> 4022521Sdyson#include <sys/lock.h> 4112662Sdg#include <vm/pmap.h> 4212662Sdg#include <vm/vm_map.h> 433332Sphk#include <vm/vm_kern.h> 4412662Sdg#include <vm/vm_extern.h> 453332Sphk 463784Sphkstruct imgact_gzip { 473784Sphk struct image_params *ip; 483784Sphk struct exec a_out; 493784Sphk int error; 503784Sphk int where; 513784Sphk u_char *inbuf; 523784Sphk u_long offset; 533784Sphk u_long output; 543784Sphk u_long len; 553784Sphk int idx; 563784Sphk u_long virtual_offset, file_offset, file_end, bss_size; 573784Sphk}; 583784Sphk 5912568Sbdestatic int exec_gzip_imgact __P((struct image_params *imgp)); 603784Sphkstatic int NextByte __P((void *vp)); 613784Sphkstatic int do_aout_hdr __P((struct imgact_gzip *)); 623784Sphkstatic int Flush __P((void *vp, u_char *, u_long siz)); 633784Sphk 6412568Sbdestatic int 6512130Sdgexec_gzip_imgact(imgp) 6612130Sdg struct image_params *imgp; 673332Sphk{ 683784Sphk int error, error2 = 0; 6917974Sbde const u_char *p = (const u_char *) imgp->image_header; 703784Sphk struct imgact_gzip igz; 713784Sphk struct inflate infl; 7217386Sphk struct vmspace *vmspace; 733332Sphk 743348Sphk /* If these four are not OK, it isn't a gzip file */ 753784Sphk if (p[0] != 0x1f) 763784Sphk return -1; /* 0 Simply magic */ 773784Sphk if (p[1] != 0x8b) 783784Sphk return -1; /* 1 Simply magic */ 793784Sphk if (p[2] != 0x08) 803784Sphk return -1; /* 2 Compression method */ 813784Sphk if (p[9] != 0x03) 823784Sphk return -1; /* 9 OS compressed on */ 833332Sphk 843784Sphk /* 853784Sphk * If this one contains anything but a comment or a filename marker, 863784Sphk * we don't want to chew on it 873348Sphk */ 883784Sphk if (p[3] & ~(0x18)) 893784Sphk return ENOEXEC; /* 3 Flags */ 903332Sphk 913348Sphk /* These are of no use to us */ 923784Sphk /* 4-7 Timestamp */ 933784Sphk /* 8 Extra flags */ 943348Sphk 953784Sphk bzero(&igz, sizeof igz); 963784Sphk bzero(&infl, sizeof infl); 973784Sphk infl.gz_private = (void *) &igz; 983784Sphk infl.gz_input = NextByte; 993784Sphk infl.gz_output = Flush; 1003332Sphk 10112130Sdg igz.ip = imgp; 1023784Sphk igz.idx = 10; 1033353Sphk 1043784Sphk if (p[3] & 0x08) { /* skip a filename */ 1053784Sphk while (p[igz.idx++]) 1063784Sphk if (igz.idx >= PAGE_SIZE) 1073784Sphk return ENOEXEC; 1083348Sphk } 1093784Sphk if (p[3] & 0x10) { /* skip a comment */ 1103784Sphk while (p[igz.idx++]) 1113784Sphk if (igz.idx >= PAGE_SIZE) 1123784Sphk return ENOEXEC; 1133348Sphk } 11417386Sphk igz.len = imgp->attr->va_size; 1153332Sphk 1163784Sphk error = inflate(&infl); 1173348Sphk 11817386Sphk if ( !error ) { 11917386Sphk vmspace = imgp->proc->p_vmspace; 12017386Sphk error = vm_map_protect(&vmspace->vm_map, 12117386Sphk (vm_offset_t) vmspace->vm_taddr, 12217386Sphk (vm_offset_t) (vmspace->vm_taddr + 12317386Sphk (vmspace->vm_tsize << PAGE_SHIFT)) , 12417386Sphk VM_PROT_READ|VM_PROT_EXECUTE,0); 12517386Sphk } 12617386Sphk 1273784Sphk if (igz.inbuf) { 1283784Sphk error2 = 1296579Sdg vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf, 1306579Sdg (vm_offset_t) igz.inbuf + PAGE_SIZE); 1313332Sphk } 1323784Sphk if (igz.error || error || error2) { 1333784Sphk printf("Output=%lu ", igz.output); 1343784Sphk printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n", 1353784Sphk error, igz.error, error2, igz.where); 1363348Sphk } 1373784Sphk if (igz.error) 1383784Sphk return igz.error; 1393784Sphk if (error) 1403784Sphk return ENOEXEC; 1418876Srgrimes if (error2) 1423784Sphk return error2; 1433784Sphk return 0; 1443332Sphk} 1453332Sphk 1463784Sphkstatic int 1473784Sphkdo_aout_hdr(struct imgact_gzip * gz) 1483332Sphk{ 1493784Sphk int error; 15024848Sdyson struct vmspace *vmspace; 15114703Sbde vm_offset_t vmaddr; 1523332Sphk 1533784Sphk /* 1543784Sphk * Set file/virtual offset based on a.out variant. We do two cases: 1553784Sphk * host byte order and network byte order (for NetBSD compatibility) 1563784Sphk */ 1573784Sphk switch ((int) (gz->a_out.a_magic & 0xffff)) { 1583332Sphk case ZMAGIC: 1593784Sphk gz->virtual_offset = 0; 1603784Sphk if (gz->a_out.a_text) { 16115538Sphk gz->file_offset = PAGE_SIZE; 1623784Sphk } else { 1633784Sphk /* Bill's "screwball mode" */ 1643784Sphk gz->file_offset = 0; 1653784Sphk } 1663784Sphk break; 1673332Sphk case QMAGIC: 16815538Sphk gz->virtual_offset = PAGE_SIZE; 1693784Sphk gz->file_offset = 0; 1703784Sphk break; 1713332Sphk default: 1723784Sphk /* NetBSD compatibility */ 1733784Sphk switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) { 1743784Sphk case ZMAGIC: 1753784Sphk case QMAGIC: 17615538Sphk gz->virtual_offset = PAGE_SIZE; 1773784Sphk gz->file_offset = 0; 1783784Sphk break; 1793784Sphk default: 1803784Sphk gz->where = __LINE__; 1813784Sphk return (-1); 1823784Sphk } 1833332Sphk } 1843332Sphk 18515538Sphk gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE); 1863332Sphk 1873784Sphk /* 1883784Sphk * Check various fields in header for validity/bounds. 1893784Sphk */ 1903784Sphk if ( /* entry point must lay with text region */ 1913784Sphk gz->a_out.a_entry < gz->virtual_offset || 1923784Sphk gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text || 1933332Sphk 1943332Sphk /* text and data size must each be page rounded */ 19515538Sphk gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) { 1963784Sphk gz->where = __LINE__; 1973784Sphk return (-1); 1983784Sphk } 1993784Sphk /* 2003784Sphk * text/data/bss must not exceed limits 2013784Sphk */ 2023784Sphk if ( /* text can't exceed maximum text size */ 2033784Sphk gz->a_out.a_text > MAXTSIZ || 2043332Sphk 2053332Sphk /* data + bss can't exceed rlimit */ 2063784Sphk gz->a_out.a_data + gz->bss_size > 2073332Sphk gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) { 2083784Sphk gz->where = __LINE__; 2093784Sphk return (ENOMEM); 2103784Sphk } 2113784Sphk /* Find out how far we should go */ 2123784Sphk gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data; 2133332Sphk 2143784Sphk /* copy in arguments and/or environment from old process */ 2153784Sphk error = exec_extract_strings(gz->ip); 2163784Sphk if (error) { 2173784Sphk gz->where = __LINE__; 2183784Sphk return (error); 2193784Sphk } 2203784Sphk /* 2213784Sphk * Destroy old process VM and create a new one (with a new stack) 2223784Sphk */ 2233784Sphk exec_new_vmspace(gz->ip); 2243348Sphk 22524848Sdyson vmspace = gz->ip->proc->p_vmspace; 22624848Sdyson 2273784Sphk vmaddr = gz->virtual_offset; 2283332Sphk 2293784Sphk error = vm_mmap(&vmspace->vm_map, 2303784Sphk &vmaddr, 23117386Sphk gz->a_out.a_text + gz->a_out.a_data, 23217386Sphk VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED, 2333784Sphk 0, 2343784Sphk 0); 2353332Sphk 2363784Sphk if (error) { 2373784Sphk gz->where = __LINE__; 2383784Sphk return (error); 2393784Sphk } 24017386Sphk 2416579Sdg if (gz->bss_size != 0) { 2426579Sdg /* 24314087Sphk * Allocate demand-zeroed area for uninitialized data. 24414087Sphk * "bss" = 'block started by symbol' - named after the 24514087Sphk * IBM 7090 instruction of the same name. 2466579Sdg */ 24714087Sphk vmaddr = gz->virtual_offset + gz->a_out.a_text + 24814087Sphk gz->a_out.a_data; 24917386Sphk error = vm_map_find(&vmspace->vm_map, 25017386Sphk NULL, 25117386Sphk 0, 25217386Sphk &vmaddr, 25317386Sphk gz->bss_size, 25417386Sphk FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 2556579Sdg if (error) { 2566579Sdg gz->where = __LINE__; 2576579Sdg return (error); 2586579Sdg } 2593784Sphk } 2603784Sphk /* Fill in process VM information */ 2613784Sphk vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT; 2623784Sphk vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT; 26337656Sbde vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset; 26437656Sbde vmspace->vm_daddr = (caddr_t) (uintptr_t) 26537656Sbde (gz->virtual_offset + gz->a_out.a_text); 2663332Sphk 2673784Sphk /* Fill in image_params */ 2683784Sphk gz->ip->interpreted = 0; 2693784Sphk gz->ip->entry_addr = gz->a_out.a_entry; 2703332Sphk 2713784Sphk gz->ip->proc->p_sysent = &aout_sysvec; 2723332Sphk 2733784Sphk return 0; 2743784Sphk} 2753332Sphk 2763784Sphkstatic int 2773784SphkNextByte(void *vp) 2783784Sphk{ 2793784Sphk int error; 2803784Sphk struct imgact_gzip *igz = (struct imgact_gzip *) vp; 2813332Sphk 2823784Sphk if (igz->idx >= igz->len) { 2833784Sphk igz->where = __LINE__; 2843784Sphk return GZ_EOF; 2853784Sphk } 2863784Sphk if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) { 2873784Sphk return igz->inbuf[(igz->idx++) - igz->offset]; 2883784Sphk } 2893784Sphk if (igz->inbuf) { 2906579Sdg error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf, 2916579Sdg (vm_offset_t) igz->inbuf + PAGE_SIZE); 2923784Sphk if (error) { 2933784Sphk igz->where = __LINE__; 2943784Sphk igz->error = error; 2953784Sphk return GZ_EOF; 2963784Sphk } 2973784Sphk } 2986342Sphk igz->offset = igz->idx & ~PAGE_MASK; 2993332Sphk 3003784Sphk error = vm_mmap(kernel_map, /* map */ 3013784Sphk (vm_offset_t *) & igz->inbuf, /* address */ 3023784Sphk PAGE_SIZE, /* size */ 3033784Sphk VM_PROT_READ, /* protection */ 3043784Sphk VM_PROT_READ, /* max protection */ 3053784Sphk 0, /* flags */ 30612130Sdg (caddr_t) igz->ip->vp, /* vnode */ 3073784Sphk igz->offset); /* offset */ 3083784Sphk if (error) { 3093784Sphk igz->where = __LINE__; 3103784Sphk igz->error = error; 3113784Sphk return GZ_EOF; 3123784Sphk } 3133784Sphk return igz->inbuf[(igz->idx++) - igz->offset]; 3143784Sphk} 3153332Sphk 3163784Sphkstatic int 3173784SphkFlush(void *vp, u_char * ptr, u_long siz) 3183784Sphk{ 3193784Sphk struct imgact_gzip *gz = (struct imgact_gzip *) vp; 3203784Sphk u_char *p = ptr, *q; 3213784Sphk int i; 3223348Sphk 3233784Sphk /* First, find a a.out-header */ 3243784Sphk if (gz->output < sizeof gz->a_out) { 3253784Sphk q = (u_char *) & gz->a_out; 3263784Sphk i = min(siz, sizeof gz->a_out - gz->output); 3273784Sphk bcopy(p, q + gz->output, i); 3283784Sphk gz->output += i; 3293784Sphk p += i; 3303784Sphk siz -= i; 3313784Sphk if (gz->output == sizeof gz->a_out) { 3323784Sphk i = do_aout_hdr(gz); 3333784Sphk if (i == -1) { 3343785Sphk if (!gz->where) 3353785Sphk gz->where = __LINE__; 3363784Sphk gz->error = ENOEXEC; 3373784Sphk return ENOEXEC; 3383784Sphk } else if (i) { 3393784Sphk gz->where = __LINE__; 3403784Sphk gz->error = i; 3413784Sphk return ENOEXEC; 3423784Sphk } 34331718Sjdp if (gz->file_offset == 0) { 34437656Sbde q = (u_char *) (uintptr_t) gz->virtual_offset; 34537015Sbde copyout(&gz->a_out, q, sizeof gz->a_out); 3463784Sphk } 3473784Sphk } 3483784Sphk } 3493784Sphk /* Skip over zero-padded first PAGE if needed */ 35037015Sbde if (gz->output < gz->file_offset && 35137015Sbde gz->output + siz > gz->file_offset) { 3523784Sphk i = min(siz, gz->file_offset - gz->output); 3533784Sphk gz->output += i; 3543784Sphk p += i; 3553784Sphk siz -= i; 3563784Sphk } 3573784Sphk if (gz->output >= gz->file_offset && gz->output < gz->file_end) { 3583784Sphk i = min(siz, gz->file_end - gz->output); 35937656Sbde q = (u_char *) (uintptr_t) 36037656Sbde (gz->virtual_offset + gz->output - gz->file_offset); 36137015Sbde copyout(p, q, i); 3623784Sphk gz->output += i; 3633784Sphk p += i; 3643784Sphk siz -= i; 3653784Sphk } 3663784Sphk gz->output += siz; 3673784Sphk return 0; 3683332Sphk} 3693332Sphk 3703784Sphk 3713332Sphk/* 3723332Sphk * Tell kern_execve.c about it, with a little help from the linker. 3733332Sphk * Since `const' objects end up in the text segment, TEXT_SET is the 3743332Sphk * correct directive to use. 3753332Sphk */ 3763784Sphk 3773784Sphkstatic const struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"}; 3783332SphkTEXT_SET(execsw_set, gzip_execsw); 379