Source-Changes-HG archive

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

[src/netbsd-9]: src/sys Pull up following revision(s) (requested by kamil in ...



details:   https://anonhg.NetBSD.org/src/rev/09bf512abf2d
branches:  netbsd-9
changeset: 458484:09bf512abf2d
user:      martin <martin%NetBSD.org@localhost>
date:      Tue Oct 15 18:32:13 2019 +0000

description:
Pull up following revision(s) (requested by kamil in ticket #311):

        sys/sys/siginfo.h: revision 1.34
        sys/kern/sys_ptrace_common.c: revision 1.59
        sys/kern/sys_lwp.c: revision 1.70
        sys/compat/sys/siginfo.h: revision 1.8
        sys/kern/kern_sig.c: revision 1.365
        sys/kern/kern_lwp.c: revision 1.203
        sys/sys/signalvar.h: revision 1.96
        sys/kern/kern_exec.c: revision 1.482
        sys/kern/kern_fork.c: revision 1.214

Move TRAP_CHLD/TRAP_LWP ptrace information from struct proc to siginfo

Storing struct ptrace_state information inside struct proc was vulnerable
to synchronization bugs, as multiple events emitted in the same time were
overwritting other ones.

Cache the original parent process id in p_oppid. Reusing here p_opptr is
in theory prone to slight race codition.

Change the semantics of PT_GET_PROCESS_STATE, reutning EINVAL for calls
prompting for the value in cases when there wasn't registered an
appropriate event.

Add an alternative approach to check the ptrace_state information, directly
from the siginfo_t value returned from PT_GET_SIGINFO. The original
PT_GET_PROCESS_STATE approach is kept for compat with older NetBSD and
OpenBSD. New code is recommended to keep using PT_GET_PROCESS_STATE.
Add a couple of compile-time asserts for assumptions in the code.

No functional change intended in existing ptrace(2) software.

All ATF ptrace(2) and ATF GDB tests pass.

This change improves reliability of the threading ptrace(2) code.

diffstat:

 sys/compat/sys/siginfo.h     |  10 ++++++++-
 sys/kern/kern_exec.c         |  12 ++++------
 sys/kern/kern_fork.c         |  28 ++++++++----------------
 sys/kern/kern_lwp.c          |   8 +++---
 sys/kern/kern_sig.c          |  14 ++++++++----
 sys/kern/sys_lwp.c           |   8 +++---
 sys/kern/sys_ptrace_common.c |  49 +++++++++++++++++--------------------------
 sys/sys/siginfo.h            |  18 +++++++++++++++-
 sys/sys/signalvar.h          |   4 +-
 9 files changed, 80 insertions(+), 71 deletions(-)

diffs (truncated from 413 to 300 lines):

diff -r 132311301d52 -r 09bf512abf2d sys/compat/sys/siginfo.h
--- a/sys/compat/sys/siginfo.h  Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/compat/sys/siginfo.h  Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: siginfo.h,v 1.7 2019/06/30 08:49:21 martin Exp $        */
+/*     $NetBSD: siginfo.h,v 1.7.2.1 2019/10/15 18:32:13 martin Exp $    */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -75,6 +75,14 @@
                        int     _error;
                        uint64_t _args[8]; /* SYS_MAXSYSARGS */
                } _syscall;
+
+               struct {
+                       int     _pe_report_event;
+                       union {
+                               pid_t   _pe_other_pid;
+                               lwpid_t _pe_lwp;
+                       } _option;
+               } _ptrace_state;
        } _reason;
 };
 
diff -r 132311301d52 -r 09bf512abf2d sys/kern/kern_exec.c
--- a/sys/kern/kern_exec.c      Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/kern/kern_exec.c      Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exec.c,v 1.478 2019/07/05 17:14:48 maxv Exp $     */
+/*     $NetBSD: kern_exec.c,v 1.478.2.1 2019/10/15 18:32:13 martin Exp $       */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.478 2019/07/05 17:14:48 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.478.2.1 2019/10/15 18:32:13 martin Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -1285,7 +1285,7 @@
        /* posix_spawn(3) reports a single event with implied exec(3) */
        if ((p->p_slflag & PSL_TRACED) && !is_spawn) {
                mutex_enter(p->p_lock);
-               eventswitch(TRAP_EXEC);
+               eventswitch(TRAP_EXEC, 0, 0);
                mutex_enter(proc_lock);
        }
 
