xref: /original-bsd/sys/kern/kern_exec.c (revision 703f6d5d)
1 /*-
2  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  *
7  *	@(#)kern_exec.c	7.47 (Berkeley) 06/21/91
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "filedesc.h"
13 #include "kernel.h"
14 #include "proc.h"
15 #include "mount.h"
16 #include "malloc.h"
17 #include "namei.h"
18 #include "vnode.h"
19 #include "seg.h"
20 #include "file.h"
21 #include "acct.h"
22 #include "exec.h"
23 #include "ktrace.h"
24 #include "resourcevar.h"
25 
26 #include "machine/cpu.h"
27 #include "machine/reg.h"
28 
29 #include "mman.h"
30 #include "vm/vm.h"
31 #include "vm/vm_param.h"
32 #include "vm/vm_map.h"
33 #include "vm/vm_kern.h"
34 #include "vm/vm_pager.h"
35 
36 #include "signalvar.h"
37 #include "kinfo_proc.h"
38 
39 #ifdef HPUXCOMPAT
40 #include "user.h"			/* for pcb */
41 #include "hp300/hpux/hpux_exec.h"
42 #endif
43 
44 #ifdef COPY_SIGCODE
45 extern char sigcode[], esigcode[];
46 #define	szsigcode	(esigcode - sigcode)
47 #else
48 #define	szsigcode	0
49 #endif
50 
51 /*
52  * exec system call
53  */
54 /* ARGSUSED */
55 execve(p, uap, retval)
56 	register struct proc *p;
57 	register struct args {
58 		char	*fname;
59 		char	**argp;
60 		char	**envp;
61 	} *uap;
62 	int *retval;
63 {
64 	register struct ucred *cred = p->p_ucred;
65 	register struct nameidata *ndp;
66 	register struct filedesc *fdp = p->p_fd;
67 	int na, ne, ucp, ap, cc;
68 	register char *cp;
69 	register int nc;
70 	unsigned len;
71 	int indir, uid, gid;
72 	char *sharg;
73 	struct vnode *vp;
74 	int resid, error, paged = 0;
75 	vm_offset_t execargs;
76 	struct vattr vattr;
77 	char cfarg[MAXINTERP];
78 	union {
79 		char	ex_shell[MAXINTERP];	/* #! and interpreter name */
80 		struct	exec ex_exec;
81 #ifdef HPUXCOMPAT
82 		struct	hpux_exec ex_hexec;
83 #endif
84 	} exdata;
85 #ifdef HPUXCOMPAT
86 	struct hpux_exec hhead;
87 #endif
88 	struct nameidata nd;
89 
90 	ndp = &nd;
91 	ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF | SAVENAME;
92 	ndp->ni_segflg = UIO_USERSPACE;
93 	ndp->ni_dirp = uap->fname;
94 	if (error = namei(ndp, p))
95 		return (error);
96 	vp = ndp->ni_vp;
97 	indir = 0;
98 	uid = cred->cr_uid;
99 	gid = cred->cr_gid;
100 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
101 		goto bad;
102 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
103 		error = EACCES;
104 		goto bad;
105 	}
106 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) == 0) {
107 		if (vattr.va_mode & VSUID)
108 			uid = vattr.va_uid;
109 		if (vattr.va_mode & VSGID)
110 			gid = vattr.va_gid;
111 	}
112 
113   again:
114 	if (error = VOP_ACCESS(vp, VEXEC, cred, p))
115 		goto bad;
116 	if ((p->p_flag & STRC) && (error = VOP_ACCESS(vp, VREAD, cred, p)))
117 		goto bad;
118 	if (vp->v_type != VREG ||
119 	    (vattr.va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
120 		error = EACCES;
121 		goto bad;
122 	}
123 
124 	/*
125 	 * Read in first few bytes of file for segment sizes, magic number:
126 	 *	OMAGIC = plain executable
127 	 *	NMAGIC = RO text
128 	 *	ZMAGIC = demand paged RO text
129 	 * Also an ASCII line beginning with #! is
130 	 * the file name of a ``shell'' and arguments may be prepended
131 	 * to the argument list if given here.
132 	 *
133 	 * SHELL NAMES ARE LIMITED IN LENGTH.
134 	 *
135 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
136 	 * THE ASCII LINE.
137 	 */
138 	exdata.ex_shell[0] = '\0';	/* for zero length files */
139 	error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata, sizeof (exdata),
140 	    (off_t)0, UIO_SYSSPACE, (IO_UNIT|IO_NODELOCKED), cred, &resid,
141 	    (struct proc *)0);
142 	if (error)
143 		goto bad;
144 #ifndef lint
145 	if (resid > sizeof(exdata) - sizeof(exdata.ex_exec) &&
146 	    exdata.ex_shell[0] != '#') {
147 		error = ENOEXEC;
148 		goto bad;
149 	}
150 #endif
151 #if defined(hp300)
152 	switch ((int)exdata.ex_exec.a_mid) {
153 
154 	/*
155 	 * An ancient hp200 or hp300 binary, shouldn't happen anymore.
156 	 * Mark as invalid.
157 	 */
158 	case MID_ZERO:
159 		exdata.ex_exec.a_magic = 0;
160 		break;
161 
162 	/*
163 	 * HP200 series has a smaller page size so we cannot
164 	 * demand-load or even write protect text, so we just
165 	 * treat as OMAGIC.
166 	 */
167 	case MID_HP200:
168 		exdata.ex_exec.a_magic = OMAGIC;
169 		break;
170 
171 	case MID_HP300:
172 		break;
173 
174 #ifdef HPUXCOMPAT
175 	case MID_HPUX:
176 		/*
177 		 * Save a.out header.  This is eventually saved in the pcb,
178 		 * but we cannot do that yet in case the exec fails before
179 		 * the image is overlayed.
180 		 */
181 		bcopy((caddr_t)&exdata.ex_hexec,
182 		      (caddr_t)&hhead, sizeof hhead);
183 		/*
184 		 * If version number is 0x2bad this is a native BSD
185 		 * binary created via the HPUX SGS.  Should not be
186 		 * treated as an HPUX binary.
187 		 */
188 		if (exdata.ex_hexec.ha_version != BSDVNUM)
189 			paged |= SHPUX;				/* XXX */
190 		/*
191 		 * Shuffle important fields to their BSD locations.
192 		 * Note that the order in which this is done is important.
193 		 */
194 		exdata.ex_exec.a_text = exdata.ex_hexec.ha_text;
195 		exdata.ex_exec.a_data = exdata.ex_hexec.ha_data;
196 		exdata.ex_exec.a_bss = exdata.ex_hexec.ha_bss;
197 		exdata.ex_exec.a_entry = exdata.ex_hexec.ha_entry;
198 		/*
199 		 * For ZMAGIC files, make sizes consistant with those
200 		 * generated by BSD ld.
201 		 */
202 		if (exdata.ex_exec.a_magic == ZMAGIC) {
203 			exdata.ex_exec.a_text =
204 				ctob(btoc(exdata.ex_exec.a_text));
205 			nc = exdata.ex_exec.a_data + exdata.ex_exec.a_bss;
206 			exdata.ex_exec.a_data =
207 				ctob(btoc(exdata.ex_exec.a_data));
208 			nc -= (int)exdata.ex_exec.a_data;
209 			exdata.ex_exec.a_bss = (nc < 0) ? 0 : nc;
210 		}
211 		break;
212 #endif
213 	}
214 #endif
215 	switch ((int)exdata.ex_exec.a_magic) {
216 
217 	case OMAGIC:
218 		exdata.ex_exec.a_data += exdata.ex_exec.a_text;
219 		exdata.ex_exec.a_text = 0;
220 		break;
221 
222 	case ZMAGIC:
223 		paged++;
224 		/* FALLTHROUGH */
225 	case NMAGIC:
226 		if (exdata.ex_exec.a_text == 0) {
227 			error = ENOEXEC;
228 			goto bad;
229 		}
230 		break;
231 
232 	default:
233 		if (exdata.ex_shell[0] != '#' ||
234 		    exdata.ex_shell[1] != '!' ||
235 		    indir) {
236 			error = ENOEXEC;
237 			goto bad;
238 		}
239 		for (cp = &exdata.ex_shell[2];; ++cp) {
240 			if (cp >= &exdata.ex_shell[MAXINTERP]) {
241 				error = ENOEXEC;
242 				goto bad;
243 			}
244 			if (*cp == '\n') {
245 				*cp = '\0';
246 				break;
247 			}
248 			if (*cp == '\t')
249 				*cp = ' ';
250 		}
251 		cp = &exdata.ex_shell[2];
252 		while (*cp == ' ')
253 			cp++;
254 		ndp->ni_dirp = cp;
255 		while (*cp && *cp != ' ')
256 			cp++;
257 		cfarg[0] = '\0';
258 		if (*cp) {
259 			*cp++ = '\0';
260 			while (*cp == ' ')
261 				cp++;
262 			if (*cp)
263 				bcopy((caddr_t)cp, (caddr_t)cfarg, MAXINTERP);
264 		}
265 		indir = 1;
266 		vput(vp);
267 		ndp->ni_segflg = UIO_SYSSPACE;
268 		if (error = namei(ndp, p))
269 			return (error);
270 		vp = ndp->ni_vp;
271 		if (error = VOP_GETATTR(vp, &vattr, cred, p))
272 			goto bad;
273 		uid = cred->cr_uid;	/* shell scripts can't be setuid */
274 		gid = cred->cr_gid;
275 		goto again;
276 	}
277 
278 	/*
279 	 * Collect arguments on "file" in swap space.
280 	 */
281 	na = 0;
282 	ne = 0;
283 	nc = 0;
284 	cc = NCARGS;
285 	execargs = kmem_alloc_wait(exec_map, NCARGS);
286 	cp = (char *) execargs;
287 	/*
288 	 * Copy arguments into file in argdev area.
289 	 */
290 	if (uap->argp) for (;;) {
291 		ap = NULL;
292 		sharg = NULL;
293 		if (indir && na == 0) {
294 			sharg = ndp->ni_ptr;
295 			ap = (int)sharg;
296 			uap->argp++;		/* ignore argv[0] */
297 		} else if (indir && (na == 1 && cfarg[0])) {
298 			sharg = cfarg;
299 			ap = (int)sharg;
300 		} else if (indir && (na == 1 || na == 2 && cfarg[0]))
301 			ap = (int)uap->fname;
302 		else if (uap->argp) {
303 			ap = fuword((caddr_t)uap->argp);
304 			uap->argp++;
305 		}
306 		if (ap == NULL && uap->envp) {
307 			uap->argp = NULL;
308 			if ((ap = fuword((caddr_t)uap->envp)) != NULL)
309 				uap->envp++, ne++;
310 		}
311 		if (ap == NULL)
312 			break;
313 		na++;
314 		if (ap == -1) {
315 			error = EFAULT;
316 			goto bad;
317 		}
318 		do {
319 			if (nc >= NCARGS-1) {
320 				error = E2BIG;
321 				break;
322 			}
323 			if (sharg) {
324 				error = copystr(sharg, cp, (unsigned)cc, &len);
325 				sharg += len;
326 			} else {
327 				error = copyinstr((caddr_t)ap, cp, (unsigned)cc,
328 				    &len);
329 				ap += len;
330 			}
331 			cp += len;
332 			nc += len;
333 			cc -= len;
334 		} while (error == ENAMETOOLONG);
335 		if (error)
336 			goto bad;
337 	}
338 	nc = (nc + NBPW-1) & ~(NBPW-1);
339 	error = getxfile(p, vp, &exdata.ex_exec, paged, nc + (na+4)*NBPW,
340 	    uid, gid);
341 	if (error)
342 		goto bad;
343 	vput(vp);
344 	vp = NULL;
345 
346 #ifdef HPUXCOMPAT
347 	/*
348 	 * We are now committed to the exec so we can save the exec
349 	 * header in the pcb where we can dump it if necessary in core()
350 	 */
351 	if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXBIN)
352 		bcopy((caddr_t)&hhead,
353 		      (caddr_t)p->p_addr->u_pcb.pcb_exec, sizeof hhead);
354 #endif
355 
356 	/*
357 	 * Copy back arglist.
358 	 */
359 	ucp = USRSTACK - szsigcode - nc - NBPW;
360 	ap = ucp - na*NBPW - 3*NBPW;
361 	p->p_regs[SP] = ap;
362 	(void) suword((caddr_t)ap, na-ne);
363 	nc = 0;
364 	cp = (char *) execargs;
365 	cc = NCARGS;
366 	for (;;) {
367 		ap += NBPW;
368 		if (na == ne) {
369 			(void) suword((caddr_t)ap, 0);
370 			ap += NBPW;
371 		}
372 		if (--na < 0)
373 			break;
374 		(void) suword((caddr_t)ap, ucp);
375 		do {
376 			error = copyoutstr(cp, (caddr_t)ucp, (unsigned)cc,
377 			    &len);
378 			ucp += len;
379 			cp += len;
380 			nc += len;
381 			cc -= len;
382 		} while (error == ENAMETOOLONG);
383 		if (error == EFAULT)
384 			panic("exec: EFAULT");
385 	}
386 	(void) suword((caddr_t)ap, 0);
387 
388 	execsigs(p);
389 
390 	for (nc = fdp->fd_lastfile; nc >= 0; --nc) {
391 		if (fdp->fd_ofileflags[nc] & UF_EXCLOSE) {
392 			(void) closef(fdp->fd_ofiles[nc], p);
393 			fdp->fd_ofiles[nc] = NULL;
394 			fdp->fd_ofileflags[nc] = 0;
395 			if (nc < fdp->fd_freefile)
396 				fdp->fd_freefile = nc;
397 		}
398 		fdp->fd_ofileflags[nc] &= ~UF_MAPPED;
399 	}
400 	/*
401 	 * Adjust fd_lastfile to account for descriptors closed above.
402 	 * Don't decrement fd_lastfile past 0, as it's unsigned.
403 	 */
404 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
405 		fdp->fd_lastfile--;
406 	setregs(p, exdata.ex_exec.a_entry, retval);
407 #ifdef COPY_SIGCODE
408 	/*
409 	 * Install sigcode at top of user stack.
410 	 */
411 	copyout((caddr_t)sigcode, (caddr_t)(USRSTACK - szsigcode), szsigcode);
412 #endif
413 	/*
414 	 * Remember file name for accounting.
415 	 */
416 	p->p_acflag &= ~AFORK;
417 	if (ndp->ni_namelen > MAXCOMLEN)
418 		ndp->ni_namelen = MAXCOMLEN;
419 	bcopy((caddr_t)ndp->ni_ptr, (caddr_t)p->p_comm,
420 	    (unsigned)(ndp->ni_namelen));
421 	p->p_comm[ndp->ni_namelen] = '\0';
422 	cpu_exec(p);
423 bad:
424 	FREE(ndp->ni_pnbuf, M_NAMEI);
425 	if (execargs)
426 		kmem_free_wakeup(exec_map, execargs, NCARGS);
427 	if (vp)
428 		vput(vp);
429 	return (error);
430 }
431 
432 /*
433  * Read in and set up memory for executed file.
434  */
435 getxfile(p, vp, ep, paged, nargc, uid, gid)
436 	register struct proc *p;
437 	register struct vnode *vp;
438 	register struct exec *ep;
439 	int paged, nargc, uid, gid;
440 {
441 	segsz_t ts, ds, ss;
442 	register struct ucred *cred = p->p_ucred;
443 	off_t toff;
444 	int error = 0;
445 	vm_offset_t addr;
446 	vm_size_t size;
447 	struct vmspace *vm = p->p_vmspace;
448 
449 #ifdef HPUXCOMPAT
450 	int hpux = (paged & SHPUX);
451 	paged &= ~SHPUX;
452 	if (ep->a_mid == MID_HPUX) {
453 		if (paged)
454 			toff = CLBYTES;
455 		else
456 			toff = sizeof (struct hpux_exec);
457 	} else
458 #endif
459 	if (paged)
460 		toff = CLBYTES;
461 	else
462 		toff = sizeof (struct exec);
463 	if (ep->a_text != 0 && (vp->v_flag & VTEXT) == 0 &&
464 	    vp->v_writecount != 0)
465 		return (ETXTBSY);
466 
467 	/*
468 	 * Compute text and data sizes and make sure not too large.
469 	 * NB - Check data and bss separately as they may overflow
470 	 * when summed together.
471 	 */
472 	ts = clrnd(btoc(ep->a_text));
473 	ds = clrnd(btoc(ep->a_data + ep->a_bss));
474 	ss = clrnd(SSIZE + btoc(nargc + szsigcode));
475 
476 	/*
477 	 * If we're sharing the address space, allocate a new space
478 	 * and release our reference to the old one.  Otherwise,
479 	 * empty out the existing vmspace.
480 	 */
481 	if (vm->vm_refcnt > 1) {
482 		p->p_vmspace = vmspace_alloc(VM_MIN_ADDRESS,
483 		    VM_MAXUSER_ADDRESS, 1);
484 		vmspace_free(vm);
485 		vm = p->p_vmspace;
486 	} else {
487 #ifdef SYSVSHM
488 		if (vm->vm_shm)
489 			shmexit(p);
490 #endif
491 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
492 		    VM_MAXUSER_ADDRESS);
493 	}
494 	/*
495 	 * If parent is waiting for us to exec or exit,
496 	 * SPPWAIT will be set; clear it and wakeup parent.
497 	 */
498 	if (p->p_flag & SPPWAIT) {
499 		p->p_flag &= ~SPPWAIT;
500 		wakeup((caddr_t) p->p_pptr);
501 	}
502 #ifdef HPUXCOMPAT
503 	p->p_addr->u_pcb.pcb_flags &= ~(PCB_HPUXMMAP|PCB_HPUXBIN);
504 	/* remember that we were loaded from an HPUX format file */
505 	if (ep->a_mid == MID_HPUX)
506 		p->p_addr->u_pcb.pcb_flags |= PCB_HPUXBIN;
507 	if (hpux)
508 		p->p_flag |= SHPUX;
509 	else
510 		p->p_flag &= ~SHPUX;
511 #endif
512 	p->p_flag |= SEXEC;
513 	addr = VM_MIN_ADDRESS;
514 	if (vm_allocate(&vm->vm_map, &addr, round_page(ctob(ts + ds)), FALSE)) {
515 		uprintf("Cannot allocate text+data space\n");
516 		error = ENOMEM;			/* XXX */
517 		goto badmap;
518 	}
519 	size = round_page(MAXSSIZ);		/* XXX */
520 #ifdef	i386
521 	addr = trunc_page(USRSTACK - size) - NBPG;	/* XXX */
522 #else
523 	addr = trunc_page(USRSTACK - size);
524 #endif
525 	if (vm_allocate(&vm->vm_map, &addr, size, FALSE)) {
526 		uprintf("Cannot allocate stack space\n");
527 		error = ENOMEM;			/* XXX */
528 		goto badmap;
529 	}
530 	size -= round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
531 	if (vm_map_protect(&vm->vm_map, addr, addr+size, VM_PROT_NONE, FALSE)) {
532 		uprintf("Cannot protect stack space\n");
533 		error = ENOMEM;
534 		goto badmap;
535 	}
536 	vm->vm_maxsaddr = (caddr_t)addr;
537 	vm->vm_taddr = (caddr_t)VM_MIN_ADDRESS;
538 	vm->vm_daddr = (caddr_t)(VM_MIN_ADDRESS + ctob(ts));
539 
540 	if (paged == 0) {
541 		/*
542 		 * Read in data segment.
543 		 */
544 		(void) vn_rdwr(UIO_READ, vp, vm->vm_daddr, (int) ep->a_data,
545 			(off_t)(toff + ep->a_text), UIO_USERSPACE,
546 			(IO_UNIT|IO_NODELOCKED), cred, (int *)0, p);
547 		/*
548 		 * Read in text segment if necessary (0410),
549 		 * and read-protect it.
550 		 */
551 		if (ep->a_text > 0) {
552 			error = vn_rdwr(UIO_READ, vp, vm->vm_taddr,
553 				(int)ep->a_text, toff, UIO_USERSPACE,
554 				(IO_UNIT|IO_NODELOCKED), cred, (int *)0, p);
555 			(void) vm_map_protect(&vm->vm_map, VM_MIN_ADDRESS,
556 				VM_MIN_ADDRESS + trunc_page(ep->a_text),
557 				VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
558 		}
559 	} else {
560 		/*
561 		 * Allocate a region backed by the exec'ed vnode.
562 		 */
563 		addr = VM_MIN_ADDRESS;
564 		size = round_page(ep->a_text + ep->a_data);
565 		error = vm_mmap(&vm->vm_map, &addr, size, VM_PROT_ALL,
566 			MAP_FILE|MAP_COPY|MAP_FIXED,
567 			(caddr_t)vp, (vm_offset_t)toff);
568 		(void) vm_map_protect(&vm->vm_map, addr,
569 			addr + trunc_page(ep->a_text),
570 			VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
571 		vp->v_flag |= VTEXT;
572 	}
573 badmap:
574 	if (error) {
575 		printf("pid %d: VM allocation failure\n", p->p_pid);
576 		uprintf("sorry, pid %d was killed in exec: VM allocation\n",
577 			p->p_pid);
578 		psignal(p, SIGKILL);
579 		p->p_flag |= SKEEP;
580 		return(error);
581 	}
582 
583 	/*
584 	 * set SUID/SGID protections, if no tracing
585 	 */
586 	if ((p->p_flag&STRC)==0) {
587 		if (uid != cred->cr_uid || gid != cred->cr_gid) {
588 			p->p_ucred = cred = crcopy(cred);
589 			/*
590 			 * If process is being ktraced, turn off - unless
591 			 * root set it.
592 			 */
593 			if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
594 				vrele(p->p_tracep);
595 				p->p_tracep = NULL;
596 				p->p_traceflag = 0;
597 			}
598 		}
599 		cred->cr_uid = uid;
600 		cred->cr_gid = gid;
601 	} else
602 		psignal(p, SIGTRAP);
603 	p->p_cred->p_svuid = cred->cr_uid;
604 	p->p_cred->p_svgid = cred->cr_gid;
605 	vm->vm_tsize = ts;
606 	vm->vm_dsize = ds;
607 	vm->vm_ssize = ss;
608 	p->p_stats->p_prof.pr_scale = 0;
609 #if defined(tahoe)
610 	/* move this when tahoe cpu_exec is created */
611 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
612 #endif
613 	return (0);
614 }
615