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...
21249583Sgabor *	tidy up unnecessary includes
223332Sphk */
233332Sphk
24116182Sobrien#include <sys/cdefs.h>
25116182Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/kern/imgact_gzip.c 255426 2013-09-09 18:11:59Z jhb $");
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)
140254025Sjeff		kmap_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;
272255426Sjhb		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
273255426Sjhb		    gz->bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL,
274255426Sjhb		    0);
2756579Sdg		if (error) {
2766579Sdg			gz->where = __LINE__;
2776579Sdg			return (error);
2786579Sdg		}
2793784Sphk	}
2803784Sphk	/* Fill in process VM information */
2813784Sphk	vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
2823784Sphk	vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
28337656Sbde	vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
28437656Sbde	vmspace->vm_daddr = (caddr_t) (uintptr_t)
28537656Sbde			    (gz->virtual_offset + gz->a_out.a_text);
2863332Sphk
2873784Sphk	/* Fill in image_params */
2883784Sphk	gz->ip->interpreted = 0;
2893784Sphk	gz->ip->entry_addr = gz->a_out.a_entry;
2903332Sphk
2913784Sphk	gz->ip->proc->p_sysent = &aout_sysvec;
2923332Sphk
2933784Sphk	return 0;
2943784Sphk}
2953332Sphk
2963784Sphkstatic int
2973784SphkNextByte(void *vp)
2983784Sphk{
2993784Sphk	int             error;
3003784Sphk	struct imgact_gzip *igz = (struct imgact_gzip *) vp;
3013332Sphk
3023784Sphk	if (igz->idx >= igz->len) {
3033784Sphk		igz->where = __LINE__;
3043784Sphk		return GZ_EOF;
3053784Sphk	}
3063784Sphk	if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
3073784Sphk		return igz->inbuf[(igz->idx++) - igz->offset];
3083784Sphk	}
309231885Skib	if (igz->inbuf)
310254025Sjeff		kmap_free_wakeup(exec_map, (vm_offset_t)igz->inbuf, PAGE_SIZE);
3116342Sphk	igz->offset = igz->idx & ~PAGE_MASK;
3123332Sphk
313231885Skib	error = vm_mmap(exec_map,	/* map */
3143784Sphk			(vm_offset_t *) & igz->inbuf,	/* address */
3153784Sphk			PAGE_SIZE,	/* size */
3163784Sphk			VM_PROT_READ,	/* protection */
3173784Sphk			VM_PROT_READ,	/* max protection */
3183784Sphk			0,	/* flags */
319144501Sjhb			OBJT_VNODE,	/* handle type */
320144501Sjhb			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];
3283784Sphk}
3293332Sphk
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;
3353784Sphk	int             i;
3363348Sphk
337108533Sschweikh	/* 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;
3453784Sphk		if (gz->output == sizeof gz->a_out) {
34648079Shoek			gz->gotheader = 1;
3473784Sphk			i = do_aout_hdr(gz);
3483784Sphk			if (i == -1) {
3493785Sphk				if (!gz->where)
3503785Sphk					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;
3573784Sphk			}
35831718Sjdp			if (gz->file_offset == 0) {
35937656Sbde				q = (u_char *) (uintptr_t) gz->virtual_offset;
36037015Sbde				copyout(&gz->a_out, q, sizeof gz->a_out);
3613784Sphk			}
3623784Sphk		}
3633784Sphk	}
3643784Sphk	/* Skip over zero-padded first PAGE if needed */
36537015Sbde	if (gz->output < gz->file_offset &&
36637015Sbde	    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) {
3733784Sphk		i = min(siz, gz->file_end - gz->output);
37437656Sbde		q = (u_char *) (uintptr_t)
37537656Sbde		    (gz->virtual_offset + gz->output - gz->file_offset);
37637015Sbde		copyout(p, q, i);
3773784Sphk		gz->output += i;
3783784Sphk		p += i;
3793784Sphk		siz -= i;
3803784Sphk	}
3813784Sphk	gz->output += siz;
3823784Sphk	return 0;
3833332Sphk}
3843332Sphk
3853784Sphk
3863332Sphk/*
3873332Sphk * Tell kern_execve.c about it, with a little help from the linker.
3883332Sphk */
38943402Sdillonstatic struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
39040435SpeterEXEC_SET(execgzip, gzip_execsw);
391