@@ -2197,7 +2197,7 @@
                }
 
                mutex_enter(p->p_lock);
-               eventswitch(TRAP_CHLD);
+               eventswitch(TRAP_CHLD, PTRACE_POSIX_SPAWN, p->p_opptr->p_pid);
        }
 
  cpu_return:
@@ -2578,8 +2578,6 @@
        if ((p1->p_slflag & (PSL_TRACEPOSIX_SPAWN|PSL_TRACED)) ==
            (PSL_TRACEPOSIX_SPAWN|PSL_TRACED)) {
                proc_changeparent(p2, p1->p_pptr);
-               p1->p_pspid = p2->p_pid;
-               p2->p_pspid = p1->p_pid;
        }
 
        LIST_INSERT_AFTER(p1, p2, p_pglist);
@@ -2633,7 +2631,7 @@
                }
 
                mutex_enter(p1->p_lock);
-               eventswitch(TRAP_CHLD);
+               eventswitch(TRAP_CHLD, PTRACE_POSIX_SPAWN, pid);
        }
        return 0;
 
diff -r 132311301d52 -r 09bf512abf2d sys/kern/kern_fork.c
--- a/sys/kern/kern_fork.c      Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/kern/kern_fork.c      Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_fork.c,v 1.213 2019/06/13 20:20:18 kamil Exp $    */
+/*     $NetBSD: kern_fork.c,v 1.213.2.1 2019/10/15 18:32:13 martin Exp $       */
 
 /*-
  * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.213 2019/06/13 20:20:18 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.213.2.1 2019/10/15 18:32:13 martin Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_dtrace.h"
@@ -252,7 +252,6 @@
        int             count;
        vaddr_t         uaddr;
        int             tnprocs;
-       bool            trace_fork, trace_vfork;
        int             error = 0;
 
        p1 = l1->l_proc;
@@ -511,17 +510,8 @@
        /*
         * Trace fork(2) and vfork(2)-like events on demand in a debugger.
         */
-       trace_fork = tracefork(p1, flags);
-       trace_vfork = tracevfork(p1, flags);
-       if (trace_fork || trace_vfork)
+       if (tracefork(p1, flags) || tracevfork(p1, flags)) {
                proc_changeparent(p2, p1->p_pptr);
-       if (trace_fork) {
-               p1->p_fpid = p2->p_pid;
-               p2->p_fpid = p1->p_pid;
-       }
-       if (trace_vfork) {
-               p1->p_vfpid = p2->p_pid;
-               p2->p_vfpid = p1->p_pid;
        }
 
        LIST_INSERT_AFTER(p1, p2, p_pglist);
@@ -605,7 +595,9 @@
         */
        if (tracefork(p1, flags) || tracevfork(p1, flags)) {
                mutex_enter(p1->p_lock);
-               eventswitch(TRAP_CHLD);
+               eventswitch(TRAP_CHLD,
+                   tracefork(p1, flags) ? PTRACE_FORK : PTRACE_VFORK,
+                   retval[0]);
                mutex_enter(proc_lock);
        }
 
@@ -621,8 +613,7 @@
         */
        if (tracevforkdone(p1, flags)) {
                mutex_enter(p1->p_lock);
-               p1->p_vfpid_done = retval[0];
-               eventswitch(TRAP_CHLD);
+               eventswitch(TRAP_CHLD, PTRACE_VFORK_DONE, retval[0]);
        } else
                mutex_exit(proc_lock);
 
@@ -645,9 +636,10 @@
                        mutex_exit(proc_lock);
                        goto my_tracer_is_gone;
                }
-
                mutex_enter(p->p_lock);
-               eventswitch(TRAP_CHLD);
+               eventswitch(TRAP_CHLD,
+                   ISSET(p->p_lflag, PL_PPWAIT) ? PTRACE_VFORK : PTRACE_FORK,
+                   p->p_opptr->p_pid);
        }
 
 my_tracer_is_gone:
diff -r 132311301d52 -r 09bf512abf2d sys/kern/kern_lwp.c
--- a/sys/kern/kern_lwp.c       Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/kern/kern_lwp.c       Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_lwp.c,v 1.202 2019/06/04 11:54:03 kamil Exp $     */
+/*     $NetBSD: kern_lwp.c,v 1.202.2.1 2019/10/15 18:32:13 martin Exp $        */
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -211,7 +211,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.202 2019/06/04 11:54:03 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.202.2.1 2019/10/15 18:32:13 martin Exp $");
 
 #include "opt_ddb.h"
 #include "opt_lockdebug.h"
