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