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