From d2679dfe28946bc5bfd2de8b72371a5c336d9a5f Mon Sep 17 00:00:00 2001 From: Victor Gomes Date: Mon, 31 May 2021 13:16:54 +0200 Subject: [PATCH] [Backport] CVE-2021-30541: Use after free in V8 Manual backport of patch originally reviewed on https://chromium-review.googlesource.com/c/v8/v8/+/2993033: Merged: [JSON] Fix GC issue in BuildJsonObject We must ensure that the sweeper is not running or has already swept mutable_double_buffer. Otherwise the GC can add it to the free list. Change-Id: If0fc7617acdb6690f0567215b78f8728e1643ec0 No-Try: true No-Presubmit: true No-Tree-Checks: true Bug: v8:11837, chromium:1214842 Reviewed-by: Michael Lippautz Reviewed-by: Toon Verwaest Commit-Queue: Victor Gomes Cr-Commit-Position: refs/branch-heads/9.1@{#75} Cr-Branched-From: 0e4ac64a8cf298b14034a22f9fe7b085d2cb238d-refs/heads/9.1.269@{#1} Cr-Branched-From: f565e72d5ba88daae35a59d0f978643e2343e912-refs/heads/master@{#73847} Reviewed-by: Michal Klocek --- chromium/v8/src/heap/heap.cc | 4 ++++ chromium/v8/src/heap/heap.h | 2 ++ chromium/v8/src/json/json-parser.cc | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/chromium/v8/src/heap/heap.cc b/chromium/v8/src/heap/heap.cc index a93990078117..190337dc2d78 100644 --- a/chromium/v8/src/heap/heap.cc +++ b/chromium/v8/src/heap/heap.cc @@ -2222,6 +2222,10 @@ void Heap::RecomputeLimits(GarbageCollector collector) { } } +void Heap::EnsureSweepingCompleted() { + mark_compact_collector()->EnsureSweepingCompleted(); +} + void Heap::CallGCPrologueCallbacks(GCType gc_type, GCCallbackFlags flags) { RuntimeCallTimerScope runtime_timer( isolate(), RuntimeCallCounterId::kGCPrologueCallback); diff --git a/chromium/v8/src/heap/heap.h b/chromium/v8/src/heap/heap.h index b8220dad5eb0..cff57d94e822 100644 --- a/chromium/v8/src/heap/heap.h +++ b/chromium/v8/src/heap/heap.h @@ -1065,6 +1065,8 @@ class Heap { Reservation* reservations, const std::vector& large_objects, const std::vector
& maps); + void EnsureSweepingCompleted(); + IncrementalMarking* incremental_marking() { return incremental_marking_.get(); } diff --git a/chromium/v8/src/json/json-parser.cc b/chromium/v8/src/json/json-parser.cc index d099fa36cba1..75e78923a4bc 100644 --- a/chromium/v8/src/json/json-parser.cc +++ b/chromium/v8/src/json/json-parser.cc @@ -633,6 +633,11 @@ Handle JsonParser::BuildJsonObject( DCHECK_EQ(mutable_double_address, end); } #endif + // Before setting the length of mutable_double_buffer back to zero, we + // must ensure that the sweeper is not running or has already swept the + // object's page. Otherwise the GC can add the contents of + // mutable_double_buffer to the free list. + isolate()->heap()->EnsureSweepingCompleted(); mutable_double_buffer->set_length(0); } }