Skip to contentSkip to conversation

x86/kaslr: Revisit entropy when CONFIG_PCI_P2PDMA is enabled

PATCH Superseded Balbir Singh 3 participants 7 messages Feb 6, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/kaslr: Revisit entropy when CONFIG_PCI_P2PDMA is enabled

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

Cc Dave Hansen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

---

Testing:

commit 0483e1fa6e09d ("x86/mm: Implement ASLR for kernel memory regions") mentions the problems that the following problems need to be addressed.

1 The three target memory sections are never at the same place between
boots.
2 The physical memory mapping can use a virtual address not aligned on
the PGD page table.
3 Have good entropy early at boot before get_random_bytes is available. 4 Add optional padding for memory hotplug compatibility.

Ran an automated test to ensure that (1) holds true across several iterations of automated reboot testing. 2, 3 and 4 are not impacted by this patch.

Manual Testing on a system where the problem reproduces

1. With KASLR

     Hotplug memory [0x240000000000-0x242000000000] exceeds maximum addressable range [0x0-0xaffffffffff]
     ------------[ cut here ]------------

2. With the fixes

added peer-to-peer DMA memory 0x240000000000-0x241fffffffff

KASLR is still enabled as seen by kaslr_offset() (difference between __START_KERNEL and _stext)
3. Without the fixes and KASLR disabled

added peer-to-peer DMA memory 0x240000000000-0x241fffffffff

KASLR is disabled, kaslr_offset() is 0.

On my system with 46 bits physical address and 4 level page tables (no LA57), the remaining entropy dropped from 49 TiB to 20 TiB

 arch/x86/mm/kaslr.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/mm/kaslr.c +8 -2
index 11a93542d198..3c306de52fd4 100644
@@ -113,8 +113,14 @@ void __init kernel_randomize_memory(void)
 	memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
 		CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
 
