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