Skip to content

Commit 8fe4845

Browse files
committed
x86/kaslr: Reduce KASLR entropy on most x86 systems
JIRA: https://issues.redhat.com/browse/RHEL-110657 This patch is a backport of the following upstream commit: commit 7ffb791 Author: Balbir Singh <balbirs@nvidia.com> Date: Fri Feb 7 10:42:34 2025 +1100 x86/kaslr: Reduce KASLR entropy on most x86 systems When CONFIG_PCI_P2PDMA=y (which is basically enabled on all large x86 distros), 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 the direct map when CONFIG_PCI_P2PDMA=y. This reduces the total available entropy, but it's better than the current work around of having to disable KASLR completely. [ mingo: Clarified the changelog to point out the broad impact ... ] Signed-off-by: Balbir Singh <balbirs@nvidia.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Kees Cook <kees@kernel.org> Acked-by: Bjorn Helgaas <bhelgaas@google.com> # drivers/pci/Kconfig Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Andy Lutomirski <luto@kernel.org> Link: https://lore.kernel.org/lkml/20250206023201.1481957-1-balbirs@nvidia.com/ Link: https://lore.kernel.org/r/20250206234234.1912585-1-balbirs@nvidia.com -- Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>
1 parent 37e4d54 commit 8fe4845

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

arch/x86/mm/kaslr.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,14 @@ void __init kernel_randomize_memory(void)
113113
memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
114114
CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
115115

116-
/* Adapt physical memory region size based on available memory */
117-
if (memory_tb < kaslr_regions[0].size_tb)
116+
/*
117+
* Adapt physical memory region size based on available memory,
118+
* except when CONFIG_PCI_P2PDMA is enabled. P2PDMA exposes the
119+
* device BAR space assuming the direct map space is large enough
120+
* for creating a ZONE_DEVICE mapping in the direct map corresponding
121+
* to the physical BAR address.
122+
*/
123+
if (!IS_ENABLED(CONFIG_PCI_P2PDMA) && (memory_tb < kaslr_regions[0].size_tb))
118124
kaslr_regions[0].size_tb = memory_tb;
119125

120126
/*

drivers/pci/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ config PCI_P2PDMA
206206
P2P DMA transactions must be between devices behind the same root
207207
port.
208208

209+
Enabling this option will reduce the entropy of x86 KASLR memory
210+
regions. For example - on a 46 bit system, the entropy goes down
211+
from 16 bits to 15 bits. The actual reduction in entropy depends
212+
on the physical address bits, on processor features, kernel config
213+
(5 level page table) and physical memory present on the system.
214+
209215
If unsure, say N.
210216

211217
config PCI_LABEL

0 commit comments

Comments
 (0)