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