Deleted Added
full compact
imgact_aout.c (92723) imgact_aout.c (94307)
1/*
2 * Copyright (c) 1993, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*
2 * Copyright (c) 1993, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/imgact_aout.c 92723 2002-03-19 21:25:46Z alfred $
26 * $FreeBSD: head/sys/kern/imgact_aout.c 94307 2002-04-09 20:10:46Z jhb $
27 */
28
29#include "opt_kstack_pages.h"
30
31#include <sys/param.h>
32#include <sys/exec.h>
33#include <sys/fcntl.h>
34#include <sys/imgact.h>
35#include <sys/imgact_aout.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/namei.h>
40#include <sys/pioctl.h>
41#include <sys/proc.h>
42#include <sys/resourcevar.h>
43#include <sys/systm.h>
44#include <sys/signalvar.h>
45#include <sys/stat.h>
46#include <sys/sysent.h>
47#include <sys/syscall.h>
48#include <sys/vnode.h>
49#include <sys/user.h>
50
51#include <machine/md_var.h>
52
53#include <vm/vm.h>
54#include <vm/vm_param.h>
55#include <vm/pmap.h>
56#include <vm/vm_map.h>
57#include <vm/vm_object.h>
58
59static int exec_aout_imgact(struct image_params *imgp);
60
61struct sysentvec aout_sysvec = {
62 SYS_MAXSYSCALL,
63 sysent,
64 0,
65 0,
66 0,
67 0,
68 0,
69 0,
70 0,
71 sendsig,
72 sigcode,
73 &szsigcode,
74 0,
75 "FreeBSD a.out",
76 aout_coredump,
77 NULL,
78 MINSIGSTKSZ
79};
80
81static int
82exec_aout_imgact(imgp)
83 struct image_params *imgp;
84{
85 const struct exec *a_out = (const struct exec *) imgp->image_header;
86 struct vmspace *vmspace;
87 struct vnode *vp;
88 vm_map_t map;
89 vm_object_t object;
90 vm_offset_t text_end, data_end;
91 unsigned long virtual_offset;
92 unsigned long file_offset;
93 unsigned long bss_size;
94 int error;
95
96 GIANT_REQUIRED;
97
98 /*
99 * Linux and *BSD binaries look very much alike,
100 * only the machine id is different:
101 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
102 * NetBSD is in network byte order.. ugh.
103 */
104 if (((a_out->a_magic >> 16) & 0xff) != 0x86 &&
105 ((a_out->a_magic >> 16) & 0xff) != 0 &&
106 ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86)
107 return -1;
108
109 /*
110 * Set file/virtual offset based on a.out variant.
111 * We do two cases: host byte order and network byte order
112 * (for NetBSD compatibility)
113 */
114 switch ((int)(a_out->a_magic & 0xffff)) {
115 case ZMAGIC:
116 virtual_offset = 0;
117 if (a_out->a_text) {
118 file_offset = PAGE_SIZE;
119 } else {
120 /* Bill's "screwball mode" */
121 file_offset = 0;
122 }
123 break;
124 case QMAGIC:
125 virtual_offset = PAGE_SIZE;
126 file_offset = 0;
127 /* Pass PS_STRINGS for BSD/OS binaries only. */
128 if (N_GETMID(*a_out) == MID_ZERO)
129 imgp->ps_strings = PS_STRINGS;
130 break;
131 default:
132 /* NetBSD compatibility */
133 switch ((int)(ntohl(a_out->a_magic) & 0xffff)) {
134 case ZMAGIC:
135 case QMAGIC:
136 virtual_offset = PAGE_SIZE;
137 file_offset = 0;
138 break;
139 default:
140 return (-1);
141 }
142 }
143
144 bss_size = roundup(a_out->a_bss, PAGE_SIZE);
145
146 /*
147 * Check various fields in header for validity/bounds.
148 */
149 if (/* entry point must lay with text region */
150 a_out->a_entry < virtual_offset ||
151 a_out->a_entry >= virtual_offset + a_out->a_text ||
152
153 /* text and data size must each be page rounded */
154 a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
155 return (-1);
156
157 /* text + data can't exceed file size */
158 if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
159 return (EFAULT);
160
161 /*
162 * text/data/bss must not exceed limits
163 */
164 mtx_assert(&Giant, MA_OWNED);
165 if (/* text can't exceed maximum text size */
166 a_out->a_text > maxtsiz ||
167
168 /* data + bss can't exceed rlimit */
169 a_out->a_data + bss_size >
170 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
171 return (ENOMEM);
172
173 /* copy in arguments and/or environment from old process */
174 error = exec_extract_strings(imgp);
175 if (error)
176 return (error);
177
178 /*
179 * Destroy old process VM and create a new one (with a new stack)
180 */
181 exec_new_vmspace(imgp);
182
183 /*
184 * The vm space can be changed by exec_new_vmspace
185 */
186 vmspace = imgp->proc->p_vmspace;
187
188 vp = imgp->vp;
189 map = &vmspace->vm_map;
190 vm_map_lock(map);
191 VOP_GETVOBJECT(vp, &object);
192 vm_object_reference(object);
193
194 text_end = virtual_offset + a_out->a_text;
195 error = vm_map_insert(map, object,
196 file_offset,
197 virtual_offset, text_end,
198 VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
199 MAP_COPY_ON_WRITE | MAP_PREFAULT);
200 if (error) {
201 vm_map_unlock(map);
202 return (error);
203 }
204 data_end = text_end + a_out->a_data;
205 if (a_out->a_data) {
206 vm_object_reference(object);
207 error = vm_map_insert(map, object,
208 file_offset + a_out->a_text,
209 text_end, data_end,
210 VM_PROT_ALL, VM_PROT_ALL,
211 MAP_COPY_ON_WRITE | MAP_PREFAULT);
212 if (error) {
213 vm_map_unlock(map);
214 return (error);
215 }
216 }
217
218 if (bss_size) {
219 error = vm_map_insert(map, NULL, 0,
220 data_end, data_end + bss_size,
221 VM_PROT_ALL, VM_PROT_ALL, 0);
222 if (error) {
223 vm_map_unlock(map);
224 return (error);
225 }
226 }
227 vm_map_unlock(map);
228
229 /* Fill in process VM information */
230 vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
231 vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
232 vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
233 vmspace->vm_daddr = (caddr_t) (uintptr_t)
234 (virtual_offset + a_out->a_text);
235
236 /* Fill in image_params */
237 imgp->interpreted = 0;
238 imgp->entry_addr = a_out->a_entry;
239
240 imgp->proc->p_sysent = &aout_sysvec;
241
242 /* Indicate that this file should not be modified */
243 imgp->vp->v_flag |= VTEXT;
244
245 return (0);
246}
247
248/*
249 * Dump core, into a file named as described in the comments for
250 * expand_name(), unless the process was setuid/setgid.
251 */
252int
253aout_coredump(td, vp, limit)
254 register struct thread *td;
255 register struct vnode *vp;
256 off_t limit;
257{
258 struct proc *p = td->td_proc;
259 register struct ucred *cred = td->td_ucred;
260 register struct vmspace *vm = p->p_vmspace;
261 int error;
262
263 if (ctob((UAREA_PAGES + KSTACK_PAGES)
264 + vm->vm_dsize + vm->vm_ssize) >= limit)
265 return (EFAULT);
27 */
28
29#include "opt_kstack_pages.h"
30
31#include <sys/param.h>
32#include <sys/exec.h>
33#include <sys/fcntl.h>
34#include <sys/imgact.h>
35#include <sys/imgact_aout.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/namei.h>
40#include <sys/pioctl.h>
41#include <sys/proc.h>
42#include <sys/resourcevar.h>
43#include <sys/systm.h>
44#include <sys/signalvar.h>
45#include <sys/stat.h>
46#include <sys/sysent.h>
47#include <sys/syscall.h>
48#include <sys/vnode.h>
49#include <sys/user.h>
50
51#include <machine/md_var.h>
52
53#include <vm/vm.h>
54#include <vm/vm_param.h>
55#include <vm/pmap.h>
56#include <vm/vm_map.h>
57#include <vm/vm_object.h>
58
59static int exec_aout_imgact(struct image_params *imgp);
60
61struct sysentvec aout_sysvec = {
62 SYS_MAXSYSCALL,
63 sysent,
64 0,
65 0,
66 0,
67 0,
68 0,
69 0,
70 0,
71 sendsig,
72 sigcode,
73 &szsigcode,
74 0,
75 "FreeBSD a.out",
76 aout_coredump,
77 NULL,
78 MINSIGSTKSZ
79};
80
81static int
82exec_aout_imgact(imgp)
83 struct image_params *imgp;
84{
85 const struct exec *a_out = (const struct exec *) imgp->image_header;
86 struct vmspace *vmspace;
87 struct vnode *vp;
88 vm_map_t map;
89 vm_object_t object;
90 vm_offset_t text_end, data_end;
91 unsigned long virtual_offset;
92 unsigned long file_offset;
93 unsigned long bss_size;
94 int error;
95
96 GIANT_REQUIRED;
97
98 /*
99 * Linux and *BSD binaries look very much alike,
100 * only the machine id is different:
101 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
102 * NetBSD is in network byte order.. ugh.
103 */
104 if (((a_out->a_magic >> 16) & 0xff) != 0x86 &&
105 ((a_out->a_magic >> 16) & 0xff) != 0 &&
106 ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86)
107 return -1;
108
109 /*
110 * Set file/virtual offset based on a.out variant.
111 * We do two cases: host byte order and network byte order
112 * (for NetBSD compatibility)
113 */
114 switch ((int)(a_out->a_magic & 0xffff)) {
115 case ZMAGIC:
116 virtual_offset = 0;
117 if (a_out->a_text) {
118 file_offset = PAGE_SIZE;
119 } else {
120 /* Bill's "screwball mode" */
121 file_offset = 0;
122 }
123 break;
124 case QMAGIC:
125 virtual_offset = PAGE_SIZE;
126 file_offset = 0;
127 /* Pass PS_STRINGS for BSD/OS binaries only. */
128 if (N_GETMID(*a_out) == MID_ZERO)
129 imgp->ps_strings = PS_STRINGS;
130 break;
131 default:
132 /* NetBSD compatibility */
133 switch ((int)(ntohl(a_out->a_magic) & 0xffff)) {
134 case ZMAGIC:
135 case QMAGIC:
136 virtual_offset = PAGE_SIZE;
137 file_offset = 0;
138 break;
139 default:
140 return (-1);
141 }
142 }
143
144 bss_size = roundup(a_out->a_bss, PAGE_SIZE);
145
146 /*
147 * Check various fields in header for validity/bounds.
148 */
149 if (/* entry point must lay with text region */
150 a_out->a_entry < virtual_offset ||
151 a_out->a_entry >= virtual_offset + a_out->a_text ||
152
153 /* text and data size must each be page rounded */
154 a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
155 return (-1);
156
157 /* text + data can't exceed file size */
158 if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
159 return (EFAULT);
160
161 /*
162 * text/data/bss must not exceed limits
163 */
164 mtx_assert(&Giant, MA_OWNED);
165 if (/* text can't exceed maximum text size */
166 a_out->a_text > maxtsiz ||
167
168 /* data + bss can't exceed rlimit */
169 a_out->a_data + bss_size >
170 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
171 return (ENOMEM);
172
173 /* copy in arguments and/or environment from old process */
174 error = exec_extract_strings(imgp);
175 if (error)
176 return (error);
177
178 /*
179 * Destroy old process VM and create a new one (with a new stack)
180 */
181 exec_new_vmspace(imgp);
182
183 /*
184 * The vm space can be changed by exec_new_vmspace
185 */
186 vmspace = imgp->proc->p_vmspace;
187
188 vp = imgp->vp;
189 map = &vmspace->vm_map;
190 vm_map_lock(map);
191 VOP_GETVOBJECT(vp, &object);
192 vm_object_reference(object);
193
194 text_end = virtual_offset + a_out->a_text;
195 error = vm_map_insert(map, object,
196 file_offset,
197 virtual_offset, text_end,
198 VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
199 MAP_COPY_ON_WRITE | MAP_PREFAULT);
200 if (error) {
201 vm_map_unlock(map);
202 return (error);
203 }
204 data_end = text_end + a_out->a_data;
205 if (a_out->a_data) {
206 vm_object_reference(object);
207 error = vm_map_insert(map, object,
208 file_offset + a_out->a_text,
209 text_end, data_end,
210 VM_PROT_ALL, VM_PROT_ALL,
211 MAP_COPY_ON_WRITE | MAP_PREFAULT);
212 if (error) {
213 vm_map_unlock(map);
214 return (error);
215 }
216 }
217
218 if (bss_size) {
219 error = vm_map_insert(map, NULL, 0,
220 data_end, data_end + bss_size,
221 VM_PROT_ALL, VM_PROT_ALL, 0);
222 if (error) {
223 vm_map_unlock(map);
224 return (error);
225 }
226 }
227 vm_map_unlock(map);
228
229 /* Fill in process VM information */
230 vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
231 vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
232 vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
233 vmspace->vm_daddr = (caddr_t) (uintptr_t)
234 (virtual_offset + a_out->a_text);
235
236 /* Fill in image_params */
237 imgp->interpreted = 0;
238 imgp->entry_addr = a_out->a_entry;
239
240 imgp->proc->p_sysent = &aout_sysvec;
241
242 /* Indicate that this file should not be modified */
243 imgp->vp->v_flag |= VTEXT;
244
245 return (0);
246}
247
248/*
249 * Dump core, into a file named as described in the comments for
250 * expand_name(), unless the process was setuid/setgid.
251 */
252int
253aout_coredump(td, vp, limit)
254 register struct thread *td;
255 register struct vnode *vp;
256 off_t limit;
257{
258 struct proc *p = td->td_proc;
259 register struct ucred *cred = td->td_ucred;
260 register struct vmspace *vm = p->p_vmspace;
261 int error;
262
263 if (ctob((UAREA_PAGES + KSTACK_PAGES)
264 + vm->vm_dsize + vm->vm_ssize) >= limit)
265 return (EFAULT);
266 PROC_LOCK(p);
266 fill_kinfo_proc(p, &p->p_uarea->u_kproc);
267 fill_kinfo_proc(p, &p->p_uarea->u_kproc);
268 PROC_UNLOCK(p);
267 error = cpu_coredump(td, vp, cred);
268 if (error == 0)
269 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
270 (int)ctob(vm->vm_dsize),
271 (off_t)ctob(UAREA_PAGES + KSTACK_PAGES), UIO_USERSPACE,
272 IO_UNIT | IO_DIRECT, cred, (int *) NULL, td);
273 if (error == 0)
274 error = vn_rdwr_inchunks(UIO_WRITE, vp,
275 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
276 round_page(ctob(vm->vm_ssize)),
277 (off_t)ctob(UAREA_PAGES + KSTACK_PAGES) +
278 ctob(vm->vm_dsize), UIO_USERSPACE,
279 IO_UNIT | IO_DIRECT, cred, (int *) NULL, td);
280 return (error);
281}
282
283/*
284 * Tell kern_execve.c about it, with a little help from the linker.
285 */
286static struct execsw aout_execsw = { exec_aout_imgact, "a.out" };
287EXEC_SET(aout, aout_execsw);
269 error = cpu_coredump(td, vp, cred);
270 if (error == 0)
271 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
272 (int)ctob(vm->vm_dsize),
273 (off_t)ctob(UAREA_PAGES + KSTACK_PAGES), UIO_USERSPACE,
274 IO_UNIT | IO_DIRECT, cred, (int *) NULL, td);
275 if (error == 0)
276 error = vn_rdwr_inchunks(UIO_WRITE, vp,
277 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
278 round_page(ctob(vm->vm_ssize)),
279 (off_t)ctob(UAREA_PAGES + KSTACK_PAGES) +
280 ctob(vm->vm_dsize), UIO_USERSPACE,
281 IO_UNIT | IO_DIRECT, cred, (int *) NULL, td);
282 return (error);
283}
284
285/*
286 * Tell kern_execve.c about it, with a little help from the linker.
287 */
288static struct execsw aout_execsw = { exec_aout_imgact, "a.out" };
289EXEC_SET(aout, aout_execsw);