imgact_gzip.c revision 139804
13332Sphk/*-
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 * ----------------------------------------------------------------------------
83332Sphk */
93332Sphk
103332Sphk/*
113784Sphk * This module handles execution of a.out files which have been run through
123332Sphk * "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...
203417Scsgr *	so is the rest...
213332Sphk *	tidy up unnecesary includes
223332Sphk */
23116182Sobrien
24116182Sobrien#include <sys/cdefs.h>
25116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/imgact_gzip.c 139804 2005-01-06 23:35:40Z imp $");
263784Sphk
273332Sphk#include <sys/param.h>
283332Sphk#include <sys/exec.h>
293332Sphk#include <sys/imgact.h>
303332Sphk#include <sys/imgact_aout.h>
3176166Smarkm#include <sys/kernel.h>
323507Scsgr#include <sys/lock.h>
3376166Smarkm#include <sys/mman.h>
3415494Sbde#include <sys/mutex.h>
353507Scsgr#include <sys/proc.h>
363332Sphk#include <sys/resourcevar.h>
373507Scsgr#include <sys/sysent.h>
3815494Sbde#include <sys/systm.h>
393784Sphk#include <sys/vnode.h>
403332Sphk#include <sys/inflate.h>
413332Sphk
4212662Sdg#include <vm/vm.h>
4312662Sdg#include <vm/vm_param.h>
4412662Sdg#include <vm/pmap.h>
453332Sphk#include <vm/vm_map.h>
4612662Sdg#include <vm/vm_kern.h>
473332Sphk#include <vm/vm_extern.h>
483784Sphk
493784Sphkstruct imgact_gzip {
503784Sphk	struct image_params *ip;
513784Sphk	struct exec     a_out;
5248079Shoek	int             error;
533784Sphk	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};
6292723Salfred
6392723Salfredstatic int exec_gzip_imgact(struct image_params *imgp);
6492723Salfredstatic int NextByte(void *vp);
6592723Salfredstatic int do_aout_hdr(struct imgact_gzip *);
663784Sphkstatic int Flush(void *vp, u_char *, u_long siz);
6712568Sbde
6812130Sdgstatic int
6912130Sdgexec_gzip_imgact(imgp)
703332Sphk	struct image_params *imgp;
713784Sphk{
7217974Sbde	int             error, error2 = 0;
733784Sphk	const u_char   *p = (const u_char *) imgp->image_header;
743784Sphk	struct imgact_gzip igz;
7517386Sphk	struct inflate  infl;
763332Sphk	struct vmspace *vmspace;
773348Sphk
783784Sphk	/* 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)
863332Sphk		return -1;	/* 9    OS compressed on	 */
873784Sphk
883784Sphk	/*
893784Sphk	 * If this one contains anything but a comment or a filename marker,
903348Sphk	 * we don't want to chew on it
913784Sphk	 */
923784Sphk	if (p[3] & ~(0x18))
933332Sphk		return ENOEXEC;	/* 3    Flags		 */
943348Sphk
953784Sphk	/* These are of no use to us */
963784Sphk	/* 4-7  Timestamp		 */
973348Sphk	/* 8    Extra flags		 */
983784Sphk
993784Sphk	bzero(&igz, sizeof igz);
1003784Sphk	bzero(&infl, sizeof infl);
1013784Sphk	infl.gz_private = (void *) &igz;
1023784Sphk	infl.gz_input = NextByte;
1033332Sphk	infl.gz_output = Flush;
10412130Sdg
1053784Sphk	igz.ip = imgp;
1063353Sphk	igz.idx = 10;
1073784Sphk
1083784Sphk	if (p[3] & 0x08) {	/* skip a filename */
1093784Sphk		while (p[igz.idx++])
1103784Sphk			if (igz.idx >= PAGE_SIZE)
1113348Sphk				return ENOEXEC;
1123784Sphk	}
1133784Sphk	if (p[3] & 0x10) {	/* skip a comment */
1143784Sphk		while (p[igz.idx++])
1153784Sphk			if (igz.idx >= PAGE_SIZE)
1163348Sphk				return ENOEXEC;
11717386Sphk	}
1183332Sphk	igz.len = imgp->attr->va_size;
1193784Sphk
1203348Sphk	error = inflate(&infl);
12148079Shoek
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;
12817386Sphk
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	}
1373784Sphk
1383784Sphk	if (igz.inbuf) {
1396579Sdg		error2 =
1406579Sdg			vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
1413332Sphk			    (vm_offset_t) igz.inbuf + PAGE_SIZE);
1423784Sphk	}
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",
1463348Sphk		       error, igz.error, error2, igz.where);
1473784Sphk	}
1483784Sphk	if (igz.error)
1493784Sphk		return igz.error;
1503784Sphk	if (error)
1518876Srgrimes		return ENOEXEC;
1523784Sphk	if (error2)
1533784Sphk		return error2;
1543332Sphk	return 0;
1553332Sphk}
1563784Sphk
1573784Sphkstatic int
1583332Sphkdo_aout_hdr(struct imgact_gzip * gz)
1593784Sphk{
16024848Sdyson	int             error;
16114703Sbde	struct vmspace *vmspace;
1623332Sphk	vm_offset_t     vmaddr;
1633784Sphk
1643784Sphk	/*
1653784Sphk	 * Set file/virtual offset based on a.out variant. We do two cases:
1663784Sphk	 * host byte order and network byte order (for NetBSD compatibility)
1673784Sphk	 */
1683332Sphk	switch ((int) (gz->a_out.a_magic & 0xffff)) {
1693784Sphk	case ZMAGIC:
1703784Sphk		gz->virtual_offset = 0;
17115538Sphk		if (gz->a_out.a_text) {
1723784Sphk			gz->file_offset = PAGE_SIZE;
1733784Sphk		} else {
1743784Sphk			/* Bill's "screwball mode" */
1753784Sphk			gz->file_offset = 0;
1763784Sphk		}
1773332Sphk		break;
17815538Sphk	case QMAGIC:
1793784Sphk		gz->virtual_offset = PAGE_SIZE;
1803784Sphk		gz->file_offset = 0;
1813332Sphk		break;
1823784Sphk	default:
1833784Sphk		/* NetBSD compatibility */
1843784Sphk		switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
1853784Sphk		case ZMAGIC:
18615538Sphk		case QMAGIC:
1873784Sphk			gz->virtual_offset = PAGE_SIZE;
1883784Sphk			gz->file_offset = 0;
1893784Sphk			break;
1903784Sphk		default:
1913784Sphk			gz->where = __LINE__;
1923784Sphk			return (-1);
1933332Sphk		}
1943332Sphk	}
19515538Sphk
1963332Sphk	gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
1973784Sphk
1983784Sphk	/*
1993784Sphk	 * Check various fields in header for validity/bounds.
2003784Sphk	 */
2013784Sphk	if (			/* entry point must lay with text region */
2023784Sphk	    gz->a_out.a_entry < gz->virtual_offset ||
2033332Sphk	    gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
2043332Sphk
20515538Sphk	/* text and data size must each be page rounded */
2063784Sphk	    gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
2073784Sphk		gz->where = __LINE__;
2083784Sphk		return (-1);
2093784Sphk	}
2103784Sphk	/*
2113784Sphk	 * text/data/bss must not exceed limits
212125454Sjhb	 */
2133784Sphk	PROC_LOCK(gz->ip->proc);
21484783Sps	if (			/* text can't exceed maximum text size */
2153332Sphk	    gz->a_out.a_text > maxtsiz ||
2163332Sphk
2173784Sphk	/* data + bss can't exceed rlimit */
218125454Sjhb	    gz->a_out.a_data + gz->bss_size >
219125454Sjhb	    lim_cur(gz->ip->proc, RLIMIT_DATA)) {
2203784Sphk		PROC_UNLOCK(gz->ip->proc);
2213784Sphk		gz->where = __LINE__;
2223784Sphk		return (ENOMEM);
223125454Sjhb	}
2243784Sphk	PROC_UNLOCK(gz->ip->proc);
2253784Sphk	/* Find out how far we should go */
2263332Sphk	gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
2273784Sphk
2283784Sphk	/* copy in arguments and/or environment from old process */
2293784Sphk	error = exec_extract_strings(gz->ip);
2303784Sphk	if (error) {
2313784Sphk		gz->where = __LINE__;
2323784Sphk		return (error);
2333784Sphk	}
2343784Sphk	/*
2353784Sphk	 * Destroy old process VM and create a new one (with a new stack)
236103767Sjake	 */
2373348Sphk	exec_new_vmspace(gz->ip, &aout_sysvec);
23824848Sdyson
23924848Sdyson	vmspace = gz->ip->proc->p_vmspace;
2403784Sphk
2413332Sphk	vmaddr = gz->virtual_offset;
2423784Sphk
2433784Sphk	error = vm_mmap(&vmspace->vm_map,
24417386Sphk			&vmaddr,
24517386Sphk			gz->a_out.a_text + gz->a_out.a_data,
2463784Sphk			VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
2473784Sphk			0,
2483332Sphk			0);
2493784Sphk
2503784Sphk	if (error) {
2513784Sphk		gz->where = __LINE__;
2523784Sphk		return (error);
25317386Sphk	}
2546579Sdg
2556579Sdg	if (gz->bss_size != 0) {
25614087Sphk		/*
25714087Sphk		 * Allocate demand-zeroed area for uninitialized data.
25814087Sphk		 * "bss" = 'block started by symbol' - named after the
2596579Sdg		 * IBM 7090 instruction of the same name.
26014087Sphk		 */
26114087Sphk		vmaddr = gz->virtual_offset + gz->a_out.a_text +
26217386Sphk			gz->a_out.a_data;
26317386Sphk		error = vm_map_find(&vmspace->vm_map,
26417386Sphk				NULL,
26517386Sphk				0,
26617386Sphk				&vmaddr,
26717386Sphk				gz->bss_size,
2686579Sdg				FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
2696579Sdg		if (error) {
2706579Sdg			gz->where = __LINE__;
2716579Sdg			return (error);
2723784Sphk		}
2733784Sphk	}
2743784Sphk	/* Fill in process VM information */
2753784Sphk	vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
27637656Sbde	vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
27737656Sbde	vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
27837656Sbde	vmspace->vm_daddr = (caddr_t) (uintptr_t)
2793332Sphk			    (gz->virtual_offset + gz->a_out.a_text);
2803784Sphk
2813784Sphk	/* Fill in image_params */
2823784Sphk	gz->ip->interpreted = 0;
2833332Sphk	gz->ip->entry_addr = gz->a_out.a_entry;
2843784Sphk
2853332Sphk	gz->ip->proc->p_sysent = &aout_sysvec;
2863784Sphk
2873784Sphk	return 0;
2883332Sphk}
2893784Sphk
2903784Sphkstatic int
2913784SphkNextByte(void *vp)
2923784Sphk{
2933784Sphk	int             error;
2943332Sphk	struct imgact_gzip *igz = (struct imgact_gzip *) vp;
2953784Sphk
2963784Sphk	if (igz->idx >= igz->len) {
2973784Sphk		igz->where = __LINE__;
2983784Sphk		return GZ_EOF;
2993784Sphk	}
3003784Sphk	if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
3013784Sphk		return igz->inbuf[(igz->idx++) - igz->offset];
3023784Sphk	}
3036579Sdg	if (igz->inbuf) {
3046579Sdg		error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
3053784Sphk			    (vm_offset_t) igz->inbuf + PAGE_SIZE);
3063784Sphk		if (error) {
3073784Sphk			igz->where = __LINE__;
3083784Sphk			igz->error = error;
3093784Sphk			return GZ_EOF;
3103784Sphk		}
3116342Sphk	}
3123332Sphk	igz->offset = igz->idx & ~PAGE_MASK;
3133784Sphk
3143784Sphk	error = vm_mmap(kernel_map,	/* map */
3153784Sphk			(vm_offset_t *) & igz->inbuf,	/* address */
3163784Sphk			PAGE_SIZE,	/* size */
3173784Sphk			VM_PROT_READ,	/* protection */
3183784Sphk			VM_PROT_READ,	/* max protection */
31912130Sdg			0,	/* flags */
3203784Sphk			(caddr_t) igz->ip->vp,	/* vnode */
3213784Sphk			igz->offset);	/* offset */
3223784Sphk	if (error) {
3233784Sphk		igz->where = __LINE__;
3243784Sphk		igz->error = error;
3253784Sphk		return GZ_EOF;
3263784Sphk	}
3273784Sphk	return igz->inbuf[(igz->idx++) - igz->offset];
3283332Sphk}
3293784Sphk
3303784Sphkstatic int
3313784SphkFlush(void *vp, u_char * ptr, u_long siz)
3323784Sphk{
3333784Sphk	struct imgact_gzip *gz = (struct imgact_gzip *) vp;
3343784Sphk	u_char         *p = ptr, *q;
3353348Sphk	int             i;
336108533Sschweikh
3373784Sphk	/* First, find an a.out-header. */
3383784Sphk	if (gz->output < sizeof gz->a_out) {
3393784Sphk		q = (u_char *) & gz->a_out;
3403784Sphk		i = min(siz, sizeof gz->a_out - gz->output);
3413784Sphk		bcopy(p, q + gz->output, i);
3423784Sphk		gz->output += i;
3433784Sphk		p += i;
3443784Sphk		siz -= i;
34548079Shoek		if (gz->output == sizeof gz->a_out) {
3463784Sphk			gz->gotheader = 1;
3473784Sphk			i = do_aout_hdr(gz);
3483785Sphk			if (i == -1) {
3493785Sphk				if (!gz->where)
3503784Sphk					gz->where = __LINE__;
3513784Sphk				gz->error = ENOEXEC;
3523784Sphk				return ENOEXEC;
3533784Sphk			} else if (i) {
3543784Sphk				gz->where = __LINE__;
3553784Sphk				gz->error = i;
3563784Sphk				return ENOEXEC;
35731718Sjdp			}
35837656Sbde			if (gz->file_offset == 0) {
35937015Sbde				q = (u_char *) (uintptr_t) gz->virtual_offset;
3603784Sphk				copyout(&gz->a_out, q, sizeof gz->a_out);
3613784Sphk			}
3623784Sphk		}
3633784Sphk	}
36437015Sbde	/* Skip over zero-padded first PAGE if needed */
36537015Sbde	if (gz->output < gz->file_offset &&
3663784Sphk	    gz->output + siz > gz->file_offset) {
3673784Sphk		i = min(siz, gz->file_offset - gz->output);
3683784Sphk		gz->output += i;
3693784Sphk		p += i;
3703784Sphk		siz -= i;
3713784Sphk	}
3723784Sphk	if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
37337656Sbde		i = min(siz, gz->file_end - gz->output);
37437656Sbde		q = (u_char *) (uintptr_t)
37537015Sbde		    (gz->virtual_offset + gz->output - gz->file_offset);
3763784Sphk		copyout(p, q, i);
3773784Sphk		gz->output += i;
3783784Sphk		p += i;
3793784Sphk		siz -= i;
3803784Sphk	}
3813784Sphk	gz->output += siz;
3823332Sphk	return 0;
3833332Sphk}
3843784Sphk
3853332Sphk
3863332Sphk/*
3873332Sphk * Tell kern_execve.c about it, with a little help from the linker.
38843402Sdillon */
38940435Speterstatic struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
390EXEC_SET(execgzip, gzip_execsw);
391