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