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