xref: /netbsd-src/sys/compat/linux/common/linux_exec_elf32.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: linux_exec_elf32.c,v 1.80 2007/10/19 12:16:39 ad 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.80 2007/10/19 12:16:39 ad Exp $");
46 
47 #ifndef ELFSIZE
48 /* XXX should die */
49 #define	ELFSIZE		32
50 #endif
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
60 #include <sys/exec.h>
61 #include <sys/exec_elf.h>
62 #include <sys/stat.h>
63 #include <sys/kauth.h>
64 
65 #include <sys/mman.h>
66 #include <sys/syscallargs.h>
67 
68 #include <sys/cpu.h>
69 #include <machine/reg.h>
70 
71 #include <compat/linux/common/linux_types.h>
72 #include <compat/linux/common/linux_signal.h>
73 #include <compat/linux/common/linux_util.h>
74 #include <compat/linux/common/linux_exec.h>
75 #include <compat/linux/common/linux_machdep.h>
76 
77 #include <compat/linux/linux_syscallargs.h>
78 #include <compat/linux/linux_syscall.h>
79 
80 #ifdef DEBUG_LINUX
81 #define DPRINTF(a)	uprintf a
82 #else
83 #define DPRINTF(a)
84 #endif
85 
86 #ifdef LINUX_ATEXIT_SIGNATURE
87 /*
88  * On the PowerPC, statically linked Linux binaries are not recognized
89  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
90  * binaries features a __libc_atexit ELF section. We therefore assume we
91  * have a Linux binary if we find this section.
92  */
93 int
94 ELFNAME2(linux,atexit_signature)(l, epp, eh)
95 	struct lwp *l;
96 	struct exec_package *epp;
97 	Elf_Ehdr *eh;
98 {
99 	size_t shsize;
100 	int strndx;
101 	size_t i;
102 	static const char signature[] = "__libc_atexit";
103 	char *strtable = NULL;
104 	Elf_Shdr *sh;
105 
106 	int error;
107 
108 	/*
109 	 * load the section header table
110 	 */
111 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
112 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
113 	error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
114 	if (error)
115 		goto out;
116 
117 	/*
118 	 * Now let's find the string table. If it does not exists, give up.
119 	 */
120 	strndx = (int)(eh->e_shstrndx);
121 	if (strndx == SHN_UNDEF) {
122 		error = ENOEXEC;
123 		goto out;
124 	}
125 
126 	/*
127 	 * strndx is the index in section header table of the string table
128 	 * section get the whole string table in strtable, and then we get access to the names
129 	 * s->sh_name is the offset of the section name in strtable.
130 	 */
131 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
132 	error = exec_read_from(l, epp->ep_vp, sh[strndx].sh_offset, strtable,
133 	    sh[strndx].sh_size);
134 	if (error)
135 		goto out;
136 
137 	for (i = 0; i < eh->e_shnum; i++) {
138 		Elf_Shdr *s = &sh[i];
139 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
140 				sizeof(signature))) {
141 			DPRINTF(("linux_atexit_sig=%s\n",
142 			    &(strtable[s->sh_name])));
143 			error = 0;
144 			goto out;
145 		}
146 	}
147 	error = ENOEXEC;
148 
149 out:
150 	free(sh, M_TEMP);
151 	if (strtable)
152 		free(strtable, M_TEMP);
153 	return (error);
154 }
155 #endif
156 
157 #ifdef LINUX_GCC_SIGNATURE
158 /*
159  * Take advantage of the fact that all the linux binaries are compiled
160  * with gcc, and gcc sticks in the comment field a signature. Note that
161  * on SVR4 binaries, the gcc signature will follow the OS name signature,
162  * that will not be a problem. We don't bother to read in the string table,
163  * but we check all the progbits headers.
164  *
165  * XXX This only works in the i386.  On the alpha (at least)
166  * XXX we have the same gcc signature which incorrectly identifies
167  * XXX NetBSD binaries as Linux.
168  */
169 int
170 ELFNAME2(linux,gcc_signature)(l, epp, eh)
171 	struct lwp *l;
172 	struct exec_package *epp;
173 	Elf_Ehdr *eh;
174 {
175 	size_t shsize;
176 	size_t i;
177 	static const char signature[] = "\0GCC: (GNU) ";
178 	char tbuf[sizeof(signature) - 1];
179 	Elf_Shdr *sh;
180 	int error;
181 
182 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
183 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
184 	error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
185 	if (error)
186 		goto out;
187 
188 	for (i = 0; i < eh->e_shnum; i++) {
189 		Elf_Shdr *s = &sh[i];
190 
191 		/*
192 		 * Identify candidates for the comment header;
193 		 * Header cannot have a load address, or flags and
194 		 * it must be large enough.
195 		 */
196 		if (s->sh_type != SHT_PROGBITS ||
197 		    s->sh_addr != 0 ||
198 		    s->sh_flags != 0 ||
199 		    s->sh_size < sizeof(signature) - 1)
200 			continue;
201 
202 		error = exec_read_from(l, epp->ep_vp, s->sh_offset, tbuf,
203 		    sizeof(signature) - 1);
204 		if (error)
205 			continue;
206 
207 		/*
208 		 * error is 0, if the signatures match we are done.
209 		 */
210 		DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
211 		if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
212 			error = 0;
213 			goto out;
214 		}
215 	}
216 	error = ENOEXEC;
217 
218 out:
219 	free(sh, M_TEMP);
220 	return (error);
221 }
222 #endif
223 
224 #ifdef LINUX_DEBUGLINK_SIGNATURE
225 /*
226  * Look for a .gnu_debuglink, specific to x86_64 interpeter
227  */
228 int
229 ELFNAME2(linux,debuglink_signature)(l, epp, eh)
230 	struct lwp *l;
231 	struct exec_package *epp;
232 	Elf_Ehdr *eh;
233 {
234 	size_t shsize;
235 	int strndx;
236 	size_t i;
237 	static const char signature[] = ".gnu_debuglink";
238 	char *strtable = NULL;
239 	Elf_Shdr *sh;
240 
241 	int error;
242 
243 	/*
244 	 * load the section header table
245 	 */
246 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
247 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
248 	error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
249 	if (error)
250 		goto out;
251 
252 	/*
253 	 * Now let's find the string table. If it does not exists, give up.
254 	 */
255 	strndx = (int)(eh->e_shstrndx);
256 	if (strndx == SHN_UNDEF) {
257 		error = ENOEXEC;
258 		goto out;
259 	}
260 
261 	/*
262 	 * strndx is the index in section header table of the string table
263 	 * section get the whole string table in strtable, and then we get access to the names
264 	 * s->sh_name is the offset of the section name in strtable.
265 	 */
266 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
267 	error = exec_read_from(l, epp->ep_vp, sh[strndx].sh_offset, strtable,
268 	    sh[strndx].sh_size);
269 	if (error)
270 		goto out;
271 
272 	for (i = 0; i < eh->e_shnum; i++) {
273 		Elf_Shdr *s = &sh[i];
274 
275 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
276 				sizeof(signature))) {
277 			DPRINTF(("linux_debuglink_sig=%s\n",
278 			    &(strtable[s->sh_name])));
279 			error = 0;
280 			goto out;
281 		}
282 	}
283 	error = ENOEXEC;
284 
285 out:
286 	free(sh, M_TEMP);
287 	if (strtable)
288 		free(strtable, M_TEMP);
289 	return (error);
290 }
291 #endif
292 
293 int
294 ELFNAME2(linux,signature)(l, epp, eh, itp)
295 	struct lwp *l;
296 	struct exec_package *epp;
297 	Elf_Ehdr *eh;
298 	char *itp;
299 {
300 	size_t i;
301 	Elf_Phdr *ph;
302 	size_t phsize;
303 	int error;
304 	static const char linux[] = "Linux";
305 
306 	if (eh->e_ident[EI_OSABI] == 3 ||
307 	    memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
308 		return 0;
309 
310 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
311 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
312 	error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
313 	if (error)
314 		goto out;
315 
316 	for (i = 0; i < eh->e_phnum; i++) {
317 		Elf_Phdr *ephp = &ph[i];
318 		Elf_Nhdr *np;
319 		u_int32_t *abi;
320 
321 		if (ephp->p_type != PT_NOTE ||
322 		    ephp->p_filesz > 1024 ||
323 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
324 			continue;
325 
326 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
327 		error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
328 		    ephp->p_filesz);
329 		if (error)
330 			goto next;
331 
332 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
333 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
334 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
335 		    memcmp((void *)(np + 1), ELF_NOTE_ABI_NAME,
336 		    ELF_NOTE_ABI_NAMESZ))
337 			goto next;
338 
339 		/* Make sure the OS is Linux. */
340 		abi = (u_int32_t *)((char *)np + sizeof(Elf_Nhdr) +
341 		    np->n_namesz);
342 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
343 			error = 0;
344 		else
345 			error = ENOEXEC;
346 		free(np, M_TEMP);
347 		goto out;
348 
349 	next:
350 		free(np, M_TEMP);
351 		continue;
352 	}
353 
354 	/* Check for certain intepreter names. */
355 	if (itp) {
356 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
357 #if (ELFSIZE == 64)
358 		    !strncmp(itp, "/lib64/ld-linux", 15) ||
359 #endif
360 		    !strncmp(itp, "/lib/ld.so.", 11))
361 			error = 0;
362 		else
363 			error = ENOEXEC;
364 		goto out;
365 	}
366 
367 	error = ENOEXEC;
368 out:
369 	free(ph, M_TEMP);
370 	return (error);
371 }
372 
373 int
374 ELFNAME2(linux,probe)(struct lwp *l, struct exec_package *epp, void *eh,
375     char *itp, vaddr_t *pos)
376 {
377 	int error;
378 
379 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
380 #ifdef LINUX_GCC_SIGNATURE
381 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
382 #endif
383 #ifdef LINUX_ATEXIT_SIGNATURE
384 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
385 #endif
386 #ifdef LINUX_DEBUGLINK_SIGNATURE
387 	    ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
388 #endif
389 	    1) {
390 			DPRINTF(("linux_probe: returning %d\n", error));
391 			return error;
392 	}
393 
394 	if (itp) {
395 		if ((error = emul_find_interp(l, epp, itp)))
396 			return (error);
397 	}
398 	DPRINTF(("linux_probe: returning 0\n"));
399 	return 0;
400 }
401 
402 #ifndef LINUX_MACHDEP_ELF_COPYARGS
403 /*
404  * Copy arguments onto the stack in the normal way, but add some
405  * extra information in case of dynamic binding.
406  */
407 int
408 ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
409     struct ps_strings *arginfo, char **stackp, void *argp)
410 {
411 	size_t len;
412 	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
413 	struct elf_args *ap;
414 	int error;
415 	struct vattr *vap;
416 
417 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
418 		return error;
419 
420 	a = ai;
421 
422 	/*
423 	 * Push extra arguments used by glibc on the stack.
424 	 */
425 
426 	a->a_type = AT_PAGESZ;
427 	a->a_v = PAGE_SIZE;
428 	a++;
429 
430 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
431 
432 		a->a_type = AT_PHDR;
433 		a->a_v = ap->arg_phaddr;
434 		a++;
435 
436 		a->a_type = AT_PHENT;
437 		a->a_v = ap->arg_phentsize;
438 		a++;
439 
440 		a->a_type = AT_PHNUM;
441 		a->a_v = ap->arg_phnum;
442 		a++;
443 
444 		a->a_type = AT_BASE;
445 		a->a_v = ap->arg_interp;
446 		a++;
447 
448 		a->a_type = AT_FLAGS;
449 		a->a_v = 0;
450 		a++;
451 
452 		a->a_type = AT_ENTRY;
453 		a->a_v = ap->arg_entry;
454 		a++;
455 
456 		free(pack->ep_emul_arg, M_TEMP);
457 		pack->ep_emul_arg = NULL;
458 	}
459 
460 	/* Linux-specific items */
461 	a->a_type = LINUX_AT_CLKTCK;
462 	a->a_v = hz;
463 	a++;
464 
465 	vap = pack->ep_vap;
466 
467 	a->a_type = LINUX_AT_UID;
468 	a->a_v = kauth_cred_getuid(l->l_cred);
469 	a++;
470 
471 	a->a_type = LINUX_AT_EUID;
472 	if (vap->va_mode & S_ISUID)
473 		a->a_v = vap->va_uid;
474 	else
475 		a->a_v = kauth_cred_geteuid(l->l_cred);
476 	a++;
477 
478 	a->a_type = LINUX_AT_GID;
479 	a->a_v = kauth_cred_getgid(l->l_cred);
480 	a++;
481 
482 	a->a_type = LINUX_AT_EGID;
483 	if (vap->va_mode & S_ISGID)
484 		a->a_v = vap->va_gid;
485 	else
486 		a->a_v = kauth_cred_getegid(l->l_cred);
487 	a++;
488 
489 	a->a_type = AT_NULL;
490 	a->a_v = 0;
491 	a++;
492 
493 	len = (a - ai) * sizeof(AuxInfo);
494 	if ((error = copyout(ai, *stackp, len)) != 0)
495 		return error;
496 	*stackp += len;
497 
498 	return 0;
499 }
500 #endif /* !LINUX_MACHDEP_ELF_COPYARGS */
501