of: Check for phys_addr_t overflows in early_init_dt_add_memory_arch
PATCH v2 1/1 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.
---
v2: Switched to sizeof(phys_addr_t) < sizeof(u64) instead of macros. Sending to right e-mails/mailing lists this time.
drivers/of/fdt.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/of/fdt.c +15 -0
@@ -880,6 +880,21 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)const u64 phys_offset =__pa (PAGE_OFFSET ); base &=PAGE_MASK ; size &=PAGE_MASK ; + +if (sizeof (phys_addr_t) <sizeof (u64)) { +if (base >ULONG_MAX ) { +pr_warning ("Ignoring memory block 0x%llx - 0x%llx\n" , + base, base + size); +return ; + } + +if (base + size >ULONG_MAX ) { +pr_warning ("Ignoring memory range 0x%lx - 0x%llx\n" , +ULONG_MAX , base + size); + size =ULONG_MAX - base; + } + } +if (base + size < phys_offset) {pr_warning ("Ignoring memory block 0x%llx - 0x%llx\n" , base, base + size);
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
Quoted 11 lines · Laura Abbott
The common
early_init_dt_add_memory_archtakes the base and size of a memory region as u64 types. The function never checks if the base and size can actually fit in aphys_addr_twhich may be smaller than 64-bits. This may result in incorrect memory being passed tomemblock_addif the memory falls outside the range ofphys_addr_t. Add range checks for the base and size ifphys_addr_tis smaller than u64.Reported-by Geert Uytterhoeven <[email protected]>Tested-by Geert Uytterhoeven <[email protected]>Signed-off-by Laura Abbott <[email protected]>
Quoted 37 lines
---
v2: Switched tosizeof(phys_addr_t)< sizeof(u64) instead of macros. Sending to right e-mails/mailing lists this time.drivers/of/fdt.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/of/fdt.c +15
-0@@ -880,6 +880,21 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)const u64 phys_offset =__pa (PAGE_OFFSET ); base &=PAGE_MASK ; size &=PAGE_MASK ; + +if (sizeof (phys_addr_t) <sizeof (u64)) { +if (base >ULONG_MAX ) { +pr_warning ("Ignoring memory block 0x%llx - 0x%llx\n" , + base, base + size); +return ; + } + +if (base + size >ULONG_MAX ) { +pr_warning ("Ignoring memory range 0x%lx - 0x%llx\n" , +ULONG_MAX , base + size); + size =ULONG_MAX - base; + } + } +if (base + size < phys_offset) {pr_warning ("Ignoring memory block 0x%llx - 0x%llx\n" , base, base + size);Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
Grant Likely
AppliedQuoted 10 lines · Nicolas Pitre
Quoted 8 lines · Laura Abbott
The common
early_init_dt_add_memory_archtakes the base and size of a memory region as u64 types. The function never checks if the base and size can actually fit in aphys_addr_twhich may be smaller than 64-bits. This may result in incorrect memory being passed tomemblock_addif the memory falls outside the range ofphys_addr_t. Add range checks for the base and size ifphys_addr_tis smaller than u64.
Quoted 5 lines
Quoted 3 lines
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]>
Applied, thanks.
g.
Quoted 38 lines
Quoted 38 lines
---
v2: Switched tosizeof(phys_addr_t)< sizeof(u64) instead of macros. Sending to right e-mails/mailing lists this time.drivers/of/fdt.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/of/fdt.c +15
-0@@ -880,6 +880,21 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)const u64 phys_offset =__pa (PAGE_OFFSET ); base &=PAGE_MASK ; size &=PAGE_MASK ; + +if (sizeof (phys_addr_t) <sizeof (u64)) { +if (base >ULONG_MAX ) { +pr_warning ("Ignoring memory block 0x%llx - 0x%llx\n" , + base, base + size); +return ; + } + +if (base + size >ULONG_MAX ) { +pr_warning ("Ignoring memory range 0x%lx - 0x%llx\n" , +ULONG_MAX , base + size); + size =ULONG_MAX - base; + } + } +if (base + size < phys_offset) {pr_warning ("Ignoring memory block 0x%llx - 0x%llx\n" , base, base + size);Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation