kern_exec.c revision 1.90 1 1.90 fvdl /* $NetBSD: kern_exec.c,v 1.90 1998/03/01 02:22:28 fvdl 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.89 mrg
35 1.89 mrg #include "opt_uvm.h"
36 1.55 cgd
37 1.55 cgd #include <sys/param.h>
38 1.55 cgd #include <sys/systm.h>
39 1.55 cgd #include <sys/filedesc.h>
40 1.55 cgd #include <sys/kernel.h>
41 1.55 cgd #include <sys/proc.h>
42 1.55 cgd #include <sys/mount.h>
43 1.55 cgd #include <sys/malloc.h>
44 1.55 cgd #include <sys/namei.h>
45 1.55 cgd #include <sys/vnode.h>
46 1.55 cgd #include <sys/file.h>
47 1.55 cgd #include <sys/acct.h>
48 1.55 cgd #include <sys/exec.h>
49 1.55 cgd #include <sys/ktrace.h>
50 1.55 cgd #include <sys/resourcevar.h>
51 1.55 cgd #include <sys/wait.h>
52 1.55 cgd #include <sys/mman.h>
53 1.55 cgd #include <sys/signalvar.h>
54 1.55 cgd #include <sys/stat.h>
55 1.55 cgd
56 1.56 cgd #include <sys/syscallargs.h>
57 1.56 cgd
58 1.55 cgd #include <vm/vm.h>
59 1.55 cgd #include <vm/vm_kern.h>
60 1.55 cgd
61 1.88 mrg #if defined(UVM)
62 1.88 mrg #include <uvm/uvm_extern.h>
63 1.88 mrg #endif
64 1.88 mrg
65 1.55 cgd #include <machine/cpu.h>
66 1.55 cgd #include <machine/reg.h>
67 1.55 cgd
68 1.55 cgd /*
69 1.55 cgd * check exec:
70 1.55 cgd * given an "executable" described in the exec package's namei info,
71 1.55 cgd * see what we can do with it.
72 1.55 cgd *
73 1.55 cgd * ON ENTRY:
74 1.55 cgd * exec package with appropriate namei info
75 1.55 cgd * proc pointer of exec'ing proc
76 1.55 cgd * NO SELF-LOCKED VNODES
77 1.55 cgd *
78 1.55 cgd * ON EXIT:
79 1.55 cgd * error: nothing held, etc. exec header still allocated.
80 1.77 cgd * ok: filled exec package, executable's vnode (unlocked).
81 1.55 cgd *
82 1.55 cgd * EXEC SWITCH ENTRY:
83 1.55 cgd * Locked vnode to check, exec package, proc.
84 1.55 cgd *
85 1.55 cgd * EXEC SWITCH EXIT:
86 1.77 cgd * ok: return 0, filled exec package, executable's vnode (unlocked).
87 1.55 cgd * error: destructive:
88 1.55 cgd * everything deallocated execept exec header.
89 1.76 cgd * non-destructive:
90 1.77 cgd * error code, executable's vnode (unlocked),
91 1.76 cgd * exec header unmodified.
92 1.55 cgd */
93 1.55 cgd int
94 1.55 cgd check_exec(p, epp)
95 1.55 cgd struct proc *p;
96 1.55 cgd struct exec_package *epp;
97 1.55 cgd {
98 1.55 cgd int error, i;
99 1.55 cgd struct vnode *vp;
100 1.55 cgd struct nameidata *ndp;
101 1.55 cgd int resid;
102 1.55 cgd
103 1.55 cgd ndp = epp->ep_ndp;
104 1.55 cgd ndp->ni_cnd.cn_nameiop = LOOKUP;
105 1.55 cgd ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
106 1.55 cgd /* first get the vnode */
107 1.74 christos if ((error = namei(ndp)) != 0)
108 1.55 cgd return error;
109 1.55 cgd epp->ep_vp = vp = ndp->ni_vp;
110 1.55 cgd
111 1.84 mycroft /* check access and type */
112 1.55 cgd if (vp->v_type != VREG) {
113 1.81 kleink error = EACCES;
114 1.55 cgd goto bad1;
115 1.55 cgd }
116 1.84 mycroft if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
117 1.84 mycroft goto bad1;
118 1.55 cgd
119 1.55 cgd /* get attributes */
120 1.74 christos if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
121 1.55 cgd goto bad1;
122 1.55 cgd
123 1.55 cgd /* Check mount point */
124 1.55 cgd if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
125 1.55 cgd error = EACCES;
126 1.55 cgd goto bad1;
127 1.55 cgd }
128 1.55 cgd if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
129 1.83 mycroft epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
130 1.55 cgd
131 1.55 cgd /* try to open it */
132 1.74 christos if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
133 1.55 cgd goto bad1;
134 1.55 cgd
135 1.77 cgd /* unlock vp, since we don't need it locked from here on out. */
136 1.90 fvdl VOP_UNLOCK(vp, 0);
137 1.77 cgd
138 1.55 cgd /* now we have the file, get the exec header */
139 1.74 christos error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
140 1.77 cgd UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
141 1.74 christos if (error)
142 1.55 cgd goto bad2;
143 1.55 cgd epp->ep_hdrvalid = epp->ep_hdrlen - resid;
144 1.55 cgd
145 1.55 cgd /*
146 1.55 cgd * set up the vmcmds for creation of the process
147 1.55 cgd * address space
148 1.55 cgd */
149 1.55 cgd error = ENOEXEC;
150 1.55 cgd for (i = 0; i < nexecs && error != 0; i++) {
151 1.68 cgd int newerror;
152 1.68 cgd
153 1.68 cgd if (execsw[i].es_check == NULL)
154 1.68 cgd continue;
155 1.68 cgd
156 1.68 cgd newerror = (*execsw[i].es_check)(p, epp);
157 1.68 cgd /* make sure the first "interesting" error code is saved. */
158 1.68 cgd if (!newerror || error == ENOEXEC)
159 1.68 cgd error = newerror;
160 1.55 cgd if (epp->ep_flags & EXEC_DESTR && error != 0)
161 1.55 cgd return error;
162 1.55 cgd }
163 1.55 cgd if (!error) {
164 1.55 cgd /* check that entry point is sane */
165 1.55 cgd if (epp->ep_entry > VM_MAXUSER_ADDRESS)
166 1.55 cgd error = ENOEXEC;
167 1.55 cgd
168 1.55 cgd /* check limits */
169 1.55 cgd if ((epp->ep_tsize > MAXTSIZ) ||
170 1.55 cgd (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
171 1.55 cgd error = ENOMEM;
172 1.55 cgd
173 1.55 cgd if (!error)
174 1.55 cgd return (0);
175 1.55 cgd }
176 1.55 cgd
177 1.55 cgd /*
178 1.55 cgd * free any vmspace-creation commands,
179 1.55 cgd * and release their references
180 1.55 cgd */
181 1.55 cgd kill_vmcmds(&epp->ep_vmcmds);
182 1.55 cgd
183 1.55 cgd bad2:
184 1.55 cgd /*
185 1.55 cgd * unlock and close the vnode, restore the old one, free the
186 1.55 cgd * pathname buf, and punt.
187 1.55 cgd */
188 1.77 cgd VOP_CLOSE(vp, FREAD, p->p_ucred, p);
189 1.77 cgd vrele(vp);
190 1.55 cgd FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
191 1.55 cgd return error;
192 1.55 cgd
193 1.55 cgd bad1:
194 1.55 cgd /*
195 1.55 cgd * free the namei pathname buffer, and put the vnode
196 1.55 cgd * (which we don't yet have open).
197 1.55 cgd */
198 1.77 cgd vput(vp); /* was still locked */
199 1.55 cgd FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
200 1.55 cgd return error;
201 1.55 cgd }
202 1.55 cgd
203 1.55 cgd /*
204 1.55 cgd * exec system call
205 1.55 cgd */
206 1.55 cgd /* ARGSUSED */
207 1.75 christos int
208 1.72 mycroft sys_execve(p, v, retval)
209 1.55 cgd register struct proc *p;
210 1.71 thorpej void *v;
211 1.71 thorpej register_t *retval;
212 1.71 thorpej {
213 1.72 mycroft register struct sys_execve_args /* {
214 1.78 cgd syscallarg(const char *) path;
215 1.78 cgd syscallarg(char * const *) argp;
216 1.78 cgd syscallarg(char * const *) envp;
217 1.71 thorpej } */ *uap = v;
218 1.55 cgd int error, i;
219 1.55 cgd struct exec_package pack;
220 1.55 cgd struct nameidata nid;
221 1.55 cgd struct vattr attr;
222 1.55 cgd struct ucred *cred = p->p_ucred;
223 1.55 cgd char *argp;
224 1.78 cgd char * const *cpp;
225 1.78 cgd char *dp, *sp;
226 1.63 cgd long argc, envc;
227 1.64 mycroft size_t len;
228 1.55 cgd char *stack;
229 1.55 cgd struct ps_strings arginfo;
230 1.86 thorpej struct vmspace *vm;
231 1.55 cgd char **tmpfap;
232 1.65 fvdl int szsigcode;
233 1.67 christos extern struct emul emul_netbsd;
234 1.55 cgd
235 1.55 cgd /*
236 1.55 cgd * figure out the maximum size of an exec header, if necessary.
237 1.55 cgd * XXX should be able to keep LKM code from modifying exec switch
238 1.55 cgd * when we're still using it, but...
239 1.55 cgd */
240 1.55 cgd if (exec_maxhdrsz == 0) {
241 1.55 cgd for (i = 0; i < nexecs; i++)
242 1.55 cgd if (execsw[i].es_check != NULL
243 1.55 cgd && execsw[i].es_hdrsz > exec_maxhdrsz)
244 1.55 cgd exec_maxhdrsz = execsw[i].es_hdrsz;
245 1.55 cgd }
246 1.55 cgd
247 1.55 cgd /* init the namei data to point the file user's program name */
248 1.77 cgd /* XXX cgd 960926: why do this here? most will be clobbered. */
249 1.56 cgd NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
250 1.55 cgd
251 1.55 cgd /*
252 1.55 cgd * initialize the fields of the exec package.
253 1.55 cgd */
254 1.56 cgd pack.ep_name = SCARG(uap, path);
255 1.55 cgd MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
256 1.55 cgd pack.ep_hdrlen = exec_maxhdrsz;
257 1.55 cgd pack.ep_hdrvalid = 0;
258 1.55 cgd pack.ep_ndp = &nid;
259 1.67 christos pack.ep_emul_arg = NULL;
260 1.55 cgd pack.ep_vmcmds.evs_cnt = 0;
261 1.55 cgd pack.ep_vmcmds.evs_used = 0;
262 1.55 cgd pack.ep_vap = &attr;
263 1.67 christos pack.ep_emul = &emul_netbsd;
264 1.55 cgd pack.ep_flags = 0;
265 1.55 cgd
266 1.55 cgd /* see if we can run it. */
267 1.74 christos if ((error = check_exec(p, &pack)) != 0)
268 1.55 cgd goto freehdr;
269 1.55 cgd
270 1.55 cgd /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
271 1.55 cgd
272 1.55 cgd /* allocate an argument buffer */
273 1.88 mrg #if defined(UVM)
274 1.88 mrg argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
275 1.88 mrg #else
276 1.55 cgd argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
277 1.88 mrg #endif
278 1.55 cgd #ifdef DIAGNOSTIC
279 1.55 cgd if (argp == (vm_offset_t) 0)
280 1.55 cgd panic("execve: argp == NULL");
281 1.55 cgd #endif
282 1.55 cgd dp = argp;
283 1.55 cgd argc = 0;
284 1.55 cgd
285 1.55 cgd /* copy the fake args list, if there's one, freeing it as we go */
286 1.55 cgd if (pack.ep_flags & EXEC_HASARGL) {
287 1.55 cgd tmpfap = pack.ep_fa;
288 1.55 cgd while (*tmpfap != NULL) {
289 1.55 cgd char *cp;
290 1.55 cgd
291 1.55 cgd cp = *tmpfap;
292 1.55 cgd while (*cp)
293 1.55 cgd *dp++ = *cp++;
294 1.74 christos dp++;
295 1.55 cgd
296 1.55 cgd FREE(*tmpfap, M_EXEC);
297 1.55 cgd tmpfap++; argc++;
298 1.55 cgd }
299 1.55 cgd FREE(pack.ep_fa, M_EXEC);
300 1.55 cgd pack.ep_flags &= ~EXEC_HASARGL;
301 1.55 cgd }
302 1.55 cgd
303 1.55 cgd /* Now get argv & environment */
304 1.56 cgd if (!(cpp = SCARG(uap, argp))) {
305 1.55 cgd error = EINVAL;
306 1.55 cgd goto bad;
307 1.55 cgd }
308 1.55 cgd
309 1.55 cgd if (pack.ep_flags & EXEC_SKIPARG)
310 1.55 cgd cpp++;
311 1.55 cgd
312 1.55 cgd while (1) {
313 1.55 cgd len = argp + ARG_MAX - dp;
314 1.74 christos if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
315 1.55 cgd goto bad;
316 1.55 cgd if (!sp)
317 1.55 cgd break;
318 1.74 christos if ((error = copyinstr(sp, dp, len, &len)) != 0) {
319 1.55 cgd if (error == ENAMETOOLONG)
320 1.55 cgd error = E2BIG;
321 1.55 cgd goto bad;
322 1.55 cgd }
323 1.55 cgd dp += len;
324 1.55 cgd cpp++;
325 1.55 cgd argc++;
326 1.55 cgd }
327 1.55 cgd
328 1.55 cgd envc = 0;
329 1.74 christos /* environment need not be there */
330 1.74 christos if ((cpp = SCARG(uap, envp)) != NULL ) {
331 1.55 cgd while (1) {
332 1.55 cgd len = argp + ARG_MAX - dp;
333 1.74 christos if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
334 1.55 cgd goto bad;
335 1.55 cgd if (!sp)
336 1.55 cgd break;
337 1.74 christos if ((error = copyinstr(sp, dp, len, &len)) != 0) {
338 1.55 cgd if (error == ENAMETOOLONG)
339 1.55 cgd error = E2BIG;
340 1.55 cgd goto bad;
341 1.55 cgd }
342 1.55 cgd dp += len;
343 1.55 cgd cpp++;
344 1.55 cgd envc++;
345 1.55 cgd }
346 1.55 cgd }
347 1.61 mycroft
348 1.61 mycroft dp = (char *) ALIGN(dp);
349 1.55 cgd
350 1.67 christos szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
351 1.65 fvdl
352 1.55 cgd /* Now check if args & environ fit into new stack */
353 1.67 christos len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
354 1.63 cgd sizeof(long) + dp + STACKGAPLEN + szsigcode +
355 1.57 deraadt sizeof(struct ps_strings)) - argp;
356 1.67 christos
357 1.55 cgd len = ALIGN(len); /* make the stack "safely" aligned */
358 1.55 cgd
359 1.55 cgd if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
360 1.55 cgd error = ENOMEM;
361 1.55 cgd goto bad;
362 1.55 cgd }
363 1.55 cgd
364 1.55 cgd /* adjust "active stack depth" for process VSZ */
365 1.55 cgd pack.ep_ssize = len; /* maybe should go elsewhere, but... */
366 1.55 cgd
367 1.86 thorpej /*
368 1.86 thorpej * Do whatever is necessary to prepare the address space
369 1.86 thorpej * for remapping. Note that this might replace the current
370 1.86 thorpej * vmspace with another!
371 1.86 thorpej */
372 1.88 mrg #if defined(UVM)
373 1.88 mrg uvmspace_exec(p);
374 1.88 mrg #else
375 1.86 thorpej vmspace_exec(p);
376 1.88 mrg #endif
377 1.55 cgd
378 1.55 cgd /* Now map address space */
379 1.86 thorpej vm = p->p_vmspace;
380 1.55 cgd vm->vm_taddr = (char *) pack.ep_taddr;
381 1.55 cgd vm->vm_tsize = btoc(pack.ep_tsize);
382 1.55 cgd vm->vm_daddr = (char *) pack.ep_daddr;
383 1.55 cgd vm->vm_dsize = btoc(pack.ep_dsize);
384 1.55 cgd vm->vm_ssize = btoc(pack.ep_ssize);
385 1.55 cgd vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
386 1.55 cgd
387 1.55 cgd /* create the new process's VM space by running the vmcmds */
388 1.55 cgd #ifdef DIAGNOSTIC
389 1.55 cgd if (pack.ep_vmcmds.evs_used == 0)
390 1.55 cgd panic("execve: no vmcmds");
391 1.55 cgd #endif
392 1.55 cgd for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
393 1.55 cgd struct exec_vmcmd *vcp;
394 1.55 cgd
395 1.55 cgd vcp = &pack.ep_vmcmds.evs_cmds[i];
396 1.55 cgd error = (*vcp->ev_proc)(p, vcp);
397 1.55 cgd }
398 1.55 cgd
399 1.55 cgd /* free the vmspace-creation commands, and release their references */
400 1.55 cgd kill_vmcmds(&pack.ep_vmcmds);
401 1.55 cgd
402 1.55 cgd /* if an error happened, deallocate and punt */
403 1.55 cgd if (error)
404 1.55 cgd goto exec_abort;
405 1.55 cgd
406 1.55 cgd /* remember information about the process */
407 1.55 cgd arginfo.ps_nargvstr = argc;
408 1.55 cgd arginfo.ps_nenvstr = envc;
409 1.55 cgd
410 1.67 christos stack = (char *) (USRSTACK - len);
411 1.55 cgd /* Now copy argc, args & environ to new stack */
412 1.67 christos if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
413 1.55 cgd goto exec_abort;
414 1.55 cgd
415 1.55 cgd /* copy out the process's ps_strings structure */
416 1.55 cgd if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
417 1.55 cgd goto exec_abort;
418 1.55 cgd
419 1.55 cgd /* copy out the process's signal trapoline code */
420 1.67 christos if (szsigcode && copyout((char *) pack.ep_emul->e_sigcode,
421 1.67 christos ((char *) PS_STRINGS) - szsigcode,
422 1.67 christos szsigcode))
423 1.55 cgd goto exec_abort;
424 1.55 cgd
425 1.55 cgd fdcloseexec(p); /* handle close on exec */
426 1.55 cgd execsigs(p); /* reset catched signals */
427 1.55 cgd
428 1.55 cgd /* set command name & other accounting info */
429 1.55 cgd len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
430 1.55 cgd bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
431 1.55 cgd p->p_comm[len] = 0;
432 1.55 cgd p->p_acflag &= ~AFORK;
433 1.55 cgd
434 1.55 cgd /* record proc's vnode, for use by procfs and others */
435 1.55 cgd if (p->p_textvp)
436 1.55 cgd vrele(p->p_textvp);
437 1.55 cgd VREF(pack.ep_vp);
438 1.55 cgd p->p_textvp = pack.ep_vp;
439 1.55 cgd
440 1.55 cgd p->p_flag |= P_EXEC;
441 1.55 cgd if (p->p_flag & P_PPWAIT) {
442 1.55 cgd p->p_flag &= ~P_PPWAIT;
443 1.55 cgd wakeup((caddr_t) p->p_pptr);
444 1.55 cgd }
445 1.55 cgd
446 1.55 cgd /*
447 1.55 cgd * deal with set[ug]id.
448 1.55 cgd * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
449 1.55 cgd */
450 1.83 mycroft if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
451 1.83 mycroft || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
452 1.55 cgd p->p_ucred = crcopy(cred);
453 1.55 cgd #ifdef KTRACE
454 1.55 cgd /*
455 1.55 cgd * If process is being ktraced, turn off - unless
456 1.55 cgd * root set it.
457 1.55 cgd */
458 1.55 cgd if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
459 1.55 cgd vrele(p->p_tracep);
460 1.55 cgd p->p_tracep = NULL;
461 1.55 cgd p->p_traceflag = 0;
462 1.55 cgd }
463 1.55 cgd #endif
464 1.83 mycroft if (attr.va_mode & S_ISUID)
465 1.55 cgd p->p_ucred->cr_uid = attr.va_uid;
466 1.83 mycroft if (attr.va_mode & S_ISGID)
467 1.55 cgd p->p_ucred->cr_gid = attr.va_gid;
468 1.55 cgd p->p_flag |= P_SUGID;
469 1.79 mrg } else
470 1.79 mrg p->p_flag &= ~P_SUGID;
471 1.55 cgd p->p_cred->p_svuid = p->p_ucred->cr_uid;
472 1.55 cgd p->p_cred->p_svgid = p->p_ucred->cr_gid;
473 1.55 cgd
474 1.88 mrg #if defined(UVM)
475 1.88 mrg uvm_km_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
476 1.88 mrg #else
477 1.55 cgd kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
478 1.88 mrg #endif
479 1.55 cgd
480 1.55 cgd FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
481 1.55 cgd VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
482 1.77 cgd vrele(pack.ep_vp);
483 1.55 cgd
484 1.55 cgd /* setup new registers and do misc. setup. */
485 1.85 mycroft (*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
486 1.55 cgd
487 1.55 cgd if (p->p_flag & P_TRACED)
488 1.55 cgd psignal(p, SIGTRAP);
489 1.55 cgd
490 1.55 cgd p->p_emul = pack.ep_emul;
491 1.55 cgd FREE(pack.ep_hdr, M_EXEC);
492 1.70 christos
493 1.70 christos #ifdef KTRACE
494 1.70 christos if (KTRPOINT(p, KTR_EMUL))
495 1.70 christos ktremul(p->p_tracep, p->p_emul->e_name);
496 1.70 christos #endif
497 1.85 mycroft
498 1.85 mycroft return (EJUSTRETURN);
499 1.55 cgd
500 1.55 cgd bad:
501 1.55 cgd /* free the vmspace-creation commands, and release their references */
502 1.55 cgd kill_vmcmds(&pack.ep_vmcmds);
503 1.55 cgd /* kill any opened file descriptor, if necessary */
504 1.55 cgd if (pack.ep_flags & EXEC_HASFD) {
505 1.55 cgd pack.ep_flags &= ~EXEC_HASFD;
506 1.66 mycroft (void) fdrelease(p, pack.ep_fd);
507 1.55 cgd }
508 1.55 cgd /* close and put the exec'd file */
509 1.55 cgd VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
510 1.77 cgd vrele(pack.ep_vp);
511 1.55 cgd FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
512 1.88 mrg #if defined(UVM)
513 1.88 mrg uvm_km_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
514 1.88 mrg #else
515 1.55 cgd kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
516 1.88 mrg #endif
517 1.55 cgd
518 1.55 cgd freehdr:
519 1.55 cgd FREE(pack.ep_hdr, M_EXEC);
520 1.55 cgd return error;
521 1.55 cgd
522 1.55 cgd exec_abort:
523 1.55 cgd /*
524 1.55 cgd * the old process doesn't exist anymore. exit gracefully.
525 1.55 cgd * get rid of the (new) address space we have created, if any, get rid
526 1.55 cgd * of our namei data and vnode, and exit noting failure
527 1.55 cgd */
528 1.88 mrg #if defined(UVM)
529 1.88 mrg uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
530 1.88 mrg VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
531 1.88 mrg #else
532 1.55 cgd vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
533 1.55 cgd VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
534 1.88 mrg #endif
535 1.73 mycroft if (pack.ep_emul_arg)
536 1.73 mycroft FREE(pack.ep_emul_arg, M_TEMP);
537 1.55 cgd FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
538 1.55 cgd VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
539 1.77 cgd vrele(pack.ep_vp);
540 1.88 mrg #if defined(UVM)
541 1.88 mrg uvm_km_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
542 1.88 mrg #else
543 1.55 cgd kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
544 1.88 mrg #endif
545 1.55 cgd FREE(pack.ep_hdr, M_EXEC);
546 1.55 cgd exit1(p, W_EXITCODE(0, SIGABRT));
547 1.55 cgd exit1(p, -1);
548 1.55 cgd
549 1.55 cgd /* NOTREACHED */
550 1.55 cgd return 0;
551 1.67 christos }
552 1.67 christos
553 1.67 christos
554 1.67 christos void *
555 1.67 christos copyargs(pack, arginfo, stack, argp)
556 1.67 christos struct exec_package *pack;
557 1.67 christos struct ps_strings *arginfo;
558 1.67 christos void *stack;
559 1.67 christos void *argp;
560 1.67 christos {
561 1.67 christos char **cpp = stack;
562 1.67 christos char *dp, *sp;
563 1.67 christos size_t len;
564 1.67 christos void *nullp = NULL;
565 1.67 christos int argc = arginfo->ps_nargvstr;
566 1.67 christos int envc = arginfo->ps_nenvstr;
567 1.67 christos
568 1.67 christos if (copyout(&argc, cpp++, sizeof(argc)))
569 1.67 christos return NULL;
570 1.67 christos
571 1.67 christos dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
572 1.67 christos sp = argp;
573 1.67 christos
574 1.67 christos /* XXX don't copy them out, remap them! */
575 1.69 mycroft arginfo->ps_argvstr = cpp; /* remember location of argv for later */
576 1.67 christos
577 1.67 christos for (; --argc >= 0; sp += len, dp += len)
578 1.67 christos if (copyout(&dp, cpp++, sizeof(dp)) ||
579 1.67 christos copyoutstr(sp, dp, ARG_MAX, &len))
580 1.67 christos return NULL;
581 1.67 christos
582 1.67 christos if (copyout(&nullp, cpp++, sizeof(nullp)))
583 1.67 christos return NULL;
584 1.67 christos
585 1.69 mycroft arginfo->ps_envstr = cpp; /* remember location of envp for later */
586 1.67 christos
587 1.67 christos for (; --envc >= 0; sp += len, dp += len)
588 1.67 christos if (copyout(&dp, cpp++, sizeof(dp)) ||
589 1.67 christos copyoutstr(sp, dp, ARG_MAX, &len))
590 1.67 christos return NULL;
591 1.67 christos
592 1.67 christos if (copyout(&nullp, cpp++, sizeof(nullp)))
593 1.67 christos return NULL;
594 1.67 christos
595 1.67 christos return cpp;
596 1.55 cgd }
597