exec_elf.c revision 1.97.2.2 1 /* $NetBSD: exec_elf.c,v 1.97.2.2 2020/04/08 14:08:51 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 2000, 2005, 2015, 2020 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 and Maxime Villard.
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.97.2.2 2020/04/08 14:08:51 martin 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
80 #include <sys/cpu.h>
81 #include <machine/reg.h>
82
83 #include <compat/common/compat_util.h>
84
85 #include <sys/pax.h>
86 #include <uvm/uvm_param.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_populate_auxv ELFNAME(populate_auxv)
93 #define elf_load_interp ELFNAME(load_interp)
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_note ELFNAME2(netbsd,note)
98 #define netbsd_elf_probe ELFNAME2(netbsd,probe)
99 #define coredump ELFNAMEEND(coredump)
100 #define elf_free_emul_arg ELFNAME(free_emul_arg)
101
102 static int
103 elf_load_interp(struct lwp *, struct exec_package *, char *,
104 struct exec_vmcmd_set *, u_long *, Elf_Addr *);
105 static int
106 elf_load_psection(struct exec_vmcmd_set *, struct vnode *, const Elf_Phdr *,
107 Elf_Addr *, u_long *, int);
108
109 int netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
110 int netbsd_elf_note(struct exec_package *, const Elf_Nhdr *, const char *,
111 const char *);
112 int netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
113 vaddr_t *);
114
115 static void elf_free_emul_arg(void *);
116
117 #ifdef DEBUG_ELF
118 #define DPRINTF(a, ...) printf("%s: " a "\n", __func__, ##__VA_ARGS__)
119 #else
120 #define DPRINTF(a, ...)
121 #endif
122
123 /* round up and down to page boundaries. */
124 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
125 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
126
127 static int
128 elf_placedynexec(struct exec_package *epp, Elf_Ehdr *eh, Elf_Phdr *ph)
129 {
130 Elf_Addr align, offset;
131 int i;
132
133 for (align = 1, i = 0; i < eh->e_phnum; i++)
134 if (ph[i].p_type == PT_LOAD && ph[i].p_align > align)
135 align = ph[i].p_align;
136
137 offset = (Elf_Addr)pax_aslr_exec_offset(epp, align);
138 if (offset < epp->ep_vm_minaddr)
139 offset = roundup(epp->ep_vm_minaddr, align);
140 if ((offset & (align - 1)) != 0) {
141 DPRINTF("bad offset=%#jx align=%#jx",
142 (uintmax_t)offset, (uintmax_t)align);
143 return EINVAL;
144 }
145
146 for (i = 0; i < eh->e_phnum; i++)
147 ph[i].p_vaddr += offset;
148 epp->ep_entryoffset = offset;
149 eh->e_entry += offset;
150 return 0;
151 }
152
153
154 int
155 elf_populate_auxv(struct lwp *l, struct exec_package *pack, char **stackp)
156 {
157 size_t len, vlen;
158 AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname;
159 struct elf_args *ap;
160 int error;
161
162 a = ai;
163
164 memset(ai, 0, sizeof(ai));
165
166 /*
167 * Push extra arguments on the stack needed by dynamically
168 * linked binaries
169 */
170 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
171 struct vattr *vap = pack->ep_vap;
172
173 a->a_type = AT_PHDR;
174 a->a_v = ap->arg_phaddr;
175 a++;
176
177 a->a_type = AT_PHENT;
178 a->a_v = ap->arg_phentsize;
179 a++;
180
181 a->a_type = AT_PHNUM;
182 a->a_v = ap->arg_phnum;
183 a++;
184
185 a->a_type = AT_PAGESZ;
186 a->a_v = PAGE_SIZE;
187 a++;
188
189 a->a_type = AT_BASE;
190 a->a_v = ap->arg_interp;
191 a++;
192
193 a->a_type = AT_FLAGS;
194 a->a_v = 0;
195 a++;
196
197 a->a_type = AT_ENTRY;
198 a->a_v = ap->arg_entry;
199 a++;
200
201 a->a_type = AT_EUID;
202 if (vap->va_mode & S_ISUID)
203 a->a_v = vap->va_uid;
204 else
205 a->a_v = kauth_cred_geteuid(l->l_cred);
206 a++;
207
208 a->a_type = AT_RUID;
209 a->a_v = kauth_cred_getuid(l->l_cred);
210 a++;
211
212 a->a_type = AT_EGID;
213 if (vap->va_mode & S_ISGID)
214 a->a_v = vap->va_gid;
215 else
216 a->a_v = kauth_cred_getegid(l->l_cred);
217 a++;
218
219 a->a_type = AT_RGID;
220 a->a_v = kauth_cred_getgid(l->l_cred);
221 a++;
222
223 a->a_type = AT_STACKBASE;
224 a->a_v = l->l_proc->p_stackbase;
225 a++;
226
227 execname = a;
228 a->a_type = AT_SUN_EXECNAME;
229 a++;
230
231 exec_free_emul_arg(pack);
232 } else {
233 execname = NULL;
234 }
235
236 a->a_type = AT_NULL;
237 a->a_v = 0;
238 a++;
239
240 vlen = (a - ai) * sizeof(ai[0]);
241
242 KASSERT(vlen <= sizeof(ai));
243
244 if (execname) {
245 char *path = l->l_proc->p_path;
246 execname->a_v = (uintptr_t)(*stackp + vlen);
247 len = strlen(path) + 1;
248 if ((error = copyout(path, (*stackp + vlen), len)) != 0)
249 return error;
250 len = ALIGN(len);
251 } else {
252 len = 0;
253 }
254
255 if ((error = copyout(ai, *stackp, vlen)) != 0)
256 return error;
257 *stackp += vlen + len;
258
259 return 0;
260 }
261
262 /*
263 * Copy arguments onto the stack in the normal way, but add some
264 * extra information in case of dynamic binding.
265 */
266 int
267 elf_copyargs(struct lwp *l, struct exec_package *pack,
268 struct ps_strings *arginfo, char **stackp, void *argp)
269 {
270 int error;
271
272 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
273 return error;
274
275 return elf_populate_auxv(l, pack, stackp);
276 }
277
278 /*
279 * elf_check_header():
280 *
281 * Check header for validity; return 0 if ok, ENOEXEC if error
282 */
283 int
284 elf_check_header(Elf_Ehdr *eh)
285 {
286
287 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
288 eh->e_ident[EI_CLASS] != ELFCLASS) {
289 DPRINTF("bad magic e_ident[EI_MAG0,EI_MAG3] %#x%x%x%x, "
290 "e_ident[EI_CLASS] %#x", eh->e_ident[EI_MAG0],
291 eh->e_ident[EI_MAG1], eh->e_ident[EI_MAG2],
292 eh->e_ident[EI_MAG3], eh->e_ident[EI_CLASS]);
293 return ENOEXEC;
294 }
295
296 switch (eh->e_machine) {
297
298 ELFDEFNNAME(MACHDEP_ID_CASES)
299
300 default:
301 DPRINTF("bad machine %#x", eh->e_machine);
302 return ENOEXEC;
303 }
304
305 if (ELF_EHDR_FLAGS_OK(eh) == 0) {
306 DPRINTF("bad flags %#x", eh->e_flags);
307 return ENOEXEC;
308 }
309
310 if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM) {
311 DPRINTF("bad shnum/phnum %#x/%#x", eh->e_shnum, eh->e_phnum);
312 return ENOEXEC;
313 }
314
315 return 0;
316 }
317
318 /*
319 * elf_load_psection():
320 *
321 * Load a psection at the appropriate address
322 */
323 static int
324 elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
325 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags)
326 {
327 u_long msize, psize, rm, rf;
328 long diff, offset;
329 int vmprot = 0;
330
331 KASSERT(VOP_ISLOCKED(vp) != LK_NONE);
332
333 /*
334 * If the user specified an address, then we load there.
335 */
336 if (*addr == ELFDEFNNAME(NO_ADDR))
337 *addr = ph->p_vaddr;
338
339 if (ph->p_align > 1) {
340 /*
341 * Make sure we are virtually aligned as we are supposed to be.
342 */
343 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
344 if (*addr - diff != ELF_TRUNC(*addr, ph->p_align)) {
345 DPRINTF("bad alignment %#jx != %#jx\n",
346 (uintptr_t)(*addr - diff),
347 (uintptr_t)ELF_TRUNC(*addr, ph->p_align));
348 return EINVAL;
349 }
350 /*
351 * But make sure to not map any pages before the start of the
352 * psection by limiting the difference to within a page.
353 */
354 diff &= PAGE_MASK;
355 } else
356 diff = 0;
357
358 vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
359 vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
360 vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
361
362 /*
363 * Adjust everything so it all starts on a page boundary.
364 */
365 *addr -= diff;
366 offset = ph->p_offset - diff;
367 *size = ph->p_filesz + diff;
368 msize = ph->p_memsz + diff;
369
370 if (ph->p_align >= PAGE_SIZE) {
371 if ((ph->p_flags & PF_W) != 0) {
372 /*
373 * Because the pagedvn pager can't handle zero fill
374 * of the last data page if it's not page aligned we
375 * map the last page readvn.
376 */
377 psize = trunc_page(*size);
378 } else {
379 psize = round_page(*size);
380 }
381 } else {
382 psize = *size;
383 }
384
385 if (psize > 0) {
386 NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
387 vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
388 offset, vmprot, flags);
389 flags &= VMCMD_RELATIVE;
390 }
391 if (psize < *size) {
392 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
393 *addr + psize, vp, offset + psize, vmprot, flags);
394 }
395
396 /*
397 * Check if we need to extend the size of the segment (does
398 * bss extend page the next page boundary)?
399 */
400 rm = round_page(*addr + msize);
401 rf = round_page(*addr + *size);
402
403 if (rm != rf) {
404 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
405 0, vmprot, flags & VMCMD_RELATIVE);
406 *size = msize;
407 }
408 return 0;
409 }
410
411 /*
412 * elf_load_interp():
413 *
414 * Load an interpreter pointed to by path.
415 */
416 static int
417 elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
418 struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last)
419 {
420 int error, i;
421 struct vnode *vp;
422 struct vattr attr;
423 Elf_Ehdr eh;
424 Elf_Phdr *ph = NULL;
425 const Elf_Phdr *base_ph;
426 const Elf_Phdr *last_ph;
427 u_long phsize;
428 Elf_Addr addr = *last;
429 struct proc *p;
430 bool use_topdown;
431
432 p = l->l_proc;
433
434 KASSERT(p->p_vmspace);
435 KASSERT(p->p_vmspace != proc0.p_vmspace);
436
437 #ifdef __USE_TOPDOWN_VM
438 use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
439 #else
440 use_topdown = false;
441 #endif
442
443 /*
444 * 1. open file
445 * 2. read filehdr
446 * 3. map text, data, and bss out of it using VM_*
447 */
448 vp = epp->ep_interp;
449 if (vp == NULL) {
450 error = emul_find_interp(l, epp, path);
451 if (error != 0)
452 return error;
453 vp = epp->ep_interp;
454 }
455 /* We'll tidy this ourselves - otherwise we have locking issues */
456 epp->ep_interp = NULL;
457 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
458
459 /*
460 * Similarly, if it's not marked as executable, or it's not a regular
461 * file, we don't allow it to be used.
462 */
463 if (vp->v_type != VREG) {
464 error = EACCES;
465 goto bad;
466 }
467 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
468 goto bad;
469
470 /* get attributes */
471 /* XXX VOP_GETATTR() is the only thing that needs LK_EXCLUSIVE ^ */
472 if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
473 goto bad;
474
475 /*
476 * Check mount point. Though we're not trying to exec this binary,
477 * we will be executing code from it, so if the mount point
478 * disallows execution or set-id-ness, we punt or kill the set-id.
479 */
480 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
481 error = EACCES;
482 goto bad;
483 }
484 if (vp->v_mount->mnt_flag & MNT_NOSUID)
485 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
486
487 error = vn_marktext(vp);
488 if (error)
489 goto bad;
490
491 error = exec_read(l, vp, 0, &eh, sizeof(eh), IO_NODELOCKED);
492 if (error != 0)
493 goto bad;
494
495 if ((error = elf_check_header(&eh)) != 0)
496 goto bad;
497 if (eh.e_type != ET_DYN || eh.e_phnum == 0) {
498 DPRINTF("bad interpreter type %#x", eh.e_type);
499 error = ENOEXEC;
500 goto bad;
501 }
502
503 phsize = eh.e_phnum * sizeof(Elf_Phdr);
504 ph = kmem_alloc(phsize, KM_SLEEP);
505
506 error = exec_read(l, vp, eh.e_phoff, ph, phsize, IO_NODELOCKED);
507 if (error != 0)
508 goto bad;
509
510 #ifdef ELF_INTERP_NON_RELOCATABLE
511 /*
512 * Evil hack: Only MIPS should be non-relocatable, and the
513 * psections should have a high address (typically 0x5ffe0000).
514 * If it's now relocatable, it should be linked at 0 and the
515 * psections should have zeros in the upper part of the address.
516 * Otherwise, force the load at the linked address.
517 */
518 if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
519 *last = ELFDEFNNAME(NO_ADDR);
520 #endif
521
522 /*
523 * If no position to load the interpreter was set by a probe
524 * function, pick the same address that a non-fixed mmap(0, ..)
525 * would (i.e. something safely out of the way).
526 */
527 if (*last == ELFDEFNNAME(NO_ADDR)) {
528 u_long limit = 0;
529 /*
530 * Find the start and ending addresses of the psections to
531 * be loaded. This will give us the size.
532 */
533 for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) {
534 if (ph[i].p_type == PT_LOAD) {
535 u_long psize = ph[i].p_vaddr + ph[i].p_memsz;
536 if (base_ph == NULL)
537 base_ph = &ph[i];
538 if (psize > limit)
539 limit = psize;
540 }
541 }
542
543 if (base_ph == NULL) {
544 DPRINTF("no interpreter loadable sections");
545 error = ENOEXEC;
546 goto bad;
547 }
548
549 /*
550 * Now compute the size and load address.
551 */
552 addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
553 epp->ep_daddr,
554 round_page(limit) - trunc_page(base_ph->p_vaddr),
555 use_topdown);
556 addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align,
557 use_topdown);
558 } else {
559 addr = *last; /* may be ELF_LINK_ADDR */
560 }
561
562 /*
563 * Load all the necessary sections
564 */
565 for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) {
566 switch (ph[i].p_type) {
567 case PT_LOAD: {
568 u_long size;
569 int flags;
570
571 if (base_ph == NULL) {
572 /*
573 * First encountered psection is always the
574 * base psection. Make sure it's aligned
575 * properly (align down for topdown and align
576 * upwards for not topdown).
577 */
578 base_ph = &ph[i];
579 flags = VMCMD_BASE;
580 if (addr == ELF_LINK_ADDR)
581 addr = ph[i].p_vaddr;
582 if (use_topdown)
583 addr = ELF_TRUNC(addr, ph[i].p_align);
584 else
585 addr = ELF_ROUND(addr, ph[i].p_align);
586 } else {
587 u_long limit = round_page(last_ph->p_vaddr
588 + last_ph->p_memsz);
589 u_long base = trunc_page(ph[i].p_vaddr);
590
591 /*
592 * If there is a gap in between the psections,
593 * map it as inaccessible so nothing else
594 * mmap'ed will be placed there.
595 */
596 if (limit != base) {
597 NEW_VMCMD2(vcset, vmcmd_map_zero,
598 base - limit,
599 limit - base_ph->p_vaddr, NULLVP,
600 0, VM_PROT_NONE, VMCMD_RELATIVE);
601 }
602
603 addr = ph[i].p_vaddr - base_ph->p_vaddr;
604 flags = VMCMD_RELATIVE;
605 }
606 last_ph = &ph[i];
607 if ((error = elf_load_psection(vcset, vp, &ph[i], &addr,
608 &size, flags)) != 0)
609 goto bad;
610 /*
611 * If entry is within this psection then this
612 * must contain the .text section. *entryoff is
613 * relative to the base psection.
614 */
615 if (eh.e_entry >= ph[i].p_vaddr &&
616 eh.e_entry < (ph[i].p_vaddr + size)) {
617 *entryoff = eh.e_entry - base_ph->p_vaddr;
618 }
619 addr += size;
620 break;
621 }
622
623 default:
624 break;
625 }
626 }
627
628 kmem_free(ph, phsize);
629 /*
630 * This value is ignored if TOPDOWN.
631 */
632 *last = addr;
633 vput(vp);
634 return 0;
635
636 bad:
637 if (ph != NULL)
638 kmem_free(ph, phsize);
639 vput(vp);
640 return error;
641 }
642
643 /*
644 * exec_elf_makecmds(): Prepare an Elf binary's exec package
645 *
646 * First, set of the various offsets/lengths in the exec package.
647 *
648 * Then, mark the text image busy (so it can be demand paged) or error
649 * out if this is not possible. Finally, set up vmcmds for the
650 * text, data, bss, and stack segments.
651 */
652 int
653 exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
654 {
655 Elf_Ehdr *eh = epp->ep_hdr;
656 Elf_Phdr *ph, *pp;
657 Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
658 int error, i;
659 char *interp = NULL;
660 u_long phsize;
661 struct elf_args *ap;
662 bool is_dyn = false;
663
664 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) {
665 DPRINTF("small header %#x", epp->ep_hdrvalid);
666 return ENOEXEC;
667 }
668 if ((error = elf_check_header(eh)) != 0)
669 return error;
670
671 if (eh->e_type == ET_DYN)
672 /* PIE, and some libs have an entry point */
673 is_dyn = true;
674 else if (eh->e_type != ET_EXEC) {
675 DPRINTF("bad type %#x", eh->e_type);
676 return ENOEXEC;
677 }
678
679 if (eh->e_phnum == 0) {
680 DPRINTF("no program headers");
681 return ENOEXEC;
682 }
683
684 /* XXX only LK_EXCLUSIVE to match all others - allow spinning */
685 vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
686 error = vn_marktext(epp->ep_vp);
687 if (error) {
688 VOP_UNLOCK(epp->ep_vp);
689 return error;
690 }
691
692 /*
693 * Allocate space to hold all the program headers, and read them
694 * from the file
695 */
696 phsize = eh->e_phnum * sizeof(Elf_Phdr);
697 ph = kmem_alloc(phsize, KM_SLEEP);
698
699 error = exec_read(l, epp->ep_vp, eh->e_phoff, ph, phsize,
700 IO_NODELOCKED);
701 if (error != 0) {
702 VOP_UNLOCK(epp->ep_vp);
703 goto bad;
704 }
705
706 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
707 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
708
709 for (i = 0; i < eh->e_phnum; i++) {
710 pp = &ph[i];
711 if (pp->p_type == PT_INTERP) {
712 if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) {
713 DPRINTF("bad interpreter namelen %#jx",
714 (uintmax_t)pp->p_filesz);
715 error = ENOEXEC;
716 VOP_UNLOCK(epp->ep_vp);
717 goto bad;
718 }
719 interp = PNBUF_GET();
720 error = exec_read(l, epp->ep_vp, pp->p_offset, interp,
721 pp->p_filesz, IO_NODELOCKED);
722 if (error != 0) {
723 VOP_UNLOCK(epp->ep_vp);
724 goto bad;
725 }
726 /* Ensure interp is NUL-terminated and of the expected length */
727 if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) {
728 DPRINTF("bad interpreter name");
729 error = ENOEXEC;
730 VOP_UNLOCK(epp->ep_vp);
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 VOP_UNLOCK(epp->ep_vp);
753 goto bad;
754 }
755 pos = (Elf_Addr)startp;
756 }
757
758 if (is_dyn && (error = elf_placedynexec(epp, eh, ph)) != 0) {
759 VOP_UNLOCK(epp->ep_vp);
760 goto bad;
761 }
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 if ((error = elf_load_psection(&epp->ep_vmcmds,
773 epp->ep_vp, &ph[i], &addr, &size, VMCMD_FIXED))
774 != 0) {
775 VOP_UNLOCK(epp->ep_vp);
776 goto bad;
777 }
778
779 /*
780 * Consider this as text segment, if it is executable.
781 * If there is more than one text segment, pick the
782 * largest.
783 */
784 if (ph[i].p_flags & PF_X) {
785 if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
786 size > epp->ep_tsize) {
787 epp->ep_taddr = addr;
788 epp->ep_tsize = size;
789 }
790 end_text = addr + size;
791 } else {
792 epp->ep_daddr = addr;
793 epp->ep_dsize = size;
794 }
795 if (ph[i].p_offset == 0) {
796 computed_phdr = ph[i].p_vaddr + eh->e_phoff;
797 }
798 break;
799
800 case PT_SHLIB:
801 /* SCO has these sections. */
802 case PT_INTERP:
803 /* Already did this one. */
804 case PT_DYNAMIC:
805 case PT_NOTE:
806 break;
807 case PT_PHDR:
808 /* Note address of program headers (in text segment) */
809 phdr = ph[i].p_vaddr;
810 break;
811
812 default:
813 /*
814 * Not fatal; we don't need to understand everything.
815 */
816 break;
817 }
818 }
819
820 /* Now done with the vnode. */
821 VOP_UNLOCK(epp->ep_vp);
822
823 if (epp->ep_vmcmds.evs_used == 0) {
824 /* No VMCMD; there was no PT_LOAD section, or those
825 * sections were empty */
826 DPRINTF("no vmcommands");
827 error = ENOEXEC;
828 goto bad;
829 }
830
831 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
832 epp->ep_daddr = round_page(end_text);
833 epp->ep_dsize = 0;
834 }
835
836 /*
837 * Check if we found a dynamically linked binary and arrange to load
838 * its interpreter
839 */
840 if (interp) {
841 u_int nused = epp->ep_vmcmds.evs_used;
842 u_long interp_offset = 0;
843
844 if ((error = elf_load_interp(l, epp, interp,
845 &epp->ep_vmcmds, &interp_offset, &pos)) != 0) {
846 goto bad;
847 }
848 if (epp->ep_vmcmds.evs_used == nused) {
849 /* elf_load_interp() has not set up any new VMCMD */
850 DPRINTF("no vmcommands for interpreter");
851 error = ENOEXEC;
852 goto bad;
853 }
854
855 ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
856 ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr;
857 epp->ep_entryoffset = interp_offset;
858 epp->ep_entry = ap->arg_interp + interp_offset;
859 PNBUF_PUT(interp);
860 interp = NULL;
861 } else {
862 epp->ep_entry = eh->e_entry;
863 if (epp->ep_flags & EXEC_FORCEAUX) {
864 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
865 ap->arg_interp = (vaddr_t)NULL;
866 } else {
867 ap = NULL;
868 }
869 }
870
871 if (ap) {
872 ap->arg_phaddr = phdr ? phdr : computed_phdr;
873 ap->arg_phentsize = eh->e_phentsize;
874 ap->arg_phnum = eh->e_phnum;
875 ap->arg_entry = eh->e_entry;
876 epp->ep_emul_arg = ap;
877 epp->ep_emul_arg_free = elf_free_emul_arg;
878 }
879
880 #ifdef ELF_MAP_PAGE_ZERO
881 /* Dell SVR4 maps page zero, yeuch! */
882 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
883 epp->ep_vp, 0, VM_PROT_READ);
884 #endif
885
886 error = (*epp->ep_esch->es_setup_stack)(l, epp);
887 if (error)
888 goto bad;
889
890 kmem_free(ph, phsize);
891 return 0;
892
893 bad:
894 if (interp)
895 PNBUF_PUT(interp);
896 exec_free_emul_arg(epp);
897 kmem_free(ph, phsize);
898 kill_vmcmds(&epp->ep_vmcmds);
899 return error;
900 }
901
902 int
903 netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
904 Elf_Ehdr *eh)
905 {
906 size_t i;
907 Elf_Phdr *ph;
908 size_t phsize;
909 char *nbuf;
910 int error;
911 int isnetbsd = 0;
912
913 epp->ep_pax_flags = 0;
914
915 if (eh->e_phnum > ELF_MAXPHNUM || eh->e_phnum == 0) {
916 DPRINTF("no signature %#x", eh->e_phnum);
917 return ENOEXEC;
918 }
919
920 phsize = eh->e_phnum * sizeof(Elf_Phdr);
921 ph = kmem_alloc(phsize, KM_SLEEP);
922 error = exec_read(l, epp->ep_vp, eh->e_phoff, ph, phsize,
923 IO_NODELOCKED);
924 if (error)
925 goto out;
926
927 nbuf = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP);
928 for (i = 0; i < eh->e_phnum; i++) {
929 const char *nptr;
930 size_t nlen;
931
932 if (ph[i].p_type != PT_NOTE ||
933 ph[i].p_filesz > ELF_MAXNOTESIZE)
934 continue;
935
936 nlen = ph[i].p_filesz;
937 error = exec_read(l, epp->ep_vp, ph[i].p_offset, nbuf, nlen,
938 IO_NODELOCKED);
939 if (error)
940 continue;
941
942 nptr = nbuf;
943 while (nlen > 0) {
944 const Elf_Nhdr *np;
945 const char *ndata, *ndesc;
946
947 /* note header */
948 np = (const Elf_Nhdr *)nptr;
949 if (nlen < sizeof(*np)) {
950 break;
951 }
952 nptr += sizeof(*np);
953 nlen -= sizeof(*np);
954
955 /* note name */
956 ndata = nptr;
957 if (nlen < roundup(np->n_namesz, 4)) {
958 break;
959 }
960 nptr += roundup(np->n_namesz, 4);
961 nlen -= roundup(np->n_namesz, 4);
962
963 /* note description */
964 ndesc = nptr;
965 if (nlen < roundup(np->n_descsz, 4)) {
966 break;
967 }
968 nptr += roundup(np->n_descsz, 4);
969 nlen -= roundup(np->n_descsz, 4);
970
971 isnetbsd |= netbsd_elf_note(epp, np, ndata, ndesc);
972 }
973 }
974 kmem_free(nbuf, ELF_MAXNOTESIZE);
975
976 error = isnetbsd ? 0 : ENOEXEC;
977 #ifdef DEBUG_ELF
978 if (error)
979 DPRINTF("not netbsd");
980 #endif
981 out:
982 kmem_free(ph, phsize);
983 return error;
984 }
985
986 int
987 netbsd_elf_note(struct exec_package *epp,
988 const Elf_Nhdr *np, const char *ndata, const char *ndesc)
989 {
990 int isnetbsd = 0;
991
992 #ifdef DIAGNOSTIC
993 const char *badnote;
994 #define BADNOTE(n) badnote = (n)
995 #else
996 #define BADNOTE(n)
997 #endif
998
999 switch (np->n_type) {
1000 case ELF_NOTE_TYPE_NETBSD_TAG:
1001 /* It is us */
1002 if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ &&
1003 np->n_descsz == ELF_NOTE_NETBSD_DESCSZ &&
1004 memcmp(ndata, ELF_NOTE_NETBSD_NAME,
1005 ELF_NOTE_NETBSD_NAMESZ) == 0) {
1006 memcpy(&epp->ep_osversion, ndesc,
1007 ELF_NOTE_NETBSD_DESCSZ);
1008 isnetbsd = 1;
1009 break;
1010 }
1011
1012 /*
1013 * Ignore SuSE tags; SuSE's n_type is the same the
1014 * NetBSD one.
1015 */
1016 if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ &&
1017 memcmp(ndata, ELF_NOTE_SUSE_NAME,
1018 ELF_NOTE_SUSE_NAMESZ) == 0)
1019 break;
1020 /*
1021 * Ignore old GCC
1022 */
1023 if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ &&
1024 memcmp(ndata, ELF_NOTE_OGCC_NAME,
1025 ELF_NOTE_OGCC_NAMESZ) == 0)
1026 break;
1027 BADNOTE("NetBSD tag");
1028 goto bad;
1029
1030 case ELF_NOTE_TYPE_PAX_TAG:
1031 if (np->n_namesz == ELF_NOTE_PAX_NAMESZ &&
1032 np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
1033 memcmp(ndata, ELF_NOTE_PAX_NAME,
1034 ELF_NOTE_PAX_NAMESZ) == 0) {
1035 uint32_t flags;
1036 memcpy(&flags, ndesc, sizeof(flags));
1037 /* Convert the flags and insert them into
1038 * the exec package. */
1039 pax_setup_elf_flags(epp, flags);
1040 break;
1041 }
1042 BADNOTE("PaX tag");
1043 goto bad;
1044
1045 case ELF_NOTE_TYPE_MARCH_TAG:
1046 /* Copy the machine arch into the package. */
1047 if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ
1048 && memcmp(ndata, ELF_NOTE_MARCH_NAME,
1049 ELF_NOTE_MARCH_NAMESZ) == 0) {
1050 /* Do not truncate the buffer */
1051 if (np->n_descsz > sizeof(epp->ep_machine_arch)) {
1052 BADNOTE("description size limit");
1053 goto bad;
1054 }
1055 /*
1056 * Ensure ndesc is NUL-terminated and of the
1057 * expected length.
1058 */
1059 if (strnlen(ndesc, np->n_descsz) + 1 !=
1060 np->n_descsz) {
1061 BADNOTE("description size");
1062 goto bad;
1063 }
1064 strlcpy(epp->ep_machine_arch, ndesc,
1065 sizeof(epp->ep_machine_arch));
1066 break;
1067 }
1068 BADNOTE("march tag");
1069 goto bad;
1070
1071 case ELF_NOTE_TYPE_MCMODEL_TAG:
1072 /* arch specific check for code model */
1073 #ifdef ELF_MD_MCMODEL_CHECK
1074 if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ
1075 && memcmp(ndata, ELF_NOTE_MCMODEL_NAME,
1076 ELF_NOTE_MCMODEL_NAMESZ) == 0) {
1077 ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz);
1078 break;
1079 }
1080 BADNOTE("mcmodel tag");
1081 goto bad;
1082 #endif
1083 break;
1084
1085 case ELF_NOTE_TYPE_SUSE_VERSION_TAG:
1086 break;
1087
1088 case ELF_NOTE_TYPE_GO_BUILDID_TAG:
1089 break;
1090
1091 case ELF_NOTE_TYPE_NETBSD_EMUL_TAG:
1092 /* Ancient NetBSD version tag */
1093 break;
1094
1095 default:
1096 BADNOTE("unknown tag");
1097 bad:
1098 #ifdef DIAGNOSTIC
1099 /* Ignore GNU tags */
1100 if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
1101 memcmp(ndata, ELF_NOTE_GNU_NAME,
1102 ELF_NOTE_GNU_NAMESZ) == 0)
1103 break;
1104
1105 int ns = (int)np->n_namesz;
1106 printf("%s: Unknown elf note type %d (%s): "
1107 "[namesz=%d, descsz=%d name=%-*.*s]\n",
1108 epp->ep_kname, np->n_type, badnote, np->n_namesz,
1109 np->n_descsz, ns, ns, ndata);
1110 #endif
1111 break;
1112 }
1113
1114 return isnetbsd;
1115 }
1116
1117 int
1118 netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
1119 vaddr_t *pos)
1120 {
1121 int error;
1122
1123 if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
1124 return error;
1125 #ifdef ELF_MD_PROBE_FUNC
1126 if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
1127 return error;
1128 #elif defined(ELF_INTERP_NON_RELOCATABLE)
1129 *pos = ELF_LINK_ADDR;
1130 #endif
1131 epp->ep_flags |= EXEC_FORCEAUX;
1132 return 0;
1133 }
1134
1135 void
1136 elf_free_emul_arg(void *arg)
1137 {
1138 struct elf_args *ap = arg;
1139 KASSERT(ap != NULL);
1140 kmem_free(ap, sizeof(*ap));
1141 }
1142