imgact_gzip.c revision 24848
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 *
924848Sdyson * $Id: imgact_gzip.c,v 1.28 1997/02/22 09:38:57 peter 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
2420821Sjoerg#include "opt_rlimit.h"
2520821Sjoerg
263784Sphk#include <sys/param.h>
273332Sphk#include <sys/exec.h>
283332Sphk#include <sys/imgact.h>
293332Sphk#include <sys/imgact_aout.h>
303332Sphk#include <sys/kernel.h>
313507Scsgr#include <sys/mman.h>
3215494Sbde#include <sys/proc.h>
333507Scsgr#include <sys/resourcevar.h>
343332Sphk#include <sys/sysent.h>
353507Scsgr#include <sys/systm.h>
3615494Sbde#include <sys/vnode.h>
373784Sphk#include <sys/inflate.h>
383332Sphk
393332Sphk#include <vm/vm.h>
4012662Sdg#include <vm/vm_param.h>
4112662Sdg#include <vm/vm_prot.h>
4222521Sdyson#include <sys/lock.h>
4312662Sdg#include <vm/pmap.h>
4412662Sdg#include <vm/vm_map.h>
453332Sphk#include <vm/vm_kern.h>
4612662Sdg#include <vm/vm_extern.h>
473332Sphk
483784Sphkstruct imgact_gzip {
493784Sphk	struct image_params *ip;
503784Sphk	struct exec     a_out;
513784Sphk	int             error;
523784Sphk	int             where;
533784Sphk	u_char         *inbuf;
543784Sphk	u_long          offset;
553784Sphk	u_long          output;
563784Sphk	u_long          len;
573784Sphk	int             idx;
583784Sphk	u_long          virtual_offset, file_offset, file_end, bss_size;
593784Sphk};
603784Sphk
6112568Sbdestatic int exec_gzip_imgact __P((struct image_params *imgp));
623784Sphkstatic int NextByte __P((void *vp));
633784Sphkstatic int do_aout_hdr __P((struct imgact_gzip *));
643784Sphkstatic int Flush __P((void *vp, u_char *, u_long siz));
653784Sphk
6612568Sbdestatic int
6712130Sdgexec_gzip_imgact(imgp)
6812130Sdg	struct image_params *imgp;
693332Sphk{
703784Sphk	int             error, error2 = 0;
7117974Sbde	const u_char   *p = (const u_char *) imgp->image_header;
723784Sphk	struct imgact_gzip igz;
733784Sphk	struct inflate  infl;
7417386Sphk	struct vmspace *vmspace;
753332Sphk
763348Sphk	/* If these four are not OK, it isn't a gzip file */
773784Sphk	if (p[0] != 0x1f)
783784Sphk		return -1;	/* 0    Simply magic	 */
793784Sphk	if (p[1] != 0x8b)
803784Sphk		return -1;	/* 1    Simply magic	 */
813784Sphk	if (p[2] != 0x08)
823784Sphk		return -1;	/* 2    Compression method	 */
833784Sphk	if (p[9] != 0x03)
843784Sphk		return -1;	/* 9    OS compressed on	 */
853332Sphk
863784Sphk	/*
873784Sphk	 * If this one contains anything but a comment or a filename marker,
883784Sphk	 * we don't want to chew on it
893348Sphk	 */
903784Sphk	if (p[3] & ~(0x18))
913784Sphk		return ENOEXEC;	/* 3    Flags		 */
923332Sphk
933348Sphk	/* These are of no use to us */
943784Sphk	/* 4-7  Timestamp		 */
953784Sphk	/* 8    Extra flags		 */
963348Sphk
973784Sphk	bzero(&igz, sizeof igz);
983784Sphk	bzero(&infl, sizeof infl);
993784Sphk	infl.gz_private = (void *) &igz;
1003784Sphk	infl.gz_input = NextByte;
1013784Sphk	infl.gz_output = Flush;
1023332Sphk
10312130Sdg	igz.ip = imgp;
1043784Sphk	igz.idx = 10;
1053353Sphk
1063784Sphk	if (p[3] & 0x08) {	/* skip a filename */
1073784Sphk		while (p[igz.idx++])
1083784Sphk			if (igz.idx >= PAGE_SIZE)
1093784Sphk				return ENOEXEC;
1103348Sphk	}
1113784Sphk	if (p[3] & 0x10) {	/* skip a comment */
1123784Sphk		while (p[igz.idx++])
1133784Sphk			if (igz.idx >= PAGE_SIZE)
1143784Sphk				return ENOEXEC;
1153348Sphk	}
11617386Sphk	igz.len = imgp->attr->va_size;
1173332Sphk
1183784Sphk	error = inflate(&infl);
1193348Sphk
12017386Sphk	if ( !error ) {
12117386Sphk		vmspace = imgp->proc->p_vmspace;
12217386Sphk		error = vm_map_protect(&vmspace->vm_map,
12317386Sphk			(vm_offset_t) vmspace->vm_taddr,
12417386Sphk			(vm_offset_t) (vmspace->vm_taddr +
12517386Sphk				      (vmspace->vm_tsize << PAGE_SHIFT)) ,
12617386Sphk			VM_PROT_READ|VM_PROT_EXECUTE,0);
12717386Sphk	}
12817386Sphk
1293784Sphk	if (igz.inbuf) {
1303784Sphk		error2 =
1316579Sdg			vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
1326579Sdg			    (vm_offset_t) igz.inbuf + PAGE_SIZE);
1333332Sphk	}
1343784Sphk	if (igz.error || error || error2) {
1353784Sphk		printf("Output=%lu ", igz.output);
1363784Sphk		printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
1373784Sphk		       error, igz.error, error2, igz.where);
1383348Sphk	}
1393784Sphk	if (igz.error)
1403784Sphk		return igz.error;
1413784Sphk	if (error)
1423784Sphk		return ENOEXEC;
1438876Srgrimes	if (error2)
1443784Sphk		return error2;
1453784Sphk	return 0;
1463332Sphk}
1473332Sphk
1483784Sphkstatic int
1493784Sphkdo_aout_hdr(struct imgact_gzip * gz)
1503332Sphk{
1513784Sphk	int             error;
15224848Sdyson	struct vmspace *vmspace;
15314703Sbde	vm_offset_t     vmaddr;
1543332Sphk
1553784Sphk	/*
1563784Sphk	 * Set file/virtual offset based on a.out variant. We do two cases:
1573784Sphk	 * host byte order and network byte order (for NetBSD compatibility)
1583784Sphk	 */
1593784Sphk	switch ((int) (gz->a_out.a_magic & 0xffff)) {
1603332Sphk	case ZMAGIC:
1613784Sphk		gz->virtual_offset = 0;
1623784Sphk		if (gz->a_out.a_text) {
16315538Sphk			gz->file_offset = PAGE_SIZE;
1643784Sphk		} else {
1653784Sphk			/* Bill's "screwball mode" */
1663784Sphk			gz->file_offset = 0;
1673784Sphk		}
1683784Sphk		break;
1693332Sphk	case QMAGIC:
17015538Sphk		gz->virtual_offset = PAGE_SIZE;
1713784Sphk		gz->file_offset = 0;
1723784Sphk		break;
1733332Sphk	default:
1743784Sphk		/* NetBSD compatibility */
1753784Sphk		switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
1763784Sphk		case ZMAGIC:
1773784Sphk		case QMAGIC:
17815538Sphk			gz->virtual_offset = PAGE_SIZE;
1793784Sphk			gz->file_offset = 0;
1803784Sphk			break;
1813784Sphk		default:
1823784Sphk			gz->where = __LINE__;
1833784Sphk			return (-1);
1843784Sphk		}
1853332Sphk	}
1863332Sphk
18715538Sphk	gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
1883332Sphk
1893784Sphk	/*
1903784Sphk	 * Check various fields in header for validity/bounds.
1913784Sphk	 */
1923784Sphk	if (			/* entry point must lay with text region */
1933784Sphk	    gz->a_out.a_entry < gz->virtual_offset ||
1943784Sphk	    gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
1953332Sphk
1963332Sphk	/* text and data size must each be page rounded */
19715538Sphk	    gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
1983784Sphk		gz->where = __LINE__;
1993784Sphk		return (-1);
2003784Sphk	}
2013784Sphk	/*
2023784Sphk	 * text/data/bss must not exceed limits
2033784Sphk	 */
2043784Sphk	if (			/* text can't exceed maximum text size */
2053784Sphk	    gz->a_out.a_text > MAXTSIZ ||
2063332Sphk
2073332Sphk	/* data + bss can't exceed maximum data size */
2083784Sphk	    gz->a_out.a_data + gz->bss_size > MAXDSIZ ||
2093332Sphk
2103332Sphk	/* data + bss can't exceed rlimit */
2113784Sphk	    gz->a_out.a_data + gz->bss_size >
2123332Sphk	    gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
2133784Sphk		gz->where = __LINE__;
2143784Sphk		return (ENOMEM);
2153784Sphk	}
2163784Sphk	/* Find out how far we should go */
2173784Sphk	gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
2183332Sphk
2193784Sphk	/* copy in arguments and/or environment from old process */
2203784Sphk	error = exec_extract_strings(gz->ip);
2213784Sphk	if (error) {
2223784Sphk		gz->where = __LINE__;
2233784Sphk		return (error);
2243784Sphk	}
2253784Sphk	/*
2263784Sphk	 * Destroy old process VM and create a new one (with a new stack)
2273784Sphk	 */
2283784Sphk	exec_new_vmspace(gz->ip);
2293348Sphk
23024848Sdyson	vmspace = gz->ip->proc->p_vmspace;
23124848Sdyson
2323784Sphk	vmaddr = gz->virtual_offset;
2333332Sphk
2343784Sphk	error = vm_mmap(&vmspace->vm_map,
2353784Sphk			&vmaddr,
23617386Sphk			gz->a_out.a_text + gz->a_out.a_data,
23717386Sphk			VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
2383784Sphk			0,
2393784Sphk			0);
2403332Sphk
2413784Sphk	if (error) {
2423784Sphk		gz->where = __LINE__;
2433784Sphk		return (error);
2443784Sphk	}
24517386Sphk
2466579Sdg	if (gz->bss_size != 0) {
2476579Sdg		/*
24814087Sphk		 * Allocate demand-zeroed area for uninitialized data.
24914087Sphk		 * "bss" = 'block started by symbol' - named after the
25014087Sphk		 * IBM 7090 instruction of the same name.
2516579Sdg		 */
25214087Sphk		vmaddr = gz->virtual_offset + gz->a_out.a_text +
25314087Sphk			gz->a_out.a_data;
25417386Sphk		error = vm_map_find(&vmspace->vm_map,
25517386Sphk				NULL,
25617386Sphk				0,
25717386Sphk				&vmaddr,
25817386Sphk				gz->bss_size,
25917386Sphk				FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
2606579Sdg		if (error) {
2616579Sdg			gz->where = __LINE__;
2626579Sdg			return (error);
2636579Sdg		}
2643784Sphk	}
2653784Sphk	/* Fill in process VM information */
2663784Sphk	vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
2673784Sphk	vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
2683784Sphk	vmspace->vm_taddr = (caddr_t) gz->virtual_offset;
2693784Sphk	vmspace->vm_daddr = (caddr_t) gz->virtual_offset + gz->a_out.a_text;
2703332Sphk
2713784Sphk	/* Fill in image_params */
2723784Sphk	gz->ip->interpreted = 0;
2733784Sphk	gz->ip->entry_addr = gz->a_out.a_entry;
2743332Sphk
2753784Sphk	gz->ip->proc->p_sysent = &aout_sysvec;
2763332Sphk
2773784Sphk	return 0;
2783784Sphk}
2793332Sphk
2803784Sphkstatic int
2813784SphkNextByte(void *vp)
2823784Sphk{
2833784Sphk	int             error;
2843784Sphk	struct imgact_gzip *igz = (struct imgact_gzip *) vp;
2853332Sphk
2863784Sphk	if (igz->idx >= igz->len) {
2873784Sphk		igz->where = __LINE__;
2883784Sphk		return GZ_EOF;
2893784Sphk	}
2903784Sphk	if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
2913784Sphk		return igz->inbuf[(igz->idx++) - igz->offset];
2923784Sphk	}
2933784Sphk	if (igz->inbuf) {
2946579Sdg		error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
2956579Sdg			    (vm_offset_t) igz->inbuf + PAGE_SIZE);
2963784Sphk		if (error) {
2973784Sphk			igz->where = __LINE__;
2983784Sphk			igz->error = error;
2993784Sphk			return GZ_EOF;
3003784Sphk		}
3013784Sphk	}
3026342Sphk	igz->offset = igz->idx & ~PAGE_MASK;
3033332Sphk
3043784Sphk	error = vm_mmap(kernel_map,	/* map */
3053784Sphk			(vm_offset_t *) & igz->inbuf,	/* address */
3063784Sphk			PAGE_SIZE,	/* size */
3073784Sphk			VM_PROT_READ,	/* protection */
3083784Sphk			VM_PROT_READ,	/* max protection */
3093784Sphk			0,	/* flags */
31012130Sdg			(caddr_t) igz->ip->vp,	/* vnode */
3113784Sphk			igz->offset);	/* offset */
3123784Sphk	if (error) {
3133784Sphk		igz->where = __LINE__;
3143784Sphk		igz->error = error;
3153784Sphk		return GZ_EOF;
3163784Sphk	}
3173784Sphk	return igz->inbuf[(igz->idx++) - igz->offset];
3183784Sphk}
3193332Sphk
3203784Sphkstatic int
3213784SphkFlush(void *vp, u_char * ptr, u_long siz)
3223784Sphk{
3233784Sphk	struct imgact_gzip *gz = (struct imgact_gzip *) vp;
3243784Sphk	u_char         *p = ptr, *q;
3253784Sphk	int             i;
3263348Sphk
3273784Sphk	/* First, find a a.out-header */
3283784Sphk	if (gz->output < sizeof gz->a_out) {
3293784Sphk		q = (u_char *) & gz->a_out;
3303784Sphk		i = min(siz, sizeof gz->a_out - gz->output);
3313784Sphk		bcopy(p, q + gz->output, i);
3323784Sphk		gz->output += i;
3333784Sphk		p += i;
3343784Sphk		siz -= i;
3353784Sphk		if (gz->output == sizeof gz->a_out) {
3363784Sphk			i = do_aout_hdr(gz);
3373784Sphk			if (i == -1) {
3383785Sphk				if (!gz->where)
3393785Sphk					gz->where = __LINE__;
3403784Sphk				gz->error = ENOEXEC;
3413784Sphk				return ENOEXEC;
3423784Sphk			} else if (i) {
3433784Sphk				gz->where = __LINE__;
3443784Sphk				gz->error = i;
3453784Sphk				return ENOEXEC;
3463784Sphk			}
3473784Sphk			if (gz->file_offset < sizeof gz->a_out) {
3483784Sphk				q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset;
3493784Sphk				bcopy(&gz->a_out, q, sizeof gz->a_out - gz->file_offset);
3503784Sphk			}
3513784Sphk		}
3523784Sphk	}
3533784Sphk	/* Skip over zero-padded first PAGE if needed */
3543784Sphk	if (gz->output < gz->file_offset && (gz->output + siz) > gz->file_offset) {
3553784Sphk		i = min(siz, gz->file_offset - gz->output);
3563784Sphk		gz->output += i;
3573784Sphk		p += i;
3583784Sphk		siz -= i;
3593784Sphk	}
3603784Sphk	if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
3613784Sphk		i = min(siz, gz->file_end - gz->output);
3623784Sphk		q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset;
3633784Sphk		bcopy(p, q, i);
3643784Sphk		gz->output += i;
3653784Sphk		p += i;
3663784Sphk		siz -= i;
3673784Sphk	}
3683784Sphk	gz->output += siz;
3693784Sphk	return 0;
3703332Sphk}
3713332Sphk
3723784Sphk
3733332Sphk/*
3743332Sphk * Tell kern_execve.c about it, with a little help from the linker.
3753332Sphk * Since `const' objects end up in the text segment, TEXT_SET is the
3763332Sphk * correct directive to use.
3773332Sphk */
3783784Sphk
3793784Sphkstatic const struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
3803332SphkTEXT_SET(execsw_set, gzip_execsw);
381