imgact_gzip.c revision 237694
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 237694 2012-06-28 07:33:43Z imp $");
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>
36220373Strasz#include <sys/racct.h>
373507Scsgr#include <sys/resourcevar.h>
383332Sphk#include <sys/sysent.h>
393507Scsgr#include <sys/systm.h>
4015494Sbde#include <sys/vnode.h>
413784Sphk#include <sys/inflate.h>
423332Sphk
433332Sphk#include <vm/vm.h>
4412662Sdg#include <vm/vm_param.h>
4512662Sdg#include <vm/pmap.h>
4612662Sdg#include <vm/vm_map.h>
473332Sphk#include <vm/vm_kern.h>
4812662Sdg#include <vm/vm_extern.h>
493332Sphk
503784Sphkstruct imgact_gzip {
513784Sphk	struct image_params *ip;
523784Sphk	struct exec     a_out;
533784Sphk	int             error;
5448079Shoek	int		gotheader;
553784Sphk	int             where;
563784Sphk	u_char         *inbuf;
573784Sphk	u_long          offset;
583784Sphk	u_long          output;
593784Sphk	u_long          len;
603784Sphk	int             idx;
613784Sphk	u_long          virtual_offset, file_offset, file_end, bss_size;
623784Sphk};
633784Sphk
6492723Salfredstatic int exec_gzip_imgact(struct image_params *imgp);
6592723Salfredstatic int NextByte(void *vp);
6692723Salfredstatic int do_aout_hdr(struct imgact_gzip *);
6792723Salfredstatic int Flush(void *vp, u_char *, u_long siz);
683784Sphk
6912568Sbdestatic int
7012130Sdgexec_gzip_imgact(imgp)
7112130Sdg	struct image_params *imgp;
723332Sphk{
73231885Skib	int             error;
7417974Sbde	const u_char   *p = (const u_char *) imgp->image_header;
753784Sphk	struct imgact_gzip igz;
763784Sphk	struct inflate  infl;
7717386Sphk	struct vmspace *vmspace;
783332Sphk
793348Sphk	/* If these four are not OK, it isn't a gzip file */
803784Sphk	if (p[0] != 0x1f)
813784Sphk		return -1;	/* 0    Simply magic	 */
823784Sphk	if (p[1] != 0x8b)
833784Sphk		return -1;	/* 1    Simply magic	 */
843784Sphk	if (p[2] != 0x08)
853784Sphk		return -1;	/* 2    Compression method	 */
863784Sphk	if (p[9] != 0x03)
873784Sphk		return -1;	/* 9    OS compressed on	 */
883332Sphk
893784Sphk	/*
903784Sphk	 * If this one contains anything but a comment or a filename marker,
913784Sphk	 * we don't want to chew on it
923348Sphk	 */
933784Sphk	if (p[3] & ~(0x18))
943784Sphk		return ENOEXEC;	/* 3    Flags		 */
953332Sphk
963348Sphk	/* These are of no use to us */
973784Sphk	/* 4-7  Timestamp		 */
983784Sphk	/* 8    Extra flags		 */
993348Sphk
1003784Sphk	bzero(&igz, sizeof igz);
1013784Sphk	bzero(&infl, sizeof infl);
1023784Sphk	infl.gz_private = (void *) &igz;
1033784Sphk	infl.gz_input = NextByte;
1043784Sphk	infl.gz_output = Flush;
1053332Sphk
10612130Sdg	igz.ip = imgp;
1073784Sphk	igz.idx = 10;
1083353Sphk
1093784Sphk	if (p[3] & 0x08) {	/* skip a filename */
1103784Sphk		while (p[igz.idx++])
1113784Sphk			if (igz.idx >= PAGE_SIZE)
1123784Sphk				return ENOEXEC;
1133348Sphk	}
1143784Sphk	if (p[3] & 0x10) {	/* skip a comment */
1153784Sphk		while (p[igz.idx++])
1163784Sphk			if (igz.idx >= PAGE_SIZE)
1173784Sphk				return ENOEXEC;
1183348Sphk	}
11917386Sphk	igz.len = imgp->attr->va_size;
1203332Sphk
1213784Sphk	error = inflate(&infl);
1223348Sphk
12348079Shoek	/*
12448079Shoek	 * The unzipped file may not even have been long enough to contain
12548079Shoek	 * a header giving Flush() a chance to return error.  Check for this.
12648079Shoek	 */
12748079Shoek	if ( !igz.gotheader )
12848079Shoek		return ENOEXEC;
12948079Shoek
13017386Sphk	if ( !error ) {
13117386Sphk		vmspace = imgp->proc->p_vmspace;
13217386Sphk		error = vm_map_protect(&vmspace->vm_map,
13317386Sphk			(vm_offset_t) vmspace->vm_taddr,
13417386Sphk			(vm_offset_t) (vmspace->vm_taddr +
13517386Sphk				      (vmspace->vm_tsize << PAGE_SHIFT)) ,
13617386Sphk			VM_PROT_READ|VM_PROT_EXECUTE,0);
13717386Sphk	}
13817386Sphk
139231885Skib	if (igz.inbuf)
140231885Skib		kmem_free_wakeup(exec_map, (vm_offset_t)igz.inbuf, PAGE_SIZE);
141231885Skib	if (igz.error || error) {
1423784Sphk		printf("Output=%lu ", igz.output);
143231885Skib		printf("Inflate_error=%d igz.error=%d where=%d\n",
144231885Skib		       error, igz.error, igz.where);
1453348Sphk	}
1463784Sphk	if (igz.error)
1473784Sphk		return igz.error;
1483784Sphk	if (error)
1493784Sphk		return ENOEXEC;
1503784Sphk	return 0;
1513332Sphk}
1523332Sphk
1533784Sphkstatic int
1543784Sphkdo_aout_hdr(struct imgact_gzip * gz)
1553332Sphk{
1563784Sphk	int             error;
15724848Sdyson	struct vmspace *vmspace;
15814703Sbde	vm_offset_t     vmaddr;
1593332Sphk
1603784Sphk	/*
1613784Sphk	 * Set file/virtual offset based on a.out variant. We do two cases:
1623784Sphk	 * host byte order and network byte order (for NetBSD compatibility)
1633784Sphk	 */
164237694Simp	switch ((int) (gz->a_out.a_midmag & 0xffff)) {
1653332Sphk	case ZMAGIC:
1663784Sphk		gz->virtual_offset = 0;
1673784Sphk		if (gz->a_out.a_text) {
16815538Sphk			gz->file_offset = PAGE_SIZE;
1693784Sphk		} else {
1703784Sphk			/* Bill's "screwball mode" */
1713784Sphk			gz->file_offset = 0;
1723784Sphk		}
1733784Sphk		break;
1743332Sphk	case QMAGIC:
17515538Sphk		gz->virtual_offset = PAGE_SIZE;
1763784Sphk		gz->file_offset = 0;
1773784Sphk		break;
1783332Sphk	default:
1793784Sphk		/* NetBSD compatibility */
180237694Simp		switch ((int) (ntohl(gz->a_out.a_midmag) & 0xffff)) {
1813784Sphk		case ZMAGIC:
1823784Sphk		case QMAGIC:
18315538Sphk			gz->virtual_offset = PAGE_SIZE;
1843784Sphk			gz->file_offset = 0;
1853784Sphk			break;
1863784Sphk		default:
1873784Sphk			gz->where = __LINE__;
1883784Sphk			return (-1);
1893784Sphk		}
1903332Sphk	}
1913332Sphk
19215538Sphk	gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
1933332Sphk
1943784Sphk	/*
1953784Sphk	 * Check various fields in header for validity/bounds.
1963784Sphk	 */
1973784Sphk	if (			/* entry point must lay with text region */
1983784Sphk	    gz->a_out.a_entry < gz->virtual_offset ||
1993784Sphk	    gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
2003332Sphk
2013332Sphk	/* text and data size must each be page rounded */
20215538Sphk	    gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
2033784Sphk		gz->where = __LINE__;
2043784Sphk		return (-1);
2053784Sphk	}
2063784Sphk	/*
2073784Sphk	 * text/data/bss must not exceed limits
2083784Sphk	 */
209125454Sjhb	PROC_LOCK(gz->ip->proc);
2103784Sphk	if (			/* text can't exceed maximum text size */
21184783Sps	    gz->a_out.a_text > maxtsiz ||
2123332Sphk
2133332Sphk	/* data + bss can't exceed rlimit */
2143784Sphk	    gz->a_out.a_data + gz->bss_size >
215220373Strasz	    lim_cur(gz->ip->proc, RLIMIT_DATA) ||
216220373Strasz	    racct_set(gz->ip->proc, RACCT_DATA,
217220373Strasz	    gz->a_out.a_data + gz->bss_size) != 0) {
218125454Sjhb		PROC_UNLOCK(gz->ip->proc);
2193784Sphk		gz->where = __LINE__;
2203784Sphk		return (ENOMEM);
2213784Sphk	}
222125454Sjhb	PROC_UNLOCK(gz->ip->proc);
2233784Sphk	/* Find out how far we should go */
2243784Sphk	gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
2253332Sphk
2263784Sphk	/*
227153698Salc	 * Avoid a possible deadlock if the current address space is destroyed
228153698Salc	 * and that address space maps the locked vnode.  In the common case,
229153698Salc	 * the locked vnode's v_usecount is decremented but remains greater
230153698Salc	 * than zero.  Consequently, the vnode lock is not needed by vrele().
231153698Salc	 * However, in cases where the vnode lock is external, such as nullfs,
232153698Salc	 * v_usecount may become zero.
233153698Salc	 */
234175294Sattilio	VOP_UNLOCK(gz->ip->vp, 0);
235153698Salc
236153698Salc	/*
2373784Sphk	 * Destroy old process VM and create a new one (with a new stack)
2383784Sphk	 */
239173361Skib	error = exec_new_vmspace(gz->ip, &aout_sysvec);
2403348Sphk
241175202Sattilio	vn_lock(gz->ip->vp, LK_EXCLUSIVE | LK_RETRY);
242173361Skib	if (error) {
243173361Skib		gz->where = __LINE__;
244173361Skib		return (error);
245173361Skib	}
246153698Salc
24724848Sdyson	vmspace = gz->ip->proc->p_vmspace;
24824848Sdyson
2493784Sphk	vmaddr = gz->virtual_offset;
2503332Sphk
2513784Sphk	error = vm_mmap(&vmspace->vm_map,
2523784Sphk			&vmaddr,
25317386Sphk			gz->a_out.a_text + gz->a_out.a_data,
25417386Sphk			VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
255144501Sjhb			OBJT_DEFAULT,
256144501Sjhb			NULL,
2573784Sphk			0);
2583332Sphk
2593784Sphk	if (error) {
2603784Sphk		gz->where = __LINE__;
2613784Sphk		return (error);
2623784Sphk	}
26317386Sphk
2646579Sdg	if (gz->bss_size != 0) {
2656579Sdg		/*
26614087Sphk		 * Allocate demand-zeroed area for uninitialized data.
26714087Sphk		 * "bss" = 'block started by symbol' - named after the
26814087Sphk		 * IBM 7090 instruction of the same name.
2696579Sdg		 */
27014087Sphk		vmaddr = gz->virtual_offset + gz->a_out.a_text +
27114087Sphk			gz->a_out.a_data;
27217386Sphk		error = vm_map_find(&vmspace->vm_map,
27317386Sphk				NULL,
27417386Sphk				0,
27517386Sphk				&vmaddr,
27617386Sphk				gz->bss_size,
27717386Sphk				FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
2786579Sdg		if (error) {
2796579Sdg			gz->where = __LINE__;
2806579Sdg			return (error);
2816579Sdg		}
2823784Sphk	}
2833784Sphk	/* Fill in process VM information */
2843784Sphk	vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
2853784Sphk	vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
28637656Sbde	vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
28737656Sbde	vmspace->vm_daddr = (caddr_t) (uintptr_t)
28837656Sbde			    (gz->virtual_offset + gz->a_out.a_text);
2893332Sphk
2903784Sphk	/* Fill in image_params */
2913784Sphk	gz->ip->interpreted = 0;
2923784Sphk	gz->ip->entry_addr = gz->a_out.a_entry;
2933332Sphk
2943784Sphk	gz->ip->proc->p_sysent = &aout_sysvec;
2953332Sphk
2963784Sphk	return 0;
2973784Sphk}
2983332Sphk
2993784Sphkstatic int
3003784SphkNextByte(void *vp)
3013784Sphk{
3023784Sphk	int             error;
3033784Sphk	struct imgact_gzip *igz = (struct imgact_gzip *) vp;
3043332Sphk
3053784Sphk	if (igz->idx >= igz->len) {
3063784Sphk		igz->where = __LINE__;
3073784Sphk		return GZ_EOF;
3083784Sphk	}
3093784Sphk	if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
3103784Sphk		return igz->inbuf[(igz->idx++) - igz->offset];
3113784Sphk	}
312231885Skib	if (igz->inbuf)
313231885Skib		kmem_free_wakeup(exec_map, (vm_offset_t)igz->inbuf, PAGE_SIZE);
3146342Sphk	igz->offset = igz->idx & ~PAGE_MASK;
3153332Sphk
316231885Skib	error = vm_mmap(exec_map,	/* map */
3173784Sphk			(vm_offset_t *) & igz->inbuf,	/* address */
3183784Sphk			PAGE_SIZE,	/* size */
3193784Sphk			VM_PROT_READ,	/* protection */
3203784Sphk			VM_PROT_READ,	/* max protection */
3213784Sphk			0,	/* flags */
322144501Sjhb			OBJT_VNODE,	/* handle type */
323144501Sjhb			igz->ip->vp,	/* vnode */
3243784Sphk			igz->offset);	/* offset */
3253784Sphk	if (error) {
3263784Sphk		igz->where = __LINE__;
3273784Sphk		igz->error = error;
3283784Sphk		return GZ_EOF;
3293784Sphk	}
3303784Sphk	return igz->inbuf[(igz->idx++) - igz->offset];
3313784Sphk}
3323332Sphk
3333784Sphkstatic int
3343784SphkFlush(void *vp, u_char * ptr, u_long siz)
3353784Sphk{
3363784Sphk	struct imgact_gzip *gz = (struct imgact_gzip *) vp;
3373784Sphk	u_char         *p = ptr, *q;
3383784Sphk	int             i;
3393348Sphk
340108533Sschweikh	/* First, find an a.out-header. */
3413784Sphk	if (gz->output < sizeof gz->a_out) {
3423784Sphk		q = (u_char *) & gz->a_out;
3433784Sphk		i = min(siz, sizeof gz->a_out - gz->output);
3443784Sphk		bcopy(p, q + gz->output, i);
3453784Sphk		gz->output += i;
3463784Sphk		p += i;
3473784Sphk		siz -= i;
3483784Sphk		if (gz->output == sizeof gz->a_out) {
34948079Shoek			gz->gotheader = 1;
3503784Sphk			i = do_aout_hdr(gz);
3513784Sphk			if (i == -1) {
3523785Sphk				if (!gz->where)
3533785Sphk					gz->where = __LINE__;
3543784Sphk				gz->error = ENOEXEC;
3553784Sphk				return ENOEXEC;
3563784Sphk			} else if (i) {
3573784Sphk				gz->where = __LINE__;
3583784Sphk				gz->error = i;
3593784Sphk				return ENOEXEC;
3603784Sphk			}
36131718Sjdp			if (gz->file_offset == 0) {
36237656Sbde				q = (u_char *) (uintptr_t) gz->virtual_offset;
36337015Sbde				copyout(&gz->a_out, q, sizeof gz->a_out);
3643784Sphk			}
3653784Sphk		}
3663784Sphk	}
3673784Sphk	/* Skip over zero-padded first PAGE if needed */
36837015Sbde	if (gz->output < gz->file_offset &&
36937015Sbde	    gz->output + siz > gz->file_offset) {
3703784Sphk		i = min(siz, gz->file_offset - gz->output);
3713784Sphk		gz->output += i;
3723784Sphk		p += i;
3733784Sphk		siz -= i;
3743784Sphk	}
3753784Sphk	if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
3763784Sphk		i = min(siz, gz->file_end - gz->output);
37737656Sbde		q = (u_char *) (uintptr_t)
37837656Sbde		    (gz->virtual_offset + gz->output - gz->file_offset);
37937015Sbde		copyout(p, q, i);
3803784Sphk		gz->output += i;
3813784Sphk		p += i;
3823784Sphk		siz -= i;
3833784Sphk	}
3843784Sphk	gz->output += siz;
3853784Sphk	return 0;
3863332Sphk}
3873332Sphk
3883784Sphk
3893332Sphk/*
3903332Sphk * Tell kern_execve.c about it, with a little help from the linker.
3913332Sphk */
39243402Sdillonstatic struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
39340435SpeterEXEC_SET(execgzip, gzip_execsw);
394