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