Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.271
      1  1.271        ad /*	$NetBSD: kern_exec.c,v 1.271 2008/04/24 15:35:29 ad Exp $	*/
      2   1.55       cgd 
      3   1.55       cgd /*-
      4   1.77       cgd  * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
      5   1.55       cgd  * Copyright (C) 1992 Wolfgang Solfrank.
      6   1.55       cgd  * Copyright (C) 1992 TooLs GmbH.
      7   1.55       cgd  * All rights reserved.
      8   1.55       cgd  *
      9   1.55       cgd  * Redistribution and use in source and binary forms, with or without
     10   1.55       cgd  * modification, are permitted provided that the following conditions
     11   1.55       cgd  * are met:
     12   1.55       cgd  * 1. Redistributions of source code must retain the above copyright
     13   1.55       cgd  *    notice, this list of conditions and the following disclaimer.
     14   1.55       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.55       cgd  *    notice, this list of conditions and the following disclaimer in the
     16   1.55       cgd  *    documentation and/or other materials provided with the distribution.
     17   1.55       cgd  * 3. All advertising materials mentioning features or use of this software
     18   1.55       cgd  *    must display the following acknowledgement:
     19   1.55       cgd  *	This product includes software developed by TooLs GmbH.
     20   1.55       cgd  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21   1.55       cgd  *    derived from this software without specific prior written permission.
     22   1.55       cgd  *
     23   1.55       cgd  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24   1.55       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.55       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.55       cgd  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27   1.55       cgd  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28   1.55       cgd  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29   1.55       cgd  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30   1.55       cgd  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31   1.55       cgd  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32   1.55       cgd  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.55       cgd  */
     34  1.146     lukem 
     35  1.146     lukem #include <sys/cdefs.h>
     36  1.271        ad __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.271 2008/04/24 15:35:29 ad Exp $");
     37   1.89       mrg 
     38   1.92   thorpej #include "opt_ktrace.h"
     39  1.124  jdolecek #include "opt_syscall_debug.h"
     40  1.174  christos #include "opt_compat_netbsd.h"
     41  1.226    dogcow #include "veriexec.h"
     42  1.232      elad #include "opt_pax.h"
     43   1.55       cgd 
     44   1.55       cgd #include <sys/param.h>
     45   1.55       cgd #include <sys/systm.h>
     46   1.55       cgd #include <sys/filedesc.h>
     47   1.55       cgd #include <sys/kernel.h>
     48   1.55       cgd #include <sys/proc.h>
     49   1.55       cgd #include <sys/mount.h>
     50   1.55       cgd #include <sys/malloc.h>
     51  1.265      yamt #include <sys/kmem.h>
     52   1.55       cgd #include <sys/namei.h>
     53   1.55       cgd #include <sys/vnode.h>
     54   1.55       cgd #include <sys/file.h>
     55   1.55       cgd #include <sys/acct.h>
     56   1.55       cgd #include <sys/exec.h>
     57   1.55       cgd #include <sys/ktrace.h>
     58   1.55       cgd #include <sys/resourcevar.h>
     59   1.55       cgd #include <sys/wait.h>
     60   1.55       cgd #include <sys/mman.h>
     61  1.155  gmcgarry #include <sys/ras.h>
     62   1.55       cgd #include <sys/signalvar.h>
     63   1.55       cgd #include <sys/stat.h>
     64  1.124  jdolecek #include <sys/syscall.h>
     65  1.218      elad #include <sys/kauth.h>
     66  1.253        ad #include <sys/lwpctl.h>
     67  1.260  christos #include <sys/pax.h>
     68  1.263        ad #include <sys/cpu.h>
     69   1.55       cgd 
     70   1.56       cgd #include <sys/syscallargs.h>
     71  1.222      elad #if NVERIEXEC > 0
     72  1.197     blymn #include <sys/verified_exec.h>
     73  1.222      elad #endif /* NVERIEXEC > 0 */
     74   1.55       cgd 
     75   1.88       mrg #include <uvm/uvm_extern.h>
     76   1.88       mrg 
     77   1.55       cgd #include <machine/reg.h>
     78   1.55       cgd 
     79  1.244       dsl #include <compat/common/compat_util.h>
     80  1.244       dsl 
     81  1.171       chs static int exec_sigcode_map(struct proc *, const struct emul *);
     82  1.171       chs 
     83  1.143  christos #ifdef DEBUG_EXEC
     84  1.143  christos #define DPRINTF(a) uprintf a
     85  1.143  christos #else
     86  1.143  christos #define DPRINTF(a)
     87  1.143  christos #endif /* DEBUG_EXEC */
     88  1.165   thorpej 
     89  1.130  jdolecek /*
     90  1.130  jdolecek  * Exec function switch:
     91  1.130  jdolecek  *
     92  1.130  jdolecek  * Note that each makecmds function is responsible for loading the
     93  1.130  jdolecek  * exec package with the necessary functions for any exec-type-specific
     94  1.130  jdolecek  * handling.
     95  1.130  jdolecek  *
     96  1.130  jdolecek  * Functions for specific exec types should be defined in their own
     97  1.130  jdolecek  * header file.
     98  1.130  jdolecek  */
     99  1.138     lukem extern const struct execsw	execsw_builtin[];
    100  1.138     lukem extern int			nexecs_builtin;
    101  1.138     lukem static const struct execsw	**execsw = NULL;
    102  1.138     lukem static int			nexecs;
    103  1.138     lukem 
    104  1.153   thorpej u_int	exec_maxhdrsz;		/* must not be static - netbsd32 needs it */
    105  1.130  jdolecek 
    106  1.130  jdolecek #ifdef LKM
    107  1.130  jdolecek /* list of supported emulations */
    108  1.130  jdolecek static
    109  1.130  jdolecek LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
    110  1.130  jdolecek struct emul_entry {
    111  1.138     lukem 	LIST_ENTRY(emul_entry)	el_list;
    112  1.138     lukem 	const struct emul	*el_emul;
    113  1.138     lukem 	int			ro_entry;
    114  1.130  jdolecek };
    115  1.130  jdolecek 
    116  1.130  jdolecek /* list of dynamically loaded execsw entries */
    117  1.130  jdolecek static
    118  1.130  jdolecek LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
    119  1.130  jdolecek struct exec_entry {
    120  1.138     lukem 	LIST_ENTRY(exec_entry)	ex_list;
    121  1.138     lukem 	const struct execsw	*es;
    122  1.130  jdolecek };
    123  1.130  jdolecek 
    124  1.130  jdolecek /* structure used for building execw[] */
    125  1.130  jdolecek struct execsw_entry {
    126  1.138     lukem 	struct execsw_entry	*next;
    127  1.138     lukem 	const struct execsw	*es;
    128  1.130  jdolecek };
    129  1.130  jdolecek #endif /* LKM */
    130  1.130  jdolecek 
    131  1.124  jdolecek #ifdef SYSCALL_DEBUG
    132  1.124  jdolecek extern const char * const syscallnames[];
    133  1.124  jdolecek #endif
    134  1.124  jdolecek 
    135  1.185  drochner #ifdef COMPAT_16
    136  1.173  christos extern char	sigcode[], esigcode[];
    137  1.171       chs struct uvm_object *emul_netbsd_object;
    138  1.173  christos #endif
    139  1.171       chs 
    140  1.203  christos #ifndef __HAVE_SYSCALL_INTERN
    141  1.203  christos void	syscall(void);
    142  1.203  christos #endif
    143  1.203  christos 
    144  1.173  christos /* NetBSD emul struct */
    145  1.124  jdolecek const struct emul emul_netbsd = {
    146  1.124  jdolecek 	"netbsd",
    147  1.127  jdolecek 	NULL,		/* emulation path */
    148  1.133   mycroft #ifndef __HAVE_MINIMAL_EMUL
    149  1.140      manu 	EMUL_HAS_SYS___syscall,
    150  1.124  jdolecek 	NULL,
    151  1.124  jdolecek 	SYS_syscall,
    152  1.161  jdolecek 	SYS_NSYSENT,
    153  1.133   mycroft #endif
    154  1.124  jdolecek 	sysent,
    155  1.124  jdolecek #ifdef SYSCALL_DEBUG
    156  1.124  jdolecek 	syscallnames,
    157  1.124  jdolecek #else
    158  1.124  jdolecek 	NULL,
    159  1.124  jdolecek #endif
    160  1.133   mycroft 	sendsig,
    161  1.142  christos 	trapsignal,
    162  1.180      fvdl 	NULL,
    163  1.185  drochner #ifdef COMPAT_16
    164  1.124  jdolecek 	sigcode,
    165  1.124  jdolecek 	esigcode,
    166  1.171       chs 	&emul_netbsd_object,
    167  1.173  christos #else
    168  1.173  christos 	NULL,
    169  1.173  christos 	NULL,
    170  1.173  christos 	NULL,
    171  1.173  christos #endif
    172  1.145  jdolecek 	setregs,
    173  1.128  jdolecek 	NULL,
    174  1.128  jdolecek 	NULL,
    175  1.128  jdolecek 	NULL,
    176  1.179      manu 	NULL,
    177  1.179      manu 	NULL,
    178  1.133   mycroft #ifdef __HAVE_SYSCALL_INTERN
    179  1.133   mycroft 	syscall_intern,
    180  1.133   mycroft #else
    181  1.133   mycroft 	syscall,
    182  1.133   mycroft #endif
    183  1.156      manu 	NULL,
    184  1.156      manu 	NULL,
    185  1.195      fvdl 
    186  1.195      fvdl 	uvm_default_mapaddr,
    187  1.216      cube 	NULL,
    188  1.237        ad 	sizeof(ucontext_t),
    189  1.239      cube 	startlwp,
    190  1.124  jdolecek };
    191  1.124  jdolecek 
    192  1.147  jdolecek #ifdef LKM
    193   1.55       cgd /*
    194  1.130  jdolecek  * Exec lock. Used to control access to execsw[] structures.
    195  1.130  jdolecek  * This must not be static so that netbsd32 can access it, too.
    196  1.130  jdolecek  */
    197  1.237        ad krwlock_t exec_lock;
    198  1.183  junyoung 
    199  1.138     lukem static void link_es(struct execsw_entry **, const struct execsw *);
    200  1.130  jdolecek #endif /* LKM */
    201  1.130  jdolecek 
    202  1.259        ad static kmutex_t sigobject_lock;
    203  1.259        ad 
    204  1.130  jdolecek /*
    205   1.55       cgd  * check exec:
    206   1.55       cgd  * given an "executable" described in the exec package's namei info,
    207   1.55       cgd  * see what we can do with it.
    208   1.55       cgd  *
    209   1.55       cgd  * ON ENTRY:
    210   1.55       cgd  *	exec package with appropriate namei info
    211  1.212  christos  *	lwp pointer of exec'ing lwp
    212   1.55       cgd  *	NO SELF-LOCKED VNODES
    213   1.55       cgd  *
    214   1.55       cgd  * ON EXIT:
    215   1.55       cgd  *	error:	nothing held, etc.  exec header still allocated.
    216   1.77       cgd  *	ok:	filled exec package, executable's vnode (unlocked).
    217   1.55       cgd  *
    218   1.55       cgd  * EXEC SWITCH ENTRY:
    219   1.55       cgd  * 	Locked vnode to check, exec package, proc.
    220   1.55       cgd  *
    221   1.55       cgd  * EXEC SWITCH EXIT:
    222   1.77       cgd  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
    223   1.55       cgd  *	error:	destructive:
    224   1.55       cgd  *			everything deallocated execept exec header.
    225   1.76       cgd  *		non-destructive:
    226   1.77       cgd  *			error code, executable's vnode (unlocked),
    227   1.76       cgd  *			exec header unmodified.
    228   1.55       cgd  */
    229   1.55       cgd int
    230  1.205  christos /*ARGSUSED*/
    231  1.233      elad check_exec(struct lwp *l, struct exec_package *epp)
    232   1.55       cgd {
    233  1.138     lukem 	int		error, i;
    234  1.138     lukem 	struct vnode	*vp;
    235   1.55       cgd 	struct nameidata *ndp;
    236  1.138     lukem 	size_t		resid;
    237   1.55       cgd 
    238   1.55       cgd 	ndp = epp->ep_ndp;
    239   1.55       cgd 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    240  1.244       dsl 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME | TRYEMULROOT;
    241   1.55       cgd 	/* first get the vnode */
    242   1.74  christos 	if ((error = namei(ndp)) != 0)
    243   1.55       cgd 		return error;
    244   1.55       cgd 	epp->ep_vp = vp = ndp->ni_vp;
    245   1.55       cgd 
    246   1.84   mycroft 	/* check access and type */
    247   1.55       cgd 	if (vp->v_type != VREG) {
    248   1.81    kleink 		error = EACCES;
    249   1.55       cgd 		goto bad1;
    250   1.55       cgd 	}
    251  1.254     pooka 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
    252   1.84   mycroft 		goto bad1;
    253   1.55       cgd 
    254   1.55       cgd 	/* get attributes */
    255  1.254     pooka 	if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred)) != 0)
    256   1.55       cgd 		goto bad1;
    257   1.55       cgd 
    258   1.55       cgd 	/* Check mount point */
    259   1.55       cgd 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    260   1.55       cgd 		error = EACCES;
    261   1.55       cgd 		goto bad1;
    262   1.55       cgd 	}
    263  1.141   thorpej 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
    264   1.83   mycroft 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    265   1.55       cgd 
    266   1.55       cgd 	/* try to open it */
    267  1.254     pooka 	if ((error = VOP_OPEN(vp, FREAD, l->l_cred)) != 0)
    268   1.55       cgd 		goto bad1;
    269   1.55       cgd 
    270   1.99  wrstuden 	/* unlock vp, since we need it unlocked from here on out. */
    271   1.90      fvdl 	VOP_UNLOCK(vp, 0);
    272   1.77       cgd 
    273  1.222      elad #if NVERIEXEC > 0
    274  1.236      elad 	error = veriexec_verify(l, vp, ndp->ni_cnd.cn_pnbuf,
    275  1.233      elad 	    epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT,
    276  1.236      elad 	    NULL);
    277  1.236      elad 	if (error)
    278  1.234      elad 		goto bad2;
    279  1.222      elad #endif /* NVERIEXEC > 0 */
    280  1.160     blymn 
    281  1.232      elad #ifdef PAX_SEGVGUARD
    282  1.240   thorpej 	error = pax_segvguard(l, vp, ndp->ni_cnd.cn_pnbuf, false);
    283  1.234      elad 	if (error)
    284  1.234      elad 		goto bad2;
    285  1.232      elad #endif /* PAX_SEGVGUARD */
    286  1.232      elad 
    287   1.55       cgd 	/* now we have the file, get the exec header */
    288   1.74  christos 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    289  1.223        ad 			UIO_SYSSPACE, 0, l->l_cred, &resid, NULL);
    290   1.74  christos 	if (error)
    291   1.55       cgd 		goto bad2;
    292   1.55       cgd 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    293   1.55       cgd 
    294   1.55       cgd 	/*
    295  1.136       eeh 	 * Set up default address space limits.  Can be overridden
    296  1.136       eeh 	 * by individual exec packages.
    297  1.183  junyoung 	 *
    298  1.235    rillig 	 * XXX probably should be all done in the exec packages.
    299  1.136       eeh 	 */
    300  1.136       eeh 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    301  1.136       eeh 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
    302  1.136       eeh 	/*
    303   1.55       cgd 	 * set up the vmcmds for creation of the process
    304   1.55       cgd 	 * address space
    305   1.55       cgd 	 */
    306   1.55       cgd 	error = ENOEXEC;
    307  1.244       dsl 	for (i = 0; i < nexecs; i++) {
    308   1.68       cgd 		int newerror;
    309   1.68       cgd 
    310  1.130  jdolecek 		epp->ep_esch = execsw[i];
    311  1.212  christos 		newerror = (*execsw[i]->es_makecmds)(l, epp);
    312  1.244       dsl 
    313  1.244       dsl 		if (!newerror) {
    314  1.244       dsl 			/* Seems ok: check that entry point is sane */
    315  1.244       dsl 			if (epp->ep_entry > VM_MAXUSER_ADDRESS) {
    316  1.244       dsl 				error = ENOEXEC;
    317  1.244       dsl 				break;
    318  1.244       dsl 			}
    319  1.244       dsl 
    320  1.244       dsl 			/* check limits */
    321  1.244       dsl 			if ((epp->ep_tsize > MAXTSIZ) ||
    322  1.244       dsl 			    (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit
    323  1.244       dsl 						    [RLIMIT_DATA].rlim_cur)) {
    324  1.244       dsl 				error = ENOMEM;
    325  1.244       dsl 				break;
    326  1.244       dsl 			}
    327  1.244       dsl 			return 0;
    328  1.244       dsl 		}
    329  1.244       dsl 
    330  1.244       dsl 		if (epp->ep_emul_root != NULL) {
    331  1.244       dsl 			vrele(epp->ep_emul_root);
    332  1.244       dsl 			epp->ep_emul_root = NULL;
    333  1.244       dsl 		}
    334  1.244       dsl 		if (epp->ep_interp != NULL) {
    335  1.244       dsl 			vrele(epp->ep_interp);
    336  1.244       dsl 			epp->ep_interp = NULL;
    337  1.244       dsl 		}
    338  1.244       dsl 
    339   1.68       cgd 		/* make sure the first "interesting" error code is saved. */
    340  1.244       dsl 		if (error == ENOEXEC)
    341   1.68       cgd 			error = newerror;
    342  1.124  jdolecek 
    343  1.244       dsl 		if (epp->ep_flags & EXEC_DESTR)
    344  1.244       dsl 			/* Error from "#!" code, tidied up by recursive call */
    345   1.55       cgd 			return error;
    346   1.55       cgd 	}
    347   1.55       cgd 
    348  1.249     pooka 	/* not found, error */
    349  1.249     pooka 
    350   1.55       cgd 	/*
    351   1.55       cgd 	 * free any vmspace-creation commands,
    352   1.55       cgd 	 * and release their references
    353   1.55       cgd 	 */
    354   1.55       cgd 	kill_vmcmds(&epp->ep_vmcmds);
    355   1.55       cgd 
    356   1.55       cgd bad2:
    357   1.55       cgd 	/*
    358   1.99  wrstuden 	 * close and release the vnode, restore the old one, free the
    359   1.55       cgd 	 * pathname buf, and punt.
    360   1.55       cgd 	 */
    361   1.99  wrstuden 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    362  1.254     pooka 	VOP_CLOSE(vp, FREAD, l->l_cred);
    363   1.99  wrstuden 	vput(vp);
    364  1.120   thorpej 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    365   1.55       cgd 	return error;
    366   1.55       cgd 
    367   1.55       cgd bad1:
    368   1.55       cgd 	/*
    369   1.55       cgd 	 * free the namei pathname buffer, and put the vnode
    370   1.55       cgd 	 * (which we don't yet have open).
    371   1.55       cgd 	 */
    372   1.77       cgd 	vput(vp);				/* was still locked */
    373  1.120   thorpej 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    374   1.55       cgd 	return error;
    375   1.55       cgd }
    376   1.55       cgd 
    377  1.188       chs #ifdef __MACHINE_STACK_GROWS_UP
    378  1.188       chs #define STACK_PTHREADSPACE NBPG
    379  1.188       chs #else
    380  1.188       chs #define STACK_PTHREADSPACE 0
    381  1.188       chs #endif
    382  1.188       chs 
    383  1.204      cube static int
    384  1.204      cube execve_fetch_element(char * const *array, size_t index, char **value)
    385  1.204      cube {
    386  1.204      cube 	return copyin(array + index, value, sizeof(*value));
    387  1.204      cube }
    388  1.204      cube 
    389   1.55       cgd /*
    390   1.55       cgd  * exec system call
    391   1.55       cgd  */
    392   1.55       cgd /* ARGSUSED */
    393   1.75  christos int
    394  1.258       dsl sys_execve(struct lwp *l, const struct sys_execve_args *uap, register_t *retval)
    395   1.71   thorpej {
    396  1.258       dsl 	/* {
    397  1.138     lukem 		syscallarg(const char *)	path;
    398  1.138     lukem 		syscallarg(char * const *)	argp;
    399  1.138     lukem 		syscallarg(char * const *)	envp;
    400  1.258       dsl 	} */
    401  1.204      cube 
    402  1.204      cube 	return execve1(l, SCARG(uap, path), SCARG(uap, argp),
    403  1.204      cube 	    SCARG(uap, envp), execve_fetch_element);
    404  1.204      cube }
    405  1.204      cube 
    406  1.204      cube int
    407  1.204      cube execve1(struct lwp *l, const char *path, char * const *args,
    408  1.204      cube     char * const *envs, execve_fetch_element_t fetch_element)
    409  1.204      cube {
    410  1.153   thorpej 	int			error;
    411  1.138     lukem 	struct exec_package	pack;
    412  1.138     lukem 	struct nameidata	nid;
    413  1.138     lukem 	struct vattr		attr;
    414  1.164   thorpej 	struct proc		*p;
    415  1.138     lukem 	char			*argp;
    416  1.138     lukem 	char			*dp, *sp;
    417  1.138     lukem 	long			argc, envc;
    418  1.248  christos 	size_t			i, len;
    419  1.138     lukem 	char			*stack;
    420  1.138     lukem 	struct ps_strings	arginfo;
    421  1.213      manu 	struct ps_strings	*aip = &arginfo;
    422  1.138     lukem 	struct vmspace		*vm;
    423  1.265      yamt 	struct exec_fakearg	*tmpfap;
    424  1.138     lukem 	int			szsigcode;
    425  1.138     lukem 	struct exec_vmcmd	*base_vcp;
    426  1.237        ad 	ksiginfo_t		ksi;
    427  1.237        ad 	ksiginfoq_t		kq;
    428  1.260  christos 	char			*pathbuf;
    429  1.255  christos 	size_t			pathbuflen;
    430   1.55       cgd 
    431  1.237        ad 	p = l->l_proc;
    432  1.164   thorpej 
    433  1.149  christos 	/*
    434  1.269  christos 	 * Check if we have exceeded our number of processes limit.
    435  1.269  christos 	 * This is so that we handle the case where a root daemon
    436  1.269  christos 	 * forked, ran setuid to become the desired user and is trying
    437  1.269  christos 	 * to exec. The obvious place to do the reference counting check
    438  1.269  christos 	 * is setuid(), but we don't do the reference counting check there
    439  1.269  christos 	 * like other OS's do because then all the programs that use setuid()
    440  1.269  christos 	 * must be modified to check the return code of setuid() and exit().
    441  1.269  christos 	 * It is dangerous to make setuid() fail, because it fails open and
    442  1.269  christos 	 * the program will continue to run as root. If we make it succeed
    443  1.269  christos 	 * and return an error code, again we are not enforcing the limit.
    444  1.269  christos 	 * The best place to enforce the limit is here, when the process tries
    445  1.269  christos 	 * to execute a new image, because eventually the process will need
    446  1.269  christos 	 * to call exec in order to do something useful.
    447  1.269  christos 	 */
    448  1.269  christos 
    449  1.269  christos 	if ((p->p_flag & PK_SUGID) &&
    450  1.269  christos 	    chgproccnt(kauth_cred_getuid(p->p_cred), 0) >
    451  1.269  christos 	    p->p_rlimit[RLIMIT_NPROC].rlim_cur)
    452  1.269  christos 		return EAGAIN;
    453  1.269  christos 
    454  1.269  christos 	/*
    455  1.237        ad 	 * Drain existing references and forbid new ones.  The process
    456  1.237        ad 	 * should be left alone until we're done here.  This is necessary
    457  1.237        ad 	 * to avoid race conditions - e.g. in ptrace() - that might allow
    458  1.237        ad 	 * a local user to illicitly obtain elevated privileges.
    459  1.237        ad 	 */
    460  1.252        ad 	rw_enter(&p->p_reflock, RW_WRITER);
    461  1.149  christos 
    462  1.138     lukem 	base_vcp = NULL;
    463   1.55       cgd 	/*
    464  1.129  jdolecek 	 * Init the namei data to point the file user's program name.
    465  1.129  jdolecek 	 * This is done here rather than in check_exec(), so that it's
    466  1.129  jdolecek 	 * possible to override this settings if any of makecmd/probe
    467  1.129  jdolecek 	 * functions call check_exec() recursively - for example,
    468  1.129  jdolecek 	 * see exec_script_makecmds().
    469  1.129  jdolecek 	 */
    470  1.260  christos 	pathbuf = PNBUF_GET();
    471  1.260  christos 	error = copyinstr(path, pathbuf, MAXPATHLEN, &pathbuflen);
    472  1.248  christos 	if (error) {
    473  1.248  christos 		DPRINTF(("execve: copyinstr path %d", error));
    474  1.200      elad 		goto clrflg;
    475  1.248  christos 	}
    476  1.200      elad 
    477  1.257     pooka 	NDINIT(&nid, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_SYSSPACE, pathbuf);
    478   1.55       cgd 
    479   1.55       cgd 	/*
    480   1.55       cgd 	 * initialize the fields of the exec package.
    481   1.55       cgd 	 */
    482  1.204      cube 	pack.ep_name = path;
    483  1.265      yamt 	pack.ep_hdr = kmem_alloc(exec_maxhdrsz, KM_SLEEP);
    484   1.55       cgd 	pack.ep_hdrlen = exec_maxhdrsz;
    485   1.55       cgd 	pack.ep_hdrvalid = 0;
    486   1.55       cgd 	pack.ep_ndp = &nid;
    487   1.67  christos 	pack.ep_emul_arg = NULL;
    488   1.55       cgd 	pack.ep_vmcmds.evs_cnt = 0;
    489   1.55       cgd 	pack.ep_vmcmds.evs_used = 0;
    490   1.55       cgd 	pack.ep_vap = &attr;
    491   1.55       cgd 	pack.ep_flags = 0;
    492  1.244       dsl 	pack.ep_emul_root = NULL;
    493  1.244       dsl 	pack.ep_interp = NULL;
    494  1.244       dsl 	pack.ep_esch = NULL;
    495   1.55       cgd 
    496  1.147  jdolecek #ifdef LKM
    497  1.237        ad 	rw_enter(&exec_lock, RW_READER);
    498  1.147  jdolecek #endif
    499  1.130  jdolecek 
    500   1.55       cgd 	/* see if we can run it. */
    501  1.248  christos 	if ((error = check_exec(l, &pack)) != 0) {
    502  1.261   xtraeme 		if (error != ENOENT) {
    503  1.260  christos 			DPRINTF(("execve: check exec failed %d\n", error));
    504  1.261   xtraeme 		}
    505   1.55       cgd 		goto freehdr;
    506  1.248  christos 	}
    507   1.55       cgd 
    508   1.55       cgd 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    509   1.55       cgd 
    510   1.55       cgd 	/* allocate an argument buffer */
    511  1.196      yamt 	argp = (char *) uvm_km_alloc(exec_map, NCARGS, 0,
    512  1.196      yamt 	    UVM_KMF_PAGEABLE|UVM_KMF_WAITVA);
    513   1.55       cgd #ifdef DIAGNOSTIC
    514  1.208  junyoung 	if (argp == NULL)
    515   1.55       cgd 		panic("execve: argp == NULL");
    516   1.55       cgd #endif
    517   1.55       cgd 	dp = argp;
    518   1.55       cgd 	argc = 0;
    519   1.55       cgd 
    520   1.55       cgd 	/* copy the fake args list, if there's one, freeing it as we go */
    521   1.55       cgd 	if (pack.ep_flags & EXEC_HASARGL) {
    522   1.55       cgd 		tmpfap = pack.ep_fa;
    523  1.265      yamt 		while (tmpfap->fa_arg != NULL) {
    524  1.265      yamt 			const char *cp;
    525   1.55       cgd 
    526  1.265      yamt 			cp = tmpfap->fa_arg;
    527   1.55       cgd 			while (*cp)
    528   1.55       cgd 				*dp++ = *cp++;
    529   1.74  christos 			dp++;
    530   1.55       cgd 
    531  1.265      yamt 			kmem_free(tmpfap->fa_arg, tmpfap->fa_len);
    532   1.55       cgd 			tmpfap++; argc++;
    533   1.55       cgd 		}
    534  1.265      yamt 		kmem_free(pack.ep_fa, pack.ep_fa_len);
    535   1.55       cgd 		pack.ep_flags &= ~EXEC_HASARGL;
    536   1.55       cgd 	}
    537   1.55       cgd 
    538   1.55       cgd 	/* Now get argv & environment */
    539  1.204      cube 	if (args == NULL) {
    540  1.248  christos 		DPRINTF(("execve: null args\n"));
    541   1.55       cgd 		error = EINVAL;
    542   1.55       cgd 		goto bad;
    543   1.55       cgd 	}
    544  1.204      cube 	/* 'i' will index the argp/envp element to be retrieved */
    545  1.204      cube 	i = 0;
    546   1.55       cgd 	if (pack.ep_flags & EXEC_SKIPARG)
    547  1.204      cube 		i++;
    548   1.55       cgd 
    549   1.55       cgd 	while (1) {
    550   1.55       cgd 		len = argp + ARG_MAX - dp;
    551  1.248  christos 		if ((error = (*fetch_element)(args, i, &sp)) != 0) {
    552  1.248  christos 			DPRINTF(("execve: fetch_element args %d\n", error));
    553   1.55       cgd 			goto bad;
    554  1.248  christos 		}
    555   1.55       cgd 		if (!sp)
    556   1.55       cgd 			break;
    557   1.74  christos 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    558  1.248  christos 			DPRINTF(("execve: copyinstr args %d\n", error));
    559   1.55       cgd 			if (error == ENAMETOOLONG)
    560   1.55       cgd 				error = E2BIG;
    561   1.55       cgd 			goto bad;
    562   1.55       cgd 		}
    563  1.247        ad 		ktrexecarg(dp, len - 1);
    564   1.55       cgd 		dp += len;
    565  1.204      cube 		i++;
    566   1.55       cgd 		argc++;
    567   1.55       cgd 	}
    568   1.55       cgd 
    569   1.55       cgd 	envc = 0;
    570   1.74  christos 	/* environment need not be there */
    571  1.204      cube 	if (envs != NULL) {
    572  1.204      cube 		i = 0;
    573   1.55       cgd 		while (1) {
    574   1.55       cgd 			len = argp + ARG_MAX - dp;
    575  1.248  christos 			if ((error = (*fetch_element)(envs, i, &sp)) != 0) {
    576  1.248  christos 				DPRINTF(("execve: fetch_element env %d\n", error));
    577   1.55       cgd 				goto bad;
    578  1.248  christos 			}
    579   1.55       cgd 			if (!sp)
    580   1.55       cgd 				break;
    581   1.74  christos 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    582  1.248  christos 				DPRINTF(("execve: copyinstr env %d\n", error));
    583   1.55       cgd 				if (error == ENAMETOOLONG)
    584   1.55       cgd 					error = E2BIG;
    585   1.55       cgd 				goto bad;
    586   1.55       cgd 			}
    587  1.247        ad 			ktrexecenv(dp, len - 1);
    588   1.55       cgd 			dp += len;
    589  1.204      cube 			i++;
    590   1.55       cgd 			envc++;
    591   1.55       cgd 		}
    592   1.55       cgd 	}
    593   1.61   mycroft 
    594   1.61   mycroft 	dp = (char *) ALIGN(dp);
    595   1.55       cgd 
    596  1.244       dsl 	szsigcode = pack.ep_esch->es_emul->e_esigcode -
    597  1.244       dsl 	    pack.ep_esch->es_emul->e_sigcode;
    598   1.65      fvdl 
    599  1.267       dsl #ifdef __MACHINE_STACK_GROWS_UP
    600  1.267       dsl /* See big comment lower down */
    601  1.267       dsl #define	RTLD_GAP	32
    602  1.267       dsl #else
    603  1.267       dsl #define	RTLD_GAP	0
    604  1.267       dsl #endif
    605  1.267       dsl 
    606   1.55       cgd 	/* Now check if args & environ fit into new stack */
    607  1.105       eeh 	if (pack.ep_flags & EXEC_32)
    608  1.244       dsl 		len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
    609  1.267       dsl 		    sizeof(int) + sizeof(int) + dp + RTLD_GAP +
    610  1.188       chs 		    szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
    611  1.188       chs 		    - argp;
    612  1.105       eeh 	else
    613  1.244       dsl 		len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
    614  1.267       dsl 		    sizeof(char *) + sizeof(int) + dp + RTLD_GAP +
    615  1.188       chs 		    szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
    616  1.188       chs 		    - argp;
    617   1.67  christos 
    618  1.262      elad #ifdef PAX_ASLR
    619  1.262      elad 	if (pax_aslr_active(l))
    620  1.262      elad 		len += (arc4random() % PAGE_SIZE);
    621  1.262      elad #endif /* PAX_ASLR */
    622  1.262      elad 
    623  1.243      matt #ifdef STACKLALIGN	/* arm, etc. */
    624  1.243      matt 	len = STACKALIGN(len);	/* make the stack "safely" aligned */
    625  1.243      matt #else
    626   1.55       cgd 	len = ALIGN(len);	/* make the stack "safely" aligned */
    627  1.243      matt #endif
    628   1.55       cgd 
    629   1.55       cgd 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    630  1.248  christos 		DPRINTF(("execve: stack limit exceeded %zu\n", len));
    631   1.55       cgd 		error = ENOMEM;
    632   1.55       cgd 		goto bad;
    633   1.55       cgd 	}
    634   1.55       cgd 
    635  1.237        ad 	/* Get rid of other LWPs. */
    636  1.237        ad 	if (p->p_nlwps > 1) {
    637  1.237        ad 		mutex_enter(&p->p_smutex);
    638  1.237        ad 		exit_lwps(l);
    639  1.237        ad 		mutex_exit(&p->p_smutex);
    640  1.237        ad 	}
    641  1.164   thorpej 	KDASSERT(p->p_nlwps == 1);
    642  1.164   thorpej 
    643  1.253        ad 	/* Destroy any lwpctl info. */
    644  1.253        ad 	if (p->p_lwpctl != NULL)
    645  1.253        ad 		lwp_ctl_exit();
    646  1.253        ad 
    647  1.164   thorpej 	/* This is now LWP 1 */
    648  1.164   thorpej 	l->l_lid = 1;
    649  1.164   thorpej 	p->p_nlwpid = 1;
    650  1.164   thorpej 
    651  1.164   thorpej 	/* Remove POSIX timers */
    652  1.164   thorpej 	timers_free(p, TIMERS_POSIX);
    653  1.164   thorpej 
    654   1.55       cgd 	/* adjust "active stack depth" for process VSZ */
    655   1.55       cgd 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    656   1.55       cgd 
    657   1.86   thorpej 	/*
    658   1.86   thorpej 	 * Do whatever is necessary to prepare the address space
    659   1.86   thorpej 	 * for remapping.  Note that this might replace the current
    660   1.86   thorpej 	 * vmspace with another!
    661   1.86   thorpej 	 */
    662  1.164   thorpej 	uvmspace_exec(l, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
    663   1.55       cgd 
    664  1.186       chs 	/* record proc's vnode, for use by procfs and others */
    665  1.186       chs         if (p->p_textvp)
    666  1.186       chs                 vrele(p->p_textvp);
    667  1.186       chs 	VREF(pack.ep_vp);
    668  1.186       chs 	p->p_textvp = pack.ep_vp;
    669  1.186       chs 
    670   1.55       cgd 	/* Now map address space */
    671   1.86   thorpej 	vm = p->p_vmspace;
    672  1.241    dogcow 	vm->vm_taddr = (void *)pack.ep_taddr;
    673   1.55       cgd 	vm->vm_tsize = btoc(pack.ep_tsize);
    674  1.241    dogcow 	vm->vm_daddr = (void*)pack.ep_daddr;
    675   1.55       cgd 	vm->vm_dsize = btoc(pack.ep_dsize);
    676   1.55       cgd 	vm->vm_ssize = btoc(pack.ep_ssize);
    677  1.241    dogcow 	vm->vm_maxsaddr = (void *)pack.ep_maxsaddr;
    678  1.241    dogcow 	vm->vm_minsaddr = (void *)pack.ep_minsaddr;
    679   1.55       cgd 
    680  1.260  christos #ifdef PAX_ASLR
    681  1.260  christos 	pax_aslr_init(l, vm);
    682  1.260  christos #endif /* PAX_ASLR */
    683  1.260  christos 
    684   1.55       cgd 	/* create the new process's VM space by running the vmcmds */
    685   1.55       cgd #ifdef DIAGNOSTIC
    686   1.55       cgd 	if (pack.ep_vmcmds.evs_used == 0)
    687   1.55       cgd 		panic("execve: no vmcmds");
    688   1.55       cgd #endif
    689   1.55       cgd 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    690   1.55       cgd 		struct exec_vmcmd *vcp;
    691   1.55       cgd 
    692   1.55       cgd 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    693  1.114      matt 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    694  1.114      matt #ifdef DIAGNOSTIC
    695  1.114      matt 			if (base_vcp == NULL)
    696  1.114      matt 				panic("execve: relative vmcmd with no base");
    697  1.114      matt 			if (vcp->ev_flags & VMCMD_BASE)
    698  1.114      matt 				panic("execve: illegal base & relative vmcmd");
    699  1.114      matt #endif
    700  1.114      matt 			vcp->ev_addr += base_vcp->ev_addr;
    701  1.114      matt 		}
    702  1.212  christos 		error = (*vcp->ev_proc)(l, vcp);
    703  1.143  christos #ifdef DEBUG_EXEC
    704  1.111      matt 		if (error) {
    705  1.248  christos 			size_t j;
    706  1.143  christos 			struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
    707  1.143  christos 			for (j = 0; j <= i; j++)
    708  1.143  christos 				uprintf(
    709  1.248  christos 			"vmcmd[%zu] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
    710  1.143  christos 				    j, vp[j].ev_addr, vp[j].ev_len,
    711  1.143  christos 				    vp[j].ev_offset, vp[j].ev_prot,
    712  1.143  christos 				    vp[j].ev_flags);
    713  1.111      matt 		}
    714  1.143  christos #endif /* DEBUG_EXEC */
    715  1.114      matt 		if (vcp->ev_flags & VMCMD_BASE)
    716  1.114      matt 			base_vcp = vcp;
    717   1.55       cgd 	}
    718   1.55       cgd 
    719   1.55       cgd 	/* free the vmspace-creation commands, and release their references */
    720   1.55       cgd 	kill_vmcmds(&pack.ep_vmcmds);
    721   1.55       cgd 
    722  1.186       chs 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    723  1.254     pooka 	VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred);
    724  1.186       chs 	vput(pack.ep_vp);
    725  1.186       chs 
    726   1.55       cgd 	/* if an error happened, deallocate and punt */
    727  1.111      matt 	if (error) {
    728  1.248  christos 		DPRINTF(("execve: vmcmd %zu failed: %d\n", i - 1, error));
    729   1.55       cgd 		goto exec_abort;
    730  1.111      matt 	}
    731   1.55       cgd 
    732   1.55       cgd 	/* remember information about the process */
    733   1.55       cgd 	arginfo.ps_nargvstr = argc;
    734   1.55       cgd 	arginfo.ps_nenvstr = envc;
    735   1.55       cgd 
    736  1.255  christos 	/* set command name & other accounting info */
    737  1.255  christos 	i = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    738  1.255  christos 	(void)memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, i);
    739  1.255  christos 	p->p_comm[i] = '\0';
    740  1.255  christos 
    741  1.255  christos 	dp = PNBUF_GET();
    742  1.255  christos 	/*
    743  1.255  christos 	 * If the path starts with /, we don't need to do any work.
    744  1.255  christos 	 * This handles the majority of the cases.
    745  1.255  christos 	 * In the future perhaps we could canonicalize it?
    746  1.255  christos 	 */
    747  1.255  christos 	if (pathbuf[0] == '/')
    748  1.255  christos 		(void)strlcpy(pack.ep_path = dp, pathbuf, MAXPATHLEN);
    749  1.255  christos #ifdef notyet
    750  1.255  christos 	/*
    751  1.255  christos 	 * Although this works most of the time [since the entry was just
    752  1.255  christos 	 * entered in the cache] we don't use it because it theoretically
    753  1.255  christos 	 * can fail and it is not the cleanest interface, because there
    754  1.255  christos 	 * could be races. When the namei cache is re-written, this can
    755  1.255  christos 	 * be changed to use the appropriate function.
    756  1.255  christos 	 */
    757  1.255  christos 	else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p)))
    758  1.255  christos 		pack.ep_path = dp;
    759  1.255  christos #endif
    760  1.255  christos 	else {
    761  1.256  christos #ifdef notyet
    762  1.255  christos 		printf("Cannot get path for pid %d [%s] (error %d)",
    763  1.255  christos 		    (int)p->p_pid, p->p_comm, error);
    764  1.255  christos #endif
    765  1.255  christos 		pack.ep_path = NULL;
    766  1.255  christos 		PNBUF_PUT(dp);
    767  1.255  christos 	}
    768  1.255  christos 
    769  1.163       chs 	stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
    770  1.188       chs 		STACK_PTHREADSPACE + sizeof(struct ps_strings) + szsigcode),
    771  1.163       chs 		len - (sizeof(struct ps_strings) + szsigcode));
    772  1.267       dsl 
    773  1.163       chs #ifdef __MACHINE_STACK_GROWS_UP
    774  1.163       chs 	/*
    775  1.163       chs 	 * The copyargs call always copies into lower addresses
    776  1.163       chs 	 * first, moving towards higher addresses, starting with
    777  1.183  junyoung 	 * the stack pointer that we give.  When the stack grows
    778  1.183  junyoung 	 * down, this puts argc/argv/envp very shallow on the
    779  1.267       dsl 	 * stack, right at the first user stack pointer.
    780  1.267       dsl 	 * When the stack grows up, the situation is reversed.
    781  1.163       chs 	 *
    782  1.163       chs 	 * Normally, this is no big deal.  But the ld_elf.so _rtld()
    783  1.183  junyoung 	 * function expects to be called with a single pointer to
    784  1.183  junyoung 	 * a region that has a few words it can stash values into,
    785  1.163       chs 	 * followed by argc/argv/envp.  When the stack grows down,
    786  1.163       chs 	 * it's easy to decrement the stack pointer a little bit to
    787  1.163       chs 	 * allocate the space for these few words and pass the new
    788  1.163       chs 	 * stack pointer to _rtld.  When the stack grows up, however,
    789  1.171       chs 	 * a few words before argc is part of the signal trampoline, XXX
    790  1.163       chs 	 * so we have a problem.
    791  1.163       chs 	 *
    792  1.183  junyoung 	 * Instead of changing how _rtld works, we take the easy way
    793  1.267       dsl 	 * out and steal 32 bytes before we call copyargs.
    794  1.267       dsl 	 * This extra space was allowed for when 'len' was calculated.
    795  1.163       chs 	 */
    796  1.267       dsl 	stack += RTLD_GAP;
    797  1.163       chs #endif /* __MACHINE_STACK_GROWS_UP */
    798  1.163       chs 
    799   1.55       cgd 	/* Now copy argc, args & environ to new stack */
    800  1.244       dsl 	error = (*pack.ep_esch->es_copyargs)(l, &pack, &arginfo, &stack, argp);
    801  1.255  christos 	if (pack.ep_path) {
    802  1.255  christos 		PNBUF_PUT(pack.ep_path);
    803  1.255  christos 		pack.ep_path = NULL;
    804  1.255  christos 	}
    805  1.144  christos 	if (error) {
    806  1.144  christos 		DPRINTF(("execve: copyargs failed %d\n", error));
    807   1.55       cgd 		goto exec_abort;
    808  1.111      matt 	}
    809  1.144  christos 	/* Move the stack back to original point */
    810  1.163       chs 	stack = (char *)STACK_GROW(vm->vm_minsaddr, len);
    811   1.55       cgd 
    812  1.121       eeh 	/* fill process ps_strings info */
    813  1.188       chs 	p->p_psstr = (struct ps_strings *)
    814  1.188       chs 	    STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, STACK_PTHREADSPACE),
    815  1.163       chs 	    sizeof(struct ps_strings));
    816  1.121       eeh 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    817  1.121       eeh 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    818  1.121       eeh 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    819  1.121       eeh 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    820  1.121       eeh 
    821   1.55       cgd 	/* copy out the process's ps_strings structure */
    822  1.213      manu 	if ((error = copyout(aip, (char *)p->p_psstr,
    823  1.144  christos 	    sizeof(arginfo))) != 0) {
    824  1.143  christos 		DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
    825  1.213      manu 		       aip, (char *)p->p_psstr, (long)sizeof(arginfo)));
    826   1.55       cgd 		goto exec_abort;
    827  1.111      matt 	}
    828  1.109    simonb 
    829  1.270        ad 	fd_closeexec();		/* handle close on exec */
    830   1.55       cgd 	execsigs(p);		/* reset catched signals */
    831  1.183  junyoung 
    832  1.164   thorpej 	l->l_ctxlink = NULL;	/* reset ucontext link */
    833   1.55       cgd 
    834  1.255  christos 
    835   1.55       cgd 	p->p_acflag &= ~AFORK;
    836  1.259        ad 	mutex_enter(&p->p_mutex);
    837  1.238     pavel 	p->p_flag |= PK_EXEC;
    838  1.259        ad 	mutex_exit(&p->p_mutex);
    839  1.237        ad 
    840  1.237        ad 	/*
    841  1.237        ad 	 * Stop profiling.
    842  1.237        ad 	 */
    843  1.237        ad 	if ((p->p_stflag & PST_PROFIL) != 0) {
    844  1.237        ad 		mutex_spin_enter(&p->p_stmutex);
    845  1.237        ad 		stopprofclock(p);
    846  1.237        ad 		mutex_spin_exit(&p->p_stmutex);
    847  1.237        ad 	}
    848  1.237        ad 
    849  1.237        ad 	/*
    850  1.237        ad 	 * It's OK to test PS_PPWAIT unlocked here, as other LWPs have
    851  1.237        ad 	 * exited and exec()/exit() are the only places it will be cleared.
    852  1.237        ad 	 */
    853  1.237        ad 	if ((p->p_sflag & PS_PPWAIT) != 0) {
    854  1.271        ad 		mutex_enter(proc_lock);
    855  1.237        ad 		mutex_enter(&p->p_smutex);
    856  1.237        ad 		p->p_sflag &= ~PS_PPWAIT;
    857  1.237        ad 		cv_broadcast(&p->p_pptr->p_waitcv);
    858  1.237        ad 		mutex_exit(&p->p_smutex);
    859  1.271        ad 		mutex_exit(proc_lock);
    860   1.55       cgd 	}
    861   1.55       cgd 
    862   1.55       cgd 	/*
    863  1.237        ad 	 * Deal with set[ug]id.  MNT_NOSUID has already been used to disable
    864  1.237        ad 	 * s[ug]id.  It's OK to check for PSL_TRACED here as we have blocked
    865  1.237        ad 	 * out additional references on the process for the moment.
    866   1.55       cgd 	 */
    867  1.237        ad 	if ((p->p_slflag & PSL_TRACED) == 0 &&
    868  1.141   thorpej 
    869  1.141   thorpej 	    (((attr.va_mode & S_ISUID) != 0 &&
    870  1.221        ad 	      kauth_cred_geteuid(l->l_cred) != attr.va_uid) ||
    871  1.141   thorpej 
    872  1.141   thorpej 	     ((attr.va_mode & S_ISGID) != 0 &&
    873  1.221        ad 	      kauth_cred_getegid(l->l_cred) != attr.va_gid))) {
    874  1.141   thorpej 		/*
    875  1.141   thorpej 		 * Mark the process as SUGID before we do
    876  1.141   thorpej 		 * anything that might block.
    877  1.141   thorpej 		 */
    878  1.237        ad 		proc_crmod_enter();
    879  1.240   thorpej 		proc_crmod_leave(NULL, NULL, true);
    880  1.152  christos 
    881  1.152  christos 		/* Make sure file descriptors 0..2 are in use. */
    882  1.270        ad 		if ((error = fd_checkstd()) != 0) {
    883  1.209  christos 			DPRINTF(("execve: fdcheckstd failed %d\n", error));
    884  1.152  christos 			goto exec_abort;
    885  1.209  christos 		}
    886  1.141   thorpej 
    887  1.220        ad 		/*
    888  1.220        ad 		 * Copy the credential so other references don't see our
    889  1.220        ad 		 * changes.
    890  1.220        ad 		 */
    891  1.221        ad 		l->l_cred = kauth_cred_copy(l->l_cred);
    892   1.55       cgd #ifdef KTRACE
    893   1.55       cgd 		/*
    894  1.268      elad 		 * If the persistent trace flag isn't set, turn off.
    895   1.55       cgd 		 */
    896  1.237        ad 		if (p->p_tracep) {
    897  1.247        ad 			mutex_enter(&ktrace_lock);
    898  1.268      elad 			if (!(p->p_traceflag & KTRFAC_PERSISTENT))
    899  1.237        ad 				ktrderef(p);
    900  1.247        ad 			mutex_exit(&ktrace_lock);
    901  1.237        ad 		}
    902   1.55       cgd #endif
    903   1.83   mycroft 		if (attr.va_mode & S_ISUID)
    904  1.221        ad 			kauth_cred_seteuid(l->l_cred, attr.va_uid);
    905   1.83   mycroft 		if (attr.va_mode & S_ISGID)
    906  1.221        ad 			kauth_cred_setegid(l->l_cred, attr.va_gid);
    907  1.210  christos 	} else {
    908  1.221        ad 		if (kauth_cred_geteuid(l->l_cred) ==
    909  1.221        ad 		    kauth_cred_getuid(l->l_cred) &&
    910  1.221        ad 		    kauth_cred_getegid(l->l_cred) ==
    911  1.221        ad 		    kauth_cred_getgid(l->l_cred))
    912  1.238     pavel 			p->p_flag &= ~PK_SUGID;
    913  1.210  christos 	}
    914  1.220        ad 
    915  1.220        ad 	/*
    916  1.220        ad 	 * Copy the credential so other references don't see our changes.
    917  1.220        ad 	 * Test to see if this is necessary first, since in the common case
    918  1.220        ad 	 * we won't need a private reference.
    919  1.220        ad 	 */
    920  1.221        ad 	if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) ||
    921  1.221        ad 	    kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) {
    922  1.221        ad 		l->l_cred = kauth_cred_copy(l->l_cred);
    923  1.221        ad 		kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred));
    924  1.221        ad 		kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred));
    925  1.220        ad 	}
    926  1.155  gmcgarry 
    927  1.221        ad 	/* Update the master credentials. */
    928  1.227        ad 	if (l->l_cred != p->p_cred) {
    929  1.227        ad 		kauth_cred_t ocred;
    930  1.227        ad 
    931  1.227        ad 		kauth_cred_hold(l->l_cred);
    932  1.237        ad 		mutex_enter(&p->p_mutex);
    933  1.227        ad 		ocred = p->p_cred;
    934  1.227        ad 		p->p_cred = l->l_cred;
    935  1.237        ad 		mutex_exit(&p->p_mutex);
    936  1.227        ad 		kauth_cred_free(ocred);
    937  1.227        ad 	}
    938  1.221        ad 
    939  1.155  gmcgarry #if defined(__HAVE_RAS)
    940  1.155  gmcgarry 	/*
    941  1.155  gmcgarry 	 * Remove all RASs from the address space.
    942  1.155  gmcgarry 	 */
    943  1.251        ad 	ras_purgeall();
    944  1.155  gmcgarry #endif
    945  1.107      fvdl 
    946  1.107      fvdl 	doexechooks(p);
    947   1.55       cgd 
    948  1.196      yamt 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
    949   1.55       cgd 
    950  1.120   thorpej 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    951  1.159  jdolecek 
    952  1.159  jdolecek 	/* notify others that we exec'd */
    953  1.159  jdolecek 	KNOTE(&p->p_klist, NOTE_EXEC);
    954   1.55       cgd 
    955   1.55       cgd 	/* setup new registers and do misc. setup. */
    956  1.244       dsl 	(*pack.ep_esch->es_emul->e_setregs)(l, &pack, (u_long) stack);
    957  1.244       dsl 	if (pack.ep_esch->es_setregs)
    958  1.244       dsl 		(*pack.ep_esch->es_setregs)(l, &pack, (u_long) stack);
    959   1.55       cgd 
    960  1.171       chs 	/* map the process's signal trampoline code */
    961  1.244       dsl 	if (exec_sigcode_map(p, pack.ep_esch->es_emul)) {
    962  1.209  christos 		DPRINTF(("execve: map sigcode failed %d\n", error));
    963  1.171       chs 		goto exec_abort;
    964  1.209  christos 	}
    965  1.171       chs 
    966  1.265      yamt 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
    967  1.122  jdolecek 
    968  1.244       dsl 	/* The emulation root will usually have been found when we looked
    969  1.244       dsl 	 * for the elf interpreter (or similar), if not look now. */
    970  1.244       dsl 	if (pack.ep_esch->es_emul->e_path != NULL && pack.ep_emul_root == NULL)
    971  1.244       dsl 		emul_find_root(l, &pack);
    972  1.244       dsl 
    973  1.244       dsl 	/* Any old emulation root got removed by fdcloseexec */
    974  1.259        ad 	rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER);
    975  1.244       dsl 	p->p_cwdi->cwdi_edir = pack.ep_emul_root;
    976  1.259        ad 	rw_exit(&p->p_cwdi->cwdi_lock);
    977  1.244       dsl 	pack.ep_emul_root = NULL;
    978  1.244       dsl 	if (pack.ep_interp != NULL)
    979  1.244       dsl 		vrele(pack.ep_interp);
    980  1.244       dsl 
    981  1.122  jdolecek 	/*
    982  1.194     peter 	 * Call emulation specific exec hook. This can setup per-process
    983  1.122  jdolecek 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    984  1.122  jdolecek 	 *
    985  1.122  jdolecek 	 * If we are executing process of different emulation than the
    986  1.122  jdolecek 	 * original forked process, call e_proc_exit() of the old emulation
    987  1.122  jdolecek 	 * first, then e_proc_exec() of new emulation. If the emulation is
    988  1.122  jdolecek 	 * same, the exec hook code should deallocate any old emulation
    989  1.122  jdolecek 	 * resources held previously by this process.
    990  1.122  jdolecek 	 */
    991  1.124  jdolecek 	if (p->p_emul && p->p_emul->e_proc_exit
    992  1.244       dsl 	    && p->p_emul != pack.ep_esch->es_emul)
    993  1.122  jdolecek 		(*p->p_emul->e_proc_exit)(p);
    994  1.122  jdolecek 
    995  1.123  jdolecek 	/*
    996  1.123  jdolecek 	 * Call exec hook. Emulation code may NOT store reference to anything
    997  1.123  jdolecek 	 * from &pack.
    998  1.123  jdolecek 	 */
    999  1.244       dsl         if (pack.ep_esch->es_emul->e_proc_exec)
   1000  1.244       dsl                 (*pack.ep_esch->es_emul->e_proc_exec)(p, &pack);
   1001  1.122  jdolecek 
   1002  1.122  jdolecek 	/* update p_emul, the old value is no longer needed */
   1003  1.244       dsl 	p->p_emul = pack.ep_esch->es_emul;
   1004  1.148   thorpej 
   1005  1.148   thorpej 	/* ...and the same for p_execsw */
   1006  1.244       dsl 	p->p_execsw = pack.ep_esch;
   1007  1.148   thorpej 
   1008  1.133   mycroft #ifdef __HAVE_SYSCALL_INTERN
   1009  1.133   mycroft 	(*p->p_emul->e_syscall_intern)(p);
   1010  1.133   mycroft #endif
   1011  1.247        ad 	ktremul();
   1012   1.85   mycroft 
   1013  1.252        ad 	/* Allow new references from the debugger/procfs. */
   1014  1.252        ad 	rw_exit(&p->p_reflock);
   1015  1.147  jdolecek #ifdef LKM
   1016  1.237        ad 	rw_exit(&exec_lock);
   1017  1.147  jdolecek #endif
   1018  1.162      manu 
   1019  1.271        ad 	mutex_enter(proc_lock);
   1020  1.237        ad 
   1021  1.237        ad 	if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) {
   1022  1.237        ad 		KSI_INIT_EMPTY(&ksi);
   1023  1.237        ad 		ksi.ksi_signo = SIGTRAP;
   1024  1.237        ad 		ksi.ksi_lid = l->l_lid;
   1025  1.237        ad 		kpsignal(p, &ksi, NULL);
   1026  1.237        ad 	}
   1027  1.162      manu 
   1028  1.237        ad 	if (p->p_sflag & PS_STOPEXEC) {
   1029  1.237        ad 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
   1030  1.175       dsl 		p->p_pptr->p_nstopchild++;
   1031  1.237        ad 		p->p_pptr->p_waited = 0;
   1032  1.237        ad 		mutex_enter(&p->p_smutex);
   1033  1.237        ad 		ksiginfo_queue_init(&kq);
   1034  1.237        ad 		sigclearall(p, &contsigmask, &kq);
   1035  1.237        ad 		lwp_lock(l);
   1036  1.237        ad 		l->l_stat = LSSTOP;
   1037  1.162      manu 		p->p_stat = SSTOP;
   1038  1.164   thorpej 		p->p_nrlwps--;
   1039  1.237        ad 		mutex_exit(&p->p_smutex);
   1040  1.271        ad 		mutex_exit(proc_lock);
   1041  1.245      yamt 		mi_switch(l);
   1042  1.237        ad 		ksiginfo_queue_drain(&kq);
   1043  1.237        ad 		KERNEL_LOCK(l->l_biglocks, l);
   1044  1.237        ad 	} else {
   1045  1.271        ad 		mutex_exit(proc_lock);
   1046  1.162      manu 	}
   1047  1.162      manu 
   1048  1.260  christos 	PNBUF_PUT(pathbuf);
   1049   1.85   mycroft 	return (EJUSTRETURN);
   1050   1.55       cgd 
   1051  1.138     lukem  bad:
   1052   1.55       cgd 	/* free the vmspace-creation commands, and release their references */
   1053   1.55       cgd 	kill_vmcmds(&pack.ep_vmcmds);
   1054   1.55       cgd 	/* kill any opened file descriptor, if necessary */
   1055   1.55       cgd 	if (pack.ep_flags & EXEC_HASFD) {
   1056   1.55       cgd 		pack.ep_flags &= ~EXEC_HASFD;
   1057  1.270        ad 		fd_close(pack.ep_fd);
   1058   1.55       cgd 	}
   1059   1.55       cgd 	/* close and put the exec'd file */
   1060   1.99  wrstuden 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   1061  1.254     pooka 	VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred);
   1062   1.99  wrstuden 	vput(pack.ep_vp);
   1063  1.120   thorpej 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
   1064  1.196      yamt 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
   1065   1.55       cgd 
   1066  1.138     lukem  freehdr:
   1067  1.265      yamt 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
   1068  1.244       dsl 	if (pack.ep_emul_root != NULL)
   1069  1.244       dsl 		vrele(pack.ep_emul_root);
   1070  1.244       dsl 	if (pack.ep_interp != NULL)
   1071  1.244       dsl 		vrele(pack.ep_interp);
   1072  1.200      elad 
   1073  1.200      elad  clrflg:
   1074  1.260  christos 	PNBUF_PUT(pathbuf);
   1075  1.252        ad 	rw_exit(&p->p_reflock);
   1076  1.147  jdolecek #ifdef LKM
   1077  1.237        ad 	rw_exit(&exec_lock);
   1078  1.147  jdolecek #endif
   1079  1.130  jdolecek 
   1080   1.55       cgd 	return error;
   1081   1.55       cgd 
   1082  1.138     lukem  exec_abort:
   1083  1.260  christos 	PNBUF_PUT(pathbuf);
   1084  1.252        ad 	rw_exit(&p->p_reflock);
   1085  1.147  jdolecek #ifdef LKM
   1086  1.237        ad 	rw_exit(&exec_lock);
   1087  1.147  jdolecek #endif
   1088  1.130  jdolecek 
   1089   1.55       cgd 	/*
   1090   1.55       cgd 	 * the old process doesn't exist anymore.  exit gracefully.
   1091   1.55       cgd 	 * get rid of the (new) address space we have created, if any, get rid
   1092   1.55       cgd 	 * of our namei data and vnode, and exit noting failure
   1093   1.55       cgd 	 */
   1094   1.88       mrg 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
   1095   1.88       mrg 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
   1096   1.73   mycroft 	if (pack.ep_emul_arg)
   1097  1.124  jdolecek 		FREE(pack.ep_emul_arg, M_TEMP);
   1098  1.120   thorpej 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
   1099  1.196      yamt 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
   1100  1.265      yamt 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
   1101  1.244       dsl 	if (pack.ep_emul_root != NULL)
   1102  1.244       dsl 		vrele(pack.ep_emul_root);
   1103  1.244       dsl 	if (pack.ep_interp != NULL)
   1104  1.244       dsl 		vrele(pack.ep_interp);
   1105  1.237        ad 
   1106  1.252        ad 	/* Acquire the sched-state mutex (exit1() will release it). */
   1107  1.264        ad 	KERNEL_LOCK(1, NULL);	/* XXXSMP */
   1108  1.237        ad 	mutex_enter(&p->p_smutex);
   1109  1.164   thorpej 	exit1(l, W_EXITCODE(error, SIGABRT));
   1110   1.55       cgd 
   1111   1.55       cgd 	/* NOTREACHED */
   1112   1.55       cgd 	return 0;
   1113   1.67  christos }
   1114   1.67  christos 
   1115   1.67  christos 
   1116  1.144  christos int
   1117  1.231      yamt copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo,
   1118  1.231      yamt     char **stackp, void *argp)
   1119   1.67  christos {
   1120  1.138     lukem 	char	**cpp, *dp, *sp;
   1121  1.138     lukem 	size_t	len;
   1122  1.138     lukem 	void	*nullp;
   1123  1.138     lukem 	long	argc, envc;
   1124  1.144  christos 	int	error;
   1125  1.138     lukem 
   1126  1.144  christos 	cpp = (char **)*stackp;
   1127  1.138     lukem 	nullp = NULL;
   1128  1.138     lukem 	argc = arginfo->ps_nargvstr;
   1129  1.138     lukem 	envc = arginfo->ps_nenvstr;
   1130  1.144  christos 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
   1131  1.144  christos 		return error;
   1132   1.67  christos 
   1133  1.244       dsl 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen);
   1134   1.67  christos 	sp = argp;
   1135   1.67  christos 
   1136   1.67  christos 	/* XXX don't copy them out, remap them! */
   1137   1.69   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
   1138   1.67  christos 
   1139   1.67  christos 	for (; --argc >= 0; sp += len, dp += len)
   1140  1.144  christos 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
   1141  1.144  christos 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
   1142  1.144  christos 			return error;
   1143   1.67  christos 
   1144  1.144  christos 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
   1145  1.144  christos 		return error;
   1146   1.67  christos 
   1147   1.69   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
   1148   1.67  christos 
   1149   1.67  christos 	for (; --envc >= 0; sp += len, dp += len)
   1150  1.144  christos 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
   1151  1.144  christos 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
   1152  1.144  christos 			return error;
   1153   1.67  christos 
   1154  1.144  christos 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
   1155  1.144  christos 		return error;
   1156   1.67  christos 
   1157  1.144  christos 	*stackp = (char *)cpp;
   1158  1.144  christos 	return 0;
   1159   1.55       cgd }
   1160  1.130  jdolecek 
   1161  1.130  jdolecek #ifdef LKM
   1162  1.130  jdolecek /*
   1163  1.130  jdolecek  * Find an emulation of given name in list of emulations.
   1164  1.151  jdolecek  * Needs to be called with the exec_lock held.
   1165  1.130  jdolecek  */
   1166  1.151  jdolecek const struct emul *
   1167  1.138     lukem emul_search(const char *name)
   1168  1.130  jdolecek {
   1169  1.130  jdolecek 	struct emul_entry *it;
   1170  1.130  jdolecek 
   1171  1.130  jdolecek 	LIST_FOREACH(it, &el_head, el_list) {
   1172  1.130  jdolecek 		if (strcmp(name, it->el_emul->e_name) == 0)
   1173  1.130  jdolecek 			return it->el_emul;
   1174  1.130  jdolecek 	}
   1175  1.130  jdolecek 
   1176  1.130  jdolecek 	return NULL;
   1177  1.130  jdolecek }
   1178  1.130  jdolecek 
   1179  1.130  jdolecek /*
   1180  1.130  jdolecek  * Add an emulation to list, if it's not there already.
   1181  1.130  jdolecek  */
   1182  1.130  jdolecek int
   1183  1.138     lukem emul_register(const struct emul *emul, int ro_entry)
   1184  1.130  jdolecek {
   1185  1.138     lukem 	struct emul_entry	*ee;
   1186  1.138     lukem 	int			error;
   1187  1.130  jdolecek 
   1188  1.138     lukem 	error = 0;
   1189  1.237        ad 	rw_enter(&exec_lock, RW_WRITER);
   1190  1.130  jdolecek 
   1191  1.130  jdolecek 	if (emul_search(emul->e_name)) {
   1192  1.130  jdolecek 		error = EEXIST;
   1193  1.130  jdolecek 		goto out;
   1194  1.130  jdolecek 	}
   1195  1.130  jdolecek 
   1196  1.266      yamt 	ee = kmem_alloc(sizeof(*ee), KM_SLEEP);
   1197  1.130  jdolecek 	ee->el_emul = emul;
   1198  1.130  jdolecek 	ee->ro_entry = ro_entry;
   1199  1.130  jdolecek 	LIST_INSERT_HEAD(&el_head, ee, el_list);
   1200  1.130  jdolecek 
   1201  1.138     lukem  out:
   1202  1.237        ad 	rw_exit(&exec_lock);
   1203  1.130  jdolecek 	return error;
   1204  1.130  jdolecek }
   1205  1.130  jdolecek 
   1206  1.130  jdolecek /*
   1207  1.130  jdolecek  * Remove emulation with name 'name' from list of supported emulations.
   1208  1.130  jdolecek  */
   1209  1.130  jdolecek int
   1210  1.138     lukem emul_unregister(const char *name)
   1211  1.130  jdolecek {
   1212  1.130  jdolecek 	const struct proclist_desc *pd;
   1213  1.138     lukem 	struct emul_entry	*it;
   1214  1.138     lukem 	int			i, error;
   1215  1.138     lukem 	struct proc		*ptmp;
   1216  1.130  jdolecek 
   1217  1.138     lukem 	error = 0;
   1218  1.237        ad 	rw_enter(&exec_lock, RW_WRITER);
   1219  1.130  jdolecek 
   1220  1.130  jdolecek 	LIST_FOREACH(it, &el_head, el_list) {
   1221  1.130  jdolecek 		if (strcmp(it->el_emul->e_name, name) == 0)
   1222  1.130  jdolecek 			break;
   1223  1.130  jdolecek 	}
   1224  1.130  jdolecek 
   1225  1.130  jdolecek 	if (!it) {
   1226  1.130  jdolecek 		error = ENOENT;
   1227  1.130  jdolecek 		goto out;
   1228  1.130  jdolecek 	}
   1229  1.130  jdolecek 
   1230  1.130  jdolecek 	if (it->ro_entry) {
   1231  1.130  jdolecek 		error = EBUSY;
   1232  1.130  jdolecek 		goto out;
   1233  1.130  jdolecek 	}
   1234  1.130  jdolecek 
   1235  1.130  jdolecek 	/* test if any execw[] entry is still using this */
   1236  1.132  jdolecek 	for(i=0; i < nexecs; i++) {
   1237  1.130  jdolecek 		if (execsw[i]->es_emul == it->el_emul) {
   1238  1.130  jdolecek 			error = EBUSY;
   1239  1.130  jdolecek 			goto out;
   1240  1.130  jdolecek 		}
   1241  1.130  jdolecek 	}
   1242  1.130  jdolecek 
   1243  1.130  jdolecek 	/*
   1244  1.130  jdolecek 	 * Test if any process is running under this emulation - since
   1245  1.130  jdolecek 	 * emul_unregister() is running quite sendomly, it's better
   1246  1.130  jdolecek 	 * to do expensive check here than to use any locking.
   1247  1.130  jdolecek 	 */
   1248  1.271        ad 	mutex_enter(proc_lock);
   1249  1.130  jdolecek 	for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
   1250  1.191      yamt 		PROCLIST_FOREACH(ptmp, pd->pd_list) {
   1251  1.130  jdolecek 			if (ptmp->p_emul == it->el_emul) {
   1252  1.130  jdolecek 				error = EBUSY;
   1253  1.130  jdolecek 				break;
   1254  1.130  jdolecek 			}
   1255  1.130  jdolecek 		}
   1256  1.130  jdolecek 	}
   1257  1.271        ad 	mutex_exit(proc_lock);
   1258  1.183  junyoung 
   1259  1.130  jdolecek 	if (error)
   1260  1.130  jdolecek 		goto out;
   1261  1.130  jdolecek 
   1262  1.183  junyoung 
   1263  1.130  jdolecek 	/* entry is not used, remove it */
   1264  1.130  jdolecek 	LIST_REMOVE(it, el_list);
   1265  1.266      yamt 	kmem_free(it, sizeof(*it));
   1266  1.130  jdolecek 
   1267  1.138     lukem  out:
   1268  1.237        ad 	rw_exit(&exec_lock);
   1269  1.130  jdolecek 	return error;
   1270  1.130  jdolecek }
   1271  1.130  jdolecek 
   1272  1.130  jdolecek /*
   1273  1.130  jdolecek  * Add execsw[] entry.
   1274  1.130  jdolecek  */
   1275  1.130  jdolecek int
   1276  1.138     lukem exec_add(struct execsw *esp, const char *e_name)
   1277  1.130  jdolecek {
   1278  1.138     lukem 	struct exec_entry	*it;
   1279  1.138     lukem 	int			error;
   1280  1.130  jdolecek 
   1281  1.138     lukem 	error = 0;
   1282  1.237        ad 	rw_enter(&exec_lock, RW_WRITER);
   1283  1.130  jdolecek 
   1284  1.130  jdolecek 	if (!esp->es_emul) {
   1285  1.130  jdolecek 		esp->es_emul = emul_search(e_name);
   1286  1.130  jdolecek 		if (!esp->es_emul) {
   1287  1.130  jdolecek 			error = ENOENT;
   1288  1.130  jdolecek 			goto out;
   1289  1.130  jdolecek 		}
   1290  1.130  jdolecek 	}
   1291  1.130  jdolecek 
   1292  1.130  jdolecek 	LIST_FOREACH(it, &ex_head, ex_list) {
   1293  1.130  jdolecek 		/* assume tuple (makecmds, probe_func, emulation) is unique */
   1294  1.182  junyoung 		if (it->es->es_makecmds == esp->es_makecmds
   1295  1.130  jdolecek 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
   1296  1.130  jdolecek 		    && it->es->es_emul == esp->es_emul) {
   1297  1.130  jdolecek 			error = EEXIST;
   1298  1.130  jdolecek 			goto out;
   1299  1.130  jdolecek 		}
   1300  1.130  jdolecek 	}
   1301  1.130  jdolecek 
   1302  1.130  jdolecek 	/* if we got here, the entry doesn't exist yet */
   1303  1.266      yamt 	it = kmem_alloc(sizeof(*it), KM_SLEEP);
   1304  1.130  jdolecek 	it->es = esp;
   1305  1.130  jdolecek 	LIST_INSERT_HEAD(&ex_head, it, ex_list);
   1306  1.130  jdolecek 
   1307  1.130  jdolecek 	/* update execsw[] */
   1308  1.130  jdolecek 	exec_init(0);
   1309  1.130  jdolecek 
   1310  1.138     lukem  out:
   1311  1.237        ad 	rw_exit(&exec_lock);
   1312  1.130  jdolecek 	return error;
   1313  1.130  jdolecek }
   1314  1.130  jdolecek 
   1315  1.130  jdolecek /*
   1316  1.130  jdolecek  * Remove execsw[] entry.
   1317  1.130  jdolecek  */
   1318  1.130  jdolecek int
   1319  1.138     lukem exec_remove(const struct execsw *esp)
   1320  1.130  jdolecek {
   1321  1.138     lukem 	struct exec_entry	*it;
   1322  1.138     lukem 	int			error;
   1323  1.130  jdolecek 
   1324  1.138     lukem 	error = 0;
   1325  1.237        ad 	rw_enter(&exec_lock, RW_WRITER);
   1326  1.130  jdolecek 
   1327  1.130  jdolecek 	LIST_FOREACH(it, &ex_head, ex_list) {
   1328  1.130  jdolecek 		/* assume tuple (makecmds, probe_func, emulation) is unique */
   1329  1.182  junyoung 		if (it->es->es_makecmds == esp->es_makecmds
   1330  1.130  jdolecek 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
   1331  1.130  jdolecek 		    && it->es->es_emul == esp->es_emul)
   1332  1.130  jdolecek 			break;
   1333  1.130  jdolecek 	}
   1334  1.130  jdolecek 	if (!it) {
   1335  1.130  jdolecek 		error = ENOENT;
   1336  1.130  jdolecek 		goto out;
   1337  1.130  jdolecek 	}
   1338  1.130  jdolecek 
   1339  1.130  jdolecek 	/* remove item from list and free resources */
   1340  1.130  jdolecek 	LIST_REMOVE(it, ex_list);
   1341  1.266      yamt 	kmem_free(it, sizeof(*it));
   1342  1.130  jdolecek 
   1343  1.130  jdolecek 	/* update execsw[] */
   1344  1.130  jdolecek 	exec_init(0);
   1345  1.130  jdolecek 
   1346  1.138     lukem  out:
   1347  1.237        ad 	rw_exit(&exec_lock);
   1348  1.130  jdolecek 	return error;
   1349  1.130  jdolecek }
   1350  1.130  jdolecek 
   1351  1.130  jdolecek static void
   1352  1.138     lukem link_es(struct execsw_entry **listp, const struct execsw *esp)
   1353  1.130  jdolecek {
   1354  1.130  jdolecek 	struct execsw_entry *et, *e1;
   1355  1.130  jdolecek 
   1356  1.215      matt 	et = (struct execsw_entry *) malloc(sizeof(struct execsw_entry),
   1357  1.130  jdolecek 			M_TEMP, M_WAITOK);
   1358  1.130  jdolecek 	et->next = NULL;
   1359  1.130  jdolecek 	et->es = esp;
   1360  1.130  jdolecek 	if (*listp == NULL) {
   1361  1.130  jdolecek 		*listp = et;
   1362  1.130  jdolecek 		return;
   1363  1.130  jdolecek 	}
   1364  1.130  jdolecek 
   1365  1.130  jdolecek 	switch(et->es->es_prio) {
   1366  1.130  jdolecek 	case EXECSW_PRIO_FIRST:
   1367  1.130  jdolecek 		/* put new entry as the first */
   1368  1.130  jdolecek 		et->next = *listp;
   1369  1.130  jdolecek 		*listp = et;
   1370  1.130  jdolecek 		break;
   1371  1.130  jdolecek 	case EXECSW_PRIO_ANY:
   1372  1.130  jdolecek 		/* put new entry after all *_FIRST and *_ANY entries */
   1373  1.130  jdolecek 		for(e1 = *listp; e1->next
   1374  1.130  jdolecek 			&& e1->next->es->es_prio != EXECSW_PRIO_LAST;
   1375  1.130  jdolecek 			e1 = e1->next);
   1376  1.130  jdolecek 		et->next = e1->next;
   1377  1.130  jdolecek 		e1->next = et;
   1378  1.130  jdolecek 		break;
   1379  1.130  jdolecek 	case EXECSW_PRIO_LAST:
   1380  1.130  jdolecek 		/* put new entry as the last one */
   1381  1.130  jdolecek 		for(e1 = *listp; e1->next; e1 = e1->next);
   1382  1.130  jdolecek 		e1->next = et;
   1383  1.130  jdolecek 		break;
   1384  1.130  jdolecek 	default:
   1385  1.130  jdolecek #ifdef DIAGNOSTIC
   1386  1.157    provos 		panic("execw[] entry with unknown priority %d found",
   1387  1.130  jdolecek 			et->es->es_prio);
   1388  1.217      elad #else
   1389  1.217      elad 		free(et, M_TEMP);
   1390  1.130  jdolecek #endif
   1391  1.130  jdolecek 		break;
   1392  1.130  jdolecek 	}
   1393  1.130  jdolecek }
   1394  1.130  jdolecek 
   1395  1.130  jdolecek /*
   1396  1.130  jdolecek  * Initialize exec structures. If init_boot is true, also does necessary
   1397  1.130  jdolecek  * one-time initialization (it's called from main() that way).
   1398  1.147  jdolecek  * Once system is multiuser, this should be called with exec_lock held,
   1399  1.130  jdolecek  * i.e. via exec_{add|remove}().
   1400  1.130  jdolecek  */
   1401  1.130  jdolecek int
   1402  1.138     lukem exec_init(int init_boot)
   1403  1.130  jdolecek {
   1404  1.138     lukem 	const struct execsw	**new_es, * const *old_es;
   1405  1.138     lukem 	struct execsw_entry	*list, *e1;
   1406  1.138     lukem 	struct exec_entry	*e2;
   1407  1.138     lukem 	int			i, es_sz;
   1408  1.130  jdolecek 
   1409  1.130  jdolecek 	if (init_boot) {
   1410  1.130  jdolecek 		/* do one-time initializations */
   1411  1.237        ad 		rw_init(&exec_lock);
   1412  1.259        ad 		mutex_init(&sigobject_lock, MUTEX_DEFAULT, IPL_NONE);
   1413  1.130  jdolecek 
   1414  1.130  jdolecek 		/* register compiled-in emulations */
   1415  1.130  jdolecek 		for(i=0; i < nexecs_builtin; i++) {
   1416  1.130  jdolecek 			if (execsw_builtin[i].es_emul)
   1417  1.130  jdolecek 				emul_register(execsw_builtin[i].es_emul, 1);
   1418  1.130  jdolecek 		}
   1419  1.130  jdolecek #ifdef DIAGNOSTIC
   1420  1.130  jdolecek 		if (i == 0)
   1421  1.157    provos 			panic("no emulations found in execsw_builtin[]");
   1422  1.130  jdolecek #endif
   1423  1.130  jdolecek 	}
   1424  1.130  jdolecek 
   1425  1.130  jdolecek 	/*
   1426  1.130  jdolecek 	 * Build execsw[] array from builtin entries and entries added
   1427  1.130  jdolecek 	 * at runtime.
   1428  1.130  jdolecek 	 */
   1429  1.130  jdolecek 	list = NULL;
   1430  1.130  jdolecek 	for(i=0; i < nexecs_builtin; i++)
   1431  1.130  jdolecek 		link_es(&list, &execsw_builtin[i]);
   1432  1.130  jdolecek 
   1433  1.130  jdolecek 	/* Add dynamically loaded entries */
   1434  1.130  jdolecek 	es_sz = nexecs_builtin;
   1435  1.130  jdolecek 	LIST_FOREACH(e2, &ex_head, ex_list) {
   1436  1.130  jdolecek 		link_es(&list, e2->es);
   1437  1.130  jdolecek 		es_sz++;
   1438  1.130  jdolecek 	}
   1439  1.183  junyoung 
   1440  1.130  jdolecek 	/*
   1441  1.130  jdolecek 	 * Now that we have sorted all execw entries, create new execsw[]
   1442  1.130  jdolecek 	 * and free no longer needed memory in the process.
   1443  1.130  jdolecek 	 */
   1444  1.266      yamt 	new_es = kmem_alloc(es_sz * sizeof(struct execsw *), KM_SLEEP);
   1445  1.130  jdolecek 	for(i=0; list; i++) {
   1446  1.130  jdolecek 		new_es[i] = list->es;
   1447  1.130  jdolecek 		e1 = list->next;
   1448  1.215      matt 		free(list, M_TEMP);
   1449  1.130  jdolecek 		list = e1;
   1450  1.130  jdolecek 	}
   1451  1.130  jdolecek 
   1452  1.130  jdolecek 	/*
   1453  1.130  jdolecek 	 * New execsw[] array built, now replace old execsw[] and free
   1454  1.130  jdolecek 	 * used memory.
   1455  1.130  jdolecek 	 */
   1456  1.130  jdolecek 	old_es = execsw;
   1457  1.266      yamt 	if (old_es)
   1458  1.266      yamt 		/*XXXUNCONST*/
   1459  1.266      yamt 		kmem_free(__UNCONST(old_es), nexecs * sizeof(struct execsw *));
   1460  1.130  jdolecek 	execsw = new_es;
   1461  1.130  jdolecek 	nexecs = es_sz;
   1462  1.130  jdolecek 
   1463  1.130  jdolecek 	/*
   1464  1.130  jdolecek 	 * Figure out the maximum size of an exec header.
   1465  1.183  junyoung 	 */
   1466  1.130  jdolecek 	exec_maxhdrsz = 0;
   1467  1.130  jdolecek 	for (i = 0; i < nexecs; i++) {
   1468  1.130  jdolecek 		if (execsw[i]->es_hdrsz > exec_maxhdrsz)
   1469  1.130  jdolecek 			exec_maxhdrsz = execsw[i]->es_hdrsz;
   1470  1.130  jdolecek 	}
   1471  1.130  jdolecek 
   1472  1.130  jdolecek 	return 0;
   1473  1.130  jdolecek }
   1474  1.130  jdolecek #endif
   1475  1.130  jdolecek 
   1476  1.130  jdolecek #ifndef LKM
   1477  1.130  jdolecek /*
   1478  1.130  jdolecek  * Simplified exec_init() for kernels without LKMs. Only initialize
   1479  1.130  jdolecek  * exec_maxhdrsz and execsw[].
   1480  1.130  jdolecek  */
   1481  1.130  jdolecek int
   1482  1.138     lukem exec_init(int init_boot)
   1483  1.130  jdolecek {
   1484  1.130  jdolecek 	int i;
   1485  1.130  jdolecek 
   1486  1.130  jdolecek #ifdef DIAGNOSTIC
   1487  1.130  jdolecek 	if (!init_boot)
   1488  1.130  jdolecek 		panic("exec_init(): called with init_boot == 0");
   1489  1.130  jdolecek #endif
   1490  1.130  jdolecek 
   1491  1.130  jdolecek 	/* do one-time initializations */
   1492  1.130  jdolecek 	nexecs = nexecs_builtin;
   1493  1.266      yamt 	execsw = kmem_alloc(nexecs * sizeof(struct execsw *), KM_SLEEP);
   1494  1.130  jdolecek 
   1495  1.130  jdolecek 	/*
   1496  1.130  jdolecek 	 * Fill in execsw[] and figure out the maximum size of an exec header.
   1497  1.130  jdolecek 	 */
   1498  1.130  jdolecek 	exec_maxhdrsz = 0;
   1499  1.130  jdolecek 	for(i=0; i < nexecs; i++) {
   1500  1.130  jdolecek 		execsw[i] = &execsw_builtin[i];
   1501  1.130  jdolecek 		if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
   1502  1.130  jdolecek 			exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
   1503  1.130  jdolecek 	}
   1504  1.130  jdolecek 
   1505  1.130  jdolecek 	return 0;
   1506  1.130  jdolecek 
   1507  1.130  jdolecek }
   1508  1.130  jdolecek #endif /* !LKM */
   1509  1.171       chs 
   1510  1.171       chs static int
   1511  1.171       chs exec_sigcode_map(struct proc *p, const struct emul *e)
   1512  1.171       chs {
   1513  1.171       chs 	vaddr_t va;
   1514  1.171       chs 	vsize_t sz;
   1515  1.171       chs 	int error;
   1516  1.171       chs 	struct uvm_object *uobj;
   1517  1.171       chs 
   1518  1.184  drochner 	sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
   1519  1.184  drochner 
   1520  1.184  drochner 	if (e->e_sigobject == NULL || sz == 0) {
   1521  1.171       chs 		return 0;
   1522  1.171       chs 	}
   1523  1.171       chs 
   1524  1.171       chs 	/*
   1525  1.171       chs 	 * If we don't have a sigobject for this emulation, create one.
   1526  1.171       chs 	 *
   1527  1.171       chs 	 * sigobject is an anonymous memory object (just like SYSV shared
   1528  1.171       chs 	 * memory) that we keep a permanent reference to and that we map
   1529  1.171       chs 	 * in all processes that need this sigcode. The creation is simple,
   1530  1.171       chs 	 * we create an object, add a permanent reference to it, map it in
   1531  1.171       chs 	 * kernel space, copy out the sigcode to it and unmap it.
   1532  1.189  jdolecek 	 * We map it with PROT_READ|PROT_EXEC into the process just
   1533  1.189  jdolecek 	 * the way sys_mmap() would map it.
   1534  1.171       chs 	 */
   1535  1.171       chs 
   1536  1.171       chs 	uobj = *e->e_sigobject;
   1537  1.171       chs 	if (uobj == NULL) {
   1538  1.259        ad 		mutex_enter(&sigobject_lock);
   1539  1.259        ad 		if ((uobj = *e->e_sigobject) == NULL) {
   1540  1.259        ad 			uobj = uao_create(sz, 0);
   1541  1.259        ad 			(*uobj->pgops->pgo_reference)(uobj);
   1542  1.259        ad 			va = vm_map_min(kernel_map);
   1543  1.259        ad 			if ((error = uvm_map(kernel_map, &va, round_page(sz),
   1544  1.259        ad 			    uobj, 0, 0,
   1545  1.259        ad 			    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
   1546  1.259        ad 			    UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
   1547  1.259        ad 				printf("kernel mapping failed %d\n", error);
   1548  1.259        ad 				(*uobj->pgops->pgo_detach)(uobj);
   1549  1.259        ad 				mutex_exit(&sigobject_lock);
   1550  1.259        ad 				return (error);
   1551  1.259        ad 			}
   1552  1.259        ad 			memcpy((void *)va, e->e_sigcode, sz);
   1553  1.171       chs #ifdef PMAP_NEED_PROCWR
   1554  1.259        ad 			pmap_procwr(&proc0, va, sz);
   1555  1.171       chs #endif
   1556  1.259        ad 			uvm_unmap(kernel_map, va, va + round_page(sz));
   1557  1.259        ad 			*e->e_sigobject = uobj;
   1558  1.259        ad 		}
   1559  1.259        ad 		mutex_exit(&sigobject_lock);
   1560  1.171       chs 	}
   1561  1.171       chs 
   1562  1.172     enami 	/* Just a hint to uvm_map where to put it. */
   1563  1.195      fvdl 	va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr,
   1564  1.195      fvdl 	    round_page(sz));
   1565  1.187       chs 
   1566  1.187       chs #ifdef __alpha__
   1567  1.187       chs 	/*
   1568  1.187       chs 	 * Tru64 puts /sbin/loader at the end of user virtual memory,
   1569  1.187       chs 	 * which causes the above calculation to put the sigcode at
   1570  1.187       chs 	 * an invalid address.  Put it just below the text instead.
   1571  1.187       chs 	 */
   1572  1.193       jmc 	if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) {
   1573  1.187       chs 		va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz);
   1574  1.187       chs 	}
   1575  1.187       chs #endif
   1576  1.187       chs 
   1577  1.171       chs 	(*uobj->pgops->pgo_reference)(uobj);
   1578  1.171       chs 	error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
   1579  1.171       chs 			uobj, 0, 0,
   1580  1.171       chs 			UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
   1581  1.171       chs 				    UVM_ADV_RANDOM, 0));
   1582  1.171       chs 	if (error) {
   1583  1.171       chs 		(*uobj->pgops->pgo_detach)(uobj);
   1584  1.171       chs 		return (error);
   1585  1.171       chs 	}
   1586  1.171       chs 	p->p_sigctx.ps_sigcode = (void *)va;
   1587  1.171       chs 	return (0);
   1588  1.171       chs }
   1589