Skip to contentSkip to conversation

X86/mm limit mmap of /dev/mem to valid addrs.

PATCH New Craig Bergstrom 2 participants 2 messages Oct 19, 2017 – Oct 20, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 X86/mm limit mmap of /dev/mem to valid addrs.

Currently, it is possible to mmap any offset from /dev/mem. If a program mmaps() /dev/mem offsets outside of the addressable limits of a system, the page table is corrupted by setting some reserved bits.

If you mmap offset 0x0001000000000000 of /dev/mem on an x86_64 with a 48-bit bus, the page fault handler will be called with error_code set to RSVD. The kernel then crashes with a page table corruption error.

This change prevents this page table corruption on x86 by refusing to mmap offsets higher than the highest valid address in the system.

---
 arch/x86/include/asm/io.h |  4 ++++
 arch/x86/mm/mmap.c        | 12 ++++++++++++
 2 files changed, 16 insertions(+)
arch/x86/include/asm/io.h +4 -0
index c40a95c33bb8..322d25ae23ab 100644
@@ -110,6 +110,10 @@ build_mmio_write(__writeq, "q", unsigned long, "r", )
 
 #endif
 
+#define ARCH_HAS_VALID_PHYS_ADDR_RANGE
+extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
+extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
+
 /**
  *	virt_to_phys	-	map virtual addresses to physical
  *	@address: address to remap
arch/x86/mm/mmap.c +12 -0
index a99679826846..320c6237e1d1 100644
@@ -174,3 +174,15 @@ const char *arch_vma_name(struct vm_area_struct *vma)
 		return "[mpx]";
 	return NULL;
 }
+
+int valid_phys_addr_range(phys_addr_t addr, size_t count)
+{
+	return addr + count <= __pa(high_memory);
+}
+
+int valid_mmap_phys_addr_range(unsigned long pfn, size_t count)
+{
+	phys_addr_t addr = (phys_addr_t)pfn << PAGE_SHIFT;
+
+	return valid_phys_addr_range(addr, count);
+}
Signature · 1 line
2.15.0.rc1.287.g2b38de12cc-goog
#1 lore raw

[tip:x86/mm] x86/mm: Limit mmap() of /dev/mem to valid physical addresses

x86/mm: Limit mmap() of /dev/mem to valid physical addresses
Commit-ID:  ce56a86e2ade45d052b3228cdfebe913a1ae7381
Gitweb:     https://git.kernel.org/tip/ce56a86e2ade45d052b3228cdfebe913a1ae7381
Author:     Craig Bergstrom <[email protected]>
AuthorDate: Thu, 19 Oct 2017 13:28:56 -0600
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 20 Oct 2017 09:48:00 +0200

x86/mm: Limit mmap() of /dev/mem to valid physical addresses

Currently, it is possible to mmap() any offset from /dev/mem. If a program mmaps() /dev/mem offsets outside of the addressable limits of a system, the page table can be corrupted by setting reserved bits.

For example if you mmap() offset 0x0001000000000000 of /dev/mem on an x86_64 system with a 48-bit bus, the page fault handler will be called with error_code set to RSVD. The kernel then crashes with a page table corruption error.

This change prevents this page table corruption on x86 by refusing to mmap offsets higher than the highest valid address in the system.

Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Luis R. Rodriguez <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Toshi Kani <[email protected]>
---
 arch/x86/include/asm/io.h |  4 ++++
 arch/x86/mm/mmap.c        | 12 ++++++++++++
 2 files changed, 16 insertions(+)
arch/x86/include/asm/io.h +4 -0
index c40a95c..322d25a 100644
@@ -110,6 +110,10 @@ build_mmio_write(__writeq, "q", unsigned long, "r", )
 
 #endif
 
+#define ARCH_HAS_VALID_PHYS_ADDR_RANGE
+extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
+extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
+
 /**
  *	virt_to_phys	-	map virtual addresses to physical
  *	@address: address to remap
arch/x86/mm/mmap.c +12 -0
index a996798..320c623 100644
@@ -174,3 +174,15 @@ const char *arch_vma_name(struct vm_area_struct *vma)
 		return "[mpx]";
 	return NULL;
 }
+
+int valid_phys_addr_range(phys_addr_t addr, size_t count)
+{
+	return addr + count <= __pa(high_memory);
+}
+
+int valid_mmap_phys_addr_range(unsigned long pfn, size_t count)
+{
+	phys_addr_t addr = (phys_addr_t)pfn << PAGE_SHIFT;
+
+	return valid_phys_addr_range(addr, count);
+}