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