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