xref: /freebsd/sys/kern/imgact_aout.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
31 #include <sys/exec.h>
32 #include <sys/imgact.h>
33 #include <sys/imgact_aout.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/racct.h>
41 #include <sys/resourcevar.h>
42 #include <sys/signalvar.h>
43 #include <sys/syscall.h>
44 #include <sys/sysent.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 
48 #include <machine/frame.h>
49 #include <machine/md_var.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_param.h>
56 
57 #ifdef __amd64__
58 #include <compat/freebsd32/freebsd32_signal.h>
59 #include <compat/freebsd32/freebsd32_util.h>
60 #include <compat/freebsd32/freebsd32_proto.h>
61 #include <compat/freebsd32/freebsd32_syscall.h>
62 #include <compat/ia32/ia32_signal.h>
63 #endif
64 
65 static int	exec_aout_imgact(struct image_params *imgp);
66 static int	aout_fixup(uintptr_t *stack_base, struct image_params *imgp);
67 
68 #define	AOUT32_USRSTACK		0xbfc00000
69 
70 #if defined(__i386__)
71 
72 #define	AOUT32_PS_STRINGS	(AOUT32_USRSTACK - sizeof(struct ps_strings))
73 
74 struct sysentvec aout_sysvec = {
75 	.sv_size	= SYS_MAXSYSCALL,
76 	.sv_table	= sysent,
77 	.sv_fixup	= aout_fixup,
78 	.sv_sendsig	= sendsig,
79 	.sv_sigcode	= sigcode,
80 	.sv_szsigcode	= &szsigcode,
81 	.sv_name	= "FreeBSD a.out",
82 	.sv_coredump	= NULL,
83 	.sv_minsigstksz	= MINSIGSTKSZ,
84 	.sv_minuser	= VM_MIN_ADDRESS,
85 	.sv_maxuser	= AOUT32_USRSTACK,
86 	.sv_usrstack	= AOUT32_USRSTACK,
87 	.sv_psstrings	= AOUT32_PS_STRINGS,
88 	.sv_psstringssz	= sizeof(struct ps_strings),
89 	.sv_stackprot	= VM_PROT_ALL,
90 	.sv_copyout_strings	= exec_copyout_strings,
91 	.sv_setregs	= exec_setregs,
92 	.sv_fixlimit	= NULL,
93 	.sv_maxssiz	= NULL,
94 	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32,
95 	.sv_set_syscall_retval = cpu_set_syscall_retval,
96 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
97 	.sv_syscallnames = syscallnames,
98 	.sv_schedtail	= NULL,
99 	.sv_thread_detach = NULL,
100 	.sv_trap	= NULL,
101 	.sv_onexec_old = exec_onexec_old,
102 	.sv_onexit =  exit_onexit,
103 	.sv_set_fork_retval = x86_set_fork_retval,
104 };
105 
106 #elif defined(__amd64__)
107 
108 #include "vdso_ia32_offsets.h"
109 
110 extern const char _binary_elf_vdso32_so_1_start[];
111 extern const char _binary_elf_vdso32_so_1_end[];
112 extern char _binary_elf_vdso32_so_1_size;
113 
114 #define	AOUT32_PS_STRINGS \
115     (AOUT32_USRSTACK - sizeof(struct freebsd32_ps_strings))
116 #define	AOUT32_MINUSER		FREEBSD32_MINUSER
117 
118 extern const char *freebsd32_syscallnames[];
119 extern u_long ia32_maxssiz;
120 
121 static int aout_szsigcode;
122 
123 struct sysentvec aout_sysvec = {
124 	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
125 	.sv_table	= freebsd32_sysent,
126 	.sv_fixup	= aout_fixup,
127 	.sv_sendsig	= ia32_sendsig,
128 	.sv_sigcode	= _binary_elf_vdso32_so_1_start,
129 	.sv_szsigcode	= &aout_szsigcode,
130 	.sv_name	= "FreeBSD a.out",
131 	.sv_coredump	= 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_psstringssz	= sizeof(struct freebsd32_ps_strings),
138 	.sv_stackprot	= VM_PROT_ALL,
139 	.sv_copyout_strings	= freebsd32_copyout_strings,
140 	.sv_setregs	= ia32_setregs,
141 	.sv_fixlimit	= ia32_fixlimit,
142 	.sv_maxssiz	= &ia32_maxssiz,
143 	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32,
144 	.sv_set_syscall_retval = ia32_set_syscall_retval,
145 	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
146 	.sv_syscallnames = freebsd32_syscallnames,
147 	.sv_onexec_old	= exec_onexec_old,
148 	.sv_onexit	= exit_onexit,
149 	.sv_set_fork_retval = x86_set_fork_retval,
150 };
151 
152 static void
153 aout_sysent(void *arg __unused)
154 {
155 	aout_szsigcode = (int)(uintptr_t)&_binary_elf_vdso32_so_1_size;
156 }
157 SYSINIT(aout_sysent, SI_SUB_EXEC, SI_ORDER_ANY, aout_sysent, NULL);
158 #else
159 #error "Only ia32 arch is supported"
160 #endif
161 
162 static int
163 aout_fixup(uintptr_t *stack_base, struct image_params *imgp)
164 {
165 
166 	*stack_base -= sizeof(uint32_t);
167 	if (suword32((void *)*stack_base, imgp->args->argc) != 0)
168 		return (EFAULT);
169 	return (0);
170 }
171 
172 static int
173 exec_aout_imgact(struct image_params *imgp)
174 {
175 	const struct exec *a_out;
176 	struct vmspace *vmspace;
177 	vm_map_t map;
178 	vm_object_t object;
179 	vm_offset_t text_end, data_end;
180 	unsigned long virtual_offset;
181 	unsigned long file_offset;
182 	unsigned long bss_size;
183 	int error;
184 
185 	a_out = (const struct exec *)imgp->image_header;
186 
187 	/*
188 	 * Linux and *BSD binaries look very much alike,
189 	 * only the machine id is different:
190 	 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
191 	 * NetBSD is in network byte order.. ugh.
192 	 */
193 	if (((a_out->a_midmag >> 16) & 0xff) != 0x86 &&
194 	    ((a_out->a_midmag >> 16) & 0xff) != 0 &&
195 	    ((((int)ntohl(a_out->a_midmag)) >> 16) & 0xff) != 0x86)
196                 return (-1);
197 
198 	/*
199 	 * Set file/virtual offset based on a.out variant.
200 	 *	We do two cases: host byte order and network byte order
201 	 *	(for NetBSD compatibility)
202 	 */
203 	switch ((int)(a_out->a_midmag & 0xffff)) {
204 	case ZMAGIC:
205 		virtual_offset = 0;
206 		if (a_out->a_text) {
207 			file_offset = PAGE_SIZE;
208 		} else {
209 			/* Bill's "screwball mode" */
210 			file_offset = 0;
211 		}
212 		break;
213 	case QMAGIC:
214 		virtual_offset = PAGE_SIZE;
215 		file_offset = 0;
216 		/* Pass PS_STRINGS for BSD/OS binaries only. */
217 		if (N_GETMID(*a_out) == MID_ZERO)
218 			imgp->ps_strings = (void *)aout_sysvec.sv_psstrings;
219 		break;
220 	default:
221 		/* NetBSD compatibility */
222 		switch ((int)(ntohl(a_out->a_midmag) & 0xffff)) {
223 		case ZMAGIC:
224 		case QMAGIC:
225 			virtual_offset = PAGE_SIZE;
226 			file_offset = 0;
227 			break;
228 		default:
229 			return (-1);
230 		}
231 	}
232 
233 	bss_size = roundup(a_out->a_bss, PAGE_SIZE);
234 
235 	/*
236 	 * Check various fields in header for validity/bounds.
237 	 */
238 	if (/* entry point must lay with text region */
239 	    a_out->a_entry < virtual_offset ||
240 	    a_out->a_entry >= virtual_offset + a_out->a_text ||
241 
242 	    /* text and data size must each be page rounded */
243 	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK
244 
245 #ifdef __amd64__
246 	    ||
247 	    /* overflows */
248 	    virtual_offset + a_out->a_text + a_out->a_data + bss_size > UINT_MAX
249 #endif
250 	    )
251 		return (-1);
252 
253 	/* text + data can't exceed file size */
254 	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
255 		return (EFAULT);
256 
257 	/*
258 	 * text/data/bss must not exceed limits
259 	 */
260 	PROC_LOCK(imgp->proc);
261 	if (/* text can't exceed maximum text size */
262 	    a_out->a_text > maxtsiz ||
263 
264 	    /* data + bss can't exceed rlimit */
265 	    a_out->a_data + bss_size > lim_cur_proc(imgp->proc, RLIMIT_DATA) ||
266 	    racct_set(imgp->proc, RACCT_DATA, a_out->a_data + bss_size) != 0) {
267 		PROC_UNLOCK(imgp->proc);
268 		return (ENOMEM);
269 	}
270 	PROC_UNLOCK(imgp->proc);
271 
272 	/*
273 	 * Avoid a possible deadlock if the current address space is destroyed
274 	 * and that address space maps the locked vnode.  In the common case,
275 	 * the locked vnode's v_usecount is decremented but remains greater
276 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
277 	 * However, in cases where the vnode lock is external, such as nullfs,
278 	 * v_usecount may become zero.
279 	 */
280 	VOP_UNLOCK(imgp->vp);
281 
282 	/*
283 	 * Destroy old process VM and create a new one (with a new stack)
284 	 */
285 	error = exec_new_vmspace(imgp, &aout_sysvec);
286 
287 	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
288 	if (error)
289 		return (error);
290 
291 	/*
292 	 * The vm space can be changed by exec_new_vmspace
293 	 */
294 	vmspace = imgp->proc->p_vmspace;
295 
296 	object = imgp->object;
297 	map = &vmspace->vm_map;
298 	vm_map_lock(map);
299 	vm_object_reference(object);
300 
301 	text_end = virtual_offset + a_out->a_text;
302 	error = vm_map_insert(map, object,
303 		file_offset,
304 		virtual_offset, text_end,
305 		VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
306 		MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
307 	if (error) {
308 		vm_map_unlock(map);
309 		vm_object_deallocate(object);
310 		return (error);
311 	}
312 	VOP_SET_TEXT_CHECKED(imgp->vp);
313 	data_end = text_end + a_out->a_data;
314 	if (a_out->a_data) {
315 		vm_object_reference(object);
316 		error = vm_map_insert(map, object,
317 			file_offset + a_out->a_text,
318 			text_end, data_end,
319 			VM_PROT_ALL, VM_PROT_ALL,
320 			MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
321 		if (error) {
322 			vm_map_unlock(map);
323 			vm_object_deallocate(object);
324 			return (error);
325 		}
326 		VOP_SET_TEXT_CHECKED(imgp->vp);
327 	}
328 
329 	if (bss_size) {
330 		error = vm_map_insert(map, NULL, 0,
331 			data_end, data_end + bss_size,
332 			VM_PROT_ALL, VM_PROT_ALL, 0);
333 		if (error) {
334 			vm_map_unlock(map);
335 			return (error);
336 		}
337 	}
338 	vm_map_unlock(map);
339 
340 	/* Fill in process VM information */
341 	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
342 	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
343 	vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
344 	vmspace->vm_daddr = (caddr_t) (uintptr_t)
345 			    (virtual_offset + a_out->a_text);
346 
347 	error = exec_map_stack(imgp);
348 	if (error != 0)
349 		return (error);
350 
351 	/* Fill in image_params */
352 	imgp->interpreted = 0;
353 	imgp->entry_addr = a_out->a_entry;
354 
355 	imgp->proc->p_sysent = &aout_sysvec;
356 
357 	return (0);
358 }
359 
360 /*
361  * Tell kern_execve.c about it, with a little help from the linker.
362  */
363 static struct execsw aout_execsw = {
364 	.ex_imgact = exec_aout_imgact,
365 	.ex_name = "a.out"
366 };
367 EXEC_SET(aout, aout_execsw);
368