imgact_gzip.c revision 15538
13332Sphk/*
23332Sphk * ----------------------------------------------------------------------------
33332Sphk * "THE BEER-WARE LICENSE" (Revision 42):
43784Sphk * <phk@login.dkuug.dk> 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 *
915538Sphk * $Id: imgact_gzip.c,v 1.21 1996/05/01 02:42:50 bde Exp $
103332Sphk *
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
243784Sphk#include <sys/param.h>
253332Sphk#include <sys/exec.h>
263332Sphk#include <sys/imgact.h>
273332Sphk#include <sys/imgact_aout.h>
283332Sphk#include <sys/kernel.h>
293507Scsgr#include <sys/mman.h>
3015494Sbde#include <sys/proc.h>
313507Scsgr#include <sys/resourcevar.h>
323332Sphk#include <sys/sysent.h>
333507Scsgr#include <sys/systm.h>
3415494Sbde#include <sys/vnode.h>
353784Sphk#include <sys/inflate.h>
363332Sphk
373332Sphk#include <vm/vm.h>
3812662Sdg#include <vm/vm_param.h>
3912662Sdg#include <vm/vm_prot.h>
4012662Sdg#include <vm/lock.h>
4112662Sdg#include <vm/pmap.h>
4212662Sdg#include <vm/vm_map.h>
433332Sphk#include <vm/vm_kern.h>
4412662Sdg#include <vm/vm_extern.h>
453332Sphk
463784Sphkstruct imgact_gzip {
473784Sphk	struct image_params *ip;
483784Sphk	struct exec     a_out;
493784Sphk	int             error;
503784Sphk	int             where;
513784Sphk	u_char         *inbuf;
523784Sphk	u_long          offset;
533784Sphk	u_long          output;
543784Sphk	u_long          len;
553784Sphk	int             idx;
563784Sphk	u_long          virtual_offset, file_offset, file_end, bss_size;
573784Sphk};
583784Sphk
5912568Sbdestatic int exec_gzip_imgact __P((struct image_params *imgp));
603784Sphkstatic int NextByte __P((void *vp));
613784Sphkstatic int do_aout_hdr __P((struct imgact_gzip *));
623784Sphkstatic int Flush __P((void *vp, u_char *, u_long siz));
633784Sphk
6412568Sbdestatic int
6512130Sdgexec_gzip_imgact(imgp)
6612130Sdg	struct image_params *imgp;
673332Sphk{
683784Sphk	int             error, error2 = 0;
6912130Sdg	u_char         *p = (u_char *) imgp->image_header;
703784Sphk	struct imgact_gzip igz;
713784Sphk	struct inflate  infl;
723332Sphk
733348Sphk	/* If these four are not OK, it isn't a gzip file */
743784Sphk	if (p[0] != 0x1f)
753784Sphk		return -1;	/* 0    Simply magic	 */
763784Sphk	if (p[1] != 0x8b)
773784Sphk		return -1;	/* 1    Simply magic	 */
783784Sphk	if (p[2] != 0x08)
793784Sphk		return -1;	/* 2    Compression method	 */
803784Sphk	if (p[9] != 0x03)
813784Sphk		return -1;	/* 9    OS compressed on	 */
823332Sphk
833784Sphk	/*
843784Sphk	 * If this one contains anything but a comment or a filename marker,
853784Sphk	 * we don't want to chew on it
863348Sphk	 */
873784Sphk	if (p[3] & ~(0x18))
883784Sphk		return ENOEXEC;	/* 3    Flags		 */
893332Sphk
903348Sphk	/* These are of no use to us */
913784Sphk	/* 4-7  Timestamp		 */
923784Sphk	/* 8    Extra flags		 */
933348Sphk
943784Sphk	bzero(&igz, sizeof igz);
953784Sphk	bzero(&infl, sizeof infl);
963784Sphk	infl.gz_private = (void *) &igz;
973784Sphk	infl.gz_input = NextByte;
983784Sphk	infl.gz_output = Flush;
993332Sphk
10012130Sdg	igz.ip = imgp;
1013784Sphk	igz.idx = 10;
1023353Sphk
1033784Sphk	if (p[3] & 0x08) {	/* skip a filename */
1043784Sphk		while (p[igz.idx++])
1053784Sphk			if (igz.idx >= PAGE_SIZE)
1063784Sphk				return ENOEXEC;
1073348Sphk	}
1083784Sphk	if (p[3] & 0x10) {	/* skip a comment */
1093784Sphk		while (p[igz.idx++])
1103784Sphk			if (igz.idx >= PAGE_SIZE)
1113784Sphk				return ENOEXEC;
1123348Sphk	}
1133784Sphk	igz.len = igz.ip->attr->va_size;
1143332Sphk
1153784Sphk	error = inflate(&infl);
1163348Sphk
1173784Sphk	if (igz.inbuf) {
1183784Sphk		error2 =
1196579Sdg			vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
1206579Sdg			    (vm_offset_t) igz.inbuf + PAGE_SIZE);
1213332Sphk	}
1223784Sphk	if (igz.error || error || error2) {
1233784Sphk		printf("Output=%lu ", igz.output);
1243784Sphk		printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
1253784Sphk		       error, igz.error, error2, igz.where);
1263348Sphk	}
1273784Sphk	if (igz.error)
1283784Sphk		return igz.error;
1293784Sphk	if (error)
1303784Sphk		return ENOEXEC;
1318876Srgrimes	if (error2)
1323784Sphk		return error2;
1333784Sphk	return 0;
1343332Sphk}
1353332Sphk
1363784Sphkstatic int
1373784Sphkdo_aout_hdr(struct imgact_gzip * gz)
1383332Sphk{
1393784Sphk	int             error;
1403784Sphk	struct vmspace *vmspace = gz->ip->proc->p_vmspace;
14114703Sbde	vm_offset_t     vmaddr;
1423332Sphk
1433784Sphk	/*
1443784Sphk	 * Set file/virtual offset based on a.out variant. We do two cases:
1453784Sphk	 * host byte order and network byte order (for NetBSD compatibility)
1463784Sphk	 */
1473784Sphk	switch ((int) (gz->a_out.a_magic & 0xffff)) {
1483332Sphk	case ZMAGIC:
1493784Sphk		gz->virtual_offset = 0;
1503784Sphk		if (gz->a_out.a_text) {
15115538Sphk			gz->file_offset = PAGE_SIZE;
1523784Sphk		} else {
1533784Sphk			/* Bill's "screwball mode" */
1543784Sphk			gz->file_offset = 0;
1553784Sphk		}
1563784Sphk		break;
1573332Sphk	case QMAGIC:
15815538Sphk		gz->virtual_offset = PAGE_SIZE;
1593784Sphk		gz->file_offset = 0;
1603784Sphk		break;
1613332Sphk	default:
1623784Sphk		/* NetBSD compatibility */
1633784Sphk		switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
1643784Sphk		case ZMAGIC:
1653784Sphk		case QMAGIC:
16615538Sphk			gz->virtual_offset = PAGE_SIZE;
1673784Sphk			gz->file_offset = 0;
1683784Sphk			break;
1693784Sphk		default:
1703784Sphk			gz->where = __LINE__;
1713784Sphk			return (-1);
1723784Sphk		}
1733332Sphk	}
1743332Sphk
17515538Sphk	gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
1763332Sphk
1773784Sphk	/*
1783784Sphk	 * Check various fields in header for validity/bounds.
1793784Sphk	 */
1803784Sphk	if (			/* entry point must lay with text region */
1813784Sphk	    gz->a_out.a_entry < gz->virtual_offset ||
1823784Sphk	    gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
1833332Sphk
1843332Sphk	/* text and data size must each be page rounded */
18515538Sphk	    gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
1863784Sphk		gz->where = __LINE__;
1873784Sphk		return (-1);
1883784Sphk	}
1893784Sphk	/*
1903784Sphk	 * text/data/bss must not exceed limits
1913784Sphk	 */
1923784Sphk	if (			/* text can't exceed maximum text size */
1933784Sphk	    gz->a_out.a_text > MAXTSIZ ||
1943332Sphk
1953332Sphk	/* data + bss can't exceed maximum data size */
1963784Sphk	    gz->a_out.a_data + gz->bss_size > MAXDSIZ ||
1973332Sphk
1983332Sphk	/* data + bss can't exceed rlimit */
1993784Sphk	    gz->a_out.a_data + gz->bss_size >
2003332Sphk	    gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
2013784Sphk		gz->where = __LINE__;
2023784Sphk		return (ENOMEM);
2033784Sphk	}
2043784Sphk	/* Find out how far we should go */
2053784Sphk	gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
2063332Sphk
2073784Sphk	/* copy in arguments and/or environment from old process */
2083784Sphk	error = exec_extract_strings(gz->ip);
2093784Sphk	if (error) {
2103784Sphk		gz->where = __LINE__;
2113784Sphk		return (error);
2123784Sphk	}
2133784Sphk	/*
2143784Sphk	 * Destroy old process VM and create a new one (with a new stack)
2153784Sphk	 */
2163784Sphk	exec_new_vmspace(gz->ip);
2173348Sphk
2183784Sphk	vmaddr = gz->virtual_offset;
2193332Sphk
2203784Sphk	error = vm_mmap(&vmspace->vm_map,	/* map */
2213784Sphk			&vmaddr,/* address */
2223784Sphk			gz->a_out.a_text,	/* size */
2233784Sphk			VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE,	/* protection */
2243784Sphk			VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE,
2253784Sphk			MAP_ANON | MAP_FIXED,	/* flags */
2263784Sphk			0,	/* vnode */
2273784Sphk			0);	/* offset */
2283332Sphk
2293784Sphk	if (error) {
2303784Sphk		gz->where = __LINE__;
2313784Sphk		return (error);
2323784Sphk	}
2333784Sphk	vmaddr = gz->virtual_offset + gz->a_out.a_text;
2343332Sphk
2353784Sphk	/*
2363784Sphk	 * Map data read/write (if text is 0, assume text is in data area
2373784Sphk	 * [Bill's screwball mode])
2383784Sphk	 */
2393332Sphk
2403784Sphk	error = vm_mmap(&vmspace->vm_map,
2413784Sphk			&vmaddr,
2423784Sphk			gz->a_out.a_data,
2433784Sphk			VM_PROT_READ | VM_PROT_WRITE | (gz->a_out.a_text ? 0 : VM_PROT_EXECUTE),
2443784Sphk			VM_PROT_ALL, MAP_ANON | MAP_FIXED,
2453784Sphk			0,
2463784Sphk			0);
2473332Sphk
2483784Sphk	if (error) {
2493784Sphk		gz->where = __LINE__;
2503784Sphk		return (error);
2513784Sphk	}
2526579Sdg	if (gz->bss_size != 0) {
2536579Sdg		/*
25414087Sphk		 * Allocate demand-zeroed area for uninitialized data.
25514087Sphk		 * "bss" = 'block started by symbol' - named after the
25614087Sphk		 * IBM 7090 instruction of the same name.
2576579Sdg		 */
25814087Sphk		vmaddr = gz->virtual_offset + gz->a_out.a_text +
25914087Sphk			gz->a_out.a_data;
26014087Sphk		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
26114087Sphk			gz->bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
2626579Sdg		if (error) {
2636579Sdg			gz->where = __LINE__;
2646579Sdg			return (error);
2656579Sdg		}
2663784Sphk	}
2673784Sphk	/* Fill in process VM information */
2683784Sphk	vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
2693784Sphk	vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
2703784Sphk	vmspace->vm_taddr = (caddr_t) gz->virtual_offset;
2713784Sphk	vmspace->vm_daddr = (caddr_t) gz->virtual_offset + gz->a_out.a_text;
2723332Sphk
2733784Sphk	/* Fill in image_params */
2743784Sphk	gz->ip->interpreted = 0;
2753784Sphk	gz->ip->entry_addr = gz->a_out.a_entry;
2763332Sphk
2773784Sphk	gz->ip->proc->p_sysent = &aout_sysvec;
2783332Sphk
2793784Sphk	return 0;
2803784Sphk}
2813332Sphk
2823784Sphkstatic int
2833784SphkNextByte(void *vp)
2843784Sphk{
2853784Sphk	int             error;
2863784Sphk	struct imgact_gzip *igz = (struct imgact_gzip *) vp;
2873332Sphk
2883784Sphk	if (igz->idx >= igz->len) {
2893784Sphk		igz->where = __LINE__;
2903784Sphk		return GZ_EOF;
2913784Sphk	}
2923784Sphk	if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
2933784Sphk		return igz->inbuf[(igz->idx++) - igz->offset];
2943784Sphk	}
2953784Sphk	if (igz->inbuf) {
2966579Sdg		error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
2976579Sdg			    (vm_offset_t) igz->inbuf + PAGE_SIZE);
2983784Sphk		if (error) {
2993784Sphk			igz->where = __LINE__;
3003784Sphk			igz->error = error;
3013784Sphk			return GZ_EOF;
3023784Sphk		}
3033784Sphk	}
3046342Sphk	igz->offset = igz->idx & ~PAGE_MASK;
3053332Sphk
3063784Sphk	error = vm_mmap(kernel_map,	/* map */
3073784Sphk			(vm_offset_t *) & igz->inbuf,	/* address */
3083784Sphk			PAGE_SIZE,	/* size */
3093784Sphk			VM_PROT_READ,	/* protection */
3103784Sphk			VM_PROT_READ,	/* max protection */
3113784Sphk			0,	/* flags */
31212130Sdg			(caddr_t) igz->ip->vp,	/* vnode */
3133784Sphk			igz->offset);	/* offset */
3143784Sphk	if (error) {
3153784Sphk		igz->where = __LINE__;
3163784Sphk		igz->error = error;
3173784Sphk		return GZ_EOF;
3183784Sphk	}
3193784Sphk	return igz->inbuf[(igz->idx++) - igz->offset];
3203784Sphk}
3213332Sphk
3223784Sphkstatic int
3233784SphkFlush(void *vp, u_char * ptr, u_long siz)
3243784Sphk{
3253784Sphk	struct imgact_gzip *gz = (struct imgact_gzip *) vp;
3263784Sphk	u_char         *p = ptr, *q;
3273784Sphk	int             i;
3283348Sphk
3293784Sphk	/* First, find a a.out-header */
3303784Sphk	if (gz->output < sizeof gz->a_out) {
3313784Sphk		q = (u_char *) & gz->a_out;
3323784Sphk		i = min(siz, sizeof gz->a_out - gz->output);
3333784Sphk		bcopy(p, q + gz->output, i);
3343784Sphk		gz->output += i;
3353784Sphk		p += i;
3363784Sphk		siz -= i;
3373784Sphk		if (gz->output == sizeof gz->a_out) {
3383784Sphk			i = do_aout_hdr(gz);
3393784Sphk			if (i == -1) {
3403785Sphk				if (!gz->where)
3413785Sphk					gz->where = __LINE__;
3423784Sphk				gz->error = ENOEXEC;
3433784Sphk				return ENOEXEC;
3443784Sphk			} else if (i) {
3453784Sphk				gz->where = __LINE__;
3463784Sphk				gz->error = i;
3473784Sphk				return ENOEXEC;
3483784Sphk			}
3493784Sphk			if (gz->file_offset < sizeof gz->a_out) {
3503784Sphk				q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset;
3513784Sphk				bcopy(&gz->a_out, q, sizeof gz->a_out - gz->file_offset);
3523784Sphk			}
3533784Sphk		}
3543784Sphk	}
3553784Sphk	/* Skip over zero-padded first PAGE if needed */
3563784Sphk	if (gz->output < gz->file_offset && (gz->output + siz) > gz->file_offset) {
3573784Sphk		i = min(siz, gz->file_offset - gz->output);
3583784Sphk		gz->output += i;
3593784Sphk		p += i;
3603784Sphk		siz -= i;
3613784Sphk	}
3623784Sphk	if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
3633784Sphk		i = min(siz, gz->file_end - gz->output);
3643784Sphk		q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset;
3653784Sphk		bcopy(p, q, i);
3663784Sphk		gz->output += i;
3673784Sphk		p += i;
3683784Sphk		siz -= i;
3693784Sphk	}
3703784Sphk	gz->output += siz;
3713784Sphk	return 0;
3723332Sphk}
3733332Sphk
3743784Sphk
3753332Sphk/*
3763332Sphk * Tell kern_execve.c about it, with a little help from the linker.
3773332Sphk * Since `const' objects end up in the text segment, TEXT_SET is the
3783332Sphk * correct directive to use.
3793332Sphk */
3803784Sphk
3813784Sphkstatic const struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
3823332SphkTEXT_SET(execsw_set, gzip_execsw);
383