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/resourcevar.h>
37 #include <sys/sysent.h>
38 #include <sys/systm.h>
39 #include <sys/vnode.h>
40 #include <sys/inflate.h>
43 #include <vm/vm_param.h>
45 #include <vm/vm_map.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_extern.h>
50 struct image_params
*ip
;
60 u_long virtual_offset
, file_offset
, file_end
, bss_size
;
63 static int exec_gzip_imgact(struct image_params
*imgp
);
64 static int NextByte(void *vp
);
65 static int do_aout_hdr(struct imgact_gzip
*);
66 static int Flush(void *vp
, u_char
*, u_long siz
);
69 exec_gzip_imgact(imgp
)
70 struct image_params
*imgp
;
72 int error
, error2
= 0;
73 const u_char
*p
= (const u_char
*) imgp
->image_header
;
74 struct imgact_gzip igz
;
76 struct vmspace
*vmspace
;
78 /* If these four are not OK, it isn't a gzip file */
80 return -1; /* 0 Simply magic */
82 return -1; /* 1 Simply magic */
84 return -1; /* 2 Compression method */
86 return -1; /* 9 OS compressed on */
89 * If this one contains anything but a comment or a filename marker,
90 * we don't want to chew on it
93 return ENOEXEC
; /* 3 Flags */
95 /* These are of no use to us */
99 bzero(&igz
, sizeof igz
);
100 bzero(&infl
, sizeof infl
);
101 infl
.gz_private
= (void *) &igz
;
102 infl
.gz_input
= NextByte
;
103 infl
.gz_output
= Flush
;
108 if (p
[3] & 0x08) { /* skip a filename */
110 if (igz
.idx
>= PAGE_SIZE
)
113 if (p
[3] & 0x10) { /* skip a comment */
115 if (igz
.idx
>= PAGE_SIZE
)
118 igz
.len
= imgp
->attr
->va_size
;
120 error
= inflate(&infl
);
123 * The unzipped file may not even have been long enough to contain
124 * a header giving Flush() a chance to return error. Check for this.
126 if ( !igz
.gotheader
)
130 vmspace
= imgp
->proc
->p_vmspace
;
131 error
= vm_map_protect(&vmspace
->vm_map
,
132 (vm_offset_t
) vmspace
->vm_taddr
,
133 (vm_offset_t
) (vmspace
->vm_taddr
+
134 (vmspace
->vm_tsize
<< PAGE_SHIFT
)) ,
135 VM_PROT_READ
|VM_PROT_EXECUTE
,0);
140 vm_map_remove(kernel_map
, (vm_offset_t
) igz
.inbuf
,
141 (vm_offset_t
) igz
.inbuf
+ PAGE_SIZE
);
143 if (igz
.error
|| error
|| error2
) {
144 printf("Output=%lu ", igz
.output
);
145 printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
146 error
, igz
.error
, error2
, igz
.where
);
158 do_aout_hdr(struct imgact_gzip
* gz
)
161 struct vmspace
*vmspace
;
165 * Set file/virtual offset based on a.out variant. We do two cases:
166 * host byte order and network byte order (for NetBSD compatibility)
168 switch ((int) (gz
->a_out
.a_magic
& 0xffff)) {
170 gz
->virtual_offset
= 0;
171 if (gz
->a_out
.a_text
) {
172 gz
->file_offset
= PAGE_SIZE
;
174 /* Bill's "screwball mode" */
179 gz
->virtual_offset
= PAGE_SIZE
;
183 /* NetBSD compatibility */
184 switch ((int) (ntohl(gz
->a_out
.a_magic
) & 0xffff)) {
187 gz
->virtual_offset
= PAGE_SIZE
;
191 gz
->where
= __LINE__
;
196 gz
->bss_size
= roundup(gz
->a_out
.a_bss
, PAGE_SIZE
);
199 * Check various fields in header for validity/bounds.
201 if ( /* entry point must lay with text region */
202 gz
->a_out
.a_entry
< gz
->virtual_offset
||
203 gz
->a_out
.a_entry
>= gz
->virtual_offset
+ gz
->a_out
.a_text
||
205 /* text and data size must each be page rounded */
206 gz
->a_out
.a_text
& PAGE_MASK
|| gz
->a_out
.a_data
& PAGE_MASK
) {
207 gz
->where
= __LINE__
;
211 * text/data/bss must not exceed limits
213 PROC_LOCK(gz
->ip
->proc
);
214 if ( /* text can't exceed maximum text size */
215 gz
->a_out
.a_text
> maxtsiz
||
217 /* data + bss can't exceed rlimit */
218 gz
->a_out
.a_data
+ gz
->bss_size
>
219 lim_cur(gz
->ip
->proc
, RLIMIT_DATA
)) {
220 PROC_UNLOCK(gz
->ip
->proc
);
221 gz
->where
= __LINE__
;
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
;
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.
236 VOP_UNLOCK(gz
->ip
->vp
, 0);
239 * Destroy old process VM and create a new one (with a new stack)
241 error
= exec_new_vmspace(gz
->ip
, &aout_sysvec
);
243 vn_lock(gz
->ip
->vp
, LK_EXCLUSIVE
| LK_RETRY
);
245 gz
->where
= __LINE__
;
249 vmspace
= gz
->ip
->proc
->p_vmspace
;
251 vmaddr
= gz
->virtual_offset
;
253 error
= vm_mmap(&vmspace
->vm_map
,
255 gz
->a_out
.a_text
+ gz
->a_out
.a_data
,
256 VM_PROT_ALL
, VM_PROT_ALL
, MAP_ANON
| MAP_FIXED
,
262 gz
->where
= __LINE__
;
266 if (gz
->bss_size
!= 0) {
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.
272 vmaddr
= gz
->virtual_offset
+ gz
->a_out
.a_text
+
274 error
= vm_map_find(&vmspace
->vm_map
,
279 FALSE
, VM_PROT_ALL
, VM_PROT_ALL
, 0);
281 gz
->where
= __LINE__
;
285 /* Fill in process VM information */
286 vmspace
->vm_tsize
= gz
->a_out
.a_text
>> PAGE_SHIFT
;
287 vmspace
->vm_dsize
= (gz
->a_out
.a_data
+ gz
->bss_size
) >> PAGE_SHIFT
;
288 vmspace
->vm_taddr
= (caddr_t
) (uintptr_t) gz
->virtual_offset
;
289 vmspace
->vm_daddr
= (caddr_t
) (uintptr_t)
290 (gz
->virtual_offset
+ gz
->a_out
.a_text
);
292 /* Fill in image_params */
293 gz
->ip
->interpreted
= 0;
294 gz
->ip
->entry_addr
= gz
->a_out
.a_entry
;
296 gz
->ip
->proc
->p_sysent
= &aout_sysvec
;
305 struct imgact_gzip
*igz
= (struct imgact_gzip
*) vp
;
307 if (igz
->idx
>= igz
->len
) {
308 igz
->where
= __LINE__
;
311 if (igz
->inbuf
&& igz
->idx
< (igz
->offset
+ PAGE_SIZE
)) {
312 return igz
->inbuf
[(igz
->idx
++) - igz
->offset
];
315 error
= vm_map_remove(kernel_map
, (vm_offset_t
) igz
->inbuf
,
316 (vm_offset_t
) igz
->inbuf
+ PAGE_SIZE
);
318 igz
->where
= __LINE__
;
323 igz
->offset
= igz
->idx
& ~PAGE_MASK
;
325 error
= vm_mmap(kernel_map
, /* map */
326 (vm_offset_t
*) & igz
->inbuf
, /* address */
327 PAGE_SIZE
, /* size */
328 VM_PROT_READ
, /* protection */
329 VM_PROT_READ
, /* max protection */
331 OBJT_VNODE
, /* handle type */
332 igz
->ip
->vp
, /* vnode */
333 igz
->offset
); /* offset */
335 igz
->where
= __LINE__
;
339 return igz
->inbuf
[(igz
->idx
++) - igz
->offset
];
343 Flush(void *vp
, u_char
* ptr
, u_long siz
)
345 struct imgact_gzip
*gz
= (struct imgact_gzip
*) vp
;
349 /* First, find an a.out-header. */
350 if (gz
->output
< sizeof gz
->a_out
) {
351 q
= (u_char
*) & gz
->a_out
;
352 i
= min(siz
, sizeof gz
->a_out
- gz
->output
);
353 bcopy(p
, q
+ gz
->output
, i
);
357 if (gz
->output
== sizeof gz
->a_out
) {
362 gz
->where
= __LINE__
;
366 gz
->where
= __LINE__
;
370 if (gz
->file_offset
== 0) {
371 q
= (u_char
*) (uintptr_t) gz
->virtual_offset
;
372 copyout(&gz
->a_out
, q
, sizeof gz
->a_out
);
376 /* Skip over zero-padded first PAGE if needed */
377 if (gz
->output
< gz
->file_offset
&&
378 gz
->output
+ siz
> gz
->file_offset
) {
379 i
= min(siz
, gz
->file_offset
- gz
->output
);
384 if (gz
->output
>= gz
->file_offset
&& gz
->output
< gz
->file_end
) {
385 i
= min(siz
, gz
->file_end
- gz
->output
);
386 q
= (u_char
*) (uintptr_t)
387 (gz
->virtual_offset
+ gz
->output
- gz
->file_offset
);
399 * Tell kern_execve.c about it, with a little help from the linker.
401 static struct execsw gzip_execsw
= {exec_gzip_imgact
, "gzip"};
402 EXEC_SET(execgzip
, gzip_execsw
);