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