kern_exec.c revision 1.88 1 /* $NetBSD: kern_exec.c,v 1.88 1998/02/05 07:59:48 mrg 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
54 #include <sys/syscallargs.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_kern.h>
58
59 #if defined(UVM)
60 #include <uvm/uvm_extern.h>
61 #endif
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 int 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 don't need it locked from here on out. */
134 VOP_UNLOCK(vp);
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 * unlock and close the vnode, restore the old one, free the
184 * pathname buf, and punt.
185 */
186 VOP_CLOSE(vp, FREAD, p->p_ucred, p);
187 vrele(vp);
188 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
189 return error;
190
191 bad1:
192 /*
193 * free the namei pathname buffer, and put the vnode
194 * (which we don't yet have open).
195 */
196 vput(vp); /* was still locked */
197 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
198 return error;
199 }
200
201 /*
202 * exec system call
203 */
204 /* ARGSUSED */
205 int
206 sys_execve(p, v, retval)
207 register struct proc *p;
208 void *v;
209 register_t *retval;
210 {
211 register struct sys_execve_args /* {
212 syscallarg(const char *) path;
213 syscallarg(char * const *) argp;
214 syscallarg(char * const *) envp;
215 } */ *uap = v;
216 int error, i;
217 struct exec_package pack;
218 struct nameidata nid;
219 struct vattr attr;
220 struct ucred *cred = p->p_ucred;
221 char *argp;
222 char * const *cpp;
223 char *dp, *sp;
224 long argc, envc;
225 size_t len;
226 char *stack;
227 struct ps_strings arginfo;
228 struct vmspace *vm;
229 char **tmpfap;
230 int szsigcode;
231 extern struct emul emul_netbsd;
232
233 /*
234 * figure out the maximum size of an exec header, if necessary.
235 * XXX should be able to keep LKM code from modifying exec switch
236 * when we're still using it, but...
237 */
238 if (exec_maxhdrsz == 0) {
239 for (i = 0; i < nexecs; i++)
240 if (execsw[i].es_check != NULL
241 && execsw[i].es_hdrsz > exec_maxhdrsz)
242 exec_maxhdrsz = execsw[i].es_hdrsz;
243 }
244
245 /* init the namei data to point the file user's program name */
246 /* XXX cgd 960926: why do this here? most will be clobbered. */
247 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
248
249 /*
250 * initialize the fields of the exec package.
251 */
252 pack.ep_name = SCARG(uap, path);
253 MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
254 pack.ep_hdrlen = exec_maxhdrsz;
255 pack.ep_hdrvalid = 0;
256 pack.ep_ndp = &nid;
257 pack.ep_emul_arg = NULL;
258 pack.ep_vmcmds.evs_cnt = 0;
259 pack.ep_vmcmds.evs_used = 0;
260 pack.ep_vap = &attr;
261 pack.ep_emul = &emul_netbsd;
262 pack.ep_flags = 0;
263
264 /* see if we can run it. */
265 if ((error = check_exec(p, &pack)) != 0)
266 goto freehdr;
267
268 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
269
270 /* allocate an argument buffer */
271 #if defined(UVM)
272 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
273 #else
274 argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
275 #endif
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 /*
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 #if defined(UVM)
371 uvmspace_exec(p);
372 #else
373 vmspace_exec(p);
374 #endif
375
376 /* Now map address space */
377 vm = p->p_vmspace;
378 vm->vm_taddr = (char *) pack.ep_taddr;
379 vm->vm_tsize = btoc(pack.ep_tsize);
380 vm->vm_daddr = (char *) pack.ep_daddr;
381 vm->vm_dsize = btoc(pack.ep_dsize);
382 vm->vm_ssize = btoc(pack.ep_ssize);
383 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
384
385 /* create the new process's VM space by running the vmcmds */
386 #ifdef DIAGNOSTIC
387 if (pack.ep_vmcmds.evs_used == 0)
388 panic("execve: no vmcmds");
389 #endif
390 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
391 struct exec_vmcmd *vcp;
392
393 vcp = &pack.ep_vmcmds.evs_cmds[i];
394 error = (*vcp->ev_proc)(p, vcp);
395 }
396
397 /* free the vmspace-creation commands, and release their references */
398 kill_vmcmds(&pack.ep_vmcmds);
399
400 /* if an error happened, deallocate and punt */
401 if (error)
402 goto exec_abort;
403
404 /* remember information about the process */
405 arginfo.ps_nargvstr = argc;
406 arginfo.ps_nenvstr = envc;
407
408 stack = (char *) (USRSTACK - len);
409 /* Now copy argc, args & environ to new stack */
410 if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
411 goto exec_abort;
412
413 /* copy out the process's ps_strings structure */
414 if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
415 goto exec_abort;
416
417 /* copy out the process's signal trapoline code */
418 if (szsigcode && copyout((char *) pack.ep_emul->e_sigcode,
419 ((char *) PS_STRINGS) - szsigcode,
420 szsigcode))
421 goto exec_abort;
422
423 fdcloseexec(p); /* handle close on exec */
424 execsigs(p); /* reset catched signals */
425
426 /* set command name & other accounting info */
427 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
428 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
429 p->p_comm[len] = 0;
430 p->p_acflag &= ~AFORK;
431
432 /* record proc's vnode, for use by procfs and others */
433 if (p->p_textvp)
434 vrele(p->p_textvp);
435 VREF(pack.ep_vp);
436 p->p_textvp = pack.ep_vp;
437
438 p->p_flag |= P_EXEC;
439 if (p->p_flag & P_PPWAIT) {
440 p->p_flag &= ~P_PPWAIT;
441 wakeup((caddr_t) p->p_pptr);
442 }
443
444 /*
445 * deal with set[ug]id.
446 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
447 */
448 if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
449 || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
450 p->p_ucred = crcopy(cred);
451 #ifdef KTRACE
452 /*
453 * If process is being ktraced, turn off - unless
454 * root set it.
455 */
456 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
457 vrele(p->p_tracep);
458 p->p_tracep = NULL;
459 p->p_traceflag = 0;
460 }
461 #endif
462 if (attr.va_mode & S_ISUID)
463 p->p_ucred->cr_uid = attr.va_uid;
464 if (attr.va_mode & S_ISGID)
465 p->p_ucred->cr_gid = attr.va_gid;
466 p->p_flag |= P_SUGID;
467 } else
468 p->p_flag &= ~P_SUGID;
469 p->p_cred->p_svuid = p->p_ucred->cr_uid;
470 p->p_cred->p_svgid = p->p_ucred->cr_gid;
471
472 #if defined(UVM)
473 uvm_km_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
474 #else
475 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
476 #endif
477
478 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
479 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
480 vrele(pack.ep_vp);
481
482 /* setup new registers and do misc. setup. */
483 (*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
484
485 if (p->p_flag & P_TRACED)
486 psignal(p, SIGTRAP);
487
488 p->p_emul = pack.ep_emul;
489 FREE(pack.ep_hdr, M_EXEC);
490
491 #ifdef KTRACE
492 if (KTRPOINT(p, KTR_EMUL))
493 ktremul(p->p_tracep, p->p_emul->e_name);
494 #endif
495
496 return (EJUSTRETURN);
497
498 bad:
499 /* free the vmspace-creation commands, and release their references */
500 kill_vmcmds(&pack.ep_vmcmds);
501 /* kill any opened file descriptor, if necessary */
502 if (pack.ep_flags & EXEC_HASFD) {
503 pack.ep_flags &= ~EXEC_HASFD;
504 (void) fdrelease(p, pack.ep_fd);
505 }
506 /* close and put the exec'd file */
507 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
508 vrele(pack.ep_vp);
509 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
510 #if defined(UVM)
511 uvm_km_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
512 #else
513 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
514 #endif
515
516 freehdr:
517 FREE(pack.ep_hdr, M_EXEC);
518 return error;
519
520 exec_abort:
521 /*
522 * the old process doesn't exist anymore. exit gracefully.
523 * get rid of the (new) address space we have created, if any, get rid
524 * of our namei data and vnode, and exit noting failure
525 */
526 #if defined(UVM)
527 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
528 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
529 #else
530 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
531 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
532 #endif
533 if (pack.ep_emul_arg)
534 FREE(pack.ep_emul_arg, M_TEMP);
535 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
536 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
537 vrele(pack.ep_vp);
538 #if defined(UVM)
539 uvm_km_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
540 #else
541 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
542 #endif
543 FREE(pack.ep_hdr, M_EXEC);
544 exit1(p, W_EXITCODE(0, SIGABRT));
545 exit1(p, -1);
546
547 /* NOTREACHED */
548 return 0;
549 }
550
551
552 void *
553 copyargs(pack, arginfo, stack, argp)
554 struct exec_package *pack;
555 struct ps_strings *arginfo;
556 void *stack;
557 void *argp;
558 {
559 char **cpp = stack;
560 char *dp, *sp;
561 size_t len;
562 void *nullp = NULL;
563 int argc = arginfo->ps_nargvstr;
564 int envc = arginfo->ps_nenvstr;
565
566 if (copyout(&argc, cpp++, sizeof(argc)))
567 return NULL;
568
569 dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
570 sp = argp;
571
572 /* XXX don't copy them out, remap them! */
573 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
574
575 for (; --argc >= 0; sp += len, dp += len)
576 if (copyout(&dp, cpp++, sizeof(dp)) ||
577 copyoutstr(sp, dp, ARG_MAX, &len))
578 return NULL;
579
580 if (copyout(&nullp, cpp++, sizeof(nullp)))
581 return NULL;
582
583 arginfo->ps_envstr = cpp; /* remember location of envp for later */
584
585 for (; --envc >= 0; sp += len, dp += len)
586 if (copyout(&dp, cpp++, sizeof(dp)) ||
587 copyoutstr(sp, dp, ARG_MAX, &len))
588 return NULL;
589
590 if (copyout(&nullp, cpp++, sizeof(nullp)))
591 return NULL;
592
593 return cpp;
594 }
595