2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> 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 * ----------------------------------------------------------------------------
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.
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...
21 * tidy up unnecesary includes
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include <sys/param.h>
29 #include <sys/imgact.h>
30 #include <sys/imgact_aout.h>
31 #include <sys/kernel.h>
34 #include <sys/mutex.h>
36 #include <sys/racct.h>
37 #include <sys/resourcevar.h>
38 #include <sys/sysent.h>
39 #include <sys/systm.h>
40 #include <sys/vnode.h>
41 #include <sys/inflate.h>
44 #include <vm/vm_param.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_extern.h>
51 struct image_params *ip;
61 u_long virtual_offset, file_offset, file_end, bss_size;
64 static int exec_gzip_imgact(struct image_params *imgp);
65 static int NextByte(void *vp);
66 static int do_aout_hdr(struct imgact_gzip *);
67 static int Flush(void *vp, u_char *, u_long siz);
70 exec_gzip_imgact(imgp)
71 struct image_params *imgp;
73 int error, error2 = 0;
74 const u_char *p = (const u_char *) imgp->image_header;
75 struct imgact_gzip igz;
77 struct vmspace *vmspace;
79 /* If these four are not OK, it isn't a gzip file */
81 return -1; /* 0 Simply magic */
83 return -1; /* 1 Simply magic */
85 return -1; /* 2 Compression method */
87 return -1; /* 9 OS compressed on */
90 * If this one contains anything but a comment or a filename marker,
91 * we don't want to chew on it
94 return ENOEXEC; /* 3 Flags */
96 /* These are of no use to us */
100 bzero(&igz, sizeof igz);
101 bzero(&infl, sizeof infl);
102 infl.gz_private = (void *) &igz;
103 infl.gz_input = NextByte;
104 infl.gz_output = Flush;
109 if (p[3] & 0x08) { /* skip a filename */
111 if (igz.idx >= PAGE_SIZE)
114 if (p[3] & 0x10) { /* skip a comment */
116 if (igz.idx >= PAGE_SIZE)
119 igz.len = imgp->attr->va_size;
121 error = inflate(&infl);
124 * The unzipped file may not even have been long enough to contain
125 * a header giving Flush() a chance to return error. Check for this.
127 if ( !igz.gotheader )
131 vmspace = imgp->proc->p_vmspace;
132 error = vm_map_protect(&vmspace->vm_map,
133 (vm_offset_t) vmspace->vm_taddr,
134 (vm_offset_t) (vmspace->vm_taddr +
135 (vmspace->vm_tsize << PAGE_SHIFT)) ,
136 VM_PROT_READ|VM_PROT_EXECUTE,0);
141 vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
142 (vm_offset_t) igz.inbuf + PAGE_SIZE);
144 if (igz.error || error || error2) {
145 printf("Output=%lu ", igz.output);
146 printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
147 error, igz.error, error2, igz.where);
159 do_aout_hdr(struct imgact_gzip * gz)
162 struct vmspace *vmspace;
166 * Set file/virtual offset based on a.out variant. We do two cases:
167 * host byte order and network byte order (for NetBSD compatibility)
169 switch ((int) (gz->a_out.a_magic & 0xffff)) {
171 gz->virtual_offset = 0;
172 if (gz->a_out.a_text) {
173 gz->file_offset = PAGE_SIZE;
175 /* Bill's "screwball mode" */
180 gz->virtual_offset = PAGE_SIZE;
184 /* NetBSD compatibility */
185 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
188 gz->virtual_offset = PAGE_SIZE;
192 gz->where = __LINE__;
197 gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
200 * Check various fields in header for validity/bounds.
202 if ( /* entry point must lay with text region */
203 gz->a_out.a_entry < gz->virtual_offset ||
204 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
206 /* text and data size must each be page rounded */
207 gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
208 gz->where = __LINE__;
212 * text/data/bss must not exceed limits
214 PROC_LOCK(gz->ip->proc);
215 if ( /* text can't exceed maximum text size */
216 gz->a_out.a_text > maxtsiz ||
218 /* data + bss can't exceed rlimit */
219 gz->a_out.a_data + gz->bss_size >
220 lim_cur(gz->ip->proc, RLIMIT_DATA) ||
221 racct_set(gz->ip->proc, RACCT_DATA,
222 gz->a_out.a_data + gz->bss_size) != 0) {
223 PROC_UNLOCK(gz->ip->proc);
224 gz->where = __LINE__;
227 PROC_UNLOCK(gz->ip->proc);
228 /* Find out how far we should go */
229 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
232 * Avoid a possible deadlock if the current address space is destroyed
233 * and that address space maps the locked vnode. In the common case,
234 * the locked vnode's v_usecount is decremented but remains greater
235 * than zero. Consequently, the vnode lock is not needed by vrele().
236 * However, in cases where the vnode lock is external, such as nullfs,
237 * v_usecount may become zero.
239 VOP_UNLOCK(gz->ip->vp, 0);
242 * Destroy old process VM and create a new one (with a new stack)
244 error = exec_new_vmspace(gz->ip, &aout_sysvec);
246 vn_lock(gz->ip->vp, LK_EXCLUSIVE | LK_RETRY);
248 gz->where = __LINE__;
252 vmspace = gz->ip->proc->p_vmspace;
254 vmaddr = gz->virtual_offset;
256 error = vm_mmap(&vmspace->vm_map,
258 gz->a_out.a_text + gz->a_out.a_data,
259 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
265 gz->where = __LINE__;
269 if (gz->bss_size != 0) {
271 * Allocate demand-zeroed area for uninitialized data.
272 * "bss" = 'block started by symbol' - named after the
273 * IBM 7090 instruction of the same name.
275 vmaddr = gz->virtual_offset + gz->a_out.a_text +
277 error = vm_map_find(&vmspace->vm_map,
282 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
284 gz->where = __LINE__;
288 /* Fill in process VM information */
289 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
290 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
291 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
292 vmspace->vm_daddr = (caddr_t) (uintptr_t)
293 (gz->virtual_offset + gz->a_out.a_text);
295 /* Fill in image_params */
296 gz->ip->interpreted = 0;
297 gz->ip->entry_addr = gz->a_out.a_entry;
299 gz->ip->proc->p_sysent = &aout_sysvec;
308 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
310 if (igz->idx >= igz->len) {
311 igz->where = __LINE__;
314 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
315 return igz->inbuf[(igz->idx++) - igz->offset];
318 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
319 (vm_offset_t) igz->inbuf + PAGE_SIZE);
321 igz->where = __LINE__;
326 igz->offset = igz->idx & ~PAGE_MASK;
328 error = vm_mmap(kernel_map, /* map */
329 (vm_offset_t *) & igz->inbuf, /* address */
330 PAGE_SIZE, /* size */
331 VM_PROT_READ, /* protection */
332 VM_PROT_READ, /* max protection */
334 OBJT_VNODE, /* handle type */
335 igz->ip->vp, /* vnode */
336 igz->offset); /* offset */
338 igz->where = __LINE__;
342 return igz->inbuf[(igz->idx++) - igz->offset];
346 Flush(void *vp, u_char * ptr, u_long siz)
348 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
352 /* First, find an a.out-header. */
353 if (gz->output < sizeof gz->a_out) {
354 q = (u_char *) & gz->a_out;
355 i = min(siz, sizeof gz->a_out - gz->output);
356 bcopy(p, q + gz->output, i);
360 if (gz->output == sizeof gz->a_out) {
365 gz->where = __LINE__;
369 gz->where = __LINE__;
373 if (gz->file_offset == 0) {
374 q = (u_char *) (uintptr_t) gz->virtual_offset;
375 copyout(&gz->a_out, q, sizeof gz->a_out);
379 /* Skip over zero-padded first PAGE if needed */
380 if (gz->output < gz->file_offset &&
381 gz->output + siz > gz->file_offset) {
382 i = min(siz, gz->file_offset - gz->output);
387 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
388 i = min(siz, gz->file_end - gz->output);
389 q = (u_char *) (uintptr_t)
390 (gz->virtual_offset + gz->output - gz->file_offset);
402 * Tell kern_execve.c about it, with a little help from the linker.
404 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
405 EXEC_SET(execgzip, gzip_execsw);