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 mtx_assert(&Giant, MA_OWNED);
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 gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
219 gz->where = __LINE__;
222 /* Find out how far we should go */
223 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
225 /* copy in arguments and/or environment from old process */
226 error = exec_extract_strings(gz->ip);
228 gz->where = __LINE__;
232 * Destroy old process VM and create a new one (with a new stack)
234 exec_new_vmspace(gz->ip, &aout_sysvec);
236 vmspace = gz->ip->proc->p_vmspace;
238 vmaddr = gz->virtual_offset;
240 error = vm_mmap(&vmspace->vm_map,
242 gz->a_out.a_text + gz->a_out.a_data,
243 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
248 gz->where = __LINE__;
252 if (gz->bss_size != 0) {
254 * Allocate demand-zeroed area for uninitialized data.
255 * "bss" = 'block started by symbol' - named after the
256 * IBM 7090 instruction of the same name.
258 vmaddr = gz->virtual_offset + gz->a_out.a_text +
260 error = vm_map_find(&vmspace->vm_map,
265 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
267 gz->where = __LINE__;
271 /* Fill in process VM information */
272 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
273 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
274 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
275 vmspace->vm_daddr = (caddr_t) (uintptr_t)
276 (gz->virtual_offset + gz->a_out.a_text);
278 /* Fill in image_params */
279 gz->ip->interpreted = 0;
280 gz->ip->entry_addr = gz->a_out.a_entry;
282 gz->ip->proc->p_sysent = &aout_sysvec;
291 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
293 if (igz->idx >= igz->len) {
294 igz->where = __LINE__;
297 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
298 return igz->inbuf[(igz->idx++) - igz->offset];
301 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
302 (vm_offset_t) igz->inbuf + PAGE_SIZE);
304 igz->where = __LINE__;
309 igz->offset = igz->idx & ~PAGE_MASK;
311 error = vm_mmap(kernel_map, /* map */
312 (vm_offset_t *) & igz->inbuf, /* address */
313 PAGE_SIZE, /* size */
314 VM_PROT_READ, /* protection */
315 VM_PROT_READ, /* max protection */
317 (caddr_t) igz->ip->vp, /* vnode */
318 igz->offset); /* offset */
320 igz->where = __LINE__;
324 return igz->inbuf[(igz->idx++) - igz->offset];
328 Flush(void *vp, u_char * ptr, u_long siz)
330 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
334 /* First, find an a.out-header. */
335 if (gz->output < sizeof gz->a_out) {
336 q = (u_char *) & gz->a_out;
337 i = min(siz, sizeof gz->a_out - gz->output);
338 bcopy(p, q + gz->output, i);
342 if (gz->output == sizeof gz->a_out) {
347 gz->where = __LINE__;
351 gz->where = __LINE__;
355 if (gz->file_offset == 0) {
356 q = (u_char *) (uintptr_t) gz->virtual_offset;
357 copyout(&gz->a_out, q, sizeof gz->a_out);
361 /* Skip over zero-padded first PAGE if needed */
362 if (gz->output < gz->file_offset &&
363 gz->output + siz > gz->file_offset) {
364 i = min(siz, gz->file_offset - gz->output);
369 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
370 i = min(siz, gz->file_end - gz->output);
371 q = (u_char *) (uintptr_t)
372 (gz->virtual_offset + gz->output - gz->file_offset);
384 * Tell kern_execve.c about it, with a little help from the linker.
386 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
387 EXEC_SET(execgzip, gzip_execsw);