linux_exec_elf32.c revision 1.94.12.5 1 /* $NetBSD: linux_exec_elf32.c,v 1.94.12.5 2019/01/24 04:41:34 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.5 2019/01/24 04:41:34 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 #include <compat/netbsd32/netbsd32.h>
77
78 #define LINUX_GO_RT0_SIGNATURE
79
80 #ifdef DEBUG_LINUX
81 #define DPRINTF(a) uprintf a
82 #else
83 #define DPRINTF(a) do {} while (0)
84 #endif
85
86 #ifdef LINUX_ATEXIT_SIGNATURE
87 /*
88 * On the PowerPC, statically linked Linux binaries are not recognized
89 * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
90 * binaries features a __libc_atexit ELF section. We therefore assume we
91 * have a Linux binary if we find this section.
92 */
93 int
94 ELFNAME2(linux,atexit_signature)(
95 struct lwp *l,
96 struct exec_package *epp,
97 Elf_Ehdr *eh)
98 {
99 Elf_Shdr *sh;
100 size_t shsize;
101 u_int shstrndx;
102 size_t i;
103 static const char signature[] = "__libc_atexit";
104 const size_t sigsz = sizeof(signature);
105 char tbuf[sizeof(signature)];
106 int error;
107
108 /* Load the section header table. */
109 shsize = eh->e_shnum * sizeof(Elf_Shdr);
110 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
111 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
112 if (error)
113 goto out;
114
115 /* Now let's find the string table. If it does not exist, give up. */
116 shstrndx = eh->e_shstrndx;
117 if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
118 error = ENOEXEC;
119 goto out;
120 }
121
122 /* Check if any section has the name we're looking for. */
123 const off_t stroff = sh[shstrndx].sh_offset;
124 for (i = 0; i < eh->e_shnum; i++) {
125 Elf_Shdr *s = &sh[i];
126
127 if (s->sh_name + sigsz > sh[shstrndx].sh_size)
128 continue;
129
130 error = exec_read_from(l, epp->ep_vp, stroff + s->sh_name, tbuf,
131 sigsz);
132 if (error)
133 goto out;
134 if (!memcmp(tbuf, signature, sigsz)) {
135 DPRINTF(("linux_atexit_sig=%s\n", tbuf));
136 error = 0;
137 goto out;
138 }
139 }
140 error = ENOEXEC;
141
142 out:
143 free(sh, M_TEMP);
144 return (error);
145 }
146 #endif
147
148 #ifdef LINUX_GCC_SIGNATURE
149 /*
150 * Take advantage of the fact that all the linux binaries are compiled
151 * with gcc, and gcc sticks in the comment field a signature. Note that
152 * on SVR4 binaries, the gcc signature will follow the OS name signature,
153 * that will not be a problem. We don't bother to read in the string table,
154 * but we check all the progbits headers.
155 *
156 * XXX This only works in the i386. On the alpha (at least)
157 * XXX we have the same gcc signature which incorrectly identifies
158 * XXX NetBSD binaries as Linux.
159 */
160 int
161 ELFNAME2(linux,gcc_signature)(
162 struct lwp *l,
163 struct exec_package *epp,
164 Elf_Ehdr *eh)
165 {
166 size_t shsize;
167 size_t i;
168 static const char signature[] = "\0GCC: (GNU) ";
169 char tbuf[sizeof(signature) - 1];
170 Elf_Shdr *sh;
171 int error;
172
173 shsize = eh->e_shnum * sizeof(Elf_Shdr);
174 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
175 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
176 if (error)
177 goto out;
178
179 for (i = 0; i < eh->e_shnum; i++) {
180 Elf_Shdr *s = &sh[i];
181
182 /*
183 * Identify candidates for the comment header;
184 * Header cannot have a load address, or flags and
185 * it must be large enough.
186 */
187 if (s->sh_type != SHT_PROGBITS ||
188 s->sh_addr != 0 ||
189 s->sh_flags != 0 ||
190 s->sh_size < sizeof(signature) - 1)
191 continue;
192
193 error = exec_read_from(l, epp->ep_vp, s->sh_offset, tbuf,
194 sizeof(signature) - 1);
195 if (error)
196 continue;
197
198 /*
199 * error is 0, if the signatures match we are done.
200 */
201 DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
202 if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
203 error = 0;
204 goto out;
205 }
206 }
207 error = ENOEXEC;
208
209 out:
210 free(sh, M_TEMP);
211 return (error);
212 }
213 #endif
214
215 #ifdef LINUX_DEBUGLINK_SIGNATURE
216 /*
217 * Look for a .gnu_debuglink, specific to x86_64 interpreter
218 */
219 int
220 ELFNAME2(linux,debuglink_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
221 {
222 Elf_Shdr *sh;
223 size_t shsize;
224 u_int shstrndx;
225 size_t i;
226 static const char signature[] = ".gnu_debuglink";
227 const size_t sigsz = sizeof(signature);
228 char tbuf[sizeof(signature)];
229 int error;
230
231 /* Load the section header table. */
232 shsize = eh->e_shnum * sizeof(Elf_Shdr);
233 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
234 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
235 if (error)
236 goto out;
237
238 /* Now let's find the string table. If it does not exist, give up. */
239 shstrndx = eh->e_shstrndx;
240 if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
241 error = ENOEXEC;
242 goto out;
243 }
244
245 /* Check if any section has the name we're looking for. */
246 const off_t stroff = sh[shstrndx].sh_offset;
247 for (i = 0; i < eh->e_shnum; i++) {
248 Elf_Shdr *s = &sh[i];
249
250 if (s->sh_name + sigsz > sh[shstrndx].sh_size)
251 continue;
252
253 error = exec_read_from(l, epp->ep_vp, stroff + s->sh_name, tbuf,
254 sigsz);
255 if (error)
256 goto out;
257 if (!memcmp(tbuf, signature, sigsz)) {
258 DPRINTF(("linux_debuglink_sig=%s\n", tbuf));
259 error = 0;
260 goto out;
261 }
262 }
263 error = ENOEXEC;
264
265 out:
266 free(sh, M_TEMP);
267 return (error);
268 }
269 #endif
270
271 #ifdef LINUX_GO_RT0_SIGNATURE
272 /*
273 * Look for a .gopclntab, specific to go binaries
274 * in it look for a symbol called _rt0_<cpu>_linux
275 */
276 static int
277 ELFNAME2(linux,go_rt0_signature)(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh)
278 {
279 Elf_Shdr *sh;
280 size_t shsize;
281 u_int shstrndx;
282 size_t i;
283 static const char signature[] = ".gopclntab";
284 const size_t sigsz = sizeof(signature);
285 char tbuf[sizeof(signature)], *tmp = NULL;
286 char mbuf[64];
287 const char *m;
288 int mlen;
289 int error;
290
291 /* Load the section header table. */
292 shsize = eh->e_shnum * sizeof(Elf_Shdr);
293 sh = malloc(shsize, M_TEMP, M_WAITOK);
294 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
295 if (error)
296 goto out;
297
298 /* Now let's find the string table. If it does not exist, give up. */
299 shstrndx = eh->e_shstrndx;
300 if (shstrndx == SHN_UNDEF || shstrndx >= eh->e_shnum) {
301 error = ENOEXEC;
302 goto out;
303 }
304
305 /* Check if any section has the name we're looking for. */
306 const off_t stroff = sh[shstrndx].sh_offset;
307 for (i = 0; i < eh->e_shnum; i++) {
308 Elf_Shdr *s = &sh[i];
309
310 if (s->sh_name + sigsz > sh[shstrndx].sh_size)
311 continue;
312
313 error = exec_read_from(l, epp->ep_vp, stroff + s->sh_name, tbuf,
314 sigsz);
315 if (error)
316 goto out;
317 if (!memcmp(tbuf, signature, sigsz)) {
318 DPRINTF(("linux_goplcntab_sig=%s\n", tbuf));
319 break;
320 }
321 }
322
323 if (i == eh->e_shnum) {
324 error = ENOEXEC;
325 goto out;
326 }
327
328 // Don't scan more than 1MB
329 if (sh[i].sh_size > 1024 * 1024)
330 sh[i].sh_size = 1024 * 1024;
331
332 tmp = malloc(sh[i].sh_size, M_TEMP, M_WAITOK);
333 error = exec_read_from(l, epp->ep_vp, sh[i].sh_offset, tmp,
334 sh[i].sh_size);
335 if (error)
336 goto out;
337
338 #if (ELFSIZE == 32)
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