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