Source-Changes-HG archive

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

[src/trunk]: src/sys/sys Fix the machine-dependent ptrace requests to respect...



details:   https://anonhg.NetBSD.org/src/rev/0e0d5313fa76
branches:  trunk
changeset: 940719:0e0d5313fa76
user:      mgorny <mgorny%NetBSD.org@localhost>
date:      Thu Oct 15 17:37:35 2020 +0000

description:
Fix the machine-dependent ptrace requests to respect LWP number

Fix the machine-dependent ptrace register-related requests (e.g.
PT_GETXMMREGS, PT_GETXSTATE on x86) to correctly respect the LWP number
passed as the data argument.  Before this change, these requests
did not operate on the requested LWP of a multithreaded program.

This change required moving ptrace_update_lwp() out of unit scope,
and changing ptrace_machdep_dorequest() function to take a pointer
to pointer as the second argument, consistently with ptrace_regs().

I am planning to extend the ATF ptrace() register tests in the future
to check for regressions in multithreaded programs, as time permits.

Reviewed by kamil.

diffstat:

 sys/arch/amd64/amd64/process_machdep.c     |  16 ++++++++++------
 sys/arch/i386/i386/process_machdep.c       |  18 +++++++++++-------
 sys/arch/powerpc/include/ptrace.h          |   4 ++--
 sys/arch/powerpc/powerpc/process_machdep.c |  12 +++++++-----
 sys/arch/sh3/include/ptrace.h              |   4 ++--
 sys/arch/sh3/sh3/process_machdep.c         |  12 +++++++-----
 sys/kern/sys_ptrace_common.c               |   8 ++++----
 sys/sys/ptrace.h                           |   5 +++--
 8 files changed, 46 insertions(+), 33 deletions(-)

diffs (truncated from 318 to 300 lines):

diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/arch/amd64/amd64/process_machdep.c
--- a/sys/arch/amd64/amd64/process_machdep.c    Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/arch/amd64/amd64/process_machdep.c    Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: process_machdep.c,v 1.47 2019/11/27 09:16:58 rin Exp $ */
+/*     $NetBSD: process_machdep.c,v 1.48 2020/10/15 17:37:35 mgorny Exp $      */
 
 /*
  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@@ -74,7 +74,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.47 2019/11/27 09:16:58 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.48 2020/10/15 17:37:35 mgorny Exp $");
 
 #include "opt_xen.h"
 #include <sys/param.h>
@@ -307,7 +307,7 @@
 int
 ptrace_machdep_dorequest(
     struct lwp *l,
-    struct lwp *lt,
+    struct lwp **lt,
     int req,
     void *addr,
     int data
@@ -326,7 +326,9 @@
                /* FALLTHROUGH */
        case PT_GETXSTATE:
                /* write = false done above. */
-               if (!process_machdep_validfpu(lt->l_proc))
+               if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0)
+                       return error;
+               if (!process_machdep_validfpu((*lt)->l_proc))
                        return EINVAL;
                if (__predict_false(l->l_proc->p_flag & PK_32)) {
                        struct netbsd32_iovec user_iov;
@@ -357,7 +359,7 @@
                uio.uio_resid = iov.iov_len;
                uio.uio_rw = write ? UIO_WRITE : UIO_READ;
                uio.uio_vmspace = vm;
-               error = process_machdep_doxstate(l, lt, &uio);
+               error = process_machdep_doxstate(l, *lt, &uio);
                uvmspace_free(vm);
                return error;
 
@@ -367,8 +369,10 @@
                /* FALLTHROUGH */
        case PT_GETXMMREGS:             /* only for COMPAT_NETBSD32 */
                /* write = false done above. */
+               if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0)
+                       return error;
                MODULE_HOOK_CALL(netbsd32_process_doxmmregs_hook,
-                   (l, lt, addr, write), EINVAL, error);
+                   (l, *lt, addr, write), EINVAL, error);
                return error;
        }
 
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/arch/i386/i386/process_machdep.c
--- a/sys/arch/i386/i386/process_machdep.c      Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/arch/i386/i386/process_machdep.c      Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: process_machdep.c,v 1.94 2019/08/06 02:04:43 kamil Exp $       */
+/*     $NetBSD: process_machdep.c,v 1.95 2020/10/15 17:37:35 mgorny Exp $      */
 
 /*-
  * Copyright (c) 1998, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.94 2019/08/06 02:04:43 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.95 2020/10/15 17:37:35 mgorny Exp $");
 
 #include "opt_ptrace.h"
 
@@ -273,7 +273,7 @@
 int
 ptrace_machdep_dorequest(
     struct lwp *l,
-    struct lwp *lt,
+    struct lwp **lt,
     int req,
     void *addr,
     int data
@@ -293,7 +293,9 @@
                /* FALLTHROUGH */
        case PT_GETXMMREGS:
                /* write = 0 done above. */
