kern_exec.c revision 1.76 1 /* $NetBSD: kern_exec.c,v 1.76 1996/09/26 23:34:47 cgd 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 #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 (locked).
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 (locked).
84 * error: destructive:
85 * everything deallocated execept exec header.
86 * non-destructive:
87 * error code, executable's vnode (locked),
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 /* now we have the file, get the exec header */
139 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
140 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
141 if (error)
142 goto bad2;
143 epp->ep_hdrvalid = epp->ep_hdrlen - resid;
144
145 /*
146 * set up the vmcmds for creation of the process
147 * address space
148 */
149 error = ENOEXEC;
150 for (i = 0; i < nexecs && error != 0; i++) {
151 int newerror;
152
153 if (execsw[i].es_check == NULL)
154 continue;
155
156 newerror = (*execsw[i].es_check)(p, epp);
157 /* make sure the first "interesting" error code is saved. */
158 if (!newerror || error == ENOEXEC)
159 error = newerror;
160 if (epp->ep_flags & EXEC_DESTR && error != 0)
161 return error;
162 }
163 if (!error) {
164 /* check that entry point is sane */
165 if (epp->ep_entry > VM_MAXUSER_ADDRESS)
166 error = ENOEXEC;
167
168 /* check limits */
169 if ((epp->ep_tsize > MAXTSIZ) ||
170 (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
171 error = ENOMEM;
172
173 if (!error)
174 return (0);
175 }
176
177 /*
178 * free any vmspace-creation commands,
179 * and release their references
180 */
181 kill_vmcmds(&epp->ep_vmcmds);
182
183 bad2:
184 /*
185 * unlock and close the vnode, restore the old one, free the
186 * pathname buf, and punt.
187 */
188 VOP_UNLOCK(vp);
189 vn_close(vp, FREAD, p->p_ucred, p);
190 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
191 return error;
192
193 bad1:
194 /*
195 * free the namei pathname buffer, and put the vnode
196 * (which we don't yet have open).
197 */
198 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
199 vput(vp);
200 return error;
201 }
202
203 /*
204 * exec system call
205 */
206 /* ARGSUSED */
207 int
208 sys_execve(p, v, retval)
209 register struct proc *p;
210 void *v;
211 register_t *retval;
212 {
213 register struct sys_execve_args /* {
214 syscallarg(char *) path;
215 syscallarg(char * *) argp;
216 syscallarg(char * *) envp;
217 } */ *uap = v;
218 int error, i;
219 struct exec_package pack;
220 struct nameidata nid;
221 struct vattr attr;
222 struct ucred *cred = p->p_ucred;
223 char *argp;
224 char **cpp, *dp, *sp;
225 long argc, envc;
226 size_t len;
227 char *stack;
228 struct ps_strings arginfo;
229 struct vmspace *vm = p->p_vmspace;
230 char **tmpfap;
231 int szsigcode;
232 extern struct emul emul_netbsd;
233
234 /*
235 * figure out the maximum size of an exec header, if necessary.
236 * XXX should be able to keep LKM code from modifying exec switch
237 * when we're still using it, but...
238 */
239 if (exec_maxhdrsz == 0) {
240 for (i = 0; i < nexecs; i++)
241 if (execsw[i].es_check != NULL
242 && execsw[i].es_hdrsz > exec_maxhdrsz)
243 exec_maxhdrsz = execsw[i].es_hdrsz;
244 }
245
246 /* init the namei data to point the file user's program name */
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 argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
272 #ifdef DIAGNOSTIC
273 if (argp == (vm_offset_t) 0)
274 panic("execve: argp == NULL");
275 #endif
276 dp = argp;
277 argc = 0;
278
279 /* copy the fake args list, if there's one, freeing it as we go */
280 if (pack.ep_flags & EXEC_HASARGL) {
281 tmpfap = pack.ep_fa;
282 while (*tmpfap != NULL) {
283 char *cp;
284
285 cp = *tmpfap;
286 while (*cp)
287 *dp++ = *cp++;
288 dp++;
289
290 FREE(*tmpfap, M_EXEC);
291 tmpfap++; argc++;
292 }
293 FREE(pack.ep_fa, M_EXEC);
294 pack.ep_flags &= ~EXEC_HASARGL;
295 }
296
297 /* Now get argv & environment */
298 if (!(cpp = SCARG(uap, argp))) {
299 error = EINVAL;
300 goto bad;
301 }
302
303 if (pack.ep_flags & EXEC_SKIPARG)
304 cpp++;
305
306 while (1) {
307 len = argp + ARG_MAX - dp;
308 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
309 goto bad;
310 if (!sp)
311 break;
312 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
313 if (error == ENAMETOOLONG)
314 error = E2BIG;
315 goto bad;
316 }
317 dp += len;
318 cpp++;
319 argc++;
320 }
321
322 envc = 0;
323 /* environment need not be there */
324 if ((cpp = SCARG(uap, envp)) != NULL ) {
325 while (1) {
326 len = argp + ARG_MAX - dp;
327 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
328 goto bad;
329 if (!sp)
330 break;
331 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
332 if (error == ENAMETOOLONG)
333 error = E2BIG;
334 goto bad;
335 }
336 dp += len;
337 cpp++;
338 envc++;
339 }
340 }
341
342 dp = (char *) ALIGN(dp);
343
344 szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
345
346 /* Now check if args & environ fit into new stack */
347 len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
348 sizeof(long) + dp + STACKGAPLEN + szsigcode +
349 sizeof(struct ps_strings)) - argp;
350
351 len = ALIGN(len); /* make the stack "safely" aligned */
352
353 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
354 error = ENOMEM;
355 goto bad;
356 }
357
358 /* adjust "active stack depth" for process VSZ */
359 pack.ep_ssize = len; /* maybe should go elsewhere, but... */
360
361 /* Unmap old program */
362 #ifdef sparc
363 kill_user_windows(p); /* before stack addresses go away */
364 #endif
365 /* Kill shared memory and unmap old program */
366 #ifdef SYSVSHM
367 if (vm->vm_shm)
368 shmexit(p);
369 #endif
370 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
371 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
372
373 /* Now map address space */
374 vm->vm_taddr = (char *) pack.ep_taddr;
375 vm->vm_tsize = btoc(pack.ep_tsize);
376 vm->vm_daddr = (char *) pack.ep_daddr;
377 vm->vm_dsize = btoc(pack.ep_dsize);
378 vm->vm_ssize = btoc(pack.ep_ssize);
379 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
380
381 /* create the new process's VM space by running the vmcmds */
382 #ifdef DIAGNOSTIC
383 if (pack.ep_vmcmds.evs_used == 0)
384 panic("execve: no vmcmds");
385 #endif
386 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
387 struct exec_vmcmd *vcp;
388
389 vcp = &pack.ep_vmcmds.evs_cmds[i];
390 error = (*vcp->ev_proc)(p, vcp);
391 }
392
393 /* free the vmspace-creation commands, and release their references */
394 kill_vmcmds(&pack.ep_vmcmds);
395
396 /* if an error happened, deallocate and punt */
397 if (error)
398 goto exec_abort;
399
400 /* remember information about the process */
401 arginfo.ps_nargvstr = argc;
402 arginfo.ps_nenvstr = envc;
403
404 stack = (char *) (USRSTACK - len);
405 /* Now copy argc, args & environ to new stack */
406 if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
407 goto exec_abort;
408
409 /* copy out the process's ps_strings structure */
410 if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
411 goto exec_abort;
412
413 /* copy out the process's signal trapoline code */
414 if (szsigcode && copyout((char *) pack.ep_emul->e_sigcode,
415 ((char *) PS_STRINGS) - szsigcode,
416 szsigcode))
417 goto exec_abort;
418
419 fdcloseexec(p); /* handle close on exec */
420 execsigs(p); /* reset catched signals */
421
422 /* set command name & other accounting info */
423 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
424 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
425 p->p_comm[len] = 0;
426 p->p_acflag &= ~AFORK;
427
428 /* record proc's vnode, for use by procfs and others */
429 if (p->p_textvp)
430 vrele(p->p_textvp);
431 VREF(pack.ep_vp);
432 p->p_textvp = pack.ep_vp;
433
434 p->p_flag |= P_EXEC;
435 if (p->p_flag & P_PPWAIT) {
436 p->p_flag &= ~P_PPWAIT;
437 wakeup((caddr_t) p->p_pptr);
438 }
439
440 /*
441 * deal with set[ug]id.
442 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
443 */
444 p->p_flag &= ~P_SUGID;
445 if (((attr.va_mode & VSUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
446 || ((attr.va_mode & VSGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
447 p->p_ucred = crcopy(cred);
448 #ifdef KTRACE
449 /*
450 * If process is being ktraced, turn off - unless
451 * root set it.
452 */
453 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
454 vrele(p->p_tracep);
455 p->p_tracep = NULL;
456 p->p_traceflag = 0;
457 }
458 #endif
459 if (attr.va_mode & VSUID)
460 p->p_ucred->cr_uid = attr.va_uid;
461 if (attr.va_mode & VSGID)
462 p->p_ucred->cr_gid = attr.va_gid;
463 p->p_flag |= P_SUGID;
464 }
465 p->p_cred->p_svuid = p->p_ucred->cr_uid;
466 p->p_cred->p_svgid = p->p_ucred->cr_gid;
467
468 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
469
470 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
471 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
472 vput(pack.ep_vp);
473
474 /* setup new registers and do misc. setup. */
475 (*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack, retval);
476
477 if (p->p_flag & P_TRACED)
478 psignal(p, SIGTRAP);
479
480 p->p_emul = pack.ep_emul;
481 FREE(pack.ep_hdr, M_EXEC);
482
483 #ifdef KTRACE
484 if (KTRPOINT(p, KTR_EMUL))
485 ktremul(p->p_tracep, p->p_emul->e_name);
486 #endif
487 return 0;
488
489 bad:
490 /* free the vmspace-creation commands, and release their references */
491 kill_vmcmds(&pack.ep_vmcmds);
492 /* kill any opened file descriptor, if necessary */
493 if (pack.ep_flags & EXEC_HASFD) {
494 pack.ep_flags &= ~EXEC_HASFD;
495 (void) fdrelease(p, pack.ep_fd);
496 }
497 /* close and put the exec'd file */
498 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
499 vput(pack.ep_vp);
500 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
501 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
502
503 freehdr:
504 FREE(pack.ep_hdr, M_EXEC);
505 return error;
506
507 exec_abort:
508 /*
509 * the old process doesn't exist anymore. exit gracefully.
510 * get rid of the (new) address space we have created, if any, get rid
511 * of our namei data and vnode, and exit noting failure
512 */
513 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
514 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
515 if (pack.ep_emul_arg)
516 FREE(pack.ep_emul_arg, M_TEMP);
517 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
518 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
519 vput(pack.ep_vp);
520 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
521 FREE(pack.ep_hdr, M_EXEC);
522 exit1(p, W_EXITCODE(0, SIGABRT));
523 exit1(p, -1);
524
525 /* NOTREACHED */
526 return 0;
527 }
528
529
530 void *
531 copyargs(pack, arginfo, stack, argp)
532 struct exec_package *pack;
533 struct ps_strings *arginfo;
534 void *stack;
535 void *argp;
536 {
537 char **cpp = stack;
538 char *dp, *sp;
539 size_t len;
540 void *nullp = NULL;
541 int argc = arginfo->ps_nargvstr;
542 int envc = arginfo->ps_nenvstr;
543
544 if (copyout(&argc, cpp++, sizeof(argc)))
545 return NULL;
546
547 dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
548 sp = argp;
549
550 /* XXX don't copy them out, remap them! */
551 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
552
553 for (; --argc >= 0; sp += len, dp += len)
554 if (copyout(&dp, cpp++, sizeof(dp)) ||
555 copyoutstr(sp, dp, ARG_MAX, &len))
556 return NULL;
557
558 if (copyout(&nullp, cpp++, sizeof(nullp)))
559 return NULL;
560
561 arginfo->ps_envstr = cpp; /* remember location of envp for later */
562
563 for (; --envc >= 0; sp += len, dp += len)
564 if (copyout(&dp, cpp++, sizeof(dp)) ||
565 copyoutstr(sp, dp, ARG_MAX, &len))
566 return NULL;
567
568 if (copyout(&nullp, cpp++, sizeof(nullp)))
569 return NULL;
570
571 return cpp;
572 }
573