Skip to content

Commit a67a6ed

Browse files
labbottglikely
authored andcommitted
of: Check for phys_addr_t overflows in early_init_dt_add_memory_arch
The common early_init_dt_add_memory_arch takes the base and size of a memory region as u64 types. The function never checks if the base and size can actually fit in a phys_addr_t which may be smaller than 64-bits. This may result in incorrect memory being passed to memblock_add if the memory falls outside the range of phys_addr_t. Add range checks for the base and size if phys_addr_t is smaller than u64. Reported-by: Geert Uytterhoeven <[email protected]> Tested-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Laura Abbott <[email protected]> Acked-by: Nicolas Pitre <[email protected]> Signed-off-by: Grant Likely <[email protected]>
1 parent a497c3b commit a67a6ed

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

drivers/of/fdt.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,21 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
880880
const u64 phys_offset = __pa(PAGE_OFFSET);
881881
base &= PAGE_MASK;
882882
size &= PAGE_MASK;
883+
884+
if (sizeof(phys_addr_t) < sizeof(u64)) {
885+
if (base > ULONG_MAX) {
886+
pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
887+
base, base + size);
888+
return;
889+
}
890+
891+
if (base + size > ULONG_MAX) {
892+
pr_warning("Ignoring memory range 0x%lx - 0x%llx\n",
893+
ULONG_MAX, base + size);
894+
size = ULONG_MAX - base;
895+
}
896+
}
897+
883898
if (base + size < phys_offset) {
884899
pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
885900
base, base + size);

0 commit comments

Comments
 (0)