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