added my responsibility for the `cpm' port
[unix-history] / sys / kern / kern_execve.c
index 1d712e2..b1de36c 100644 (file)
@@ -19,7 +19,7 @@
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@@ -28,7 +28,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *     $Id: kern_execve.c,v 1.13 1994/01/03 07:56:43 davidg Exp $
+ *     $Id: kern_execve.c,v 1.19 1994/03/21 09:35:30 davidg Exp $
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -78,9 +78,6 @@ execve(p, uap, retval)
        char *stringbase, *stringp;
        int *stack_base;
        int error, resid, len, i;
        char *stringbase, *stringp;
        int *stack_base;
        int error, resid, len, i;
-#if 0
-       char image_header[256];
-#endif
        struct image_params image_params, *iparams;
        struct vnode *vnodep;
        struct vattr attr;
        struct image_params image_params, *iparams;
        struct vnode *vnodep;
        struct vattr attr;
@@ -147,40 +144,20 @@ interpret:
        if (error)
                goto exec_fail_dealloc;
 
        if (error)
                goto exec_fail_dealloc;
 
-#if 0
-       /*
-        * Read the image header from the file.
-        */
-       error = vn_rdwr(UIO_READ,
-                       vnodep,
-                       image_header,
-                       sizeof(image_header),
-                       0,
-                       UIO_SYSSPACE, IO_NODELOCKED,
-                       p->p_ucred,
-                       &resid,
-                       p);
-       if (error)
-               goto exec_fail_dealloc;
-
-       /* Clear out junk in image_header if a partial read (small file) */
-       if (resid)
-               bzero(image_header + (sizeof(image_header) - resid), resid);
-#endif
        /*
         * Map the image header (first page) of the file into
         *      kernel address space
         */
        error = vm_mmap(kernel_map,                     /* map */
                        (vm_offset_t *)&image_header,   /* address */
        /*
         * Map the image header (first page) of the file into
         *      kernel address space
         */
        error = vm_mmap(kernel_map,                     /* map */
                        (vm_offset_t *)&image_header,   /* address */
-                       NBPG,                           /* size */
+                       PAGE_SIZE,                      /* size */
                        VM_PROT_READ,                   /* protection */
                        VM_PROT_READ,                   /* max protection */
                        MAP_FILE,                       /* flags */
                        (caddr_t)vnodep,                /* vnode */
                        0);                             /* offset */
        if (error) {
                        VM_PROT_READ,                   /* protection */
                        VM_PROT_READ,                   /* max protection */
                        MAP_FILE,                       /* flags */
                        (caddr_t)vnodep,                /* vnode */
                        0);                             /* offset */
        if (error) {
-               printf("mmap failed: %d\n",error);
+               uprintf("mmap failed: %d\n",error);
                goto exec_fail_dealloc;
        }
        iparams->image_header = image_header;
                goto exec_fail_dealloc;
        }
        iparams->image_header = image_header;
@@ -208,7 +185,7 @@ interpret:
                        vput(ndp->ni_vp);
                        FREE(ndp->ni_pnbuf, M_NAMEI);
                        if (vm_deallocate(kernel_map, 
                        vput(ndp->ni_vp);
                        FREE(ndp->ni_pnbuf, M_NAMEI);
                        if (vm_deallocate(kernel_map, 
-                                         (vm_offset_t)image_header, NBPG))
+                                         (vm_offset_t)image_header, PAGE_SIZE))
                                panic("execve: header dealloc failed (1)");
 
                        /* set new name to that of the interpreter */
                                panic("execve: header dealloc failed (1)");
 
                        /* set new name to that of the interpreter */
@@ -231,15 +208,15 @@ interpret:
        stack_base = exec_copyout_strings(iparams);
        p->p_vmspace->vm_minsaddr = (char *)stack_base;
 
        stack_base = exec_copyout_strings(iparams);
        p->p_vmspace->vm_minsaddr = (char *)stack_base;
 
-       p->p_vmspace->vm_ssize = (((caddr_t)USRSTACK - (char *)stack_base) >> PAGE_SHIFT) + 1;
-
        /*
         * Stuff argument count as first item on stack
         */
        *(--stack_base) = iparams->argc;
 
        /*
         * Stuff argument count as first item on stack
         */
        *(--stack_base) = iparams->argc;
 
-       /* close files on exec, fixup signals */
+       /* close files on exec */
        fdcloseexec(p);
        fdcloseexec(p);
+
+       /* reset caught signals */
        execsigs(p);
 
        /* name this process - nameiexec(p, ndp) */
        execsigs(p);
 
        /* name this process - nameiexec(p, ndp) */
@@ -258,15 +235,35 @@ interpret:
        }
        
        /* implement set userid/groupid */
        }
        
        /* implement set userid/groupid */
-       if ((attr.va_mode&VSUID) && (p->p_flag & STRC) == 0) {
+       p->p_flag &= ~SUGID;
+
+       /*
+        * Turn off kernel tracing for set-id programs, except for
+        * root.
+        */
+       if (p->p_tracep && (attr.va_mode & (VSUID | VSGID)) &&
+           suser(p->p_ucred, &p->p_acflag)) {
+               p->p_traceflag = 0;
+               vrele(p->p_tracep);
+               p->p_tracep = 0;
+       }
+       if ((attr.va_mode & VSUID) && (p->p_flag & STRC) == 0) {
                p->p_ucred = crcopy(p->p_ucred);
                p->p_ucred = crcopy(p->p_ucred);
-               p->p_cred->p_svuid = p->p_ucred->cr_uid = attr.va_uid;
+               p->p_ucred->cr_uid = attr.va_uid;
+               p->p_flag |= SUGID;
        }
        }
-       if ((attr.va_mode&VSGID) && (p->p_flag & STRC) == 0) {
+       if ((attr.va_mode & VSGID) && (p->p_flag & STRC) == 0) {
                p->p_ucred = crcopy(p->p_ucred);
                p->p_ucred = crcopy(p->p_ucred);
-               p->p_cred->p_svgid = p->p_ucred->cr_groups[0] = attr.va_gid;
+               p->p_ucred->cr_groups[0] = attr.va_gid;
+               p->p_flag |= SUGID;
        }
 
        }
 
+       /*
+        * Implement correct POSIX saved uid behavior.
+        */
+       p->p_cred->p_svuid = p->p_ucred->cr_uid;
+       p->p_cred->p_svgid = p->p_ucred->cr_gid;
+
        /* mark vnode pure text */
        ndp->ni_vp->v_flag |= VTEXT;
 
        /* mark vnode pure text */
        ndp->ni_vp->v_flag |= VTEXT;
 
@@ -286,10 +283,9 @@ interpret:
        /*
         * free various allocated resources
         */
        /*
         * free various allocated resources
         */
-       if (vm_deallocate(kernel_map, (vm_offset_t)iparams->stringbase,
-                         ARG_MAX))
+       if (vm_deallocate(kernel_map, (vm_offset_t)iparams->stringbase, ARG_MAX))
                panic("execve: string buffer dealloc failed (1)");
                panic("execve: string buffer dealloc failed (1)");
-       if (vm_deallocate(kernel_map, (vm_offset_t)image_header, NBPG))
+       if (vm_deallocate(kernel_map, (vm_offset_t)image_header, PAGE_SIZE))
                panic("execve: header dealloc failed (2)");
        vput(ndp->ni_vp);
        FREE(ndp->ni_pnbuf, M_NAMEI);
                panic("execve: header dealloc failed (2)");
        vput(ndp->ni_vp);
        FREE(ndp->ni_pnbuf, M_NAMEI);
@@ -303,7 +299,7 @@ exec_fail_dealloc:
                        panic("execve: string buffer dealloc failed (2)");
        if (iparams->image_header && iparams->image_header != (char *)-1)
                if (vm_deallocate(kernel_map, 
                        panic("execve: string buffer dealloc failed (2)");
        if (iparams->image_header && iparams->image_header != (char *)-1)
                if (vm_deallocate(kernel_map, 
-                                 (vm_offset_t)iparams->image_header, NBPG))
+                                 (vm_offset_t)iparams->image_header, PAGE_SIZE))
                        panic("execve: header dealloc failed (3)");
        vput(ndp->ni_vp);
        FREE(ndp->ni_pnbuf, M_NAMEI);
                        panic("execve: header dealloc failed (3)");
        vput(ndp->ni_vp);
        FREE(ndp->ni_pnbuf, M_NAMEI);
@@ -324,7 +320,7 @@ exec_fail:
 
 /*
  * Destroy old address space, and allocate a new stack
 
 /*
  * Destroy old address space, and allocate a new stack
- *     The new stack is only DFLSSIZ large because it is grown
+ *     The new stack is only SGROWSIZ large because it is grown
  *     automatically in trap.c.
  */
 int
  *     automatically in trap.c.
  */
 int
@@ -333,7 +329,7 @@ exec_new_vmspace(iparams)
 {
        int error;
        struct vmspace *vmspace = iparams->proc->p_vmspace;
 {
        int error;
        struct vmspace *vmspace = iparams->proc->p_vmspace;
-       caddr_t stack_addr = (caddr_t) (USRSTACK - DFLSSIZ);
+       caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
 
        iparams->vmspace_destroyed = 1;
 
 
        iparams->vmspace_destroyed = 1;
 
@@ -342,10 +338,12 @@ exec_new_vmspace(iparams)
 
        /* Allocate a new stack */
        error = vm_allocate(&vmspace->vm_map, (vm_offset_t *)&stack_addr,
 
        /* Allocate a new stack */
        error = vm_allocate(&vmspace->vm_map, (vm_offset_t *)&stack_addr,
-                           DFLSSIZ, FALSE);
+                           SGROWSIZ, FALSE);
        if (error)
                return(error);
 
        if (error)
                return(error);
 
+       vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
+
        /* Initialize maximum stack address */
        vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
 
        /* Initialize maximum stack address */
        vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;