1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)kern_exec.c 7.18 (Berkeley) 12/29/89 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "map.h" 23 #include "user.h" 24 #include "kernel.h" 25 #include "proc.h" 26 #include "mount.h" 27 #include "ucred.h" 28 #include "malloc.h" 29 #include "buf.h" 30 #include "vnode.h" 31 #include "seg.h" 32 #include "vm.h" 33 #include "text.h" 34 #include "file.h" 35 #include "uio.h" 36 #include "acct.h" 37 #include "exec.h" 38 39 #include "machine/reg.h" 40 #include "machine/pte.h" 41 #include "machine/psl.h" 42 #include "machine/mtpr.h" 43 44 /* 45 * exec system call, with and without environments. 46 */ 47 struct execa { 48 char *fname; 49 char **argp; 50 char **envp; 51 }; 52 53 execv() 54 { 55 ((struct execa *)u.u_ap)->envp = NULL; 56 execve(); 57 } 58 59 execve() 60 { 61 register nc; 62 register char *cp; 63 register struct buf *bp; 64 struct buf *tbp; 65 register struct execa *uap; 66 int na, ne, ucp, ap, cc; 67 unsigned len; 68 int indir, uid, gid; 69 char *sharg; 70 struct vnode *vp; 71 swblk_t bno; 72 struct vattr vattr; 73 char cfname[MAXCOMLEN + 1]; 74 char cfarg[MAXINTERP]; 75 union { 76 char ex_shell[MAXINTERP]; /* #! and interpreter name */ 77 struct exec ex_exec; 78 } exdata; 79 register struct nameidata *ndp = &u.u_nd; 80 int resid, error; 81 82 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 83 ndp->ni_segflg = UIO_USERSPACE; 84 ndp->ni_dirp = ((struct execa *)u.u_ap)->fname; 85 if (u.u_error = namei(ndp)) { 86 return; 87 } 88 vp = ndp->ni_vp; 89 bno = 0; 90 bp = 0; 91 indir = 0; 92 uid = u.u_cred->cr_uid; 93 gid = u.u_cred->cr_gid; 94 if (u.u_error = VOP_GETATTR(vp, &vattr, u.u_cred)) 95 goto bad; 96 if (vp->v_mount->m_flag & M_NOEXEC) { 97 u.u_error = EACCES; 98 goto bad; 99 } 100 if ((vp->v_mount->m_flag & M_NOSUID) == 0) { 101 if (vattr.va_mode & VSUID) 102 uid = vattr.va_uid; 103 if (vattr.va_mode & VSGID) 104 gid = vattr.va_gid; 105 } 106 107 again: 108 if (u.u_error = VOP_ACCESS(vp, VEXEC, u.u_cred)) 109 goto bad; 110 if ((u.u_procp->p_flag & STRC) && 111 (u.u_error = VOP_ACCESS(vp, VREAD, u.u_cred))) 112 goto bad; 113 if (vp->v_type != VREG || 114 (vattr.va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) { 115 u.u_error = EACCES; 116 goto bad; 117 } 118 119 /* 120 * Read in first few bytes of file for segment sizes, magic number: 121 * OMAGIC = plain executable 122 * NMAGIC = RO text 123 * ZMAGIC = demand paged RO text 124 * Also an ASCII line beginning with #! is 125 * the file name of a ``shell'' and arguments may be prepended 126 * to the argument list if given here. 127 * 128 * SHELL NAMES ARE LIMITED IN LENGTH. 129 * 130 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM 131 * THE ASCII LINE. 132 */ 133 exdata.ex_shell[0] = '\0'; /* for zero length files */ 134 u.u_error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata, sizeof (exdata), 135 (off_t)0, UIO_SYSSPACE, (IO_UNIT|IO_NODELOCKED), u.u_cred, &resid); 136 if (u.u_error) 137 goto bad; 138 #ifndef lint 139 if (resid > sizeof(exdata) - sizeof(exdata.ex_exec) && 140 exdata.ex_shell[0] != '#') { 141 u.u_error = ENOEXEC; 142 goto bad; 143 } 144 #endif 145 switch ((int)exdata.ex_exec.a_magic) { 146 147 case OMAGIC: 148 exdata.ex_exec.a_data += exdata.ex_exec.a_text; 149 exdata.ex_exec.a_text = 0; 150 break; 151 152 case ZMAGIC: 153 case NMAGIC: 154 if (exdata.ex_exec.a_text == 0) { 155 u.u_error = ENOEXEC; 156 goto bad; 157 } 158 break; 159 160 default: 161 if (exdata.ex_shell[0] != '#' || 162 exdata.ex_shell[1] != '!' || 163 indir) { 164 u.u_error = ENOEXEC; 165 goto bad; 166 } 167 for (cp = &exdata.ex_shell[2];; ++cp) { 168 if (cp >= &exdata.ex_shell[MAXINTERP]) { 169 u.u_error = ENOEXEC; 170 goto bad; 171 } 172 if (*cp == '\n') { 173 *cp = '\0'; 174 break; 175 } 176 if (*cp == '\t') 177 *cp = ' '; 178 } 179 cp = &exdata.ex_shell[2]; 180 while (*cp == ' ') 181 cp++; 182 ndp->ni_dirp = cp; 183 while (*cp && *cp != ' ') 184 cp++; 185 cfarg[0] = '\0'; 186 if (*cp) { 187 *cp++ = '\0'; 188 while (*cp == ' ') 189 cp++; 190 if (*cp) 191 bcopy((caddr_t)cp, (caddr_t)cfarg, MAXINTERP); 192 } 193 indir = 1; 194 vput(vp); 195 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 196 ndp->ni_segflg = UIO_SYSSPACE; 197 if (u.u_error = namei(ndp)) 198 return; 199 vp = ndp->ni_vp; 200 if (u.u_error = VOP_GETATTR(vp, &vattr, u.u_cred)) 201 goto bad; 202 bcopy((caddr_t)ndp->ni_dent.d_name, (caddr_t)cfname, 203 MAXCOMLEN); 204 cfname[MAXCOMLEN] = '\0'; 205 uid = u.u_cred->cr_uid; /* shell scripts can't be setuid */ 206 gid = u.u_cred->cr_gid; 207 goto again; 208 } 209 210 /* 211 * Collect arguments on "file" in swap space. 212 */ 213 na = 0; 214 ne = 0; 215 nc = 0; 216 cc = 0; 217 uap = (struct execa *)u.u_ap; 218 bno = rmalloc(argmap, (long)ctod(clrnd((int)btoc(NCARGS)))); 219 if (bno == 0) { 220 swkill(u.u_procp, "exec: no swap space"); 221 goto bad; 222 } 223 if (bno % CLSIZE) 224 panic("execa rmalloc"); 225 /* 226 * Copy arguments into file in argdev area. 227 */ 228 if (uap->argp) for (;;) { 229 ap = NULL; 230 sharg = NULL; 231 if (indir && na == 0) { 232 sharg = cfname; 233 ap = (int)sharg; 234 uap->argp++; /* ignore argv[0] */ 235 } else if (indir && (na == 1 && cfarg[0])) { 236 sharg = cfarg; 237 ap = (int)sharg; 238 } else if (indir && (na == 1 || na == 2 && cfarg[0])) 239 ap = (int)uap->fname; 240 else if (uap->argp) { 241 ap = fuword((caddr_t)uap->argp); 242 uap->argp++; 243 } 244 if (ap == NULL && uap->envp) { 245 uap->argp = NULL; 246 if ((ap = fuword((caddr_t)uap->envp)) != NULL) 247 uap->envp++, ne++; 248 } 249 if (ap == NULL) 250 break; 251 na++; 252 if (ap == -1) { 253 u.u_error = EFAULT; 254 break; 255 } 256 do { 257 if (cc <= 0) { 258 /* 259 * We depend on NCARGS being a multiple of 260 * CLBYTES. This way we need only check 261 * overflow before each buffer allocation. 262 */ 263 if (nc >= NCARGS-1) { 264 error = E2BIG; 265 break; 266 } 267 if (bp) 268 bdwrite(bp); 269 cc = CLBYTES; 270 bp = getblk(argdev_vp, bno + ctod(nc/NBPG), cc); 271 cp = bp->b_un.b_addr; 272 } 273 if (sharg) { 274 error = copystr(sharg, cp, (unsigned)cc, &len); 275 sharg += len; 276 } else { 277 error = copyinstr((caddr_t)ap, cp, (unsigned)cc, 278 &len); 279 ap += len; 280 } 281 cp += len; 282 nc += len; 283 cc -= len; 284 } while (error == ENOENT); 285 if (error) { 286 u.u_error = error; 287 if (bp) 288 brelse(bp); 289 bp = 0; 290 goto badarg; 291 } 292 } 293 if (bp) 294 bdwrite(bp); 295 bp = 0; 296 nc = (nc + NBPW-1) & ~(NBPW-1); 297 getxfile(vp, &exdata.ex_exec, nc + (na+4)*NBPW, uid, gid, u.u_cred); 298 if (u.u_error) { 299 badarg: 300 for (cc = 0; cc < nc; cc += CLBYTES) { 301 (void) baddr(argdev_vp, bno + ctod(cc/NBPG), 302 CLBYTES, NOCRED, &tbp); 303 bp = tbp; 304 if (bp) { 305 bp->b_flags |= B_AGE; /* throw away */ 306 bp->b_flags &= ~B_DELWRI; /* cancel io */ 307 brelse(bp); 308 bp = 0; 309 } 310 } 311 goto bad; 312 } 313 vput(vp); 314 vp = NULL; 315 316 /* 317 * Copy back arglist. 318 */ 319 ucp = USRSTACK - nc - NBPW; 320 ap = ucp - na*NBPW - 3*NBPW; 321 u.u_ar0[SP] = ap; 322 (void) suword((caddr_t)ap, na-ne); 323 nc = 0; 324 cc = 0; 325 for (;;) { 326 ap += NBPW; 327 if (na == ne) { 328 (void) suword((caddr_t)ap, 0); 329 ap += NBPW; 330 } 331 if (--na < 0) 332 break; 333 (void) suword((caddr_t)ap, ucp); 334 do { 335 if (cc <= 0) { 336 if (bp) 337 brelse(bp); 338 cc = CLBYTES; 339 error = bread(argdev_vp, 340 (daddr_t)(bno + ctod(nc / NBPG)), cc, 341 NOCRED, &tbp); 342 bp = tbp; 343 bp->b_flags |= B_AGE; /* throw away */ 344 bp->b_flags &= ~B_DELWRI; /* cancel io */ 345 cp = bp->b_un.b_addr; 346 } 347 error = copyoutstr(cp, (caddr_t)ucp, (unsigned)cc, 348 &len); 349 ucp += len; 350 cp += len; 351 nc += len; 352 cc -= len; 353 } while (error == ENOENT); 354 if (error == EFAULT) 355 panic("exec: EFAULT"); 356 } 357 (void) suword((caddr_t)ap, 0); 358 359 execsigs(u.u_procp); 360 361 for (nc = u.u_lastfile; nc >= 0; --nc) { 362 if (u.u_pofile[nc] & UF_EXCLOSE) { 363 (void) closef(u.u_ofile[nc]); 364 u.u_ofile[nc] = NULL; 365 u.u_pofile[nc] = 0; 366 } 367 u.u_pofile[nc] &= ~UF_MAPPED; 368 } 369 while (u.u_lastfile >= 0 && u.u_ofile[u.u_lastfile] == NULL) 370 u.u_lastfile--; 371 setregs(exdata.ex_exec.a_entry); 372 /* 373 * Remember file name for accounting. 374 */ 375 u.u_acflag &= ~AFORK; 376 if (indir) 377 bcopy((caddr_t)cfname, (caddr_t)u.u_comm, MAXCOMLEN); 378 else { 379 if (ndp->ni_dent.d_namlen > MAXCOMLEN) 380 ndp->ni_dent.d_namlen = MAXCOMLEN; 381 bcopy((caddr_t)ndp->ni_dent.d_name, (caddr_t)u.u_comm, 382 (unsigned)(ndp->ni_dent.d_namlen + 1)); 383 } 384 bad: 385 if (bp) 386 brelse(bp); 387 if (bno) 388 rmfree(argmap, (long)ctod(clrnd((int) btoc(NCARGS))), bno); 389 if (vp) 390 vput(vp); 391 } 392 393 /* 394 * Read in and set up memory for executed file. 395 */ 396 getxfile(vp, ep, nargc, uid, gid, cred) 397 register struct vnode *vp; 398 register struct exec *ep; 399 int nargc, uid, gid; 400 struct ucred *cred; 401 { 402 register struct proc *p = u.u_procp; 403 size_t ts, ds, ids, uds, ss; 404 int pagi; 405 406 if (ep->a_magic == ZMAGIC) 407 pagi = SPAGV; 408 else 409 pagi = 0; 410 if (vp->v_text && (vp->v_text->x_flag & XTRC)) { 411 u.u_error = ETXTBSY; 412 goto bad; 413 } 414 if (ep->a_text != 0 && (vp->v_flag & VTEXT) == 0 && 415 vp->v_usecount != 1) { 416 register struct file *fp; 417 418 for (fp = file; fp < fileNFILE; fp++) { 419 if (fp->f_type == DTYPE_VNODE && 420 fp->f_count > 0 && 421 (struct vnode *)fp->f_data == vp && 422 (fp->f_flag & FWRITE)) { 423 u.u_error = ETXTBSY; 424 goto bad; 425 } 426 } 427 } 428 429 /* 430 * Compute text and data sizes and make sure not too large. 431 * NB - Check data and bss separately as they may overflow 432 * when summed together. 433 */ 434 ts = clrnd(btoc(ep->a_text)); 435 ids = clrnd(btoc(ep->a_data)); 436 uds = clrnd(btoc(ep->a_bss)); 437 ds = clrnd(btoc(ep->a_data + ep->a_bss)); 438 ss = clrnd(SSIZE + btoc(nargc)); 439 if (chksize((unsigned)ts, (unsigned)ids, (unsigned)uds, (unsigned)ss)) 440 goto bad; 441 442 /* 443 * Make sure enough space to start process. 444 */ 445 u.u_cdmap = zdmap; 446 u.u_csmap = zdmap; 447 if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) 448 goto bad; 449 450 /* 451 * At this point, we are committed to the new image! 452 * Release virtual memory resources of old process, and 453 * initialize the virtual memory of the new process. 454 * If we resulted from vfork(), instead wakeup our 455 * parent who will set SVFDONE when he has taken back 456 * our resources. 457 */ 458 if ((p->p_flag & SVFORK) == 0) 459 vrelvm(); 460 else { 461 p->p_flag &= ~SVFORK; 462 p->p_flag |= SKEEP; 463 wakeup((caddr_t)p); 464 while ((p->p_flag & SVFDONE) == 0) 465 sleep((caddr_t)p, PZERO - 1); 466 p->p_flag &= ~(SVFDONE|SKEEP); 467 } 468 p->p_flag &= ~(SPAGV|SSEQL|SUANOM); 469 p->p_flag |= pagi | SEXEC; 470 u.u_dmap = u.u_cdmap; 471 u.u_smap = u.u_csmap; 472 vgetvm(ts, ds, ss); 473 474 if (pagi == 0) 475 u.u_error = vn_rdwr(UIO_READ, vp, 476 (char *)ctob(dptov(u.u_procp, 0)), 477 (int)ep->a_data, 478 (off_t)(sizeof (struct exec) + ep->a_text), 479 UIO_USERSPACE, (IO_UNIT|IO_NODELOCKED), cred, (int *)0); 480 xalloc(vp, ep, pagi, cred); 481 #if defined(tahoe) 482 /* 483 * Define new keys. 484 */ 485 if (p->p_textp == 0) { /* use existing code key if shared */ 486 ckeyrelease(p->p_ckey); 487 p->p_ckey = getcodekey(); 488 } 489 mtpr(CCK, p->p_ckey); 490 dkeyrelease(p->p_dkey); 491 p->p_dkey = getdatakey(); 492 mtpr(DCK, p->p_dkey); 493 #endif 494 if (pagi && p->p_textp) 495 vinifod(u.u_procp, (struct fpte *)dptopte(p, 0), 496 PG_FTEXT, p->p_textp->x_vptr, 497 (long)(1 + ts/CLSIZE), (size_t)btoc(ep->a_data)); 498 499 #if defined(vax) || defined(tahoe) 500 /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ 501 mtpr(TBIA, 0); 502 #endif 503 504 /* 505 * set SUID/SGID protections, if no tracing 506 */ 507 if ((p->p_flag&STRC)==0) { 508 if (uid != u.u_cred->cr_uid || gid != u.u_cred->cr_gid) 509 u.u_cred = crcopy(u.u_cred); 510 u.u_cred->cr_uid = uid; 511 u.u_cred->cr_gid = gid; 512 p->p_uid = uid; 513 } else 514 psignal(p, SIGTRAP); 515 p->p_svuid = p->p_uid; 516 p->p_svgid = u.u_cred->cr_gid; 517 u.u_tsize = ts; 518 u.u_dsize = ds; 519 u.u_ssize = ss; 520 u.u_prof.pr_scale = 0; 521 #if defined(tahoe) 522 u.u_pcb.pcb_savacc.faddr = (float *)NULL; 523 #endif 524 bad: 525 return; 526 } 527