-               if (!process_machdep_validxmmregs(lt->l_proc))
+               if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0)
+                       return error;
+               if (!process_machdep_validxmmregs((*lt)->l_proc))
                        return (EINVAL);
                error = proc_vmspace_getref(l->l_proc, &vm);
                if (error) {
@@ -307,7 +309,7 @@
                uio.uio_resid = sizeof(struct xmmregs);
                uio.uio_rw = write ? UIO_WRITE : UIO_READ;
                uio.uio_vmspace = vm;
-               error = process_machdep_doxmmregs(l, lt, &uio);
+               error = process_machdep_doxmmregs(l, *lt, &uio);
                uvmspace_free(vm);
                return error;
 
@@ -317,7 +319,9 @@
                /* FALLTHROUGH */
        case PT_GETXSTATE:
                /* write = 0 done above. */
-               if (!process_machdep_validxstate(lt->l_proc))
+               if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0)
+                       return error;
+               if (!process_machdep_validxstate((*lt)->l_proc))
                        return EINVAL;
                if ((error = copyin(addr, &user_iov, sizeof(user_iov))) != 0)
                        return error;
@@ -335,7 +339,7 @@
                uio.uio_resid = iov.iov_len;
                uio.uio_rw = write ? UIO_WRITE : UIO_READ;
                uio.uio_vmspace = vm;
-               error = process_machdep_doxstate(l, lt, &uio);
+               error = process_machdep_doxstate(l, *lt, &uio);
                uvmspace_free(vm);
                return error;
        }
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/arch/powerpc/include/ptrace.h
--- a/sys/arch/powerpc/include/ptrace.h Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/arch/powerpc/include/ptrace.h Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ptrace.h,v 1.17 2020/06/21 00:39:59 rin Exp $  */
+/*     $NetBSD: ptrace.h,v 1.18 2020/10/15 17:37:35 mgorny Exp $       */
 
 #ifndef _POWERPC_PTRACE_H
 #define        _POWERPC_PTRACE_H
@@ -41,7 +41,7 @@
        case PT_GETVECREGS: \
        case PT_SETVECREGS:
 
-int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, void *, int);
+int ptrace_machdep_dorequest(struct lwp *, struct lwp **, int, void *, int);
 int process_machdep_dovecregs(struct lwp *, struct lwp *, struct uio *);
 int process_machdep_validvecregs(struct proc *);
 
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/arch/powerpc/powerpc/process_machdep.c
--- a/sys/arch/powerpc/powerpc/process_machdep.c        Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/arch/powerpc/powerpc/process_machdep.c        Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: process_machdep.c,v 1.39 2020/07/06 09:34:18 rin Exp $ */
+/*     $NetBSD: process_machdep.c,v 1.40 2020/10/15 17:37:36 mgorny Exp $      */
 
 /*
  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.39 2020/07/06 09:34:18 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.40 2020/10/15 17:37:36 mgorny Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_altivec.h"
@@ -188,7 +188,7 @@
 }
 
 int
-ptrace_machdep_dorequest(struct lwp *l, struct lwp *lt,
+ptrace_machdep_dorequest(struct lwp *l, struct lwp **lt,
        int req, void *addr, int data)
 {
        struct uio uio;
@@ -201,7 +201,9 @@
 
        case PT_GETVECREGS:
                /* write = 0 done above. */
-               if (!process_machdep_validvecregs(lt->l_proc))
+               if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0)
+                       return error;
+               if (!process_machdep_validvecregs((*lt)->l_proc))
                        return (EINVAL);
                iov.iov_base = addr;
                iov.iov_len = sizeof(struct vreg);
@@ -211,7 +213,7 @@
                uio.uio_resid = sizeof(struct vreg);
                uio.uio_rw = write ? UIO_WRITE : UIO_READ;
                uio.uio_vmspace = l->l_proc->p_vmspace;
