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