imgact_gzip.c revision 12568
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 * 912568Sbde * $Id: imgact_gzip.c,v 1.15 1995/11/06 12:52:30 davidg 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> 303507Scsgr#include <sys/resourcevar.h> 313332Sphk#include <sys/sysent.h> 323507Scsgr#include <sys/systm.h> 333784Sphk#include <sys/inflate.h> 343332Sphk 353332Sphk#include <vm/vm.h> 363332Sphk#include <vm/vm_kern.h> 373332Sphk 383784Sphkstruct imgact_gzip { 393784Sphk struct image_params *ip; 403784Sphk struct exec a_out; 413784Sphk int error; 423784Sphk int where; 433784Sphk u_char *inbuf; 443784Sphk u_long offset; 453784Sphk u_long output; 463784Sphk u_long len; 473784Sphk int idx; 483784Sphk u_long virtual_offset, file_offset, file_end, bss_size; 493784Sphk}; 503784Sphk 5112568Sbdestatic int exec_gzip_imgact __P((struct image_params *imgp)); 523784Sphkstatic int NextByte __P((void *vp)); 533784Sphkstatic int do_aout_hdr __P((struct imgact_gzip *)); 543784Sphkstatic int Flush __P((void *vp, u_char *, u_long siz)); 553784Sphk 5612568Sbdestatic int 5712130Sdgexec_gzip_imgact(imgp) 5812130Sdg struct image_params *imgp; 593332Sphk{ 603784Sphk int error, error2 = 0; 6112130Sdg u_char *p = (u_char *) imgp->image_header; 623784Sphk struct imgact_gzip igz; 633784Sphk struct inflate infl; 643332Sphk 653348Sphk /* If these four are not OK, it isn't a gzip file */ 663784Sphk if (p[0] != 0x1f) 673784Sphk return -1; /* 0 Simply magic */ 683784Sphk if (p[1] != 0x8b) 693784Sphk return -1; /* 1 Simply magic */ 703784Sphk if (p[2] != 0x08) 713784Sphk return -1; /* 2 Compression method */ 723784Sphk if (p[9] != 0x03) 733784Sphk return -1; /* 9 OS compressed on */ 743332Sphk 753784Sphk /* 763784Sphk * If this one contains anything but a comment or a filename marker, 773784Sphk * we don't want to chew on it 783348Sphk */ 793784Sphk if (p[3] & ~(0x18)) 803784Sphk return ENOEXEC; /* 3 Flags */ 813332Sphk 823348Sphk /* These are of no use to us */ 833784Sphk /* 4-7 Timestamp */ 843784Sphk /* 8 Extra flags */ 853348Sphk 863784Sphk bzero(&igz, sizeof igz); 873784Sphk bzero(&infl, sizeof infl); 883784Sphk infl.gz_private = (void *) &igz; 893784Sphk infl.gz_input = NextByte; 903784Sphk infl.gz_output = Flush; 913332Sphk 9212130Sdg igz.ip = imgp; 933784Sphk igz.idx = 10; 943353Sphk 953784Sphk if (p[3] & 0x08) { /* skip a filename */ 963784Sphk while (p[igz.idx++]) 973784Sphk if (igz.idx >= PAGE_SIZE) 983784Sphk return ENOEXEC; 993348Sphk } 1003784Sphk if (p[3] & 0x10) { /* skip a comment */ 1013784Sphk while (p[igz.idx++]) 1023784Sphk if (igz.idx >= PAGE_SIZE) 1033784Sphk return ENOEXEC; 1043348Sphk } 1053784Sphk igz.len = igz.ip->attr->va_size; 1063332Sphk 1073784Sphk error = inflate(&infl); 1083348Sphk 1093784Sphk if (igz.inbuf) { 1103784Sphk error2 = 1116579Sdg vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf, 1126579Sdg (vm_offset_t) igz.inbuf + PAGE_SIZE); 1133332Sphk } 1143784Sphk if (igz.error || error || error2) { 1153784Sphk printf("Output=%lu ", igz.output); 1163784Sphk printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n", 1173784Sphk error, igz.error, error2, igz.where); 1183348Sphk } 1193784Sphk if (igz.error) 1203784Sphk return igz.error; 1213784Sphk if (error) 1223784Sphk return ENOEXEC; 1238876Srgrimes if (error2) 1243784Sphk return error2; 1253784Sphk return 0; 1263332Sphk} 1273332Sphk 1283784Sphkstatic int 1293784Sphkdo_aout_hdr(struct imgact_gzip * gz) 1303332Sphk{ 1313784Sphk int error; 1323784Sphk struct vmspace *vmspace = gz->ip->proc->p_vmspace; 1333784Sphk u_long vmaddr; 1343332Sphk 1353784Sphk /* 1363784Sphk * Set file/virtual offset based on a.out variant. We do two cases: 1373784Sphk * host byte order and network byte order (for NetBSD compatibility) 1383784Sphk */ 1393784Sphk switch ((int) (gz->a_out.a_magic & 0xffff)) { 1403332Sphk case ZMAGIC: 1413784Sphk gz->virtual_offset = 0; 1423784Sphk if (gz->a_out.a_text) { 1433784Sphk gz->file_offset = NBPG; 1443784Sphk } else { 1453784Sphk /* Bill's "screwball mode" */ 1463784Sphk gz->file_offset = 0; 1473784Sphk } 1483784Sphk break; 1493332Sphk case QMAGIC: 1503784Sphk gz->virtual_offset = NBPG; 1513784Sphk gz->file_offset = 0; 1523784Sphk break; 1533332Sphk default: 1543784Sphk /* NetBSD compatibility */ 1553784Sphk switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) { 1563784Sphk case ZMAGIC: 1573784Sphk case QMAGIC: 1583784Sphk gz->virtual_offset = NBPG; 1593784Sphk gz->file_offset = 0; 1603784Sphk break; 1613784Sphk default: 1623784Sphk gz->where = __LINE__; 1633784Sphk return (-1); 1643784Sphk } 1653332Sphk } 1663332Sphk 1673784Sphk gz->bss_size = roundup(gz->a_out.a_bss, NBPG); 1683332Sphk 1693784Sphk /* 1703784Sphk * Check various fields in header for validity/bounds. 1713784Sphk */ 1723784Sphk if ( /* entry point must lay with text region */ 1733784Sphk gz->a_out.a_entry < gz->virtual_offset || 1743784Sphk gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text || 1753332Sphk 1763332Sphk /* text and data size must each be page rounded */ 1773784Sphk gz->a_out.a_text % NBPG || 1783784Sphk gz->a_out.a_data % NBPG) { 1793784Sphk gz->where = __LINE__; 1803784Sphk return (-1); 1813784Sphk } 1823784Sphk /* 1833784Sphk * text/data/bss must not exceed limits 1843784Sphk */ 1853784Sphk if ( /* text can't exceed maximum text size */ 1863784Sphk gz->a_out.a_text > MAXTSIZ || 1873332Sphk 1883332Sphk /* data + bss can't exceed maximum data size */ 1893784Sphk gz->a_out.a_data + gz->bss_size > MAXDSIZ || 1903332Sphk 1913332Sphk /* data + bss can't exceed rlimit */ 1923784Sphk gz->a_out.a_data + gz->bss_size > 1933332Sphk gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) { 1943784Sphk gz->where = __LINE__; 1953784Sphk return (ENOMEM); 1963784Sphk } 1973784Sphk /* Find out how far we should go */ 1983784Sphk gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data; 1993332Sphk 2003784Sphk /* copy in arguments and/or environment from old process */ 2013784Sphk error = exec_extract_strings(gz->ip); 2023784Sphk if (error) { 2033784Sphk gz->where = __LINE__; 2043784Sphk return (error); 2053784Sphk } 2063784Sphk /* 2073784Sphk * Destroy old process VM and create a new one (with a new stack) 2083784Sphk */ 2093784Sphk exec_new_vmspace(gz->ip); 2103348Sphk 2113784Sphk vmaddr = gz->virtual_offset; 2123332Sphk 2133784Sphk error = vm_mmap(&vmspace->vm_map, /* map */ 2143784Sphk &vmaddr,/* address */ 2153784Sphk gz->a_out.a_text, /* size */ 2163784Sphk VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, /* protection */ 2173784Sphk VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, 2183784Sphk MAP_ANON | MAP_FIXED, /* flags */ 2193784Sphk 0, /* vnode */ 2203784Sphk 0); /* offset */ 2213332Sphk 2223784Sphk if (error) { 2233784Sphk gz->where = __LINE__; 2243784Sphk return (error); 2253784Sphk } 2263784Sphk vmaddr = gz->virtual_offset + gz->a_out.a_text; 2273332Sphk 2283784Sphk /* 2293784Sphk * Map data read/write (if text is 0, assume text is in data area 2303784Sphk * [Bill's screwball mode]) 2313784Sphk */ 2323332Sphk 2333784Sphk error = vm_mmap(&vmspace->vm_map, 2343784Sphk &vmaddr, 2353784Sphk gz->a_out.a_data, 2363784Sphk VM_PROT_READ | VM_PROT_WRITE | (gz->a_out.a_text ? 0 : VM_PROT_EXECUTE), 2373784Sphk VM_PROT_ALL, MAP_ANON | MAP_FIXED, 2383784Sphk 0, 2393784Sphk 0); 2403332Sphk 2413784Sphk if (error) { 2423784Sphk gz->where = __LINE__; 2433784Sphk return (error); 2443784Sphk } 2456579Sdg if (gz->bss_size != 0) { 2466579Sdg /* 2476579Sdg * Allocate demand-zeroed area for uninitialized data "bss" = 'block 2486579Sdg * started by symbol' - named after the IBM 7090 instruction of the 2496579Sdg * same name. 2506579Sdg */ 2516579Sdg vmaddr = gz->virtual_offset + gz->a_out.a_text + gz->a_out.a_data; 2526579Sdg error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, gz->bss_size, FALSE); 2536579Sdg if (error) { 2546579Sdg gz->where = __LINE__; 2556579Sdg return (error); 2566579Sdg } 2573784Sphk } 2583784Sphk /* Fill in process VM information */ 2593784Sphk vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT; 2603784Sphk vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT; 2613784Sphk vmspace->vm_taddr = (caddr_t) gz->virtual_offset; 2623784Sphk vmspace->vm_daddr = (caddr_t) gz->virtual_offset + gz->a_out.a_text; 2633332Sphk 2643784Sphk /* Fill in image_params */ 2653784Sphk gz->ip->interpreted = 0; 2663784Sphk gz->ip->entry_addr = gz->a_out.a_entry; 2673332Sphk 2683784Sphk gz->ip->proc->p_sysent = &aout_sysvec; 2693332Sphk 2703784Sphk return 0; 2713784Sphk} 2723332Sphk 2733784Sphkstatic int 2743784SphkNextByte(void *vp) 2753784Sphk{ 2763784Sphk int error; 2773784Sphk struct imgact_gzip *igz = (struct imgact_gzip *) vp; 2783332Sphk 2793784Sphk if (igz->idx >= igz->len) { 2803784Sphk igz->where = __LINE__; 2813784Sphk return GZ_EOF; 2823784Sphk } 2833784Sphk if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) { 2843784Sphk return igz->inbuf[(igz->idx++) - igz->offset]; 2853784Sphk } 2863784Sphk if (igz->inbuf) { 2876579Sdg error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf, 2886579Sdg (vm_offset_t) igz->inbuf + PAGE_SIZE); 2893784Sphk if (error) { 2903784Sphk igz->where = __LINE__; 2913784Sphk igz->error = error; 2923784Sphk return GZ_EOF; 2933784Sphk } 2943784Sphk } 2956342Sphk igz->offset = igz->idx & ~PAGE_MASK; 2963332Sphk 2973784Sphk error = vm_mmap(kernel_map, /* map */ 2983784Sphk (vm_offset_t *) & igz->inbuf, /* address */ 2993784Sphk PAGE_SIZE, /* size */ 3003784Sphk VM_PROT_READ, /* protection */ 3013784Sphk VM_PROT_READ, /* max protection */ 3023784Sphk 0, /* flags */ 30312130Sdg (caddr_t) igz->ip->vp, /* vnode */ 3043784Sphk igz->offset); /* offset */ 3053784Sphk if (error) { 3063784Sphk igz->where = __LINE__; 3073784Sphk igz->error = error; 3083784Sphk return GZ_EOF; 3093784Sphk } 3103784Sphk return igz->inbuf[(igz->idx++) - igz->offset]; 3113784Sphk} 3123332Sphk 3133784Sphkstatic int 3143784SphkFlush(void *vp, u_char * ptr, u_long siz) 3153784Sphk{ 3163784Sphk struct imgact_gzip *gz = (struct imgact_gzip *) vp; 3173784Sphk u_char *p = ptr, *q; 3183784Sphk int i; 3193348Sphk 3203784Sphk /* First, find a a.out-header */ 3213784Sphk if (gz->output < sizeof gz->a_out) { 3223784Sphk q = (u_char *) & gz->a_out; 3233784Sphk i = min(siz, sizeof gz->a_out - gz->output); 3243784Sphk bcopy(p, q + gz->output, i); 3253784Sphk gz->output += i; 3263784Sphk p += i; 3273784Sphk siz -= i; 3283784Sphk if (gz->output == sizeof gz->a_out) { 3293784Sphk i = do_aout_hdr(gz); 3303784Sphk if (i == -1) { 3313785Sphk if (!gz->where) 3323785Sphk gz->where = __LINE__; 3333784Sphk gz->error = ENOEXEC; 3343784Sphk return ENOEXEC; 3353784Sphk } else if (i) { 3363784Sphk gz->where = __LINE__; 3373784Sphk gz->error = i; 3383784Sphk return ENOEXEC; 3393784Sphk } 3403784Sphk if (gz->file_offset < sizeof gz->a_out) { 3413784Sphk q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset; 3423784Sphk bcopy(&gz->a_out, q, sizeof gz->a_out - gz->file_offset); 3433784Sphk } 3443784Sphk } 3453784Sphk } 3463784Sphk /* Skip over zero-padded first PAGE if needed */ 3473784Sphk if (gz->output < gz->file_offset && (gz->output + siz) > gz->file_offset) { 3483784Sphk i = min(siz, gz->file_offset - gz->output); 3493784Sphk gz->output += i; 3503784Sphk p += i; 3513784Sphk siz -= i; 3523784Sphk } 3533784Sphk if (gz->output >= gz->file_offset && gz->output < gz->file_end) { 3543784Sphk i = min(siz, gz->file_end - gz->output); 3553784Sphk q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset; 3563784Sphk bcopy(p, q, i); 3573784Sphk gz->output += i; 3583784Sphk p += i; 3593784Sphk siz -= i; 3603784Sphk } 3613784Sphk gz->output += siz; 3623784Sphk return 0; 3633332Sphk} 3643332Sphk 3653784Sphk 3663332Sphk/* 3673332Sphk * Tell kern_execve.c about it, with a little help from the linker. 3683332Sphk * Since `const' objects end up in the text segment, TEXT_SET is the 3693332Sphk * correct directive to use. 3703332Sphk */ 3713784Sphk 3723784Sphkstatic const struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"}; 3733332SphkTEXT_SET(execsw_set, gzip_execsw); 374