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