Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/x86/x86 x86: avoid accessing invalid addresses in d...



details:   https://anonhg.NetBSD.org/src/rev/a139b69e81bd
branches:  trunk
changeset: 321428:a139b69e81bd
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Fri Mar 16 04:48:19 2018 +0000

description:
x86: avoid accessing invalid addresses in ddb like arm32

This avoids that a command stops in the middle of an execution if
a fault occurs due to an access to an invalid address.

diffstat:

 sys/arch/x86/x86/db_memrw.c |  33 ++++++++++++++++++++++++++++++---
 1 files changed, 30 insertions(+), 3 deletions(-)

diffs (69 lines):

diff -r fa461e8aedc0 -r a139b69e81bd sys/arch/x86/x86/db_memrw.c
--- a/sys/arch/x86/x86/db_memrw.c       Fri Mar 16 04:45:20 2018 +0000
+++ b/sys/arch/x86/x86/db_memrw.c       Fri Mar 16 04:48:19 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: db_memrw.c,v 1.5 2018/03/15 03:45:05 ozaki-r Exp $     */
+/*     $NetBSD: db_memrw.c,v 1.6 2018/03/16 04:48:19 ozaki-r Exp $     */
 
 /*-
  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
@@ -53,7 +53,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.5 2018/03/15 03:45:05 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.6 2018/03/16 04:48:19 ozaki-r Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -64,6 +64,22 @@
 #include <ddb/db_access.h>
 #include <ddb/db_output.h>
 
+static int
+db_validate_address(vaddr_t addr)
+{
+       struct proc *p = curproc;
+       struct pmap *pmap;
+
+       if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap ||
+           addr >= VM_MIN_KERNEL_ADDRESS
+          )
+               pmap = pmap_kernel();
+       else
+               pmap = p->p_vmspace->vm_map.pmap;
+
+       return (pmap_extract(pmap, addr, NULL) == false);
+}
+
 /*
  * Read bytes from kernel address space for debugger.
  */
@@ -74,6 +90,11 @@
 
        src = (char *)addr;
 
+       if (db_validate_address((vaddr_t)src)) {
+               db_printf("address %p is invalid\n", src);
+               return;
+       }
+
        if (size == 8) {
                *((long *)data) = *((long *)src);
                return;
@@ -89,8 +110,14 @@
                return;
        }
 
-       while (size-- > 0)
+       while (size-- > 0) {
+               if (db_validate_address((vaddr_t)src)) {
+                       db_printf("address %p is invalid\n", src);
+                       return;
+               }
+
                *data++ = *src++;
+       }
 }
 
 /*



Home | Main Index | Thread Index | Old Index