kern_exec.c revision 1.64 1 /* $NetBSD: kern_exec.c,v 1.64 1995/03/09 12:05:39 mycroft Exp $ */
2
3 /*-
4 * Copyright (C) 1993, 1994 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 #include <machine/cpu.h>
60 #include <machine/reg.h>
61
62 #ifdef COPY_SIGCODE
63 extern char sigcode[], esigcode[];
64
65 #define szsigcode (esigcode - sigcode)
66 #else
67 #define szsigcode 0
68 #endif
69
70 /*
71 * check exec:
72 * given an "executable" described in the exec package's namei info,
73 * see what we can do with it.
74 *
75 * ON ENTRY:
76 * exec package with appropriate namei info
77 * proc pointer of exec'ing proc
78 * NO SELF-LOCKED VNODES
79 *
80 * ON EXIT:
81 * error: nothing held, etc. exec header still allocated.
82 * ok: filled exec package, one locked vnode.
83 *
84 * EXEC SWITCH ENTRY:
85 * Locked vnode to check, exec package, proc.
86 *
87 * EXEC SWITCH EXIT:
88 * ok: return 0, filled exec package, one locked vnode.
89 * error: destructive:
90 * everything deallocated execept exec header.
91 * non-descructive:
92 * error code, locked vnode, 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 char *cp, *ep, *name;
102 struct nameidata *ndp;
103 int resid;
104
105 ndp = epp->ep_ndp;
106 ndp->ni_cnd.cn_nameiop = LOOKUP;
107 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
108 /* first get the vnode */
109 if (error = namei(ndp))
110 return error;
111 epp->ep_vp = vp = ndp->ni_vp;
112
113 /* check for regular file */
114 if (vp->v_type != VREG) {
115 error = EACCES;
116 goto bad1;
117 }
118
119 /* get attributes */
120 if (error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p))
121 goto bad1;
122
123 /* Check mount point */
124 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
125 error = EACCES;
126 goto bad1;
127 }
128 if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
129 epp->ep_vap->va_mode &= ~(VSUID | VSGID);
130
131 /* check access. for root we have to see if any exec bit on */
132 if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p))
133 goto bad1;
134 if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
135 error = EACCES;
136 goto bad1;
137 }
138
139 /* try to open it */
140 if (error = VOP_OPEN(vp, FREAD, p->p_ucred, p))
141 goto bad1;
142
143 /* now we have the file, get the exec header */
144 if (error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
145 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p))
146 goto bad2;
147 epp->ep_hdrvalid = epp->ep_hdrlen - resid;
148
149 /*
150 * set up the vmcmds for creation of the process
151 * address space
152 */
153 error = ENOEXEC;
154 for (i = 0; i < nexecs && error != 0; i++) {
155 if (execsw[i].es_check != NULL)
156 error = (*execsw[i].es_check)(p, epp);
157 if (epp->ep_flags & EXEC_DESTR && error != 0)
158 return error;
159 }
160 if (!error) {
161 /* check that entry point is sane */
162 if (epp->ep_entry > VM_MAXUSER_ADDRESS)
163 error = ENOEXEC;
164
165 /* check limits */
166 if ((epp->ep_tsize > MAXTSIZ) ||
167 (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
168 error = ENOMEM;
169
170 if (!error)
171 return (0);
172 }
173
174 /*
175 * free any vmspace-creation commands,
176 * and release their references
177 */
178 kill_vmcmds(&epp->ep_vmcmds);
179
180 bad2:
181 /*
182 * unlock and close the vnode, restore the old one, free the
183 * pathname buf, and punt.
184 */
185 VOP_UNLOCK(vp);
186 vn_close(vp, FREAD, p->p_ucred, p);
187 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
188 return error;
189
190 bad1:
191 /*
192 * free the namei pathname buffer, and put the vnode
193 * (which we don't yet have open).
194 */
195 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
196 vput(vp);
197 return error;
198 }
199
200 /*
201 * exec system call
202 */
203 /* ARGSUSED */
204 execve(p, uap, retval)
205 register struct proc *p;
206 register struct execve_args /* {
207 syscallarg(char *) path;
208 syscallarg(char * *) argp;
209 syscallarg(char * *) envp;
210 } */ *uap;
211 register_t *retval;
212 {
213 int error, i;
214 struct exec_package pack;
215 struct nameidata nid;
216 struct vattr attr;
217 struct ucred *cred = p->p_ucred;
218 char *argp;
219 char **cpp, *dp, *sp, *np;
220 long argc, envc;
221 size_t len;
222 char *stack;
223 struct ps_strings arginfo;
224 struct vmspace *vm = p->p_vmspace;
225 char **tmpfap;
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 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
241
242 /*
243 * initialize the fields of the exec package.
244 */
245 pack.ep_name = SCARG(uap, path);
246 MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
247 pack.ep_hdrlen = exec_maxhdrsz;
248 pack.ep_hdrvalid = 0;
249 pack.ep_ndp = &nid;
250 pack.ep_setup = NULL; /* assume no setup function */
251 pack.ep_setup_arg = NULL;
252 pack.ep_setup_arglen = 0;
253 pack.ep_vmcmds.evs_cnt = 0;
254 pack.ep_vmcmds.evs_used = 0;
255 pack.ep_vap = &attr;
256 pack.ep_emul = EMUL_NETBSD;
257 pack.ep_flags = 0;
258
259 /* see if we can run it. */
260 if (error = check_exec(p, &pack))
261 goto freehdr;
262
263 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
264
265 /* allocate an argument buffer */
266 argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
267 #ifdef DIAGNOSTIC
268 if (argp == (vm_offset_t) 0)
269 panic("execve: argp == NULL");
270 #endif
271 dp = argp;
272 argc = 0;
273
274 /* copy the fake args list, if there's one, freeing it as we go */
275 if (pack.ep_flags & EXEC_HASARGL) {
276 tmpfap = pack.ep_fa;
277 while (*tmpfap != NULL) {
278 char *cp;
279
280 cp = *tmpfap;
281 while (*cp)
282 *dp++ = *cp++;
283 *dp++;
284
285 FREE(*tmpfap, M_EXEC);
286 tmpfap++; argc++;
287 }
288 FREE(pack.ep_fa, M_EXEC);
289 pack.ep_flags &= ~EXEC_HASARGL;
290 }
291
292 /* Now get argv & environment */
293 if (!(cpp = SCARG(uap, argp))) {
294 error = EINVAL;
295 goto bad;
296 }
297
298 if (pack.ep_flags & EXEC_SKIPARG)
299 cpp++;
300
301 while (1) {
302 len = argp + ARG_MAX - dp;
303 if (error = copyin(cpp, &sp, sizeof(sp)))
304 goto bad;
305 if (!sp)
306 break;
307 if (error = copyinstr(sp, dp, len, &len)) {
308 if (error == ENAMETOOLONG)
309 error = E2BIG;
310 goto bad;
311 }
312 dp += len;
313 cpp++;
314 argc++;
315 }
316
317 envc = 0;
318 if (cpp = SCARG(uap, envp)) { /* environment need not be there */
319 while (1) {
320 len = argp + ARG_MAX - dp;
321 if (error = copyin(cpp, &sp, sizeof(sp)))
322 goto bad;
323 if (!sp)
324 break;
325 if (error = copyinstr(sp, dp, len, &len)) {
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 /* Now check if args & environ fit into new stack */
339 len = ((argc + envc + 2 + pack.ep_setup_arglen) * sizeof(char *) +
340 sizeof(long) + dp + STACKGAPLEN + szsigcode +
341 sizeof(struct ps_strings)) - argp;
342 #ifdef COMPAT_LINUX
343 /* XXXX need this for envp and argv on stack */
344 if (pack.ep_emul == EMUL_LINUX)
345 len += 2 * sizeof (char *);
346 #endif
347 len = ALIGN(len); /* make the stack "safely" aligned */
348
349 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
350 error = ENOMEM;
351 goto bad;
352 }
353
354 /* adjust "active stack depth" for process VSZ */
355 pack.ep_ssize = len; /* maybe should go elsewhere, but... */
356
357 /* Unmap old program */
358 #ifdef sparc
359 kill_user_windows(p); /* before stack addresses go away */
360 #endif
361 /* Kill shared memory and unmap old program */
362 #ifdef SYSVSHM
363 if (vm->vm_shm)
364 shmexit(p);
365 #endif
366 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
367 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
368
369 /* Now map address space */
370 vm->vm_taddr = (char *) pack.ep_taddr;
371 vm->vm_tsize = btoc(pack.ep_tsize);
372 vm->vm_daddr = (char *) pack.ep_daddr;
373 vm->vm_dsize = btoc(pack.ep_dsize);
374 vm->vm_ssize = btoc(pack.ep_ssize);
375 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
376
377 /* create the new process's VM space by running the vmcmds */
378 #ifdef DIAGNOSTIC
379 if (pack.ep_vmcmds.evs_used == 0)
380 panic("execve: no vmcmds");
381 #endif
382 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
383 struct exec_vmcmd *vcp;
384
385 vcp = &pack.ep_vmcmds.evs_cmds[i];
386 error = (*vcp->ev_proc)(p, vcp);
387 }
388
389 /* free the vmspace-creation commands, and release their references */
390 kill_vmcmds(&pack.ep_vmcmds);
391
392 /* if an error happened, deallocate and punt */
393 if (error)
394 goto exec_abort;
395
396 /* remember information about the process */
397 arginfo.ps_nargvstr = argc;
398 arginfo.ps_nenvstr = envc;
399
400 /* Now copy argc, args & environ to new stack */
401 stack = (char *) (USRSTACK - len);
402 cpp = (char **) stack;
403
404 if (copyout(&argc, cpp++, sizeof(argc)))
405 goto exec_abort;
406 #ifdef COMPAT_LINUX
407 /* XXXX Linux puts argv and envp on stack too, store argv now */
408 if (pack.ep_emul == EMUL_LINUX) {
409 char **argv_loc = cpp + 2, **stk = (char **) stack;
410
411 if (copyout(&argv_loc, &stk[1], sizeof (argv_loc)))
412 goto exec_abort;
413 /* leave room for envp and argv */
414 cpp += 2;
415 }
416 #endif
417 dp = (char *) (cpp + argc + envc + 2 + pack.ep_setup_arglen);
418 sp = argp;
419 np = 0;
420
421 /* XXX don't copy them out, remap them! */
422 arginfo.ps_argvstr = dp; /* remember location of argv for later */
423 for (; --argc >= 0; sp += len, dp += len) {
424 if (copyout(&dp, cpp++, sizeof(dp)) ||
425 copyoutstr(sp, dp, ARG_MAX, &len))
426 goto exec_abort;
427 }
428 if (copyout(&np, cpp++, sizeof(np)))
429 goto exec_abort;
430
431 #ifdef COMPAT_LINUX
432 /* XXXX Linux puts argv and envp on stack too, store envp now */
433 if (pack.ep_emul == EMUL_LINUX) {
434 char **envp_loc = cpp, **stk = (char **) stack;
435
436 if (copyout(&envp_loc, &stk[2], sizeof (envp_loc)))
437 goto exec_abort;
438 }
439 #endif
440
441 arginfo.ps_envstr = dp; /* remember location of envp for later */
442 for (; --envc >= 0; sp += len, dp += len) {
443 if (copyout(&dp, cpp++, sizeof(dp)) ||
444 copyoutstr(sp, dp, ARG_MAX, &len))
445 goto exec_abort;
446 }
447 if (copyout(&np, cpp++, sizeof(np)))
448 goto exec_abort;
449
450 if (pack.ep_setup != NULL)
451 (*pack.ep_setup)(EXEC_SETUP_ADDARGS, p, &pack, cpp);
452
453 /* copy out the process's ps_strings structure */
454 if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
455 goto exec_abort;
456
457 #ifdef COPY_SIGCODE
458 /* copy out the process's signal trapoline code */
459 if (copyout((char *) sigcode, ((char *) PS_STRINGS) - szsigcode,
460 szsigcode)) {
461 goto exec_abort;
462 }
463 #endif
464
465 fdcloseexec(p); /* handle close on exec */
466 execsigs(p); /* reset catched signals */
467
468 /* set command name & other accounting info */
469 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
470 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
471 p->p_comm[len] = 0;
472 p->p_acflag &= ~AFORK;
473
474 /* record proc's vnode, for use by procfs and others */
475 if (p->p_textvp)
476 vrele(p->p_textvp);
477 VREF(pack.ep_vp);
478 p->p_textvp = pack.ep_vp;
479
480 p->p_flag |= P_EXEC;
481 if (p->p_flag & P_PPWAIT) {
482 p->p_flag &= ~P_PPWAIT;
483 wakeup((caddr_t) p->p_pptr);
484 }
485
486 /*
487 * deal with set[ug]id.
488 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
489 */
490 p->p_flag &= ~P_SUGID;
491 if (((attr.va_mode & VSUID) != 0 &&
492 p->p_ucred->cr_uid != attr.va_uid)
493 || (attr.va_mode & VSGID) != 0 &&
494 p->p_ucred->cr_gid != attr.va_gid) {
495 p->p_ucred = crcopy(cred);
496 #ifdef KTRACE
497 /*
498 * If process is being ktraced, turn off - unless
499 * root set it.
500 */
501 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
502 vrele(p->p_tracep);
503 p->p_tracep = NULL;
504 p->p_traceflag = 0;
505 }
506 #endif
507 if (attr.va_mode & VSUID)
508 p->p_ucred->cr_uid = attr.va_uid;
509 if (attr.va_mode & VSGID)
510 p->p_ucred->cr_gid = attr.va_gid;
511 p->p_flag |= P_SUGID;
512 }
513 p->p_cred->p_svuid = p->p_ucred->cr_uid;
514 p->p_cred->p_svgid = p->p_ucred->cr_gid;
515
516 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
517
518 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
519 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
520 vput(pack.ep_vp);
521
522 /* setup new registers and do misc. setup. */
523 setregs(p, pack.ep_entry, (u_long) stack, retval);
524 if (pack.ep_setup != NULL)
525 (*pack.ep_setup)(EXEC_SETUP_FINISH, p, &pack, NULL);
526
527 if (p->p_flag & P_TRACED)
528 psignal(p, SIGTRAP);
529
530 p->p_emul = pack.ep_emul;
531 FREE(pack.ep_hdr, M_EXEC);
532 return 0;
533
534 bad:
535 if (pack.ep_setup != NULL)
536 (*pack.ep_setup)(EXEC_SETUP_CLEANUP, p, &pack, dp);
537 /* free the vmspace-creation commands, and release their references */
538 kill_vmcmds(&pack.ep_vmcmds);
539 /* kill any opened file descriptor, if necessary */
540 if (pack.ep_flags & EXEC_HASFD) {
541 pack.ep_flags &= ~EXEC_HASFD;
542 (void) fdclose(p, pack.ep_fd);
543 }
544 /* close and put the exec'd file */
545 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
546 vput(pack.ep_vp);
547 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
548 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
549
550 freehdr:
551 FREE(pack.ep_hdr, M_EXEC);
552 return error;
553
554 exec_abort:
555 /*
556 * the old process doesn't exist anymore. exit gracefully.
557 * get rid of the (new) address space we have created, if any, get rid
558 * of our namei data and vnode, and exit noting failure
559 */
560 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
561 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
562 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
563 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
564 vput(pack.ep_vp);
565 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
566 FREE(pack.ep_hdr, M_EXEC);
567 exit1(p, W_EXITCODE(0, SIGABRT));
568 exit1(p, -1);
569
570 /* NOTREACHED */
571 return 0;
572 }
573