xref: /netbsd-src/sys/compat/linux/common/linux_exec_elf32.c (revision 836f4a42d7a79a1c175b37993d30bcd89d8b0e5c)
1*836f4a42Sryo /*	$NetBSD: linux_exec_elf32.c,v 1.102 2021/11/26 09:05:05 ryo Exp $	*/
28fb507a3Schristos 
38fb507a3Schristos /*-
4ac10cf69Smanu  * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
58fb507a3Schristos  * All rights reserved.
68fb507a3Schristos  *
78fb507a3Schristos  * This code is derived from software contributed to The NetBSD Foundation
8ac10cf69Smanu  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
9ac10cf69Smanu  * Emmanuel Dreyfus.
108fb507a3Schristos  *
118fb507a3Schristos  * Redistribution and use in source and binary forms, with or without
128fb507a3Schristos  * modification, are permitted provided that the following conditions
138fb507a3Schristos  * are met:
148fb507a3Schristos  * 1. Redistributions of source code must retain the above copyright
158fb507a3Schristos  *    notice, this list of conditions and the following disclaimer.
168fb507a3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
178fb507a3Schristos  *    notice, this list of conditions and the following disclaimer in the
188fb507a3Schristos  *    documentation and/or other materials provided with the distribution.
198fb507a3Schristos  *
208fb507a3Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
218fb507a3Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
228fb507a3Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
238fb507a3Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
248fb507a3Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
258fb507a3Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
268fb507a3Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
278fb507a3Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
288fb507a3Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
298fb507a3Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
308fb507a3Schristos  * POSSIBILITY OF SUCH DAMAGE.
318fb507a3Schristos  */
323bf459f3Sfvdl 
333bf459f3Sfvdl /*
34fb777788Sfvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
353bf459f3Sfvdl  */
363bf459f3Sfvdl 
37dab6ef8bSlukem #include <sys/cdefs.h>
38*836f4a42Sryo __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.102 2021/11/26 09:05:05 ryo Exp $");
39dab6ef8bSlukem 
40f7ac1bd3Serh #ifndef ELFSIZE
41ea0cda2cSchristos /* XXX should die */
42ea0cda2cSchristos #define	ELFSIZE		32
43f7ac1bd3Serh #endif
449c3e274cScgd 
453bf459f3Sfvdl #include <sys/param.h>
463bf459f3Sfvdl #include <sys/systm.h>
473bf459f3Sfvdl #include <sys/kernel.h>
483bf459f3Sfvdl #include <sys/proc.h>
493bf459f3Sfvdl #include <sys/malloc.h>
503bf459f3Sfvdl #include <sys/namei.h>
513bf459f3Sfvdl #include <sys/vnode.h>
52151fa70fSchristos #include <sys/mount.h>
53d551a4edSchristos #include <sys/exec.h>
54c4aaa600Sfvdl #include <sys/exec_elf.h>
55cee43b67Sjdolecek #include <sys/stat.h>
56874fef37Selad #include <sys/kauth.h>
57e5a75de9Schs #include <sys/cprng.h>
58d91f98a8Spgoyette #include <sys/compat_stub.h>
593bf459f3Sfvdl 
603bf459f3Sfvdl #include <sys/mman.h>
61151fa70fSchristos #include <sys/syscallargs.h>
62151fa70fSchristos 
63a2a38285Sad #include <sys/cpu.h>
643bf459f3Sfvdl #include <machine/reg.h>
653bf459f3Sfvdl 
66908291d2Schristos #include <compat/linux/common/linux_types.h>
67908291d2Schristos #include <compat/linux/common/linux_signal.h>
68908291d2Schristos #include <compat/linux/common/linux_util.h>
69908291d2Schristos #include <compat/linux/common/linux_exec.h>
70908291d2Schristos #include <compat/linux/common/linux_machdep.h>
711ab7ca8fSad #include <compat/linux/common/linux_ipc.h>
721ab7ca8fSad #include <compat/linux/common/linux_sem.h>
733bf459f3Sfvdl 
74908291d2Schristos #include <compat/linux/linux_syscallargs.h>
75908291d2Schristos #include <compat/linux/linux_syscall.h>
76fc7cfb5fSfvdl 
7765fc8539Schristos #ifdef DEBUG_LINUX
7865fc8539Schristos #define DPRINTF(a)	uprintf a
7965fc8539Schristos #else
809ab53722Smsaitoh #define DPRINTF(a)	do {} while (0)
8165fc8539Schristos #endif
8265fc8539Schristos 
83ac10cf69Smanu #ifdef LINUX_ATEXIT_SIGNATURE
84ac10cf69Smanu /*
85ac10cf69Smanu  * On the PowerPC, statically linked Linux binaries are not recognized
86ac10cf69Smanu  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
87ac10cf69Smanu  * binaries features a __libc_atexit ELF section. We therefore assume we
88ac10cf69Smanu  * have a Linux binary if we find this section.
89ac10cf69Smanu  */
90ee0c5b44Smanu int
ELFNAME2(linux,atexit_signature)911d7f24eaSmatt ELFNAME2(linux,atexit_signature)(
921d7f24eaSmatt 	struct lwp *l,
931d7f24eaSmatt 	struct exec_package *epp,
941d7f24eaSmatt 	Elf_Ehdr *eh)
95ac10cf69Smanu {
96c1cf2f47Smaxv 	Elf_Shdr *sh;
97ac10cf69Smanu 	size_t shsize;
98c1cf2f47Smaxv 	u_int shstrndx;
99ac10cf69Smanu 	size_t i;
100ac10cf69Smanu 	static const char signature[] = "__libc_atexit";
101c1cf2f47Smaxv 	const size_t sigsz = sizeof(signature);
102c1cf2f47Smaxv 	char tbuf[sizeof(signature)];
103ac10cf69Smanu 	int error;
104ac10cf69Smanu 
105c1cf2f47Smaxv 	/* Load the section header table. */
106ac10cf69Smanu 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
107ac10cf69Smanu 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
108f1ecb271Sad 	error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
109f1ecb271Sad 	    IO_NODELOCKED);
110ac10cf69Smanu 	if (error)
111ac10cf69Smanu 		goto out;
112ac10cf69Smanu 
113c1cf2f47Smaxv 	/* Now let's find the string table. If it does not exist, give up. */
114c1cf2f47Smaxv 	shstrndx = eh->e_shstrndx;
115c1cf2f47Smaxv 	if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
116ac10cf69Smanu 		error = ENOEXEC;
117ac10cf69Smanu 		goto out;
118ac10cf69Smanu 	}
119ac10cf69Smanu 
120c1cf2f47Smaxv 	/* Check if any section has the name we're looking for. */
121c1cf2f47Smaxv 	const off_t stroff = sh[shstrndx].sh_offset;
122ac10cf69Smanu 	for (i = 0; i < eh->e_shnum; i++) {
123ac10cf69Smanu 		Elf_Shdr *s = &sh[i];
124c1cf2f47Smaxv 
125c1cf2f47Smaxv 		if (s->sh_name + sigsz > sh[shstrndx].sh_size)
126c1cf2f47Smaxv 			continue;
127c1cf2f47Smaxv 
128f1ecb271Sad 		error = exec_read(l, epp->ep_vp, stroff + s->sh_name, tbuf,
129f1ecb271Sad 		    sigsz, IO_NODELOCKED);
130c1cf2f47Smaxv 		if (error)
131c1cf2f47Smaxv 			goto out;
132c1cf2f47Smaxv 		if (!memcmp(tbuf, signature, sigsz)) {
133c1cf2f47Smaxv 			DPRINTF(("linux_atexit_sig=%s\n", tbuf));
134ac10cf69Smanu 			error = 0;
135ac10cf69Smanu 			goto out;
136ac10cf69Smanu 		}
137ac10cf69Smanu 	}
138ac10cf69Smanu 	error = ENOEXEC;
139ac10cf69Smanu 
140ac10cf69Smanu out:
141ac10cf69Smanu 	free(sh, M_TEMP);
142ac10cf69Smanu 	return (error);
143ac10cf69Smanu }
144ac10cf69Smanu #endif
145f7ac1bd3Serh 
146f7ac1bd3Serh #ifdef LINUX_GCC_SIGNATURE
1474d9a6e09Schristos /*
1484d9a6e09Schristos  * Take advantage of the fact that all the linux binaries are compiled
1494d9a6e09Schristos  * with gcc, and gcc sticks in the comment field a signature. Note that
1504d9a6e09Schristos  * on SVR4 binaries, the gcc signature will follow the OS name signature,
1514d9a6e09Schristos  * that will not be a problem. We don't bother to read in the string table,
1524d9a6e09Schristos  * but we check all the progbits headers.
153f7ac1bd3Serh  *
154f7ac1bd3Serh  * XXX This only works in the i386.  On the alpha (at least)
155f7ac1bd3Serh  * XXX we have the same gcc signature which incorrectly identifies
156f7ac1bd3Serh  * XXX NetBSD binaries as Linux.
1574d9a6e09Schristos  */
158ee0c5b44Smanu int
ELFNAME2(linux,gcc_signature)1591d7f24eaSmatt ELFNAME2(linux,gcc_signature)(
1601d7f24eaSmatt 	struct lwp *l,
1611d7f24eaSmatt 	struct exec_package *epp,
1621d7f24eaSmatt 	Elf_Ehdr *eh)
1634d9a6e09Schristos {
164b29180b2Smycroft 	size_t shsize;
1654d9a6e09Schristos 	size_t i;
1664d9a6e09Schristos 	static const char signature[] = "\0GCC: (GNU) ";
167fb4b40b7Schristos 	char tbuf[sizeof(signature) - 1];
168f7ac1bd3Serh 	Elf_Shdr *sh;
1694d9a6e09Schristos 	int error;
1704d9a6e09Schristos 
171b29180b2Smycroft 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
172f7ac1bd3Serh 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
173f1ecb271Sad 	error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
174f1ecb271Sad 	    IO_NODELOCKED);
175b29180b2Smycroft 	if (error)
1764d9a6e09Schristos 		goto out;
1774d9a6e09Schristos 
1784d9a6e09Schristos 	for (i = 0; i < eh->e_shnum; i++) {
179f7ac1bd3Serh 		Elf_Shdr *s = &sh[i];
1804d9a6e09Schristos 
1814d9a6e09Schristos 		/*
1824d9a6e09Schristos 		 * Identify candidates for the comment header;
183d83602c1Schristos 		 * Header cannot have a load address, or flags and
1844d9a6e09Schristos 		 * it must be large enough.
1854d9a6e09Schristos 		 */
186522cbf02Skleink 		if (s->sh_type != SHT_PROGBITS ||
1874d9a6e09Schristos 		    s->sh_addr != 0 ||
1884d9a6e09Schristos 		    s->sh_flags != 0 ||
1894d9a6e09Schristos 		    s->sh_size < sizeof(signature) - 1)
1904d9a6e09Schristos 			continue;
1914d9a6e09Schristos 
192f1ecb271Sad 		error = exec_read(l, epp->ep_vp, s->sh_offset, tbuf,
193f1ecb271Sad 		    sizeof(signature) - 1, IO_NODELOCKED);
194b29180b2Smycroft 		if (error)
195b29180b2Smycroft 			continue;
1964d9a6e09Schristos 
197d83602c1Schristos 		/*
198d83602c1Schristos 		 * error is 0, if the signatures match we are done.
199d83602c1Schristos 		 */
200fb4b40b7Schristos 		DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
201fb4b40b7Schristos 		if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
202b29180b2Smycroft 			error = 0;
2034d9a6e09Schristos 			goto out;
2044d9a6e09Schristos 		}
205b29180b2Smycroft 	}
206b29180b2Smycroft 	error = ENOEXEC;
2074d9a6e09Schristos 
2084d9a6e09Schristos out:
2094d9a6e09Schristos 	free(sh, M_TEMP);
210b29180b2Smycroft 	return (error);
2114d9a6e09Schristos }
212f7ac1bd3Serh #endif
2134d9a6e09Schristos 
21405c8a1b8Smanu #ifdef LINUX_DEBUGLINK_SIGNATURE
21505c8a1b8Smanu /*
216c1cf2f47Smaxv  * Look for a .gnu_debuglink, specific to x86_64 interpreter
21705c8a1b8Smanu  */
21805c8a1b8Smanu int
ELFNAME2(linux,debuglink_signature)2199fca5da6Scegger ELFNAME2(linux,debuglink_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
22005c8a1b8Smanu {
221c1cf2f47Smaxv 	Elf_Shdr *sh;
22205c8a1b8Smanu 	size_t shsize;
223c1cf2f47Smaxv 	u_int shstrndx;
22405c8a1b8Smanu 	size_t i;
22505c8a1b8Smanu 	static const char signature[] = ".gnu_debuglink";
226c1cf2f47Smaxv 	const size_t sigsz = sizeof(signature);
227c1cf2f47Smaxv 	char tbuf[sizeof(signature)];
22805c8a1b8Smanu 	int error;
22905c8a1b8Smanu 
230c1cf2f47Smaxv 	/* Load the section header table. */
23105c8a1b8Smanu 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
23205c8a1b8Smanu 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
233f1ecb271Sad 	error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
234f1ecb271Sad 	    IO_NODELOCKED);
23505c8a1b8Smanu 	if (error)
23605c8a1b8Smanu 		goto out;
23705c8a1b8Smanu 
238c1cf2f47Smaxv 	/* Now let's find the string table. If it does not exist, give up. */
239c1cf2f47Smaxv 	shstrndx = eh->e_shstrndx;
240c1cf2f47Smaxv 	if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
24105c8a1b8Smanu 		error = ENOEXEC;
24205c8a1b8Smanu 		goto out;
24305c8a1b8Smanu 	}
24405c8a1b8Smanu 
245c1cf2f47Smaxv 	/* Check if any section has the name we're looking for. */
246c1cf2f47Smaxv 	const off_t stroff = sh[shstrndx].sh_offset;
24705c8a1b8Smanu 	for (i = 0; i < eh->e_shnum; i++) {
24805c8a1b8Smanu 		Elf_Shdr *s = &sh[i];
24905c8a1b8Smanu 
250c1cf2f47Smaxv 		if (s->sh_name + sigsz > sh[shstrndx].sh_size)
251c1cf2f47Smaxv 			continue;
252c1cf2f47Smaxv 
253f1ecb271Sad 		error = exec_read(l, epp->ep_vp, stroff + s->sh_name, tbuf,
254f1ecb271Sad 		    sigsz, IO_NODELOCKED);
255c1cf2f47Smaxv 		if (error)
256c1cf2f47Smaxv 			goto out;
257c1cf2f47Smaxv 		if (!memcmp(tbuf, signature, sigsz)) {
258c1cf2f47Smaxv 			DPRINTF(("linux_debuglink_sig=%s\n", tbuf));
25905c8a1b8Smanu 			error = 0;
26005c8a1b8Smanu 			goto out;
26105c8a1b8Smanu 		}
26205c8a1b8Smanu 	}
26305c8a1b8Smanu 	error = ENOEXEC;
26405c8a1b8Smanu 
26505c8a1b8Smanu out:
26605c8a1b8Smanu 	free(sh, M_TEMP);
26705c8a1b8Smanu 	return (error);
26805c8a1b8Smanu }
26905c8a1b8Smanu #endif
27005c8a1b8Smanu 
2718be2e96dSchristos #ifdef LINUX_GO_RT0_SIGNATURE
2728be2e96dSchristos /*
2738be2e96dSchristos  * Look for a .gopclntab, specific to go binaries
2748be2e96dSchristos  * in it look for a symbol called _rt0_<cpu>_linux
2758be2e96dSchristos  */
276bbb9625bSryo int
ELFNAME2(linux,go_rt0_signature)2778be2e96dSchristos ELFNAME2(linux,go_rt0_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
2788be2e96dSchristos {
2798be2e96dSchristos 	Elf_Shdr *sh;
2808be2e96dSchristos 	size_t shsize;
2818be2e96dSchristos 	u_int shstrndx;
2828be2e96dSchristos 	size_t i;
2838be2e96dSchristos 	static const char signature[] = ".gopclntab";
2848be2e96dSchristos 	const size_t sigsz = sizeof(signature);
2858be2e96dSchristos 	char tbuf[sizeof(signature)], *tmp = NULL;
2868be2e96dSchristos 	char mbuf[64];
2878be2e96dSchristos 	const char *m;
2888be2e96dSchristos 	int mlen;
2898be2e96dSchristos 	int error;
2908be2e96dSchristos 
2918be2e96dSchristos 	/* Load the section header table. */
2928be2e96dSchristos 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
2938be2e96dSchristos 	sh = malloc(shsize, M_TEMP, M_WAITOK);
294f1ecb271Sad 	error = exec_read(l, epp->ep_vp, eh->e_shoff, sh, shsize,
295f1ecb271Sad 	    IO_NODELOCKED);
2968be2e96dSchristos 	if (error)
2978be2e96dSchristos 		goto out;
2988be2e96dSchristos 
2998be2e96dSchristos 	/* Now let's find the string table. If it does not exist, give up. */
3008be2e96dSchristos 	shstrndx = eh->e_shstrndx;
3018be2e96dSchristos 	if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
3028be2e96dSchristos 		error = ENOEXEC;
3038be2e96dSchristos 		goto out;
3048be2e96dSchristos 	}
3058be2e96dSchristos 
3068be2e96dSchristos 	/* Check if any section has the name we're looking for. */
3078be2e96dSchristos 	const off_t stroff = sh[shstrndx].sh_offset;
3088be2e96dSchristos 	for (i = 0; i < eh->e_shnum; i++) {
3098be2e96dSchristos 		Elf_Shdr *s = &sh[i];
3108be2e96dSchristos 
3118be2e96dSchristos 		if (s->sh_name + sigsz > sh[shstrndx].sh_size)
3128be2e96dSchristos 			continue;
3138be2e96dSchristos 
314f1ecb271Sad 		error = exec_read(l, epp->ep_vp, stroff + s->sh_name, tbuf,
315f1ecb271Sad 		    sigsz, IO_NODELOCKED);
3168be2e96dSchristos 		if (error)
3178be2e96dSchristos 			goto out;
3188be2e96dSchristos 		if (!memcmp(tbuf, signature, sigsz)) {
3199ab53722Smsaitoh 			DPRINTF(("linux_goplcntab_sig=%s\n", tbuf));
3208be2e96dSchristos 			break;
3218be2e96dSchristos 		}
3228be2e96dSchristos 	}
3238be2e96dSchristos 
3248be2e96dSchristos 	if (i == eh->e_shnum) {
3258be2e96dSchristos 		error = ENOEXEC;
3268be2e96dSchristos 		goto out;
3278be2e96dSchristos 	}
3288be2e96dSchristos 
329a03af6c5Schristos 	// Don't scan more than 1MB
330a03af6c5Schristos 	if (sh[i].sh_size > 1024 * 1024)
331a03af6c5Schristos 		sh[i].sh_size = 1024 * 1024;
3328be2e96dSchristos 
3338be2e96dSchristos 	tmp = malloc(sh[i].sh_size, M_TEMP, M_WAITOK);
334f1ecb271Sad 	error = exec_read(l, epp->ep_vp, sh[i].sh_offset, tmp,
335f1ecb271Sad 	    sh[i].sh_size, IO_NODELOCKED);
3368be2e96dSchristos 	if (error)
3378be2e96dSchristos 		goto out;
3388be2e96dSchristos 
3398be2e96dSchristos #if (ELFSIZE == 32)
340*836f4a42Sryo #ifdef LINUX_GO_RT0_SIGNATURE_ARCH32
341*836f4a42Sryo 	m = LINUX_GO_RT0_SIGNATURE_ARCH32;
342*836f4a42Sryo #else
343d91f98a8Spgoyette 	extern struct netbsd32_machine32_hook_t netbsd32_machine32_hook;
3448c2f80f1Spgoyette 	MODULE_HOOK_CALL(netbsd32_machine32_hook, (), machine, m);
345*836f4a42Sryo #endif
346*836f4a42Sryo #else /* (ELFSIZE == 32) */
347*836f4a42Sryo #ifdef LINUX_GO_RT0_SIGNATURE_ARCH64
348*836f4a42Sryo 	m = LINUX_GO_RT0_SIGNATURE_ARCH64;
3498be2e96dSchristos #else
3508be2e96dSchristos 	m = machine;
3518be2e96dSchristos #endif
352*836f4a42Sryo #endif /* (ELFSIZE == 32) */
3538be2e96dSchristos 	mlen = snprintf(mbuf, sizeof(mbuf), "_rt0_%s_linux", m);
3548be2e96dSchristos 	if (memmem(tmp, sh[i].sh_size, mbuf, mlen) == NULL)
3558be2e96dSchristos 		error = ENOEXEC;
3568be2e96dSchristos 	else
3578be2e96dSchristos 		DPRINTF(("linux_rt0_sig=%s\n", mbuf));
3588be2e96dSchristos out:
3598be2e96dSchristos 	if (tmp)
3608be2e96dSchristos 		free(tmp, M_TEMP);
3618be2e96dSchristos 	free(sh, M_TEMP);
3628be2e96dSchristos 	return error;
3638be2e96dSchristos }
3648be2e96dSchristos #endif
3658be2e96dSchristos 
366ee0c5b44Smanu int
ELFNAME2(linux,signature)3679fca5da6Scegger ELFNAME2(linux,signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh, char *itp)
368f7ac1bd3Serh {
369f7ac1bd3Serh 	size_t i;
370f7ac1bd3Serh 	Elf_Phdr *ph;
371f7ac1bd3Serh 	size_t phsize;
372b29180b2Smycroft 	int error;
3736cc14962Schristos 	static const char linux[] = "Linux";
3746cc14962Schristos 
375a2ad93deSuwe 	if (eh->e_ident[EI_OSABI] == ELFOSABI_LINUX ||
3766cc14962Schristos 	    memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
3776cc14962Schristos 		return 0;
378f7ac1bd3Serh 
379f7ac1bd3Serh 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
380f7ac1bd3Serh 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
381f1ecb271Sad 	error = exec_read(l, epp->ep_vp, eh->e_phoff, ph, phsize,
382f1ecb271Sad 	    IO_NODELOCKED);
383b29180b2Smycroft 	if (error)
384b29180b2Smycroft 		goto out;
385f7ac1bd3Serh 
386f7ac1bd3Serh 	for (i = 0; i < eh->e_phnum; i++) {
387f7ac1bd3Serh 		Elf_Phdr *ephp = &ph[i];
388b29180b2Smycroft 		Elf_Nhdr *np;
389b29180b2Smycroft 		u_int32_t *abi;
390f7ac1bd3Serh 
391b29180b2Smycroft 		if (ephp->p_type != PT_NOTE ||
392b29180b2Smycroft 		    ephp->p_filesz > 1024 ||
393b29180b2Smycroft 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
394f7ac1bd3Serh 			continue;
395f7ac1bd3Serh 
396b29180b2Smycroft 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
397f1ecb271Sad 		error = exec_read(l, epp->ep_vp, ephp->p_offset, np,
398f1ecb271Sad 		    ephp->p_filesz, IO_NODELOCKED);
399b29180b2Smycroft 		if (error)
400b29180b2Smycroft 			goto next;
401f7ac1bd3Serh 
402b29180b2Smycroft 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
403b29180b2Smycroft 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
404b29180b2Smycroft 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
40553524e44Schristos 		    memcmp((void *)(np + 1), ELF_NOTE_ABI_NAME,
406b29180b2Smycroft 		    ELF_NOTE_ABI_NAMESZ))
407b29180b2Smycroft 			goto next;
408b29180b2Smycroft 
409b29180b2Smycroft 		/* Make sure the OS is Linux. */
41053524e44Schristos 		abi = (u_int32_t *)((char *)np + sizeof(Elf_Nhdr) +
411b29180b2Smycroft 		    np->n_namesz);
412b29180b2Smycroft 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
413f7ac1bd3Serh 			error = 0;
414b29180b2Smycroft 		else
415b29180b2Smycroft 			error = ENOEXEC;
416b29180b2Smycroft 		free(np, M_TEMP);
417b29180b2Smycroft 		goto out;
418f7ac1bd3Serh 
419b29180b2Smycroft 	next:
420b29180b2Smycroft 		free(np, M_TEMP);
421f7ac1bd3Serh 		continue;
422f7ac1bd3Serh 	}
423f7ac1bd3Serh 
424c1cf2f47Smaxv 	/* Check for certain interpreter names. */
425714de045Sdrochner 	if (itp) {
426b29180b2Smycroft 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
42789647c7cSmanu #if (ELFSIZE == 64)
42889647c7cSmanu 		    !strncmp(itp, "/lib64/ld-linux", 15) ||
42989647c7cSmanu #endif
430b29180b2Smycroft 		    !strncmp(itp, "/lib/ld.so.", 11))
431f7ac1bd3Serh 			error = 0;
432b29180b2Smycroft 		else
433b29180b2Smycroft 			error = ENOEXEC;
434b29180b2Smycroft 		goto out;
435f7ac1bd3Serh 	}
436f7ac1bd3Serh 
437f7ac1bd3Serh 	error = ENOEXEC;
438b29180b2Smycroft out:
439f7ac1bd3Serh 	free(ph, M_TEMP);
440b29180b2Smycroft 	return (error);
441f7ac1bd3Serh }
442f7ac1bd3Serh 
443f7ac1bd3Serh int
ELFNAME2(linux,probe)4444d595fd7Schristos ELFNAME2(linux,probe)(struct lwp *l, struct exec_package *epp, void *eh,
445168cd830Schristos     char *itp, vaddr_t *pos)
446fc7cfb5fSfvdl {
447fc7cfb5fSfvdl 	int error;
448fc7cfb5fSfvdl 
44995e1ffb1Schristos 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
450f7ac1bd3Serh #ifdef LINUX_GCC_SIGNATURE
45195e1ffb1Schristos 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
4528537f76cSmanu #endif
453ac10cf69Smanu #ifdef LINUX_ATEXIT_SIGNATURE
45495e1ffb1Schristos 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
455ac10cf69Smanu #endif
45605c8a1b8Smanu #ifdef LINUX_DEBUGLINK_SIGNATURE
45705c8a1b8Smanu 	    ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
45805c8a1b8Smanu #endif
4598be2e96dSchristos #ifdef LINUX_GO_RT0_SIGNATURE
4608be2e96dSchristos 	    ((error = ELFNAME2(linux,go_rt0_signature)(l, epp, eh)) != 0) &&
4618be2e96dSchristos #endif
46205c8a1b8Smanu 	    1) {
46305c8a1b8Smanu 			DPRINTF(("linux_probe: returning %d\n", error));
4648537f76cSmanu 			return error;
46505c8a1b8Smanu 	}
4664d9a6e09Schristos 
467714de045Sdrochner 	if (itp) {
468b8fbaf8cSdsl 		if ((error = emul_find_interp(l, epp, itp)))
46947cd9b85Sjdolecek 			return (error);
470fc7cfb5fSfvdl 	}
471590b9069Schs 	epp->ep_flags |= EXEC_FORCEAUX;
47265fc8539Schristos 	DPRINTF(("linux_probe: returning 0\n"));
473c4aaa600Sfvdl 	return 0;
474fc7cfb5fSfvdl }
475c4aaa600Sfvdl 
476cee43b67Sjdolecek #ifndef LINUX_MACHDEP_ELF_COPYARGS
477cee43b67Sjdolecek /*
478cee43b67Sjdolecek  * Copy arguments onto the stack in the normal way, but add some
479cee43b67Sjdolecek  * extra information in case of dynamic binding.
480cee43b67Sjdolecek  */
481cee43b67Sjdolecek int
ELFNAME2(linux,copyargs)48295e1ffb1Schristos ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
483cee43b67Sjdolecek     struct ps_strings *arginfo, char **stackp, void *argp)
484cee43b67Sjdolecek {
485cee43b67Sjdolecek 	size_t len;
486cee43b67Sjdolecek 	AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
487cee43b67Sjdolecek 	struct elf_args *ap;
488cee43b67Sjdolecek 	int error;
489cee43b67Sjdolecek 	struct vattr *vap;
4902084516cSchs 	uint32_t randbytes[4];
491cee43b67Sjdolecek 
49295e1ffb1Schristos 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
493cee43b67Sjdolecek 		return error;
494cee43b67Sjdolecek 
495cee43b67Sjdolecek 	a = ai;
496cee43b67Sjdolecek 
49706b89b30Smaxv 	memset(ai, 0, sizeof(ai));
49806b89b30Smaxv 
499cee43b67Sjdolecek 	/*
500cee43b67Sjdolecek 	 * Push extra arguments used by glibc on the stack.
501cee43b67Sjdolecek 	 */
502cee43b67Sjdolecek 
503cee43b67Sjdolecek 	a->a_type = AT_PAGESZ;
504cee43b67Sjdolecek 	a->a_v = PAGE_SIZE;
505cee43b67Sjdolecek 	a++;
506cee43b67Sjdolecek 
507cee43b67Sjdolecek 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
508cee43b67Sjdolecek 
509cee43b67Sjdolecek 		a->a_type = AT_PHDR;
510cee43b67Sjdolecek 		a->a_v = ap->arg_phaddr;
511cee43b67Sjdolecek 		a++;
512cee43b67Sjdolecek 
513cee43b67Sjdolecek 		a->a_type = AT_PHENT;
514cee43b67Sjdolecek 		a->a_v = ap->arg_phentsize;
515cee43b67Sjdolecek 		a++;
516cee43b67Sjdolecek 
517cee43b67Sjdolecek 		a->a_type = AT_PHNUM;
518cee43b67Sjdolecek 		a->a_v = ap->arg_phnum;
519cee43b67Sjdolecek 		a++;
520cee43b67Sjdolecek 
521cee43b67Sjdolecek 		a->a_type = AT_BASE;
522cee43b67Sjdolecek 		a->a_v = ap->arg_interp;
523cee43b67Sjdolecek 		a++;
524cee43b67Sjdolecek 
525cee43b67Sjdolecek 		a->a_type = AT_FLAGS;
526cee43b67Sjdolecek 		a->a_v = 0;
527cee43b67Sjdolecek 		a++;
528cee43b67Sjdolecek 
529cee43b67Sjdolecek 		a->a_type = AT_ENTRY;
530cee43b67Sjdolecek 		a->a_v = ap->arg_entry;
531cee43b67Sjdolecek 		a++;
532cee43b67Sjdolecek 
5332210079eSmatt 		exec_free_emul_arg(pack);
534cee43b67Sjdolecek 	}
535cee43b67Sjdolecek 
536cee43b67Sjdolecek 	/* Linux-specific items */
537cee43b67Sjdolecek 	a->a_type = LINUX_AT_CLKTCK;
538cee43b67Sjdolecek 	a->a_v = hz;
539cee43b67Sjdolecek 	a++;
540cee43b67Sjdolecek 
541cee43b67Sjdolecek 	vap = pack->ep_vap;
542cee43b67Sjdolecek 
543cee43b67Sjdolecek 	a->a_type = LINUX_AT_UID;
544f474dcebSad 	a->a_v = kauth_cred_getuid(l->l_cred);
545cee43b67Sjdolecek 	a++;
546cee43b67Sjdolecek 
547cee43b67Sjdolecek 	a->a_type = LINUX_AT_EUID;
548cee43b67Sjdolecek 	if (vap->va_mode & S_ISUID)
549cee43b67Sjdolecek 		a->a_v = vap->va_uid;
550cee43b67Sjdolecek 	else
551f474dcebSad 		a->a_v = kauth_cred_geteuid(l->l_cred);
552cee43b67Sjdolecek 	a++;
553cee43b67Sjdolecek 
554cee43b67Sjdolecek 	a->a_type = LINUX_AT_GID;
555f474dcebSad 	a->a_v = kauth_cred_getgid(l->l_cred);
556cee43b67Sjdolecek 	a++;
557cee43b67Sjdolecek 
558cee43b67Sjdolecek 	a->a_type = LINUX_AT_EGID;
559cee43b67Sjdolecek 	if (vap->va_mode & S_ISGID)
560cee43b67Sjdolecek 		a->a_v = vap->va_gid;
561cee43b67Sjdolecek 	else
562f474dcebSad 		a->a_v = kauth_cred_getegid(l->l_cred);
563cee43b67Sjdolecek 	a++;
564cee43b67Sjdolecek 
5652084516cSchs 	a->a_type = LINUX_AT_RANDOM;
566d99aa73fSmatt 	a->a_v = (Elf_Addr)(uintptr_t)*stackp;
5672084516cSchs 	a++;
5682084516cSchs 
569cee43b67Sjdolecek 	a->a_type = AT_NULL;
570cee43b67Sjdolecek 	a->a_v = 0;
571cee43b67Sjdolecek 	a++;
572cee43b67Sjdolecek 
573e5a75de9Schs 	randbytes[0] = cprng_strong32();
574e5a75de9Schs 	randbytes[1] = cprng_strong32();
575e5a75de9Schs 	randbytes[2] = cprng_strong32();
576e5a75de9Schs 	randbytes[3] = cprng_strong32();
5772084516cSchs 
5782084516cSchs 	len = sizeof(randbytes);
5792084516cSchs 	if ((error = copyout(randbytes, *stackp, len)) != 0)
5802084516cSchs 		return error;
5812084516cSchs 	*stackp += len;
5822084516cSchs 
583cee43b67Sjdolecek 	len = (a - ai) * sizeof(AuxInfo);
5846e5d6d35Snjoly 	KASSERT(len <= LINUX_ELF_AUX_ENTRIES * sizeof(AuxInfo));
585cee43b67Sjdolecek 	if ((error = copyout(ai, *stackp, len)) != 0)
586cee43b67Sjdolecek 		return error;
587cee43b67Sjdolecek 	*stackp += len;
588cee43b67Sjdolecek 
589cee43b67Sjdolecek 	return 0;
590cee43b67Sjdolecek }
591cee43b67Sjdolecek #endif /* !LINUX_MACHDEP_ELF_COPYARGS */
592