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