Type Confusion in V8 (CVE-2024-5830)
A type confusion in V8's object cloning allows an attacker to overwrite internal properties of a dictionary, leading to out-of-bounds access. This can be used to leak the hole object or cause a crash.
Originally disclosed on Chrome VRP
- Fix shipped in 19 days
VULNERABILITY DETAILS
When cloning an object using FastAssign [1], if the source object has an accessor, it'll be called to get the property value that is then used in the call of CreateDataProperty [2]. In particular, the accessor can cause the map of target to become deprecated:
var x = {};
x.a0 = 1;
var x1 = {};
x1.a0 = 1;
//Creates a transition from map {a0} to {a0, prop}
x1.prop = 1;
x.__defineGetter__("prop", function() {
let obj = {};
obj.a0 = 1.5; //<------ map of x and x1 are now deprecated
return 1;
});
x.z = 1;
delete x.z;
//Cloning calls accessor `prop` of x before calling `CreateDataProperty` to create property `prop` on target
var y = {...x};
CreateDataProperty then uses TryFastAddDataProperty to add the property prop to the target object:
bool TryFastAddDataProperty(Isolate* isolate, Handle<JSObject> object,
Handle<Name> name, Handle<Object> value,
PropertyAttributes attributes) {
Tagged<Map> map =
TransitionsAccessor(isolate, object->map())
.SearchTransition(*name, PropertyKind::kData, attributes);
if (map.is_null()) return false;
...
new_map = Map::PrepareForDataProperty(isolate, new_map, descriptor,
PropertyConstness::kConst, value);
...
object->WriteToField(descriptor,
new_map->instance_descriptors()->GetDetails(descriptor),
*value);
return true;
}
TryFastAddDataProperty first search for a transition map to property prop. This will find the map of x1, which is also deprecated. The deprecated map is then passed to Map::PrepareForDataProperty, which will try to update it:
Handle<Map> Map::PrepareForDataProperty(Isolate* isolate, Handle<Map> map,
InternalIndex descriptor,
PropertyConstness constness,
Handle<Object> value) {
// Update to the newest map before storing the property.
map = Update(isolate, map);
// Dictionaries can store any property value.
DCHECK(!map->is_dictionary_map());
return UpdateDescriptorForValue(isolate, map, descriptor, constness, value);
However, as pointed out in bug 40062884 (https://issues.chromium.org/issues/40062884), updating a deprecated map can cause it to become a dictionary map. As the call WriteToField [3] in TryFastAddDataProperty still assumes new_map is a fast map, it'll use FastPropertyAtPut to write the property:
void JSObject::WriteToField(InternalIndex descriptor, PropertyDetails details,
Tagged<Object> value) {
...
FieldIndex index = FieldIndex::ForDetails(map(), details);
if (details.representation().IsDouble()) {
...
} else {
FastPropertyAtPut(index, value);
}
}
When new_map becomes a dictionary map due to update, this will cause a confusion between PropertyArray and NameDictionary and can cause internal properties of the NameDictionary to be overwritten, which can then be used to cause OOB access from the NameDictionary.
- https://source.chromium.org/chromium/chromium/src/+/43a0264b838298d9cb369562af2cf419a2b62987:v8/src/objects/js-objects.cc;l=277
- https://source.chromium.org/chromium/chromium/src/+/43a0264b838298d9cb369562af2cf419a2b62987:v8/src/objects/js-objects.cc;l=394
- https://source.chromium.org/chromium/chromium/src/+/43a0264b838298d9cb369562af2cf419a2b62987:v8/src/objects/js-objects.cc;l=3556
The bug affects stable version from 125.0.6422.60 onwards and is introduced in this commit: https://source.chromium.org/chromium/_/chromium/v8/v8/+/f6b1dd8ec7cad9f9794b5176be1bed7e06584015
Thank you very much for your help and please let me know if there is anything I can help.
VERSION
d8 commit 1ecc8c6 (head) and 12.5.277.8 (stable) OS: Ubuntu 22.04 LTS
REPRODUCTION CASE
Please find attached two test cases, crash.js and leak_hole.js. The test case crash.js will just crash with an OOB write, and leak_hole.js will use the OOB access to leak the hole object. Both run on standalone d8. The leak_hole.js test case uses --allow-natives-syntax to print out the hole object that is leaked.
CREDIT INFORMATION
Reporter credit: Man Yue Mo of GitHub Security Lab
Context
V8 is Chrome's JavaScript engine, and the object spread syntax ({...x}) is implemented there through a fast path called FastAssign. When the source object has an accessor for a property, cloning calls that accessor to obtain the value, and the value is then written onto the target with CreateDataProperty. This report, filed through the Chrome VRP by Man Yue Mo of GitHub Security Lab, shows that an accessor can change the shape of the target object mid-clone, leaving the fast path operating on a map that is no longer a fast map. The result is a type confusion between a PropertyArray and a NameDictionary that can overwrite internal properties of the NameDictionary and cause out-of-bounds access from it. The issue is tracked as CVE-2024-5830 and was disclosed on 11 June 2024.
Setup
The researcher reproduced the bug on standalone d8, the V8 shell, using d8 commit 1ecc8c6 (head) and V8 12.5.277.8 (stable) on Ubuntu 22.04 LTS. The bug affects stable Chrome from 125.0.6422.60 onwards and was introduced in V8 commit [redacted]. Two test cases were attached: crash.js, which crashes with an out-of-bounds write, and leak_hole.js, which uses the out-of-bounds access to leak the hole object. The leak test needs --allow-natives-syntax so it can print the leaked hole object.
Finding it
The starting observation is that FastAssign calls the source's accessor before it calls CreateDataProperty on the target. That ordering gives attacker-controlled JavaScript a window in which it can run arbitrary code, including code that changes the map of the target object. In particular, the accessor can cause the target's map to become deprecated.
The researcher's proof of concept builds two objects with the same initial shape and then deprecates both maps from inside the getter:
var x = {};
x.a0 = 1;
var x1 = {};
x1.a0 = 1;
//Creates a transition from map {a0} to {a0, prop}
x1.prop = 1;
x.__defineGetter__("prop", function() {
let obj = {};
obj.a0 = 1.5; //<------ map of x and x1 are now deprecated
return 1;
});
x.z = 1;
delete x.z;
//Cloning calls accessor `prop` of x before calling `CreateDataProperty` to create property `prop` on target
var y = {...x};
x1.prop = 1 creates a transition from the map {a0} to the map {a0, prop}, so a transition for the name prop exists. The getter for prop on x creates a fresh object with a0 set to a double (1.5), which deprecates the maps of both x and x1. The x.z = 1; delete x.z; pair leaves x in a state where the clone proceeds through the fast path. When {...x} runs, the accessor prop is invoked before CreateDataProperty creates prop on the target.
Exploitation
Step 1: the fast path searches for a transition
CreateDataProperty uses TryFastAddDataProperty to add prop to the target:
bool TryFastAddDataProperty(Isolate* isolate, Handle<JSObject> object,
Handle<Name> name, Handle<Object> value,
PropertyAttributes attributes) {
Tagged<Map> map =
TransitionsAccessor(isolate, object->map())
.SearchTransition(*name, PropertyKind::kData, attributes);
if (map.is_null()) return false;
...
new_map = Map::PrepareForDataProperty(isolate, new_map, descriptor,
PropertyConstness::kConst, value);
...
object->WriteToField(descriptor,
new_map->instance_descriptors()->GetDetails(descriptor),
*value);
return true;
}
The transition search for prop finds the map of x1, which is also deprecated because of the getter. That deprecated map is passed on to Map::PrepareForDataProperty.
Step 2: the deprecated map is updated
Map::PrepareForDataProperty tries to bring the map up to date:
Handle<Map> Map::PrepareForDataProperty(Isolate* isolate, Handle<Map> map,
InternalIndex descriptor,
PropertyConstness constness,
Handle<Object> value) {
// Update to the newest map before storing the property.
map = Update(isolate, map);
// Dictionaries can store any property value.
DCHECK(!map->is_dictionary_map());
return UpdateDescriptorForValue(isolate, map, descriptor, constness, value);
Updating a deprecated map can cause it to become a dictionary map. The researcher points to bug 40062884 as the place where this behaviour was already noted. The DCHECK that the map is not a dictionary map only fires in debug builds, so in release builds execution continues.
Step 3: the write assumes a fast map
Back in TryFastAddDataProperty, the call to WriteToField still assumes that new_map is a fast map:
void JSObject::WriteToField(InternalIndex descriptor, PropertyDetails details,
Tagged<Object> value) {
...
FieldIndex index = FieldIndex::ForDetails(map(), details);
if (details.representation().IsDouble()) {
...
} else {
FastPropertyAtPut(index, value);
}
}
Because new_map has become a dictionary map, the property storage is a NameDictionary rather than a PropertyArray, but FastPropertyAtPut writes as if it were the latter. This confuses the two layouts and can overwrite internal properties of the NameDictionary, which can then be used to cause out-of-bounds access from the NameDictionary.
Step 4: crash and leak
The two attached test cases exercise the two outcomes. crash.js crashes with an out-of-bounds write. leak_hole.js uses the out-of-bounds access to leak the hole object, and with --allow-natives-syntax it prints the leaked hole object.
Root cause
The mistake is a stale assumption in the fast path. TryFastAddDataProperty searches for a transition map and hands it to Map::PrepareForDataProperty, which may update a deprecated map into a dictionary map. The subsequent WriteToField does not re-check the kind of map it received and uses FastPropertyAtPut, which is only valid for fast properties backed by a PropertyArray. When the map is a dictionary map, the same offset addresses fields of a NameDictionary, so the write corrupts dictionary internals. The only guard shown in the code is a DCHECK, which is compiled out of release builds, so it does not stop the confusion in shipping Chrome.
Impact
The report describes an out-of-bounds write and an out-of-bounds access reachable from JavaScript through object cloning with an accessor. The write can overwrite internal properties of a NameDictionary, and the read can leak the hole object. The researcher does not spell out a full exploit chain beyond these primitives. The affected range is stable Chrome from 125.0.6422.60 onwards.
Fix
The report does not describe the patch. The report does not describe the patch.
Variants to test
- A positive result is a write that lands in a
NameDictionaryafter the map was deprecated during the operation. - A positive result is a crash or a leaked
holeobject underd8. - A positive result is a corrupted dictionary or an out-of-bounds access.
What happened, in plain words
Chrome runs the JavaScript code on web pages using a component called V8. V8 is the JavaScript engine: the part of the browser that reads the code a page contains and actually carries out what it says. When a page writes something like {...x}, which means "make a copy of the object x", V8 has a shortcut for doing it quickly. This shortcut is called FastAssign.
A shortcut is only safe if the situation stays exactly as it was when the shortcut was chosen. This report shows a way to change the situation halfway through the shortcut, so that V8 ends up writing data into the wrong kind of container. That is called a type confusion: the program treats one kind of thing as if it were another kind. The result can be a write outside the intended memory area and an out-of-bounds access, which are the raw ingredients for serious attacks. The issue is tracked as CVE-2024-5830, was reported through the Chrome VRP (Chrome's Vulnerability Reward Program, which pays people for finding and reporting security bugs) by Man Yue Mo of GitHub Security Lab, and was disclosed on 11 June 2024.
Context
V8 is Chrome's JavaScript engine. The object spread syntax ({...x}) is implemented there through a fast path called FastAssign. A "fast path" is a shortcut in the code that handles the common, simple case quickly, instead of going through the general, slower route.
When the source object has an accessor for a property, cloning calls that accessor to obtain the value, and the value is then written onto the target with CreateDataProperty. An accessor is a property whose value is not stored directly but produced by a small function whenever someone asks for it. A getter is the kind of accessor that runs when a property is read. So when you copy an object, V8 asks the source object for each property value, and if a property is served by a getter, that getter's code runs.
This report shows that an accessor can change the shape of the target object mid-clone, leaving the fast path operating on a map that is no longer a fast map. A "map" here is not a street map: it is V8's internal description of an object's shape, meaning which properties it has, in which order, and how each value is stored. The result is a type confusion between a PropertyArray and a NameDictionary that can overwrite internal properties of the NameDictionary and cause out-of-bounds access from it. A PropertyArray and a NameDictionary are two different ways V8 can store an object's properties; they have different internal layouts, so code that assumes one and gets the other will read and write the wrong places. "Out-of-bounds" means accessing memory outside the area that was meant to be used.
Setup
The researcher reproduced the bug on standalone d8, the V8 shell. d8 is a small command-line program that runs JavaScript using V8 alone, without the rest of Chrome, which makes it convenient for testing engine behaviour. The tests used d8 commit 1ecc8c6 (head) and V8 12.5.277.8 (stable) on Ubuntu 22.04 LTS. A commit is a saved snapshot of the source code; "head" means the newest development version at the time.
The bug affects stable Chrome from 125.0.6422.60 onwards and was introduced in V8 commit [redacted]. Two test cases were attached: crash.js, which crashes with an out-of-bounds write, and leak_hole.js, which uses the out-of-bounds access to leak the hole object. The hole is a special internal marker V8 uses to mean "no value here"; leaking it means making that internal marker visible to JavaScript code, which should not normally happen. The leak test needs --allow-natives-syntax so it can print the leaked hole object. That flag is a switch that lets a script use special debugging functions normally reserved for V8's own developers.
Finding it
The starting observation is that FastAssign calls the source's accessor before it calls CreateDataProperty on the target. That ordering gives attacker-controlled JavaScript a window in which it can run arbitrary code, including code that changes the map of the target object. In plain terms: while V8 is in the middle of copying, it pauses to run code that the page supplied, and that code can rearrange things underneath V8's feet. In particular, the accessor can cause the target's map to become deprecated. A deprecated map is an old description of a shape that V8 has since replaced with a newer one; objects still using the old description have to be brought up to date before they can be modified in the fast way.
The researcher's proof of concept builds two objects with the same initial shape and then deprecates both maps from inside the getter:
var x = {};
x.a0 = 1;
var x1 = {};
x1.a0 = 1;
//Creates a transition from map {a0} to {a0, prop}
x1.prop = 1;
x.__defineGetter__("prop", function() {
let obj = {};
obj.a0 = 1.5; //<------ map of x and x1 are now deprecated
return 1;
});
x.z = 1;
delete x.z;
//Cloning calls accessor `prop` of x before calling `CreateDataProperty` to create property `prop` on target
var y = {...x};
Line by line:
var x = {};creates an empty object namedx.x.a0 = 1;givesxa property calleda0holding the number 1. This fixesx's shape as "hasa0".var x1 = {};andx1.a0 = 1;create a second objectx1with the same shape.x1.prop = 1;adds a propertyproptox1. V8 records this as a transition: a rule saying "an object shaped like{a0}can become shaped like{a0, prop}". The comment in the code says exactly this.x.__defineGetter__("prop", function() { ... });definesproponxas a getter. From now on, whenever anything readsx.prop, the function runs instead of a stored value being returned.- Inside the getter,
let obj = {}; obj.a0 = 1.5;creates a fresh object and stores a decimal number (a double, in V8 terms) ina0. Becausexandx1were built with an integer ina0, this new object with a decimal in the same slot makes V8 mark the old shapes as deprecated. The comment marks this as the moment the maps ofxandx1become deprecated. return 1;makes the getter hand back the value 1.x.z = 1; delete x.z;adds a property and immediately removes it. This leavesxin a state where the clone proceeds through the fast path.var y = {...x};is the clone. V8 starts copyingxinto a new objecty. To get the value ofprop, it calls the getter, which deprecates the maps. Only after the getter returns does V8 try to createpropon the target.
x1.prop = 1 creates a transition from the map {a0} to the map {a0, prop}, so a transition for the name prop exists. The getter for prop on x creates a fresh object with a0 set to a double (1.5), which deprecates the maps of both x and x1. The x.z = 1; delete x.z; pair leaves x in a state where the clone proceeds through the fast path. When {...x} runs, the accessor prop is invoked before CreateDataProperty creates prop on the target.
Exploitation
Step 1: the fast path searches for a transition
CreateDataProperty uses TryFastAddDataProperty to add prop to the target:
bool TryFastAddDataProperty(Isolate* isolate, Handle<JSObject> object,
Handle<Name> name, Handle<Object> value,
PropertyAttributes attributes) {
Tagged<Map> map =
TransitionsAccessor(isolate, object->map())
.SearchTransition(*name, PropertyKind::kData, attributes);
if (map.is_null()) return false;
...
new_map = Map::PrepareForDataProperty(isolate, new_map, descriptor,
PropertyConstness::kConst, value);
...
object->WriteToField(descriptor,
new_map->instance_descriptors()->GetDetails(descriptor),
*value);
return true;
}
What this function does, in order:
- It receives the object to modify, the name of the property to add, the value, and the property's attributes (settings such as whether it can be changed).
TransitionsAccessor(isolate, object->map()).SearchTransition(*name, PropertyKind::kData, attributes)looks through the object's current shape for a recorded transition that leads to a shape containing the requested property as a normal data property. The result is stored inmap.if (map.is_null()) return false;means: if no such transition exists, give up on the fast path and report failure, so the caller falls back to the slow, general route.Map::PrepareForDataProperty(...)brings the found shape up to date and adjusts it for the value being stored. The result isnew_map.object->WriteToField(...)writes the value into the object using the descriptor information fromnew_map.return true;reports that the fast path succeeded.
The transition search for prop finds the map of x1, which is also deprecated because of the getter. That deprecated map is passed on to Map::PrepareForDataProperty.
Step 2: the deprecated map is updated
Map::PrepareForDataProperty tries to bring the map up to date:
Handle<Map> Map::PrepareForDataProperty(Isolate* isolate, Handle<Map> map,
InternalIndex descriptor,
PropertyConstness constness,
Handle<Object> value) {
// Update to the newest map before storing the property.
map = Update(isolate, map);
// Dictionaries can store any property value.
DCHECK(!map->is_dictionary_map());
return UpdateDescriptorForValue(isolate, map, descriptor, constness, value);
Line by line:
- The function takes the map to prepare, the position (descriptor) of the property, whether the value is constant, and the value itself.
map = Update(isolate, map);replaces the deprecated map with its newest version. The comment explains the intent: update to the newest map before storing the property.DCHECK(!map->is_dictionary_map());is an assertion: a check that is supposed to guarantee the updated map is not a dictionary map. A dictionary map means the object stores its properties in aNameDictionary, a lookup structure suited to objects with many or frequently changing properties, rather than in the compactPropertyArrayused for simple shapes.DCHECKis a debug-only check: it is compiled into debug builds and removed from release builds.return UpdateDescriptorForValue(...)adjusts the descriptor for the value and returns the resulting map.
Updating a deprecated map can cause it to become a dictionary map. The researcher points to bug 40062884 as the place where this behaviour was already noted. The DCHECK that the map is not a dictionary map only fires in debug builds, so in release builds execution continues. In other words, the safety net exists only for developers testing V8, not for the Chrome that people actually use.
Step 3: the write assumes a fast map
Back in TryFastAddDataProperty, the call to WriteToField still assumes that new_map is a fast map:
void JSObject::WriteToField(InternalIndex descriptor, PropertyDetails details,
Tagged<Object> value) {
...
FieldIndex index = FieldIndex::ForDetails(map(), details);
if (details.representation().IsDouble()) {
...
} else {
FastPropertyAtPut(index, value);
}
}
Line by line:
- The function receives the property's position, its details, and the value to store.
FieldIndex index = FieldIndex::ForDetails(map(), details);works out where in the object's storage the value should go, based on the object's current map and the property details.if (details.representation().IsDouble())checks whether the property is stored as a double (a decimal number). If so, a different branch handles it.else { FastPropertyAtPut(index, value); }is the branch for everything else: it writes the value at the computed position usingFastPropertyAtPut, a routine that assumes the properties live in aPropertyArray.
Because new_map has become a dictionary map, the property storage is a NameDictionary rather than a PropertyArray, but FastPropertyAtPut writes as if it were the latter. This confuses the two layouts and can overwrite internal properties of the NameDictionary, which can then be used to cause out-of-bounds access from the NameDictionary. A useful comparison: it is like a form that says "write your answer in box 3 of the short form", but the form in front of you has been swapped for a long, differently laid out one, so box 3 lands on top of something else entirely.
Step 4: crash and leak
The two attached test cases exercise the two outcomes. crash.js crashes with an out-of-bounds write. leak_hole.js uses the out-of-bounds access to leak the hole object, and with --allow-natives-syntax it prints the leaked hole object.
Root cause
The mistake is a stale assumption in the fast path. TryFastAddDataProperty searches for a transition map and hands it to Map::PrepareForDataProperty, which may update a deprecated map into a dictionary map. The subsequent WriteToField does not re-check the kind of map it received and uses FastPropertyAtPut, which is only valid for fast properties backed by a PropertyArray. When the map is a dictionary map, the same offset addresses fields of a NameDictionary, so the write corrupts dictionary internals. The only guard shown in the code is a DCHECK, which is compiled out of release builds, so it does not stop the confusion in shipping Chrome.
Impact
The report describes an out-of-bounds write and an out-of-bounds access reachable from JavaScript through object cloning with an accessor. The write can overwrite internal properties of a NameDictionary, and the read can leak the hole object. The researcher does not spell out a full exploit chain beyond these primitives. A primitive, in this context, is a small, reliable capability such as "write a value out of bounds" that a later stage of an attack could build on. The affected range is stable Chrome from 125.0.6422.60 onwards.
Fix
The report does not describe the patch. The report does not describe the patch.
Variants to test
- An inline cache is a small piece of remembered information that lets repeated property accesses skip the slow lookup. A positive result is a write that lands in a
NameDictionaryafter the map was deprecated during the operation. - A positive result is a crash or a leaked
holeobject underd8. - A positive result is a corrupted dictionary or an out-of-bounds access.
Glossary
- Accessor: a property whose value is produced by a function when it is read or written, rather than stored directly.
- Chrome VRP: Chrome's Vulnerability Reward Program, which pays researchers for reporting security bugs.
- Commit: a saved snapshot of a source code repository.
- CVE: a public identifier assigned to a known security vulnerability.
- d8: a command-line shell that runs JavaScript using V8 alone, used for testing the engine.
- Deprecated map: an old description of an object's shape that V8 has replaced with a newer one and must update before fast modification.
- Descriptor: V8's record of a single property's position and details within an object's shape.
- Dictionary map: a shape in which properties are stored in a
NameDictionarylookup structure rather than a compact array. - DCHECK: a check that only exists in debug builds and is removed from release builds.
- Double: a decimal number stored in a floating-point format.
- Fast path: a shortcut in code that handles the common case quickly, with a slower general route as fallback.
- Getter: an accessor that runs when a property is read.
- hole: a special internal V8 marker meaning "no value here".
- Inline cache: remembered information that lets repeated property accesses skip a slow lookup.
- Map: V8's internal description of an object's shape: its properties, their order and how values are stored.
- NameDictionary: a lookup structure V8 uses to store properties, suited to objects with many or changing properties.
- Object spread: the
{...x}syntax that copies the properties of one object into a new one. - Out-of-bounds: accessing memory outside the area that was intended to be used.
- Primitive: a small, reliable capability, such as an out-of-bounds write, that a later stage of an attack could build on.
- PropertyArray: a compact array V8 uses to store properties for simple object shapes.
- Transition: a recorded rule saying that an object of one shape can become an object of another shape when a property is added.
- Type confusion: a bug where a program treats one kind of data structure as if it were another kind.
- V8: the JavaScript engine inside Chrome that reads and executes the JavaScript on web pages.