Source-Changes-HG archive

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

[src/trunk]: src PR kern/51514: ptrace(2) fails for 32-bit process on 64-bit ...



details:   https://anonhg.NetBSD.org/src/rev/c8f257364ec0
branches:  trunk
changeset: 348459:c8f257364ec0
user:      skrll <skrll%NetBSD.org@localhost>
date:      Wed Oct 19 09:44:00 2016 +0000

description:
PR kern/51514: ptrace(2) fails for 32-bit process on 64-bit kernel

Updated from the original patch in the PR by me.

diffstat:

 distrib/sets/lists/modules/ad.arm           |    4 +-
 distrib/sets/lists/modules/ad.mips          |    4 +-
 distrib/sets/lists/modules/md.amd64         |    4 +-
 sys/arch/amd64/amd64/netbsd32_machdep.c     |   70 ++++++--
 sys/arch/amd64/include/netbsd32_machdep.h   |    5 +-
 sys/arch/amd64/include/ptrace.h             |    5 +-
 sys/arch/sparc64/include/netbsd32_machdep.h |    5 +-
 sys/arch/sparc64/include/ptrace.h           |    5 +-
 sys/arch/sparc64/sparc64/netbsd32_machdep.c |   15 +-
 sys/compat/netbsd32/files.netbsd32          |    3 +-
 sys/compat/netbsd32/netbsd32.h              |   12 +-
 sys/compat/netbsd32/netbsd32_fs.c           |    5 +-
 sys/compat/netbsd32/netbsd32_netbsd.c       |   23 +--
 sys/compat/netbsd32/netbsd32_ptrace.c       |  217 ++++++++++++++++++++++++++++
 sys/compat/netbsd32/syscalls.master         |    5 +-
 sys/kern/sys_process.c                      |  123 +++++++++------
 sys/modules/Makefile                        |    4 +-
 sys/modules/compat_netbsd32_ptrace/Makefile |   15 +
 sys/sys/proc.h                              |    6 +-
 sys/sys/ptrace.h                            |   25 +++-
 20 files changed, 442 insertions(+), 113 deletions(-)

diffs (truncated from 1103 to 300 lines):

diff -r 93798c819890 -r c8f257364ec0 distrib/sets/lists/modules/ad.arm
--- a/distrib/sets/lists/modules/ad.arm Wed Oct 19 08:55:23 2016 +0000
+++ b/distrib/sets/lists/modules/ad.arm Wed Oct 19 09:44:00 2016 +0000
@@ -1,9 +1,11 @@
-# $NetBSD: ad.arm,v 1.8 2015/12/03 10:38:21 pgoyette Exp $
+# $NetBSD: ad.arm,v 1.9 2016/10/19 09:44:00 skrll Exp $
 
 ./@MODULEDIR@/compat_netbsd32                  base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32/compat_netbsd32.kmod     base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_mqueue           base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_mqueue/compat_netbsd32_mqueue.kmod       base-kernel-modules     kmod
+./@MODULEDIR@/compat_netbsd32_ptrace           base-kernel-modules     kmod
+./@MODULEDIR@/compat_netbsd32_ptrace/compat_netbsd32_ptrace.kmod       base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_nfssrv           base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_nfssrv/compat_netbsd32_nfssrv.kmod       base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_sysvipc          base-kernel-modules     kmod
diff -r 93798c819890 -r c8f257364ec0 distrib/sets/lists/modules/ad.mips
--- a/distrib/sets/lists/modules/ad.mips        Wed Oct 19 08:55:23 2016 +0000
+++ b/distrib/sets/lists/modules/ad.mips        Wed Oct 19 09:44:00 2016 +0000
@@ -1,8 +1,10 @@
-# $NetBSD: ad.mips,v 1.8 2015/12/03 10:38:21 pgoyette Exp $
+# $NetBSD: ad.mips,v 1.9 2016/10/19 09:44:00 skrll Exp $
 ./@MODULEDIR@/compat_netbsd32                  base-kernel-modules     kmod,arch64,nocompatmodules
 ./@MODULEDIR@/compat_netbsd32/compat_netbsd32.kmod     base-kernel-modules     kmod,arch64,nocompatmodules
 ./@MODULEDIR@/compat_netbsd32_mqueue                   base-kernel-modules     kmod,arch64,nocompatmodules
 ./@MODULEDIR@/compat_netbsd32_mqueue/compat_netbsd32_mqueue.kmod       base-kernel-modules     kmod,arch64,nocompatmodules
