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