powerpc/kaslr_booke: Fix reserved region overlap checks
Subsystems (2)
PATCH 1/2 powerpc/kaslr_booke: Fix reserved region overlap checks
FDT reserved region addresses can be 64-bit, but regions_overlap() takes 32-bit arguments, which could truncate values before comparing them and cause KASLR to incorrectly detect or miss overlaps. Make regions_overlap() work with 64-bit values instead.
Also clamp reservation sizes before computing their end addresses to prevent the addition from overflowing.
--- arch/powerpc/mm/nohash/kaslr_booke.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/powerpc/mm/nohash/kaslr_booke.c +6 -6
@@ -91,7 +91,7 @@ static __init u64 get_kaslr_seed(void *fdt)return ret; } -static __init bool regions_overlap (u32 s1,u32 e1,u32 s2,u32 e2) +static __init bool regions_overlap (u64 s1,u64 e1,u64 s2,u64 e2) {return e1 >= s2 && e2 >= s1; }
@@ -100,13 +100,15 @@ static __init bool overlaps_reserved_region(const void *fdt, u32 start, u32 end) {int subnode, len, i; -u64 base, size; +u64 base, size, rsv_end;/* check for overlap with /memreserve/ entries */ for (i =0 ; i <fdt_num_mem_rsv (fdt); i++) {if (fdt_get_mem_rsv (fdt, i, &base, &size) <0 )continue ; -if (regions_overlap (start, end, base, base + size)) + + rsv_end = base +min (size,U64_MAX - base); +if (regions_overlap (start, end, base, rsv_end))return true; }
@@ -118,7 +120,6 @@ static __init bool overlaps_reserved_region(const void *fdt, u32 start, subnode >=0 ; subnode = fdt_next_subnode(fdt, subnode)) {const fdt32_t *reg; -u64 rsv_end; len =0 ; reg =fdt_getprop (fdt, subnode,"reg" , &len);
@@ -141,8 +142,7 @@ static __init bool overlaps_reserved_region(const void *fdt, u32 start,if (base >= regions.pa_end)continue ; - rsv_end =min (base + size, (u64 )U32_MAX ); - + rsv_end = base +min (size,U64_MAX - base);if (regions_overlap (start, end, base, rsv_end))return true; }
PATCH 2/2 powerpc/kaslr_booke: Fix reserved-memory reg property length check
In overlaps_reserved_region(), fdt_getprop() returns the reg property length in bytes, which the loop condition compares against a cell count. Since each cell is 4 bytes, scale the count to bytes before comparing it with len to avoid reading past the end of a truncated reg property.
--- arch/powerpc/mm/nohash/kaslr_booke.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/mm/nohash/kaslr_booke.c +2 -2
@@ -123,8 +123,8 @@ static __init bool overlaps_reserved_region(const void *fdt, u32 start, len =0 ; reg =fdt_getprop (fdt, subnode,"reg" , &len); -while (len >= (regions.reserved_mem_addr_cells + - regions.reserved_mem_size_cells)) { +while (len >=4 * (regions.reserved_mem_addr_cells + + regions.reserved_mem_size_cells)) { base =fdt32_to_cpu (reg[0 ]);if (regions.reserved_mem_addr_cells ==2 ) base = (base <<32 ) |fdt32_to_cpu (reg[1 ]);