+./@MODULEDIR@/compat_netbsd32_ptrace                   base-kernel-modules     kmod,arch64,nocompatmodules
+./@MODULEDIR@/compat_netbsd32_ptrace/compat_netbsd32_ptrace.kmod       base-kernel-modules     kmod,arch64,nocompatmodules
 ./@MODULEDIR@/compat_netbsd32_nfssrv                   base-kernel-modules     kmod,arch64,nocompatmodules
 ./@MODULEDIR@/compat_netbsd32_nfssrv/compat_netbsd32_nfssrv.kmod       base-kernel-modules     kmod,arch64,nocompatmodules
 ./@MODULEDIR@/compat_netbsd32_sysvipc                  base-kernel-modules     kmod,arch64,nocompatmodules
diff -r 93798c819890 -r c8f257364ec0 distrib/sets/lists/modules/md.amd64
--- a/distrib/sets/lists/modules/md.amd64       Wed Oct 19 08:55:23 2016 +0000
+++ b/distrib/sets/lists/modules/md.amd64       Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: md.amd64,v 1.69 2016/09/27 20:52:43 pgoyette Exp $
+# $NetBSD: md.amd64,v 1.70 2016/10/19 09:44:00 skrll Exp $
 #
 # NOTE that there are two sets of files here:
 # @MODULEDIR@ and amd64-xen
@@ -53,6 +53,8 @@
 ./@MODULEDIR@/compat_netbsd32/compat_netbsd32.kmod     base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_mqueue           base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_mqueue/compat_netbsd32_mqueue.kmod       base-kernel-modules     kmod
+./@MODULEDIR@/compat_netbsd32_ptrace           base-kernel-modules     kmod
+./@MODULEDIR@/compat_netbsd32_ptrace/compat_netbsd32_ptrace.kmod       base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_nfssrv           base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_nfssrv/compat_netbsd32_nfssrv.kmod       base-kernel-modules     kmod
 ./@MODULEDIR@/compat_netbsd32_sysvipc          base-kernel-modules     kmod
diff -r 93798c819890 -r c8f257364ec0 sys/arch/amd64/amd64/netbsd32_machdep.c
--- a/sys/arch/amd64/amd64/netbsd32_machdep.c   Wed Oct 19 08:55:23 2016 +0000
+++ b/sys/arch/amd64/amd64/netbsd32_machdep.c   Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: netbsd32_machdep.c,v 1.96 2016/09/02 07:51:05 maxv Exp $       */
+/*     $NetBSD: netbsd32_machdep.c,v 1.97 2016/10/19 09:44:00 skrll Exp $      */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.96 2016/09/02 07:51:05 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.97 2016/10/19 09:44:00 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -487,12 +487,12 @@
 {
        struct trapframe *tf = l->l_md.md_regs;
 
-       regs->r_gs = LSEL(LUCODE32_SEL, SEL_UPL);
-       regs->r_fs = LSEL(LUCODE32_SEL, SEL_UPL);
-       regs->r_es = LSEL(LUCODE32_SEL, SEL_UPL);
-       regs->r_ds = LSEL(LUCODE32_SEL, SEL_UPL);
+       /* XXX avoid sign extension problems with unknown upper bits? */
+       regs->r_gs = tf->tf_gs & 0xffff;
+       regs->r_fs = tf->tf_fs & 0xffff;
+       regs->r_es = tf->tf_es & 0xffff;
+       regs->r_ds = tf->tf_ds & 0xffff;
        regs->r_eflags = tf->tf_rflags;
-       /* XXX avoid sign extension problems with unknown upper bits? */
        regs->r_edi = tf->tf_rdi & 0xffffffff;
        regs->r_esi = tf->tf_rsi & 0xffffffff;
        regs->r_ebp = tf->tf_rbp & 0xffffffff;
@@ -501,9 +501,9 @@
        regs->r_ecx = tf->tf_rcx & 0xffffffff;
        regs->r_eax = tf->tf_rax & 0xffffffff;
        regs->r_eip = tf->tf_rip & 0xffffffff;
-       regs->r_cs = tf->tf_cs;
+       regs->r_cs = tf->tf_cs & 0xffff;
        regs->r_esp = tf->tf_rsp & 0xffffffff;
-       regs->r_ss = tf->tf_ss;
+       regs->r_ss = tf->tf_ss & 0xffff;
 
        return (0);
 }
