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