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