kern_exec.c revision 1.77 1 /* $NetBSD: kern_exec.c,v 1.77 1996/09/30 23:18:46 cgd 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 <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/filedesc.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/mount.h>
41 #include <sys/malloc.h>
42 #include <sys/namei.h>
43 #include <sys/vnode.h>
44 #include <sys/file.h>
45 #include <sys/acct.h>
46 #include <sys/exec.h>
47 #include <sys/ktrace.h>
48 #include <sys/resourcevar.h>
49 #include <sys/wait.h>
50 #include <sys/mman.h>
51 #include <sys/signalvar.h>
52 #include <sys/stat.h>
53 #ifdef SYSVSHM
54 #include <sys/shm.h>
55 #endif
56
57 #include <sys/syscallargs.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_kern.h>
61
62 #include <machine/cpu.h>
63 #include <machine/reg.h>
64
65 /*
66 * check exec:
67 * given an "executable" described in the exec package's namei info,
68 * see what we can do with it.
69 *
70 * ON ENTRY:
71 * exec package with appropriate namei info
72 * proc pointer of exec'ing proc
73 * NO SELF-LOCKED VNODES
74 *
75 * ON EXIT:
76 * error: nothing held, etc. exec header still allocated.
77 * ok: filled exec package, executable's vnode (unlocked).
78 *
79 * EXEC SWITCH ENTRY:
80 * Locked vnode to check, exec package, proc.
81 *
82 * EXEC SWITCH EXIT:
83 * ok: return 0, filled exec package, executable's vnode (unlocked).
84 * error: destructive:
85 * everything deallocated execept exec header.
86 * non-destructive:
87 * error code, executable's vnode (unlocked),
88 * exec header unmodified.
89 */
90 int
91 check_exec(p, epp)
92 struct proc *p;
93 struct exec_package *epp;
94 {
95 int error, i;
96 struct vnode *vp;
97 struct nameidata *ndp;
98 int resid;
99
100 ndp = epp->ep_ndp;
101 ndp->ni_cnd.cn_nameiop = LOOKUP;
102 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
103 /* first get the vnode */
104 if ((error = namei(ndp)) != 0)
105 return error;
106 epp->ep_vp = vp = ndp->ni_vp;
107
108 /* check for regular file */
109 if (vp->v_type != VREG) {
110 error = EACCES;
111 goto bad1;
112 }
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 &= ~(VSUID | VSGID);
125
126 /* check access. for root we have to see if any exec bit on */
127 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
128 goto bad1;
129 if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
130 error = EACCES;
131 goto bad1;
132 }
133
134 /* try to open it */
135 if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
136 goto bad1;
137
138 /* unlock vp, since we don't need it locked from here on out. */
139 VOP_UNLOCK(vp);
140
141 /* now we have the file, get the exec header */
142 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
143 UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
144 if (error)
145 goto bad2;
146 epp->ep_hdrvalid = epp->ep_hdrlen - resid;
147
148 /*
149 * set up the vmcmds for creation of the process
150 * address space
151 */
152 error = ENOEXEC;
153 for (i = 0; i < nexecs && error != 0; i++) {
154 int newerror;
155
156 if (execsw[i].es_check == NULL)
157 continue;
158
159 newerror = (*execsw[i].es_check)(p, epp);
160 /* make sure the first "interesting" error code is saved. */
161 if (!newerror || error == ENOEXEC)
162 error = newerror;
163 if (epp->ep_flags & EXEC_DESTR && error != 0)
164 return error;
165 }
166 if (!error) {
167 /* check that entry point is sane */
168 if (epp->ep_entry > VM_MAXUSER_ADDRESS)
169 error = ENOEXEC;
170
171 /* check limits */
172 if ((epp->ep_tsize > MAXTSIZ) ||
173 (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
174 error = ENOMEM;
175
176 if (!error)
177 return (0);
178 }
179
180 /*
181 * free any vmspace-creation commands,
182 * and release their references
183 */
184 kill_vmcmds(&epp->ep_vmcmds);
185
186 bad2:
187 /*
188 * unlock and close the vnode, restore the old one, free the
189 * pathname buf, and punt.
190 */
191 VOP_CLOSE(vp, FREAD, p->p_ucred, p);
192 vrele(vp);
193 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
194 return error;
195
196 bad1:
197 /*
198 * free the namei pathname buffer, and put the vnode
199 * (which we don't yet have open).
200 */
201 vput(vp); /* was still locked */
202 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
203 return error;
204 }
205
206 /*
207 * exec system call
208 */
209 /* ARGSUSED */
210 int
211 sys_execve(p, v, retval)
212 register struct proc *p;
213 void *v;
214 register_t *retval;
215 {
216 register struct sys_execve_args /* {
217 syscallarg(char *) path;
218 syscallarg(char * *) argp;
219 syscallarg(char * *) envp;
220 } */ *uap = v;
221 int error, i;
222 struct exec_package pack;
223 struct nameidata nid;
224 struct vattr attr;
225 struct ucred *cred = p->p_ucred;
226 char *argp;
227 char **cpp, *dp, *sp;
228 long argc, envc;
229 size_t len;
230 char *stack;
231 struct ps_strings arginfo;
232 struct vmspace *vm = p->p_vmspace;
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 argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
276 #ifdef DIAGNOSTIC
277 if (argp == (vm_offset_t) 0)
278 panic("execve: argp == NULL");
279 #endif
280 dp = argp;
281 argc = 0;
282
283 /* copy the fake args list, if there's one, freeing it as we go */
284 if (pack.ep_flags & EXEC_HASARGL) {
285 tmpfap = pack.ep_fa;
286 while (*tmpfap != NULL) {
287 char *cp;
288
289 cp = *tmpfap;
290 while (*cp)
291 *dp++ = *cp++;
292 dp++;
293
294 FREE(*tmpfap, M_EXEC);
295 tmpfap++; argc++;
296 }
297 FREE(pack.ep_fa, M_EXEC);
298 pack.ep_flags &= ~EXEC_HASARGL;
299 }
300
301 /* Now get argv & environment */
302 if (!(cpp = SCARG(uap, argp))) {
303 error = EINVAL;
304 goto bad;
305 }
306
307 if (pack.ep_flags & EXEC_SKIPARG)
308 cpp++;
309
310 while (1) {
311 len = argp + ARG_MAX - dp;
312 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
313 goto bad;
314 if (!sp)
315 break;
316 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
317 if (error == ENAMETOOLONG)
318 error = E2BIG;
319 goto bad;
320 }
321 dp += len;
322 cpp++;
323 argc++;
324 }
325
326 envc = 0;
327 /* environment need not be there */
328 if ((cpp = SCARG(uap, envp)) != NULL ) {
329 while (1) {
330 len = argp + ARG_MAX - dp;
331 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
332 goto bad;
333 if (!sp)
334 break;
335 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
336 if (error == ENAMETOOLONG)
337 error = E2BIG;
338 goto bad;
339 }
340 dp += len;
341 cpp++;
342 envc++;
343 }
344 }
345
346 dp = (char *) ALIGN(dp);
347
348 szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
349
350 /* Now check if args & environ fit into new stack */
351 len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
352 sizeof(long) + 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 /* Unmap old program */
366 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */
367 #ifdef sparc
368 kill_user_windows(p); /* before stack addresses go away */
369 #endif
370 /* Kill shared memory and unmap old program */
371 #ifdef SYSVSHM
372 if (vm->vm_shm)
373 shmexit(p);
374 #endif
375 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
376 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
377
378 /* Now map address space */
379 vm->vm_taddr = (char *) pack.ep_taddr;
380 vm->vm_tsize = btoc(pack.ep_tsize);
381 vm->vm_daddr = (char *) pack.ep_daddr;
382 vm->vm_dsize = btoc(pack.ep_dsize);
383 vm->vm_ssize = btoc(pack.ep_ssize);
384 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
385
386 /* create the new process's VM space by running the vmcmds */
387 #ifdef DIAGNOSTIC
388 if (pack.ep_vmcmds.evs_used == 0)
389 panic("execve: no vmcmds");
390 #endif
391 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
392 struct exec_vmcmd *vcp;
393
394 vcp = &pack.ep_vmcmds.evs_cmds[i];
395 error = (*vcp->ev_proc)(p, vcp);
396 }
397
398 /* free the vmspace-creation commands, and release their references */
399 kill_vmcmds(&pack.ep_vmcmds);
400
401 /* if an error happened, deallocate and punt */
402 if (error)
403 goto exec_abort;
404
405 /* remember information about the process */
406 arginfo.ps_nargvstr = argc;
407 arginfo.ps_nenvstr = envc;
408
409 stack = (char *) (USRSTACK - len);
410 /* Now copy argc, args & environ to new stack */
411 if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
412 goto exec_abort;
413
414 /* copy out the process's ps_strings structure */
415 if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
416 goto exec_abort;
417
418 /* copy out the process's signal trapoline code */
419 if (szsigcode && copyout((char *) pack.ep_emul->e_sigcode,
420 ((char *) PS_STRINGS) - szsigcode,
421 szsigcode))
422 goto exec_abort;
423
424 fdcloseexec(p); /* handle close on exec */
425 execsigs(p); /* reset catched signals */
426
427 /* set command name & other accounting info */
428 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
429 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, 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_NOEXEC and P_TRACED have already been used to disable s[ug]id.
448 */
449 p->p_flag &= ~P_SUGID;
450 if (((attr.va_mode & VSUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
451 || ((attr.va_mode & VSGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
452 p->p_ucred = crcopy(cred);
453 #ifdef KTRACE
454 /*
455 * If process is being ktraced, turn off - unless
456 * root set it.
457 */
458 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
459 vrele(p->p_tracep);
460 p->p_tracep = NULL;
461 p->p_traceflag = 0;
462 }
463 #endif
464 if (attr.va_mode & VSUID)
465 p->p_ucred->cr_uid = attr.va_uid;
466 if (attr.va_mode & VSGID)
467 p->p_ucred->cr_gid = attr.va_gid;
468 p->p_flag |= P_SUGID;
469 }
470 p->p_cred->p_svuid = p->p_ucred->cr_uid;
471 p->p_cred->p_svgid = p->p_ucred->cr_gid;
472
473 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
474
475 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
476 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
477 vrele(pack.ep_vp);
478
479 /* setup new registers and do misc. setup. */
480 (*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack, retval);
481
482 if (p->p_flag & P_TRACED)
483 psignal(p, SIGTRAP);
484
485 p->p_emul = pack.ep_emul;
486 FREE(pack.ep_hdr, M_EXEC);
487
488 #ifdef KTRACE
489 if (KTRPOINT(p, KTR_EMUL))
490 ktremul(p->p_tracep, p->p_emul->e_name);
491 #endif
492 return 0;
493
494 bad:
495 /* free the vmspace-creation commands, and release their references */
496 kill_vmcmds(&pack.ep_vmcmds);
497 /* kill any opened file descriptor, if necessary */
498 if (pack.ep_flags & EXEC_HASFD) {
499 pack.ep_flags &= ~EXEC_HASFD;
500 (void) fdrelease(p, pack.ep_fd);
501 }
502 /* close and put the exec'd file */
503 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
504 vrele(pack.ep_vp);
505 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
506 kmem_free_wakeup(exec_map, (vm_offset_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 vm_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 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
524 vrele(pack.ep_vp);
525 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
526 FREE(pack.ep_hdr, M_EXEC);
527 exit1(p, W_EXITCODE(0, SIGABRT));
528 exit1(p, -1);
529
530 /* NOTREACHED */
531 return 0;
532 }
533
534
535 void *
536 copyargs(pack, arginfo, stack, argp)
537 struct exec_package *pack;
538 struct ps_strings *arginfo;
539 void *stack;
540 void *argp;
541 {
542 char **cpp = stack;
543 char *dp, *sp;
544 size_t len;
545 void *nullp = NULL;
546 int argc = arginfo->ps_nargvstr;
547 int envc = arginfo->ps_nenvstr;
548
549 if (copyout(&argc, cpp++, sizeof(argc)))
550 return NULL;
551
552 dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
553 sp = argp;
554
555 /* XXX don't copy them out, remap them! */
556 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
557
558 for (; --argc >= 0; sp += len, dp += len)
559 if (copyout(&dp, cpp++, sizeof(dp)) ||
560 copyoutstr(sp, dp, ARG_MAX, &len))
561 return NULL;
562
563 if (copyout(&nullp, cpp++, sizeof(nullp)))
564 return NULL;
565
566 arginfo->ps_envstr = cpp; /* remember location of envp for later */
567
568 for (; --envc >= 0; sp += len, dp += len)
569 if (copyout(&dp, cpp++, sizeof(dp)) ||
570 copyoutstr(sp, dp, ARG_MAX, &len))
571 return NULL;
572
573 if (copyout(&nullp, cpp++, sizeof(nullp)))
574 return NULL;
575
576 return cpp;
577 }
578