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