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