:: commit 6bdd5dd85278435cff322dbc07e2aecf5cbbee0d

Mintsuki <mintsuki@protonmail.com> — 2026-03-31 12:12

parents: b0569fed95

mm/pmm: Check for overflow when aligning count in ext_mem_alloc

diff --git a/common/mm/pmm.s2.c b/common/mm/pmm.s2.c
index 6cc74cb8..b1c54222 100644
--- a/common/mm/pmm.s2.c
+++ b/common/mm/pmm.s2.c
@@ -630,7 +630,10 @@ void *ext_mem_alloc_type_aligned_mode(uint64_t count, uint32_t type, size_t alig
     (void)allow_high_allocs;
 #endif
 
-    count = ALIGN_UP(count, alignment);
+    if (__builtin_add_overflow(count, alignment - 1, &count)) {
+        panic(false, "ext_mem_alloc: count overflows when aligning");
+    }
+    count = ALIGN_DOWN(count, alignment);
 
     if (allocations_disallowed)
         panic(false, "Memory allocations disallowed");
tab: 248 wrap: offon