Skip to content

Commit ac7385a

Browse files
yadan-fangregkh
authored andcommitted
MIPS: mm: Fix out-of-bounds write in maar_res_walk()
[ Upstream commit 1b001b1 ] maar_res_walk() uses wi->num_cfg as the index into the fixed-size wi->cfg array, but checks whether the array is full only after it has filled the selected entry. If walk_system_ram_range() reports more than 16 memory ranges, the overflow call writes one struct maar_config past the end of the array before WARN_ON() prevents num_cfg from advancing. Move the full-array check before taking the array slot and return non-zero when the scratch array is full, so walk_system_ram_range() terminates the walk instead of invoking the callback for further ranges. Fixes: a5718fe ("MIPS: mm: Drop boot_mem_map") Signed-off-by: Yadan Fan <ydfan@suse.com> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c05a0ec commit ac7385a

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

arch/mips/mm/init.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,15 @@ static int maar_res_walk(unsigned long start_pfn, unsigned long nr_pages,
281281
void *data)
282282
{
283283
struct maar_walk_info *wi = data;
284-
struct maar_config *cfg = &wi->cfg[wi->num_cfg];
284+
struct maar_config *cfg;
285285
unsigned int maar_align;
286286

287+
/* Ensure we don't overflow the cfg array */
288+
if (WARN_ON(wi->num_cfg >= ARRAY_SIZE(wi->cfg)))
289+
return -1;
290+
291+
cfg = &wi->cfg[wi->num_cfg];
292+
287293
/* MAAR registers hold physical addresses right shifted by 4 bits */
288294
maar_align = BIT(MIPS_MAAR_ADDR_SHIFT + 4);
289295

@@ -292,9 +298,7 @@ static int maar_res_walk(unsigned long start_pfn, unsigned long nr_pages,
292298
cfg->upper = ALIGN_DOWN(PFN_PHYS(start_pfn + nr_pages), maar_align) - 1;
293299
cfg->attrs = MIPS_MAAR_S;
294300

295-
/* Ensure we don't overflow the cfg array */
296-
if (!WARN_ON(wi->num_cfg >= ARRAY_SIZE(wi->cfg)))
297-
wi->num_cfg++;
301+
wi->num_cfg++;
298302

299303
return 0;
300304
}

0 commit comments

Comments
 (0)