Calling Compact on a nsTArray will lead to use-after-free error in JS_GC because of the reallocation
A test harness for garbage-collection post-barriers passes a pointer to an array element into the GC tracer, and calling Compact on the array reallocates its storage so the tracer later reads a stale address. The result is a use-after-free read during JS_GC, demonstrated under AddressSanitizer.
Originally disclosed on Mozilla
- Fixed in 13 days
- Paid in 18 days
- Disclosed after 13 days
Created attachment 9180814 test_and_output.zip
Version
commit 62c443a7c801ba9672de34c2867ec1665a4bbe67
parent b2dee52fd392f7130bc3858a70c683faa773aa8f
hg 48f10c4139655c2c4d52fa773f1fd760ea681d42
git 62c443a7c801ba9672de34c2867ec1665a4bbe67
author Gijs Kruitbosch <[address removed]>
committer Gijs Kruitbosch <[address removed]>
commit time Fri, 11 Sep 2020 15:05:50 +0000
Steps to reproduce
- Enable address sanitizer in mozconfig.
- In TestGCPostBarriers.cpp,
add array->Compact() after line 58 and before line 64
- run the gtest with
./mach gtest GCPostBarriers.nsTArray
More details
The key intuition is that the added Compact will cause the array to reallocate
and trying to access array items using an old address will lead to use-after-free error.
We came across this when we were working on something similar to a ChaosMode feature where we wanted to make array reallocation happen more often.
We have attached a test file with some print statements and the output file.
Our output file shows that the error happens when RunTest is first called with a nsTArray at line 96.
Our output file also shows that reallocation happened several times in the loop line 52-58,
which indicates that the extra reallocation triggered by Compact should not
affect later code.
Before being populated, the array was passed to JS_AddExtraGCRootsTracer
together with TraceArray at line 44.
In TraceArray, we found an unsafe use of addresses of array items.
template <class ArrayT>
static void TraceArray(JSTracer* trc, void* data) {
ArrayT* array = static_cast<ArrayT*>(data);
for (unsigned i = 0; i < array->Length(); ++i)
JS::TraceEdge(trc, &array->ElementAt(i), "array-element");
}
Here, &array->ElementAt(i) will become an invalid address if the array gets reallocated.
We added a print statement in TraceArray but didn't see it in the output file.
This could mean that there are other similarly unsafe code in JS_GC.
We also dumped all the array addresses and saw that
the UAF read is accessing the address right before the last reallocation from Compact.
The UAF address is the last element in the array but we don't know if the last one
is different from other elements.
We think there might be unsafe use of array addresses in the GC code or
the way Compact reallocates array is problematic.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the tracer takes the address of an array element and holds it across a reallocation triggered by Compact, so the stored address becomes stale.
Impact
A use-after-free read occurs during garbage collection in this test scenario; the report does not show a way to trigger it in a shipped browser or what an attacker could gain.
What the attacker needed
Build with address sanitizer; modify the gtest source; run the gtest locally
How it was found
- Enable the address sanitizer in the build configuration.
- Add a Compact call on the array in the GC post-barrier test between the existing lines.
- Run the GCPostBarriers.nsTArray gtest and observe the sanitizer error.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
An array is a list stored in one block of memory. When the list grows or is compacted, the program may move it to a new block and free the old one. Here a saved pointer into the old block was used after the move, so the program read memory that no longer belonged to it, which is called a use-after-free.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.