@@ -239,6 +239,7 @@
 #include <sys/fstrans.h>
 #include <sys/dtrace_bsd.h>
 #include <sys/sdt.h>
+#include <sys/ptrace.h>
 #include <sys/xcall.h>
 #include <sys/uidinfo.h>
 #include <sys/sysctl.h>
@@ -1091,8 +1092,7 @@
                         * about a terminating LWP as it would deadlock.
                         */
                } else {
-                       p->p_lwp_exited = l->l_lid;
-                       eventswitch(TRAP_LWP);
+                       eventswitch(TRAP_LWP, PTRACE_LWP_EXIT, l->l_lid);
                        mutex_enter(proc_lock);
                }
        }
diff -r 132311301d52 -r 09bf512abf2d sys/kern/kern_sig.c
--- a/sys/kern/kern_sig.c       Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/kern/kern_sig.c       Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_sig.c,v 1.364 2019/06/21 04:28:12 kamil Exp $     */
+/*     $NetBSD: kern_sig.c,v 1.364.2.1 2019/10/15 18:32:13 martin Exp $        */
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364 2019/06/21 04:28:12 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.1 2019/10/15 18:32:13 martin Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1560,7 +1560,7 @@
  * an event specific to a traced process only.
  */
 void
-eventswitch(int code)
+eventswitch(int code, int pe_report_event, int entity)
 {
        struct lwp *l = curlwp;
        struct proc *p = l->l_proc;
@@ -1605,8 +1605,12 @@
 
        KSI_INIT_TRAP(&ksi);
        ksi.ksi_lid = l->l_lid;
-       ksi.ksi_info._signo = signo;
-       ksi.ksi_info._code = code;
+       ksi.ksi_signo = signo;
+       ksi.ksi_code = code;
+       ksi.ksi_pe_report_event = pe_report_event;
+
+       CTASSERT(sizeof(ksi.ksi_pe_other_pid) == sizeof(ksi.ksi_pe_lwp));
+       ksi.ksi_pe_other_pid = entity;
 
        /* Needed for ktrace */
        ps = p->p_sigacts;
diff -r 132311301d52 -r 09bf512abf2d sys/kern/sys_lwp.c
--- a/sys/kern/sys_lwp.c        Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/kern/sys_lwp.c        Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_lwp.c,v 1.69 2019/07/10 17:52:22 maxv Exp $        */
+/*     $NetBSD: sys_lwp.c,v 1.69.2.1 2019/10/15 18:32:13 martin Exp $  */
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.69 2019/07/10 17:52:22 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.69.2.1 2019/10/15 18:32:13 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -45,6 +45,7 @@
 #include <sys/syscallargs.h>
 #include <sys/kauth.h>
 #include <sys/kmem.h>
+#include <sys/ptrace.h>
 #include <sys/sleepq.h>
 #include <sys/lwpctl.h>
 #include <sys/cpu.h>
@@ -91,8 +92,7 @@
                }
 
                mutex_enter(p->p_lock);
-               p->p_lwp_created = l->l_lid;
-               eventswitch(TRAP_LWP);
+               eventswitch(TRAP_LWP, PTRACE_LWP_CREATE, l->l_lid);
        }
 }
 
diff -r 132311301d52 -r 09bf512abf2d sys/kern/sys_ptrace_common.c
--- a/sys/kern/sys_ptrace_common.c      Tue Oct 15 18:22:55 2019 +0000
+++ b/sys/kern/sys_ptrace_common.c      Tue Oct 15 18:32:13 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_ptrace_common.c,v 1.58.2.1 2019/10/15 18:21:06 martin Exp $        */
+/*     $NetBSD: sys_ptrace_common.c,v 1.58.2.2 2019/10/15 18:32:13 martin Exp $        */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.1 2019/10/15 18:21:06 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.58.2.2 2019/10/15 18:32:13 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -694,27 +694,25 @@
                DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(ps)));
                return EINVAL;
        }
-       memset(&ps, 0, sizeof(ps));
+
+       if (t->p_sigctx.ps_info._signo != SIGTRAP ||
+           (t->p_sigctx.ps_info._code != TRAP_CHLD &&
+               t->p_sigctx.ps_info._code != TRAP_LWP)) {
+               return EINVAL;
+       }



Home | Main Index | Thread Index | Old Index