Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Make sure that the pointer to old parent process for ptr...



details:   https://anonhg.NetBSD.org/src/rev/4a2d0e683c49
branches:  trunk
changeset: 534425:4a2d0e683c49
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Thu Jul 25 20:04:02 2002 +0000

description:
Make sure that the pointer to old parent process for ptraced children
gets reset properly when the old parent exits before the child. A flag
is set in old parent process when the child is reparented in ptrace(2).
If it's set when process is exiting, all running processes have their
'old parent process' pointer checked and reset if appropriate. Also
change to use 'struct proc *' pointer directly, rather than pid_t.
This fixes security/14444 by David Sainty.

Reviewed by Christos Zoulas.

diffstat:

 sys/compat/svr4/svr4_misc.c    |  11 +++++------
 sys/kern/kern_exit.c           |  32 ++++++++++++++++++++++++++------
 sys/kern/sys_process.c         |  20 ++++++++++----------
 sys/miscfs/procfs/procfs_ctl.c |  18 +++++++++---------
 sys/sys/proc.h                 |   8 ++++----
 5 files changed, 54 insertions(+), 35 deletions(-)

diffs (235 lines):

diff -r 5c8b22c09dd5 -r 4a2d0e683c49 sys/compat/svr4/svr4_misc.c
--- a/sys/compat/svr4/svr4_misc.c       Thu Jul 25 19:49:27 2002 +0000
+++ b/sys/compat/svr4/svr4_misc.c       Thu Jul 25 20:04:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: svr4_misc.c,v 1.97 2002/03/31 22:22:49 christos Exp $   */
+/*     $NetBSD: svr4_misc.c,v 1.98 2002/07/25 20:04:04 jdolecek Exp $   */
 
 /*-
  * Copyright (c) 1994 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: svr4_misc.c,v 1.97 2002/03/31 22:22:49 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svr4_misc.c,v 1.98 2002/07/25 20:04:04 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1240,11 +1240,10 @@
                         * parent a SIGCHLD.  The rest of the cleanup will be
                         * done when the old parent waits on the child.
                         */
