2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> 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 * ----------------------------------------------------------------------------
10 * This module handles execution of a.out files which have been run through
11 * "gzip". This saves diskspace, but wastes cpu-cycles and VM.
14 * text-segments should be made R/O after being filled
15 * is the vm-stuff safe ?
16 * should handle the entire header of gzip'ed stuff.
17 * inflate isn't quite reentrant yet...
18 * error-handling is a mess...
20 * tidy up unnecesary includes
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/imgact.h>
29 #include <sys/imgact_aout.h>
30 #include <sys/kernel.h>
33 #include <sys/mutex.h>
35 #include <sys/resourcevar.h>
36 #include <sys/sysent.h>
37 #include <sys/systm.h>
38 #include <sys/vnode.h>
39 #include <sys/inflate.h>
42 #include <vm/vm_param.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_extern.h>
49 struct image_params *ip;
59 u_long virtual_offset, file_offset, file_end, bss_size;
62 static int exec_gzip_imgact(struct image_params *imgp);
63 static int NextByte(void *vp);
64 static int do_aout_hdr(struct imgact_gzip *);
65 static int Flush(void *vp, u_char *, u_long siz);
68 exec_gzip_imgact(imgp)
69 struct image_params *imgp;
71 int error, error2 = 0;
72 const u_char *p = (const u_char *) imgp->image_header;
73 struct imgact_gzip igz;
75 struct vmspace *vmspace;
77 /* If these four are not OK, it isn't a gzip file */
79 return -1; /* 0 Simply magic */
81 return -1; /* 1 Simply magic */
83 return -1; /* 2 Compression method */
85 return -1; /* 9 OS compressed on */
88 * If this one contains anything but a comment or a filename marker,
89 * we don't want to chew on it
92 return ENOEXEC; /* 3 Flags */
94 /* These are of no use to us */
98 bzero(&igz, sizeof igz);
99 bzero(&infl, sizeof infl);
100 infl.gz_private = (void *) &igz;
101 infl.gz_input = NextByte;
102 infl.gz_output = Flush;
107 if (p[3] & 0x08) { /* skip a filename */
109 if (igz.idx >= PAGE_SIZE)
112 if (p[3] & 0x10) { /* skip a comment */
114 if (igz.idx >= PAGE_SIZE)
117 igz.len = imgp->attr->va_size;
119 error = inflate(&infl);
122 * The unzipped file may not even have been long enough to contain
123 * a header giving Flush() a chance to return error. Check for this.
125 if ( !igz.gotheader )
129 vmspace = imgp->proc->p_vmspace;
130 error = vm_map_protect(&vmspace->vm_map,
131 (vm_offset_t) vmspace->vm_taddr,
132 (vm_offset_t) (vmspace->vm_taddr +
133 (vmspace->vm_tsize << PAGE_SHIFT)) ,
134 VM_PROT_READ|VM_PROT_EXECUTE,0);
139 vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
140 (vm_offset_t) igz.inbuf + PAGE_SIZE);
142 if (igz.error || error || error2) {
143 printf("Output=%lu ", igz.output);
144 printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
145 error, igz.error, error2, igz.where);
157 do_aout_hdr(struct imgact_gzip * gz)
160 struct vmspace *vmspace;
164 * Set file/virtual offset based on a.out variant. We do two cases:
165 * host byte order and network byte order (for NetBSD compatibility)
167 switch ((int) (gz->a_out.a_magic & 0xffff)) {
169 gz->virtual_offset = 0;
170 if (gz->a_out.a_text) {
171 gz->file_offset = PAGE_SIZE;
173 /* Bill's "screwball mode" */
178 gz->virtual_offset = PAGE_SIZE;
182 /* NetBSD compatibility */
183 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
186 gz->virtual_offset = PAGE_SIZE;
190 gz->where = __LINE__;
195 gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
198 * Check various fields in header for validity/bounds.
200 if ( /* entry point must lay with text region */
201 gz->a_out.a_entry < gz->virtual_offset ||
202 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
204 /* text and data size must each be page rounded */
205 gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
206 gz->where = __LINE__;
210 * text/data/bss must not exceed limits
212 PROC_LOCK(gz->ip->proc);
213 if ( /* text can't exceed maximum text size */
214 gz->a_out.a_text > maxtsiz ||
216 /* data + bss can't exceed rlimit */
217 gz->a_out.a_data + gz->bss_size >
218 lim_cur(gz->ip->proc, RLIMIT_DATA)) {
219 PROC_UNLOCK(gz->ip->proc);
220 gz->where = __LINE__;
223 PROC_UNLOCK(gz->ip->proc);
224 /* Find out how far we should go */
225 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
227 /* copy in arguments and/or environment from old process */
228 error = exec_extract_strings(gz->ip);
230 gz->where = __LINE__;
234 * Destroy old process VM and create a new one (with a new stack)
236 exec_new_vmspace(gz->ip, &aout_sysvec);
238 vmspace = gz->ip->proc->p_vmspace;
240 vmaddr = gz->virtual_offset;
242 error = vm_mmap(&vmspace->vm_map,
244 gz->a_out.a_text + gz->a_out.a_data,
245 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
250 gz->where = __LINE__;
254 if (gz->bss_size != 0) {
256 * Allocate demand-zeroed area for uninitialized data.
257 * "bss" = 'block started by symbol' - named after the
258 * IBM 7090 instruction of the same name.
260 vmaddr = gz->virtual_offset + gz->a_out.a_text +
262 error = vm_map_find(&vmspace->vm_map,
267 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
269 gz->where = __LINE__;
273 /* Fill in process VM information */
274 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
275 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
276 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
277 vmspace->vm_daddr = (caddr_t) (uintptr_t)
278 (gz->virtual_offset + gz->a_out.a_text);
280 /* Fill in image_params */
281 gz->ip->interpreted = 0;
282 gz->ip->entry_addr = gz->a_out.a_entry;
284 gz->ip->proc->p_sysent = &aout_sysvec;
293 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
295 if (igz->idx >= igz->len) {
296 igz->where = __LINE__;
299 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
300 return igz->inbuf[(igz->idx++) - igz->offset];
303 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
304 (vm_offset_t) igz->inbuf + PAGE_SIZE);
306 igz->where = __LINE__;
311 igz->offset = igz->idx & ~PAGE_MASK;
313 error = vm_mmap(kernel_map, /* map */
314 (vm_offset_t *) & igz->inbuf, /* address */
315 PAGE_SIZE, /* size */
316 VM_PROT_READ, /* protection */
317 VM_PROT_READ, /* max protection */
319 (caddr_t) igz->ip->vp, /* vnode */
320 igz->offset); /* offset */
322 igz->where = __LINE__;
326 return igz->inbuf[(igz->idx++) - igz->offset];
330 Flush(void *vp, u_char * ptr, u_long siz)
332 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
336 /* First, find an a.out-header. */
337 if (gz->output < sizeof gz->a_out) {
338 q = (u_char *) & gz->a_out;
339 i = min(siz, sizeof gz->a_out - gz->output);
340 bcopy(p, q + gz->output, i);
344 if (gz->output == sizeof gz->a_out) {
349 gz->where = __LINE__;
353 gz->where = __LINE__;
357 if (gz->file_offset == 0) {
358 q = (u_char *) (uintptr_t) gz->virtual_offset;
359 copyout(&gz->a_out, q, sizeof gz->a_out);
363 /* Skip over zero-padded first PAGE if needed */
364 if (gz->output < gz->file_offset &&
365 gz->output + siz > gz->file_offset) {
366 i = min(siz, gz->file_offset - gz->output);
371 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
372 i = min(siz, gz->file_end - gz->output);
373 q = (u_char *) (uintptr_t)
374 (gz->virtual_offset + gz->output - gz->file_offset);
386 * Tell kern_execve.c about it, with a little help from the linker.
388 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
389 EXEC_SET(execgzip, gzip_execsw);