xref: /freebsd/sys/kern/imgact_aout.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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$
27  */
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/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/resourcevar.h>
39 #include <sys/signalvar.h>
40 #include <sys/syscall.h>
41 #include <sys/sysent.h>
42 #include <sys/systm.h>
43 #include <sys/vnode.h>
44 #include <sys/user.h>
45 
46 #include <machine/frame.h>
47 #include <machine/md_var.h>
48 
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_param.h>
54 
55 static int	exec_aout_imgact(struct image_params *imgp);
56 static int	aout_fixup(register_t **stack_base, struct image_params *imgp);
57 
58 struct sysentvec aout_sysvec = {
59 	SYS_MAXSYSCALL,
60 	sysent,
61 	0,
62 	0,
63 	NULL,
64 	0,
65 	NULL,
66 	NULL,
67 	aout_fixup,
68 	sendsig,
69 	sigcode,
70 	&szsigcode,
71 	NULL,
72 	"FreeBSD a.out",
73 	aout_coredump,
74 	NULL,
75 	MINSIGSTKSZ,
76 	PAGE_SIZE,
77 	VM_MIN_ADDRESS,
78 	VM_MAXUSER_ADDRESS,
79 	USRSTACK,
80 	PS_STRINGS,
81 	VM_PROT_ALL,
82 	exec_copyout_strings,
83 	exec_setregs
84 };
85 
86 static int
87 aout_fixup(stack_base, imgp)
88 	register_t **stack_base;
89 	struct image_params *imgp;
90 {
91 
92 	return (suword(--(*stack_base), imgp->argc));
93 }
94 
95 static int
96 exec_aout_imgact(imgp)
97 	struct image_params *imgp;
98 {
99 	const struct exec *a_out = (const struct exec *) imgp->image_header;
100 	struct vmspace *vmspace;
101 	struct vnode *vp;
102 	vm_map_t map;
103 	vm_object_t object;
104 	vm_offset_t text_end, data_end;
105 	unsigned long virtual_offset;
106 	unsigned long file_offset;
107 	unsigned long bss_size;
108 	int error;
109 
110 	GIANT_REQUIRED;
111 
112 	/*
113 	 * Linux and *BSD binaries look very much alike,
114 	 * only the machine id is different:
115 	 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
116 	 * NetBSD is in network byte order.. ugh.
117 	 */
118 	if (((a_out->a_magic >> 16) & 0xff) != 0x86 &&
119 	    ((a_out->a_magic >> 16) & 0xff) != 0 &&
120 	    ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86)
121                 return -1;
122 
123 	/*
124 	 * Set file/virtual offset based on a.out variant.
125 	 *	We do two cases: host byte order and network byte order
126 	 *	(for NetBSD compatibility)
127 	 */
128 	switch ((int)(a_out->a_magic & 0xffff)) {
129 	case ZMAGIC:
130 		virtual_offset = 0;
131 		if (a_out->a_text) {
132 			file_offset = PAGE_SIZE;
133 		} else {
134 			/* Bill's "screwball mode" */
135 			file_offset = 0;
136 		}
137 		break;
138 	case QMAGIC:
139 		virtual_offset = PAGE_SIZE;
140 		file_offset = 0;
141 		/* Pass PS_STRINGS for BSD/OS binaries only. */
142 		if (N_GETMID(*a_out) == MID_ZERO)
143 			imgp->ps_strings = aout_sysvec.sv_psstrings;
144 		break;
145 	default:
146 		/* NetBSD compatibility */
147 		switch ((int)(ntohl(a_out->a_magic) & 0xffff)) {
148 		case ZMAGIC:
149 		case QMAGIC:
150 			virtual_offset = PAGE_SIZE;
151 			file_offset = 0;
152 			break;
153 		default:
154 			return (-1);
155 		}
156 	}
157 
158 	bss_size = roundup(a_out->a_bss, PAGE_SIZE);
159 
160 	/*
161 	 * Check various fields in header for validity/bounds.
162 	 */
163 	if (/* entry point must lay with text region */
164 	    a_out->a_entry < virtual_offset ||
165 	    a_out->a_entry >= virtual_offset + a_out->a_text ||
166 
167 	    /* text and data size must each be page rounded */
168 	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
169 		return (-1);
170 
171 	/* text + data can't exceed file size */
172 	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
173 		return (EFAULT);
174 
175 	/*
176 	 * text/data/bss must not exceed limits
177 	 */
178 	mtx_assert(&Giant, MA_OWNED);
179 	if (/* text can't exceed maximum text size */
180 	    a_out->a_text > maxtsiz ||
181 
182 	    /* data + bss can't exceed rlimit */
183 	    a_out->a_data + bss_size >
184 		imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
185 			return (ENOMEM);
186 
187 	/* copy in arguments and/or environment from old process */
188 	error = exec_extract_strings(imgp);
189 	if (error)
190 		return (error);
191 
192 	/*
193 	 * Destroy old process VM and create a new one (with a new stack)
194 	 */
195 	exec_new_vmspace(imgp, &aout_sysvec);
196 
197 	/*
198 	 * The vm space can be changed by exec_new_vmspace
199 	 */
200 	vmspace = imgp->proc->p_vmspace;
201 
202 	vp = imgp->vp;
203 	object = imgp->object;
204 	map = &vmspace->vm_map;
205 	vm_map_lock(map);
206 	vm_object_reference(object);
207 
208 	text_end = virtual_offset + a_out->a_text;
209 	error = vm_map_insert(map, object,
210 		file_offset,
211 		virtual_offset, text_end,
212 		VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
213 		MAP_COPY_ON_WRITE | MAP_PREFAULT);
214 	if (error) {
215 		vm_map_unlock(map);
216 		return (error);
217 	}
218 	data_end = text_end + a_out->a_data;
219 	if (a_out->a_data) {
220 		vm_object_reference(object);
221 		error = vm_map_insert(map, object,
222 			file_offset + a_out->a_text,
223 			text_end, data_end,
224 			VM_PROT_ALL, VM_PROT_ALL,
225 			MAP_COPY_ON_WRITE | MAP_PREFAULT);
226 		if (error) {
227 			vm_map_unlock(map);
228 			return (error);
229 		}
230 	}
231 
232 	if (bss_size) {
233 		error = vm_map_insert(map, NULL, 0,
234 			data_end, data_end + bss_size,
235 			VM_PROT_ALL, VM_PROT_ALL, 0);
236 		if (error) {
237 			vm_map_unlock(map);
238 			return (error);
239 		}
240 	}
241 	vm_map_unlock(map);
242 
243 	/* Fill in process VM information */
244 	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
245 	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
246 	vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
247 	vmspace->vm_daddr = (caddr_t) (uintptr_t)
248 			    (virtual_offset + a_out->a_text);
249 
250 	/* Fill in image_params */
251 	imgp->interpreted = 0;
252 	imgp->entry_addr = a_out->a_entry;
253 
254 	imgp->proc->p_sysent = &aout_sysvec;
255 
256 	return (0);
257 }
258 
259 /*
260  * Dump core, into a file named as described in the comments for
261  * expand_name(), unless the process was setuid/setgid.
262  */
263 int
264 aout_coredump(td, vp, limit)
265 	register struct thread *td;
266 	register struct vnode *vp;
267 	off_t limit;
268 {
269 	struct proc *p = td->td_proc;
270 	register struct ucred *cred = td->td_ucred;
271 	register struct vmspace *vm = p->p_vmspace;
272 	char *tempuser;
273 	int error;
274 
275 	if (ctob((uarea_pages + kstack_pages)
276 	    + vm->vm_dsize + vm->vm_ssize) >= limit)
277 		return (EFAULT);
278 	tempuser = malloc(ctob(uarea_pages + kstack_pages), M_TEMP,
279 	    M_WAITOK | M_ZERO);
280 	if (tempuser == NULL)
281 		return (ENOMEM);
282 	PROC_LOCK(p);
283 	fill_kinfo_proc(p, &p->p_uarea->u_kproc);
284 	PROC_UNLOCK(p);
285 	bcopy(p->p_uarea, tempuser, sizeof(struct user));
286 	bcopy(td->td_frame,
287 	    tempuser + ctob(uarea_pages) +
288 	    ((caddr_t)td->td_frame - (caddr_t)td->td_kstack),
289 	    sizeof(struct trapframe));
290 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)tempuser,
291 	    ctob(uarea_pages + kstack_pages),
292 	    (off_t)0, UIO_SYSSPACE, IO_UNIT, cred, NOCRED,
293 	    (int *)NULL, td);
294 	free(tempuser, M_TEMP);
295 	if (error == 0)
296 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
297 		    (int)ctob(vm->vm_dsize),
298 		    (off_t)ctob(uarea_pages + kstack_pages), UIO_USERSPACE,
299 		    IO_UNIT | IO_DIRECT, cred, NOCRED, (int *) NULL, td);
300 	if (error == 0)
301 		error = vn_rdwr_inchunks(UIO_WRITE, vp,
302 		    (caddr_t)trunc_page(p->p_sysent->sv_usrstack -
303 		    ctob(vm->vm_ssize)), round_page(ctob(vm->vm_ssize)),
304 		    (off_t)ctob(uarea_pages + kstack_pages) +
305 		        ctob(vm->vm_dsize), UIO_USERSPACE,
306 		    IO_UNIT | IO_DIRECT, cred, NOCRED, (int *) NULL, td);
307 	return (error);
308 }
309 
310 /*
311  * Tell kern_execve.c about it, with a little help from the linker.
312  */
313 static struct execsw aout_execsw = { exec_aout_imgact, "a.out" };
314 EXEC_SET(aout, aout_execsw);
315