exec_elf32.c revision 1.14
1/*	$NetBSD: exec_elf32.c,v 1.14 1996/10/07 18:24:48 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#include <machine/exec.h>
59
60#ifdef COMPAT_LINUX
61#include <compat/linux/linux_exec.h>
62#endif
63
64#ifdef COMPAT_SVR4
65#include <compat/svr4/svr4_exec.h>
66#endif
67
68#define	CONCAT(x,y)	__CONCAT(x,y)
69#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
70#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
71#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
72#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
73
74static int ELFNAME(check_header) __P((Elf_Ehdr *, int));
75static int ELFNAME(load_file) __P((struct proc *, struct exec_package *,
76	    char *, struct exec_vmcmd_set *, u_long *, struct elf_args *,
77	    Elf_Addr *));
78static void ELFNAME(load_psection) __P((struct exec_vmcmd_set *,
79	struct vnode *, Elf_Phdr *, Elf_Addr *, u_long *, int *));
80
81extern char sigcode[], esigcode[];
82#ifdef SYSCALL_DEBUG
83extern char *syscallnames[];
84#endif
85
86struct emul ELFNAMEEND(emul_netbsd) = {
87	"netbsd",
88	NULL,
89	sendsig,
90	SYS_syscall,
91	SYS_MAXSYSCALL,
92	sysent,
93#ifdef SYSCALL_DEBUG
94	syscallnames,
95#else
96	NULL,
97#endif
98	ELF_AUX_ENTRIES * sizeof(AuxInfo),
99	ELFNAME(copyargs),
100	setregs,
101	sigcode,
102	esigcode,
103};
104
105int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
106    Elf_Ehdr *, char *, Elf_Addr *)) = {
107#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
108	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
109#endif
110#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
111	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
112#endif
113};
114
115#define ELF_ALIGN(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 */
190static int
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 */
218static void
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_ALIGN(*addr + ph->p_align, ph->p_align);
236			uaddr = ELF_ALIGN(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_ALIGN(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, 0, *prot);
280		*size = msize;
281	}
282}
283
284/*
285 * elf_read_from():
286 *
287 *	Read from vnode into buffer at offset.
288 */
289int
290ELFNAME(read_from)(p, vp, off, buf, size)
291	struct vnode *vp;
292	u_long off;
293	struct proc *p;
294	caddr_t buf;
295	int size;
296{
297	int error;
298	int resid;
299
300	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
301			     off, UIO_SYSSPACE, 0, p->p_ucred,
302			     &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 */
319static int
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 (or
363	 * tracing) disallows execution or set-id-ness, we punt or kill
364	 * the set-id.
365	 */
366	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
367		error = EACCES;
368		goto badunlock;
369	}
370	if (vp->v_mount->mnt_flag & MNT_NOSUID)
371		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
372
373	/*
374	 * Similarly, if it's not marked as executable, we don't allow
375	 * it to be used.  For root we have to see if any exec bit on.
376	 */
377	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
378		goto badunlock;
379	if ((attr.va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
380		error = EACCES;
381		goto badunlock;
382	}
383
384#ifdef notyet /* XXX cgd 960926 */
385	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
386#endif
387	VOP_UNLOCK(vp);
388
389	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
390				    sizeof(eh))) != 0)
391		goto bad;
392
393	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
394		goto bad;
395
396	phsize = eh.e_phnum * sizeof(Elf_Phdr);
397	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
398
399	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
400	    (caddr_t) ph, phsize)) != 0)
401		goto bad;
402
403	/*
404         * Load all the necessary sections
405         */
406	for (i = 0; i < eh.e_phnum; i++) {
407		u_long size = 0;
408		int prot = 0;
409
410		switch (ph[i].p_type) {
411		case Elf_pt_load:
412			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
413						&size, &prot);
414			/* If entry is within this section it must be text */
415			if (eh.e_entry >= ph[i].p_vaddr &&
416			    eh.e_entry < (ph[i].p_vaddr + size)) {
417				*entry = addr + eh.e_entry;
418				ap->arg_interp = addr;
419			}
420			addr += size;
421			break;
422
423		case Elf_pt_dynamic:
424		case Elf_pt_phdr:
425		case Elf_pt_note:
426			break;
427
428		default:
429			break;
430		}
431	}
432
433	free((char *) ph, M_TEMP);
434	*last = addr;
435	vrele(vp);
436	return 0;
437
438badunlock:
439	VOP_UNLOCK(vp);
440
441bad:
442	if (ph != NULL)
443		free((char *) ph, M_TEMP);
444#ifdef notyet /* XXX cgd 960926 */
445	(maybe) VOP_CLOSE it
446#endif
447	vrele(vp);
448	return error;
449}
450
451/*
452 * exec_elf_makecmds(): Prepare an Elf binary's exec package
453 *
454 * First, set of the various offsets/lengths in the exec package.
455 *
456 * Then, mark the text image busy (so it can be demand paged) or error
457 * out if this is not possible.  Finally, set up vmcmds for the
458 * text, data, bss, and stack segments.
459 */
460int
461ELFNAME2(exec,makecmds)(p, epp)
462	struct proc *p;
463	struct exec_package *epp;
464{
465	Elf_Ehdr *eh = epp->ep_hdr;
466	Elf_Phdr *ph, *pp;
467	Elf_Addr phdr = 0, pos = 0;
468	int error, i, n, nload;
469	char interp[MAXPATHLEN];
470	u_long phsize;
471
472	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
473		return ENOEXEC;
474
475	if (ELFNAME(check_header)(eh, Elf_et_exec))
476		return ENOEXEC;
477
478	/*
479         * check if vnode is in open for writing, because we want to
480         * demand-page out of it.  if it is, don't do it, for various
481         * reasons
482         */
483	if (epp->ep_vp->v_writecount != 0) {
484#ifdef DIAGNOSTIC
485		if (epp->ep_vp->v_flag & VTEXT)
486			panic("exec: a VTEXT vnode has writecount != 0\n");
487#endif
488		return ETXTBSY;
489	}
490	/*
491         * Allocate space to hold all the program headers, and read them
492         * from the file
493         */
494	phsize = eh->e_phnum * sizeof(Elf_Phdr);
495	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
496
497	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
498	    (caddr_t) ph, phsize)) != 0)
499		goto bad;
500
501	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
502	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
503
504	interp[0] = '\0';
505
506	for (i = 0; i < eh->e_phnum; i++) {
507		pp = &ph[i];
508		if (pp->p_type == Elf_pt_interp) {
509			if (pp->p_filesz >= sizeof(interp))
510				goto bad;
511			if ((error = ELFNAME(read_from)(p, epp->ep_vp, pp->p_offset,
512				      (caddr_t) interp, 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	if ((n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0])) {
535		error = ENOEXEC;
536		for (i = 0; i < n && error; i++)
537			error = ELFNAME(probe_funcs)[i](p, epp, eh, interp, &pos);
538
539#ifdef notyet
540		/*
541		 * We should really use a signature in our native binaries
542		 * and have our own probe function for matching binaries,
543		 * before trying the emulations. For now, if the emulation
544		 * probes failed we default to native.
545		 */
546		if (error)
547			goto bad;
548#endif
549	}
550
551	/*
552         * Load all the necessary sections
553         */
554	for (i = nload = 0; i < eh->e_phnum; i++) {
555		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
556		u_long size = 0;
557		int prot = 0;
558
559		pp = &ph[i];
560
561		switch (ph[i].p_type) {
562		case Elf_pt_load:
563			/*
564			 * XXX
565			 * Can handle only 2 sections: text and data
566			 */
567			if (nload++ == 2)
568				goto bad;
569			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
570				&ph[i], &addr, &size, &prot);
571			/*
572			 * Decide whether it's text or data by looking
573			 * at the entry point.
574			 */
575			if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
576				epp->ep_taddr = addr;
577				epp->ep_tsize = size;
578			} else {
579				epp->ep_daddr = addr;
580				epp->ep_dsize = size;
581			}
582			break;
583
584		case Elf_pt_shlib:
585			error = ENOEXEC;
586			goto bad;
587
588		case Elf_pt_interp:
589			/* Already did this one */
590		case Elf_pt_dynamic:
591		case Elf_pt_note:
592			break;
593
594		case Elf_pt_phdr:
595			/* Note address of program headers (in text segment) */
596			phdr = pp->p_vaddr;
597			break;
598
599		default:
600			/*
601			 * Not fatal; we don't need to understand everything.
602			 */
603			break;
604		}
605	}
606
607	/*
608	 * If no position to load the interpreter was set by a probe
609	 * function, pick the same address that a non-fixed mmap(0, ..)
610	 * would (i.e. something safely out of the way).
611	 */
612	if (pos == ELFDEFNNAME(NO_ADDR) &&
613	    epp->ep_emul == &ELFNAMEEND(emul_netbsd))
614		pos = round_page(epp->ep_daddr + MAXDSIZ);
615
616	/*
617         * Check if we found a dynamically linked binary and arrange to load
618         * it's interpreter
619         */
620	if (interp[0]) {
621		struct elf_args *ap;
622
623		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
624						 M_TEMP, M_WAITOK);
625		if ((error = ELFNAME(load_file)(p, epp, interp,
626		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
627			free((char *) ap, M_TEMP);
628			goto bad;
629		}
630		pos += phsize;
631		ap->arg_phaddr = phdr;
632
633		ap->arg_phentsize = eh->e_phentsize;
634		ap->arg_phnum = eh->e_phnum;
635		ap->arg_entry = eh->e_entry;
636
637		epp->ep_emul_arg = ap;
638	} else
639		epp->ep_entry = eh->e_entry;
640
641#ifdef ELF_MAP_PAGE_ZERO
642	/* Dell SVR4 maps page zero, yeuch! */
643	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
644	    VM_PROT_READ);
645#endif
646	free((char *) ph, M_TEMP);
647	epp->ep_vp->v_flag |= VTEXT;
648	return exec_elf_setup_stack(p, epp);
649
650bad:
651	free((char *) ph, M_TEMP);
652	kill_vmcmds(&epp->ep_vmcmds);
653	return ENOEXEC;
654}
655