Module Name:    src
Committed By:   ozaki-r
Date:           Fri Mar 16 04:48:19 UTC 2018

Modified Files:
        src/sys/arch/x86/x86: db_memrw.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/x86/x86/db_memrw.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/db_memrw.c
diff -u src/sys/arch/x86/x86/db_memrw.c:1.5 src/sys/arch/x86/x86/db_memrw.c:1.6
--- src/sys/arch/x86/x86/db_memrw.c:1.5	Thu Mar 15 03:45:05 2018
+++ src/sys/arch/x86/x86/db_memrw.c	Fri Mar 16 04:48:19 2018
@@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v
 #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 @@ db_read_bytes(vaddr_t addr, size_t size,
 
 	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 @@ db_read_bytes(vaddr_t addr, size_t size,
 		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++;
+	}
 }
 
 /*

Reply via email to