imgact_gzip.c revision 173361
1139804Simp/*- 23332Sphk * ---------------------------------------------------------------------------- 33332Sphk * "THE BEER-WARE LICENSE" (Revision 42): 493149Sphk * <phk@FreeBSD.org> 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 * ---------------------------------------------------------------------------- 8139804Simp */ 9139804Simp 10139804Simp/* 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 24116182Sobrien#include <sys/cdefs.h> 25116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/imgact_gzip.c 173361 2007-11-05 11:36:16Z kib $"); 26116182Sobrien 273784Sphk#include <sys/param.h> 283332Sphk#include <sys/exec.h> 293332Sphk#include <sys/imgact.h> 303332Sphk#include <sys/imgact_aout.h> 313332Sphk#include <sys/kernel.h> 3276166Smarkm#include <sys/lock.h> 333507Scsgr#include <sys/mman.h> 3476166Smarkm#include <sys/mutex.h> 3515494Sbde#include <sys/proc.h> 363507Scsgr#include <sys/resourcevar.h> 373332Sphk#include <sys/sysent.h> 383507Scsgr#include <sys/systm.h> 3915494Sbde#include <sys/vnode.h> 403784Sphk#include <sys/inflate.h> 413332Sphk 423332Sphk#include <vm/vm.h> 4312662Sdg#include <vm/vm_param.h> 4412662Sdg#include <vm/pmap.h> 4512662Sdg#include <vm/vm_map.h> 463332Sphk#include <vm/vm_kern.h> 4712662Sdg#include <vm/vm_extern.h> 483332Sphk 493784Sphkstruct imgact_gzip { 503784Sphk struct image_params *ip; 513784Sphk struct exec a_out; 523784Sphk int error; 5348079Shoek int gotheader; 543784Sphk int where; 553784Sphk u_char *inbuf; 563784Sphk u_long offset; 573784Sphk u_long output; 583784Sphk u_long len; 593784Sphk int idx; 603784Sphk u_long virtual_offset, file_offset, file_end, bss_size; 613784Sphk}; 623784Sphk 6392723Salfredstatic int exec_gzip_imgact(struct image_params *imgp); 6492723Salfredstatic int NextByte(void *vp); 6592723Salfredstatic int do_aout_hdr(struct imgact_gzip *); 6692723Salfredstatic int Flush(void *vp, u_char *, u_long siz); 673784Sphk 6812568Sbdestatic int 6912130Sdgexec_gzip_imgact(imgp) 7012130Sdg struct image_params *imgp; 713332Sphk{ 723784Sphk int error, error2 = 0; 7317974Sbde const u_char *p = (const u_char *) imgp->image_header; 743784Sphk struct imgact_gzip igz; 753784Sphk struct inflate infl; 7617386Sphk struct vmspace *vmspace; 773332Sphk 783348Sphk /* If these four are not OK, it isn't a gzip file */ 793784Sphk if (p[0] != 0x1f) 803784Sphk return -1; /* 0 Simply magic */ 813784Sphk if (p[1] != 0x8b) 823784Sphk return -1; /* 1 Simply magic */ 833784Sphk if (p[2] != 0x08) 843784Sphk return -1; /* 2 Compression method */ 853784Sphk if (p[9] != 0x03) 863784Sphk return -1; /* 9 OS compressed on */ 873332Sphk 883784Sphk /* 893784Sphk * If this one contains anything but a comment or a filename marker, 903784Sphk * we don't want to chew on it 913348Sphk */ 923784Sphk if (p[3] & ~(0x18)) 933784Sphk return ENOEXEC; /* 3 Flags */ 943332Sphk 953348Sphk /* These are of no use to us */ 963784Sphk /* 4-7 Timestamp */ 973784Sphk /* 8 Extra flags */ 983348Sphk 993784Sphk bzero(&igz, sizeof igz); 1003784Sphk bzero(&infl, sizeof infl); 1013784Sphk infl.gz_private = (void *) &igz; 1023784Sphk infl.gz_input = NextByte; 1033784Sphk infl.gz_output = Flush; 1043332Sphk 10512130Sdg igz.ip = imgp; 1063784Sphk igz.idx = 10; 1073353Sphk 1083784Sphk if (p[3] & 0x08) { /* skip a filename */ 1093784Sphk while (p[igz.idx++]) 1103784Sphk if (igz.idx >= PAGE_SIZE) 1113784Sphk return ENOEXEC; 1123348Sphk } 1133784Sphk if (p[3] & 0x10) { /* skip a comment */ 1143784Sphk while (p[igz.idx++]) 1153784Sphk if (igz.idx >= PAGE_SIZE) 1163784Sphk return ENOEXEC; 1173348Sphk } 11817386Sphk igz.len = imgp->attr->va_size; 1193332Sphk 1203784Sphk error = inflate(&infl); 1213348Sphk 12248079Shoek /* 12348079Shoek * The unzipped file may not even have been long enough to contain 12448079Shoek * a header giving Flush() a chance to return error. Check for this. 12548079Shoek */ 12648079Shoek if ( !igz.gotheader ) 12748079Shoek return ENOEXEC; 12848079Shoek 12917386Sphk if ( !error ) { 13017386Sphk vmspace = imgp->proc->p_vmspace; 13117386Sphk error = vm_map_protect(&vmspace->vm_map, 13217386Sphk (vm_offset_t) vmspace->vm_taddr, 13317386Sphk (vm_offset_t) (vmspace->vm_taddr + 13417386Sphk (vmspace->vm_tsize << PAGE_SHIFT)) , 13517386Sphk VM_PROT_READ|VM_PROT_EXECUTE,0); 13617386Sphk } 13717386Sphk 1383784Sphk if (igz.inbuf) { 1393784Sphk error2 = 1406579Sdg vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf, 1416579Sdg (vm_offset_t) igz.inbuf + PAGE_SIZE); 1423332Sphk } 1433784Sphk if (igz.error || error || error2) { 1443784Sphk printf("Output=%lu ", igz.output); 1453784Sphk printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n", 1463784Sphk error, igz.error, error2, igz.where); 1473348Sphk } 1483784Sphk if (igz.error) 1493784Sphk return igz.error; 1503784Sphk if (error) 1513784Sphk return ENOEXEC; 1528876Srgrimes if (error2) 1533784Sphk return error2; 1543784Sphk return 0; 1553332Sphk} 1563332Sphk 1573784Sphkstatic int 1583784Sphkdo_aout_hdr(struct imgact_gzip * gz) 1593332Sphk{ 1603784Sphk int error; 161153698Salc struct thread *td = curthread; 16224848Sdyson struct vmspace *vmspace; 16314703Sbde vm_offset_t vmaddr; 1643332Sphk 1653784Sphk /* 1663784Sphk * Set file/virtual offset based on a.out variant. We do two cases: 1673784Sphk * host byte order and network byte order (for NetBSD compatibility) 1683784Sphk */ 1693784Sphk switch ((int) (gz->a_out.a_magic & 0xffff)) { 1703332Sphk case ZMAGIC: 1713784Sphk gz->virtual_offset = 0; 1723784Sphk if (gz->a_out.a_text) { 17315538Sphk gz->file_offset = PAGE_SIZE; 1743784Sphk } else { 1753784Sphk /* Bill's "screwball mode" */ 1763784Sphk gz->file_offset = 0; 1773784Sphk } 1783784Sphk break; 1793332Sphk case QMAGIC: 18015538Sphk gz->virtual_offset = PAGE_SIZE; 1813784Sphk gz->file_offset = 0; 1823784Sphk break; 1833332Sphk default: 1843784Sphk /* NetBSD compatibility */ 1853784Sphk switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) { 1863784Sphk case ZMAGIC: 1873784Sphk case QMAGIC: 18815538Sphk gz->virtual_offset = PAGE_SIZE; 1893784Sphk gz->file_offset = 0; 1903784Sphk break; 1913784Sphk default: 1923784Sphk gz->where = __LINE__; 1933784Sphk return (-1); 1943784Sphk } 1953332Sphk } 1963332Sphk 19715538Sphk gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE); 1983332Sphk 1993784Sphk /* 2003784Sphk * Check various fields in header for validity/bounds. 2013784Sphk */ 2023784Sphk if ( /* entry point must lay with text region */ 2033784Sphk gz->a_out.a_entry < gz->virtual_offset || 2043784Sphk gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text || 2053332Sphk 2063332Sphk /* text and data size must each be page rounded */ 20715538Sphk gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) { 2083784Sphk gz->where = __LINE__; 2093784Sphk return (-1); 2103784Sphk } 2113784Sphk /* 2123784Sphk * text/data/bss must not exceed limits 2133784Sphk */ 214125454Sjhb PROC_LOCK(gz->ip->proc); 2153784Sphk if ( /* text can't exceed maximum text size */ 21684783Sps gz->a_out.a_text > maxtsiz || 2173332Sphk 2183332Sphk /* data + bss can't exceed rlimit */ 2193784Sphk gz->a_out.a_data + gz->bss_size > 220125454Sjhb lim_cur(gz->ip->proc, RLIMIT_DATA)) { 221125454Sjhb PROC_UNLOCK(gz->ip->proc); 2223784Sphk gz->where = __LINE__; 2233784Sphk return (ENOMEM); 2243784Sphk } 225125454Sjhb PROC_UNLOCK(gz->ip->proc); 2263784Sphk /* Find out how far we should go */ 2273784Sphk gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data; 2283332Sphk 2293784Sphk /* 230153698Salc * Avoid a possible deadlock if the current address space is destroyed 231153698Salc * and that address space maps the locked vnode. In the common case, 232153698Salc * the locked vnode's v_usecount is decremented but remains greater 233153698Salc * than zero. Consequently, the vnode lock is not needed by vrele(). 234153698Salc * However, in cases where the vnode lock is external, such as nullfs, 235153698Salc * v_usecount may become zero. 236153698Salc */ 237153698Salc VOP_UNLOCK(gz->ip->vp, 0, td); 238153698Salc 239153698Salc /* 2403784Sphk * Destroy old process VM and create a new one (with a new stack) 2413784Sphk */ 242173361Skib error = exec_new_vmspace(gz->ip, &aout_sysvec); 2433348Sphk 244153698Salc vn_lock(gz->ip->vp, LK_EXCLUSIVE | LK_RETRY, td); 245173361Skib if (error) { 246173361Skib gz->where = __LINE__; 247173361Skib return (error); 248173361Skib } 249153698Salc 25024848Sdyson vmspace = gz->ip->proc->p_vmspace; 25124848Sdyson 2523784Sphk vmaddr = gz->virtual_offset; 2533332Sphk 2543784Sphk error = vm_mmap(&vmspace->vm_map, 2553784Sphk &vmaddr, 25617386Sphk gz->a_out.a_text + gz->a_out.a_data, 25717386Sphk VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED, 258144501Sjhb OBJT_DEFAULT, 259144501Sjhb NULL, 2603784Sphk 0); 2613332Sphk 2623784Sphk if (error) { 2633784Sphk gz->where = __LINE__; 2643784Sphk return (error); 2653784Sphk } 26617386Sphk 2676579Sdg if (gz->bss_size != 0) { 2686579Sdg /* 26914087Sphk * Allocate demand-zeroed area for uninitialized data. 27014087Sphk * "bss" = 'block started by symbol' - named after the 27114087Sphk * IBM 7090 instruction of the same name. 2726579Sdg */ 27314087Sphk vmaddr = gz->virtual_offset + gz->a_out.a_text + 27414087Sphk gz->a_out.a_data; 27517386Sphk error = vm_map_find(&vmspace->vm_map, 27617386Sphk NULL, 27717386Sphk 0, 27817386Sphk &vmaddr, 27917386Sphk gz->bss_size, 28017386Sphk FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 2816579Sdg if (error) { 2826579Sdg gz->where = __LINE__; 2836579Sdg return (error); 2846579Sdg } 2853784Sphk } 2863784Sphk /* Fill in process VM information */ 2873784Sphk vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT; 2883784Sphk vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT; 28937656Sbde vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset; 29037656Sbde vmspace->vm_daddr = (caddr_t) (uintptr_t) 29137656Sbde (gz->virtual_offset + gz->a_out.a_text); 2923332Sphk 2933784Sphk /* Fill in image_params */ 2943784Sphk gz->ip->interpreted = 0; 2953784Sphk gz->ip->entry_addr = gz->a_out.a_entry; 2963332Sphk 2973784Sphk gz->ip->proc->p_sysent = &aout_sysvec; 2983332Sphk 2993784Sphk return 0; 3003784Sphk} 3013332Sphk 3023784Sphkstatic int 3033784SphkNextByte(void *vp) 3043784Sphk{ 3053784Sphk int error; 3063784Sphk struct imgact_gzip *igz = (struct imgact_gzip *) vp; 3073332Sphk 3083784Sphk if (igz->idx >= igz->len) { 3093784Sphk igz->where = __LINE__; 3103784Sphk return GZ_EOF; 3113784Sphk } 3123784Sphk if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) { 3133784Sphk return igz->inbuf[(igz->idx++) - igz->offset]; 3143784Sphk } 3153784Sphk if (igz->inbuf) { 3166579Sdg error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf, 3176579Sdg (vm_offset_t) igz->inbuf + PAGE_SIZE); 3183784Sphk if (error) { 3193784Sphk igz->where = __LINE__; 3203784Sphk igz->error = error; 3213784Sphk return GZ_EOF; 3223784Sphk } 3233784Sphk } 3246342Sphk igz->offset = igz->idx & ~PAGE_MASK; 3253332Sphk 3263784Sphk error = vm_mmap(kernel_map, /* map */ 3273784Sphk (vm_offset_t *) & igz->inbuf, /* address */ 3283784Sphk PAGE_SIZE, /* size */ 3293784Sphk VM_PROT_READ, /* protection */ 3303784Sphk VM_PROT_READ, /* max protection */ 3313784Sphk 0, /* flags */ 332144501Sjhb OBJT_VNODE, /* handle type */ 333144501Sjhb igz->ip->vp, /* vnode */ 3343784Sphk igz->offset); /* offset */ 3353784Sphk if (error) { 3363784Sphk igz->where = __LINE__; 3373784Sphk igz->error = error; 3383784Sphk return GZ_EOF; 3393784Sphk } 3403784Sphk return igz->inbuf[(igz->idx++) - igz->offset]; 3413784Sphk} 3423332Sphk 3433784Sphkstatic int 3443784SphkFlush(void *vp, u_char * ptr, u_long siz) 3453784Sphk{ 3463784Sphk struct imgact_gzip *gz = (struct imgact_gzip *) vp; 3473784Sphk u_char *p = ptr, *q; 3483784Sphk int i; 3493348Sphk 350108533Sschweikh /* First, find an a.out-header. */ 3513784Sphk if (gz->output < sizeof gz->a_out) { 3523784Sphk q = (u_char *) & gz->a_out; 3533784Sphk i = min(siz, sizeof gz->a_out - gz->output); 3543784Sphk bcopy(p, q + gz->output, i); 3553784Sphk gz->output += i; 3563784Sphk p += i; 3573784Sphk siz -= i; 3583784Sphk if (gz->output == sizeof gz->a_out) { 35948079Shoek gz->gotheader = 1; 3603784Sphk i = do_aout_hdr(gz); 3613784Sphk if (i == -1) { 3623785Sphk if (!gz->where) 3633785Sphk gz->where = __LINE__; 3643784Sphk gz->error = ENOEXEC; 3653784Sphk return ENOEXEC; 3663784Sphk } else if (i) { 3673784Sphk gz->where = __LINE__; 3683784Sphk gz->error = i; 3693784Sphk return ENOEXEC; 3703784Sphk } 37131718Sjdp if (gz->file_offset == 0) { 37237656Sbde q = (u_char *) (uintptr_t) gz->virtual_offset; 37337015Sbde copyout(&gz->a_out, q, sizeof gz->a_out); 3743784Sphk } 3753784Sphk } 3763784Sphk } 3773784Sphk /* Skip over zero-padded first PAGE if needed */ 37837015Sbde if (gz->output < gz->file_offset && 37937015Sbde gz->output + siz > gz->file_offset) { 3803784Sphk i = min(siz, gz->file_offset - gz->output); 3813784Sphk gz->output += i; 3823784Sphk p += i; 3833784Sphk siz -= i; 3843784Sphk } 3853784Sphk if (gz->output >= gz->file_offset && gz->output < gz->file_end) { 3863784Sphk i = min(siz, gz->file_end - gz->output); 38737656Sbde q = (u_char *) (uintptr_t) 38837656Sbde (gz->virtual_offset + gz->output - gz->file_offset); 38937015Sbde copyout(p, q, i); 3903784Sphk gz->output += i; 3913784Sphk p += i; 3923784Sphk siz -= i; 3933784Sphk } 3943784Sphk gz->output += siz; 3953784Sphk return 0; 3963332Sphk} 3973332Sphk 3983784Sphk 3993332Sphk/* 4003332Sphk * Tell kern_execve.c about it, with a little help from the linker. 4013332Sphk */ 40243402Sdillonstatic struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"}; 40340435SpeterEXEC_SET(execgzip, gzip_execsw); 404