-                       if ((q->p_flag & P_TRACED) &&
-                           q->p_oppid != q->p_pptr->p_pid) {
-                               t = pfind(q->p_oppid);
+                       if ((q->p_flag & P_TRACED) && q->p_opptr != q->p_pptr){
+                               t = q->p_opptr;
                                proc_reparent(q, t ? t : initproc);
-                               q->p_oppid = 0;
+                               q->p_opptr = NULL;
                                q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
                                psignal(q->p_pptr, SIGCHLD);
                                wakeup((caddr_t)q->p_pptr);
diff -r 5c8b22c09dd5 -r 4a2d0e683c49 sys/kern/kern_exit.c
--- a/sys/kern/kern_exit.c      Thu Jul 25 19:49:27 2002 +0000
+++ b/sys/kern/kern_exit.c      Thu Jul 25 20:04:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exit.c,v 1.94 2002/06/17 16:22:50 christos Exp $  */
+/*     $NetBSD: kern_exit.c,v 1.95 2002/07/25 20:04:02 jdolecek Exp $  */
 
 /*-
  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.94 2002/06/17 16:22:50 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.95 2002/07/25 20:04:02 jdolecek Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_systrace.h"
@@ -258,6 +258,27 @@
        }
 
        /*
+        * Reset p_opptr pointer of all former children which got
+        * traced by another process and were reparented. We reset
+        * it to NULL here; the trace detach code then reparents
+        * the child to initproc. We only check allproc list, since
+        * eventual former children on zombproc list won't reference
+        * p_opptr anymore.
+        */
+       if (p->p_flag & P_CHTRACED) {
+               struct proc *t;
+
+               proclist_lock_read();
+
+               LIST_FOREACH(t, &allproc, p_list) {
+                       if (t->p_opptr == p)
+                               t->p_opptr = NULL;
+               }
+
+               proclist_unlock_read();
+       }
+
+       /*
         * Save exit status and final rusage info, adding in child rusage
         * info and self times.
         */
@@ -459,11 +480,10 @@
                         * parent the exit signal.  The rest of the cleanup
                         * will be done when the old parent waits on the child.
                         */
-                       if ((p->p_flag & P_TRACED) &&
-                           p->p_oppid != p->p_pptr->p_pid) {
-                               t = pfind(p->p_oppid);
+                       if ((p->p_flag & P_TRACED) && p->p_opptr != p->p_pptr){
+                               t = p->p_opptr;
                                proc_reparent(p, t ? t : initproc);
-                               p->p_oppid = 0;
+                               p->p_opptr = NULL;
                                p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
                                if (p->p_exitsig != 0)
                                        psignal(p->p_pptr, P_EXITSIG(p));
diff -r 5c8b22c09dd5 -r 4a2d0e683c49 sys/kern/sys_process.c
--- a/sys/kern/sys_process.c    Thu Jul 25 19:49:27 2002 +0000
+++ b/sys/kern/sys_process.c    Thu Jul 25 20:04:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_process.c,v 1.74 2002/05/09 15:44:45 thorpej Exp $ */
+/*     $NetBSD: sys_process.c,v 1.75 2002/07/25 20:04:02 jdolecek Exp $        */
 
 /*-
  * Copyright (c) 1993 Jan-Simon Pendry.
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.74 2002/05/09 15:44:45 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.75 2002/07/25 20:04:02 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -237,7 +237,7 @@
        case  PT_TRACE_ME:
                /* Just set the trace flag. */
                SET(t->p_flag, P_TRACED);
-               t->p_oppid = t->p_pptr->p_pid;
+               t->p_opptr = t->p_pptr;
                return (0);
 
        case  PT_WRITE_I:               /* XXX no separate I and D spaces */
@@ -337,15 +337,13 @@
 
                if (SCARG(uap, req) == PT_DETACH) {
                        /* give process back to original parent or init */
-                       if (t->p_oppid != t->p_pptr->p_pid) {
-                               struct proc *pp;
-
-                               pp = pfind(t->p_oppid);
+                       if (t->p_opptr != t->p_pptr) {
+                               struct proc *pp = t->p_opptr;
                                proc_reparent(t, pp ? pp : initproc);
                        }
 
                        /* not being traced any more */
-                       t->p_oppid = 0;
+                       t->p_opptr = NULL;
                        CLR(t->p_flag, P_TRACED|P_WAITED);
                }
 
@@ -381,9 +379,11 @@
                 * Stop the target.
                 */
                SET(t->p_flag, P_TRACED);
-               t->p_oppid = t->p_pptr->p_pid;
-               if (t->p_pptr != p)
+               t->p_opptr = t->p_pptr;
+               if (t->p_pptr != p) {
+                       t->p_pptr->p_flag |= P_CHTRACED;
                        proc_reparent(t, p);
+               }
                SCARG(uap, data) = SIGSTOP;
                goto sendsig;
 
diff -r 5c8b22c09dd5 -r 4a2d0e683c49 sys/miscfs/procfs/procfs_ctl.c
--- a/sys/miscfs/procfs/procfs_ctl.c    Thu Jul 25 19:49:27 2002 +0000
+++ b/sys/miscfs/procfs/procfs_ctl.c    Thu Jul 25 20:04:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: procfs_ctl.c,v 1.22 2002/01/11 22:02:56 christos Exp $ */
+/*     $NetBSD: procfs_ctl.c,v 1.23 2002/07/25 20:04:03 jdolecek Exp $ */
 
 /*
  * Copyright (c) 1993 Jan-Simon Pendry
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: procfs_ctl.c,v 1.22 2002/01/11 22:02:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_ctl.c,v 1.23 2002/07/25 20:04:03 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -226,9 +226,11 @@
                 * Stop the target.
                 */
                SET(p->p_flag, P_TRACED|P_FSTRACE);
-               p->p_oppid = p->p_pptr->p_pid;
-               if (p->p_pptr != curp)
+               p->p_opptr = p->p_pptr;
+               if (p->p_pptr != curp) {
+                       p->p_pptr->p_flag |= P_CHTRACED;
                        proc_reparent(p, curp);
+               }
                sig = SIGSTOP;
                goto sendsig;
 
@@ -250,15 +252,13 @@
 
                if (op == PROCFS_CTL_DETACH) {
                        /* give process back to original parent */
-                       if (p->p_oppid != p->p_pptr->p_pid) {
-                               struct proc *pp;
-       
-                               pp = pfind(p->p_oppid);
+                       if (p->p_opptr != p->p_pptr) {
+                               struct proc *pp = p->p_opptr;
                                proc_reparent(p, pp ? pp : initproc);
                        }
 
                        /* not being traced any more */
-                       p->p_oppid = 0;
+                       p->p_opptr = NULL;
                        CLR(p->p_flag, P_TRACED|P_FSTRACE|P_WAITED);
                }
 
diff -r 5c8b22c09dd5 -r 4a2d0e683c49 sys/sys/proc.h
--- a/sys/sys/proc.h    Thu Jul 25 19:49:27 2002 +0000
+++ b/sys/sys/proc.h    Thu Jul 25 20:04:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: proc.h,v 1.141 2002/07/11 10:37:27 pooka Exp $ */
+/*     $NetBSD: proc.h,v 1.142 2002/07/25 20:04:04 jdolecek Exp $      */
 
 /*-
  * Copyright (c) 1986, 1989, 1991, 1993
@@ -177,9 +177,9 @@
 /*
  * The following fields are all zeroed upon creation in fork.
  */
-#define        p_startzero     p_oppid
+#define        p_startzero     p_opptr
 
-       pid_t           p_oppid;        /* Save parent pid during ptrace. XXX */
+       struct proc     *p_opptr;       /* Save parent during ptrace. */
        int             p_dupfd;        /* Sideways return value from filedescopen. XXX */
 
        /* Scheduling */
@@ -299,7 +299,7 @@
 #define        P_BIGLOCK       0x080000 /* Process needs kernel "big lock" to run */
 #define        P_INEXEC        0x100000 /* Process is exec'ing and cannot be traced */
 #define        P_SYSTRACE      0x200000 /* Process system call tracing active */
-
+#define        P_CHTRACED      0x400000 /* Child has been traced & reparented */
 
 /*
  * Macro to compute the exit signal to be delivered.



Home | Main Index | Thread Index | Old Index