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