exec_elf32.c revision 1.18
1/*	$NetBSD: exec_elf32.c,v 1.18 1996/10/11 19:44:02 cgd Exp $	*/
2
3/*
4 * Copyright (c) 1996 Christopher G. Demetriou
5 * Copyright (c) 1994 Christos Zoulas
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
32#ifndef ELFSIZE
33#define	ELFSIZE		32
34#endif
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/proc.h>
40#include <sys/malloc.h>
41#include <sys/namei.h>
42#include <sys/vnode.h>
43#include <sys/exec.h>
44#include <sys/exec_elf.h>
45#include <sys/fcntl.h>
46#include <sys/syscall.h>
47#include <sys/signalvar.h>
48#include <sys/mount.h>
49#include <sys/stat.h>
50
51#include <sys/mman.h>
52#include <vm/vm.h>
53#include <vm/vm_param.h>
54#include <vm/vm_map.h>
55
56#include <machine/cpu.h>
57#include <machine/reg.h>
58
59#ifdef COMPAT_LINUX
60#include <compat/linux/linux_exec.h>
61#endif
62
63#ifdef COMPAT_SVR4
64#include <compat/svr4/svr4_exec.h>
65#endif
66
67#define	CONCAT(x,y)	__CONCAT(x,y)
68#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
69#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
70#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
71#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
72
73int	ELFNAME(check_header) __P((Elf_Ehdr *, int));
74int	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
75	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
76void	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
77	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
78
79extern char sigcode[], esigcode[];
80#ifdef SYSCALL_DEBUG
81extern char *syscallnames[];
82#endif
83
84struct emul ELFNAMEEND(emul_netbsd) = {
85	"netbsd",
86	NULL,
87	sendsig,
88	SYS_syscall,
89	SYS_MAXSYSCALL,
90	sysent,
91#ifdef SYSCALL_DEBUG
92	syscallnames,
93#else
94	NULL,
95#endif
96	ELF_AUX_ENTRIES * sizeof(AuxInfo),
97	ELFNAME(copyargs),
98	setregs,
99	sigcode,
100	esigcode,
101};
102
103int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
104    Elf_Ehdr *, char *, Elf_Addr *)) = {
105#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
106	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
107#endif
108#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
109	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
110#endif
111};
112
113/* round up and down to page boundaries. */
114#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
115#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
116
117/*
118 * Copy arguments onto the stack in the normal way, but add some
119 * extra information in case of dynamic binding.
120 */
121void *
122ELFNAME(copyargs)(pack, arginfo, stack, argp)
123	struct exec_package *pack;
124	struct ps_strings *arginfo;
125	void *stack;
126	void *argp;
127{
128	size_t len;
129	AuxInfo ai[ELF_AUX_ENTRIES], *a;
130	struct elf_args *ap;
131
132	stack = copyargs(pack, arginfo, stack, argp);
133	if (!stack)
134		return NULL;
135
136	/*
137	 * Push extra arguments on the stack needed by dynamically
138	 * linked binaries
139	 */
140	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
141		a = ai;
142
143		a->au_id = AUX_phdr;
144		a->au_v = ap->arg_phaddr;
145		a++;
146
147		a->au_id = AUX_phent;
148		a->au_v = ap->arg_phentsize;
149		a++;
150
151		a->au_id = AUX_phnum;
152		a->au_v = ap->arg_phnum;
153		a++;
154
155		a->au_id = AUX_pagesz;
156		a->au_v = NBPG;
157		a++;
158
159		a->au_id = AUX_base;
160		a->au_v = ap->arg_interp;
161		a++;
162
163		a->au_id = AUX_flags;
164		a->au_v = 0;
165		a++;
166
167		a->au_id = AUX_entry;
168		a->au_v = ap->arg_entry;
169		a++;
170
171		a->au_id = AUX_null;
172		a->au_v = 0;
173		a++;
174
175		free((char *)ap, M_TEMP);
176		pack->ep_emul_arg = NULL;
177		len = ELF_AUX_ENTRIES * sizeof (AuxInfo);
178		if (copyout(ai, stack, len))
179			return NULL;
180		stack += len;
181	}
182	return stack;
183}
184
185/*
186 * elf_check_header():
187 *
188 * Check header for validity; return 0 of ok ENOEXEC if error
189 */
190int
191ELFNAME(check_header)(eh, type)
192	Elf_Ehdr *eh;
193	int type;
194{
195
196	if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
197		return ENOEXEC;
198
199	switch (eh->e_machine) {
200
201	ELFDEFNNAME(MACHDEP_ID_CASES)
202
203	default:
204		return ENOEXEC;
205	}
206
207	if (eh->e_type != type)
208		return ENOEXEC;
209
210	return 0;
211}
212
213/*
214 * elf_load_psection():
215 *
216 * Load a psection at the appropriate address
217 */
218void
219ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
220	struct exec_vmcmd_set *vcset;
221	struct vnode *vp;
222	Elf_Phdr *ph;
223	Elf_Addr *addr;
224	u_long *size;
225	int *prot;
226{
227	u_long uaddr, msize, psize, rm, rf;
228	long diff, offset;
229
230	/*
231	 * If the user specified an address, then we load there.
232	 */
233	if (*addr != ELFDEFNNAME(NO_ADDR)) {
234		if (ph->p_align > 1) {
235			*addr = ELF_ROUND(*addr, ph->p_align);
236			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
237		} else
238			uaddr = ph->p_vaddr;
239		diff = ph->p_vaddr - uaddr;
240	} else {
241		*addr = uaddr = ph->p_vaddr;
242		if (ph->p_align > 1)
243			*addr = ELF_TRUNC(uaddr, ph->p_align);
244		diff = uaddr - *addr;
245	}
246
247	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
248	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
249	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
250
251	offset = ph->p_offset - diff;
252	*size = ph->p_filesz + diff;
253	msize = ph->p_memsz + diff;
254	psize = round_page(*size);
255
256	if ((ph->p_flags & Elf_pf_w) != 0) {
257		/*
258		 * Because the pagedvn pager can't handle zero fill of the last
259		 * data page if it's not page aligned we map the last page
260		 * readvn.
261		 */
262		psize = trunc_page(*size);
263		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
264		    offset, *prot);
265		if(psize != *size)
266			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
267			    *addr + psize, vp, offset + psize, *prot);
268	} else
269		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
270		    offset, *prot);
271
272	/*
273	 * Check if we need to extend the size of the segment
274	 */
275	rm = round_page(*addr + msize);
276	rf = round_page(*addr + *size);
277
278	if (rm != rf) {
279		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
280		    0, *prot);
281		*size = msize;
282	}
283}
284
285/*
286 * elf_read_from():
287 *
288 *	Read from vnode into buffer at offset.
289 */
290int
291ELFNAME(read_from)(p, vp, off, buf, size)
292	struct vnode *vp;
293	u_long off;
294	struct proc *p;
295	caddr_t buf;
296	int size;
297{
298	int error;
299	int resid;
300
301	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
302	    0, p->p_ucred, &resid, p)) != 0)
303		return error;
304	/*
305	 * See if we got all of it
306	 */
307	if (resid != 0)
308		return ENOEXEC;
309	return 0;
310}
311
312/*
313 * elf_load_file():
314 *
315 * Load a file (interpreter/library) pointed to by path
316 * [stolen from coff_load_shlib()]. Made slightly generic
317 * so it might be used externally.
318 */
319int
320ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
321	struct proc *p;
322	struct exec_package *epp;
323	char *path;
324	struct exec_vmcmd_set *vcset;
325	u_long *entry;
326	struct elf_args	*ap;
327	Elf_Addr *last;
328{
329	int error, i;
330	struct nameidata nd;
331	struct vnode *vp;
332	struct vattr attr;
333	Elf_Ehdr eh;
334	Elf_Phdr *ph = NULL;
335	u_long phsize;
336	char *bp = NULL;
337	Elf_Addr addr = *last;
338
339	bp = path;
340	/*
341	 * 1. open file
342	 * 2. read filehdr
343	 * 3. map text, data, and bss out of it using VM_*
344	 */
345	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
346	if ((error = namei(&nd)) != 0)
347		return error;
348	vp = nd.ni_vp;
349
350	/* check for regular file */
351	if (vp->v_type != VREG) {
352		error = EACCES;
353		goto badunlock;
354	}
355
356	/* get attributes */
357	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
358		goto badunlock;
359
360	/*
361	 * Check mount point.  Though we're not trying to exec this binary,
362	 * we will be executing code from it, so if the mount point
363	 * disallows execution or set-id-ness, we punt or kill the set-id.
364	 */
365	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
366		error = EACCES;
367		goto badunlock;
368	}
369	if (vp->v_mount->mnt_flag & MNT_NOSUID)
370		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
371
372	/*
373	 * Similarly, if it's not marked as executable, we don't allow
374	 * it to be used.  For root we have to see if any exec bit on.
375	 */
376	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
377		goto badunlock;
378	if ((attr.va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
379		error = EACCES;
380		goto badunlock;
381	}
382
383#ifdef notyet /* XXX cgd 960926 */
384	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
385#endif
386	VOP_UNLOCK(vp);
387
388	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
389	    sizeof(eh))) != 0)
390		goto bad;
391
392	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
393		goto bad;
394
395	phsize = eh.e_phnum * sizeof(Elf_Phdr);
396	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
397
398	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
399	    (caddr_t) ph, phsize)) != 0)
400		goto bad;
401
402	/*
403	 * Load all the necessary sections
404	 */
405	for (i = 0; i < eh.e_phnum; i++) {
406		u_long size = 0;
407		int prot = 0;
408
409		switch (ph[i].p_type) {
410		case Elf_pt_load:
411			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
412			    &size, &prot);
413			/* If entry is within this section it must be text */
414			if (eh.e_entry >= ph[i].p_vaddr &&
415			    eh.e_entry < (ph[i].p_vaddr + size)) {
416				*entry = addr + eh.e_entry;
417				ap->arg_interp = addr;
418			}
419			addr += size;
420			break;
421
422		case Elf_pt_dynamic:
423		case Elf_pt_phdr:
424		case Elf_pt_note:
425			break;
426
427		default:
428			break;
429		}
430	}
431
432	free((char *)ph, M_TEMP);
433	*last = addr;
434	vrele(vp);
435	return 0;
436
437badunlock:
438	VOP_UNLOCK(vp);
439
440bad:
441	if (ph != NULL)
442		free((char *)ph, M_TEMP);
443#ifdef notyet /* XXX cgd 960926 */
444	(maybe) VOP_CLOSE it
445#endif
446	vrele(vp);
447	return error;
448}
449
450/*
451 * exec_elf_makecmds(): Prepare an Elf binary's exec package
452 *
453 * First, set of the various offsets/lengths in the exec package.
454 *
455 * Then, mark the text image busy (so it can be demand paged) or error
456 * out if this is not possible.  Finally, set up vmcmds for the
457 * text, data, bss, and stack segments.
458 */
459int
460ELFNAME2(exec,makecmds)(p, epp)
461	struct proc *p;
462	struct exec_package *epp;
463{
464	Elf_Ehdr *eh = epp->ep_hdr;
465	Elf_Phdr *ph, *pp;
466	Elf_Addr phdr = 0, pos = 0;
467	int error, i, n, nload;
468	char interp[MAXPATHLEN];
469	u_long phsize;
470
471	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
472		return ENOEXEC;
473
474	if (ELFNAME(check_header)(eh, Elf_et_exec))
475		return ENOEXEC;
476
477	/*
478	 * check if vnode is in open for writing, because we want to
479	 * demand-page out of it.  if it is, don't do it, for various
480	 * reasons
481	 */
482	if (epp->ep_vp->v_writecount != 0) {
483#ifdef DIAGNOSTIC
484		if (epp->ep_vp->v_flag & VTEXT)
485			panic("exec: a VTEXT vnode has writecount != 0\n");
486#endif
487		return ETXTBSY;
488	}
489	/*
490	 * Allocate space to hold all the program headers, and read them
491	 * from the file
492	 */
493	phsize = eh->e_phnum * sizeof(Elf_Phdr);
494	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
495
496	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
497	    (caddr_t) ph, phsize)) != 0)
498		goto bad;
499
500	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
501	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
502
503	interp[0] = '\0';
504
505	for (i = 0; i < eh->e_phnum; i++) {
506		pp = &ph[i];
507		if (pp->p_type == Elf_pt_interp) {
508			if (pp->p_filesz >= sizeof(interp))
509				goto bad;
510			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
511			    pp->p_offset, (caddr_t) interp,
512			    pp->p_filesz)) != 0)
513				goto bad;
514			break;
515		}
516	}
517
518	/*
519	 * Setup things for native emulation.
520	 */
521	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
522	pos = ELFDEFNNAME(NO_ADDR);
523
524	/*
525	 * On the same architecture, we may be emulating different systems.
526	 * See which one will accept this executable. This currently only
527	 * applies to Linux and SVR4 on the i386.
528	 *
529	 * Probe functions would normally see if the interpreter (if any)
530	 * exists. Emulation packages may possibly replace the interpreter in
531	 * interp[] with a changed path (/emul/xxx/<path>), and also
532	 * set the ep_emul field in the exec package structure.
533	 */
534	n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0];
535	if (n != 0) {
536		error = ENOEXEC;
537		for (i = 0; i < n && error; i++)
538			error = ELFNAME(probe_funcs)[i](p, epp, eh,
539			    interp, &pos);
540
541#ifdef notyet
542		/*
543		 * We should really use a signature in our native binaries
544		 * and have our own probe function for matching binaries,
545		 * before trying the emulations. For now, if the emulation
546		 * probes failed we default to native.
547		 */
548		if (error)
549			goto bad;
550#endif
551	}
552
553	/*
554	 * Load all the necessary sections
555	 */
556	for (i = nload = 0; i < eh->e_phnum; i++) {
557		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
558		u_long size = 0;
559		int prot = 0;
560
561		pp = &ph[i];
562
563		switch (ph[i].p_type) {
564		case Elf_pt_load:
565			/*
566			 * XXX
567			 * Can handle only 2 sections: text and data
568			 */
569			if (nload++ == 2)
570				goto bad;
571			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
572			    &ph[i], &addr, &size, &prot);
573
574			/*
575			 * Decide whether it's text or data by looking
576			 * at the entry point.
577			 */
578			if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
579				epp->ep_taddr = addr;
580				epp->ep_tsize = size;
581			} else {
582				epp->ep_daddr = addr;
583				epp->ep_dsize = size;
584			}
585			break;
586
587		case Elf_pt_shlib:
588			error = ENOEXEC;
589			goto bad;
590
591		case Elf_pt_interp:
592			/* Already did this one */
593		case Elf_pt_dynamic:
594		case Elf_pt_note:
595			break;
596
597		case Elf_pt_phdr:
598			/* Note address of program headers (in text segment) */
599			phdr = pp->p_vaddr;
600			break;
601
602		default:
603			/*
604			 * Not fatal; we don't need to understand everything.
605			 */
606			break;
607		}
608	}
609
610	/*
611	 * If no position to load the interpreter was set by a probe
612	 * function, pick the same address that a non-fixed mmap(0, ..)
613	 * would (i.e. something safely out of the way).
614	 */
615	if (pos == ELFDEFNNAME(NO_ADDR) &&
616	    epp->ep_emul == &ELFNAMEEND(emul_netbsd))
617		pos = round_page(epp->ep_daddr + MAXDSIZ);
618
619	/*
620	 * Check if we found a dynamically linked binary and arrange to load
621	 * it's interpreter
622	 */
623	if (interp[0]) {
624		struct elf_args *ap;
625
626		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
627		    M_TEMP, M_WAITOK);
628		if ((error = ELFNAME(load_file)(p, epp, interp,
629		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
630			free((char *)ap, M_TEMP);
631			goto bad;
632		}
633		pos += phsize;
634		ap->arg_phaddr = phdr;
635
636		ap->arg_phentsize = eh->e_phentsize;
637		ap->arg_phnum = eh->e_phnum;
638		ap->arg_entry = eh->e_entry;
639
640		epp->ep_emul_arg = ap;
641	} else
642		epp->ep_entry = eh->e_entry;
643
644#ifdef ELF_MAP_PAGE_ZERO
645	/* Dell SVR4 maps page zero, yeuch! */
646	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
647	    VM_PROT_READ);
648#endif
649	free((char *)ph, M_TEMP);
650	epp->ep_vp->v_flag |= VTEXT;
651	return exec_elf_setup_stack(p, epp);
652
653bad:
654	free((char *)ph, M_TEMP);
655	kill_vmcmds(&epp->ep_vmcmds);
656	return ENOEXEC;
657}
658