exec_elf.c revision 1.83 1 1.83 christos /* $NetBSD: exec_elf.c,v 1.83 2016/05/08 01:28:09 christos Exp $ */
2 1.10 ad
3 1.10 ad /*-
4 1.78 maxv * Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc.
5 1.10 ad * All rights reserved.
6 1.10 ad *
7 1.10 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.78 maxv * by Christos Zoulas and Maxime Villard.
9 1.10 ad *
10 1.10 ad * Redistribution and use in source and binary forms, with or without
11 1.10 ad * modification, are permitted provided that the following conditions
12 1.10 ad * are met:
13 1.10 ad * 1. Redistributions of source code must retain the above copyright
14 1.10 ad * notice, this list of conditions and the following disclaimer.
15 1.10 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.10 ad * notice, this list of conditions and the following disclaimer in the
17 1.10 ad * documentation and/or other materials provided with the distribution.
18 1.10 ad *
19 1.10 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.10 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.10 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.10 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.10 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.10 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.10 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.10 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.10 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.10 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.10 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.10 ad */
31 1.1 fvdl
32 1.1 fvdl /*
33 1.10 ad * Copyright (c) 1996 Christopher G. Demetriou
34 1.1 fvdl * All rights reserved.
35 1.1 fvdl *
36 1.1 fvdl * Redistribution and use in source and binary forms, with or without
37 1.1 fvdl * modification, are permitted provided that the following conditions
38 1.1 fvdl * are met:
39 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
40 1.1 fvdl * notice, this list of conditions and the following disclaimer.
41 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
43 1.1 fvdl * documentation and/or other materials provided with the distribution.
44 1.1 fvdl * 3. The name of the author may not be used to endorse or promote products
45 1.1 fvdl * derived from this software without specific prior written permission
46 1.1 fvdl *
47 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 fvdl */
58 1.1 fvdl
59 1.10 ad #include <sys/cdefs.h>
60 1.83 christos __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.83 2016/05/08 01:28:09 christos Exp $");
61 1.10 ad
62 1.10 ad #ifdef _KERNEL_OPT
63 1.10 ad #include "opt_pax.h"
64 1.10 ad #endif /* _KERNEL_OPT */
65 1.10 ad
66 1.1 fvdl #include <sys/param.h>
67 1.1 fvdl #include <sys/proc.h>
68 1.10 ad #include <sys/kmem.h>
69 1.1 fvdl #include <sys/namei.h>
70 1.1 fvdl #include <sys/vnode.h>
71 1.1 fvdl #include <sys/exec.h>
72 1.1 fvdl #include <sys/exec_elf.h>
73 1.8 christos #include <sys/syscall.h>
74 1.8 christos #include <sys/signalvar.h>
75 1.10 ad #include <sys/mount.h>
76 1.10 ad #include <sys/stat.h>
77 1.10 ad #include <sys/kauth.h>
78 1.10 ad #include <sys/bitops.h>
79 1.10 ad
80 1.10 ad #include <sys/cpu.h>
81 1.10 ad #include <machine/reg.h>
82 1.1 fvdl
83 1.10 ad #include <compat/common/compat_util.h>
84 1.1 fvdl
85 1.10 ad #include <sys/pax.h>
86 1.37 martin #include <uvm/uvm_param.h>
87 1.1 fvdl
88 1.10 ad extern struct emul emul_netbsd;
89 1.1 fvdl
90 1.10 ad #define elf_check_header ELFNAME(check_header)
91 1.10 ad #define elf_copyargs ELFNAME(copyargs)
92 1.65 maxv #define elf_load_interp ELFNAME(load_interp)
93 1.10 ad #define elf_load_psection ELFNAME(load_psection)
94 1.10 ad #define exec_elf_makecmds ELFNAME2(exec,makecmds)
95 1.10 ad #define netbsd_elf_signature ELFNAME2(netbsd,signature)
96 1.10 ad #define netbsd_elf_probe ELFNAME2(netbsd,probe)
97 1.10 ad #define coredump ELFNAMEEND(coredump)
98 1.35 matt #define elf_free_emul_arg ELFNAME(free_emul_arg)
99 1.1 fvdl
100 1.58 maxv static int
101 1.65 maxv elf_load_interp(struct lwp *, struct exec_package *, char *,
102 1.58 maxv struct exec_vmcmd_set *, u_long *, Elf_Addr *);
103 1.58 maxv static void
104 1.58 maxv elf_load_psection(struct exec_vmcmd_set *, struct vnode *, const Elf_Phdr *,
105 1.64 maxv Elf_Addr *, u_long *, int);
106 1.6 christos
107 1.10 ad int netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
108 1.10 ad int netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
109 1.10 ad vaddr_t *);
110 1.1 fvdl
111 1.35 matt static void elf_free_emul_arg(void *);
112 1.35 matt
113 1.10 ad /* round up and down to page boundaries. */
114 1.10 ad #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
115 1.10 ad #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
116 1.1 fvdl
117 1.10 ad static void
118 1.77 maxv elf_placedynexec(struct exec_package *epp, Elf_Ehdr *eh, Elf_Phdr *ph)
119 1.10 ad {
120 1.19 drochner Elf_Addr align, offset;
121 1.19 drochner int i;
122 1.19 drochner
123 1.19 drochner for (align = i = 0; i < eh->e_phnum; i++)
124 1.19 drochner if (ph[i].p_type == PT_LOAD && ph[i].p_align > align)
125 1.19 drochner align = ph[i].p_align;
126 1.10 ad
127 1.83 christos #ifndef PAX_ASLR
128 1.83 christos # define pax_aslr_exec_offset(epp, align) MAX(align, PAGE_SIZE)
129 1.83 christos #endif
130 1.83 christos offset = (Elf_Addr)pax_aslr_exec_offset(epp, align);
131 1.32 reinoud offset += epp->ep_vm_minaddr;
132 1.32 reinoud
133 1.10 ad for (i = 0; i < eh->e_phnum; i++)
134 1.18 christos ph[i].p_vaddr += offset;
135 1.63 matt epp->ep_entryoffset = offset;
136 1.16 christos eh->e_entry += offset;
137 1.10 ad }
138 1.8 christos
139 1.8 christos /*
140 1.1 fvdl * Copy arguments onto the stack in the normal way, but add some
141 1.1 fvdl * extra information in case of dynamic binding.
142 1.1 fvdl */
143 1.10 ad int
144 1.10 ad elf_copyargs(struct lwp *l, struct exec_package *pack,
145 1.10 ad struct ps_strings *arginfo, char **stackp, void *argp)
146 1.1 fvdl {
147 1.10 ad size_t len, vlen;
148 1.10 ad AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname;
149 1.1 fvdl struct elf_args *ap;
150 1.10 ad int error;
151 1.1 fvdl
152 1.10 ad if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
153 1.10 ad return error;
154 1.10 ad
155 1.10 ad a = ai;
156 1.10 ad execname = NULL;
157 1.1 fvdl
158 1.71 maxv memset(ai, 0, sizeof(ai));
159 1.71 maxv
160 1.1 fvdl /*
161 1.1 fvdl * Push extra arguments on the stack needed by dynamically
162 1.1 fvdl * linked binaries
163 1.1 fvdl */
164 1.10 ad if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
165 1.10 ad struct vattr *vap = pack->ep_vap;
166 1.10 ad
167 1.10 ad a->a_type = AT_PHDR;
168 1.10 ad a->a_v = ap->arg_phaddr;
169 1.10 ad a++;
170 1.10 ad
171 1.10 ad a->a_type = AT_PHENT;
172 1.10 ad a->a_v = ap->arg_phentsize;
173 1.10 ad a++;
174 1.10 ad
175 1.10 ad a->a_type = AT_PHNUM;
176 1.10 ad a->a_v = ap->arg_phnum;
177 1.10 ad a++;
178 1.1 fvdl
179 1.10 ad a->a_type = AT_PAGESZ;
180 1.10 ad a->a_v = PAGE_SIZE;
181 1.1 fvdl a++;
182 1.1 fvdl
183 1.10 ad a->a_type = AT_BASE;
184 1.10 ad a->a_v = ap->arg_interp;
185 1.1 fvdl a++;
186 1.1 fvdl
187 1.10 ad a->a_type = AT_FLAGS;
188 1.10 ad a->a_v = 0;
189 1.1 fvdl a++;
190 1.1 fvdl
191 1.10 ad a->a_type = AT_ENTRY;
192 1.10 ad a->a_v = ap->arg_entry;
193 1.1 fvdl a++;
194 1.1 fvdl
195 1.10 ad a->a_type = AT_EUID;
196 1.10 ad if (vap->va_mode & S_ISUID)
197 1.10 ad a->a_v = vap->va_uid;
198 1.10 ad else
199 1.10 ad a->a_v = kauth_cred_geteuid(l->l_cred);
200 1.1 fvdl a++;
201 1.1 fvdl
202 1.10 ad a->a_type = AT_RUID;
203 1.10 ad a->a_v = kauth_cred_getuid(l->l_cred);
204 1.1 fvdl a++;
205 1.1 fvdl
206 1.10 ad a->a_type = AT_EGID;
207 1.10 ad if (vap->va_mode & S_ISGID)
208 1.10 ad a->a_v = vap->va_gid;
209 1.10 ad else
210 1.10 ad a->a_v = kauth_cred_getegid(l->l_cred);
211 1.1 fvdl a++;
212 1.1 fvdl
213 1.10 ad a->a_type = AT_RGID;
214 1.10 ad a->a_v = kauth_cred_getgid(l->l_cred);
215 1.1 fvdl a++;
216 1.1 fvdl
217 1.36 joerg a->a_type = AT_STACKBASE;
218 1.36 joerg a->a_v = l->l_proc->p_stackbase;
219 1.36 joerg a++;
220 1.36 joerg
221 1.10 ad if (pack->ep_path) {
222 1.10 ad execname = a;
223 1.10 ad a->a_type = AT_SUN_EXECNAME;
224 1.10 ad a++;
225 1.10 ad }
226 1.10 ad
227 1.35 matt exec_free_emul_arg(pack);
228 1.1 fvdl }
229 1.10 ad
230 1.10 ad a->a_type = AT_NULL;
231 1.10 ad a->a_v = 0;
232 1.10 ad a++;
233 1.10 ad
234 1.36 joerg vlen = (a - ai) * sizeof(ai[0]);
235 1.36 joerg
236 1.36 joerg KASSERT(vlen <= sizeof(ai));
237 1.10 ad
238 1.10 ad if (execname) {
239 1.10 ad char *path = pack->ep_path;
240 1.10 ad execname->a_v = (uintptr_t)(*stackp + vlen);
241 1.10 ad len = strlen(path) + 1;
242 1.10 ad if ((error = copyout(path, (*stackp + vlen), len)) != 0)
243 1.10 ad return error;
244 1.10 ad len = ALIGN(len);
245 1.10 ad } else
246 1.10 ad len = 0;
247 1.10 ad
248 1.10 ad if ((error = copyout(ai, *stackp, vlen)) != 0)
249 1.10 ad return error;
250 1.10 ad *stackp += vlen + len;
251 1.10 ad
252 1.10 ad return 0;
253 1.1 fvdl }
254 1.1 fvdl
255 1.1 fvdl /*
256 1.1 fvdl * elf_check_header():
257 1.1 fvdl *
258 1.46 martin * Check header for validity; return 0 if ok, ENOEXEC if error
259 1.1 fvdl */
260 1.1 fvdl int
261 1.56 maxv elf_check_header(Elf_Ehdr *eh)
262 1.1 fvdl {
263 1.3 thorpej
264 1.10 ad if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
265 1.10 ad eh->e_ident[EI_CLASS] != ELFCLASS)
266 1.1 fvdl return ENOEXEC;
267 1.1 fvdl
268 1.1 fvdl switch (eh->e_machine) {
269 1.10 ad
270 1.10 ad ELFDEFNNAME(MACHDEP_ID_CASES)
271 1.1 fvdl
272 1.1 fvdl default:
273 1.1 fvdl return ENOEXEC;
274 1.1 fvdl }
275 1.1 fvdl
276 1.10 ad if (ELF_EHDR_FLAGS_OK(eh) == 0)
277 1.10 ad return ENOEXEC;
278 1.10 ad
279 1.69 maxv if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM)
280 1.10 ad return ENOEXEC;
281 1.10 ad
282 1.1 fvdl return 0;
283 1.1 fvdl }
284 1.1 fvdl
285 1.1 fvdl /*
286 1.1 fvdl * elf_load_psection():
287 1.10 ad *
288 1.1 fvdl * Load a psection at the appropriate address
289 1.1 fvdl */
290 1.58 maxv static void
291 1.10 ad elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
292 1.64 maxv const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags)
293 1.1 fvdl {
294 1.10 ad u_long msize, psize, rm, rf;
295 1.1 fvdl long diff, offset;
296 1.64 maxv int vmprot = 0;
297 1.1 fvdl
298 1.1 fvdl /*
299 1.10 ad * If the user specified an address, then we load there.
300 1.10 ad */
301 1.10 ad if (*addr == ELFDEFNNAME(NO_ADDR))
302 1.10 ad *addr = ph->p_vaddr;
303 1.10 ad
304 1.10 ad if (ph->p_align > 1) {
305 1.10 ad /*
306 1.10 ad * Make sure we are virtually aligned as we are supposed to be.
307 1.10 ad */
308 1.10 ad diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
309 1.10 ad KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align));
310 1.10 ad /*
311 1.10 ad * But make sure to not map any pages before the start of the
312 1.10 ad * psection by limiting the difference to within a page.
313 1.10 ad */
314 1.10 ad diff &= PAGE_MASK;
315 1.10 ad } else
316 1.10 ad diff = 0;
317 1.1 fvdl
318 1.64 maxv vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
319 1.64 maxv vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
320 1.64 maxv vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
321 1.1 fvdl
322 1.10 ad /*
323 1.10 ad * Adjust everything so it all starts on a page boundary.
324 1.10 ad */
325 1.10 ad *addr -= diff;
326 1.1 fvdl offset = ph->p_offset - diff;
327 1.1 fvdl *size = ph->p_filesz + diff;
328 1.1 fvdl msize = ph->p_memsz + diff;
329 1.1 fvdl
330 1.10 ad if (ph->p_align >= PAGE_SIZE) {
331 1.10 ad if ((ph->p_flags & PF_W) != 0) {
332 1.10 ad /*
333 1.10 ad * Because the pagedvn pager can't handle zero fill
334 1.10 ad * of the last data page if it's not page aligned we
335 1.10 ad * map the last page readvn.
336 1.10 ad */
337 1.10 ad psize = trunc_page(*size);
338 1.10 ad } else {
339 1.10 ad psize = round_page(*size);
340 1.10 ad }
341 1.10 ad } else {
342 1.10 ad psize = *size;
343 1.10 ad }
344 1.10 ad
345 1.10 ad if (psize > 0) {
346 1.10 ad NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
347 1.10 ad vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
348 1.64 maxv offset, vmprot, flags);
349 1.10 ad flags &= VMCMD_RELATIVE;
350 1.10 ad }
351 1.10 ad if (psize < *size) {
352 1.10 ad NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
353 1.64 maxv *addr + psize, vp, offset + psize, vmprot, flags);
354 1.10 ad }
355 1.1 fvdl
356 1.1 fvdl /*
357 1.10 ad * Check if we need to extend the size of the segment (does
358 1.10 ad * bss extend page the next page boundary)?
359 1.10 ad */
360 1.1 fvdl rm = round_page(*addr + msize);
361 1.1 fvdl rf = round_page(*addr + *size);
362 1.1 fvdl
363 1.1 fvdl if (rm != rf) {
364 1.10 ad NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
365 1.64 maxv 0, vmprot, flags & VMCMD_RELATIVE);
366 1.1 fvdl *size = msize;
367 1.1 fvdl }
368 1.1 fvdl }
369 1.1 fvdl
370 1.1 fvdl /*
371 1.65 maxv * elf_load_interp():
372 1.1 fvdl *
373 1.65 maxv * Load an interpreter pointed to by path.
374 1.1 fvdl */
375 1.58 maxv static int
376 1.65 maxv elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
377 1.58 maxv struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last)
378 1.1 fvdl {
379 1.1 fvdl int error, i;
380 1.10 ad struct vnode *vp;
381 1.10 ad struct vattr attr;
382 1.10 ad Elf_Ehdr eh;
383 1.10 ad Elf_Phdr *ph = NULL;
384 1.10 ad const Elf_Phdr *base_ph;
385 1.10 ad const Elf_Phdr *last_ph;
386 1.1 fvdl u_long phsize;
387 1.10 ad Elf_Addr addr = *last;
388 1.10 ad struct proc *p;
389 1.37 martin bool use_topdown;
390 1.10 ad
391 1.10 ad p = l->l_proc;
392 1.10 ad
393 1.38 martin KASSERT(p->p_vmspace);
394 1.81 martin KASSERT(p->p_vmspace != proc0.p_vmspace);
395 1.81 martin
396 1.54 christos #ifdef __USE_TOPDOWN_VM
397 1.81 martin use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
398 1.37 martin #else
399 1.81 martin use_topdown = false;
400 1.37 martin #endif
401 1.37 martin
402 1.10 ad /*
403 1.10 ad * 1. open file
404 1.10 ad * 2. read filehdr
405 1.10 ad * 3. map text, data, and bss out of it using VM_*
406 1.10 ad */
407 1.10 ad vp = epp->ep_interp;
408 1.10 ad if (vp == NULL) {
409 1.10 ad error = emul_find_interp(l, epp, path);
410 1.10 ad if (error != 0)
411 1.10 ad return error;
412 1.10 ad vp = epp->ep_interp;
413 1.10 ad }
414 1.10 ad /* We'll tidy this ourselves - otherwise we have locking issues */
415 1.10 ad epp->ep_interp = NULL;
416 1.10 ad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
417 1.1 fvdl
418 1.1 fvdl /*
419 1.10 ad * Similarly, if it's not marked as executable, or it's not a regular
420 1.10 ad * file, we don't allow it to be used.
421 1.10 ad */
422 1.10 ad if (vp->v_type != VREG) {
423 1.10 ad error = EACCES;
424 1.10 ad goto badunlock;
425 1.10 ad }
426 1.10 ad if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
427 1.10 ad goto badunlock;
428 1.10 ad
429 1.10 ad /* get attributes */
430 1.10 ad if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
431 1.10 ad goto badunlock;
432 1.10 ad
433 1.10 ad /*
434 1.10 ad * Check mount point. Though we're not trying to exec this binary,
435 1.10 ad * we will be executing code from it, so if the mount point
436 1.10 ad * disallows execution or set-id-ness, we punt or kill the set-id.
437 1.10 ad */
438 1.10 ad if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
439 1.10 ad error = EACCES;
440 1.10 ad goto badunlock;
441 1.1 fvdl }
442 1.10 ad if (vp->v_mount->mnt_flag & MNT_NOSUID)
443 1.10 ad epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
444 1.10 ad
445 1.10 ad error = vn_marktext(vp);
446 1.10 ad if (error)
447 1.10 ad goto badunlock;
448 1.10 ad
449 1.23 hannken VOP_UNLOCK(vp);
450 1.10 ad
451 1.10 ad if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
452 1.10 ad goto bad;
453 1.10 ad
454 1.56 maxv if ((error = elf_check_header(&eh)) != 0)
455 1.1 fvdl goto bad;
456 1.56 maxv if (eh.e_type != ET_DYN || eh.e_phnum == 0) {
457 1.10 ad error = ENOEXEC;
458 1.1 fvdl goto bad;
459 1.10 ad }
460 1.1 fvdl
461 1.10 ad phsize = eh.e_phnum * sizeof(Elf_Phdr);
462 1.10 ad ph = kmem_alloc(phsize, KM_SLEEP);
463 1.1 fvdl
464 1.10 ad if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
465 1.1 fvdl goto bad;
466 1.1 fvdl
467 1.10 ad #ifdef ELF_INTERP_NON_RELOCATABLE
468 1.10 ad /*
469 1.10 ad * Evil hack: Only MIPS should be non-relocatable, and the
470 1.10 ad * psections should have a high address (typically 0x5ffe0000).
471 1.10 ad * If it's now relocatable, it should be linked at 0 and the
472 1.10 ad * psections should have zeros in the upper part of the address.
473 1.10 ad * Otherwise, force the load at the linked address.
474 1.10 ad */
475 1.10 ad if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
476 1.10 ad *last = ELFDEFNNAME(NO_ADDR);
477 1.10 ad #endif
478 1.10 ad
479 1.10 ad /*
480 1.10 ad * If no position to load the interpreter was set by a probe
481 1.10 ad * function, pick the same address that a non-fixed mmap(0, ..)
482 1.10 ad * would (i.e. something safely out of the way).
483 1.10 ad */
484 1.10 ad if (*last == ELFDEFNNAME(NO_ADDR)) {
485 1.10 ad u_long limit = 0;
486 1.10 ad /*
487 1.10 ad * Find the start and ending addresses of the psections to
488 1.10 ad * be loaded. This will give us the size.
489 1.10 ad */
490 1.58 maxv for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) {
491 1.58 maxv if (ph[i].p_type == PT_LOAD) {
492 1.58 maxv u_long psize = ph[i].p_vaddr + ph[i].p_memsz;
493 1.10 ad if (base_ph == NULL)
494 1.58 maxv base_ph = &ph[i];
495 1.10 ad if (psize > limit)
496 1.10 ad limit = psize;
497 1.10 ad }
498 1.10 ad }
499 1.10 ad
500 1.10 ad if (base_ph == NULL) {
501 1.10 ad error = ENOEXEC;
502 1.10 ad goto bad;
503 1.10 ad }
504 1.10 ad
505 1.10 ad /*
506 1.10 ad * Now compute the size and load address.
507 1.10 ad */
508 1.10 ad addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
509 1.10 ad epp->ep_daddr,
510 1.81 martin round_page(limit) - trunc_page(base_ph->p_vaddr),
511 1.81 martin use_topdown);
512 1.81 martin } else {
513 1.10 ad addr = *last; /* may be ELF_LINK_ADDR */
514 1.81 martin }
515 1.10 ad
516 1.1 fvdl /*
517 1.10 ad * Load all the necessary sections
518 1.10 ad */
519 1.58 maxv for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) {
520 1.58 maxv switch (ph[i].p_type) {
521 1.10 ad case PT_LOAD: {
522 1.10 ad u_long size;
523 1.10 ad int flags;
524 1.10 ad
525 1.10 ad if (base_ph == NULL) {
526 1.10 ad /*
527 1.10 ad * First encountered psection is always the
528 1.10 ad * base psection. Make sure it's aligned
529 1.10 ad * properly (align down for topdown and align
530 1.10 ad * upwards for not topdown).
531 1.10 ad */
532 1.58 maxv base_ph = &ph[i];
533 1.10 ad flags = VMCMD_BASE;
534 1.10 ad if (addr == ELF_LINK_ADDR)
535 1.58 maxv addr = ph[i].p_vaddr;
536 1.37 martin if (use_topdown)
537 1.58 maxv addr = ELF_TRUNC(addr, ph[i].p_align);
538 1.10 ad else
539 1.58 maxv addr = ELF_ROUND(addr, ph[i].p_align);
540 1.10 ad } else {
541 1.10 ad u_long limit = round_page(last_ph->p_vaddr
542 1.10 ad + last_ph->p_memsz);
543 1.58 maxv u_long base = trunc_page(ph[i].p_vaddr);
544 1.10 ad
545 1.10 ad /*
546 1.10 ad * If there is a gap in between the psections,
547 1.10 ad * map it as inaccessible so nothing else
548 1.10 ad * mmap'ed will be placed there.
549 1.10 ad */
550 1.10 ad if (limit != base) {
551 1.10 ad NEW_VMCMD2(vcset, vmcmd_map_zero,
552 1.10 ad base - limit,
553 1.10 ad limit - base_ph->p_vaddr, NULLVP,
554 1.10 ad 0, VM_PROT_NONE, VMCMD_RELATIVE);
555 1.10 ad }
556 1.1 fvdl
557 1.58 maxv addr = ph[i].p_vaddr - base_ph->p_vaddr;
558 1.10 ad flags = VMCMD_RELATIVE;
559 1.10 ad }
560 1.58 maxv last_ph = &ph[i];
561 1.10 ad elf_load_psection(vcset, vp, &ph[i], &addr,
562 1.64 maxv &size, flags);
563 1.10 ad /*
564 1.10 ad * If entry is within this psection then this
565 1.10 ad * must contain the .text section. *entryoff is
566 1.10 ad * relative to the base psection.
567 1.10 ad */
568 1.58 maxv if (eh.e_entry >= ph[i].p_vaddr &&
569 1.58 maxv eh.e_entry < (ph[i].p_vaddr + size)) {
570 1.10 ad *entryoff = eh.e_entry - base_ph->p_vaddr;
571 1.1 fvdl }
572 1.1 fvdl addr += size;
573 1.1 fvdl break;
574 1.10 ad }
575 1.1 fvdl
576 1.1 fvdl default:
577 1.1 fvdl break;
578 1.1 fvdl }
579 1.1 fvdl }
580 1.1 fvdl
581 1.10 ad kmem_free(ph, phsize);
582 1.10 ad /*
583 1.10 ad * This value is ignored if TOPDOWN.
584 1.10 ad */
585 1.10 ad *last = addr;
586 1.10 ad vrele(vp);
587 1.10 ad return 0;
588 1.10 ad
589 1.10 ad badunlock:
590 1.23 hannken VOP_UNLOCK(vp);
591 1.10 ad
592 1.1 fvdl bad:
593 1.1 fvdl if (ph != NULL)
594 1.10 ad kmem_free(ph, phsize);
595 1.10 ad vrele(vp);
596 1.1 fvdl return error;
597 1.1 fvdl }
598 1.1 fvdl
599 1.1 fvdl /*
600 1.1 fvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
601 1.1 fvdl *
602 1.1 fvdl * First, set of the various offsets/lengths in the exec package.
603 1.1 fvdl *
604 1.1 fvdl * Then, mark the text image busy (so it can be demand paged) or error
605 1.1 fvdl * out if this is not possible. Finally, set up vmcmds for the
606 1.1 fvdl * text, data, bss, and stack segments.
607 1.1 fvdl */
608 1.1 fvdl int
609 1.10 ad exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
610 1.10 ad {
611 1.10 ad Elf_Ehdr *eh = epp->ep_hdr;
612 1.10 ad Elf_Phdr *ph, *pp;
613 1.26 chs Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
614 1.58 maxv int error, i;
615 1.10 ad char *interp = NULL;
616 1.10 ad u_long phsize;
617 1.65 maxv struct elf_args *ap;
618 1.56 maxv bool is_dyn = false;
619 1.1 fvdl
620 1.10 ad if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
621 1.1 fvdl return ENOEXEC;
622 1.56 maxv if ((error = elf_check_header(eh)) != 0)
623 1.56 maxv return error;
624 1.1 fvdl
625 1.56 maxv if (eh->e_type == ET_DYN)
626 1.76 maxv /* PIE, and some libs have an entry point */
627 1.56 maxv is_dyn = true;
628 1.56 maxv else if (eh->e_type != ET_EXEC)
629 1.1 fvdl return ENOEXEC;
630 1.1 fvdl
631 1.46 martin if (eh->e_phnum == 0)
632 1.10 ad return ENOEXEC;
633 1.10 ad
634 1.10 ad error = vn_marktext(epp->ep_vp);
635 1.10 ad if (error)
636 1.10 ad return error;
637 1.10 ad
638 1.1 fvdl /*
639 1.10 ad * Allocate space to hold all the program headers, and read them
640 1.10 ad * from the file
641 1.10 ad */
642 1.10 ad phsize = eh->e_phnum * sizeof(Elf_Phdr);
643 1.10 ad ph = kmem_alloc(phsize, KM_SLEEP);
644 1.1 fvdl
645 1.10 ad if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
646 1.10 ad 0)
647 1.1 fvdl goto bad;
648 1.1 fvdl
649 1.10 ad epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
650 1.10 ad epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
651 1.1 fvdl
652 1.1 fvdl for (i = 0; i < eh->e_phnum; i++) {
653 1.1 fvdl pp = &ph[i];
654 1.10 ad if (pp->p_type == PT_INTERP) {
655 1.52 christos if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) {
656 1.10 ad error = ENOEXEC;
657 1.1 fvdl goto bad;
658 1.10 ad }
659 1.10 ad interp = PNBUF_GET();
660 1.53 skrll if ((error = exec_read_from(l, epp->ep_vp,
661 1.53 skrll pp->p_offset, interp, pp->p_filesz)) != 0)
662 1.53 skrll goto bad;
663 1.52 christos /* Ensure interp is NUL-terminated and of the expected length */
664 1.52 christos if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) {
665 1.52 christos error = ENOEXEC;
666 1.52 christos goto bad;
667 1.52 christos }
668 1.1 fvdl break;
669 1.1 fvdl }
670 1.1 fvdl }
671 1.1 fvdl
672 1.1 fvdl /*
673 1.1 fvdl * On the same architecture, we may be emulating different systems.
674 1.10 ad * See which one will accept this executable.
675 1.1 fvdl *
676 1.1 fvdl * Probe functions would normally see if the interpreter (if any)
677 1.1 fvdl * exists. Emulation packages may possibly replace the interpreter in
678 1.74 maxv * interp with a changed path (/emul/xxx/<path>).
679 1.1 fvdl */
680 1.10 ad pos = ELFDEFNNAME(NO_ADDR);
681 1.10 ad if (epp->ep_esch->u.elf_probe_func) {
682 1.10 ad vaddr_t startp = (vaddr_t)pos;
683 1.1 fvdl
684 1.10 ad error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
685 1.10 ad &startp);
686 1.1 fvdl if (error)
687 1.1 fvdl goto bad;
688 1.10 ad pos = (Elf_Addr)startp;
689 1.1 fvdl }
690 1.1 fvdl
691 1.13 drochner if (is_dyn)
692 1.77 maxv elf_placedynexec(epp, eh, ph);
693 1.10 ad
694 1.1 fvdl /*
695 1.10 ad * Load all the necessary sections
696 1.10 ad */
697 1.58 maxv for (i = 0; i < eh->e_phnum; i++) {
698 1.58 maxv Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
699 1.10 ad u_long size = 0;
700 1.1 fvdl
701 1.1 fvdl switch (ph[i].p_type) {
702 1.10 ad case PT_LOAD:
703 1.1 fvdl elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
704 1.64 maxv &ph[i], &addr, &size, VMCMD_FIXED);
705 1.10 ad
706 1.4 fvdl /*
707 1.24 joerg * Consider this as text segment, if it is executable.
708 1.24 joerg * If there is more than one text segment, pick the
709 1.24 joerg * largest.
710 1.4 fvdl */
711 1.24 joerg if (ph[i].p_flags & PF_X) {
712 1.24 joerg if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
713 1.24 joerg size > epp->ep_tsize) {
714 1.24 joerg epp->ep_taddr = addr;
715 1.24 joerg epp->ep_tsize = size;
716 1.10 ad }
717 1.25 joerg end_text = addr + size;
718 1.4 fvdl } else {
719 1.4 fvdl epp->ep_daddr = addr;
720 1.4 fvdl epp->ep_dsize = size;
721 1.4 fvdl }
722 1.26 chs if (ph[i].p_offset == 0) {
723 1.26 chs computed_phdr = ph[i].p_vaddr + eh->e_phoff;
724 1.26 chs }
725 1.1 fvdl break;
726 1.1 fvdl
727 1.10 ad case PT_SHLIB:
728 1.10 ad /* SCO has these sections. */
729 1.10 ad case PT_INTERP:
730 1.10 ad /* Already did this one. */
731 1.10 ad case PT_DYNAMIC:
732 1.10 ad case PT_NOTE:
733 1.1 fvdl break;
734 1.10 ad case PT_PHDR:
735 1.4 fvdl /* Note address of program headers (in text segment) */
736 1.58 maxv phdr = ph[i].p_vaddr;
737 1.7 christos break;
738 1.4 fvdl
739 1.1 fvdl default:
740 1.1 fvdl /*
741 1.10 ad * Not fatal; we don't need to understand everything.
742 1.1 fvdl */
743 1.1 fvdl break;
744 1.1 fvdl }
745 1.1 fvdl }
746 1.59 maxv
747 1.68 maxv if (epp->ep_vmcmds.evs_used == 0) {
748 1.59 maxv /* No VMCMD; there was no PT_LOAD section, or those
749 1.59 maxv * sections were empty */
750 1.59 maxv error = ENOEXEC;
751 1.59 maxv goto bad;
752 1.59 maxv }
753 1.59 maxv
754 1.24 joerg if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
755 1.27 chs epp->ep_daddr = round_page(end_text);
756 1.25 joerg epp->ep_dsize = 0;
757 1.24 joerg }
758 1.24 joerg
759 1.5 fvdl /*
760 1.10 ad * Check if we found a dynamically linked binary and arrange to load
761 1.10 ad * its interpreter
762 1.5 fvdl */
763 1.10 ad if (interp) {
764 1.65 maxv u_int nused = epp->ep_vmcmds.evs_used;
765 1.57 christos u_long interp_offset = 0;
766 1.1 fvdl
767 1.65 maxv if ((error = elf_load_interp(l, epp, interp,
768 1.58 maxv &epp->ep_vmcmds, &interp_offset, &pos)) != 0) {
769 1.1 fvdl goto bad;
770 1.1 fvdl }
771 1.59 maxv if (epp->ep_vmcmds.evs_used == nused) {
772 1.65 maxv /* elf_load_interp() has not set up any new VMCMD */
773 1.59 maxv error = ENOEXEC;
774 1.59 maxv goto bad;
775 1.59 maxv }
776 1.59 maxv
777 1.65 maxv ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
778 1.59 maxv ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr;
779 1.63 matt epp->ep_entryoffset = interp_offset;
780 1.10 ad epp->ep_entry = ap->arg_interp + interp_offset;
781 1.26 chs PNBUF_PUT(interp);
782 1.78 maxv interp = NULL;
783 1.65 maxv } else {
784 1.26 chs epp->ep_entry = eh->e_entry;
785 1.65 maxv if (epp->ep_flags & EXEC_FORCEAUX) {
786 1.65 maxv ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
787 1.65 maxv ap->arg_interp = (vaddr_t)NULL;
788 1.65 maxv } else
789 1.65 maxv ap = NULL;
790 1.65 maxv }
791 1.1 fvdl
792 1.26 chs if (ap) {
793 1.26 chs ap->arg_phaddr = phdr ? phdr : computed_phdr;
794 1.1 fvdl ap->arg_phentsize = eh->e_phentsize;
795 1.1 fvdl ap->arg_phnum = eh->e_phnum;
796 1.1 fvdl ap->arg_entry = eh->e_entry;
797 1.1 fvdl epp->ep_emul_arg = ap;
798 1.35 matt epp->ep_emul_arg_free = elf_free_emul_arg;
799 1.26 chs }
800 1.1 fvdl
801 1.8 christos #ifdef ELF_MAP_PAGE_ZERO
802 1.8 christos /* Dell SVR4 maps page zero, yeuch! */
803 1.10 ad NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
804 1.10 ad epp->ep_vp, 0, VM_PROT_READ);
805 1.8 christos #endif
806 1.78 maxv
807 1.78 maxv error = (*epp->ep_esch->es_setup_stack)(l, epp);
808 1.78 maxv if (error)
809 1.78 maxv goto bad;
810 1.78 maxv
811 1.10 ad kmem_free(ph, phsize);
812 1.78 maxv return 0;
813 1.1 fvdl
814 1.1 fvdl bad:
815 1.10 ad if (interp)
816 1.10 ad PNBUF_PUT(interp);
817 1.35 matt exec_free_emul_arg(epp);
818 1.10 ad kmem_free(ph, phsize);
819 1.1 fvdl kill_vmcmds(&epp->ep_vmcmds);
820 1.10 ad return error;
821 1.10 ad }
822 1.10 ad
823 1.10 ad int
824 1.10 ad netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
825 1.10 ad Elf_Ehdr *eh)
826 1.10 ad {
827 1.10 ad size_t i;
828 1.15 christos Elf_Shdr *sh;
829 1.15 christos Elf_Nhdr *np;
830 1.60 maxv size_t shsize, nsize;
831 1.10 ad int error;
832 1.10 ad int isnetbsd = 0;
833 1.62 maxv char *ndata, *ndesc;
834 1.67 christos
835 1.66 christos #ifdef DIAGNOSTIC
836 1.66 christos const char *badnote;
837 1.66 christos #define BADNOTE(n) badnote = (n)
838 1.66 christos #else
839 1.66 christos #define BADNOTE(n)
840 1.66 christos #endif
841 1.10 ad
842 1.10 ad epp->ep_pax_flags = 0;
843 1.69 maxv if (eh->e_shnum > ELF_MAXSHNUM || eh->e_shnum == 0)
844 1.10 ad return ENOEXEC;
845 1.10 ad
846 1.15 christos shsize = eh->e_shnum * sizeof(Elf_Shdr);
847 1.15 christos sh = kmem_alloc(shsize, KM_SLEEP);
848 1.15 christos error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
849 1.10 ad if (error)
850 1.10 ad goto out;
851 1.10 ad
852 1.69 maxv np = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP);
853 1.15 christos for (i = 0; i < eh->e_shnum; i++) {
854 1.15 christos Elf_Shdr *shp = &sh[i];
855 1.15 christos
856 1.15 christos if (shp->sh_type != SHT_NOTE ||
857 1.69 maxv shp->sh_size > ELF_MAXNOTESIZE ||
858 1.15 christos shp->sh_size < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
859 1.10 ad continue;
860 1.10 ad
861 1.15 christos error = exec_read_from(l, epp->ep_vp, shp->sh_offset, np,
862 1.15 christos shp->sh_size);
863 1.10 ad if (error)
864 1.15 christos continue;
865 1.10 ad
866 1.60 maxv /* Point to the note, skip the header */
867 1.10 ad ndata = (char *)(np + 1);
868 1.60 maxv
869 1.60 maxv /*
870 1.60 maxv * Padding is present if necessary to ensure 4-byte alignment.
871 1.60 maxv * The actual section size is therefore:
872 1.60 maxv * header size + 4-byte aligned name + 4-byte aligned desc
873 1.60 maxv * Ensure this size is consistent with what is indicated
874 1.60 maxv * in sh_size. The first check avoids integer overflows.
875 1.70 chs *
876 1.70 chs * Binaries from before NetBSD 1.6 have two notes in the same
877 1.70 chs * note section. The second note was never used, so as long as
878 1.70 chs * the section is at least as big as it should be, it's ok.
879 1.70 chs * These binaries also have a second note section with a note of
880 1.70 chs * type ELF_NOTE_TYPE_NETBSD_TAG, which can be ignored as well.
881 1.60 maxv */
882 1.66 christos if (np->n_namesz > shp->sh_size || np->n_descsz > shp->sh_size) {
883 1.66 christos BADNOTE("note size limit");
884 1.39 christos goto bad;
885 1.66 christos }
886 1.60 maxv nsize = sizeof(*np) + roundup(np->n_namesz, 4) +
887 1.60 maxv roundup(np->n_descsz, 4);
888 1.70 chs if (nsize > shp->sh_size) {
889 1.66 christos BADNOTE("note size");
890 1.60 maxv goto bad;
891 1.66 christos }
892 1.62 maxv ndesc = ndata + roundup(np->n_namesz, 4);
893 1.60 maxv
894 1.10 ad switch (np->n_type) {
895 1.10 ad case ELF_NOTE_TYPE_NETBSD_TAG:
896 1.61 maxv /* It is us */
897 1.39 christos if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ &&
898 1.39 christos np->n_descsz == ELF_NOTE_NETBSD_DESCSZ &&
899 1.10 ad memcmp(ndata, ELF_NOTE_NETBSD_NAME,
900 1.39 christos ELF_NOTE_NETBSD_NAMESZ) == 0) {
901 1.62 maxv memcpy(&epp->ep_osversion, ndesc,
902 1.48 joerg ELF_NOTE_NETBSD_DESCSZ);
903 1.39 christos isnetbsd = 1;
904 1.39 christos break;
905 1.39 christos }
906 1.61 maxv
907 1.39 christos /*
908 1.80 christos * Ignore SuSE tags; SuSE's n_type is the same the
909 1.80 christos * NetBSD one.
910 1.39 christos */
911 1.39 christos if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ &&
912 1.39 christos memcmp(ndata, ELF_NOTE_SUSE_NAME,
913 1.39 christos ELF_NOTE_SUSE_NAMESZ) == 0)
914 1.39 christos break;
915 1.80 christos /*
916 1.80 christos * Ignore old GCC
917 1.80 christos */
918 1.80 christos if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ &&
919 1.80 christos memcmp(ndata, ELF_NOTE_OGCC_NAME,
920 1.80 christos ELF_NOTE_OGCC_NAMESZ) == 0)
921 1.80 christos break;
922 1.66 christos BADNOTE("NetBSD tag");
923 1.39 christos goto bad;
924 1.10 ad
925 1.10 ad case ELF_NOTE_TYPE_PAX_TAG:
926 1.61 maxv if (np->n_namesz == ELF_NOTE_PAX_NAMESZ &&
927 1.61 maxv np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
928 1.10 ad memcmp(ndata, ELF_NOTE_PAX_NAME,
929 1.61 maxv ELF_NOTE_PAX_NAMESZ) == 0) {
930 1.77 maxv uint32_t flags;
931 1.77 maxv memcpy(&flags, ndesc, sizeof(flags));
932 1.77 maxv #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
933 1.77 maxv /* Convert the flags and insert them into
934 1.77 maxv * the exec package. */
935 1.77 maxv pax_setup_elf_flags(epp, flags);
936 1.77 maxv #else
937 1.77 maxv (void)flags; /* UNUSED */
938 1.77 maxv #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
939 1.61 maxv break;
940 1.39 christos }
941 1.66 christos BADNOTE("PaX tag");
942 1.61 maxv goto bad;
943 1.10 ad
944 1.47 matt case ELF_NOTE_TYPE_MARCH_TAG:
945 1.61 maxv /* Copy the machine arch into the package. */
946 1.47 matt if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ
947 1.47 matt && memcmp(ndata, ELF_NOTE_MARCH_NAME,
948 1.47 matt ELF_NOTE_MARCH_NAMESZ) == 0) {
949 1.62 maxv /* Do not truncate the buffer */
950 1.66 christos if (np->n_descsz > sizeof(epp->ep_machine_arch)) {
951 1.66 christos BADNOTE("description size limit");
952 1.62 maxv goto bad;
953 1.66 christos }
954 1.62 maxv /*
955 1.62 maxv * Ensure ndesc is NUL-terminated and of the
956 1.62 maxv * expected length.
957 1.62 maxv */
958 1.62 maxv if (strnlen(ndesc, np->n_descsz) + 1 !=
959 1.66 christos np->n_descsz) {
960 1.66 christos BADNOTE("description size");
961 1.62 maxv goto bad;
962 1.66 christos }
963 1.62 maxv strlcpy(epp->ep_machine_arch, ndesc,
964 1.47 matt sizeof(epp->ep_machine_arch));
965 1.47 matt break;
966 1.47 matt }
967 1.66 christos BADNOTE("march tag");
968 1.61 maxv goto bad;
969 1.61 maxv
970 1.49 martin case ELF_NOTE_TYPE_MCMODEL_TAG:
971 1.61 maxv /* arch specific check for code model */
972 1.49 martin #ifdef ELF_MD_MCMODEL_CHECK
973 1.49 martin if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ
974 1.49 martin && memcmp(ndata, ELF_NOTE_MCMODEL_NAME,
975 1.49 martin ELF_NOTE_MCMODEL_NAMESZ) == 0) {
976 1.62 maxv ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz);
977 1.49 martin break;
978 1.49 martin }
979 1.66 christos BADNOTE("mcmodel tag");
980 1.61 maxv goto bad;
981 1.49 martin #endif
982 1.49 martin break;
983 1.47 matt
984 1.39 christos case ELF_NOTE_TYPE_SUSE_VERSION_TAG:
985 1.31 christos break;
986 1.31 christos
987 1.79 christos case ELF_NOTE_TYPE_GO_BUILDID_TAG:
988 1.79 christos break;
989 1.79 christos
990 1.10 ad default:
991 1.66 christos BADNOTE("unknown tag");
992 1.61 maxv bad:
993 1.15 christos #ifdef DIAGNOSTIC
994 1.61 maxv /* Ignore GNU tags */
995 1.61 maxv if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
996 1.61 maxv memcmp(ndata, ELF_NOTE_GNU_NAME,
997 1.61 maxv ELF_NOTE_GNU_NAMESZ) == 0)
998 1.61 maxv break;
999 1.61 maxv
1000 1.61 maxv int ns = MIN(np->n_namesz, shp->sh_size - sizeof(*np));
1001 1.66 christos printf("%s: Unknown elf note type %d (%s): "
1002 1.66 christos "[namesz=%d, descsz=%d name=%-*.*s]\n",
1003 1.66 christos epp->ep_kname, np->n_type, badnote, np->n_namesz,
1004 1.61 maxv np->n_descsz, ns, ns, ndata);
1005 1.15 christos #endif
1006 1.10 ad break;
1007 1.10 ad }
1008 1.10 ad }
1009 1.69 maxv kmem_free(np, ELF_MAXNOTESIZE);
1010 1.10 ad
1011 1.10 ad error = isnetbsd ? 0 : ENOEXEC;
1012 1.10 ad out:
1013 1.15 christos kmem_free(sh, shsize);
1014 1.10 ad return error;
1015 1.10 ad }
1016 1.10 ad
1017 1.10 ad int
1018 1.10 ad netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
1019 1.10 ad vaddr_t *pos)
1020 1.10 ad {
1021 1.10 ad int error;
1022 1.10 ad
1023 1.10 ad if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
1024 1.10 ad return error;
1025 1.12 matt #ifdef ELF_MD_PROBE_FUNC
1026 1.12 matt if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
1027 1.12 matt return error;
1028 1.12 matt #elif defined(ELF_INTERP_NON_RELOCATABLE)
1029 1.10 ad *pos = ELF_LINK_ADDR;
1030 1.10 ad #endif
1031 1.29 joerg epp->ep_flags |= EXEC_FORCEAUX;
1032 1.10 ad return 0;
1033 1.1 fvdl }
1034 1.35 matt
1035 1.35 matt void
1036 1.35 matt elf_free_emul_arg(void *arg)
1037 1.35 matt {
1038 1.35 matt struct elf_args *ap = arg;
1039 1.35 matt KASSERT(ap != NULL);
1040 1.35 matt kmem_free(ap, sizeof(*ap));
1041 1.35 matt }
1042