From 1d18d182e14df3dfa2221ef9cc0eae15d0d0a5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 9 Oct 2026 14:21:39 +0200 Subject: [PATCH] MemSlabMap: Validate slab ranges from savestates FillHeads() trusts start/end, so a bad slab wrote out of bounds of heads_. On failure, reset the map since heads_ may point to the deleted old slabs. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/Debugger/MemBlockInfo.cpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Core/Debugger/MemBlockInfo.cpp b/Core/Debugger/MemBlockInfo.cpp index bd0838e3dc..1fac8bcd00 100644 --- a/Core/Debugger/MemBlockInfo.cpp +++ b/Core/Debugger/MemBlockInfo.cpp @@ -209,13 +209,29 @@ void MemSlabMap::DoState(PointerWrap &p) { Slab *old = first_; Slab *oldBulk = bulkStorage_; Do(p, count); + // Every slab takes up at least one byte. + if (count < 1 || !p.CheckRead(count)) { + p.SetError(p.ERROR_FAILURE); + count = 1; + } + + // The slabs must cover the address space in order, or FillHeads() writes out of bounds. + bool valid = true; + auto checkSlab = [&](const Slab *slab, uint32_t expectedStart) { + if (slab->start != expectedStart || slab->end <= slab->start || slab->end > MAX_SIZE) { + valid = false; + } + return valid; + }; first_ = new Slab(); first_->DoState(p); lastFind_ = first_; --count; - FillHeads(first_); + if (checkSlab(first_, 0)) { + FillHeads(first_); + } bulkStorage_ = new Slab[count]; @@ -228,7 +244,12 @@ void MemSlabMap::DoState(PointerWrap &p) { slab->next->prev = slab; slab = slab->next; - FillHeads(slab); + if (checkSlab(slab, slab->prev->end)) { + FillHeads(slab); + } + } + if (slab->end != MAX_SIZE) { + valid = false; } // Now that it's entirely disconnected, delete the old slabs. @@ -239,6 +260,13 @@ void MemSlabMap::DoState(PointerWrap &p) { old = next; } delete [] oldBulk; + + if (!valid || p.Failed()) { + ERROR_LOG(Log::SaveState, "Savestate loading error: invalid MemSlabMap"); + p.SetError(p.ERROR_FAILURE); + // heads_ may still point to the slabs deleted above. + Reset(); + } } else { for (Slab *slab = first_; slab != nullptr; slab = slab->next) ++count;