-	/* Adapt physical memory region size based on available memory */
-	if (memory_tb < kaslr_regions[0].size_tb)
+	/*
+	 * Adapt physical memory region size based on available memory,
+	 * except when CONFIG_PCI_P2PDMA is enabled. P2PDMA exposes the
+	 * device BAR space assuming the direct map space is large enough
+	 * for creating a ZONE_DEVICE mapping in the direct map corresponding
+	 * to the physical BAR address.
+	 */
+	if (!IS_ENABLED(CONFIG_PCI_P2PDMA) && (memory_tb < kaslr_regions[0].size_tb))
 		kaslr_regions[0].size_tb = memory_tb;
 
 	/*
Signature · 1 line
2.48.0
maintainer
#1 lore raw
Quoted 10 lines · Balbir Singh

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Quoted 4 lines

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

I'm thinking this CONFIG is going to be on by default for pretty much all distro kernels? As such, does it make sense to have this depend on this config symbol?

Also +Kees

Quoted 77 lines
Cc Dave Hansen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

---

Testing:

commit 0483e1fa6e09d ("x86/mm: Implement ASLR for kernel memory regions") mentions the problems that the following problems need to be addressed.

1 The three target memory sections are never at the same place between
boots.
2 The physical memory mapping can use a virtual address not aligned on
the PGD page table.
3 Have good entropy early at boot before get_random_bytes is available. 4 Add optional padding for memory hotplug compatibility.

Ran an automated test to ensure that (1) holds true across several iterations of automated reboot testing. 2, 3 and 4 are not impacted by this patch.

Manual Testing on a system where the problem reproduces

1. With KASLR

     Hotplug memory [0x240000000000-0x242000000000] exceeds maximum addressable range [0x0-0xaffffffffff]
     ------------[ cut here ]------------

2. With the fixes

added peer-to-peer DMA memory 0x240000000000-0x241fffffffff

KASLR is still enabled as seen by kaslr_offset() (difference between __START_KERNEL and _stext)
3. Without the fixes and KASLR disabled

added peer-to-peer DMA memory 0x240000000000-0x241fffffffff

KASLR is disabled, kaslr_offset() is 0.

On my system with 46 bits physical address and 4 level page tables (no LA57), the remaining entropy dropped from 49 TiB to 20 TiB

 arch/x86/mm/kaslr.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/mm/kaslr.c +8 -2
index 11a93542d198..3c306de52fd4 100644
@@ -113,8 +113,14 @@ void __init kernel_randomize_memory(void)
 	memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
 		CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
 
-	/* Adapt physical memory region size based on available memory */
-	if (memory_tb < kaslr_regions[0].size_tb)
+	/*
+	 * Adapt physical memory region size based on available memory,
+	 * except when CONFIG_PCI_P2PDMA is enabled. P2PDMA exposes the
+	 * device BAR space assuming the direct map space is large enough
+	 * for creating a ZONE_DEVICE mapping in the direct map corresponding
+	 * to the physical BAR address.
+	 */
+	if (!IS_ENABLED(CONFIG_PCI_P2PDMA) && (memory_tb < kaslr_regions[0].size_tb))
 		kaslr_regions[0].size_tb = memory_tb;
 
 	/*
Signature · 1 line
2.48.0
#2 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Balbir Singh

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Quoted 4 lines
Quoted 4 lines

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

So, just to restate my understanding: this is about only the direct map (i.e. kaslr_region[0]). The notes (which I think should be left in the commit log) say that the entropy dropped from 49 TiB (46 bits) to 20 TiB (45 bits). If I'm reading right, the offset granularity is in PUD_SIZE (30 bits) steps, so the entropy is going from 16 bits to 15 bits. I don't see any general problem with that. Especially if the alternative is 0 bits of entropy. :)

Quoted 3 lines

I'm thinking this CONFIG is going to be on by default for pretty much all distro kernels? As such, does it make sense to have this depend on this config symbol?

If checking the CONFIG means we get back the 1 bit of entropy, I'd say keep the check. Some folks will want that bit over P2PDMA.

Quoted 1 line

Also +Kees

Thanks for CCing me! :)

-Kees

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 17 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Balbir Singh

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Quoted 4 lines
Quoted 4 lines

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

Quoted 8 lines

So, just to restate my understanding: this is about only the direct map (i.e. kaslr_region[0]). The notes (which I think should be left in the commit log) say that the entropy dropped from 49 TiB (46 bits) to 20 TiB (45 bits). If I'm reading right, the offset granularity is in PUD_SIZE (30 bits) steps, so the entropy is going from 16 bits to 15 bits. I don't see any general problem with that. Especially if the alternative is 0 bits of entropy. :)

Yes, this is about the direct map (kaslr_region[0]) and the data is from my system which has 46 bits of physical address. On larger systems with LA57 the drop might be higher. I am happy to repost the patch with my testing notes in the commit log, if you think it's useful to have in the commit log.

Quoted 11 lines
Quoted 3 lines

I'm thinking this CONFIG is going to be on by default for pretty much all distro kernels? As such, does it make sense to have this depend on this config symbol?

If checking the CONFIG means we get back the 1 bit of entropy, I'd say keep the check. Some folks will want that bit over P2PDMA.

Quoted 1 line

Also +Kees

Thanks for CCing me! :)

Thanks for review!
Balbir Singh

#4 lore raw
Quoted 27 lines · Balbir Singh
Quoted 17 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Balbir Singh

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Quoted 4 lines
Quoted 4 lines

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

Quoted 8 lines

So, just to restate my understanding: this is about only the direct map (i.e. kaslr_region[0]). The notes (which I think should be left in the commit log) say that the entropy dropped from 49 TiB (46 bits) to 20 TiB (45 bits). If I'm reading right, the offset granularity is in PUD_SIZE (30 bits) steps, so the entropy is going from 16 bits to 15 bits. I don't see any general problem with that. Especially if the alternative is 0 bits of entropy. :)

Quoted 4 lines

Yes, this is about the direct map (kaslr_region[0]) and the data is from my system which has 46 bits of physical address. On larger systems with LA57 the drop might be higher. I am happy to repost the patch with my testing notes in the commit log, if you think it's useful to have in the commit log.

Actually, it might be more useful to note it in the CONFIG help text? "This may reduce direct map ASLR entropy by 1 bit with 46 physical bits, X bits with YY physical bits, [etc...]"

-Kees

Signature · 1 line
Kees Cook
#5 lore raw
Quoted 28 lines · Kees Cook
Quoted 27 lines · Balbir Singh
Quoted 25 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Balbir Singh

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Quoted 4 lines
Quoted 4 lines

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

So, just to restate my understanding: this is about only the direct map (i.e. kaslr_region[0]). The notes (which I think should be left in the commit log) say that the entropy dropped from 49 TiB (46 bits) to 20 TiB (45 bits). If I'm reading right, the offset granularity is in PUD_SIZE (30 bits) steps, so the entropy is going from 16 bits to 15 bits. I don't see any general problem with that. Especially if the alternative is 0 bits of entropy. :)

Quoted 9 lines
Quoted 4 lines

Yes, this is about the direct map (kaslr_region[0]) and the data is from my system which has 46 bits of physical address. On larger systems with LA57 the drop might be higher. I am happy to repost the patch with my testing notes in the commit log, if you think it's useful to have in the commit log.

Actually, it might be more useful to note it in the CONFIG help text? "This may reduce direct map ASLR entropy by 1 bit with 46 physical bits, X bits with YY physical bits, [etc...]"

I worded it as follows in drivers/pci/Kconfig

Enabling this option will reduce the entropy of x86 KASLR memory regions. For example - on a 46 bit system, the entropy goes down from 16 bits to 15 bits. The actual reduction in entropy depends on the physical address bits, on processor features, kernel config (5 level page table) and physical memory present on the system.

Does that seem reasonable?

Balbir

#6 lore raw
Quoted 41 lines · Balbir Singh
Quoted 28 lines · Kees Cook
Quoted 27 lines · Balbir Singh
Quoted 25 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Balbir Singh

When CONFIG_PCI_P2PDMA is enabled, it maps the PFN's via a ZONE_DEVICE mapping using devm_memremap_pages(). The mapped virtual address range corresponds to the pci_resource_start() of the BAR address and size corresponding to the BAR length.

When KASLR is enabled, the direct map range of the kernel is reduced to the size of physical memory plus additional padding. If the BAR address is beyond this limit, PCI peer to peer DMA mappings fail.

Quoted 4 lines
Quoted 4 lines

Fix this by not shrinking the size of direct map when CONFIG_PCI_P2PDMA is enabled. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely.

So, just to restate my understanding: this is about only the direct map (i.e. kaslr_region[0]). The notes (which I think should be left in the commit log) say that the entropy dropped from 49 TiB (46 bits) to 20 TiB (45 bits). If I'm reading right, the offset granularity is in PUD_SIZE (30 bits) steps, so the entropy is going from 16 bits to 15 bits. I don't see any general problem with that. Especially if the alternative is 0 bits of entropy. :)

Quoted 9 lines
Quoted 4 lines

Yes, this is about the direct map (kaslr_region[0]) and the data is from my system which has 46 bits of physical address. On larger systems with LA57 the drop might be higher. I am happy to repost the patch with my testing notes in the commit log, if you think it's useful to have in the commit log.

Actually, it might be more useful to note it in the CONFIG help text? "This may reduce direct map ASLR entropy by 1 bit with 46 physical bits, X bits with YY physical bits, [etc...]"

I worded it as follows in drivers/pci/Kconfig

Quoted 7 lines

Enabling this option will reduce the entropy of x86 KASLR memory regions. For example - on a 46 bit system, the entropy goes down from 16 bits to 15 bits. The actual reduction in entropy depends on the physical address bits, on processor features, kernel config (5 level page table) and physical memory present on the system.

Does that seem reasonable?

Yeah, I like it. Thanks!

Signature · 1 line
Kees Cook