X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/d7e7a4ec0579987641ab9021cb40c0e5c0c867d2..4e6e38871c68cf8ddfa65c3f6a0d0b2f3a1b2caf:/usr/src/sys/kern/kern_proc.c diff --git a/usr/src/sys/kern/kern_proc.c b/usr/src/sys/kern/kern_proc.c index 1c72a95e44..53cab4f7c7 100644 --- a/usr/src/sys/kern/kern_proc.c +++ b/usr/src/sys/kern/kern_proc.c @@ -1,4 +1,4 @@ -/* kern_proc.c 3.14 %G% */ +/* kern_proc.c 4.25 82/04/02 */ #include "../h/param.h" #include "../h/systm.h" @@ -17,6 +17,8 @@ #include "../h/vm.h" #include "../h/text.h" #include "../h/psl.h" +#include "../h/vlimit.h" +#include "../h/file.h" /* * exec system call, with and without environments. @@ -40,20 +42,124 @@ exece() register struct buf *bp; register struct execa *uap; int na, ne, ucp, ap, c; + int indir, uid, gid; + char *sharg; struct inode *ip; swblk_t bno; + char cfname[DIRSIZ]; + char cfarg[SHSIZE]; - if ((ip = namei(uchar, 0)) == NULL) + if ((ip = namei(uchar, 0, 1)) == NULL) return; bno = 0; bp = 0; - if(access(ip, IEXEC)) + indir = 0; + uid = u.u_uid; + gid = u.u_gid; + if (ip->i_mode & ISUID) + uid = ip->i_uid; + if (ip->i_mode & ISGID) + gid = ip->i_gid; + + again: + if (access(ip, IEXEC)) goto bad; - if((ip->i_mode & IFMT) != IFREG || + if ((u.u_procp->p_flag&STRC) && access(ip, IREAD)) + goto bad; + if ((ip->i_mode & IFMT) != IFREG || (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { u.u_error = EACCES; goto bad; } + + /* + * Read in first few bytes of file for segment sizes, ux_mag: + * 407 = plain executable + * 410 = RO text + * 413 = demand paged RO text + * Also an ASCII line beginning with #! is + * the file name of a ``shell'' and arguments may be prepended + * to the argument list if given here. + * + * SHELL NAMES ARE LIMITED IN LENGTH. + * + * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM + * THE ASCII LINE. + */ + u.u_base = (caddr_t)&u.u_exdata; + u.u_count = sizeof(u.u_exdata); + u.u_offset = 0; + u.u_segflg = 1; + readi(ip); + u.u_segflg = 0; + if (u.u_error) + goto bad; + if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) && + u.u_exdata.ux_shell[0] != '#') { + u.u_error = ENOEXEC; + goto bad; + } + switch (u.u_exdata.ux_mag) { + + case 0407: + u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; + u.u_exdata.ux_tsize = 0; + break; + + case 0413: + case 0410: + if (u.u_exdata.ux_tsize == 0) { + u.u_error = ENOEXEC; + goto bad; + } + break; + + default: + if (u.u_exdata.ux_shell[0] != '#' || + u.u_exdata.ux_shell[1] != '!' || + indir) { + u.u_error = ENOEXEC; + goto bad; + } + cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */ + while (cp < &u.u_exdata.ux_shell[SHSIZE]) { + if (*cp == '\t') + *cp = ' '; + else if (*cp == '\n') { + *cp = '\0'; + break; + } + cp++; + } + if (*cp != '\0') { + u.u_error = ENOEXEC; + goto bad; + } + cp = &u.u_exdata.ux_shell[2]; + while (*cp == ' ') + cp++; + u.u_dirp = cp; + while (*cp && *cp != ' ') + cp++; + sharg = NULL; + if (*cp) { + *cp++ = '\0'; + while (*cp == ' ') + cp++; + if (*cp) { + bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE); + sharg = cfarg; + } + } + bcopy((caddr_t)u.u_dbuf, (caddr_t)cfname, DIRSIZ); + indir = 1; + iput(ip); + ip = namei(schar, 0, 1); + if (ip == NULL) + return; + goto again; + } + /* * Collect arguments on "file" in swap space. */ @@ -61,15 +167,17 @@ exece() ne = 0; nc = 0; uap = (struct execa *)u.u_ap; - if ((bno = malloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { + if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { swkill(u.u_procp, "exece"); goto bad; } if (bno % CLSIZE) - panic("execa malloc"); + panic("execa rmalloc"); if (uap->argp) for (;;) { ap = NULL; - if (uap->argp) { + if (indir && (na == 1 || na == 2 && sharg)) + ap = (int)uap->fname; + else if (uap->argp) { ap = fuword((caddr_t)uap->argp); uap->argp++; } @@ -83,12 +191,14 @@ exece() if (ap==NULL) break; na++; - if(ap == -1) + if (ap == -1) u.u_error = EFAULT; do { if (nc >= NCARGS-1) u.u_error = E2BIG; - if ((c = fubyte((caddr_t)ap++)) < 0) + if (indir && na == 2 && sharg != NULL) + c = *sharg++ & 0377; + else if ((c = fubyte((caddr_t)ap++)) < 0) u.u_error = EFAULT; if (u.u_error) { if (bp) @@ -111,7 +221,10 @@ exece() bdwrite(bp); bp = 0; nc = (nc + NBPW-1) & ~(NBPW-1); - if (getxfile(ip, nc) || u.u_error) { + if (indir) + bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ); + getxfile(ip, nc + (na+4)*NBPW, uid, gid); + if (u.u_error) { badarg: for (c = 0; c < nc; c += BSIZE) if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) { @@ -126,7 +239,6 @@ badarg: /* * copy back arglist */ - ucp = USRSTACK - nc - NBPW; ap = ucp - na*NBPW - 3*NBPW; u.u_ar0[SP] = ap; @@ -162,181 +274,112 @@ bad: if (bp) brelse(bp); if (bno) - mfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); + rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); iput(ip); } /* * Read in and set up memory for executed file. - * Zero return is normal; - * non-zero means only the text is being replaced */ -getxfile(ip, nargc) +getxfile(ip, nargc, uid, gid) register struct inode *ip; { - register sep; register size_t ts, ds, ss; - register int overlay; - int pagi = 0; - - /* - * read in first few bytes - * of file for segment - * sizes: - * ux_mag = 407/410/411/405 - * 407 is plain executable - * 410 is RO text - * 411 is separated ID - * 405 is overlaid text - * 412 is demand paged plain executable (NOT IMPLEMENTED) - * 413 is demand paged RO text - */ - - u.u_base = (caddr_t)&u.u_exdata; - u.u_count = sizeof(u.u_exdata); - u.u_offset = 0; - u.u_segflg = 1; - readi(ip); - u.u_segflg = 0; - if(u.u_error) - goto bad; - if (u.u_count!=0) { - u.u_error = ENOEXEC; - goto bad; - } - sep = 0; - overlay = 0; - switch (u.u_exdata.ux_mag) { - - case 0405: - overlay++; - break; + int pagi; - case 0412: - u.u_error = ENOEXEC; - goto bad; - - case 0407: - u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; - u.u_exdata.ux_tsize = 0; - break; - - case 0413: + if (u.u_exdata.ux_mag == 0413) pagi = SPAGI; - /* fall into ... */ - - case 0410: - if (u.u_exdata.ux_tsize == 0) { - u.u_error = ENOEXEC; - goto bad; + else + pagi = 0; + if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && + ip->i_count!=1) { + register struct file *fp; + + for (fp = file; fp < fileNFILE; fp++) { + if (fp->f_flag & FSOCKET) + continue; + if (fp->f_inode == ip && (fp->f_flag&FWRITE)) { + u.u_error = ETXTBSY; + goto bad; + } } - break; - - case 0411: - u.u_error = ENOEXEC; - goto bad; - - default: - u.u_error = ENOEXEC; - goto bad; - } - if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) { - u.u_error = ETXTBSY; - goto bad; } /* - * find text and data sizes - * try them out for possible - * exceed of max sizes + * Compute text and data sizes and make sure not too large. */ - ts = clrnd(btoc(u.u_exdata.ux_tsize)); ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); ss = clrnd(SSIZE + btoc(nargc)); - if (overlay) { - if ((u.u_procp->p_flag & SPAGI) || - u.u_sep==0 && ctos(ts) != ctos(u.u_tsize) || nargc) { - u.u_error = ENOMEM; - goto bad; - } - ds = u.u_dsize; - ss = u.u_ssize; - sep = u.u_sep; - xfree(); - xalloc(ip, pagi); - u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ - } else { - if (chksize(ts, ds, ss)) - goto bad; - u.u_cdmap = zdmap; - u.u_csmap = zdmap; - if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) - goto bad; - - /* - * At this point, committed to the new image! - * Release virtual memory resources of old process, and - * initialize the virtual memory of the new process. - * If we resulted from vfork(), instead wakeup our - * parent who will set SVFDONE when he has taken back - * our resources. - */ - u.u_prof.pr_scale = 0; - if ((u.u_procp->p_flag & SVFORK) == 0) - vrelvm(); - else { - u.u_procp->p_flag &= ~SVFORK; - u.u_procp->p_flag |= SKEEP; - wakeup((caddr_t)u.u_procp); - while ((u.u_procp->p_flag & SVFDONE) == 0) - sleep((caddr_t)u.u_procp, PZERO - 1); - u.u_procp->p_flag &= ~(SVFDONE|SKEEP); - } - u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM); - u.u_procp->p_flag |= pagi; - u.u_dmap = u.u_cdmap; - u.u_smap = u.u_csmap; - vgetvm(ts, ds, ss); + if (chksize(ts, ds, ss)) + goto bad; - if (pagi == 0) { - /* - * Read in data segment. - */ - u.u_base = (char *)ctob(ts); - u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize; - u.u_count = u.u_exdata.ux_dsize; - readi(ip); - } - xalloc(ip, pagi); - if (pagi && u.u_procp->p_textp) - vinifod((struct fpte *)dptopte(u.u_procp, 0), - PG_FTEXT, u.u_procp->p_textp->x_iptr, - 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); + /* + * Make sure enough space to start process. + */ + u.u_cdmap = zdmap; + u.u_csmap = zdmap; + if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) + goto bad; - /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ - mtpr(TBIA, 0); + /* + * At this point, committed to the new image! + * Release virtual memory resources of old process, and + * initialize the virtual memory of the new process. + * If we resulted from vfork(), instead wakeup our + * parent who will set SVFDONE when he has taken back + * our resources. + */ + u.u_prof.pr_scale = 0; + if ((u.u_procp->p_flag & SVFORK) == 0) + vrelvm(); + else { + u.u_procp->p_flag &= ~SVFORK; + u.u_procp->p_flag |= SKEEP; + wakeup((caddr_t)u.u_procp); + while ((u.u_procp->p_flag & SVFDONE) == 0) + sleep((caddr_t)u.u_procp, PZERO - 1); + u.u_procp->p_flag &= ~(SVFDONE|SKEEP); + } + u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG); + u.u_procp->p_flag |= pagi; + u.u_dmap = u.u_cdmap; + u.u_smap = u.u_csmap; + vgetvm(ts, ds, ss); + if (pagi == 0) { /* - * set SUID/SGID protections, if no tracing + * Read in data segment. */ - if ((u.u_procp->p_flag&STRC)==0) { - if(ip->i_mode&ISUID) - if(u.u_uid != 0) { - u.u_uid = ip->i_uid; - u.u_procp->p_uid = ip->i_uid; - } - if(ip->i_mode&ISGID) - u.u_gid = ip->i_gid; - } else - psignal(u.u_procp, SIGTRAP); + u.u_base = (char *)ctob(ts); + u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize; + u.u_count = u.u_exdata.ux_dsize; + readi(ip); } + xalloc(ip, pagi); + if (pagi && u.u_procp->p_textp) + vinifod((struct fpte *)dptopte(u.u_procp, 0), + PG_FTEXT, u.u_procp->p_textp->x_iptr, + 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); + + /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ + mtpr(TBIA, 0); + + /* + * set SUID/SGID protections, if no tracing + */ + if ((u.u_procp->p_flag&STRC)==0) { + u.u_uid = uid; + u.u_procp->p_uid = uid; + u.u_gid = gid; + u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8)); + } else + psignal(u.u_procp, SIGTRAP); u.u_tsize = ts; u.u_dsize = ds; u.u_ssize = ss; - u.u_sep = sep; bad: - return(overlay); + return; } /* @@ -348,7 +391,7 @@ setregs() register i; long sigmask; - for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG]; + for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG]; sigmask <<= 1, rp++) { switch (*rp) { @@ -366,7 +409,7 @@ setregs() if ((int)*rp & 1) u.u_procp->p_siga0 |= sigmask; else - u.u_procp->p_siga1 &= ~sigmask; + u.u_procp->p_siga0 &= ~sigmask; if ((int)*rp & 2) u.u_procp->p_siga1 |= sigmask; else @@ -376,17 +419,18 @@ setregs() } } /* - for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) + for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) *rp++ = 0; */ u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ - for(i=0; ip_siga1 = 0; (void) spl0(); - p->p_aveflt = 0; - for(i=0; ip_cpticks = 0; + p->p_pctcpu = 0; + for (i=0; ip_flag &= ~SVFDONE; } - for(i=0; ip_stat = SZOMB; + noproc = 1; i = PIDHASH(p->p_pid); x = p - proc; if (pidhash[i] == x) @@ -486,20 +531,21 @@ exit(rv) } panic("exit"); } + if (p->p_pid == 1) + panic("init died"); done: ((struct xproc *)p)->xp_xstat = rv; /* overlay */ ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); - for(q = &proc[0]; q < &proc[NPROC]; q++) - if(q->p_pptr == p) { + for (q = proc; q < procNPROC; q++) + if (q->p_pptr == p) { q->p_pptr = &proc[1]; q->p_ppid = 1; - q->p_flag |= SDETACH; wakeup((caddr_t)&proc[1]); /* * Traced processes are killed * since their existence means someone is screwing up. - * Traced processes are sent a hangup and a continue. + * Stopped processes are sent a hangup and a continue. * This is designed to be ``safe'' for setuid * processes since they must be willing to tolerate * hangups anyways. @@ -513,12 +559,12 @@ done: } /* * Protect this process from future - * tty signals, and clear TSTP/TTIN/TTOU if pending. + * tty signals, clear TSTP/TTIN/TTOU if pending. */ - spgrp(q, -1); + (void) spgrp(q, -1); } - wakeup((caddr_t)p->p_pptr); psignal(p->p_pptr, SIGCHLD); + wakeup((caddr_t)p->p_pptr); swtch(); } @@ -554,10 +600,10 @@ wait1(options, vp) f = 0; loop: - for(p = &proc[0]; p < &proc[NPROC]; p++) - if(p->p_pptr == u.u_procp) { + for (p = proc; p < procNPROC; p++) + if (p->p_pptr == u.u_procp) { f++; - if(p->p_stat == SZOMB) { + if (p->p_stat == SZOMB) { u.u_r.r_val1 = p->p_pid; u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; ((struct xproc *)p)->xp_xstat = 0; @@ -594,12 +640,10 @@ loop: u.u_r.r_val1 = 0; return; } -/* - if (setjmp(u.u_qsav)) { + if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { u.u_eosys = RESTARTSYS; return; } -*/ sleep((caddr_t)u.u_procp, PWAIT); goto loop; } @@ -626,7 +670,7 @@ fork1(isvfork) a = 0; p2 = NULL; - for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) { + for (p1 = proc; p1 < procNPROC; p1++) { if (p1->p_stat==NULL && p2==NULL) p2 = p1; else { @@ -640,7 +684,9 @@ fork1(isvfork) * not su and too many procs owned; or * not su and would take last slot. */ - if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) { + if (p2==NULL) + tablefull("proc"); + if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { u.u_error = EAGAIN; if (!isvfork) { (void) vsexpand(0, &u.u_cdmap, 1); @@ -649,7 +695,7 @@ fork1(isvfork) goto out; } p1 = u.u_procp; - if(newproc(isvfork)) { + if (newproc(isvfork)) { u.u_r.r_val1 = p1->p_pid; u.u_r.r_val2 = 1; /* child */ u.u_start = time; @@ -684,6 +730,10 @@ sbreak() if (n < 0) n = 0; d = clrnd(n - u.u_dsize); + if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) { + u.u_error = ENOMEM; + return; + } if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) return; if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)