@@ -511,22 +511,52 @@
 int
 netbsd32_process_read_fpregs(struct lwp *l, struct fpreg32 *regs, size_t *sz)
 {
-       struct fpreg regs64;
-       int error;
-       size_t fp_size;
+
+       __CTASSERT(sizeof *regs == sizeof (struct save87));
+       process_read_fpregs_s87(l, (struct save87 *)regs);
+       return 0;
+}
+
+int
+netbsd32_process_write_regs(struct lwp *l, const struct reg32 *regs)
+{
+       struct trapframe *tf = l->l_md.md_regs;
 
        /*
-        * All that stuff makes no sense in i386 code :(
+        * Check for security violations. Taken from i386/process_machdep.c.
         */
+       if (((regs->r_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0 ||
+           !VALID_USER_CSEL32(regs->r_cs))
+               return EINVAL;
 
-       fp_size = sizeof regs64;
-       error = process_read_fpregs(l, &regs64, &fp_size);
-       if (error)
-               return error;
+       tf->tf_rax = regs->r_eax;
+       tf->tf_rcx = regs->r_ecx;
+       tf->tf_rdx = regs->r_edx;
+       tf->tf_rbx = regs->r_ebx;
+       tf->tf_rsp = regs->r_esp;
+       tf->tf_rbp = regs->r_ebp;
+       tf->tf_rsi = regs->r_esi;
+       tf->tf_rdi = regs->r_edi;
+       tf->tf_rip = regs->r_eip;
+       tf->tf_rflags = regs->r_eflags;
+       tf->tf_cs = regs->r_cs;
+       tf->tf_ss = regs->r_ss;
+       tf->tf_ds = regs->r_ds;
+       tf->tf_es = regs->r_es;
+       tf->tf_fs = regs->r_fs;
+       tf->tf_gs = regs->r_gs;
+
+       return 0;
+}
+
+int
+netbsd32_process_write_fpregs(struct lwp *l, const struct fpreg32 *regs,
+    size_t sz)
+{
+
        __CTASSERT(sizeof *regs == sizeof (struct save87));
-       process_xmm_to_s87(&regs64.fxstate, (struct save87 *)regs);
-
-       return (0);
+       process_write_fpregs_s87(l, (const struct save87 *)regs);
+       return 0;
 }
 
 int
diff -r 93798c819890 -r c8f257364ec0 sys/arch/amd64/include/netbsd32_machdep.h
--- a/sys/arch/amd64/include/netbsd32_machdep.h Wed Oct 19 08:55:23 2016 +0000
+++ b/sys/arch/amd64/include/netbsd32_machdep.h Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: netbsd32_machdep.h,v 1.19 2014/02/07 22:40:22 dsl Exp $        */
+/*     $NetBSD: netbsd32_machdep.h,v 1.20 2016/10/19 09:44:00 skrll Exp $      */
 
 #ifndef _MACHINE_NETBSD32_H_
 #define _MACHINE_NETBSD32_H_
@@ -138,4 +138,7 @@
 int netbsd32_process_read_regs(struct lwp *, struct reg32 *);
 int netbsd32_process_read_fpregs(struct lwp *, struct fpreg32 *, size_t *);
 
+int netbsd32_process_write_regs(struct lwp *, const struct reg32 *);
+int netbsd32_process_write_fpregs(struct lwp *, const struct fpreg32 *, size_t);
+
 #endif /* _MACHINE_NETBSD32_H_ */
diff -r 93798c819890 -r c8f257364ec0 sys/arch/amd64/include/ptrace.h
--- a/sys/arch/amd64/include/ptrace.h   Wed Oct 19 08:55:23 2016 +0000
+++ b/sys/arch/amd64/include/ptrace.h   Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ptrace.h,v 1.6 2015/09/25 16:05:17 christos Exp $      */
+/*     $NetBSD: ptrace.h,v 1.7 2016/10/19 09:44:00 skrll Exp $ */
 
 /*
  * Copyright (c) 1993 Christopher G. Demetriou
@@ -68,6 +68,9 @@
 #define process_read_regs32    netbsd32_process_read_regs
 #define process_read_fpregs32  netbsd32_process_read_fpregs
 
+#define process_write_regs32   netbsd32_process_write_regs
+#define process_write_fpregs32 netbsd32_process_write_fpregs
+
 #define process_reg32          struct reg32
 #define process_fpreg32                struct fpreg32
 #endif /* COMPAT_NETBSD32 */
diff -r 93798c819890 -r c8f257364ec0 sys/arch/sparc64/include/netbsd32_machdep.h
--- a/sys/arch/sparc64/include/netbsd32_machdep.h       Wed Oct 19 08:55:23 2016 +0000
+++ b/sys/arch/sparc64/include/netbsd32_machdep.h       Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: netbsd32_machdep.h,v 1.28 2014/01/04 00:10:03 dsl Exp $        */
+/*     $NetBSD: netbsd32_machdep.h,v 1.29 2016/10/19 09:44:00 skrll Exp $      */
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -79,4 +79,7 @@
 int netbsd32_process_read_regs(struct lwp *, struct reg32 *);
 int netbsd32_process_read_fpregs(struct lwp *, struct fpreg32 *, size_t *);
 
+int netbsd32_process_write_regs(struct lwp *, const struct reg32 *);
+int netbsd32_process_write_fpregs(struct lwp *, const struct fpreg32 *, size_t);
+
 #endif /* _MACHINE_NETBSD32_H_ */
diff -r 93798c819890 -r c8f257364ec0 sys/arch/sparc64/include/ptrace.h
--- a/sys/arch/sparc64/include/ptrace.h Wed Oct 19 08:55:23 2016 +0000
+++ b/sys/arch/sparc64/include/ptrace.h Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-/*       $NetBSD: ptrace.h,v 1.4 2016/09/28 11:44:16 skrll Exp $        */
+/*       $NetBSD: ptrace.h,v 1.5 2016/10/19 09:44:00 skrll Exp $        */
 
 #include <sparc/ptrace.h>
 
@@ -11,6 +11,9 @@
 #define process_read_regs32    netbsd32_process_read_regs
 #define process_read_fpregs32  netbsd32_process_read_fpregs
 
+#define process_write_regs32   netbsd32_process_write_regs
+#define process_write_fpregs32 netbsd32_process_write_fpregs
+
 #define process_reg32          struct reg32
 #define process_fpreg32                struct fpreg32
 #endif
diff -r 93798c819890 -r c8f257364ec0 sys/arch/sparc64/sparc64/netbsd32_machdep.c
--- a/sys/arch/sparc64/sparc64/netbsd32_machdep.c       Wed Oct 19 08:55:23 2016 +0000
+++ b/sys/arch/sparc64/sparc64/netbsd32_machdep.c       Wed Oct 19 09:44:00 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: netbsd32_machdep.c,v 1.109 2015/11/26 13:15:34 martin Exp $    */
+/*     $NetBSD: netbsd32_machdep.c,v 1.110 2016/10/19 09:44:01 skrll Exp $     */
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.109 2015/11/26 13:15:34 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.110 2016/10/19 09:44:01 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -624,16 +624,15 @@
        return (0);
 }
 
-#if 0
 int
 netbsd32_process_write_regs(struct lwp *l, const struct reg32 *regs)
 {
-       struct trapframe64* tf = p->p_md.md_tf;
+       struct trapframe64* tf = l->l_md.md_tf;
        int i;
 
        tf->tf_pc = regs->r_pc;
        tf->tf_npc = regs->r_npc;
-       tf->tf_y = regs->r_pc;
+       tf->tf_y = regs->r_y;
        for (i = 0; i < 8; i++) {
                tf->tf_global[i] = regs->r_global[i];
                tf->tf_out[i] = regs->r_out[i];
@@ -643,7 +642,6 @@
                PSRCC_TO_TSTATE(regs->r_psr);
        return (0);
 }
-#endif
 
 int
 netbsd32_process_read_fpregs(struct lwp *l, struct fpreg32 *regs, size_t *sz)
@@ -661,9 +659,9 @@
        return 0;
 }
 
-#if 0
 int
-netbsd32_process_write_fpregs(struct lwp *l, const struct fpreg32 *regs)
+netbsd32_process_write_fpregs(struct lwp *l, const struct fpreg32 *regs,
+    size_t sz)
 {
        struct fpstate64        *statep;
        int i;
@@ -678,7 +676,6 @@
 
        return 0;
 }
-#endif
 
 /*
  * 32-bit version of cpu_coredump.
diff -r 93798c819890 -r c8f257364ec0 sys/compat/netbsd32/files.netbsd32
--- a/sys/compat/netbsd32/files.netbsd32        Wed Oct 19 08:55:23 2016 +0000



Home | Main Index | Thread Index | Old Index