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: releng/12.0/sys/kern/imgact_gzip.c 330842 2018-03-13 13:09:10Z emaste $");
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(struct image_params *imgp)
73 {
74 int error;
75 const u_char *p = (const u_char *) imgp->image_header;
76 struct imgact_gzip igz;
77 struct inflate infl;
78 struct vmspace *vmspace;
79
80 /* If these four are not OK, it isn't a gzip file */
81 if (p[0] != 0x1f)
82 return -1; /* 0 Simply magic */
83 if (p[1] != 0x8b)
84 return -1; /* 1 Simply magic */
85 if (p[2] != 0x08)
86 return -1; /* 2 Compression method */
87 if (p[9] != 0x03)
88 return -1; /* 9 OS compressed on */
89
90 /*
91 * If this one contains anything but a comment or a filename marker,
92 * we don't want to chew on it
93 */
94 if (p[3] & ~(0x18))
95 return ENOEXEC; /* 3 Flags */
96
97 /* These are of no use to us */
98 /* 4-7 Timestamp */
99 /* 8 Extra flags */
100
101 bzero(&igz, sizeof igz);
102 bzero(&infl, sizeof infl);
103 infl.gz_private = (void *) &igz;
104 infl.gz_input = NextByte;
105 infl.gz_output = Flush;
106
107 igz.ip = imgp;
108 igz.idx = 10;
109
110 if (p[3] & 0x08) { /* skip a filename */
111 while (p[igz.idx++])
112 if (igz.idx >= PAGE_SIZE)
113 return ENOEXEC;
114 }
115 if (p[3] & 0x10) { /* skip a comment */
116 while (p[igz.idx++])
117 if (igz.idx >= PAGE_SIZE)
118 return ENOEXEC;
119 }
120 igz.len = imgp->attr->va_size;
121
122 error = inflate(&infl);
123
124 /*
125 * The unzipped file may not even have been long enough to contain
126 * a header giving Flush() a chance to return error. Check for this.
127 */
128 if ( !igz.gotheader )
129 return ENOEXEC;
130
131 if ( !error ) {
132 vmspace = imgp->proc->p_vmspace;
133 error = vm_map_protect(&vmspace->vm_map,
134 (vm_offset_t) vmspace->vm_taddr,
135 (vm_offset_t) (vmspace->vm_taddr +
136 (vmspace->vm_tsize << PAGE_SHIFT)) ,
137 VM_PROT_READ|VM_PROT_EXECUTE,0);
138 }
139
140 if (igz.inbuf)
141 kmap_free_wakeup(exec_map, (vm_offset_t)igz.inbuf, PAGE_SIZE);
142 if (igz.error || error) {
143 printf("Output=%lu ", igz.output);
144 printf("Inflate_error=%d igz.error=%d where=%d\n",
145 error, igz.error, igz.where);
146 }
147 if (igz.error)
148 return igz.error;
149 if (error)
150 return ENOEXEC;
151 return 0;
152 }
153
154 static int
155 do_aout_hdr(struct imgact_gzip * gz)
156 {
157 int error;
158 struct vmspace *vmspace;
159 vm_offset_t vmaddr;
160
161 /*
162 * Set file/virtual offset based on a.out variant. We do two cases:
163 * host byte order and network byte order (for NetBSD compatibility)
164 */
165 switch ((int) (gz->a_out.a_midmag & 0xffff)) {
166 case ZMAGIC:
167 gz->virtual_offset = 0;
168 if (gz->a_out.a_text) {
169 gz->file_offset = PAGE_SIZE;
170 } else {
171 /* Bill's "screwball mode" */
172 gz->file_offset = 0;
173 }
174 break;
175 case QMAGIC:
176 gz->virtual_offset = PAGE_SIZE;
177 gz->file_offset = 0;
178 break;
179 default:
180 /* NetBSD compatibility */
181 switch ((int) (ntohl(gz->a_out.a_midmag) & 0xffff)) {
182 case ZMAGIC:
183 case QMAGIC:
184 gz->virtual_offset = PAGE_SIZE;
185 gz->file_offset = 0;
186 break;
187 default:
188 gz->where = __LINE__;
189 return (-1);
190 }
191 }
192
193 gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
194
195 /*
196 * Check various fields in header for validity/bounds.
197 */
198 if ( /* entry point must lay with text region */
199 gz->a_out.a_entry < gz->virtual_offset ||
200 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
201
202 /* text and data size must each be page rounded */
203 gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
204 gz->where = __LINE__;
205 return (-1);
206 }
207 /*
208 * text/data/bss must not exceed limits
209 */
210 PROC_LOCK(gz->ip->proc);
211 if ( /* text can't exceed maximum text size */
212 gz->a_out.a_text > maxtsiz ||
213
214 /* data + bss can't exceed rlimit */
215 gz->a_out.a_data + gz->bss_size >
216 lim_cur_proc(gz->ip->proc, RLIMIT_DATA) ||
217 racct_set(gz->ip->proc, RACCT_DATA,
218 gz->a_out.a_data + gz->bss_size) != 0) {
219 PROC_UNLOCK(gz->ip->proc);
220 gz->where = __LINE__;
221 return (ENOMEM);
222 }
223 PROC_UNLOCK(gz->ip->proc);
224 /* Find out how far we should go */
225 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
226
227 /*
228 * Avoid a possible deadlock if the current address space is destroyed
229 * and that address space maps the locked vnode. In the common case,
230 * the locked vnode's v_usecount is decremented but remains greater
231 * than zero. Consequently, the vnode lock is not needed by vrele().
232 * However, in cases where the vnode lock is external, such as nullfs,
233 * v_usecount may become zero.
234 */
235 VOP_UNLOCK(gz->ip->vp, 0);
236
237 /*
238 * Destroy old process VM and create a new one (with a new stack)
239 */
240 error = exec_new_vmspace(gz->ip, &aout_sysvec);
241
242 vn_lock(gz->ip->vp, LK_EXCLUSIVE | LK_RETRY);
243 if (error) {
244 gz->where = __LINE__;
245 return (error);
246 }
247
248 vmspace = gz->ip->proc->p_vmspace;
249
250 vmaddr = gz->virtual_offset;
251
252 error = vm_mmap(&vmspace->vm_map,
253 &vmaddr,
254 gz->a_out.a_text + gz->a_out.a_data,
255 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
256 OBJT_DEFAULT,
257 NULL,
258 0);
259
260 if (error) {
261 gz->where = __LINE__;
262 return (error);
263 }
264
265 if (gz->bss_size != 0) {
266 /*
267 * Allocate demand-zeroed area for uninitialized data.
268 * "bss" = 'block started by symbol' - named after the
269 * IBM 7090 instruction of the same name.
270 */
271 vmaddr = gz->virtual_offset + gz->a_out.a_text +
272 gz->a_out.a_data;
273 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
274 gz->bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL,
275 0);
276 if (error) {
277 gz->where = __LINE__;
278 return (error);
279 }
280 }
281 /* Fill in process VM information */
282 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
283 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
284 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
285 vmspace->vm_daddr = (caddr_t) (uintptr_t)
286 (gz->virtual_offset + gz->a_out.a_text);
287
288 /* Fill in image_params */
289 gz->ip->interpreted = 0;
290 gz->ip->entry_addr = gz->a_out.a_entry;
291
292 gz->ip->proc->p_sysent = &aout_sysvec;
293
294 return 0;
295 }
296
297 static int
298 NextByte(void *vp)
299 {
300 int error;
301 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
302
303 if (igz->idx >= igz->len) {
304 igz->where = __LINE__;
305 return GZ_EOF;
306 }
307 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
308 return igz->inbuf[(igz->idx++) - igz->offset];
309 }
310 if (igz->inbuf)
311 kmap_free_wakeup(exec_map, (vm_offset_t)igz->inbuf, PAGE_SIZE);
312 igz->offset = igz->idx & ~PAGE_MASK;
313
314 error = vm_mmap(exec_map, /* map */
315 (vm_offset_t *) & igz->inbuf, /* address */
316 PAGE_SIZE, /* size */
317 VM_PROT_READ, /* protection */
318 VM_PROT_READ, /* max protection */
319 0, /* flags */
320 OBJT_VNODE, /* handle type */
321 igz->ip->vp, /* vnode */
322 igz->offset); /* offset */
323 if (error) {
324 igz->where = __LINE__;
325 igz->error = error;
326 return GZ_EOF;
327 }
328 return igz->inbuf[(igz->idx++) - igz->offset];
329 }
330
331 static int
332 Flush(void *vp, u_char * ptr, u_long siz)
333 {
334 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
335 u_char *p = ptr, *q;
336 int i;
337
338 /* First, find an a.out-header. */
339 if (gz->output < sizeof gz->a_out) {
340 q = (u_char *) & gz->a_out;
341 i = min(siz, sizeof gz->a_out - gz->output);
342 bcopy(p, q + gz->output, i);
343 gz->output += i;
344 p += i;
345 siz -= i;
346 if (gz->output == sizeof gz->a_out) {
347 gz->gotheader = 1;
348 i = do_aout_hdr(gz);
349 if (i == -1) {
350 if (!gz->where)
351 gz->where = __LINE__;
352 gz->error = ENOEXEC;
353 return ENOEXEC;
354 } else if (i) {
355 gz->where = __LINE__;
356 gz->error = i;
357 return ENOEXEC;
358 }
359 if (gz->file_offset == 0) {
360 q = (u_char *) (uintptr_t) gz->virtual_offset;
361 copyout(&gz->a_out, q, sizeof gz->a_out);
362 }
363 }
364 }
365 /* Skip over zero-padded first PAGE if needed */
366 if (gz->output < gz->file_offset &&
367 gz->output + siz > gz->file_offset) {
368 i = min(siz, gz->file_offset - gz->output);
369 gz->output += i;
370 p += i;
371 siz -= i;
372 }
373 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
374 i = min(siz, gz->file_end - gz->output);
375 q = (u_char *) (uintptr_t)
376 (gz->virtual_offset + gz->output - gz->file_offset);
377 copyout(p, q, i);
378 gz->output += i;
379 p += i;
380 siz -= i;
381 }
382 gz->output += siz;
383 return 0;
384 }
385
386
387 /*
388 * Tell kern_execve.c about it, with a little help from the linker.
389 */
390 static struct execsw gzip_execsw = {
391 .ex_imgact = exec_gzip_imgact,
392 .ex_name = "gzip"
393 };
394 EXEC_SET(execgzip, gzip_execsw);
Cache object: ad8a651781a4de9b4dcbad98ee9a10e0
|