imgact_gzip.c revision 153698
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 153698 2005-12-24 04:57:50Z alc $");
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	 */
242103767Sjake	exec_new_vmspace(gz->ip, &aout_sysvec);
2433348Sphk
244153698Salc	vn_lock(gz->ip->vp, LK_EXCLUSIVE | LK_RETRY, td);
245153698Salc
24624848Sdyson	vmspace = gz->ip->proc->p_vmspace;
24724848Sdyson
2483784Sphk	vmaddr = gz->virtual_offset;
2493332Sphk
2503784Sphk	error = vm_mmap(&vmspace->vm_map,
2513784Sphk			&vmaddr,
25217386Sphk			gz->a_out.a_text + gz->a_out.a_data,
25317386Sphk			VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
254144501Sjhb			OBJT_DEFAULT,
255144501Sjhb			NULL,
2563784Sphk			0);
2573332Sphk
2583784Sphk	if (error) {
2593784Sphk		gz->where = __LINE__;
2603784Sphk		return (error);
2613784Sphk	}
26217386Sphk
2636579Sdg	if (gz->bss_size != 0) {
2646579Sdg		/*
26514087Sphk		 * Allocate demand-zeroed area for uninitialized data.
26614087Sphk		 * "bss" = 'block started by symbol' - named after the
26714087Sphk		 * IBM 7090 instruction of the same name.
2686579Sdg		 */
26914087Sphk		vmaddr = gz->virtual_offset + gz->a_out.a_text +
27014087Sphk			gz->a_out.a_data;
27117386Sphk		error = vm_map_find(&vmspace->vm_map,
27217386Sphk				NULL,
27317386Sphk				0,
27417386Sphk				&vmaddr,
27517386Sphk				gz->bss_size,
27617386Sphk				FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
2776579Sdg		if (error) {
2786579Sdg			gz->where = __LINE__;
2796579Sdg			return (error);
2806579Sdg		}
2813784Sphk	}
2823784Sphk	/* Fill in process VM information */
2833784Sphk	vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
2843784Sphk	vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
28537656Sbde	vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
28637656Sbde	vmspace->vm_daddr = (caddr_t) (uintptr_t)
28737656Sbde			    (gz->virtual_offset + gz->a_out.a_text);
2883332Sphk
2893784Sphk	/* Fill in image_params */
2903784Sphk	gz->ip->interpreted = 0;
2913784Sphk	gz->ip->entry_addr = gz->a_out.a_entry;
2923332Sphk
2933784Sphk	gz->ip->proc->p_sysent = &aout_sysvec;
2943332Sphk
2953784Sphk	return 0;
2963784Sphk}
2973332Sphk
2983784Sphkstatic int
2993784SphkNextByte(void *vp)
3003784Sphk{
3013784Sphk	int             error;
3023784Sphk	struct imgact_gzip *igz = (struct imgact_gzip *) vp;
3033332Sphk
3043784Sphk	if (igz->idx >= igz->len) {
3053784Sphk		igz->where = __LINE__;
3063784Sphk		return GZ_EOF;
3073784Sphk	}
3083784Sphk	if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
3093784Sphk		return igz->inbuf[(igz->idx++) - igz->offset];
3103784Sphk	}
3113784Sphk	if (igz->inbuf) {
3126579Sdg		error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
3136579Sdg			    (vm_offset_t) igz->inbuf + PAGE_SIZE);
3143784Sphk		if (error) {
3153784Sphk			igz->where = __LINE__;
3163784Sphk			igz->error = error;
3173784Sphk			return GZ_EOF;
3183784Sphk		}
3193784Sphk	}
3206342Sphk	igz->offset = igz->idx & ~PAGE_MASK;
3213332Sphk
3223784Sphk	error = vm_mmap(kernel_map,	/* map */
3233784Sphk			(vm_offset_t *) & igz->inbuf,	/* address */
3243784Sphk			PAGE_SIZE,	/* size */
3253784Sphk			VM_PROT_READ,	/* protection */
3263784Sphk			VM_PROT_READ,	/* max protection */
3273784Sphk			0,	/* flags */
328144501Sjhb			OBJT_VNODE,	/* handle type */
329144501Sjhb			igz->ip->vp,	/* vnode */
3303784Sphk			igz->offset);	/* offset */
3313784Sphk	if (error) {
3323784Sphk		igz->where = __LINE__;
3333784Sphk		igz->error = error;
3343784Sphk		return GZ_EOF;
3353784Sphk	}
3363784Sphk	return igz->inbuf[(igz->idx++) - igz->offset];
3373784Sphk}
3383332Sphk
3393784Sphkstatic int
3403784SphkFlush(void *vp, u_char * ptr, u_long siz)
3413784Sphk{
3423784Sphk	struct imgact_gzip *gz = (struct imgact_gzip *) vp;
3433784Sphk	u_char         *p = ptr, *q;
3443784Sphk	int             i;
3453348Sphk
346108533Sschweikh	/* First, find an a.out-header. */
3473784Sphk	if (gz->output < sizeof gz->a_out) {
3483784Sphk		q = (u_char *) & gz->a_out;
3493784Sphk		i = min(siz, sizeof gz->a_out - gz->output);
3503784Sphk		bcopy(p, q + gz->output, i);
3513784Sphk		gz->output += i;
3523784Sphk		p += i;
3533784Sphk		siz -= i;
3543784Sphk		if (gz->output == sizeof gz->a_out) {
35548079Shoek			gz->gotheader = 1;
3563784Sphk			i = do_aout_hdr(gz);
3573784Sphk			if (i == -1) {
3583785Sphk				if (!gz->where)
3593785Sphk					gz->where = __LINE__;
3603784Sphk				gz->error = ENOEXEC;
3613784Sphk				return ENOEXEC;
3623784Sphk			} else if (i) {
3633784Sphk				gz->where = __LINE__;
3643784Sphk				gz->error = i;
3653784Sphk				return ENOEXEC;
3663784Sphk			}
36731718Sjdp			if (gz->file_offset == 0) {
36837656Sbde				q = (u_char *) (uintptr_t) gz->virtual_offset;
36937015Sbde				copyout(&gz->a_out, q, sizeof gz->a_out);
3703784Sphk			}
3713784Sphk		}
3723784Sphk	}
3733784Sphk	/* Skip over zero-padded first PAGE if needed */
37437015Sbde	if (gz->output < gz->file_offset &&
37537015Sbde	    gz->output + siz > gz->file_offset) {
3763784Sphk		i = min(siz, gz->file_offset - gz->output);
3773784Sphk		gz->output += i;
3783784Sphk		p += i;
3793784Sphk		siz -= i;
3803784Sphk	}
3813784Sphk	if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
3823784Sphk		i = min(siz, gz->file_end - gz->output);
38337656Sbde		q = (u_char *) (uintptr_t)
38437656Sbde		    (gz->virtual_offset + gz->output - gz->file_offset);
38537015Sbde		copyout(p, q, i);
3863784Sphk		gz->output += i;
3873784Sphk		p += i;
3883784Sphk		siz -= i;
3893784Sphk	}
3903784Sphk	gz->output += siz;
3913784Sphk	return 0;
3923332Sphk}
3933332Sphk
3943784Sphk
3953332Sphk/*
3963332Sphk * Tell kern_execve.c about it, with a little help from the linker.
3973332Sphk */
39843402Sdillonstatic struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
39940435SpeterEXEC_SET(execgzip, gzip_execsw);
400