Skip to contentSkip to conversation

Fix devmem_is_allowed for below 1MB accesses for an efi machine

PATCH In review T Makphaibulchoke 4 participants 9 messages Oct 2, 2012 – Oct 3, 2012

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

PATCH 1/1 Fix devmem_is_allowed for below 1MB accesses for an efi machine

Changing devmem_is_allowed so that on an EFI machine, access to physical address below 1 MB is allowed only to physical pages that are valid in the EFI memory map. This prevents the possibility of an MCE due to accessing an invalid physical address.

---
 arch/x86/mm/init.c |   12 ++++++++++--
 include/linux/mm.h |    1 +
 kernel/resource.c  |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 2 deletions(-)
arch/x86/mm/init.c +10 -2
index ab1f6a9..3ed95c5 100644
@@ -4,6 +4,7 @@
 #include <linux/swap.h>
 #include <linux/memblock.h>
 #include <linux/bootmem.h>	/* for max_low_pfn */
+#include <linux/efi.h>		/* for efi_enabled */
 
 #include <asm/cacheflush.h>
 #include <asm/e820.h>
@@ -319,8 +320,15 @@ unsigned long __init_refok init_memory_mapping(unsigned long start,
  */
 int devmem_is_allowed(unsigned long pagenr)
 {
-	if (pagenr < 256)
-		return 1;
+	if (pagenr < 256) {
+		if (!efi_enabled)
+			return 1;
+		/* For EFI, allow access only to valid physical addresses. */
+		if (page_is_valid(pagenr))
+			return 1;
+		return 0;
+	}
+
 	if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
 		return 0;
 	if (!page_is_ram(pagenr))
include/linux/mm.h +1 -0
index 311be90..fd1bcd4 100644
@@ -288,6 +288,7 @@ static inline int get_page_unless_zero(struct page *page)
 }
 
 extern int page_is_ram(unsigned long pfn);
+extern int page_is_valid(unsigned long pfn);
 
 /* Support for virtually mapped pages */
 struct page *vmalloc_to_page(const void *addr);
kernel/resource.c +47 -0

53 lines not on this page · raw message

Signature · 7 lines
1.7.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 4 lines · T Makphaibulchoke

Changing devmem_is_allowed so that on an EFI machine, access to physical address below 1 MB is allowed only to physical pages that are valid in the EFI memory map. This prevents the possibility of an MCE due to accessing an invalid physical address.

What?

That sounds like exactly the opposite of normal /dev/mem behavior... we allow access to non-memory resources (which really could do anything if misused), but not memory.

You seem like you're flipping it on its head.

-hpa

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 5 lines · H. Peter Anvin

That sounds like exactly the opposite of normal /dev/mem behavior... we allow access to non-memory resources (which really could do anything if misused), but not memory.

From arch/x86/mm/init.c:

* On x86, access has to be given to the first megabyte of ram because that area
* contains bios code and data regions used by X and dosemu and similar apps.

Limiting this to just RAM would be safer than it currently is. I'm not convinced that there's any good reason to allow *any* access down there for EFI systems, though.

Signature · 1 line
Matthew Garrett | [email protected]
#3 lore raw
Quoted 6 lines · Matthew Garrett
Quoted 3 lines · H. Peter Anvin

That sounds like exactly the opposite of normal /dev/mem behavior... we allow access to non-memory resources (which really could do anything if misused), but not memory.

Quoted 9 lines

From arch/x86/mm/init.c:

* On x86, access has to be given to the first megabyte of ram because that area
* contains bios code and data regions used by X and dosemu and similar apps.

Limiting this to just RAM would be safer than it currently is. I'm not convinced that there's any good reason to allow *any* access down there for EFI systems, though.

Sorry, fail.

We *always* expose the I/O regions to /dev/mem. That is what /dev/mem *does*. The above is an exception (which is really obsolete, too: we should simply disallow access to anything which is treated as system RAM, which doesn't include the BIOS regions in question; the only reason we don't is that some versions of X take a checksum of the RAM in the first megabyte as some kind of idiotic random seed.)

-hpa

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 6 lines · H. Peter Anvin

We *always* expose the I/O regions to /dev/mem. That is what /dev/mem *does*. The above is an exception (which is really obsolete, too: we should simply disallow access to anything which is treated as system RAM, which doesn't include the BIOS regions in question; the only reason we don't is that some versions of X take a checksum of the RAM in the first megabyte as some kind of idiotic random seed.)

Oh, right, got you. In that case I think we potentially need a finer-grained check on EFI platforms - the EFI memory map is kind enough to tell us the difference between unusable regions and io regions, and we could avoid access to the unusable ones.

Signature · 7 lines
Matthew Garrett | [email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

Thank you both for the comments.

Sounds like a better solution is to allow accesses to only I/O regions presented in the EFI memory map for physical addresses below 1 MB.

Do we need to worry about the X checksum in the first MB on an EFI system?

Thanks,
Mak.

Quoted 9 lines · Matthew Garrett
Quoted 6 lines · H. Peter Anvin

We *always* expose the I/O regions to /dev/mem. That is what /dev/mem *does*. The above is an exception (which is really obsolete, too: we should simply disallow access to anything which is treated as system RAM, which doesn't include the BIOS regions in question; the only reason we don't is that some versions of X take a checksum of the RAM in the first megabyte as some kind of idiotic random seed.)

Quoted 5 lines

Oh, right, got you. In that case I think we potentially need a finer-grained check on EFI platforms - the EFI memory map is kind enough to tell us the difference between unusable regions and io regions, and we could avoid access to the unusable ones.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
In reply to Matthew Garrett
Quoted 9 lines · Matthew Garrett
Quoted 6 lines · H. Peter Anvin

We *always* expose the I/O regions to /dev/mem. That is what /dev/mem *does*. The above is an exception (which is really obsolete, too: we should simply disallow access to anything which is treated as system RAM, which doesn't include the BIOS regions in question; the only reason we don't is that some versions of X take a checksum of the RAM in the first megabyte as some kind of idiotic random seed.)

Quoted 5 lines

Oh, right, got you. In that case I think we potentially need a finer-grained check on EFI platforms - the EFI memory map is kind enough to tell us the difference between unusable regions and io regions, and we could avoid access to the unusable ones.

Well, we have the same in BIOS space with "reserved" regions. The problem is that they are actually I/O regions as far as programs like X, dmidecode and so on.

-hpa

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
In reply to Thavatchai Makphaibulchoke
Quoted 2 lines · Thavatchai Makphaibulchoke

Sounds like a better solution is to allow accesses to only I/O regions presented in the EFI memory map for physical addresses below 1 MB.

That won't work - unfortunately we do still need the low region to be available for X because some platforms expect us to use int10 even on EFI (yes, yes, I know). Do you have a copy of the EFI memory map for a system that's broken with the current code?

Signature · 1 line
Matthew Garrett | [email protected]
#7 lore raw
Quoted 10 lines · Matthew Garrett
Quoted 2 lines · Thavatchai Makphaibulchoke

Sounds like a better solution is to allow accesses to only I/O regions presented in the EFI memory map for physical addresses below 1 MB.

That won't work - unfortunately we do still need the low region to be available for X because some platforms expect us to use int10 even on EFI (yes, yes, I know). Do you have a copy of the EFI memory map for a system that's broken with the current code?

I honestly think this calls for a quirk, or more likely, no action at all ("don't do that, then.")

-hpa

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>