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