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