1994-10-03 05:23:01 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
1994-10-22 11:40:38 +00:00
|
|
|
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
|
1994-10-03 05:23:01 +00:00
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
1995-12-02 16:32:03 +00:00
|
|
|
* $Id: imgact_gzip.c,v 1.15 1995/11/06 12:52:30 davidg Exp $
|
1994-10-03 05:23:01 +00:00
|
|
|
*
|
|
|
|
* This module handles execution of a.out files which have been run through
|
1994-10-22 11:40:38 +00:00
|
|
|
* "gzip". This saves diskspace, but wastes cpu-cycles and VM.
|
1994-10-03 05:23:01 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* text-segments should be made R/O after being filled
|
|
|
|
* is the vm-stuff safe ?
|
|
|
|
* should handle the entire header of gzip'ed stuff.
|
|
|
|
* inflate isn't quite reentrant yet...
|
|
|
|
* error-handling is a mess...
|
|
|
|
* so is the rest...
|
1994-10-07 22:27:00 +00:00
|
|
|
* tidy up unnecesary includes
|
1994-10-03 05:23:01 +00:00
|
|
|
*/
|
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
#include <sys/param.h>
|
1994-10-03 05:23:01 +00:00
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/imgact.h>
|
|
|
|
#include <sys/imgact_aout.h>
|
|
|
|
#include <sys/kernel.h>
|
1994-10-11 11:29:17 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/resourcevar.h>
|
1994-10-03 05:23:01 +00:00
|
|
|
#include <sys/sysent.h>
|
1994-10-11 11:29:17 +00:00
|
|
|
#include <sys/systm.h>
|
1994-10-22 11:40:38 +00:00
|
|
|
#include <sys/inflate.h>
|
1994-10-03 05:23:01 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_kern.h>
|
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
struct imgact_gzip {
|
|
|
|
struct image_params *ip;
|
|
|
|
struct exec a_out;
|
|
|
|
int error;
|
|
|
|
int where;
|
|
|
|
u_char *inbuf;
|
|
|
|
u_long offset;
|
|
|
|
u_long output;
|
|
|
|
u_long len;
|
|
|
|
int idx;
|
|
|
|
u_long virtual_offset, file_offset, file_end, bss_size;
|
|
|
|
};
|
|
|
|
|
1995-12-02 16:32:03 +00:00
|
|
|
static int exec_gzip_imgact __P((struct image_params *imgp));
|
1994-10-22 11:40:38 +00:00
|
|
|
static int NextByte __P((void *vp));
|
|
|
|
static int do_aout_hdr __P((struct imgact_gzip *));
|
|
|
|
static int Flush __P((void *vp, u_char *, u_long siz));
|
|
|
|
|
1995-12-02 16:32:03 +00:00
|
|
|
static int
|
1995-11-06 12:52:37 +00:00
|
|
|
exec_gzip_imgact(imgp)
|
|
|
|
struct image_params *imgp;
|
1994-10-03 05:23:01 +00:00
|
|
|
{
|
1994-10-22 11:40:38 +00:00
|
|
|
int error, error2 = 0;
|
1995-11-06 12:52:37 +00:00
|
|
|
u_char *p = (u_char *) imgp->image_header;
|
1994-10-22 11:40:38 +00:00
|
|
|
struct imgact_gzip igz;
|
|
|
|
struct inflate infl;
|
1994-10-03 05:23:01 +00:00
|
|
|
|
1994-10-04 03:09:13 +00:00
|
|
|
/* If these four are not OK, it isn't a gzip file */
|
1994-10-22 11:40:38 +00:00
|
|
|
if (p[0] != 0x1f)
|
|
|
|
return -1; /* 0 Simply magic */
|
|
|
|
if (p[1] != 0x8b)
|
|
|
|
return -1; /* 1 Simply magic */
|
|
|
|
if (p[2] != 0x08)
|
|
|
|
return -1; /* 2 Compression method */
|
|
|
|
if (p[9] != 0x03)
|
|
|
|
return -1; /* 9 OS compressed on */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this one contains anything but a comment or a filename marker,
|
|
|
|
* we don't want to chew on it
|
1994-10-04 03:09:13 +00:00
|
|
|
*/
|
1994-10-22 11:40:38 +00:00
|
|
|
if (p[3] & ~(0x18))
|
|
|
|
return ENOEXEC; /* 3 Flags */
|
1994-10-04 03:09:13 +00:00
|
|
|
|
|
|
|
/* These are of no use to us */
|
1994-10-22 11:40:38 +00:00
|
|
|
/* 4-7 Timestamp */
|
|
|
|
/* 8 Extra flags */
|
|
|
|
|
|
|
|
bzero(&igz, sizeof igz);
|
|
|
|
bzero(&infl, sizeof infl);
|
|
|
|
infl.gz_private = (void *) &igz;
|
|
|
|
infl.gz_input = NextByte;
|
|
|
|
infl.gz_output = Flush;
|
|
|
|
|
1995-11-06 12:52:37 +00:00
|
|
|
igz.ip = imgp;
|
1994-10-22 11:40:38 +00:00
|
|
|
igz.idx = 10;
|
|
|
|
|
|
|
|
if (p[3] & 0x08) { /* skip a filename */
|
|
|
|
while (p[igz.idx++])
|
|
|
|
if (igz.idx >= PAGE_SIZE)
|
|
|
|
return ENOEXEC;
|
1994-10-04 03:09:13 +00:00
|
|
|
}
|
1994-10-22 11:40:38 +00:00
|
|
|
if (p[3] & 0x10) { /* skip a comment */
|
|
|
|
while (p[igz.idx++])
|
|
|
|
if (igz.idx >= PAGE_SIZE)
|
|
|
|
return ENOEXEC;
|
1994-10-04 03:09:13 +00:00
|
|
|
}
|
1994-10-22 11:40:38 +00:00
|
|
|
igz.len = igz.ip->attr->va_size;
|
1994-10-04 03:09:13 +00:00
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
error = inflate(&infl);
|
1994-10-04 03:09:13 +00:00
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
if (igz.inbuf) {
|
|
|
|
error2 =
|
1995-02-20 22:23:31 +00:00
|
|
|
vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
|
|
|
|
(vm_offset_t) igz.inbuf + PAGE_SIZE);
|
1994-10-03 05:23:01 +00:00
|
|
|
}
|
1994-10-22 11:40:38 +00:00
|
|
|
if (igz.error || error || error2) {
|
|
|
|
printf("Output=%lu ", igz.output);
|
|
|
|
printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
|
|
|
|
error, igz.error, error2, igz.where);
|
1994-10-04 03:09:13 +00:00
|
|
|
}
|
1994-10-22 11:40:38 +00:00
|
|
|
if (igz.error)
|
|
|
|
return igz.error;
|
|
|
|
if (error)
|
|
|
|
return ENOEXEC;
|
1995-05-30 08:16:23 +00:00
|
|
|
if (error2)
|
1994-10-22 11:40:38 +00:00
|
|
|
return error2;
|
|
|
|
return 0;
|
1994-10-03 05:23:01 +00:00
|
|
|
}
|
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
static int
|
|
|
|
do_aout_hdr(struct imgact_gzip * gz)
|
1994-10-03 05:23:01 +00:00
|
|
|
{
|
1994-10-22 11:40:38 +00:00
|
|
|
int error;
|
|
|
|
struct vmspace *vmspace = gz->ip->proc->p_vmspace;
|
|
|
|
u_long vmaddr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set file/virtual offset based on a.out variant. We do two cases:
|
|
|
|
* host byte order and network byte order (for NetBSD compatibility)
|
|
|
|
*/
|
|
|
|
switch ((int) (gz->a_out.a_magic & 0xffff)) {
|
1994-10-03 05:23:01 +00:00
|
|
|
case ZMAGIC:
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->virtual_offset = 0;
|
|
|
|
if (gz->a_out.a_text) {
|
|
|
|
gz->file_offset = NBPG;
|
|
|
|
} else {
|
|
|
|
/* Bill's "screwball mode" */
|
|
|
|
gz->file_offset = 0;
|
|
|
|
}
|
|
|
|
break;
|
1994-10-03 05:23:01 +00:00
|
|
|
case QMAGIC:
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->virtual_offset = NBPG;
|
|
|
|
gz->file_offset = 0;
|
|
|
|
break;
|
1994-10-03 05:23:01 +00:00
|
|
|
default:
|
1994-10-22 11:40:38 +00:00
|
|
|
/* NetBSD compatibility */
|
|
|
|
switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
|
|
|
|
case ZMAGIC:
|
|
|
|
case QMAGIC:
|
|
|
|
gz->virtual_offset = NBPG;
|
|
|
|
gz->file_offset = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gz->where = __LINE__;
|
|
|
|
return (-1);
|
|
|
|
}
|
1994-10-03 05:23:01 +00:00
|
|
|
}
|
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->bss_size = roundup(gz->a_out.a_bss, NBPG);
|
1994-10-03 05:23:01 +00:00
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
/*
|
|
|
|
* Check various fields in header for validity/bounds.
|
|
|
|
*/
|
|
|
|
if ( /* entry point must lay with text region */
|
|
|
|
gz->a_out.a_entry < gz->virtual_offset ||
|
|
|
|
gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
|
1994-10-03 05:23:01 +00:00
|
|
|
|
|
|
|
/* text and data size must each be page rounded */
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->a_out.a_text % NBPG ||
|
|
|
|
gz->a_out.a_data % NBPG) {
|
|
|
|
gz->where = __LINE__;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* text/data/bss must not exceed limits
|
|
|
|
*/
|
|
|
|
if ( /* text can't exceed maximum text size */
|
|
|
|
gz->a_out.a_text > MAXTSIZ ||
|
1994-10-03 05:23:01 +00:00
|
|
|
|
|
|
|
/* data + bss can't exceed maximum data size */
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->a_out.a_data + gz->bss_size > MAXDSIZ ||
|
1994-10-03 05:23:01 +00:00
|
|
|
|
|
|
|
/* data + bss can't exceed rlimit */
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->a_out.a_data + gz->bss_size >
|
1994-10-03 05:23:01 +00:00
|
|
|
gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->where = __LINE__;
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
/* Find out how far we should go */
|
|
|
|
gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
|
|
|
|
|
|
|
|
/* copy in arguments and/or environment from old process */
|
|
|
|
error = exec_extract_strings(gz->ip);
|
|
|
|
if (error) {
|
|
|
|
gz->where = __LINE__;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Destroy old process VM and create a new one (with a new stack)
|
|
|
|
*/
|
|
|
|
exec_new_vmspace(gz->ip);
|
|
|
|
|
|
|
|
vmaddr = gz->virtual_offset;
|
|
|
|
|
|
|
|
error = vm_mmap(&vmspace->vm_map, /* map */
|
|
|
|
&vmaddr,/* address */
|
|
|
|
gz->a_out.a_text, /* size */
|
|
|
|
VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, /* protection */
|
|
|
|
VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE,
|
|
|
|
MAP_ANON | MAP_FIXED, /* flags */
|
|
|
|
0, /* vnode */
|
|
|
|
0); /* offset */
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
gz->where = __LINE__;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
vmaddr = gz->virtual_offset + gz->a_out.a_text;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map data read/write (if text is 0, assume text is in data area
|
|
|
|
* [Bill's screwball mode])
|
|
|
|
*/
|
|
|
|
|
|
|
|
error = vm_mmap(&vmspace->vm_map,
|
|
|
|
&vmaddr,
|
|
|
|
gz->a_out.a_data,
|
|
|
|
VM_PROT_READ | VM_PROT_WRITE | (gz->a_out.a_text ? 0 : VM_PROT_EXECUTE),
|
|
|
|
VM_PROT_ALL, MAP_ANON | MAP_FIXED,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
gz->where = __LINE__;
|
|
|
|
return (error);
|
|
|
|
}
|
1995-02-20 22:23:31 +00:00
|
|
|
if (gz->bss_size != 0) {
|
|
|
|
/*
|
|
|
|
* Allocate demand-zeroed area for uninitialized data "bss" = 'block
|
|
|
|
* started by symbol' - named after the IBM 7090 instruction of the
|
|
|
|
* same name.
|
|
|
|
*/
|
|
|
|
vmaddr = gz->virtual_offset + gz->a_out.a_text + gz->a_out.a_data;
|
|
|
|
error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, gz->bss_size, FALSE);
|
|
|
|
if (error) {
|
|
|
|
gz->where = __LINE__;
|
|
|
|
return (error);
|
|
|
|
}
|
1994-10-22 11:40:38 +00:00
|
|
|
}
|
|
|
|
/* Fill in process VM information */
|
|
|
|
vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
|
|
|
|
vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
|
|
|
|
vmspace->vm_taddr = (caddr_t) gz->virtual_offset;
|
|
|
|
vmspace->vm_daddr = (caddr_t) gz->virtual_offset + gz->a_out.a_text;
|
|
|
|
|
|
|
|
/* Fill in image_params */
|
|
|
|
gz->ip->interpreted = 0;
|
|
|
|
gz->ip->entry_addr = gz->a_out.a_entry;
|
|
|
|
|
|
|
|
gz->ip->proc->p_sysent = &aout_sysvec;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NextByte(void *vp)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct imgact_gzip *igz = (struct imgact_gzip *) vp;
|
|
|
|
|
|
|
|
if (igz->idx >= igz->len) {
|
|
|
|
igz->where = __LINE__;
|
|
|
|
return GZ_EOF;
|
|
|
|
}
|
|
|
|
if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
|
|
|
|
return igz->inbuf[(igz->idx++) - igz->offset];
|
|
|
|
}
|
|
|
|
if (igz->inbuf) {
|
1995-02-20 22:23:31 +00:00
|
|
|
error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
|
|
|
|
(vm_offset_t) igz->inbuf + PAGE_SIZE);
|
1994-10-22 11:40:38 +00:00
|
|
|
if (error) {
|
|
|
|
igz->where = __LINE__;
|
|
|
|
igz->error = error;
|
|
|
|
return GZ_EOF;
|
|
|
|
}
|
|
|
|
}
|
1995-02-13 07:40:33 +00:00
|
|
|
igz->offset = igz->idx & ~PAGE_MASK;
|
1994-10-22 11:40:38 +00:00
|
|
|
|
|
|
|
error = vm_mmap(kernel_map, /* map */
|
|
|
|
(vm_offset_t *) & igz->inbuf, /* address */
|
|
|
|
PAGE_SIZE, /* size */
|
|
|
|
VM_PROT_READ, /* protection */
|
|
|
|
VM_PROT_READ, /* max protection */
|
|
|
|
0, /* flags */
|
1995-11-06 12:52:37 +00:00
|
|
|
(caddr_t) igz->ip->vp, /* vnode */
|
1994-10-22 11:40:38 +00:00
|
|
|
igz->offset); /* offset */
|
|
|
|
if (error) {
|
|
|
|
igz->where = __LINE__;
|
|
|
|
igz->error = error;
|
|
|
|
return GZ_EOF;
|
|
|
|
}
|
|
|
|
return igz->inbuf[(igz->idx++) - igz->offset];
|
1994-10-03 05:23:01 +00:00
|
|
|
}
|
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
static int
|
|
|
|
Flush(void *vp, u_char * ptr, u_long siz)
|
|
|
|
{
|
|
|
|
struct imgact_gzip *gz = (struct imgact_gzip *) vp;
|
|
|
|
u_char *p = ptr, *q;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* First, find a a.out-header */
|
|
|
|
if (gz->output < sizeof gz->a_out) {
|
|
|
|
q = (u_char *) & gz->a_out;
|
|
|
|
i = min(siz, sizeof gz->a_out - gz->output);
|
|
|
|
bcopy(p, q + gz->output, i);
|
|
|
|
gz->output += i;
|
|
|
|
p += i;
|
|
|
|
siz -= i;
|
|
|
|
if (gz->output == sizeof gz->a_out) {
|
|
|
|
i = do_aout_hdr(gz);
|
|
|
|
if (i == -1) {
|
1994-10-22 11:55:16 +00:00
|
|
|
if (!gz->where)
|
|
|
|
gz->where = __LINE__;
|
1994-10-22 11:40:38 +00:00
|
|
|
gz->error = ENOEXEC;
|
|
|
|
return ENOEXEC;
|
|
|
|
} else if (i) {
|
|
|
|
gz->where = __LINE__;
|
|
|
|
gz->error = i;
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
|
|
|
if (gz->file_offset < sizeof gz->a_out) {
|
|
|
|
q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset;
|
|
|
|
bcopy(&gz->a_out, q, sizeof gz->a_out - gz->file_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Skip over zero-padded first PAGE if needed */
|
|
|
|
if (gz->output < gz->file_offset && (gz->output + siz) > gz->file_offset) {
|
|
|
|
i = min(siz, gz->file_offset - gz->output);
|
|
|
|
gz->output += i;
|
|
|
|
p += i;
|
|
|
|
siz -= i;
|
|
|
|
}
|
|
|
|
if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
|
|
|
|
i = min(siz, gz->file_end - gz->output);
|
|
|
|
q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset;
|
|
|
|
bcopy(p, q, i);
|
|
|
|
gz->output += i;
|
|
|
|
p += i;
|
|
|
|
siz -= i;
|
|
|
|
}
|
|
|
|
gz->output += siz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-10-03 05:23:01 +00:00
|
|
|
/*
|
|
|
|
* Tell kern_execve.c about it, with a little help from the linker.
|
|
|
|
* Since `const' objects end up in the text segment, TEXT_SET is the
|
|
|
|
* correct directive to use.
|
|
|
|
*/
|
|
|
|
|
1994-10-22 11:40:38 +00:00
|
|
|
static const struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
|
|
|
|
TEXT_SET(execsw_set, gzip_execsw);
|