exec_elf.c revision 1.99 1 /* $NetBSD: exec_elf.c,v 1.99 2019/09/15 20:20:26 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 2000, 2005, 2015 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 and Maxime Villard.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1996 Christopher G. Demetriou
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.99 2019/09/15 20:20:26 christos Exp $");
61
62 #ifdef _KERNEL_OPT
63 #include "opt_pax.h"
64 #endif /* _KERNEL_OPT */
65
66 #include <sys/param.h>
67 #include <sys/proc.h>
68 #include <sys/kmem.h>
69 #include <sys/namei.h>
70 #include <sys/vnode.h>
71 #include <sys/exec.h>
72 #include <sys/exec_elf.h>
73 #include <sys/syscall.h>
74 #include <sys/signalvar.h>
75 #include <sys/mount.h>
76 #include <sys/stat.h>
77 #include <sys/kauth.h>
78 #include <sys/bitops.h>
79
80 #include <sys/cpu.h>
81 #include <machine/reg.h>
82
83 #include <compat/common/compat_util.h>
84
85 #include <sys/pax.h>
86 #include <uvm/uvm_param.h>
87
88 extern struct emul emul_netbsd;
89
90 #define elf_check_header ELFNAME(check_header)
91 #define elf_copyargs ELFNAME(copyargs)
92 #define elf_populate_auxv ELFNAME(populate_auxv)
93 #define elf_load_interp ELFNAME(load_interp)
94 #define elf_load_psection ELFNAME(load_psection)
95 #define exec_elf_makecmds ELFNAME2(exec,makecmds)
96 #define netbsd_elf_signature ELFNAME2(netbsd,signature)
97 #define netbsd_elf_note ELFNAME2(netbsd,note)
98 #define netbsd_elf_probe ELFNAME2(netbsd,probe)
99 #define coredump ELFNAMEEND(coredump)
100 #define elf_free_emul_arg ELFNAME(free_emul_arg)
101
102 static int
103 elf_load_interp(struct lwp *, struct exec_package *, char *,
104 struct exec_vmcmd_set *, u_long *, Elf_Addr *);
105 static int
106 elf_load_psection(struct exec_vmcmd_set *, struct vnode *, const Elf_Phdr *,
107 Elf_Addr *, u_long *, int);
108
109 int netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
110 int netbsd_elf_note(struct exec_package *, const Elf_Nhdr *, const char *,
111 const char *);
112 int netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
113 vaddr_t *);
114
115 static void elf_free_emul_arg(void *);
116
117 #ifdef DEBUG_ELF
118 #define DPRINTF(a, ...) printf("%s: " a "\n", __func__, ##__VA_ARGS__)
119 #else
120 #define DPRINTF(a, ...)
121 #endif
122
123 /* round up and down to page boundaries. */
124 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
125 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
126
127 static int
128 elf_placedynexec(struct exec_package *epp, Elf_Ehdr *eh, Elf_Phdr *ph)
129 {
130 Elf_Addr align, offset;
131 int i;
132
133 for (align = 1, i = 0; i < eh->e_phnum; i++)
134 if (ph[i].p_type == PT_LOAD && ph[i].p_align > align)
135 align = ph[i].p_align;
136
137 offset = (Elf_Addr)pax_aslr_exec_offset(epp, align);
138 if (offset < epp->ep_vm_minaddr)
139 offset = roundup(epp->ep_vm_minaddr, align);
140 if ((offset & (align - 1)) != 0) {
141 DPRINTF("bad offset=%#jx align=%#jx",
142 (uintmax_t)offset, (uintmax_t)align);
143 return EINVAL;
144 }
145
146 for (i = 0; i < eh->e_phnum; i++)
147 ph[i].p_vaddr += offset;
148 epp->ep_entryoffset = offset;
149 eh->e_entry += offset;
150 return 0;
151 }
152
153
154 int
155 elf_populate_auxv(struct lwp *l, struct exec_package *pack, char **stackp)
156 {
157 size_t len, vlen;
158 AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname;
159 struct elf_args *ap;
160 char *path = l->l_proc->p_path;
161 int error;
162
163 a = ai;
164
165 memset(ai, 0, sizeof(ai));
166
167 /*
168 * Push extra arguments on the stack needed by dynamically
169 * linked binaries
170 */
171 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
172 struct vattr *vap = pack->ep_vap;
173
174 a->a_type = AT_PHDR;
175 a->a_v = ap->arg_phaddr;
176 a++;
177
178 a->a_type = AT_PHENT;
179 a->a_v = ap->arg_phentsize;
180 a++;
181
182 a->a_type = AT_PHNUM;
183 a->a_v = ap->arg_phnum;
184 a++;
185
186 a->a_type = AT_PAGESZ;
187 a->a_v = PAGE_SIZE;
188 a++;
189
190 a->a_type = AT_BASE;
191 a->a_v = ap->arg_interp;
192 a++;
193
194 a->a_type = AT_FLAGS;
195 a->a_v = 0;
196 a++;
197
198 a->a_type = AT_ENTRY;
199 a->a_v = ap->arg_entry;
200 a++;
201
202 a->a_type = AT_EUID;
203 if (vap->va_mode & S_ISUID)
204 a->a_v = vap->va_uid;
205 else
206 a->a_v = kauth_cred_geteuid(l->l_cred);
207 a++;
208
209 a->a_type = AT_RUID;
210 a->a_v = kauth_cred_getuid(l->l_cred);
211 a++;
212
213 a->a_type = AT_EGID;
214 if (vap->va_mode & S_ISGID)
215 a->a_v = vap->va_gid;
216 else
217 a->a_v = kauth_cred_getegid(l->l_cred);
218 a++;
219
220 a->a_type = AT_RGID;
221 a->a_v = kauth_cred_getgid(l->l_cred);
222 a++;
223
224 a->a_type = AT_STACKBASE;
225 a->a_v = l->l_proc->p_stackbase;
226 a++;
227
228 /* "/" means fexecve(2) could not resolve the pathname */
229 if (path[0] == '/' && path[1] != '\0') {
230 execname = a;
231 a->a_type = AT_SUN_EXECNAME;
232 a++;
233 }
234
235 exec_free_emul_arg(pack);
236 } else {
237 execname = NULL;
238 }
239
240 a->a_type = AT_NULL;
241 a->a_v = 0;
242 a++;
243
244 vlen = (a - ai) * sizeof(ai[0]);
245
246 KASSERT(vlen <= sizeof(ai));
247
248 if (execname) {
249 execname->a_v = (uintptr_t)(*stackp + vlen);
250 len = strlen(path) + 1;
251 if ((error = copyout(path, (*stackp + vlen), len)) != 0)
252 return error;
253 len = ALIGN(len);
254 } else {
255 len = 0;
256 }
257
258 if ((error = copyout(ai, *stackp, vlen)) != 0)
259 return error;
260 *stackp += vlen + len;
261
262 return 0;
263 }
264
265 /*
266 * Copy arguments onto the stack in the normal way, but add some
267 * extra information in case of dynamic binding.
268 */
269 int
270 elf_copyargs(struct lwp *l, struct exec_package *pack,
271 struct ps_strings *arginfo, char **stackp, void *argp)
272 {
273 int error;
274
275 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
276 return error;
277
278 return elf_populate_auxv(l, pack, stackp);
279 }
280
281 /*
282 * elf_check_header():
283 *
284 * Check header for validity; return 0 if ok, ENOEXEC if error
285 */
286 int
287 elf_check_header(Elf_Ehdr *eh)
288 {
289
290 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
291 eh->e_ident[EI_CLASS] != ELFCLASS) {
292 DPRINTF("bad magic e_ident[EI_MAG0,EI_MAG3] %#x%x%x%x, "
293 "e_ident[EI_CLASS] %#x", eh->e_ident[EI_MAG0],
294 eh->e_ident[EI_MAG1], eh->e_ident[EI_MAG2],
295 eh->e_ident[EI_MAG3], eh->e_ident[EI_CLASS]);
296 return ENOEXEC;
297 }
298
299 switch (eh->e_machine) {
300
301 ELFDEFNNAME(MACHDEP_ID_CASES)
302
303 default:
304 DPRINTF("bad machine %#x", eh->e_machine);
305 return ENOEXEC;
306 }
307
308 if (ELF_EHDR_FLAGS_OK(eh) == 0) {
309 DPRINTF("bad flags %#x", eh->e_flags);
310 return ENOEXEC;
311 }
312
313 if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM) {
314 DPRINTF("bad shnum/phnum %#x/%#x", eh->e_shnum, eh->e_phnum);
315 return ENOEXEC;
316 }
317
318 return 0;
319 }
320
321 /*
322 * elf_load_psection():
323 *
324 * Load a psection at the appropriate address
325 */
326 static int
327 elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
328 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags)
329 {
330 u_long msize, psize, rm, rf;
331 long diff, offset;
332 int vmprot = 0;
333
334 /*
335 * If the user specified an address, then we load there.
336 */
337 if (*addr == ELFDEFNNAME(NO_ADDR))
338 *addr = ph->p_vaddr;
339
340 if (ph->p_align > 1) {
341 /*
342 * Make sure we are virtually aligned as we are supposed to be.
343 */
344 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
345 if (*addr - diff != ELF_TRUNC(*addr, ph->p_align)) {
346 DPRINTF("bad alignment %#jx != %#jx\n",
347 (uintptr_t)(*addr - diff),
348 (uintptr_t)ELF_TRUNC(*addr, ph->p_align));
349 return EINVAL;
350 }
351 /*
352 * But make sure to not map any pages before the start of the
353 * psection by limiting the difference to within a page.
354 */
355 diff &= PAGE_MASK;
356 } else
357 diff = 0;
358
359 vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
360 vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
361 vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
362
363 /*
364 * Adjust everything so it all starts on a page boundary.
365 */
366 *addr -= diff;
367 offset = ph->p_offset - diff;
368 *size = ph->p_filesz + diff;
369 msize = ph->p_memsz + diff;
370
371 if (ph->p_align >= PAGE_SIZE) {
372 if ((ph->p_flags & PF_W) != 0) {
373 /*
374 * Because the pagedvn pager can't handle zero fill
375 * of the last data page if it's not page aligned we
376 * map the last page readvn.
377 */
378 psize = trunc_page(*size);
379 } else {
380 psize = round_page(*size);
381 }
382 } else {
383 psize = *size;
384 }
385
386 if (psize > 0) {
387 NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
388 vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
389 offset, vmprot, flags);
390 flags &= VMCMD_RELATIVE;
391 }
392 if (psize < *size) {
393 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
394 *addr + psize, vp, offset + psize, vmprot, flags);
395 }
396
397 /*
398 * Check if we need to extend the size of the segment (does
399 * bss extend page the next page boundary)?
400 */
401 rm = round_page(*addr + msize);
402 rf = round_page(*addr + *size);
403
404 if (rm != rf) {
405 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
406 0, vmprot, flags & VMCMD_RELATIVE);
407 *size = msize;
408 }
409 return 0;
410 }
411
412 /*
413 * elf_load_interp():
414 *
415 * Load an interpreter pointed to by path.
416 */
417 static int
418 elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
419 struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last)
420 {
421 int error, i;
422 struct vnode *vp;
423 struct vattr attr;
424 Elf_Ehdr eh;
425 Elf_Phdr *ph = NULL;
426 const Elf_Phdr *base_ph;
427 const Elf_Phdr *last_ph;
428 u_long phsize;
429 Elf_Addr addr = *last;
430 struct proc *p;
431 bool use_topdown;
432
433 p = l->l_proc;
434
435 KASSERT(p->p_vmspace);
436 KASSERT(p->p_vmspace != proc0.p_vmspace);
437
438 #ifdef __USE_TOPDOWN_VM
439 use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
440 #else
441 use_topdown = false;
442 #endif
443
444 /*
445 * 1. open file
446 * 2. read filehdr
447 * 3. map text, data, and bss out of it using VM_*
448 */
449 vp = epp->ep_interp;
450 if (vp == NULL) {
451 error = emul_find_interp(l, epp, path);
452 if (error != 0)
453 return error;
454 vp = epp->ep_interp;
455 }
456 /* We'll tidy this ourselves - otherwise we have locking issues */
457 epp->ep_interp = NULL;
458 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
459
460 /*
461 * Similarly, if it's not marked as executable, or it's not a regular
462 * file, we don't allow it to be used.
463 */
464 if (vp->v_type != VREG) {
465 error = EACCES;
466 goto badunlock;
467 }
468 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
469 goto badunlock;
470
471 /* get attributes */
472 if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
473 goto badunlock;
474
475 /*
476 * Check mount point. Though we're not trying to exec this binary,
477 * we will be executing code from it, so if the mount point
478 * disallows execution or set-id-ness, we punt or kill the set-id.
479 */
480 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
481 error = EACCES;
482 goto badunlock;
483 }
484 if (vp->v_mount->mnt_flag & MNT_NOSUID)
485 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
486
487 error = vn_marktext(vp);
488 if (error)
489 goto badunlock;
490
491 VOP_UNLOCK(vp);
492
493 if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
494 goto bad;
495
496 if ((error = elf_check_header(&eh)) != 0)
497 goto bad;
498 if (eh.e_type != ET_DYN || eh.e_phnum == 0) {
499 DPRINTF("bad interpreter type %#x", eh.e_type);
500 error = ENOEXEC;
501 goto bad;
502 }
503
504 phsize = eh.e_phnum * sizeof(Elf_Phdr);
505 ph = kmem_alloc(phsize, KM_SLEEP);
506
507 if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
508 goto bad;
509
510 #ifdef ELF_INTERP_NON_RELOCATABLE
511 /*
512 * Evil hack: Only MIPS should be non-relocatable, and the
513 * psections should have a high address (typically 0x5ffe0000).
514 * If it's now relocatable, it should be linked at 0 and the
515 * psections should have zeros in the upper part of the address.
516 * Otherwise, force the load at the linked address.
517 */
518 if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
519 *last = ELFDEFNNAME(NO_ADDR);
520 #endif
521
522 /*
523 * If no position to load the interpreter was set by a probe
524 * function, pick the same address that a non-fixed mmap(0, ..)
525 * would (i.e. something safely out of the way).
526 */
527 if (*last == ELFDEFNNAME(NO_ADDR)) {
528 u_long limit = 0;
529 /*
530 * Find the start and ending addresses of the psections to
531 * be loaded. This will give us the size.
532 */
533 for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) {
534 if (ph[i].p_type == PT_LOAD) {
535 u_long psize = ph[i].p_vaddr + ph[i].p_memsz;
536 if (base_ph == NULL)
537 base_ph = &ph[i];
538 if (psize > limit)
539 limit = psize;
540 }
541 }
542
543 if (base_ph == NULL) {
544 DPRINTF("no interpreter loadable sections");
545 error = ENOEXEC;
546 goto bad;
547 }
548
549 /*
550 * Now compute the size and load address.
551 */
552 addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
553 epp->ep_daddr,
554 round_page(limit) - trunc_page(base_ph->p_vaddr),
555 use_topdown);
556 addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align,
557 use_topdown);
558 } else {
559 addr = *last; /* may be ELF_LINK_ADDR */
560 }
561
562 /*
563 * Load all the necessary sections
564 */
565 for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) {
566 switch (ph[i].p_type) {
567 case PT_LOAD: {
568 u_long size;
569 int flags;
570
571 if (base_ph == NULL) {
572 /*
573 * First encountered psection is always the
574 * base psection. Make sure it's aligned
575 * properly (align down for topdown and align
576 * upwards for not topdown).
577 */
578 base_ph = &ph[i];
579 flags = VMCMD_BASE;
580 if (addr == ELF_LINK_ADDR)
581 addr = ph[i].p_vaddr;
582 if (use_topdown)
583 addr = ELF_TRUNC(addr, ph[i].p_align);
584 else
585 addr = ELF_ROUND(addr, ph[i].p_align);
586 } else {
587 u_long limit = round_page(last_ph->p_vaddr
588 + last_ph->p_memsz);
589 u_long base = trunc_page(ph[i].p_vaddr);
590
591 /*
592 * If there is a gap in between the psections,
593 * map it as inaccessible so nothing else
594 * mmap'ed will be placed there.
595 */
596 if (limit != base) {
597 NEW_VMCMD2(vcset, vmcmd_map_zero,
598 base - limit,
599 limit - base_ph->p_vaddr, NULLVP,
600 0, VM_PROT_NONE, VMCMD_RELATIVE);
601 }
602
603 addr = ph[i].p_vaddr - base_ph->p_vaddr;
604 flags = VMCMD_RELATIVE;
605 }
606 last_ph = &ph[i];
607 if ((error = elf_load_psection(vcset, vp, &ph[i], &addr,
608 &size, flags)) != 0)
609 goto bad;
610 /*
611 * If entry is within this psection then this
612 * must contain the .text section. *entryoff is
613 * relative to the base psection.
614 */
615 if (eh.e_entry >= ph[i].p_vaddr &&
616 eh.e_entry < (ph[i].p_vaddr + size)) {
617 *entryoff = eh.e_entry - base_ph->p_vaddr;
618 }
619 addr += size;
620 break;
621 }
622
623 default:
624 break;
625 }
626 }
627
628 kmem_free(ph, phsize);
629 /*
630 * This value is ignored if TOPDOWN.
631 */
632 *last = addr;
633 vrele(vp);
634 return 0;
635
636 badunlock:
637 VOP_UNLOCK(vp);
638
639 bad:
640 if (ph != NULL)
641 kmem_free(ph, phsize);
642 vrele(vp);
643 return error;
644 }
645
646 /*
647 * exec_elf_makecmds(): Prepare an Elf binary's exec package
648 *
649 * First, set of the various offsets/lengths in the exec package.
650 *
651 * Then, mark the text image busy (so it can be demand paged) or error
652 * out if this is not possible. Finally, set up vmcmds for the
653 * text, data, bss, and stack segments.
654 */
655 int
656 exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
657 {
658 Elf_Ehdr *eh = epp->ep_hdr;
659 Elf_Phdr *ph, *pp;
660 Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
661 int error, i;
662 char *interp = NULL;
663 u_long phsize;
664 struct elf_args *ap;
665 bool is_dyn = false;
666
667 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) {
668 DPRINTF("small header %#x", epp->ep_hdrvalid);
669 return ENOEXEC;
670 }
671 if ((error = elf_check_header(eh)) != 0)
672 return error;
673
674 if (eh->e_type == ET_DYN)
675 /* PIE, and some libs have an entry point */
676 is_dyn = true;
677 else if (eh->e_type != ET_EXEC) {
678 DPRINTF("bad type %#x", eh->e_type);
679 return ENOEXEC;
680 }
681
682 if (eh->e_phnum == 0) {
683 DPRINTF("no program headers");
684 return ENOEXEC;
685 }
686
687 error = vn_marktext(epp->ep_vp);
688 if (error)
689 return error;
690
691 /*
692 * Allocate space to hold all the program headers, and read them
693 * from the file
694 */
695 phsize = eh->e_phnum * sizeof(Elf_Phdr);
696 ph = kmem_alloc(phsize, KM_SLEEP);
697
698 if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
699 0)
700 goto bad;
701
702 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
703 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
704
705 for (i = 0; i < eh->e_phnum; i++) {
706 pp = &ph[i];
707 if (pp->p_type == PT_INTERP) {
708 if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) {
709 DPRINTF("bad interpreter namelen %#jx",
710 (uintmax_t)pp->p_filesz);
711 error = ENOEXEC;
712 goto bad;
713 }
714 interp = PNBUF_GET();
715 if ((error = exec_read_from(l, epp->ep_vp,
716 pp->p_offset, interp, pp->p_filesz)) != 0)
717 goto bad;
718 /* Ensure interp is NUL-terminated and of the expected length */
719 if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) {
720 DPRINTF("bad interpreter name");
721 error = ENOEXEC;
722 goto bad;
723 }
724 break;
725 }
726 }
727
728 /*
729 * On the same architecture, we may be emulating different systems.
730 * See which one will accept this executable.
731 *
732 * Probe functions would normally see if the interpreter (if any)
733 * exists. Emulation packages may possibly replace the interpreter in
734 * interp with a changed path (/emul/xxx/<path>).
735 */
736 pos = ELFDEFNNAME(NO_ADDR);
737 if (epp->ep_esch->u.elf_probe_func) {
738 vaddr_t startp = (vaddr_t)pos;
739
740 error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
741 &startp);
742 if (error)
743 goto bad;
744 pos = (Elf_Addr)startp;
745 }
746
747 if (is_dyn && (error = elf_placedynexec(epp, eh, ph)) != 0)
748 goto bad;
749
750 /*
751 * Load all the necessary sections
752 */
753 for (i = 0; i < eh->e_phnum; i++) {
754 Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
755 u_long size = 0;
756
757 switch (ph[i].p_type) {
758 case PT_LOAD:
759 if ((error = elf_load_psection(&epp->ep_vmcmds,
760 epp->ep_vp, &ph[i], &addr, &size, VMCMD_FIXED))
761 != 0)
762 goto bad;
763
764 /*
765 * Consider this as text segment, if it is executable.
766 * If there is more than one text segment, pick the
767 * largest.
768 */
769 if (ph[i].p_flags & PF_X) {
770 if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
771 size > epp->ep_tsize) {
772 epp->ep_taddr = addr;
773 epp->ep_tsize = size;
774 }
775 end_text = addr + size;
776 } else {
777 epp->ep_daddr = addr;
778 epp->ep_dsize = size;
779 }
780 if (ph[i].p_offset == 0) {
781 computed_phdr = ph[i].p_vaddr + eh->e_phoff;
782 }
783 break;
784
785 case PT_SHLIB:
786 /* SCO has these sections. */
787 case PT_INTERP:
788 /* Already did this one. */
789 case PT_DYNAMIC:
790 case PT_NOTE:
791 break;
792 case PT_PHDR:
793 /* Note address of program headers (in text segment) */
794 phdr = ph[i].p_vaddr;
795 break;
796
797 default:
798 /*
799 * Not fatal; we don't need to understand everything.
800 */
801 break;
802 }
803 }
804
805 if (epp->ep_vmcmds.evs_used == 0) {
806 /* No VMCMD; there was no PT_LOAD section, or those
807 * sections were empty */
808 DPRINTF("no vmcommands");
809 error = ENOEXEC;
810 goto bad;
811 }
812
813 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
814 epp->ep_daddr = round_page(end_text);
815 epp->ep_dsize = 0;
816 }
817
818 /*
819 * Check if we found a dynamically linked binary and arrange to load
820 * its interpreter
821 */
822 if (interp) {
823 u_int nused = epp->ep_vmcmds.evs_used;
824 u_long interp_offset = 0;
825
826 if ((error = elf_load_interp(l, epp, interp,
827 &epp->ep_vmcmds, &interp_offset, &pos)) != 0) {
828 goto bad;
829 }
830 if (epp->ep_vmcmds.evs_used == nused) {
831 /* elf_load_interp() has not set up any new VMCMD */
832 DPRINTF("no vmcommands for interpreter");
833 error = ENOEXEC;
834 goto bad;
835 }
836
837 ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
838 ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr;
839 epp->ep_entryoffset = interp_offset;
840 epp->ep_entry = ap->arg_interp + interp_offset;
841 PNBUF_PUT(interp);
842 interp = NULL;
843 } else {
844 epp->ep_entry = eh->e_entry;
845 if (epp->ep_flags & EXEC_FORCEAUX) {
846 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
847 ap->arg_interp = (vaddr_t)NULL;
848 } else {
849 ap = NULL;
850 }
851 }
852
853 if (ap) {
854 ap->arg_phaddr = phdr ? phdr : computed_phdr;
855 ap->arg_phentsize = eh->e_phentsize;
856 ap->arg_phnum = eh->e_phnum;
857 ap->arg_entry = eh->e_entry;
858 epp->ep_emul_arg = ap;
859 epp->ep_emul_arg_free = elf_free_emul_arg;
860 }
861
862 #ifdef ELF_MAP_PAGE_ZERO
863 /* Dell SVR4 maps page zero, yeuch! */
864 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
865 epp->ep_vp, 0, VM_PROT_READ);
866 #endif
867
868 error = (*epp->ep_esch->es_setup_stack)(l, epp);
869 if (error)
870 goto bad;
871
872 kmem_free(ph, phsize);
873 return 0;
874
875 bad:
876 if (interp)
877 PNBUF_PUT(interp);
878 exec_free_emul_arg(epp);
879 kmem_free(ph, phsize);
880 kill_vmcmds(&epp->ep_vmcmds);
881 return error;
882 }
883
884 int
885 netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
886 Elf_Ehdr *eh)
887 {
888 size_t i;
889 Elf_Phdr *ph;
890 size_t phsize;
891 char *nbuf;
892 int error;
893 int isnetbsd = 0;
894
895 epp->ep_pax_flags = 0;
896
897 if (eh->e_phnum > ELF_MAXPHNUM || eh->e_phnum == 0) {
898 DPRINTF("no signature %#x", eh->e_phnum);
899 return ENOEXEC;
900 }
901
902 phsize = eh->e_phnum * sizeof(Elf_Phdr);
903 ph = kmem_alloc(phsize, KM_SLEEP);
904 error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
905 if (error)
906 goto out;
907
908 nbuf = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP);
909 for (i = 0; i < eh->e_phnum; i++) {
910 const char *nptr;
911 size_t nlen;
912
913 if (ph[i].p_type != PT_NOTE ||
914 ph[i].p_filesz > ELF_MAXNOTESIZE)
915 continue;
916
917 nlen = ph[i].p_filesz;
918 error = exec_read_from(l, epp->ep_vp, ph[i].p_offset,
919 nbuf, nlen);
920 if (error)
921 continue;
922
923 nptr = nbuf;
924 while (nlen > 0) {
925 const Elf_Nhdr *np;
926 const char *ndata, *ndesc;
927
928 /* note header */
929 np = (const Elf_Nhdr *)nptr;
930 if (nlen < sizeof(*np)) {
931 break;
932 }
933 nptr += sizeof(*np);
934 nlen -= sizeof(*np);
935
936 /* note name */
937 ndata = nptr;
938 if (nlen < roundup(np->n_namesz, 4)) {
939 break;
940 }
941 nptr += roundup(np->n_namesz, 4);
942 nlen -= roundup(np->n_namesz, 4);
943
944 /* note description */
945 ndesc = nptr;
946 if (nlen < roundup(np->n_descsz, 4)) {
947 break;
948 }
949 nptr += roundup(np->n_descsz, 4);
950 nlen -= roundup(np->n_descsz, 4);
951
952 isnetbsd |= netbsd_elf_note(epp, np, ndata, ndesc);
953 }
954 }
955 kmem_free(nbuf, ELF_MAXNOTESIZE);
956
957 error = isnetbsd ? 0 : ENOEXEC;
958 #ifdef DEBUG_ELF
959 if (error)
960 DPRINTF("not netbsd");
961 #endif
962 out:
963 kmem_free(ph, phsize);
964 return error;
965 }
966
967 int
968 netbsd_elf_note(struct exec_package *epp,
969 const Elf_Nhdr *np, const char *ndata, const char *ndesc)
970 {
971 int isnetbsd = 0;
972
973 #ifdef DIAGNOSTIC
974 const char *badnote;
975 #define BADNOTE(n) badnote = (n)
976 #else
977 #define BADNOTE(n)
978 #endif
979
980 switch (np->n_type) {
981 case ELF_NOTE_TYPE_NETBSD_TAG:
982 /* It is us */
983 if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ &&
984 np->n_descsz == ELF_NOTE_NETBSD_DESCSZ &&
985 memcmp(ndata, ELF_NOTE_NETBSD_NAME,
986 ELF_NOTE_NETBSD_NAMESZ) == 0) {
987 memcpy(&epp->ep_osversion, ndesc,
988 ELF_NOTE_NETBSD_DESCSZ);
989 isnetbsd = 1;
990 break;
991 }
992
993 /*
994 * Ignore SuSE tags; SuSE's n_type is the same the
995 * NetBSD one.
996 */
997 if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ &&
998 memcmp(ndata, ELF_NOTE_SUSE_NAME,
999 ELF_NOTE_SUSE_NAMESZ) == 0)
1000 break;
1001 /*
1002 * Ignore old GCC
1003 */
1004 if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ &&
1005 memcmp(ndata, ELF_NOTE_OGCC_NAME,
1006 ELF_NOTE_OGCC_NAMESZ) == 0)
1007 break;
1008 BADNOTE("NetBSD tag");
1009 goto bad;
1010
1011 case ELF_NOTE_TYPE_PAX_TAG:
1012 if (np->n_namesz == ELF_NOTE_PAX_NAMESZ &&
1013 np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
1014 memcmp(ndata, ELF_NOTE_PAX_NAME,
1015 ELF_NOTE_PAX_NAMESZ) == 0) {
1016 uint32_t flags;
1017 memcpy(&flags, ndesc, sizeof(flags));
1018 /* Convert the flags and insert them into
1019 * the exec package. */
1020 pax_setup_elf_flags(epp, flags);
1021 break;
1022 }
1023 BADNOTE("PaX tag");
1024 goto bad;
1025
1026 case ELF_NOTE_TYPE_MARCH_TAG:
1027 /* Copy the machine arch into the package. */
1028 if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ
1029 && memcmp(ndata, ELF_NOTE_MARCH_NAME,
1030 ELF_NOTE_MARCH_NAMESZ) == 0) {
1031 /* Do not truncate the buffer */
1032 if (np->n_descsz > sizeof(epp->ep_machine_arch)) {
1033 BADNOTE("description size limit");
1034 goto bad;
1035 }
1036 /*
1037 * Ensure ndesc is NUL-terminated and of the
1038 * expected length.
1039 */
1040 if (strnlen(ndesc, np->n_descsz) + 1 !=
1041 np->n_descsz) {
1042 BADNOTE("description size");
1043 goto bad;
1044 }
1045 strlcpy(epp->ep_machine_arch, ndesc,
1046 sizeof(epp->ep_machine_arch));
1047 break;
1048 }
1049 BADNOTE("march tag");
1050 goto bad;
1051
1052 case ELF_NOTE_TYPE_MCMODEL_TAG:
1053 /* arch specific check for code model */
1054 #ifdef ELF_MD_MCMODEL_CHECK
1055 if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ
1056 && memcmp(ndata, ELF_NOTE_MCMODEL_NAME,
1057 ELF_NOTE_MCMODEL_NAMESZ) == 0) {
1058 ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz);
1059 break;
1060 }
1061 BADNOTE("mcmodel tag");
1062 goto bad;
1063 #endif
1064 break;
1065
1066 case ELF_NOTE_TYPE_SUSE_VERSION_TAG:
1067 break;
1068
1069 case ELF_NOTE_TYPE_GO_BUILDID_TAG:
1070 break;
1071
1072 case ELF_NOTE_TYPE_NETBSD_EMUL_TAG:
1073 /* Ancient NetBSD version tag */
1074 break;
1075
1076 default:
1077 BADNOTE("unknown tag");
1078 bad:
1079 #ifdef DIAGNOSTIC
1080 /* Ignore GNU tags */
1081 if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
1082 memcmp(ndata, ELF_NOTE_GNU_NAME,
1083 ELF_NOTE_GNU_NAMESZ) == 0)
1084 break;
1085
1086 int ns = (int)np->n_namesz;
1087 printf("%s: Unknown elf note type %d (%s): "
1088 "[namesz=%d, descsz=%d name=%-*.*s]\n",
1089 epp->ep_kname, np->n_type, badnote, np->n_namesz,
1090 np->n_descsz, ns, ns, ndata);
1091 #endif
1092 break;
1093 }
1094
1095 return isnetbsd;
1096 }
1097
1098 int
1099 netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
1100 vaddr_t *pos)
1101 {
1102 int error;
1103
1104 if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
1105 return error;
1106 #ifdef ELF_MD_PROBE_FUNC
1107 if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
1108 return error;
1109 #elif defined(ELF_INTERP_NON_RELOCATABLE)
1110 *pos = ELF_LINK_ADDR;
1111 #endif
1112 epp->ep_flags |= EXEC_FORCEAUX;
1113 return 0;
1114 }
1115
1116 void
1117 elf_free_emul_arg(void *arg)
1118 {
1119 struct elf_args *ap = arg;
1120 KASSERT(ap != NULL);
1121 kmem_free(ap, sizeof(*ap));
1122 }
1123