exec_elf.c revision 1.33 1 /* $NetBSD: exec_elf.c,v 1.33 2011/11/19 22:51:25 tls Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 2000, 2005 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.
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.33 2011/11/19 22:51:25 tls 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/malloc.h>
69 #include <sys/kmem.h>
70 #include <sys/namei.h>
71 #include <sys/vnode.h>
72 #include <sys/exec.h>
73 #include <sys/exec_elf.h>
74 #include <sys/syscall.h>
75 #include <sys/signalvar.h>
76 #include <sys/mount.h>
77 #include <sys/stat.h>
78 #include <sys/kauth.h>
79 #include <sys/bitops.h>
80 #include <sys/cprng.h>
81
82 #include <sys/cpu.h>
83 #include <machine/reg.h>
84
85 #include <compat/common/compat_util.h>
86
87 #include <sys/pax.h>
88
89 extern struct emul emul_netbsd;
90
91 #define elf_check_header ELFNAME(check_header)
92 #define elf_copyargs ELFNAME(copyargs)
93 #define elf_load_file ELFNAME(load_file)
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_probe ELFNAME2(netbsd,probe)
98 #define coredump ELFNAMEEND(coredump)
99
100 int elf_load_file(struct lwp *, struct exec_package *, char *,
101 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
102 void elf_load_psection(struct exec_vmcmd_set *, struct vnode *,
103 const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
104
105 int netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
106 int netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
107 vaddr_t *);
108
109 /* round up and down to page boundaries. */
110 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
111 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
112
113 /*
114 * Arbitrary limits to avoid DoS for excessive memory allocation.
115 */
116 #define MAXPHNUM 128
117 #define MAXSHNUM 32768
118 #define MAXNOTESIZE 1024
119
120 static void
121 elf_placedynexec(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh,
122 Elf_Phdr *ph)
123 {
124 Elf_Addr align, offset;
125 int i;
126
127 for (align = i = 0; i < eh->e_phnum; i++)
128 if (ph[i].p_type == PT_LOAD && ph[i].p_align > align)
129 align = ph[i].p_align;
130
131 #ifdef PAX_ASLR
132 if (pax_aslr_active(l)) {
133 size_t pax_align, l2, delta;
134 uint32_t r;
135
136 pax_align = align;
137
138 r = cprng_fast32();
139
140 if (pax_align == 0)
141 pax_align = PGSHIFT;
142 l2 = ilog2(pax_align);
143 delta = PAX_ASLR_DELTA(r, l2, PAX_ASLR_DELTA_EXEC_LEN);
144 offset = ELF_TRUNC(delta, pax_align) + PAGE_SIZE;
145 #ifdef PAX_ASLR_DEBUG
146 uprintf("r=0x%x l2=0x%zx PGSHIFT=0x%x Delta=0x%zx\n", r, l2,
147 PGSHIFT, delta);
148 uprintf("pax offset=0x%llx entry=0x%llx\n",
149 (unsigned long long)offset,
150 (unsigned long long)eh->e_entry);
151 #endif /* PAX_ASLR_DEBUG */
152 } else
153 #endif /* PAX_ASLR */
154 offset = MAX(align, PAGE_SIZE);
155
156 offset += epp->ep_vm_minaddr;
157
158 for (i = 0; i < eh->e_phnum; i++)
159 ph[i].p_vaddr += offset;
160 eh->e_entry += offset;
161 }
162
163 /*
164 * Copy arguments onto the stack in the normal way, but add some
165 * extra information in case of dynamic binding.
166 */
167 int
168 elf_copyargs(struct lwp *l, struct exec_package *pack,
169 struct ps_strings *arginfo, char **stackp, void *argp)
170 {
171 size_t len, vlen;
172 AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname;
173 struct elf_args *ap;
174 int error;
175
176 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
177 return error;
178
179 a = ai;
180 execname = NULL;
181
182 /*
183 * Push extra arguments on the stack needed by dynamically
184 * linked binaries
185 */
186 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
187 struct vattr *vap = pack->ep_vap;
188
189 a->a_type = AT_PHDR;
190 a->a_v = ap->arg_phaddr;
191 a++;
192
193 a->a_type = AT_PHENT;
194 a->a_v = ap->arg_phentsize;
195 a++;
196
197 a->a_type = AT_PHNUM;
198 a->a_v = ap->arg_phnum;
199 a++;
200
201 a->a_type = AT_PAGESZ;
202 a->a_v = PAGE_SIZE;
203 a++;
204
205 a->a_type = AT_BASE;
206 a->a_v = ap->arg_interp;
207 a++;
208
209 a->a_type = AT_FLAGS;
210 a->a_v = 0;
211 a++;
212
213 a->a_type = AT_ENTRY;
214 a->a_v = ap->arg_entry;
215 a++;
216
217 a->a_type = AT_EUID;
218 if (vap->va_mode & S_ISUID)
219 a->a_v = vap->va_uid;
220 else
221 a->a_v = kauth_cred_geteuid(l->l_cred);
222 a++;
223
224 a->a_type = AT_RUID;
225 a->a_v = kauth_cred_getuid(l->l_cred);
226 a++;
227
228 a->a_type = AT_EGID;
229 if (vap->va_mode & S_ISGID)
230 a->a_v = vap->va_gid;
231 else
232 a->a_v = kauth_cred_getegid(l->l_cred);
233 a++;
234
235 a->a_type = AT_RGID;
236 a->a_v = kauth_cred_getgid(l->l_cred);
237 a++;
238
239 if (pack->ep_path) {
240 execname = a;
241 a->a_type = AT_SUN_EXECNAME;
242 a++;
243 }
244
245 free(ap, M_TEMP);
246 pack->ep_emul_arg = NULL;
247 }
248
249 a->a_type = AT_NULL;
250 a->a_v = 0;
251 a++;
252
253 vlen = (a - ai) * sizeof(AuxInfo);
254
255 if (execname) {
256 char *path = pack->ep_path;
257 execname->a_v = (uintptr_t)(*stackp + vlen);
258 len = strlen(path) + 1;
259 if ((error = copyout(path, (*stackp + vlen), len)) != 0)
260 return error;
261 len = ALIGN(len);
262 } else
263 len = 0;
264
265 if ((error = copyout(ai, *stackp, vlen)) != 0)
266 return error;
267 *stackp += vlen + len;
268
269 return 0;
270 }
271
272 /*
273 * elf_check_header():
274 *
275 * Check header for validity; return 0 of ok ENOEXEC if error
276 */
277 int
278 elf_check_header(Elf_Ehdr *eh, int type)
279 {
280
281 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
282 eh->e_ident[EI_CLASS] != ELFCLASS)
283 return ENOEXEC;
284
285 switch (eh->e_machine) {
286
287 ELFDEFNNAME(MACHDEP_ID_CASES)
288
289 default:
290 return ENOEXEC;
291 }
292
293 if (ELF_EHDR_FLAGS_OK(eh) == 0)
294 return ENOEXEC;
295
296 if (eh->e_type != type)
297 return ENOEXEC;
298
299 if (eh->e_shnum > MAXSHNUM || eh->e_phnum > MAXPHNUM)
300 return ENOEXEC;
301
302 return 0;
303 }
304
305 /*
306 * elf_load_psection():
307 *
308 * Load a psection at the appropriate address
309 */
310 void
311 elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
312 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
313 {
314 u_long msize, psize, rm, rf;
315 long diff, offset;
316
317 /*
318 * If the user specified an address, then we load there.
319 */
320 if (*addr == ELFDEFNNAME(NO_ADDR))
321 *addr = ph->p_vaddr;
322
323 if (ph->p_align > 1) {
324 /*
325 * Make sure we are virtually aligned as we are supposed to be.
326 */
327 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
328 KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align));
329 /*
330 * But make sure to not map any pages before the start of the
331 * psection by limiting the difference to within a page.
332 */
333 diff &= PAGE_MASK;
334 } else
335 diff = 0;
336
337 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
338 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
339 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
340
341 /*
342 * Adjust everything so it all starts on a page boundary.
343 */
344 *addr -= diff;
345 offset = ph->p_offset - diff;
346 *size = ph->p_filesz + diff;
347 msize = ph->p_memsz + diff;
348
349 if (ph->p_align >= PAGE_SIZE) {
350 if ((ph->p_flags & PF_W) != 0) {
351 /*
352 * Because the pagedvn pager can't handle zero fill
353 * of the last data page if it's not page aligned we
354 * map the last page readvn.
355 */
356 psize = trunc_page(*size);
357 } else {
358 psize = round_page(*size);
359 }
360 } else {
361 psize = *size;
362 }
363
364 if (psize > 0) {
365 NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
366 vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
367 offset, *prot, flags);
368 flags &= VMCMD_RELATIVE;
369 }
370 if (psize < *size) {
371 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
372 *addr + psize, vp, offset + psize, *prot, flags);
373 }
374
375 /*
376 * Check if we need to extend the size of the segment (does
377 * bss extend page the next page boundary)?
378 */
379 rm = round_page(*addr + msize);
380 rf = round_page(*addr + *size);
381
382 if (rm != rf) {
383 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
384 0, *prot, flags & VMCMD_RELATIVE);
385 *size = msize;
386 }
387 }
388
389 /*
390 * elf_load_file():
391 *
392 * Load a file (interpreter/library) pointed to by path
393 * [stolen from coff_load_shlib()]. Made slightly generic
394 * so it might be used externally.
395 */
396 int
397 elf_load_file(struct lwp *l, struct exec_package *epp, char *path,
398 struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap,
399 Elf_Addr *last)
400 {
401 int error, i;
402 struct vnode *vp;
403 struct vattr attr;
404 Elf_Ehdr eh;
405 Elf_Phdr *ph = NULL;
406 const Elf_Phdr *ph0;
407 const Elf_Phdr *base_ph;
408 const Elf_Phdr *last_ph;
409 u_long phsize;
410 Elf_Addr addr = *last;
411 struct proc *p;
412
413 p = l->l_proc;
414
415 /*
416 * 1. open file
417 * 2. read filehdr
418 * 3. map text, data, and bss out of it using VM_*
419 */
420 vp = epp->ep_interp;
421 if (vp == NULL) {
422 error = emul_find_interp(l, epp, path);
423 if (error != 0)
424 return error;
425 vp = epp->ep_interp;
426 }
427 /* We'll tidy this ourselves - otherwise we have locking issues */
428 epp->ep_interp = NULL;
429 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
430
431 /*
432 * Similarly, if it's not marked as executable, or it's not a regular
433 * file, we don't allow it to be used.
434 */
435 if (vp->v_type != VREG) {
436 error = EACCES;
437 goto badunlock;
438 }
439 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
440 goto badunlock;
441
442 /* get attributes */
443 if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
444 goto badunlock;
445
446 /*
447 * Check mount point. Though we're not trying to exec this binary,
448 * we will be executing code from it, so if the mount point
449 * disallows execution or set-id-ness, we punt or kill the set-id.
450 */
451 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
452 error = EACCES;
453 goto badunlock;
454 }
455 if (vp->v_mount->mnt_flag & MNT_NOSUID)
456 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
457
458 #ifdef notyet /* XXX cgd 960926 */
459 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
460
461 XXXps: this problem will make it impossible to use an interpreter
462 from a file system which actually does something in VOP_OPEN
463 #endif
464
465 error = vn_marktext(vp);
466 if (error)
467 goto badunlock;
468
469 VOP_UNLOCK(vp);
470
471 if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
472 goto bad;
473
474 if ((error = elf_check_header(&eh, ET_DYN)) != 0)
475 goto bad;
476
477 if (eh.e_phnum > MAXPHNUM || eh.e_phnum == 0) {
478 error = ENOEXEC;
479 goto bad;
480 }
481
482 phsize = eh.e_phnum * sizeof(Elf_Phdr);
483 ph = kmem_alloc(phsize, KM_SLEEP);
484
485 if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
486 goto bad;
487
488 #ifdef ELF_INTERP_NON_RELOCATABLE
489 /*
490 * Evil hack: Only MIPS should be non-relocatable, and the
491 * psections should have a high address (typically 0x5ffe0000).
492 * If it's now relocatable, it should be linked at 0 and the
493 * psections should have zeros in the upper part of the address.
494 * Otherwise, force the load at the linked address.
495 */
496 if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
497 *last = ELFDEFNNAME(NO_ADDR);
498 #endif
499
500 /*
501 * If no position to load the interpreter was set by a probe
502 * function, pick the same address that a non-fixed mmap(0, ..)
503 * would (i.e. something safely out of the way).
504 */
505 if (*last == ELFDEFNNAME(NO_ADDR)) {
506 u_long limit = 0;
507 /*
508 * Find the start and ending addresses of the psections to
509 * be loaded. This will give us the size.
510 */
511 for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum;
512 i++, ph0++) {
513 if (ph0->p_type == PT_LOAD) {
514 u_long psize = ph0->p_vaddr + ph0->p_memsz;
515 if (base_ph == NULL)
516 base_ph = ph0;
517 if (psize > limit)
518 limit = psize;
519 }
520 }
521
522 if (base_ph == NULL) {
523 error = ENOEXEC;
524 goto bad;
525 }
526
527 /*
528 * Now compute the size and load address.
529 */
530 addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
531 epp->ep_daddr,
532 round_page(limit) - trunc_page(base_ph->p_vaddr));
533 } else
534 addr = *last; /* may be ELF_LINK_ADDR */
535
536 /*
537 * Load all the necessary sections
538 */
539 for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
540 i < eh.e_phnum; i++, ph0++) {
541 switch (ph0->p_type) {
542 case PT_LOAD: {
543 u_long size;
544 int prot = 0;
545 int flags;
546
547 if (base_ph == NULL) {
548 /*
549 * First encountered psection is always the
550 * base psection. Make sure it's aligned
551 * properly (align down for topdown and align
552 * upwards for not topdown).
553 */
554 base_ph = ph0;
555 flags = VMCMD_BASE;
556 if (addr == ELF_LINK_ADDR)
557 addr = ph0->p_vaddr;
558 if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
559 addr = ELF_TRUNC(addr, ph0->p_align);
560 else
561 addr = ELF_ROUND(addr, ph0->p_align);
562 } else {
563 u_long limit = round_page(last_ph->p_vaddr
564 + last_ph->p_memsz);
565 u_long base = trunc_page(ph0->p_vaddr);
566
567 /*
568 * If there is a gap in between the psections,
569 * map it as inaccessible so nothing else
570 * mmap'ed will be placed there.
571 */
572 if (limit != base) {
573 NEW_VMCMD2(vcset, vmcmd_map_zero,
574 base - limit,
575 limit - base_ph->p_vaddr, NULLVP,
576 0, VM_PROT_NONE, VMCMD_RELATIVE);
577 }
578
579 addr = ph0->p_vaddr - base_ph->p_vaddr;
580 flags = VMCMD_RELATIVE;
581 }
582 last_ph = ph0;
583 elf_load_psection(vcset, vp, &ph[i], &addr,
584 &size, &prot, flags);
585 /*
586 * If entry is within this psection then this
587 * must contain the .text section. *entryoff is
588 * relative to the base psection.
589 */
590 if (eh.e_entry >= ph0->p_vaddr &&
591 eh.e_entry < (ph0->p_vaddr + size)) {
592 *entryoff = eh.e_entry - base_ph->p_vaddr;
593 }
594 addr += size;
595 break;
596 }
597
598 case PT_DYNAMIC:
599 case PT_PHDR:
600 break;
601
602 case PT_NOTE:
603 break;
604
605 default:
606 break;
607 }
608 }
609
610 kmem_free(ph, phsize);
611 /*
612 * This value is ignored if TOPDOWN.
613 */
614 *last = addr;
615 vrele(vp);
616 return 0;
617
618 badunlock:
619 VOP_UNLOCK(vp);
620
621 bad:
622 if (ph != NULL)
623 kmem_free(ph, phsize);
624 #ifdef notyet /* XXX cgd 960926 */
625 (maybe) VOP_CLOSE it
626 #endif
627 vrele(vp);
628 return error;
629 }
630
631 /*
632 * exec_elf_makecmds(): Prepare an Elf binary's exec package
633 *
634 * First, set of the various offsets/lengths in the exec package.
635 *
636 * Then, mark the text image busy (so it can be demand paged) or error
637 * out if this is not possible. Finally, set up vmcmds for the
638 * text, data, bss, and stack segments.
639 */
640 int
641 exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
642 {
643 Elf_Ehdr *eh = epp->ep_hdr;
644 Elf_Phdr *ph, *pp;
645 Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
646 int error, i, nload;
647 char *interp = NULL;
648 u_long phsize;
649 struct proc *p;
650 struct elf_args *ap = NULL;
651 bool is_dyn;
652
653 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
654 return ENOEXEC;
655
656 is_dyn = elf_check_header(eh, ET_DYN) == 0;
657 /*
658 * XXX allow for executing shared objects. It seems silly
659 * but other ELF-based systems allow it as well.
660 */
661 if (elf_check_header(eh, ET_EXEC) != 0 && !is_dyn)
662 return ENOEXEC;
663
664 if (eh->e_phnum > MAXPHNUM || eh->e_phnum == 0)
665 return ENOEXEC;
666
667 error = vn_marktext(epp->ep_vp);
668 if (error)
669 return error;
670
671 /*
672 * Allocate space to hold all the program headers, and read them
673 * from the file
674 */
675 p = l->l_proc;
676 phsize = eh->e_phnum * sizeof(Elf_Phdr);
677 ph = kmem_alloc(phsize, KM_SLEEP);
678
679 if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
680 0)
681 goto bad;
682
683 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
684 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
685
686 for (i = 0; i < eh->e_phnum; i++) {
687 pp = &ph[i];
688 if (pp->p_type == PT_INTERP) {
689 if (pp->p_filesz >= MAXPATHLEN) {
690 error = ENOEXEC;
691 goto bad;
692 }
693 interp = PNBUF_GET();
694 interp[0] = '\0';
695 if ((error = exec_read_from(l, epp->ep_vp,
696 pp->p_offset, interp, pp->p_filesz)) != 0)
697 goto bad;
698 break;
699 }
700 }
701
702 /*
703 * On the same architecture, we may be emulating different systems.
704 * See which one will accept this executable.
705 *
706 * Probe functions would normally see if the interpreter (if any)
707 * exists. Emulation packages may possibly replace the interpreter in
708 * interp[] with a changed path (/emul/xxx/<path>).
709 */
710 pos = ELFDEFNNAME(NO_ADDR);
711 if (epp->ep_esch->u.elf_probe_func) {
712 vaddr_t startp = (vaddr_t)pos;
713
714 error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
715 &startp);
716 if (error)
717 goto bad;
718 pos = (Elf_Addr)startp;
719 }
720
721 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
722 p->p_pax = epp->ep_pax_flags;
723 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
724
725 if (is_dyn)
726 elf_placedynexec(l, epp, eh, ph);
727
728 /*
729 * Load all the necessary sections
730 */
731 for (i = nload = 0; i < eh->e_phnum; i++) {
732 Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
733 u_long size = 0;
734 int prot = 0;
735
736 pp = &ph[i];
737
738 switch (ph[i].p_type) {
739 case PT_LOAD:
740 elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
741 &ph[i], &addr, &size, &prot, VMCMD_FIXED);
742
743 /*
744 * Consider this as text segment, if it is executable.
745 * If there is more than one text segment, pick the
746 * largest.
747 */
748 if (ph[i].p_flags & PF_X) {
749 if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
750 size > epp->ep_tsize) {
751 epp->ep_taddr = addr;
752 epp->ep_tsize = size;
753 }
754 end_text = addr + size;
755 } else {
756 epp->ep_daddr = addr;
757 epp->ep_dsize = size;
758 }
759 if (ph[i].p_offset == 0) {
760 computed_phdr = ph[i].p_vaddr + eh->e_phoff;
761 }
762 break;
763
764 case PT_SHLIB:
765 /* SCO has these sections. */
766 case PT_INTERP:
767 /* Already did this one. */
768 case PT_DYNAMIC:
769 break;
770 case PT_NOTE:
771 break;
772 case PT_PHDR:
773 /* Note address of program headers (in text segment) */
774 phdr = pp->p_vaddr;
775 break;
776
777 default:
778 /*
779 * Not fatal; we don't need to understand everything.
780 */
781 break;
782 }
783 }
784 if (interp || (epp->ep_flags & EXEC_FORCEAUX) != 0) {
785 ap = malloc(sizeof(struct elf_args), M_TEMP, M_WAITOK);
786 ap->arg_interp = (vaddr_t)NULL;
787 }
788
789 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
790 epp->ep_daddr = round_page(end_text);
791 epp->ep_dsize = 0;
792 }
793
794 /*
795 * Check if we found a dynamically linked binary and arrange to load
796 * its interpreter
797 */
798 if (interp) {
799 int j = epp->ep_vmcmds.evs_used;
800 u_long interp_offset;
801
802 if ((error = elf_load_file(l, epp, interp,
803 &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
804 goto bad;
805 }
806 ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr;
807 epp->ep_entry = ap->arg_interp + interp_offset;
808 PNBUF_PUT(interp);
809 } else
810 epp->ep_entry = eh->e_entry;
811
812 if (ap) {
813 ap->arg_phaddr = phdr ? phdr : computed_phdr;
814 ap->arg_phentsize = eh->e_phentsize;
815 ap->arg_phnum = eh->e_phnum;
816 ap->arg_entry = eh->e_entry;
817 epp->ep_emul_arg = ap;
818 }
819
820 #ifdef ELF_MAP_PAGE_ZERO
821 /* Dell SVR4 maps page zero, yeuch! */
822 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
823 epp->ep_vp, 0, VM_PROT_READ);
824 #endif
825 kmem_free(ph, phsize);
826 return (*epp->ep_esch->es_setup_stack)(l, epp);
827
828 bad:
829 if (interp)
830 PNBUF_PUT(interp);
831 if (ap)
832 free(ap, M_TEMP);
833 kmem_free(ph, phsize);
834 kill_vmcmds(&epp->ep_vmcmds);
835 return error;
836 }
837
838 int
839 netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
840 Elf_Ehdr *eh)
841 {
842 size_t i;
843 Elf_Shdr *sh;
844 Elf_Nhdr *np;
845 size_t shsize;
846 int error;
847 int isnetbsd = 0;
848 char *ndata;
849
850 epp->ep_pax_flags = 0;
851 if (eh->e_shnum > MAXSHNUM || eh->e_shnum == 0)
852 return ENOEXEC;
853
854 shsize = eh->e_shnum * sizeof(Elf_Shdr);
855 sh = kmem_alloc(shsize, KM_SLEEP);
856 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
857 if (error)
858 goto out;
859
860 np = kmem_alloc(MAXNOTESIZE, KM_SLEEP);
861 for (i = 0; i < eh->e_shnum; i++) {
862 Elf_Shdr *shp = &sh[i];
863
864 if (shp->sh_type != SHT_NOTE ||
865 shp->sh_size > MAXNOTESIZE ||
866 shp->sh_size < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
867 continue;
868
869 error = exec_read_from(l, epp->ep_vp, shp->sh_offset, np,
870 shp->sh_size);
871 if (error)
872 continue;
873
874 ndata = (char *)(np + 1);
875 switch (np->n_type) {
876 case ELF_NOTE_TYPE_NETBSD_TAG:
877 if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
878 np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
879 memcmp(ndata, ELF_NOTE_NETBSD_NAME,
880 ELF_NOTE_NETBSD_NAMESZ))
881 goto bad;
882 isnetbsd = 1;
883 break;
884
885 case ELF_NOTE_TYPE_PAX_TAG:
886 if (np->n_namesz != ELF_NOTE_PAX_NAMESZ ||
887 np->n_descsz != ELF_NOTE_PAX_DESCSZ ||
888 memcmp(ndata, ELF_NOTE_PAX_NAME,
889 ELF_NOTE_PAX_NAMESZ)) {
890 bad:
891 /*
892 * Ignore GNU tags
893 */
894 if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
895 memcmp(ndata, ELF_NOTE_GNU_NAME,
896 ELF_NOTE_GNU_NAMESZ) == 0)
897 break;
898 #ifdef DIAGNOSTIC
899 printf("%s: bad tag %d: "
900 "[%d %d, %d %d, %*.*s %*.*s]\n",
901 epp->ep_kname,
902 np->n_type,
903 np->n_namesz, ELF_NOTE_PAX_NAMESZ,
904 np->n_descsz, ELF_NOTE_PAX_DESCSZ,
905 ELF_NOTE_PAX_NAMESZ,
906 ELF_NOTE_PAX_NAMESZ,
907 ndata,
908 ELF_NOTE_PAX_NAMESZ,
909 ELF_NOTE_PAX_NAMESZ,
910 ELF_NOTE_PAX_NAME);
911 #endif
912 continue;
913 }
914 (void)memcpy(&epp->ep_pax_flags,
915 ndata + ELF_NOTE_PAX_NAMESZ,
916 sizeof(epp->ep_pax_flags));
917 break;
918
919 case ELF_NOTE_TYPE_SUSE_TAG:
920 break;
921
922 default:
923 #ifdef DIAGNOSTIC
924 printf("%s: unknown note type %d\n", epp->ep_kname,
925 np->n_type);
926 #endif
927 break;
928 }
929 }
930 kmem_free(np, MAXNOTESIZE);
931
932 error = isnetbsd ? 0 : ENOEXEC;
933 out:
934 kmem_free(sh, shsize);
935 return error;
936 }
937
938 int
939 netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
940 vaddr_t *pos)
941 {
942 int error;
943
944 if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
945 return error;
946 #ifdef ELF_MD_PROBE_FUNC
947 if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
948 return error;
949 #elif defined(ELF_INTERP_NON_RELOCATABLE)
950 *pos = ELF_LINK_ADDR;
951 #endif
952 epp->ep_flags |= EXEC_FORCEAUX;
953 return 0;
954 }
955