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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/kern/imgact_gzip.c,v 1.40.2.1 2001/11/03 01:41:08 ps Exp $
10 * $DragonFly: src/sys/kern/imgact_gzip.c,v 1.8 2006/12/23 23:47:54 swildner Exp $
12 * This module handles execution of a.out files which have been run through
13 * "gzip". This saves diskspace, but wastes cpu-cycles and VM.
16 * text-segments should be made R/O after being filled
17 * is the vm-stuff safe ?
18 * should handle the entire header of gzip'ed stuff.
19 * inflate isn't quite reentrant yet...
20 * error-handling is a mess...
22 * tidy up unnecesary includes
25 #include <sys/param.h>
27 #include <sys/imgact.h>
28 #include <sys/imgact_aout.h>
29 #include <sys/kernel.h>
32 #include <sys/resourcevar.h>
33 #include <sys/sysent.h>
34 #include <sys/systm.h>
35 #include <sys/vnode.h>
36 #include <sys/inflate.h>
39 #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(struct image_params *imgp)
68 int error, error2 = 0;
69 const u_char *p = (const u_char *) imgp->image_header;
70 struct imgact_gzip igz;
72 struct vmspace *vmspace;
74 /* If these four are not OK, it isn't a gzip file */
76 return -1; /* 0 Simply magic */
78 return -1; /* 1 Simply magic */
80 return -1; /* 2 Compression method */
82 return -1; /* 9 OS compressed on */
85 * If this one contains anything but a comment or a filename marker,
86 * we don't want to chew on it
89 return ENOEXEC; /* 3 Flags */
91 /* These are of no use to us */
95 bzero(&igz, sizeof igz);
96 bzero(&infl, sizeof infl);
97 infl.gz_private = (void *) &igz;
98 infl.gz_input = NextByte;
99 infl.gz_output = Flush;
104 if (p[3] & 0x08) { /* skip a filename */
106 if (igz.idx >= PAGE_SIZE)
109 if (p[3] & 0x10) { /* skip a comment */
111 if (igz.idx >= PAGE_SIZE)
114 igz.len = imgp->attr->va_size;
116 error = inflate(&infl);
119 * The unzipped file may not even have been long enough to contain
120 * a header giving Flush() a chance to return error. Check for this.
122 if ( !igz.gotheader )
126 vmspace = imgp->proc->p_vmspace;
127 error = vm_map_protect(&vmspace->vm_map,
128 (vm_offset_t) vmspace->vm_taddr,
129 (vm_offset_t) (vmspace->vm_taddr +
130 (vmspace->vm_tsize << PAGE_SHIFT)) ,
131 VM_PROT_READ|VM_PROT_EXECUTE,0);
136 vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
137 (vm_offset_t) igz.inbuf + PAGE_SIZE);
139 if (igz.error || error || error2) {
140 kprintf("Output=%lu ", igz.output);
141 kprintf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
142 error, igz.error, error2, igz.where);
154 do_aout_hdr(struct imgact_gzip * gz)
157 struct vmspace *vmspace;
161 * Set file/virtual offset based on a.out variant. We do two cases:
162 * host byte order and network byte order (for NetBSD compatibility)
164 switch ((int) (gz->a_out.a_magic & 0xffff)) {
166 gz->virtual_offset = 0;
167 if (gz->a_out.a_text) {
168 gz->file_offset = PAGE_SIZE;
170 /* Bill's "screwball mode" */
175 gz->virtual_offset = PAGE_SIZE;
179 /* NetBSD compatibility */
180 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
183 gz->virtual_offset = PAGE_SIZE;
187 gz->where = __LINE__;
192 gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
195 * Check various fields in header for validity/bounds.
197 if ( /* entry point must lay with text region */
198 gz->a_out.a_entry < gz->virtual_offset ||
199 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
201 /* text and data size must each be page rounded */
202 gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
203 gz->where = __LINE__;
207 * text/data/bss must not exceed limits
209 if ( /* text can't exceed maximum text size */
210 gz->a_out.a_text > maxtsiz ||
212 /* data + bss can't exceed rlimit */
213 gz->a_out.a_data + gz->bss_size >
214 gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
215 gz->where = __LINE__;
218 /* Find out how far we should go */
219 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
222 * Destroy old process VM and create a new one (with a new stack)
224 exec_new_vmspace(gz->ip, NULL);
226 vmspace = gz->ip->proc->p_vmspace;
228 vmaddr = gz->virtual_offset;
230 error = vm_mmap(&vmspace->vm_map,
232 gz->a_out.a_text + gz->a_out.a_data,
233 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
238 gz->where = __LINE__;
242 if (gz->bss_size != 0) {
244 * Allocate demand-zeroed area for uninitialized data.
245 * "bss" = 'block started by symbol' - named after the
246 * IBM 7090 instruction of the same name.
248 vmaddr = gz->virtual_offset + gz->a_out.a_text +
250 error = vm_map_find(&vmspace->vm_map,
252 &vmaddr, gz->bss_size,
255 VM_PROT_ALL, VM_PROT_ALL,
258 gz->where = __LINE__;
262 /* Fill in process VM information */
263 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
264 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
265 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
266 vmspace->vm_daddr = (caddr_t) (uintptr_t)
267 (gz->virtual_offset + gz->a_out.a_text);
269 /* Fill in image_params */
270 gz->ip->interpreted = 0;
271 gz->ip->entry_addr = gz->a_out.a_entry;
273 gz->ip->proc->p_sysent = &aout_sysvec;
282 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
284 if (igz->idx >= igz->len) {
285 igz->where = __LINE__;
288 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
289 return igz->inbuf[(igz->idx++) - igz->offset];
292 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
293 (vm_offset_t) igz->inbuf + PAGE_SIZE);
295 igz->where = __LINE__;
300 igz->offset = igz->idx & ~PAGE_MASK;
302 error = vm_mmap(kernel_map, /* map */
303 (vm_offset_t *) & igz->inbuf, /* address */
304 PAGE_SIZE, /* size */
305 VM_PROT_READ, /* protection */
306 VM_PROT_READ, /* max protection */
308 (caddr_t) igz->ip->vp, /* vnode */
309 igz->offset); /* offset */
311 igz->where = __LINE__;
315 return igz->inbuf[(igz->idx++) - igz->offset];
319 Flush(void *vp, u_char * ptr, u_long siz)
321 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
325 /* First, find a a.out-header */
326 if (gz->output < sizeof gz->a_out) {
327 q = (u_char *) & gz->a_out;
328 i = min(siz, sizeof gz->a_out - gz->output);
329 bcopy(p, q + gz->output, i);
333 if (gz->output == sizeof gz->a_out) {
338 gz->where = __LINE__;
342 gz->where = __LINE__;
346 if (gz->file_offset == 0) {
347 q = (u_char *) (uintptr_t) gz->virtual_offset;
348 copyout(&gz->a_out, q, sizeof gz->a_out);
352 /* Skip over zero-padded first PAGE if needed */
353 if (gz->output < gz->file_offset &&
354 gz->output + siz > gz->file_offset) {
355 i = min(siz, gz->file_offset - gz->output);
360 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
361 i = min(siz, gz->file_end - gz->output);
362 q = (u_char *) (uintptr_t)
363 (gz->virtual_offset + gz->output - gz->file_offset);
375 * Tell kern_execve.c about it, with a little help from the linker.
377 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
378 EXEC_SET(execgzip, gzip_execsw);