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) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-09 15:32:05 +02:00
1 parent d7a577044f
commit 1d18d182e1
1 file changed
+30 -2
+30 -2
View File
@@ -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;