1 /*	$NetBSD: linux_exec_elf32.c,v 1.56 2002/03/22 17:14:18 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
9  * Emmanuel Dreyfus.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.56 2002/03/22 17:14:18 christos Exp $");
46 
47 #ifndef ELFSIZE
48 #define	ELFSIZE		32				/* XXX should die */
49 #endif
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/proc.h>
55 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/exec.h>
60 #include <sys/exec_elf.h>
61 
62 #include <sys/mman.h>
63 #include <sys/syscallargs.h>
64 
65 #include <machine/cpu.h>
66 #include <machine/reg.h>
67 
68 #include <compat/linux/common/linux_types.h>
69 #include <compat/linux/common/linux_signal.h>
70 #include <compat/linux/common/linux_util.h>
71 #include <compat/linux/common/linux_exec.h>
72 #include <compat/linux/common/linux_machdep.h>
73 
74 #include <compat/linux/linux_syscallargs.h>
75 #include <compat/linux/linux_syscall.h>
76 
77 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
78 	Elf_Ehdr *, char *));
79 #ifdef LINUX_GCC_SIGNATURE
80 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
81 	struct exec_package *, Elf_Ehdr *));
82 #endif
83 #ifdef LINUX_ATEXIT_SIGNATURE
84 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
85 	struct exec_package *, Elf_Ehdr *));
86 #endif
87 
88 #ifdef DEBUG_LINUX
89 #define DPRINTF(a)	uprintf a
90 #else
91 #define DPRINTF(a)
92 #endif
93 
94 #ifdef LINUX_ATEXIT_SIGNATURE
95 /*
96  * On the PowerPC, statically linked Linux binaries are not recognized
97  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
98  * binaries features a __libc_atexit ELF section. We therefore assume we
99  * have a Linux binary if we find this section.
100  */
101 static int
102 ELFNAME2(linux,atexit_signature)(p, epp, eh)
103 	struct proc *p;
104 	struct exec_package *epp;
105 	Elf_Ehdr *eh;
106 {
107 	size_t shsize;
108 	int	strndx;
109 	size_t i;
110 	static const char signature[] = "__libc_atexit";
111 	char* strtable;
112 	Elf_Shdr *sh;
113 
114 	int error;
115 
116 	/*
117 	 * load the section header table
118 	 */
119 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
120 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
121 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
122 	if (error)
123 		goto out;
124 
125 	/*
126 	 * Now let's find the string table. If it does not exists, give up.
127 	 */
128 	strndx = (int)(eh->e_shstrndx);
129 	if (strndx == SHN_UNDEF) {
130 		error = ENOEXEC;
131 		goto out;
132 	}
133 
134 	/*
135 	 * strndx is the index in section header table of the string table
136 	 * section get the whole string table in strtable, and then we get access to the names
137 	 * s->sh_name is the offset of the section name in strtable.
138 	 */
139 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
140 	error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
141 	    sh[strndx].sh_size);
142 	if (error)
143 		goto out;
144 
145 	for (i = 0; i < eh->e_shnum; i++) {
146 		Elf_Shdr *s = &sh[i];
147 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
148 				sizeof(signature))) {
149 			DPRINTF(("linux_atexit_sig=%s\n",
150 			    &(strtable[s->sh_name])));
151 			error = 0;
152 			goto out;
153 		}
154 	}
155 	error = ENOEXEC;
156 
157 out:
158 	free(sh, M_TEMP);
159 	free(strtable, M_TEMP);
160 	return (error);
161 }
162 #endif
163 
164 #ifdef LINUX_GCC_SIGNATURE
165 /*
166  * Take advantage of the fact that all the linux binaries are compiled
167  * with gcc, and gcc sticks in the comment field a signature. Note that
168  * on SVR4 binaries, the gcc signature will follow the OS name signature,
169  * that will not be a problem. We don't bother to read in the string table,
170  * but we check all the progbits headers.
171  *
172  * XXX This only works in the i386.  On the alpha (at least)
173  * XXX we have the same gcc signature which incorrectly identifies
174  * XXX NetBSD binaries as Linux.
175  */
176 static int
177 ELFNAME2(linux,gcc_signature)(p, epp, eh)
178 	struct proc *p;
179 	struct exec_package *epp;
180 	Elf_Ehdr *eh;
181 {
182 	size_t shsize;
183 	size_t i;
184 	static const char signature[] = "\0GCC: (GNU) ";
185 	char buf[sizeof(signature) - 1];
186 	Elf_Shdr *sh;
187 	int error;
188 
189 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
190 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
191 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
192 	if (error)
193 		goto out;
194 
195 	for (i = 0; i < eh->e_shnum; i++) {
196 		Elf_Shdr *s = &sh[i];
197 
198 		/*
199 		 * Identify candidates for the comment header;
200 		 * Header cannot have a load address, or flags and
201 		 * it must be large enough.
202 		 */
203 		if (s->sh_type != SHT_PROGBITS ||
204 		    s->sh_addr != 0 ||
205 		    s->sh_flags != 0 ||
206 		    s->sh_size < sizeof(signature) - 1)
207 			continue;
208 
209 		error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
210 		    sizeof(signature) - 1);
211 		if (error)
212 			continue;
213 
214 		/*
215 		 * error is 0, if the signatures match we are done.
216 		 */
217 		DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
218 		if (!memcmp(buf, signature, sizeof(signature) - 1)) {
219 			error = 0;
220 			goto out;
221 		}
222 	}
223 	error = ENOEXEC;
224 
225 out:
226 	free(sh, M_TEMP);
227 	return (error);
228 }
229 #endif
230 
231 static int
232 ELFNAME2(linux,signature)(p, epp, eh, itp)
233 	struct proc *p;
234 	struct exec_package *epp;
235 	Elf_Ehdr *eh;
236 	char *itp;
237 {
238 	size_t i;
239 	Elf_Phdr *ph;
240 	size_t phsize;
241 	int error;
242 
243 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
244 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
245 	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
246 	if (error)
247 		goto out;
248 
249 	for (i = 0; i < eh->e_phnum; i++) {
250 		Elf_Phdr *ephp = &ph[i];
251 		Elf_Nhdr *np;
252 		u_int32_t *abi;
253 
254 		if (ephp->p_type != PT_NOTE ||
255 		    ephp->p_filesz > 1024 ||
256 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
257 			continue;
258 
259 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
260 		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
261 		    ephp->p_filesz);
262 		if (error)
263 			goto next;
264 
265 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
266 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
267 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
268 		    memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
269 		    ELF_NOTE_ABI_NAMESZ))
270 			goto next;
271 
272 		/* Make sure the OS is Linux. */
273 		abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
274 		    np->n_namesz);
275 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
276 			error = 0;
277 		else
278 			error = ENOEXEC;
279 		free(np, M_TEMP);
280 		goto out;
281 
282 	next:
283 		free(np, M_TEMP);
284 		continue;
285 	}
286 
287 	/* Check for certain intepreter names. */
288 	if (itp[0]) {
289 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
290 		    !strncmp(itp, "/lib/ld.so.", 11))
291 			error = 0;
292 		else
293 			error = ENOEXEC;
294 		goto out;
295 	}
296 
297 	error = ENOEXEC;
298 out:
299 	free(ph, M_TEMP);
300 	return (error);
301 }
302 
303 int
304 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
305 	struct proc *p;
306 	struct exec_package *epp;
307 	void *eh;
308 	char *itp;
309 	vaddr_t *pos;
310 {
311 	const char *bp;
312 	int error;
313 	size_t len;
314 
315 	if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
316 #ifdef LINUX_GCC_SIGNATURE
317 	    ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
318 #endif
319 #ifdef LINUX_ATEXIT_SIGNATURE
320 	    ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
321 #endif
322 	    1)
323 			return error;
324 
325 	if (itp[0]) {
326 		if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
327 		    itp, &bp, 0)))
328 			return error;
329 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
330 			return error;
331 		free((void *)bp, M_TEMP);
332 	}
333 	*pos = ELF_NO_ADDR;
334 	DPRINTF(("linux_probe: returning 0\n"));
335 	return 0;
336 }
337 
338