- GALILEO -> MARVELL
[dragonfly.git] / sys / kern / imgact_gzip.c
1 /*
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  * ----------------------------------------------------------------------------
8  *
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.6 2006/09/11 20:25:01 dillon Exp $
11  *
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.
14  *
15  * TODO:
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...
21  *      so is the rest...
22  *      tidy up unnecesary includes
23  */
24
25 #include <sys/param.h>
26 #include <sys/exec.h>
27 #include <sys/imgact.h>
28 #include <sys/imgact_aout.h>
29 #include <sys/kernel.h>
30 #include <sys/mman.h>
31 #include <sys/proc.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>
37
38 #include <vm/vm.h>
39 #include <vm/vm_param.h>
40 #include <sys/lock.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_map.h>
43 #include <vm/vm_kern.h>
44 #include <vm/vm_extern.h>
45
46 struct imgact_gzip {
47         struct image_params *ip;
48         struct exec     a_out;
49         int             error;
50         int             gotheader;
51         int             where;
52         u_char         *inbuf;
53         u_long          offset;
54         u_long          output;
55         u_long          len;
56         int             idx;
57         u_long          virtual_offset, file_offset, file_end, bss_size;
58 };
59
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);
64
65 static int
66 exec_gzip_imgact(imgp)
67         struct image_params *imgp;
68 {
69         int             error, error2 = 0;
70         const u_char   *p = (const u_char *) imgp->image_header;
71         struct imgact_gzip igz;
72         struct inflate  infl;
73         struct vmspace *vmspace;
74
75         /* If these four are not OK, it isn't a gzip file */
76         if (p[0] != 0x1f)
77                 return -1;      /* 0    Simply magic     */
78         if (p[1] != 0x8b)
79                 return -1;      /* 1    Simply magic     */
80         if (p[2] != 0x08)
81                 return -1;      /* 2    Compression method       */
82         if (p[9] != 0x03)
83                 return -1;      /* 9    OS compressed on         */
84
85         /*
86          * If this one contains anything but a comment or a filename marker,
87          * we don't want to chew on it
88          */
89         if (p[3] & ~(0x18))
90                 return ENOEXEC; /* 3    Flags            */
91
92         /* These are of no use to us */
93         /* 4-7  Timestamp                */
94         /* 8    Extra flags              */
95
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;
101
102         igz.ip = imgp;
103         igz.idx = 10;
104
105         if (p[3] & 0x08) {      /* skip a filename */
106                 while (p[igz.idx++])
107                         if (igz.idx >= PAGE_SIZE)
108                                 return ENOEXEC;
109         }
110         if (p[3] & 0x10) {      /* skip a comment */
111                 while (p[igz.idx++])
112                         if (igz.idx >= PAGE_SIZE)
113                                 return ENOEXEC;
114         }
115         igz.len = imgp->attr->va_size;
116
117         error = inflate(&infl);
118
119         /*
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.
122          */
123         if ( !igz.gotheader )
124                 return ENOEXEC;
125
126         if ( !error ) {
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);
133         }
134
135         if (igz.inbuf) {
136                 error2 =
137                         vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
138                             (vm_offset_t) igz.inbuf + PAGE_SIZE);
139         }
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);
144         }
145         if (igz.error)
146                 return igz.error;
147         if (error)
148                 return ENOEXEC;
149         if (error2)
150                 return error2;
151         return 0;
152 }
153
154 static int
155 do_aout_hdr(struct imgact_gzip * gz)
156 {
157         int             error;
158         struct vmspace *vmspace;
159         vm_offset_t     vmaddr;
160
161         /*
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)
164          */
165         switch ((int) (gz->a_out.a_magic & 0xffff)) {
166         case ZMAGIC:
167                 gz->virtual_offset = 0;
168                 if (gz->a_out.a_text) {
169                         gz->file_offset = PAGE_SIZE;
170                 } else {
171                         /* Bill's "screwball mode" */
172                         gz->file_offset = 0;
173                 }
174                 break;
175         case QMAGIC:
176                 gz->virtual_offset = PAGE_SIZE;
177                 gz->file_offset = 0;
178                 break;
179         default:
180                 /* NetBSD compatibility */
181                 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
182                 case ZMAGIC:
183                 case QMAGIC:
184                         gz->virtual_offset = PAGE_SIZE;
185                         gz->file_offset = 0;
186                         break;
187                 default:
188                         gz->where = __LINE__;
189                         return (-1);
190                 }
191         }
192
193         gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
194
195         /*
196          * Check various fields in header for validity/bounds.
197          */
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 ||
201
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__;
205                 return (-1);
206         }
207         /*
208          * text/data/bss must not exceed limits
209          */
210         if (                    /* text can't exceed maximum text size */
211             gz->a_out.a_text > maxtsiz ||
212
213         /* data + bss can't exceed rlimit */
214             gz->a_out.a_data + gz->bss_size >
215             gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
216                 gz->where = __LINE__;
217                 return (ENOMEM);
218         }
219         /* Find out how far we should go */
220         gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
221
222         /*
223          * Destroy old process VM and create a new one (with a new stack)
224          */
225         exec_new_vmspace(gz->ip, NULL);
226
227         vmspace = gz->ip->proc->p_vmspace;
228
229         vmaddr = gz->virtual_offset;
230
231         error = vm_mmap(&vmspace->vm_map,
232                         &vmaddr,
233                         gz->a_out.a_text + gz->a_out.a_data,
234                         VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
235                         0,
236                         0);
237
238         if (error) {
239                 gz->where = __LINE__;
240                 return (error);
241         }
242
243         if (gz->bss_size != 0) {
244                 /*
245                  * Allocate demand-zeroed area for uninitialized data.
246                  * "bss" = 'block started by symbol' - named after the 
247                  * IBM 7090 instruction of the same name.
248                  */
249                 vmaddr = gz->virtual_offset + gz->a_out.a_text + 
250                         gz->a_out.a_data;
251                 error = vm_map_find(&vmspace->vm_map,
252                                     NULL, 0,
253                                     &vmaddr, gz->bss_size,
254                                     FALSE,
255                                     VM_MAPTYPE_NORMAL,
256                                     VM_PROT_ALL, VM_PROT_ALL,
257                                     0);
258                 if (error) {
259                         gz->where = __LINE__;
260                         return (error);
261                 }
262         }
263         /* Fill in process VM information */
264         vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
265         vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
266         vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
267         vmspace->vm_daddr = (caddr_t) (uintptr_t)
268                             (gz->virtual_offset + gz->a_out.a_text);
269
270         /* Fill in image_params */
271         gz->ip->interpreted = 0;
272         gz->ip->entry_addr = gz->a_out.a_entry;
273
274         gz->ip->proc->p_sysent = &aout_sysvec;
275
276         return 0;
277 }
278
279 static int
280 NextByte(void *vp)
281 {
282         int             error;
283         struct imgact_gzip *igz = (struct imgact_gzip *) vp;
284
285         if (igz->idx >= igz->len) {
286                 igz->where = __LINE__;
287                 return GZ_EOF;
288         }
289         if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
290                 return igz->inbuf[(igz->idx++) - igz->offset];
291         }
292         if (igz->inbuf) {
293                 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
294                             (vm_offset_t) igz->inbuf + PAGE_SIZE);
295                 if (error) {
296                         igz->where = __LINE__;
297                         igz->error = error;
298                         return GZ_EOF;
299                 }
300         }
301         igz->offset = igz->idx & ~PAGE_MASK;
302
303         error = vm_mmap(kernel_map,     /* map */
304                         (vm_offset_t *) & igz->inbuf,   /* address */
305                         PAGE_SIZE,      /* size */
306                         VM_PROT_READ,   /* protection */
307                         VM_PROT_READ,   /* max protection */
308                         0,      /* flags */
309                         (caddr_t) igz->ip->vp,  /* vnode */
310                         igz->offset);   /* offset */
311         if (error) {
312                 igz->where = __LINE__;
313                 igz->error = error;
314                 return GZ_EOF;
315         }
316         return igz->inbuf[(igz->idx++) - igz->offset];
317 }
318
319 static int
320 Flush(void *vp, u_char * ptr, u_long siz)
321 {
322         struct imgact_gzip *gz = (struct imgact_gzip *) vp;
323         u_char         *p = ptr, *q;
324         int             i;
325
326         /* First, find a a.out-header */
327         if (gz->output < sizeof gz->a_out) {
328                 q = (u_char *) & gz->a_out;
329                 i = min(siz, sizeof gz->a_out - gz->output);
330                 bcopy(p, q + gz->output, i);
331                 gz->output += i;
332                 p += i;
333                 siz -= i;
334                 if (gz->output == sizeof gz->a_out) {
335                         gz->gotheader = 1;
336                         i = do_aout_hdr(gz);
337                         if (i == -1) {
338                                 if (!gz->where)
339                                         gz->where = __LINE__;
340                                 gz->error = ENOEXEC;
341                                 return ENOEXEC;
342                         } else if (i) {
343                                 gz->where = __LINE__;
344                                 gz->error = i;
345                                 return ENOEXEC;
346                         }
347                         if (gz->file_offset == 0) {
348                                 q = (u_char *) (uintptr_t) gz->virtual_offset;
349                                 copyout(&gz->a_out, q, sizeof gz->a_out);
350                         }
351                 }
352         }
353         /* Skip over zero-padded first PAGE if needed */
354         if (gz->output < gz->file_offset &&
355             gz->output + siz > gz->file_offset) {
356                 i = min(siz, gz->file_offset - gz->output);
357                 gz->output += i;
358                 p += i;
359                 siz -= i;
360         }
361         if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
362                 i = min(siz, gz->file_end - gz->output);
363                 q = (u_char *) (uintptr_t)
364                     (gz->virtual_offset + gz->output - gz->file_offset);
365                 copyout(p, q, i);
366                 gz->output += i;
367                 p += i;
368                 siz -= i;
369         }
370         gz->output += siz;
371         return 0;
372 }
373
374
375 /*
376  * Tell kern_execve.c about it, with a little help from the linker.
377  */
378 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
379 EXEC_SET(execgzip, gzip_execsw);