1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1993, David Greenman
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/exec.h>
34#include <sys/imgact.h>
35#include <sys/imgact_aout.h>
36#include <sys/kernel.h>
37#include <sys/limits.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/mutex.h>
41#include <sys/proc.h>
42#include <sys/racct.h>
43#include <sys/resourcevar.h>
44#include <sys/signalvar.h>
45#include <sys/syscall.h>
46#include <sys/sysent.h>
47#include <sys/systm.h>
48#include <sys/vnode.h>
49
50#include <machine/frame.h>
51#include <machine/md_var.h>
52
53#include <vm/vm.h>
54#include <vm/pmap.h>
55#include <vm/vm_map.h>
56#include <vm/vm_object.h>
57#include <vm/vm_param.h>
58
59#ifdef __amd64__
60#include <compat/freebsd32/freebsd32_signal.h>
61#include <compat/freebsd32/freebsd32_util.h>
62#include <compat/freebsd32/freebsd32_proto.h>
63#include <compat/freebsd32/freebsd32_syscall.h>
64#include <compat/ia32/ia32_signal.h>
65#endif
66
67static int	exec_aout_imgact(struct image_params *imgp);
68static int	aout_fixup(register_t **stack_base, struct image_params *imgp);
69
70#define	AOUT32_USRSTACK		0xbfc00000
71
72#if defined(__i386__)
73
74#define	AOUT32_PS_STRINGS	(AOUT32_USRSTACK - sizeof(struct ps_strings))
75
76struct sysentvec aout_sysvec = {
77	.sv_size	= SYS_MAXSYSCALL,
78	.sv_table	= sysent,
79	.sv_mask	= 0,
80	.sv_errsize	= 0,
81	.sv_errtbl	= NULL,
82	.sv_transtrap	= NULL,
83	.sv_fixup	= aout_fixup,
84	.sv_sendsig	= sendsig,
85	.sv_sigcode	= sigcode,
86	.sv_szsigcode	= &szsigcode,
87	.sv_name	= "FreeBSD a.out",
88	.sv_coredump	= NULL,
89	.sv_imgact_try	= NULL,
90	.sv_minsigstksz	= MINSIGSTKSZ,
91	.sv_minuser	= VM_MIN_ADDRESS,
92	.sv_maxuser	= AOUT32_USRSTACK,
93	.sv_usrstack	= AOUT32_USRSTACK,
94	.sv_psstrings	= AOUT32_PS_STRINGS,
95	.sv_stackprot	= VM_PROT_ALL,
96	.sv_copyout_strings	= exec_copyout_strings,
97	.sv_setregs	= exec_setregs,
98	.sv_fixlimit	= NULL,
99	.sv_maxssiz	= NULL,
100	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32,
101	.sv_set_syscall_retval = cpu_set_syscall_retval,
102	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
103	.sv_syscallnames = syscallnames,
104	.sv_schedtail	= NULL,
105	.sv_thread_detach = NULL,
106	.sv_trap	= NULL,
107};
108
109#elif defined(__amd64__)
110
111#define	AOUT32_PS_STRINGS \
112    (AOUT32_USRSTACK - sizeof(struct freebsd32_ps_strings))
113#define	AOUT32_MINUSER		FREEBSD32_MINUSER
114
115extern const char *freebsd32_syscallnames[];
116extern u_long ia32_maxssiz;
117
118struct sysentvec aout_sysvec = {
119	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
120	.sv_table	= freebsd32_sysent,
121	.sv_mask	= 0,
122	.sv_errsize	= 0,
123	.sv_errtbl	= NULL,
124	.sv_transtrap	= NULL,
125	.sv_fixup	= aout_fixup,
126	.sv_sendsig	= ia32_sendsig,
127	.sv_sigcode	= ia32_sigcode,
128	.sv_szsigcode	= &sz_ia32_sigcode,
129	.sv_name	= "FreeBSD a.out",
130	.sv_coredump	= NULL,
131	.sv_imgact_try	= NULL,
132	.sv_minsigstksz	= MINSIGSTKSZ,
133	.sv_minuser	= AOUT32_MINUSER,
134	.sv_maxuser	= AOUT32_USRSTACK,
135	.sv_usrstack	= AOUT32_USRSTACK,
136	.sv_psstrings	= AOUT32_PS_STRINGS,
137	.sv_stackprot	= VM_PROT_ALL,
138	.sv_copyout_strings	= freebsd32_copyout_strings,
139	.sv_setregs	= ia32_setregs,
140	.sv_fixlimit	= ia32_fixlimit,
141	.sv_maxssiz	= &ia32_maxssiz,
142	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32,
143	.sv_set_syscall_retval = ia32_set_syscall_retval,
144	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
145	.sv_syscallnames = freebsd32_syscallnames,
146};
147#else
148#error "Port me"
149#endif
150
151static int
152aout_fixup(register_t **stack_base, struct image_params *imgp)
153{
154
155	*(char **)stack_base -= sizeof(uint32_t);
156	return (suword32(*stack_base, imgp->args->argc));
157}
158
159static int
160exec_aout_imgact(struct image_params *imgp)
161{
162	const struct exec *a_out = (const struct exec *) imgp->image_header;
163	struct vmspace *vmspace;
164	vm_map_t map;
165	vm_object_t object;
166	vm_offset_t text_end, data_end;
167	unsigned long virtual_offset;
168	unsigned long file_offset;
169	unsigned long bss_size;
170	int error;
171
172	/*
173	 * Linux and *BSD binaries look very much alike,
174	 * only the machine id is different:
175	 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
176	 * NetBSD is in network byte order.. ugh.
177	 */
178	if (((a_out->a_midmag >> 16) & 0xff) != 0x86 &&
179	    ((a_out->a_midmag >> 16) & 0xff) != 0 &&
180	    ((((int)ntohl(a_out->a_midmag)) >> 16) & 0xff) != 0x86)
181                return -1;
182
183	/*
184	 * Set file/virtual offset based on a.out variant.
185	 *	We do two cases: host byte order and network byte order
186	 *	(for NetBSD compatibility)
187	 */
188	switch ((int)(a_out->a_midmag & 0xffff)) {
189	case ZMAGIC:
190		virtual_offset = 0;
191		if (a_out->a_text) {
192			file_offset = PAGE_SIZE;
193		} else {
194			/* Bill's "screwball mode" */
195			file_offset = 0;
196		}
197		break;
198	case QMAGIC:
199		virtual_offset = PAGE_SIZE;
200		file_offset = 0;
201		/* Pass PS_STRINGS for BSD/OS binaries only. */
202		if (N_GETMID(*a_out) == MID_ZERO)
203			imgp->ps_strings = aout_sysvec.sv_psstrings;
204		break;
205	default:
206		/* NetBSD compatibility */
207		switch ((int)(ntohl(a_out->a_midmag) & 0xffff)) {
208		case ZMAGIC:
209		case QMAGIC:
210			virtual_offset = PAGE_SIZE;
211			file_offset = 0;
212			break;
213		default:
214			return (-1);
215		}
216	}
217
218	bss_size = roundup(a_out->a_bss, PAGE_SIZE);
219
220	/*
221	 * Check various fields in header for validity/bounds.
222	 */
223	if (/* entry point must lay with text region */
224	    a_out->a_entry < virtual_offset ||
225	    a_out->a_entry >= virtual_offset + a_out->a_text ||
226
227	    /* text and data size must each be page rounded */
228	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK
229
230#ifdef __amd64__
231	    ||
232	    /* overflows */
233	    virtual_offset + a_out->a_text + a_out->a_data + bss_size > UINT_MAX
234#endif
235	    )
236		return (-1);
237
238	/* text + data can't exceed file size */
239	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
240		return (EFAULT);
241
242	/*
243	 * text/data/bss must not exceed limits
244	 */
245	PROC_LOCK(imgp->proc);
246	if (/* text can't exceed maximum text size */
247	    a_out->a_text > maxtsiz ||
248
249	    /* data + bss can't exceed rlimit */
250	    a_out->a_data + bss_size > lim_cur_proc(imgp->proc, RLIMIT_DATA) ||
251	    racct_set(imgp->proc, RACCT_DATA, a_out->a_data + bss_size) != 0) {
252		PROC_UNLOCK(imgp->proc);
253		return (ENOMEM);
254	}
255	PROC_UNLOCK(imgp->proc);
256
257	/*
258	 * Avoid a possible deadlock if the current address space is destroyed
259	 * and that address space maps the locked vnode.  In the common case,
260	 * the locked vnode's v_usecount is decremented but remains greater
261	 * than zero.  Consequently, the vnode lock is not needed by vrele().
262	 * However, in cases where the vnode lock is external, such as nullfs,
263	 * v_usecount may become zero.
264	 */
265	VOP_UNLOCK(imgp->vp, 0);
266
267	/*
268	 * Destroy old process VM and create a new one (with a new stack)
269	 */
270	error = exec_new_vmspace(imgp, &aout_sysvec);
271
272	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
273	if (error)
274		return (error);
275
276	/*
277	 * The vm space can be changed by exec_new_vmspace
278	 */
279	vmspace = imgp->proc->p_vmspace;
280
281	object = imgp->object;
282	map = &vmspace->vm_map;
283	vm_map_lock(map);
284	vm_object_reference(object);
285
286	text_end = virtual_offset + a_out->a_text;
287	error = vm_map_insert(map, object,
288		file_offset,
289		virtual_offset, text_end,
290		VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
291		MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
292	if (error) {
293		vm_map_unlock(map);
294		vm_object_deallocate(object);
295		return (error);
296	}
297	VOP_SET_TEXT_CHECKED(imgp->vp);
298	data_end = text_end + a_out->a_data;
299	if (a_out->a_data) {
300		vm_object_reference(object);
301		error = vm_map_insert(map, object,
302			file_offset + a_out->a_text,
303			text_end, data_end,
304			VM_PROT_ALL, VM_PROT_ALL,
305			MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
306		if (error) {
307			vm_map_unlock(map);
308			vm_object_deallocate(object);
309			return (error);
310		}
311		VOP_SET_TEXT_CHECKED(imgp->vp);
312	}
313
314	if (bss_size) {
315		error = vm_map_insert(map, NULL, 0,
316			data_end, data_end + bss_size,
317			VM_PROT_ALL, VM_PROT_ALL, 0);
318		if (error) {
319			vm_map_unlock(map);
320			return (error);
321		}
322	}
323	vm_map_unlock(map);
324
325	/* Fill in process VM information */
326	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
327	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
328	vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
329	vmspace->vm_daddr = (caddr_t) (uintptr_t)
330			    (virtual_offset + a_out->a_text);
331
332	/* Fill in image_params */
333	imgp->interpreted = 0;
334	imgp->entry_addr = a_out->a_entry;
335
336	imgp->proc->p_sysent = &aout_sysvec;
337
338	return (0);
339}
340
341/*
342 * Tell kern_execve.c about it, with a little help from the linker.
343 */
344static struct execsw aout_execsw = {
345	.ex_imgact = exec_aout_imgact,
346	.ex_name = "a.out"
347};
348EXEC_SET(aout, aout_execsw);
349