xref: /freebsd/sys/i386/linux/imgact_linux.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1996 Søren Schmidt
5  * All rights reserved.
6  *
7  * Based heavily on /sys/kern/imgact_aout.c which is:
8  * Copyright (c) 1993, David Greenman
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/exec.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mman.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/racct.h>
44 #include <sys/resourcevar.h>
45 #include <sys/vnode.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
53 
54 #include <i386/linux/linux.h>
55 
56 static int	exec_linux_imgact(struct image_params *iparams);
57 
58 static int
59 exec_linux_imgact(struct image_params *imgp)
60 {
61 	const struct exec *a_out = (const struct exec *) imgp->image_header;
62 	struct vmspace *vmspace;
63 	vm_offset_t vmaddr;
64 	unsigned long virtual_offset, file_offset;
65 	unsigned long bss_size;
66 	ssize_t aresid;
67 	int error;
68 
69 	if (((a_out->a_magic >> 16) & 0xff) != 0x64)
70 		return (-1);
71 
72 	/*
73 	 * Set file/virtual offset based on a.out variant.
74 	 */
75 	switch ((int)(a_out->a_magic & 0xffff)) {
76 	case 0413:
77 		virtual_offset = 0;
78 		file_offset = 1024;
79 		break;
80 	case 0314:
81 		virtual_offset = 4096;
82 		file_offset = 0;
83 		break;
84 	default:
85 		return (-1);
86 	}
87 	bss_size = round_page(a_out->a_bss);
88 #ifdef DEBUG
89 	printf("imgact: text: %08lx, data: %08lx, bss: %08lx\n",
90 	    (u_long)a_out->a_text, (u_long)a_out->a_data, bss_size);
91 #endif
92 
93 	/*
94 	 * Check various fields in header for validity/bounds.
95 	 */
96 	if (a_out->a_entry < virtual_offset ||
97 	    a_out->a_entry >= virtual_offset + a_out->a_text ||
98 	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
99 		return (-1);
100 
101 	/* text + data can't exceed file size */
102 	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
103 		return (EFAULT);
104 	/*
105 	 * text/data/bss must not exceed limits
106 	 */
107 	PROC_LOCK(imgp->proc);
108 	if (a_out->a_text > maxtsiz ||
109 	    a_out->a_data + bss_size > lim_cur_proc(imgp->proc, RLIMIT_DATA) ||
110 	    racct_set(imgp->proc, RACCT_DATA, a_out->a_data + bss_size) != 0) {
111 		PROC_UNLOCK(imgp->proc);
112 		return (ENOMEM);
113 	}
114 	PROC_UNLOCK(imgp->proc);
115 
116 	VOP_UNLOCK(imgp->vp);
117 
118 	/*
119 	 * Destroy old process VM and create a new one (with a new stack)
120 	 */
121 	error = exec_new_vmspace(imgp, &linux_sysvec);
122 	if (error)
123 		goto fail;
124 	vmspace = imgp->proc->p_vmspace;
125 
126 	/*
127 	 * Check if file_offset page aligned,.
128 	 * Currently we cannot handle misaligned file offsets,
129 	 * and so we read in the entire image (what a waste).
130 	 */
131 	if (file_offset & PAGE_MASK) {
132 #ifdef DEBUG
133 		printf("imgact: Non page aligned binary %lu\n", file_offset);
134 #endif
135 		/*
136 		 * Map text+data+bss read/write/execute
137 		 */
138 		vmaddr = virtual_offset;
139 		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
140 		    a_out->a_text + a_out->a_data + bss_size, 0, VMFS_NO_SPACE,
141 		    VM_PROT_ALL, VM_PROT_ALL, 0);
142 		if (error)
143 			goto fail;
144 
145 		error = vn_rdwr(UIO_READ, imgp->vp, (void *)vmaddr, file_offset,
146 		    a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
147 		    curthread->td_ucred, NOCRED, &aresid, curthread);
148 		if (error != 0)
149 			goto fail;
150 		if (aresid != 0) {
151 			error = ENOEXEC;
152 			goto fail;
153 		}
154 
155 		/*
156 		 * remove write enable on the 'text' part
157 		 */
158 		error = vm_map_protect(&vmspace->vm_map, vmaddr,
159 		    vmaddr + a_out->a_text, 0, VM_PROT_EXECUTE | VM_PROT_READ,
160 		    VM_MAP_PROTECT_SET_MAXPROT);
161 		if (error)
162 			goto fail;
163 	} else {
164 #ifdef DEBUG
165 		printf("imgact: Page aligned binary %lu\n", file_offset);
166 #endif
167 		/*
168 		 * Map text+data read/execute
169 		 */
170 		vmaddr = virtual_offset;
171 		error = vm_mmap(&vmspace->vm_map, &vmaddr,
172 		    a_out->a_text + a_out->a_data,
173 		    VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
174 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, imgp->vp, file_offset);
175 		if (error)
176 			goto fail;
177 
178 #ifdef DEBUG
179 		printf("imgact: startaddr=%08lx, length=%08lx\n",
180 		    (u_long)vmaddr,
181 		    (u_long)a_out->a_text + (u_long)a_out->a_data);
182 #endif
183 		/*
184 		 * allow read/write of data
185 		 */
186 		error = vm_map_protect(&vmspace->vm_map, vmaddr + a_out->a_text,
187 		    vmaddr + a_out->a_text + a_out->a_data, VM_PROT_ALL, 0,
188 		    VM_MAP_PROTECT_SET_PROT);
189 		if (error)
190 			goto fail;
191 
192 		/*
193 		 * Allocate anon demand-zeroed area for uninitialized data
194 		 */
195 		if (bss_size != 0) {
196 			vmaddr = virtual_offset + a_out->a_text + a_out->a_data;
197 		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
198 		    bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
199 		if (error)
200 			goto fail;
201 #ifdef DEBUG
202 		printf("imgact: bssaddr=%08lx, length=%08lx\n", (u_long)vmaddr,
203 		    bss_size);
204 #endif
205 		}
206 	}
207 	/* Fill in process VM information */
208 	vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT;
209 	vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT;
210 	vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)virtual_offset;
211 	vmspace->vm_daddr =
212 	    (caddr_t)(void *)(uintptr_t)(virtual_offset + a_out->a_text);
213 
214 	error = exec_map_stack(imgp);
215 	if (error != 0)
216 		goto fail;
217 
218 	/* Fill in image_params */
219 	imgp->interpreted = 0;
220 	imgp->entry_addr = a_out->a_entry;
221 
222 	imgp->proc->p_sysent = &linux_sysvec;
223 
224 fail:
225 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
226 	return (error);
227 }
228 
229 /*
230  * Tell kern_execve.c about it, with a little help from the linker.
231  */
232 static struct execsw linux_execsw = { exec_linux_imgact, "Linux a.out" };
233 EXEC_SET(linuxaout, linux_execsw);
234