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