2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
11 * This module handles execution of a.out files which have been run through
12 * "gzip". This saves diskspace, but wastes cpu-cycles and VM.
15 * text-segments should be made R/O after being filled
16 * is the vm-stuff safe ?
17 * should handle the entire header of gzip'ed stuff.
18 * inflate isn't quite reentrant yet...
19 * error-handling is a mess...
21 * tidy up unnecesary includes
24 #include <sys/param.h>
26 #include <sys/imgact.h>
27 #include <sys/imgact_aout.h>
28 #include <sys/kernel.h>
31 #include <sys/mutex.h>
33 #include <sys/resourcevar.h>
34 #include <sys/sysent.h>
35 #include <sys/systm.h>
36 #include <sys/vnode.h>
37 #include <sys/inflate.h>
40 #include <vm/vm_param.h>
42 #include <vm/vm_map.h>
43 #include <vm/vm_kern.h>
44 #include <vm/vm_extern.h>
47 struct image_params *ip;
57 u_long virtual_offset, file_offset, file_end, bss_size;
60 static int exec_gzip_imgact(struct image_params *imgp);
61 static int NextByte(void *vp);
62 static int do_aout_hdr(struct imgact_gzip *);
63 static int Flush(void *vp, u_char *, u_long siz);
66 exec_gzip_imgact(imgp)
67 struct image_params *imgp;
69 int error, error2 = 0;
70 const u_char *p = (const u_char *) imgp->image_header;
71 struct imgact_gzip igz;
73 struct vmspace *vmspace;
75 /* If these four are not OK, it isn't a gzip file */
77 return -1; /* 0 Simply magic */
79 return -1; /* 1 Simply magic */
81 return -1; /* 2 Compression method */
83 return -1; /* 9 OS compressed on */
86 * If this one contains anything but a comment or a filename marker,
87 * we don't want to chew on it
90 return ENOEXEC; /* 3 Flags */
92 /* These are of no use to us */
96 bzero(&igz, sizeof igz);
97 bzero(&infl, sizeof infl);
98 infl.gz_private = (void *) &igz;
99 infl.gz_input = NextByte;
100 infl.gz_output = Flush;
105 if (p[3] & 0x08) { /* skip a filename */
107 if (igz.idx >= PAGE_SIZE)
110 if (p[3] & 0x10) { /* skip a comment */
112 if (igz.idx >= PAGE_SIZE)
115 igz.len = imgp->attr->va_size;
117 error = inflate(&infl);
120 * The unzipped file may not even have been long enough to contain
121 * a header giving Flush() a chance to return error. Check for this.
123 if ( !igz.gotheader )
127 vmspace = imgp->proc->p_vmspace;
128 error = vm_map_protect(&vmspace->vm_map,
129 (vm_offset_t) vmspace->vm_taddr,
130 (vm_offset_t) (vmspace->vm_taddr +
131 (vmspace->vm_tsize << PAGE_SHIFT)) ,
132 VM_PROT_READ|VM_PROT_EXECUTE,0);
137 vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
138 (vm_offset_t) igz.inbuf + PAGE_SIZE);
140 if (igz.error || error || error2) {
141 printf("Output=%lu ", igz.output);
142 printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
143 error, igz.error, error2, igz.where);
155 do_aout_hdr(struct imgact_gzip * gz)
158 struct vmspace *vmspace;
162 * Set file/virtual offset based on a.out variant. We do two cases:
163 * host byte order and network byte order (for NetBSD compatibility)
165 switch ((int) (gz->a_out.a_magic & 0xffff)) {
167 gz->virtual_offset = 0;
168 if (gz->a_out.a_text) {
169 gz->file_offset = PAGE_SIZE;
171 /* Bill's "screwball mode" */
176 gz->virtual_offset = PAGE_SIZE;
180 /* NetBSD compatibility */
181 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
184 gz->virtual_offset = PAGE_SIZE;
188 gz->where = __LINE__;
193 gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
196 * Check various fields in header for validity/bounds.
198 if ( /* entry point must lay with text region */
199 gz->a_out.a_entry < gz->virtual_offset ||
200 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
202 /* text and data size must each be page rounded */
203 gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
204 gz->where = __LINE__;
208 * text/data/bss must not exceed limits
210 mtx_assert(&Giant, MA_OWNED);
211 if ( /* text can't exceed maximum text size */
212 gz->a_out.a_text > maxtsiz ||
214 /* data + bss can't exceed rlimit */
215 gz->a_out.a_data + gz->bss_size >
216 gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
217 gz->where = __LINE__;
220 /* Find out how far we should go */
221 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
223 /* copy in arguments and/or environment from old process */
224 error = exec_extract_strings(gz->ip);
226 gz->where = __LINE__;
230 * Destroy old process VM and create a new one (with a new stack)
232 exec_new_vmspace(gz->ip);
234 vmspace = gz->ip->proc->p_vmspace;
236 vmaddr = gz->virtual_offset;
238 error = vm_mmap(&vmspace->vm_map,
240 gz->a_out.a_text + gz->a_out.a_data,
241 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
246 gz->where = __LINE__;
250 if (gz->bss_size != 0) {
252 * Allocate demand-zeroed area for uninitialized data.
253 * "bss" = 'block started by symbol' - named after the
254 * IBM 7090 instruction of the same name.
256 vmaddr = gz->virtual_offset + gz->a_out.a_text +
258 error = vm_map_find(&vmspace->vm_map,
263 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
265 gz->where = __LINE__;
269 /* Fill in process VM information */
270 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
271 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
272 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
273 vmspace->vm_daddr = (caddr_t) (uintptr_t)
274 (gz->virtual_offset + gz->a_out.a_text);
276 /* Fill in image_params */
277 gz->ip->interpreted = 0;
278 gz->ip->entry_addr = gz->a_out.a_entry;
280 gz->ip->proc->p_sysent = &aout_sysvec;
289 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
291 if (igz->idx >= igz->len) {
292 igz->where = __LINE__;
295 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
296 return igz->inbuf[(igz->idx++) - igz->offset];
299 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
300 (vm_offset_t) igz->inbuf + PAGE_SIZE);
302 igz->where = __LINE__;
307 igz->offset = igz->idx & ~PAGE_MASK;
309 error = vm_mmap(kernel_map, /* map */
310 (vm_offset_t *) & igz->inbuf, /* address */
311 PAGE_SIZE, /* size */
312 VM_PROT_READ, /* protection */
313 VM_PROT_READ, /* max protection */
315 (caddr_t) igz->ip->vp, /* vnode */
316 igz->offset); /* offset */
318 igz->where = __LINE__;
322 return igz->inbuf[(igz->idx++) - igz->offset];
326 Flush(void *vp, u_char * ptr, u_long siz)
328 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
332 /* First, find a a.out-header */
333 if (gz->output < sizeof gz->a_out) {
334 q = (u_char *) & gz->a_out;
335 i = min(siz, sizeof gz->a_out - gz->output);
336 bcopy(p, q + gz->output, i);
340 if (gz->output == sizeof gz->a_out) {
345 gz->where = __LINE__;
349 gz->where = __LINE__;
353 if (gz->file_offset == 0) {
354 q = (u_char *) (uintptr_t) gz->virtual_offset;
355 copyout(&gz->a_out, q, sizeof gz->a_out);
359 /* Skip over zero-padded first PAGE if needed */
360 if (gz->output < gz->file_offset &&
361 gz->output + siz > gz->file_offset) {
362 i = min(siz, gz->file_offset - gz->output);
367 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
368 i = min(siz, gz->file_end - gz->output);
369 q = (u_char *) (uintptr_t)
370 (gz->virtual_offset + gz->output - gz->file_offset);
382 * Tell kern_execve.c about it, with a little help from the linker.
384 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
385 EXEC_SET(execgzip, gzip_execsw);