-               return process_machdep_dovecregs(l, lt, &uio);
+               return process_machdep_dovecregs(l, *lt, &uio);
        }
 
 #ifdef DIAGNOSTIC
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/arch/sh3/include/ptrace.h
--- a/sys/arch/sh3/include/ptrace.h     Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/arch/sh3/include/ptrace.h     Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ptrace.h,v 1.18 2020/09/14 05:04:05 mrg Exp $  */
+/*     $NetBSD: ptrace.h,v 1.19 2020/10/15 17:37:36 mgorny Exp $       */
 
 /*
  * Copyright (c) 1993 Christopher G. Demetriou
@@ -95,7 +95,7 @@
 #endif /* COMPAT_40 */
 
 #ifdef __HAVE_PTRACE_MACHDEP
-int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, void *, int);
+int ptrace_machdep_dorequest(struct lwp *, struct lwp **, int, void *, int);
 #endif
 
 #define PTRACE_LWP_GETPRIVATE(l) (l)->l_md.md_regs->tf_gbr
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/arch/sh3/sh3/process_machdep.c
--- a/sys/arch/sh3/sh3/process_machdep.c        Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/arch/sh3/sh3/process_machdep.c        Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: process_machdep.c,v 1.22 2019/11/20 19:37:52 pgoyette Exp $    */
+/*     $NetBSD: process_machdep.c,v 1.23 2020/10/15 17:37:36 mgorny Exp $      */
 
 /*
  * Copyright (c) 1993 The Regents of the University of California.
@@ -77,7 +77,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.22 2019/11/20 19:37:52 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.23 2020/10/15 17:37:36 mgorny Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -183,7 +183,7 @@
 #ifdef __HAVE_PTRACE_MACHDEP
 
 int
-ptrace_machdep_dorequest(struct lwp *l, struct lwp *lt,
+ptrace_machdep_dorequest(struct lwp *l, struct lwp **lt,
                         int req, void *addr, int data)
 {
        struct uio uio;
@@ -200,7 +200,9 @@
                /* FALLTHROUGH*/
 
        case PT___GETREGS40:
-               if (!process_validregs(lt))
+               if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0)
+                       return error;
+               if (!process_validregs(*lt))
                        return EINVAL;
                iov.iov_base = addr;
                iov.iov_len = sizeof(struct __reg40);
@@ -210,7 +212,7 @@
                uio.uio_resid = sizeof(struct __reg40);
                uio.uio_rw = write ? UIO_WRITE : UIO_READ;
                uio.uio_vmspace = l->l_proc->p_vmspace;
-               return process_machdep_doregs40(l, lt, &uio);
+               return process_machdep_doregs40(l, *lt, &uio);
 #endif /* COMPAT_40 */
        }
 }
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/kern/sys_ptrace_common.c
--- a/sys/kern/sys_ptrace_common.c      Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/kern/sys_ptrace_common.c      Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_ptrace_common.c,v 1.83 2020/05/30 08:41:22 maxv Exp $      */
+/*     $NetBSD: sys_ptrace_common.c,v 1.84 2020/10/15 17:37:36 mgorny 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.83 2020/05/30 08:41:22 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.84 2020/10/15 17:37:36 mgorny Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ptrace.h"
@@ -565,7 +565,7 @@
        }
 }
 
-static int
+int
 ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid)
 {
        if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1)
@@ -1569,7 +1569,7 @@
 
 #ifdef __HAVE_PTRACE_MACHDEP
        PTRACE_MACHDEP_REQUEST_CASES
-               error = ptrace_machdep_dorequest(l, lt, req, addr, data);
+               error = ptrace_machdep_dorequest(l, &lt, req, addr, data);
                break;
 #endif
        }
diff -r 08b39a0b8574 -r 0e0d5313fa76 sys/sys/ptrace.h
--- a/sys/sys/ptrace.h  Thu Oct 15 10:20:44 2020 +0000
+++ b/sys/sys/ptrace.h  Thu Oct 15 17:37:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ptrace.h,v 1.70 2020/05/14 13:32:15 kamil Exp $        */
+/*     $NetBSD: ptrace.h,v 1.71 2020/10/15 17:37:36 mgorny Exp $       */
 
 /*-



Home | Main Index | Thread Index | Old Index