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